diff options
author | Christoph Lameter <cl@linux.com> | 2013-01-10 14:14:19 -0500 |
---|---|---|
committer | Pekka Enberg <penberg@kernel.org> | 2013-02-01 05:32:08 -0500 |
commit | 2c59dd6544212faa5ce761920d2251f4152f408d (patch) | |
tree | c2547eb50205b72368e0b4758fc7c9a0111238a5 /mm | |
parent | 9e5e8deca74603357626471a9b44f05dea9e32b1 (diff) |
slab: Common Kmalloc cache determination
Extract the optimized lookup functions from slub and put them into
slab_common.c. Then make slab use these functions as well.
Joonsoo notes that this fixes some issues with constant folding which
also reduces the code size for slub.
https://lkml.org/lkml/2012/10/20/82
Signed-off-by: Christoph Lameter <cl@linux.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
Diffstat (limited to 'mm')
-rw-r--r-- | mm/slab.c | 40 | ||||
-rw-r--r-- | mm/slab.h | 3 | ||||
-rw-r--r-- | mm/slab_common.c | 105 | ||||
-rw-r--r-- | mm/slub.c | 108 |
4 files changed, 114 insertions, 142 deletions
@@ -656,40 +656,6 @@ static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep) | |||
656 | return cachep->array[smp_processor_id()]; | 656 | return cachep->array[smp_processor_id()]; |
657 | } | 657 | } |
658 | 658 | ||
659 | static inline struct kmem_cache *__find_general_cachep(size_t size, | ||
660 | gfp_t gfpflags) | ||
661 | { | ||
662 | int i; | ||
663 | |||
664 | #if DEBUG | ||
665 | /* This happens if someone tries to call | ||
666 | * kmem_cache_create(), or __kmalloc(), before | ||
667 | * the generic caches are initialized. | ||
668 | */ | ||
669 | BUG_ON(kmalloc_caches[INDEX_AC] == NULL); | ||
670 | #endif | ||
671 | if (!size) | ||
672 | return ZERO_SIZE_PTR; | ||
673 | |||
674 | i = kmalloc_index(size); | ||
675 | |||
676 | /* | ||
677 | * Really subtle: The last entry with cs->cs_size==ULONG_MAX | ||
678 | * has cs_{dma,}cachep==NULL. Thus no special case | ||
679 | * for large kmalloc calls required. | ||
680 | */ | ||
681 | #ifdef CONFIG_ZONE_DMA | ||
682 | if (unlikely(gfpflags & GFP_DMA)) | ||
683 | return kmalloc_dma_caches[i]; | ||
684 | #endif | ||
685 | return kmalloc_caches[i]; | ||
686 | } | ||
687 | |||
688 | static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags) | ||
689 | { | ||
690 | return __find_general_cachep(size, gfpflags); | ||
691 | } | ||
692 | |||
693 | static size_t slab_mgmt_size(size_t nr_objs, size_t align) | 659 | static size_t slab_mgmt_size(size_t nr_objs, size_t align) |
694 | { | 660 | { |
695 | return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align); | 661 | return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align); |
@@ -2426,7 +2392,7 @@ __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags) | |||
2426 | cachep->reciprocal_buffer_size = reciprocal_value(size); | 2392 | cachep->reciprocal_buffer_size = reciprocal_value(size); |
2427 | 2393 | ||
2428 | if (flags & CFLGS_OFF_SLAB) { | 2394 | if (flags & CFLGS_OFF_SLAB) { |
2429 | cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u); | 2395 | cachep->slabp_cache = kmalloc_slab(slab_size, 0u); |
2430 | /* | 2396 | /* |
2431 | * This is a possibility for one of the malloc_sizes caches. | 2397 | * This is a possibility for one of the malloc_sizes caches. |
2432 | * But since we go off slab only for object size greater than | 2398 | * But since we go off slab only for object size greater than |
@@ -3729,7 +3695,7 @@ __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller) | |||
3729 | { | 3695 | { |
3730 | struct kmem_cache *cachep; | 3696 | struct kmem_cache *cachep; |
3731 | 3697 | ||
3732 | cachep = kmem_find_general_cachep(size, flags); | 3698 | cachep = kmalloc_slab(size, flags); |
3733 | if (unlikely(ZERO_OR_NULL_PTR(cachep))) | 3699 | if (unlikely(ZERO_OR_NULL_PTR(cachep))) |
3734 | return cachep; | 3700 | return cachep; |
3735 | return kmem_cache_alloc_node_trace(cachep, flags, node, size); | 3701 | return kmem_cache_alloc_node_trace(cachep, flags, node, size); |
@@ -3774,7 +3740,7 @@ static __always_inline void *__do_kmalloc(size_t size, gfp_t flags, | |||
3774 | * Then kmalloc uses the uninlined functions instead of the inline | 3740 | * Then kmalloc uses the uninlined functions instead of the inline |
3775 | * functions. | 3741 | * functions. |
3776 | */ | 3742 | */ |
3777 | cachep = __find_general_cachep(size, flags); | 3743 | cachep = kmalloc_slab(size, flags); |
3778 | if (unlikely(ZERO_OR_NULL_PTR(cachep))) | 3744 | if (unlikely(ZERO_OR_NULL_PTR(cachep))) |
3779 | return cachep; | 3745 | return cachep; |
3780 | ret = slab_alloc(cachep, flags, caller); | 3746 | ret = slab_alloc(cachep, flags, caller); |
@@ -38,6 +38,9 @@ unsigned long calculate_alignment(unsigned long flags, | |||
38 | #ifndef CONFIG_SLOB | 38 | #ifndef CONFIG_SLOB |
39 | /* Kmalloc array related functions */ | 39 | /* Kmalloc array related functions */ |
40 | void create_kmalloc_caches(unsigned long); | 40 | void create_kmalloc_caches(unsigned long); |
41 | |||
42 | /* Find the kmalloc slab corresponding for a certain size */ | ||
43 | struct kmem_cache *kmalloc_slab(size_t, gfp_t); | ||
41 | #endif | 44 | #endif |
42 | 45 | ||
43 | 46 | ||
diff --git a/mm/slab_common.c b/mm/slab_common.c index 2b0ebb6d071d..6d73f0b7f21c 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c | |||
@@ -328,6 +328,68 @@ EXPORT_SYMBOL(kmalloc_dma_caches); | |||
328 | #endif | 328 | #endif |
329 | 329 | ||
330 | /* | 330 | /* |
331 | * Conversion table for small slabs sizes / 8 to the index in the | ||
332 | * kmalloc array. This is necessary for slabs < 192 since we have non power | ||
333 | * of two cache sizes there. The size of larger slabs can be determined using | ||
334 | * fls. | ||
335 | */ | ||
336 | static s8 size_index[24] = { | ||
337 | 3, /* 8 */ | ||
338 | 4, /* 16 */ | ||
339 | 5, /* 24 */ | ||
340 | 5, /* 32 */ | ||
341 | 6, /* 40 */ | ||
342 | 6, /* 48 */ | ||
343 | 6, /* 56 */ | ||
344 | 6, /* 64 */ | ||
345 | 1, /* 72 */ | ||
346 | 1, /* 80 */ | ||
347 | 1, /* 88 */ | ||
348 | 1, /* 96 */ | ||
349 | 7, /* 104 */ | ||
350 | 7, /* 112 */ | ||
351 | 7, /* 120 */ | ||
352 | 7, /* 128 */ | ||
353 | 2, /* 136 */ | ||
354 | 2, /* 144 */ | ||
355 | 2, /* 152 */ | ||
356 | 2, /* 160 */ | ||
357 | 2, /* 168 */ | ||
358 | 2, /* 176 */ | ||
359 | 2, /* 184 */ | ||
360 | 2 /* 192 */ | ||
361 | }; | ||
362 | |||
363 | static inline int size_index_elem(size_t bytes) | ||
364 | { | ||
365 | return (bytes - 1) / 8; | ||
366 | } | ||
367 | |||
368 | /* | ||
369 | * Find the kmem_cache structure that serves a given size of | ||
370 | * allocation | ||
371 | */ | ||
372 | struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags) | ||
373 | { | ||
374 | int index; | ||
375 | |||
376 | if (size <= 192) { | ||
377 | if (!size) | ||
378 | return ZERO_SIZE_PTR; | ||
379 | |||
380 | index = size_index[size_index_elem(size)]; | ||
381 | } else | ||
382 | index = fls(size - 1); | ||
383 | |||
384 | #ifdef CONFIG_ZONE_DMA | ||
385 | if (unlikely((flags & SLAB_CACHE_DMA))) | ||
386 | return kmalloc_dma_caches[index]; | ||
387 | |||
388 | #endif | ||
389 | return kmalloc_caches[index]; | ||
390 | } | ||
391 | |||
392 | /* | ||
331 | * Create the kmalloc array. Some of the regular kmalloc arrays | 393 | * Create the kmalloc array. Some of the regular kmalloc arrays |
332 | * may already have been created because they were needed to | 394 | * may already have been created because they were needed to |
333 | * enable allocations for slab creation. | 395 | * enable allocations for slab creation. |
@@ -336,6 +398,47 @@ void __init create_kmalloc_caches(unsigned long flags) | |||
336 | { | 398 | { |
337 | int i; | 399 | int i; |
338 | 400 | ||
401 | /* | ||
402 | * Patch up the size_index table if we have strange large alignment | ||
403 | * requirements for the kmalloc array. This is only the case for | ||
404 | * MIPS it seems. The standard arches will not generate any code here. | ||
405 | * | ||
406 | * Largest permitted alignment is 256 bytes due to the way we | ||
407 | * handle the index determination for the smaller caches. | ||
408 | * | ||
409 | * Make sure that nothing crazy happens if someone starts tinkering | ||
410 | * around with ARCH_KMALLOC_MINALIGN | ||
411 | */ | ||
412 | BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 || | ||
413 | (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1))); | ||
414 | |||
415 | for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) { | ||
416 | int elem = size_index_elem(i); | ||
417 | |||
418 | if (elem >= ARRAY_SIZE(size_index)) | ||
419 | break; | ||
420 | size_index[elem] = KMALLOC_SHIFT_LOW; | ||
421 | } | ||
422 | |||
423 | if (KMALLOC_MIN_SIZE >= 64) { | ||
424 | /* | ||
425 | * The 96 byte size cache is not used if the alignment | ||
426 | * is 64 byte. | ||
427 | */ | ||
428 | for (i = 64 + 8; i <= 96; i += 8) | ||
429 | size_index[size_index_elem(i)] = 7; | ||
430 | |||
431 | } | ||
432 | |||
433 | if (KMALLOC_MIN_SIZE >= 128) { | ||
434 | /* | ||
435 | * The 192 byte sized cache is not used if the alignment | ||
436 | * is 128 byte. Redirect kmalloc to use the 256 byte cache | ||
437 | * instead. | ||
438 | */ | ||
439 | for (i = 128 + 8; i <= 192; i += 8) | ||
440 | size_index[size_index_elem(i)] = 8; | ||
441 | } | ||
339 | /* Caches that are not of the two-to-the-power-of size */ | 442 | /* Caches that are not of the two-to-the-power-of size */ |
340 | if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1]) | 443 | if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1]) |
341 | kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags); | 444 | kmalloc_caches[1] = create_kmalloc_cache(NULL, 96, flags); |
@@ -379,8 +482,6 @@ void __init create_kmalloc_caches(unsigned long flags) | |||
379 | } | 482 | } |
380 | #endif | 483 | #endif |
381 | } | 484 | } |
382 | |||
383 | |||
384 | #endif /* !CONFIG_SLOB */ | 485 | #endif /* !CONFIG_SLOB */ |
385 | 486 | ||
386 | 487 | ||
@@ -2982,7 +2982,7 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) | |||
2982 | s->allocflags |= __GFP_COMP; | 2982 | s->allocflags |= __GFP_COMP; |
2983 | 2983 | ||
2984 | if (s->flags & SLAB_CACHE_DMA) | 2984 | if (s->flags & SLAB_CACHE_DMA) |
2985 | s->allocflags |= SLUB_DMA; | 2985 | s->allocflags |= GFP_DMA; |
2986 | 2986 | ||
2987 | if (s->flags & SLAB_RECLAIM_ACCOUNT) | 2987 | if (s->flags & SLAB_RECLAIM_ACCOUNT) |
2988 | s->allocflags |= __GFP_RECLAIMABLE; | 2988 | s->allocflags |= __GFP_RECLAIMABLE; |
@@ -3210,64 +3210,6 @@ static int __init setup_slub_nomerge(char *str) | |||
3210 | 3210 | ||
3211 | __setup("slub_nomerge", setup_slub_nomerge); | 3211 | __setup("slub_nomerge", setup_slub_nomerge); |
3212 | 3212 | ||
3213 | /* | ||
3214 | * Conversion table for small slabs sizes / 8 to the index in the | ||
3215 | * kmalloc array. This is necessary for slabs < 192 since we have non power | ||
3216 | * of two cache sizes there. The size of larger slabs can be determined using | ||
3217 | * fls. | ||
3218 | */ | ||
3219 | static s8 size_index[24] = { | ||
3220 | 3, /* 8 */ | ||
3221 | 4, /* 16 */ | ||
3222 | 5, /* 24 */ | ||
3223 | 5, /* 32 */ | ||
3224 | 6, /* 40 */ | ||
3225 | 6, /* 48 */ | ||
3226 | 6, /* 56 */ | ||
3227 | 6, /* 64 */ | ||
3228 | 1, /* 72 */ | ||
3229 | 1, /* 80 */ | ||
3230 | 1, /* 88 */ | ||
3231 | 1, /* 96 */ | ||
3232 | 7, /* 104 */ | ||
3233 | 7, /* 112 */ | ||
3234 | 7, /* 120 */ | ||
3235 | 7, /* 128 */ | ||
3236 | 2, /* 136 */ | ||
3237 | 2, /* 144 */ | ||
3238 | 2, /* 152 */ | ||
3239 | 2, /* 160 */ | ||
3240 | 2, /* 168 */ | ||
3241 | 2, /* 176 */ | ||
3242 | 2, /* 184 */ | ||
3243 | 2 /* 192 */ | ||
3244 | }; | ||
3245 | |||
3246 | static inline int size_index_elem(size_t bytes) | ||
3247 | { | ||
3248 | return (bytes - 1) / 8; | ||
3249 | } | ||
3250 | |||
3251 | static struct kmem_cache *get_slab(size_t size, gfp_t flags) | ||
3252 | { | ||
3253 | int index; | ||
3254 | |||
3255 | if (size <= 192) { | ||
3256 | if (!size) | ||
3257 | return ZERO_SIZE_PTR; | ||
3258 | |||
3259 | index = size_index[size_index_elem(size)]; | ||
3260 | } else | ||
3261 | index = fls(size - 1); | ||
3262 | |||
3263 | #ifdef CONFIG_ZONE_DMA | ||
3264 | if (unlikely((flags & SLUB_DMA))) | ||
3265 | return kmalloc_dma_caches[index]; | ||
3266 | |||
3267 | #endif | ||
3268 | return kmalloc_caches[index]; | ||
3269 | } | ||
3270 | |||
3271 | void *__kmalloc(size_t size, gfp_t flags) | 3213 | void *__kmalloc(size_t size, gfp_t flags) |
3272 | { | 3214 | { |
3273 | struct kmem_cache *s; | 3215 | struct kmem_cache *s; |
@@ -3276,7 +3218,7 @@ void *__kmalloc(size_t size, gfp_t flags) | |||
3276 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) | 3218 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) |
3277 | return kmalloc_large(size, flags); | 3219 | return kmalloc_large(size, flags); |
3278 | 3220 | ||
3279 | s = get_slab(size, flags); | 3221 | s = kmalloc_slab(size, flags); |
3280 | 3222 | ||
3281 | if (unlikely(ZERO_OR_NULL_PTR(s))) | 3223 | if (unlikely(ZERO_OR_NULL_PTR(s))) |
3282 | return s; | 3224 | return s; |
@@ -3319,7 +3261,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node) | |||
3319 | return ret; | 3261 | return ret; |
3320 | } | 3262 | } |
3321 | 3263 | ||
3322 | s = get_slab(size, flags); | 3264 | s = kmalloc_slab(size, flags); |
3323 | 3265 | ||
3324 | if (unlikely(ZERO_OR_NULL_PTR(s))) | 3266 | if (unlikely(ZERO_OR_NULL_PTR(s))) |
3325 | return s; | 3267 | return s; |
@@ -3632,7 +3574,6 @@ void __init kmem_cache_init(void) | |||
3632 | { | 3574 | { |
3633 | static __initdata struct kmem_cache boot_kmem_cache, | 3575 | static __initdata struct kmem_cache boot_kmem_cache, |
3634 | boot_kmem_cache_node; | 3576 | boot_kmem_cache_node; |
3635 | int i; | ||
3636 | 3577 | ||
3637 | if (debug_guardpage_minorder()) | 3578 | if (debug_guardpage_minorder()) |
3638 | slub_max_order = 0; | 3579 | slub_max_order = 0; |
@@ -3663,45 +3604,6 @@ void __init kmem_cache_init(void) | |||
3663 | kmem_cache_node = bootstrap(&boot_kmem_cache_node); | 3604 | kmem_cache_node = bootstrap(&boot_kmem_cache_node); |
3664 | 3605 | ||
3665 | /* Now we can use the kmem_cache to allocate kmalloc slabs */ | 3606 | /* Now we can use the kmem_cache to allocate kmalloc slabs */ |
3666 | |||
3667 | /* | ||
3668 | * Patch up the size_index table if we have strange large alignment | ||
3669 | * requirements for the kmalloc array. This is only the case for | ||
3670 | * MIPS it seems. The standard arches will not generate any code here. | ||
3671 | * | ||
3672 | * Largest permitted alignment is 256 bytes due to the way we | ||
3673 | * handle the index determination for the smaller caches. | ||
3674 | * | ||
3675 | * Make sure that nothing crazy happens if someone starts tinkering | ||
3676 | * around with ARCH_KMALLOC_MINALIGN | ||
3677 | */ | ||
3678 | BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 || | ||
3679 | (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1))); | ||
3680 | |||
3681 | for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) { | ||
3682 | int elem = size_index_elem(i); | ||
3683 | if (elem >= ARRAY_SIZE(size_index)) | ||
3684 | break; | ||
3685 | size_index[elem] = KMALLOC_SHIFT_LOW; | ||
3686 | } | ||
3687 | |||
3688 | if (KMALLOC_MIN_SIZE == 64) { | ||
3689 | /* | ||
3690 | * The 96 byte size cache is not used if the alignment | ||
3691 | * is 64 byte. | ||
3692 | */ | ||
3693 | for (i = 64 + 8; i <= 96; i += 8) | ||
3694 | size_index[size_index_elem(i)] = 7; | ||
3695 | } else if (KMALLOC_MIN_SIZE == 128) { | ||
3696 | /* | ||
3697 | * The 192 byte sized cache is not used if the alignment | ||
3698 | * is 128 byte. Redirect kmalloc to use the 256 byte cache | ||
3699 | * instead. | ||
3700 | */ | ||
3701 | for (i = 128 + 8; i <= 192; i += 8) | ||
3702 | size_index[size_index_elem(i)] = 8; | ||
3703 | } | ||
3704 | |||
3705 | create_kmalloc_caches(0); | 3607 | create_kmalloc_caches(0); |
3706 | 3608 | ||
3707 | #ifdef CONFIG_SMP | 3609 | #ifdef CONFIG_SMP |
@@ -3877,7 +3779,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller) | |||
3877 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) | 3779 | if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) |
3878 | return kmalloc_large(size, gfpflags); | 3780 | return kmalloc_large(size, gfpflags); |
3879 | 3781 | ||
3880 | s = get_slab(size, gfpflags); | 3782 | s = kmalloc_slab(size, gfpflags); |
3881 | 3783 | ||
3882 | if (unlikely(ZERO_OR_NULL_PTR(s))) | 3784 | if (unlikely(ZERO_OR_NULL_PTR(s))) |
3883 | return s; | 3785 | return s; |
@@ -3907,7 +3809,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags, | |||
3907 | return ret; | 3809 | return ret; |
3908 | } | 3810 | } |
3909 | 3811 | ||
3910 | s = get_slab(size, gfpflags); | 3812 | s = kmalloc_slab(size, gfpflags); |
3911 | 3813 | ||
3912 | if (unlikely(ZERO_OR_NULL_PTR(s))) | 3814 | if (unlikely(ZERO_OR_NULL_PTR(s))) |
3913 | return s; | 3815 | return s; |