Skip to content

Commit

Permalink
mm: kmem: move memcg_kmem_bypass() calls to get_mem/obj_cgroup_from_c…
Browse files Browse the repository at this point in the history
…urrent()

Patch series "mm: kmem: kernel memory accounting in an interrupt context".

This patchset implements memcg-based memory accounting of allocations made
from an interrupt context.

Historically, such allocations were passed unaccounted mostly because
charging the memory cgroup of the current process wasn't an option.  Also
performance reasons were likely a reason too.

The remote charging API allows to temporarily overwrite the currently
active memory cgroup, so that all memory allocations are accounted towards
some specified memory cgroup instead of the memory cgroup of the current
process.

This patchset extends the remote charging API so that it can be used from
an interrupt context.  Then it removes the fence that prevented the
accounting of allocations made from an interrupt context.  It also
contains a couple of optimizations/code refactorings.

This patchset doesn't directly enable accounting for any specific
allocations, but prepares the code base for it.  The bpf memory accounting
will likely be the first user of it: a typical example is a bpf program
parsing an incoming network packet, which allocates an entry in hashmap
map to store some information.

This patch (of 4):

Currently memcg_kmem_bypass() is called before obtaining the current
memory/obj cgroup using get_mem/obj_cgroup_from_current().  Moving
memcg_kmem_bypass() into get_mem/obj_cgroup_from_current() reduces the
number of call sites and allows further code simplifications.

Signed-off-by: Roman Gushchin <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Reviewed-by: Shakeel Butt <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Michal Hocko <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
rgushchin authored and torvalds committed Oct 18, 2020
1 parent b87d8ce commit 279c339
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
13 changes: 8 additions & 5 deletions mm/memcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@ EXPORT_SYMBOL(get_mem_cgroup_from_page);
*/
static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void)
{
if (memcg_kmem_bypass())
return NULL;

if (unlikely(current->active_memcg)) {
struct mem_cgroup *memcg;

Expand Down Expand Up @@ -2933,6 +2936,9 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
struct obj_cgroup *objcg = NULL;
struct mem_cgroup *memcg;

if (memcg_kmem_bypass())
return NULL;

if (unlikely(!current->mm && !current->active_memcg))
return NULL;

Expand Down Expand Up @@ -3059,19 +3065,16 @@ int __memcg_kmem_charge_page(struct page *page, gfp_t gfp, int order)
struct mem_cgroup *memcg;
int ret = 0;

if (memcg_kmem_bypass())
return 0;

memcg = get_mem_cgroup_from_current();
if (!mem_cgroup_is_root(memcg)) {
if (memcg && !mem_cgroup_is_root(memcg)) {
ret = __memcg_kmem_charge(memcg, gfp, 1 << order);
if (!ret) {
page->mem_cgroup = memcg;
__SetPageKmemcg(page);
return 0;
}
css_put(&memcg->css);
}
css_put(&memcg->css);
return ret;
}

Expand Down
3 changes: 1 addition & 2 deletions mm/percpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1584,8 +1584,7 @@ static enum pcpu_chunk_type pcpu_memcg_pre_alloc_hook(size_t size, gfp_t gfp,
{
struct obj_cgroup *objcg;

if (!memcg_kmem_enabled() || !(gfp & __GFP_ACCOUNT) ||
memcg_kmem_bypass())
if (!memcg_kmem_enabled() || !(gfp & __GFP_ACCOUNT))
return PCPU_CHUNK_ROOT;

objcg = get_obj_cgroup_from_current();
Expand Down
3 changes: 0 additions & 3 deletions mm/slab.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,6 @@ static inline struct obj_cgroup *memcg_slab_pre_alloc_hook(struct kmem_cache *s,
{
struct obj_cgroup *objcg;

if (memcg_kmem_bypass())
return NULL;

objcg = get_obj_cgroup_from_current();
if (!objcg)
return NULL;
Expand Down

0 comments on commit 279c339

Please sign in to comment.