History

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg/slab-2.6

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg/slab-2.6: slob: fix lockup in slob_free() slub: use get_track() slub: rename calculate_min_partial() to set_min_partial() slub: add min_partial sysfs tunable slub: move min_partial to struct kmem_cache SLUB: Fix default slab order for big object sizes SLUB: Do not pass 8k objects through to the page allocator SLUB: Introduce and use SLUB_MAX_SIZE and SLUB_PAGE_SHIFT constants slob: clean up the code SLUB: Use ->objsize from struct kmem_cache_cpu in slab_free()

Linus Torvalds committed 17 years ago

Commit be0ea692 parents 4496d93 15a5b0a3 files · +96 −50Browse files

Unified · Split

3 files
  1. include/linux/slub_def.h +17 −4
  2. mm/slob.c +27 −16
  3. mm/slub.c +52 −30
include/linux/slub_def.h+17−4
@@ -46,7 +46,6 @@ struct kmem_cache_cpu { · 45 unmodified lines
4646 struct kmem_cache_node {
4747 spinlock_t list_lock; /* Protect partial list and nr_partial */
4848 unsigned long nr_partial;
49− unsigned long min_partial;
5049 struct list_head partial;
5150 #ifdef CONFIG_SLUB_DEBUG
5251 atomic_long_t nr_slabs;
@@ -89,6 +88,7 @@ struct kmem_cache { · 36 unmodified lines
8988 void (*ctor)(void *);
9089 int inuse; /* Offset to metadata */
9190 int align; /* Alignment */
91+ unsigned long min_partial;
9292 const char *name; /* Name (only for display!) */
9393 struct list_head list; /* List of slab caches */
9494 #ifdef CONFIG_SLUB_DEBUG
@@ -120,11 +120,24 @@ struct kmem_cache { · 25 unmodified lines
120120
121121 #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
122122
123+/*
124+ * Maximum kmalloc object size handled by SLUB. Larger object allocations
125+ * are passed through to the page allocator. The page allocator "fastpath"
126+ * is relatively slow so we need this value sufficiently high so that
127+ * performance critical objects are allocated through the SLUB fastpath.
128+ *
129+ * This should be dropped to PAGE_SIZE / 2 once the page allocator
130+ * "fastpath" becomes competitive with the slab allocator fastpaths.
131+ */
132+#define SLUB_MAX_SIZE (2 * PAGE_SIZE)
133+
134+#define SLUB_PAGE_SHIFT (PAGE_SHIFT + 2)
135+
123136 /*
124137 * We keep the general caches in an array of slab caches that are used for
125138 * 2^x bytes of allocations.
126139 */
127−extern struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1];
140+extern struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT];
128141
129142 /*
130143 * Sorry that the following has to be that ugly but some versions of GCC
@@ -212,7 +225,7 @@ static __always_inline void *kmalloc_large(size_t size, gfp_t flags) · 81 unmodified lines
212225 static __always_inline void *kmalloc(size_t size, gfp_t flags)
213226 {
214227 if (__builtin_constant_p(size)) {
215− if (size > PAGE_SIZE)
228+ if (size > SLUB_MAX_SIZE)
216229 return kmalloc_large(size, flags);
217230
218231 if (!(flags & SLUB_DMA)) {
@@ -234,7 +247,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node); · 15 unmodified lines
234247 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
235248 {
236249 if (__builtin_constant_p(size) &&
237− size <= PAGE_SIZE && !(flags & SLUB_DMA)) {
250+ size <= SLUB_MAX_SIZE && !(flags & SLUB_DMA)) {
238251 struct kmem_cache *s = kmalloc_slab(size);
239252
240253 if (!s)
mm/slob.c+27−16
@@ -126,9 +126,9 @@ static LIST_HEAD(free_slob_medium); · 125 unmodified lines
126126 static LIST_HEAD(free_slob_large);
127127
128128 /*
129− * slob_page: True for all slob pages (false for bigblock pages)
129+ * is_slob_page: True for all slob pages (false for bigblock pages)
130130 */
131−static inline int slob_page(struct slob_page *sp)
131+static inline int is_slob_page(struct slob_page *sp)
132132 {
133133 return PageSlobPage((struct page *)sp);
134134 }
@@ -143,6 +143,11 @@ static inline void clear_slob_page(struct slob_page *sp) · 8 unmodified lines
143143 __ClearPageSlobPage((struct page *)sp);
144144 }
145145
146+static inline struct slob_page *slob_page(const void *addr)
147+{
148+ return (struct slob_page *)virt_to_page(addr);
149+}
150+
146151 /*
147152 * slob_page_free: true for pages on free_slob_pages list.
148153 */
@@ -230,7 +235,7 @@ static int slob_last(slob_t *s) · 81 unmodified lines
230235 return !((unsigned long)slob_next(s) & ~PAGE_MASK);
231236 }
232237
233−static void *slob_new_page(gfp_t gfp, int order, int node)
238+static void *slob_new_pages(gfp_t gfp, int order, int node)
234239 {
235240 void *page;
236241
@@ -247,12 +252,17 @@ static void *slob_new_page(gfp_t gfp, int order, int node) · 10 unmodified lines
247252 return page_address(page);
248253 }
249254
255+static void slob_free_pages(void *b, int order)
256+{
257+ free_pages((unsigned long)b, order);
258+}
259+
250260 /*
251261 * Allocate a slob block within a given slob_page sp.
252262 */
253263 static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
254264 {
255− slob_t *prev, *cur, *aligned = 0;
265+ slob_t *prev, *cur, *aligned = NULL;
256266 int delta = 0, units = SLOB_UNITS(size);
257267
258268 for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
@@ -349,10 +359,10 @@ static void *slob_alloc(size_t size, gfp_t gfp, int align, int node) · 90 unmodified lines
349359
350360 /* Not enough space: must allocate a new page */
351361 if (!b) {
352− b = slob_new_page(gfp & ~__GFP_ZERO, 0, node);
362+ b = slob_new_pages(gfp & ~__GFP_ZERO, 0, node);
353363 if (!b)
354− return 0;
355− sp = (struct slob_page *)virt_to_page(b);
364+ return NULL;
365+ sp = slob_page(b);
356366 set_slob_page(sp);
357367
358368 spin_lock_irqsave(&slob_lock, flags);
@@ -384,7 +394,7 @@ static void slob_free(void *block, int size) · 25 unmodified lines
384394 return;
385395 BUG_ON(!size);
386396
387− sp = (struct slob_page *)virt_to_page(block);
397+ sp = slob_page(block);
388398 units = SLOB_UNITS(size);
389399
390400 spin_lock_irqsave(&slob_lock, flags);
@@ -393,10 +403,11 @@ static void slob_free(void *block, int size) · 2 unmodified lines
393403 /* Go directly to page allocator. Do not pass slob allocator */
394404 if (slob_page_free(sp))
395405 clear_slob_page_free(sp);
406+ spin_unlock_irqrestore(&slob_lock, flags);
396407 clear_slob_page(sp);
397408 free_slob_page(sp);
398409 free_page((unsigned long)b);
399− goto out;
410+ return;
400411 }
401412
402413 if (!slob_page_free(sp)) {
@@ -476,7 +487,7 @@ void *__kmalloc_node(size_t size, gfp_t gfp, int node) · 73 unmodified lines
476487 } else {
477488 void *ret;
478489
479− ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node);
490+ ret = slob_new_pages(gfp | __GFP_COMP, get_order(size), node);
480491 if (ret) {
481492 struct page *page;
482493 page = virt_to_page(ret);
@@ -494,8 +505,8 @@ void kfree(const void *block) · 11 unmodified lines
494505 if (unlikely(ZERO_OR_NULL_PTR(block)))
495506 return;
496507
497− sp = (struct slob_page *)virt_to_page(block);
498− if (slob_page(sp)) {
508+ sp = slob_page(block);
509+ if (is_slob_page(sp)) {
499510 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
500511 unsigned int *m = (unsigned int *)(block - align);
501512 slob_free(m, *m + align);
@@ -513,8 +524,8 @@ size_t ksize(const void *block) · 11 unmodified lines
513524 if (unlikely(block == ZERO_SIZE_PTR))
514525 return 0;
515526
516− sp = (struct slob_page *)virt_to_page(block);
517− if (slob_page(sp)) {
527+ sp = slob_page(block);
528+ if (is_slob_page(sp)) {
518529 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
519530 unsigned int *m = (unsigned int *)(block - align);
520531 return SLOB_UNITS(*m) * SLOB_UNIT;
@@ -573,7 +584,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node) · 52 unmodified lines
573584 if (c->size < PAGE_SIZE)
574585 b = slob_alloc(c->size, flags, c->align, node);
575586 else
576− b = slob_new_page(flags, get_order(c->size), node);
587+ b = slob_new_pages(flags, get_order(c->size), node);
577588
578589 if (c->ctor)
579590 c->ctor(b);
@@ -587,7 +598,7 @@ static void __kmem_cache_free(void *b, int size) · 7 unmodified lines
587598 if (size < PAGE_SIZE)
588599 slob_free(b, size);
589600 else
590− free_pages((unsigned long)b, get_order(size));
601+ slob_free_pages(b, get_order(size));
591602 }
592603
593604 static void kmem_rcu_free(struct rcu_head *head)
mm/slub.c+52−30
@@ -374,14 +374,8 @@ static struct track *get_track(struct kmem_cache *s, void *object, · 373 unmodified lines
374374 static void set_track(struct kmem_cache *s, void *object,
375375 enum track_item alloc, unsigned long addr)
376376 {
377− struct track *p;
378−
379− if (s->offset)
380− p = object + s->offset + sizeof(void *);
381− else
382− p = object + s->inuse;
377+ struct track *p = get_track(s, object, alloc);
383378
384− p += alloc;
385379 if (addr) {
386380 p->addr = addr;
387381 p->cpu = smp_processor_id();
@@ -1335,7 +1329,7 @@ static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags) · 947 unmodified lines
13351329 n = get_node(s, zone_to_nid(zone));
13361330
13371331 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
1338− n->nr_partial > n->min_partial) {
1332+ n->nr_partial > s->min_partial) {
13391333 page = get_partial_node(n);
13401334 if (page)
13411335 return page;
@@ -1387,7 +1381,7 @@ static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail) · 45 unmodified lines
13871381 slab_unlock(page);
13881382 } else {
13891383 stat(c, DEACTIVATE_EMPTY);
1390− if (n->nr_partial < n->min_partial) {
1384+ if (n->nr_partial < s->min_partial) {
13911385 /*
13921386 * Adding an empty slab to the partial slabs in order
13931387 * to avoid page allocator overhead. This slab needs
@@ -1724,7 +1718,7 @@ static __always_inline void slab_free(struct kmem_cache *s, · 330 unmodified lines
17241718 c = get_cpu_slab(s, smp_processor_id());
17251719 debug_check_no_locks_freed(object, c->objsize);
17261720 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1727− debug_check_no_obj_freed(object, s->objsize);
1721+ debug_check_no_obj_freed(object, c->objsize);
17281722 if (likely(page == c->page && c->node >= 0)) {
17291723 object[c->offset] = c->freelist;
17301724 c->freelist = object;
@@ -1844,6 +1838,7 @@ static inline int calculate_order(int size) · 113 unmodified lines
18441838 int order;
18451839 int min_objects;
18461840 int fraction;
1841+ int max_objects;
18471842
18481843 /*
18491844 * Attempt to find best configuration for a slab. This
@@ -1856,6 +1851,9 @@ static inline int calculate_order(int size) · 6 unmodified lines
18561851 min_objects = slub_min_objects;
18571852 if (!min_objects)
18581853 min_objects = 4 * (fls(nr_cpu_ids) + 1);
1854+ max_objects = (PAGE_SIZE << slub_max_order)/size;
1855+ min_objects = min(min_objects, max_objects);
1856+
18591857 while (min_objects > 1) {
18601858 fraction = 16;
18611859 while (fraction >= 4) {
@@ -1865,7 +1863,7 @@ static inline int calculate_order(int size) · 3 unmodified lines
18651863 return order;
18661864 fraction /= 2;
18671865 }
1868− min_objects /= 2;
1866+ min_objects --;
18691867 }
18701868
18711869 /*
@@ -1928,17 +1926,6 @@ static void · 56 unmodified lines
19281926 init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
19291927 {
19301928 n->nr_partial = 0;
1931−
1932− /*
1933− * The larger the object size is, the more pages we want on the partial
1934− * list to avoid pounding the page allocator excessively.
1935− */
1936− n->min_partial = ilog2(s->size);
1937− if (n->min_partial < MIN_PARTIAL)
1938− n->min_partial = MIN_PARTIAL;
1939− else if (n->min_partial > MAX_PARTIAL)
1940− n->min_partial = MAX_PARTIAL;
1941−
19421929 spin_lock_init(&n->list_lock);
19431930 INIT_LIST_HEAD(&n->partial);
19441931 #ifdef CONFIG_SLUB_DEBUG
@@ -2181,6 +2168,15 @@ static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags) · 236 unmodified lines
21812168 }
21822169 #endif
21832170
2171+static void set_min_partial(struct kmem_cache *s, unsigned long min)
2172+{
2173+ if (min < MIN_PARTIAL)
2174+ min = MIN_PARTIAL;
2175+ else if (min > MAX_PARTIAL)
2176+ min = MAX_PARTIAL;
2177+ s->min_partial = min;
2178+}
2179+
21842180 /*
21852181 * calculate_sizes() determines the order and the distribution of data within
21862182 * a slab object.
@@ -2319,6 +2315,11 @@ static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags, · 132 unmodified lines
23192315 if (!calculate_sizes(s, -1))
23202316 goto error;
23212317
2318+ /*
2319+ * The larger the object size is, the more pages we want on the partial
2320+ * list to avoid pounding the page allocator excessively.
2321+ */
2322+ set_min_partial(s, ilog2(s->size));
23222323 s->refcount = 1;
23232324 #ifdef CONFIG_NUMA
23242325 s->remote_node_defrag_ratio = 1000;
@@ -2475,7 +2476,7 @@ EXPORT_SYMBOL(kmem_cache_destroy); · 150 unmodified lines
24752476 * Kmalloc subsystem
24762477 *******************************************************************/
24772478
2478−struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned;
2479+struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT] __cacheline_aligned;
24792480 EXPORT_SYMBOL(kmalloc_caches);
24802481
24812482 static int __init setup_slub_min_order(char *str)
@@ -2537,7 +2538,7 @@ panic: · 55 unmodified lines
25372538 }
25382539
25392540 #ifdef CONFIG_ZONE_DMA
2540−static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1];
2541+static struct kmem_cache *kmalloc_caches_dma[SLUB_PAGE_SHIFT];
25412542
25422543 static void sysfs_add_func(struct work_struct *w)
25432544 {
@@ -2658,7 +2659,7 @@ void *__kmalloc(size_t size, gfp_t flags) · 114 unmodified lines
26582659 {
26592660 struct kmem_cache *s;
26602661
2661− if (unlikely(size > PAGE_SIZE))
2662+ if (unlikely(size > SLUB_MAX_SIZE))
26622663 return kmalloc_large(size, flags);
26632664
26642665 s = get_slab(size, flags);
@@ -2686,7 +2687,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node) · 21 unmodified lines
26862687 {
26872688 struct kmem_cache *s;
26882689
2689− if (unlikely(size > PAGE_SIZE))
2690+ if (unlikely(size > SLUB_MAX_SIZE))
26902691 return kmalloc_large_node(size, flags, node);
26912692
26922693 s = get_slab(size, flags);
@@ -2986,7 +2987,7 @@ void __init kmem_cache_init(void) · 293 unmodified lines
29862987 caches++;
29872988 }
29882989
2989− for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) {
2990+ for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
29902991 create_kmalloc_cache(&kmalloc_caches[i],
29912992 "kmalloc", 1 << i, GFP_KERNEL);
29922993 caches++;
@@ -3023,7 +3024,7 @@ void __init kmem_cache_init(void) · 30 unmodified lines
30233024 slab_state = UP;
30243025
30253026 /* Provide the correct kmalloc names now that the caches are up */
3026− for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++)
3027+ for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++)
30273028 kmalloc_caches[i]. name =
30283029 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
30293030
@@ -3223,7 +3224,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller) · 193 unmodified lines
32233224 {
32243225 struct kmem_cache *s;
32253226
3226− if (unlikely(size > PAGE_SIZE))
3227+ if (unlikely(size > SLUB_MAX_SIZE))
32273228 return kmalloc_large(size, gfpflags);
32283229
32293230 s = get_slab(size, gfpflags);
@@ -3239,7 +3240,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags, · 9 unmodified lines
32393240 {
32403241 struct kmem_cache *s;
32413242
3242− if (unlikely(size > PAGE_SIZE))
3243+ if (unlikely(size > SLUB_MAX_SIZE))
32433244 return kmalloc_large_node(size, gfpflags, node);
32443245
32453246 s = get_slab(size, gfpflags);
@@ -3836,6 +3837,26 @@ static ssize_t order_show(struct kmem_cache *s, char *buf) · 590 unmodified lines
38363837 }
38373838 SLAB_ATTR(order);
38383839
3840+static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
3841+{
3842+ return sprintf(buf, "%lu\n", s->min_partial);
3843+}
3844+
3845+static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
3846+ size_t length)
3847+{
3848+ unsigned long min;
3849+ int err;
3850+
3851+ err = strict_strtoul(buf, 10, &min);
3852+ if (err)
3853+ return err;
3854+
3855+ set_min_partial(s, min);
3856+ return length;
3857+}
3858+SLAB_ATTR(min_partial);
3859+
38393860 static ssize_t ctor_show(struct kmem_cache *s, char *buf)
38403861 {
38413862 if (s->ctor) {
@@ -4151,6 +4172,7 @@ static struct attribute *slab_attrs[] = { · 309 unmodified lines
41514172 &object_size_attr.attr,
41524173 &objs_per_slab_attr.attr,
41534174 &order_attr.attr,
4175+ &min_partial_attr.attr,
41544176 &objects_attr.attr,
41554177 &objects_partial_attr.attr,
41564178 &total_objects_attr.attr,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t