mm: add source selector to memory.reclaim
Subsystems (8)
PATCH v3 1/1 mm: add source selector to memory.reclaim
memory.reclaim can apply proactive reclaim to a memcg, but it cannot target source nodes the way global node reclaim can. Proactive memcg reclaim follows a fast-to-slow fallback order, which attempts demotion before reclaiming from slower tiers.
As a result, a memcg controller cannot decide whether reclaim should start from DRAM or from a slower tier. For a cgroup that is close to OOM and needs charge reduction, or for a controller trying to shape a workload's DRAM/CXL resident distribution, the default policy may spend reclaim effort on the wrong source tier.
Add source=nodes:<nodelist> to memory.reclaim. The selector only chooses the resident source nodes where the existing memcg reclaim path applies pressure. It does not select demotion targets, force demotion, force eviction, promote pages, or change completion accounting. Leaving source unspecified preserves existing behavior.
With this, userspace can build the policy it needs on top of existing kernel reclaim behavior:
1. start reclaim from lower-tier nodes to reduce the cgroup's memory usage more directly;
2. steer reclaim toward over-represented nodes to shape a workload's DRAM/CXL resident distribution; and
3. reclaim from lower-tier nodes first so later top-tier reclaim has room for demotion.
Example: nodes 0-2=DRAM, node3=CXL
echo 1G > memory.reclaim # progress from nodes 0-3
echo "1G source=nodes:3" > memory.reclaim # reclaim from CXL echo "1G source=nodes:0-2" > memory.reclaim # usually demote to CXL
The typed source= form also leaves room for future source selectors without changing the ABI introduced here.
Tested in a CXL setup with nodes 0-2 as DRAM and node 3 as CXL. A userspace controller steered eight cgroups toward a 50:50 DRAM:CXL resident ratio while a background workload created reclaim and demotion pressure. With source=nodes, all eight cgroups ended with 45%-55% of their memory on DRAM. Under the same pressure, the plain memory.reclaim baseline left the worst cgroup far away from the target range.
---
v2: https://lore.kernel.org/all/[email protected]/#t
v1: https://lore.kernel.org/all/[email protected]/
Changes since v2:
- Dropped the separate cover letter and shortened/refocused the commit message, as suggested by Joshua Hahn and Gregory Price.
- Added an eight-cgroup CXL test summary under background reclaim/demotion pressure, as suggested by Joshua Hahn.
--- Documentation/admin-guide/cgroup-v2.rst | 11 ++++++ mm/bpf_memcontrol.c | 3 +- mm/internal.h | 3 +- mm/memcontrol-v1.c | 6 ++- mm/memcontrol.c | 11 ++++-- mm/vmscan.c | 49 +++++++++++++++++++++++-- 6 files changed, 72 insertions(+), 11 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +11 -0
@@ -1458,6 +1458,7 @@ The following nested keys are defined. ========== ================================ swappiness Swappiness value to reclaim with + source Source to apply reclaim pressure to ========== ================================ Specifying a swappiness value instructs the kernel to perform
@@ -1468,6 +1469,16 @@ The following nested keys are defined. The valid range for swappiness is [0-200, max], setting swappiness=max exclusively reclaims anonymous memory. + Specifying a source value constrains where proactive reclaim applies + pressure. Currently only source=nodes:<nodelist> is supported. For + example:: + + echo "1G source=nodes:0-1" > memory.reclaim + + This applies proactive reclaim pressure to memory of the target cgroup + resident on nodes 0-1. It does not constrain demotion targets and does + not change memory.reclaim completion accounting. + memory.peak A read-write single value file which exists on non-root cgroups.
mm/bpf_memcontrol.c +2 -1
@@ -201,7 +201,8 @@ __bpf_kfunc long bpf_proactive_reclaim(struct mem_cgroup *memcg, nr_reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages,GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP |MEMCG_RECLAIM_PROACTIVE , - swappiness ==-1 ?NULL : &swappiness); + swappiness ==-1 ?NULL : &swappiness, +NULL );return nr_reclaimed *PAGE_SIZE ; }
mm/internal.h +2 -1
@@ -83,7 +83,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness); + int *swappiness, + const nodemask_t *source_nodes);#ifdef CONFIG_NUMA extern int sysctl_min_unmapped_ratio;
mm/memcontrol-v1.c +4 -2
@@ -1464,7 +1464,8 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg, }if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , - memsw ?0 :MEMCG_RECLAIM_MAY_SWAP ,NULL )) { + memsw ?0 :MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) { ret = -EBUSY ;break ; }
@@ -1500,7 +1501,8 @@ static int mem_cgroup_force_empty(struct mem_cgroup *memcg)break ;if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , -MEMCG_RECLAIM_MAY_SWAP ,NULL )) +MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_retries--; }
mm/memcontrol.c +7 -4
@@ -2444,7 +2444,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg, nr_reclaimed +=try_to_free_mem_cgroup_pages (memcg, nr_pages, gfp_mask,MEMCG_RECLAIM_MAY_SWAP , -NULL ); +NULL ,NULL );psi_memstall_leave (&pflags); }while ((memcg =parent_mem_cgroup (memcg)) && !mem_cgroup_is_root (memcg));
@@ -2774,7 +2774,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,psi_memstall_enter (&pflags); nr_reclaimed =try_to_free_mem_cgroup_pages (mem_over_limit, nr_pages, - gfp_mask, reclaim_options,NULL ); + gfp_mask, reclaim_options, +NULL ,NULL );psi_memstall_leave (&pflags);if (mem_cgroup_margin (mem_over_limit) >= nr_pages)
@@ -5001,7 +5002,8 @@ static ssize_t memory_high_write(struct kernfs_open_file *of, } reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages - high, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL ); +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL );if (!reclaimed && !nr_retries--)break ;
@@ -5060,7 +5062,8 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,if (nr_reclaims) {if (!try_to_free_mem_cgroup_pages (memcg, nr_pages - max, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL )) +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_reclaims--;continue ; }
mm/vmscan.c +46 -3
98 lines not on this page · raw message
Signature · 1 line
2.20.1
Quoted 11 lines · Li Zhe
memory.reclaim can apply proactive reclaim to a memcg, but it cannot target source nodes the way global node reclaim can. Proactive memcg reclaim follows a fast-to-slow fallback order, which attempts demotion before reclaiming from slower tiers.
As a result, a memcg controller cannot decide whether reclaim should start from DRAM or from a slower tier. For a cgroup that is close to OOM and needs charge reduction, or for a controller trying to shape a workload's DRAM/CXL resident distribution, the default policy may spend reclaim effort on the wrong source tier.
Quoted 5 lines
Add source=nodes:<nodelist> to memory.reclaim. The selector only chooses the resident source nodes where the existing memcg reclaim path applies pressure. It does not select demotion targets, force demotion, force eviction, promote pages, or change completion accounting. Leaving source unspecified preserves existing behavior.
Hi Zhe,
Thanks for the patch and sorry that it took so long to reply. I've actually spent a lot of time thinking about this commit to see if it can be used to establish per-memcg-tier residency limits on machines running multiple workloads via memcgs.
So you could have a daemon running, checking the per-tier usage for a cgroup, and creating pressure to evict pages downward from that cgroup specifically.
So if a cgroup is using 10G more node0 (DRAM) than it should be, you can write "echo 10G source=nodes:0" > memory.reclaim to try and free up that space. I'm running some experiments locally and really thinking through this to see what that would lead to.
Anyways, to me the only concern that I have is what happens when that reclaim fails. Let's say that you have a situation where CXL is completely full, preventing future demtions. At the same time, we have a memcg that is using too much DRAM, so we try to reclaim from the DRAM node and pressure downwards.
The thing is, I think proactive reclaim doesn't allow swapouts on tiered systems.
So what would happen is, you would (1) fail demotions, and then (2) fail to swap out, which means the pages have nowhere to go. And repeatedly running the same command over and over again will not lead to any progress at all.
Quoted 10 lines
With this, userspace can build the policy it needs on top of existing kernel reclaim behavior:
1. start reclaim from lower-tier nodes to reduce the cgroup's memory usage more directly;
2. steer reclaim toward over-represented nodes to shape a workload's DRAM/CXL resident distribution; and
3. reclaim from lower-tier nodes first so later top-tier reclaim has room for demotion.
Quoted 5 lines
Example: nodes 0-2=DRAM, node3=CXL
echo 1G > memory.reclaim # progress from nodes 0-3
echo "1G source=nodes:3" > memory.reclaim # reclaim from CXL echo "1G source=nodes:0-2" > memory.reclaim # usually demote to CXL
And so I can see why you would have to run something like this in order to actually make room, moving from the bottom going up. Make space for the memory that's about to come in from the top.
To me this makes a lot of sense, and it's something that we (Meta) has thought about quite extensively internally as well (from Johannes and Gregory, who are also CC-ed in this mail).
But at the same time, this is not really an atomic operation. So memory usage between the two operations can change very rapidly. I wanted to start a conversation about this point and whether this is something that the commit should address. I would like to hone in on a use case and think about how the users should be expected to use these things first.
Thanks a lot Zhe, and sorry again for the delayed response. I hope you have a great day!
Joshua
Quoted 263 lines
The typed source= form also leaves room for future source selectors without changing the ABI introduced here.
Tested in a CXL setup with nodes 0-2 as DRAM and node 3 as CXL. A userspace controller steered eight cgroups toward a 50:50 DRAM:CXL resident ratio while a background workload created reclaim and demotion pressure. With source=nodes, all eight cgroups ended with 45%-55% of their memory on DRAM. Under the same pressure, the plain memory.reclaim baseline left the worst cgroup far away from the target range.
Signed-off-by Li Zhe <[email protected]>---
v2: https://lore.kernel.org/all/[email protected]/#t
v1: https://lore.kernel.org/all/[email protected]/Changes since v2:
- Dropped the separate cover letter and shortened/refocused the commit message, as suggested by Joshua Hahn and Gregory Price.
- Added an eight-cgroup CXL test summary under background reclaim/demotion pressure, as suggested by Joshua Hahn.--- Documentation/admin-guide/cgroup-v2.rst | 11 ++++++ mm/bpf_memcontrol.c | 3 +- mm/internal.h | 3 +- mm/memcontrol-v1.c | 6 ++- mm/memcontrol.c | 11 ++++-- mm/vmscan.c | 49 +++++++++++++++++++++++-- 6 files changed, 72 insertions(+), 11 deletions(-)Documentation/admin-guide/cgroup-v2.rst +11
-0@@ -1458,6 +1458,7 @@ The following nested keys are defined. ========== ================================ swappiness Swappiness value to reclaim with + source Source to apply reclaim pressure to ========== ================================ Specifying a swappiness value instructs the kernel to perform@@ -1468,6 +1469,16 @@ The following nested keys are defined. The valid range for swappiness is [0-200, max], setting swappiness=max exclusively reclaims anonymous memory. + Specifying a source value constrains where proactive reclaim applies + pressure. Currently only source=nodes:<nodelist> is supported. For + example:: + + echo "1G source=nodes:0-1" > memory.reclaim + + This applies proactive reclaim pressure to memory of the target cgroup + resident on nodes 0-1. It does not constrain demotion targets and does + not change memory.reclaim completion accounting. + memory.peak A read-write single value file which exists on non-root cgroups.mm/bpf_memcontrol.c +2
-1@@ -201,7 +201,8 @@ __bpf_kfunc long bpf_proactive_reclaim(struct mem_cgroup *memcg, nr_reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages,GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP |MEMCG_RECLAIM_PROACTIVE , - swappiness ==-1 ?NULL : &swappiness); + swappiness ==-1 ?NULL : &swappiness, +NULL );return nr_reclaimed *PAGE_SIZE ; }mm/internal.h +2
-1@@ -83,7 +83,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness); + int *swappiness, + const nodemask_t *source_nodes);#ifdef CONFIG_NUMA extern int sysctl_min_unmapped_ratio;mm/memcontrol-v1.c +4
-2@@ -1464,7 +1464,8 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg, }if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , - memsw ?0 :MEMCG_RECLAIM_MAY_SWAP ,NULL )) { + memsw ?0 :MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) { ret = -EBUSY ;break ; }@@ -1500,7 +1501,8 @@ static int mem_cgroup_force_empty(struct mem_cgroup *memcg)break ;if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , -MEMCG_RECLAIM_MAY_SWAP ,NULL )) +MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_retries--; }mm/memcontrol.c +7
-4@@ -2444,7 +2444,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg, nr_reclaimed +=try_to_free_mem_cgroup_pages (memcg, nr_pages, gfp_mask,MEMCG_RECLAIM_MAY_SWAP , -NULL ); +NULL ,NULL );psi_memstall_leave (&pflags); }while ((memcg =parent_mem_cgroup (memcg)) && !mem_cgroup_is_root (memcg));@@ -2774,7 +2774,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,psi_memstall_enter (&pflags); nr_reclaimed =try_to_free_mem_cgroup_pages (mem_over_limit, nr_pages, - gfp_mask, reclaim_options,NULL ); + gfp_mask, reclaim_options, +NULL ,NULL );psi_memstall_leave (&pflags);if (mem_cgroup_margin (mem_over_limit) >= nr_pages)@@ -5001,7 +5002,8 @@ static ssize_t memory_high_write(struct kernfs_open_file *of, } reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages - high, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL ); +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL );if (!reclaimed && !nr_retries--)break ;@@ -5060,7 +5062,8 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,if (nr_reclaims) {if (!try_to_free_mem_cgroup_pages (memcg, nr_pages - max, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL )) +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_reclaims--;continue ; }mm/vmscan.c +46
-3@@ -7013,12 +7013,14 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness) + int *swappiness, + const nodemask_t *source_nodes) { unsigned long nr_reclaimed;unsigned int noreclaim_flag; struct scan_control sc = { .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), + .nodemask = source_nodes, .proactive_swappiness = swappiness, .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),@@ -7054,7 +7056,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness) + int *swappiness, + const nodemask_t *source_nodes) { return 0; }@@ -8098,20 +8101,51 @@ static unsigned long __node_reclaim(struct pglist_data *pgdat,enum {MEMORY_RECLAIM_SWAPPINESS =0 ,MEMORY_RECLAIM_SWAPPINESS_MAX , +MEMORY_RECLAIM_SOURCE ,MEMORY_RECLAIM_NULL , };static const match_table_t tokens = { {MEMORY_RECLAIM_SWAPPINESS ,"swappiness=%d" }, {MEMORY_RECLAIM_SWAPPINESS_MAX ,"swappiness=max" }, + {MEMORY_RECLAIM_SOURCE ,"source=%s" }, {MEMORY_RECLAIM_NULL ,NULL }, }; +#define MEMORY_RECLAIM_SOURCE_NODES "nodes:" +#define MEMORY_RECLAIM_SOURCE_NODES_LEN (sizeof(MEMORY_RECLAIM_SOURCE_NODES) - 1) + +static int parse_memory_reclaim_source (const char *source, +nodemask_t *source_nodes) +{ +const char *nodes; + +if (strncmp (source,MEMORY_RECLAIM_SOURCE_NODES , +MEMORY_RECLAIM_SOURCE_NODES_LEN )) +return -EINVAL ; + + nodes = source +MEMORY_RECLAIM_SOURCE_NODES_LEN ; +if (!*nodes) +return -EINVAL ; + +nodes_clear (*source_nodes); +if (nodelist_parse (nodes, *source_nodes)) +return -EINVAL ; + +nodes_and (*source_nodes, *source_nodes, node_states[N_MEMORY ]); +if (nodes_empty (*source_nodes)) +return -EINVAL ; + +return 0 ; +} +int user_proactive_reclaim (char *buf,struct mem_cgroup *memcg,pg_data_t *pgdat) {unsigned int nr_retries =MAX_RECLAIM_RETRIES ;unsigned long nr_to_reclaim, nr_reclaimed =0 ;int swappiness =-1 ; +nodemask_t source_nodes; +const nodemask_t *source_nodemask =NULL ;char *old_buf, *start;substring_t args[MAX_OPT_ARGS ];gfp_t gfp_mask =GFP_KERNEL ;@@ -8142,6 +8176,14 @@ int user_proactive_reclaim(char *buf,case MEMORY_RECLAIM_SWAPPINESS_MAX : swappiness =SWAPPINESS_ANON_ONLY ;break ; +case MEMORY_RECLAIM_SOURCE : +if (!memcg || source_nodemask) +return -EINVAL ; +if (parse_memory_reclaim_source (args[0 ].from, + &source_nodes)) +return -EINVAL ; + source_nodemask = &source_nodes; +break ;default :return -EINVAL ; }@@ -8181,7 +8223,8 @@ int user_proactive_reclaim(char *buf, reclaimed =try_to_free_mem_cgroup_pages (memcg, batch_size, gfp_mask, reclaim_options, - swappiness ==-1 ?NULL : &swappiness); + swappiness ==-1 ?NULL : &swappiness, + source_nodemask); }else {struct scan_control sc = { .gfp_mask = current_gfp_context(gfp_mask),Signature · 1 line
2.20.1
Quoted 34 lines · Joshua Hahn
Quoted 11 lines · Li Zhe
memory.reclaim can apply proactive reclaim to a memcg, but it cannot target source nodes the way global node reclaim can. Proactive memcg reclaim follows a fast-to-slow fallback order, which attempts demotion before reclaiming from slower tiers.
As a result, a memcg controller cannot decide whether reclaim should start from DRAM or from a slower tier. For a cgroup that is close to OOM and needs charge reduction, or for a controller trying to shape a workload's DRAM/CXL resident distribution, the default policy may spend reclaim effort on the wrong source tier.
Quoted 5 lines
Add source=nodes:<nodelist> to memory.reclaim. The selector only chooses the resident source nodes where the existing memcg reclaim path applies pressure. It does not select demotion targets, force demotion, force eviction, promote pages, or change completion accounting. Leaving source unspecified preserves existing behavior.
Hi Zhe,
Thanks for the patch and sorry that it took so long to reply. I've actually spent a lot of time thinking about this commit to see if it can be used to establish per-memcg-tier residency limits on machines running multiple workloads via memcgs.
So you could have a daemon running, checking the per-tier usage for a cgroup, and creating pressure to evict pages downward from that cgroup specifically.
So if a cgroup is using 10G more node0 (DRAM) than it should be, you can write "echo 10G source=nodes:0" > memory.reclaim to try and free up that space. I'm running some experiments locally and really thinking through this to see what that would lead to.
Quoted 8 lines
Anyways, to me the only concern that I have is what happens when that reclaim fails. Let's say that you have a situation where CXL is completely full, preventing future demtions. At the same time, we have a memcg that is using too much DRAM, so we try to reclaim from the DRAM node and pressure downwards.
The thing is, I think proactive reclaim doesn't allow swapouts on tiered systems.
Sorry, I had meant to add more context here. I meant that proactive reclaim doesn't allow swapouts directly. See6b426d071419 mm: disable top-tier fallback to reclaim on proactive reclaim for some more context.
Quoted 302 lines
So what would happen is, you would (1) fail demotions, and then (2) fail to swap out, which means the pages have nowhere to go. And repeatedly running the same command over and over again will not lead to any progress at all.
Quoted 10 lines
With this, userspace can build the policy it needs on top of existing kernel reclaim behavior:
1. start reclaim from lower-tier nodes to reduce the cgroup's memory usage more directly;
2. steer reclaim toward over-represented nodes to shape a workload's DRAM/CXL resident distribution; and
3. reclaim from lower-tier nodes first so later top-tier reclaim has room for demotion.Quoted 5 lines
Example: nodes 0-2=DRAM, node3=CXL
echo 1G > memory.reclaim # progress from nodes 0-3
echo "1G source=nodes:3" > memory.reclaim # reclaim from CXL echo "1G source=nodes:0-2" > memory.reclaim # usually demote to CXLAnd so I can see why you would have to run something like this in order to actually make room, moving from the bottom going up. Make space for the memory that's about to come in from the top.
To me this makes a lot of sense, and it's something that we (Meta) has thought about quite extensively internally as well (from Johannes and Gregory, who are also CC-ed in this mail).
But at the same time, this is not really an atomic operation. So memory usage between the two operations can change very rapidly. I wanted to start a conversation about this point and whether this is something that the commit should address. I would like to hone in on a use case and think about how the users should be expected to use these things first.
Thanks a lot Zhe, and sorry again for the delayed response. I hope you have a great day!
JoshuaQuoted 263 lines
The typed source= form also leaves room for future source selectors without changing the ABI introduced here.
Tested in a CXL setup with nodes 0-2 as DRAM and node 3 as CXL. A userspace controller steered eight cgroups toward a 50:50 DRAM:CXL resident ratio while a background workload created reclaim and demotion pressure. With source=nodes, all eight cgroups ended with 45%-55% of their memory on DRAM. Under the same pressure, the plain memory.reclaim baseline left the worst cgroup far away from the target range.
Signed-off-by Li Zhe <[email protected]>---
v2: https://lore.kernel.org/all/[email protected]/#t
v1: https://lore.kernel.org/all/[email protected]/Changes since v2:
- Dropped the separate cover letter and shortened/refocused the commit message, as suggested by Joshua Hahn and Gregory Price.
- Added an eight-cgroup CXL test summary under background reclaim/demotion pressure, as suggested by Joshua Hahn.--- Documentation/admin-guide/cgroup-v2.rst | 11 ++++++ mm/bpf_memcontrol.c | 3 +- mm/internal.h | 3 +- mm/memcontrol-v1.c | 6 ++- mm/memcontrol.c | 11 ++++-- mm/vmscan.c | 49 +++++++++++++++++++++++-- 6 files changed, 72 insertions(+), 11 deletions(-)Documentation/admin-guide/cgroup-v2.rst +11
-0@@ -1458,6 +1458,7 @@ The following nested keys are defined. ========== ================================ swappiness Swappiness value to reclaim with + source Source to apply reclaim pressure to ========== ================================ Specifying a swappiness value instructs the kernel to perform@@ -1468,6 +1469,16 @@ The following nested keys are defined. The valid range for swappiness is [0-200, max], setting swappiness=max exclusively reclaims anonymous memory. + Specifying a source value constrains where proactive reclaim applies + pressure. Currently only source=nodes:<nodelist> is supported. For + example:: + + echo "1G source=nodes:0-1" > memory.reclaim + + This applies proactive reclaim pressure to memory of the target cgroup + resident on nodes 0-1. It does not constrain demotion targets and does + not change memory.reclaim completion accounting. + memory.peak A read-write single value file which exists on non-root cgroups.mm/bpf_memcontrol.c +2
-1@@ -201,7 +201,8 @@ __bpf_kfunc long bpf_proactive_reclaim(struct mem_cgroup *memcg, nr_reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages,GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP |MEMCG_RECLAIM_PROACTIVE , - swappiness ==-1 ?NULL : &swappiness); + swappiness ==-1 ?NULL : &swappiness, +NULL );return nr_reclaimed *PAGE_SIZE ; }mm/internal.h +2
-1@@ -83,7 +83,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness); + int *swappiness, + const nodemask_t *source_nodes);#ifdef CONFIG_NUMA extern int sysctl_min_unmapped_ratio;mm/memcontrol-v1.c +4
-2@@ -1464,7 +1464,8 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg, }if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , - memsw ?0 :MEMCG_RECLAIM_MAY_SWAP ,NULL )) { + memsw ?0 :MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) { ret = -EBUSY ;break ; }@@ -1500,7 +1501,8 @@ static int mem_cgroup_force_empty(struct mem_cgroup *memcg)break ;if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , -MEMCG_RECLAIM_MAY_SWAP ,NULL )) +MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_retries--; }mm/memcontrol.c +7
-4@@ -2444,7 +2444,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg, nr_reclaimed +=try_to_free_mem_cgroup_pages (memcg, nr_pages, gfp_mask,MEMCG_RECLAIM_MAY_SWAP , -NULL ); +NULL ,NULL );psi_memstall_leave (&pflags); }while ((memcg =parent_mem_cgroup (memcg)) && !mem_cgroup_is_root (memcg));@@ -2774,7 +2774,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,psi_memstall_enter (&pflags); nr_reclaimed =try_to_free_mem_cgroup_pages (mem_over_limit, nr_pages, - gfp_mask, reclaim_options,NULL ); + gfp_mask, reclaim_options, +NULL ,NULL );psi_memstall_leave (&pflags);if (mem_cgroup_margin (mem_over_limit) >= nr_pages)@@ -5001,7 +5002,8 @@ static ssize_t memory_high_write(struct kernfs_open_file *of, } reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages - high, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL ); +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL );if (!reclaimed && !nr_retries--)break ;@@ -5060,7 +5062,8 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,if (nr_reclaims) {if (!try_to_free_mem_cgroup_pages (memcg, nr_pages - max, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL )) +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_reclaims--;continue ; }mm/vmscan.c +46
-3@@ -7013,12 +7013,14 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness) + int *swappiness, + const nodemask_t *source_nodes) { unsigned long nr_reclaimed;unsigned int noreclaim_flag; struct scan_control sc = { .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), + .nodemask = source_nodes, .proactive_swappiness = swappiness, .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),@@ -7054,7 +7056,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness) + int *swappiness, + const nodemask_t *source_nodes) { return 0; }@@ -8098,20 +8101,51 @@ static unsigned long __node_reclaim(struct pglist_data *pgdat,enum {MEMORY_RECLAIM_SWAPPINESS =0 ,MEMORY_RECLAIM_SWAPPINESS_MAX , +MEMORY_RECLAIM_SOURCE ,MEMORY_RECLAIM_NULL , };static const match_table_t tokens = { {MEMORY_RECLAIM_SWAPPINESS ,"swappiness=%d" }, {MEMORY_RECLAIM_SWAPPINESS_MAX ,"swappiness=max" }, + {MEMORY_RECLAIM_SOURCE ,"source=%s" }, {MEMORY_RECLAIM_NULL ,NULL }, }; +#define MEMORY_RECLAIM_SOURCE_NODES "nodes:" +#define MEMORY_RECLAIM_SOURCE_NODES_LEN (sizeof(MEMORY_RECLAIM_SOURCE_NODES) - 1) + +static int parse_memory_reclaim_source (const char *source, +nodemask_t *source_nodes) +{ +const char *nodes; + +if (strncmp (source,MEMORY_RECLAIM_SOURCE_NODES , +MEMORY_RECLAIM_SOURCE_NODES_LEN )) +return -EINVAL ; + + nodes = source +MEMORY_RECLAIM_SOURCE_NODES_LEN ; +if (!*nodes) +return -EINVAL ; + +nodes_clear (*source_nodes); +if (nodelist_parse (nodes, *source_nodes)) +return -EINVAL ; + +nodes_and (*source_nodes, *source_nodes, node_states[N_MEMORY ]); +if (nodes_empty (*source_nodes)) +return -EINVAL ; + +return 0 ; +} +int user_proactive_reclaim (char *buf,struct mem_cgroup *memcg,pg_data_t *pgdat) {unsigned int nr_retries =MAX_RECLAIM_RETRIES ;unsigned long nr_to_reclaim, nr_reclaimed =0 ;int swappiness =-1 ; +nodemask_t source_nodes; +const nodemask_t *source_nodemask =NULL ;char *old_buf, *start;substring_t args[MAX_OPT_ARGS ];gfp_t gfp_mask =GFP_KERNEL ;@@ -8142,6 +8176,14 @@ int user_proactive_reclaim(char *buf,case MEMORY_RECLAIM_SWAPPINESS_MAX : swappiness =SWAPPINESS_ANON_ONLY ;break ; +case MEMORY_RECLAIM_SOURCE : +if (!memcg || source_nodemask) +return -EINVAL ; +if (parse_memory_reclaim_source (args[0 ].from, + &source_nodes)) +return -EINVAL ; + source_nodemask = &source_nodes; +break ;default :return -EINVAL ; }@@ -8181,7 +8223,8 @@ int user_proactive_reclaim(char *buf, reclaimed =try_to_free_mem_cgroup_pages (memcg, batch_size, gfp_mask, reclaim_options, - swappiness ==-1 ?NULL : &swappiness); + swappiness ==-1 ?NULL : &swappiness, + source_nodemask); }else {struct scan_control sc = { .gfp_mask = current_gfp_context(gfp_mask),Signature · 1 line
2.20.1
Quoted 17 lines · Joshua Hahn
Quoted 11 lines · Li Zhe
memory.reclaim can apply proactive reclaim to a memcg, but it cannot target source nodes the way global node reclaim can. Proactive memcg reclaim follows a fast-to-slow fallback order, which attempts demotion before reclaiming from slower tiers.
As a result, a memcg controller cannot decide whether reclaim should start from DRAM or from a slower tier. For a cgroup that is close to OOM and needs charge reduction, or for a controller trying to shape a workload's DRAM/CXL resident distribution, the default policy may spend reclaim effort on the wrong source tier.
Quoted 5 lines
Add source=nodes:<nodelist> to memory.reclaim. The selector only chooses the resident source nodes where the existing memcg reclaim path applies pressure. It does not select demotion targets, force demotion, force eviction, promote pages, or change completion accounting. Leaving source unspecified preserves existing behavior.
Quoted 6 lines
Hi Zhe,
Thanks for the patch and sorry that it took so long to reply. I've actually spent a lot of time thinking about this commit to see if it can be used to establish per-memcg-tier residency limits on machines running multiple workloads via memcgs.
Hi Joshua,
Thanks for the careful review and for thinking through this use case.
I am also interested in using this for per-memcg-tier residency control. My current understanding is that source=nodes can support soft, feedback-driven per-memcg-tier residency limit control, not strict kernel-enforced per-tier limits. Strict limits would need stronger kernel enforcement or reservation, while this patch only exposes a way to choose the source of reclaim pressure. I think such soft limits are still useful for many practical placement controllers.
Quoted 4 lines
So you could have a daemon running, checking the per-tier usage for a cgroup, and creating pressure to evict pages downward from that cgroup specifically.
Yes, this is one of the intended use cases.
The contract I have in mind is intentionally narrow: source=nodes says where reclaim pressure is applied. It does not say where reclaimed folios must go, reserve target-tier space, or turn memory.reclaim into a placement transaction.
Quoted 6 lines
So if a cgroup is using 10G more node0 (DRAM) than it should be, you can write "echo 10G source=nodes:0" > memory.reclaim to try and free up that space. I'm running some experiments locally and really thinking through this to see what that would lead to.
Quoted 8 lines
Anyways, to me the only concern that I have is what happens when that reclaim fails. Let's say that you have a situation where CXL is completely full, preventing future demtions. At the same time, we have a memcg that is using too much DRAM, so we try to reclaim from the DRAM node and pressure downwards.
The thing is, I think proactive reclaim doesn't allow swapouts on tiered systems.
Thanks for clarifying this in the follow-up. If I understand correctly, this is about top-tier proactive reclaim: when demotion is possible but fails, proactive reclaim does not retry those folios for direct reclaim/swapout. That is different from proactive reclaim on a lower or terminal tier, where there is no further demotion target and reclaim can still free pages and swap anon when swap is available.
Quoted 5 lines
So what would happen is, you would (1) fail demotions, and then (2) fail to swap out, which means the pages have nowhere to go. And repeatedly running the same command over and over again will not lead to any progress at all.
Right. I think that is a valid failure mode of the existing proactive reclaim policy. If top-tier reclaim makes no progress because the demotion target is full, that is feedback to userspace: reclaim lower-tier memory first. This means the daemon needs to consider available or reclaimable headroom across tiers, not only the overage on one source node.
For example, if there is reclaimable memory on the CXL tier:
echo "10G source=nodes:<cxl_node>" > memory.reclaim # create room echo "10G source=nodes:<dram_node>" > memory.reclaim # pressure DRAM
The first request creates room on the lower tier. If that succeeds, the second request has a demotion target and can make progress.
Quoted 24 lines
Quoted 11 lines
With this, userspace can build the policy it needs on top of existing kernel reclaim behavior:
1. start reclaim from lower-tier nodes to reduce the cgroup's memory
usage more directly;2. steer reclaim toward over-represented nodes to shape a workload's DRAM/CXL resident distribution; and
3. reclaim from lower-tier nodes first so later top-tier reclaim has room for demotion.Quoted 5 lines
Example: nodes 0-2=DRAM, node3=CXL
echo 1G > memory.reclaim # progress from nodes 0-3
echo "1G source=nodes:3" > memory.reclaim # reclaim from CXL echo "1G source=nodes:0-2" > memory.reclaim # usually demote to CXLAnd so I can see why you would have to run something like this in order to actually make room, moving from the bottom going up. Make space for the memory that's about to come in from the top.
To me this makes a lot of sense, and it's something that we (Meta) has thought about quite extensively internally as well (from Johannes and Gregory, who are also CC-ed in this mail).
Quoted 5 lines
But at the same time, this is not really an atomic operation. So memory usage between the two operations can change very rapidly. I wanted to start a conversation about this point and whether this is something that the commit should address. I would like to hone in on a use case and think about how the users should be expected to use these things first.
I agree this is not an atomic operation. I do not think this patch should try to make a multi-step placement policy atomic inside memory.reclaim. The existing interface is already a best-effort pressure interface:
userspace asks the kernel to reclaim some amount, observes what actually changed, and then decides the next step.
So my expected use model is a userspace feedback loop, mainly for reducing a cgroup's memory usage from selected source nodes and shaping the workload's DRAM/CXL resident distribution. I am happy to keep discussing whether this model is sufficient, and what other use cases or limitations the interface should consider.
Thanks again for the review and for thinking through the use case.
Thanks,
Zhe
Quoted 268 lines
Thanks a lot Zhe, and sorry again for the delayed response. I hope you have a great day!
JoshuaQuoted 263 lines
The typed source= form also leaves room for future source selectors without changing the ABI introduced here.
Tested in a CXL setup with nodes 0-2 as DRAM and node 3 as CXL. A userspace controller steered eight cgroups toward a 50:50 DRAM:CXL resident ratio while a background workload created reclaim and demotion pressure. With source=nodes, all eight cgroups ended with 45%-55% of their memory on DRAM. Under the same pressure, the plain memory.reclaim baseline left the worst cgroup far away from the target range.
Signed-off-by Li Zhe <[email protected]>---
v2: https://lore.kernel.org/all/[email protected]/#t
v1: https://lore.kernel.org/all/[email protected]/Changes since v2:
- Dropped the separate cover letter and shortened/refocused the commit message, as suggested by Joshua Hahn and Gregory Price.
- Added an eight-cgroup CXL test summary under background reclaim/demotion pressure, as suggested by Joshua Hahn.--- Documentation/admin-guide/cgroup-v2.rst | 11 ++++++ mm/bpf_memcontrol.c | 3 +- mm/internal.h | 3 +- mm/memcontrol-v1.c | 6 ++- mm/memcontrol.c | 11 ++++-- mm/vmscan.c | 49 +++++++++++++++++++++++-- 6 files changed, 72 insertions(+), 11 deletions(-)Documentation/admin-guide/cgroup-v2.rst +11
-0@@ -1458,6 +1458,7 @@ The following nested keys are defined. ========== ================================ swappiness Swappiness value to reclaim with + source Source to apply reclaim pressure to ========== ================================ Specifying a swappiness value instructs the kernel to perform@@ -1468,6 +1469,16 @@ The following nested keys are defined. The valid range for swappiness is [0-200, max], setting swappiness=max exclusively reclaims anonymous memory. + Specifying a source value constrains where proactive reclaim applies + pressure. Currently only source=nodes:<nodelist> is supported. For + example:: + + echo "1G source=nodes:0-1" > memory.reclaim + + This applies proactive reclaim pressure to memory of the target cgroup + resident on nodes 0-1. It does not constrain demotion targets and does + not change memory.reclaim completion accounting. + memory.peak A read-write single value file which exists on non-root cgroups.mm/bpf_memcontrol.c +2
-1@@ -201,7 +201,8 @@ __bpf_kfunc long bpf_proactive_reclaim(struct mem_cgroup *memcg, nr_reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages,GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP |MEMCG_RECLAIM_PROACTIVE , - swappiness ==-1 ?NULL : &swappiness); + swappiness ==-1 ?NULL : &swappiness, +NULL );return nr_reclaimed *PAGE_SIZE ; }mm/internal.h +2
-1@@ -83,7 +83,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness); + int *swappiness, + const nodemask_t *source_nodes);#ifdef CONFIG_NUMA extern int sysctl_min_unmapped_ratio;mm/memcontrol-v1.c +4
-2@@ -1464,7 +1464,8 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg, }if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , - memsw ?0 :MEMCG_RECLAIM_MAY_SWAP ,NULL )) { + memsw ?0 :MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) { ret = -EBUSY ;break ; }@@ -1500,7 +1501,8 @@ static int mem_cgroup_force_empty(struct mem_cgroup *memcg)break ;if (!try_to_free_mem_cgroup_pages (memcg,1 ,GFP_KERNEL , -MEMCG_RECLAIM_MAY_SWAP ,NULL )) +MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_retries--; }mm/memcontrol.c +7
-4@@ -2444,7 +2444,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg, nr_reclaimed +=try_to_free_mem_cgroup_pages (memcg, nr_pages, gfp_mask,MEMCG_RECLAIM_MAY_SWAP , -NULL ); +NULL ,NULL );psi_memstall_leave (&pflags); }while ((memcg =parent_mem_cgroup (memcg)) && !mem_cgroup_is_root (memcg));@@ -2774,7 +2774,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,psi_memstall_enter (&pflags); nr_reclaimed =try_to_free_mem_cgroup_pages (mem_over_limit, nr_pages, - gfp_mask, reclaim_options,NULL ); + gfp_mask, reclaim_options, +NULL ,NULL );psi_memstall_leave (&pflags);if (mem_cgroup_margin (mem_over_limit) >= nr_pages)@@ -5001,7 +5002,8 @@ static ssize_t memory_high_write(struct kernfs_open_file *of, } reclaimed =try_to_free_mem_cgroup_pages (memcg, nr_pages - high, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL ); +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL );if (!reclaimed && !nr_retries--)break ;@@ -5060,7 +5062,8 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,if (nr_reclaims) {if (!try_to_free_mem_cgroup_pages (memcg, nr_pages - max, -GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP ,NULL )) +GFP_KERNEL ,MEMCG_RECLAIM_MAY_SWAP , +NULL ,NULL )) nr_reclaims--;continue ; }mm/vmscan.c +46
-3@@ -7013,12 +7013,14 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness) + int *swappiness, + const nodemask_t *source_nodes) { unsigned long nr_reclaimed;unsigned int noreclaim_flag; struct scan_control sc = { .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), + .nodemask = source_nodes, .proactive_swappiness = swappiness, .gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),@@ -7054,7 +7056,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,unsigned long nr_pages, gfp_t gfp_mask, unsigned int reclaim_options, - int *swappiness) + int *swappiness, + const nodemask_t *source_nodes) { return 0; }@@ -8098,20 +8101,51 @@ static unsigned long __node_reclaim(struct pglist_data *pgdat,enum {MEMORY_RECLAIM_SWAPPINESS =0 ,MEMORY_RECLAIM_SWAPPINESS_MAX , +MEMORY_RECLAIM_SOURCE ,MEMORY_RECLAIM_NULL , };static const match_table_t tokens = { {MEMORY_RECLAIM_SWAPPINESS ,"swappiness=%d" }, {MEMORY_RECLAIM_SWAPPINESS_MAX ,"swappiness=max" }, + {MEMORY_RECLAIM_SOURCE ,"source=%s" }, {MEMORY_RECLAIM_NULL ,NULL }, }; +#define MEMORY_RECLAIM_SOURCE_NODES "nodes:" +#define MEMORY_RECLAIM_SOURCE_NODES_LEN (sizeof(MEMORY_RECLAIM_SOURCE_NODES) - 1) + +static int parse_memory_reclaim_source (const char *source, +nodemask_t *source_nodes) +{ +const char *nodes; + +if (strncmp (source,MEMORY_RECLAIM_SOURCE_NODES , +MEMORY_RECLAIM_SOURCE_NODES_LEN )) +return -EINVAL ; + + nodes = source +MEMORY_RECLAIM_SOURCE_NODES_LEN ; +if (!*nodes) +return -EINVAL ; + +nodes_clear (*source_nodes); +if (nodelist_parse (nodes, *source_nodes)) +return -EINVAL ; + +nodes_and (*source_nodes, *source_nodes, node_states[N_MEMORY ]); +if (nodes_empty (*source_nodes)) +return -EINVAL ; + +return 0 ; +} +int user_proactive_reclaim (char *buf,struct mem_cgroup *memcg,pg_data_t *pgdat) {unsigned int nr_retries =MAX_RECLAIM_RETRIES ;unsigned long nr_to_reclaim, nr_reclaimed =0 ;int swappiness =-1 ; +nodemask_t source_nodes; +const nodemask_t *source_nodemask =NULL ;char *old_buf, *start;substring_t args[MAX_OPT_ARGS ];gfp_t gfp_mask =GFP_KERNEL ;@@ -8142,6 +8176,14 @@ int user_proactive_reclaim(char *buf,case MEMORY_RECLAIM_SWAPPINESS_MAX : swappiness =SWAPPINESS_ANON_ONLY ;break ; +case MEMORY_RECLAIM_SOURCE : +if (!memcg || source_nodemask) +return -EINVAL ; +if (parse_memory_reclaim_source (args[0 ].from, + &source_nodes)) +return -EINVAL ; + source_nodemask = &source_nodes; +break ;default :return -EINVAL ; }@@ -8181,7 +8223,8 @@ int user_proactive_reclaim(char *buf, reclaimed =try_to_free_mem_cgroup_pages (memcg, batch_size, gfp_mask, reclaim_options, - swappiness ==-1 ?NULL : &swappiness); + swappiness ==-1 ?NULL : &swappiness, + source_nodemask); }else {struct scan_control sc = { .gfp_mask = current_gfp_context(gfp_mask),Signature · 1 line
2.20.1