Commit 130984e3 authored by Roman Gushchin's avatar Roman Gushchin Committed by Stephen Rothwell
Browse files

mm-memcg-slab-pre-allocate-obj_cgroups-for-slab-caches-with-slab_account-v2

open-code set_page_objcgs() and add some comments, by Johannes

Link: https://lkml.kernel.org/r/20201113001926.GA2934489@carbon.dhcp.thefacebook.com

Signed-off-by: default avatarRoman Gushchin <guro@fb.com>
Cc: Christoph Lameter <cl@linux.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Shakeel Butt <shakeelb@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarStephen Rothwell <sfr@canb.auug.org.au>
parent 9c7d6a6e
......@@ -475,25 +475,6 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
return (struct obj_cgroup **)(memcg_data & ~MEMCG_DATA_FLAGS_MASK);
}
/*
* set_page_objcgs - associate a page with a object cgroups vector
* @page: a pointer to the page struct
* @objcgs: a pointer to the object cgroups vector
* @atomic: save the value atomically
*
* Atomically associates a page with a vector of object cgroups.
*/
static inline bool set_page_objcgs(struct page *page,
struct obj_cgroup **objcgs, bool atomic)
{
unsigned long memcg_data = (unsigned long) objcgs | MEMCG_DATA_OBJCGS;
if (atomic)
return !cmpxchg(&page->memcg_data, 0, memcg_data);
page->memcg_data = memcg_data;
return true;
}
#else
static inline struct obj_cgroup **page_objcgs(struct page *page)
{
......@@ -504,12 +485,6 @@ static inline struct obj_cgroup **page_objcgs_check(struct page *page)
{
return NULL;
}
static inline bool set_page_objcgs(struct page *page,
struct obj_cgroup **objcgs, bool atomic)
{
return true;
}
#endif
static __always_inline bool memcg_stat_item_in_bytes(int idx)
......
......@@ -2952,9 +2952,10 @@ static void commit_charge(struct page *page, struct mem_cgroup *memcg)
#ifdef CONFIG_MEMCG_KMEM
int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
gfp_t gfp, bool atomic)
gfp_t gfp, bool new_page)
{
unsigned int objects = objs_per_slab_page(s, page);
unsigned long memcg_data;
void *vec;
vec = kcalloc_node(objects, sizeof(struct obj_cgroup *), gfp,
......@@ -2962,11 +2963,25 @@ int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
if (!vec)
return -ENOMEM;
if (!set_page_objcgs(page, vec, atomic))
memcg_data = (unsigned long) vec | MEMCG_DATA_OBJCGS;
if (new_page) {
/*
* If the slab page is brand new and nobody can yet access
* it's memcg_data, no synchronization is required and
* memcg_data can be simply assigned.
*/
page->memcg_data = memcg_data;
} else if (cmpxchg(&page->memcg_data, 0, memcg_data)) {
/*
* If the slab page is already in use, somebody can allocate
* and assign obj_cgroups in parallel. In this case the existing
* objcg vector should be reused.
*/
kfree(vec);
else
kmemleak_not_leak(vec);
return 0;
}
kmemleak_not_leak(vec);
return 0;
}
......
......@@ -240,7 +240,7 @@ static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla
#ifdef CONFIG_MEMCG_KMEM
int memcg_alloc_page_obj_cgroups(struct page *page, struct kmem_cache *s,
gfp_t gfp, bool atomic);
gfp_t gfp, bool new_page);
static inline void memcg_free_page_obj_cgroups(struct page *page)
{
......@@ -308,7 +308,7 @@ static inline void memcg_slab_post_alloc_hook(struct kmem_cache *s,
if (!page_objcgs(page) &&
memcg_alloc_page_obj_cgroups(page, s, flags,
true)) {
false)) {
obj_cgroup_uncharge(objcg, obj_full_size(s));
continue;
}
......@@ -373,7 +373,7 @@ static inline struct mem_cgroup *memcg_from_slab_obj(void *ptr)
static inline int memcg_alloc_page_obj_cgroups(struct page *page,
struct kmem_cache *s, gfp_t gfp,
bool atomic)
bool new_page)
{
return 0;
}
......@@ -418,7 +418,7 @@ static __always_inline void account_slab_page(struct page *page, int order,
gfp_t gfp)
{
if (memcg_kmem_enabled() && (s->flags & SLAB_ACCOUNT))
memcg_alloc_page_obj_cgroups(page, s, gfp, false);
memcg_alloc_page_obj_cgroups(page, s, gfp, true);
mod_node_page_state(page_pgdat(page), cache_vmstat_idx(s),
PAGE_SIZE << order);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment