From: I Hsin Cheng <richard120310@gmail.com>
To: Theodore Ts'o <tytso@mit.edu>
Cc: adilger.kernel@dilger.ca, linux-ext4@vger.kernel.org,
linux-kernel@vger.kernel.org, skhan@linuxfoundation.org,
linux-kernel-mentees@lists.linuxfoundation.org
Subject: Re: [PATCH] ext4: Refactor breaking condition for xattr_find_entry()
Date: Tue, 8 Jul 2025 11:01:31 +0800 [thread overview]
Message-ID: <aGyKC3ig4N74PPcX@vaxr-BM6660-BM6360> (raw)
In-Reply-To: <20250708022453.GB1845251@mit.edu>
On Mon, Jul 07, 2025 at 10:24:53PM -0400, Theodore Ts'o wrote:
> On Tue, Jul 08, 2025 at 10:00:13AM +0800, I Hsin Cheng wrote:
> > diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
> > index 8d15acbacc20..1993622e3c74 100644
> > --- a/fs/ext4/xattr.c
> > +++ b/fs/ext4/xattr.c
> > @@ -338,7 +338,7 @@ xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
> > cmp = name_len - entry->e_name_len;
> > if (!cmp)
> > cmp = memcmp(name, entry->e_name, name_len);
> > - if (cmp <= 0 && (sorted || cmp == 0))
> > + if (!cmp || (cmp < 0 && sorted))
>
> This is *not* identical. Suppose memcmp returns a positive value
> (say, 1). Previously, the conditional would be false. With your
> change, !cmp would be true, so the overall conditional would be true.
>
> So this does not appear to be a valid transformation.
>
> (Note that valid transformations will be done by the compiler
> automatically, without needing to make code changes.)
>
> - Ted
Hi Ted,
> This is *not* identical. Suppose memcmp returns a positive value
> (say, 1). Previously, the conditional would be false. With your
> change, !cmp would be true, so the overall conditional would be true.
I would argue that "!cmp" is only true when "cmp" is zero, otherwise
it'll be false no matter the number is positive or negative.
With some transformation according to Demorgan's Law, the following
expressions are equivalent
* "cmp <= 0 && (sorted || cmp == 0)"
* "(cmp <= 0 && sorted) || (cmp <= 0 && cmp == 0)"
* "(cmp <= 0 && sorted) || (cmp == 0)"
* "(cmp == 0) || (cmp <= 0 && sorted)"
Because when "cmp == 0" (which is "!cmp"), the condition is going to
take shortcut, so we can further simplify "(cmp <= 0 && sorted)" to
"(cmp < 0 && sorted)", since "cmp" isn't going to be 0 when entering
this part.
When you put any non-zero value for "cmp", "!cmp" is going to be false
so it will further check whether "(cmp < 0 && sorted)".
This is my derivation flow, let me know if there's anything wrong in it.
> (Note that valid transformations will be done by the compiler
> automatically, without needing to make code changes.)
Makes sense, thanks for the head up, but I think we do have some
benefits from it when compiling with -O2 optimization level?
As the bloat-o-meter indicates the code generation size can actually be
shrinked.
Best regards,
I Hsin Cheng
next prev parent reply other threads:[~2025-07-08 3:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-08 2:00 I Hsin Cheng
2025-07-08 2:24 ` Theodore Ts'o
2025-07-08 3:01 ` I Hsin Cheng [this message]
2025-07-19 21:45 ` Theodore Ts'o
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=aGyKC3ig4N74PPcX@vaxr-BM6660-BM6360 \
--to=richard120310@gmail.com \
--cc=adilger.kernel@dilger.ca \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=skhan@linuxfoundation.org \
--cc=tytso@mit.edu \
/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
all inboxes | Powered by JetHome®