Skip to contentSkip to conversation

mm, memcg: fix memory.peak reset clobbering other fds' watermark

PATCH Superseded Ridong Chen 2 participants 6 messages Aug 14 1 message not in the archive

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover mm, memcg: fix memory.peak reset clobbering other fds' watermark

From: Ridong Chen <[email protected]>

The memory.peak / memory.swap.peak per-fd watermark tracking has two issues. Each open fd is a watcher and reads back max(its own value, the shared local_watermark); both bugs live in that scheme.

Worst case for both is the same and is userspace-visible: a reader of memory.peak (or memory.swap.peak) gets a value lower than the true peak, so a tool that sizes or bills a cgroup by its peak usage under-reports it.

Patch 1 (read side) fixes the race Sashiko pointed out in the v1 review [1]: peak_show() inspects local_watermark and the per-fd values without holding peaks_lock, so a reader that races an unrelated peak_write() reset briefly observes the lowered value. Transient. It takes peaks_lock in the show path.

Patch 2 (write side) fixes peak_write(): on a reset it stores the current usage into the other watchers instead of the old watermark, so once usage has dropped from a peak a reset on one fd drags every other fd's peak down too, even fds that never reset.

---

Changes since v2:
- Spell out the worst-case userspace-visible effect, per Andrew's Go back to v1 [2].

Changes since v1:
- New patch 1: hold peaks_lock in the peak readers (Sashiko).
- Patch 2: floor the peers with max(usage, local_watermark), mirroring peak_show(), and skip the writing fd (Johannes Weiner).

[1] https://sashiko.dev/#/patchset/[email protected]?part=1
[2] https://lore.kernel.org/all/[email protected]/

Ridong Chen (2):
memcg: acquire peaks_lock when reading memory.peak mm, memcg: fix memory.peak reset clobbering other fds' watermark

 mm/memcontrol.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/2 memcg: acquire peaks_lock when reading memory.peak

From: Ridong Chen <[email protected]>

Sashiko reported that a reader can transiently observe a lower peak within a race window [1]. peak_show() returns max(local_watermark, ofp->value), but peak_write() updates those two under peaks_lock while the reader takes no lock. The interleaving is:

writer (reset on fd A) reader (fd B)
---------------------- -------------
usage = page_counter_read(pc)
WRITE_ONCE(local_watermark, usage)
// watermark lowered to usage

                                         lw  = READ_ONCE(local_watermark)
                                         // sees the lowered usage
                                         val = READ_ONCE(ofp->value)
                                         // B's value not updated yet
                                         return max(lw, val)
                                         // both low -> low peak

WRITE_ONCE(peer_ctx->value, usage)
// B updated, but too late

Fix it by acquiring peaks_lock when reading the peak, so the reader sees a consistent snapshot of local_watermark and the per-fd values. The same race applies to memory.swap.peak, which shares peaks_lock and the peak_write() path, so take the lock there as well.

[1] https://sashiko.dev/#/patchset/[email protected]?part=1

Fixes c6f53ed8f213 ("mm, memcg: cg2 memory{.swap,}.peak write handlers")
Assisted-by Claude:claude-opus-4-8
---
 mm/memcontrol.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
mm/memcontrol.c +12 -2
index 17da1f43b7d3..6dd8756870ff 100644
@@ -4713,8 +4713,13 @@ static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc)
 static int memory_peak_show(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
+	int ret;
 
-	return peak_show(sf, v, &memcg->memory);
+	spin_lock(&memcg->peaks_lock);
+	ret = peak_show(sf, v, &memcg->memory);
+	spin_unlock(&memcg->peaks_lock);
+
+	return ret;
 }
 
 static int peak_open(struct kernfs_open_file *of)
@@ -5858,8 +5863,13 @@ static u64 swap_current_read(struct cgroup_subsys_state *css,
 static int swap_peak_show(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
+	int ret;
 
-	return peak_show(sf, v, &memcg->swap);
+	spin_lock(&memcg->peaks_lock);
+	ret = peak_show(sf, v, &memcg->swap);
+	spin_unlock(&memcg->peaks_lock);
+
+	return ret;
 }
 
 static ssize_t swap_peak_write(struct kernfs_open_file *of, char *buf,
Signature · 1 line
2.34.1
reviewer
#2 lore raw

Re: [PATCH v3 1/2] memcg: acquire peaks_lock when reading memory.peak

Quoted 45 lines · Ridong Chen

From: Ridong Chen <[email protected]>

Sashiko reported that a reader can transiently observe a lower peak within a race window [1]. peak_show() returns max(local_watermark, ofp->value), but peak_write() updates those two under peaks_lock while the reader takes no lock. The interleaving is:

writer (reset on fd A) reader (fd B)
---------------------- -------------
usage = page_counter_read(pc)
WRITE_ONCE(local_watermark, usage)
// watermark lowered to usage

                                        lw  = READ_ONCE(local_watermark)
                                        // sees the lowered usage
                                        val = READ_ONCE(ofp->value)
                                        // B's value not updated yet
                                        return max(lw, val)
                                        // both low -> low peak

WRITE_ONCE(peer_ctx->value, usage)
// B updated, but too late

Fix it by acquiring peaks_lock when reading the peak, so the reader sees a consistent snapshot of local_watermark and the per-fd values. The same race applies to memory.swap.peak, which shares peaks_lock and the peak_write() path, so take the lock there as well.

[1] https://sashiko.dev/#/patchset/[email protected]?part=1

Fixes c6f53ed8f213 ("mm, memcg: cg2 memory{.swap,}.peak write handlers")
Assisted-by Claude:claude-opus-4-8

---
mm/memcontrol.c | 14 ++++++++++++--

1 file changed, 12 insertions(+), 2 deletions(-)
mm/memcontrol.c +0 -0
index 17da1f43b7d3..6dd8756870ff 100644

mm/memcontrol.c static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc) · line 4718

Quoted 7 lines
mm/memcontrol.c +2 -1
@@ -4713,8 +4713,13 @@ static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc)
 static int memory_peak_show(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
+ 	int ret;
 
- 	return peak_show(sf, v, &memcg->memory);
+ 	spin_lock(&memcg->peaks_lock);

Why not to use guard(spinlock)(&memcg->peaks_lock) to simplify the code.

Muchun,
Thanks.

Quoted 25 lines
mm/memcontrol.c +10 -1
+ 	ret = peak_show(sf, v, &memcg->memory);
+ 	spin_unlock(&memcg->peaks_lock);
+
+ 	return ret;
 }
 
 static int peak_open(struct kernfs_open_file *of)
@@ -5858,8 +5863,13 @@ static u64 swap_current_read(struct cgroup_subsys_state *css,
 static int swap_peak_show(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
+ 	int ret;
 
- 	return peak_show(sf, v, &memcg->swap);
+ 	spin_lock(&memcg->peaks_lock);
+ 	ret = peak_show(sf, v, &memcg->swap);
+ 	spin_unlock(&memcg->peaks_lock);
+
+ 	return ret;
 }
 
 static ssize_t swap_peak_write(struct kernfs_open_file *of, char *buf,
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH v3 1/2] memcg: acquire peaks_lock when reading memory.peak

Quoted 50 lines · Muchun Song
Quoted 50 lines · Ridong Chen

From: Ridong Chen <[email protected]>

Sashiko reported that a reader can transiently observe a lower peak within a race window [1]. peak_show() returns max(local_watermark, ofp->value), but peak_write() updates those two under peaks_lock while the reader takes no lock. The interleaving is:

writer (reset on fd A) reader (fd B)
---------------------- -------------
usage = page_counter_read(pc)
WRITE_ONCE(local_watermark, usage)
// watermark lowered to usage

                                         lw  = READ_ONCE(local_watermark)
                                         // sees the lowered usage
                                         val = READ_ONCE(ofp->value)
                                         // B's value not updated yet
                                         return max(lw, val)
                                         // both low -> low peak

WRITE_ONCE(peer_ctx->value, usage)
// B updated, but too late

Fix it by acquiring peaks_lock when reading the peak, so the reader sees a consistent snapshot of local_watermark and the per-fd values. The same race applies to memory.swap.peak, which shares peaks_lock and the peak_write() path, so take the lock there as well.

[1] https://sashiko.dev/#/patchset/[email protected]?part=1

Fixes c6f53ed8f213 ("mm, memcg: cg2 memory{.swap,}.peak write handlers")
Assisted-by Claude:claude-opus-4-8

---
mm/memcontrol.c | 14 ++++++++++++--

1 file changed, 12 insertions(+), 2 deletions(-)
mm/memcontrol.c +1 -0
index 17da1f43b7d3..6dd8756870ff 100644
@@ -4713,8 +4713,13 @@ static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc)
 static int memory_peak_show(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
+ 	int ret;
 
Quoted 8 lines

mm/memcontrol.c static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc) · line 4718

Quoted 2 lines
mm/memcontrol.c +1 -1
- 	return peak_show(sf, v, &memcg->memory);
+ 	spin_lock(&memcg->peaks_lock);

Why not to use guard(spinlock)(&memcg->peaks_lock) to simplify the code.

Muchun,
Thanks.

Good idea. Will update.

Quoted 26 lines
Quoted 25 lines
mm/memcontrol.c +10 -1
+ 	ret = peak_show(sf, v, &memcg->memory);
+ 	spin_unlock(&memcg->peaks_lock);
+
+ 	return ret;
 }
 
 static int peak_open(struct kernfs_open_file *of)
@@ -5858,8 +5863,13 @@ static u64 swap_current_read(struct cgroup_subsys_state *css,
 static int swap_peak_show(struct seq_file *sf, void *v)
 {
 	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
+ 	int ret;
 
- 	return peak_show(sf, v, &memcg->swap);
+ 	spin_lock(&memcg->peaks_lock);
+ 	ret = peak_show(sf, v, &memcg->swap);
+ 	spin_unlock(&memcg->peaks_lock);
+
+ 	return ret;
 }
 
 static ssize_t swap_peak_write(struct kernfs_open_file *of, char *buf,
Signature · 1 line
2.34.1
Signature · 2 lines
Best regards
Ridong

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

Re: [PATCH v3 1/2] memcg: acquire peaks_lock when reading memory.peak

Quoted 50 lines · On 8/14/2026 10:06 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] peak_write() overwrites pc->local_watermark without preserving the historical peak for other watchers. --

--- Patch [1]: [PATCH v3 1/2] memcg: acquire peaks_lock when reading memory.peak --- commit 4ced2059ac574bcfd14e2b9ed8899640facbe49f
Author: Ridong Chen <[email protected]>

memcg: acquire peaks_lock when reading memory.peak

This patch fixes a race condition when reading memory.peak or memory.swap.peak. It acquires peaks_lock in memory_peak_show() and swap_peak_show() so that readers observe a consistent snapshot of local_watermark and per-fd values.

[1] https://sashiko.dev/#/patchset/[email protected]?part=1

Quoted 7 lines
mm/memcontrol.c +0 -0
index 1d3339520809f..0495b844c3750 100644
@@ -4712,8 +4712,13 @@ static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc)
  static int memory_peak_show(struct seq_file *sf, void *v)
  {

mm/memcontrol.c static int peak_show(struct seq_file *sf, void *v, struct page_counter *pc) · line 4722

Quoted 10 lines
mm/memcontrol.c +6 -1
  	struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(sf));
+	int ret;
 
-	return peak_show(sf, v, &memcg->memory);
+	spin_lock(&memcg->peaks_lock);
+	ret = peak_show(sf, v, &memcg->memory);
+	spin_unlock(&memcg->peaks_lock);
+
+	return ret;
  }

[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the peaks_lock synchronization changes, does the shared peak_write() path permanently lose the historical peak for other watchers?

Looking at peak_write() in mm/memcontrol.c:

	usage = page_counter_read(pc);
	WRITE_ONCE(pc->local_watermark, usage);

	list_for_each_entry(peer_ctx, watchers, list)
		if (usage > peer_ctx->value)
			WRITE_ONCE(peer_ctx->value, usage);
Quoted 7 lines

If watcher A resets memory.peak, peak_write() updates pc->local_watermark to the current usage. It then updates watcher B's tracking value to that same current usage.

If memory usage hit a peak of 100 but dropped to 50 before watcher A reset it, won't watcher B incorrectly read 50 instead of the actual peak of 100?

The patch 2 will fix this issue.

Signature · 2 lines
Best regards
Ridong
#6 lore raw

PATCH v3 2/2 mm, memcg: fix memory.peak reset clobbering other fds' watermark

In reply to Ridong Chen

From: Ridong Chen <[email protected]>

Writing to memory.peak resets the peak for that fd only. Each fd is a watcher and reads back max(its own value, the shared local_watermark).

peak_write() resets by lowering local_watermark to the current usage. To keep the other watchers' peaks it then walks the watcher list, but it stores the current usage into them instead of the old watermark. So once usage has dropped from a peak, a reset on one fd wrongly drags every other fd's peak down too, even fds that never reset.

Reproduced on 7.2.0-rc5-next under QEMU, two fds A and B on one cgroup:
B sees the peak (410624 KB), usage drops, then A resets -- and B's peak collapses to 1060 KB although B never reset. With this patch B keeps reading 410624 KB.

Fix: save the old watermark before lowering it and use that to floor the other watchers, so a reset only affects the fd that issued it.

Fixes c6f53ed8f213 ("mm, memcg: cg2 memory{.swap,}.peak write handlers")
Assisted-by Claude:claude-opus-4-8
---
 mm/memcontrol.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mm/memcontrol.c +4 -3
index 6dd8756870ff..5e5edc988119 100644
@@ -4748,7 +4748,7 @@ static ssize_t peak_write(struct kernfs_open_file *of, char *buf, size_t nbytes,
 			  loff_t off, struct page_counter *pc,
 			  struct list_head *watchers)
 {
-	unsigned long usage;
+	unsigned long usage, old_watermark;
 	struct cgroup_of_peak *peer_ctx;
 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
 	struct cgroup_of_peak *ofp = of_peak(of);
@@ -4756,11 +4756,12 @@ static ssize_t peak_write(struct kernfs_open_file *of, char *buf, size_t nbytes,
 	spin_lock(&memcg->peaks_lock);
 
 	usage = page_counter_read(pc);
+	old_watermark = READ_ONCE(pc->local_watermark);
 	WRITE_ONCE(pc->local_watermark, usage);
 
 	list_for_each_entry(peer_ctx, watchers, list)
-		if (usage > peer_ctx->value)
-			WRITE_ONCE(peer_ctx->value, usage);
+		if (peer_ctx != ofp && old_watermark > peer_ctx->value)
+			WRITE_ONCE(peer_ctx->value, old_watermark);
 
 	/* initial write, register watcher */
 	if (ofp->value == OFP_PEAK_UNSET)
Signature · 1 line
2.34.1