From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14A772FFDC2; Tue, 9 Dec 2025 10:03:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274617; cv=none; b=ZALPXkbeYQjVMIQnhfuU/3kueXI9KaGabk9+0S4OY6PFgHZyAys8k2YJu3mfPzJZFpYYVa69kpvx+7c+Oc+ZeBqMveCUYBYUdBz8N/tIit8+yBaA0Eydf0JnxneGRc/OBiqt8W9eI4lHi7tej0cSkzjb3Lxx777uOhT/NZjx1+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274617; c=relaxed/simple; bh=rP9m/rvG+prYCOhaV5Vk15KN9R/2YCE3Pl0ckBAm7Jc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZXk5p+A9U2bdwnHBlUSbwpIoijoc0NJ6a2p99eNuyPvCvfT4Ac42RAtIg5BqHBXoCA+jkxDkb6p281B0GFZdipOS4i+qqPyW24m4dpEuTehH30gqoqhSPcQOUxLUK6g9fm1yWD4dknNpr0JqJ2B97kU7eiVCPW0gjDpidgpp+08= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=ahMmWdVl; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="ahMmWdVl" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZ7-00HD4r-Bh; Tue, 09 Dec 2025 11:03:29 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=H2G28ggF6h59sr0IjReK2Fd4VYXjVxOwedV//eDOpk8=; b=ahMmWdVl8RLYzOF1MAadDGXmPt xiAVULaxy0b6WCEXncURcdHsvKgFllppN/7/PM0lipbp4T2aA7v7gtR4LOoqUlttXAXB/RkDo1Rs1 Glnti1EHMlGAQPmqgXtw+IMiS+npBsYIaLHTPt4OUY+bQE1ejrTMNBuLIxiDKQRyvAGidwWLTZCR+ yLQL3mPS9BvwWk4AhlbSTuKRKrtUI64a+XU6ghMHUbs6ilqpagwCy1S87ccKg+wlPy0KMBRU8SlRP ApkuNVAlW7JhmxkF7ycp0z6eWMMHaRTWatx70ihKcL/1/gpgPsldlmkzQiB5UNpoq97WCu1tcJWiD VSp2Wffg==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZ6-0000Dx-LZ; Tue, 09 Dec 2025 11:03:28 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ1-00CND9-CB; Tue, 09 Dec 2025 11:03:23 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 1/9] nfp: Call FIELD_PREP() in NFP_ETH_SET_BIT_CONFIG() wrapper Date: Tue, 9 Dec 2025 10:03:05 +0000 Message-Id: <20251209100313.2867-2-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Rather than use a define that should be internal to the implementation of FIELD_PREP(), pass the shifted 'val' to nfp_eth_set_bit_config() and change the test for 'value unchanged' to match. This is a simpler change than the one used to avoid calling both FIELD_GET() and FIELD_PREP() with non-constant mask values. Signed-off-by: David Laight --- .../ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c b/dri= vers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c index 5cfddc9a5d87..4a71ff47fbef 100644 --- a/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c +++ b/drivers/net/ethernet/netronome/nfp/nfpcore/nfp_nsp_eth.c @@ -509,8 +509,7 @@ int nfp_eth_set_configured(struct nfp_cpp *cpp, unsigne= d int idx, bool configed) =20 static int nfp_eth_set_bit_config(struct nfp_nsp *nsp, unsigned int raw_idx, - const u64 mask, const unsigned int shift, - u64 val, const u64 ctrl_bit) + const u64 mask, u64 shifted_val, const u64 ctrl_bit) { union eth_table_entry *entries =3D nfp_nsp_config_entries(nsp); unsigned int idx =3D nfp_nsp_config_idx(nsp); @@ -527,11 +526,11 @@ nfp_eth_set_bit_config(struct nfp_nsp *nsp, unsigned = int raw_idx, =20 /* Check if we are already in requested state */ reg =3D le64_to_cpu(entries[idx].raw[raw_idx]); - if (val =3D=3D (reg & mask) >> shift) + if (shifted_val =3D=3D (reg & mask)) return 0; =20 reg &=3D ~mask; - reg |=3D (val << shift) & mask; + reg |=3D shifted_val; entries[idx].raw[raw_idx] =3D cpu_to_le64(reg); =20 entries[idx].control |=3D cpu_to_le64(ctrl_bit); @@ -571,12 +570,9 @@ int nfp_eth_set_idmode(struct nfp_cpp *cpp, unsigned i= nt idx, bool state) return nfp_eth_config_commit_end(nsp); } =20 -#define NFP_ETH_SET_BIT_CONFIG(nsp, raw_idx, mask, val, ctrl_bit) \ - ({ \ - __BF_FIELD_CHECK(mask, 0ULL, val, "NFP_ETH_SET_BIT_CONFIG: "); \ - nfp_eth_set_bit_config(nsp, raw_idx, mask, __bf_shf(mask), \ - val, ctrl_bit); \ - }) +#define NFP_ETH_SET_BIT_CONFIG(nsp, raw_idx, mask, val, ctrl_bit) \ + nfp_eth_set_bit_config(nsp, raw_idx, mask, FIELD_PREP(mask, val), \ + ctrl_bit) =20 /** * __nfp_eth_set_aneg() - set PHY autonegotiation control bit --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81CC4322B78; Tue, 9 Dec 2025 10:03:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274630; cv=none; b=QCoxxgHM/HWZFk9KB4FsURcE0EyjldLTTpwAsdbj4GKXSyDIiVG+PKbzrbcngEIT9FXRHEEzANeztXh1IjYsG0QVZaVDtX9YUuJN3nz2DwgzswGh+vPpwX2pn944CkDPWQR/g7+5UTWJU/3wZcMtzTxy7L+ULMh9aUKGF+l7dHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274630; c=relaxed/simple; bh=tsxt7GqGU2Y6aR3ftGYfuyEw4FuK0QVGoidAfI/vkNw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FD7EkLvG1GEYVQZ8YokcVCghh2mHfCVX3hmQ/oSs2U0ceejGlrQXw3gG+Ds75uMNFOSOUmbHlSv63p3SdlCKpSoulf+T+61hiQCbIjFdbP4Mbt1tlV63S+7qx8P8lxl4BsnF7k4gg/XCz6wIRzkOE+k4iMXU2MPis6kPqz8KGpY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=ZQfRzPzL; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="ZQfRzPzL" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZ7-00GyzU-Bw; Tue, 09 Dec 2025 11:03:29 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=pUFM0r0NZk4jg8TL6THMB9VhU15mhoWWVIu+KybWqt4=; b=ZQfRzPzLWP1GmEp0SqbsMwethi W1EzKrihUmwgeYDcIyPWkDzOH57KzFGGqkH62T/UzvyUDQyOLB/wN/PY3HozPR6wBVTuRpt4WtWt2 Ar4QVg7n9DcNn+tLOFWRhVxRCpTu+2oxnZLTSzuewV62yMd1I8qu7u2/afxS7fWPHKFBjDVPLFlNC HxLD5xiO7T7KUmrsZhI6565OOA/nsYl/DWWY+MHlts5deUJUAw9UjClq8m4mfrwCkCJS1vc8aJTYd XohmYfKzwbeDTBKzthLr9nt/ocGhuWQjDXEeIJTb1UxQZG877GtsyxWU/U63bSVua8qgOpO8QFoXu muS0Z2zA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZ4-0000Dd-U5; Tue, 09 Dec 2025 11:03:27 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ2-00CND9-G4; Tue, 09 Dec 2025 11:03:24 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 2/9] thunderblot: Don't pass a bitfield to FIELD_GET Date: Tue, 9 Dec 2025 10:03:06 +0000 Message-Id: <20251209100313.2867-3-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight FIELD_GET needs to use __auto_type to get the value of the 'reg' parameter, this can't be used with bifields. FIELD_GET also want to verify the size of 'reg' so can't add zero to force the type to int. So add a zero here. Signed-off-by: David Laight --- drivers/thunderbolt/tb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index e96474f17067..7ca2b5a0f01e 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -1307,7 +1307,7 @@ static inline struct tb_retimer *tb_to_retimer(struct= device *dev) */ static inline unsigned int usb4_switch_version(const struct tb_switch *sw) { - return FIELD_GET(USB4_VERSION_MAJOR_MASK, sw->config.thunderbolt_version); + return FIELD_GET(USB4_VERSION_MAJOR_MASK, sw->config.thunderbolt_version = + 0); } =20 /** --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC0412FBDE2 for ; Tue, 9 Dec 2025 10:03:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274616; cv=none; b=nGM6RiMpi6GJx5aoZKyxmJ91F1lL53V7zva/X/lImU3AmYV3TUvhUa+y+isBWu5u40FK+hRjmFcw0HWfk1BQXGkvd7o4uQ3tt+iiOYIDlL4DdXnPqjIwsssAUC0YlMtg6hdfUfnXd9WNTOBVnS7eoLAgYz74Mlx5JsOUJWrN/yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274616; c=relaxed/simple; bh=9GEZ1qcQcq+OU+/HJx1o4UIA998BTTxrtDXBo1mZRD4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gPBpi9xrWd7ecvLrlGVCKQjsrt5sCKV0cR4SWi87IirpsZz49cgRZHeutQMbEknhk9ul1BcUa/C42dWC+e4jvVzrZwEQoTYXRU8xIhOZwYrlswHfZd4Z7oNJb1Z00gHA35l0w5oMsLoSEg1mA12zsMfZc9xH44mVGHGQST+AEB4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=mUrpEN13; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="mUrpEN13" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZ7-00GyzW-BZ; Tue, 09 Dec 2025 11:03:29 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=YGnBBG9ZzprxJLEp94vtepq3lcPSfdeE6BD41wIj1gg=; b=mUrpEN13REkNMaUzxXJy60b3ra lR3EJczQGfp6BhxvZmLKXwOJRG4Cjf58rrzBDK1tRGJheVUlF3nI/pVTnGrLV0b5VXs99f1YboTU1 TppUIsxBWefpljsR+brKOkzToAxRqku61ZAY0Y/+z9uQxq/LwIprA+6oNSq0Kp424/KkHow+kLK3h LZ17mF3czhqWsCVWxRMvhaU3Fk6i8SThjEFQTxi/E6+0XStJwrBf/j5rCrUm6hnWJFgIkuHTkTH/Y Ffl1xT6d90VvW3bJuyR/5T4KDHqAxwPsZWx4p1zgCWXdO8N96DB9wPxob5uAylZqs0pucEMj8wlel fmJMdaiQ==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZ5-00074w-Vv; Tue, 09 Dec 2025 11:03:28 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ3-00CND9-Jl; Tue, 09 Dec 2025 11:03:25 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 3/9] bitmap: Use FIELD_PREP() in expansion of FIELD_PREP_WM16() Date: Tue, 9 Dec 2025 10:03:07 +0000 Message-Id: <20251209100313.2867-4-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Instead of directly expanding __BF_FIELD_CHECK() (which really ought not be used outside bitfield) and open-coding the generation of the masked value, just call FIELD_PREP() and add an extra check for the mask being at most 16 bits. Signed-off-by: David Laight --- include/linux/hw_bitfield.h | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/include/linux/hw_bitfield.h b/include/linux/hw_bitfield.h index df202e167ce4..d7f21b60449b 100644 --- a/include/linux/hw_bitfield.h +++ b/include/linux/hw_bitfield.h @@ -23,15 +23,14 @@ * register, a bit in the lower half is only updated if the corresponding = bit * in the upper half is high. */ -#define FIELD_PREP_WM16(_mask, _val) \ - ({ \ - typeof(_val) __val =3D _val; \ - typeof(_mask) __mask =3D _mask; \ - __BF_FIELD_CHECK(__mask, ((u16)0U), __val, \ - "HWORD_UPDATE: "); \ - (((typeof(__mask))(__val) << __bf_shf(__mask)) & (__mask)) | \ - ((__mask) << 16); \ - }) +#define FIELD_PREP_WM16(mask, val) \ +({ \ + __auto_type _mask =3D mask; \ + u32 _val =3D FIELD_PREP(_mask, val); \ + BUILD_BUG_ON_MSG(_mask > 0xffffu, \ + "FIELD_PREP_WM16: mask too large"); \ + _val | (_mask << 16); \ +}) =20 /** * FIELD_PREP_WM16_CONST() - prepare a constant bitfield element with a ma= sk in --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 327F7301027; Tue, 9 Dec 2025 10:03:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274618; cv=none; b=cu0JTkUsg7d7h7kdBskdhyhE7PVe9RFhbva7sh/k9gqQMZXuO44185fhxSxf/CofQLH4vJ5S1dleMZ4R0H+vxhAG+c+EgckkuAWujYytf1yc6mTOXPJzDX7O+LFs4cNdh1RpsEv1FZoJOeOG/gsikWNIw3I4x8Xco2eQM5OkcnM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274618; c=relaxed/simple; bh=j3EM9vwF8jfgyFYKrbFC2169fDDUS7n4xUANAz/Tg68=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MLbeSfQBrKUwAliMvb69qvA8GUCBS5iwRcMxH/5Y3sfLdzTQvjZB6wnHozwNFTuCG+d+rOUO0DalgtNGKk/cwOL/PaGMqLrwVPTVAoYoaYIkBu1Ckst5sbmIF1PpWiCZfbhoMxp6054zdPvjQLs4KyBi2MsNlFklxU0cvr16k74= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=DJOG268O; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="DJOG268O" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZ7-00HD4z-LL; Tue, 09 Dec 2025 11:03:29 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=Tw0kZy0+QPkTSZB4LgJsoDNSboZbkMKQvG1G43UWtIA=; b=DJOG268ODJk6H7R4ZhRhFsGQ4x H3ceN3Wgjo3boucI0N/5qe7L87mSESVDC+6H1o8+r747UwGKtxAr6TIcekaGhg7D70RvyqgGInszE JAAczl3nvwJA5bAs8MiVSJRBU2Onh+/WZZCR5/ZQYLjwNlmhFlctZmh18qBbn20rkVPNOtqH+HFNP mfg1pjP+SAsCAGH3saG1X+SA4YlM3QfSp8LL7YgFSGfl+NT0U0l2Bv0ehgDyCZIlDGYjyQoavZ8c6 NTwxGTEbI411RbwebXy1rAmhCKJjVdJp5RbvAd3Mt/Z/xb31R/ljoblZRMYslBvBsXlLzeMRdNB+m 8FBaA0oQ==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZ5-0000Dk-CV; Tue, 09 Dec 2025 11:03:27 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ4-00CND9-Nq; Tue, 09 Dec 2025 11:03:26 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 4/9] bitfield: Copy #define parameters to locals Date: Tue, 9 Dec 2025 10:03:08 +0000 Message-Id: <20251209100313.2867-5-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Use __auto_type to take copies of parameters to both ensure they are evaluated only once and to avoid bloating the pre-processor output. In particular 'mask' is likely to be GENMASK() and the expension of FIELD_GET() is then about 18KB. Remove any extra (), update kerneldoc. Consistently use xxx for #define formal parameters and _xxx for local variables. Rather than use (typeof(mask))(val) to ensure bits aren't lost when val is shifted left, use '__auto_type _val =3D 1 ? (val) : _mask;' relying on the ?: operator to generate a type that is large enough. Remove the (typeof(mask)) cast from __FIELD_GET(), it can only make a difference if 'reg' is larger than 'mask' and the caller cares about the actual type. Note that mask usually comes from GENMASK() and is then 'unsigned long'. Rename the internal defines __FIELD_PREP to __BF_FIELD_PREP and __FIELD_GET to __BF_FIELD_GET. Now that field_prep() and field_get() copy their parameters there is no need for the __field_prep() and __field_get() defines. But add a define to generate the required 'shift' to use in both defines. Signed-off-by: David Laight --- include/linux/bitfield.h | 150 ++++++++++++++++++++------------------- 1 file changed, 78 insertions(+), 72 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 126dc5b380af..3e013da9ea12 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -61,17 +61,17 @@ =20 #define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x)) =20 -#define __BF_FIELD_CHECK_MASK(_mask, _val, _pfx) \ +#define __BF_FIELD_CHECK_MASK(mask, val, pfx) \ ({ \ - BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \ - _pfx "mask is not constant"); \ - BUILD_BUG_ON_MSG((_mask) =3D=3D 0, _pfx "mask is zero"); \ - BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \ - ~((_mask) >> __bf_shf(_mask)) & \ - (0 + (_val)) : 0, \ - _pfx "value too large for the field"); \ - __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \ - (1ULL << __bf_shf(_mask))); \ + BUILD_BUG_ON_MSG(!__builtin_constant_p(mask), \ + pfx "mask is not constant"); \ + BUILD_BUG_ON_MSG((mask) =3D=3D 0, _pfx "mask is zero"); \ + BUILD_BUG_ON_MSG(__builtin_constant_p(val) ? \ + ~((mask) >> __bf_shf(mask)) & \ + (0 + (val)) : 0, \ + pfx "value too large for the field"); \ + __BUILD_BUG_ON_NOT_POWER_OF_2((mask) + \ + (1ULL << __bf_shf(mask))); \ }) =20 #define __BF_FIELD_CHECK_REG(mask, reg, pfx) \ @@ -85,64 +85,69 @@ __BF_FIELD_CHECK_REG(mask, reg, pfx); \ }) =20 -#define __FIELD_PREP(mask, val, pfx) \ +#define __BF_FIELD_PREP(mask, val, pfx) \ ({ \ __BF_FIELD_CHECK_MASK(mask, val, pfx); \ - ((typeof(mask))(val) << __bf_shf(mask)) & (mask); \ + ((val) << __bf_shf(mask)) & (mask); \ }) =20 -#define __FIELD_GET(mask, reg, pfx) \ +#define __BF_FIELD_GET(mask, reg, pfx) \ ({ \ __BF_FIELD_CHECK_MASK(mask, 0U, pfx); \ - (typeof(mask))(((reg) & (mask)) >> __bf_shf(mask)); \ + ((reg) & (mask)) >> __bf_shf(mask); \ }) =20 /** * FIELD_MAX() - produce the maximum value representable by a field - * @_mask: shifted mask defining the field's length and position + * @mask: shifted mask defining the field's length and position * * FIELD_MAX() returns the maximum value that can be held in the field - * specified by @_mask. + * specified by @mask. */ -#define FIELD_MAX(_mask) \ +#define FIELD_MAX(mask) \ ({ \ + __auto_type _mask =3D mask; \ __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \ - (typeof(_mask))((_mask) >> __bf_shf(_mask)); \ + (_mask >> __bf_shf(_mask)); \ }) =20 /** * FIELD_FIT() - check if value fits in the field - * @_mask: shifted mask defining the field's length and position - * @_val: value to test against the field + * @mask: shifted mask defining the field's length and position + * @val: value to test against the field * - * Return: true if @_val can fit inside @_mask, false if @_val is too big. + * Return: true if @val can fit inside @mask, false if @val is too big. */ -#define FIELD_FIT(_mask, _val) \ +#define FIELD_FIT(mask, val) \ ({ \ + __auto_type _mask =3D mask; \ + __auto_type _val =3D 1 ? (val) : _mask; \ __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \ - !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \ + !((_val << __bf_shf(_mask)) & ~_mask); \ }) =20 /** * FIELD_PREP() - prepare a bitfield element - * @_mask: shifted mask defining the field's length and position - * @_val: value to put in the field + * @mask: shifted mask defining the field's length and position + * @val: value to put in the field * * FIELD_PREP() masks and shifts up the value. The result should * be combined with other fields of the bitfield using logical OR. */ -#define FIELD_PREP(_mask, _val) \ +#define FIELD_PREP(mask, val) \ ({ \ + __auto_type _mask =3D mask; \ + __auto_type _val =3D 1 ? (val) : _mask; \ __BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \ - __FIELD_PREP(_mask, _val, "FIELD_PREP: "); \ + __BF_FIELD_PREP(_mask, _val, "FIELD_PREP: "); \ }) =20 #define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) !=3D 0) =20 /** * FIELD_PREP_CONST() - prepare a constant bitfield element - * @_mask: shifted mask defining the field's length and position - * @_val: value to put in the field + * @mask: shifted mask defining the field's length and position + * @val: value to put in the field * * FIELD_PREP_CONST() masks and shifts up the value. The result should * be combined with other fields of the bitfield using logical OR. @@ -151,47 +156,52 @@ * be used in initializers. Error checking is less comfortable for this * version, and non-constant masks cannot be used. */ -#define FIELD_PREP_CONST(_mask, _val) \ +#define FIELD_PREP_CONST(mask, val) \ ( \ /* mask must be non-zero */ \ - BUILD_BUG_ON_ZERO((_mask) =3D=3D 0) + \ + BUILD_BUG_ON_ZERO((mask) =3D=3D 0) + \ /* check if value fits */ \ - BUILD_BUG_ON_ZERO(~((_mask) >> __bf_shf(_mask)) & (_val)) + \ + BUILD_BUG_ON_ZERO(~((mask) >> __bf_shf(mask)) & (val)) + \ /* check if mask is contiguous */ \ - __BF_CHECK_POW2((_mask) + (1ULL << __bf_shf(_mask))) + \ + __BF_CHECK_POW2((mask) + (1ULL << __bf_shf(mask))) + \ /* and create the value */ \ - (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)) \ + (((typeof(mask))(val) << __bf_shf(mask)) & (mask)) \ ) =20 /** * FIELD_GET() - extract a bitfield element - * @_mask: shifted mask defining the field's length and position - * @_reg: value of entire bitfield + * @mask: shifted mask defining the field's length and position + * @reg: value of entire bitfield * - * FIELD_GET() extracts the field specified by @_mask from the - * bitfield passed in as @_reg by masking and shifting it down. + * FIELD_GET() extracts the field specified by @mask from the + * bitfield passed in as @reg by masking and shifting it down. */ -#define FIELD_GET(_mask, _reg) \ +#define FIELD_GET(mask, reg) \ ({ \ + __auto_type _mask =3D mask; \ + __auto_type _reg =3D reg; \ __BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \ - __FIELD_GET(_mask, _reg, "FIELD_GET: "); \ + __BF_FIELD_GET(_mask, _reg, "FIELD_GET: "); \ }) =20 /** * FIELD_MODIFY() - modify a bitfield element - * @_mask: shifted mask defining the field's length and position - * @_reg_p: pointer to the memory that should be updated - * @_val: value to store in the bitfield + * @mask: shifted mask defining the field's length and position + * @reg_p: pointer to the memory that should be updated + * @val: value to store in the bitfield * - * FIELD_MODIFY() modifies the set of bits in @_reg_p specified by @_mask, - * by replacing them with the bitfield value passed in as @_val. + * FIELD_MODIFY() modifies the set of bits in @reg_p specified by @mask, + * by replacing them with the bitfield value passed in as @val. */ -#define FIELD_MODIFY(_mask, _reg_p, _val) \ +#define FIELD_MODIFY(mask, reg_p, val) \ ({ \ + __auto_type _mask =3D mask; \ + __auto_type _reg_p =3D reg_p; \ + __auto_type _val =3D 1 ? (val) : _mask; \ typecheck_pointer(_reg_p); \ - __BF_FIELD_CHECK(_mask, *(_reg_p), _val, "FIELD_MODIFY: "); \ - *(_reg_p) &=3D ~(_mask); \ - *(_reg_p) |=3D (((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask)); \ + __BF_FIELD_CHECK(_mask, *_reg_p, _val, "FIELD_MODIFY: "); \ + *_reg_p &=3D ~_mask; \ + *_reg_p |=3D ((_val << __bf_shf(_mask)) & _mask); \ }) =20 extern void __compiletime_error("value doesn't fit into mask") @@ -241,23 +251,9 @@ __MAKE_OP(64) #undef __MAKE_OP #undef ____MAKE_OP =20 -#define __field_prep(mask, val) \ - ({ \ - __auto_type __mask =3D (mask); \ - typeof(__mask) __val =3D (val); \ - unsigned int __shift =3D BITS_PER_TYPE(__mask) <=3D 32 ? \ - __ffs(__mask) : __ffs64(__mask); \ - (__val << __shift) & __mask; \ - }) - -#define __field_get(mask, reg) \ - ({ \ - __auto_type __mask =3D (mask); \ - typeof(__mask) __reg =3D (reg); \ - unsigned int __shift =3D BITS_PER_TYPE(__mask) <=3D 32 ? \ - __ffs(__mask) : __ffs64(__mask); \ - (__reg & __mask) >> __shift; \ - }) +/* As __bf_shf() but for non-zero variables */ +#define __BF_SHIFT(mask) \ + (BITS_PER_TYPE(_mask) <=3D 32 ? __ffs(_mask) : __ffs64(_mask)) =20 /** * field_prep() - prepare a bitfield element @@ -275,9 +271,14 @@ __MAKE_OP(64) * If you want to ensure that @mask is a compile-time constant, please use * FIELD_PREP() directly instead. */ -#define field_prep(mask, val) \ - (__builtin_constant_p(mask) ? __FIELD_PREP(mask, val, "field_prep: ") \ - : __field_prep(mask, val)) +#define field_prep(mask, val) \ +({ \ + __auto_type _mask =3D mask; \ + __auto_type _val =3D 1 ? (val) : _mask; \ + __builtin_constant_p(_mask) ? \ + __BF_FIELD_PREP(_mask, _val, "field_prep: ") : \ + (_val << __BF_SHIFT(_mask)) & _mask; \ +}) =20 /** * field_get() - extract a bitfield element @@ -295,8 +296,13 @@ __MAKE_OP(64) * If you want to ensure that @mask is a compile-time constant, please use * FIELD_GET() directly instead. */ -#define field_get(mask, reg) \ - (__builtin_constant_p(mask) ? __FIELD_GET(mask, reg, "field_get: ") \ - : __field_get(mask, reg)) +#define field_get(mask, reg) \ +({ \ + __auto_type _mask =3D mask; \ + __auto_type _reg =3D reg; \ + __builtin_constant_p(_mask) ? \ + __BF_FIELD_GET(_mask, _reg, "field_get: ") : \ + (_reg & _mask) >> __BF_SHIFT(_mask); \ +}) =20 #endif --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 149FF2FF664; Tue, 9 Dec 2025 10:03:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274617; cv=none; b=KuCiSO7/fYZMWH7B+UIJvKKEsuudQrqDybI1ccwK9bN9blZ8IqGNKNWhlqf3c5pdpGGkFFik10HHu6C8ZkfEh9RIQCpoAIv2PiNKcrc40gxyZ1MeDMFWJZzeOJpqeUoX7b8QarMERDwsCxc9dtqbaGtNF3Ux1OIb4nBOH4B1UwE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274617; c=relaxed/simple; bh=r0JyqLB5s8BUcwpuoP7IpOoZ4xhcyEvbalDSYeF1rWU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HYUIf0ykP+2ecClSfRyYszQasxg/67dpxrx3Qo/MCIsDtmu+MHXBvVDlgKVriDIGGvjDjjhLPCTdu8URIAMBKhhHH2eHFen9cDeou8N3QFcOJe2ae+WCZ4kluhqC9duCTOpk6jxFR5GVP7EZJUrVxDm4GCvgvJ9byNVIqpg3dbw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=S3lbH9Q/; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="S3lbH9Q/" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZ7-00HD52-V2; Tue, 09 Dec 2025 11:03:29 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=KikFFrDxLsLsfdxtpsUWRZaSixAcB7BcUoyENyDfy4k=; b=S3lbH9Q/Yl+VFXGmEwPyKtPYEz 77WjtnVzjVmxMyudQgjI4elVjnc5hHMLe5PAB0Z2/jDfXid7Db6ytvRIQew2bJymKwQZScV8OdgtT AF3Rqm2dMKEhDVxFnqq4+eKWMn+6WBNa3raHvqITS0kdq6SGqgOMTvfvB4Glfsbi7dnzBVjOZpMIS afZ4CaJngiCWsxSX5gkKmgcuP/JN3SX+92z2ifHFj7+UxPOn8CIviP6UJ7zkMXSJ55FtAizxJ3ChO EHFgkS5dg6LHc+TbFGn4K6QtAX9P+zt7Vs+ka7eZEg+7qR68sf/V34gYafO6cnGVOkBBMXzACccmC wAXN02ZQ==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZ7-0000E4-5L; Tue, 09 Dec 2025 11:03:29 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ5-00CND9-TL; Tue, 09 Dec 2025 11:03:28 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 5/9] bitfield: FIELD_MODIFY: Only do a single read/write on the target Date: Tue, 9 Dec 2025 10:03:09 +0000 Message-Id: <20251209100313.2867-6-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Replace the: *reg &=3D ~mask; *reg |=3D new_value; with a single assignment. While the compiler will usually optimise the extra access away it isn't guaranteed. Signed-off-by: David Laight --- include/linux/bitfield.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 3e013da9ea12..3e0e8533bb66 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -200,8 +200,7 @@ __auto_type _val =3D 1 ? (val) : _mask; \ typecheck_pointer(_reg_p); \ __BF_FIELD_CHECK(_mask, *_reg_p, _val, "FIELD_MODIFY: "); \ - *_reg_p &=3D ~_mask; \ - *_reg_p |=3D ((_val << __bf_shf(_mask)) & _mask); \ + *_reg_p =3D (*_reg_p & ~_mask) | ((_val << __bf_shf(_mask)) & _mask); \ }) =20 extern void __compiletime_error("value doesn't fit into mask") --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84C5E3242D8; Tue, 9 Dec 2025 10:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274635; cv=none; b=Y+Ur92valSGoz5wmzQ2W/k9qMzgilsQlKhjA1mT16E+3/Z2efSZr7P+zjZwFoM0a+UbG9iuTkDxgijiXo708NwjvyDFs4sPdmUkuWtgqsFruu795LudssLmcvo9GHuROf8AYj9TmsD7dcDAG3hFI7aB3tmz+LHFnTLDSMIpoSXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274635; c=relaxed/simple; bh=tSCid1sPM0NX+JcS8589gqV/qYOqQ9nwuLmgYPNoSTU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DQVqfX9s4GG7GbHhR4zdUbNzHgEGhDL0ZKojWTFzANxVgb79uD6WbTW1/BBN3oQ58l8rDhExlX1UKARi2Q8N7Ki1hI9OJZOInk7eXJTkWnh0XXV/IiuwvE2FnAh5uRIxSYwV7M4jPDXA+DPvvhV+LmGU9uNui8QzSihJ5p5KH4w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=q+8ONBud; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="q+8ONBud" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZR-00Gz5p-EH; Tue, 09 Dec 2025 11:03:49 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=OPG7fOtrkSC18N/RtHW1QBQqlawki4Y6k2/a0XZqajk=; b=q+8ONBud9kB/D38Wv1XtzGNYgz 1Q1ZbG9xuiuKUQp4/4kM2uKX9UDSELqnfSRW/ilOx+zsdHIpa4T4KI7RztYAW7WMHybSeYJukmfyl L+sY6AmQu1RLKTApX9zviUrpEWDFwcPWFX1oe2QexB51kPP0EHDIaFioBtEbxHOyCQlyFhaE+Z19X hADKVYgueALMBFeik/FUuxqcSlt4wUZTp/LO8J/sGVmBemyLeglQc8MdgbjpWrSnzrvskKaGsOW23 UOdVagj3D1KSBlhjSjHVRn2mUeXqsl2PeQheSxERdN1iQXLtG94fO6255X7YjuLOGllEqfh4gYcsS DkOeS7pQ==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZQ-00075o-VA; Tue, 09 Dec 2025 11:03:49 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ7-00CND9-0p; Tue, 09 Dec 2025 11:03:29 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 6/9] bitfield: Update sanity checks Date: Tue, 9 Dec 2025 10:03:10 +0000 Message-Id: <20251209100313.2867-7-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Simplify the check for 'reg' being large enough to hold 'mask' using sizeof (reg) rather than a convoluted scheme to generate an unsigned type the same size as 'reg'. There are three places where the mask is checked for being non-zero and contiguous. Add a simple expression that checks it and use in all three places. Three of the five calls to __BF_FIELD_CHECK_MASK() don't have a 'value' to check, separate out as was done to __BF_FIELD_CHECK_REG(). There is no point checking a 'val' of zero or a 'reg' of 0ULL (both are placeholders) - remove/change the calls. There should be a check of __BF_FIELD_CHECK_REG() when __BF_FIELD_GET() is called from field_get(). Move the check from FIELD_GET() into __BF_FIELD_GET(). Delete the now-unused __BF_FIELD_CHECK(). Signed-off-by: David Laight --- include/linux/bitfield.h | 74 ++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 48 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 3e0e8533bb66..7e8d436b6571 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -45,55 +45,38 @@ =20 #define __bf_shf(x) (__builtin_ffsll(x) - 1) =20 -#define __scalar_type_to_unsigned_cases(type) \ - unsigned type: (unsigned type)0, \ - signed type: (unsigned type)0 +#define __BF_VALIDATE_MASK(mask) \ + (!(mask) || ((mask) & ((mask) + ((mask) & -(mask))))) =20 -#define __unsigned_scalar_typeof(x) typeof( \ - _Generic((x), \ - char: (unsigned char)0, \ - __scalar_type_to_unsigned_cases(char), \ - __scalar_type_to_unsigned_cases(short), \ - __scalar_type_to_unsigned_cases(int), \ - __scalar_type_to_unsigned_cases(long), \ - __scalar_type_to_unsigned_cases(long long), \ - default: (x))) - -#define __bf_cast_unsigned(type, x) ((__unsigned_scalar_typeof(type))(x)) - -#define __BF_FIELD_CHECK_MASK(mask, val, pfx) \ - ({ \ +#define __BF_FIELD_CHECK_MASK(mask, pfx) \ + do { \ BUILD_BUG_ON_MSG(!__builtin_constant_p(mask), \ pfx "mask is not constant"); \ - BUILD_BUG_ON_MSG((mask) =3D=3D 0, _pfx "mask is zero"); \ - BUILD_BUG_ON_MSG(__builtin_constant_p(val) ? \ - ~((mask) >> __bf_shf(mask)) & \ - (0 + (val)) : 0, \ - pfx "value too large for the field"); \ - __BUILD_BUG_ON_NOT_POWER_OF_2((mask) + \ - (1ULL << __bf_shf(mask))); \ - }) + BUILD_BUG_ON_MSG(__BF_VALIDATE_MASK(mask), \ + pfx "mask is zero or not contiguous"); \ + } while (0) + +#define __BF_FIELD_CHECK_VAL(mask, val, pfx) \ + BUILD_BUG_ON_MSG(__builtin_constant_p(val) && \ + ~((mask) >> __bf_shf(mask)) & (val), \ + pfx "value too large for the field") =20 #define __BF_FIELD_CHECK_REG(mask, reg, pfx) \ - BUILD_BUG_ON_MSG(__bf_cast_unsigned(mask, mask) > \ - __bf_cast_unsigned(reg, ~0ull), \ + BUILD_BUG_ON_MSG(mask + 0U + 0UL + 0ULL > \ + ~0ULL >> (64 - 8 * sizeof (reg)), \ pfx "type of reg too small for mask") =20 -#define __BF_FIELD_CHECK(mask, reg, val, pfx) \ - ({ \ - __BF_FIELD_CHECK_MASK(mask, val, pfx); \ - __BF_FIELD_CHECK_REG(mask, reg, pfx); \ - }) - #define __BF_FIELD_PREP(mask, val, pfx) \ ({ \ - __BF_FIELD_CHECK_MASK(mask, val, pfx); \ + __BF_FIELD_CHECK_MASK(mask, pfx); \ + __BF_FIELD_CHECK_VAL(mask, val, pfx); \ ((val) << __bf_shf(mask)) & (mask); \ }) =20 #define __BF_FIELD_GET(mask, reg, pfx) \ ({ \ - __BF_FIELD_CHECK_MASK(mask, 0U, pfx); \ + __BF_FIELD_CHECK_MASK(mask, pfx); \ + __BF_FIELD_CHECK_REG(mask, reg, pfx); \ ((reg) & (mask)) >> __bf_shf(mask); \ }) =20 @@ -107,7 +90,7 @@ #define FIELD_MAX(mask) \ ({ \ __auto_type _mask =3D mask; \ - __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_MAX: "); \ + __BF_FIELD_CHECK_MASK(_mask, "FIELD_MAX: "); \ (_mask >> __bf_shf(_mask)); \ }) =20 @@ -122,7 +105,7 @@ ({ \ __auto_type _mask =3D mask; \ __auto_type _val =3D 1 ? (val) : _mask; \ - __BF_FIELD_CHECK(_mask, 0ULL, 0ULL, "FIELD_FIT: "); \ + __BF_FIELD_CHECK_MASK(_mask, "FIELD_FIT: "); \ !((_val << __bf_shf(_mask)) & ~_mask); \ }) =20 @@ -138,12 +121,9 @@ ({ \ __auto_type _mask =3D mask; \ __auto_type _val =3D 1 ? (val) : _mask; \ - __BF_FIELD_CHECK_REG(_mask, 0ULL, "FIELD_PREP: "); \ __BF_FIELD_PREP(_mask, _val, "FIELD_PREP: "); \ }) =20 -#define __BF_CHECK_POW2(n) BUILD_BUG_ON_ZERO(((n) & ((n) - 1)) !=3D 0) - /** * FIELD_PREP_CONST() - prepare a constant bitfield element * @mask: shifted mask defining the field's length and position @@ -158,12 +138,10 @@ */ #define FIELD_PREP_CONST(mask, val) \ ( \ - /* mask must be non-zero */ \ - BUILD_BUG_ON_ZERO((mask) =3D=3D 0) + \ + /* mask must be non-zero and contiguous */ \ + BUILD_BUG_ON_ZERO(__BF_VALIDATE_MASK(mask)) + \ /* check if value fits */ \ BUILD_BUG_ON_ZERO(~((mask) >> __bf_shf(mask)) & (val)) + \ - /* check if mask is contiguous */ \ - __BF_CHECK_POW2((mask) + (1ULL << __bf_shf(mask))) + \ /* and create the value */ \ (((typeof(mask))(val) << __bf_shf(mask)) & (mask)) \ ) @@ -180,7 +158,6 @@ ({ \ __auto_type _mask =3D mask; \ __auto_type _reg =3D reg; \ - __BF_FIELD_CHECK_REG(_mask, _reg, "FIELD_GET: "); \ __BF_FIELD_GET(_mask, _reg, "FIELD_GET: "); \ }) =20 @@ -198,8 +175,9 @@ __auto_type _mask =3D mask; \ __auto_type _reg_p =3D reg_p; \ __auto_type _val =3D 1 ? (val) : _mask; \ - typecheck_pointer(_reg_p); \ - __BF_FIELD_CHECK(_mask, *_reg_p, _val, "FIELD_MODIFY: "); \ + __BF_FIELD_CHECK_MASK(_mask, "FIELD_MODIFY: "); \ + __BF_FIELD_CHECK_VAL(_mask, _val, "FIELD_MODIFY: "); \ + __BF_FIELD_CHECK_REG(_mask, *_reg_p, "FIELD_MODIFY: "); \ *_reg_p =3D (*_reg_p & ~_mask) | ((_val << __bf_shf(_mask)) & _mask); \ }) =20 @@ -209,7 +187,7 @@ extern void __compiletime_error("bad bitfield mask") __bad_mask(void); static __always_inline u64 field_multiplier(u64 field) { - if ((field | (field - 1)) & ((field | (field - 1)) + 1)) + if (__BF_VALIDATE_MASK(field)) __bad_mask(); return field & -field; } --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E284A2FF14F; Tue, 9 Dec 2025 10:03:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274616; cv=none; b=suiIW1GMzICus1357csBQRut/mwfPv/q+8aa0xOk1oWnE4JN+YUcCIxAqddGObgjjF7p9aIvss/NuzJF5+kZIRzK0sx7YK7DyL0RczW9GKrHON1uspuo66WkVQTL/dKz+nsUQeU7j+4XM6c0B6hGCXNFwwgN/RAJImdjXbM55N4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274616; c=relaxed/simple; bh=bZ63dbuTIfAobODlW60dWN4FIiJkGkip2hP19VOFpPU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a4+LeG3qhxiuIa9yMwyRPu3uNGp+ZAsD/XveC3V2WRAb0HmlDNAGQ8JPDgJ3WpZMXWeP50TvBhjIvoNwPg110269s2Y3I54RhJ+EA2wQp6JmGJwTscThdchaereoMwAGGEXgtxlf0L5530NV3tG6m3KLIbbgbmDB7aSrJLh7x38= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=Y97+WlGs; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="Y97+WlGs" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZ9-00HD5F-7x; Tue, 09 Dec 2025 11:03:31 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=s5qu4+g3giptobjZuw21YQ9JrNuy8EW0sZD8ggimS5M=; b=Y97+WlGsTWr8ayyMAX7yQFexlb vd1Rn1Ax5VrBMfyA/JYZNqLl+1aR8Vv7Je/NC4qrFord6ZSUC5q9HxVSfqTIiO5rMhZfT9LpqxPXp udmRO7p8FdYeYmXxXgaVibJQx7HL7X/U2orXfDh/33+VTi/bph06fxjq6V1J3htPDoTP6ooZgKfhD wRex3LMVhuegS89r/0LwdPvIMNj3fQ/OpqIVWu8471uFcxqMVxX7lTbG7uzvmg9WELl6dBry2A9PS gT7pF1NaJ2R1yCDLsDRfzjxxciUYCsbA2P3xeTy+KxwG0sWp/m2UnmAMtawcHhJXY2+BqQYhXRKow fFTg4+NA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZ8-000758-Ps; Tue, 09 Dec 2025 11:03:30 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ8-00CND9-8w; Tue, 09 Dec 2025 11:03:30 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 7/9] bitfield: Reduce indentation Date: Tue, 9 Dec 2025 10:03:11 +0000 Message-Id: <20251209100313.2867-8-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight There is no need to double indent the body of #defines. Leave the opening ( and closing ) on their own lines. Delete extra tabs before continuation markers. Signed-off-by: David Laight --- include/linux/bitfield.h | 132 +++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 7e8d436b6571..bfd80ebd25b1 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -48,37 +48,37 @@ #define __BF_VALIDATE_MASK(mask) \ (!(mask) || ((mask) & ((mask) + ((mask) & -(mask))))) =20 -#define __BF_FIELD_CHECK_MASK(mask, pfx) \ - do { \ - BUILD_BUG_ON_MSG(!__builtin_constant_p(mask), \ - pfx "mask is not constant"); \ - BUILD_BUG_ON_MSG(__BF_VALIDATE_MASK(mask), \ - pfx "mask is zero or not contiguous"); \ - } while (0) +#define __BF_FIELD_CHECK_MASK(mask, pfx) \ +do { \ + BUILD_BUG_ON_MSG(!__builtin_constant_p(mask), \ + pfx "mask is not constant"); \ + BUILD_BUG_ON_MSG(__BF_VALIDATE_MASK(mask), \ + pfx "mask is zero or not contiguous"); \ +} while (0) =20 #define __BF_FIELD_CHECK_VAL(mask, val, pfx) \ BUILD_BUG_ON_MSG(__builtin_constant_p(val) && \ ~((mask) >> __bf_shf(mask)) & (val), \ pfx "value too large for the field") =20 -#define __BF_FIELD_CHECK_REG(mask, reg, pfx) \ - BUILD_BUG_ON_MSG(mask + 0U + 0UL + 0ULL > \ - ~0ULL >> (64 - 8 * sizeof (reg)), \ +#define __BF_FIELD_CHECK_REG(mask, reg, pfx) \ + BUILD_BUG_ON_MSG(mask + 0U + 0UL + 0ULL > \ + ~0ULL >> (64 - 8 * sizeof (reg)), \ pfx "type of reg too small for mask") =20 -#define __BF_FIELD_PREP(mask, val, pfx) \ - ({ \ - __BF_FIELD_CHECK_MASK(mask, pfx); \ - __BF_FIELD_CHECK_VAL(mask, val, pfx); \ - ((val) << __bf_shf(mask)) & (mask); \ - }) +#define __BF_FIELD_PREP(mask, val, pfx) \ +({ \ + __BF_FIELD_CHECK_MASK(mask, pfx); \ + __BF_FIELD_CHECK_VAL(mask, val, pfx); \ + ((val) << __bf_shf(mask)) & (mask); \ +}) =20 -#define __BF_FIELD_GET(mask, reg, pfx) \ - ({ \ - __BF_FIELD_CHECK_MASK(mask, pfx); \ - __BF_FIELD_CHECK_REG(mask, reg, pfx); \ - ((reg) & (mask)) >> __bf_shf(mask); \ - }) +#define __BF_FIELD_GET(mask, reg, pfx) \ +({ \ + __BF_FIELD_CHECK_MASK(mask, pfx); \ + __BF_FIELD_CHECK_REG(mask, reg, pfx); \ + ((reg) & (mask)) >> __bf_shf(mask); \ +}) =20 /** * FIELD_MAX() - produce the maximum value representable by a field @@ -87,12 +87,12 @@ * FIELD_MAX() returns the maximum value that can be held in the field * specified by @mask. */ -#define FIELD_MAX(mask) \ - ({ \ - __auto_type _mask =3D mask; \ - __BF_FIELD_CHECK_MASK(_mask, "FIELD_MAX: "); \ - (_mask >> __bf_shf(_mask)); \ - }) +#define FIELD_MAX(mask) \ +({ \ + __auto_type _mask =3D mask; \ + __BF_FIELD_CHECK_MASK(_mask, "FIELD_MAX: "); \ + (_mask >> __bf_shf(_mask)); \ +}) =20 /** * FIELD_FIT() - check if value fits in the field @@ -101,13 +101,13 @@ * * Return: true if @val can fit inside @mask, false if @val is too big. */ -#define FIELD_FIT(mask, val) \ - ({ \ - __auto_type _mask =3D mask; \ - __auto_type _val =3D 1 ? (val) : _mask; \ - __BF_FIELD_CHECK_MASK(_mask, "FIELD_FIT: "); \ - !((_val << __bf_shf(_mask)) & ~_mask); \ - }) +#define FIELD_FIT(mask, val) \ +({ \ + __auto_type _mask =3D mask; \ + __auto_type _val =3D 1 ? (val) : _mask; \ + __BF_FIELD_CHECK_MASK(_mask, "FIELD_FIT: "); \ + !((_val << __bf_shf(_mask)) & ~_mask); \ +}) =20 /** * FIELD_PREP() - prepare a bitfield element @@ -117,12 +117,12 @@ * FIELD_PREP() masks and shifts up the value. The result should * be combined with other fields of the bitfield using logical OR. */ -#define FIELD_PREP(mask, val) \ - ({ \ - __auto_type _mask =3D mask; \ - __auto_type _val =3D 1 ? (val) : _mask; \ - __BF_FIELD_PREP(_mask, _val, "FIELD_PREP: "); \ - }) +#define FIELD_PREP(mask, val) \ +({ \ + __auto_type _mask =3D mask; \ + __auto_type _val =3D 1 ? (val) : _mask; \ + __BF_FIELD_PREP(_mask, _val, "FIELD_PREP: "); \ +}) =20 /** * FIELD_PREP_CONST() - prepare a constant bitfield element @@ -136,15 +136,15 @@ * be used in initializers. Error checking is less comfortable for this * version, and non-constant masks cannot be used. */ -#define FIELD_PREP_CONST(mask, val) \ - ( \ - /* mask must be non-zero and contiguous */ \ - BUILD_BUG_ON_ZERO(__BF_VALIDATE_MASK(mask)) + \ - /* check if value fits */ \ - BUILD_BUG_ON_ZERO(~((mask) >> __bf_shf(mask)) & (val)) + \ - /* and create the value */ \ - (((typeof(mask))(val) << __bf_shf(mask)) & (mask)) \ - ) +#define FIELD_PREP_CONST(mask, val) \ +( \ + /* mask must be non-zero and contiguous */ \ + BUILD_BUG_ON_ZERO(__BF_VALIDATE_MASK(mask)) + \ + /* check if value fits */ \ + BUILD_BUG_ON_ZERO(~((mask) >> __bf_shf(mask)) & (val)) + \ + /* and create the value */ \ + (((typeof(mask))(val) << __bf_shf(mask)) & (mask)) \ +) =20 /** * FIELD_GET() - extract a bitfield element @@ -154,12 +154,12 @@ * FIELD_GET() extracts the field specified by @mask from the * bitfield passed in as @reg by masking and shifting it down. */ -#define FIELD_GET(mask, reg) \ - ({ \ - __auto_type _mask =3D mask; \ - __auto_type _reg =3D reg; \ - __BF_FIELD_GET(_mask, _reg, "FIELD_GET: "); \ - }) +#define FIELD_GET(mask, reg) \ +({ \ + __auto_type _mask =3D mask; \ + __auto_type _reg =3D reg; \ + __BF_FIELD_GET(_mask, _reg, "FIELD_GET: "); \ +}) =20 /** * FIELD_MODIFY() - modify a bitfield element @@ -170,16 +170,16 @@ * FIELD_MODIFY() modifies the set of bits in @reg_p specified by @mask, * by replacing them with the bitfield value passed in as @val. */ -#define FIELD_MODIFY(mask, reg_p, val) \ - ({ \ - __auto_type _mask =3D mask; \ - __auto_type _reg_p =3D reg_p; \ - __auto_type _val =3D 1 ? (val) : _mask; \ - __BF_FIELD_CHECK_MASK(_mask, "FIELD_MODIFY: "); \ - __BF_FIELD_CHECK_VAL(_mask, _val, "FIELD_MODIFY: "); \ - __BF_FIELD_CHECK_REG(_mask, *_reg_p, "FIELD_MODIFY: "); \ - *_reg_p =3D (*_reg_p & ~_mask) | ((_val << __bf_shf(_mask)) & _mask); \ - }) +#define FIELD_MODIFY(mask, reg_p, val) \ +({ \ + __auto_type _mask =3D mask; \ + __auto_type _reg_p =3D reg_p; \ + __auto_type _val =3D 1 ? (val) : _mask; \ + __BF_FIELD_CHECK_MASK(_mask, "FIELD_MODIFY: "); \ + __BF_FIELD_CHECK_VAL(_mask, _val, "FIELD_MODIFY: "); \ + __BF_FIELD_CHECK_REG(_mask, *_reg_p, "FIELD_MODIFY: "); \ + *_reg_p =3D (*_reg_p & ~_mask) | ((_val << __bf_shf(_mask)) & _mask); \ +}) =20 extern void __compiletime_error("value doesn't fit into mask") __field_overflow(void); --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2C0A324B19; Tue, 9 Dec 2025 10:03:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274635; cv=none; b=okVszCBiH+yStg4q/mywGITVz8+4l+L9IE6xo0Yz+ANKH3JPw27PAFyNhxtGv/qiwpnMOAUGrvO1qJoR+F9PYRgYrcAcySLJ1I+qyC6qHzH4LYP+5a4MrdiKmQDqlhXV5vOLXBjDYX3C+FX9DUxkYI563dpnqYI0vtMFvSnI9a4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274635; c=relaxed/simple; bh=fXGZ2M7xxYHgmTpQmtTDzEYbKvZXrYIjTdOkA45M/Q4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X8915odni4zA8RK2p2Phym+/2NjsiessmhLj06i67eoRIt5XTCvaTlUoRPATnmkPPiyc3DN/mivZ+/4Zxbu4mHb2cXvaWxf4upPfYxamPLOqnC18DokOnxnVsJpAJr89n8GMW/S7r8kdGDGnS141HHoWl/TZ9T2l4N8oRzr1eEE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=vP5z3pHL; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="vP5z3pHL" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZP-00HDAJ-Bp; Tue, 09 Dec 2025 11:03:47 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=pDzfAzksr2ZkqPsHEML95dRnTX61DmWnWyNEXtTl4FY=; b=vP5z3pHLMaiwh958rxwLVuNQE1 Y9JGtD4u9c0ZZ8jAWOjpGBDpHzeRyANB3cPxsDjTSmHxQq2h3rocfjqASw9EHxdNXGuxd9JDAIiVV PpV1Y/K3or9uf7KS0Eo1B6Eig+tw0IeEKpCT7DRGqy70gIggLa9BkfdkxnS7gPgWltZNxcr/tORSE 2uj0k8brqG0KNhWspzqC5tD2Cg6PRnWg8s4HnMA0XzoJT6UiM34zoe9LH4VWzLSLsoFUt644+Pt5Q XTLtzpCQybAauBzRIjQ13UI5SXHdittYtIKn+VCFHGR2gxVMiEJdFVRwEB3keoNiRUjAGifEBCnFh nW4K1ZmA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZO-00075d-UF; Tue, 09 Dec 2025 11:03:47 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZ9-00CND9-Ca; Tue, 09 Dec 2025 11:03:31 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 8/9] bitfield: Add comment block for the host/fixed endian functions Date: Tue, 9 Dec 2025 10:03:12 +0000 Message-Id: <20251209100313.2867-9-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Copied almost verbatim from the commit message that added the functions. Signed-off-by: David Laight --- include/linux/bitfield.h | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index bfd80ebd25b1..9feb489a8da3 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -181,6 +181,49 @@ do { \ *_reg_p =3D (*_reg_p & ~_mask) | ((_val << __bf_shf(_mask)) & _mask); \ }) =20 +/* + * Primitives for manipulating bitfields both in host- and fixed-endian. + * + * * u32 le32_get_bits(__le32 val, u32 field) extracts the contents of the + * bitfield specified by @field in little-endian 32bit object @val and + * converts it to host-endian. + * + * * void le32p_replace_bits(__le32 *p, u32 v, u32 field) replaces + * the contents of the bitfield specified by @field in little-endian + * 32bit object pointed to by @p with the value of @v. New value is + * given in host-endian and stored as little-endian. + * + * * __le32 le32_replace_bits(__le32 old, u32 v, u32 field) is equivalent = to + * ({__le32 tmp =3D old; le32p_replace_bits(&tmp, v, field); tmp;}) + * In other words, instead of modifying an object in memory, it takes + * the initial value and returns the modified one. + * + * * __le32 le32_encode_bits(u32 v, u32 field) is equivalent to + * le32_replace_bits(0, v, field). In other words, it returns a little-= endian + * 32bit object with the bitfield specified by @field containing the + * value of @v and all bits outside that bitfield being zero. + * + * Such set of helpers is defined for each of little-, big- and host-endian + * types; e.g. u64_get_bits(val, field) will return the contents of the bi= tfield + * specified by @field in host-endian 64bit object @val, etc. Of course, = for + * host-endian no conversion is involved. + * + * Fields to access are specified as GENMASK() values - an N-bit field + * starting at bit #M is encoded as GENMASK(M + N - 1, M). Note that + * bit numbers refer to endianness of the object we are working with - + * e.g. GENMASK(11, 0) in __be16 refers to the second byte and the lower + * 4 bits of the first byte. In __le16 it would refer to the first byte + * and the lower 4 bits of the second byte, etc. + * + * Field specification must be a constant; __builtin_constant_p() doesn't + * have to be true for it, but compiler must be able to evaluate it at + * build time. If it cannot or if the value does not encode any bitfield, + * the build will fail. + * + * If the value being stored in a bitfield is a constant that does not fit + * into that bitfield, a warning will be generated at compile time. + */ + extern void __compiletime_error("value doesn't fit into mask") __field_overflow(void); extern void __compiletime_error("bad bitfield mask") --=20 2.39.5 From nobody Wed Feb 11 17:20:55 2026 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84B603242D7; Tue, 9 Dec 2025 10:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274633; cv=none; b=NllL/TycQBFiLdqJ0JxtGkWXNBSK39fgbdaxRyea+baV2JvlIvPhk0R7SjnuoOskArj2ycZ6ihEaSENLDBj3963x1Dp56gtsSGzU8zt5xKxXgZhEvJFL6cvJlGlT+tUxk5rCDQFT9rz4TXsAMEpRE5J/w/6Jft0A0DkNS/gFj8A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765274633; c=relaxed/simple; bh=E4cqdyWnTy6bAeX8CvB1yK9sMkTu2li1KwGCMYFsl7o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=d5xVBc8u973wiJT2afY9fRFI7JRtwPg32Yn2MpRmNq71s6lUIkiq3gtQunvMQXUwExVaummZ51kd+CmkX9UUwYgONoLAN7wEJg7+bMZf+gPohvvody7knvN3n/8/F/hsMsHJKSgrje7PSy+WrfQetLVj46E07HPboXhgYllXLn4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=fail (0-bit key) header.d=runbox.com header.i=@runbox.com header.b=sl+fWZA1 reason="key not found in DNS"; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=runbox.com header.i=@runbox.com header.b="sl+fWZA1" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vSuZQ-00Gz5h-TL; Tue, 09 Dec 2025 11:03:48 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=bsZokk41/Z+0gE+q67wcjT3/DTqcuPp1ab0Znae2e7U=; b=sl+fWZA1KrHVQlduXXfJhykvKr 2WT2lPOU5hY2jlC0JNcG2BE3iNunIRBT0f3+1bIYNlrQJvuzjrxaxTHN15TPPpzNV+qDs5dmaMbK+ MHt/Gt7XoasFhPX1JZrdUrjkI619HjFJTraQsVEYOY12Dz/m4dsyBOuSfC19rapOWADhGGWP895XD b8tf/mX9lvoeFelTnhdGSmHecg3XLel1Aeh7hDUCnT7+NJzu3+l+cS0esOZDzUCXG2eA//9lTCaqo A8MDyhhAoXUfTPxlGx26wW0G1YQ5Aw6Ly3FsjH7R499v52fR4oWfOw/imEMzOSYJn6yd76ufDaT3o e8TDWTYA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vSuZQ-0000Ib-Hl; Tue, 09 Dec 2025 11:03:48 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vSuZA-00CND9-G9; Tue, 09 Dec 2025 11:03:32 +0100 From: david.laight.linux@gmail.com To: Yury Norov , Rasmus Villemoes , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Geert Uytterhoeven , Alexandre Belloni , Jonathan Cameron , Crt Mori , Richard Genoud , Andy Shevchenko , Luo Jie , Peter Zijlstra , Jakub Kicinski , netdev@vger.kernel.org, "David S . Miller" , Simon Horman , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH 9/9] bitfield: Update comments for le/be functions Date: Tue, 9 Dec 2025 10:03:13 +0000 Message-Id: <20251209100313.2867-10-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251209100313.2867-1-david.laight.linux@gmail.com> References: <20251209100313.2867-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Laight Make it clear the the values are converted to host order before being acted on. Order the explantions with the simple functions first. These still need converting to kerneldoc format. Signed-off-by: David Laight --- include/linux/bitfield.h | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 9feb489a8da3..dcc4809b4706 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -184,24 +184,24 @@ do { \ /* * Primitives for manipulating bitfields both in host- and fixed-endian. * - * * u32 le32_get_bits(__le32 val, u32 field) extracts the contents of the - * bitfield specified by @field in little-endian 32bit object @val and - * converts it to host-endian. * - * * void le32p_replace_bits(__le32 *p, u32 v, u32 field) replaces - * the contents of the bitfield specified by @field in little-endian - * 32bit object pointed to by @p with the value of @v. New value is - * given in host-endian and stored as little-endian. + * * u32 le32_get_bits(__le32 val, u32 field) converts the little-endian 3= 2bit + * object @val to host-endian then extracts the contents of the bitfield + * specified by @field. + * + * * __le32 le32_encode_bits(u32 v, u32 field) encodes the value of @v into + * the bitfield specified by @field then converts the value to little-en= dian. + * All the bits outside that bitfield being zero. * - * * __le32 le32_replace_bits(__le32 old, u32 v, u32 field) is equivalent = to - * ({__le32 tmp =3D old; le32p_replace_bits(&tmp, v, field); tmp;}) - * In other words, instead of modifying an object in memory, it takes - * the initial value and returns the modified one. + * * __le32 le32_replace_bits(__le32 old, u32 v, u32 field) converts the + * little-endian 32bit object @old to host order, replaces the contents + * of the bitfield specified by @field with @v, then returns the value + * converted back to little-endian. * - * * __le32 le32_encode_bits(u32 v, u32 field) is equivalent to - * le32_replace_bits(0, v, field). In other words, it returns a little-= endian - * 32bit object with the bitfield specified by @field containing the - * value of @v and all bits outside that bitfield being zero. + * * void le32p_replace_bits(__le32 *p, u32 v, u32 field) replaces + * the contents of the bitfield specified by @field in little-endian + * 32bit object pointed to by @p with the value of @v. + * Equivalent to *p =3D le32_replace_bits(*p, v, field). * * Such set of helpers is defined for each of little-, big- and host-endian * types; e.g. u64_get_bits(val, field) will return the contents of the bi= tfield @@ -210,15 +210,13 @@ do { \ * * Fields to access are specified as GENMASK() values - an N-bit field * starting at bit #M is encoded as GENMASK(M + N - 1, M). Note that - * bit numbers refer to endianness of the object we are working with - + * bit numbers refer to the value after being converted to host order - * e.g. GENMASK(11, 0) in __be16 refers to the second byte and the lower * 4 bits of the first byte. In __le16 it would refer to the first byte * and the lower 4 bits of the second byte, etc. * - * Field specification must be a constant; __builtin_constant_p() doesn't - * have to be true for it, but compiler must be able to evaluate it at - * build time. If it cannot or if the value does not encode any bitfield, - * the build will fail. + * Field specification must be a non-zero constant, otherwise the build + * will fail. * * If the value being stored in a bitfield is a constant that does not fit * into that bitfield, a warning will be generated at compile time. --=20 2.39.5