From patchwork Thu Feb 15 12:17:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13558301 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 6E47EC48BEB for ; Thu, 15 Feb 2024 12:18:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3AE58D000E; Thu, 15 Feb 2024 07:18:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC3288D0001; Thu, 15 Feb 2024 07:18:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AEF728D000E; Thu, 15 Feb 2024 07:18:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9A11D8D0001 for ; Thu, 15 Feb 2024 07:18:11 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 68686160A3F for ; Thu, 15 Feb 2024 12:18:11 +0000 (UTC) X-FDA: 81793940382.07.9AB7ECA Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id D44971C000D for ; Thu, 15 Feb 2024 12:18:08 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.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=1707999489; 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=5v058h4Aw+ECiRT25vTaV+XZu2TbsaL/aTwq/D7+cl0=; b=WT1UUzW/fy1/S3VDdy6HDeYx5Xy5QrxTsgnOyMtP5MiAyJGyBBLYUNkWnUFpe0wml8dppb naS7Ype3k/eLmuUXQubeAWRltKq3hi0n7T7qk9yYDqNm7SLVEqVvByixm9P6G62R4NNV++ yB0KLSI5cQXJiqip9E4apMJEu/SRBB8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707999489; a=rsa-sha256; cv=none; b=tyvha4VVVhJcexCd/4cN2am4CRW/3Zmv8/0LdZ/QVdi/qkJzc6RxlQu9pNIZs2dHMVd5fN uny6H+f7wHsFs0GRdxRAz3sfmIociaro1h+ZzPOe6uLRj4rqxLbsvDUD2zNBaRSRYwCVHW f0Kx4SzFrYNPQin7VH7jkwE+3JK39k8= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.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 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 BA08EDA7; Thu, 15 Feb 2024 04:18:48 -0800 (PST) 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 1E9A03F766; Thu, 15 Feb 2024 04:18:06 -0800 (PST) From: Ryan Roberts To: David Hildenbrand , Mark Rutland , Catalin Marinas , Will Deacon , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Andrew Morton , Muchun Song Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v1 1/4] mm: Introduce ptep_get_lockless_norecency() Date: Thu, 15 Feb 2024 12:17:53 +0000 Message-Id: <20240215121756.2734131-2-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240215121756.2734131-1-ryan.roberts@arm.com> References: <20240215121756.2734131-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D44971C000D X-Rspam-User: X-Stat-Signature: arhtc1a5wjqmrpdmggwm9wzso4q31gha X-Rspamd-Server: rspam03 X-HE-Tag: 1707999488-942640 X-HE-Meta: U2FsdGVkX18a4iX+cx5SQKHTohU+3ohkvGE3Mb6nVBZa72ACwkDArmTmiRJGNuh4Zq3xpltAJL913DNg1ApFirSO7pXA2ggvEEauN/CJf+qf2eGysnrq99XJVE15AjvMWWpE488RwHT47zisNzsFX/TMZm+Q3lHyP/GSrBXgGJnXKEOtX63F5rTZgxHiRJoByjqgHqwqIePr7Patdh5dqBlRhueEKPrV0RznW2vX0fQUbzw4sN2so0aysioe5W2/4RHS9F1ZDC/WN8vLEfj9rj23BlFTYvIImBwJQ5onsGSfh/NaxIZg8CBBRsGi16mFBdhWK6EFElIxD7U0samRrFTdcB6P6vGduG+bZwVtVi3cGxVzaA5G4gGpIIIrswvVBkWiZ5rSK08whhRByDhszhvqkP1Qx5zDyt+IMSvKB6RLZCdRuKEA9D3iV+/J5vhKK3QNpw6wK/mRYXhrAPYOaDgFzEpN7cp2CsRV6I8oPvK2D8FK7Ise1ooPCZlTYWaG4T4ykop79zomCl3UlDSaWtcrQCASdGhr1p0JUR3Td0AWKc05A0MsfSUctTr3M6DrCZZRO1zlvkzvb8AUKZwkKnAfqD0Ij1PxqDcOCVc+kgd38k1qcb9u7DXRlDQMPPnYGO2JyikrtyDXhQ3X/KMbzHbxiW6mjmsBbkZZFv3rFNKSg+DsFiYLTq0dsBT9547z/GYYyQy6FoYIKaCG1gecWKC3VfWKwCDpB0E/vGctumRNSGfWtMtwy3Y7NJnHo8R0567C/Mv80vE4e+cOAVuK488OKfwIOV36KUkqxPBaMN9+/3mTiHUbDN7AtYYzygfOeDcvOv4qZPxzdMSdOZUHbVK8YoVipRSUa4gSPE8DfMqZQTqBVnCzPcGCitylsM+jyhSYE4s8gWg= 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: With the introduction of contpte mapping support for arm64, that architecture's implementation of ptep_get_lockless() has become very complex due to the need to gather access and dirty bits from across all of the ptes in the contpte block. This requires careful implementation to ensure the returned value is consistent (because its not possible to read all ptes atomically), but even in the common case when there is no racing modification, we have to read all ptes, which gives an ~O(n^2) cost if the core-mm is iterating over a range, and performing a ptep_get_lockless() on each pte. Solve this by introducing ptep_get_lockless_norecency(), which does not make any guarantees about access and dirty bits. Therefore it can simply read the single target pte. At the same time, convert all call sites that previously used ptep_get_lockless() but don't care about access and dirty state. We may want to do something similar for ptep_get() (i.e. ptep_get_norecency()) in future; it doesn't suffer from the consistency problem because the PTL serializes it with any modifications, but does suffer the same O(n^2) cost. Signed-off-by: Ryan Roberts --- include/linux/pgtable.h | 37 ++++++++++++++++++++++++++++++++++--- kernel/events/core.c | 2 +- mm/hugetlb.c | 2 +- mm/khugepaged.c | 2 +- mm/memory.c | 2 +- mm/swap_state.c | 2 +- mm/swapfile.c | 2 +- 7 files changed, 40 insertions(+), 9 deletions(-) -- 2.25.1 diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index a36cf4e124b0..9dd40fdbd825 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -528,16 +528,47 @@ static inline pmd_t pmdp_get_lockless(pmd_t *pmdp) #endif /* CONFIG_PGTABLE_LEVELS > 2 */ #endif /* CONFIG_GUP_GET_PXX_LOW_HIGH */ -/* - * We require that the PTE can be read atomically. - */ #ifndef ptep_get_lockless +/** + * ptep_get_lockless - Get a pte without holding the page table lock. Young and + * dirty bits are guaranteed to accurately reflect the state + * of the pte at the time of the call. + * @ptep: Page table pointer for pte to get. + * + * If young and dirty information is not required, use + * ptep_get_lockless_norecency() which can be faster on some architectures. + * + * May be overridden by the architecture; otherwise, implemented using + * ptep_get(), on the assumption that it is atomic. + * + * Context: Any. + */ static inline pte_t ptep_get_lockless(pte_t *ptep) { return ptep_get(ptep); } #endif +#ifndef ptep_get_lockless_norecency +/** + * ptep_get_lockless_norecency - Get a pte without holding the page table lock. + * Young and dirty bits may not be accurate. + * @ptep: Page table pointer for pte to get. + * + * Prefer this over ptep_get_lockless() when young and dirty information is not + * required since it can be faster on some architectures. + * + * May be overridden by the architecture; otherwise, implemented using the more + * precise ptep_get_lockless(). + * + * Context: Any. + */ +static inline pte_t ptep_get_lockless_norecency(pte_t *ptep) +{ + return ptep_get_lockless(ptep); +} +#endif + #ifndef pmdp_get_lockless static inline pmd_t pmdp_get_lockless(pmd_t *pmdp) { diff --git a/kernel/events/core.c b/kernel/events/core.c index f0f0f71213a1..27991312d635 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -7583,7 +7583,7 @@ static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr) if (!ptep) goto again; - pte = ptep_get_lockless(ptep); + pte = ptep_get_lockless_norecency(ptep); if (pte_present(pte)) size = pte_leaf_size(pte); pte_unmap(ptep); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 68283e54c899..41dc44eb8454 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7517,7 +7517,7 @@ pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, } if (pte) { - pte_t pteval = ptep_get_lockless(pte); + pte_t pteval = ptep_get_lockless_norecency(pte); BUG_ON(pte_present(pteval) && !pte_huge(pteval)); } diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 2771fc043b3b..1a6c9ed8237a 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1019,7 +1019,7 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm, } } - vmf.orig_pte = ptep_get_lockless(pte); + vmf.orig_pte = ptep_get_lockless_norecency(pte); if (!is_swap_pte(vmf.orig_pte)) continue; diff --git a/mm/memory.c b/mm/memory.c index 4dd8e35b593a..8e65fa1884f1 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4353,7 +4353,7 @@ static bool pte_range_none(pte_t *pte, int nr_pages) int i; for (i = 0; i < nr_pages; i++) { - if (!pte_none(ptep_get_lockless(pte + i))) + if (!pte_none(ptep_get_lockless_norecency(pte + i))) return false; } diff --git a/mm/swap_state.c b/mm/swap_state.c index 2f540748f7c0..061c6c16c7ff 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -837,7 +837,7 @@ static struct folio *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, if (!pte) break; } - pentry = ptep_get_lockless(pte); + pentry = ptep_get_lockless_norecency(pte); if (!is_swap_pte(pentry)) continue; entry = pte_to_swp_entry(pentry); diff --git a/mm/swapfile.c b/mm/swapfile.c index d1bd8d1e17bd..c414dd238814 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1857,7 +1857,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, break; } - ptent = ptep_get_lockless(pte); + ptent = ptep_get_lockless_norecency(pte); if (!is_swap_pte(ptent)) continue;