Skip to contentSkip to conversation

Add swappiness argument to memory.reclaim

PATCH New Dan Schatzberg 5 participants 13 messages Dec 13, 2023 – Dec 15, 2023

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v4 cover Add swappiness argument to memory.reclaim

Changes since V3:
* Added #define for MIN_SWAPPINESS and MAX_SWAPPINESS
* Added explicit calls to mem_cgroup_swappiness

Changes since V2:
* No functional change
* Used int consistently rather than a pointer

Changes since V1:
* Added documentation

This patch proposes augmenting the memory.reclaim interface with a swappiness=<val> argument that overrides the swappiness value for that instance of proactive reclaim.

Userspace proactive reclaimers use the memory.reclaim interface to trigger reclaim. The memory.reclaim interface does not allow for any way to effect the balance of file vs anon during proactive reclaim. The only approach is to adjust the vm.swappiness setting. However, there are a few reasons we look to control the balance of file vs anon during proactive reclaim, separately from reactive reclaim:

* Swapout should be limited to manage SSD write endurance. In near-OOM situations we are fine with lots of swap-out to avoid OOMs. As these are typically rare events, they have relatively little impact on write endurance. However, proactive reclaim runs continuously and so its impact on SSD write endurance is more significant. Therefore it is desireable to control swap-out for proactive reclaim separately from reactive reclaim

* Some userspace OOM killers like systemd-oomd[1] support OOM killing on swap exhaustion. This makes sense if the swap exhaustion is triggered due to reactive reclaim but less so if it is triggered due to proactive reclaim (e.g. one could see OOMs when free memory is ample but anon is just particularly cold). Therefore, it's desireable to have proactive reclaim reduce or stop swap-out before the threshold at which OOM killing occurs.

In the case of Meta's Senpai proactive reclaimer, we adjust vm.swappiness before writes to memory.reclaim[2]. This has been in production for nearly two years and has addressed our needs to control proactive vs reactive reclaim behavior but is still not ideal for a number of reasons:

* vm.swappiness is a global setting, adjusting it can race/interfere with other system administration that wishes to control vm.swappiness. In our case, we need to disable Senpai before adjusting vm.swappiness.

* vm.swappiness is stateful - so a crash or restart of Senpai can leave a misconfigured setting. This requires some additional management to record the "desired" setting and ensure Senpai always adjusts to it.

With this patch, we avoid these downsides of adjusting vm.swappiness globally.

Previously, this exact interface addition was proposed by Yosry[3]. In response, Roman proposed instead an interface to specify precise file/anon/slab reclaim amounts[4]. More recently Huan also proposed this as well[5] and others similarly questioned if this was the proper interface.

Previous proposals sought to use this to allow proactive reclaimers to effectively perform a custom reclaim algorithm by issuing proactive reclaim with different settings to control file vs anon reclaim (e.g. to only reclaim anon from some applications). Responses argued that adjusting swappiness is a poor interface for custom reclaim.

In contrast, I argue in favor of a swappiness setting not as a way to implement custom reclaim algorithms but rather to bias the balance of anon vs file due to differences of proactive vs reactive reclaim. In this context, swappiness is the existing interface for controlling this balance and this patch simply allows for it to be configured differently for proactive vs reactive reclaim.

Specifying explicit amounts of anon vs file pages to reclaim feels inappropriate for this prupose. Proactive reclaimers are un-aware of the relative age of file vs anon for a cgroup which makes it difficult to manage proactive reclaim of different memory pools. A proactive reclaimer would need some amount of anon reclaim attempts separate from the amount of file reclaim attempts which seems brittle given that it's difficult to observe the impact.

[1]https://www.freedesktop.org/software/systemd/man/latest/systemd-oomd.service.html [2]https://github.com/facebookincubator/oomd/blob/main/src/oomd/plugins/Senpai.cpp#L585-L598 [3]https://lore.kernel.org/linux-mm/CAJD7tkbDpyoODveCsnaqBBMZEkDvshXJmNdbk51yKSNgD7aGdg@mail.gmail.com/ [4]https://lore.kernel.org/linux-mm/YoPHtHXzpK51F%2F1Z@carbon/ [5]https://lore.kernel.org/lkml/[email protected]/

Dan Schatzberg (2):
mm: add defines for min/max swappiness
mm: add swapiness= arg to memory.reclaim

 Documentation/admin-guide/cgroup-v2.rst | 19 +++++---
 include/linux/swap.h                    |  5 +-
 mm/memcontrol.c                         | 63 ++++++++++++++++++++-----
 mm/vmscan.c                             | 21 +++++----
 4 files changed, 80 insertions(+), 28 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/2 mm: add defines for min/max swappiness

We use the constants 0 and 200 in a few places in the mm code when referring to the min and max swappiness. This patch adds MIN_SWAPPINESS and MAX_SWAPPINESS #defines to improve clarity. There are no functional changes.

---
 include/linux/swap.h |  2 ++
 mm/memcontrol.c      |  2 +-
 mm/vmscan.c          | 10 +++++-----
 3 files changed, 8 insertions(+), 6 deletions(-)
include/linux/swap.h +2 -0
index f6dd6575b905..e2ab76c25b4a 100644
@@ -407,6 +407,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 
 #define MEMCG_RECLAIM_MAY_SWAP (1 << 1)
 #define MEMCG_RECLAIM_PROACTIVE (1 << 2)
+#define MIN_SWAPPINESS 0
+#define MAX_SWAPPINESS 200
 extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 						  unsigned long nr_pages,
 						  gfp_t gfp_mask,
mm/memcontrol.c +1 -1
index 1c1061df9cd1..9748a6b88bb8 100644
@@ -4337,7 +4337,7 @@ static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(css);
 
-	if (val > 200)
+	if (val > MAX_SWAPPINESS)
 		return -EINVAL;
 
 	if (!mem_cgroup_is_root(memcg))
mm/vmscan.c +5 -5
index 506f8220c5fe..2aa671fe938b 100644
@@ -2403,7 +2403,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 	ap = swappiness * (total_cost + 1);
 	ap /= anon_cost + 1;
 
-	fp = (200 - swappiness) * (total_cost + 1);
+	fp = (MAX_SWAPPINESS - swappiness) * (total_cost + 1);
 	fp /= file_cost + 1;
 
 	fraction[0] = ap;
@@ -4400,7 +4400,7 @@ static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx
 {
 	int type, tier;
 	struct ctrl_pos sp, pv;
-	int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness };
+	int gain[ANON_AND_FILE] = { swappiness, MAX_SWAPPINESS - swappiness };
 
 	/*
 	 * Compare the first tier of anon with that of file to determine which
@@ -4444,7 +4444,7 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw
 		type = LRU_GEN_ANON;
 	else if (swappiness == 1)
 		type = LRU_GEN_FILE;
-	else if (swappiness == 200)
+	else if (swappiness == MAX_SWAPPINESS)
 		type = LRU_GEN_ANON;
 	else
 		type = get_type_to_scan(lruvec, swappiness, &tier);
@@ -5368,9 +5368,9 @@ static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq,
 
 	lruvec = get_lruvec(memcg, nid);
 
-	if (swappiness < 0)
+	if (swappiness < MIN_SWAPPINESS)
 		swappiness = get_swappiness(lruvec, sc);
-	else if (swappiness > 200)
+	else if (swappiness > MAX_SWAPPINESS)
 		goto done;
 
 	switch (cmd) {
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH V4 1/2] mm: add defines for min/max swappiness

Quoted 19 lines · Dan Schatzberg

[????????? [email protected] ????????? https://aka.ms/LearnAboutSenderIdentification,????????????]

We use the constants 0 and 200 in a few places in the mm code when referring to the min and max swappiness. This patch adds MIN_SWAPPINESS and MAX_SWAPPINESS #defines to improve clarity. There are no functional changes.

---
  include/linux/swap.h |  2 ++
  mm/memcontrol.c      |  2 +-
  mm/vmscan.c          | 10 +++++-----
  3 files changed, 8 insertions(+), 6 deletions(-)
include/linux/swap.h +0 -0
index f6dd6575b905..e2ab76c25b4a 100644

include/linux/swap.h extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order, · line 411

Quoted 5 lines
include/linux/swap.h +2 -0
@@ -407,6 +407,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 
  #define MEMCG_RECLAIM_MAY_SWAP (1 << 1)
  #define MEMCG_RECLAIM_PROACTIVE (1 << 2)
+#define MIN_SWAPPINESS 0
+#define MAX_SWAPPINESS 200

Do MAX_SWAPPINESS apply for all swapppiness? If so, maybe better change swappiness sysctl define:

```
sysctl.c:

{
        .procname    = "swappiness",
        .data        = &vm_swappiness,
        .maxlen        = sizeof(vm_swappiness),
        .mode        = 0644,
        .proc_handler    = proc_dointvec_minmax,
        .extra1        = SYSCTL_ZERO,
        .extra2        = SYSCTL_TWO_HUNDRED,
    },

```

Here hard code swappiness in [0, 200], and now add a new define.

And many other code hard reference 200 into max value of swappiness, like:

```
memcontrol.c:
static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
                       struct cftype *cft, u64 val)
{
    struct mem_cgroup *memcg = mem_cgroup_from_css(css);

    if (val > 200)
        return -EINVAL;

    if (!mem_cgroup_is_root(memcg))
        memcg->swappiness = val;
    else
        vm_swappiness = val;

    return 0;
}
vmscan.c:

/*
 * From 0 .. 200.  Higher means more swappy.
 */
int vm_swappiness = 60;

```
Quoted 62 lines
include/linux/swap.h +0 -0
  extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
                                                   unsigned long nr_pages,
                                                   gfp_t gfp_mask,
mm/memcontrol.c +1 -1
index 1c1061df9cd1..9748a6b88bb8 100644
@@ -4337,7 +4337,7 @@ static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
  {
         struct mem_cgroup *memcg = mem_cgroup_from_css(css);
 
-       if (val > 200)
+       if (val > MAX_SWAPPINESS)
                 return -EINVAL;
 
         if (!mem_cgroup_is_root(memcg))
mm/vmscan.c +5 -6
index 506f8220c5fe..2aa671fe938b 100644
@@ -2403,7 +2403,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
         ap = swappiness * (total_cost + 1);
         ap /= anon_cost + 1;
 
-       fp = (200 - swappiness) * (total_cost + 1);
+       fp = (MAX_SWAPPINESS - swappiness) * (total_cost + 1);
         fp /= file_cost + 1;
 
         fraction[0] = ap;
@@ -4400,7 +4400,7 @@ static int get_type_to_scan(struct lruvec *lruvec, int swappiness, int *tier_idx
  {
         int type, tier;
         struct ctrl_pos sp, pv;
-       int gain[ANON_AND_FILE] = { swappiness, 200 - swappiness };
+       int gain[ANON_AND_FILE] = { swappiness, MAX_SWAPPINESS - swappiness };
 
         /*
          * Compare the first tier of anon with that of file to determine which
@@ -4444,7 +4444,7 @@ static int isolate_folios(struct lruvec *lruvec, struct scan_control *sc, int sw
                 type = LRU_GEN_ANON;
         else if (swappiness == 1)
                 type = LRU_GEN_FILE;
-       else if (swappiness == 200)
+       else if (swappiness == MAX_SWAPPINESS)
                 type = LRU_GEN_ANON;
         else
                 type = get_type_to_scan(lruvec, swappiness, &tier);
@@ -5368,9 +5368,9 @@ static int run_cmd(char cmd, int memcg_id, int nid, unsigned long seq,
 
         lruvec = get_lruvec(memcg, nid);
 
-       if (swappiness < 0)
+       if (swappiness < MIN_SWAPPINESS)
                 swappiness = get_swappiness(lruvec, sc);
-       else if (swappiness > 200)
+       else if (swappiness > MAX_SWAPPINESS)
                 goto done;
 
         switch (cmd) {
--

2.34.1

#3 lore raw

Re: [PATCH V4 1/2] mm: add defines for min/max swappiness

Quoted 44 lines · Huan Yang
Quoted 19 lines · Dan Schatzberg

[????????? [email protected] ????????? https://aka.ms/LearnAboutSenderIdentification,????????????]

We use the constants 0 and 200 in a few places in the mm code when referring to the min and max swappiness. This patch adds MIN_SWAPPINESS and MAX_SWAPPINESS #defines to improve clarity. There are no functional changes.

---
  include/linux/swap.h |  2 ++
  mm/memcontrol.c      |  2 +-
  mm/vmscan.c          | 10 +++++-----
  3 files changed, 8 insertions(+), 6 deletions(-)
include/linux/swap.h +0 -0
index f6dd6575b905..e2ab76c25b4a 100644

include/linux/swap.h extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order, · line 411

Quoted 5 lines
include/linux/swap.h +2 -0
@@ -407,6 +407,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 
  #define MEMCG_RECLAIM_MAY_SWAP (1 << 1)
  #define MEMCG_RECLAIM_PROACTIVE (1 << 2)
+#define MIN_SWAPPINESS 0
+#define MAX_SWAPPINESS 200

Do MAX_SWAPPINESS apply for all swapppiness? If so, maybe better change swappiness sysctl define:

```
sysctl.c:

{
        .procname    = "swappiness",
        .data        = &vm_swappiness,
        .maxlen        = sizeof(vm_swappiness),
        .mode        = 0644,
        .proc_handler    = proc_dointvec_minmax,
        .extra1        = SYSCTL_ZERO,
        .extra2        = SYSCTL_TWO_HUNDRED,
    },

```
Quoted 1 line

Here hard code swappiness in [0, 200], and now add a new define.

Yes, MAX_SWAPPINESS is a hard limit. I'm not sure what you're proposing here - the SYSCTL_ZERO and SYSCTL_TWO_HUNDRED values are a little different than the defines I added. I think most of the value is just consistently using the defines in the core mm code.

Quoted 13 lines

And many other code hard reference 200 into max value of swappiness, like:

```
memcontrol.c:
static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css, struct cftype *cft, u64 val)
{
    struct mem_cgroup *memcg = mem_cgroup_from_css(css);

    if (val > 200)
        return -EINVAL;

Quoted 7 lines

    if (!mem_cgroup_is_root(memcg))
        memcg->swappiness = val;
    else
        vm_swappiness = val;

    return 0;
}

This one is already fixed in my patch.

#4 lore raw

Re: [PATCH V4 1/2] mm: add defines for min/max swappiness

Quoted 43 lines · Dan Schatzberg

[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 40 lines · Huan Yang
Quoted 19 lines · Dan Schatzberg

[????????? [email protected] ????????? https://aka.ms/LearnAboutSenderIdentification,????????????]

We use the constants 0 and 200 in a few places in the mm code when referring to the min and max swappiness. This patch adds MIN_SWAPPINESS and MAX_SWAPPINESS #defines to improve clarity. There are no functional changes.

---
   include/linux/swap.h |  2 ++
   mm/memcontrol.c      |  2 +-
   mm/vmscan.c          | 10 +++++-----
   3 files changed, 8 insertions(+), 6 deletions(-)
include/linux/swap.h +0 -0
index f6dd6575b905..e2ab76c25b4a 100644

include/linux/swap.h extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order, · line 411

Quoted 5 lines
include/linux/swap.h +2 -0
@@ -407,6 +407,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 
   #define MEMCG_RECLAIM_MAY_SWAP (1 << 1)
   #define MEMCG_RECLAIM_PROACTIVE (1 << 2)
+#define MIN_SWAPPINESS 0
+#define MAX_SWAPPINESS 200

Do MAX_SWAPPINESS apply for all swapppiness? If so, maybe better change swappiness sysctl define:
```
sysctl.c:

{
.procname = "swappiness",
.data = &vm_swappiness,
.maxlen = sizeof(vm_swappiness),
.mode = 0644,
.proc_handler = proc_dointvec_minmax,
.extra1 = SYSCTL_ZERO,
.extra2 = SYSCTL_TWO_HUNDRED,
},

Quoted 5 lines
Quoted 3 lines

```

Here hard code swappiness in [0, 200], and now add a new define.

Yes, MAX_SWAPPINESS is a hard limit. I'm not sure what you're proposing here - the SYSCTL_ZERO and SYSCTL_TWO_HUNDRED values are a

I mean, MAX_SWAPPINESS and SYSCTL_TWO_HUNDRED  all limit swappiness, but  are different definitions.

If swappiness change 200 into 400, you need to change here extra2 and your MAX_SWAPPINESS. It's wierd.

Quoted 23 lines

little different than the defines I added. I think most of the value is just consistently using the defines in the core mm code.

Quoted 6 lines

And many other code hard reference 200 into max value of swappiness, like:

```
memcontrol.c:
static int mem_cgroup_swappiness_write(struct cgroup_subsys_state *css,
struct cftype *cft, u64 val)

Quoted 13 lines
{
     struct mem_cgroup *memcg = mem_cgroup_from_css(css);

     if (val > 200)
         return -EINVAL;

     if (!mem_cgroup_is_root(memcg))
         memcg->swappiness = val;
     else
         vm_swappiness = val;

     return 0;
}

This one is already fixed in my patch.

#5 lore raw

PATCH v4 2/2 mm: add swapiness= arg to memory.reclaim

In reply to Dan Schatzberg

Allow proactive reclaimers to submit an additional swappiness=<val> argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt.

For example:

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

will perform reclaim on the rootcg with a swappiness setting of 0 (no swap) regardless of the vm.swappiness sysctl setting.

---
 Documentation/admin-guide/cgroup-v2.rst | 19 +++++---
 include/linux/swap.h                    |  3 +-
 mm/memcontrol.c                         | 61 ++++++++++++++++++++-----
 mm/vmscan.c                             | 11 +++--
 4 files changed, 72 insertions(+), 22 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +12 -7
index 3f85254f3cef..06319349c072 100644
@@ -1282,17 +1282,10 @@ PAGE_SIZE multiple when read back.
 	This is a simple interface to trigger memory reclaim in the
 	target cgroup.
 
-	This file accepts a single key, the number of bytes to reclaim.
-	No nested keys are currently supported.
-
 	Example::
 
 	  echo "1G" > memory.reclaim
 
-	The interface can be later extended with nested keys to
-	configure the reclaim behavior. For example, specify the
-	type of memory to reclaim from (anon, file, ..).
-
 	Please note that the kernel can over or under reclaim from
 	the target cgroup. If less bytes are reclaimed than the
 	specified amount, -EAGAIN is returned.
@@ -1304,6 +1297,18 @@ PAGE_SIZE multiple when read back.
 	This means that the networking layer will not adapt based on
 	reclaim induced by memory.reclaim.
 
+The following nested keys are defined.
+
+	  ==========		================================
+	  swappiness		Swappiness value to reclaim with
+	  ==========		================================
+
+	Specifying a swappiness value instructs the kernel to perform
+	the reclaim with that swappiness value. Note that this has the
+	same semantics as the vm.swappiness sysctl - it sets the
+	relative IO cost of reclaiming anon vs file memory but does
+	not allow for reclaiming specific amounts of anon or file memory.
+
   memory.peak
 	A read-only single value file which exists on non-root
 	cgroups.
include/linux/swap.h +2 -1
index e2ab76c25b4a..690898c347de 100644
@@ -412,7 +412,8 @@ extern unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
 extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 						  unsigned long nr_pages,
 						  gfp_t gfp_mask,
-						  unsigned int reclaim_options);
+						  unsigned int reclaim_options,
+						  int swappiness);
 extern unsigned long mem_cgroup_shrink_node(struct mem_cgroup *mem,
 						gfp_t gfp_mask, bool noswap,
 						pg_data_t *pgdat,
mm/memcontrol.c +50 -11

123 lines not on this page · raw message

mm/vmscan.c +8 -3
index 2aa671fe938b..cfef06c7c23f 100644
@@ -136,6 +136,9 @@ struct scan_control {
 	/* Always discard instead of demoting to lower tier memory */
 	unsigned int no_demotion:1;
 
+	/* Swappiness value for reclaim */
+	int swappiness;
+
 	/* Allocation order */
 	s8 order;
 
@@ -2327,7 +2330,7 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc,
 	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
 	struct mem_cgroup *memcg = lruvec_memcg(lruvec);
 	unsigned long anon_cost, file_cost, total_cost;
-	int swappiness = mem_cgroup_swappiness(memcg);
+	int swappiness = sc->swappiness;
 	u64 fraction[ANON_AND_FILE];
 	u64 denominator = 0;	/* gcc */
 	enum scan_balance scan_balance;
@@ -2608,7 +2611,7 @@ static int get_swappiness(struct lruvec *lruvec, struct scan_control *sc)
 	    mem_cgroup_get_nr_swap_pages(memcg) < MIN_LRU_BATCH)
 		return 0;
 
-	return mem_cgroup_swappiness(memcg);
+	return sc->swappiness;
 }
 
 static int get_nr_gens(struct lruvec *lruvec, int type)
@@ -6433,7 +6436,8 @@ unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
 unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 					   unsigned long nr_pages,
 					   gfp_t gfp_mask,
-					   unsigned int reclaim_options)
+					   unsigned int reclaim_options,
+					   int swappiness)
 {
 	unsigned long nr_reclaimed;
 	unsigned int noreclaim_flag;
@@ -6448,6 +6452,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 		.may_unmap = 1,
 		.may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP),
 		.proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE),
+		.swappiness = swappiness,
 	};
 	/*
 	 * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH V4 2/2] mm: add swapiness= arg to memory.reclaim

Quoted 5 lines · Dan Schatzberg

Allow proactive reclaimers to submit an additional swappiness=<val> argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt.

Quoted 8 lines

For example:

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

will perform reclaim on the rootcg with a swappiness setting of 0 (no swap) regardless of the vm.swappiness sysctl setting.

NAK.

Please initialize new variables properly and test code changes thoroughly before submission.

#7 lore raw

Re: [PATCH V4 2/2] mm: add swapiness= arg to memory.reclaim

Quoted 13 lines · Yu Zhao
Quoted 12 lines · Dan Schatzberg

Allow proactive reclaimers to submit an additional swappiness=<val> argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt.

For example:

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

will perform reclaim on the rootcg with a swappiness setting of 0 (no swap) regardless of the vm.swappiness sysctl setting.

Quoted 6 lines
Quoted 1 line

NAK.

Please initialize new variables properly and test code changes thoroughly before submission.

Could you be a bit more specific? The patch is compiling and working locally but perhaps there's some configuration or behavior that I haven't been testing.

#8 lore raw

Re: [PATCH V4 2/2] mm: add swapiness= arg to memory.reclaim

Quoted 17 lines · Dan Schatzberg
Quoted 15 lines · Yu Zhao
Quoted 13 lines · Dan Schatzberg

Allow proactive reclaimers to submit an additional swappiness=<val> argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt.

For example:

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

will perform reclaim on the rootcg with a swappiness setting of 0 (no swap) regardless of the vm.swappiness sysctl setting.

Quoted 8 lines
Quoted 4 lines

NAK.

Please initialize new variables properly and test code changes thoroughly before submission.

Could you be a bit more specific? The patch is compiling and working locally but perhaps there's some configuration or behavior that I haven't been testing.

scan_control.swappiness is only initialized from try_to_free_mem_cgroup_pages(), which means that swappiness is now 0 for all other types of reclaim, which can be a huge problem.

It might be easier to restore a special value (-1, 201, whatever) that means "use mem_cgroup_swappiness()".

#9 lore raw

Re: [PATCH V4 2/2] mm: add swapiness= arg to memory.reclaim

In reply to Dan Schatzberg
Quoted 3 lines · Dan Schatzberg

Allow proactive reclaimers to submit an additional swappiness=<val> argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt.

You are providing the usecase in the cover letter and Andrew usually appends that to the first patch in the series. I think it would be better to have the usecase described here.

[...]

Documentation/admin-guide/cgroup-v2.rst PAGE_SIZE multiple when read back. · line 1308

Quoted 13 lines
Documentation/admin-guide/cgroup-v2.rst +9 -0
@@ -1304,6 +1297,18 @@ PAGE_SIZE multiple when read back.
 	This means that the networking layer will not adapt based on
 	reclaim induced by memory.reclaim.
 
+The following nested keys are defined.
+
+	  ==========		================================
+	  swappiness		Swappiness value to reclaim with
+	  ==========		================================
+
+	Specifying a swappiness value instructs the kernel to perform
+	the reclaim with that swappiness value. Note that this has the
+	same semantics as the vm.swappiness sysctl - it sets the

same semantics as vm.swappiness applied to memcg reclaim with all the existing limitations and potential future extensions.

Documentation/admin-guide/cgroup-v2.rst PAGE_SIZE multiple when read back. · line 1314

Quoted 6 lines
Documentation/admin-guide/cgroup-v2.rst +3 -0
+	relative IO cost of reclaiming anon vs file memory but does
+	not allow for reclaiming specific amounts of anon or file memory.
+
   memory.peak
 	A read-only single value file which exists on non-root
 	cgroups.

The biggest problem with the implementation I can see, and others have pointed out the same, is how fragile this is. You really have to check the code and _every_ place which defines scan_control to learn that mem_cgroup_shrink_node, reclaim_clean_pages_from_list, reclaim_folio_list, lru_gen_seq_write, try_to_free_pages, balance_pgdat, shrink_all_memory and __node_reclaim. I have only checked couple of them, like direct reclaim and kswapd and none of them really sets up swappiness to the default memcg nor global value. So you effectively end up with swappiness == 0.

While the review can point those out it is quite easy to break and you will only learn about that very indirectly. I think it would be easier to review and maintain if you go with a pointer that would fallback to mem_cgroup_swappiness() if NULL which will be the case for every existing reclaimer except memory.reclaim with swappiness value.

Signature · 2 lines
Michal Hocko
SUSE Labs
#10 lore raw

Re: [PATCH V4 2/2] mm: add swapiness= arg to memory.reclaim

Quoted 14 lines · Michal Hocko
Quoted 3 lines · Dan Schatzberg

Allow proactive reclaimers to submit an additional swappiness=<val> argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt.

You are providing the usecase in the cover letter and Andrew usually appends that to the first patch in the series. I think it would be better to have the usecase described here.

[...]

Quoted 4 lines
Documentation/admin-guide/cgroup-v2.rst +0 -0
@@ -1304,6 +1297,18 @@ PAGE_SIZE multiple when read back.
 	This means that the networking layer will not adapt based on
 	reclaim induced by memory.reclaim.
 
Quoted 12 lines

Documentation/admin-guide/cgroup-v2.rst PAGE_SIZE multiple when read back. · line 1308

Quoted 9 lines
Documentation/admin-guide/cgroup-v2.rst +9 -0
+The following nested keys are defined.
+
+	  ==========		================================
+	  swappiness		Swappiness value to reclaim with
+	  ==========		================================
+
+	Specifying a swappiness value instructs the kernel to perform
+	the reclaim with that swappiness value. Note that this has the
+	same semantics as the vm.swappiness sysctl - it sets the

same semantics as vm.swappiness applied to memcg reclaim with all the existing limitations and potential future extensions.

Thanks, will make this change.

Quoted 18 lines

Documentation/admin-guide/cgroup-v2.rst PAGE_SIZE multiple when read back. · line 1314

Quoted 7 lines
Documentation/admin-guide/cgroup-v2.rst +3 -0
+	relative IO cost of reclaiming anon vs file memory but does
+	not allow for reclaiming specific amounts of anon or file memory.
+
   memory.peak
 	A read-only single value file which exists on non-root
 	cgroups.

The biggest problem with the implementation I can see, and others have pointed out the same, is how fragile this is. You really have to check the code and _every_ place which defines scan_control to learn that mem_cgroup_shrink_node, reclaim_clean_pages_from_list, reclaim_folio_list, lru_gen_seq_write, try_to_free_pages, balance_pgdat, shrink_all_memory and __node_reclaim. I have only checked couple of them, like direct reclaim and kswapd and none of them really sets up swappiness to the default memcg nor global value. So you effectively end up with swappiness == 0.

Quoted 5 lines

While the review can point those out it is quite easy to break and you will only learn about that very indirectly. I think it would be easier to review and maintain if you go with a pointer that would fallback to mem_cgroup_swappiness() if NULL which will be the case for every existing reclaimer except memory.reclaim with swappiness value.

I agree. My initial implementation used a pointer for this reason. I'll switch this back. Just to be clear - I still need to initialize scan_control.swappiness in all these other places right? It appears to mostly be stack-initialized which means it has indeterminate value, not necessarily zero.

#11 lore raw

Re: [PATCH V4 2/2] mm: add swapiness= arg to memory.reclaim

Quoted 55 lines · Dan Schatzberg
Quoted 14 lines · Michal Hocko
Quoted 3 lines · Dan Schatzberg

Allow proactive reclaimers to submit an additional swappiness=<val> argument to memory.reclaim. This overrides the global or per-memcg swappiness setting for that reclaim attempt.

You are providing the usecase in the cover letter and Andrew usually appends that to the first patch in the series. I think it would be better to have the usecase described here.

[...]

Quoted 4 lines
Documentation/admin-guide/cgroup-v2.rst +0 -0
@@ -1304,6 +1297,18 @@ PAGE_SIZE multiple when read back.
    This means that the networking layer will not adapt based on
    reclaim induced by memory.reclaim.
 
Quoted 12 lines

Documentation/admin-guide/cgroup-v2.rst PAGE_SIZE multiple when read back. · line 1308

Quoted 9 lines
Documentation/admin-guide/cgroup-v2.rst +9 -0
+The following nested keys are defined.
+
+     ==========            ================================
+     swappiness            Swappiness value to reclaim with
+     ==========            ================================
+
+   Specifying a swappiness value instructs the kernel to perform
+   the reclaim with that swappiness value. Note that this has the
+   same semantics as the vm.swappiness sysctl - it sets the

same semantics as vm.swappiness applied to memcg reclaim with all the existing limitations and potential future extensions.

Thanks, will make this change.

Quoted 18 lines

Documentation/admin-guide/cgroup-v2.rst PAGE_SIZE multiple when read back. · line 1314

Quoted 7 lines
Documentation/admin-guide/cgroup-v2.rst +3 -0
+   relative IO cost of reclaiming anon vs file memory but does
+   not allow for reclaiming specific amounts of anon or file memory.
+
   memory.peak
    A read-only single value file which exists on non-root
    cgroups.

The biggest problem with the implementation I can see, and others have pointed out the same, is how fragile this is. You really have to check the code and _every_ place which defines scan_control to learn that mem_cgroup_shrink_node, reclaim_clean_pages_from_list, reclaim_folio_list, lru_gen_seq_write, try_to_free_pages, balance_pgdat, shrink_all_memory and __node_reclaim. I have only checked couple of them, like direct reclaim and kswapd and none of them really sets up swappiness to the default memcg nor global value. So you effectively end up with swappiness == 0.

Quoted 5 lines

While the review can point those out it is quite easy to break and you will only learn about that very indirectly. I think it would be easier to review and maintain if you go with a pointer that would fallback to mem_cgroup_swappiness() if NULL which will be the case for every existing reclaimer except memory.reclaim with swappiness value.

Quoted 5 lines

I agree. My initial implementation used a pointer for this reason. I'll switch this back. Just to be clear - I still need to initialize scan_control.swappiness in all these other places right? It appears to mostly be stack-initialized which means it has indeterminate value, not necessarily zero.

My understanding is that in a partially initialized struct, uninitialized members default to 0, but I am not a C expert :)

#12 lore raw

Re: [PATCH V4 2/2] mm: add swapiness= arg to memory.reclaim

In reply to Dan Schatzberg
Quoted 1 line · Dan Schatzberg

On Thu, Dec 14, 2023 at 09:38:55AM +0100, Michal Hocko wrote:

[...]

Quoted 9 lines
Quoted 5 lines

While the review can point those out it is quite easy to break and you will only learn about that very indirectly. I think it would be easier to review and maintain if you go with a pointer that would fallback to mem_cgroup_swappiness() if NULL which will be the case for every existing reclaimer except memory.reclaim with swappiness value.

I agree. My initial implementation used a pointer for this reason. I'll switch this back. Just to be clear - I still need to initialize scan_control.swappiness in all these other places right?

No. They will just get initialized to 0. All you need to make sure is that the swappiness is used consistently. I would propose something like scan_control_swappiness() (or sc_swappiness) which would actually do

	if (sc->swappiness)
		return sc->swappiness;
	return mem_cgroup_swappiness(sc->target_mem_cgroup);

and then make sure that mem_cgroup_swappiness is never used directly.

Signature · 2 lines
Michal Hocko
SUSE Labs