Skip to content

Commit 270c6a7

Browse files
rgushchinAlexei Starovoitov
authored andcommitted
mm: memcontrol/slab: Use helpers to access slab page's memcg_data
To gather all direct accesses to struct page's memcg_data field in one place, let's introduce 3 new helpers to use in the slab accounting code: struct obj_cgroup **page_objcgs(struct page *page); struct obj_cgroup **page_objcgs_check(struct page *page); bool set_page_objcgs(struct page *page, struct obj_cgroup **objcgs); They are similar to the corresponding API for generic pages, except that the setter can return false, indicating that the value has been already set from a different thread. Signed-off-by: Roman Gushchin <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Reviewed-by: Shakeel Butt <[email protected]> Acked-by: Johannes Weiner <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Link: https://lore.kernel.org/bpf/[email protected]
1 parent bcfe06b commit 270c6a7

File tree

3 files changed

+75
-30
lines changed

3 files changed

+75
-30
lines changed

include/linux/memcontrol.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,70 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page)
416416
return (struct mem_cgroup *)memcg_data;
417417
}
418418

419+
#ifdef CONFIG_MEMCG_KMEM
420+
/*
421+
* page_objcgs - get the object cgroups vector associated with a page
422+
* @page: a pointer to the page struct
423+
*
424+
* Returns a pointer to the object cgroups vector associated with the page,
425+
* or NULL. This function assumes that the page is known to have an
426+
* associated object cgroups vector. It's not safe to call this function
427+
* against pages, which might have an associated memory cgroup: e.g.
428+
* kernel stack pages.
429+
*/
430+
static inline struct obj_cgroup **page_objcgs(struct page *page)
431+
{
432+
return (struct obj_cgroup **)(READ_ONCE(page->memcg_data) & ~0x1UL);
433+
}
434+
435+
/*
436+
* page_objcgs_check - get the object cgroups vector associated with a page
437+
* @page: a pointer to the page struct
438+
*
439+
* Returns a pointer to the object cgroups vector associated with the page,
440+
* or NULL. This function is safe to use if the page can be directly associated
441+
* with a memory cgroup.
442+
*/
443+
static inline struct obj_cgroup **page_objcgs_check(struct page *page)
444+
{
445+
unsigned long memcg_data = READ_ONCE(page->memcg_data);
446+
447+
if (memcg_data && (memcg_data & 0x1UL))
448+
return (struct obj_cgroup **)(memcg_data & ~0x1UL);
449+
450+
return NULL;
451+
}
452+
453+
/*
454+
* set_page_objcgs - associate a page with a object cgroups vector
455+
* @page: a pointer to the page struct
456+
* @objcgs: a pointer to the object cgroups vector
457+
*
458+
* Atomically associates a page with a vector of object cgroups.
459+
*/
460+
static inline bool set_page_objcgs(struct page *page,
461+
struct obj_cgroup **objcgs)
462+
{
463+
return !cmpxchg(&page->memcg_data, 0, (unsigned long)objcgs | 0x1UL);
464+
}
465+
#else
466+
static inline struct obj_cgroup **page_objcgs(struct page *page)
467+
{
468+
return NULL;
469+
}
470+
471+
static inline struct obj_cgroup **page_objcgs_check(struct page *page)
472+
{
473+
return NULL;
474+
}
475+
476+
static inline bool set_page_objcgs(struct page *page,
477+
struct obj_cgroup **objcgs)
478+
{
479+
return true;
480+
}
481+
#endif
482+
419483
static __always_inline bool memcg_stat_item_in_bytes(int idx)
420484
{
421485
if (idx == MEMCG_PERCPU_B)

mm/memcontrol.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2899,7 +2899,7 @@ int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
28992899
if (!vec)
29002900
return -ENOMEM;
29012901

2902-
if (cmpxchg(&page->memcg_data, 0, (unsigned long)vec | 0x1UL))
2902+
if (!set_page_objcgs(page, vec))
29032903
kfree(vec);
29042904
else
29052905
kmemleak_not_leak(vec);
@@ -2933,12 +2933,12 @@ struct mem_cgroup *mem_cgroup_from_obj(void *p)
29332933
* Memcg membership data for each individual object is saved in
29342934
* the page->obj_cgroups.
29352935
*/
2936-
if (page_has_obj_cgroups(page)) {
2936+
if (page_objcgs_check(page)) {
29372937
struct obj_cgroup *objcg;
29382938
unsigned int off;
29392939

29402940
off = obj_to_index(page->slab_cache, page, p);
2941-
objcg = page_obj_cgroups(page)[off];
2941+
objcg = page_objcgs(page)[off];
29422942
if (objcg)
29432943
return obj_cgroup_memcg(objcg);
29442944

mm/slab.h

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -239,28 +239,12 @@ static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla
239239
}
240240

241241
#ifdef CONFIG_MEMCG_KMEM
242-
static inline struct obj_cgroup **page_obj_cgroups(struct page *page)
243-
{
244-
/*
245-
* Page's memory cgroup and obj_cgroups vector are sharing the same
246-
* space. To distinguish between them in case we don't know for sure
247-
* that the page is a slab page (e.g. page_cgroup_ino()), let's
248-
* always set the lowest bit of obj_cgroups.
249-
*/
250-
return (struct obj_cgroup **)(page->memcg_data & ~0x1UL);
251-
}
252-
253-
static inline bool page_has_obj_cgroups(struct page *page)
254-
{
255-
return page->memcg_data & 0x1UL;
256-
}
257-
258242
int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
259243
gfp_t gfp);
260244

261245
static inline void memcg_free_page_obj_cgroups(struct page *page)
262246
{
263-
kfree(page_obj_cgroups(page));
247+
kfree(page_objcgs(page));
264248
page->memcg_data = 0;
265249
}
266250

@@ -322,15 +306,15 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
322306
if (likely(p[i])) {
323307
page = virt_to_head_page(p[i]);
324308

325-
if (!page_has_obj_cgroups(page) &&
309+
if (!page_objcgs(page) &&
326310
memcg_alloc_page_obj_cgroups(page, s, flags)) {
327311
obj_cgroup_uncharge(objcg, obj_full_size(s));
328312
continue;
329313
}
330314

331315
off = obj_to_index(s, page, p[i]);
332316
obj_cgroup_get(objcg);
333-
page_obj_cgroups(page)[off] = objcg;
317+
page_objcgs(page)[off] = objcg;
334318
mod_objcg_state(objcg, page_pgdat(page),
335319
cache_vmstat_idx(s), obj_full_size(s));
336320
} else {
@@ -344,6 +328,7 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s_orig,
344328
void **p, int objects)
345329
{
346330
struct kmem_cache *s;
331+
struct obj_cgroup **objcgs;
347332
struct obj_cgroup *objcg;
348333
struct page *page;
349334
unsigned int off;
@@ -357,7 +342,8 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s_orig,
357342
continue;
358343

359344
page = virt_to_head_page(p[i]);
360-
if (!page_has_obj_cgroups(page))
345+
objcgs = page_objcgs(page);
346+
if (!objcgs)
361347
continue;
362348

363349
if (!s_orig)
@@ -366,11 +352,11 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s_orig,
366352
s = s_orig;
367353

368354
off = obj_to_index(s, page, p[i]);
369-
objcg = page_obj_cgroups(page)[off];
355+
objcg = objcgs[off];
370356
if (!objcg)
371357
continue;
372358

373-
page_obj_cgroups(page)[off] = NULL;
359+
objcgs[off] = NULL;
374360
obj_cgroup_uncharge(objcg, obj_full_size(s));
375361
mod_objcg_state(objcg, page_pgdat(page), cache_vmstat_idx(s),
376362
-obj_full_size(s));
@@ -379,11 +365,6 @@ static inline void memcg_slab_free_hook(struct kmem_cache *s_orig,
379365
}
380366

381367
#else /* CONFIG_MEMCG_KMEM */
382-
static inline bool page_has_obj_cgroups(struct page *page)
383-
{
384-
return false;
385-
}
386-
387368
static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
388369
{
389370
return NULL;

0 commit comments

Comments
 (0)