From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796405266; b=QsGQeHDZrkQhoEjPF26ml5X2sg2CpCv7gHW8m+dUA/tTm0Ckw0mNFXl8fkMchJGrco6I 2vbyuB7sKHEr2kQuyqNsFmk+ZaMmba+GbMu6f5XbTSrAXCLIYl8YQOhTX/IPpqhJNE206 po6+09rBq2VhN7RyzbZThv6BVWYnS3BI0vP2qSCvYAiai1lPHlw2nznQ7U0gLaOyx+5fk S1PXwNsAMQxuFCXnkGiF+zS4/tlTs4SsZfyeVE1pxl8g42WeN1sEhrZm6MTqhnIeJ5/xX UijPk6/Gbbn/soQP/zarJWlqp0gaNSymcYMA0GlolqmJJeZnuR9j+TmwZQCYUkHrV8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796405266; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To:Organization; bh=trrxvqlPUMfnF3DClJLimpJnKUYmpgOodGF2mmjZV8U=; b=N2pU5WczLu8X4Qz4jWNeZA2oW2L1REjTodZowKdmuQ+JtCIgbkCdVWGnRXQNOGt5R7ER SwkiynexOC5/f2nek7chNQSNWb1y3jXoI58u4eXtLn9bGAaCLd03TcCg/8lLEvEPxv3JZ hTHRQFoIk2Babt84bPjZpHRAJd+daQIhSSuOgFxhd45ILKPnBmsKbze3/6jkx0+IOwn4q rXIvVyWcxXEdnNtxJ1mf2wcXue7CKxlWcXdrsDWRKqPO98jOPUWxjwuyQVtuah7s8a/zp 9ngbR/PMLaFk7wo1vYqxVWCS6VCtR3Gl/LUAZzUgckQBbRzGZNnICnMbetHgU08edgQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Zxjzlaeq; arc=none smtp.remote-ip=192.198.163.19 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEBBA606A687 for ; Fri, 4 Dec 2026 17:27:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EEBBA606A687 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Zxjzlaeq DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796405265; x=1827941265; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=trrxvqlPUMfnF3DClJLimpJnKUYmpgOodGF2mmjZV8U=; b=ZxjzlaeqRNSDDrTOamaVd96/9mBVjjn5nLMeMbtWdTIvNxoQgv1YhUuE zeIpc03zwSieHlNfe6GYfVZ4SV8woS2+FJJh4D6Iw+2yuKAsUUfswNqDi yTTHpsFWssZkAkaZhaWA3PQdb3vpN9NA09Y9f9BMEldxl15Lkfq6aLn6S NZV8MRv5YnqMMM6kC/3bqfArIMxePOwknhsK7PYdKvn31r7VOYZOMEZOI zfeqV9m+cRKqDug1L3Nkwh+XlvtlBOTzRPS9Yxo/wRezW/QJIqefCDPCC b4cxZ6sz3frcxcRvnwj2RpWkrqxcW0K3vj951yFLd/f/xRftewbrwyo+O g==; X-CSE-ConnectionGUID: XlmOI6inRTKNuUentV4nDQ== X-CSE-MsgGUID: sZUZbVnDSQO8pHMpw68eGQ== X-IronPort-AV: E=McAfee;i="6800,10657,11986"; a="5411787" X-IronPort-AV: E=Sophos;i="6.29,78,1794902400"; d="scan'208";a="5411787" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 09:27:44 -0800 X-CSE-ConnectionGUID: aooTIT4HTFCFFJeRJWdVIQ== X-CSE-MsgGUID: RLVS7fJ2QMOWWijTwYSAeg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,78,1794902400"; d="scan'208";a="14250571" Received: from alekseim-mobl.ger.corp.intel.com (HELO localhost) ([10.245.245.10]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 09:27:43 -0800 Date: Fri, 4 Dec 2026 19:27:40 +0200 From: Andy Shevchenko To: Arnav Kapoor Cc: Yury Norov , Rasmus Villemoes , Jonathan Cameron , linux-kernel@vger.kernel.org Subject: Re: [PATCH 0/2] bitfield: fix FIELD_FIT() for values that overflow the shift Message-ID: References: <20261204163317.924658-1-kapoorarnav43@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261204163317.924658-1-kapoorarnav43@gmail.com> Organization: Intel Finland Oy - BIC 0357606-4 - c/o Alberga Business Park, 6 krs, Bertel Jungin Aukio 5, 02600 Espoo 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 -- With Best Regards, Andy Shevchenko