mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Tejun Heo <tj@kernel.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Muchun Song <muchun.song@linux.dev>,
	David Hildenbrand <david@kernel.org>,
	Qi Zheng <qi.zheng@linux.dev>, Lorenzo Stoakes <ljs@kernel.org>,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	Jan Kara <jack@suse.cz>, Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>,
	Jens Axboe <axboe@kernel.dk>, Christoph Hellwig <hch@lst.de>,
	Greg Thelen <gthelen@google.com>, Greg KH <greg@kroah.com>,
	Ming Lei <tom.leiming@gmail.com>,
	linux-mm@kvack.org, cgroups@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH RESEND] mm/vmscan: Fix legacy memcg reclaim deadlock against filesystem freeze
Date: Thu, 26 Nov 2026 18:02:28 -0800	[thread overview]
Message-ID: <20261126180228.9b672a945d480e3d3f301985@linux-foundation.org> (raw)
In-Reply-To: <f188775aea9580f9ab2b477788503434@kernel.org>

On Thu, 26 Nov 2026 02:17:38 -1000 Tejun Heo <tj@kernel.org> wrote:

> Hello, Andrew.
> 
> Your follow-up, "mm/vmscan: scope freeze writeback skip to the reclaim
> target", applies on top of this patch and tests fine here.

Cool, thanks.  I'd forgotten about that ;) It was a hastily slop-coded
"maybe you'd like to include something like this in your patch" thing.

Can people please take a look and suggest whether it should be a
standalone patch, or just fold it into this patch, "mm/vmscan: Fix
legacy memcg reclaim deadlock against filesystem freeze"?


(That's mem_cgroup's fifth bool - maybe scrunch them all into an
atomic_t and do atomic bitops on it?)

(I am told that moving wb_wait_skip to just after oom_group would avoid
increasing sizeof(mem_cgroup), but that puts wb_wait_skip into the
read-mostly section. wdyt?) 


From: Andrew Morton <akpm@linux-foundation.org>
Subject: mm/vmscan: scope freeze writeback skip to the reclaim target
Date: Tue, 17 Nov 2026 14:04:43 -0800

The freeze workaround sets memcg_wb_wait_skip globally when a memcg's
writeback wait times out.  While any superblock freeze is pending, this
makes reclaimers in unrelated memcgs bypass their writeback waits and can
drive an otherwise recoverable cgroup to OOM.

Store the flag in the reclaim target memcg instead.  Use
sc->target_mem_cgroup, not the folio's memcg: reclaim against a parent
limit scans descendants.  This also keeps the skip across successive
try_to_free_mem_cgroup_pages() calls in try_charge_memcg(), each of which
creates a new scan_control.  Reclaim against a different target must first
time out on its own writeback.

Reclaimers sharing the target can still OOM early during a freeze.

Reclaimers of the same target can run concurrently, without a lock
protecting this flag.  Keep READ_ONCE() and WRITE_ONCE() for its lockless
loads and stores.  The flag does not publish any other data.  As with the
global flag, a clear racing with a new timeout may cost another 15-second
wait; the timeout path can set the flag again.

Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Tejun Heo <tj@kernel.org>

 include/linux/memcontrol.h |    2 +
 mm/vmscan.c                |   52 ++++++++++++++++++++---------------
 2 files changed, 33 insertions(+), 21 deletions(-)

--- a/include/linux/memcontrol.h~mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target
+++ a/include/linux/memcontrol.h
@@ -248,6 +248,8 @@ struct mem_cgroup {
 	struct list_head memory_peaks;
 	struct list_head swap_peaks;
 	spinlock_t	 peaks_lock;
+	/* Legacy writeback wait timed out during a filesystem freeze. */
+	bool wb_wait_skip;
 
 	/* memory.events and memory.events.local */
 	struct cgroup_file events_file;
--- a/mm/vmscan.c~mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target
+++ a/mm/vmscan.c
@@ -1171,39 +1171,49 @@ static bool may_enter_fs(struct folio *f
  *
  * Reclaim can't tell which filesystem a writeback depends on. As a crude
  * workaround, memcg_wait_writeback() times out and, if any filesystem is being
- * frozen at that point, sets memcg_wb_wait_skip. While it is set, legacy memcg
- * reclaim neither waits for nor throttles on writeback, so that the task a
- * freeze may be waiting for isn't held up in reclaim and can drop its freeze
- * protection. This can cause premature memcg OOM kills.
+ * frozen at that point, sets a skip flag on the reclaim target memcg. While
+ * it is set, reclaim against that target neither waits for nor throttles on
+ * writeback, so that the task a freeze may be waiting for isn't held up in
+ * reclaim and can drop its freeze protection. This can cause premature memcg
+ * OOM kills in that reclaim target.
  */
-static bool memcg_wb_wait_skip;
-
-/*
- * Test memcg_wb_wait_skip. It takes effect only while a filesystem is being
- * frozen and is cleared here once none is. A freeze that starts before it is
- * cleared inherits the skip.
- */
-static bool memcg_skip_wb_wait(void)
+#ifdef CONFIG_MEMCG
+/* Retain the skip across separate reclaim attempts against the same limit. */
+static bool memcg_skip_wb_wait(struct scan_control *sc)
 {
-	if (!READ_ONCE(memcg_wb_wait_skip))
+	struct mem_cgroup *memcg = sc->target_mem_cgroup;
+
+	if (!memcg || !READ_ONCE(memcg->wb_wait_skip))
 		return false;
 	if (atomic_read(&sb_nr_freezing))
 		return true;
-	WRITE_ONCE(memcg_wb_wait_skip, false);
+	WRITE_ONCE(memcg->wb_wait_skip, false);
 	return false;
 }
 
-/* wait for writeback of @folio, see memcg_wb_wait_skip for the timeout */
-static void memcg_wait_writeback(struct folio *folio)
+/* Wait for writeback, unless a freeze stalls this reclaim target. */
+static void memcg_wait_writeback(struct folio *folio, struct scan_control *sc)
 {
 	while (!folio_wait_bit_timeout(folio, PG_writeback, 15 * HZ)) {
 		if (atomic_read(&sb_nr_freezing)) {
-			WRITE_ONCE(memcg_wb_wait_skip, true);
-			pr_warn("memcg1 writeback wait timed out during fs freeze, skipping waits, may OOM early\n");
+			WRITE_ONCE(sc->target_mem_cgroup->wb_wait_skip, true);
+			pr_warn("memcg writeback wait timed out during fs freeze; "
+				"skipping waits for reclaim target, may OOM early\n");
 			return;
 		}
 	}
 }
+#else
+static bool memcg_skip_wb_wait(struct scan_control *sc)
+{
+	return false;
+}
+
+static void memcg_wait_writeback(struct folio *folio, struct scan_control *sc)
+{
+	folio_wait_writeback(folio);
+}
+#endif
 
 /*
  * shrink_folio_list() returns the number of reclaimed pages
@@ -1351,7 +1361,7 @@ retry:
 
 			/* Case 2 above */
 			} else if (writeback_throttling_sane(sc) ||
-			    memcg_skip_wb_wait() ||
+			    memcg_skip_wb_wait(sc) ||
 			    !folio_test_reclaim(folio) ||
 			    !may_enter_fs(folio, sc->gfp_mask) ||
 			    (mapping &&
@@ -1377,7 +1387,7 @@ retry:
 			/* Case 3 above */
 			} else {
 				folio_unlock(folio);
-				memcg_wait_writeback(folio);
+				memcg_wait_writeback(folio, sc);
 				/* then go back and try same folio again */
 				list_add_tail(&folio->lru, folio_list);
 				continue;
@@ -2138,7 +2148,7 @@ static void handle_reclaim_writeback(uns
 		 * enough for cgroupv1 writeback throttling to work
 		 * on a large system.
 		 */
-		if (!writeback_throttling_sane(sc) && !memcg_skip_wb_wait())
+		if (!writeback_throttling_sane(sc) && !memcg_skip_wb_wait(sc))
 			reclaim_throttle(pgdat, VMSCAN_THROTTLE_WRITEBACK);
 	}
 
_


  reply	other threads:[~2026-11-27  2:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 12:08 Tejun Heo
2026-11-26 12:17 ` Tejun Heo
2026-11-27  2:02   ` Andrew Morton [this message]
2026-11-26 13:29 ` Jan Kara
2026-11-26 15:22 ` Shakeel Butt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126180228.9b672a945d480e3d3f301985@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=axboe@kernel.dk \
    --cc=brauner@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=david@kernel.org \
    --cc=greg@kroah.com \
    --cc=gthelen@google.com \
    --cc=hannes@cmpxchg.org \
    --cc=hch@lst.de \
    --cc=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=qi.zheng@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=tj@kernel.org \
    --cc=tom.leiming@gmail.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®