Kernel Newbies archive mirror
 help / color / mirror / Atom feed
From: "Valdis Klētnieks" <valdis.kletnieks@vt.edu>
To: Camila Alvarez Inostroza <cam.alvarez.i@gmail.com>
Cc: kernelnewbies@kernelnewbies.org
Subject: Re: Coding style when using atomic_dec_and_test
Date: Sun, 31 Mar 2024 01:52:54 -0400	[thread overview]
Message-ID: <33205.1711864374@turing-police> (raw)
In-Reply-To: <6b587fa4-4069-419f-bae7-b53aeb86a955@macbook-pro-de-camila.local>


[-- 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

      reply	other threads:[~2024-03-31  5:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=33205.1711864374@turing-police \
    --to=valdis.kletnieks@vt.edu \
    --cc=cam.alvarez.i@gmail.com \
    --cc=kernelnewbies@kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).