Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 other existing users.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Joe Perches <joe@perches.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: https://lkml.kernel.org/r/20200910164152.GA1891694@smile.fi.intel.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andy Shevchenko and committed by
Linus Torvalds
b296a6d5 ce9bebe6

+170 -154
+1
include/linux/blkdev.h
··· 8 8 #include <linux/genhd.h> 9 9 #include <linux/list.h> 10 10 #include <linux/llist.h> 11 + #include <linux/minmax.h> 11 12 #include <linux/timer.h> 12 13 #include <linux/workqueue.h> 13 14 #include <linux/pagemap.h>
+5 -1
include/linux/bvec.h
··· 7 7 #ifndef __LINUX_BVEC_ITER_H 8 8 #define __LINUX_BVEC_ITER_H 9 9 10 - #include <linux/kernel.h> 11 10 #include <linux/bug.h> 12 11 #include <linux/errno.h> 12 + #include <linux/limits.h> 13 + #include <linux/minmax.h> 13 14 #include <linux/mm.h> 15 + #include <linux/types.h> 16 + 17 + struct page; 14 18 15 19 /** 16 20 * struct bio_vec - a contiguous range of physical memory addresses
+2 -1
include/linux/jiffies.h
··· 3 3 #define _LINUX_JIFFIES_H 4 4 5 5 #include <linux/cache.h> 6 + #include <linux/limits.h> 6 7 #include <linux/math64.h> 7 - #include <linux/kernel.h> 8 + #include <linux/minmax.h> 8 9 #include <linux/types.h> 9 10 #include <linux/time.h> 10 11 #include <linux/timex.h>
+1 -149
include/linux/kernel.h
··· 11 11 #include <linux/compiler.h> 12 12 #include <linux/bitops.h> 13 13 #include <linux/log2.h> 14 + #include <linux/minmax.h> 14 15 #include <linux/typecheck.h> 15 16 #include <linux/printk.h> 16 17 #include <linux/build_bug.h> ··· 833 832 } 834 833 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } 835 834 #endif /* CONFIG_TRACING */ 836 - 837 - /* 838 - * min()/max()/clamp() macros must accomplish three things: 839 - * 840 - * - avoid multiple evaluations of the arguments (so side-effects like 841 - * "x++" happen only once) when non-constant. 842 - * - perform strict type-checking (to generate warnings instead of 843 - * nasty runtime surprises). See the "unnecessary" pointer comparison 844 - * in __typecheck(). 845 - * - retain result as a constant expressions when called with only 846 - * constant expressions (to avoid tripping VLA warnings in stack 847 - * allocation usage). 848 - */ 849 - #define __typecheck(x, y) \ 850 - (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) 851 - 852 - /* 853 - * This returns a constant expression while determining if an argument is 854 - * a constant expression, most importantly without evaluating the argument. 855 - * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de> 856 - */ 857 - #define __is_constexpr(x) \ 858 - (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) 859 - 860 - #define __no_side_effects(x, y) \ 861 - (__is_constexpr(x) && __is_constexpr(y)) 862 - 863 - #define __safe_cmp(x, y) \ 864 - (__typecheck(x, y) && __no_side_effects(x, y)) 865 - 866 - #define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) 867 - 868 - #define __cmp_once(x, y, unique_x, unique_y, op) ({ \ 869 - typeof(x) unique_x = (x); \ 870 - typeof(y) unique_y = (y); \ 871 - __cmp(unique_x, unique_y, op); }) 872 - 873 - #define __careful_cmp(x, y, op) \ 874 - __builtin_choose_expr(__safe_cmp(x, y), \ 875 - __cmp(x, y, op), \ 876 - __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) 877 - 878 - /** 879 - * min - return minimum of two values of the same or compatible types 880 - * @x: first value 881 - * @y: second value 882 - */ 883 - #define min(x, y) __careful_cmp(x, y, <) 884 - 885 - /** 886 - * max - return maximum of two values of the same or compatible types 887 - * @x: first value 888 - * @y: second value 889 - */ 890 - #define max(x, y) __careful_cmp(x, y, >) 891 - 892 - /** 893 - * min3 - return minimum of three values 894 - * @x: first value 895 - * @y: second value 896 - * @z: third value 897 - */ 898 - #define min3(x, y, z) min((typeof(x))min(x, y), z) 899 - 900 - /** 901 - * max3 - return maximum of three values 902 - * @x: first value 903 - * @y: second value 904 - * @z: third value 905 - */ 906 - #define max3(x, y, z) max((typeof(x))max(x, y), z) 907 - 908 - /** 909 - * min_not_zero - return the minimum that is _not_ zero, unless both are zero 910 - * @x: value1 911 - * @y: value2 912 - */ 913 - #define min_not_zero(x, y) ({ \ 914 - typeof(x) __x = (x); \ 915 - typeof(y) __y = (y); \ 916 - __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 917 - 918 - /** 919 - * clamp - return a value clamped to a given range with strict typechecking 920 - * @val: current value 921 - * @lo: lowest allowable value 922 - * @hi: highest allowable value 923 - * 924 - * This macro does strict typechecking of @lo/@hi to make sure they are of the 925 - * same type as @val. See the unnecessary pointer comparisons. 926 - */ 927 - #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 928 - 929 - /* 930 - * ..and if you can't take the strict 931 - * types, you can specify one yourself. 932 - * 933 - * Or not use min/max/clamp at all, of course. 934 - */ 935 - 936 - /** 937 - * min_t - return minimum of two values, using the specified type 938 - * @type: data type to use 939 - * @x: first value 940 - * @y: second value 941 - */ 942 - #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <) 943 - 944 - /** 945 - * max_t - return maximum of two values, using the specified type 946 - * @type: data type to use 947 - * @x: first value 948 - * @y: second value 949 - */ 950 - #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >) 951 - 952 - /** 953 - * clamp_t - return a value clamped to a given range using a given type 954 - * @type: the type of variable to use 955 - * @val: current value 956 - * @lo: minimum allowable value 957 - * @hi: maximum allowable value 958 - * 959 - * This macro does no typechecking and uses temporary variables of type 960 - * @type to make all the comparisons. 961 - */ 962 - #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 963 - 964 - /** 965 - * clamp_val - return a value clamped to a given range using val's type 966 - * @val: current value 967 - * @lo: minimum allowable value 968 - * @hi: maximum allowable value 969 - * 970 - * This macro does no typechecking and uses temporary variables of whatever 971 - * type the input argument @val is. This is useful when @val is an unsigned 972 - * type and @lo and @hi are literals that will otherwise be assigned a signed 973 - * integer type. 974 - */ 975 - #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 976 - 977 - 978 - /** 979 - * swap - swap values of @a and @b 980 - * @a: first value 981 - * @b: second value 982 - */ 983 - #define swap(a, b) \ 984 - do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 985 835 986 836 /* This counts to 12. Any more, it will return 13th argument. */ 987 837 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
+153
include/linux/minmax.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef _LINUX_MINMAX_H 3 + #define _LINUX_MINMAX_H 4 + 5 + /* 6 + * min()/max()/clamp() macros must accomplish three things: 7 + * 8 + * - avoid multiple evaluations of the arguments (so side-effects like 9 + * "x++" happen only once) when non-constant. 10 + * - perform strict type-checking (to generate warnings instead of 11 + * nasty runtime surprises). See the "unnecessary" pointer comparison 12 + * in __typecheck(). 13 + * - retain result as a constant expressions when called with only 14 + * constant expressions (to avoid tripping VLA warnings in stack 15 + * allocation usage). 16 + */ 17 + #define __typecheck(x, y) \ 18 + (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) 19 + 20 + /* 21 + * This returns a constant expression while determining if an argument is 22 + * a constant expression, most importantly without evaluating the argument. 23 + * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de> 24 + */ 25 + #define __is_constexpr(x) \ 26 + (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) 27 + 28 + #define __no_side_effects(x, y) \ 29 + (__is_constexpr(x) && __is_constexpr(y)) 30 + 31 + #define __safe_cmp(x, y) \ 32 + (__typecheck(x, y) && __no_side_effects(x, y)) 33 + 34 + #define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) 35 + 36 + #define __cmp_once(x, y, unique_x, unique_y, op) ({ \ 37 + typeof(x) unique_x = (x); \ 38 + typeof(y) unique_y = (y); \ 39 + __cmp(unique_x, unique_y, op); }) 40 + 41 + #define __careful_cmp(x, y, op) \ 42 + __builtin_choose_expr(__safe_cmp(x, y), \ 43 + __cmp(x, y, op), \ 44 + __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) 45 + 46 + /** 47 + * min - return minimum of two values of the same or compatible types 48 + * @x: first value 49 + * @y: second value 50 + */ 51 + #define min(x, y) __careful_cmp(x, y, <) 52 + 53 + /** 54 + * max - return maximum of two values of the same or compatible types 55 + * @x: first value 56 + * @y: second value 57 + */ 58 + #define max(x, y) __careful_cmp(x, y, >) 59 + 60 + /** 61 + * min3 - return minimum of three values 62 + * @x: first value 63 + * @y: second value 64 + * @z: third value 65 + */ 66 + #define min3(x, y, z) min((typeof(x))min(x, y), z) 67 + 68 + /** 69 + * max3 - return maximum of three values 70 + * @x: first value 71 + * @y: second value 72 + * @z: third value 73 + */ 74 + #define max3(x, y, z) max((typeof(x))max(x, y), z) 75 + 76 + /** 77 + * min_not_zero - return the minimum that is _not_ zero, unless both are zero 78 + * @x: value1 79 + * @y: value2 80 + */ 81 + #define min_not_zero(x, y) ({ \ 82 + typeof(x) __x = (x); \ 83 + typeof(y) __y = (y); \ 84 + __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); }) 85 + 86 + /** 87 + * clamp - return a value clamped to a given range with strict typechecking 88 + * @val: current value 89 + * @lo: lowest allowable value 90 + * @hi: highest allowable value 91 + * 92 + * This macro does strict typechecking of @lo/@hi to make sure they are of the 93 + * same type as @val. See the unnecessary pointer comparisons. 94 + */ 95 + #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 96 + 97 + /* 98 + * ..and if you can't take the strict 99 + * types, you can specify one yourself. 100 + * 101 + * Or not use min/max/clamp at all, of course. 102 + */ 103 + 104 + /** 105 + * min_t - return minimum of two values, using the specified type 106 + * @type: data type to use 107 + * @x: first value 108 + * @y: second value 109 + */ 110 + #define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <) 111 + 112 + /** 113 + * max_t - return maximum of two values, using the specified type 114 + * @type: data type to use 115 + * @x: first value 116 + * @y: second value 117 + */ 118 + #define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >) 119 + 120 + /** 121 + * clamp_t - return a value clamped to a given range using a given type 122 + * @type: the type of variable to use 123 + * @val: current value 124 + * @lo: minimum allowable value 125 + * @hi: maximum allowable value 126 + * 127 + * This macro does no typechecking and uses temporary variables of type 128 + * @type to make all the comparisons. 129 + */ 130 + #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 131 + 132 + /** 133 + * clamp_val - return a value clamped to a given range using val's type 134 + * @val: current value 135 + * @lo: minimum allowable value 136 + * @hi: maximum allowable value 137 + * 138 + * This macro does no typechecking and uses temporary variables of whatever 139 + * type the input argument @val is. This is useful when @val is an unsigned 140 + * type and @lo and @hi are literals that will otherwise be assigned a signed 141 + * integer type. 142 + */ 143 + #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 144 + 145 + /** 146 + * swap - swap values of @a and @b 147 + * @a: first value 148 + * @b: second value 149 + */ 150 + #define swap(a, b) \ 151 + do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 152 + 153 + #endif /* _LINUX_MINMAX_H */
+1 -1
include/linux/nodemask.h
··· 90 90 * for such situations. See below and CPUMASK_ALLOC also. 91 91 */ 92 92 93 - #include <linux/kernel.h> 94 93 #include <linux/threads.h> 95 94 #include <linux/bitmap.h> 95 + #include <linux/minmax.h> 96 96 #include <linux/numa.h> 97 97 98 98 typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
+1
include/linux/uaccess.h
··· 3 3 #define __LINUX_UACCESS_H__ 4 4 5 5 #include <linux/instrumented.h> 6 + #include <linux/minmax.h> 6 7 #include <linux/sched.h> 7 8 #include <linux/thread_info.h> 8 9
+2 -1
kernel/range.c
··· 2 2 /* 3 3 * Range add and subtract 4 4 */ 5 - #include <linux/kernel.h> 6 5 #include <linux/init.h> 6 + #include <linux/minmax.h> 7 + #include <linux/printk.h> 7 8 #include <linux/sort.h> 8 9 #include <linux/string.h> 9 10 #include <linux/range.h>
+1
lib/find_bit.c
··· 16 16 #include <linux/bitmap.h> 17 17 #include <linux/export.h> 18 18 #include <linux/kernel.h> 19 + #include <linux/minmax.h> 19 20 20 21 #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ 21 22 !defined(find_next_bit_le) || !defined(find_next_zero_bit_le) || \
+1
lib/hexdump.c
··· 7 7 #include <linux/ctype.h> 8 8 #include <linux/errno.h> 9 9 #include <linux/kernel.h> 10 + #include <linux/minmax.h> 10 11 #include <linux/export.h> 11 12 #include <asm/unaligned.h> 12 13
+1 -1
lib/math/rational.c
··· 11 11 #include <linux/rational.h> 12 12 #include <linux/compiler.h> 13 13 #include <linux/export.h> 14 - #include <linux/kernel.h> 14 + #include <linux/minmax.h> 15 15 16 16 /* 17 17 * calculate best rational approximation for a given fraction
+1
lib/math/reciprocal_div.c
··· 4 4 #include <asm/div64.h> 5 5 #include <linux/reciprocal_div.h> 6 6 #include <linux/export.h> 7 + #include <linux/minmax.h> 7 8 8 9 /* 9 10 * For a description of the algorithm please have a look at