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 */ 29307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_SLAB_H_ 30307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_SLAB_H_ 318d59ecb2SHans Petter Selasky 32a2b83b59SVladimir Kondratyev #include <sys/types.h> 338d59ecb2SHans Petter Selasky #include <sys/malloc.h> 340a475c59SHans Petter Selasky #include <sys/limits.h> 358d59ecb2SHans Petter Selasky 36ebe5cf35SHans Petter Selasky #include <linux/compat.h> 378d59ecb2SHans Petter Selasky #include <linux/types.h> 388d59ecb2SHans Petter Selasky #include <linux/gfp.h> 39ec25b6faSVladimir Kondratyev #include <linux/llist.h> 40f1a7639aSVladimir Kondratyev #include <linux/overflow.h> 418d59ecb2SHans Petter Selasky 428d59ecb2SHans Petter Selasky MALLOC_DECLARE(M_KMALLOC); 438d59ecb2SHans Petter Selasky 44c0b8047bSVladimir Kondratyev #define kmalloc(size, flags) lkpi_kmalloc(size, flags) 45925245aaSHans Petter Selasky #define kvmalloc(size, flags) kmalloc(size, flags) 46925245aaSHans Petter Selasky #define kvzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO) 47925245aaSHans Petter Selasky #define kvcalloc(n, size, flags) kvmalloc_array(n, size, (flags) | __GFP_ZERO) 4814c5024dSHans Petter Selasky #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO) 49ebe5cf35SHans Petter Selasky #define kzalloc_node(size, flags, node) kmalloc_node(size, (flags) | __GFP_ZERO, node) 50510ebed7SHans Petter Selasky #define kfree_const(ptr) kfree(ptr) 518a8e86b8SJean-Sébastien Pédron #define vzalloc(size) __vmalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO, 0) 528d59ecb2SHans Petter Selasky #define vfree(arg) kfree(arg) 538d59ecb2SHans Petter Selasky #define kvfree(arg) kfree(arg) 548a8e86b8SJean-Sébastien Pédron #define vmalloc_node(size, node) __vmalloc_node(size, GFP_KERNEL, node) 558a8e86b8SJean-Sébastien Pédron #define vmalloc_user(size) __vmalloc(size, GFP_KERNEL | __GFP_ZERO, 0) 568a8e86b8SJean-Sébastien Pédron #define vmalloc(size) __vmalloc(size, GFP_KERNEL, 0) 577fdce5c4SHans Petter Selasky 587fdce5c4SHans Petter Selasky /* 597fdce5c4SHans Petter Selasky * Prefix some functions with linux_ to avoid namespace conflict 607fdce5c4SHans Petter Selasky * with the OpenSolaris code in the kernel. 617fdce5c4SHans Petter Selasky */ 627fdce5c4SHans Petter Selasky #define kmem_cache linux_kmem_cache 637fdce5c4SHans Petter Selasky #define kmem_cache_create(...) linux_kmem_cache_create(__VA_ARGS__) 64a2b83b59SVladimir Kondratyev #define kmem_cache_alloc(...) lkpi_kmem_cache_alloc(__VA_ARGS__) 65a2b83b59SVladimir Kondratyev #define kmem_cache_zalloc(...) lkpi_kmem_cache_zalloc(__VA_ARGS__) 66a2b83b59SVladimir Kondratyev #define kmem_cache_free(...) lkpi_kmem_cache_free(__VA_ARGS__) 677fdce5c4SHans Petter Selasky #define kmem_cache_destroy(...) linux_kmem_cache_destroy(__VA_ARGS__) 6835b7625eSEmmanuel Vadot #define kmem_cache_shrink(x) (0) 697fdce5c4SHans Petter Selasky 7078d74419SHans Petter Selasky #define KMEM_CACHE(__struct, flags) \ 7178d74419SHans Petter Selasky linux_kmem_cache_create(#__struct, sizeof(struct __struct), \ 7278d74419SHans Petter Selasky __alignof(struct __struct), (flags), NULL) 7378d74419SHans Petter Selasky 7414c5024dSHans Petter Selasky typedef void linux_kmem_ctor_t (void *); 7514c5024dSHans Petter Selasky 76a2b83b59SVladimir Kondratyev struct linux_kmem_cache; 778d59ecb2SHans Petter Selasky 7814c5024dSHans Petter Selasky #define SLAB_HWCACHE_ALIGN (1 << 0) 79782a90d1SHans Petter Selasky #define SLAB_TYPESAFE_BY_RCU (1 << 1) 8014c5024dSHans Petter Selasky #define SLAB_RECLAIM_ACCOUNT (1 << 2) 818d59ecb2SHans Petter Selasky 82ce930365SHans Petter Selasky #define SLAB_DESTROY_BY_RCU \ 83ce930365SHans Petter Selasky SLAB_TYPESAFE_BY_RCU 84ce930365SHans Petter Selasky 8571ee95ddSHans Petter Selasky #define ARCH_KMALLOC_MINALIGN \ 8671ee95ddSHans Petter Selasky __alignof(unsigned long long) 8771ee95ddSHans Petter Selasky 880b1244bdSVladimir Kondratyev /* drm-kmod 5.4 compat */ 890b1244bdSVladimir Kondratyev #define kfree_async(ptr) kfree(ptr); 90ec25b6faSVladimir Kondratyev 911b4e08b4SJean-Sébastien Pédron #define ZERO_SIZE_PTR ((void *)16) 921b4e08b4SJean-Sébastien Pédron #define ZERO_OR_NULL_PTR(x) ((x) == NULL || (x) == ZERO_SIZE_PTR) 93208d02fdSJean-Sébastien Pédron 94ab6e1167SVladimir Kondratyev extern void *lkpi_kmalloc(size_t size, gfp_t flags); 95ab6e1167SVladimir Kondratyev 9614c5024dSHans Petter Selasky static inline gfp_t 9714c5024dSHans Petter Selasky linux_check_m_flags(gfp_t flags) 988d59ecb2SHans Petter Selasky { 9914c5024dSHans Petter Selasky const gfp_t m = M_NOWAIT | M_WAITOK; 1008d59ecb2SHans Petter Selasky 10114c5024dSHans Petter Selasky /* make sure either M_NOWAIT or M_WAITOK is set */ 10214c5024dSHans Petter Selasky if ((flags & m) == 0) 10314c5024dSHans Petter Selasky flags |= M_NOWAIT; 10414c5024dSHans Petter Selasky else if ((flags & m) == m) 10514c5024dSHans Petter Selasky flags &= ~M_WAITOK; 1068d59ecb2SHans Petter Selasky 10714c5024dSHans Petter Selasky /* mask away LinuxKPI specific flags */ 10814c5024dSHans Petter Selasky return (flags & GFP_NATIVE_MASK); 1098d59ecb2SHans Petter Selasky } 1108d59ecb2SHans Petter Selasky 1118d59ecb2SHans Petter Selasky static inline void * 112c0b8047bSVladimir Kondratyev __kmalloc(size_t size, gfp_t flags) 1138d59ecb2SHans Petter Selasky { 1140b1244bdSVladimir Kondratyev return (malloc(MAX(size, sizeof(struct llist_node)), M_KMALLOC, 1150b1244bdSVladimir Kondratyev linux_check_m_flags(flags))); 11614c5024dSHans Petter Selasky } 11714c5024dSHans Petter Selasky 11814c5024dSHans Petter Selasky static inline void * 119ebe5cf35SHans Petter Selasky kmalloc_node(size_t size, gfp_t flags, int node) 120ebe5cf35SHans Petter Selasky { 121ebe5cf35SHans Petter Selasky return (malloc_domainset(size, M_KMALLOC, 122ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 123ebe5cf35SHans Petter Selasky } 124ebe5cf35SHans Petter Selasky 125ebe5cf35SHans Petter Selasky static inline void * 126e70c77caSKristof Provost kcalloc(size_t n, size_t size, gfp_t flags) 127e70c77caSKristof Provost { 128e70c77caSKristof Provost flags |= __GFP_ZERO; 129e70c77caSKristof Provost return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 130e70c77caSKristof Provost } 131e70c77caSKristof Provost 132e70c77caSKristof Provost static inline void * 133ebe5cf35SHans Petter Selasky kcalloc_node(size_t n, size_t size, gfp_t flags, int node) 134ebe5cf35SHans Petter Selasky { 135ebe5cf35SHans Petter Selasky flags |= __GFP_ZERO; 136ebe5cf35SHans Petter Selasky return (mallocarray_domainset(n, size, M_KMALLOC, 137ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 138ebe5cf35SHans Petter Selasky } 139ebe5cf35SHans Petter Selasky 140ebe5cf35SHans Petter Selasky static inline void * 14114c5024dSHans Petter Selasky __vmalloc(size_t size, gfp_t flags, int other) 14214c5024dSHans Petter Selasky { 14314c5024dSHans Petter Selasky return (malloc(size, M_KMALLOC, linux_check_m_flags(flags))); 14414c5024dSHans Petter Selasky } 14514c5024dSHans Petter Selasky 14614c5024dSHans Petter Selasky static inline void * 147ebe5cf35SHans Petter Selasky __vmalloc_node(size_t size, gfp_t flags, int node) 148ebe5cf35SHans Petter Selasky { 149ebe5cf35SHans Petter Selasky return (malloc_domainset(size, M_KMALLOC, 150ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 151ebe5cf35SHans Petter Selasky } 152ebe5cf35SHans Petter Selasky 153ebe5cf35SHans Petter Selasky static inline void * 1540a475c59SHans Petter Selasky vmalloc_32(size_t size) 1550a475c59SHans Petter Selasky { 1560a475c59SHans Petter Selasky return (contigmalloc(size, M_KMALLOC, M_WAITOK, 0, UINT_MAX, 1, 1)); 1570a475c59SHans Petter Selasky } 1580a475c59SHans Petter Selasky 1590a475c59SHans Petter Selasky static inline void * 16014c5024dSHans Petter Selasky kmalloc_array(size_t n, size_t size, gfp_t flags) 16114c5024dSHans Petter Selasky { 162ed595433SPedro F. Giffuni return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 16314c5024dSHans Petter Selasky } 16414c5024dSHans Petter Selasky 16514c5024dSHans Petter Selasky static inline void * 166ebe5cf35SHans Petter Selasky kmalloc_array_node(size_t n, size_t size, gfp_t flags, int node) 167ebe5cf35SHans Petter Selasky { 168ebe5cf35SHans Petter Selasky return (mallocarray_domainset(n, size, M_KMALLOC, 169ebe5cf35SHans Petter Selasky linux_get_vm_domain_set(node), linux_check_m_flags(flags))); 170ebe5cf35SHans Petter Selasky } 171ebe5cf35SHans Petter Selasky 172ebe5cf35SHans Petter Selasky static inline void * 173f1aa567bSHans Petter Selasky kvmalloc_array(size_t n, size_t size, gfp_t flags) 174f1aa567bSHans Petter Selasky { 175f1aa567bSHans Petter Selasky return (mallocarray(n, size, M_KMALLOC, linux_check_m_flags(flags))); 176f1aa567bSHans Petter Selasky } 177f1aa567bSHans Petter Selasky 178f1aa567bSHans Petter Selasky static inline void * 17914c5024dSHans Petter Selasky krealloc(void *ptr, size_t size, gfp_t flags) 18014c5024dSHans Petter Selasky { 18114c5024dSHans Petter Selasky return (realloc(ptr, size, M_KMALLOC, linux_check_m_flags(flags))); 1828d59ecb2SHans Petter Selasky } 1838d59ecb2SHans Petter Selasky 1841ad6b2b1SJean-Sébastien Pédron static inline void * 1851ad6b2b1SJean-Sébastien Pédron krealloc_array(void *ptr, size_t n, size_t size, gfp_t flags) 1861ad6b2b1SJean-Sébastien Pédron { 1871ad6b2b1SJean-Sébastien Pédron if (WOULD_OVERFLOW(n, size)) { 1881ad6b2b1SJean-Sébastien Pédron return NULL; 1891ad6b2b1SJean-Sébastien Pédron } 1901ad6b2b1SJean-Sébastien Pédron 1911ad6b2b1SJean-Sébastien Pédron return (realloc(ptr, n * size, M_KMALLOC, linux_check_m_flags(flags))); 1921ad6b2b1SJean-Sébastien Pédron } 1931ad6b2b1SJean-Sébastien Pédron 1940b1244bdSVladimir Kondratyev extern void linux_kfree_async(void *); 1950b1244bdSVladimir Kondratyev 1968d59ecb2SHans Petter Selasky static inline void 19714c5024dSHans Petter Selasky kfree(const void *ptr) 1988d59ecb2SHans Petter Selasky { 1991b4e08b4SJean-Sébastien Pédron if (ZERO_OR_NULL_PTR(ptr)) 2001b4e08b4SJean-Sébastien Pédron return; 2011b4e08b4SJean-Sébastien Pédron 2020b1244bdSVladimir Kondratyev if (curthread->td_critnest != 0) 2030b1244bdSVladimir Kondratyev linux_kfree_async(__DECONST(void *, ptr)); 2040b1244bdSVladimir Kondratyev else 20514c5024dSHans Petter Selasky free(__DECONST(void *, ptr), M_KMALLOC); 20614c5024dSHans Petter Selasky } 20714c5024dSHans Petter Selasky 208abcac97fSBjoern A. Zeeb static __inline void 209abcac97fSBjoern A. Zeeb kfree_sensitive(const void *ptr) 210abcac97fSBjoern A. Zeeb { 2111b4e08b4SJean-Sébastien Pédron if (ZERO_OR_NULL_PTR(ptr)) 2121b4e08b4SJean-Sébastien Pédron return; 2131b4e08b4SJean-Sébastien Pédron 214abcac97fSBjoern A. Zeeb zfree(__DECONST(void *, ptr), M_KMALLOC); 215abcac97fSBjoern A. Zeeb } 216abcac97fSBjoern A. Zeeb 217ab6e1167SVladimir Kondratyev static inline void * 218ab6e1167SVladimir Kondratyev kvrealloc(const void *ptr, size_t oldsize, size_t newsize, gfp_t flags) 219ab6e1167SVladimir Kondratyev { 220ab6e1167SVladimir Kondratyev void *newptr; 221ab6e1167SVladimir Kondratyev 222ab6e1167SVladimir Kondratyev if (newsize <= oldsize) 223ab6e1167SVladimir Kondratyev return (__DECONST(void *, ptr)); 224ab6e1167SVladimir Kondratyev 225ab6e1167SVladimir Kondratyev newptr = kvmalloc(newsize, flags); 226ab6e1167SVladimir Kondratyev if (newptr != NULL) { 227ab6e1167SVladimir Kondratyev memcpy(newptr, ptr, oldsize); 228ab6e1167SVladimir Kondratyev kvfree(ptr); 229ab6e1167SVladimir Kondratyev } 230ab6e1167SVladimir Kondratyev 231ab6e1167SVladimir Kondratyev return (newptr); 232ab6e1167SVladimir Kondratyev } 233ab6e1167SVladimir Kondratyev 2345d4bf057SVladimir Kondratyev static inline size_t 2355d4bf057SVladimir Kondratyev ksize(const void *ptr) 2365d4bf057SVladimir Kondratyev { 2375d4bf057SVladimir Kondratyev return (malloc_usable_size(ptr)); 2385d4bf057SVladimir Kondratyev } 2395d4bf057SVladimir Kondratyev 240*ab1a5d8eSVladimir Kondratyev static inline size_t 241*ab1a5d8eSVladimir Kondratyev kmalloc_size_roundup(size_t size) 242*ab1a5d8eSVladimir Kondratyev { 243*ab1a5d8eSVladimir Kondratyev if (unlikely(size == 0 || size == SIZE_MAX)) 244*ab1a5d8eSVladimir Kondratyev return (size); 245*ab1a5d8eSVladimir Kondratyev return (malloc_size(size)); 246*ab1a5d8eSVladimir Kondratyev } 247*ab1a5d8eSVladimir Kondratyev 24814c5024dSHans Petter Selasky extern struct linux_kmem_cache *linux_kmem_cache_create(const char *name, 24914c5024dSHans Petter Selasky size_t size, size_t align, unsigned flags, linux_kmem_ctor_t *ctor); 250a2b83b59SVladimir Kondratyev extern void *lkpi_kmem_cache_alloc(struct linux_kmem_cache *, gfp_t); 251a2b83b59SVladimir Kondratyev extern void *lkpi_kmem_cache_zalloc(struct linux_kmem_cache *, gfp_t); 252a2b83b59SVladimir Kondratyev extern void lkpi_kmem_cache_free(struct linux_kmem_cache *, void *); 25314c5024dSHans Petter Selasky extern void linux_kmem_cache_destroy(struct linux_kmem_cache *); 2548d59ecb2SHans Petter Selasky 255307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_SLAB_H_ */ 256