From patchwork Thu Feb 15 10:32:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557865 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 958E1C4829E for ; Thu, 15 Feb 2024 10:33:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32D0F8D0021; Thu, 15 Feb 2024 05:33:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 28DD28D000E; Thu, 15 Feb 2024 05:33:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0E1C88D0021; Thu, 15 Feb 2024 05:33:14 -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 DFBB48D000E for ; Thu, 15 Feb 2024 05:33:13 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9741C1A0852 for ; Thu, 15 Feb 2024 10:33:13 +0000 (UTC) X-FDA: 81793675866.25.354D811 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf28.hostedemail.com (Postfix) with ESMTP id 0332BC001F for ; Thu, 15 Feb 2024 10:33:11 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf28.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=1707993192; 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=BF6nKnbz4CAqHZQZG0uUn3HcEcmnuX1IgW4gkLn6dJk=; b=bEu5t/AKhPlPGpB2NfGQokCoEGEWP2OZx3JecOp4ji5IzinPsDlyIsEowpBg+bCUEFOiKc 2GlavTgraEwjtNZ8ralmBUIOEf1El+mxJYZFyR5foN5tSeNrUpsCmjl9OwQOl3XuvGICqW JiqvokMOHgthXOuDAAt+XQIw8optq8Q= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf28.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=1707993192; a=rsa-sha256; cv=none; b=Cgv6cwNiVZdYcMAIsfG5YtWGOSnsAW3eJcFtHvSEvNWB85Wq5B7LJgILad0UI9hLAMmY1f tK0niRZcM0bKdL942bGdGkJecWE37PltQt5thefFrGhFFEl0hihf7TyWkeVmQ6IRDINyev nAuCyWjbrYo9aXGTeq63G5v6BIr2QPk= 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 20A7A15A1; Thu, 15 Feb 2024 02:33:52 -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 17B8B3F7B4; Thu, 15 Feb 2024 02:33:07 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , James Morse , Andrey Ryabinin , Andrew Morton , Matthew Wilcox , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, x86@kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v6 15/18] mm: Add pte_batch_hint() to reduce scanning in folio_pte_batch() Date: Thu, 15 Feb 2024 10:32:02 +0000 Message-Id: <20240215103205.2607016-16-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240215103205.2607016-1-ryan.roberts@arm.com> References: <20240215103205.2607016-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0332BC001F X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: wxuppsujtw4u4rzp7jqhc11nhmrdxu1o X-HE-Tag: 1707993191-301214 X-HE-Meta: U2FsdGVkX1/td3yAIozWcgKlPQFG0D5/63iKaI20tu3Q8pyBsCD9A5rXJlYyuRwWZouJuYCqKm58yMHWW616N8Wq4+mQKeF9J+Cujb/fUZ7nY/dhUcBjhuVTYdB8K+OWWQS9383hOff45B8yD64XP/2QhK60ATm2hXIIaBMVI5OrOCaxXPk7JgeXrXyE1J7lveuSoOe9MNyD67BKs8ziP527JWfWzU1F/8tsVDqVJww3bhTAORQl4kr8N7w69A0E7UvPeO8xVFPZ3+q1zgRhcKD1GKEnj4bVl20+alYoFqJ8EaAPqL4lFb0Jw4D7CD27pHOxNh+BW74y4ueomCEgcxMp4CA07j2wxyxi10Rm+tlGnaHUhmwfMLRAJdgZHLRXO7AK6j86LQ2xmnsGlJM43QloNQa0nABwgbexgyIFbQ8NEZJ6Hpy93twrUDeNDg/22z/b2DQvwT+A5FS6p6siLKbxDQTPQJ4a03kTgy2od0TV5jsSH5xBJx0OqW6TeEBYjxqh42rfyRZ3CdEols2ugkuUZ0EhywZLtHRO7lPdb4lVZSPVaRuZ7op603PbdqWly4Cc8AnQWnLVkfWZmpEyoCA76YNYTuMToIJY5DbIxhwlqz22JsSbUvTrbpEPwxC+eU74orord8/kVyjTMRFdXBBY3sICS6z5rwCDr9aCjpWNX3zIm3ZTN8sXAZrkN5B0Q5yJlETDGUptff3Yk8N8TVP+icKt84CbKzmmp+S38b5nrbOGucWMuSmiG0J44XHCrupeB+iLUqQ0FYSe2xDB26Kr4w05lSN4BVkK5FS/QJcpsCE0At/SoEcGCNlrmf/yKvw/2PUv6Ba8XxrrfCZQuke2h2jJXA/Bsp1Jit2A1QOAFPF1So2ax849GBdGtZBu6t//HDu3EXMj1LxTxABFxt5JaiOUh+xiIQGZS68A/yohclLNFY9yBhBzABpwet7N6xQYuGroBUQsmeua8k2 FUg== 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: Some architectures (e.g. arm64) can tell from looking at a pte, if some follow-on ptes also map contiguous physical memory with the same pgprot. (for arm64, these are contpte mappings). Take advantage of this knowledge to optimize folio_pte_batch() so that it can skip these ptes when scanning to create a batch. By default, if an arch does not opt-in, folio_pte_batch() returns a compile-time 1, so the changes are optimized out and the behaviour is as before. arm64 will opt-in to providing this hint in the next patch, which will greatly reduce the cost of ptep_get() when scanning a range of contptes. Acked-by: David Hildenbrand Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- include/linux/pgtable.h | 21 +++++++++++++++++++++ mm/memory.c | 19 ++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index bc005d84f764..a36cf4e124b0 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -212,6 +212,27 @@ static inline int pmd_dirty(pmd_t pmd) #define arch_flush_lazy_mmu_mode() do {} while (0) #endif +#ifndef pte_batch_hint +/** + * pte_batch_hint - Number of pages that can be added to batch without scanning. + * @ptep: Page table pointer for the entry. + * @pte: Page table entry. + * + * Some architectures know that a set of contiguous ptes all map the same + * contiguous memory with the same permissions. In this case, it can provide a + * hint to aid pte batching without the core code needing to scan every pte. + * + * An architecture implementation may ignore the PTE accessed state. Further, + * the dirty state must apply atomically to all the PTEs described by the hint. + * + * May be overridden by the architecture, else pte_batch_hint is always 1. + */ +static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte) +{ + return 1; +} +#endif + #ifndef pte_advance_pfn static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) { diff --git a/mm/memory.c b/mm/memory.c index 3b8e56eb08a3..4dd8e35b593a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -988,16 +988,20 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, { unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio); const pte_t *end_ptep = start_ptep + max_nr; - pte_t expected_pte = __pte_batch_clear_ignored(pte_next_pfn(pte), flags); - pte_t *ptep = start_ptep + 1; + pte_t expected_pte, *ptep; bool writable; + int nr; if (any_writable) *any_writable = false; VM_WARN_ON_FOLIO(!pte_present(pte), folio); - while (ptep != end_ptep) { + nr = pte_batch_hint(start_ptep, pte); + expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); + ptep = start_ptep + nr; + + while (ptep < end_ptep) { pte = ptep_get(ptep); if (any_writable) writable = !!pte_write(pte); @@ -1011,17 +1015,18 @@ static inline int folio_pte_batch(struct folio *folio, unsigned long addr, * corner cases the next PFN might fall into a different * folio. */ - if (pte_pfn(pte) == folio_end_pfn) + if (pte_pfn(pte) >= folio_end_pfn) break; if (any_writable) *any_writable |= writable; - expected_pte = pte_next_pfn(expected_pte); - ptep++; + nr = pte_batch_hint(ptep, pte); + expected_pte = pte_advance_pfn(expected_pte, nr); + ptep += nr; } - return ptep - start_ptep; + return min(ptep - start_ptep, max_nr); } /*