From: David Sterba <dsterba@suse.cz>
To: Leo Martins <loemra.dev@gmail.com>
Cc: linux-btrfs@vger.kernel.org, kernel-team@fb.com
Subject: Re: [PATCH 0/2] btrfs: add __free attribute and improve xattr cleanup
Date: Thu, 15 Aug 2024 20:46:17 +0200 [thread overview]
Message-ID: <20240815184617.GF25962@suse.cz> (raw)
In-Reply-To: <i9tc0.0f867os9viy6@gmail.com>
On Thu, Aug 15, 2024 at 10:38:24AM -0700, Leo Martins wrote:
> On Tue, 13 Aug 2024 14:29, David Sterba <dsterba@suse.cz> wrote:
> >On Fri, Aug 09, 2024 at 04:11:47PM -0700, Leo Martins wrote:
> This makes sense, I will drop the xattr patch. Do you think there would
> be any benefit in using the __free pattern in situations where it
> is clear that btrfs_free_path is the last thing called before returning?
> For example:
>
>
> int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
> struct btrfs_root *root, u64 offset)
> {
> struct btrfs_path *path;
> struct btrfs_key key;
> int ret = 0;
>
> key.objectid = BTRFS_ORPHAN_OBJECTID;
> key.type = BTRFS_ORPHAN_ITEM_KEY;
> key.offset = offset;
>
> path = btrfs_alloc_path();
> if (!path)
> return -ENOMEM;
>
> ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
> if (ret < 0)
> goto out;
> if (ret) { /* JDM: Really? */
> ret = -ENOENT;
> goto out;
> }
>
> ret = btrfs_del_item(trans, root, path);
>
> out:
> btrfs_free_path(path);
> return ret;
> }
>
>
> In this code the behavior would be the same except it would eliminate
> the need for goto out as the path is freed automatically on exit.
Yes, this is where I coudl be used, basically it's a pattern where the
lock/allocation is done early at the beginning of a function (after some
initial checks) and then right before a return and all exit paths lead
there.
For that I'd suggest to make it clear that the function uses the
automatic unlock/deletion so the declaration of the variable would be
done like
BTRFS_PATH_AUTOCLEAN(name);
that declares it with the proper __free callback and initializes it to
NULL.
There's another thing that's a common pattern in btrfs and other kernel,
code, the single exit block. The __free callback allows to do a return
anywhere which is the opposite of that. As this is new we should look up
good examples that will be the patterns to follow or exceptions to
avoidd so we can declare it current best practice and recommended coding
style.
prev parent reply other threads:[~2024-08-15 18:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-09 23:11 [PATCH 0/2] btrfs: add __free attribute and improve xattr cleanup Leo Martins
2024-08-09 23:11 ` [PATCH 1/2] btrfs: use __free in linux/cleanup.h to reduce btrfs_free_path boilerplate Leo Martins
2024-08-09 23:11 ` [PATCH 2/2] btrfs: use __free to automatically free btrfs_path on exit Leo Martins
2024-08-13 21:34 ` David Sterba
2024-08-13 21:29 ` [PATCH 0/2] btrfs: add __free attribute and improve xattr cleanup David Sterba
2024-08-15 17:38 ` Leo Martins
2024-08-15 18:46 ` David Sterba [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=20240815184617.GF25962@suse.cz \
--to=dsterba@suse.cz \
--cc=kernel-team@fb.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=loemra.dev@gmail.com \
/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