From: Ryan Roberts <ryan.roberts@arm.com>
To: Lance Yang <ioworker0@gmail.com>
Cc: Barry Song <21cnbao@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@redhat.com>,
Matthew Wilcox <willy@infradead.org>,
Huang Ying <ying.huang@intel.com>, Gao Xiang <xiang@kernel.org>,
Yu Zhao <yuzhao@google.com>, Yang Shi <shy828301@gmail.com>,
Michal Hocko <mhocko@suse.com>,
Kefeng Wang <wangkefeng.wang@huawei.com>,
Chris Li <chrisl@kernel.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 6/6] mm: madvise: Avoid split during MADV_PAGEOUT and MADV_COLD
Date: Thu, 21 Mar 2024 15:24:33 +0000 [thread overview]
Message-ID: <9930c86a-c0c8-4112-9122-0e4faca475f5@arm.com> (raw)
In-Reply-To: <CAK1f24m+oQgBYdxUaTASMtJpnUEQvWh-t_kVw7CJzVM4Siddcg@mail.gmail.com>
On 21/03/2024 14:55, Lance Yang wrote:
> On Thu, Mar 21, 2024 at 9:38 PM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>
>>>>>>>>>> - VM_BUG_ON_FOLIO(folio_test_large(folio), folio);
>>>>>>>>>> -
>>>>>>>>>> - if (!pageout && pte_young(ptent)) {
>>>>>>>>>> - ptent = ptep_get_and_clear_full(mm, addr, pte,
>>>>>>>>>> - tlb->fullmm);
>>>>>>>>>> - ptent = pte_mkold(ptent);
>>>>>>>>>> - set_pte_at(mm, addr, pte, ptent);
>>>>>>>>>> - tlb_remove_tlb_entry(tlb, pte, addr);
>>>>>>>>>> + if (!pageout) {
>>>>>>>>>> + for (; nr != 0; nr--, pte++, addr += PAGE_SIZE) {
>>>>>>>>>> + if (ptep_test_and_clear_young(vma, addr, pte))
>>>>>>>>>> + tlb_remove_tlb_entry(tlb, pte, addr);
>>>>>>>
>>>>>>> IIRC, some of the architecture(ex, PPC) don't update TLB with set_pte_at and
>>>>>>> tlb_remove_tlb_entry. So, didn't we consider remapping the PTE with old after
>>>>>>> pte clearing?
>>>>>>
>>>>>> Sorry Lance, I don't understand this question, can you rephrase? Are you saying
>>>>>> there is a good reason to do the original clear-mkold-set for some arches?
>>>>>
>>>>> IIRC, some of the architecture(ex, PPC) don't update TLB with
>>>>> ptep_test_and_clear_young()
>>>>> and tlb_remove_tlb_entry().
>>
>> Afraid I'm still struggling with this comment. Do you mean to say that powerpc
>> invalidates the TLB entry as part of the call to ptep_test_and_clear_young()? So
>> tlb_remove_tlb_entry() would be redundant here, and likely cause performance
>> degradation on that architecture?
>
> I just thought that using ptep_test_and_clear_young() instead of
> ptep_get_and_clear_full() + pte_mkold() might not be correct.
> However, it's most likely that I was mistaken :(
OK, I'm pretty confident that my usage is correct.
>
> I also have a question. Why aren't we using ptep_test_and_clear_young() in
> madvise_cold_or_pageout_pte_range(), but instead
> ptep_get_and_clear_full() + pte_mkold() as we did previously.
>
> /*
> * Some of architecture(ex, PPC) don't update TLB
> * with set_pte_at and tlb_remove_tlb_entry so for
> * the portability, remap the pte with old|clean
> * after pte clearing.
> */
Ahh, I see; this is a comment from madvise_free_pte_range() I don't quite
understand that comment. I suspect it might be out of date, or saying that doing
set_pte_at(pte_mkold(ptep_get(ptent))) is not correct because it is not atomic
and the HW could set the dirty bit between the get and the set. Doing the atomic
ptep_get_and_clear_full() means you go via a pte_none() state, so if the TLB is
racing it will see the entry isn't valid and fault.
Note that madvise_free_pte_range() is trying to clear both the access and dirty
bits, whereas madvise_cold_or_pageout_pte_range() is only trying to clear the
access bit. There is a special helper to clear the access bit atomically -
ptep_test_and_clear_young() - but there is no helper to clear the access *and*
dirty bit, I don't believe. There is ptep_set_access_flags(), but that sets
flags to a "more permissive setting" (i.e. allows setting the flags, not
clearing them). Perhaps this constraint can be relaxed given we will follow up
with an explicit TLBI - it would require auditing all the implementations.
>
> According to this comment from madvise_free_pte_range. IIUC, we need to
> call ptep_get_and_clear_full() to clear the PTE, and then remap the
> PTE with old|clean.
>
> Thanks,
> Lance
>
>>
>> IMHO, ptep_test_and_clear_young() really shouldn't be invalidating the TLB
>> entry, that's what ptep_clear_flush_young() is for.
>>
>> But I do see that for some cases of the 32-bit ppc, there appears to be a flush:
>>
>> #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
>> static inline int __ptep_test_and_clear_young(struct mm_struct *mm,
>> unsigned long addr, pte_t *ptep)
>> {
>> unsigned long old;
>> old = pte_update(mm, addr, ptep, _PAGE_ACCESSED, 0, 0);
>> if (old & _PAGE_HASHPTE)
>> flush_hash_entry(mm, ptep, addr); <<<<<<<<
>>
>> return (old & _PAGE_ACCESSED) != 0;
>> }
>> #define ptep_test_and_clear_young(__vma, __addr, __ptep) \
>> __ptep_test_and_clear_young((__vma)->vm_mm, __addr, __ptep)
>>
>> Is that what you are describing? Does any anyone know why flush_hash_entry() is
>> called? I'd say that's a bug in ppc and not a reason not to use
>> ptep_test_and_clear_young() in the common code!
>>
>> Thanks,
>> Ryan
>>
>>
>>>>
>>>> Err, I assumed tlb_remove_tlb_entry() meant "invalidate the TLB entry for this
>>>> address please" - albeit its deferred and batched. I'll look into this.
>>>>
>>>>>
>>>>> In my new patch[1], I use refresh_full_ptes() and
>>>>> tlb_remove_tlb_entries() to batch-update the
>>>>> access and dirty bits.
>>>>
>>>> I want to avoid the per-pte clear-modify-set approach, because this doesn't
>>>> perform well on arm64 when using contpte mappings; it will cause the contpe
>>>> mapping to be unfolded by the first clear that touches the contpte block, then
>>>> refolded by the last set to touch the block. That's expensive.
>>>> ptep_test_and_clear_young() doesn't suffer that problem.
>>>
>>> Thanks for explaining. I got it.
>>>
>>> I think that other architectures will benefit from the per-pte clear-modify-set
>>> approach. IMO, refresh_full_ptes() can be overridden by arm64.
>>>
>>> Thanks,
>>> Lance
>>>>
>>>>>
>>>>> [1] https://lore.kernel.org/linux-mm/20240316102952.39233-1-ioworker0@gmail.com
>>>>>
>>>>> Thanks,
>>>>> Lance
>>>>>
>>>>>>
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Lance
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>>>> + }
>>>>>>>>>
>>>>>>>>> This looks so smart. if it is not pageout, we have increased pte
>>>>>>>>> and addr here; so nr is 0 and we don't need to increase again in
>>>>>>>>> for (; addr < end; pte += nr, addr += nr * PAGE_SIZE)
>>>>>>>>>
>>>>>>>>> otherwise, nr won't be 0. so we will increase addr and
>>>>>>>>> pte by nr.
>>>>>>>>
>>>>>>>> Indeed. I'm hoping that Lance is able to follow a similar pattern for
>>>>>>>> madvise_free_pte_range().
>>>>>>>>
>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>>> /*
>>>>>>>>>> --
>>>>>>>>>> 2.25.1
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Overall, LGTM,
>>>>>>>>>
>>>>>>>>> Reviewed-by: Barry Song <v-songbaohua@oppo.com>
>>>>>>>>
>>>>>>>> Thanks!
>>>>>>>>
>>>>>>>>
>>>>>>
>>>>
>>
next prev parent reply other threads:[~2024-03-21 15:24 UTC|newest]
Thread overview: 73+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-11 15:00 [PATCH v4 0/6] Swap-out mTHP without splitting Ryan Roberts
2024-03-11 15:00 ` [PATCH v4 1/6] mm: swap: Remove CLUSTER_FLAG_HUGE from swap_cluster_info:flags Ryan Roberts
2024-03-11 15:00 ` [PATCH v4 2/6] mm: swap: free_swap_and_cache_nr() as batched free_swap_and_cache() Ryan Roberts
2024-03-20 11:10 ` Ryan Roberts
2024-03-20 14:13 ` David Hildenbrand
2024-03-20 14:21 ` Ryan Roberts
2024-03-11 15:00 ` [PATCH v4 3/6] mm: swap: Simplify struct percpu_cluster Ryan Roberts
2024-03-12 7:52 ` Huang, Ying
2024-03-12 8:51 ` Ryan Roberts
2024-03-13 1:34 ` Huang, Ying
2024-03-11 15:00 ` [PATCH v4 4/6] mm: swap: Allow storage of all mTHP orders Ryan Roberts
2024-03-12 7:51 ` Huang, Ying
2024-03-12 9:40 ` Ryan Roberts
2024-03-13 1:33 ` Huang, Ying
2024-03-20 12:22 ` Ryan Roberts
2024-03-21 4:39 ` Huang, Ying
2024-03-21 12:21 ` Ryan Roberts
2024-03-22 2:38 ` Can you help us on memory barrier usage? (was Re: [PATCH v4 4/6] mm: swap: Allow storage of all mTHP orders) Huang, Ying
2024-03-22 9:23 ` Ryan Roberts
2024-03-25 3:20 ` Huang, Ying
2024-03-22 13:19 ` Chris Li
2024-03-23 2:11 ` Akira Yokosawa
2024-03-25 0:01 ` Paul E. McKenney
2024-03-25 3:16 ` Huang, Ying
2024-03-26 17:08 ` Ryan Roberts
2024-03-25 3:00 ` Huang, Ying
2024-03-22 2:39 ` [PATCH v4 4/6] mm: swap: Allow storage of all mTHP orders Huang, Ying
2024-03-22 9:39 ` Ryan Roberts
2024-03-11 15:00 ` [PATCH v4 5/6] mm: vmscan: Avoid split during shrink_folio_list() Ryan Roberts
2024-03-11 22:30 ` Barry Song
2024-03-12 8:12 ` Ryan Roberts
2024-03-12 8:40 ` Barry Song
2024-03-15 10:43 ` David Hildenbrand
2024-03-15 10:49 ` Ryan Roberts
2024-03-15 11:12 ` David Hildenbrand
2024-03-15 11:38 ` Ryan Roberts
2024-03-18 2:16 ` Huang, Ying
2024-03-18 10:00 ` Yin, Fengwei
2024-03-18 10:05 ` David Hildenbrand
2024-03-18 15:35 ` Ryan Roberts
2024-03-18 15:36 ` Ryan Roberts
2024-03-19 2:20 ` Yin Fengwei
2024-03-19 14:40 ` Ryan Roberts
2024-03-19 2:31 ` Yin Fengwei
2024-03-11 15:00 ` [PATCH v4 6/6] mm: madvise: Avoid split during MADV_PAGEOUT and MADV_COLD Ryan Roberts
2024-03-13 7:19 ` Barry Song
2024-03-13 9:03 ` Ryan Roberts
2024-03-13 9:16 ` Barry Song
2024-03-13 9:36 ` Ryan Roberts
2024-03-13 10:37 ` Barry Song
2024-03-13 11:08 ` Ryan Roberts
2024-03-13 11:37 ` Barry Song
2024-03-13 12:02 ` Ryan Roberts
2024-03-13 9:19 ` Lance Yang
2024-03-13 14:02 ` Lance Yang
2024-03-20 13:49 ` Ryan Roberts
2024-03-20 14:35 ` Lance Yang
2024-03-20 17:38 ` Ryan Roberts
2024-03-21 1:38 ` Lance Yang
2024-03-21 13:38 ` Ryan Roberts
2024-03-21 14:55 ` Lance Yang
2024-03-21 15:24 ` Ryan Roberts [this message]
2024-03-22 0:56 ` Lance Yang
[not found] ` <ffeee7da-e625-40dc-8da8-b70e4e6ef935@redhat.com>
2024-03-15 10:55 ` Ryan Roberts
2024-03-15 11:13 ` David Hildenbrand
2024-03-20 13:57 ` Ryan Roberts
2024-03-20 14:09 ` David Hildenbrand
2024-03-12 8:01 ` [PATCH v4 0/6] Swap-out mTHP without splitting Huang, Ying
2024-03-12 8:49 ` Ryan Roberts
2024-03-12 13:56 ` Ryan Roberts
2024-03-13 1:15 ` Huang, Ying
2024-03-13 8:50 ` Ryan Roberts
2024-03-12 8:45 ` Ryan Roberts
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=9930c86a-c0c8-4112-9122-0e4faca475f5@arm.com \
--to=ryan.roberts@arm.com \
--cc=21cnbao@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=chrisl@kernel.org \
--cc=david@redhat.com \
--cc=ioworker0@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
--cc=shy828301@gmail.com \
--cc=wangkefeng.wang@huawei.com \
--cc=willy@infradead.org \
--cc=xiang@kernel.org \
--cc=ying.huang@intel.com \
--cc=yuzhao@google.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