git: c7312643b7e5 - main - LinuxKPI: Add ida_alloc_min()

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Sun, 24 Dec 2023 08:23:08 UTC
The branch main has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=c7312643b7e5f01adc2a3094c5139f5dcab5f0a4

commit c7312643b7e5f01adc2a3094c5139f5dcab5f0a4
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2023-12-24 08:19:59 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2023-12-24 08:19:59 +0000

    LinuxKPI: Add ida_alloc_min()
    
    ida_alloc_min() allocates an unused ID. between min and INT_MAX.
    
    While here allow end parameter of ida_simple_get() be larger than
    INT_MAX. Linux caps the value to INT_MAX.
    
    Sponsored by:   Serenity Cyber Security, LLC
    Reviewers:      manu, bz
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D42806
---
 sys/compat/linuxkpi/common/include/linux/idr.h | 7 +++++++
 sys/compat/linuxkpi/common/src/linux_idr.c     | 5 ++---
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/idr.h b/sys/compat/linuxkpi/common/include/linux/idr.h
index 2fb2e1734878..ca3f8171ff44 100644
--- a/sys/compat/linuxkpi/common/include/linux/idr.h
+++ b/sys/compat/linuxkpi/common/include/linux/idr.h
@@ -31,6 +31,7 @@
 
 #include <sys/param.h>
 #include <sys/lock.h>
+#include <sys/limits.h>
 #include <sys/mutex.h>
 
 #include <linux/types.h>
@@ -131,6 +132,12 @@ ida_get_new(struct ida *ida, int *p_id)
 	return (ida_get_new_above(ida, 0, p_id));
 }
 
+static inline int
+ida_alloc_min(struct ida *ida, unsigned int min, gfp_t gfp)
+{
+	return (ida_simple_get(ida, min, UINT_MAX, gfp));
+}
+
 static inline int
 ida_alloc_max(struct ida *ida, unsigned int max, gfp_t gfp)
 {
diff --git a/sys/compat/linuxkpi/common/src/linux_idr.c b/sys/compat/linuxkpi/common/src/linux_idr.c
index 3be106150513..dc64da0d7cf5 100644
--- a/sys/compat/linuxkpi/common/src/linux_idr.c
+++ b/sys/compat/linuxkpi/common/src/linux_idr.c
@@ -754,10 +754,9 @@ ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
 	unsigned int max;
 
 	MPASS((int)start >= 0);
-	MPASS((int)end >= 0);
 
-	if (end == 0)
-		max = 0x80000000;
+	if ((int)end <= 0)
+		max = INT_MAX;
 	else {
 		MPASS(end > start);
 		max = end - 1;