aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorGlauber Costa <glommer@parallels.com>2012-12-18 17:22:46 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2012-12-18 18:02:14 -0500
commitb9ce5ef49f00daf2254c6953c8d31f79aabccd34 (patch)
tree1da6afdbb3dc2e2d31f588dbfbf502e984af48a6 /mm
parent0e9d92f2d02d8c8320f0502307c688d07bdac2b3 (diff)
sl[au]b: always get the cache from its page in kmem_cache_free()
struct page already has this information. If we start chaining caches, this information will always be more trustworthy than whatever is passed into the function. Signed-off-by: Glauber Costa <glommer@parallels.com> Cc: Christoph Lameter <cl@linux.com> Cc: David Rientjes <rientjes@google.com> Cc: Frederic Weisbecker <fweisbec@redhat.com> Cc: Greg Thelen <gthelen@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: JoonSoo Kim <js1304@gmail.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Cc: Mel Gorman <mel@csn.ul.ie> Cc: Michal Hocko <mhocko@suse.cz> Cc: Pekka Enberg <penberg@cs.helsinki.fi> Cc: Rik van Riel <riel@redhat.com> Cc: Suleiman Souhlal <suleiman@google.com> Cc: Tejun Heo <tj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm')
-rw-r--r--mm/slab.c6
-rw-r--r--mm/slab.h39
-rw-r--r--mm/slob.c2
-rw-r--r--mm/slub.c15
4 files changed, 48 insertions, 14 deletions
diff --git a/mm/slab.c b/mm/slab.c
index c26ab9fbe1f5..bab6fec765a7 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -87,7 +87,6 @@
87 */ 87 */
88 88
89#include <linux/slab.h> 89#include <linux/slab.h>
90#include "slab.h"
91#include <linux/mm.h> 90#include <linux/mm.h>
92#include <linux/poison.h> 91#include <linux/poison.h>
93#include <linux/swap.h> 92#include <linux/swap.h>
@@ -128,6 +127,8 @@
128 127
129#include "internal.h" 128#include "internal.h"
130 129
130#include "slab.h"
131
131/* 132/*
132 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON. 133 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
133 * 0 for faster, smaller code (especially in the critical paths). 134 * 0 for faster, smaller code (especially in the critical paths).
@@ -3883,6 +3884,9 @@ EXPORT_SYMBOL(__kmalloc);
3883void kmem_cache_free(struct kmem_cache *cachep, void *objp) 3884void kmem_cache_free(struct kmem_cache *cachep, void *objp)
3884{ 3885{
3885 unsigned long flags; 3886 unsigned long flags;
3887 cachep = cache_from_obj(cachep, objp);
3888 if (!cachep)
3889 return;
3886 3890
3887 local_irq_save(flags); 3891 local_irq_save(flags);
3888 debug_check_no_locks_freed(objp, cachep->object_size); 3892 debug_check_no_locks_freed(objp, cachep->object_size);
diff --git a/mm/slab.h b/mm/slab.h
index abe582d20c79..c95e922b166d 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -116,6 +116,13 @@ static inline bool cache_match_memcg(struct kmem_cache *cachep,
116 return (is_root_cache(cachep) && !memcg) || 116 return (is_root_cache(cachep) && !memcg) ||
117 (cachep->memcg_params->memcg == memcg); 117 (cachep->memcg_params->memcg == memcg);
118} 118}
119
120static inline bool slab_equal_or_root(struct kmem_cache *s,
121 struct kmem_cache *p)
122{
123 return (p == s) ||
124 (s->memcg_params && (p == s->memcg_params->root_cache));
125}
119#else 126#else
120static inline bool is_root_cache(struct kmem_cache *s) 127static inline bool is_root_cache(struct kmem_cache *s)
121{ 128{
@@ -127,5 +134,37 @@ static inline bool cache_match_memcg(struct kmem_cache *cachep,
127{ 134{
128 return true; 135 return true;
129} 136}
137
138static inline bool slab_equal_or_root(struct kmem_cache *s,
139 struct kmem_cache *p)
140{
141 return true;
142}
130#endif 143#endif
144
145static inline struct kmem_cache *cache_from_obj(struct kmem_cache *s, void *x)
146{
147 struct kmem_cache *cachep;
148 struct page *page;
149
150 /*
151 * When kmemcg is not being used, both assignments should return the
152 * same value. but we don't want to pay the assignment price in that
153 * case. If it is not compiled in, the compiler should be smart enough
154 * to not do even the assignment. In that case, slab_equal_or_root
155 * will also be a constant.
156 */
157 if (!memcg_kmem_enabled() && !unlikely(s->flags & SLAB_DEBUG_FREE))
158 return s;
159
160 page = virt_to_head_page(x);
161 cachep = page->slab_cache;
162 if (slab_equal_or_root(cachep, s))
163 return cachep;
164
165 pr_err("%s: Wrong slab cache. %s but object is from %s\n",
166 __FUNCTION__, cachep->name, s->name);
167 WARN_ON_ONCE(1);
168 return s;
169}
131#endif 170#endif
diff --git a/mm/slob.c b/mm/slob.c
index 795bab7d391d..a99fdf7a0907 100644
--- a/mm/slob.c
+++ b/mm/slob.c
@@ -58,7 +58,6 @@
58 58
59#include <linux/kernel.h> 59#include <linux/kernel.h>
60#include <linux/slab.h> 60#include <linux/slab.h>
61#include "slab.h"
62 61
63#include <linux/mm.h> 62#include <linux/mm.h>
64#include <linux/swap.h> /* struct reclaim_state */ 63#include <linux/swap.h> /* struct reclaim_state */
@@ -73,6 +72,7 @@
73 72
74#include <linux/atomic.h> 73#include <linux/atomic.h>
75 74
75#include "slab.h"
76/* 76/*
77 * slob_block has a field 'units', which indicates size of block if +ve, 77 * slob_block has a field 'units', which indicates size of block if +ve,
78 * or offset of next block if -ve (in SLOB_UNITs). 78 * or offset of next block if -ve (in SLOB_UNITs).
diff --git a/mm/slub.c b/mm/slub.c
index 985332b38852..6d5f2305d7a4 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -2611,19 +2611,10 @@ redo:
2611 2611
2612void kmem_cache_free(struct kmem_cache *s, void *x) 2612void kmem_cache_free(struct kmem_cache *s, void *x)
2613{ 2613{
2614 struct page *page; 2614 s = cache_from_obj(s, x);
2615 2615 if (!s)
2616 page = virt_to_head_page(x);
2617
2618 if (kmem_cache_debug(s) && page->slab_cache != s) {
2619 pr_err("kmem_cache_free: Wrong slab cache. %s but object"
2620 " is from %s\n", page->slab_cache->name, s->name);
2621 WARN_ON_ONCE(1);
2622 return; 2616 return;
2623 } 2617 slab_free(s, virt_to_head_page(x), x, _RET_IP_);
2624
2625 slab_free(s, page, x, _RET_IP_);
2626
2627 trace_kmem_cache_free(_RET_IP_, x); 2618 trace_kmem_cache_free(_RET_IP_, x);
2628} 2619}
2629EXPORT_SYMBOL(kmem_cache_free); 2620EXPORT_SYMBOL(kmem_cache_free);