Merge branch 'slab-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/christoph/vm

* 'slab-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/christoph/vm:
slub: Support 4k kmallocs again to compensate for page allocator slowness
slub: Fallback to kmalloc_large for failing higher order allocs
slub: Determine gfpflags once and not every time a slab is allocated
make slub.c:slab_address() static
slub: kmalloc page allocator pass-through cleanup
slab: avoid double initialization & do initialization in 1 place

+75 -37
+10 -5
include/linux/slub_def.h
··· 71 72 /* Allocation and freeing of slabs */ 73 int objects; /* Number of objects in slab */ 74 int refcount; /* Refcount for slab cache destroy */ 75 void (*ctor)(struct kmem_cache *, void *); 76 int inuse; /* Offset to metadata */ ··· 111 * We keep the general caches in an array of slab caches that are used for 112 * 2^x bytes of allocations. 113 */ 114 - extern struct kmem_cache kmalloc_caches[PAGE_SHIFT]; 115 116 /* 117 * Sorry that the following has to be that ugly but some versions of GCC ··· 189 void *kmem_cache_alloc(struct kmem_cache *, gfp_t); 190 void *__kmalloc(size_t size, gfp_t flags); 191 192 static __always_inline void *kmalloc(size_t size, gfp_t flags) 193 { 194 if (__builtin_constant_p(size)) { 195 - if (size > PAGE_SIZE / 2) 196 - return (void *)__get_free_pages(flags | __GFP_COMP, 197 - get_order(size)); 198 199 if (!(flags & SLUB_DMA)) { 200 struct kmem_cache *s = kmalloc_slab(size); ··· 219 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) 220 { 221 if (__builtin_constant_p(size) && 222 - size <= PAGE_SIZE / 2 && !(flags & SLUB_DMA)) { 223 struct kmem_cache *s = kmalloc_slab(size); 224 225 if (!s)
··· 71 72 /* Allocation and freeing of slabs */ 73 int objects; /* Number of objects in slab */ 74 + gfp_t allocflags; /* gfp flags to use on each alloc */ 75 int refcount; /* Refcount for slab cache destroy */ 76 void (*ctor)(struct kmem_cache *, void *); 77 int inuse; /* Offset to metadata */ ··· 110 * We keep the general caches in an array of slab caches that are used for 111 * 2^x bytes of allocations. 112 */ 113 + extern struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1]; 114 115 /* 116 * Sorry that the following has to be that ugly but some versions of GCC ··· 188 void *kmem_cache_alloc(struct kmem_cache *, gfp_t); 189 void *__kmalloc(size_t size, gfp_t flags); 190 191 + static __always_inline void *kmalloc_large(size_t size, gfp_t flags) 192 + { 193 + return (void *)__get_free_pages(flags | __GFP_COMP, get_order(size)); 194 + } 195 + 196 static __always_inline void *kmalloc(size_t size, gfp_t flags) 197 { 198 if (__builtin_constant_p(size)) { 199 + if (size > PAGE_SIZE) 200 + return kmalloc_large(size, flags); 201 202 if (!(flags & SLUB_DMA)) { 203 struct kmem_cache *s = kmalloc_slab(size); ··· 214 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node) 215 { 216 if (__builtin_constant_p(size) && 217 + size <= PAGE_SIZE && !(flags & SLUB_DMA)) { 218 struct kmem_cache *s = kmalloc_slab(size); 219 220 if (!s)
+1 -2
mm/slab.c
··· 2630 slabp->colouroff = colour_off; 2631 slabp->s_mem = objp + colour_off; 2632 slabp->nodeid = nodeid; 2633 return slabp; 2634 } 2635 ··· 2684 slab_bufctl(slabp)[i] = i + 1; 2685 } 2686 slab_bufctl(slabp)[i - 1] = BUFCTL_END; 2687 - slabp->free = 0; 2688 } 2689 2690 static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags) ··· 2816 if (!slabp) 2817 goto opps1; 2818 2819 - slabp->nodeid = nodeid; 2820 slab_map_pages(cachep, slabp, objp); 2821 2822 cache_init_objs(cachep, slabp);
··· 2630 slabp->colouroff = colour_off; 2631 slabp->s_mem = objp + colour_off; 2632 slabp->nodeid = nodeid; 2633 + slabp->free = 0; 2634 return slabp; 2635 } 2636 ··· 2683 slab_bufctl(slabp)[i] = i + 1; 2684 } 2685 slab_bufctl(slabp)[i - 1] = BUFCTL_END; 2686 } 2687 2688 static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags) ··· 2816 if (!slabp) 2817 goto opps1; 2818 2819 slab_map_pages(cachep, slabp, objp); 2820 2821 cache_init_objs(cachep, slabp);
+64 -30
mm/slub.c
··· 211 /* Internal SLUB flags */ 212 #define __OBJECT_POISON 0x80000000 /* Poison object */ 213 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */ 214 215 /* Not all arches define cache_line_size */ 216 #ifndef cache_line_size ··· 310 return (unsigned long)addr & PAGE_MAPPING_ANON; 311 } 312 313 - void *slab_address(struct page *page) 314 { 315 return page->end - PAGE_MAPPING_ANON; 316 } ··· 1080 struct page *page; 1081 int pages = 1 << s->order; 1082 1083 - if (s->order) 1084 - flags |= __GFP_COMP; 1085 - 1086 - if (s->flags & SLAB_CACHE_DMA) 1087 - flags |= SLUB_DMA; 1088 - 1089 - if (s->flags & SLAB_RECLAIM_ACCOUNT) 1090 - flags |= __GFP_RECLAIMABLE; 1091 1092 if (node == -1) 1093 page = alloc_pages(flags, s->order); ··· 1541 unlock_out: 1542 slab_unlock(c->page); 1543 stat(c, ALLOC_SLOWPATH); 1544 - out: 1545 #ifdef SLUB_FASTPATH 1546 local_irq_restore(flags); 1547 #endif ··· 1575 c->page = new; 1576 goto load_freelist; 1577 } 1578 - object = NULL; 1579 - goto out; 1580 debug: 1581 object = c->page->freelist; 1582 if (!alloc_debug_processing(s, c->page, object, addr)) ··· 2339 size = ALIGN(size, align); 2340 s->size = size; 2341 2342 - s->order = calculate_order(size); 2343 if (s->order < 0) 2344 return 0; 2345 2346 /* 2347 * Determine the number of objects per slab ··· 2517 * Kmalloc subsystem 2518 *******************************************************************/ 2519 2520 - struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned; 2521 EXPORT_SYMBOL(kmalloc_caches); 2522 2523 #ifdef CONFIG_ZONE_DMA 2524 - static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT]; 2525 #endif 2526 2527 static int __init setup_slub_min_order(char *str) ··· 2569 2570 down_write(&slub_lock); 2571 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN, 2572 - flags, NULL)) 2573 goto panic; 2574 2575 list_add(&s->list, &slab_caches); ··· 2703 { 2704 struct kmem_cache *s; 2705 2706 - if (unlikely(size > PAGE_SIZE / 2)) 2707 - return (void *)__get_free_pages(flags | __GFP_COMP, 2708 - get_order(size)); 2709 2710 s = get_slab(size, flags); 2711 ··· 2720 { 2721 struct kmem_cache *s; 2722 2723 - if (unlikely(size > PAGE_SIZE / 2)) 2724 - return (void *)__get_free_pages(flags | __GFP_COMP, 2725 - get_order(size)); 2726 2727 s = get_slab(size, flags); 2728 ··· 3032 caches++; 3033 } 3034 3035 - for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) { 3036 create_kmalloc_cache(&kmalloc_caches[i], 3037 "kmalloc", 1 << i, GFP_KERNEL); 3038 caches++; ··· 3059 slab_state = UP; 3060 3061 /* Provide the correct kmalloc names now that the caches are up */ 3062 - for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) 3063 kmalloc_caches[i]. name = 3064 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i); 3065 ··· 3086 static int slab_unmergeable(struct kmem_cache *s) 3087 { 3088 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE)) 3089 return 1; 3090 3091 if (s->ctor) ··· 3252 { 3253 struct kmem_cache *s; 3254 3255 - if (unlikely(size > PAGE_SIZE / 2)) 3256 - return (void *)__get_free_pages(gfpflags | __GFP_COMP, 3257 - get_order(size)); 3258 s = get_slab(size, gfpflags); 3259 3260 if (unlikely(ZERO_OR_NULL_PTR(s))) ··· 3268 { 3269 struct kmem_cache *s; 3270 3271 - if (unlikely(size > PAGE_SIZE / 2)) 3272 - return (void *)__get_free_pages(gfpflags | __GFP_COMP, 3273 - get_order(size)); 3274 s = get_slab(size, gfpflags); 3275 3276 if (unlikely(ZERO_OR_NULL_PTR(s)))
··· 211 /* Internal SLUB flags */ 212 #define __OBJECT_POISON 0x80000000 /* Poison object */ 213 #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */ 214 + #define __KMALLOC_CACHE 0x20000000 /* objects freed using kfree */ 215 + #define __PAGE_ALLOC_FALLBACK 0x10000000 /* Allow fallback to page alloc */ 216 217 /* Not all arches define cache_line_size */ 218 #ifndef cache_line_size ··· 308 return (unsigned long)addr & PAGE_MAPPING_ANON; 309 } 310 311 + static void *slab_address(struct page *page) 312 { 313 return page->end - PAGE_MAPPING_ANON; 314 } ··· 1078 struct page *page; 1079 int pages = 1 << s->order; 1080 1081 + flags |= s->allocflags; 1082 1083 if (node == -1) 1084 page = alloc_pages(flags, s->order); ··· 1546 unlock_out: 1547 slab_unlock(c->page); 1548 stat(c, ALLOC_SLOWPATH); 1549 #ifdef SLUB_FASTPATH 1550 local_irq_restore(flags); 1551 #endif ··· 1581 c->page = new; 1582 goto load_freelist; 1583 } 1584 + #ifdef SLUB_FASTPATH 1585 + local_irq_restore(flags); 1586 + #endif 1587 + /* 1588 + * No memory available. 1589 + * 1590 + * If the slab uses higher order allocs but the object is 1591 + * smaller than a page size then we can fallback in emergencies 1592 + * to the page allocator via kmalloc_large. The page allocator may 1593 + * have failed to obtain a higher order page and we can try to 1594 + * allocate a single page if the object fits into a single page. 1595 + * That is only possible if certain conditions are met that are being 1596 + * checked when a slab is created. 1597 + */ 1598 + if (!(gfpflags & __GFP_NORETRY) && (s->flags & __PAGE_ALLOC_FALLBACK)) 1599 + return kmalloc_large(s->objsize, gfpflags); 1600 + 1601 + return NULL; 1602 debug: 1603 object = c->page->freelist; 1604 if (!alloc_debug_processing(s, c->page, object, addr)) ··· 2329 size = ALIGN(size, align); 2330 s->size = size; 2331 2332 + if ((flags & __KMALLOC_CACHE) && 2333 + PAGE_SIZE / size < slub_min_objects) { 2334 + /* 2335 + * Kmalloc cache that would not have enough objects in 2336 + * an order 0 page. Kmalloc slabs can fallback to 2337 + * page allocator order 0 allocs so take a reasonably large 2338 + * order that will allows us a good number of objects. 2339 + */ 2340 + s->order = max(slub_max_order, PAGE_ALLOC_COSTLY_ORDER); 2341 + s->flags |= __PAGE_ALLOC_FALLBACK; 2342 + s->allocflags |= __GFP_NOWARN; 2343 + } else 2344 + s->order = calculate_order(size); 2345 + 2346 if (s->order < 0) 2347 return 0; 2348 + 2349 + s->allocflags = 0; 2350 + if (s->order) 2351 + s->allocflags |= __GFP_COMP; 2352 + 2353 + if (s->flags & SLAB_CACHE_DMA) 2354 + s->allocflags |= SLUB_DMA; 2355 + 2356 + if (s->flags & SLAB_RECLAIM_ACCOUNT) 2357 + s->allocflags |= __GFP_RECLAIMABLE; 2358 2359 /* 2360 * Determine the number of objects per slab ··· 2484 * Kmalloc subsystem 2485 *******************************************************************/ 2486 2487 + struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned; 2488 EXPORT_SYMBOL(kmalloc_caches); 2489 2490 #ifdef CONFIG_ZONE_DMA 2491 + static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1]; 2492 #endif 2493 2494 static int __init setup_slub_min_order(char *str) ··· 2536 2537 down_write(&slub_lock); 2538 if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN, 2539 + flags | __KMALLOC_CACHE, NULL)) 2540 goto panic; 2541 2542 list_add(&s->list, &slab_caches); ··· 2670 { 2671 struct kmem_cache *s; 2672 2673 + if (unlikely(size > PAGE_SIZE)) 2674 + return kmalloc_large(size, flags); 2675 2676 s = get_slab(size, flags); 2677 ··· 2688 { 2689 struct kmem_cache *s; 2690 2691 + if (unlikely(size > PAGE_SIZE)) 2692 + return kmalloc_large(size, flags); 2693 2694 s = get_slab(size, flags); 2695 ··· 3001 caches++; 3002 } 3003 3004 + for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) { 3005 create_kmalloc_cache(&kmalloc_caches[i], 3006 "kmalloc", 1 << i, GFP_KERNEL); 3007 caches++; ··· 3028 slab_state = UP; 3029 3030 /* Provide the correct kmalloc names now that the caches are up */ 3031 + for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) 3032 kmalloc_caches[i]. name = 3033 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i); 3034 ··· 3055 static int slab_unmergeable(struct kmem_cache *s) 3056 { 3057 if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE)) 3058 + return 1; 3059 + 3060 + if ((s->flags & __PAGE_ALLOC_FALLBACK)) 3061 return 1; 3062 3063 if (s->ctor) ··· 3218 { 3219 struct kmem_cache *s; 3220 3221 + if (unlikely(size > PAGE_SIZE)) 3222 + return kmalloc_large(size, gfpflags); 3223 + 3224 s = get_slab(size, gfpflags); 3225 3226 if (unlikely(ZERO_OR_NULL_PTR(s))) ··· 3234 { 3235 struct kmem_cache *s; 3236 3237 + if (unlikely(size > PAGE_SIZE)) 3238 + return kmalloc_large(size, gfpflags); 3239 + 3240 s = get_slab(size, gfpflags); 3241 3242 if (unlikely(ZERO_OR_NULL_PTR(s)))