From: Sun YangKai <sunk67188@gmail.com>
To: dsterba@suse.cz
Cc: linux-btrfs@vger.kernel.org, sunk67188@gmail.com
Subject: Re: [PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early
Date: Fri, 22 Aug 2025 20:58:52 +0800 [thread overview]
Message-ID: <2255718.Icojqenx9y@saltykitkat> (raw)
In-Reply-To: <20250822125027.GU22430@twin.jikos.cz>
> On Thu, Aug 21, 2025 at 09:12:23PM +0800, Sun YangKai wrote:
> > Move the NULL pointer check for the @info parameter to the beginning
> > of get_inode_info() function to avoid unnecessary operations when no
> > output is required.
> >
> > This change provides two benefits:
> > 1. Avoids allocating path and performing tree lookups when @info is NULL
> > 2. Simplifies the control flow by eliminating the redundant check
> >
> > before writing output
> >
> > The functional behavior remains unchanged except for the performance
> > improvement when called with NULL info parameter.
>
> Reordering conditional usually leads to behaviour changes and I think it
> does in this case as well.
>
> > Signed-off-by: Sun YangKai <sunk67188@gmail.com>
> > ---
> >
> > fs/btrfs/send.c | 6 +++---
> > 1 file changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
> > index 7664025a5af4..5010d17878f9 100644
> > --- a/fs/btrfs/send.c
> > +++ b/fs/btrfs/send.c
> > @@ -913,6 +913,9 @@ static int get_inode_info(struct btrfs_root *root, u64
> > ino,>
> > struct btrfs_inode_item *ii;
> > struct btrfs_key key;
> >
> > + if (!info)
> > + return 0;
>
> Moving the check as the first statement will skip anything else. There's
> one call of get_inode_info() with info == NULL, in apply_dir_move(),
> otherwise all callers fill in a stack variable.
>
> > +
> >
> > path = alloc_path_for_send();
> > if (!path)
> >
> > return -ENOMEM;
> >
> > @@ -927,9 +930,6 @@ static int get_inode_info(struct btrfs_root *root, u64
> > ino,>
> > goto out;
> >
> > }
>
> Right before this there's call to btrfs_search_slot() looking up
> INODE_ITEM for 'ino', there are 3 results, and two are still valid for
> the callers. Only if ret == 0 then the 'if (!info)' check is done.
> Otherwise it's either an error (ret < 0) or -ENOENT (when ret == 1).
>
> So get_inode_info() is also has the semantics of that the inode exists,
> with the reordered 'info' check it never happens.
I got it. Sorry. My bad. I'll fix it in patch v3.
>
> > - if (!info)
> > - goto out;
> > -
> >
> > ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
> >
> > struct btrfs_inode_item);
> >
> > info->size = btrfs_inode_size(path->nodes[0], ii);
Thanks,
Sun YangKai
next prev parent reply other threads:[~2025-08-22 12:59 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-21 13:12 [PATCH v2 0/2] btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions Sun YangKai
2025-08-21 13:12 ` [PATCH 1/2] btrfs: get_inode_info(): check NULL info parameter early Sun YangKai
2025-08-22 12:50 ` David Sterba
2025-08-22 12:58 ` Sun YangKai [this message]
2025-08-30 7:00 ` kernel test robot
2025-08-21 13:12 ` [PATCH 2/2] btrfs: more trivial BTRFS_PATH_AUTO_FREE conversions Sun YangKai
2025-08-22 13:01 ` David Sterba
2025-08-22 13:08 ` Sun YangKai
2025-08-22 15:51 ` Sun YangKai
2025-08-26 16:56 ` David Sterba
2025-08-27 12:24 ` Sun YangKai
2025-08-29 0:02 ` David Sterba
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=2255718.Icojqenx9y@saltykitkat \
--to=sunk67188@gmail.com \
--cc=dsterba@suse.cz \
--cc=linux-btrfs@vger.kernel.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