]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 NS |
3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
1da177e4 LT |
5 | */ |
6 | #ifndef __XFS_SUPPORT_KMEM_H__ | |
7 | #define __XFS_SUPPORT_KMEM_H__ | |
8 | ||
9 | #include <linux/slab.h> | |
10 | #include <linux/sched.h> | |
11 | #include <linux/mm.h> | |
bdfb0430 | 12 | #include <linux/vmalloc.h> |
1da177e4 | 13 | |
8758280f NS |
14 | /* |
15 | * General memory allocation interfaces | |
16 | */ | |
17 | ||
77ba7877 AV |
18 | typedef unsigned __bitwise xfs_km_flags_t; |
19 | #define KM_SLEEP ((__force xfs_km_flags_t)0x0001u) | |
20 | #define KM_NOSLEEP ((__force xfs_km_flags_t)0x0002u) | |
21 | #define KM_NOFS ((__force xfs_km_flags_t)0x0004u) | |
22 | #define KM_MAYFAIL ((__force xfs_km_flags_t)0x0008u) | |
359d992b | 23 | #define KM_ZERO ((__force xfs_km_flags_t)0x0010u) |
8758280f NS |
24 | |
25 | /* | |
26 | * We use a special process flag to avoid recursive callbacks into | |
27 | * the filesystem during transactions. We will also issue our own | |
28 | * warnings, so we explicitly skip any generic ones (silly of us). | |
29 | */ | |
30 | static inline gfp_t | |
77ba7877 | 31 | kmem_flags_convert(xfs_km_flags_t flags) |
1da177e4 | 32 | { |
8758280f | 33 | gfp_t lflags; |
1da177e4 | 34 | |
359d992b | 35 | BUG_ON(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL|KM_ZERO)); |
1da177e4 LT |
36 | |
37 | if (flags & KM_NOSLEEP) { | |
8758280f | 38 | lflags = GFP_ATOMIC | __GFP_NOWARN; |
1da177e4 | 39 | } else { |
8758280f | 40 | lflags = GFP_KERNEL | __GFP_NOWARN; |
7dea19f9 | 41 | if (flags & KM_NOFS) |
1da177e4 LT |
42 | lflags &= ~__GFP_FS; |
43 | } | |
359d992b | 44 | |
91c63ecd MH |
45 | /* |
46 | * Default page/slab allocator behavior is to retry for ever | |
47 | * for small allocations. We can override this behavior by using | |
48 | * __GFP_RETRY_MAYFAIL which will tell the allocator to retry as long | |
49 | * as it is feasible but rather fail than retry forever for all | |
50 | * request sizes. | |
51 | */ | |
52 | if (flags & KM_MAYFAIL) | |
53 | lflags |= __GFP_RETRY_MAYFAIL; | |
54 | ||
359d992b GZ |
55 | if (flags & KM_ZERO) |
56 | lflags |= __GFP_ZERO; | |
57 | ||
8758280f | 58 | return lflags; |
1da177e4 LT |
59 | } |
60 | ||
77ba7877 | 61 | extern void *kmem_alloc(size_t, xfs_km_flags_t); |
cb0a8d23 | 62 | extern void *kmem_alloc_large(size_t size, xfs_km_flags_t); |
664b60f6 | 63 | extern void *kmem_realloc(const void *, size_t, xfs_km_flags_t); |
f3d21552 WY |
64 | static inline void kmem_free(const void *ptr) |
65 | { | |
66 | kvfree(ptr); | |
67 | } | |
8758280f | 68 | |
bdfb0430 | 69 | |
359d992b GZ |
70 | static inline void * |
71 | kmem_zalloc(size_t size, xfs_km_flags_t flags) | |
72 | { | |
73 | return kmem_alloc(size, flags | KM_ZERO); | |
cb0a8d23 DC |
74 | } |
75 | ||
76 | static inline void * | |
77 | kmem_zalloc_large(size_t size, xfs_km_flags_t flags) | |
78 | { | |
79 | return kmem_alloc_large(size, flags | KM_ZERO); | |
359d992b GZ |
80 | } |
81 | ||
8758280f NS |
82 | /* |
83 | * Zone interfaces | |
84 | */ | |
85 | ||
86 | #define KM_ZONE_HWALIGN SLAB_HWCACHE_ALIGN | |
87 | #define KM_ZONE_RECLAIM SLAB_RECLAIM_ACCOUNT | |
b0196009 | 88 | #define KM_ZONE_SPREAD SLAB_MEM_SPREAD |
5d097056 | 89 | #define KM_ZONE_ACCOUNT SLAB_ACCOUNT |
8758280f NS |
90 | |
91 | #define kmem_zone kmem_cache | |
92 | #define kmem_zone_t struct kmem_cache | |
93 | ||
94 | static inline kmem_zone_t * | |
1da177e4 LT |
95 | kmem_zone_init(int size, char *zone_name) |
96 | { | |
20c2df83 | 97 | return kmem_cache_create(zone_name, size, 0, 0, NULL); |
1da177e4 LT |
98 | } |
99 | ||
8758280f | 100 | static inline kmem_zone_t * |
d50112ed | 101 | kmem_zone_init_flags(int size, char *zone_name, slab_flags_t flags, |
51cc5068 | 102 | void (*construct)(void *)) |
8758280f | 103 | { |
20c2df83 | 104 | return kmem_cache_create(zone_name, size, 0, flags, construct); |
8758280f NS |
105 | } |
106 | ||
107 | static inline void | |
1da177e4 LT |
108 | kmem_zone_free(kmem_zone_t *zone, void *ptr) |
109 | { | |
110 | kmem_cache_free(zone, ptr); | |
111 | } | |
112 | ||
8758280f | 113 | static inline void |
1da177e4 LT |
114 | kmem_zone_destroy(kmem_zone_t *zone) |
115 | { | |
478f8da0 | 116 | kmem_cache_destroy(zone); |
1da177e4 LT |
117 | } |
118 | ||
77ba7877 | 119 | extern void *kmem_zone_alloc(kmem_zone_t *, xfs_km_flags_t); |
359d992b GZ |
120 | |
121 | static inline void * | |
122 | kmem_zone_zalloc(kmem_zone_t *zone, xfs_km_flags_t flags) | |
123 | { | |
124 | return kmem_zone_alloc(zone, flags | KM_ZERO); | |
125 | } | |
1da177e4 | 126 | |
1da177e4 | 127 | #endif /* __XFS_SUPPORT_KMEM_H__ */ |