mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: Arnav Kapoor <kapoorarnav43@gmail.com>,
	Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift
Date: Fri, 4 Dec 2026 19:53:07 +0000	[thread overview]
Message-ID: <20261204195307.6ba97beb@jic23-hlaptop> (raw)
In-Reply-To: <axL4DC99LZIm1t83@ashevche-desk.local>

On Fri, 4 Dec 2026 19:27:40 +0200
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:

> On Fri, Dec 04, 2026 at 10:03:10PM +0530, Arnav Kapoor wrote:
> > FIELD_FIT() shifts the value up into the field before checking it, so
> > high bits that fall off the top of the mask's type are lost and a value
> > that is too big passes. On 32-bit, FIELD_FIT(GENMASK(9, 8), BIT(24)) and
> > FIELD_FIT(GENMASK(9, 8), INT_MIN) are true; on 64-bit the same happens
> > with a u32 mask. This came up in the aw96103 v2 review on linux-iio,
> > where Jonathan suggested shifting the mask down instead and asked
> > whether the compiler still produces good code.
> > 
> > 1: shift the mask down and compare, as FIELD_MAX() does.
> > 2: kunit cases that fail before patch 1 (on um and i386) and pass after.
> > 
> > Code generation, FIELD_FIT(GENMASK(9, 8), int):
> > - gcc -m32 -O2: cmpl $3 + setbe (was a test against 0x00fffffc, which
> >   is the bug)
> > - clang ARMv7 -O2: cmp #4 + movwlo
> > - gcc x86-64 -O2: one instruction shorter than before
> > 
> > Not changed: the value is still cast to the mask's type first, so with
> > a u8 or u16 mask a wider value is still truncated (FIELD_FIT((u8)0xf0,
> > 0x100) is true). Casting to u64 instead fixes that as well, but gcc -m32
> > then emits 12 instructions instead of 4 for an int value, so I left it
> > out; happy to add it if you prefer.
> > 
> > Tested with kunit (bitfields suite) on um and i386, and W=1 builds of
> > every non-arch FIELD_FIT() user (allmodconfig, x86_64) with no new
> > warnings.  
> 
> I like the series and I was attending the discussion in the mailing list when
> this bug was discovered.
> 
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> 

Looks good to me as well. 

Reviewed-by: Jonathan Cameron <jonathan.cameron@oss.qualcomm.com>




  reply	other threads:[~2026-12-04 19:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:33 Arnav Kapoor
2026-12-04 16:33 ` [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value Arnav Kapoor
2026-12-04 16:33 ` [PATCH 2/2] bitfield: kunit: test FIELD_FIT() with values that overflow the shift Arnav Kapoor
2026-12-04 17:26   ` Andy Shevchenko
2026-12-05 19:43     ` Yury Norov
2026-12-06 11:09       ` Andy Shevchenko
2026-12-04 17:27 ` [PATCH 0/2] bitfield: fix FIELD_FIT() for " Andy Shevchenko
2026-12-04 19:53   ` Jonathan Cameron [this message]
2026-12-05  4:52 ` Yury Norov

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=20261204195307.6ba97beb@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=kapoorarnav43@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=yury.norov@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

all inboxes | Powered by JetHome®