Skip to content

Commit

Permalink
mm: memcontrol: don't allocate cgroup swap arrays when memcg is disabled
Browse files Browse the repository at this point in the history
Patch series "memcg swap fix & cleanups".


This patch (of 4):

Since commit 2d1c498 ("mm: memcontrol: make swap tracking an integral
part of memory control"), the cgroup swap arrays are used to track memory
ownership at the time of swap readahead and swapoff, even if swap space
*accounting* has been turned off by the user via swapaccount=0 (which sets
cgroup_memory_noswap).

However, the patch was overzealous: by simply dropping the
cgroup_memory_noswap conditionals in the swapon, swapoff and uncharge
path, it caused the cgroup arrays being allocated even when the memory
controller as a whole is disabled.  This is a waste of that memory.

Restore mem_cgroup_disabled() checks, implied previously by
cgroup_memory_noswap, in the swapon, swapoff, and swap_entry_free
callbacks.

Link: https://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/[email protected]
Fixes: 2d1c498 ("mm: memcontrol: make swap tracking an integral part of memory control")
Signed-off-by: Johannes Weiner <[email protected]>
Reported-by: Hugh Dickins <[email protected]>
Reviewed-by: Shakeel Butt <[email protected]>
Acked-by: Hugh Dickins <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Cc: Roman Gushchin <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
hnaz authored and akpm00 committed Oct 3, 2022
1 parent f7c5b1a commit c91bdc9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions mm/memcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -7459,6 +7459,9 @@ void __mem_cgroup_uncharge_swap(swp_entry_t entry, unsigned int nr_pages)
struct mem_cgroup *memcg;
unsigned short id;

if (mem_cgroup_disabled())
return;

id = swap_cgroup_record(entry, 0, nr_pages);
rcu_read_lock();
memcg = mem_cgroup_from_id(id);
Expand Down
6 changes: 6 additions & 0 deletions mm/swap_cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ int swap_cgroup_swapon(int type, unsigned long max_pages)
unsigned long length;
struct swap_cgroup_ctrl *ctrl;

if (mem_cgroup_disabled())
return 0;

length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);

array = vcalloc(length, sizeof(void *));
Expand Down Expand Up @@ -204,6 +207,9 @@ void swap_cgroup_swapoff(int type)
unsigned long i, length;
struct swap_cgroup_ctrl *ctrl;

if (mem_cgroup_disabled())
return;

mutex_lock(&swap_cgroup_mutex);
ctrl = &swap_cgroup_ctrl[type];
map = ctrl->map;
Expand Down

0 comments on commit c91bdc9

Please sign in to comment.