From mboxrd@z Thu Jan 1 00:00:00 1970 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 EDD6DC369D7 for ; Tue, 22 Apr 2025 08:18:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D571A6B002E; Tue, 22 Apr 2025 04:18:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CFF196B0030; Tue, 22 Apr 2025 04:18:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9ED96B0031; Tue, 22 Apr 2025 04:18:44 -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 9EC586B002E for ; Tue, 22 Apr 2025 04:18:44 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D460FC65A8 for ; Tue, 22 Apr 2025 08:18:45 +0000 (UTC) X-FDA: 83360978610.21.524A20D Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf12.hostedemail.com (Postfix) with ESMTP id 35C6840004 for ; Tue, 22 Apr 2025 08:18:44 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745309924; 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=/vay08M5nzA9ByG9MVxzvVy54geHVioG6NueW0ZaQ7A=; b=dktsTI700vqY11zsk11i3VdjgGFY+OkpjLOtNN6jvMAPfhruM3F9SEV1vVHqi+KBOAAjAD at2CN2BUfCXmIrbysBOjHqrrSIwkiVwL9eWwVSNwhUQNTe1F6YPx7dzx/qkAGM1S4Yr4GP 3NPGkeR/1I+ysqZISRdNE3lH+52STdw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745309924; a=rsa-sha256; cv=none; b=zETiegZxM/S8sLcEoXIwzZ7v9spwTHGAH9K+h9tyHD8ZEkn6J6zHQ7cllhR7Mat5/U6xBu qhUgw8n3cioXxd8683knEYNRShooIUXy7k0OQvjpRI+ZolbN8/RKyIbqdf3+6DYgPMctEE eXpGSq6sDD2L0lDr21wnkrS1Nhz2Ww4= 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 E5D9F176A; Tue, 22 Apr 2025 01:18:38 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3F1B13F66E; Tue, 22 Apr 2025 01:18:41 -0700 (PDT) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Pasha Tatashin , Andrew Morton , Uladzislau Rezki , Christoph Hellwig , David Hildenbrand , "Matthew Wilcox (Oracle)" , Mark Rutland , Anshuman Khandual , Alexandre Ghiti , Kevin Brodsky Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 01/11] arm64: hugetlb: Cleanup huge_pte size discovery mechanisms Date: Tue, 22 Apr 2025 09:18:09 +0100 Message-ID: <20250422081822.1836315-2-ryan.roberts@arm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250422081822.1836315-1-ryan.roberts@arm.com> References: <20250422081822.1836315-1-ryan.roberts@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: 8mgqjemggoeo4u35g14g4fzg394cmnjt X-Rspamd-Queue-Id: 35C6840004 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1745309924-764286 X-HE-Meta: U2FsdGVkX19nr0m9P2vZiaGArciKjWtI3NEan8FCM/2Qivfa9BuuiKZtcmvZKcuYsx/EQqGfLYZuj11ligP+1f1SfllvSLf9eNVd+Rnv+mBCBYTyHf4YuU+GHv+eqEvgsd4T4FrJAoS/FotUmfgebAoGP60/q+ER9/cQwuZ3Se6CF4QLcYCGuzrXm0CbbW18FGSn/XMpjatbjXLDlR7FuKXpgkLggnuM/XCn/aNkX9HwybOBD8xCzr+aD8R3TjhshHnz7J2ctSsmz9hNACO/liHxEZxEQKOI9y42aIfXzrDjECAlTJiasIO7yjRxr6WsCbVSNgm4d60jXt6eCg3It+qBuKmGjaNzL+QfkfnFHP6DjWg6Xajrgj7FihymZ3lbC3PT542p9hJPiSgz3QZ30EZ838yFWbSAL7jMG6rMorzBiMVoAIXzuebVswwfvvWBXyK4iSnIbxTjOkE4+il+8GOlTFlpzVC3TrqcjnJyGiEzXmLjaM0NMvgjoY1wxySzasF5E+7Z4MkgSOLXvIdJ3EA9ornUPByBbEUrTypu/MeHWi0XfZfYDb0bs3h6Rm3O2pPJ0/JX6R59Vs7qO58GAQ8JQOi36tw+lv2/puHxiO9iYO01h3Lu4QxVVzGOoyqieMwo7G5wo1xQFY12TJxZhcOZAx325oTMQQwgYBN4oagaLCvtyp/Dsa2dE9sM5ZDIASAfBNcyr5aepukilhL5wwXWEIv+Qasvd54dTijjmixZFHhnBK4xeMd2DBkn0/+PX/UwvJTpT9aPksD4f7x/nFMhPZVJo0iPnMwdgeq8qV83lys1CGrXQ3BBoRLwoZ6X9XVRqHrSEZsjGdJ47XAJVInGqBvhqwJsnh/aYMD+iDxIdkkvJ4TkMM/RR6EF+JAvNKte8jwxTNyoZqBeJt2JzTvB8yUDabNXybW9ySdyzfx8qU10NVhbAt5tt/kiMhFC5Sl5Z/ns23f0l23PHx9 dX+ONXR4 Fhpz7A0Dnp8hst3t6/ANtrnOZRPhmcriZmbVOcWbADysTC2iSK+IjCTkEPHqxUj/BTcHwfqep4GoWx7vNuriQTYF5HEAM/tQtfiL08suw5FlW/Dm8Yg6Wd+mR88YJ0MOb7u2nk+tdMHLP2/85SLWIllqdsgb/f6FGjhcMZYLpzFsK0hPoAX/aZSxwXQjMV8pnbLPqfMhKFgy+ZEc= 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: List-Subscribe: List-Unsubscribe: Not all huge_pte helper APIs explicitly provide the size of the huge_pte. So the helpers have to depend on various methods to determine the size of the huge_pte. Some of these methods are dubious. Let's clean up the code to use preferred methods and retire the dubious ones. The options in order of preference: - If size is provided as parameter, use it together with num_contig_ptes(). This is explicit and works for both present and non-present ptes. - If vma is provided as a parameter, retrieve size via huge_page_size(hstate_vma(vma)) and use it together with num_contig_ptes(). This is explicit and works for both present and non-present ptes. - If the pte is present and contiguous, use find_num_contig() to walk the pgtable to find the level and infer the number of ptes from level. Only works for *present* ptes. - If the pte is present and not contiguous and you can infer from this that only 1 pte needs to be operated on. This is ok if you don't care about the absolute size, and just want to know the number of ptes. - NEVER rely on resolving the PFN of a present pte to a folio and getting the folio's size. This is fragile at best, because there is nothing to stop the core-mm from allocating a folio twice as big as the huge_pte then mapping it across 2 consecutive huge_ptes. Or just partially mapping it. Where we require that the pte is present, add warnings if not-present. Reviewed-by: Catalin Marinas Reviewed-by: Anshuman Khandual Signed-off-by: Ryan Roberts --- arch/arm64/mm/hugetlbpage.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index cfe8cb8ba1cc..701394aa7734 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -129,7 +129,7 @@ pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep) if (!pte_present(orig_pte) || !pte_cont(orig_pte)) return orig_pte; - ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize); + ncontig = find_num_contig(mm, addr, ptep, &pgsize); for (i = 0; i < ncontig; i++, ptep++) { pte_t pte = __ptep_get(ptep); @@ -438,16 +438,19 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, pgprot_t hugeprot; pte_t orig_pte; + VM_WARN_ON(!pte_present(pte)); + if (!pte_cont(pte)) return __ptep_set_access_flags(vma, addr, ptep, pte, dirty); - ncontig = find_num_contig(mm, addr, ptep, &pgsize); + ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize); dpfn = pgsize >> PAGE_SHIFT; if (!__cont_access_flags_changed(ptep, pte, ncontig)) return 0; orig_pte = get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); + VM_WARN_ON(!pte_present(orig_pte)); /* Make sure we don't lose the dirty or young state */ if (pte_dirty(orig_pte)) @@ -472,7 +475,10 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, size_t pgsize; pte_t pte; - if (!pte_cont(__ptep_get(ptep))) { + pte = __ptep_get(ptep); + VM_WARN_ON(!pte_present(pte)); + + if (!pte_cont(pte)) { __ptep_set_wrprotect(mm, addr, ptep); return; } @@ -496,11 +502,15 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, struct mm_struct *mm = vma->vm_mm; size_t pgsize; int ncontig; + pte_t pte; + + pte = __ptep_get(ptep); + VM_WARN_ON(!pte_present(pte)); - if (!pte_cont(__ptep_get(ptep))) + if (!pte_cont(pte)) return ptep_clear_flush(vma, addr, ptep); - ncontig = find_num_contig(mm, addr, ptep, &pgsize); + ncontig = num_contig_ptes(huge_page_size(hstate_vma(vma)), &pgsize); return get_clear_contig_flush(mm, addr, ptep, pgsize, ncontig); } -- 2.43.0