* [PATCH 2/8] treewide: fix dependency on indirectly included linux/kernel.h
2024-07-16 11:58 [PATCH 0/8] include: list: remove dependency on linux/kernel.h Ahmad Fatoum
2024-07-16 11:58 ` [PATCH 1/8] regmap-mmio: don't call kfree on non kmalloc allocated buffers Ahmad Fatoum
@ 2024-07-16 11:58 ` Ahmad Fatoum
2024-07-16 11:58 ` [PATCH 3/8] stringlist: make header self-contained Ahmad Fatoum
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Ahmad Fatoum @ 2024-07-16 11:58 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
We currently include <linux/kernel.h> in <linux/list.h>, which pulls in
definitions that many source files depend on.
Include <linux/kernel.h> directly or other more specific files if
available in preparation for removing the <linux/kernel.h> include from
<linux/list.h>.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
arch/arm/lib32/io.c | 1 +
arch/mips/include/asm/dma.h | 1 +
commands/bthread.c | 1 +
commands/mmc.c | 1 +
drivers/clk/at91/at91sam9260.c | 1 +
drivers/clk/at91/at91sam9g45.c | 1 +
drivers/clk/at91/at91sam9n12.c | 1 +
drivers/clk/at91/at91sam9rl.c | 1 +
drivers/clk/at91/at91sam9x5.c | 1 +
drivers/clk/at91/clk-audio-pll.c | 1 +
drivers/clk/at91/clk-generated.c | 1 +
drivers/clk/at91/clk-master.c | 1 +
drivers/clk/at91/clk-pll.c | 1 +
drivers/clk/at91/clk-usb.c | 1 +
drivers/clk/at91/sama5d2.c | 1 +
drivers/clk/at91/sama5d3.c | 1 +
drivers/clk/at91/sama5d4.c | 1 +
drivers/clk/rockchip/rst-rk3588.c | 1 +
drivers/soc/sifive/sifive_l2_cache.c | 2 ++
drivers/usb/misc/onboard_usb_hub.c | 1 +
include/linux/mtd/nand.h | 1 +
include/mach/mvebu/debug_ll.h | 1 +
lib/idr.c | 2 ++
23 files changed, 25 insertions(+)
diff --git a/arch/arm/lib32/io.c b/arch/arm/lib32/io.c
index 780b1083a641..9e3acfe02246 100644
--- a/arch/arm/lib32/io.c
+++ b/arch/arm/lib32/io.c
@@ -3,6 +3,7 @@
#include <module.h>
#include <linux/types.h>
#include <asm/unaligned.h>
+#include <linux/align.h>
#include <io.h>
/*
diff --git a/arch/mips/include/asm/dma.h b/arch/mips/include/asm/dma.h
index dbcbfa40768d..46fae14ae5aa 100644
--- a/arch/mips/include/asm/dma.h
+++ b/arch/mips/include/asm/dma.h
@@ -8,6 +8,7 @@
#include <linux/pagemap.h>
#include <linux/types.h>
+#include <linux/minmax.h>
#include <malloc.h>
#include <xfuncs.h>
#include <asm/addrspace.h>
diff --git a/commands/bthread.c b/commands/bthread.c
index aaade46e9206..00ff1837cc69 100644
--- a/commands/bthread.c
+++ b/commands/bthread.c
@@ -11,6 +11,7 @@
#include <getopt.h>
#include <clock.h>
#include <slice.h>
+#include <linux/kernel.h>
static int bthread_time(void)
{
diff --git a/commands/mmc.c b/commands/mmc.c
index 041a721d3661..718558f18ba7 100644
--- a/commands/mmc.c
+++ b/commands/mmc.c
@@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <getopt.h>
+#include <linux/kernel.h>
#include <dma.h>
static int mmc_enh_area_setmax(struct mci *mci, u8 *ext_csd)
diff --git a/drivers/clk/at91/at91sam9260.c b/drivers/clk/at91/at91sam9260.c
index c94cd9556616..b590dc629855 100644
--- a/drivers/clk/at91/at91sam9260.c
+++ b/drivers/clk/at91/at91sam9260.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/at91/at91sam9g45.c b/drivers/clk/at91/at91sam9g45.c
index fedf96139348..3d2197c7f288 100644
--- a/drivers/clk/at91/at91sam9g45.c
+++ b/drivers/clk/at91/at91sam9g45.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/at91/at91sam9n12.c b/drivers/clk/at91/at91sam9n12.c
index bb075de9fde6..3899242a33de 100644
--- a/drivers/clk/at91/at91sam9n12.c
+++ b/drivers/clk/at91/at91sam9n12.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/at91/at91sam9rl.c b/drivers/clk/at91/at91sam9rl.c
index 95b02d86d596..a671c6be541e 100644
--- a/drivers/clk/at91/at91sam9rl.c
+++ b/drivers/clk/at91/at91sam9rl.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/at91/at91sam9x5.c b/drivers/clk/at91/at91sam9x5.c
index f4dc7ceeea9f..e351848bf7a7 100644
--- a/drivers/clk/at91/at91sam9x5.c
+++ b/drivers/clk/at91/at91sam9x5.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/at91/clk-audio-pll.c b/drivers/clk/at91/clk-audio-pll.c
index 71976567ea7f..019d68b7b7e7 100644
--- a/drivers/clk/at91/clk-audio-pll.c
+++ b/drivers/clk/at91/clk-audio-pll.c
@@ -38,6 +38,7 @@
#include <mfd/syscon.h>
#include <linux/regmap.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include "pmc.h"
diff --git a/drivers/clk/at91/clk-generated.c b/drivers/clk/at91/clk-generated.c
index e59cff2bdf9f..807c8179c0f1 100644
--- a/drivers/clk/at91/clk-generated.c
+++ b/drivers/clk/at91/clk-generated.c
@@ -14,6 +14,7 @@
#include <of.h>
#include <mfd/syscon.h>
#include <linux/regmap.h>
+#include <linux/kernel.h>
#include "pmc.h"
diff --git a/drivers/clk/at91/clk-master.c b/drivers/clk/at91/clk-master.c
index db5e235b6b8f..ed358257dcb6 100644
--- a/drivers/clk/at91/clk-master.c
+++ b/drivers/clk/at91/clk-master.c
@@ -11,6 +11,7 @@
#include <mfd/syscon.h>
#include <linux/regmap.h>
#include <linux/printk.h>
+#include <linux/kernel.h>
#include "pmc.h"
diff --git a/drivers/clk/at91/clk-pll.c b/drivers/clk/at91/clk-pll.c
index 027e1fc77349..917b2224fe19 100644
--- a/drivers/clk/at91/clk-pll.c
+++ b/drivers/clk/at91/clk-pll.c
@@ -9,6 +9,7 @@
#include <of.h>
#include <mfd/syscon.h>
#include <linux/regmap.h>
+#include <linux/kernel.h>
#include "pmc.h"
diff --git a/drivers/clk/at91/clk-usb.c b/drivers/clk/at91/clk-usb.c
index 4473dc7c3491..96a35b5cb09e 100644
--- a/drivers/clk/at91/clk-usb.c
+++ b/drivers/clk/at91/clk-usb.c
@@ -9,6 +9,7 @@
#include <of.h>
#include <mfd/syscon.h>
#include <linux/regmap.h>
+#include <linux/kernel.h>
#include "pmc.h"
diff --git a/drivers/clk/at91/sama5d2.c b/drivers/clk/at91/sama5d2.c
index 96c0d1f6a46d..cb4e1b78a5e3 100644
--- a/drivers/clk/at91/sama5d2.c
+++ b/drivers/clk/at91/sama5d2.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/at91/sama5d3.c b/drivers/clk/at91/sama5d3.c
index 53a1a7413a02..4212afb21827 100644
--- a/drivers/clk/at91/sama5d3.c
+++ b/drivers/clk/at91/sama5d3.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/at91/sama5d4.c b/drivers/clk/at91/sama5d4.c
index 8fbd81088349..bd02bdc915f5 100644
--- a/drivers/clk/at91/sama5d4.c
+++ b/drivers/clk/at91/sama5d4.c
@@ -2,6 +2,7 @@
#include <linux/clk-provider.h>
#include <mfd/syscon.h>
#include <linux/slab.h>
+#include <linux/kernel.h>
#include <stdio.h>
#include <dt-bindings/clock/at91.h>
diff --git a/drivers/clk/rockchip/rst-rk3588.c b/drivers/clk/rockchip/rst-rk3588.c
index 7501b92b45f7..fdc83cefc7ad 100644
--- a/drivers/clk/rockchip/rst-rk3588.c
+++ b/drivers/clk/rockchip/rst-rk3588.c
@@ -6,6 +6,7 @@
*/
#include <dt-bindings/reset/rockchip,rk3588-cru.h>
+#include <linux/array_size.h>
#include "clk.h"
/* 0xFD7C0000 + 0x0A00 */
diff --git a/drivers/soc/sifive/sifive_l2_cache.c b/drivers/soc/sifive/sifive_l2_cache.c
index c404143974fc..239e65292ae2 100644
--- a/drivers/soc/sifive/sifive_l2_cache.c
+++ b/drivers/soc/sifive/sifive_l2_cache.c
@@ -16,7 +16,9 @@
#include <init.h>
#include <soc/sifive/l2_cache.h>
#include <asm/barrier.h>
+#include <linux/align.h>
#include <linux/bitops.h>
+#include <linux/bug.h>
#define SIFIVE_L2_DIRECCFIX_LOW 0x100
#define SIFIVE_L2_DIRECCFIX_HIGH 0x104
diff --git a/drivers/usb/misc/onboard_usb_hub.c b/drivers/usb/misc/onboard_usb_hub.c
index 9e94caaa8456..603fd693a137 100644
--- a/drivers/usb/misc/onboard_usb_hub.c
+++ b/drivers/usb/misc/onboard_usb_hub.c
@@ -12,6 +12,7 @@
#include <linux/printk.h>
#include <of_device.h>
#include <regulator.h>
+#include <xfuncs.h>
#include <linux/usb/usb.h>
#include "onboard_usb_hub.h"
diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
index ef2546490d65..fda55df59a26 100644
--- a/include/linux/mtd/nand.h
+++ b/include/linux/mtd/nand.h
@@ -11,6 +11,7 @@
#define __LINUX_MTD_NAND_H
#include <linux/mtd/mtd.h>
+#include <linux/minmax.h>
struct nand_device;
diff --git a/include/mach/mvebu/debug_ll.h b/include/mach/mvebu/debug_ll.h
index 9197cd68eb99..cf08776d4fcc 100644
--- a/include/mach/mvebu/debug_ll.h
+++ b/include/mach/mvebu/debug_ll.h
@@ -5,6 +5,7 @@
#define __MACH_MVEBU_DEBUG_LL_H__
#include <io.h>
+#include <linux/bits.h>
#define UART_BASE 0xf1012000
#define UARTn_BASE(n) (UART_BASE + ((n) * 0x100))
diff --git a/lib/idr.c b/lib/idr.c
index 10a714ac03f0..a25e46b17b95 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -6,6 +6,8 @@
#include <errno.h>
#include <linux/idr.h>
+#include <malloc.h>
+#include <linux/minmax.h>
struct idr *__idr_find(struct idr *head, int lookup_id)
{
--
2.39.2
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 7/8] include: linux/kernel.h: split off into multiple headers
2024-07-16 11:58 [PATCH 0/8] include: list: remove dependency on linux/kernel.h Ahmad Fatoum
` (5 preceding siblings ...)
2024-07-16 11:58 ` [PATCH 6/8] include: asm-generic/bug: break recursive dependency Ahmad Fatoum
@ 2024-07-16 11:58 ` Ahmad Fatoum
2024-07-16 11:58 ` [PATCH 8/8] include: list: replace full kernel.h inclusion with smaller container_of.h Ahmad Fatoum
2024-07-19 6:31 ` [PATCH 0/8] include: list: remove dependency on linux/kernel.h Sascha Hauer
8 siblings, 0 replies; 10+ messages in thread
From: Ahmad Fatoum @ 2024-07-16 11:58 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum
The upstream version in Linux has successively factored out parts into
newly created headers for specific functions. What remains in
<linux/kernel.h> is mostly ftrace and annotations like might_sleep(),
which we don't define in <linux/kernel.h>.
Let's follow suit and factor out everything into headers. The intention
is that other headers won't include <linux/kernel.h>, but one of the
more specific headers and that way we avoid recursive dependencies
and reduce parse time.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
include/linux/array_size.h | 14 +++
include/linux/hex.h | 34 ++++++
include/linux/kernel.h | 234 +------------------------------------
include/linux/kstrtox.h | 123 +++++++++++++++++++
include/linux/math.h | 83 +++++++++++++
include/linux/wordpart.h | 49 ++++++++
6 files changed, 308 insertions(+), 229 deletions(-)
create mode 100644 include/linux/array_size.h
create mode 100644 include/linux/hex.h
create mode 100644 include/linux/kstrtox.h
create mode 100644 include/linux/math.h
create mode 100644 include/linux/wordpart.h
diff --git a/include/linux/array_size.h b/include/linux/array_size.h
new file mode 100644
index 000000000000..5411d730c819
--- /dev/null
+++ b/include/linux/array_size.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_ARRAY_SIZE_H
+#define _LINUX_ARRAY_SIZE_H
+
+#include <linux/compiler.h>
+
+/**
+ * ARRAY_SIZE - get the number of elements in array @arr
+ * @arr: array to be sized
+ */
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
+#define ARRAY_AND_SIZE(x) (x), ARRAY_SIZE(x)
+
+#endif /* _LINUX_ARRAY_SIZE_H */
diff --git a/include/linux/hex.h b/include/linux/hex.h
new file mode 100644
index 000000000000..b77ed6d5441f
--- /dev/null
+++ b/include/linux/hex.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_HEX_H
+#define _LINUX_HEX_H
+
+#include <linux/types.h>
+#include <linux/compiler.h>
+
+extern const char hex_asc[];
+#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
+#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
+
+static inline char *hex_byte_pack(char *buf, u8 byte)
+{
+ *buf++ = hex_asc_hi(byte);
+ *buf++ = hex_asc_lo(byte);
+ return buf;
+}
+
+extern const char hex_asc_upper[];
+#define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
+#define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
+
+static inline char *hex_byte_pack_upper(char *buf, u8 byte)
+{
+ *buf++ = hex_asc_upper_hi(byte);
+ *buf++ = hex_asc_upper_lo(byte);
+ return buf;
+}
+
+extern int hex_to_bin(char ch);
+extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
+extern char *bin2hex(char *dst, const void *src, size_t count);
+
+#endif
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index dd108ba0abf7..fbb3cfe844cb 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -3,241 +3,17 @@
#define _LINUX_KERNEL_H
#include <linux/compiler.h>
+#include <linux/array_size.h>
#include <linux/bug.h>
#include <linux/barebox-wrapper.h>
#include <linux/limits.h>
-#include <linux/math64.h>
+#include <linux/math.h>
#include <linux/container_of.h>
+#include <linux/wordpart.h>
#include <linux/instruction_pointer.h>
#include <linux/minmax.h>
+#include <linux/kstrtox.h>
#include <linux/align.h>
-
-/**
- * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
- * @x: value to repeat
- *
- * NOTE: @x is not checked for > 0xff; larger values produce odd results.
- */
-#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
-
-#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
-#define ARRAY_AND_SIZE(x) (x), ARRAY_SIZE(x)
-
-/*
- * This looks more complex than it should be. But we need to
- * get the type for the ~ right in round_down (it needs to be
- * as wide as the result!), and we want to evaluate the macro
- * arguments just once each.
- *
- * NOTE these functions only round to power-of-2 arguments. Use
- * roundup/rounddown for non power-of-2-arguments.
- */
-#define __round_mask(x, y) ((__typeof__(x))((y)-1))
-#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
-#define round_down(x, y) ((x) & ~__round_mask(x, y))
-
-#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
-
-#define DIV_ROUND_DOWN_ULL(ll, d) \
- ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
-
-#define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
-
-#define DIV_ROUND_CLOSEST(x, divisor)( \
-{ \
- typeof(divisor) __divisor = divisor; \
- (((x) + ((__divisor) / 2)) / (__divisor)); \
-} \
-)
-/*
- * Same as above but for u64 dividends. divisor must be a 32-bit
- * number.
- */
-#define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
-{ \
- typeof(divisor) __d = divisor; \
- unsigned long long _tmp = (x) + (__d) / 2; \
- do_div(_tmp, __d); \
- _tmp; \
-} \
-)
-
-/**
- * upper_32_bits - return bits 32-63 of a number
- * @n: the number we're accessing
- *
- * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
- * the "right shift count >= width of type" warning when that quantity is
- * 32-bits.
- */
-#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
-
-/**
- * lower_32_bits - return bits 0-31 of a number
- * @n: the number we're accessing
- */
-#define lower_32_bits(n) ((u32)(n))
-
-#define abs(x) ({ \
- long __x = (x); \
- (__x < 0) ? -__x : __x; \
- })
-
-#define abs64(x) ({ \
- s64 __x = (x); \
- (__x < 0) ? -__x : __x; \
- })
-
-extern unsigned long simple_strtoul(const char *,char **,unsigned int);
-extern long simple_strtol(const char *,char **,unsigned int);
-extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
-extern long long simple_strtoll(const char *,char **,unsigned int);
-extern s64 simple_strtofract(const char *cp, char **endp, u32 division);
-
-/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
-#define roundup(x, y) ( \
-{ \
- const typeof(y) __y = y; \
- (((x) + (__y - 1)) / __y) * __y; \
-} \
-)
-#define rounddown(x, y) ( \
-{ \
- typeof(x) __x = (x); \
- __x - (__x % (y)); \
-} \
-)
-
-/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
-#define mult_frac(x, n, d) \
-({ \
- typeof(x) x_ = (x); \
- typeof(n) n_ = (n); \
- typeof(d) d_ = (d); \
- \
- typeof(x_) q = x_ / d_; \
- typeof(x_) r = x_ % d_; \
- q * n_ + r * n_ / d_; \
-})
-
-extern const char hex_asc[];
-#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
-#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
-
-static inline char *hex_byte_pack(char *buf, u8 byte)
-{
- *buf++ = hex_asc_hi(byte);
- *buf++ = hex_asc_lo(byte);
- return buf;
-}
-
-extern const char hex_asc_upper[];
-#define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
-#define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
-
-static inline char *hex_byte_pack_upper(char *buf, u8 byte)
-{
- *buf++ = hex_asc_upper_hi(byte);
- *buf++ = hex_asc_upper_lo(byte);
- return buf;
-}
-
-extern int hex_to_bin(char ch);
-extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
-extern char *bin2hex(char *dst, const void *src, size_t count);
-
-/* Internal, do not use. */
-int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
-int __must_check _kstrtol(const char *s, unsigned int base, long *res);
-
-int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
-int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
-
-/**
- * kstrtoul - convert a string to an unsigned long
- * @s: The start of the string. The string must be null-terminated, and may also
- * include a single newline before its terminating null. The first character
- * may also be a plus sign, but not a minus sign.
- * @base: The number base to use. The maximum supported base is 16. If base is
- * given as 0, then the base of the string is automatically detected with the
- * conventional semantics - If it begins with 0x the number will be parsed as a
- * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
- * parsed as an octal number. Otherwise it will be parsed as a decimal.
- * @res: Where to write the result of the conversion on success.
- *
- * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
- * Used as a replacement for the obsolete simple_strtoull. Return code must
- * be checked.
-*/
-static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
-{
- /*
- * We want to shortcut function call, but
- * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
- */
- if (sizeof(unsigned long) == sizeof(unsigned long long) &&
- __alignof__(unsigned long) == __alignof__(unsigned long long))
- return kstrtoull(s, base, (unsigned long long *)res);
- else
- return _kstrtoul(s, base, res);
-}
-
-/**
- * kstrtol - convert a string to a long
- * @s: The start of the string. The string must be null-terminated, and may also
- * include a single newline before its terminating null. The first character
- * may also be a plus sign or a minus sign.
- * @base: The number base to use. The maximum supported base is 16. If base is
- * given as 0, then the base of the string is automatically detected with the
- * conventional semantics - If it begins with 0x the number will be parsed as a
- * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
- * parsed as an octal number. Otherwise it will be parsed as a decimal.
- * @res: Where to write the result of the conversion on success.
- *
- * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
- * Used as a replacement for the obsolete simple_strtoull. Return code must
- * be checked.
- */
-static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
-{
- /*
- * We want to shortcut function call, but
- * __builtin_types_compatible_p(long, long long) = 0.
- */
- if (sizeof(long) == sizeof(long long) &&
- __alignof__(long) == __alignof__(long long))
- return kstrtoll(s, base, (long long *)res);
- else
- return _kstrtol(s, base, res);
-}
-
-int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
-int __must_check kstrtoint(const char *s, unsigned int base, int *res);
-
-static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
-{
- return kstrtoull(s, base, res);
-}
-
-static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
-{
- return kstrtoll(s, base, res);
-}
-
-static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
-{
- return kstrtouint(s, base, res);
-}
-
-static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
-{
- return kstrtoint(s, base, res);
-}
-
-int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
-int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
-int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
-int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
-int __must_check kstrtobool(const char *s, bool *res);
+#include <linux/hex.h>
#endif /* _LINUX_KERNEL_H */
diff --git a/include/linux/kstrtox.h b/include/linux/kstrtox.h
new file mode 100644
index 000000000000..51493c71292b
--- /dev/null
+++ b/include/linux/kstrtox.h
@@ -0,0 +1,123 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_KSTRTOX_H
+#define _LINUX_KSTRTOX_H
+
+#include <linux/types.h>
+#include <linux/compiler.h>
+
+/* Internal, do not use. */
+int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
+int __must_check _kstrtol(const char *s, unsigned int base, long *res);
+
+int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
+int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
+
+/**
+ * kstrtoul - convert a string to an unsigned long
+ * @s: The start of the string. The string must be null-terminated, and may also
+ * include a single newline before its terminating null. The first character
+ * may also be a plus sign, but not a minus sign.
+ * @base: The number base to use. The maximum supported base is 16. If base is
+ * given as 0, then the base of the string is automatically detected with the
+ * conventional semantics - If it begins with 0x the number will be parsed as a
+ * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
+ * parsed as an octal number. Otherwise it will be parsed as a decimal.
+ * @res: Where to write the result of the conversion on success.
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoull. Return code must
+ * be checked.
+*/
+static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
+{
+ /*
+ * We want to shortcut function call, but
+ * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
+ */
+ if (sizeof(unsigned long) == sizeof(unsigned long long) &&
+ __alignof__(unsigned long) == __alignof__(unsigned long long))
+ return kstrtoull(s, base, (unsigned long long *)res);
+ else
+ return _kstrtoul(s, base, res);
+}
+
+/**
+ * kstrtol - convert a string to a long
+ * @s: The start of the string. The string must be null-terminated, and may also
+ * include a single newline before its terminating null. The first character
+ * may also be a plus sign or a minus sign.
+ * @base: The number base to use. The maximum supported base is 16. If base is
+ * given as 0, then the base of the string is automatically detected with the
+ * conventional semantics - If it begins with 0x the number will be parsed as a
+ * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
+ * parsed as an octal number. Otherwise it will be parsed as a decimal.
+ * @res: Where to write the result of the conversion on success.
+ *
+ * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
+ * Used as a replacement for the obsolete simple_strtoull. Return code must
+ * be checked.
+ */
+static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
+{
+ /*
+ * We want to shortcut function call, but
+ * __builtin_types_compatible_p(long, long long) = 0.
+ */
+ if (sizeof(long) == sizeof(long long) &&
+ __alignof__(long) == __alignof__(long long))
+ return kstrtoll(s, base, (long long *)res);
+ else
+ return _kstrtol(s, base, res);
+}
+
+int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
+int __must_check kstrtoint(const char *s, unsigned int base, int *res);
+
+static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
+{
+ return kstrtoull(s, base, res);
+}
+
+static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
+{
+ return kstrtoll(s, base, res);
+}
+
+static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
+{
+ return kstrtouint(s, base, res);
+}
+
+static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
+{
+ return kstrtoint(s, base, res);
+}
+
+int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
+int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
+int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
+int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
+int __must_check kstrtobool(const char *s, bool *res);
+
+
+/*
+ * Use kstrto<foo> instead.
+ *
+ * NOTE: simple_strto<foo> does not check for the range overflow and,
+ * depending on the input, may give interesting results.
+ *
+ * Use these functions if and only if you cannot use kstrto<foo>, because
+ * the conversion ends on the first non-digit character, which may be far
+ * beyond the supported range. It might be useful to parse the strings like
+ * 10x50 or 12:21 without altering original string or temporary buffer in use.
+ * Keep in mind above caveat.
+ */
+
+extern unsigned long simple_strtoul(const char *,char **,unsigned int);
+extern long simple_strtol(const char *,char **,unsigned int);
+extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
+extern long long simple_strtoll(const char *,char **,unsigned int);
+
+extern s64 simple_strtofract(const char *cp, char **endp, u32 division);
+
+#endif
diff --git a/include/linux/math.h b/include/linux/math.h
new file mode 100644
index 000000000000..acbffa96e6eb
--- /dev/null
+++ b/include/linux/math.h
@@ -0,0 +1,83 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_MATH_H
+#define _LINUX_MATH_H
+
+#include <linux/types.h>
+#include <linux/math64.h>
+
+/*
+ * This looks more complex than it should be. But we need to
+ * get the type for the ~ right in round_down (it needs to be
+ * as wide as the result!), and we want to evaluate the macro
+ * arguments just once each.
+ *
+ * NOTE these functions only round to power-of-2 arguments. Use
+ * roundup/rounddown for non power-of-2-arguments.
+ */
+#define __round_mask(x, y) ((__typeof__(x))((y)-1))
+#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
+#define round_down(x, y) ((x) & ~__round_mask(x, y))
+
+#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
+
+#define DIV_ROUND_DOWN_ULL(ll, d) \
+ ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
+
+#define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
+
+#define DIV_ROUND_CLOSEST(x, divisor)( \
+{ \
+ typeof(divisor) __divisor = divisor; \
+ (((x) + ((__divisor) / 2)) / (__divisor)); \
+} \
+)
+/*
+ * Same as above but for u64 dividends. divisor must be a 32-bit
+ * number.
+ */
+#define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
+{ \
+ typeof(divisor) __d = divisor; \
+ unsigned long long _tmp = (x) + (__d) / 2; \
+ do_div(_tmp, __d); \
+ _tmp; \
+} \
+)
+
+/* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
+#define roundup(x, y) ( \
+{ \
+ const typeof(y) __y = y; \
+ (((x) + (__y - 1)) / __y) * __y; \
+} \
+)
+#define rounddown(x, y) ( \
+{ \
+ typeof(x) __x = (x); \
+ __x - (__x % (y)); \
+} \
+)
+
+/* Calculate "x * n / d" without unnecessary overflow or loss of precision. */
+#define mult_frac(x, n, d) \
+({ \
+ typeof(x) x_ = (x); \
+ typeof(n) n_ = (n); \
+ typeof(d) d_ = (d); \
+ \
+ typeof(x_) q = x_ / d_; \
+ typeof(x_) r = x_ % d_; \
+ q * n_ + r * n_ / d_; \
+})
+
+#define abs(x) ({ \
+ long __x = (x); \
+ (__x < 0) ? -__x : __x; \
+ })
+
+#define abs64(x) ({ \
+ s64 __x = (x); \
+ (__x < 0) ? -__x : __x; \
+ })
+
+#endif
diff --git a/include/linux/wordpart.h b/include/linux/wordpart.h
new file mode 100644
index 000000000000..72a83685c95d
--- /dev/null
+++ b/include/linux/wordpart.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _LINUX_WORDPART_H
+#define _LINUX_WORDPART_H
+
+/**
+ * upper_32_bits - return bits 32-63 of a number
+ * @n: the number we're accessing
+ *
+ * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
+ * the "right shift count >= width of type" warning when that quantity is
+ * 32-bits.
+ */
+#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
+
+/**
+ * lower_32_bits - return bits 0-31 of a number
+ * @n: the number we're accessing
+ */
+#define lower_32_bits(n) ((u32)((n) & 0xffffffff))
+
+/**
+ * upper_16_bits - return bits 16-31 of a number
+ * @n: the number we're accessing
+ */
+#define upper_16_bits(n) ((u16)((n) >> 16))
+
+/**
+ * lower_16_bits - return bits 0-15 of a number
+ * @n: the number we're accessing
+ */
+#define lower_16_bits(n) ((u16)((n) & 0xffff))
+
+/**
+ * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
+ * @x: value to repeat
+ *
+ * NOTE: @x is not checked for > 0xff; larger values produce odd results.
+ */
+#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
+
+/* Set bits in the first 'n' bytes when loaded from memory */
+#ifdef __LITTLE_ENDIAN
+# define aligned_byte_mask(n) ((1UL << 8*(n))-1)
+#else
+# define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n)))
+#endif
+
+#endif // _LINUX_WORDPART_H
--
2.39.2
^ permalink raw reply [flat|nested] 10+ messages in thread