kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* Coding style when using atomic_dec_and_test
@ 2024-03-31  0:52 Camila Alvarez Inostroza
  2024-03-31  5:52 ` Valdis Klētnieks
  0 siblings, 1 reply; 2+ messages in thread
From: Camila Alvarez Inostroza @ 2024-03-31  0:52 UTC (permalink / raw)
  To: kernelnewbies

I understand that atomic_dec_and_test returns 1 when the result of the
operation is 0, and 0 otherwise.
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?

Thanks in advance!
Camila Alvarez

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Coding style when using atomic_dec_and_test
  2024-03-31  0:52 Coding style when using atomic_dec_and_test Camila Alvarez Inostroza
@ 2024-03-31  5:52 ` Valdis Klētnieks
  0 siblings, 0 replies; 2+ messages in thread
From: Valdis Klētnieks @ 2024-03-31  5:52 UTC (permalink / raw)
  To: Camila Alvarez Inostroza; +Cc: kernelnewbies


[-- Attachment #1.1: Type: text/plain, Size: 920 bytes --]

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.

[-- Attachment #1.2: Type: application/pgp-signature, Size: 494 bytes --]

[-- Attachment #2: Type: text/plain, Size: 170 bytes --]

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-31  5:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-31  0:52 Coding style when using atomic_dec_and_test Camila Alvarez Inostroza
2024-03-31  5:52 ` Valdis Klētnieks

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).