mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Viacheslav Dubeyko <Slava.Dubeyko@ibm.com>
To: "lyican53@gmail.com" <lyican53@gmail.com>
Cc: "ceph-devel@vger.kernel.org" <ceph-devel@vger.kernel.org>,
	"idryomov@gmail.com" <idryomov@gmail.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Xiubo Li <xiubli@redhat.com>
Subject: RE: [PATCH] ceph: Fix potential undefined behavior in crush_ln() with GCC 11.1.0
Date: Mon, 22 Sep 2025 17:19:37 +0000	[thread overview]
Message-ID: <2eddc77bea32f3baa47cfb1bafb4e20edfe00417.camel@ibm.com> (raw)
In-Reply-To: <A246BD33-C009-4C12-94E7-E95CABB94D04@gmail.com>

On Sat, 2025-09-20 at 20:06 +0800, 陈华昭(Lyican) wrote:
> > 2025年9月20日 02:51,Viacheslav Dubeyko <Slava.Dubeyko@ibm.com> 写道:
> > 
> > On Fri, 2025-09-19 at 10:34 +0800, 陈华昭(Lyican) wrote:
> > > > 2025年9月19日 02:07,Viacheslav Dubeyko <Slava.Dubeyko@ibm.com> 写道:
> > > > 
> > 
> > <skipped>
> > I still have the same issue with the new patch. Your patch is trying to modify
> > the line 262. However, we have comments on this line [1]:
> > 
> > 260 /*
> > 261 * figure out number of bits we need to shift and
> > 262 * do it in one step instead of iteratively
> > 263 */
> > 264 if (!(x & 0x18000)) {
> > 265 int bits = __builtin_clz(x & 0x1FFFF) - 16;
> > 266 x <<= bits;
> > 267 iexpon = 15 - bits;
> > 268 }
> > 
> > Thanks,
> > Slava.
> > 
> > [1]
> > https://elixir.bootlin.com/linux/v6.17-rc6/source/net/ceph/crush/mapper.c#L262  
> Hi Slava,
> 
> Thank you for your patience with this patch. I want to clarify the confusion about the line numbering.
> 
> The patch header "@@ -262,7 +262,7 @@" was automatically generated by git format-patch - I did not manually specify line 262. This is how git diff format works: it shows context lines starting from line 262, but the actual code modification is on line 265 where the `__builtin_clz()` call is located (exactly as you referenced in [1]).
> 
> To be absolutely clear:
> - I am NOT trying to modify line 262 (which contains comments)
> - I AM modifying line 265: `int bits = __builtin_clz(x & 0x1FFFF) - 16;`
> - The "@@ -262,7 +262,7 @@" header is git's standard way of providing context
> - Git automatically chooses how many context lines to show and where to start them
> 
> The patch content clearly shows the actual change:
> ```diff
> - int bits = __builtin_clz(x & 0x1FFFF) - 16;
> + int bits = (x & 0x1FFFF) ? __builtin_clz(x & 0x1FFFF) - 16 : 16;
> ```
> 
> This line-by-line diff shows exactly what gets modified - line 265 in the official kernel source.
> 
> Here is the git-generated patch:
> 
> ---
> 
> From ac3a55a6a18761d613971ef6f78fa39e6d7d2172 Mon Sep 17 00:00:00 2001
> From: Huazhao Chen <lyican53@gmail.com>
> Date: Sat, 20 Sep 2025 19:42:54 +0800
> Subject: [PATCH] ceph: Fix potential undefined behavior in crush_ln() with GCC
>  11.1.0
> 
> When x & 0x1FFFF equals zero, __builtin_clz() is called with a zero
> argument, which results in undefined behavior. This can happen during
> ceph's consistent hashing calculations and may lead to incorrect
> placement group mappings.
> 
> Fix by checking if the masked value is non-zero before calling
> __builtin_clz(). If the masked value is zero, use the expected result
> of 16 directly.
> 
> Signed-off-by: Huazhao Chen <lyican53@gmail.com>
> ---
>  net/ceph/crush/mapper.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/net/ceph/crush/mapper.c b/net/ceph/crush/mapper.c
> index 3a5bd1cd1..000f7a633 100644
> --- a/net/ceph/crush/mapper.c
> +++ b/net/ceph/crush/mapper.c
> @@ -262,7 +262,7 @@ static __u64 crush_ln(unsigned int xin)
>   * do it in one step instead of iteratively
>   */
>   if (!(x & 0x18000)) {
> - int bits = __builtin_clz(x & 0x1FFFF) - 16;
> + int bits = (x & 0x1FFFF) ? __builtin_clz(x & 0x1FFFF) - 16 : 16;
>   x <<= bits;
>   iexpon = 15 - bits;
>   }

I completely lost myself in the multiple versions of the patch. Could you please
send one formal patch only that I can successfully apply?

Thanks,
Slava.

  reply	other threads:[~2025-09-22 17:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-18  1:50 陈华昭(Lyican)
2025-09-18  2:09 ` Viacheslav Dubeyko
2025-09-18 18:07 ` Viacheslav Dubeyko
2025-09-19  2:34   ` 陈华昭(Lyican)
2025-09-19 18:51     ` Viacheslav Dubeyko
2025-09-20 12:06       ` 陈华昭(Lyican)
2025-09-22 17:19         ` Viacheslav Dubeyko [this message]
2025-09-24  1:27           ` 陈华昭(Lyican)
2025-09-26 23:42 陈华昭

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=2eddc77bea32f3baa47cfb1bafb4e20edfe00417.camel@ibm.com \
    --to=slava.dubeyko@ibm.com \
    --cc=ceph-devel@vger.kernel.org \
    --cc=idryomov@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyican53@gmail.com \
    --cc=xiubli@redhat.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

all inboxes | Powered by JetHome®