From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796402010; b=lq3vfX7H6z+AW9qalp/15bsZPRY28yfdYLenDTH/aWd9LGR5ox+LROFBu2tHw9Kt16dS I6Wy7HQ+mok/MoNroit5CTTXxHrqSm3NdJv2YdV59R0Qm6LDLCBswj99FzxshDKc/TkDB m3hsG4VL0yb5NWhEbmNPjF2Sgg6tsWDpO825h77cHXDIv+OC+2RXSNJFDw9Kyo3/++amf wXXpA1nZ0g6r3uUMmjJCRs+p7PcS7odETPp9Cqcjhx4nCtIr6xasZ0RE0iVj2UIQ8XeLb jkZcI+VUpEvmIgWjU8AQfLcQJtFYEe2dldYfAgW3LBSJFfAx47prRbIfg5kOH3RVH5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796402010; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=esrQao4LOaC2lzu91bPKT4eiQ9U/Hbm+9AC0YlUfKvA=; b=kEKYFuyGe0E4ptJiw6eUYNO3A1D0hNIb3WAbTt1kKggdXPUHJk18jLKioBhNlLgwrw3b rpAeECNud98jY5gBapsj2Jkv7BiKr9vc/iTdT533LUVKypmpb5IBrd0YaBIfbzmBoJMWz UYMvQj1jDN38Nju/J9iUsYw7RvIvLhgbBeplsqIpqlH4hIOfuKKDfwZlk7EaARgHggUgl soWZHVWugcVI/QpKsdsbo7KETVPxEtEeqnln1GKPOA+ESBORfilWsJafXdKXF/b6J905F jOUcYLX5aFaLyHw4BMaHagg/XrdV8ovI4pvijSJLt5R9XbeIMhEbePyTgP2pIofCTlQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=tFvj+ME0; arc=none smtp.remote-ip=74.125.82.51 Received: from mail-dl1-f51.google.com (mail-dl1-f51.google.com [74.125.82.51]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F0F56606A330 for ; Fri, 4 Dec 2026 16:33:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F0F56606A330 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=tFvj+ME0 Received: by mail-dl1-f51.google.com with SMTP id a92af1059eb24-1b5d5f9938cso1348703c88.1 for ; Fri, 04 Dec 2026 08:33:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796402009; x=1797006809; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=esrQao4LOaC2lzu91bPKT4eiQ9U/Hbm+9AC0YlUfKvA=; b=tFvj+ME08TIagh2l3FShn2z3tnww8nkRrD0Xq0Gd6Tin3Q0bt8BOvkQnKsy2iaRVZR M36EjM10v5QmsFz/x0ghUzXRhMM6rIKIUxVsMDD/tZBnHR6kiiX0oVq/frZzOe5aJteW U86mHKqlGy1xnUWdzDTdAhzjmV+PL2rR4fXevJAJtXhi8p1DTaRO94i8ojuybHlawBU9 8tDw5Mw/BUXLR3zA8s29SEMGQfTuDoYbX6Ui/aWbUTmxNOp9aWnQ8g/oQdskz1JWRrf4 s3oGgs5S8aKSYQ948AMgbETX0IWjw9mtP1QQiAqZLVvBwY3LpOD3RqRCMMAV+pzl/0YO 2K5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796402009; x=1797006809; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=esrQao4LOaC2lzu91bPKT4eiQ9U/Hbm+9AC0YlUfKvA=; b=bsYSlLhH3qUZOFUcfsb2Gm/6adiJuX1IC/+BdshmlLzif2WKf33esnDa9s4qhc8fIV L2FAXIRj+YidvPTSH0TurPUfxXvgCZSFbtPLtTvXATGNTgCgNQaE4rN8Wrll048OHypL JIUM1dU4sZjQe+ZqB+X/i7RTFCdgjxMfDvwldfxyZ7xnX0+bJJiDXrFSg8MmHCscfK1D X7sDBba7dz4JCPxm0RMs7t4XL5B2waq1GK0gCB9hzpVqZU/kFGGDc821vGwXYNFMkWkc LRn2ITSZjojP8pH2K760a8sxQKXMvbKnm9On7frgs4Wufi5WlWuXQEpmrU+PwXwRYBQi WyQw== X-Forwarded-Encrypted: i=1; AL+hErGl0lLYbvxc9rFGrwUqoSjzi7oPQp2s2vVf4FAVFrZ+tfGAOtFF/hOQQ7FYVWWEVJZP10KIogyFT+BRUb4=@vger.kernel.org X-Gm-Message-State: APuU4Z+hFEUFGIKpnIszmBaGkcjjpXPTmb3gTQitZkIWSPCdOFwCFIXR K4ldkG6IaOUf5vDtmYVL148PR9HaoYVQGCPrSHO/A8pWE9tMwwRn7J66 X-Gm-Gg: AZFdTZfYZez57dPZUF3sUPWJDH27tX11NPWANesIsN8hzS+SlVQYNk/YMqseUEkXSkm lhS1D7JYmKyvax5vvT/7pQeOJXImfXnsKi+o/c36KqZne2lLrAVuDJud8UNhEbXiCuKkkpR6Re4 O3DVHNQji+BDi48YOR9V5fcIfZHs3LXHdnUS8vznXvo4HZJuSmssejsd+RdC4H4J0LeRjs7NLFs stMmhp92JN84P760dxzhzj1w+2aj5U5SpneI3EaXmacIHuAF8KofnRydM+kjuyyFRr4L410HLky 3icKDoctNOsfH/9DPTQSqvN20Ur4Pi6Y3H9QRlDjKRx3Bl/0mz6Ofl5dohUOC8ki+cQGZRwITM6 tXp3XvPtoBgqBTZlvdHXimuypgYw9tTDV2rEBIO+oAaUjsrmP1G+DE78Wm4/2ixHv9qL1e5SJTi vwD4RFjuS5lAG4sD+/BWF+jn1/CImx1UMXz6hdRXcv8MHbvRmDuch17WwFdiGGYY0AmRakYdsqd Nti6anz+a5aDlWBFHG8o7c/5IvFFCA1En/4i7qGtwD9Dm/qSSdZB+s1qL1Y1gSAVL1X4SUfHw8L bM9pYZoNetx2YThec8+7MUIqJCTtdOuFQkdlNVscaMQxqLVdhMx8CbFGJUIVzSu/vSbng1ToL44 p+VqKIYUpbKR2SWWnMMrTNIHBYze3bczDVi0Y7dYepFgncGlUzRCJq254chzStwdZ+ma/oBwtnK Pe3P0l0QlSmOtNHPOxhk79qt2ZpXXMLO8hd9BdZM/jQA== X-Received: by 2002:a05:701a:ccce:10b0:1b5:7148:95b5 with SMTP id a92af1059eb24-1b5d71033ddmr2200995c88.8.1796402009158; Fri, 04 Dec 2026 08:33:29 -0800 (PST) Received: from kapoor.iiserb.ac.in ([203.18.51.239]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b5d642de41sm7844767c88.3.2026.12.04.08.33.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 08:33:28 -0800 (PST) From: Arnav Kapoor To: Yury Norov Cc: Rasmus Villemoes , Jonathan Cameron , Andy Shevchenko , linux-kernel@vger.kernel.org, Arnav Kapoor Subject: [PATCH 1/2] bitfield: make FIELD_FIT() check the value, not the shifted value Date: Fri, 4 Dec 2026 22:03:11 +0530 Message-ID: <20261204163317.924658-2-kapoorarnav43@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204163317.924658-1-kapoorarnav43@gmail.com> 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-Transfer-Encoding: 8bit FIELD_FIT() casts the value to the mask's type, shifts it up into the field and checks for bits outside the mask. Bits that the shift moves past the top of the type are lost, so a value that is too big can pass. On 32-bit, FIELD_FIT(GENMASK(9, 8), val) is true for val = BIT(24) and for val = INT_MIN, and FIELD_FIT(GENMASK(31, 28), val) is true for every val, since only the low four bits survive the shift. The same happens on 64-bit with a 32-bit mask such as (u32)GENMASK(9, 8). For a signed mask type the shift can also overflow, which is undefined behaviour. Shift the mask down instead and check the value against it, as FIELD_MAX() does. Nothing is shifted out, so every value that is outside the field after the cast to the mask's type is now rejected. Negative values still fail, as before, because the cast turns them into large unsigned values. The result is a plain compare against FIELD_MAX(): for FIELD_FIT(GENMASK(9, 8), int) gcc -m32 -O2 emits cmpl $3 + setbe, where it used to emit a test against 0x00fffffc, and clang for ARMv7 a single cmp, so the code is not worse. Suggested-by: Jonathan Cameron Fixes: 1697599ee301 ("bitfield.h: add FIELD_FIT() helper") Signed-off-by: Arnav Kapoor --- include/linux/bitfield.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index e3f7315c1d53..773874724b8e 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -121,7 +121,7 @@ #define FIELD_FIT(_mask, _val) \ ({ \ __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \ - !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \ + !(((typeof(_mask))(_val)) & ~((_mask) >> __bf_shf(_mask))); \ }) /** -- 2.53.0