From patchwork Thu Sep 21 16:20:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13394013 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 F244BE71090 for ; Thu, 21 Sep 2023 16:21:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 86A206B0246; Thu, 21 Sep 2023 12:21:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 842516B020F; Thu, 21 Sep 2023 12:21:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 708E16B0246; Thu, 21 Sep 2023 12:21:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6022B6B020D for ; Thu, 21 Sep 2023 12:21:03 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2609AA059B for ; Thu, 21 Sep 2023 16:21:03 +0000 (UTC) X-FDA: 81261118806.04.8580344 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 567E920022 for ; Thu, 21 Sep 2023 16:21:01 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.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=1695313261; 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:in-reply-to:references:references; bh=NfUxN4bvi8i9D4AKXzSQb1pdt1stW1rn/rlDRpXWERQ=; b=GjGsfmnmyPynWh5hGkFnyLSocJzDsvjYATSmd0723pzanCykI5kHIE70riJEPT2VMGDtbl LMbFE+YGhdHORGNbJo0OfnFAxjCl96pJ6z4bEnucsJ/7BqV30xkpMJVtgBX/onEL9DPb1D vaAYc0r6pWB+yzWSJFmUUBAyfS6seF0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.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=1695313261; a=rsa-sha256; cv=none; b=KE1kV33kUNlNwEXFSVY8CvbUDa0DXPkLn+TN3i59laOQnREsFgD4VLTjxqeIX0/CmL4kkZ rdHtLqol0ULXP4/m1Qr0wBB4ywEJnsAMqpFi7CKDkbPEL1U3YMTTk13cTXgviBJWCR/y1W 0fETpMiIo6mnfhHh5qALMZmJMCAWrbo= 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 911D21764; Thu, 21 Sep 2023 09:21:37 -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 26AFE3F59C; Thu, 21 Sep 2023 09:20:56 -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 v1 8/8] arm64: hugetlb: Fix set_huge_pte_at() to work with all swap entries Date: Thu, 21 Sep 2023 17:20:07 +0100 Message-Id: <20230921162007.1630149-9-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230921162007.1630149-1-ryan.roberts@arm.com> References: <20230921162007.1630149-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 567E920022 X-Stat-Signature: 9cnb4yeb3banriqumtz1onw9ar8akke7 X-Rspam-User: X-HE-Tag: 1695313261-429507 X-HE-Meta: U2FsdGVkX1/RfoUtnO7RB+qPq/X+YQwcQMw6eD4TU2mKdC+nX04s+UZGwF9VbXFzenks/mPPuKk95WTYqOB/Cdp5XmeoE4lWQkMPq4Plmji0zDqmCGay+nwJiXm3VRdKtTgqFrCwja5D7FzuX1opUe1S3dEQNw0yFcZTcU3Yhl7IdWk61vrKb+IW8GYtwIehHInwftsSdkU22yU1Hbd8tfY7+CvuCDLn+SsXGFFJIt34UmlbDYexq1OCuyuh4gaR8FMtWakGvv7ZUVpZHxZSuXy0u3E3FOv+YHT1Au+4hHE1ek4F4jmI9Ea7wLKHSzw7sFLNcpJthEpUX9GPyJlLT68IXSj7eoLIfSEUJCbaGbt5mlI1x18DlIIPyNg/ulZ/vIW0M9DKY6taBBFCvxEZ7vS7rXUz1KziaNzVQkASKu+LXCb7QLipj+V4PqMkJssjCZEQvASbi/2uydODA2wqoNWUgfroq1CQbZxgTHm1t9WC4WBZJLP+RP2Sp1FtuPQexD9wmo4XhU8EL3PhZCmTx9LIlZt9hLP1Lda3dbZa9cOPSML8VEnr2xnf9Fd9U132ykT2y1AMusm53dllNP+1+tsIkViJ5whKb1wc8wi1vMuzfu0HiMCLyvB1Hrrzs4tSRdlcFFMNPuFRSx/B87CkvGHnBA466vZpCopr+vICoku6GF4uc7TLGgCmlTAaZ/ORX1G3v2QNKg65BXC4tJY4J2GOpf3oIYhAJ02NcPzYozQEYWK0G+eRepwiwT9yfbZSsodeYa//buxCva5q03huoUUh8CFOQqxvND4hVTomcJ3kAELFePQMObHWb+ujdZ01/8VE9HTdxLWOgNSwuyVTMXToLlaSNOjGCxdaSbcVxwDcbyS6Qm0ZSypApQNoskpdfVZDhbCmSupReylvhYaLbGntI0c7pF2Y7FQo8GUmb6buDHJ2fHO8X7GfrdCHTeyZN1hvItnkF6Smf/NixVE FWmUfYyZ /8ElendTJi2gWja1dc0Wxm835jHXDaySiN2D7dv4S50eojl44x2GwJR/Nq8O2bzYmByIq9cPIy80Zrjd3fzrquI2r/UoaXzBlLtT9D98ScrVaEH91YH9lDyrL6KI//8GkiHb/cg+H6BSWq8YDOHgNUoX5/VVXD7txjPnmi7wsw3g5w+at2LPwKe0i7CxTTxyqIJJG2XIub7if8PE= 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: When called with a swap entry that does not embed a PFN (e.g. PTE_MARKER_POISONED or PTE_MARKER_UFFD_WP), the previous implementation of set_huge_pte_at() would either cause a BUG() to fire (if CONFIG_DEBUG_VM is enabled) or cause a dereference of an invalid address and subsequent panic. arm64's huge pte implementation supports multiple huge page sizes, some of which are implemented in the page table with contiguous mappings. 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 does 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 sets a PTE_MARKER_POISONED swap entry. But review shows there are other places too (PTE_MARKER_UFFD_WP). So the root cause is due to commit 18f3962953e4 ("mm: hugetlb: kill set_huge_swap_pte_at()"), which aimed to simplify the interface to the core code by removing set_huge_swap_pte_at() (which took a page size parameter) and replacing it with calls to set_huge_swap_pte_at() where the size was inferred from the folio, as descibed above. While that commit didn't break anything at the time, it did break the interface because it couldn't handle swap entries without PFNs. And since then new callers have come along which rely on this working. Now that we have modified the set_huge_pte_at() interface to pass the vma, we can extract the huge page size from it and fix this issue. I'm tagging the commit that added the uffd poison feature, since that is what exposed the problem, as well as the original change that broke the interface. Hopefully this is valuable for people doing bisect. Signed-off-by: Ryan Roberts Fixes: 18f3962953e4 ("mm: hugetlb: kill set_huge_swap_pte_at()") Fixes: 8a13897fb0da ("mm: userfaultfd: support UFFDIO_POISON for hugetlbfs") --- arch/arm64/mm/hugetlbpage.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 844832511c1e..a08601a14689 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -241,13 +241,6 @@ static void clear_flush(struct mm_struct *mm, flush_tlb_range(&vma, saddr, addr); } -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))); -} - void set_huge_pte_at(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t pte) { @@ -258,13 +251,10 @@ void set_huge_pte_at(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, dpfn; pgprot_t hugeprot; - if (!pte_present(pte)) { - struct folio *folio; - - folio = hugetlb_swap_entry_to_folio(pte_to_swp_entry(pte)); - ncontig = num_contig_ptes(folio_size(folio), &pgsize); + ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize); - for (i = 0; i < ncontig; i++, ptep++) + if (!pte_present(pte)) { + for (i = 0; i < ncontig; i++, ptep++, addr += pgsize) set_pte_at(mm, addr, ptep, pte); return; } @@ -274,7 +264,6 @@ void set_huge_pte_at(struct vm_area_struct *vma, unsigned long addr, return; } - ncontig = find_num_contig(mm, addr, ptep, &pgsize); pfn = pte_pfn(pte); dpfn = pgsize >> PAGE_SHIFT; hugeprot = pte_pgprot(pte);