[PATCH RFC net-next 1/3] uapi: add INT_MAX and INT_MIN constants

Thomas Weißschuh posted 3 patches 1 month ago
There is a newer version of this series
[PATCH RFC net-next 1/3] uapi: add INT_MAX and INT_MIN constants
Posted by Thomas Weißschuh 1 month ago
Some UAPI headers use INT_MAX and INT_MIN. Currently they include
<limits.h> for their definitions, which introduces a problematic
dependency on libc.

Add custom, namespaced definitions of INT_MAX and INT_MIN using the
same values as the regular kernel code.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
---
 include/uapi/linux/limits.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/include/uapi/linux/limits.h b/include/uapi/linux/limits.h
index 6bcbe3068761..35ffa2667309 100644
--- a/include/uapi/linux/limits.h
+++ b/include/uapi/linux/limits.h
@@ -18,4 +18,7 @@
 
 #define RTSIG_MAX	  32
 
+#define __KERNEL_INT_MAX ((int)(~0U >> 1))
+#define __KERNEL_INT_MIN (-__KERNEL_INT_MAX - 1)
+
 #endif

-- 
2.52.0

Re: [PATCH RFC net-next 1/3] uapi: add INT_MAX and INT_MIN constants
Posted by Andrew Lunn 1 month ago
On Mon, Jan 05, 2026 at 09:26:47AM +0100, Thomas Weißschuh wrote:
> Some UAPI headers use INT_MAX and INT_MIN. Currently they include
> <limits.h> for their definitions, which introduces a problematic
> dependency on libc.
> 
> Add custom, namespaced definitions of INT_MAX and INT_MIN using the
> same values as the regular kernel code.

Maybe a dumb question.

> +#define __KERNEL_INT_MAX ((int)(~0U >> 1))
> +#define __KERNEL_INT_MIN (-__KERNEL_INT_MAX - 1)

How does this work for a 32 bit userspace on top of a 64 bit kernel?

And do we need to be careful with KERNEL in the name, in that for a 32
bit userspace, this is going to be 32bit max int, when in fact the
kernel is using 64 bit max int?

       Andrew
Re: [PATCH RFC net-next 1/3] uapi: add INT_MAX and INT_MIN constants
Posted by Thomas Weißschuh 1 month ago
On Mon, Jan 05, 2026 at 03:37:14PM +0100, Andrew Lunn wrote:
> On Mon, Jan 05, 2026 at 09:26:47AM +0100, Thomas Weißschuh wrote:
> > Some UAPI headers use INT_MAX and INT_MIN. Currently they include
> > <limits.h> for their definitions, which introduces a problematic
> > dependency on libc.
> > 
> > Add custom, namespaced definitions of INT_MAX and INT_MIN using the
> > same values as the regular kernel code.
> 
> Maybe a dumb question.
> 
> > +#define __KERNEL_INT_MAX ((int)(~0U >> 1))
> > +#define __KERNEL_INT_MIN (-__KERNEL_INT_MAX - 1)
> 
> How does this work for a 32 bit userspace on top of a 64 bit kernel?
>
> And do we need to be careful with KERNEL in the name, in that for a 32
> bit userspace, this is going to be 32bit max int, when in fact the
> kernel is using 64 bit max int?

'int' is always 32 bit on Linux.


Thomas