From: David Hildenbrand <david@redhat.com>
To: Peter Collingbourne <pcc@google.com>
Cc: "Catalin Marinas" <catalin.marinas@arm.com>,
"Qun-wei Lin (林群崴)" <Qun-wei.Lin@mediatek.com>,
linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org,
"surenb@google.com" <surenb@google.com>,
"Chinwen Chang (張錦文)" <chinwen.chang@mediatek.com>,
"kasan-dev@googlegroups.com" <kasan-dev@googlegroups.com>,
"Kuan-Ying Lee (李冠穎)" <Kuan-Ying.Lee@mediatek.com>,
"Casper Li (李中榮)" <casper.li@mediatek.com>,
"gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
vincenzo.frascino@arm.com,
"Alexandru Elisei" <alexandru.elisei@arm.com>,
will@kernel.org, eugenis@google.com,
"Steven Price" <steven.price@arm.com>,
stable@vger.kernel.org
Subject: Re: [PATCH 1/3] mm: Move arch_do_swap_page() call to before swap_free()
Date: Wed, 17 May 2023 10:34:41 +0200 [thread overview]
Message-ID: <a9312c59-215a-1213-459e-bf42af555f0c@redhat.com> (raw)
In-Reply-To: <ZGLr7CzUL0A+mCRp@google.com>
On 16.05.23 04:35, Peter Collingbourne wrote:
> On Mon, May 15, 2023 at 05:16:09PM -0700, Peter Collingbourne wrote:
>> On Sat, May 13, 2023 at 05:29:53AM +0200, David Hildenbrand wrote:
>>> On 13.05.23 01:57, Peter Collingbourne wrote:
>>>> Commit c145e0b47c77 ("mm: streamline COW logic in do_swap_page()") moved
>>>> the call to swap_free() before the call to set_pte_at(), which meant that
>>>> the MTE tags could end up being freed before set_pte_at() had a chance
>>>> to restore them. One other possibility was to hook arch_do_swap_page(),
>>>> but this had a number of problems:
>>>>
>>>> - The call to the hook was also after swap_free().
>>>>
>>>> - The call to the hook was after the call to set_pte_at(), so there was a
>>>> racy window where uninitialized metadata may be exposed to userspace.
>>>> This likely also affects SPARC ADI, which implements this hook to
>>>> restore tags.
>>>>
>>>> - As a result of commit 1eba86c096e3 ("mm: change page type prior to
>>>> adding page table entry"), we were also passing the new PTE as the
>>>> oldpte argument, preventing the hook from knowing the swap index.
>>>>
>>>> Fix all of these problems by moving the arch_do_swap_page() call before
>>>> the call to free_page(), and ensuring that we do not set orig_pte until
>>>> after the call.
>>>>
>>>> Signed-off-by: Peter Collingbourne <pcc@google.com>
>>>> Suggested-by: Catalin Marinas <catalin.marinas@arm.com>
>>>> Link: https://linux-review.googlesource.com/id/I6470efa669e8bd2f841049b8c61020c510678965
>>>> Cc: <stable@vger.kernel.org> # 6.1
>>>> Fixes: ca827d55ebaa ("mm, swap: Add infrastructure for saving page metadata on swap")
>>>> Fixes: 1eba86c096e3 ("mm: change page type prior to adding page table entry")
>>>
>>> I'm confused. You say c145e0b47c77 changed something (which was after above
>>> commits), indicate that it fixes two other commits, and indicate "6.1" as
>>> stable which does not apply to any of these commits.
>>
>> Sorry, the situation is indeed a bit confusing.
>>
>> - In order to make the arch_do_swap_page() hook suitable for fixing the
>> bug introduced by c145e0b47c77, patch 1 addresses a number of issues,
>> including fixing bugs introduced by ca827d55ebaa and 1eba86c096e3,
>> but we haven't fixed the c145e0b47c77 bug yet, so there's no Fixes:
>> tag for it yet.
>>
>> - Patch 2, relying on the fixes in patch 1, makes MTE install an
>> arch_do_swap_page() hook (indirectly, by making arch_swap_restore()
>> also hook arch_do_swap_page()), thereby fixing the c145e0b47c77 bug.
>>
>> - 6.1 is the first stable version in which all 3 commits in my Fixes: tags
>> are present, so that is the version that I've indicated in my stable
>> tag for this series. In theory patch 1 could be applied to older kernel
>> versions, but it wouldn't fix any problems that we are facing with MTE
>> (because it only fixes problems relating to the arch_do_swap_page()
>> hook, which older kernel versions don't hook with MTE), and there are
>> some merge conflicts if we go back further anyway. If the SPARC folks
>> (the previous only user of this hook) want to fix these issues with ADI,
>> they can propose their own backport.
>>
>>>> @@ -3959,7 +3960,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)
>>>> VM_BUG_ON(!folio_test_anon(folio) ||
>>>> (pte_write(pte) && !PageAnonExclusive(page)));
>>>> set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte);
>>>> - arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte);
>>>> folio_unlock(folio);
>>>> if (folio != swapcache && swapcache) {
>>>
>>>
>>> You are moving the folio_free_swap() call after the folio_ref_count(folio)
>>> == 1 check, which means that such (previously) swapped pages that are
>>> exclusive cannot be detected as exclusive.
>>
>> Ack. I will fix this in v2.
>
> I gave this some thought and concluded that the added complexity needed
> to make this hook suitable for arm64 without breaking sparc probably
> isn't worth it in the end, and as I explained in patch 2, sparc ought
> to be moving away from this hook anyway. So in v2 I replaced patches 1
> and 2 with a patch that adds a direct call to the arch_swap_restore()
> hook before the call to swap_free().
As a side note, I recall that sparc code might be a bit fragile and
eventually broken already (arch_unmap_one()):
https://lkml.kernel.org/r/d98bd1f9-e9b7-049c-7bde-3348b074eb18@redhat.com
--
Thanks,
David / dhildenb
next prev parent reply other threads:[~2023-05-17 8:34 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-12 23:57 [PATCH 0/3] mm: Fix bug affecting swapping in MTE tagged pages Peter Collingbourne
2023-05-12 23:57 ` [PATCH 1/3] mm: Move arch_do_swap_page() call to before swap_free() Peter Collingbourne
2023-05-13 3:29 ` David Hildenbrand
2023-05-15 17:34 ` Catalin Marinas
2023-05-15 23:40 ` Peter Collingbourne
2023-05-16 12:35 ` David Hildenbrand
2023-05-17 1:57 ` Peter Collingbourne
2023-05-17 8:30 ` David Hildenbrand
2023-05-18 20:06 ` Peter Collingbourne
2023-05-19 9:21 ` David Hildenbrand
2023-05-19 16:21 ` Catalin Marinas
2023-05-16 12:30 ` David Hildenbrand
2023-05-17 1:37 ` Peter Collingbourne
2023-05-17 8:31 ` David Hildenbrand
2023-05-16 0:16 ` Peter Collingbourne
2023-05-16 2:35 ` Peter Collingbourne
2023-05-17 8:34 ` David Hildenbrand [this message]
2023-05-16 12:40 ` David Hildenbrand
2023-05-17 2:13 ` Peter Collingbourne
2023-05-12 23:57 ` [PATCH 2/3] mm: Call arch_swap_restore() from arch_do_swap_page() and deprecate the latter Peter Collingbourne
2023-05-13 3:34 ` David Hildenbrand
2023-05-12 23:57 ` [PATCH 3/3] arm64: mte: Simplify swap tag restoration logic and fix uninitialized tag issue Peter Collingbourne
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=a9312c59-215a-1213-459e-bf42af555f0c@redhat.com \
--to=david@redhat.com \
--cc=Kuan-Ying.Lee@mediatek.com \
--cc=Qun-wei.Lin@mediatek.com \
--cc=alexandru.elisei@arm.com \
--cc=casper.li@mediatek.com \
--cc=catalin.marinas@arm.com \
--cc=chinwen.chang@mediatek.com \
--cc=eugenis@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=kasan-dev@googlegroups.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pcc@google.com \
--cc=stable@vger.kernel.org \
--cc=steven.price@arm.com \
--cc=surenb@google.com \
--cc=vincenzo.frascino@arm.com \
--cc=will@kernel.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