git: ebe210ca703b - stable/14 - LinuxKPI; cleanup slab.h a bit; move more free() into slab.c

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Fri, 18 Apr 2025 14:36:56 UTC
The branch stable/14 has been updated by bz:

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

commit ebe210ca703bd584274396b2c0fa1694750f05c1
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2025-03-29 17:33:08 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2025-04-18 14:35:58 +0000

    LinuxKPI; cleanup slab.h a bit; move more free() into slab.c
    
    Move kfree() into slab.c as an implementation and hide the private
    function linux_kfree_async() entirely.
    
    Remove a ; at the end of a define and sort some defines into place.
    
    Remove extern from function declarations and move the closer to where
    they belong.
    
    Sort the functions into "base allocator/free" functions--these have
    an implementation in slab.c and are ensuring contiguous physical
    memory allocations.
    
    Followed by inline functions using these base allocators to implement
    their functionality; vmalloc/kvalloc, and misc functions.
    
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    dumbbell
    Differential Revision: https://reviews.freebsd.org/D49572
    
    (cherry picked from commit a3e6f97bf57c1d323487d07369aec66542f995ce)
---
 sys/compat/linuxkpi/common/include/linux/slab.h | 111 ++++++++++++++----------
 sys/compat/linuxkpi/common/src/linux_slab.c     |  19 +++-
 2 files changed, 84 insertions(+), 46 deletions(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/slab.h b/sys/compat/linuxkpi/common/include/linux/slab.h
index a692ccb8b944..f3a840d9bf4b 100644
--- a/sys/compat/linuxkpi/common/include/linux/slab.h
+++ b/sys/compat/linuxkpi/common/include/linux/slab.h
@@ -4,6 +4,10 @@
  * Copyright (c) 2010 Panasas, Inc.
  * Copyright (c) 2013-2021 Mellanox Technologies, Ltd.
  * All rights reserved.
+ * Copyright (c) 2024-2025 The FreeBSD Foundation
+ *
+ * Portions of this software were developed by Björn Zeeb
+ * under sponsorship from the FreeBSD Foundation.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -41,12 +45,12 @@
 
 MALLOC_DECLARE(M_KMALLOC);
 
-#define	kmalloc(size, flags)		lkpi_kmalloc(size, flags)
 #define	kvzalloc(size, flags)		kmalloc(size, (flags) | __GFP_ZERO)
 #define	kvcalloc(n, size, flags)	kvmalloc_array(n, size, (flags) | __GFP_ZERO)
 #define	kzalloc(size, flags)		kmalloc(size, (flags) | __GFP_ZERO)
 #define	kzalloc_node(size, flags, node)	kmalloc_node(size, (flags) | __GFP_ZERO, node)
 #define	kfree_const(ptr)		kfree(ptr)
+#define kfree_async(ptr)		kfree(ptr)		/* drm-kmod 5.4 compat */
 #define	vzalloc(size)			__vmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0)
 #define	vfree(arg)			kfree(arg)
 #define	kvfree(arg)			kfree(arg)
@@ -84,17 +88,21 @@ struct linux_kmem_cache;
 #define	ARCH_KMALLOC_MINALIGN \
 	__alignof(unsigned long long)
 
-/* drm-kmod 5.4 compat */
-#define kfree_async(ptr)	kfree(ptr);
-
 #define	ZERO_SIZE_PTR		((void *)16)
 #define ZERO_OR_NULL_PTR(x)	((x) == NULL || (x) == ZERO_SIZE_PTR)
 
-extern void *lkpi_kmalloc(size_t size, gfp_t flags);
-void *lkpi___kmalloc(size_t size, gfp_t flags);
-void *lkpi___kmalloc_node(size_t size, gfp_t flags, int node);
-#define	__kmalloc(_s, _f)	lkpi___kmalloc(_s, _f)
+struct linux_kmem_cache *linux_kmem_cache_create(const char *name,
+    size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor);
+void *lkpi_kmem_cache_alloc(struct linux_kmem_cache *, gfp_t);
+void *lkpi_kmem_cache_zalloc(struct linux_kmem_cache *, gfp_t);
+void lkpi_kmem_cache_free(struct linux_kmem_cache *, void *);
+void linux_kmem_cache_destroy(struct linux_kmem_cache *);
+
+void *lkpi_kmalloc(size_t, gfp_t);
+void *lkpi___kmalloc(size_t, gfp_t);
+void *lkpi___kmalloc_node(size_t, gfp_t, int);
 void *lkpi_krealloc(void *, size_t, gfp_t);
+void lkpi_kfree(const void *);
 
 static inline gfp_t
 linux_check_m_flags(gfp_t flags)
@@ -111,12 +119,43 @@ linux_check_m_flags(gfp_t flags)
 	return (flags & GFP_NATIVE_MASK);
 }
 
+/*
+ * Base functions with a native implementation.
+ */
+static inline void *
+kmalloc(size_t size, gfp_t flags)
+{
+	return (lkpi_kmalloc(size, flags));
+}
+
+static inline void *
+__kmalloc(size_t size, gfp_t flags)
+{
+	return (lkpi___kmalloc(size, flags));
+}
+
 static inline void *
 kmalloc_node(size_t size, gfp_t flags, int node)
 {
 	return (lkpi___kmalloc_node(size, flags, node));
 }
 
+static inline void *
+krealloc(void *ptr, size_t size, gfp_t flags)
+{
+	return (lkpi_krealloc(ptr, size, flags));
+}
+
+static inline void
+kfree(const void *ptr)
+{
+	lkpi_kfree(ptr);
+}
+
+/*
+ * Other k*alloc() funtions using the above as underlying allocator.
+ */
+/* kmalloc */
 static inline void *
 kmalloc_array(size_t n, size_t size, gfp_t flags)
 {
@@ -130,9 +169,10 @@ static inline void *
 kcalloc(size_t n, size_t size, gfp_t flags)
 {
 	flags |= __GFP_ZERO;
-	return (kmalloc_array(n, size, linux_check_m_flags(flags)));
+	return (kmalloc_array(n, size, flags));
 }
 
+/* kmalloc_node */
 static inline void *
 kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node)
 {
@@ -149,12 +189,7 @@ kcalloc_node(size_t n, size_t size, gfp_t flags, int node)
 	return (kmalloc_array_node(n, size, flags, node));
 }
 
-static inline void *
-krealloc(void *ptr, size_t size, gfp_t flags)
-{
-	return (lkpi_krealloc(ptr, size, flags));
-}
-
+/* krealloc */
 static inline void *
 krealloc_array(void *ptr, size_t n, size_t size, gfp_t flags)
 {
@@ -164,6 +199,9 @@ krealloc_array(void *ptr, size_t n, size_t size, gfp_t flags)
 	return (krealloc(ptr, n * size, flags));
 }
 
+/*
+ * vmalloc/kvalloc functions.
+ */
 static inline void *
 __vmalloc(size_t size, gfp_t flags, int other)
 {
@@ -199,29 +237,6 @@ kvmalloc_array(size_t n, size_t size, gfp_t flags)
 	return (kvmalloc(size * n, flags));
 }
 
-extern void linux_kfree_async(void *);
-
-static inline void
-kfree(const void *ptr)
-{
-	if (ZERO_OR_NULL_PTR(ptr))
-		return;
-
-	if (curthread->td_critnest != 0)
-		linux_kfree_async(__DECONST(void *, ptr));
-	else
-		free(__DECONST(void *, ptr), M_KMALLOC);
-}
-
-static __inline void
-kfree_sensitive(const void *ptr)
-{
-	if (ZERO_OR_NULL_PTR(ptr))
-		return;
-
-	zfree(__DECONST(void *, ptr), M_KMALLOC);
-}
-
 static inline void *
 kvrealloc(const void *ptr, size_t oldsize, size_t newsize, gfp_t flags)
 {
@@ -239,6 +254,19 @@ kvrealloc(const void *ptr, size_t oldsize, size_t newsize, gfp_t flags)
 	return (newptr);
 }
 
+/*
+ * Misc.
+ */
+
+static __inline void
+kfree_sensitive(const void *ptr)
+{
+	if (ZERO_OR_NULL_PTR(ptr))
+		return;
+
+	zfree(__DECONST(void *, ptr), M_KMALLOC);
+}
+
 static inline size_t
 ksize(const void *ptr)
 {
@@ -253,11 +281,4 @@ kmalloc_size_roundup(size_t size)
 	return (malloc_size(size));
 }
 
-extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name,
-    size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor);
-extern void *lkpi_kmem_cache_alloc(struct linux_kmem_cache *, gfp_t);
-extern void *lkpi_kmem_cache_zalloc(struct linux_kmem_cache *, gfp_t);
-extern void lkpi_kmem_cache_free(struct linux_kmem_cache *, void *);
-extern void linux_kmem_cache_destroy(struct linux_kmem_cache *);
-
 #endif					/* _LINUXKPI_LINUX_SLAB_H_ */
diff --git a/sys/compat/linuxkpi/common/src/linux_slab.c b/sys/compat/linuxkpi/common/src/linux_slab.c
index abcffdb094f5..3d75ca480661 100644
--- a/sys/compat/linuxkpi/common/src/linux_slab.c
+++ b/sys/compat/linuxkpi/common/src/linux_slab.c
@@ -1,6 +1,10 @@
 /*-
  * Copyright (c) 2017 Mellanox Technologies, Ltd.
  * All rights reserved.
+ * Copyright (c) 2024-2025 The FreeBSD Foundation
+ *
+ * Portions of this software were developed by Björn Zeeb
+ * under sponsorship from the FreeBSD Foundation.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -303,7 +307,7 @@ linux_kfree_async_fn(void *context, int pending)
 static struct task linux_kfree_async_task =
     TASK_INITIALIZER(0, linux_kfree_async_fn, &linux_kfree_async_task);
 
-void
+static void
 linux_kfree_async(void *addr)
 {
 	if (addr == NULL)
@@ -311,3 +315,16 @@ linux_kfree_async(void *addr)
 	llist_add(addr, &linux_kfree_async_list);
 	taskqueue_enqueue(linux_irq_work_tq, &linux_kfree_async_task);
 }
+
+void
+lkpi_kfree(const void *ptr)
+{
+	if (ZERO_OR_NULL_PTR(ptr))
+		return;
+
+	if (curthread->td_critnest != 0)
+		linux_kfree_async(__DECONST(void *, ptr));
+	else
+		free(__DECONST(void *, ptr), M_KMALLOC);
+}
+