Blame view

mm/slob.c 15.9 KB
10cef6029   Matt Mackall   [PATCH] slob: int...
1
2
3
4
5
  /*
   * SLOB Allocator: Simple List Of Blocks
   *
   * Matt Mackall <mpm@selenic.com> 12/30/03
   *
6193a2ff1   Paul Mundt   slob: initial NUM...
6
7
   * NUMA support by Paul Mundt, 2007.
   *
10cef6029   Matt Mackall   [PATCH] slob: int...
8
9
10
11
   * How SLOB works:
   *
   * The core of SLOB is a traditional K&R style heap allocator, with
   * support for returning aligned objects. The granularity of this
553948491   Nick Piggin   slob: improved al...
12
13
   * allocator is as little as 2 bytes, however typically most architectures
   * will require 4 bytes on 32-bit and 8 bytes on 64-bit.
95b35127f   Nick Piggin   slob: rework free...
14
   *
20cecbae4   Matt Mackall   slob: reduce exte...
15
16
17
18
19
20
21
22
23
24
25
   * The slob heap is a set of linked list of pages from alloc_pages(),
   * and within each page, there is a singly-linked list of free blocks
   * (slob_t). The heap is grown on demand. To reduce fragmentation,
   * heap pages are segregated into three lists, with objects less than
   * 256 bytes, objects less than 1024 bytes, and all other objects.
   *
   * Allocation from heap involves first searching for a page with
   * sufficient free blocks (using a next-fit-like approach) followed by
   * a first-fit scan of the page. Deallocation inserts objects back
   * into the free list in address order, so this is effectively an
   * address-ordered first fit.
10cef6029   Matt Mackall   [PATCH] slob: int...
26
27
   *
   * Above this is an implementation of kmalloc/kfree. Blocks returned
553948491   Nick Piggin   slob: improved al...
28
   * from kmalloc are prepended with a 4-byte header with the kmalloc size.
10cef6029   Matt Mackall   [PATCH] slob: int...
29
   * If kmalloc is asked for objects of PAGE_SIZE or larger, it calls
6193a2ff1   Paul Mundt   slob: initial NUM...
30
   * alloc_pages() directly, allocating compound pages so the page order
d87a133fc   Nick Piggin   slob: remove bigb...
31
32
33
34
   * does not have to be separately tracked, and also stores the exact
   * allocation size in page->private so that it can be used to accurately
   * provide ksize(). These objects are detected in kfree() because slob_page()
   * is false for them.
10cef6029   Matt Mackall   [PATCH] slob: int...
35
36
   *
   * SLAB is emulated on top of SLOB by simply calling constructors and
95b35127f   Nick Piggin   slob: rework free...
37
38
39
40
   * destructors for every SLAB allocation. Objects are returned with the
   * 4-byte alignment unless the SLAB_HWCACHE_ALIGN flag is set, in which
   * case the low-level allocator will fragment blocks to create the proper
   * alignment. Again, objects of page-size or greater are allocated by
6193a2ff1   Paul Mundt   slob: initial NUM...
41
   * calling alloc_pages(). As SLAB objects know their size, no separate
95b35127f   Nick Piggin   slob: rework free...
42
   * size bookkeeping is necessary and there is essentially no allocation
d87a133fc   Nick Piggin   slob: remove bigb...
43
44
   * space overhead, and compound pages aren't needed for multi-page
   * allocations.
6193a2ff1   Paul Mundt   slob: initial NUM...
45
46
47
48
49
50
51
52
53
54
55
56
57
   *
   * NUMA support in SLOB is fairly simplistic, pushing most of the real
   * logic down to the page allocator, and simply doing the node accounting
   * on the upper levels. In the event that a node id is explicitly
   * provided, alloc_pages_node() with the specified node id is used
   * instead. The common case (or when the node id isn't explicitly provided)
   * will default to the current node, as per numa_node_id().
   *
   * Node aware pages are still inserted in to the global freelist, and
   * these are scanned for by matching against the node id encoded in the
   * page flags. As a result, block allocations that can be satisfied from
   * the freelist will only be done so on pages residing on the same node,
   * in order to prevent random node placement.
10cef6029   Matt Mackall   [PATCH] slob: int...
58
   */
95b35127f   Nick Piggin   slob: rework free...
59
  #include <linux/kernel.h>
10cef6029   Matt Mackall   [PATCH] slob: int...
60
61
62
63
64
  #include <linux/slab.h>
  #include <linux/mm.h>
  #include <linux/cache.h>
  #include <linux/init.h>
  #include <linux/module.h>
afc0cedbe   Nick Piggin   slob: implement R...
65
  #include <linux/rcupdate.h>
95b35127f   Nick Piggin   slob: rework free...
66
67
  #include <linux/list.h>
  #include <asm/atomic.h>
95b35127f   Nick Piggin   slob: rework free...
68
69
70
71
72
73
74
75
  /*
   * slob_block has a field 'units', which indicates size of block if +ve,
   * or offset of next block if -ve (in SLOB_UNITs).
   *
   * Free blocks of size 1 unit simply contain the offset of the next block.
   * Those with larger size contain their size in the first SLOB_UNIT of
   * memory, and the offset of the next free block in the second SLOB_UNIT.
   */
553948491   Nick Piggin   slob: improved al...
76
  #if PAGE_SIZE <= (32767 * 2)
95b35127f   Nick Piggin   slob: rework free...
77
78
79
80
  typedef s16 slobidx_t;
  #else
  typedef s32 slobidx_t;
  #endif
10cef6029   Matt Mackall   [PATCH] slob: int...
81
  struct slob_block {
95b35127f   Nick Piggin   slob: rework free...
82
  	slobidx_t units;
553948491   Nick Piggin   slob: improved al...
83
  };
10cef6029   Matt Mackall   [PATCH] slob: int...
84
  typedef struct slob_block slob_t;
95b35127f   Nick Piggin   slob: rework free...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  /*
   * We use struct page fields to manage some slob allocation aspects,
   * however to avoid the horrible mess in include/linux/mm_types.h, we'll
   * just define our own struct page type variant here.
   */
  struct slob_page {
  	union {
  		struct {
  			unsigned long flags;	/* mandatory */
  			atomic_t _count;	/* mandatory */
  			slobidx_t units;	/* free units left in page */
  			unsigned long pad[2];
  			slob_t *free;		/* first free slob_t in page */
  			struct list_head list;	/* linked list of free pages */
  		};
  		struct page page;
  	};
  };
  static inline void struct_slob_page_wrong_size(void)
  { BUILD_BUG_ON(sizeof(struct slob_page) != sizeof(struct page)); }
  
  /*
   * free_slob_page: call before a slob_page is returned to the page allocator.
   */
  static inline void free_slob_page(struct slob_page *sp)
  {
  	reset_page_mapcount(&sp->page);
  	sp->page.mapping = NULL;
  }
  
  /*
20cecbae4   Matt Mackall   slob: reduce exte...
116
   * All partially free slob pages go on these lists.
95b35127f   Nick Piggin   slob: rework free...
117
   */
20cecbae4   Matt Mackall   slob: reduce exte...
118
119
120
121
122
  #define SLOB_BREAK1 256
  #define SLOB_BREAK2 1024
  static LIST_HEAD(free_slob_small);
  static LIST_HEAD(free_slob_medium);
  static LIST_HEAD(free_slob_large);
95b35127f   Nick Piggin   slob: rework free...
123
124
125
126
127
128
  
  /*
   * slob_page: True for all slob pages (false for bigblock pages)
   */
  static inline int slob_page(struct slob_page *sp)
  {
9023cb7e8   Andy Whitcroft   slob: record page...
129
  	return PageSlobPage((struct page *)sp);
95b35127f   Nick Piggin   slob: rework free...
130
131
132
133
  }
  
  static inline void set_slob_page(struct slob_page *sp)
  {
9023cb7e8   Andy Whitcroft   slob: record page...
134
  	__SetPageSlobPage((struct page *)sp);
95b35127f   Nick Piggin   slob: rework free...
135
136
137
138
  }
  
  static inline void clear_slob_page(struct slob_page *sp)
  {
9023cb7e8   Andy Whitcroft   slob: record page...
139
  	__ClearPageSlobPage((struct page *)sp);
95b35127f   Nick Piggin   slob: rework free...
140
141
142
143
144
145
146
  }
  
  /*
   * slob_page_free: true for pages on free_slob_pages list.
   */
  static inline int slob_page_free(struct slob_page *sp)
  {
9023cb7e8   Andy Whitcroft   slob: record page...
147
  	return PageSlobFree((struct page *)sp);
95b35127f   Nick Piggin   slob: rework free...
148
  }
20cecbae4   Matt Mackall   slob: reduce exte...
149
  static void set_slob_page_free(struct slob_page *sp, struct list_head *list)
95b35127f   Nick Piggin   slob: rework free...
150
  {
20cecbae4   Matt Mackall   slob: reduce exte...
151
  	list_add(&sp->list, list);
9023cb7e8   Andy Whitcroft   slob: record page...
152
  	__SetPageSlobFree((struct page *)sp);
95b35127f   Nick Piggin   slob: rework free...
153
154
155
156
157
  }
  
  static inline void clear_slob_page_free(struct slob_page *sp)
  {
  	list_del(&sp->list);
9023cb7e8   Andy Whitcroft   slob: record page...
158
  	__ClearPageSlobFree((struct page *)sp);
95b35127f   Nick Piggin   slob: rework free...
159
  }
10cef6029   Matt Mackall   [PATCH] slob: int...
160
161
162
  #define SLOB_UNIT sizeof(slob_t)
  #define SLOB_UNITS(size) (((size) + SLOB_UNIT - 1)/SLOB_UNIT)
  #define SLOB_ALIGN L1_CACHE_BYTES
afc0cedbe   Nick Piggin   slob: implement R...
163
164
165
166
167
168
169
170
171
  /*
   * struct slob_rcu is inserted at the tail of allocated slob blocks, which
   * were created with a SLAB_DESTROY_BY_RCU slab. slob_rcu is used to free
   * the block using call_rcu.
   */
  struct slob_rcu {
  	struct rcu_head head;
  	int size;
  };
95b35127f   Nick Piggin   slob: rework free...
172
173
174
  /*
   * slob_lock protects all slob allocator structures.
   */
10cef6029   Matt Mackall   [PATCH] slob: int...
175
  static DEFINE_SPINLOCK(slob_lock);
10cef6029   Matt Mackall   [PATCH] slob: int...
176

95b35127f   Nick Piggin   slob: rework free...
177
178
179
180
181
182
183
  /*
   * Encode the given size and next info into a free slob block s.
   */
  static void set_slob(slob_t *s, slobidx_t size, slob_t *next)
  {
  	slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  	slobidx_t offset = next - base;
bcb4ddb46   Dimitri Gorokhovik   [PATCH] MM: SLOB ...
184

95b35127f   Nick Piggin   slob: rework free...
185
186
187
188
189
190
  	if (size > 1) {
  		s[0].units = size;
  		s[1].units = offset;
  	} else
  		s[0].units = -offset;
  }
10cef6029   Matt Mackall   [PATCH] slob: int...
191

95b35127f   Nick Piggin   slob: rework free...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  /*
   * Return the size of a slob block.
   */
  static slobidx_t slob_units(slob_t *s)
  {
  	if (s->units > 0)
  		return s->units;
  	return 1;
  }
  
  /*
   * Return the next free slob block pointer after this one.
   */
  static slob_t *slob_next(slob_t *s)
  {
  	slob_t *base = (slob_t *)((unsigned long)s & PAGE_MASK);
  	slobidx_t next;
  
  	if (s[0].units < 0)
  		next = -s[0].units;
  	else
  		next = s[1].units;
  	return base+next;
  }
  
  /*
   * Returns true if s is the last free block in its page.
   */
  static int slob_last(slob_t *s)
  {
  	return !((unsigned long)slob_next(s) & ~PAGE_MASK);
  }
6193a2ff1   Paul Mundt   slob: initial NUM...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  static void *slob_new_page(gfp_t gfp, int order, int node)
  {
  	void *page;
  
  #ifdef CONFIG_NUMA
  	if (node != -1)
  		page = alloc_pages_node(node, gfp, order);
  	else
  #endif
  		page = alloc_pages(gfp, order);
  
  	if (!page)
  		return NULL;
  
  	return page_address(page);
  }
95b35127f   Nick Piggin   slob: rework free...
240
241
242
243
  /*
   * Allocate a slob block within a given slob_page sp.
   */
  static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
10cef6029   Matt Mackall   [PATCH] slob: int...
244
245
246
  {
  	slob_t *prev, *cur, *aligned = 0;
  	int delta = 0, units = SLOB_UNITS(size);
10cef6029   Matt Mackall   [PATCH] slob: int...
247

95b35127f   Nick Piggin   slob: rework free...
248
249
  	for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
  		slobidx_t avail = slob_units(cur);
10cef6029   Matt Mackall   [PATCH] slob: int...
250
251
252
253
  		if (align) {
  			aligned = (slob_t *)ALIGN((unsigned long)cur, align);
  			delta = aligned - cur;
  		}
95b35127f   Nick Piggin   slob: rework free...
254
255
  		if (avail >= units + delta) { /* room enough? */
  			slob_t *next;
10cef6029   Matt Mackall   [PATCH] slob: int...
256
  			if (delta) { /* need to fragment head to align? */
95b35127f   Nick Piggin   slob: rework free...
257
258
259
  				next = slob_next(cur);
  				set_slob(aligned, avail - delta, next);
  				set_slob(cur, delta, aligned);
10cef6029   Matt Mackall   [PATCH] slob: int...
260
261
  				prev = cur;
  				cur = aligned;
95b35127f   Nick Piggin   slob: rework free...
262
  				avail = slob_units(cur);
10cef6029   Matt Mackall   [PATCH] slob: int...
263
  			}
95b35127f   Nick Piggin   slob: rework free...
264
265
266
267
268
269
270
271
272
273
274
275
  			next = slob_next(cur);
  			if (avail == units) { /* exact fit? unlink. */
  				if (prev)
  					set_slob(prev, slob_units(prev), next);
  				else
  					sp->free = next;
  			} else { /* fragment */
  				if (prev)
  					set_slob(prev, slob_units(prev), cur + units);
  				else
  					sp->free = cur + units;
  				set_slob(cur + units, avail - units, next);
10cef6029   Matt Mackall   [PATCH] slob: int...
276
  			}
95b35127f   Nick Piggin   slob: rework free...
277
278
279
  			sp->units -= units;
  			if (!sp->units)
  				clear_slob_page_free(sp);
10cef6029   Matt Mackall   [PATCH] slob: int...
280
281
  			return cur;
  		}
95b35127f   Nick Piggin   slob: rework free...
282
283
284
285
  		if (slob_last(cur))
  			return NULL;
  	}
  }
10cef6029   Matt Mackall   [PATCH] slob: int...
286

95b35127f   Nick Piggin   slob: rework free...
287
288
289
  /*
   * slob_alloc: entry point into the slob allocator.
   */
6193a2ff1   Paul Mundt   slob: initial NUM...
290
  static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)
95b35127f   Nick Piggin   slob: rework free...
291
292
  {
  	struct slob_page *sp;
d6269543e   Matt Mackall   slob: reduce list...
293
  	struct list_head *prev;
20cecbae4   Matt Mackall   slob: reduce exte...
294
  	struct list_head *slob_list;
95b35127f   Nick Piggin   slob: rework free...
295
296
  	slob_t *b = NULL;
  	unsigned long flags;
10cef6029   Matt Mackall   [PATCH] slob: int...
297

20cecbae4   Matt Mackall   slob: reduce exte...
298
299
300
301
302
303
  	if (size < SLOB_BREAK1)
  		slob_list = &free_slob_small;
  	else if (size < SLOB_BREAK2)
  		slob_list = &free_slob_medium;
  	else
  		slob_list = &free_slob_large;
95b35127f   Nick Piggin   slob: rework free...
304
305
  	spin_lock_irqsave(&slob_lock, flags);
  	/* Iterate through each partially free page, try to find room */
20cecbae4   Matt Mackall   slob: reduce exte...
306
  	list_for_each_entry(sp, slob_list, list) {
6193a2ff1   Paul Mundt   slob: initial NUM...
307
308
309
310
311
312
313
314
  #ifdef CONFIG_NUMA
  		/*
  		 * If there's a node specification, search for a partial
  		 * page with a matching node id in the freelist.
  		 */
  		if (node != -1 && page_to_nid(&sp->page) != node)
  			continue;
  #endif
d6269543e   Matt Mackall   slob: reduce list...
315
316
317
  		/* Enough room on this page? */
  		if (sp->units < SLOB_UNITS(size))
  			continue;
6193a2ff1   Paul Mundt   slob: initial NUM...
318

d6269543e   Matt Mackall   slob: reduce list...
319
320
321
322
323
324
325
326
327
  		/* Attempt to alloc */
  		prev = sp->list.prev;
  		b = slob_page_alloc(sp, size, align);
  		if (!b)
  			continue;
  
  		/* Improve fragment distribution and reduce our average
  		 * search time by starting our next search here. (see
  		 * Knuth vol 1, sec 2.5, pg 449) */
20cecbae4   Matt Mackall   slob: reduce exte...
328
329
330
  		if (prev != slob_list->prev &&
  				slob_list->next != prev->next)
  			list_move_tail(slob_list, prev->next);
d6269543e   Matt Mackall   slob: reduce list...
331
  		break;
10cef6029   Matt Mackall   [PATCH] slob: int...
332
  	}
95b35127f   Nick Piggin   slob: rework free...
333
334
335
336
  	spin_unlock_irqrestore(&slob_lock, flags);
  
  	/* Not enough space: must allocate a new page */
  	if (!b) {
7fd272550   Linus Torvalds   Avoid double memc...
337
  		b = slob_new_page(gfp & ~__GFP_ZERO, 0, node);
95b35127f   Nick Piggin   slob: rework free...
338
339
340
341
342
343
344
345
346
347
  		if (!b)
  			return 0;
  		sp = (struct slob_page *)virt_to_page(b);
  		set_slob_page(sp);
  
  		spin_lock_irqsave(&slob_lock, flags);
  		sp->units = SLOB_UNITS(PAGE_SIZE);
  		sp->free = b;
  		INIT_LIST_HEAD(&sp->list);
  		set_slob(b, SLOB_UNITS(PAGE_SIZE), b + SLOB_UNITS(PAGE_SIZE));
20cecbae4   Matt Mackall   slob: reduce exte...
348
  		set_slob_page_free(sp, slob_list);
95b35127f   Nick Piggin   slob: rework free...
349
350
351
352
  		b = slob_page_alloc(sp, size, align);
  		BUG_ON(!b);
  		spin_unlock_irqrestore(&slob_lock, flags);
  	}
d07dbea46   Christoph Lameter   Slab allocators: ...
353
354
  	if (unlikely((gfp & __GFP_ZERO) && b))
  		memset(b, 0, size);
95b35127f   Nick Piggin   slob: rework free...
355
  	return b;
10cef6029   Matt Mackall   [PATCH] slob: int...
356
  }
95b35127f   Nick Piggin   slob: rework free...
357
358
359
  /*
   * slob_free: entry point into the slob allocator.
   */
10cef6029   Matt Mackall   [PATCH] slob: int...
360
361
  static void slob_free(void *block, int size)
  {
95b35127f   Nick Piggin   slob: rework free...
362
363
364
  	struct slob_page *sp;
  	slob_t *prev, *next, *b = (slob_t *)block;
  	slobidx_t units;
10cef6029   Matt Mackall   [PATCH] slob: int...
365
  	unsigned long flags;
2408c5503   Satyam Sharma   {slub, slob}: use...
366
  	if (unlikely(ZERO_OR_NULL_PTR(block)))
10cef6029   Matt Mackall   [PATCH] slob: int...
367
  		return;
95b35127f   Nick Piggin   slob: rework free...
368
  	BUG_ON(!size);
10cef6029   Matt Mackall   [PATCH] slob: int...
369

95b35127f   Nick Piggin   slob: rework free...
370
371
  	sp = (struct slob_page *)virt_to_page(block);
  	units = SLOB_UNITS(size);
10cef6029   Matt Mackall   [PATCH] slob: int...
372

10cef6029   Matt Mackall   [PATCH] slob: int...
373
  	spin_lock_irqsave(&slob_lock, flags);
10cef6029   Matt Mackall   [PATCH] slob: int...
374

95b35127f   Nick Piggin   slob: rework free...
375
376
377
378
379
380
381
382
383
  	if (sp->units + units == SLOB_UNITS(PAGE_SIZE)) {
  		/* Go directly to page allocator. Do not pass slob allocator */
  		if (slob_page_free(sp))
  			clear_slob_page_free(sp);
  		clear_slob_page(sp);
  		free_slob_page(sp);
  		free_page((unsigned long)b);
  		goto out;
  	}
10cef6029   Matt Mackall   [PATCH] slob: int...
384

95b35127f   Nick Piggin   slob: rework free...
385
386
387
388
389
390
391
  	if (!slob_page_free(sp)) {
  		/* This slob page is about to become partially free. Easy! */
  		sp->units = units;
  		sp->free = b;
  		set_slob(b, units,
  			(void *)((unsigned long)(b +
  					SLOB_UNITS(PAGE_SIZE)) & PAGE_MASK));
20cecbae4   Matt Mackall   slob: reduce exte...
392
  		set_slob_page_free(sp, &free_slob_small);
95b35127f   Nick Piggin   slob: rework free...
393
394
395
396
397
398
399
400
  		goto out;
  	}
  
  	/*
  	 * Otherwise the page is already partially free, so find reinsertion
  	 * point.
  	 */
  	sp->units += units;
10cef6029   Matt Mackall   [PATCH] slob: int...
401

95b35127f   Nick Piggin   slob: rework free...
402
  	if (b < sp->free) {
679299b32   Matt Mackall   slob: fix free bl...
403
404
405
406
  		if (b + units == sp->free) {
  			units += slob_units(sp->free);
  			sp->free = slob_next(sp->free);
  		}
95b35127f   Nick Piggin   slob: rework free...
407
408
409
410
411
412
413
414
415
  		set_slob(b, units, sp->free);
  		sp->free = b;
  	} else {
  		prev = sp->free;
  		next = slob_next(prev);
  		while (b > next) {
  			prev = next;
  			next = slob_next(prev);
  		}
10cef6029   Matt Mackall   [PATCH] slob: int...
416

95b35127f   Nick Piggin   slob: rework free...
417
418
419
420
421
422
423
424
425
426
427
428
429
  		if (!slob_last(prev) && b + units == next) {
  			units += slob_units(next);
  			set_slob(b, units, slob_next(next));
  		} else
  			set_slob(b, units, next);
  
  		if (prev + slob_units(prev) == b) {
  			units = slob_units(b) + slob_units(prev);
  			set_slob(prev, units, slob_next(b));
  		} else
  			set_slob(prev, slob_units(prev), b);
  	}
  out:
10cef6029   Matt Mackall   [PATCH] slob: int...
430
431
  	spin_unlock_irqrestore(&slob_lock, flags);
  }
95b35127f   Nick Piggin   slob: rework free...
432
433
434
  /*
   * End of slob allocator proper. Begin kmem_cache_alloc and kmalloc frontend.
   */
553948491   Nick Piggin   slob: improved al...
435
436
437
438
439
440
441
  #ifndef ARCH_KMALLOC_MINALIGN
  #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long)
  #endif
  
  #ifndef ARCH_SLAB_MINALIGN
  #define ARCH_SLAB_MINALIGN __alignof__(unsigned long)
  #endif
6193a2ff1   Paul Mundt   slob: initial NUM...
442
  void *__kmalloc_node(size_t size, gfp_t gfp, int node)
10cef6029   Matt Mackall   [PATCH] slob: int...
443
  {
6cb8f9132   Christoph Lameter   Slab allocators: ...
444
  	unsigned int *m;
553948491   Nick Piggin   slob: improved al...
445
446
447
  	int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  
  	if (size < PAGE_SIZE - align) {
6cb8f9132   Christoph Lameter   Slab allocators: ...
448
449
  		if (!size)
  			return ZERO_SIZE_PTR;
6193a2ff1   Paul Mundt   slob: initial NUM...
450
  		m = slob_alloc(size + align, gfp, align, node);
239f49c08   MinChan Kim   slob: Fix to retu...
451
452
453
  		if (!m)
  			return NULL;
  		*m = size;
553948491   Nick Piggin   slob: improved al...
454
  		return (void *)m + align;
d87a133fc   Nick Piggin   slob: remove bigb...
455
456
  	} else {
  		void *ret;
6193a2ff1   Paul Mundt   slob: initial NUM...
457
  		ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node);
d87a133fc   Nick Piggin   slob: remove bigb...
458
459
460
461
462
463
  		if (ret) {
  			struct page *page;
  			page = virt_to_page(ret);
  			page->private = size;
  		}
  		return ret;
10cef6029   Matt Mackall   [PATCH] slob: int...
464
  	}
10cef6029   Matt Mackall   [PATCH] slob: int...
465
  }
6193a2ff1   Paul Mundt   slob: initial NUM...
466
  EXPORT_SYMBOL(__kmalloc_node);
10cef6029   Matt Mackall   [PATCH] slob: int...
467
468
469
  
  void kfree(const void *block)
  {
95b35127f   Nick Piggin   slob: rework free...
470
  	struct slob_page *sp;
10cef6029   Matt Mackall   [PATCH] slob: int...
471

2408c5503   Satyam Sharma   {slub, slob}: use...
472
  	if (unlikely(ZERO_OR_NULL_PTR(block)))
10cef6029   Matt Mackall   [PATCH] slob: int...
473
  		return;
95b35127f   Nick Piggin   slob: rework free...
474
  	sp = (struct slob_page *)virt_to_page(block);
d87a133fc   Nick Piggin   slob: remove bigb...
475
  	if (slob_page(sp)) {
553948491   Nick Piggin   slob: improved al...
476
477
478
  		int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  		unsigned int *m = (unsigned int *)(block - align);
  		slob_free(m, *m + align);
d87a133fc   Nick Piggin   slob: remove bigb...
479
480
  	} else
  		put_page(&sp->page);
10cef6029   Matt Mackall   [PATCH] slob: int...
481
  }
10cef6029   Matt Mackall   [PATCH] slob: int...
482
  EXPORT_SYMBOL(kfree);
d87a133fc   Nick Piggin   slob: remove bigb...
483
  /* can't use ksize for kmem_cache_alloc memory, only kmalloc */
fd76bab2f   Pekka Enberg   slab: introduce k...
484
  size_t ksize(const void *block)
10cef6029   Matt Mackall   [PATCH] slob: int...
485
  {
95b35127f   Nick Piggin   slob: rework free...
486
  	struct slob_page *sp;
10cef6029   Matt Mackall   [PATCH] slob: int...
487

ef8b4520b   Christoph Lameter   Slab allocators: ...
488
489
  	BUG_ON(!block);
  	if (unlikely(block == ZERO_SIZE_PTR))
10cef6029   Matt Mackall   [PATCH] slob: int...
490
  		return 0;
95b35127f   Nick Piggin   slob: rework free...
491
  	sp = (struct slob_page *)virt_to_page(block);
70096a561   Matt Mackall   SLOB: fix bogus k...
492
493
494
495
496
  	if (slob_page(sp)) {
  		int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
  		unsigned int *m = (unsigned int *)(block - align);
  		return SLOB_UNITS(*m) * SLOB_UNIT;
  	} else
d87a133fc   Nick Piggin   slob: remove bigb...
497
  		return sp->page.private;
10cef6029   Matt Mackall   [PATCH] slob: int...
498
499
500
501
  }
  
  struct kmem_cache {
  	unsigned int size, align;
afc0cedbe   Nick Piggin   slob: implement R...
502
  	unsigned long flags;
10cef6029   Matt Mackall   [PATCH] slob: int...
503
  	const char *name;
51cc50685   Alexey Dobriyan   SL*B: drop kmem c...
504
  	void (*ctor)(void *);
10cef6029   Matt Mackall   [PATCH] slob: int...
505
506
507
  };
  
  struct kmem_cache *kmem_cache_create(const char *name, size_t size,
51cc50685   Alexey Dobriyan   SL*B: drop kmem c...
508
  	size_t align, unsigned long flags, void (*ctor)(void *))
10cef6029   Matt Mackall   [PATCH] slob: int...
509
510
  {
  	struct kmem_cache *c;
0701a9e64   Yi Li   slob: fix bug - w...
511
  	c = slob_alloc(sizeof(struct kmem_cache),
5e18e2b8b   Catalin Marinas   slob: do not pass...
512
  		GFP_KERNEL, ARCH_KMALLOC_MINALIGN, -1);
10cef6029   Matt Mackall   [PATCH] slob: int...
513
514
515
516
  
  	if (c) {
  		c->name = name;
  		c->size = size;
afc0cedbe   Nick Piggin   slob: implement R...
517
  		if (flags & SLAB_DESTROY_BY_RCU) {
afc0cedbe   Nick Piggin   slob: implement R...
518
519
520
521
  			/* leave room for rcu footer at the end of object */
  			c->size += sizeof(struct slob_rcu);
  		}
  		c->flags = flags;
10cef6029   Matt Mackall   [PATCH] slob: int...
522
  		c->ctor = ctor;
10cef6029   Matt Mackall   [PATCH] slob: int...
523
  		/* ignore alignment unless it's forced */
5af608399   Christoph Lameter   slab allocators: ...
524
  		c->align = (flags & SLAB_HWCACHE_ALIGN) ? SLOB_ALIGN : 0;
553948491   Nick Piggin   slob: improved al...
525
526
  		if (c->align < ARCH_SLAB_MINALIGN)
  			c->align = ARCH_SLAB_MINALIGN;
10cef6029   Matt Mackall   [PATCH] slob: int...
527
528
  		if (c->align < align)
  			c->align = align;
bc0055aee   Akinobu Mita   slob: handle SLAB...
529
530
531
  	} else if (flags & SLAB_PANIC)
  		panic("Cannot create slab cache %s
  ", name);
10cef6029   Matt Mackall   [PATCH] slob: int...
532
533
534
535
  
  	return c;
  }
  EXPORT_SYMBOL(kmem_cache_create);
133d205a1   Alexey Dobriyan   [PATCH] Make kmem...
536
  void kmem_cache_destroy(struct kmem_cache *c)
10cef6029   Matt Mackall   [PATCH] slob: int...
537
538
  {
  	slob_free(c, sizeof(struct kmem_cache));
10cef6029   Matt Mackall   [PATCH] slob: int...
539
540
  }
  EXPORT_SYMBOL(kmem_cache_destroy);
6193a2ff1   Paul Mundt   slob: initial NUM...
541
  void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
10cef6029   Matt Mackall   [PATCH] slob: int...
542
543
544
545
  {
  	void *b;
  
  	if (c->size < PAGE_SIZE)
6193a2ff1   Paul Mundt   slob: initial NUM...
546
  		b = slob_alloc(c->size, flags, c->align, node);
10cef6029   Matt Mackall   [PATCH] slob: int...
547
  	else
6193a2ff1   Paul Mundt   slob: initial NUM...
548
  		b = slob_new_page(flags, get_order(c->size), node);
10cef6029   Matt Mackall   [PATCH] slob: int...
549
550
  
  	if (c->ctor)
51cc50685   Alexey Dobriyan   SL*B: drop kmem c...
551
  		c->ctor(b);
10cef6029   Matt Mackall   [PATCH] slob: int...
552
553
554
  
  	return b;
  }
6193a2ff1   Paul Mundt   slob: initial NUM...
555
  EXPORT_SYMBOL(kmem_cache_alloc_node);
10cef6029   Matt Mackall   [PATCH] slob: int...
556

afc0cedbe   Nick Piggin   slob: implement R...
557
  static void __kmem_cache_free(void *b, int size)
10cef6029   Matt Mackall   [PATCH] slob: int...
558
  {
afc0cedbe   Nick Piggin   slob: implement R...
559
560
  	if (size < PAGE_SIZE)
  		slob_free(b, size);
10cef6029   Matt Mackall   [PATCH] slob: int...
561
  	else
afc0cedbe   Nick Piggin   slob: implement R...
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
  		free_pages((unsigned long)b, get_order(size));
  }
  
  static void kmem_rcu_free(struct rcu_head *head)
  {
  	struct slob_rcu *slob_rcu = (struct slob_rcu *)head;
  	void *b = (void *)slob_rcu - (slob_rcu->size - sizeof(struct slob_rcu));
  
  	__kmem_cache_free(b, slob_rcu->size);
  }
  
  void kmem_cache_free(struct kmem_cache *c, void *b)
  {
  	if (unlikely(c->flags & SLAB_DESTROY_BY_RCU)) {
  		struct slob_rcu *slob_rcu;
  		slob_rcu = b + (c->size - sizeof(struct slob_rcu));
  		INIT_RCU_HEAD(&slob_rcu->head);
  		slob_rcu->size = c->size;
  		call_rcu(&slob_rcu->head, kmem_rcu_free);
  	} else {
afc0cedbe   Nick Piggin   slob: implement R...
582
583
  		__kmem_cache_free(b, c->size);
  	}
10cef6029   Matt Mackall   [PATCH] slob: int...
584
585
586
587
588
589
590
591
592
593
594
595
596
597
  }
  EXPORT_SYMBOL(kmem_cache_free);
  
  unsigned int kmem_cache_size(struct kmem_cache *c)
  {
  	return c->size;
  }
  EXPORT_SYMBOL(kmem_cache_size);
  
  const char *kmem_cache_name(struct kmem_cache *c)
  {
  	return c->name;
  }
  EXPORT_SYMBOL(kmem_cache_name);
2e892f43c   Christoph Lameter   [PATCH] Cleanup s...
598
599
600
601
602
  int kmem_cache_shrink(struct kmem_cache *d)
  {
  	return 0;
  }
  EXPORT_SYMBOL(kmem_cache_shrink);
55935a34a   Christoph Lameter   [PATCH] More slab...
603
  int kmem_ptr_validate(struct kmem_cache *a, const void *b)
2e892f43c   Christoph Lameter   [PATCH] Cleanup s...
604
605
606
  {
  	return 0;
  }
84a01c2f8   Paul Mundt   slob: sparsemem s...
607
608
609
610
611
612
  static unsigned int slob_ready __read_mostly;
  
  int slab_is_available(void)
  {
  	return slob_ready;
  }
bcb4ddb46   Dimitri Gorokhovik   [PATCH] MM: SLOB ...
613
614
  void __init kmem_cache_init(void)
  {
84a01c2f8   Paul Mundt   slob: sparsemem s...
615
  	slob_ready = 1;
10cef6029   Matt Mackall   [PATCH] slob: int...
616
  }