From nobody Fri Apr 18 14:36:56 2025 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4ZfHQd0sj8z5stdr; Fri, 18 Apr 2025 14:36:57 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZfHQc63yzz3Jxd; Fri, 18 Apr 2025 14:36:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744987016; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YxmKw1nekafzVXrQxc0/4kdYXoHvqfdVrdNAuSlJaHo=; b=aYMNz4LmlYGfiXf/ntRlwaj6onZlOVUi0D4ZtdpqK5LVy/y9gtJEvLoaCJMxxOlot8Ms1i bbg1RNjz/7C9nc1ki2k6/MLIOoD3snj6lTGZj6ha68ov/3QgYW3eBlR+QLebQ0GFIL9ZQ8 2Mri2aXRnRNqGeZEFRXoH2IS/FAsm4uhok4ngaT+zNF+5ReiCbB8TPZ90npFkJShIttaDD DtvSVzaUhRi7mGzdRAH4+aQSnMUYzsEhae3eVHVwEBKBrPUQ1MUVSYgH58pVsMjxgb3Ez0 hI6eakq0L/3uZOACwztmwpUVwOSxzNoQ9BElHA1Z2jY34a5gjUMdk/yOpYpcig== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1744987016; a=rsa-sha256; cv=none; b=PPrjcaFbOqNRbO8kxUdi5PcDjX2xOOkZjV41GeKBtUYBqD+ooD1rnfax9vM1bkyW4zM0Lj xUa6yp7oS2Jb8+vOPKsNoKAtw0hUVHu5Bse+LtWoeZA8umNcWWHjxTrOfNwWG50BVKQZ+0 dlPeYH+rVi/JzKdJGq78QaTGIChqFHaeBGMJfbgm8fyNKe0CbYGmCj+lIf2sdKtMxl0YcE 5IuPl4e2pMFnsEP2oHkm9AN5ygWSY1v1BQroN6wPHYCZ7ySvbr3oy18k/5CY4FTEKOOT9R box5uuBWyrBkDUPHmJZtbDSol3ZxeuqoLmfizFrozCOFleI++ic5MeqKjOHDzw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744987016; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=YxmKw1nekafzVXrQxc0/4kdYXoHvqfdVrdNAuSlJaHo=; b=qfAZXVOk/4A/FrkKZfLcYOQsZfMuXnwdzPqU3DoGMvt/2jAId6uXcmQcqNDXRGHrxFzYzo t8vLGd6mxtNT2kz9a6xm25kGJV8E7qJWKeCYfhaEnXIz2EEx8AOR84eVJUwPVvERpq9+c6 LSRHwsfyCb/lCemuGNsSkh92zQ40pmI5HYRUj3c/vkxhZIdE5gWoVsimACnqHMXu6nd3ly pdwc96t0yfXy6h8avk5a6KTu5aJxwBeBmPA6cAhm4dk0dvDSdD7C0Aok+g+f89cQCBfahW sKuCL702eeZPzteeAfnhGZA8EqRYdMwAEoTGCx/O0SuwCgcdybi1toq7snAWuA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4ZfHQc5N6Hzf3n; Fri, 18 Apr 2025 14:36:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 53IEauxG091265; Fri, 18 Apr 2025 14:36:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 53IEaub8091262; Fri, 18 Apr 2025 14:36:56 GMT (envelope-from git) Date: Fri, 18 Apr 2025 14:36:56 GMT Message-Id: <202504181436.53IEaub8091262@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: "Bjoern A. Zeeb" Subject: git: ebe210ca703b - stable/14 - LinuxKPI; cleanup slab.h a bit; move more free() into slab.c List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: bz X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: ebe210ca703bd584274396b2c0fa1694750f05c1 Auto-Submitted: auto-generated The branch stable/14 has been updated by bz: URL: https://cgit.FreeBSD.org/src/commit/?id=ebe210ca703bd584274396b2c0fa1694750f05c1 commit ebe210ca703bd584274396b2c0fa1694750f05c1 Author: Bjoern A. Zeeb AuthorDate: 2025-03-29 17:33:08 +0000 Commit: Bjoern A. Zeeb 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); +} +