Re: Coding style when using atomic_dec_and_test

"Valdis Klētnieks" <[email protected]>
Newsgroups org.kernelnewbies.kernelnewbies
Message-ID <33205.1711864374@turing-police>
On Sat, 30 Mar 2024 21:52:29 -0300, Camila Alvarez Inostroza said:

> I've seen two ways of handling the resulting operation, below are a couple
> of examples:
> (1) if (!atomic_dec_and_test(&rd->refcount))
>            return;
>       call_rcu(&rd->rcu, free_rootdomain);
>
> (2)  if (atomic64_dec_and_test(&map->refcnt)) {
>            /* bpf_map_free_id() must be called first */
>            ...
>         }
>
> Is it prefered to use one option over the other? Or is it just personal
> preference?

Yes. :)

Basically, it will depend on what the code logic structure is like.  If the logical
way to do it is "if it fails, return now", then you do (1).  If the logical structure
is "if it succeeds, do this before continuing", you do (2).  In other cases,
you may want to do the "structured goto" the kernel uses to unwind allocations
and locks that happened before the failure.

The above is not an exhaustive list.

_______________________________________________
Kernelnewbies mailing list
[email protected]
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
signature.asc (application/pgp-signature, 494 B)
-----BEGIN PGP SIGNATURE-----
Comment: Exmh version 2.9.0 11/07/2018

iQEcBAEBCAAGBQJmCPo2AAoJEI0DS38y7CIclwAIAI56+V+ygMv7bo5utvjNEfUK
IrX/+HVZ9rMw+vyrfx+woAOi6UQa36P5/aahOasAkDyrCcvvQs+tQ+VBPmEiHwvY
sIQzPcTT10cbNHsw65k0Z+JaBveKWcSG3cbl7yGOanqHtXIZr9xin9MPaTLxEMnd
rOQP4XYMOBKMA3Z8AyOSVPXzl2XHsoDroOjB8aDlwFDV8ir2AZt3aIfKbdUdqolH
LWHueMLroBouaNvbpvKCE12nSK1L0Z7z0Jr2giETgL+7VqkOzjkYLRog2mGu5AVD
oq8To/+Q34E26fCRoFGUUGhFfiE55RnLHm5dqFG9ZTMp+xsmpzFnJmg9k1lgC98=
=Brig
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.