Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: add swappiness=max arg to memory.reclaim for only anon reclaim

Patch series "add max arg to swappiness in memory.reclaim and lru_gen", v4.

This patchset adds max arg to swappiness in memory.reclaim and lru_gen for
anon only proactive memory reclaim.

With commit <68cd9050d871> ("mm: add swappiness= arg to memory.reclaim")
we can submit an additional swappiness=<val> argument to memory.reclaim.
It is very useful because we can dynamically adjust the reclamation ratio
based on the anonymous folios and file folios of each cgroup. For
example,when swappiness is set to 0, we only reclaim from file folios.
But we can not relciam memory just from anon folios.

This patchset introduces a new macro, SWAPPINESS_ANON_ONLY, defined as
MAX_SWAPPINESS + 1, represent the max arg semantics. It specifically
indicates that reclamation should occur only from anonymous pages.

Patch 1 adds swappiness=max arg to memory.reclaim suggested-by: Yosry
Ahmed

Patch 2 add more comments for cache_trim_mode from Johannes Weiner in [1].

Patch 3 add max arg to lru_gen for proactive memory reclaim in MGLRU. The
MGLRU already supports reclaiming exclusively from anonymous pages. This
patch formalizes that behavior by introducing a max parameter to represent
the corresponding semantics.

Patch 4 using SWAPPINESS_ANON_ONLY in MGLRU Using SWAPPINESS_ANON_ONLY
instead of MAX_SWAPPINESS + 1 to indicate reclaiming only from anonymous
pages makes the code more readable and explicit

Here is the previous discussion:
https://lore.kernel.org/all/20250314033350.1156370-1-hezhongkun.hzk@bytedance.com/
https://lore.kernel.org/all/20250312094337.2296278-1-hezhongkun.hzk@bytedance.com/
https://lore.kernel.org/all/20250318135330.3358345-1-hezhongkun.hzk@bytedance.com/


This patch (of 4):

With commit <68cd9050d871> ("mm: add swappiness= arg to memory.reclaim")
we can submit an additional swappiness=<val> argument to memory.reclaim.
It is very useful because we can dynamically adjust the reclamation ratio
based on the anonymous folios and file folios of each cgroup. For
example,when swappiness is set to 0, we only reclaim from file folios.

However,we have also encountered a new issue: when swappiness is set to
the MAX_SWAPPINESS, it may still only reclaim file folios.

So, we hope to add a new arg 'swappiness=max' in memory.reclaim where
proactive memory reclaim only reclaims from anonymous folios when
swappiness is set to max. The swappiness semantics from a user
perspective remain unchanged.

For example, something like this:

echo "2M swappiness=max" > /sys/fs/cgroup/memory.reclaim

will perform reclaim on the rootcg with a swappiness setting of 'max' (a
new mode) regardless of the file folios. Users have a more comprehensive
view of the application's memory distribution because there are many
metrics available. For example, if we find that a certain cgroup has a
large number of inactive anon folios, we can reclaim only those and skip
file folios, because with the zram/zswap, the IO tradeoff that
cache_trim_mode or other file first logic is making doesn't hold - file
refaults will cause IO, whereas anon decompression will not.

With this patch, the swappiness argument of memory.reclaim has a new
mode 'max', means reclaiming just from anonymous folios both in traditional
LRU and MGLRU.

Link: https://lkml.kernel.org/r/cover.1745225696.git.hezhongkun.hzk@bytedance.com
Link: https://lore.kernel.org/all/20250314141833.GA1316033@cmpxchg.org/ [1]
Link: https://lkml.kernel.org/r/519e12b9b1f8c31a01e228c8b4b91a2419684f77.1745225696.git.hezhongkun.hzk@bytedance.com
Signed-off-by: Zhongkun He <hezhongkun.hzk@bytedance.com>
Suggested-by: Yosry Ahmed <yosry.ahmed@linux.dev>
Acked-by: Muchun Song <muchun.song@linux.dev>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Yu Zhao <yuzhao@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Zhongkun He and committed by
Andrew Morton
68a1436b c6c895cf

+19
+3
Documentation/admin-guide/cgroup-v2.rst
··· 1372 1372 same semantics as vm.swappiness applied to memcg reclaim with 1373 1373 all the existing limitations and potential future extensions. 1374 1374 1375 + The valid range for swappiness is [0-200, max], setting 1376 + swappiness=max exclusively reclaims anonymous memory. 1377 + 1375 1378 memory.peak 1376 1379 A read-write single value file which exists on non-root cgroups. 1377 1380
+4
include/linux/swap.h
··· 414 414 #define MEMCG_RECLAIM_PROACTIVE (1 << 2) 415 415 #define MIN_SWAPPINESS 0 416 416 #define MAX_SWAPPINESS 200 417 + 418 + /* Just recliam from anon folios in proactive memory reclaim */ 419 + #define SWAPPINESS_ANON_ONLY (MAX_SWAPPINESS + 1) 420 + 417 421 extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 418 422 unsigned long nr_pages, 419 423 gfp_t gfp_mask,
+5
mm/memcontrol.c
··· 4474 4474 4475 4475 enum { 4476 4476 MEMORY_RECLAIM_SWAPPINESS = 0, 4477 + MEMORY_RECLAIM_SWAPPINESS_MAX, 4477 4478 MEMORY_RECLAIM_NULL, 4478 4479 }; 4479 4480 4480 4481 static const match_table_t tokens = { 4481 4482 { MEMORY_RECLAIM_SWAPPINESS, "swappiness=%d"}, 4483 + { MEMORY_RECLAIM_SWAPPINESS_MAX, "swappiness=max"}, 4482 4484 { MEMORY_RECLAIM_NULL, NULL }, 4483 4485 }; 4484 4486 ··· 4513 4511 return -EINVAL; 4514 4512 if (swappiness < MIN_SWAPPINESS || swappiness > MAX_SWAPPINESS) 4515 4513 return -EINVAL; 4514 + break; 4515 + case MEMORY_RECLAIM_SWAPPINESS_MAX: 4516 + swappiness = SWAPPINESS_ANON_ONLY; 4516 4517 break; 4517 4518 default: 4518 4519 return -EINVAL;
+7
mm/vmscan.c
··· 2509 2509 goto out; 2510 2510 } 2511 2511 2512 + /* Proactive reclaim initiated by userspace for anonymous memory only */ 2513 + if (swappiness == SWAPPINESS_ANON_ONLY) { 2514 + WARN_ON_ONCE(!sc->proactive); 2515 + scan_balance = SCAN_ANON; 2516 + goto out; 2517 + } 2518 + 2512 2519 /* 2513 2520 * Do not apply any pressure balancing cleverness when the 2514 2521 * system is close to OOM, scan both anon and file equally