From nobody Sun Dec 14 12:12:45 2025 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 B732E30FF3A for ; Fri, 12 Dec 2025 20:15:26 +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=1765570528; cv=none; b=i12knvR6OCXr9AkKiA/VvzmMMTEe3K4jR14G/tMPfbQQTFXzI+y7kNvz73GwFSiGEXkGMefbpjHzCZQv9YxO1d0sAz0bPs1KjQiUGFi6hecmd8SRTfkdC3vXoGWn+3806SVqeMYVuKktTVi/mr/ogmKNaWCnn3WfaacpYlZOApk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765570528; c=relaxed/simple; bh=Ccr1EarAMFaJl6TVejUdtnp6jxA9i93IxK5KG/WKPVo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s9UUKwPuEEZdNty4AqWr763QSvWoStBkUSv4xpUR0ODKhHeC61Fhknh7C0QoNzmfULwAUNY7jB53INC21Iip1xzJTDtgilo34pqB2SeMebsSCqnehdotFabahj6gQIksG8p8F/rJDhUnu6cell67QuBpGuwN7x4oX6liup+diVA= 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=qzp4HEFr; 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="qzp4HEFr" 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 1vU8xv-0087bv-Ca; Fri, 12 Dec 2025 20:38:11 +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=6+sXrUr5LvBqW8hHD2oULe/kxjAvnPiwR/5Jz/SJlSY=; b=qzp4HEFrE9mNFqiaaneQOvR2Bh mIGWY/Vbrpdzq8LWo5aPKldFXJ5VE5en4WQybR+PgzmrXFipuNHFCDItilEJ74OvVA5lfEwtl/Wht mXgRil8IKGcf/tvxRwwekGJswfmJVq6dG6ZBr1we6n0oinQPkaErGd/VIPVklOgL35wxEMeclCtnG vamzhonZWS7+29s3CvoddOh2Y5ZYVJAg24efZxAm4lsYsAifVS3WtPnyX9nc8yLK9g+umVOeNu4pt v23EHn44cACRuiy47aWRme6OPpz2YHyBE3vlom4/QJ5fHIC5kv+ggi6zXP/2Q8GHUJR8ObepWnRN+ viGskkcA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vU8xv-0006un-0k; Fri, 12 Dec 2025 20:38:11 +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 1vU8xk-0030pR-1q; Fri, 12 Dec 2025 20:38:00 +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" , Mika Westerberg , Andreas Noever , Yehezkel Bernat , Nicolas Frattaroli Cc: David Laight Subject: [PATCH v2 11/16] bitfield: Common up validation of the mask parameter Date: Fri, 12 Dec 2025 19:37:16 +0000 Message-Id: <20251212193721.740055-12-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251212193721.740055-1-david.laight.linux@gmail.com> References: <20251212193721.740055-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 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. Signed-off-by: David Laight --- include/linux/bitfield.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h index 138f4c14786d..6f454ef43d24 100644 --- a/include/linux/bitfield.h +++ b/include/linux/bitfield.h @@ -45,13 +45,15 @@ =20 #define __bf_shf(x) (__builtin_ffsll(x) - 1) =20 +#define __BF_VALIDATE_MASK(mask) \ + (!(mask) || ((mask) & ((mask) + ((mask) & -(mask))))) + #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_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) =20 #define __BF_FIELD_CHECK_VAL(mask, val, pfx) \ @@ -122,8 +124,6 @@ __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 @@ -138,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)) \ ) @@ -189,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