From patchwork Fri Sep 22 11:58:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13395667 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 64C40CD4945 for ; Fri, 22 Sep 2023 11:58:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 77B926B02C2; Fri, 22 Sep 2023 07:58:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 72C306B02C3; Fri, 22 Sep 2023 07:58:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F4096B02C4; Fri, 22 Sep 2023 07:58:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5063C6B02C2 for ; Fri, 22 Sep 2023 07:58:19 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 06432810BD for ; Fri, 22 Sep 2023 11:58:19 +0000 (UTC) X-FDA: 81264085518.29.B74EA85 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf29.hostedemail.com (Postfix) with ESMTP id 118E8120013 for ; Fri, 22 Sep 2023 11:58:16 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf29.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695383897; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=31f4aUAVz/0Q33PRfDUN8I5Zm8yBJPQTdRAC553VfNY=; b=kj3hT2txCmPe3KiXpPB8eQnYdGhrrlWfKvf64Lc7k6jMR3kX9761que+wjnC7Brxvnrjif M3Mmjq+QsS6ZByhaBfthPezvu+APZvjOX+4phfjogmhDBK33gNwxS91VjDjYNtPL5JjNH8 2r5aSsVEaNE6p3wZhGYfq5tgd14mJ3Y= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf29.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695383897; a=rsa-sha256; cv=none; b=aBJyEQn+kOfnPBlRAU7Pr4TypuL9cEww1zIbO/0Mn385FngkQKzZP3OUFcexmSh4gaUZjd V57ZiPcRUirDjUdrR+a92IOZ83JLQiRJXwI5XCn8aMhstRSydS/oeIsAXJwoTskIt28bDg 4jLup0Uj2xzykpg3RNdt3jZgx72RH7s= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 13FA5DA7; Fri, 22 Sep 2023 04:58:53 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 9C73D3F67D; Fri, 22 Sep 2023 04:58:11 -0700 (PDT) From: Ryan Roberts To: Catalin Marinas , Will Deacon , "James E.J. Bottomley" , Helge Deller , Nicholas Piggin , Christophe Leroy , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Gerald Schaefer , "David S. Miller" , Arnd Bergmann , Mike Kravetz , Muchun Song , SeongJae Park , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , Lorenzo Stoakes , Anshuman Khandual , Peter Xu , Axel Rasmussen , Qi Zheng Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org Subject: [PATCH v2 0/2] Fix set_huge_pte_at() panic on arm64 Date: Fri, 22 Sep 2023 12:58:02 +0100 Message-Id: <20230922115804.2043771-1-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 118E8120013 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: drgcmwasbahaqz8kqs4guuns4rmadmek X-HE-Tag: 1695383896-163908 X-HE-Meta: U2FsdGVkX1+63HNfzPTV2C0xS4hVNZmjwaBk8i2vHochhL7QoamDbzWQsc9n6r9269ng5IvevEEa7jNjSXr7GutQjHdz/oI6qxcF+Fu+ahzGXhTTdM6sLrpZZyNAxAcbkREzKcVi5ISnEcOold1ol2f5kGbjH8A93KUtYvmlq2apnHI4l3Mpa/GhtJnOElhSCFDH0bq6yCKmAFJ1VB5UWrRFa2Ab54Tj5qPr5PZ5bT//zHSuV4i+3GXPXsnYqhG2s9igk7bUYv2q3vB/0chWvgYsogpsk4Bu4rcpEbnVO7Mb1IlnCaARThKRKCmYoZEyUNKcTmqUNLpc7aUrUfe1W68Ya8a+hlosNW17C5+7qlmG/RLfF9GWmNeNXam1+OxeVjYnXPWynYROSwHeEBYR4hT15CQo8MK6K+mkEYBYKyeZ57AUmp43E9pLpIjk+ptVESs/fH1RShGDMC0Ax4XRVj+f7YuhBb5J3ff7j+G4bQaB19JJueiWFxc/tOpxdLV9SBvGuF1NFkNjdPe+fzF4MpgGQiNctSvEYVCVbZGkSmOVbSOWVZPyyJkAjiTdQAiwcsPNIWjtaEw9/RphNwLepwUc8vOW6lU8BgWyaa8EmDCfoTRXWKUA1AtzjMeqsS47WaBThnIN0lWPYT1LxJl3Nt0O5vrCGPDeQ4S7PDaOqblnvkw9YtKJ446BV3LVxF3cPRgVxB/B7hDGRAXISVkgoYtBLoEVmZZf7z0DnhmcumGsIogZN7jgovhD116vGmYGNymhwLkFRFkX9mBvj5WTBp47o0pm7X5I9+14/rC5gNUO5JCtAb1OszWr6CaqK+exM6oM6Ro4HGWXLWL2Z/MIdh9gUqZ2sF/zAAoO1Tss3s/LQTKWcWnDKmmiQ7Jyc8HFn0IOQ4Moq4C5GaDVUTyBZhNqEUE7wLq9bgqHB2CNiysXzBlG3rIdRf3TcuRpy281KQGUIEBCm+nxl9brMdx BgIxxQcy H2OpiDJgGSV0Dm5+lrqw802STdzKpFaz26LFyPZj0cK+LPo5VkLROP0JEvVrfeIKMJ9VCf7qTVpi0rOt25iRckUem+0SFCwfEzWwoys+Kl4QaUYGvt3kFLspcOgC5Kutup/xCKNmZ4dj7xQt048jI6xguvYGC5U63FX7XKmkNTKj1UOScrNIAAegGAgSgZ2xLZYb2wmUnHUBloSnxBNhUvX+b9/NiMFRfen/iXC1KH8mo4D1gxFll6d1WAmZtBvoNIQ9ZlB4YZ1O5MGc= 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: Hi All, This series fixes a bug in arm64's implementation of set_huge_pte_at(), which can result in an unprivileged user causing a kernel panic. The problem was triggered when running the new uffd poison mm selftest for HUGETLB memory. This test (and the uffd poison feature) was merged for v6.5-rc7. Ideally, I'd like to get this fix in for v6.6 and I've cc'ed stable (correctly this time) to get it backported to v6.5, where the issue first showed up. Description of Bug ------------------ arm64's huge pte implementation supports multiple huge page sizes, some of which are implemented in the page table with multiple contiguous entries. So set_huge_pte_at() needs to work out how big the logical pte is, so that it can also work out how many physical ptes (or pmds) need to be written. It previously did this by grabbing the folio out of the pte and querying its size. However, there are cases when the pte being set is actually a swap entry. But this also used to work fine, because for huge ptes, we only ever saw migration entries and hwpoison entries. And both of these types of swap entries have a PFN embedded, so the code would grab that and everything still worked out. But over time, more calls to set_huge_pte_at() have been added that set swap entry types that do not embed a PFN. And this causes the code to go bang. The triggering case is for the uffd poison test, commit 99aa77215ad0 ("selftests/mm: add uffd unit test for UFFDIO_POISON"), which causes a PTE_MARKER_POISONED swap entry to be set, coutesey of commit 8a13897fb0da ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs") - added in v6.5-rc7. Although review shows that there are other call sites that set PTE_MARKER_UFFD_WP (which also has no PFN), these don't trigger on arm64 because arm64 doesn't support UFFD WP. If CONFIG_DEBUG_VM is enabled, we do at least get a BUG(), but otherwise, it will dereference a bad pointer in page_folio(): static inline struct folio *hugetlb_swap_entry_to_folio(swp_entry_t entry) { VM_BUG_ON(!is_migration_entry(entry) && !is_hwpoison_entry(entry)); return page_folio(pfn_to_page(swp_offset_pfn(entry))); } Fix --- The simplest fix would have been to revert the dodgy cleanup commit 18f3962953e4 ("mm: hugetlb: kill set_huge_swap_pte_at()"), but since things have moved on, this would have required an audit of all the new set_huge_pte_at() call sites to see if they should be converted to set_huge_swap_pte_at(). As per the original intent of the change, it would also leave us open to future bugs when people invariably get it wrong and call the wrong helper. So instead, I've added a huge page size parameter to set_huge_pte_at(). This means that the arm64 code has the size in all cases. It's a bigger change, due to needing to touch the arches that implement the function, but it is entirely mechanical, so in my view, low risk. I've compile-tested all touched arches; arm64, parisc, powerpc, riscv, s390, sparc (and additionally x86_64). I've additionally booted and run mm selftests against arm64, where I observe the uffd poison test is fixed, and there are no other regressions. Patches ------- patch 1: Convert core mm and arches to pass extra param (no behavioral change) patch 8: Fix the arm64 bug Patches based on v6.6-rc2. Changes since v1 [1] -------------------- - Pass extra size param instead of converting mm to vma. - Passing vma was problematic for kernel mapping case without vma - Squash all interface changes to single patch - Simplify powerpc so that is doesn't require __set_huge_page_at() - Added Reviewed-bys [1] https://lore.kernel.org/linux-arm-kernel/20230921162007.1630149-1-ryan.roberts@arm.com/ Thanks, Ryan Ryan Roberts (2): mm: hugetlb: Add huge page size param to set_huge_pte_at() arm64: hugetlb: Fix set_huge_pte_at() to work with all swap entries arch/arm64/include/asm/hugetlb.h | 2 +- arch/arm64/mm/hugetlbpage.c | 23 +++------- arch/parisc/include/asm/hugetlb.h | 2 +- arch/parisc/mm/hugetlbpage.c | 2 +- .../include/asm/nohash/32/hugetlb-8xx.h | 3 +- arch/powerpc/mm/book3s64/hugetlbpage.c | 5 ++- arch/powerpc/mm/book3s64/radix_hugetlbpage.c | 3 +- arch/powerpc/mm/nohash/8xx.c | 3 +- arch/powerpc/mm/pgtable.c | 3 +- arch/riscv/include/asm/hugetlb.h | 3 +- arch/riscv/mm/hugetlbpage.c | 3 +- arch/s390/include/asm/hugetlb.h | 6 ++- arch/s390/mm/hugetlbpage.c | 8 +++- arch/sparc/include/asm/hugetlb.h | 6 ++- arch/sparc/mm/hugetlbpage.c | 8 +++- include/asm-generic/hugetlb.h | 2 +- include/linux/hugetlb.h | 6 ++- mm/damon/vaddr.c | 3 +- mm/hugetlb.c | 43 +++++++++++-------- mm/migrate.c | 7 ++- mm/rmap.c | 23 +++++++--- mm/vmalloc.c | 2 +- 22 files changed, 103 insertions(+), 63 deletions(-) -- 2.25.1