aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/kernel.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/kernel.h')
-rw-r--r--include/linux/kernel.h91
1 files changed, 75 insertions, 16 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 0ad4c3044cf9..4b484ab9e163 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -1,3 +1,4 @@
1/* SPDX-License-Identifier: GPL-2.0 */
1#ifndef _LINUX_KERNEL_H 2#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H 3#define _LINUX_KERNEL_H
3 4
@@ -44,6 +45,12 @@
44 45
45#define STACK_MAGIC 0xdeadbeef 46#define STACK_MAGIC 0xdeadbeef
46 47
48/**
49 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
50 * @x: value to repeat
51 *
52 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
53 */
47#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) 54#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
48 55
49/* @a is a power of 2 value */ 56/* @a is a power of 2 value */
@@ -57,6 +64,10 @@
57#define READ 0 64#define READ 0
58#define WRITE 1 65#define WRITE 1
59 66
67/**
68 * ARRAY_SIZE - get the number of elements in array @arr
69 * @arr: array to be sized
70 */
60#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 71#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
61 72
62#define u64_to_user_ptr(x) ( \ 73#define u64_to_user_ptr(x) ( \
@@ -76,7 +87,15 @@
76#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 87#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
77#define round_down(x, y) ((x) & ~__round_mask(x, y)) 88#define round_down(x, y) ((x) & ~__round_mask(x, y))
78 89
90/**
91 * FIELD_SIZEOF - get the size of a struct's field
92 * @t: the target struct
93 * @f: the target struct's field
94 * Return: the size of @f in the struct definition without having a
95 * declared instance of @t.
96 */
79#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 97#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
98
80#define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP 99#define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
81 100
82#define DIV_ROUND_DOWN_ULL(ll, d) \ 101#define DIV_ROUND_DOWN_ULL(ll, d) \
@@ -107,7 +126,7 @@
107/* 126/*
108 * Divide positive or negative dividend by positive or negative divisor 127 * Divide positive or negative dividend by positive or negative divisor
109 * and round to closest integer. Result is undefined for negative 128 * and round to closest integer. Result is undefined for negative
110 * divisors if he dividend variable type is unsigned and for negative 129 * divisors if the dividend variable type is unsigned and for negative
111 * dividends if the divisor variable type is unsigned. 130 * dividends if the divisor variable type is unsigned.
112 */ 131 */
113#define DIV_ROUND_CLOSEST(x, divisor)( \ 132#define DIV_ROUND_CLOSEST(x, divisor)( \
@@ -247,13 +266,13 @@ extern int _cond_resched(void);
247 * @ep_ro: right open interval endpoint 266 * @ep_ro: right open interval endpoint
248 * 267 *
249 * Perform a "reciprocal multiplication" in order to "scale" a value into 268 * Perform a "reciprocal multiplication" in order to "scale" a value into
250 * range [0, ep_ro), where the upper interval endpoint is right-open. 269 * range [0, @ep_ro), where the upper interval endpoint is right-open.
251 * This is useful, e.g. for accessing a index of an array containing 270 * This is useful, e.g. for accessing a index of an array containing
252 * ep_ro elements, for example. Think of it as sort of modulus, only that 271 * @ep_ro elements, for example. Think of it as sort of modulus, only that
253 * the result isn't that of modulo. ;) Note that if initial input is a 272 * the result isn't that of modulo. ;) Note that if initial input is a
254 * small value, then result will return 0. 273 * small value, then result will return 0.
255 * 274 *
256 * Return: a result based on val in interval [0, ep_ro). 275 * Return: a result based on @val in interval [0, @ep_ro).
257 */ 276 */
258static inline u32 reciprocal_scale(u32 val, u32 ep_ro) 277static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
259{ 278{
@@ -618,8 +637,8 @@ do { \
618 * trace_printk - printf formatting in the ftrace buffer 637 * trace_printk - printf formatting in the ftrace buffer
619 * @fmt: the printf format for printing 638 * @fmt: the printf format for printing
620 * 639 *
621 * Note: __trace_printk is an internal function for trace_printk and 640 * Note: __trace_printk is an internal function for trace_printk() and
622 * the @ip is passed in via the trace_printk macro. 641 * the @ip is passed in via the trace_printk() macro.
623 * 642 *
624 * This function allows a kernel developer to debug fast path sections 643 * This function allows a kernel developer to debug fast path sections
625 * that printk is not appropriate for. By scattering in various 644 * that printk is not appropriate for. By scattering in various
@@ -629,7 +648,7 @@ do { \
629 * This is intended as a debugging tool for the developer only. 648 * This is intended as a debugging tool for the developer only.
630 * Please refrain from leaving trace_printks scattered around in 649 * Please refrain from leaving trace_printks scattered around in
631 * your code. (Extra memory is used for special buffers that are 650 * your code. (Extra memory is used for special buffers that are
632 * allocated when trace_printk() is used) 651 * allocated when trace_printk() is used.)
633 * 652 *
634 * A little optization trick is done here. If there's only one 653 * A little optization trick is done here. If there's only one
635 * argument, there's no need to scan the string for printf formats. 654 * argument, there's no need to scan the string for printf formats.
@@ -681,7 +700,7 @@ int __trace_printk(unsigned long ip, const char *fmt, ...);
681 * the @ip is passed in via the trace_puts macro. 700 * the @ip is passed in via the trace_puts macro.
682 * 701 *
683 * This is similar to trace_printk() but is made for those really fast 702 * This is similar to trace_printk() but is made for those really fast
684 * paths that a developer wants the least amount of "Heisenbug" affects, 703 * paths that a developer wants the least amount of "Heisenbug" effects,
685 * where the processing of the print format is still too much. 704 * where the processing of the print format is still too much.
686 * 705 *
687 * This function allows a kernel developer to debug fast path sections 706 * This function allows a kernel developer to debug fast path sections
@@ -692,7 +711,7 @@ int __trace_printk(unsigned long ip, const char *fmt, ...);
692 * This is intended as a debugging tool for the developer only. 711 * This is intended as a debugging tool for the developer only.
693 * Please refrain from leaving trace_puts scattered around in 712 * Please refrain from leaving trace_puts scattered around in
694 * your code. (Extra memory is used for special buffers that are 713 * your code. (Extra memory is used for special buffers that are
695 * allocated when trace_puts() is used) 714 * allocated when trace_puts() is used.)
696 * 715 *
697 * Returns: 0 if nothing was written, positive # if string was. 716 * Returns: 0 if nothing was written, positive # if string was.
698 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) 717 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
@@ -771,6 +790,12 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
771 t2 min2 = (y); \ 790 t2 min2 = (y); \
772 (void) (&min1 == &min2); \ 791 (void) (&min1 == &min2); \
773 min1 < min2 ? min1 : min2; }) 792 min1 < min2 ? min1 : min2; })
793
794/**
795 * min - return minimum of two values of the same or compatible types
796 * @x: first value
797 * @y: second value
798 */
774#define min(x, y) \ 799#define min(x, y) \
775 __min(typeof(x), typeof(y), \ 800 __min(typeof(x), typeof(y), \
776 __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \ 801 __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \
@@ -781,12 +806,31 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
781 t2 max2 = (y); \ 806 t2 max2 = (y); \
782 (void) (&max1 == &max2); \ 807 (void) (&max1 == &max2); \
783 max1 > max2 ? max1 : max2; }) 808 max1 > max2 ? max1 : max2; })
809
810/**
811 * max - return maximum of two values of the same or compatible types
812 * @x: first value
813 * @y: second value
814 */
784#define max(x, y) \ 815#define max(x, y) \
785 __max(typeof(x), typeof(y), \ 816 __max(typeof(x), typeof(y), \
786 __UNIQUE_ID(max1_), __UNIQUE_ID(max2_), \ 817 __UNIQUE_ID(max1_), __UNIQUE_ID(max2_), \
787 x, y) 818 x, y)
788 819
820/**
821 * min3 - return minimum of three values
822 * @x: first value
823 * @y: second value
824 * @z: third value
825 */
789#define min3(x, y, z) min((typeof(x))min(x, y), z) 826#define min3(x, y, z) min((typeof(x))min(x, y), z)
827
828/**
829 * max3 - return maximum of three values
830 * @x: first value
831 * @y: second value
832 * @z: third value
833 */
790#define max3(x, y, z) max((typeof(x))max(x, y), z) 834#define max3(x, y, z) max((typeof(x))max(x, y), z)
791 835
792/** 836/**
@@ -805,8 +849,8 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
805 * @lo: lowest allowable value 849 * @lo: lowest allowable value
806 * @hi: highest allowable value 850 * @hi: highest allowable value
807 * 851 *
808 * This macro does strict typechecking of lo/hi to make sure they are of the 852 * This macro does strict typechecking of @lo/@hi to make sure they are of the
809 * same type as val. See the unnecessary pointer comparisons. 853 * same type as @val. See the unnecessary pointer comparisons.
810 */ 854 */
811#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) 855#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
812 856
@@ -816,11 +860,24 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
816 * 860 *
817 * Or not use min/max/clamp at all, of course. 861 * Or not use min/max/clamp at all, of course.
818 */ 862 */
863
864/**
865 * min_t - return minimum of two values, using the specified type
866 * @type: data type to use
867 * @x: first value
868 * @y: second value
869 */
819#define min_t(type, x, y) \ 870#define min_t(type, x, y) \
820 __min(type, type, \ 871 __min(type, type, \
821 __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \ 872 __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \
822 x, y) 873 x, y)
823 874
875/**
876 * max_t - return maximum of two values, using the specified type
877 * @type: data type to use
878 * @x: first value
879 * @y: second value
880 */
824#define max_t(type, x, y) \ 881#define max_t(type, x, y) \
825 __max(type, type, \ 882 __max(type, type, \
826 __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \ 883 __UNIQUE_ID(min1_), __UNIQUE_ID(min2_), \
@@ -834,7 +891,7 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
834 * @hi: maximum allowable value 891 * @hi: maximum allowable value
835 * 892 *
836 * This macro does no typechecking and uses temporary variables of type 893 * This macro does no typechecking and uses temporary variables of type
837 * 'type' to make all the comparisons. 894 * @type to make all the comparisons.
838 */ 895 */
839#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi) 896#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
840 897
@@ -845,15 +902,17 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
845 * @hi: maximum allowable value 902 * @hi: maximum allowable value
846 * 903 *
847 * This macro does no typechecking and uses temporary variables of whatever 904 * This macro does no typechecking and uses temporary variables of whatever
848 * type the input argument 'val' is. This is useful when val is an unsigned 905 * type the input argument @val is. This is useful when @val is an unsigned
849 * type and min and max are literals that will otherwise be assigned a signed 906 * type and @lo and @hi are literals that will otherwise be assigned a signed
850 * integer type. 907 * integer type.
851 */ 908 */
852#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 909#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
853 910
854 911
855/* 912/**
856 * swap - swap value of @a and @b 913 * swap - swap values of @a and @b
914 * @a: first value
915 * @b: second value
857 */ 916 */
858#define swap(a, b) \ 917#define swap(a, b) \
859 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 918 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)