linux-acpi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers
@ 2020-08-17 16:36 Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 2/8] resource: Simplify region_intersects() by reducing conditionals Andy Shevchenko
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi; +Cc: 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 other existing users.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v2: new patch, prerequisite for the rest (Rafael)
 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

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index bb5636cc17b9..3641c7234afb 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -8,6 +8,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>
diff --git a/include/linux/bvec.h b/include/linux/bvec.h
index ac0c7299d5b8..8c7b3926b69c 100644
--- a/include/linux/bvec.h
+++ b/include/linux/bvec.h
@@ -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;
 
 /**
  * struct bio_vec - a contiguous range of physical memory addresses
diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h
index fed6ba96c527..5e13f801c902 100644
--- a/include/linux/jiffies.h
+++ b/include/linux/jiffies.h
@@ -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>
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 500def620d8f..397f66bef709 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -11,6 +11,7 @@
 #include <linux/compiler.h>
 #include <linux/bitops.h>
 #include <linux/log2.h>
+#include <linux/minmax.h>
 #include <linux/typecheck.h>
 #include <linux/printk.h>
 #include <linux/build_bug.h>
@@ -834,147 +835,6 @@ ftrace_vprintk(const char *fmt, va_list ap)
 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
 #endif /* CONFIG_TRACING */
 
-/*
- * min()/max()/clamp() macros must accomplish three things:
- *
- * - avoid multiple evaluations of the arguments (so side-effects like
- *   "x++" happen only once) when non-constant.
- * - perform strict type-checking (to generate warnings instead of
- *   nasty runtime surprises). See the "unnecessary" pointer comparison
- *   in __typecheck().
- * - retain result as a constant expressions when called with only
- *   constant expressions (to avoid tripping VLA warnings in stack
- *   allocation usage).
- */
-#define __typecheck(x, y) \
-		(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
-
-/*
- * This returns a constant expression while determining if an argument is
- * a constant expression, most importantly without evaluating the argument.
- * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
- */
-#define __is_constexpr(x) \
-	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
-
-#define __no_side_effects(x, y) \
-		(__is_constexpr(x) && __is_constexpr(y))
-
-#define __safe_cmp(x, y) \
-		(__typecheck(x, y) && __no_side_effects(x, y))
-
-#define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
-
-#define __cmp_once(x, y, unique_x, unique_y, op) ({	\
-		typeof(x) unique_x = (x);		\
-		typeof(y) unique_y = (y);		\
-		__cmp(unique_x, unique_y, op); })
-
-#define __careful_cmp(x, y, op) \
-	__builtin_choose_expr(__safe_cmp(x, y), \
-		__cmp(x, y, op), \
-		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
-
-/**
- * min - return minimum of two values of the same or compatible types
- * @x: first value
- * @y: second value
- */
-#define min(x, y)	__careful_cmp(x, y, <)
-
-/**
- * max - return maximum of two values of the same or compatible types
- * @x: first value
- * @y: second value
- */
-#define max(x, y)	__careful_cmp(x, y, >)
-
-/**
- * min3 - return minimum of three values
- * @x: first value
- * @y: second value
- * @z: third value
- */
-#define min3(x, y, z) min((typeof(x))min(x, y), z)
-
-/**
- * max3 - return maximum of three values
- * @x: first value
- * @y: second value
- * @z: third value
- */
-#define max3(x, y, z) max((typeof(x))max(x, y), z)
-
-/**
- * min_not_zero - return the minimum that is _not_ zero, unless both are zero
- * @x: value1
- * @y: value2
- */
-#define min_not_zero(x, y) ({			\
-	typeof(x) __x = (x);			\
-	typeof(y) __y = (y);			\
-	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
-
-/**
- * clamp - return a value clamped to a given range with strict typechecking
- * @val: current value
- * @lo: lowest allowable value
- * @hi: highest allowable value
- *
- * This macro does strict typechecking of @lo/@hi to make sure they are of the
- * same type as @val.  See the unnecessary pointer comparisons.
- */
-#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
-
-/*
- * ..and if you can't take the strict
- * types, you can specify one yourself.
- *
- * Or not use min/max/clamp at all, of course.
- */
-
-/**
- * min_t - return minimum of two values, using the specified type
- * @type: data type to use
- * @x: first value
- * @y: second value
- */
-#define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
-
-/**
- * max_t - return maximum of two values, using the specified type
- * @type: data type to use
- * @x: first value
- * @y: second value
- */
-#define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
-
-/**
- * clamp_t - return a value clamped to a given range using a given type
- * @type: the type of variable to use
- * @val: current value
- * @lo: minimum allowable value
- * @hi: maximum allowable value
- *
- * This macro does no typechecking and uses temporary variables of type
- * @type to make all the comparisons.
- */
-#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
-
-/**
- * clamp_val - return a value clamped to a given range using val's type
- * @val: current value
- * @lo: minimum allowable value
- * @hi: maximum allowable value
- *
- * This macro does no typechecking and uses temporary variables of whatever
- * type the input argument @val is.  This is useful when @val is an unsigned
- * type and @lo and @hi are literals that will otherwise be assigned a signed
- * integer type.
- */
-#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
-
-
 /**
  * swap - swap values of @a and @b
  * @a: first value
diff --git a/include/linux/minmax.h b/include/linux/minmax.h
new file mode 100644
index 000000000000..bfd6ad822914
--- /dev/null
+++ b/include/linux/minmax.h
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_MINMAX_H
+#define _LINUX_MINMAX_H
+
+/*
+ * min()/max()/clamp() macros must accomplish three things:
+ *
+ * - avoid multiple evaluations of the arguments (so side-effects like
+ *   "x++" happen only once) when non-constant.
+ * - perform strict type-checking (to generate warnings instead of
+ *   nasty runtime surprises). See the "unnecessary" pointer comparison
+ *   in __typecheck().
+ * - retain result as a constant expressions when called with only
+ *   constant expressions (to avoid tripping VLA warnings in stack
+ *   allocation usage).
+ */
+#define __typecheck(x, y) \
+	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
+
+/*
+ * This returns a constant expression while determining if an argument is
+ * a constant expression, most importantly without evaluating the argument.
+ * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
+ */
+#define __is_constexpr(x) \
+	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
+
+#define __no_side_effects(x, y) \
+		(__is_constexpr(x) && __is_constexpr(y))
+
+#define __safe_cmp(x, y) \
+		(__typecheck(x, y) && __no_side_effects(x, y))
+
+#define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
+
+#define __cmp_once(x, y, unique_x, unique_y, op) ({	\
+		typeof(x) unique_x = (x);		\
+		typeof(y) unique_y = (y);		\
+		__cmp(unique_x, unique_y, op); })
+
+#define __careful_cmp(x, y, op) \
+	__builtin_choose_expr(__safe_cmp(x, y), \
+		__cmp(x, y, op), \
+		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
+
+/**
+ * min - return minimum of two values of the same or compatible types
+ * @x: first value
+ * @y: second value
+ */
+#define min(x, y)	__careful_cmp(x, y, <)
+
+/**
+ * max - return maximum of two values of the same or compatible types
+ * @x: first value
+ * @y: second value
+ */
+#define max(x, y)	__careful_cmp(x, y, >)
+
+/**
+ * min3 - return minimum of three values
+ * @x: first value
+ * @y: second value
+ * @z: third value
+ */
+#define min3(x, y, z) min((typeof(x))min(x, y), z)
+
+/**
+ * max3 - return maximum of three values
+ * @x: first value
+ * @y: second value
+ * @z: third value
+ */
+#define max3(x, y, z) max((typeof(x))max(x, y), z)
+
+/**
+ * min_not_zero - return the minimum that is _not_ zero, unless both are zero
+ * @x: value1
+ * @y: value2
+ */
+#define min_not_zero(x, y) ({			\
+	typeof(x) __x = (x);			\
+	typeof(y) __y = (y);			\
+	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
+
+/**
+ * clamp - return a value clamped to a given range with strict typechecking
+ * @val: current value
+ * @lo: lowest allowable value
+ * @hi: highest allowable value
+ *
+ * This macro does strict typechecking of @lo/@hi to make sure they are of the
+ * same type as @val.  See the unnecessary pointer comparisons.
+ */
+#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
+
+/*
+ * ..and if you can't take the strict
+ * types, you can specify one yourself.
+ *
+ * Or not use min/max/clamp at all, of course.
+ */
+
+/**
+ * min_t - return minimum of two values, using the specified type
+ * @type: data type to use
+ * @x: first value
+ * @y: second value
+ */
+#define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
+
+/**
+ * max_t - return maximum of two values, using the specified type
+ * @type: data type to use
+ * @x: first value
+ * @y: second value
+ */
+#define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
+
+/**
+ * clamp_t - return a value clamped to a given range using a given type
+ * @type: the type of variable to use
+ * @val: current value
+ * @lo: minimum allowable value
+ * @hi: maximum allowable value
+ *
+ * This macro does no typechecking and uses temporary variables of type
+ * @type to make all the comparisons.
+ */
+#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
+
+/**
+ * clamp_val - return a value clamped to a given range using val's type
+ * @val: current value
+ * @lo: minimum allowable value
+ * @hi: maximum allowable value
+ *
+ * This macro does no typechecking and uses temporary variables of whatever
+ * type the input argument @val is.  This is useful when @val is an unsigned
+ * type and @lo and @hi are literals that will otherwise be assigned a signed
+ * integer type.
+ */
+#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
+
+#endif	/* _LINUX_MINMAX_H */
diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
index 27e7fa36f707..7f38399cc9fe 100644
--- a/include/linux/nodemask.h
+++ b/include/linux/nodemask.h
@@ -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;
diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
index ec980bbb786a..6d21d1d87a41 100644
--- a/include/linux/uaccess.h
+++ b/include/linux/uaccess.h
@@ -3,6 +3,7 @@
 #define __LINUX_UACCESS_H__
 
 #include <linux/instrumented.h>
+#include <linux/minmax.h>
 #include <linux/sched.h>
 #include <linux/thread_info.h>
 
diff --git a/kernel/range.c b/kernel/range.c
index d84de6766472..56435f96da73 100644
--- a/kernel/range.c
+++ b/kernel/range.c
@@ -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>
diff --git a/lib/find_bit.c b/lib/find_bit.c
index 49f875f1baf7..4a8751010d59 100644
--- a/lib/find_bit.c
+++ b/lib/find_bit.c
@@ -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) ||	\
diff --git a/lib/hexdump.c b/lib/hexdump.c
index 147133f8eb2f..9301578f98e8 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -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>
 
diff --git a/lib/math/rational.c b/lib/math/rational.c
index df75c8809693..9781d521963d 100644
--- a/lib/math/rational.c
+++ b/lib/math/rational.c
@@ -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
diff --git a/lib/math/reciprocal_div.c b/lib/math/reciprocal_div.c
index bf043258fa00..32436dd4171e 100644
--- a/lib/math/reciprocal_div.c
+++ b/lib/math/reciprocal_div.c
@@ -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
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 2/8] resource: Simplify region_intersects() by reducing conditionals
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
@ 2020-08-17 16:36 ` Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 3/8] resource: Group resource_overlaps() with other inline helpers Andy Shevchenko
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi; +Cc: Andy Shevchenko

Now we have for 'other' and 'type' variables

other	type	return
  0	  0	REGION_DISJOINT
  0	  x	REGION_INTERSECTS
  x	  0	REGION_DISJOINT
  x	  x	REGION_MIXED

Obviously it's easier to check 'type' for 0 first instead of
currently checked 'other'.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v2: converted ternary to usual if-cond (Rafael)
 kernel/resource.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/resource.c b/kernel/resource.c
index 841737bbda9e..be134bd5d82e 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -554,13 +554,13 @@ int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 	}
 	read_unlock(&resource_lock);
 
-	if (other == 0)
-		return type ? REGION_INTERSECTS : REGION_DISJOINT;
+	if (type == 0)
+		return REGION_DISJOINT;
 
-	if (type)
-		return REGION_MIXED;
+	if (other == 0)
+		return REGION_INTERSECTS;
 
-	return REGION_DISJOINT;
+	return REGION_MIXED;
 }
 EXPORT_SYMBOL_GPL(region_intersects);
 
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 3/8] resource: Group resource_overlaps() with other inline helpers
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 2/8] resource: Simplify region_intersects() by reducing conditionals Andy Shevchenko
@ 2020-08-17 16:36 ` Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 4/8] resource: Introduce resource_union() for overlapping resources Andy Shevchenko
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi; +Cc: Andy Shevchenko

For better maintenance group resource_overlaps() with other inline helpers.
While at it, drop extra parentheses.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v2: dropped parentheses (Rafael)
 include/linux/ioport.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index 6c2b06fe8beb..50fed618d3fb 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -226,6 +226,11 @@ static inline bool resource_contains(struct resource *r1, struct resource *r2)
 	return r1->start <= r2->start && r1->end >= r2->end;
 }
 
+/* True if any part of r1 overlaps r2 */
+static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
+{
+       return r1->start <= r2->end && r1->end >= r2->start;
+}
 
 /* Convenience shorthand with allocation */
 #define request_region(start,n,name)		__request_region(&ioport_resource, (start), (n), (name), 0)
@@ -291,12 +296,6 @@ extern int
 walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end,
 		    void *arg, int (*func)(struct resource *, void *));
 
-/* True if any part of r1 overlaps r2 */
-static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
-{
-       return (r1->start <= r2->end && r1->end >= r2->start);
-}
-
 struct resource *devm_request_free_mem_region(struct device *dev,
 		struct resource *base, unsigned long size);
 struct resource *request_free_mem_region(struct resource *base,
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 4/8] resource: Introduce resource_union() for overlapping resources
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 2/8] resource: Simplify region_intersects() by reducing conditionals Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 3/8] resource: Group resource_overlaps() with other inline helpers Andy Shevchenko
@ 2020-08-17 16:36 ` Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 5/8] resource: Introduce resource_intersection() " Andy Shevchenko
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi
  Cc: Andy Shevchenko, Mika Westerberg, Kuppuswamy Sathyanarayanan,
	Bjorn Helgaas, linux-pci

Some already present users may utilize resource_union() helper.
Provide it for them and for wider use in the future.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Mika Westerberg <mika.westerberg@linux.intel.com>
Cc: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: linux-pci@vger.kernel.org
---
v2: reused min/max (Rafael)
 include/linux/ioport.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index 50fed618d3fb..a7d50b9a3406 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -10,9 +10,10 @@
 #define _LINUX_IOPORT_H
 
 #ifndef __ASSEMBLY__
+#include <linux/bits.h>
 #include <linux/compiler.h>
+#include <linux/minmax.h>
 #include <linux/types.h>
-#include <linux/bits.h>
 /*
  * Resources are tree-like, allowing
  * nesting etc..
@@ -232,6 +233,16 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
        return r1->start <= r2->end && r1->end >= r2->start;
 }
 
+static inline bool
+resource_union(struct resource *r1, struct resource *r2, struct resource *r)
+{
+	if (!resource_overlaps(r1, r2))
+		return false;
+	r->start = min(r1->start, r2->start);
+	r->end = max(r1->end, r2->end);
+	return true;
+}
+
 /* Convenience shorthand with allocation */
 #define request_region(start,n,name)		__request_region(&ioport_resource, (start), (n), (name), 0)
 #define request_muxed_region(start,n,name)	__request_region(&ioport_resource, (start), (n), (name), IORESOURCE_MUXED)
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 5/8] resource: Introduce resource_intersection() for overlapping resources
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
                   ` (2 preceding siblings ...)
  2020-08-17 16:36 ` [PATCH v2 4/8] resource: Introduce resource_union() for overlapping resources Andy Shevchenko
@ 2020-08-17 16:36 ` Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 6/8] PCI/ACPI: Replace open coded variant of resource_union() Andy Shevchenko
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi; +Cc: Andy Shevchenko

There will be at least one user that can utilize new helper.
Provide the helper for future user and for wider use.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
v2: reused min/max (Rafael)
 include/linux/ioport.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/include/linux/ioport.h b/include/linux/ioport.h
index a7d50b9a3406..b4c3ca229f66 100644
--- a/include/linux/ioport.h
+++ b/include/linux/ioport.h
@@ -233,6 +233,16 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
        return r1->start <= r2->end && r1->end >= r2->start;
 }
 
+static inline bool
+resource_intersection(struct resource *r1, struct resource *r2, struct resource *r)
+{
+	if (!resource_overlaps(r1, r2))
+		return false;
+	r->start = max(r1->start, r2->start);
+	r->end = min(r1->end, r2->end);
+	return true;
+}
+
 static inline bool
 resource_union(struct resource *r1, struct resource *r2, struct resource *r)
 {
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 6/8] PCI/ACPI: Replace open coded variant of resource_union()
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
                   ` (3 preceding siblings ...)
  2020-08-17 16:36 ` [PATCH v2 5/8] resource: Introduce resource_intersection() " Andy Shevchenko
@ 2020-08-17 16:36 ` Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 7/8] PCI/ACPI: Fix description of @handle for acpi_is_root_bridge() Andy Shevchenko
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi
  Cc: Andy Shevchenko, Kuppuswamy Sathyanarayanan, Bjorn Helgaas,
	linux-pci, Rafael J . Wysocki

Since we have resource_union() helper, let's utilize it here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: linux-pci@vger.kernel.org
Reviewed-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
v2: added Rb tag (Rafael)
 drivers/acpi/pci_root.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/acpi/pci_root.c b/drivers/acpi/pci_root.c
index f90e841c59f5..2a6a741896de 100644
--- a/drivers/acpi/pci_root.c
+++ b/drivers/acpi/pci_root.c
@@ -724,9 +724,7 @@ static void acpi_pci_root_validate_resources(struct device *dev,
 			 * our resources no longer match the ACPI _CRS, but
 			 * the kernel resource tree doesn't allow overlaps.
 			 */
-			if (resource_overlaps(res1, res2)) {
-				res2->start = min(res1->start, res2->start);
-				res2->end = max(res1->end, res2->end);
+			if (resource_union(res1, res2, res2)) {
 				dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
 					 res2, res1);
 				free = true;
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 7/8] PCI/ACPI: Fix description of @handle for acpi_is_root_bridge()
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
                   ` (4 preceding siblings ...)
  2020-08-17 16:36 ` [PATCH v2 6/8] PCI/ACPI: Replace open coded variant of resource_union() Andy Shevchenko
@ 2020-08-17 16:36 ` Andy Shevchenko
  2020-08-17 16:36 ` [PATCH v2 8/8] ACPI: watchdog: Replace open coded variant of resource_union() Andy Shevchenko
  2020-08-27 11:44 ` [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
  7 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi
  Cc: Andy Shevchenko, Kuppuswamy Sathyanarayanan, Bjorn Helgaas, linux-pci

Fix description of handle parameter in documentation of acpi_is_root_bridge().
Otherwise we get the following warning:

  CHECK   drivers/acpi/pci_root.c
  drivers/acpi/pci_root.c:71: warning: Function parameter or member 'handle' not described in 'acpi_is_root_bridge'

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Kuppuswamy Sathyanarayanan <sathyanarayanan.kuppuswamy@linux.intel.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: linux-pci@vger.kernel.org
---
v2: left in the series, but maintainers still may apply it separately
 drivers/acpi/pci_root.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/acpi/pci_root.c b/drivers/acpi/pci_root.c
index 2a6a741896de..f723679954d7 100644
--- a/drivers/acpi/pci_root.c
+++ b/drivers/acpi/pci_root.c
@@ -62,7 +62,7 @@ static DEFINE_MUTEX(osc_lock);
 
 /**
  * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
- * @handle - the ACPI CA node in question.
+ * @handle: the ACPI CA node in question.
  *
  * Note: we could make this API take a struct acpi_device * instead, but
  * for now, it's more convenient to operate on an acpi_handle.
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v2 8/8] ACPI: watchdog: Replace open coded variant of resource_union()
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
                   ` (5 preceding siblings ...)
  2020-08-17 16:36 ` [PATCH v2 7/8] PCI/ACPI: Fix description of @handle for acpi_is_root_bridge() Andy Shevchenko
@ 2020-08-17 16:36 ` Andy Shevchenko
  2020-08-17 16:54   ` Mika Westerberg
  2020-08-27 11:44 ` [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
  7 siblings, 1 reply; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-17 16:36 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi
  Cc: Andy Shevchenko, Mika Westerberg, Rafael J . Wysocki

Since we have resource_union() helper, let's utilize it here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
v2: added Rb tag (Rafael)
 drivers/acpi/acpi_watchdog.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/acpi/acpi_watchdog.c b/drivers/acpi/acpi_watchdog.c
index 5c1e9ea43123..ca28183f4d13 100644
--- a/drivers/acpi/acpi_watchdog.c
+++ b/drivers/acpi/acpi_watchdog.c
@@ -151,11 +151,7 @@ void __init acpi_watchdog_init(void)
 		found = false;
 		resource_list_for_each_entry(rentry, &resource_list) {
 			if (rentry->res->flags == res.flags &&
-			    resource_overlaps(rentry->res, &res)) {
-				if (res.start < rentry->res->start)
-					rentry->res->start = res.start;
-				if (res.end > rentry->res->end)
-					rentry->res->end = res.end;
+			    resource_union(rentry->res, &res, rentry->res)) {
 				found = true;
 				break;
 			}
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 8/8] ACPI: watchdog: Replace open coded variant of resource_union()
  2020-08-17 16:36 ` [PATCH v2 8/8] ACPI: watchdog: Replace open coded variant of resource_union() Andy Shevchenko
@ 2020-08-17 16:54   ` Mika Westerberg
  0 siblings, 0 replies; 10+ messages in thread
From: Mika Westerberg @ 2020-08-17 16:54 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Rafael J. Wysocki, Len Brown, linux-acpi, Rafael J . Wysocki

On Mon, Aug 17, 2020 at 07:36:47PM +0300, Andy Shevchenko wrote:
> Since we have resource_union() helper, let's utilize it here.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Mika Westerberg <mika.westerberg@linux.intel.com>

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers
  2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
                   ` (6 preceding siblings ...)
  2020-08-17 16:36 ` [PATCH v2 8/8] ACPI: watchdog: Replace open coded variant of resource_union() Andy Shevchenko
@ 2020-08-27 11:44 ` Andy Shevchenko
  7 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2020-08-27 11:44 UTC (permalink / raw)
  To: Rafael J. Wysocki, Len Brown, linux-acpi

On Mon, Aug 17, 2020 at 07:36:40PM +0300, Andy Shevchenko wrote:
> 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.

Rafael, does it look good now?

> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
> v2: new patch, prerequisite for the rest (Rafael)
>  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
> 
> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
> index bb5636cc17b9..3641c7234afb 100644
> --- a/include/linux/blkdev.h
> +++ b/include/linux/blkdev.h
> @@ -8,6 +8,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>
> diff --git a/include/linux/bvec.h b/include/linux/bvec.h
> index ac0c7299d5b8..8c7b3926b69c 100644
> --- a/include/linux/bvec.h
> +++ b/include/linux/bvec.h
> @@ -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;
>  
>  /**
>   * struct bio_vec - a contiguous range of physical memory addresses
> diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h
> index fed6ba96c527..5e13f801c902 100644
> --- a/include/linux/jiffies.h
> +++ b/include/linux/jiffies.h
> @@ -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>
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index 500def620d8f..397f66bef709 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -11,6 +11,7 @@
>  #include <linux/compiler.h>
>  #include <linux/bitops.h>
>  #include <linux/log2.h>
> +#include <linux/minmax.h>
>  #include <linux/typecheck.h>
>  #include <linux/printk.h>
>  #include <linux/build_bug.h>
> @@ -834,147 +835,6 @@ ftrace_vprintk(const char *fmt, va_list ap)
>  static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
>  #endif /* CONFIG_TRACING */
>  
> -/*
> - * min()/max()/clamp() macros must accomplish three things:
> - *
> - * - avoid multiple evaluations of the arguments (so side-effects like
> - *   "x++" happen only once) when non-constant.
> - * - perform strict type-checking (to generate warnings instead of
> - *   nasty runtime surprises). See the "unnecessary" pointer comparison
> - *   in __typecheck().
> - * - retain result as a constant expressions when called with only
> - *   constant expressions (to avoid tripping VLA warnings in stack
> - *   allocation usage).
> - */
> -#define __typecheck(x, y) \
> -		(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
> -
> -/*
> - * This returns a constant expression while determining if an argument is
> - * a constant expression, most importantly without evaluating the argument.
> - * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
> - */
> -#define __is_constexpr(x) \
> -	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
> -
> -#define __no_side_effects(x, y) \
> -		(__is_constexpr(x) && __is_constexpr(y))
> -
> -#define __safe_cmp(x, y) \
> -		(__typecheck(x, y) && __no_side_effects(x, y))
> -
> -#define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
> -
> -#define __cmp_once(x, y, unique_x, unique_y, op) ({	\
> -		typeof(x) unique_x = (x);		\
> -		typeof(y) unique_y = (y);		\
> -		__cmp(unique_x, unique_y, op); })
> -
> -#define __careful_cmp(x, y, op) \
> -	__builtin_choose_expr(__safe_cmp(x, y), \
> -		__cmp(x, y, op), \
> -		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
> -
> -/**
> - * min - return minimum of two values of the same or compatible types
> - * @x: first value
> - * @y: second value
> - */
> -#define min(x, y)	__careful_cmp(x, y, <)
> -
> -/**
> - * max - return maximum of two values of the same or compatible types
> - * @x: first value
> - * @y: second value
> - */
> -#define max(x, y)	__careful_cmp(x, y, >)
> -
> -/**
> - * min3 - return minimum of three values
> - * @x: first value
> - * @y: second value
> - * @z: third value
> - */
> -#define min3(x, y, z) min((typeof(x))min(x, y), z)
> -
> -/**
> - * max3 - return maximum of three values
> - * @x: first value
> - * @y: second value
> - * @z: third value
> - */
> -#define max3(x, y, z) max((typeof(x))max(x, y), z)
> -
> -/**
> - * min_not_zero - return the minimum that is _not_ zero, unless both are zero
> - * @x: value1
> - * @y: value2
> - */
> -#define min_not_zero(x, y) ({			\
> -	typeof(x) __x = (x);			\
> -	typeof(y) __y = (y);			\
> -	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
> -
> -/**
> - * clamp - return a value clamped to a given range with strict typechecking
> - * @val: current value
> - * @lo: lowest allowable value
> - * @hi: highest allowable value
> - *
> - * This macro does strict typechecking of @lo/@hi to make sure they are of the
> - * same type as @val.  See the unnecessary pointer comparisons.
> - */
> -#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
> -
> -/*
> - * ..and if you can't take the strict
> - * types, you can specify one yourself.
> - *
> - * Or not use min/max/clamp at all, of course.
> - */
> -
> -/**
> - * min_t - return minimum of two values, using the specified type
> - * @type: data type to use
> - * @x: first value
> - * @y: second value
> - */
> -#define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
> -
> -/**
> - * max_t - return maximum of two values, using the specified type
> - * @type: data type to use
> - * @x: first value
> - * @y: second value
> - */
> -#define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
> -
> -/**
> - * clamp_t - return a value clamped to a given range using a given type
> - * @type: the type of variable to use
> - * @val: current value
> - * @lo: minimum allowable value
> - * @hi: maximum allowable value
> - *
> - * This macro does no typechecking and uses temporary variables of type
> - * @type to make all the comparisons.
> - */
> -#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
> -
> -/**
> - * clamp_val - return a value clamped to a given range using val's type
> - * @val: current value
> - * @lo: minimum allowable value
> - * @hi: maximum allowable value
> - *
> - * This macro does no typechecking and uses temporary variables of whatever
> - * type the input argument @val is.  This is useful when @val is an unsigned
> - * type and @lo and @hi are literals that will otherwise be assigned a signed
> - * integer type.
> - */
> -#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
> -
> -
>  /**
>   * swap - swap values of @a and @b
>   * @a: first value
> diff --git a/include/linux/minmax.h b/include/linux/minmax.h
> new file mode 100644
> index 000000000000..bfd6ad822914
> --- /dev/null
> +++ b/include/linux/minmax.h
> @@ -0,0 +1,145 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _LINUX_MINMAX_H
> +#define _LINUX_MINMAX_H
> +
> +/*
> + * min()/max()/clamp() macros must accomplish three things:
> + *
> + * - avoid multiple evaluations of the arguments (so side-effects like
> + *   "x++" happen only once) when non-constant.
> + * - perform strict type-checking (to generate warnings instead of
> + *   nasty runtime surprises). See the "unnecessary" pointer comparison
> + *   in __typecheck().
> + * - retain result as a constant expressions when called with only
> + *   constant expressions (to avoid tripping VLA warnings in stack
> + *   allocation usage).
> + */
> +#define __typecheck(x, y) \
> +	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
> +
> +/*
> + * This returns a constant expression while determining if an argument is
> + * a constant expression, most importantly without evaluating the argument.
> + * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
> + */
> +#define __is_constexpr(x) \
> +	(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
> +
> +#define __no_side_effects(x, y) \
> +		(__is_constexpr(x) && __is_constexpr(y))
> +
> +#define __safe_cmp(x, y) \
> +		(__typecheck(x, y) && __no_side_effects(x, y))
> +
> +#define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
> +
> +#define __cmp_once(x, y, unique_x, unique_y, op) ({	\
> +		typeof(x) unique_x = (x);		\
> +		typeof(y) unique_y = (y);		\
> +		__cmp(unique_x, unique_y, op); })
> +
> +#define __careful_cmp(x, y, op) \
> +	__builtin_choose_expr(__safe_cmp(x, y), \
> +		__cmp(x, y, op), \
> +		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
> +
> +/**
> + * min - return minimum of two values of the same or compatible types
> + * @x: first value
> + * @y: second value
> + */
> +#define min(x, y)	__careful_cmp(x, y, <)
> +
> +/**
> + * max - return maximum of two values of the same or compatible types
> + * @x: first value
> + * @y: second value
> + */
> +#define max(x, y)	__careful_cmp(x, y, >)
> +
> +/**
> + * min3 - return minimum of three values
> + * @x: first value
> + * @y: second value
> + * @z: third value
> + */
> +#define min3(x, y, z) min((typeof(x))min(x, y), z)
> +
> +/**
> + * max3 - return maximum of three values
> + * @x: first value
> + * @y: second value
> + * @z: third value
> + */
> +#define max3(x, y, z) max((typeof(x))max(x, y), z)
> +
> +/**
> + * min_not_zero - return the minimum that is _not_ zero, unless both are zero
> + * @x: value1
> + * @y: value2
> + */
> +#define min_not_zero(x, y) ({			\
> +	typeof(x) __x = (x);			\
> +	typeof(y) __y = (y);			\
> +	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
> +
> +/**
> + * clamp - return a value clamped to a given range with strict typechecking
> + * @val: current value
> + * @lo: lowest allowable value
> + * @hi: highest allowable value
> + *
> + * This macro does strict typechecking of @lo/@hi to make sure they are of the
> + * same type as @val.  See the unnecessary pointer comparisons.
> + */
> +#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
> +
> +/*
> + * ..and if you can't take the strict
> + * types, you can specify one yourself.
> + *
> + * Or not use min/max/clamp at all, of course.
> + */
> +
> +/**
> + * min_t - return minimum of two values, using the specified type
> + * @type: data type to use
> + * @x: first value
> + * @y: second value
> + */
> +#define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
> +
> +/**
> + * max_t - return maximum of two values, using the specified type
> + * @type: data type to use
> + * @x: first value
> + * @y: second value
> + */
> +#define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
> +
> +/**
> + * clamp_t - return a value clamped to a given range using a given type
> + * @type: the type of variable to use
> + * @val: current value
> + * @lo: minimum allowable value
> + * @hi: maximum allowable value
> + *
> + * This macro does no typechecking and uses temporary variables of type
> + * @type to make all the comparisons.
> + */
> +#define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
> +
> +/**
> + * clamp_val - return a value clamped to a given range using val's type
> + * @val: current value
> + * @lo: minimum allowable value
> + * @hi: maximum allowable value
> + *
> + * This macro does no typechecking and uses temporary variables of whatever
> + * type the input argument @val is.  This is useful when @val is an unsigned
> + * type and @lo and @hi are literals that will otherwise be assigned a signed
> + * integer type.
> + */
> +#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
> +
> +#endif	/* _LINUX_MINMAX_H */
> diff --git a/include/linux/nodemask.h b/include/linux/nodemask.h
> index 27e7fa36f707..7f38399cc9fe 100644
> --- a/include/linux/nodemask.h
> +++ b/include/linux/nodemask.h
> @@ -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;
> diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h
> index ec980bbb786a..6d21d1d87a41 100644
> --- a/include/linux/uaccess.h
> +++ b/include/linux/uaccess.h
> @@ -3,6 +3,7 @@
>  #define __LINUX_UACCESS_H__
>  
>  #include <linux/instrumented.h>
> +#include <linux/minmax.h>
>  #include <linux/sched.h>
>  #include <linux/thread_info.h>
>  
> diff --git a/kernel/range.c b/kernel/range.c
> index d84de6766472..56435f96da73 100644
> --- a/kernel/range.c
> +++ b/kernel/range.c
> @@ -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>
> diff --git a/lib/find_bit.c b/lib/find_bit.c
> index 49f875f1baf7..4a8751010d59 100644
> --- a/lib/find_bit.c
> +++ b/lib/find_bit.c
> @@ -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) ||	\
> diff --git a/lib/hexdump.c b/lib/hexdump.c
> index 147133f8eb2f..9301578f98e8 100644
> --- a/lib/hexdump.c
> +++ b/lib/hexdump.c
> @@ -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>
>  
> diff --git a/lib/math/rational.c b/lib/math/rational.c
> index df75c8809693..9781d521963d 100644
> --- a/lib/math/rational.c
> +++ b/lib/math/rational.c
> @@ -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
> diff --git a/lib/math/reciprocal_div.c b/lib/math/reciprocal_div.c
> index bf043258fa00..32436dd4171e 100644
> --- a/lib/math/reciprocal_div.c
> +++ b/lib/math/reciprocal_div.c
> @@ -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
> -- 
> 2.28.0
> 

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-08-27 11:46 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-17 16:36 [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko
2020-08-17 16:36 ` [PATCH v2 2/8] resource: Simplify region_intersects() by reducing conditionals Andy Shevchenko
2020-08-17 16:36 ` [PATCH v2 3/8] resource: Group resource_overlaps() with other inline helpers Andy Shevchenko
2020-08-17 16:36 ` [PATCH v2 4/8] resource: Introduce resource_union() for overlapping resources Andy Shevchenko
2020-08-17 16:36 ` [PATCH v2 5/8] resource: Introduce resource_intersection() " Andy Shevchenko
2020-08-17 16:36 ` [PATCH v2 6/8] PCI/ACPI: Replace open coded variant of resource_union() Andy Shevchenko
2020-08-17 16:36 ` [PATCH v2 7/8] PCI/ACPI: Fix description of @handle for acpi_is_root_bridge() Andy Shevchenko
2020-08-17 16:36 ` [PATCH v2 8/8] ACPI: watchdog: Replace open coded variant of resource_union() Andy Shevchenko
2020-08-17 16:54   ` Mika Westerberg
2020-08-27 11:44 ` [PATCH v2 1/8] kernel.h: Split out min()/max() et al. helpers Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).