From patchwork Fri May 12 23:57:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 13239991 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 666BEC77B7C for ; Fri, 12 May 2023 23:58:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E4F786B0072; Fri, 12 May 2023 19:58:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E26786B0074; Fri, 12 May 2023 19:58:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CA0F26B0078; Fri, 12 May 2023 19:58:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id BABA06B0072 for ; Fri, 12 May 2023 19:58:18 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 863C341094 for ; Fri, 12 May 2023 23:58:18 +0000 (UTC) X-FDA: 80783269476.03.FD10DD2 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf22.hostedemail.com (Postfix) with ESMTP id BE374C0005 for ; Fri, 12 May 2023 23:58:15 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="f5hclik/"; spf=pass (imf22.hostedemail.com: domain of 3ltJeZAMKCN8QDDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--pcc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3ltJeZAMKCN8QDDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--pcc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1683935895; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LVFc+Brp/nv5bcVUuCiFrcDkBMn4WnVlAUQLAdw9K1k=; b=mFHTWqZDbChus+ybWMnSwB8QQiXQFh57TLiJRp9j4Z+Ay4w47SrjnfgfRKHmIv0qtpMhAb 6rtHx5rcZHP0aRKlR/gvMswY1LCPtfhcDeZsr5+134aTWPZUqI4iXJkbOGYDKt2FyYXo9q gSs2+zRWfAne/sC5k6/wtFCsn3ejoXg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683935895; a=rsa-sha256; cv=none; b=KI1XFn0aajwM+FCDI1jUtL6Wi7lpDhaY+PUswr26DZ+qPlKX2DNbBXjFZG/frW5b8pRaM/ XRLcsnmbycef+IeySaLwENoJdeqJifjBIOX8zwJrNat56A5Pq+34RvmDRZjNN37hULCw2U 0kRTQ4fEUDSkEIWdT16YhPnuN3FRC40= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="f5hclik/"; spf=pass (imf22.hostedemail.com: domain of 3ltJeZAMKCN8QDDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--pcc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3ltJeZAMKCN8QDDHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--pcc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-55ffc3d2b63so107875907b3.2 for ; Fri, 12 May 2023 16:58:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683935895; x=1686527895; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LVFc+Brp/nv5bcVUuCiFrcDkBMn4WnVlAUQLAdw9K1k=; b=f5hclik/Ew6KC4whYAG5VntaAWwKmaBWXX4bCpgRdYjBpbSj5Apr3PkSoK7/QAJd0N RpHxTIRW/bLimRvcbUmZjobDE/Eutpj6tvgC/Rdg9nOt+ZvW2UeGjSSNnpNfchKgE9qV bfsrP2dthFHNMRT6c5aCQw0Ff/JTyx0DIAtRow1fvLqu2M2UZFqFRteLQ4IPrCBhdwYv ZUWPKO0rlZI6hiVGzQF5WVmuVs1EGnbfoUeE09/HLSnj37gvPWFw6+cmVnJFfHK5WbAH YWYFfgDih13LoVhaHD+4rYuds+BE6rKkLJ/AoCA0fp0U2j35styXF7xvUDDBUZNd5sjd vJuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683935895; x=1686527895; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LVFc+Brp/nv5bcVUuCiFrcDkBMn4WnVlAUQLAdw9K1k=; b=KBNvlxRJXyxqM+1p38ICO1inRi8AoF70IhipzfJUxABECjpwb5hapGnQovTbzCD8oA uUpZ2RPVrr1jW5ch4B8OfLoNWUSwHX5nzTnlzwaGTRYaF1KmcqD52LNed/msOftbOhvZ eQaC7HYsLkmadi1ocOBzgkHALjHMRIbT0DG5wRqKNKCauBXWq8CEbQWKQtG3oa2VMDz9 ZWjMF61ShfN29iAaueh2VFwyvhoAu2iRfNjZ6zM2ZVLuWxRq5rKqlzE4o/3HYs686QG5 BrBov48IpNEnsi88nroqnINW30AoE+7ZihTSY3mADnFdYNlvCrjTBnROxQ/sr8qUd7Q0 qCPg== X-Gm-Message-State: AC+VfDyPMCIXzs8ild9V8dzd56FZvc96n9ZEvP1anQKtl0S+9j++g/cv yJtx9/MeN72A7YGYbzbgLwyfDVk= X-Google-Smtp-Source: ACHHUZ7VMsxVospQ1XLBd1n7/FkUbi+qPyj+Q+Eihu+tg6ttj97Cmt/cOaDOuPcaz9q0znnK8MBI35s= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2d3:205:ff6:108b:739d:6a1c]) (user=pcc job=sendgmr) by 2002:a81:b285:0:b0:559:f1b0:6eb with SMTP id q127-20020a81b285000000b00559f1b006ebmr16091480ywh.4.1683935894947; Fri, 12 May 2023 16:58:14 -0700 (PDT) Date: Fri, 12 May 2023 16:57:50 -0700 In-Reply-To: <20230512235755.1589034-1-pcc@google.com> Message-Id: <20230512235755.1589034-2-pcc@google.com> Mime-Version: 1.0 References: <20230512235755.1589034-1-pcc@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Subject: [PATCH 1/3] mm: Move arch_do_swap_page() call to before swap_free() From: Peter Collingbourne To: Catalin Marinas Cc: Peter Collingbourne , " =?utf-8?b?UXVuLXdlaSBMaW4gKA==?= =?utf-8?b?5p6X576k5bS0KQ==?= " , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "surenb@google.com" , "david@redhat.com" , " =?utf-8?b?Q2hpbndlbiBDaGFuZyAo?= =?utf-8?b?5by16Yym5paHKQ==?= " , "kasan-dev@googlegroups.com" , " =?utf-8?b?S3Vhbi1ZaW5nIExlZSAo5p2O5Yag56mOKQ==?= " , " =?utf-8?b?Q2FzcGVyIExpICjmnY7kuK3mpq4p?= " , "gregkh@linuxfoundation.org" , vincenzo.frascino@arm.com, Alexandru Elisei , will@kernel.org, eugenis@google.com, Steven Price , stable@vger.kernel.org X-Rspamd-Queue-Id: BE374C0005 X-Stat-Signature: j4zdskhraopgxd36m3ru3ig3irurohg7 X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1683935895-659138 X-HE-Meta: U2FsdGVkX1+cRfKxSJ6t3y63deCjoc4Q6hhB4bcj6Y9waoGuMrd7MccuAOJx/tVShQMh/jbXBRRA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Qz68a2Sb3s7rLv4tYPvIBfGxHEFwS1w4d8c1+JFD/afZh4wckLeYv3uXwkRqbiCxX9Z9IZEbuvJX27fLzY7XsWMjC0TOnFmuX0nP5XxJT2lVwoNPl4cgTYqMdy9qBqXR3+jKEvZpaDABI5ibowCyEmqJThscYxX5Pe1DfH3+10A+ffErpY7xQ2KSmiYnqdcjmXAk74iTzykL1+GDyF+aqZ4FqlsB5GJu6yjtT0V+7F7PSjw58j507vtQQRkbMSA76dSdudm0tCaUrGtIZwKEGTYDOPn7weB/wxkZn6XdMi1cbneHGjIQW68O72D7/VYCp0wsw2J0tiVbxC5UEw/o6ChgE3GhJm/SmR9PWfnUmwpAkF1dfF3N9mBOzs3QtLkB6lwQNJTR5JvEPIgmYehvyOsCb8t6wmzkVCl0C/tUNfa5DeKl5erx9Z9XNXBSZLhno1U+Dq3OqZk5CFUEiVyySldx5SJ4lN4zjJpjgD3VL78CY8iRnR4D9YTDxNsLYXxer/n5E5bpuf8LeRBTjACmTcKcJzU6EB2sopYd056VEJQZdGZxV6ZKWLEk5E16q4DuETMp16CV/DMVYJZ+SDwvEtKov+awbSDM3QHTqHFG4Y7pQEZArPgfoBixsw8f6KaQyVHvCso+5VngybPb6IakyV7QUFsYarDwnnyZOcvZqHGC7/NZbkVj1uXRQROmE X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Suggested-by: Catalin Marinas Link: https://linux-review.googlesource.com/id/I6470efa669e8bd2f841049b8c61020c510678965 Cc: # 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") --- mm/memory.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 01a23ad48a04..83268d287ff1 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3914,19 +3914,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) } } - /* - * Remove the swap entry and conditionally try to free up the swapcache. - * We're already holding a reference on the page but haven't mapped it - * yet. - */ - swap_free(entry); - if (should_try_to_free_swap(folio, vma, vmf->flags)) - folio_free_swap(folio); - - inc_mm_counter(vma->vm_mm, MM_ANONPAGES); - dec_mm_counter(vma->vm_mm, MM_SWAPENTS); pte = mk_pte(page, vma->vm_page_prot); - /* * Same logic as in do_wp_page(); however, optimize for pages that are * certainly not shared either because we just allocated them without @@ -3946,8 +3934,21 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) pte = pte_mksoft_dirty(pte); if (pte_swp_uffd_wp(vmf->orig_pte)) pte = pte_mkuffd_wp(pte); + arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); vmf->orig_pte = pte; + /* + * Remove the swap entry and conditionally try to free up the swapcache. + * We're already holding a reference on the page but haven't mapped it + * yet. + */ + swap_free(entry); + if (should_try_to_free_swap(folio, vma, vmf->flags)) + folio_free_swap(folio); + + inc_mm_counter(vma->vm_mm, MM_ANONPAGES); + dec_mm_counter(vma->vm_mm, MM_SWAPENTS); + /* ksm created a completely new copy */ if (unlikely(folio != swapcache && swapcache)) { page_add_new_anon_rmap(page, vma, vmf->address); @@ -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) {