Skip to contentSkip to conversation

mm: memcg: Use larger batches for proactive reclaim

PATCH Superseded T.J. Mercier 3 participants 6 messages Feb 2, 2024

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm: memcg: Use larger batches for proactive reclaim

Before 388536ac291 ("mm:vmscan: fix inaccurate reclaim during proactive reclaim") we passed the number of pages for the reclaim request directly to try_to_free_mem_cgroup_pages, which could lead to significant overreclaim. After 0388536ac291 the number of pages was limited to a maximum 32 (SWAP_CLUSTER_MAX) to reduce the amount of overreclaim. However such a small batch size caused a regression in reclaim performance due to many more reclaim start/stop cycles inside memory_reclaim.

Reclaim tries to balance nr_to_reclaim fidelity with fairness across nodes and cgroups over which the pages are spread. As such, the bigger the request, the bigger the absolute overreclaim error. Historic in-kernel users of reclaim have used fixed, small sized requests to approach an appropriate reclaim rate over time. When we reclaim a user request of arbitrary size, use decaying batch sizes to manage error while maintaining reasonable throughput.

root - full reclaim pages/sec time (sec)

pre-0388536ac291      :    68047        10.46
post-0388536ac291     :    13742        inf

(reclaim-reclaimed)/4 : 67352 10.51

/uid_0 - 1G reclaim pages/sec time (sec) overreclaim (MiB)

pre-0388536ac291      :    258822       1.12            107.8
post-0388536ac291     :    105174       2.49            3.5

(reclaim-reclaimed)/4 : 233396 1.12 -7.4

/uid_0 - full reclaim pages/sec time (sec)

pre-0388536ac291      :    72334        7.09
post-0388536ac291     :    38105        14.45

(reclaim-reclaimed)/4 : 72914 6.96

Fixes 0388536ac291 ("mm:vmscan: fix inaccurate reclaim during proactive reclaim")

---
v2: Simplify the request size calculation per Johannes Weiner and Michal Koutný

 mm/memcontrol.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/memcontrol.c +4 -1
index 46d8d02114cf..e6f921555e07 100644
@@ -6965,6 +6965,9 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
 	while (nr_reclaimed < nr_to_reclaim) {
 		unsigned long reclaimed;
 
+		/* Will converge on zero, but reclaim enforces a minimum */
+		unsigned long batch_size = (nr_to_reclaim - nr_reclaimed) / 4;
+
 		if (signal_pending(current))
 			return -EINTR;
 
@@ -6977,7 +6980,7 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
 			lru_add_drain_all();
 
 		reclaimed = try_to_free_mem_cgroup_pages(memcg,
-					min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX),
+					batch_size,
 					GFP_KERNEL, reclaim_options);
 
 		if (!reclaimed && !nr_retries--)
Signature · 1 line
2.43.0.594.gd9cf4e227d-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 28 lines · T.J. Mercier

Before 388536ac291 ("mm:vmscan: fix inaccurate reclaim during proactive reclaim") we passed the number of pages for the reclaim request directly to try_to_free_mem_cgroup_pages, which could lead to significant overreclaim. After 0388536ac291 the number of pages was limited to a maximum 32 (SWAP_CLUSTER_MAX) to reduce the amount of overreclaim. However such a small batch size caused a regression in reclaim performance due to many more reclaim start/stop cycles inside memory_reclaim.

Reclaim tries to balance nr_to_reclaim fidelity with fairness across nodes and cgroups over which the pages are spread. As such, the bigger the request, the bigger the absolute overreclaim error. Historic in-kernel users of reclaim have used fixed, small sized requests to approach an appropriate reclaim rate over time. When we reclaim a user request of arbitrary size, use decaying batch sizes to manage error while maintaining reasonable throughput.

root - full reclaim pages/sec time (sec)

pre-0388536ac291      :    68047        10.46
post-0388536ac291     :    13742        inf

(reclaim-reclaimed)/4 : 67352 10.51

/uid_0 - 1G reclaim pages/sec time (sec) overreclaim (MiB)

pre-0388536ac291      :    258822       1.12            107.8
post-0388536ac291     :    105174       2.49            3.5

(reclaim-reclaimed)/4 : 233396 1.12 -7.4

Quoted 7 lines

/uid_0 - full reclaim pages/sec time (sec)

pre-0388536ac291      :    72334        7.09
post-0388536ac291     :    38105        14.45

(reclaim-reclaimed)/4 : 72914 6.96

Fixes 0388536ac291 ("mm:vmscan: fix inaccurate reclaim during proactive reclaim")

LGTM with a nit below:

Quoted 22 lines

---
v2: Simplify the request size calculation per Johannes Weiner and Michal Koutný

 mm/memcontrol.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/memcontrol.c +3 -0
index 46d8d02114cf..e6f921555e07 100644
@@ -6965,6 +6965,9 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
        while (nr_reclaimed < nr_to_reclaim) {
                unsigned long reclaimed;
 
+               /* Will converge on zero, but reclaim enforces a minimum */
+               unsigned long batch_size = (nr_to_reclaim - nr_reclaimed) / 4;
+
                if (signal_pending(current))
                        return -EINTR;
 

mm/memcontrol.c static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, · line 6984

Quoted 6 lines
mm/memcontrol.c +1 -1
@@ -6977,7 +6980,7 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
                        lru_add_drain_all();
 
                reclaimed = try_to_free_mem_cgroup_pages(memcg,
-                                       min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX),
+                                       batch_size,
                                        GFP_KERNEL, reclaim_options);

I think the above two lines should now fit into one.

#2 lore raw
Quoted 35 lines · Yosry Ahmed
Quoted 33 lines · T.J. Mercier

Before 388536ac291 ("mm:vmscan: fix inaccurate reclaim during proactive reclaim") we passed the number of pages for the reclaim request directly to try_to_free_mem_cgroup_pages, which could lead to significant overreclaim. After 0388536ac291 the number of pages was limited to a maximum 32 (SWAP_CLUSTER_MAX) to reduce the amount of overreclaim. However such a small batch size caused a regression in reclaim performance due to many more reclaim start/stop cycles inside memory_reclaim.

Reclaim tries to balance nr_to_reclaim fidelity with fairness across nodes and cgroups over which the pages are spread. As such, the bigger the request, the bigger the absolute overreclaim error. Historic in-kernel users of reclaim have used fixed, small sized requests to approach an appropriate reclaim rate over time. When we reclaim a user request of arbitrary size, use decaying batch sizes to manage error while maintaining reasonable throughput.

root - full reclaim pages/sec time (sec)

pre-0388536ac291      :    68047        10.46
post-0388536ac291     :    13742        inf

(reclaim-reclaimed)/4 : 67352 10.51

/uid_0 - 1G reclaim pages/sec time (sec) overreclaim (MiB)

pre-0388536ac291      :    258822       1.12            107.8
post-0388536ac291     :    105174       2.49            3.5

(reclaim-reclaimed)/4 : 233396 1.12 -7.4

/uid_0 - full reclaim pages/sec time (sec)

pre-0388536ac291      :    72334        7.09
post-0388536ac291     :    38105        14.45

(reclaim-reclaimed)/4 : 72914 6.96

Quoted 5 lines
Quoted 2 lines
Fixes 0388536ac291 ("mm:vmscan: fix inaccurate reclaim during proactive reclaim")

LGTM with a nit below:

Thanks

Quoted 24 lines
Quoted 24 lines

---
v2: Simplify the request size calculation per Johannes Weiner and Michal Koutný

 mm/memcontrol.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/memcontrol.c +3 -0
index 46d8d02114cf..e6f921555e07 100644
@@ -6965,6 +6965,9 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
        while (nr_reclaimed < nr_to_reclaim) {
                unsigned long reclaimed;
 
+               /* Will converge on zero, but reclaim enforces a minimum */
+               unsigned long batch_size = (nr_to_reclaim - nr_reclaimed) / 4;
+
                if (signal_pending(current))
                        return -EINTR;
 
@@ -6977,7 +6980,7 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
                        lru_add_drain_all();
 
Quoted 6 lines

mm/memcontrol.c static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, · line 6984

Quoted 4 lines
mm/memcontrol.c +1 -1
                reclaimed = try_to_free_mem_cgroup_pages(memcg,
-                                       min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX),
+                                       batch_size,
                                        GFP_KERNEL, reclaim_options);

I think the above two lines should now fit into one.

It goes out to 81 characters. I wasn't brave enough, even though the 80 char limit is no more. :)

This takes it out to 100 but gets rid of batch_size if folks are ok with it:

reclaimed = try_to_free_mem_cgroup_pages(memcg,
- min(nr_to_reclaim -
nr_reclaimed, SWAP_CLUSTER_MAX),
+ /* Will converge on zero, but
reclaim enforces a minimum */
+ (nr_to_reclaim - nr_reclaimed) / 4,

                                        GFP_KERNEL, reclaim_options);
#3 lore raw
Quoted 11 lines
Quoted 11 lines

mm/memcontrol.c static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, · line 6970

Quoted 11 lines
mm/memcontrol.c +3 -0
index 46d8d02114cf..e6f921555e07 100644
@@ -6965,6 +6965,9 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
        while (nr_reclaimed < nr_to_reclaim) {
                unsigned long reclaimed;
 
+               /* Will converge on zero, but reclaim enforces a minimum */
+               unsigned long batch_size = (nr_to_reclaim - nr_reclaimed) / 4;
+

I think it's clearer with no blank lines between declarations. Perhaps add these two lines right above the declaration of "reclaimed"?

Quoted 13 lines
Quoted 6 lines
Quoted 6 lines
mm/memcontrol.c +0 -0
                if (signal_pending(current))
                        return -EINTR;
 
@@ -6977,7 +6980,7 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
                        lru_add_drain_all();
 
Quoted 6 lines

mm/memcontrol.c static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, · line 6984

Quoted 4 lines
mm/memcontrol.c +1 -1
                reclaimed = try_to_free_mem_cgroup_pages(memcg,
-                                       min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX),
+                                       batch_size,
                                        GFP_KERNEL, reclaim_options);

I think the above two lines should now fit into one.

Quoted 2 lines

It goes out to 81 characters. I wasn't brave enough, even though the 80 char limit is no more. :)

Oh okay, I would leave it as-is or rename batch_size to something slightly shorter. Not a big deal either way. Going to 81 chars is probably fine too.

maintainer Acked-by
#4 lore raw
In reply to Yosry Ahmed
Quoted 14 lines · Yosry Ahmed
Quoted 13 lines · T.J. Mercier
mm/memcontrol.c +3 -0
@@ -6965,6 +6965,9 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
        while (nr_reclaimed < nr_to_reclaim) {
                unsigned long reclaimed;
 
+               /* Will converge on zero, but reclaim enforces a minimum */
+               unsigned long batch_size = (nr_to_reclaim - nr_reclaimed) / 4;
+
                if (signal_pending(current))
                        return -EINTR;
 
@@ -6977,7 +6980,7 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
                        lru_add_drain_all();
 
Quoted 6 lines

mm/memcontrol.c static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, · line 6984

Quoted 4 lines
mm/memcontrol.c +1 -1
                reclaimed = try_to_free_mem_cgroup_pages(memcg,
-                                       min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX),
+                                       batch_size,
                                        GFP_KERNEL, reclaim_options);

I think the above two lines should now fit into one.

Yeah might as well compact that again. The newline in the declarations is a bit unusual for this codebase as well, and puts the comment sort of away from the "reclaim" it refers to. This?

		/* Will converge on zero, but reclaim enforces a minimum */
		batch_size = (nr_to_reclaim - nr_reclaimed) / 4;

		reclaimed = try_to_free_mem_cgroup_pages(memcg, batch_size,
					GFP_KERNEL, reclaim_options);

But agreed, it's all just nitpickety nickpicking. :)

#5 lore raw
Quoted 33 lines · Johannes Weiner
Quoted 14 lines · Yosry Ahmed
Quoted 13 lines · T.J. Mercier
mm/memcontrol.c +3 -0
@@ -6965,6 +6965,9 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
        while (nr_reclaimed < nr_to_reclaim) {
                unsigned long reclaimed;
 
+               /* Will converge on zero, but reclaim enforces a minimum */
+               unsigned long batch_size = (nr_to_reclaim - nr_reclaimed) / 4;
+
                if (signal_pending(current))
                        return -EINTR;
 
@@ -6977,7 +6980,7 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
                        lru_add_drain_all();
 
Quoted 6 lines

mm/memcontrol.c static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf, · line 6984

Quoted 4 lines
mm/memcontrol.c +1 -1
                reclaimed = try_to_free_mem_cgroup_pages(memcg,
-                                       min(nr_to_reclaim - nr_reclaimed, SWAP_CLUSTER_MAX),
+                                       batch_size,
                                        GFP_KERNEL, reclaim_options);

I think the above two lines should now fit into one.

Yeah might as well compact that again. The newline in the declarations is a bit unusual for this codebase as well, and puts the comment sort of away from the "reclaim" it refers to. This?

                /* Will converge on zero, but reclaim enforces a minimum */
                batch_size = (nr_to_reclaim - nr_reclaimed) / 4;

                reclaimed = try_to_free_mem_cgroup_pages(memcg, batch_size,
                                        GFP_KERNEL, reclaim_options);
Quoted 3 lines

But agreed, it's all just nitpickety nickpicking. :)

-std=gnu11 to the rescue

+ /* Will converge on zero, but reclaim enforces a minimum */
+ unsigned long batch_size = (nr_to_reclaim - nr_reclaimed) / 4;
reclaimed = try_to_free_mem_cgroup_pages(memcg,
- min(nr_to_reclaim -
nr_reclaimed, SWAP_CLUSTER_MAX),
+ batch_size,

                                        GFP_KERNEL, reclaim_options);