diff mbox series

[03/16] tools/include: Add _RET_IP_ and math definitions to kernel.h

Message ID 5baa036aadb6436c7c36589ce591baaf827aec0b.1643206612.git.karolinadrobnik@gmail.com (mailing list archive)
State New
Headers show
Series Introduce memblock simulator | expand

Commit Message

Karolina Drobnik Jan. 27, 2022, 1:21 p.m. UTC
Add max_t, min_t and clamp functions, together with _RET_IP_
definition, so they can be used in testing.

Signed-off-by: Karolina Drobnik <karolinadrobnik@gmail.com>
---
 tools/include/linux/kernel.h | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Matthew Wilcox Jan. 27, 2022, 1:54 p.m. UTC | #1
On Thu, Jan 27, 2022 at 02:21:21PM +0100, Karolina Drobnik wrote:
> Add max_t, min_t and clamp functions, together with _RET_IP_
> definition, so they can be used in testing.

Rather than adding our own definitions of min/max/clamp, have
you considered using #include "../../../include/linux/minmax.h"?
In my experience reusing this kind of "leaf" header works out
better than duplicating it.
Karolina Drobnik Jan. 28, 2022, 11:05 a.m. UTC | #2
On Thu, 2022-01-27 at 13:54 +0000, Matthew Wilcox wrote:
> On Thu, Jan 27, 2022 at 02:21:21PM +0100, Karolina Drobnik wrote:
> > Add max_t, min_t and clamp functions, together with _RET_IP_
> > definition, so they can be used in testing.
> 
> Rather than adding our own definitions of min/max/clamp, have
> you considered using #include "../../../include/linux/minmax.h"?

I tried doing it in the very beginning and couldn't get it to compile.
Now, I see it's because in minmax.h we use __UNIQUE_ID, which is
undefined because of "#ifdef __KERNEL__" in include/linux/compiler.h.

> In my experience reusing this kind of "leaf" header works out
> better than duplicating it.

I like this approach as well, and I tried to use it in other memblock
headers. Still, as it's not that straightforward here, and I went for
duplication.
diff mbox series

Patch

diff --git a/tools/include/linux/kernel.h b/tools/include/linux/kernel.h
index 9701e8307db0..4b0673bf52c2 100644
--- a/tools/include/linux/kernel.h
+++ b/tools/include/linux/kernel.h
@@ -15,6 +15,8 @@ 
 #define UINT_MAX	(~0U)
 #endif
 
+#define _RET_IP_		((unsigned long)__builtin_return_address(0))
+
 #define PERF_ALIGN(x, a)	__PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
 #define __PERF_ALIGN_MASK(x, mask)	(((x)+(mask))&~(mask))
 
@@ -51,6 +53,10 @@ 
 	_min1 < _min2 ? _min1 : _min2; })
 #endif
 
+#define max_t(type, x, y)	max((type)x, (type)y)
+#define min_t(type, x, y)	min((type)x, (type)y)
+#define clamp(val, lo, hi)	min((typeof(val))max(val, lo), hi)
+
 #ifndef BUG_ON
 #ifdef NDEBUG
 #define BUG_ON(cond) do { if (cond) {} } while (0)