Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 22 Apr 2026 21:08:58 +0000
From:      Bjoern A. Zeeb <bz@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: e6f14601d39c - stable/15 - LinuxKPI: add kmalloc_obj[s], kzalloc_obj[s], and kzalloc_flex
Message-ID:  <69e938ea.2767d.68d5e87c@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch stable/15 has been updated by bz:

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

commit e6f14601d39c6a6fb41ad725dd4ae2488e4707ce
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2026-04-14 15:13:18 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2026-04-22 20:57:09 +0000

    LinuxKPI: add kmalloc_obj[s], kzalloc_obj[s], and kzalloc_flex
    
    Drivers in Linux v7.0 seem to have changed to the new allocation
    macros using a sweep.  Add the ones I encountered with wireless
    drivers so far.  They all take an optional argument for a gfp_t,
    which default_gfp() deals with.
    
    The plural version "objs" takes an extra nitems argument in addition
    to the size.  We use size_mul() to possibly detect overflows.
    
    The "flex" version uses an extra variable to track the variable sized
    array allocations and if supported by the compiler will use
    __builtin_counted_by_ref() to properly track bounds.
    
    Sponsored by:   The FreeBSD Foundation
    Reviewed by:    dumbbell
    Differential Revision: https://reviews.freebsd.org/D56395
    
    (cherry picked from commit c72ac6af086962d236f4712761abacac0c62b48e)
---
 sys/compat/linuxkpi/common/include/linux/slab.h | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/slab.h b/sys/compat/linuxkpi/common/include/linux/slab.h
index 0e649e1e3c4a..6c05c77819a5 100644
--- a/sys/compat/linuxkpi/common/include/linux/slab.h
+++ b/sys/compat/linuxkpi/common/include/linux/slab.h
@@ -4,7 +4,7 @@
  * Copyright (c) 2010 Panasas, Inc.
  * Copyright (c) 2013-2021 Mellanox Technologies, Ltd.
  * All rights reserved.
- * Copyright (c) 2024-2025 The FreeBSD Foundation
+ * Copyright (c) 2024-2026 The FreeBSD Foundation
  *
  * Portions of this software were developed by Björn Zeeb
  * under sponsorship from the FreeBSD Foundation.
@@ -51,6 +51,22 @@ MALLOC_DECLARE(M_KMALLOC);
 #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	kzalloc_obj(_p, ...)						\
+    kzalloc(sizeof(typeof(_p)), default_gfp(__VA_ARGS__))
+#define	kzalloc_objs(_p, _n, ...)					\
+    kzalloc(size_mul((_n), sizeof(typeof(_p))), default_gfp(__VA_ARGS__))
+#define	kzalloc_flex(_p, _field, _n, ...)				\
+({									\
+	const size_t __n = (_n);					\
+	const size_t __psize = struct_size_t(typeof(_p), _field, __n);	\
+	typeof(_p) *__p_obj;						\
+									\
+	__p_obj = kzalloc(__psize, default_gfp(__VA_ARGS__));		\
+	if (__p_obj != NULL)						\
+		__set_flex_counter(__p_obj->_field, __n);		\
+									\
+	__p_obj;							\
+})
 #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)
@@ -143,6 +159,12 @@ kmalloc_node(size_t size, gfp_t flags, int node)
 	return (lkpi___kmalloc_node(size, flags, node));
 }
 
+#define	kmalloc_obj(_p, ...)						\
+    kmalloc(sizeof(typeof(_p)), default_gfp(__VA_ARGS__))
+
+#define	kmalloc_objs(_p, _n, ...)					\
+    kmalloc(size_mul((_n) * sizeof(typeof(_p))), default_gfp(__VA_ARGS__))
+
 static inline void *
 krealloc(void *ptr, size_t size, gfp_t flags)
 {


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69e938ea.2767d.68d5e87c>