1 From: Peter Zijlstra <a.p.zijlstra@chello.nl>
2 Subject: mm: kmem_alloc_estimate()
4 References: FATE#303834
6 Provide a method to get the upper bound on the pages needed to allocate
7 a given number of objects from a given kmem_cache.
9 This lays the foundation for a generic reserve framework as presented in
10 a later patch in this series. This framework needs to convert object demand
11 (kmalloc() bytes, kmem_cache_alloc() objects) to pages.
13 Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
14 Acked-by: Neil Brown <neilb@suse.de>
15 Acked-by: Suresh Jayaraman <sjayaraman@suse.de>
18 include/linux/slab.h | 4 ++
19 mm/slab.c | 75 +++++++++++++++++++++++++++++++++++++++++++
20 mm/slob.c | 67 +++++++++++++++++++++++++++++++++++++++
21 mm/slub.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++
22 4 files changed, 233 insertions(+)
24 --- a/include/linux/slab.h
25 +++ b/include/linux/slab.h
26 @@ -65,6 +65,8 @@ void kmem_cache_free(struct kmem_cache *
27 unsigned int kmem_cache_size(struct kmem_cache *);
28 const char *kmem_cache_name(struct kmem_cache *);
29 int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr);
30 +unsigned kmem_alloc_estimate(struct kmem_cache *cachep,
31 + gfp_t flags, int objects);
34 * Please use this macro to create slab caches. Simply specify the
35 @@ -100,6 +102,8 @@ void * __must_check __krealloc(const voi
36 void * __must_check krealloc(const void *, size_t, gfp_t);
37 void kfree(const void *);
38 size_t ksize(const void *);
39 +unsigned kmalloc_estimate_objs(size_t, gfp_t, int);
40 +unsigned kmalloc_estimate_bytes(gfp_t, size_t);
43 * Allocator specific definitions. These are mainly used to establish optimized
46 @@ -3846,6 +3846,81 @@ const char *kmem_cache_name(struct kmem_
47 EXPORT_SYMBOL_GPL(kmem_cache_name);
50 + * Calculate the upper bound of pages required to sequentially allocate
51 + * @objects objects from @cachep.
53 +unsigned kmem_alloc_estimate(struct kmem_cache *cachep,
54 + gfp_t flags, int objects)
57 + * (1) memory for objects,
59 + unsigned nr_slabs = DIV_ROUND_UP(objects, cachep->num);
60 + unsigned nr_pages = nr_slabs << cachep->gfporder;
63 + * (2) memory for each per-cpu queue (nr_cpu_ids),
64 + * (3) memory for each per-node alien queues (nr_cpu_ids), and
65 + * (4) some amount of memory for the slab management structures
67 + * XXX: truely account these
69 + nr_pages += 1 + ilog2(nr_pages);
75 + * Calculate the upper bound of pages required to sequentially allocate
76 + * @count objects of @size bytes from kmalloc given @flags.
78 +unsigned kmalloc_estimate_objs(size_t size, gfp_t flags, int count)
80 + struct kmem_cache *s = kmem_find_general_cachep(size, flags);
84 + return kmem_alloc_estimate(s, flags, count);
86 +EXPORT_SYMBOL_GPL(kmalloc_estimate_objs);
89 + * Calculate the upper bound of pages requires to sequentially allocate @bytes
90 + * from kmalloc in an unspecified number of allocations of nonuniform size.
92 +unsigned kmalloc_estimate_bytes(gfp_t flags, size_t bytes)
94 + unsigned long pages;
95 + struct cache_sizes *csizep = malloc_sizes;
98 + * multiply by two, in order to account the worst case slack space
99 + * due to the power-of-two allocation sizes.
101 + pages = DIV_ROUND_UP(2 * bytes, PAGE_SIZE);
104 + * add the kmem_cache overhead of each possible kmalloc cache
106 + for (csizep = malloc_sizes; csizep->cs_cachep; csizep++) {
107 + struct kmem_cache *s;
109 +#ifdef CONFIG_ZONE_DMA
110 + if (unlikely(flags & __GFP_DMA))
111 + s = csizep->cs_dmacachep;
114 + s = csizep->cs_cachep;
117 + pages += kmem_alloc_estimate(s, flags, 0);
122 +EXPORT_SYMBOL_GPL(kmalloc_estimate_bytes);
125 * This initializes kmem_list3 or resizes various caches for all nodes.
127 static int alloc_kmemlist(struct kmem_cache *cachep)
130 @@ -661,3 +661,70 @@ void __init kmem_cache_init(void)
135 +static __slob_estimate(unsigned size, unsigned align, unsigned objects)
139 + size = SLOB_UNIT * SLOB_UNITS(size + align - 1);
141 + if (size <= PAGE_SIZE) {
142 + nr_pages = DIV_ROUND_UP(objects, PAGE_SIZE / size);
144 + nr_pages = objects << get_order(size);
151 + * Calculate the upper bound of pages required to sequentially allocate
152 + * @objects objects from @cachep.
154 +unsigned kmem_alloc_estimate(struct kmem_cache *c, gfp_t flags, int objects)
156 + unsigned size = c->size;
158 + if (c->flags & SLAB_DESTROY_BY_RCU)
159 + size += sizeof(struct slob_rcu);
161 + return __slob_estimate(size, c->align, objects);
165 + * Calculate the upper bound of pages required to sequentially allocate
166 + * @count objects of @size bytes from kmalloc given @flags.
168 +unsigned kmalloc_estimate_objs(size_t size, gfp_t flags, int count)
170 + unsigned align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
172 + return __slob_estimate(size, align, count);
174 +EXPORT_SYMBOL_GPL(kmalloc_estimate_objs);
177 + * Calculate the upper bound of pages requires to sequentially allocate @bytes
178 + * from kmalloc in an unspecified number of allocations of nonuniform size.
180 +unsigned kmalloc_estimate_bytes(gfp_t flags, size_t bytes)
182 + unsigned long pages;
185 + * Multiply by two, in order to account the worst case slack space
186 + * due to the power-of-two allocation sizes.
188 + * While not true for slob, it cannot do worse than that for sequential
191 + pages = DIV_ROUND_UP(2 * bytes, PAGE_SIZE);
194 + * Our power of two series starts at PAGE_SIZE, so add one page.
200 +EXPORT_SYMBOL_GPL(kmalloc_estimate_bytes);
203 @@ -2399,6 +2399,42 @@ const char *kmem_cache_name(struct kmem_
205 EXPORT_SYMBOL(kmem_cache_name);
208 + * Calculate the upper bound of pages required to sequentially allocate
209 + * @objects objects from @cachep.
211 + * We should use s->min_objects because those are the least efficient.
213 +unsigned kmem_alloc_estimate(struct kmem_cache *s, gfp_t flags, int objects)
215 + unsigned long pages;
216 + struct kmem_cache_order_objects x;
218 + if (WARN_ON(!s) || WARN_ON(!oo_objects(s->min)))
222 + pages = DIV_ROUND_UP(objects, oo_objects(x)) << oo_order(x);
225 + * Account the possible additional overhead if the slab holds more that
226 + * one object. Use s->max_objects because that's the worst case.
229 + if (oo_objects(x) > 1) {
231 + * Account the possible additional overhead if per cpu slabs
232 + * are currently empty and have to be allocated. This is very
233 + * unlikely but a possible scenario immediately after
234 + * kmem_cache_shrink.
236 + pages += num_possible_cpus() << oo_order(x);
241 +EXPORT_SYMBOL_GPL(kmem_alloc_estimate);
243 static void list_slab_objects(struct kmem_cache *s, struct page *page,
246 @@ -2778,6 +2814,57 @@ void kfree(const void *x)
247 EXPORT_SYMBOL(kfree);
250 + * Calculate the upper bound of pages required to sequentially allocate
251 + * @count objects of @size bytes from kmalloc given @flags.
253 +unsigned kmalloc_estimate_objs(size_t size, gfp_t flags, int count)
255 + struct kmem_cache *s = get_slab(size, flags);
259 + return kmem_alloc_estimate(s, flags, count);
262 +EXPORT_SYMBOL_GPL(kmalloc_estimate_objs);
265 + * Calculate the upper bound of pages requires to sequentially allocate @bytes
266 + * from kmalloc in an unspecified number of allocations of nonuniform size.
268 +unsigned kmalloc_estimate_bytes(gfp_t flags, size_t bytes)
271 + unsigned long pages;
274 + * multiply by two, in order to account the worst case slack space
275 + * due to the power-of-two allocation sizes.
277 + pages = DIV_ROUND_UP(2 * bytes, PAGE_SIZE);
280 + * add the kmem_cache overhead of each possible kmalloc cache
282 + for (i = 1; i < PAGE_SHIFT; i++) {
283 + struct kmem_cache *s;
285 +#ifdef CONFIG_ZONE_DMA
286 + if (unlikely(flags & SLUB_DMA))
287 + s = dma_kmalloc_cache(i, flags);
290 + s = &kmalloc_caches[i];
293 + pages += kmem_alloc_estimate(s, flags, 0);
298 +EXPORT_SYMBOL_GPL(kmalloc_estimate_bytes);
301 * kmem_cache_shrink removes empty slabs from the partial lists and sorts
302 * the remaining slabs by the number of items in use. The slabs with the
303 * most items in use come first. New allocations will then fill those up