All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Pierre-Clément Tosi" <ptosi@google.com>
To: u-boot@lists.denx.de
Cc: "Pierre-Clément Tosi" <ptosi@google.com>,
	"Simon Glass" <sjg@chromium.org>, "Tom Rini" <trini@konsulko.com>
Subject: [PATCH 5/9] include: Import <linux/bits.h> & Update bitops.h
Date: Wed, 16 Mar 2022 15:39:44 +0000	[thread overview]
Message-ID: <20220316153948.197650-5-ptosi@google.com> (raw)
In-Reply-To: <20220316153948.197650-1-ptosi@google.com>

Import the header from version 5.16 of the kernel:

    commit df0cc57e057f18e44dac8e6c18aba47ab53202f9

Inline the included <vdso/bits.h> and prevent U-Boot from including
<asm/bitsperlong.h> as BITS_PER_LONG is defined in <asm/types.h>.

Remove now-duplicate definitions from <linux/bitops.h>.

Note: This brings extra compile-time checks through GENMASK_INPUT_CHECK.

Signed-off-by: Pierre-Clément Tosi <ptosi@google.com>
Cc: Simon Glass <sjg@chromium.org>
Cc: Tom Rini <trini@konsulko.com>
---
 include/linux/bitops.h | 27 ++++++---------------
 include/linux/bits.h   | 55 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 20 deletions(-)
 create mode 100644 include/linux/bits.h

diff --git a/include/linux/bitops.h b/include/linux/bitops.h
index d2e5ca026e..6d465077d6 100644
--- a/include/linux/bitops.h
+++ b/include/linux/bitops.h
@@ -5,37 +5,24 @@
 
 #include <asm/types.h>
 #include <asm-generic/bitsperlong.h>
+#include <linux/bits.h>
 #include <linux/compiler.h>
 
 #ifdef	__KERNEL__
-#define BIT(nr)			(1UL << (nr))
-#define BIT_ULL(nr)		(1ULL << (nr))
-#define BIT_MASK(nr)		(1UL << ((nr) % BITS_PER_LONG))
-#define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
-#define BIT_ULL_MASK(nr)	(1ULL << ((nr) % BITS_PER_LONG_LONG))
-#define BIT_ULL_WORD(nr)	((nr) / BITS_PER_LONG_LONG)
-#define BITS_PER_BYTE		8
 #define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
 #endif
 
 /* kernel.h includes log.h which include bitops.h */
 #include <linux/kernel.h>
 
-/*
- * Create a contiguous bitmask starting at bit position @l and ending at
- * position @h. For example
- * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
- */
 #ifdef CONFIG_SANDBOX
-#define GENMASK(h, l) \
-	(((~0UL) << (l)) & (~0UL >> (CONFIG_SANDBOX_BITS_PER_LONG - 1 - (h))))
-#else
-#define GENMASK(h, l) \
-	(((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
+#ifdef __GENMASK
+#undef __GENMASK
+#endif
+#define __GENMASK(h, l) \
+	(((~UL(0)) - (UL(1) << (l)) + 1) & \
+	 (~UL(0) >> (CONFIG_SANDBOX_BITS_PER_LONG  - 1 - (h))))
 #endif
-
-#define GENMASK_ULL(h, l) \
-	(((~0ULL) << (l)) & (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
 
 /*
  * ffs: find first bit set. This is defined the same way as
diff --git a/include/linux/bits.h b/include/linux/bits.h
new file mode 100644
index 0000000000..04e7dae7f9
--- /dev/null
+++ b/include/linux/bits.h
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_BITS_H
+#define __LINUX_BITS_H
+
+#include <linux/const.h>
+#ifndef __UBOOT__
+#include <vdso/bits.h>
+#else
+#define BIT(nr)                 (UL(1) << (nr))
+#endif
+#ifndef __UBOOT__
+#include <asm/bitsperlong.h>
+#else
+/* U-Boot defines BITS_PER_LONG in <asm/types.h>. */
+#include <asm/types.h>
+#endif
+
+#define BIT_ULL(nr)		(ULL(1) << (nr))
+#define BIT_MASK(nr)		(UL(1) << ((nr) % BITS_PER_LONG))
+#define BIT_WORD(nr)		((nr) / BITS_PER_LONG)
+#define BIT_ULL_MASK(nr)	(ULL(1) << ((nr) % BITS_PER_LONG_LONG))
+#define BIT_ULL_WORD(nr)	((nr) / BITS_PER_LONG_LONG)
+#define BITS_PER_BYTE		8
+
+/*
+ * Create a contiguous bitmask starting at bit position @l and ending at
+ * position @h. For example
+ * GENMASK_ULL(39, 21) gives us the 64bit vector 0x000000ffffe00000.
+ */
+#if !defined(__ASSEMBLY__)
+#include <linux/build_bug.h>
+#define GENMASK_INPUT_CHECK(h, l) \
+	(BUILD_BUG_ON_ZERO(__builtin_choose_expr( \
+		__is_constexpr((l) > (h)), (l) > (h), 0)))
+#else
+/*
+ * BUILD_BUG_ON_ZERO is not available in h files included from asm files,
+ * disable the input check if that is the case.
+ */
+#define GENMASK_INPUT_CHECK(h, l) 0
+#endif
+
+#define __GENMASK(h, l) \
+	(((~UL(0)) - (UL(1) << (l)) + 1) & \
+	 (~UL(0) >> (BITS_PER_LONG - 1 - (h))))
+#define GENMASK(h, l) \
+	(GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l))
+
+#define __GENMASK_ULL(h, l) \
+	(((~ULL(0)) - (ULL(1) << (l)) + 1) & \
+	 (~ULL(0) >> (BITS_PER_LONG_LONG - 1 - (h))))
+#define GENMASK_ULL(h, l) \
+	(GENMASK_INPUT_CHECK(h, l) + __GENMASK_ULL(h, l))
+
+#endif	/* __LINUX_BITS_H */
-- 
2.35.1.723.g4982287a31-goog


  parent reply	other threads:[~2022-03-16 15:41 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-16 15:39 [PATCH 1/9] net: gmac_rockchip: Fix misuse of GENMASK macro Pierre-Clément Tosi
2022-03-16 15:39 ` [PATCH 2/9] lib: crypt: Avoid redefining static_assert Pierre-Clément Tosi
2022-03-16 19:23   ` Simon Glass
2022-03-16 19:41     ` Steffen Jaeckel
2022-03-16 15:39 ` [PATCH 3/9] scripts: Makefile.lib: Pass __UBOOT__ to DTC's CPP Pierre-Clément Tosi
2022-03-16 19:23   ` Simon Glass
2022-03-17 10:42     ` Pierre-Clément Tosi
2022-03-16 15:39 ` [PATCH 4/9] linux/const.h: Upgrade & Merge vDSO and uAPI Pierre-Clément Tosi
2022-03-16 19:23   ` Simon Glass
2022-03-17 11:00     ` Pierre-Clément Tosi
2022-03-28  6:35       ` Simon Glass
2022-03-16 15:39 ` Pierre-Clément Tosi [this message]
2022-03-16 19:23   ` [PATCH 5/9] include: Import <linux/bits.h> & Update bitops.h Simon Glass
2022-03-16 15:39 ` [PATCH 6/9] include: Carve <linux/export.h> out of compat.h Pierre-Clément Tosi
2022-03-16 19:23   ` Simon Glass
2022-03-16 15:39 ` [PATCH 7/9] include: Upgrade <linux/typecheck.h> Pierre-Clément Tosi
2022-03-16 19:23   ` Simon Glass
2022-03-17 11:46     ` Pierre-Clément Tosi
2022-03-28  6:35       ` Simon Glass
2022-03-16 15:39 ` [PATCH 8/9] arm64: Import <asm/sysreg.h> from Linux Pierre-Clément Tosi
2022-03-16 15:39 ` [PATCH 9/9] arm64: Import <asm/esr.h> " Pierre-Clément Tosi
2022-03-16 19:47   ` Sean Anderson
2022-03-17 17:39     ` Pierre-Clément Tosi
2022-03-22 20:39       ` Sean Anderson
2022-03-17 19:13 ` [PATCH 1/9] net: gmac_rockchip: Fix misuse of GENMASK macro Ramon Fried
2022-04-06 15:08 ` Kever Yang
2022-04-07 12:28   ` David Wu

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=20220316153948.197650-5-ptosi@google.com \
    --to=ptosi@google.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.