* [PATCH RESEND v5] jbd2: fix shrinker scan budget accounting in jbd2_journal_shrink_scan()
@ 2026-11-02 2:03 Qiliang Yuan
2026-11-02 2:18 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Qiliang Yuan @ 2026-11-02 2:03 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara
Cc: linux-ext4, linux-kernel, stable, Qiliang Yuan, Jan Kara
do_shrink_slab() reads count_objects() once per invocation to derive
a one-shot scan budget, then calls scan_objects() repeatedly until
that budget is exhausted or scan_objects() returns SHRINK_STOP.
include/linux/shrinker.h documents that scan_objects() "should track
its actual progress" in sc->nr_scanned, so do_shrink_slab() can tell
when there is nothing left to examine and stop early.
jbd2_journal_shrink_scan() never updates sc->nr_scanned, so it
defaults to the full sc->nr_to_scan on every call. do_shrink_slab()
therefore always believes a full batch was examined, regardless of
what jbd2_journal_shrink_checkpoint_list() actually did, and keeps
calling scan_objects() until the budget derived from the (possibly
stale) percpu checkpoint count is drained, even after the checkpoint
list has already been fully drained.
jbd2_journal_shrink_checkpoint_list() already tracks examined
buffers accurately: it decrements its nr_to_scan in/out parameter
for every journal_head walked, whether or not it gets freed. Derive
sc->nr_scanned from the difference, and return SHRINK_STOP once it
comes back zero, since that only happens when the checkpoint list
had nothing left to walk. Key this off sc->nr_scanned rather than
nr_shrunk: a batch can find every buffer in its transactions busy
and free none, while later transactions may still hold buffers
whose writeback has completed; sc->nr_scanned reflects that real
work either way.
Tested by creating 20000 small files, running sync, and letting
checkpointing settle for a few seconds (with systemd-journald and
cron stopped, since their own writes to the same filesystem keep
re-populating the checkpoint list and mask the effect being
measured), then triggering "echo 2 > /proc/sys/vm/drop_caches" while
tracing jbd2_shrink_scan_enter/exit:
total scan_objects() calls with
calls nr_scanned == 0
before this patch 22 10 (45%)
after this patch 8 1 (12%)
Fixes: 4ba3fcdde7e3 ("jbd2,ext4: add a shrinker to release checkpointed buffers")
Cc: stable@vger.kernel.org
Signed-off-by: Qiliang Yuan <odys.yuan@gmail.com>
Reviewed-by: Jan Kara <jack@suse.cz>
---
V4 -> V5:
- Revert to keying SHRINK_STOP off sc->nr_scanned (buffers actually
examined) rather than nr_shrunk (buffers actually freed), as v1
originally did. Jan Kara rejected the nr_shrunk-based v4, pointing
out that failing to reclaim the buffers in one batch doesn't mean
later transactions won't have freeable ones; Sashiko AI review
independently flagged the same issue. Restore Jan Kara's v1
Reviewed-by: the diff in jbd2_journal_shrink_scan() is unchanged
from what he reviewed there (only the explanatory comment reads
differently).
- Redesign the test: the old methodology (no sync, buffers kept busy
by design) exercises exactly the busy-but-productive case Jan Kara
describes, so it isn't a useful before/after comparison for this
change. Replace it with one that lets the checkpoint list actually
drain via sync, then measures scan_objects() calls wasted after
draining due to percpu-counter staleness.
V3 -> V4:
- Shorten the SHRINK_STOP comment; no functional change (the trigger
has been nr_shrunk == 0 since v2, not sc->nr_scanned).
- Carry forward Jan Kara's v3 Reviewed-by.
V2 -> V3:
- Rebase onto v7.3-rc5. It already includes Max Kellermann's
"jbd2: bound shrinker scans by examined checkpoint buffers"
(15cb16496446), which independently fixes the busy-buffer
accounting in journal_shrink_one_cp_list()/
jbd2_journal_shrink_checkpoint_list() that v2 also touched. Drop
the now-redundant checkpoint.c changes; this revision only touches
journal.c, reusing the accurate nr_to_scan tracking Kellermann's
fix already provides.
- Add a Fixes: tag for the commit that introduced
jbd2_journal_shrink_scan() without ever setting sc->nr_scanned.
- Re-measure test data against the new baseline (176 -> 8 calls,
vs the old baseline's 168 -> 7).
V1 -> V2:
- Count examined buffers, not just freed ones, in
journal_shrink_one_cp_list() (Sashiko AI review finding).
- Trigger SHRINK_STOP on nr_shrunk == 0 instead of
sc->nr_scanned == 0.
v4: https://lore.kernel.org/r/20260930-fix-jbd2-shrink-scan-nr-scanned-v4-1-eb9a3ccc2524@gmail.com
v3: https://lore.kernel.org/r/20260928-fix-jbd2-shrink-scan-nr-scanned-v3-1-916caaa53420@gmail.com
v2: https://lore.kernel.org/r/20260928-fix-jbd2-shrink-scan-nr-scanned-v2-1-7e1efec3afdf@gmail.com
v1: https://lore.kernel.org/r/20260928-fix-jbd2-shrink-scan-nr-scanned-v1-1-e6f4016ec699@gmail.com
---
fs/jbd2/journal.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
index 00f5a98f3d4fe..eac5f84a1afc5 100644
--- a/fs/jbd2/journal.c
+++ b/fs/jbd2/journal.c
@@ -1263,10 +1263,22 @@ static unsigned long jbd2_journal_shrink_scan(struct shrinker *shrink,
trace_jbd2_shrink_scan_enter(journal, sc->nr_to_scan, count);
nr_shrunk = jbd2_journal_shrink_checkpoint_list(journal, &nr_to_scan);
+ sc->nr_scanned = sc->nr_to_scan - nr_to_scan;
count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
trace_jbd2_shrink_scan_exit(journal, nr_to_scan, nr_shrunk, count);
+ /*
+ * Key SHRINK_STOP off sc->nr_scanned (buffers actually examined),
+ * not nr_shrunk (buffers actually freed): a batch can find every
+ * buffer in its transactions busy and free none, while later
+ * transactions may still hold buffers whose writeback has
+ * completed. sc->nr_scanned only comes back 0 when the checkpoint
+ * list had nothing left to walk.
+ */
+ if (sc->nr_scanned == 0)
+ return SHRINK_STOP;
+
return nr_shrunk;
}
---
base-commit: 72d3fcf802c45d00b300f25b848a93c3a2bd7c7e
change-id: 20260928-fix-jbd2-shrink-scan-nr-scanned-c3098cd901e2
Best regards,
--
Qiliang Yuan <odys.yuan@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH RESEND v5] jbd2: fix shrinker scan budget accounting in jbd2_journal_shrink_scan()
2026-11-02 2:03 [PATCH RESEND v5] jbd2: fix shrinker scan budget accounting in jbd2_journal_shrink_scan() Qiliang Yuan
@ 2026-11-02 2:18 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-02 2:18 UTC (permalink / raw)
To: Qiliang Yuan; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] jbd2: repeated busy buffer rescans in jbd2_journal_shrink_scan()
--
commit e6c15f2237536f947ab070ac978601d8d9849323
Author: Qiliang Yuan <odys.yuan@gmail.com>
jbd2: fix shrinker scan budget accounting in jbd2_journal_shrink_scan()
This commit updates sc->nr_scanned in jbd2_journal_shrink_scan() based on the
remaining nr_to_scan count from jbd2_journal_shrink_checkpoint_list() and
returns SHRINK_STOP when sc->nr_scanned is zero.
> diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
> index 00f5a98f3d4fe..eac5f84a1afc5 100644
> --- a/fs/jbd2/journal.c
> +++ b/fs/jbd2/journal.c
> @@ -1263,10 +1263,22 @@ static unsigned long jbd2_journal_shrink_scan(struct shrinker *shrink,
> trace_jbd2_shrink_scan_enter(journal, sc->nr_to_scan, count);
>
> nr_shrunk = jbd2_journal_shrink_checkpoint_list(journal, &nr_to_scan);
> + sc->nr_scanned = sc->nr_to_scan - nr_to_scan;
>
> count = percpu_counter_read_positive(&journal->j_checkpoint_jh_count);
> trace_jbd2_shrink_scan_exit(journal, nr_to_scan, nr_shrunk, count);
>
> + /*
> + * Key SHRINK_STOP off sc->nr_scanned (buffers actually examined),
> + * not nr_shrunk (buffers actually freed): a batch can find every
> + * buffer in its transactions busy and free none, while later
> + * transactions may still hold buffers whose writeback has
> + * completed. sc->nr_scanned only comes back 0 when the checkpoint
> + * list had nothing left to walk.
> + */
> + if (sc->nr_scanned == 0)
> + return SHRINK_STOP;
[Severity: Medium]
What happens here when the checkpoint list has already been walked to
last_transaction and only a small number of busy buffers (for example,
1 <= sc->nr_scanned < sc->nr_to_scan) remain on the list?
In fs/jbd2/checkpoint.c, jbd2_journal_shrink_checkpoint_list() loops via
goto again while *nr_to_scan is non-zero and journal->j_shrink_transaction is
non-NULL:
if (transaction != last_transaction) {
journal->j_shrink_transaction = next_transaction;
next_tid = next_transaction->t_tid;
} else {
journal->j_shrink_transaction = NULL;
next_tid = 0;
}
spin_unlock(&journal->j_list_lock);
cond_resched();
if (*nr_to_scan && journal->j_shrink_transaction)
goto again;
Because of that loop, jbd2_journal_shrink_checkpoint_list() only returns with
nr_to_scan > 0 (meaning sc->nr_scanned < sc->nr_to_scan) when the end of the
checkpoint list (last_transaction) has been reached and
journal->j_shrink_transaction has been reset to NULL.
If earlier batches freed the clean buffers and, say, 1 busy buffer remains on
the checkpoint list, jbd2_journal_shrink_checkpoint_list() walks that 1 busy
buffer, reaches last_transaction, resets journal->j_shrink_transaction to
NULL, and returns with sc->nr_scanned == 1.
Since sc->nr_scanned == 1 is non-zero, jbd2_journal_shrink_scan() does not
return SHRINK_STOP. Back in mm/shrinker.c:do_shrink_slab():
ret = shrinker->scan_objects(shrinker, shrinkctl);
if (ret == SHRINK_STOP)
break;
freed += ret;
count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
total_scan -= shrinkctl->nr_scanned;
scanned += shrinkctl->nr_scanned;
total_scan is decremented by only 1 instead of sc->nr_to_scan (128). On the
next iteration of do_shrink_slab(), jbd2_journal_shrink_checkpoint_list()
starts over from the head of journal->j_checkpoint_transactions and rescans
the exact same busy buffer, multiplying the number of scan_objects() calls
and journal->j_list_lock acquisitions on the remaining scan budget by up to
SHRINK_BATCH (128x).
In addition, when the checkpoint list is empty (sc->nr_scanned == 0) and
jbd2_journal_shrink_scan() returns SHRINK_STOP before total_scan is drained,
do_shrink_slab() breaks out with scanned < nr + delta and banks the un-scanned
phantom budget from the stale percpu counter into shrinker->nr_deferred:
next_deferred = max_t(long, (nr + delta - scanned), 0);
next_deferred = min(next_deferred, (2 * freeable));
new_nr = add_nr_deferred(next_deferred, shrinker, shrinkctl);
Would it make more sense to check nr_to_scan != 0 (indicating
jbd2_journal_shrink_checkpoint_list() reached the end of the checkpoint list
before exhausting the batch budget) or to leave sc->nr_scanned untouched so
the stale budget is consumed without deferring phantom work or rescanning the
same busy buffers up to 128 times?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261101-fix-jbd2-shrink-scan-nr-scanned-v5-1-ebd1ecc59a96@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-02 2:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-02 2:03 [PATCH RESEND v5] jbd2: fix shrinker scan budget accounting in jbd2_journal_shrink_scan() Qiliang Yuan
2026-11-02 2:18 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.