]> git.proxmox.com Git - mirror_spl.git/blob - include/sys/kmem.h
Refactor generic memory allocation interfaces
[mirror_spl.git] / include / sys / kmem.h
1 /*
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
6 * UCRL-CODE-235197
7 *
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
10 *
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
15 *
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 * for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #ifndef _SPL_KMEM_H
26 #define _SPL_KMEM_H
27
28 #include <linux/slab.h>
29 #include <linux/sched.h>
30
31 extern int kmem_debugging(void);
32 extern char *kmem_vasprintf(const char *fmt, va_list ap);
33 extern char *kmem_asprintf(const char *fmt, ...);
34 extern char *strdup(const char *str);
35 extern void strfree(char *str);
36
37 /*
38 * Memory allocation interfaces
39 */
40 #define KM_SLEEP 0x0000 /* can block for memory; success guaranteed */
41 #define KM_NOSLEEP 0x0001 /* cannot block for memory; may fail */
42 #define KM_PUSHPAGE 0x0004 /* can block for memory; may use reserve */
43 #define KM_ZERO 0x1000 /* zero the allocation */
44 #define KM_VMEM 0x2000 /* caller is vmem_* wrapper */
45
46 #define KM_PUBLIC_MASK (KM_SLEEP | KM_NOSLEEP | KM_PUSHPAGE)
47
48 /*
49 * Convert a KM_* flags mask to its Linux GFP_* counterpart. The conversion
50 * function is context aware which means that KM_SLEEP allocations can be
51 * safely used in syncing contexts which have set PF_FSTRANS.
52 */
53 static inline gfp_t
54 kmem_flags_convert(int flags)
55 {
56 gfp_t lflags = __GFP_NOWARN | __GFP_COMP;
57
58 if (flags & KM_NOSLEEP) {
59 lflags |= GFP_ATOMIC | __GFP_NORETRY;
60 } else {
61 lflags |= GFP_KERNEL;
62 if ((current->flags & PF_FSTRANS))
63 lflags &= ~(__GFP_IO|__GFP_FS);
64 }
65
66 if (flags & KM_PUSHPAGE)
67 lflags |= __GFP_HIGH;
68
69 if (flags & KM_ZERO)
70 lflags |= __GFP_ZERO;
71
72 return (lflags);
73 }
74
75 #ifdef HAVE_ATOMIC64_T
76 #define kmem_alloc_used_add(size) atomic64_add(size, &kmem_alloc_used)
77 #define kmem_alloc_used_sub(size) atomic64_sub(size, &kmem_alloc_used)
78 #define kmem_alloc_used_read() atomic64_read(&kmem_alloc_used)
79 #define kmem_alloc_used_set(size) atomic64_set(&kmem_alloc_used, size)
80 extern atomic64_t kmem_alloc_used;
81 extern unsigned long long kmem_alloc_max;
82 #else /* HAVE_ATOMIC64_T */
83 #define kmem_alloc_used_add(size) atomic_add(size, &kmem_alloc_used)
84 #define kmem_alloc_used_sub(size) atomic_sub(size, &kmem_alloc_used)
85 #define kmem_alloc_used_read() atomic_read(&kmem_alloc_used)
86 #define kmem_alloc_used_set(size) atomic_set(&kmem_alloc_used, size)
87 extern atomic_t kmem_alloc_used;
88 extern unsigned long long kmem_alloc_max;
89 #endif /* HAVE_ATOMIC64_T */
90
91 extern unsigned int spl_kmem_alloc_warn;
92 extern unsigned int spl_kmem_alloc_max;
93
94 #define kmem_alloc(sz, fl) spl_kmem_alloc((sz), (fl), __func__, __LINE__)
95 #define kmem_zalloc(sz, fl) spl_kmem_zalloc((sz), (fl), __func__, __LINE__)
96 #define kmem_free(ptr, sz) spl_kmem_free((ptr), (sz))
97
98 extern void *spl_kmem_alloc(size_t sz, int fl, const char *func, int line);
99 extern void *spl_kmem_zalloc(size_t sz, int fl, const char *func, int line);
100 extern void spl_kmem_free(const void *ptr, size_t sz);
101
102 /*
103 * The following functions are only available for internal use.
104 */
105 extern void *spl_kmem_alloc_impl(size_t size, int flags, int node);
106 extern void *spl_kmem_alloc_debug(size_t size, int flags, int node);
107 extern void *spl_kmem_alloc_track(size_t size, int flags,
108 const char *func, int line, int node);
109 extern void spl_kmem_free_impl(const void *buf, size_t size);
110 extern void spl_kmem_free_debug(const void *buf, size_t size);
111 extern void spl_kmem_free_track(const void *buf, size_t size);
112
113 extern int spl_kmem_init(void);
114 extern void spl_kmem_fini(void);
115
116 #endif /* _SPL_KMEM_H */