Skip to content

Commit bff157b

Browse files
committed
Merge branch 'slab/next' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg/linux
Pull SLAB update from Pekka Enberg: "Nothing terribly exciting here apart from Christoph's kmalloc unification patches that brings sl[aou]b implementations closer to each other" * 'slab/next' of git://git.kernel.org/pub/scm/linux/kernel/git/penberg/linux: slab: Use correct GFP_DMA constant slub: remove verify_mem_not_deleted() mm/sl[aou]b: Move kmallocXXX functions to common code mm, slab_common: add 'unlikely' to size check of kmalloc_slab() mm/slub.c: beautify code for removing redundancy 'break' statement. slub: Remove unnecessary page NULL check slub: don't use cpu partial pages on UP mm/slub: beautify code for 80 column limitation and tab alignment mm/slub: remove 'per_cpu' which is useless variable
2 parents 8bf5e36 + 23774a2 commit bff157b

8 files changed

Lines changed: 216 additions & 371 deletions

File tree

‎include/linux/slab.h‎

Lines changed: 124 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* (C) SGI 2006, Christoph Lameter
55
* Cleaned up and restructured to ease the addition of alternative
66
* implementations of SLAB allocators.
7+
* (C) Linux Foundation 2008-2013
8+
* Unified interface for all slab allocators
79
*/
810

911
#ifndef _LINUX_SLAB_H
@@ -94,6 +96,7 @@
9496
#define ZERO_OR_NULL_PTR(x) ((unsigned long)(x) <= \
9597
(unsigned long)ZERO_SIZE_PTR)
9698

99+
#include <linux/kmemleak.h>
97100

98101
struct mem_cgroup;
99102
/*
@@ -289,6 +292,57 @@ static __always_inline int kmalloc_index(size_t size)
289292
}
290293
#endif /* !CONFIG_SLOB */
291294

295+
void *__kmalloc(size_t size, gfp_t flags);
296+
void *kmem_cache_alloc(struct kmem_cache *, gfp_t flags);
297+
298+
#ifdef CONFIG_NUMA
299+
void *__kmalloc_node(size_t size, gfp_t flags, int node);
300+
void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
301+
#else
302+
static __always_inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
303+
{
304+
return __kmalloc(size, flags);
305+
}
306+
307+
static __always_inline void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t flags, int node)
308+
{
309+
return kmem_cache_alloc(s, flags);
310+
}
311+
#endif
312+
313+
#ifdef CONFIG_TRACING
314+
extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t);
315+
316+
#ifdef CONFIG_NUMA
317+
extern void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
318+
gfp_t gfpflags,
319+
int node, size_t size);
320+
#else
321+
static __always_inline void *
322+
kmem_cache_alloc_node_trace(struct kmem_cache *s,
323+
gfp_t gfpflags,
324+
int node, size_t size)
325+
{
326+
return kmem_cache_alloc_trace(s, gfpflags, size);
327+
}
328+
#endif /* CONFIG_NUMA */
329+
330+
#else /* CONFIG_TRACING */
331+
static __always_inline void *kmem_cache_alloc_trace(struct kmem_cache *s,
332+
gfp_t flags, size_t size)
333+
{
334+
return kmem_cache_alloc(s, flags);
335+
}
336+
337+
static __always_inline void *
338+
kmem_cache_alloc_node_trace(struct kmem_cache *s,
339+
gfp_t gfpflags,
340+
int node, size_t size)
341+
{
342+
return kmem_cache_alloc_node(s, gfpflags, node);
343+
}
344+
#endif /* CONFIG_TRACING */
345+
292346
#ifdef CONFIG_SLAB
293347
#include <linux/slab_def.h>
294348
#endif
@@ -297,9 +351,60 @@ static __always_inline int kmalloc_index(size_t size)
297351
#include <linux/slub_def.h>
298352
#endif
299353

300-
#ifdef CONFIG_SLOB
301-
#include <linux/slob_def.h>
354+
static __always_inline void *
355+
kmalloc_order(size_t size, gfp_t flags, unsigned int order)
356+
{
357+
void *ret;
358+
359+
flags |= (__GFP_COMP | __GFP_KMEMCG);
360+
ret = (void *) __get_free_pages(flags, order);
361+
kmemleak_alloc(ret, size, 1, flags);
362+
return ret;
363+
}
364+
365+
#ifdef CONFIG_TRACING
366+
extern void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order);
367+
#else
368+
static __always_inline void *
369+
kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
370+
{
371+
return kmalloc_order(size, flags, order);
372+
}
373+
#endif
374+
375+
static __always_inline void *kmalloc_large(size_t size, gfp_t flags)
376+
{
377+
unsigned int order = get_order(size);
378+
return kmalloc_order_trace(size, flags, order);
379+
}
380+
381+
/**
382+
* kmalloc - allocate memory
383+
* @size: how many bytes of memory are required.
384+
* @flags: the type of memory to allocate (see kcalloc).
385+
*
386+
* kmalloc is the normal method of allocating memory
387+
* for objects smaller than page size in the kernel.
388+
*/
389+
static __always_inline void *kmalloc(size_t size, gfp_t flags)
390+
{
391+
if (__builtin_constant_p(size)) {
392+
if (size > KMALLOC_MAX_CACHE_SIZE)
393+
return kmalloc_large(size, flags);
394+
#ifndef CONFIG_SLOB
395+
if (!(flags & GFP_DMA)) {
396+
int index = kmalloc_index(size);
397+
398+
if (!index)
399+
return ZERO_SIZE_PTR;
400+
401+
return kmem_cache_alloc_trace(kmalloc_caches[index],
402+
flags, size);
403+
}
302404
#endif
405+
}
406+
return __kmalloc(size, flags);
407+
}
303408

304409
/*
305410
* Determine size used for the nth kmalloc cache.
@@ -321,6 +426,23 @@ static __always_inline int kmalloc_size(int n)
321426
return 0;
322427
}
323428

429+
static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
430+
{
431+
#ifndef CONFIG_SLOB
432+
if (__builtin_constant_p(size) &&
433+
size <= KMALLOC_MAX_CACHE_SIZE && !(flags & GFP_DMA)) {
434+
int i = kmalloc_index(size);
435+
436+
if (!i)
437+
return ZERO_SIZE_PTR;
438+
439+
return kmem_cache_alloc_node_trace(kmalloc_caches[i],
440+
flags, node, size);
441+
}
442+
#endif
443+
return __kmalloc_node(size, flags, node);
444+
}
445+
324446
/*
325447
* Setting ARCH_SLAB_MINALIGN in arch headers allows a different alignment.
326448
* Intended for arches that get misalignment faults even for 64 bit integer
@@ -451,36 +573,6 @@ static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
451573
return kmalloc_array(n, size, flags | __GFP_ZERO);
452574
}
453575

454-
#if !defined(CONFIG_NUMA) && !defined(CONFIG_SLOB)
455-
/**
456-
* kmalloc_node - allocate memory from a specific node
457-
* @size: how many bytes of memory are required.
458-
* @flags: the type of memory to allocate (see kmalloc).
459-
* @node: node to allocate from.
460-
*
461-
* kmalloc() for non-local nodes, used to allocate from a specific node
462-
* if available. Equivalent to kmalloc() in the non-NUMA single-node
463-
* case.
464-
*/
465-
static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
466-
{
467-
return kmalloc(size, flags);
468-
}
469-
470-
static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
471-
{
472-
return __kmalloc(size, flags);
473-
}
474-
475-
void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
476-
477-
static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
478-
gfp_t flags, int node)
479-
{
480-
return kmem_cache_alloc(cachep, flags);
481-
}
482-
#endif /* !CONFIG_NUMA && !CONFIG_SLOB */
483-
484576
/*
485577
* kmalloc_track_caller is a special version of kmalloc that records the
486578
* calling function of the routine calling it for slab leak tracking instead

‎include/linux/slab_def.h‎

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,6 @@
33

44
/*
55
* Definitions unique to the original Linux SLAB allocator.
6-
*
7-
* What we provide here is a way to optimize the frequent kmalloc
8-
* calls in the kernel by selecting the appropriate general cache
9-
* if kmalloc was called with a size that can be established at
10-
* compile time.
11-
*/
12-
13-
#include <linux/init.h>
14-
#include <linux/compiler.h>
15-
16-
/*
17-
* struct kmem_cache
18-
*
19-
* manages a cache.
206
*/
217

228
struct kmem_cache {
@@ -102,96 +88,4 @@ struct kmem_cache {
10288
*/
10389
};
10490

105-
void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
106-
void *__kmalloc(size_t size, gfp_t flags);
107-
108-
#ifdef CONFIG_TRACING
109-
extern void *kmem_cache_alloc_trace(struct kmem_cache *, gfp_t, size_t);
110-
#else
111-
static __always_inline void *
112-
kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
113-
{
114-
return kmem_cache_alloc(cachep, flags);
115-
}
116-
#endif
117-
118-
static __always_inline void *kmalloc(size_t size, gfp_t flags)
119-
{
120-
struct kmem_cache *cachep;
121-
void *ret;
122-
123-
if (__builtin_constant_p(size)) {
124-
int i;
125-
126-
if (!size)
127-
return ZERO_SIZE_PTR;
128-
129-
if (WARN_ON_ONCE(size > KMALLOC_MAX_SIZE))
130-
return NULL;
131-
132-
i = kmalloc_index(size);
133-
134-
#ifdef CONFIG_ZONE_DMA
135-
if (flags & GFP_DMA)
136-
cachep = kmalloc_dma_caches[i];
137-
else
138-
#endif
139-
cachep = kmalloc_caches[i];
140-
141-
ret = kmem_cache_alloc_trace(cachep, flags, size);
142-
143-
return ret;
144-
}
145-
return __kmalloc(size, flags);
146-
}
147-
148-
#ifdef CONFIG_NUMA
149-
extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
150-
extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
151-
152-
#ifdef CONFIG_TRACING
153-
extern void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
154-
gfp_t flags,
155-
int nodeid,
156-
size_t size);
157-
#else
158-
static __always_inline void *
159-
kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
160-
gfp_t flags,
161-
int nodeid,
162-
size_t size)
163-
{
164-
return kmem_cache_alloc_node(cachep, flags, nodeid);
165-
}
166-
#endif
167-
168-
static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
169-
{
170-
struct kmem_cache *cachep;
171-
172-
if (__builtin_constant_p(size)) {
173-
int i;
174-
175-
if (!size)
176-
return ZERO_SIZE_PTR;
177-
178-
if (WARN_ON_ONCE(size > KMALLOC_MAX_SIZE))
179-
return NULL;
180-
181-
i = kmalloc_index(size);
182-
183-
#ifdef CONFIG_ZONE_DMA
184-
if (flags & GFP_DMA)
185-
cachep = kmalloc_dma_caches[i];
186-
else
187-
#endif
188-
cachep = kmalloc_caches[i];
189-
190-
return kmem_cache_alloc_node_trace(cachep, flags, node, size);
191-
}
192-
return __kmalloc_node(size, flags, node);
193-
}
194-
195-
#endif /* CONFIG_NUMA */
196-
19791
#endif /* _LINUX_SLAB_DEF_H */

‎include/linux/slob_def.h‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)