Skip to contentSkip to conversation

kernel.h: Split out min()/max() et al helpers

PATCH Superseded Andy Shevchenko 4 participants 7 messages Feb 4, 2020 – Feb 6, 2020

Subsystems (7)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#1 lore raw

PATCH v2 2/2 kernel.h: Split out mathematical helpers

In reply to Andy Shevchenko

kernel.h is being used as a dump for all kinds of stuff for a long time. Here is the attempt to start cleaning it up by splitting out mathematical helpers.

At the same time convert users in header and lib folder to use new header. Though for time being include new header back to kernel.h to avoid twisted indirected includes for existing users.

---
v2: include new header in kernel.h for time being (Andrew)

 fs/nfs/callback_proc.c        |   1 +
 include/linux/bitops.h        |  11 ++-
 include/linux/dcache.h        |   1 +
 include/linux/iommu-helper.h  |   4 +-
 include/linux/kernel.h        | 174 +--------------------------------
 include/linux/math.h          | 177 ++++++++++++++++++++++++++++++++++
 include/linux/rcu_node_tree.h |   2 +
 include/linux/units.h         |   2 +-
 lib/errname.c                 |   1 +
 lib/find_bit.c                |   3 +-
 lib/math/div64.c              |   2 +-
 lib/math/int_pow.c            |   2 +-
 lib/math/int_sqrt.c           |   3 +-
 lib/math/reciprocal_div.c     |   7 +-
 14 files changed, 206 insertions(+), 184 deletions(-)
 create mode 100644 include/linux/math.h
fs/nfs/callback_proc.c +1 -0
index cd4c6bc81cae..9290eea5b17f 100644
@@ -6,6 +6,7 @@
  *
  * NFSv4 callback procedures
  */
+#include <linux/math.h>
 #include <linux/nfs4.h>
 #include <linux/nfs_fs.h>
 #include <linux/slab.h>
include/linux/bitops.h +7 -4
index 47f54b459c26..21696ea359d1 100644
@@ -1,9 +1,12 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #ifndef _LINUX_BITOPS_H
 #define _LINUX_BITOPS_H
+
 #include <asm/types.h>
 #include <linux/bits.h>
 
+#include <uapi/linux/kernel.h>
+
 /* Set bits in the first 'n' bytes when loaded from memory */
 #ifdef __LITTLE_ENDIAN
 #  define aligned_byte_mask(n) ((1UL << 8*(n))-1)
@@ -12,10 +15,10 @@
 #endif
 
 #define BITS_PER_TYPE(type)	(sizeof(type) * BITS_PER_BYTE)
-#define BITS_TO_LONGS(nr)	DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
-#define BITS_TO_U64(nr)		DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
-#define BITS_TO_U32(nr)		DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
-#define BITS_TO_BYTES(nr)	DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
+#define BITS_TO_LONGS(nr)	__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
+#define BITS_TO_U64(nr)		__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u64))
+#define BITS_TO_U32(nr)		__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(u32))
+#define BITS_TO_BYTES(nr)	__KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(char))
 
 extern unsigned int __sw_hweight8(unsigned int w);
 extern unsigned int __sw_hweight16(unsigned int w);
include/linux/dcache.h +1 -0
index c1488cc84fd9..096e04b7f38d 100644
@@ -4,6 +4,7 @@
 
 #include <linux/atomic.h>
 #include <linux/list.h>
+#include <linux/math.h>
 #include <linux/rculist.h>
 #include <linux/rculist_bl.h>
 #include <linux/spinlock.h>
include/linux/iommu-helper.h +3 -1
index 70d01edcbf8b..74be34f3a20a 100644
@@ -3,7 +3,9 @@
 #define _LINUX_IOMMU_HELPER_H
 
 #include <linux/bug.h>
-#include <linux/kernel.h>
+#include <linux/log2.h>
+#include <linux/math.h>
+#include <linux/types.h>
 
 static inline unsigned long iommu_device_max_index(unsigned long size,
 						   unsigned long offset,
include/linux/kernel.h +3 -171

215 lines not on this page · raw message

include/linux/math.h new file +177 -0

177 lines not on this page · raw message

include/linux/rcu_node_tree.h +2 -0
index b8e094b125ee..78feb8ba7358 100644
@@ -20,6 +20,8 @@
 #ifndef __LINUX_RCU_NODE_TREE_H
 #define __LINUX_RCU_NODE_TREE_H
 
+#include <linux/math.h>
+
 /*
  * Define shape of hierarchy based on NR_CPUS, CONFIG_RCU_FANOUT, and
  * CONFIG_RCU_FANOUT_LEAF.
include/linux/units.h +1 -1
index aaf716364ec3..5c115c809507 100644
@@ -2,7 +2,7 @@
 #ifndef _LINUX_UNITS_H
 #define _LINUX_UNITS_H
 
-#include <linux/kernel.h>
+#include <linux/math.h>
 
 #define ABSOLUTE_ZERO_MILLICELSIUS -273150
 
lib/errname.c +1 -0
index 0c4d3e66170e..05cbf731545f 100644
@@ -3,6 +3,7 @@
 #include <linux/errno.h>
 #include <linux/errname.h>
 #include <linux/kernel.h>
+#include <linux/math.h>
 
 /*
  * Ensure these tables do not accidentally become gigantic if some
lib/find_bit.c +2 -1
index 4a8751010d59..f67f86fd2f62 100644
@@ -15,8 +15,9 @@
 #include <linux/bitops.h>
 #include <linux/bitmap.h>
 #include <linux/export.h>
-#include <linux/kernel.h>
+#include <linux/math.h>
 #include <linux/minmax.h>
+#include <linux/swab.h>
 
 #if !defined(find_next_bit) || !defined(find_next_zero_bit) ||			\
 	!defined(find_next_bit_le) || !defined(find_next_zero_bit_le) ||	\
lib/math/div64.c +1 -1
index 368ca7fd0d82..34ea6147b364 100644
@@ -19,7 +19,7 @@
  */
 
 #include <linux/export.h>
-#include <linux/kernel.h>
+#include <linux/math.h>
 #include <linux/math64.h>
 
 /* Not needed on 64bit architectures */
lib/math/int_pow.c +1 -1
index 622fc1ab3c74..0cf426e69bda 100644
@@ -6,7 +6,7 @@
  */
 
 #include <linux/export.h>
-#include <linux/kernel.h>
+#include <linux/math.h>
 #include <linux/types.h>
 
 /**
lib/math/int_sqrt.c +2 -1
index 30e0f9770f88..a8170bb9142f 100644
@@ -6,9 +6,10 @@
  *  square root from Guy L. Steele.
  */
 
-#include <linux/kernel.h>
 #include <linux/export.h>
 #include <linux/bitops.h>
+#include <linux/limits.h>
+#include <linux/math.h>
 
 /**
  * int_sqrt - computes the integer square root
lib/math/reciprocal_div.c +4 -3
index 32436dd4171e..d6431b34ac29 100644
@@ -1,10 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <linux/bug.h>
-#include <linux/kernel.h>
-#include <asm/div64.h>
-#include <linux/reciprocal_div.h>
 #include <linux/export.h>
+#include <linux/math.h>
 #include <linux/minmax.h>
+#include <linux/types.h>
+
+#include <linux/reciprocal_div.h>
 
 /*
  * For a description of the algorithm please have a look at
Signature · 1 line
2.24.1
#0 lore raw

PATCH v2 1/2 kernel.h: Split out min()/max() et al helpers

kernel.h is being used as a dump for all kinds of stuff for a long time. Here is the attempt to start cleaning it up by splitting out min()/max() et al helpers.

At the same time convert users in header and lib folder to use new header. Though for time being include new header back to kernel.h to avoid twisted indirected includes for existing users.

---
v2: new patch

 include/linux/blkdev.h    |   1 +
 include/linux/bvec.h      |   6 +-
 include/linux/jiffies.h   |   3 +-
 include/linux/kernel.h    | 142 +------------------------------------
 include/linux/minmax.h    | 145 ++++++++++++++++++++++++++++++++++++++
 include/linux/nodemask.h  |   2 +-
 include/linux/uaccess.h   |   1 +
 kernel/range.c            |   3 +-
 lib/find_bit.c            |   1 +
 lib/hexdump.c             |   1 +
 lib/math/rational.c       |   2 +-
 lib/math/reciprocal_div.c |   1 +
 12 files changed, 162 insertions(+), 146 deletions(-)
 create mode 100644 include/linux/minmax.h
include/linux/blkdev.h +1 -0
index 053ea4b51988..0cea2c66d6c4 100644
@@ -11,6 +11,7 @@
 #include <linux/genhd.h>
 #include <linux/list.h>
 #include <linux/llist.h>
+#include <linux/minmax.h>
 #include <linux/timer.h>
 #include <linux/workqueue.h>
 #include <linux/pagemap.h>
include/linux/bvec.h +5 -1
index a81c13ac1972..56d4dec74926 100644
@@ -7,10 +7,14 @@
 #ifndef __LINUX_BVEC_ITER_H
 #define __LINUX_BVEC_ITER_H
 
-#include <linux/kernel.h>
 #include <linux/bug.h>
 #include <linux/errno.h>
+#include <linux/limits.h>
+#include <linux/minmax.h>
 #include <linux/mm.h>
+#include <linux/types.h>
+
+struct page;
 
 /*
  * was unsigned short, but we might as well be ready for > 64kB I/O pages
include/linux/jiffies.h +2 -1
index e3279ef24d28..d5a41a509cc0 100644
@@ -3,8 +3,9 @@
 #define _LINUX_JIFFIES_H
 
 #include <linux/cache.h>
+#include <linux/limits.h>
 #include <linux/math64.h>
-#include <linux/kernel.h>
+#include <linux/minmax.h>
 #include <linux/types.h>
 #include <linux/time.h>
 #include <linux/timex.h>
include/linux/kernel.h +1 -141

154 lines not on this page · raw message

include/linux/minmax.h new file +145 -0

145 lines not on this page · raw message

include/linux/nodemask.h +1 -1
index 27e7fa36f707..7f38399cc9fe 100644
@@ -90,9 +90,9 @@
  * for such situations. See below and CPUMASK_ALLOC also.
  */
 
-#include <linux/kernel.h>
 #include <linux/threads.h>
 #include <linux/bitmap.h>
+#include <linux/minmax.h>
 #include <linux/numa.h>
 
 typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
include/linux/uaccess.h +1 -0
index 67f016010aad..014a84799f56 100644
@@ -5,6 +5,7 @@
 #include <linux/sched.h>
 #include <linux/thread_info.h>
 #include <linux/kasan-checks.h>
+#include <linux/minmax.h>
 
 #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS)
 
kernel/range.c +2 -1
index d84de6766472..56435f96da73 100644
@@ -2,8 +2,9 @@
 /*
  * Range add and subtract
  */
-#include <linux/kernel.h>
 #include <linux/init.h>
+#include <linux/minmax.h>
+#include <linux/printk.h>
 #include <linux/sort.h>
 #include <linux/string.h>
 #include <linux/range.h>
lib/find_bit.c +1 -0
index 49f875f1baf7..4a8751010d59 100644
@@ -16,6 +16,7 @@
 #include <linux/bitmap.h>
 #include <linux/export.h>
 #include <linux/kernel.h>
+#include <linux/minmax.h>
 
 #if !defined(find_next_bit) || !defined(find_next_zero_bit) ||			\
 	!defined(find_next_bit_le) || !defined(find_next_zero_bit_le) ||	\
lib/hexdump.c +1 -0
index 147133f8eb2f..9301578f98e8 100644
@@ -7,6 +7,7 @@
 #include <linux/ctype.h>
 #include <linux/errno.h>
 #include <linux/kernel.h>
+#include <linux/minmax.h>
 #include <linux/export.h>
 #include <asm/unaligned.h>
 
lib/math/rational.c +1 -1
index 31fb27db2deb..d8e985850d10 100644
@@ -11,7 +11,7 @@
 #include <linux/rational.h>
 #include <linux/compiler.h>
 #include <linux/export.h>
-#include <linux/kernel.h>
+#include <linux/minmax.h>
 
 /*
  * calculate best rational approximation for a given fraction
lib/math/reciprocal_div.c +1 -0
index bf043258fa00..32436dd4171e 100644
@@ -4,6 +4,7 @@
 #include <asm/div64.h>
 #include <linux/reciprocal_div.h>
 #include <linux/export.h>
+#include <linux/minmax.h>
 
 /*
  * For a description of the algorithm please have a look at
Signature · 1 line
2.24.1
#3 lore raw
Quoted 7 lines · Andy Shevchenko

kernel.h is being used as a dump for all kinds of stuff for a long time. Here is the attempt to start cleaning it up by splitting out min()/max() et al helpers.

At the same time convert users in header and lib folder to use new header. Though for time being include new header back to kernel.h to avoid twisted indirected includes for existing users.

This is definitely long overdue, so thanks for taking this on. I think minmax.h is fine as a header on its own, but for the other one, I think you should go even further - and perhaps all these should go in a include/math/ dir (include/linux/ has ~1200 files), so we'd have math/minmax.h, math/round.h, math/ilog2.h, math/gcd.h etc., each containing just enough #includes to be self-contained (so if there's a declaration of something taking a u32, there's no way around having it include types.h (or wherever that's defined).

Rasmus

#4 lore raw
Quoted 9 lines · Rasmus Villemoes
Quoted 7 lines · Andy Shevchenko

kernel.h is being used as a dump for all kinds of stuff for a long time. Here is the attempt to start cleaning it up by splitting out min()/max() et al helpers.

At the same time convert users in header and lib folder to use new header. Though for time being include new header back to kernel.h to avoid twisted indirected includes for existing users.

Quoted 8 lines

This is definitely long overdue, so thanks for taking this on. I think minmax.h is fine as a header on its own, but for the other one, I think you should go even further - and perhaps all these should go in a include/math/ dir (include/linux/ has ~1200 files), so we'd have math/minmax.h, math/round.h, math/ilog2.h, math/gcd.h etc., each containing just enough #includes to be self-contained (so if there's a declaration of something taking a u32, there's no way around having it include types.h (or wherever that's defined).

I think that's not at all desirable.

kernel.h as a monolithic include block is pretty useful.

Separating out the various bits into separate files is OK, but kernel.h should #include them all.

One day a precompiled header of just kernel.h would be useful to reduce overall compilation time. Converting all the other source files that use a small part of the existing kernel.h into multiple includes would not allow precompiled headers to work efficiently.

#2 lore raw

Re: [PATCH v2 2/2] kernel.h: Split out mathematical helpers

In reply to Andy Shevchenko
I love your patch! Yet something to improve:

Hi Andy,

I love your patch! Yet something to improve:

[auto build test ERROR on next-20200203]
[cannot apply to nfs/linux-next rcu/dev linux/master linus/master v5.5 v5.5-rc7 v5.5-rc6 v5.5] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Andy-Shevchenko/kernel-h-Split-out-min-max-et-al-helpers/20200205-040141
base:    cee5a42837d4a6c4189f06f7bf355b97a24c3c93
config: m68k-multi_defconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 7.5.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.5.0 make.cross ARCH=m68k

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

lib/math/div64.c: In function 'div64_u64_rem':

>> lib/math/div64.c:112:11: error: implicit declaration of function 'fls' [-Werror=implicit-function-declaration]
      int n = fls(high);
              ^~~

cc1: some warnings being treated as errors

vim +/fls +112 lib/math/div64.c

2418f4f28f8467 lib/div64.c Roman Zippel      2008-05-01   89  
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   90  /**
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   91   * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   92   * @dividend:	64bit dividend
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   93   * @divisor:	64bit divisor
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   94   * @remainder:  64bit remainder
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   95   *
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   96   * This implementation is a comparable to algorithm used by div64_u64.
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   97   * But this operation, which includes math for calculating the remainder,
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   98   * is kept distinct to avoid slowing down the div64_u64 operation on 32bit
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20   99   * systems.
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  100   */
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  101  #ifndef div64_u64_rem
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  102  u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  103  {
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  104  	u32 high = divisor >> 32;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  105  	u64 quot;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  106  
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  107  	if (high == 0) {
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  108  		u32 rem32;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  109  		quot = div_u64_rem(dividend, divisor, &rem32);
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  110  		*remainder = rem32;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  111  	} else {
cdc94a37493135 lib/div64.c Stanislaw Gruszka 2019-03-07 @112  		int n = fls(high);
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  113  		quot = div_u64(dividend >> n, divisor >> n);
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  114  
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  115  		if (quot != 0)
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  116  			quot--;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  117  
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  118  		*remainder = dividend - quot * divisor;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  119  		if (*remainder >= divisor) {
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  120  			quot++;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  121  			*remainder -= divisor;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  122  		}
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  123  	}
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  124  
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  125  	return quot;
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  126  }
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  127  EXPORT_SYMBOL(div64_u64_rem);
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  128  #endif
eb18cba78c2b92 lib/div64.c Mike Snitzer      2013-08-20  129  

:::::: The code at line 112 was first introduced by commit
:::::: cdc94a37493135e355dfc0b0e086d84e3eadb50d lib/div64.c: off by one in shift

:::::: TO: Stanislaw Gruszka <[email protected]> :::::: CC: Linus Torvalds <[email protected]>

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org Intel Corporation

#5 lore raw
In reply to Joe Perches
Quoted 19 lines · Joe Perches
Quoted 9 lines · Rasmus Villemoes
Quoted 7 lines · Andy Shevchenko

kernel.h is being used as a dump for all kinds of stuff for a long time. Here is the attempt to start cleaning it up by splitting out min()/max() et al helpers.

At the same time convert users in header and lib folder to use new header. Though for time being include new header back to kernel.h to avoid twisted indirected includes for existing users.

Quoted 8 lines

This is definitely long overdue, so thanks for taking this on. I think minmax.h is fine as a header on its own, but for the other one, I think you should go even further - and perhaps all these should go in a include/math/ dir (include/linux/ has ~1200 files), so we'd have math/minmax.h, math/round.h, math/ilog2.h, math/gcd.h etc., each containing just enough #includes to be self-contained (so if there's a declaration of something taking a u32, there's no way around having it include types.h (or wherever that's defined).

Quoted 1 line

I think that's not at all desirable.

device.h has been recently split to a 4 files (by Linus [old] request). Any comments on that?

Quoted 4 lines

kernel.h as a monolithic include block is pretty useful.

Separating out the various bits into separate files is OK, but kernel.h should #include them all.

That's fine but should not make people think this is a good idea to include only one header to their modules.

Quoted 2 lines

One day a precompiled header of just kernel.h would be useful to reduce overall compilation time.

All years no change.

Quoted 4 lines

Converting
all the other source files that use a small part of the existing kernel.h into multiple includes would not allow precompiled headers to work efficiently.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
In reply to Rasmus Villemoes
Quoted 9 lines · Rasmus Villemoes
Quoted 7 lines · Andy Shevchenko

kernel.h is being used as a dump for all kinds of stuff for a long time. Here is the attempt to start cleaning it up by splitting out min()/max() et al helpers.

At the same time convert users in header and lib folder to use new header. Though for time being include new header back to kernel.h to avoid twisted indirected includes for existing users.

Quoted 8 lines

This is definitely long overdue, so thanks for taking this on. I think minmax.h is fine as a header on its own, but for the other one, I think you should go even further - and perhaps all these should go in a include/math/ dir (include/linux/ has ~1200 files), so we'd have math/minmax.h, math/round.h, math/ilog2.h, math/gcd.h etc., each containing just enough #includes to be self-contained (so if there's a declaration of something taking a u32, there's no way around having it include types.h (or wherever that's defined).

While I like the idea, I think we may do it in a next iteration.

Signature · 2 lines
With Best Regards,
Andy Shevchenko