From: Kefeng Wang <wangkefeng.wang@huawei.com>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Barry Song <21cnbao@gmail.com>, <akpm@linux-foundation.org>,
<linux-mm@kvack.org>, Suren Baghdasaryan <surenb@google.com>,
<linux-kernel@vger.kernel.org>,
Barry Song <v-songbaohua@oppo.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
David Hildenbrand <david@redhat.com>,
Vlastimil Babka <vbabka@suse.cz>, Jann Horn <jannh@google.com>,
Lokesh Gidra <lokeshgidra@google.com>,
Tangquan Zheng <zhengtangquan@oppo.com>,
Qi Zheng <zhengqi.arch@bytedance.com>
Subject: Re: [PATCH v4] mm: use per_vma lock for MADV_DONTNEED
Date: Wed, 5 Nov 2025 09:04:44 +0800 [thread overview]
Message-ID: <545c6e40-db22-4964-868a-74893d6ad03f@huawei.com> (raw)
In-Reply-To: <7998c1f1-fd53-45e9-b748-55043522f1c7@lucifer.local>
On 2025/11/4 23:21, Lorenzo Stoakes wrote:
> On Tue, Nov 04, 2025 at 08:09:58PM +0800, Kefeng Wang wrote:
>>
>>
>> On 2025/11/4 17:01, Lorenzo Stoakes wrote:
>>> On Tue, Nov 04, 2025 at 04:34:35PM +0800, Kefeng Wang wrote:
>>>>> +static enum madvise_lock_mode get_lock_mode(struct madvise_behavior *madv_behavior)
>>>>> {
>>>>> + int behavior = madv_behavior->behavior;
>>>>> +
>>>>> if (is_memory_failure(behavior))
>>>>> - return 0;
>>>>> + return MADVISE_NO_LOCK;
>>>>> - if (madvise_need_mmap_write(behavior)) {
>>>>> + switch (behavior) {
>>>>> + case MADV_REMOVE:
>>>>> + case MADV_WILLNEED:
>>>>> + case MADV_COLD:
>>>>> + case MADV_PAGEOUT:
>>>>> + case MADV_FREE:
>>>>> + case MADV_POPULATE_READ:
>>>>> + case MADV_POPULATE_WRITE:
>>>>> + case MADV_COLLAPSE:
>>>>> + case MADV_GUARD_INSTALL:
>>>>> + case MADV_GUARD_REMOVE:
>>>>> + return MADVISE_MMAP_READ_LOCK;
>>>>> + case MADV_DONTNEED:
>>>>> + case MADV_DONTNEED_LOCKED:
>>>>> + return MADVISE_VMA_READ_LOCK;
>>>>
>>>> I have a question, we will try per-vma lock for dontneed,
>>>> but there is a mmap_assert_locked() during madvise_dontneed_free(),
>>>
>>> Hmm, this is only in the THP PUD huge case, and MADV_FREE is only valid for
>>> anonymous memory, and I think only DAX can have some weird THP PUD case.
>>>
>>> So I don't think we can hit this.
>>
>> Yes, we don't support pud THP for anonymous pages.
>
> Right, so we can't hit this.
>
>>
>>>
>>> In any event, I think this mmap_assert_locked() is mistaken, as we should
>>> only need a VMA lock here.
>>>
>>> So we could replace with a:
>>>
>>> if (!rwsem_is_locked(&tlb->mm->mmap_lock))
>>> vma_assert_locked(vma);
>>>
>>> ?
>>>
>>
>> The pmd dax/anon split don't have assert, for PUD dax, we maybe remove this
>> assert?
>
> Well, we probably do want to assert that we hold a lock.
OK, let's convert to vma_assert_locked.
>
>>
>>
>>
>>
>>>>
>>>> madvise_dontneed_free
>>>> madvise_dontneed_single_vma
>>>> zap_page_range_single_batched
>>>> unmap_single_vma
>>>> unmap_page_range
>>>> zap_pud_range
>>>> mmap_assert_locked
>>>>
>>>> We could fix it by passing the lock_mode into zap_detial and then check
>>>> the right lock here, but I'm not sure whether it is safe to zap page
>>>> only with vma lock?
>>>
>>> It's fine to zap with the VMA lock. You need only hold the VMA stable which
>>> a VMA lock achieves.
>>>
>>> See https://docs.kernel.org/mm/process_addrs.html
>>
>> Thanks, I will learn it.
>
> Hopefully useful, I made it to remind myself of these things as they're very
> fiddly + otherwise I find myself constantly forgetting these details :)
That should be definitely useful :)
>
>>
>>>
>>>>
>>>> And another about 4f8ba33bbdfc ("mm: madvise: use per_vma lock
>>>> for MADV_FREE"), it called walk_page_range_vma() in
>>>> madvise_free_single_vma(), but from link[1] and 5631da56c9a8
>>>> ("fs/proc/task_mmu: read proc/pid/maps under per-vma lock"), it saids
>>>>
>>>> "Note that similar approach would not work for /proc/pid/smaps
>>>> reading as it also walks the page table and that's not RCU-safe"
>>>>
>>>> We could use walk_page_range_vma() instead of walk_page_range() in
>>>> smap_gather_stats(), and same question, why 4f8ba33bbdfc(for MADV_FREEE)
>>>> is safe but not for show_numa_map()/show_smap()?
>>>
>>> We only use walk_page_range() there in case 4 listed in show_smaps_rollup()
>>> where the mmap lock is dropped on contention.
>>
>> Sorry, I mean the walk_page_range() in smap_gather_stats() called by
>> show_smap() from /proc/pid/smaps, not the walk_page_range() in
>> show_smaps_rollup() from /proc/pid/smaps_rollup.
>
> show_smaps()
> -> smap_gather_stats(..., start = 0)
> -> walk_page_vma()
>
> Because:
>
> if (!start)
> walk_page_vma(vma, ops, mss);
>
> The only case where start is non-zero is show_smaps_rollup() case 4. So we are
> already using walk_page_vma() here right?
>
> I may be missing something here :)
You are right, I don't check start value :(
>
>>
>>
>>>
>>>>
>>>> Thanks.
>>>>
>>>> [1] https://lkml.kernel.org/r/20250719182854.3166724-1-surenb@google.com
>>>
>>> AFAICT That's referring to a previous approach that tried to walk
>>> /proc/$pid/swaps under RCU _alone_ without VMA locks. This is not safe as
>>> page tables can be yanked from under you not under RCU.
>>
>> But for now it tries per-vma lock or fallback to mmap lock, not lockless, so
>> do you mean we could try per-vma lock for /proc/pid/numa_maps or
>> /proc/pid/smaps ?
>
> Probably we could, but I'm not sure if it'd be really worth it, since traversing
> page tables is a very heavy operation and so optimising it against contention
> like this seems probably not all that worth it?
>
> Suren maybe could comment on this.
They only operate a single vma(walk_page_vma), I think it is always
better if we could only hold the vma lock, but wait for Suren comment on it.
next prev parent reply other threads:[~2025-11-05 1:04 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-07 22:01 Barry Song
2025-06-09 7:21 ` Qi Zheng
2025-06-17 13:38 ` Lorenzo Stoakes
2025-06-18 2:25 ` Lance Yang
2025-06-18 9:52 ` Barry Song
2025-06-18 10:18 ` David Hildenbrand
2025-06-18 10:30 ` Barry Song
2025-06-18 10:32 ` Barry Song
2025-06-18 13:05 ` Lance Yang
2025-06-18 13:13 ` David Hildenbrand
2025-06-18 10:11 ` Barry Song
2025-06-18 10:33 ` Lorenzo Stoakes
2025-06-18 10:36 ` Barry Song
2025-08-04 0:58 ` Lai, Yi
2025-08-04 7:19 ` Barry Song
2025-08-04 7:57 ` David Hildenbrand
2025-08-04 8:26 ` Qi Zheng
2025-08-04 8:30 ` David Hildenbrand
2025-08-04 8:49 ` Lai, Yi
2025-08-04 9:15 ` Barry Song
2025-08-04 9:35 ` Qi Zheng
2025-08-04 9:52 ` Qi Zheng
2025-08-04 10:04 ` Barry Song
2025-08-04 21:48 ` Barry Song
2025-08-05 2:52 ` Lai, Yi
2025-08-04 8:19 ` Barry Song
2025-11-04 8:34 ` Kefeng Wang
2025-11-04 9:01 ` Lorenzo Stoakes
2025-11-04 12:09 ` Kefeng Wang
2025-11-04 15:21 ` Lorenzo Stoakes
2025-11-05 1:04 ` Kefeng Wang [this message]
2025-11-17 23:35 ` Suren Baghdasaryan
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=545c6e40-db22-4964-868a-74893d6ad03f@huawei.com \
--to=wangkefeng.wang@huawei.com \
--cc=21cnbao@gmail.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=jannh@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lokeshgidra@google.com \
--cc=lorenzo.stoakes@oracle.com \
--cc=surenb@google.com \
--cc=v-songbaohua@oppo.com \
--cc=vbabka@suse.cz \
--cc=zhengqi.arch@bytedance.com \
--cc=zhengtangquan@oppo.com \
/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®