From patchwork Fri Feb 2 08:07: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: 13542357 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 B448BC48292 for ; Fri, 2 Feb 2024 08:09:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43C386B00AD; Fri, 2 Feb 2024 03:09:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3EC3C6B00AF; Fri, 2 Feb 2024 03:09:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B43C6B00B0; Fri, 2 Feb 2024 03:09:43 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1CF8E6B00AD for ; Fri, 2 Feb 2024 03:09:43 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E2706120659 for ; Fri, 2 Feb 2024 08:09:42 +0000 (UTC) X-FDA: 81746139804.05.C48E06D Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 152F71C0021 for ; Fri, 2 Feb 2024 08:09:40 +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=1706861381; 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=RG2xpbHo+BZWR1EUw1E88djCi8W1k8Ens4WKUhSzkA8=; b=E0nOH/4eS29Rjng9ljhsG8BweM1PpjXLfxW0DDS10Jgou6bhlqkhuNFpEra2DrbePfkMQp bxXqQFNgdqWT4z/dLN6mnlzv8JP86jWKWs2ywmSrtLAUZFLNx/MazQmQ25TrLu7hTTS3Z4 Z2C9GiaWhcRFTZdAqs+m52v20CriDYQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706861381; a=rsa-sha256; cv=none; b=mgV7ApM6O2pIFPSEPxwKFtqWx0iyeYe6ucIHKkf5dYohdEF0nO39TyfmdPM6+GhrhP+iW4 1R/jq2cwg/Y71GeCEYzR6AK77LQQtqPl7JMuMYUA9vDOft5UfEaWY8lJGgYba1+fBOFCMz tZT+7iqPNmNRJcF8nNYUirLD2VZCAbQ= 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 B640A1BD0; Fri, 2 Feb 2024 00:10:22 -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 CA6BF3F5A1; Fri, 2 Feb 2024 00:09:36 -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 , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , 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 v5 22/25] mm: Add pte_batch_hint() to reduce scanning in folio_pte_batch() Date: Fri, 2 Feb 2024 08:07:53 +0000 Message-Id: <20240202080756.1453939-23-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240202080756.1453939-1-ryan.roberts@arm.com> References: <20240202080756.1453939-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Stat-Signature: 6d1cw7bh1m5dgeqohqrfkzyzf7wh9wr8 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 152F71C0021 X-Rspam-User: X-HE-Tag: 1706861380-112572 X-HE-Meta: U2FsdGVkX1+CcJT+VKsxVPwJY+CvL5Pc9v5OsUA2SXD44BITqvqE2cmb4AYHIxZLiyhoe6P0HC5xaXNL+Kv8NSE1n3TwcdPIlvnd0lpCtpdEKSgXBW2ywoeIRffeYiUcm0pmoosvNsy07DHfeal38HVt7hkmqvMdCgQMQqVk2vixGIFgPGRQ+YULUvDZ3bfTDhosnXrbaxPgYOHSrLq1Y4MHRKildxOaNaJu258zZcTI2/LpwL2PqT8D76YUwdHhCPFMsiTAaItDiM5LXqyag41ocWyZmdI9zwmPXVPGaCdZlqHFBOuwct9gixEaxsu3ZJhQPj7EONxEjpBSMZDsTyhkbV4rOmeBSBeKUih3zKUmQDs3lfkrHtZ7E1rumOmE+Ml4v9CN7EzaB+1hHAC6CMPsw6nm+qKR/vkWy1Hj3fQFTsrcLlOgK9VA7YjAcoJQ1vUH3bFOuaPMYPoTlt2prHiXKTH//ZScrErHVyRN7o5YlCpllvWShGJvM8hsetEkyZUnpIFiVmc8GNEmmj0vboqbr/D6wrpzbTd3oX6peaKaaZ5Asy9mo6OAH4Sfae0TnQKbAsBM+2A42qQHvUEPyLwDqrvsexUQVBrmuotY3feFL7otYmj1ZvqyoJdVVafStH2vyKgAxwGrQA7mokkULNKkXHpWWzOX0KqH0mJv5seVmrBGz5/L7k3AydlVUrl+d5o1p7z5hpCz+eDtau7S3IMqLyZFsaEOp6+EEOWnA+2NKO0weyKielFs9VgeXOh8nXJc9o2/q+abacMwH+0X7sOzA080zJBYoCpeNUHDIQ8R9GCQDtOBlrfMNjORsZPX9CWxPwq5RfvYY5EQ0SUAgRcHsuiTxf9m+Eu+LG8BAiH9Mwn1z7SLevLQI+RLR8+sXoyThYHaTMQndLgdZGWMFeohiD/Qsl4ekSKGQEG3dk9oOnXZ4XFp+36zwxGFJk+8WeMYF/8oKAeEV+QXsQo iffg4PGO BsLs9FcxxSzpclF8O65D20g0fYXYWuT9E7eiymcQ1KgT+iLh7dT6YmKxJugYw/Q3+XOB6U0/pYoxNB7Ney1/IhqlNves5HBww4SAzAwC7jn96u5OFBw0lPVVq5VJD7Q3aqR9OKm20a03LPoij5juekOBIyMrwXYUdVhobf94yRkjvwG8fZt7ou5bUa/suWpaa5AYf8uHlH+aSN9HdWRiFl+teq0gN2ThPn1iVngyVTMV+4sm6SfyvTcnd+gb/KyBq9JjU4u0/0BCOxcJCZfI2sSq66IbKEwdL8KrQAoGaAwR9WqU= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: David Hildenbrand --- include/linux/pgtable.h | 18 ++++++++++++++++++ mm/memory.c | 20 +++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 50f32cccbd92..cba31f177d27 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -212,6 +212,24 @@ 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. + * + * 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 65fbe4f886c1..902665b27702 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -988,16 +988,21 @@ 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_advance_pfn(pte, 1), flags); - pte_t *ptep = start_ptep + 1; + pte_t expected_pte = __pte_batch_clear_ignored(pte, flags); + pte_t *ptep = start_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(ptep, pte); + expected_pte = pte_advance_pfn(expected_pte, nr); + ptep += nr; + + while (ptep < end_ptep) { pte = ptep_get(ptep); if (any_writable) writable = !!pte_write(pte); @@ -1011,17 +1016,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_advance_pfn(expected_pte, 1); - 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); } /*