18d59ecb2SHans Petter Selasky /*- 28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc. 38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc. 48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc. 5ebe5cf35SHans Petter Selasky * Copyright (c) 2013-2021 Mellanox Technologies, Ltd. 68d59ecb2SHans Petter Selasky * All rights reserved. 78d59ecb2SHans Petter Selasky * 88d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 98d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions 108d59ecb2SHans Petter Selasky * are met: 118d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 128d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following 138d59ecb2SHans Petter Selasky * disclaimer. 148d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 158d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 168d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution. 178d59ecb2SHans Petter Selasky * 188d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 198d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 208d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 218d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 228d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 238d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 248d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 258d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 268d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 278d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 288d59ecb2SHans Petter Selasky * 298d59ecb2SHans Petter Selasky * $FreeBSD$ 308d59ecb2SHans Petter Selasky */ 31307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_SLAB_H_ 32307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_SLAB_H_ 338d59ecb2SHans Petter Selasky 34a2b83b59SVladimir Kondratyev #include <sys/types.h> 358d59ecb2SHans Petter Selasky #include <sys/malloc.h> 360a475c59SHans Petter Selasky #include <sys/limits.h> 378d59ecb2SHans Petter Selasky 38ebe5cf35SHans Petter Selasky #include <linux/compat.h> 398d59ecb2SHans Petter Selasky #include <linux/types.h> 408d59ecb2SHans Petter Selasky #include <linux/gfp.h> 41ec25b6faSVladimir Kondratyev #include <linux/llist.h> 42f1a7639aSVladimir Kondratyev #include <linux/overflow.h> 438d59ecb2SHans Petter Selasky 448d59ecb2SHans Petter Selasky MALLOC_DECLARE(M_KMALLOC); 458d59ecb2SHans Petter Selasky 46925245aaSHans Petter Selasky #define kvmalloc(size, flags) kmalloc(size, flags) 47925245aaSHans Petter Selasky #define kvzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO) 48925245aaSHans Petter Selasky #define kvcalloc(n, size, flags) kvmalloc_array(n, size, (flags) | __GFP_ZERO) 4914c5024dSHans Petter Selasky #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO) 50ebe5cf35SHans Petter Selasky #define kzalloc_node(size, flags, node) kmalloc_node(size, (flags) | __GFP_ZERO, node) 51510ebed7SHans Petter Selasky #define kfree_const(ptr) kfree(ptr) 5214c5024dSHans Petter Selasky #define vzalloc(size) __vmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0) 538d59ecb2SHans Petter Selasky #define vfree(arg) kfree(arg) 548d59ecb2SHans Petter Selasky #define kvfree(arg) kfree(arg) 55ebe5cf35SHans Petter Selasky #define vmalloc_node(size, node) __vmalloc_node(size, GFP_KERNEL, node) 5614c5024dSHans Petter Selasky #define vmalloc_user(size) __vmalloc(size, GFP_KERNEL | __GFP_ZERO, 0) 5714c5024dSHans Petter Selasky #define vmalloc(size) __vmalloc(size, GFP_KERNEL, 0) 5814c5024dSHans Petter Selasky #define __kmalloc(...) kmalloc(__VA_ARGS__) 597fdce5c4SHans Petter Selasky 607fdce5c4SHans Petter Selasky /* 617fdce5c4SHans Petter Selasky * Prefix some functions with linux_ to avoid namespace conflict 627fdce5c4SHans Petter Selasky * with the OpenSolaris code in the kernel. 637fdce5c4SHans Petter Selasky */ 647fdce5c4SHans Petter Selasky #define kmem_cache linux_kmem_cache 657fdce5c4SHans Petter Selasky #define kmem_cache_create(...) linux_kmem_cache_create(__VA_ARGS__) 66a2b83b59SVladimir Kondratyev #define kmem_cache_alloc(...) lkpi_kmem_cache_alloc(__VA_ARGS__) 67a2b83b59SVladimir Kondratyev #define kmem_cache_zalloc(...) lkpi_kmem_cache_zalloc(__VA_ARGS__) 68a2b83b59SVladimir Kondratyev #define kmem_cache_free(...) lkpi_kmem_cache_free(__VA_ARGS__) 697fdce5c4SHans Petter Selasky #define kmem_cache_destroy(...) linux_kmem_cache_destroy(__VA_ARGS__) 7035b7625eSEmmanuel Vadot #define kmem_cache_shrink(x) (0) 717fdce5c4SHans Petter Selasky 7278d74419SHans Petter Selasky #define KMEM_CACHE(__struct, flags) \ 7378d74419SHans Petter Selasky linux_kmem_cache_create(#__struct, sizeof(struct __struct), \ 7478d74419SHans Petter Selasky __alignof(struct __struct), (flags), NULL) 7578d74419SHans Petter Selasky 7614c5024dSHans Petter Selasky typedef void linux_kmem_ctor_t (void *); 7714c5024dSHans Petter Selasky 78a2b83b59SVladimir Kondratyev struct linux_kmem_cache; 798d59ecb2SHans Petter Selasky 8014c5024dSHans Petter Selasky #define SLAB_HWCACHE_ALIGN (1 << 0) 81782a90d1SHans Petter Selasky #define SLAB_TYPESAFE_BY_RCU (1 << 1) 8214c5024dSHans Petter Selasky #define SLAB_RECLAIM_ACCOUNT (1 << 2) 838d59ecb2SHans Petter Selasky 84ce930365SHans Petter Selasky #define SLAB_DESTROY_BY_RCU \ 85ce930365SHans Petter Selasky SLAB_TYPESAFE_BY_RCU 86ce930365SHans Petter Selasky 8771ee95ddSHans Petter Selasky #define ARCH_KMALLOC_MINALIGN \ 8871ee95ddSHans Petter Selasky __alignof(unsigned long long) 8971ee95ddSHans Petter Selasky 900b1244bdSVladimir Kondratyev /* drm-kmod 5.4 compat */ 910b1244bdSVladimir Kondratyev #define kfree_async(ptr) kfree(ptr); 92ec25b6faSVladimir Kondratyev 93*208d02fdSJean-Sébastien Pédron #define ZERO_OR_NULL_PTR(x) ((x) == NULL) 94*208d02fdSJean-Sébastien Pédron 9514c5024dSHans Petter Selasky static inline gfp_t 9614c5024dSHans Petter Selasky linux_check_m_flags(gfp_t flags) 978d59ecb2SHans Petter Selasky { 9814c5024dSHans Petter Selasky const gfp_t m = M_NOWAIT | M_WAITOK; 998d59ecb2SHans Petter Selasky 10014c5024dSHans Petter Selasky /* make sure either M_NOWAIT or M_WAITOK is set */ 10114c5024dSHans Petter Selasky if ((flags & m) == 0) 10214c5024dSHans Petter Selasky flags |= M_NOWAIT; 10314c5024dSHans Petter Selasky else if ((flags & m) == m) 10414c5024dSHans Petter Selasky flags &= ~M_WAITOK; 1058d59ecb2SHans Petter Selasky 10614c5024dSHans Petter Selasky /* mask away LinuxKPI specific flags */ 10714c5024dSHans Petter Selasky return (flags & GFP_NATIVE_MASK); 1088d59ecb2SHans Petter Selasky } 1098d59ecb2SHans Petter Selasky 1108d59ecb2SHans Petter Selasky static inline void * 11114c5024dSHans Petter Selasky kmalloc(size_t size, gfp_t flags) 1128d59ecb2SHans Petter Selasky { 1130b1244bdSVladimir Kondratyev return (malloc(MAX(size, sizeof(struct llist_node)), M_KMALLOC, 1140b1244bdSVladimir Kondratyev linux_check_m_flags(flags))); 11514c5024dSHans Petter Selasky } 11614c5024dSHans Petter Selasky 11714c5024dSHans Petter Selasky static inline void * 118ebe5cf35SHans Petter Selasky kmalloc_node(size_t size, gfp_t flags, int node) 119ebe5cf35SHans Petter Selasky { 120ebe5cf35SHans Petter Selasky return (malloc_domainset(size, M_KMALLOC, 121ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 122ebe5cf35SHans Petter Selasky } 123ebe5cf35SHans Petter Selasky 124ebe5cf35SHans Petter Selasky static inline void * 125e70c77caSKristof Provost kcalloc(size_t n, size_t size, gfp_t flags) 126e70c77caSKristof Provost { 127e70c77caSKristof Provost flags |= __GFP_ZERO; 128e70c77caSKristof Provost return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 129e70c77caSKristof Provost } 130e70c77caSKristof Provost 131e70c77caSKristof Provost static inline void * 132ebe5cf35SHans Petter Selasky kcalloc_node(size_t n, size_t size, gfp_t flags, int node) 133ebe5cf35SHans Petter Selasky { 134ebe5cf35SHans Petter Selasky flags |= __GFP_ZERO; 135ebe5cf35SHans Petter Selasky return (mallocarray_domainset(n, size, M_KMALLOC, 136ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 137ebe5cf35SHans Petter Selasky } 138ebe5cf35SHans Petter Selasky 139ebe5cf35SHans Petter Selasky static inline void * 14014c5024dSHans Petter Selasky __vmalloc(size_t size, gfp_t flags, int other) 14114c5024dSHans Petter Selasky { 14214c5024dSHans Petter Selasky return (malloc(size, M_KMALLOC, linux_check_m_flags(flags))); 14314c5024dSHans Petter Selasky } 14414c5024dSHans Petter Selasky 14514c5024dSHans Petter Selasky static inline void * 146ebe5cf35SHans Petter Selasky __vmalloc_node(size_t size, gfp_t flags, int node) 147ebe5cf35SHans Petter Selasky { 148ebe5cf35SHans Petter Selasky return (malloc_domainset(size, M_KMALLOC, 149ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 150ebe5cf35SHans Petter Selasky } 151ebe5cf35SHans Petter Selasky 152ebe5cf35SHans Petter Selasky static inline void * 1530a475c59SHans Petter Selasky vmalloc_32(size_t size) 1540a475c59SHans Petter Selasky { 1550a475c59SHans Petter Selasky return (contigmalloc(size, M_KMALLOC, M_WAITOK, 0, UINT_MAX, 1, 1)); 1560a475c59SHans Petter Selasky } 1570a475c59SHans Petter Selasky 1580a475c59SHans Petter Selasky static inline void * 15914c5024dSHans Petter Selasky kmalloc_array(size_t n, size_t size, gfp_t flags) 16014c5024dSHans Petter Selasky { 161ed595433SPedro F. Giffuni return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 16214c5024dSHans Petter Selasky } 16314c5024dSHans Petter Selasky 16414c5024dSHans Petter Selasky static inline void * 165ebe5cf35SHans Petter Selasky kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node) 166ebe5cf35SHans Petter Selasky { 167ebe5cf35SHans Petter Selasky return (mallocarray_domainset(n, size, M_KMALLOC, 168ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 169ebe5cf35SHans Petter Selasky } 170ebe5cf35SHans Petter Selasky 171ebe5cf35SHans Petter Selasky static inline void * 172f1aa567bSHans Petter Selasky kvmalloc_array(size_t n, size_t size, gfp_t flags) 173f1aa567bSHans Petter Selasky { 174f1aa567bSHans Petter Selasky return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 175f1aa567bSHans Petter Selasky } 176f1aa567bSHans Petter Selasky 177f1aa567bSHans Petter Selasky static inline void * 17814c5024dSHans Petter Selasky krealloc(void *ptr, size_t size, gfp_t flags) 17914c5024dSHans Petter Selasky { 18014c5024dSHans Petter Selasky return (realloc(ptr, size, M_KMALLOC, linux_check_m_flags(flags))); 1818d59ecb2SHans Petter Selasky } 1828d59ecb2SHans Petter Selasky 1831ad6b2b1SJean-Sébastien Pédron static inline void * 1841ad6b2b1SJean-Sébastien Pédron krealloc_array(void *ptr, size_t n, size_t size, gfp_t flags) 1851ad6b2b1SJean-Sébastien Pédron { 1861ad6b2b1SJean-Sébastien Pédron if (WOULD_OVERFLOW(n, size)) { 1871ad6b2b1SJean-Sébastien Pédron return NULL; 1881ad6b2b1SJean-Sébastien Pédron } 1891ad6b2b1SJean-Sébastien Pédron 1901ad6b2b1SJean-Sébastien Pédron return (realloc(ptr, n * size, M_KMALLOC, linux_check_m_flags(flags))); 1911ad6b2b1SJean-Sébastien Pédron } 1921ad6b2b1SJean-Sébastien Pédron 1930b1244bdSVladimir Kondratyev extern void linux_kfree_async(void *); 1940b1244bdSVladimir Kondratyev 1958d59ecb2SHans Petter Selasky static inline void 19614c5024dSHans Petter Selasky kfree(const void *ptr) 1978d59ecb2SHans Petter Selasky { 1980b1244bdSVladimir Kondratyev if (curthread->td_critnest != 0) 1990b1244bdSVladimir Kondratyev linux_kfree_async(__DECONST(void *, ptr)); 2000b1244bdSVladimir Kondratyev else 20114c5024dSHans Petter Selasky free(__DECONST(void *, ptr), M_KMALLOC); 20214c5024dSHans Petter Selasky } 20314c5024dSHans Petter Selasky 204abcac97fSBjoern A. Zeeb static __inline void 205abcac97fSBjoern A. Zeeb kfree_sensitive(const void *ptr) 206abcac97fSBjoern A. Zeeb { 207abcac97fSBjoern A. Zeeb zfree(__DECONST(void *, ptr), M_KMALLOC); 208abcac97fSBjoern A. Zeeb } 209abcac97fSBjoern A. Zeeb 2105d4bf057SVladimir Kondratyev static inline size_t 2115d4bf057SVladimir Kondratyev ksize(const void *ptr) 2125d4bf057SVladimir Kondratyev { 2135d4bf057SVladimir Kondratyev return (malloc_usable_size(ptr)); 2145d4bf057SVladimir Kondratyev } 2155d4bf057SVladimir Kondratyev 21614c5024dSHans Petter Selasky extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name, 21714c5024dSHans Petter Selasky size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor); 218a2b83b59SVladimir Kondratyev extern void *lkpi_kmem_cache_alloc(struct linux_kmem_cache *, gfp_t); 219a2b83b59SVladimir Kondratyev extern void *lkpi_kmem_cache_zalloc(struct linux_kmem_cache *, gfp_t); 220a2b83b59SVladimir Kondratyev extern void lkpi_kmem_cache_free(struct linux_kmem_cache *, void *); 22114c5024dSHans Petter Selasky extern void linux_kmem_cache_destroy(struct linux_kmem_cache *); 2228d59ecb2SHans Petter Selasky 223307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_SLAB_H_ */ 224