From: Hui Zhu <hui.zhu@linux.dev>
To: Baolin Wang <baolin.wang@linux.alibaba.com>,
Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
David Hildenbrand <david@kernel.org>,
Michal Hocko <mhocko@kernel.org>, Qi Zheng <qi.zheng@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Lorenzo Stoakes <ljs@kernel.org>,
Kairui Song <kasong@tencent.com>, Barry Song <baohua@kernel.org>,
Axel Rasmussen <axelrasmussen@google.com>,
Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
Baoquan He <baoquan.he@linux.dev>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: Hui Zhu <zhuhui@kylinos.cn>
Subject: Re: [PATCH v2] mm/mglru: yield the lru_lock earlier in fill/drain_evictable()
Date: Fri, 13 Nov 2026 11:08:12 +0800 [thread overview]
Message-ID: <1c6137c0-297a-4993-b386-6e8a1824d8dc@linux.dev> (raw)
In-Reply-To: <a72af87a-0f10-4717-9d50-2d3480c32caf@linux.alibaba.com>
2026/11/13 10:50, Baolin Wang :
>
>
> On 11/13/26 10:01 AM, Hui Zhu wrote:
>> From: Hui Zhu <zhuhui@kylinos.cn>
>>
>> lru_gen_change_state() migrates folios in batches of up to
>> MAX_LRU_BATCH (4096) folios under lruvec_lock_irq(). A full batch
>> keeps local interrupts off for up to about a millisecond, and CPUs
>> contending for the same lru_lock spin with interrupts disabled for
>> that long.
>>
>> The caller already reschedules between batches, but a batch itself
>> only ends after 4096 folios. Stop it early when a resched is
>> pending, or when the lock has waiters and the current hold has moved
>> at least MIN_LRU_BATCH folios; the caller restarts where it left off.
>> TIF_NEED_RESCHED can still be set with interrupts off: a remote
>> resched_curr() sets it directly in the target's thread_info. The
>> minimum batch avoids lock bouncing under sustained contention, while
>> a waiter now waits ~15us instead of ~1ms.
>>
>> This follows lru_gen_look_around() in this file, security/keys/gc.c
>> and jbd2_log_do_checkpoint(), and commit 6e9be217a3ce ("mm/mglru:
>> use a smaller batch for reclaim").
>>
>> Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
>> ---
>> Changlog:
>> v2:
>> Also yield on lock contention after a minimum batch (Kairui Song).
>>
>> mm/vmscan.c | 10 ++++++++--
>> 1 file changed, 8 insertions(+), 2 deletions(-)
>>
>> diff --git a/mm/vmscan.c b/mm/vmscan.c
>> index 6a1af9b92349..c1231925159f 100644
>> --- a/mm/vmscan.c
>> +++ b/mm/vmscan.c
>> @@ -5533,7 +5533,10 @@ static bool fill_evictable(struct lruvec *lruvec)
>> success = lru_gen_add_folio(lruvec, folio, !active);
>> VM_WARN_ON_ONCE(!success);
>> - if (!--remaining)
>> + /* stop early to shorten the irq-off section */
>> + if (!--remaining || need_resched() ||
>> + (MAX_LRU_BATCH - remaining >= MIN_LRU_BATCH &&
>> + spin_is_contended(&lruvec->lru_lock)))
>> return false;
>
> IIUC, Kairui's point[1] is to just drop the 'MAX_LRU_BATCH - remaining
> >= MIN_LRU_BATCH' condition, no?
>
> if (!--remaining || need_resched() ||
> spin_is_contended(&lruvec->lru_lock))
>
> [1]
> https://lore.kernel.org/all/CAMgjq7CUMuNSdCXZ7_kDdo9+0BFvPNHuO=mq=ct_Q5YUyyckvQ@mail.gmail.com/
>
Not quite. [1] was about the extra `moved` counter in the version
discussed on the v1 thread; Kairui was fine with the rest. Since
`remaining` counts down from MAX_LRU_BATCH and one call is exactly one
lock hold, `MAX_LRU_BATCH - remaining` is the number of folios moved
in the current hold, so the condition is just `moved >= MIN_LRU_BATCH`
without the extra variable.
The minimum-batch test itself is a response to Michal's lock bouncing
concern [2]. Without it, under sustained contention the loop could
yield after every single folio: each yield costs an unlock, a requeue
and a restart (microseconds), while one folio move costs a few hundred
nanoseconds, so the migration could slow down by tens of times, with
cgroup_lock and cpus_read_lock held throughout. With it, each hold
moves at least MIN_LRU_BATCH (64) folios, i.e. does ~15us of work
before it may hand the lock over, while a waiter still waits only
~15us instead of the ~1ms of a full MAX_LRU_BATCH batch.
[2] https://lore.kernel.org/all/avV0IbvSx4iuv0tF@tiehlicka/
Best,
Hui
next prev parent reply other threads:[~2026-11-13 3:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 2:01 Hui Zhu
2026-11-13 2:50 ` Baolin Wang
2026-11-13 3:08 ` Hui Zhu [this message]
2026-11-13 3:22 ` Baolin Wang
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=1c6137c0-297a-4993-b386-6e8a1824d8dc@linux.dev \
--to=hui.zhu@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=baoquan.he@linux.dev \
--cc=david@kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kasong@tencent.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@kernel.org \
--cc=qi.zheng@linux.dev \
--cc=shakeel.butt@linux.dev \
--cc=weixugc@google.com \
--cc=yuanchu@google.com \
--cc=zhuhui@kylinos.cn \
/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®