From: "Yury Norov (NVIDIA)" <yury.norov@gmail.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
Linus Walleij <linus.walleij@linaro.org>,
Nicolas Frattaroli <nicolas.frattaroli@collabora.com>,
Yury Norov <yury.norov@gmail.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
linux-kernel@vger.kernel.org
Subject: [PATCH 07/21] bits: Add BITS() macro
Date: Sat, 25 Oct 2025 12:28:43 -0400 [thread overview]
Message-ID: <20251025162858.305236-8-yury.norov@gmail.com> (raw)
In-Reply-To: <20251025162858.305236-1-yury.norov@gmail.com>
The BITS(low, high) macro is preferable over a similar GENMASK(high, low)
because (low, high) parameters order is more natural. The (high, low)
order is confusing and has a record of misuse.
To enforce unintuitive parameters order, GENMASK() is enforced with
compile time checks. In addition, fixed-width versions of GENMASK() had
been developed. They make sense in describing hardware registers.
In generic code, using standard ordering (low to high) is more preferable,
and fixed-width features are not that useful.
In non-driver code, BITS() must be taken over GENMASK(). In drivers code,
BITS() is preferable over GENMASK().
The following pattern of using GENMASK() is highly unfavorable:
/* Status register (SR) */
#define I2C_SR_OP GENMASK(1, 0) /* Operation */
#define I2C_SR_STATUS GENMASK(3, 2) /* controller status */
#define I2C_SR_CAUSE GENMASK(6, 4) /* Abort cause */
#define I2C_SR_TYPE GENMASK(8, 7) /* Receive type */
#define I2C_SR_LENGTH GENMASK(19, 9) /* Transfer length */
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://lore.kernel.org/all/CAHk-=whoOUsqPKb7OQwhQf9H_3=5sXGPJrDbfQfwLB3Bi13tcQ@mail.gmail.com/
Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
---
include/linux/bits.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/include/linux/bits.h b/include/linux/bits.h
index a40cc861b3a7..c7c587e90e2d 100644
--- a/include/linux/bits.h
+++ b/include/linux/bits.h
@@ -57,6 +57,9 @@
#define GENMASK_U64(h, l) GENMASK_TYPE(u64, h, l)
#define GENMASK_U128(h, l) GENMASK_TYPE(u128, h, l)
+#define BITS(l, h) GENMASK(h, l)
+#define BITS_ULL(l, h) GENMASK_ULL(h, l)
+
/*
* Fixed-type variants of BIT(), with additional checks like GENMASK_TYPE(). The
* following examples generate compiler warnings due to -Wshift-count-overflow:
--
2.43.0
next prev parent reply other threads:[~2025-10-25 16:29 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-25 16:28 [PATCH 00/21] lib: add alternatives for GENMASK() Yury Norov (NVIDIA)
2025-10-25 16:28 ` [PATCH 01/21] arc: disasm: rename BITS() for FIELD() Yury Norov (NVIDIA)
2025-10-25 16:28 ` [PATCH 02/21] iwlwifi: drop unused BITS() Yury Norov (NVIDIA)
2025-10-25 16:28 ` [PATCH 03/21] select: rename BITS() to FDS_BITS() Yury Norov (NVIDIA)
2025-10-25 16:28 ` [PATCH 04/21] ALSA: rename BITS to R_BITS Yury Norov (NVIDIA)
2025-10-27 9:09 ` Takashi Iwai
2025-10-25 16:28 ` [PATCH 05/21] zlib: rename BITS() to LOWBITS() Yury Norov (NVIDIA)
2025-10-25 16:28 ` [PATCH 06/21] mfd: prepare to generalize BITS() macro Yury Norov (NVIDIA)
2025-11-06 16:48 ` Lee Jones
2025-10-25 16:28 ` Yury Norov (NVIDIA) [this message]
2025-10-25 16:32 ` [PATCH 08/21] mfd: drop local " Yury Norov (NVIDIA)
2025-10-27 22:21 ` Linus Walleij
2025-10-25 16:32 ` [PATCH 09/21] bits: generalize BITMAP_{FIRST,LAST}_WORD_MASK Yury Norov (NVIDIA)
2025-10-25 16:32 ` [PATCH 10/21] i2c: nomadik: don't use GENMASK() Yury Norov (NVIDIA)
2025-10-27 22:22 ` Linus Walleij
2025-10-25 16:32 ` [PATCH 11/21] drivers: don't use GENMASK() in FIELD_PREP_WM16() Yury Norov (NVIDIA)
2025-10-25 16:32 ` [PATCH 12/21] bitmap: don't use GENMASK() Yury Norov (NVIDIA)
2025-10-25 16:32 ` [PATCH 13/21] trace: " Yury Norov (NVIDIA)
2025-10-25 19:29 ` Steven Rostedt
2025-10-25 19:36 ` Steven Rostedt
2025-10-25 16:32 ` [PATCH 14/21] lib: 842: don't use GENMASK_ULL() Yury Norov (NVIDIA)
2025-10-25 16:32 ` [PATCH 15/21] bpf: don't use GENMASK() Yury Norov (NVIDIA)
2025-10-25 16:32 ` [PATCH 16/21] kcsan: " Yury Norov (NVIDIA)
2025-10-25 16:32 ` [PATCH 17/21] mm: " Yury Norov (NVIDIA)
2025-10-25 16:33 ` [PATCH 18/21] fprobe: " Yury Norov (NVIDIA)
2025-10-25 16:33 ` [PATCH 19/21] fs: " Yury Norov (NVIDIA)
2025-10-25 16:33 ` [PATCH 20/21] fortify-string: " Yury Norov (NVIDIA)
2025-10-25 16:33 ` [PATCH 21/21] Docs: add Functions parameters order section Yury Norov (NVIDIA)
2025-10-27 9:02 ` Jani Nikula
2025-10-27 16:08 ` Jeff Johnson
2025-10-27 18:22 ` Andi Shyti
2025-10-27 18:43 ` Randy Dunlap
2025-10-25 16:37 ` [PATCH 00/21] lib: add alternatives for GENMASK() Yury Norov
2025-10-25 16:39 Yury Norov (NVIDIA)
2025-10-25 16:40 ` [PATCH 07/21] bits: Add BITS() macro Yury Norov (NVIDIA)
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=20251025162858.305236-8-yury.norov@gmail.com \
--to=yury.norov@gmail.com \
--cc=linus.walleij@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=nicolas.frattaroli@collabora.com \
--cc=torvalds@linux-foundation.org \
/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®