Commit | Line | Data |
---|---|---|
10cef602 MM |
1 | /* |
2 | * SLOB Allocator: Simple List Of Blocks | |
3 | * | |
4 | * Matt Mackall <mpm@selenic.com> 12/30/03 | |
5 | * | |
6193a2ff PM |
6 | * NUMA support by Paul Mundt, 2007. |
7 | * | |
10cef602 MM |
8 | * How SLOB works: |
9 | * | |
10 | * The core of SLOB is a traditional K&R style heap allocator, with | |
11 | * support for returning aligned objects. The granularity of this | |
55394849 NP |
12 | * allocator is as little as 2 bytes, however typically most architectures |
13 | * will require 4 bytes on 32-bit and 8 bytes on 64-bit. | |
95b35127 | 14 | * |
20cecbae MM |
15 | * The slob heap is a set of linked list of pages from alloc_pages(), |
16 | * and within each page, there is a singly-linked list of free blocks | |
17 | * (slob_t). The heap is grown on demand. To reduce fragmentation, | |
18 | * heap pages are segregated into three lists, with objects less than | |
19 | * 256 bytes, objects less than 1024 bytes, and all other objects. | |
20 | * | |
21 | * Allocation from heap involves first searching for a page with | |
22 | * sufficient free blocks (using a next-fit-like approach) followed by | |
23 | * a first-fit scan of the page. Deallocation inserts objects back | |
24 | * into the free list in address order, so this is effectively an | |
25 | * address-ordered first fit. | |
10cef602 MM |
26 | * |
27 | * Above this is an implementation of kmalloc/kfree. Blocks returned | |
55394849 | 28 | * from kmalloc are prepended with a 4-byte header with the kmalloc size. |
10cef602 | 29 | * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls |
6193a2ff | 30 | * alloc_pages() directly, allocating compound pages so the page order |
d87a133f NP |
31 | * does not have to be separately tracked, and also stores the exact |
32 | * allocation size in page->private so that it can be used to accurately | |
33 | * provide ksize(). These objects are detected in kfree() because slob_page() | |
34 | * is false for them. | |
10cef602 MM |
35 | * |
36 | * SLAB is emulated on top of SLOB by simply calling constructors and | |
95b35127 NP |
37 | * destructors for every SLAB allocation. Objects are returned with the |
38 | * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which | |
39 | * case the low-level allocator will fragment blocks to create the proper | |
40 | * alignment. Again, objects of page-size or greater are allocated by | |
6193a2ff | 41 | * calling alloc_pages(). As SLAB objects know their size, no separate |
95b35127 | 42 | * size bookkeeping is necessary and there is essentially no allocation |
d87a133f NP |
43 | * space overhead, and compound pages aren't needed for multi-page |
44 | * allocations. | |
6193a2ff PM |
45 | * |
46 | * NUMA support in SLOB is fairly simplistic, pushing most of the real | |
47 | * logic down to the page allocator, and simply doing the node accounting | |
48 | * on the upper levels. In the event that a node id is explicitly | |
6484eb3e | 49 | * provided, alloc_pages_exact_node() with the specified node id is used |
6193a2ff PM |
50 | * instead. The common case (or when the node id isn't explicitly provided) |
51 | * will default to the current node, as per numa_node_id(). | |
52 | * | |
53 | * Node aware pages are still inserted in to the global freelist, and | |
54 | * these are scanned for by matching against the node id encoded in the | |
55 | * page flags. As a result, block allocations that can be satisfied from | |
56 | * the freelist will only be done so on pages residing on the same node, | |
57 | * in order to prevent random node placement. | |
10cef602 MM |
58 | */ |
59 | ||
95b35127 | 60 | #include <linux/kernel.h> |
10cef602 MM |
61 | #include <linux/slab.h> |
62 | #include <linux/mm.h> | |
1f0532eb | 63 | #include <linux/swap.h> /* struct reclaim_state */ |
10cef602 MM |
64 | #include <linux/cache.h> |
65 | #include <linux/init.h> | |
b95f1b31 | 66 | #include <linux/export.h> |
afc0cedb | 67 | #include <linux/rcupdate.h> |
95b35127 | 68 | #include <linux/list.h> |
4374e616 | 69 | #include <linux/kmemleak.h> |
039ca4e7 LZ |
70 | |
71 | #include <trace/events/kmem.h> | |
72 | ||
60063497 | 73 | #include <linux/atomic.h> |
95b35127 | 74 | |
95b35127 NP |
75 | /* |
76 | * slob_block has a field 'units', which indicates size of block if +ve, | |
77 | * or offset of next block if -ve (in SLOB_UNITs). | |
78 | * | |
79 | * Free blocks of size 1 unit simply contain the offset of the next block. | |
80 | * Those with larger size contain their size in the first SLOB_UNIT of | |
81 | * memory, and the offset of the next free block in the second SLOB_UNIT. | |
82 | */ | |
55394849 | 83 | #if PAGE_SIZE <= (32767 * 2) |
95b35127 NP |
84 | typedef s16 slobidx_t; |
85 | #else | |
86 | typedef s32 slobidx_t; | |
87 | #endif | |
88 | ||
10cef602 | 89 | struct slob_block { |
95b35127 | 90 | slobidx_t units; |
55394849 | 91 | }; |
10cef602 MM |
92 | typedef struct slob_block slob_t; |
93 | ||
95b35127 NP |
94 | /* |
95 | * free_slob_page: call before a slob_page is returned to the page allocator. | |
96 | */ | |
b8c24c4a | 97 | static inline void free_slob_page(struct page *sp) |
95b35127 | 98 | { |
b8c24c4a | 99 | reset_page_mapcount(sp); |
95b35127 NP |
100 | } |
101 | ||
102 | /* | |
20cecbae | 103 | * All partially free slob pages go on these lists. |
95b35127 | 104 | */ |
20cecbae MM |
105 | #define SLOB_BREAK1 256 |
106 | #define SLOB_BREAK2 1024 | |
107 | static LIST_HEAD(free_slob_small); | |
108 | static LIST_HEAD(free_slob_medium); | |
109 | static LIST_HEAD(free_slob_large); | |
95b35127 NP |
110 | |
111 | /* | |
6e9ed0cc | 112 | * is_slob_page: True for all slob pages (false for bigblock pages) |
95b35127 | 113 | */ |
b8c24c4a | 114 | static inline int is_slob_page(struct page *sp) |
95b35127 | 115 | { |
b8c24c4a | 116 | return PageSlab(sp); |
95b35127 NP |
117 | } |
118 | ||
b8c24c4a | 119 | static inline void set_slob_page(struct page *sp) |
95b35127 | 120 | { |
b8c24c4a | 121 | __SetPageSlab(sp); |
95b35127 NP |
122 | } |
123 | ||
b8c24c4a | 124 | static inline void clear_slob_page(struct page *sp) |
95b35127 | 125 | { |
b8c24c4a | 126 | __ClearPageSlab(sp); |
95b35127 NP |
127 | } |
128 | ||
b8c24c4a | 129 | static inline struct page *slob_page(const void *addr) |
6e9ed0cc | 130 | { |
b8c24c4a | 131 | return virt_to_page(addr); |
6e9ed0cc AW |
132 | } |
133 | ||
95b35127 NP |
134 | /* |
135 | * slob_page_free: true for pages on free_slob_pages list. | |
136 | */ | |
b8c24c4a | 137 | static inline int slob_page_free(struct page *sp) |
95b35127 | 138 | { |
b8c24c4a | 139 | return PageSlobFree(sp); |
95b35127 NP |
140 | } |
141 | ||
b8c24c4a | 142 | static void set_slob_page_free(struct page *sp, struct list_head *list) |
95b35127 | 143 | { |
20cecbae | 144 | list_add(&sp->list, list); |
b8c24c4a | 145 | __SetPageSlobFree(sp); |
95b35127 NP |
146 | } |
147 | ||
b8c24c4a | 148 | static inline void clear_slob_page_free(struct page *sp) |
95b35127 NP |
149 | { |
150 | list_del(&sp->list); | |
b8c24c4a | 151 | __ClearPageSlobFree(sp); |
95b35127 NP |
152 | } |
153 | ||
10cef602 MM |
154 | #define SLOB_UNIT sizeof(slob_t) |
155 | #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT) | |
156 | #define SLOB_ALIGN L1_CACHE_BYTES | |
157 | ||
afc0cedb NP |
158 | /* |
159 | * struct slob_rcu is inserted at the tail of allocated slob blocks, which | |
160 | * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free | |
161 | * the block using call_rcu. | |
162 | */ | |
163 | struct slob_rcu { | |
164 | struct rcu_head head; | |
165 | int size; | |
166 | }; | |
167 | ||
95b35127 NP |
168 | /* |
169 | * slob_lock protects all slob allocator structures. | |
170 | */ | |
10cef602 | 171 | static DEFINE_SPINLOCK(slob_lock); |
10cef602 | 172 | |
95b35127 NP |
173 | /* |
174 | * Encode the given size and next info into a free slob block s. | |
175 | */ | |
176 | static void set_slob(slob_t *s, slobidx_t size, slob_t *next) | |
177 | { | |
178 | slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK); | |
179 | slobidx_t offset = next - base; | |
bcb4ddb4 | 180 | |
95b35127 NP |
181 | if (size > 1) { |
182 | s[0].units = size; | |
183 | s[1].units = offset; | |
184 | } else | |
185 | s[0].units = -offset; | |
186 | } | |
10cef602 | 187 | |
95b35127 NP |
188 | /* |
189 | * Return the size of a slob block. | |
190 | */ | |
191 | static slobidx_t slob_units(slob_t *s) | |
192 | { | |
193 | if (s->units > 0) | |
194 | return s->units; | |
195 | return 1; | |
196 | } | |
197 | ||
198 | /* | |
199 | * Return the next free slob block pointer after this one. | |
200 | */ | |
201 | static slob_t *slob_next(slob_t *s) | |
202 | { | |
203 | slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK); | |
204 | slobidx_t next; | |
205 | ||
206 | if (s[0].units < 0) | |
207 | next = -s[0].units; | |
208 | else | |
209 | next = s[1].units; | |
210 | return base+next; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Returns true if s is the last free block in its page. | |
215 | */ | |
216 | static int slob_last(slob_t *s) | |
217 | { | |
218 | return !((unsigned long)slob_next(s) & ~PAGE_MASK); | |
219 | } | |
220 | ||
6e9ed0cc | 221 | static void *slob_new_pages(gfp_t gfp, int order, int node) |
6193a2ff PM |
222 | { |
223 | void *page; | |
224 | ||
225 | #ifdef CONFIG_NUMA | |
226 | if (node != -1) | |
6484eb3e | 227 | page = alloc_pages_exact_node(node, gfp, order); |
6193a2ff PM |
228 | else |
229 | #endif | |
230 | page = alloc_pages(gfp, order); | |
231 | ||
232 | if (!page) | |
233 | return NULL; | |
234 | ||
235 | return page_address(page); | |
236 | } | |
237 | ||
6e9ed0cc AW |
238 | static void slob_free_pages(void *b, int order) |
239 | { | |
1f0532eb NP |
240 | if (current->reclaim_state) |
241 | current->reclaim_state->reclaimed_slab += 1 << order; | |
6e9ed0cc AW |
242 | free_pages((unsigned long)b, order); |
243 | } | |
244 | ||
95b35127 NP |
245 | /* |
246 | * Allocate a slob block within a given slob_page sp. | |
247 | */ | |
b8c24c4a | 248 | static void *slob_page_alloc(struct page *sp, size_t size, int align) |
10cef602 | 249 | { |
6e9ed0cc | 250 | slob_t *prev, *cur, *aligned = NULL; |
10cef602 | 251 | int delta = 0, units = SLOB_UNITS(size); |
10cef602 | 252 | |
b8c24c4a | 253 | for (prev = NULL, cur = sp->freelist; ; prev = cur, cur = slob_next(cur)) { |
95b35127 NP |
254 | slobidx_t avail = slob_units(cur); |
255 | ||
10cef602 MM |
256 | if (align) { |
257 | aligned = (slob_t *)ALIGN((unsigned long)cur, align); | |
258 | delta = aligned - cur; | |
259 | } | |
95b35127 NP |
260 | if (avail >= units + delta) { /* room enough? */ |
261 | slob_t *next; | |
262 | ||
10cef602 | 263 | if (delta) { /* need to fragment head to align? */ |
95b35127 NP |
264 | next = slob_next(cur); |
265 | set_slob(aligned, avail - delta, next); | |
266 | set_slob(cur, delta, aligned); | |
10cef602 MM |
267 | prev = cur; |
268 | cur = aligned; | |
95b35127 | 269 | avail = slob_units(cur); |
10cef602 MM |
270 | } |
271 | ||
95b35127 NP |
272 | next = slob_next(cur); |
273 | if (avail == units) { /* exact fit? unlink. */ | |
274 | if (prev) | |
275 | set_slob(prev, slob_units(prev), next); | |
276 | else | |
b8c24c4a | 277 | sp->freelist = next; |
95b35127 NP |
278 | } else { /* fragment */ |
279 | if (prev) | |
280 | set_slob(prev, slob_units(prev), cur + units); | |
281 | else | |
b8c24c4a | 282 | sp->freelist = cur + units; |
95b35127 | 283 | set_slob(cur + units, avail - units, next); |
10cef602 MM |
284 | } |
285 | ||
95b35127 NP |
286 | sp->units -= units; |
287 | if (!sp->units) | |
288 | clear_slob_page_free(sp); | |
10cef602 MM |
289 | return cur; |
290 | } | |
95b35127 NP |
291 | if (slob_last(cur)) |
292 | return NULL; | |
293 | } | |
294 | } | |
10cef602 | 295 | |
95b35127 NP |
296 | /* |
297 | * slob_alloc: entry point into the slob allocator. | |
298 | */ | |
6193a2ff | 299 | static void *slob_alloc(size_t size, gfp_t gfp, int align, int node) |
95b35127 | 300 | { |
b8c24c4a | 301 | struct page *sp; |
d6269543 | 302 | struct list_head *prev; |
20cecbae | 303 | struct list_head *slob_list; |
95b35127 NP |
304 | slob_t *b = NULL; |
305 | unsigned long flags; | |
10cef602 | 306 | |
20cecbae MM |
307 | if (size < SLOB_BREAK1) |
308 | slob_list = &free_slob_small; | |
309 | else if (size < SLOB_BREAK2) | |
310 | slob_list = &free_slob_medium; | |
311 | else | |
312 | slob_list = &free_slob_large; | |
313 | ||
95b35127 NP |
314 | spin_lock_irqsave(&slob_lock, flags); |
315 | /* Iterate through each partially free page, try to find room */ | |
20cecbae | 316 | list_for_each_entry(sp, slob_list, list) { |
6193a2ff PM |
317 | #ifdef CONFIG_NUMA |
318 | /* | |
319 | * If there's a node specification, search for a partial | |
320 | * page with a matching node id in the freelist. | |
321 | */ | |
b8c24c4a | 322 | if (node != -1 && page_to_nid(sp) != node) |
6193a2ff PM |
323 | continue; |
324 | #endif | |
d6269543 MM |
325 | /* Enough room on this page? */ |
326 | if (sp->units < SLOB_UNITS(size)) | |
327 | continue; | |
6193a2ff | 328 | |
d6269543 MM |
329 | /* Attempt to alloc */ |
330 | prev = sp->list.prev; | |
331 | b = slob_page_alloc(sp, size, align); | |
332 | if (!b) | |
333 | continue; | |
334 | ||
335 | /* Improve fragment distribution and reduce our average | |
336 | * search time by starting our next search here. (see | |
337 | * Knuth vol 1, sec 2.5, pg 449) */ | |
20cecbae MM |
338 | if (prev != slob_list->prev && |
339 | slob_list->next != prev->next) | |
340 | list_move_tail(slob_list, prev->next); | |
d6269543 | 341 | break; |
10cef602 | 342 | } |
95b35127 NP |
343 | spin_unlock_irqrestore(&slob_lock, flags); |
344 | ||
345 | /* Not enough space: must allocate a new page */ | |
346 | if (!b) { | |
6e9ed0cc | 347 | b = slob_new_pages(gfp & ~__GFP_ZERO, 0, node); |
95b35127 | 348 | if (!b) |
6e9ed0cc AW |
349 | return NULL; |
350 | sp = slob_page(b); | |
95b35127 NP |
351 | set_slob_page(sp); |
352 | ||
353 | spin_lock_irqsave(&slob_lock, flags); | |
354 | sp->units = SLOB_UNITS(PAGE_SIZE); | |
b8c24c4a | 355 | sp->freelist = b; |
95b35127 NP |
356 | INIT_LIST_HEAD(&sp->list); |
357 | set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE)); | |
20cecbae | 358 | set_slob_page_free(sp, slob_list); |
95b35127 NP |
359 | b = slob_page_alloc(sp, size, align); |
360 | BUG_ON(!b); | |
361 | spin_unlock_irqrestore(&slob_lock, flags); | |
362 | } | |
d07dbea4 CL |
363 | if (unlikely((gfp & __GFP_ZERO) && b)) |
364 | memset(b, 0, size); | |
95b35127 | 365 | return b; |
10cef602 MM |
366 | } |
367 | ||
95b35127 NP |
368 | /* |
369 | * slob_free: entry point into the slob allocator. | |
370 | */ | |
10cef602 MM |
371 | static void slob_free(void *block, int size) |
372 | { | |
b8c24c4a | 373 | struct page *sp; |
95b35127 NP |
374 | slob_t *prev, *next, *b = (slob_t *)block; |
375 | slobidx_t units; | |
10cef602 | 376 | unsigned long flags; |
d602daba | 377 | struct list_head *slob_list; |
10cef602 | 378 | |
2408c550 | 379 | if (unlikely(ZERO_OR_NULL_PTR(block))) |
10cef602 | 380 | return; |
95b35127 | 381 | BUG_ON(!size); |
10cef602 | 382 | |
6e9ed0cc | 383 | sp = slob_page(block); |
95b35127 | 384 | units = SLOB_UNITS(size); |
10cef602 | 385 | |
10cef602 | 386 | spin_lock_irqsave(&slob_lock, flags); |
10cef602 | 387 | |
95b35127 NP |
388 | if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) { |
389 | /* Go directly to page allocator. Do not pass slob allocator */ | |
390 | if (slob_page_free(sp)) | |
391 | clear_slob_page_free(sp); | |
6fb8f424 | 392 | spin_unlock_irqrestore(&slob_lock, flags); |
95b35127 NP |
393 | clear_slob_page(sp); |
394 | free_slob_page(sp); | |
1f0532eb | 395 | slob_free_pages(b, 0); |
6fb8f424 | 396 | return; |
95b35127 | 397 | } |
10cef602 | 398 | |
95b35127 NP |
399 | if (!slob_page_free(sp)) { |
400 | /* This slob page is about to become partially free. Easy! */ | |
401 | sp->units = units; | |
b8c24c4a | 402 | sp->freelist = b; |
95b35127 NP |
403 | set_slob(b, units, |
404 | (void *)((unsigned long)(b + | |
405 | SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK)); | |
d602daba BL |
406 | if (size < SLOB_BREAK1) |
407 | slob_list = &free_slob_small; | |
408 | else if (size < SLOB_BREAK2) | |
409 | slob_list = &free_slob_medium; | |
410 | else | |
411 | slob_list = &free_slob_large; | |
412 | set_slob_page_free(sp, slob_list); | |
95b35127 NP |
413 | goto out; |
414 | } | |
415 | ||
416 | /* | |
417 | * Otherwise the page is already partially free, so find reinsertion | |
418 | * point. | |
419 | */ | |
420 | sp->units += units; | |
10cef602 | 421 | |
b8c24c4a CL |
422 | if (b < (slob_t *)sp->freelist) { |
423 | if (b + units == sp->freelist) { | |
424 | units += slob_units(sp->freelist); | |
425 | sp->freelist = slob_next(sp->freelist); | |
679299b3 | 426 | } |
b8c24c4a CL |
427 | set_slob(b, units, sp->freelist); |
428 | sp->freelist = b; | |
95b35127 | 429 | } else { |
b8c24c4a | 430 | prev = sp->freelist; |
95b35127 NP |
431 | next = slob_next(prev); |
432 | while (b > next) { | |
433 | prev = next; | |
434 | next = slob_next(prev); | |
435 | } | |
10cef602 | 436 | |
95b35127 NP |
437 | if (!slob_last(prev) && b + units == next) { |
438 | units += slob_units(next); | |
439 | set_slob(b, units, slob_next(next)); | |
440 | } else | |
441 | set_slob(b, units, next); | |
442 | ||
443 | if (prev + slob_units(prev) == b) { | |
444 | units = slob_units(b) + slob_units(prev); | |
445 | set_slob(prev, units, slob_next(b)); | |
446 | } else | |
447 | set_slob(prev, slob_units(prev), b); | |
448 | } | |
449 | out: | |
10cef602 MM |
450 | spin_unlock_irqrestore(&slob_lock, flags); |
451 | } | |
452 | ||
95b35127 NP |
453 | /* |
454 | * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend. | |
455 | */ | |
456 | ||
6193a2ff | 457 | void *__kmalloc_node(size_t size, gfp_t gfp, int node) |
10cef602 | 458 | { |
6cb8f913 | 459 | unsigned int *m; |
55394849 | 460 | int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); |
3eae2cb2 | 461 | void *ret; |
55394849 | 462 | |
bd50cfa8 SR |
463 | gfp &= gfp_allowed_mask; |
464 | ||
19cefdff | 465 | lockdep_trace_alloc(gfp); |
cf40bd16 | 466 | |
55394849 | 467 | if (size < PAGE_SIZE - align) { |
6cb8f913 CL |
468 | if (!size) |
469 | return ZERO_SIZE_PTR; | |
470 | ||
6193a2ff | 471 | m = slob_alloc(size + align, gfp, align, node); |
3eae2cb2 | 472 | |
239f49c0 MK |
473 | if (!m) |
474 | return NULL; | |
475 | *m = size; | |
3eae2cb2 EGM |
476 | ret = (void *)m + align; |
477 | ||
ca2b84cb EGM |
478 | trace_kmalloc_node(_RET_IP_, ret, |
479 | size, size + align, gfp, node); | |
d87a133f | 480 | } else { |
3eae2cb2 | 481 | unsigned int order = get_order(size); |
d87a133f | 482 | |
8df275af DR |
483 | if (likely(order)) |
484 | gfp |= __GFP_COMP; | |
485 | ret = slob_new_pages(gfp, order, node); | |
d87a133f NP |
486 | if (ret) { |
487 | struct page *page; | |
488 | page = virt_to_page(ret); | |
489 | page->private = size; | |
490 | } | |
3eae2cb2 | 491 | |
ca2b84cb EGM |
492 | trace_kmalloc_node(_RET_IP_, ret, |
493 | size, PAGE_SIZE << order, gfp, node); | |
10cef602 | 494 | } |
3eae2cb2 | 495 | |
4374e616 | 496 | kmemleak_alloc(ret, size, 1, gfp); |
3eae2cb2 | 497 | return ret; |
10cef602 | 498 | } |
6193a2ff | 499 | EXPORT_SYMBOL(__kmalloc_node); |
10cef602 MM |
500 | |
501 | void kfree(const void *block) | |
502 | { | |
b8c24c4a | 503 | struct page *sp; |
10cef602 | 504 | |
2121db74 PE |
505 | trace_kfree(_RET_IP_, block); |
506 | ||
2408c550 | 507 | if (unlikely(ZERO_OR_NULL_PTR(block))) |
10cef602 | 508 | return; |
4374e616 | 509 | kmemleak_free(block); |
10cef602 | 510 | |
6e9ed0cc AW |
511 | sp = slob_page(block); |
512 | if (is_slob_page(sp)) { | |
55394849 NP |
513 | int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); |
514 | unsigned int *m = (unsigned int *)(block - align); | |
515 | slob_free(m, *m + align); | |
d87a133f | 516 | } else |
b8c24c4a | 517 | put_page(sp); |
10cef602 | 518 | } |
10cef602 MM |
519 | EXPORT_SYMBOL(kfree); |
520 | ||
d87a133f | 521 | /* can't use ksize for kmem_cache_alloc memory, only kmalloc */ |
fd76bab2 | 522 | size_t ksize(const void *block) |
10cef602 | 523 | { |
b8c24c4a | 524 | struct page *sp; |
10cef602 | 525 | |
ef8b4520 CL |
526 | BUG_ON(!block); |
527 | if (unlikely(block == ZERO_SIZE_PTR)) | |
10cef602 MM |
528 | return 0; |
529 | ||
6e9ed0cc AW |
530 | sp = slob_page(block); |
531 | if (is_slob_page(sp)) { | |
70096a56 MM |
532 | int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); |
533 | unsigned int *m = (unsigned int *)(block - align); | |
534 | return SLOB_UNITS(*m) * SLOB_UNIT; | |
535 | } else | |
b8c24c4a | 536 | return sp->private; |
10cef602 | 537 | } |
b1aabecd | 538 | EXPORT_SYMBOL(ksize); |
10cef602 MM |
539 | |
540 | struct kmem_cache { | |
541 | unsigned int size, align; | |
afc0cedb | 542 | unsigned long flags; |
10cef602 | 543 | const char *name; |
51cc5068 | 544 | void (*ctor)(void *); |
10cef602 MM |
545 | }; |
546 | ||
547 | struct kmem_cache *kmem_cache_create(const char *name, size_t size, | |
51cc5068 | 548 | size_t align, unsigned long flags, void (*ctor)(void *)) |
10cef602 MM |
549 | { |
550 | struct kmem_cache *c; | |
551 | ||
0701a9e6 | 552 | c = slob_alloc(sizeof(struct kmem_cache), |
5e18e2b8 | 553 | GFP_KERNEL, ARCH_KMALLOC_MINALIGN, -1); |
10cef602 MM |
554 | |
555 | if (c) { | |
556 | c->name = name; | |
557 | c->size = size; | |
afc0cedb | 558 | if (flags & SLAB_DESTROY_BY_RCU) { |
afc0cedb NP |
559 | /* leave room for rcu footer at the end of object */ |
560 | c->size += sizeof(struct slob_rcu); | |
561 | } | |
562 | c->flags = flags; | |
10cef602 | 563 | c->ctor = ctor; |
10cef602 | 564 | /* ignore alignment unless it's forced */ |
5af60839 | 565 | c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0; |
55394849 NP |
566 | if (c->align < ARCH_SLAB_MINALIGN) |
567 | c->align = ARCH_SLAB_MINALIGN; | |
10cef602 MM |
568 | if (c->align < align) |
569 | c->align = align; | |
bc0055ae AM |
570 | } else if (flags & SLAB_PANIC) |
571 | panic("Cannot create slab cache %s\n", name); | |
10cef602 | 572 | |
4374e616 | 573 | kmemleak_alloc(c, sizeof(struct kmem_cache), 1, GFP_KERNEL); |
10cef602 MM |
574 | return c; |
575 | } | |
576 | EXPORT_SYMBOL(kmem_cache_create); | |
577 | ||
133d205a | 578 | void kmem_cache_destroy(struct kmem_cache *c) |
10cef602 | 579 | { |
4374e616 | 580 | kmemleak_free(c); |
7ed9f7e5 PM |
581 | if (c->flags & SLAB_DESTROY_BY_RCU) |
582 | rcu_barrier(); | |
10cef602 | 583 | slob_free(c, sizeof(struct kmem_cache)); |
10cef602 MM |
584 | } |
585 | EXPORT_SYMBOL(kmem_cache_destroy); | |
586 | ||
6193a2ff | 587 | void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node) |
10cef602 MM |
588 | { |
589 | void *b; | |
590 | ||
bd50cfa8 SR |
591 | flags &= gfp_allowed_mask; |
592 | ||
593 | lockdep_trace_alloc(flags); | |
594 | ||
3eae2cb2 | 595 | if (c->size < PAGE_SIZE) { |
6193a2ff | 596 | b = slob_alloc(c->size, flags, c->align, node); |
ca2b84cb EGM |
597 | trace_kmem_cache_alloc_node(_RET_IP_, b, c->size, |
598 | SLOB_UNITS(c->size) * SLOB_UNIT, | |
599 | flags, node); | |
3eae2cb2 | 600 | } else { |
6e9ed0cc | 601 | b = slob_new_pages(flags, get_order(c->size), node); |
ca2b84cb EGM |
602 | trace_kmem_cache_alloc_node(_RET_IP_, b, c->size, |
603 | PAGE_SIZE << get_order(c->size), | |
604 | flags, node); | |
3eae2cb2 | 605 | } |
10cef602 MM |
606 | |
607 | if (c->ctor) | |
51cc5068 | 608 | c->ctor(b); |
10cef602 | 609 | |
4374e616 | 610 | kmemleak_alloc_recursive(b, c->size, 1, c->flags, flags); |
10cef602 MM |
611 | return b; |
612 | } | |
6193a2ff | 613 | EXPORT_SYMBOL(kmem_cache_alloc_node); |
10cef602 | 614 | |
afc0cedb | 615 | static void __kmem_cache_free(void *b, int size) |
10cef602 | 616 | { |
afc0cedb NP |
617 | if (size < PAGE_SIZE) |
618 | slob_free(b, size); | |
10cef602 | 619 | else |
6e9ed0cc | 620 | slob_free_pages(b, get_order(size)); |
afc0cedb NP |
621 | } |
622 | ||
623 | static void kmem_rcu_free(struct rcu_head *head) | |
624 | { | |
625 | struct slob_rcu *slob_rcu = (struct slob_rcu *)head; | |
626 | void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu)); | |
627 | ||
628 | __kmem_cache_free(b, slob_rcu->size); | |
629 | } | |
630 | ||
631 | void kmem_cache_free(struct kmem_cache *c, void *b) | |
632 | { | |
4374e616 | 633 | kmemleak_free_recursive(b, c->flags); |
afc0cedb NP |
634 | if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) { |
635 | struct slob_rcu *slob_rcu; | |
636 | slob_rcu = b + (c->size - sizeof(struct slob_rcu)); | |
afc0cedb NP |
637 | slob_rcu->size = c->size; |
638 | call_rcu(&slob_rcu->head, kmem_rcu_free); | |
639 | } else { | |
afc0cedb NP |
640 | __kmem_cache_free(b, c->size); |
641 | } | |
3eae2cb2 | 642 | |
ca2b84cb | 643 | trace_kmem_cache_free(_RET_IP_, b); |
10cef602 MM |
644 | } |
645 | EXPORT_SYMBOL(kmem_cache_free); | |
646 | ||
647 | unsigned int kmem_cache_size(struct kmem_cache *c) | |
648 | { | |
649 | return c->size; | |
650 | } | |
651 | EXPORT_SYMBOL(kmem_cache_size); | |
652 | ||
2e892f43 CL |
653 | int kmem_cache_shrink(struct kmem_cache *d) |
654 | { | |
655 | return 0; | |
656 | } | |
657 | EXPORT_SYMBOL(kmem_cache_shrink); | |
658 | ||
84a01c2f PM |
659 | static unsigned int slob_ready __read_mostly; |
660 | ||
661 | int slab_is_available(void) | |
662 | { | |
663 | return slob_ready; | |
664 | } | |
665 | ||
bcb4ddb4 DG |
666 | void __init kmem_cache_init(void) |
667 | { | |
84a01c2f | 668 | slob_ready = 1; |
10cef602 | 669 | } |
bbff2e43 WF |
670 | |
671 | void __init kmem_cache_init_late(void) | |
672 | { | |
673 | /* Nothing to do */ | |
674 | } |