Skip to content

Commit becaba6

Browse files
rgushchintorvalds
authored andcommitted
mm: memcg/slab: fix obj_cgroup_charge() return value handling
Commit 10befea ("mm: memcg/slab: use a single set of kmem_caches for all allocations") introduced a regression into the handling of the obj_cgroup_charge() return value. If a non-zero value is returned (indicating of exceeding one of memory.max limits), the allocation should fail, instead of falling back to non-accounted mode. To make the code more readable, move memcg_slab_pre_alloc_hook() and memcg_slab_post_alloc_hook() calling conditions into bodies of these hooks. Fixes: 10befea ("mm: memcg/slab: use a single set of kmem_caches for all allocations") Signed-off-by: Roman Gushchin <guro@fb.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Shakeel Butt <shakeelb@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Cc: <stable@vger.kernel.org> Link: https://lkml.kernel.org/r/20201127161828.GD840171@carbon.dhcp.thefacebook.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 2bf509d commit becaba6

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

mm/slab.h

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -274,22 +274,32 @@ static inline size_t obj_full_size(struct kmem_cache *s)
274274
return s->size + sizeof(struct obj_cgroup *);
275275
}
276276

277-
static inline struct obj_cgroup *memcg_slab_pre_alloc_hook(struct kmem_cache *s,
278-
size_t objects,
279-
gfp_t flags)
277+
/*
278+
* Returns false if the allocation should fail.
279+
*/
280+
static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
281+
struct obj_cgroup **objcgp,
282+
size_t objects, gfp_t flags)
280283
{
281284
struct obj_cgroup *objcg;
282285

286+
if (!memcg_kmem_enabled())
287+
return true;
288+
289+
if (!(flags & __GFP_ACCOUNT) && !(s->flags & SLAB_ACCOUNT))
290+
return true;
291+
283292
objcg = get_obj_cgroup_from_current();
284293
if (!objcg)
285-
return NULL;
294+
return true;
286295

287296
if (obj_cgroup_charge(objcg, flags, objects * obj_full_size(s))) {
288297
obj_cgroup_put(objcg);
289-
return NULL;
298+
return false;
290299
}
291300

292-
return objcg;
301+
*objcgp = objcg;
302+
return true;
293303
}
294304

295305
static inline void mod_objcg_state(struct obj_cgroup *objcg,
@@ -315,7 +325,7 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
315325
unsigned long off;
316326
size_t i;
317327

318-
if (!objcg)
328+
if (!memcg_kmem_enabled() || !objcg)
319329
return;
320330

321331
flags &= ~__GFP_ACCOUNT;
@@ -400,11 +410,11 @@ static inline void memcg_free_page_obj_cgroups(struct page *page)
400410
{
401411
}
402412

403-
static inline struct obj_cgroup *memcg_slab_pre_alloc_hook(struct kmem_cache *s,
404-
size_t objects,
405-
gfp_t flags)
413+
static inline bool memcg_slab_pre_alloc_hook(struct kmem_cache *s,
414+
struct obj_cgroup **objcgp,
415+
size_t objects, gfp_t flags)
406416
{
407-
return NULL;
417+
return true;
408418
}
409419

410420
static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
@@ -508,9 +518,8 @@ static inline struct kmem_cache *slab_pre_alloc_hook(struct kmem_cache *s,
508518
if (should_failslab(s, flags))
509519
return NULL;
510520

511-
if (memcg_kmem_enabled() &&
512-
((flags & __GFP_ACCOUNT) || (s->flags & SLAB_ACCOUNT)))
513-
*objcgp = memcg_slab_pre_alloc_hook(s, size, flags);
521+
if (!memcg_slab_pre_alloc_hook(s, objcgp, size, flags))
522+
return NULL;
514523

515524
return s;
516525
}
@@ -529,8 +538,7 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s,
529538
s->flags, flags);
530539
}
531540

532-
if (memcg_kmem_enabled())
533-
memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
541+
memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
534542
}
535543

536544
#ifndef CONFIG_SLOB

0 commit comments

Comments
 (0)