From patchwork Thu Feb 15 10:31:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557851 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 1B865C48BEB for ; Thu, 15 Feb 2024 10:32:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 881F98D0013; Thu, 15 Feb 2024 05:32:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8316A8D0001; Thu, 15 Feb 2024 05:32:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FB7C8D0013; Thu, 15 Feb 2024 05:32:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 590658D0001 for ; Thu, 15 Feb 2024 05:32:26 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 10300120F31 for ; Thu, 15 Feb 2024 10:32:26 +0000 (UTC) X-FDA: 81793673892.05.F82D03A Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 7434040015 for ; Thu, 15 Feb 2024 10:32:24 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.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=1707993144; 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=f6gb/nfiijie/R90uJlYvW3OmZJqXxR5XmD97JtGnxc=; b=IHnJCddOKXHD0A2gai+N3Dqfh2fJATIngBS0pISa6eAlglRQNaHysZtMTH1srz/Rlon7Ma v4pHrcgn+YKDR0KUNyP9x/hxoZjgf7nOGMNQxEMYo+/1uRr26AaH0aklSaqu9kVrGt3zIF mywvw1eLyWBF+xUyrCYjnUbs53pCimk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707993144; a=rsa-sha256; cv=none; b=4VtTzCVmV3HmdWXVeCRr5Aofd8Ir8yLelqGndIF3MYUEYD8xCCFmmH9SOOdaAD8r/1kMkh w8yhRnCm5mBBZ/ub/zZVSsAQK4woedEf4gvsiCuvfDgDZRWVxOlpicN1vhcmz/lrCY8PPJ q5os14WcNZufd9TEcNpMuUP7EPhWcUk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; spf=pass (imf04.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 40DFDDA7; Thu, 15 Feb 2024 02:33:04 -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 3722C3F7B4; Thu, 15 Feb 2024 02:32:20 -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 01/18] mm: Clarify the spec for set_ptes() Date: Thu, 15 Feb 2024 10:31:48 +0000 Message-Id: <20240215103205.2607016-2-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: 7434040015 X-Rspam-User: X-Stat-Signature: jm8uh1inbsuierit5oxzg4e4f71oxu3x X-Rspamd-Server: rspam03 X-HE-Tag: 1707993144-543589 X-HE-Meta: U2FsdGVkX1+4S9rIjjOzTMENccPl1Yhp9NRVPOmL41NIB+HEmw0LoEP2//ttdYRKFI5aGofIVjDy0x3tRaS1/hcEwkC3kVY8FVsmEuLx3FIxHx9J1AwIdsRQ/Eu640FKsm4nuqCr4MqKHdf9eIPrH2cQk57Z8kJvCIK777y8mcqTXvxIUy6DOLykvo6urjQma8mKxmX7rUljgMEkVTfJt6hzS5/WajkxFboH4EvAbVI/rR7srV68STyxpuY5IwLy3+iYbB2oi5iJikWDQfzrXQ7YAIXgfgPSknYE2YcT8IFp9J7BDekqvPa5M1aeVELI1kuZr4xWjfmYH6rzvEoDJqr4/FHv67GDTOwsG+az2Pd1aOCSfYbDN4K4J9m3OUmjrQoMb2eNiaXg0kUtsOBJBPFhSxmvwjWqi3/88uI3X7yj8iXkt6jqQ8iqCQnbFJu+2NCfcQ4U/RRlwA7tfTHqnaArM+bioeO3Zj2eOJ134jjJnKtzgtTm8oL8Ci7j9yefOIodpVeuOnT9DWzswHdixh4PWueYGruELJ3vTytKly4x1DT8fDTdhEx3ARLG2sQCq4c+gAlsHhRVcoAxCr+Dz2gbU0obTCjvicWH0LNp0+wLzEYbw88LGYcH1Dys3zOAVOwWTmVkmXOAgByOSTir0nfmFLZKl+fIpss0/XQ/EJFVxqGPRyIUacP0Rt55Sp1ChBjq7IBc5j1jB7mx+fMSKbPa48myMzSfhkCckiy41KhOUy+54QbdYRBem7r5sFiHUNyakk1ij8qTyXDXoe4xmxAfnHOMMaLwTLXeBJqjFpXdHvqGnwdfZYUXfplC1PTM0D26v3409jROHN9mCLjPPYZaWRyWkwAn1nMPl/xUEDy8Dr2kaU+EjG1tnFXgSh6VeMAN6V5EFmJPLECQR2brH0oM0fJ0/6Ujhmmvb3BMiMd5lI9Rd7Hm4Ch6mEFKzC9GfM7N/CDD3osbtV719O3 t3m1JxoS 0Ulgp6sT/6yZCdBGKoUeFgaKsVnDaTYHfcGRSzvF5ya0Z+wssrCjKiEhJfaml9aYZ0/e7XzfsBm3ZYF/FddjlG4LSNQbZeNH3ErriHzC/ONMKNkbJdi8n0nK/UvNM2FRyTTU+thtKo2sazytirxkA06+lfBtbLXE+GwSmb5RCnNEnTctESC5FR9rtTxos2DJXW/edJjGCMuqSnlCYY9PxMfVbvHNmk9XeCEu6nd+44EFV0VgV6UDHebYYE9Mjllt1Vp5aWORc9r14DZyXP4LkVGFOwq4xcksIaHsammzxfmVpLJokZi7lfJR4joLCp5JLPdyl6MShOwRN7WQqfeDJhI7rGTXtngvR2bOM 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: set_ptes() spec implies that it can only be used to set a present pte because it interprets the PFN field to increment it. However, set_pte_at() has been implemented on top of set_ptes() since set_ptes() was introduced, and set_pte_at() allows setting a pte to a not-present state. So clarify the spec to state that when nr==1, new state of pte may be present or not present. When nr>1, new state of all ptes must be present. While we are at it, tighten the spec to set requirements around the initial state of ptes; when nr==1 it may be either present or not-present. But when nr>1 all ptes must initially be not-present. All set_ptes() callsites already conform to this requirement. Stating it explicitly is useful because it allows for a simplification to the upcoming arm64 contpte implementation. Acked-by: David Hildenbrand Signed-off-by: Ryan Roberts --- include/linux/pgtable.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 49ab1f73b5c2..231370e1b80f 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -229,6 +229,10 @@ static inline pte_t pte_next_pfn(pte_t pte) * @pte: Page table entry for the first page. * @nr: Number of pages to map. * + * When nr==1, initial state of pte may be present or not present, and new state + * may be present or not present. When nr>1, initial state of all ptes must be + * not present, and new state must be present. + * * May be overridden by the architecture, or the architecture can define * set_pte() and PFN_PTE_SHIFT. * From patchwork Thu Feb 15 10:31:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557852 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 434F5C48BEB for ; Thu, 15 Feb 2024 10:32:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D3E628D0014; Thu, 15 Feb 2024 05:32:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C9E9A8D0001; Thu, 15 Feb 2024 05:32:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB4668D0014; Thu, 15 Feb 2024 05:32:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id AAE408D0001 for ; Thu, 15 Feb 2024 05:32:29 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7B80D120F31 for ; Thu, 15 Feb 2024 10:32:29 +0000 (UTC) X-FDA: 81793674018.05.AF3E497 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf04.hostedemail.com (Postfix) with ESMTP id BF4F44000E for ; Thu, 15 Feb 2024 10:32:27 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf04.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=1707993148; a=rsa-sha256; cv=none; b=ht8fmrq9N5wvgEHOPKGH3UtZFvCgtJFPEeNZPL7WOKI8uIYlu5HivNcB7ZTPp5CnoMFc7r v4rJAIxlraQnIsG3vVddAzPlJO1oqhsNdUZRv4u5i6XqUvW0W6FLZZxknSgi+afG0S69fE 0MW4ar9vo0ECVs2GJajLn9Bh8w7XWqM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf04.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=1707993148; 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=iYGxY8gKE5MXdRT9yHHw6Vo1X8bbi/EsI1w4KtEcUbM=; b=7wH84Uo1/iu+Ep+3hcX1wwinPJin6g+fy51G2/ONvTq4BMx44y6/MA4K1nGc16NA0eXZG9 4cBak/pnlSBiMvnic2AQlI8a2pMsWS8J8GH0us9QfKK6d43A9B5RVl3wTOjDIbqVAM0KtS B+WYUs1W+k1yAyBEqz1iOzwdr/8ts0E= 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 A52231480; Thu, 15 Feb 2024 02:33:07 -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 9BFF83F7B4; Thu, 15 Feb 2024 02:32:23 -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 02/18] mm: thp: Batch-collapse PMD with set_ptes() Date: Thu, 15 Feb 2024 10:31:49 +0000 Message-Id: <20240215103205.2607016-3-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-Server: rspam08 X-Rspamd-Queue-Id: BF4F44000E X-Stat-Signature: gp8ak3pbe6mfois31zgo6z66tbb457ae X-Rspam-User: X-HE-Tag: 1707993147-876281 X-HE-Meta: U2FsdGVkX19yBpUXbdI+qbDzk91hJK+DXtJD/6cJnRgEoK7ytaNNWH0fml99ydGaFKkK/GYLI/lWcrLl0l/b07//Uw6mduKry0sulsJQzWcasaX6oYidxM13rkcH2L+c8mI9bY3ZM/8I93Iuj2JnF4TqF9FVy6DWkHo/V3PDe1d0aNzo22lESkX5gH8IJfrim6wKOFAb19TjgL6J6iVRuCpmBC1yV6I762ZgLPpEa4W10rvLYcivBOGpDCsx6TKUTvav9w4maWXbQtjJe5laVb941I4FsNPHxJY6BVqEMvlJlKKmxu8ARNVmKGgWA7QxrXjGaXqS92hJIvhrDgJXqjRHI08SvdjerzMU/5QCx9al90nA6RBOSyz63q3cn+aARdBeeZLNpk2TB+PNIbyJAJESFLRfI1IXzx7zwQ4ais7X5j3dCNROITaGEIS/1wDkJndiDLPh14imVGWAOpFu2R48jb9KN2ja8X3xsoM0NZVm70bDy00V/mGID7cMj0as++0tQuOF983sUiXKcW39I+1QiSa1bwfgQAOScTur8UeTb7Wfb/XbhQTwdDxbQOrwbTkKBz1mIXxAz0wdj1E5mN9jLRcNA8xNGbb9Mx4RRjz+QO90TN1Pc3stxmMURsaLmE+akfk04GI2NVnn2HNM2gnTrM2TJUCjfg/INJ8YInz0NRVZKAE2ynYevAwzS9JPWO4iur6Ds8Ju0Zt8O6oGFZ7kDdpDT3FnC0UiNIkqYJ7vpQI8L+8durrXILGKbuaFn5bkGsU4QfAkZVGLUPpyn1q50Xzr+FH524P8ysqgLDGR893XV1ty7tgkTVpfE99rIV3u1LjVH7DTjUOCtNNgQ7UQ2B9u+mnLEHg7KZnO1jl44QGm2iTnvZMi84djvLsyCPhciU5Y/sm8dGPPWLXnlXn4CUnT7GoXBZ6Yp6RZYvNnIus6RejVfDoO4Wp1fy0SfduE/tSczH6yEnbMSYh N8xewGSx 07na56FSqlcVxaBzsmS4PECAAQs6lyvYFpKOg3H52hV7H/lD5auenuAlwyw7Lq3a1ISdYdjujOLxisoJpKEK4IDFUoEh2/qtnCJhtio2nqrWG4ajl5HEuXhuVAmH1bKupUaKcLbGzZyZNkiRYtBLHVGNyxBh4mHAES+79dBtVSjIZNDthryL/qCoc1K9y+o3RGGeSBtVbPKUy1T4QFuLbJx14xlIgK/2GWnVWQHMlEYiL10RNFNP860afllLA+FfJu5utxEgkBQA1cKqBeSWS5/Kak5mu4wPSoXiOPs/9cYdsnbQ= 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: Refactor __split_huge_pmd_locked() so that a present PMD can be collapsed to PTEs in a single batch using set_ptes(). This should improve performance a little bit, but the real motivation is to remove the need for the arm64 backend to have to fold the contpte entries. Instead, since the ptes are set as a batch, the contpte blocks can be initially set up pre-folded (once the arm64 contpte support is added in the next few patches). This leads to noticeable performance improvement during split. Acked-by: David Hildenbrand Signed-off-by: Ryan Roberts --- mm/huge_memory.c | 58 +++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 016e20bd813e..14888b15121e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2579,15 +2579,16 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, pte = pte_offset_map(&_pmd, haddr); VM_BUG_ON(!pte); - for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { - pte_t entry; - /* - * Note that NUMA hinting access restrictions are not - * transferred to avoid any possibility of altering - * permissions across VMAs. - */ - if (freeze || pmd_migration) { + + /* + * Note that NUMA hinting access restrictions are not transferred to + * avoid any possibility of altering permissions across VMAs. + */ + if (freeze || pmd_migration) { + for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { + pte_t entry; swp_entry_t swp_entry; + if (write) swp_entry = make_writable_migration_entry( page_to_pfn(page + i)); @@ -2606,25 +2607,32 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, entry = pte_swp_mksoft_dirty(entry); if (uffd_wp) entry = pte_swp_mkuffd_wp(entry); - } else { - entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot)); - if (write) - entry = pte_mkwrite(entry, vma); - if (!young) - entry = pte_mkold(entry); - /* NOTE: this may set soft-dirty too on some archs */ - if (dirty) - entry = pte_mkdirty(entry); - if (soft_dirty) - entry = pte_mksoft_dirty(entry); - if (uffd_wp) - entry = pte_mkuffd_wp(entry); + + VM_WARN_ON(!pte_none(ptep_get(pte + i))); + set_pte_at(mm, addr, pte + i, entry); } - VM_BUG_ON(!pte_none(ptep_get(pte))); - set_pte_at(mm, addr, pte, entry); - pte++; + } else { + pte_t entry; + + entry = mk_pte(page, READ_ONCE(vma->vm_page_prot)); + if (write) + entry = pte_mkwrite(entry, vma); + if (!young) + entry = pte_mkold(entry); + /* NOTE: this may set soft-dirty too on some archs */ + if (dirty) + entry = pte_mkdirty(entry); + if (soft_dirty) + entry = pte_mksoft_dirty(entry); + if (uffd_wp) + entry = pte_mkuffd_wp(entry); + + for (i = 0; i < HPAGE_PMD_NR; i++) + VM_WARN_ON(!pte_none(ptep_get(pte + i))); + + set_ptes(mm, haddr, pte, entry, HPAGE_PMD_NR); } - pte_unmap(pte - 1); + pte_unmap(pte); if (!pmd_migration) folio_remove_rmap_pmd(folio, page, vma); From patchwork Thu Feb 15 10:31:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557853 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 76B22C48BEB for ; Thu, 15 Feb 2024 10:32:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0B9768D0017; Thu, 15 Feb 2024 05:32:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 069A88D0001; Thu, 15 Feb 2024 05:32:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9CBF8D0017; Thu, 15 Feb 2024 05:32:32 -0500 (EST) 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 D2F0E8D0001 for ; Thu, 15 Feb 2024 05:32:32 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 947C8A0F23 for ; Thu, 15 Feb 2024 10:32:32 +0000 (UTC) X-FDA: 81793674144.14.72F3DB2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf28.hostedemail.com (Postfix) with ESMTP id 02D0BC001C for ; Thu, 15 Feb 2024 10:32:30 +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=1707993151; 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=AKllLFpkdBdD4+23+umGEBd3K5r9BpGIsgRkHDi52Cw=; b=18+7daeQT8JIj3R2wSEXpXPeJgdeMoskH6aw0pE8cTxYO1P+wBBcjcJwrZnOL9yS5ShWi7 y14qgJz9aNm3ZvDAqfr1QuB0YRjonGsvRsbtd1nFCfhcDhBdC2GQSAqiyXD3W3L5Rsyi26 wwM09HYVszYUxQOvGt31wjxb4PWJCK8= 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=1707993151; a=rsa-sha256; cv=none; b=hXTq8HjCX0wpt2/vydcP+Gk1ZQMvBANhrBcfD9S5q8I2a54qFZrW7sRw4OnBCXHsMmTClK PmV14CXIh0+RIDpqNEc/L99qwi+3cKNd6v0H4/OzgAH7VybI61n3QLQFiM1kZFYoFGPudH SAVje1fvYF9tds96hQWzCn888zxvOMM= 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 149FD14BF; Thu, 15 Feb 2024 02:33:11 -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 0B7CA3F7B4; Thu, 15 Feb 2024 02:32:26 -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 03/18] mm: Introduce pte_advance_pfn() and use for pte_next_pfn() Date: Thu, 15 Feb 2024 10:31:50 +0000 Message-Id: <20240215103205.2607016-4-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: 02D0BC001C X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: kshazpme7n8kz7ypkws3e9udznubpcjw X-HE-Tag: 1707993150-757809 X-HE-Meta: U2FsdGVkX1/zq48TRXZQRwYkpB3+LknE6Pxnr92qHnk3tmDVKKdXPl+e57cIWJoqcp2Iq9WoWl5fnotAuY0Xx5T1aBikHlze0NeSmD+5is43+yFhPI/2svhzogZPV2VV4IDAk2sUfJZd7q7CG2QPnf7jdV8biQAuuC3mabUVLu0uaEc+F9qG7mkx0w/IdenJhjcajvqUuEivuC6GoRaXSxEY4wZ/qRLSVk7MVLSlgC9LXA9YLcKEEijBpndahuNHGMeBvO9UQR4XKyv0ZCNeBQKVobOcB9WrVm/J5RVPJHoB+d47h4WBjtX51LHsiPAiNnWs3q6vX9wyMMt70NvJXhEQxQxdR/66FK/jSI1Z1UbIHm0SghkFg7IgeYILKL3wbipOJaH5X3fsvipSyy4VY9guJrmr/pbfdZUjKOaZNvWuc5GkzgpOK4T4DuYdcdJWyj6dF6/+4+5T4GEZbEN8dPb2eUf7PG++JJItVAC38t2fQJLYBD6LjGdK8eyeY6qh8XJz+8RCU0N5nD+h9V0tyr5wU2eK//PFHWO42RtkJJM/sEkPIXrVj2BZ5RxTcwtQ+98LHj/T1bG8TAkdkHAoffTmrJQ/m2/Q2efxHJmPwIlvRuPq3GibxyLloA17yEcPUx6zeLDSJ/U3a+m3plexyxBrxwweDIUVl2HW+wKsTDK7Dok0HsKrE4d9V0VhGwfVNxAqcKi8PTKPT30LMavZVV9HzmP5zUMzJ3wHR9OTvVDyshJaRqH3PW4aAnTP8FxVlL51MofInCqrn63vMi1SvdQKNKZ31htG85Q63MWsQRqOm16ILuTqlTT85pEmJRQmrT+ioVIB3eS0miDx5Jd2llfGClwhTJKGTBySJKxOZ5sk0mT1YCc+YO+bEuq1JLCrLhzzB+/G5PoHg6140kdAbvKUvxNfixKG/i2dwEOwh+NXFsZUFEHvOHnGfwaWWIq5p6xtbftD0NYJ0/BABUV w1oTmdHe a26MEyXAjZ5IejiuGRCY6vTQxjIht9VhAnggnohDJ/vyy98cwXyn+Md5KfMyTS7BYTHVNRiPEVPOgF5+DomAI9LM7FbNtPE1R5Q0j5R4G0gvkIACAi55txEgxn0zmRw0HBrpduhTfQiqejLkpi3qdKq8I9b9zaaL/2N6u5ThMP3teDXmawnlP7N91R1QMd8k20BsmVuusumd/vVSGl7AVDc5Wl3jiHN0YLEzWBCPtwgI/mPHQDs7fK6hMBQuH9AeFOVAA6MQP/dTdPphemkb/DyLEfQ== 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: The goal is to be able to advance a PTE by an arbitrary number of PFNs. So introduce a new API that takes a nr param. Define the default implementation here and allow for architectures to override. pte_next_pfn() becomes a wrapper around pte_advance_pfn(). Follow up commits will convert each overriding architecture's pte_next_pfn() to pte_advance_pfn(). Signed-off-by: Ryan Roberts Acked-by: David Hildenbrand --- include/linux/pgtable.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index 231370e1b80f..b7ac8358f2aa 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -212,14 +212,17 @@ static inline int pmd_dirty(pmd_t pmd) #define arch_flush_lazy_mmu_mode() do {} while (0) #endif - #ifndef pte_next_pfn -static inline pte_t pte_next_pfn(pte_t pte) +#ifndef pte_advance_pfn +static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) { - return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); + return __pte(pte_val(pte) + (nr << PFN_PTE_SHIFT)); } #endif +#define pte_next_pfn(pte) pte_advance_pfn(pte, 1) +#endif + #ifndef set_ptes /** * set_ptes - Map consecutive pages to a contiguous range of addresses. From patchwork Thu Feb 15 10:31:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557854 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 99FB6C48BEF for ; Thu, 15 Feb 2024 10:32:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28F258D0019; Thu, 15 Feb 2024 05:32:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 23F0A8D0001; Thu, 15 Feb 2024 05:32:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 106588D0019; Thu, 15 Feb 2024 05:32:36 -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 0091E8D0001 for ; Thu, 15 Feb 2024 05:32:35 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AACDEC0F4C for ; Thu, 15 Feb 2024 10:32:35 +0000 (UTC) X-FDA: 81793674270.21.B152087 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 433784000E for ; Thu, 15 Feb 2024 10:32:34 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf04.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=1707993154; 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=rWO7ZiASv+iNEWh1oRqCx438LeAl19u3CVwsECquxEQ=; b=1A4gb1huIhPg1i2G4gl/nPF9X9wx4rh1Z7A6616Ac/QsNg4rlDhPOmto4w6loMooLLDmDP 778eZUTkZJ0fXkk/u10fJoDPjhNywU8O2EHupQzWtKNj/mB8YQUmzLkl+fl9X/RMRa7oEi T+53QHRlJV7GMIHvZsljMBa7jSJsXqM= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf04.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=1707993154; a=rsa-sha256; cv=none; b=xYHBboemuXEYP6kPwnf5PvZg8dXP6IZHNf1c0db7tcd2vx4gZzbGmj8Y4VuSAZ2Qp+3sUL e5joOuaJkjud1aDG+jewT7zhEBwpv4psJJhYplb83Qv7i0okbqNcrITAM1CzzsZRtfNtix j+xWHc4raa3GuJSrHGJcdUko2qoS+Ls= 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 7862D150C; Thu, 15 Feb 2024 02:33:14 -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 6F72D3F7B4; Thu, 15 Feb 2024 02:32:30 -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 04/18] arm64/mm: Convert pte_next_pfn() to pte_advance_pfn() Date: Thu, 15 Feb 2024 10:31:51 +0000 Message-Id: <20240215103205.2607016-5-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: 433784000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: pdg9rqs7qtjeaxbodr9p6t48crjr96ai X-HE-Tag: 1707993154-375599 X-HE-Meta: U2FsdGVkX1+AOk/VMcnJepC+hzk6SQ+rMGIo1YVAtMh46MljnvPtIayvH+IwARm9bhqevyoMC1mLFKGvFwJ0wW4vM2JUM5SuJfFu8DfqPc5if1W4rFeALZgCUSPMxxahOBJ9axwbBz1GVoAxzzrygK35aaIaV358xb12ar8YbVhfra48BxYxYL/+Qp8wfJwrMXQLKV1T8lHq3DBbiRAUKnLtXC96wTmxoeQB2sRECzd3684s3lp1rbv7BJxphqf6HhB0txe5RJvR2v5AzxtfcBAfhQ26qapbMevfytP5B3jj4t3fprqMG4Ms9yM3PXwgLPPkc+XQTa6F39oaFGRg9/Tb2CTl2za2FCFU0D93uuNgYF26JAzRQAoNnVBmRrWhMjUUdPV6huELrpkbt1akxakhtjUf0sqYec+yyXftnn7saaM/ttBYGPAhiP5g4vw3KosQlCljRRSN1bwCcME4kRckkFcQ6w2vNt6SOcVhGN2AAoq8oQJOewcNnSY1XrcY85VnnhZZvmx36YRPZeaJec2VBC2kwY/LU55dI+mqFqF2hcTb/6xS0+HOqS++U9KoUSqHoeBAT0i2YQn8AYK/OI1EHHnDEFHwdRiN6YjQ5zD3os2d0XVgiQUAsTSphh8Yx0rmXwJhxlsf76edm81b7rxZSKZ+lgPUQkIr7koOQKBspwNr5gY7IjXHrl1hcSoBiNBZzqi+eu0uakbiKY3xbY75Ow0rAfSgpEX//TX/pGYgAZwZnVpK0fVpFxJ085E87tOsh/hZY03eSU5N07rsAkurfQlqNuUcnLjvsdfhJhhUJwYHGL/R5IeIYOuVftaZhS8GSD/eSKUNag6on9PnBdE4WJVVNhwY8mSHoqGRpfU3ecJoxtAft74ybQn1F6tjHHOuBw/MUHV9va2OuQtrQ7KOWrIzMosSUdbV9N2/1RBzkSNde5W9ARcOm8O2A6FpShyilPf0KdUpf4crW0Z ScQ== 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: Core-mm needs to be able to advance the pfn by an arbitrary amount, so override the new pte_advance_pfn() API to do so. Signed-off-by: Ryan Roberts Acked-by: David Hildenbrand Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 52d0b0a763f1..b6d3e9e0a946 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -351,10 +351,10 @@ static inline pgprot_t pte_pgprot(pte_t pte) return __pgprot(pte_val(pfn_pte(pfn, __pgprot(0))) ^ pte_val(pte)); } -#define pte_next_pfn pte_next_pfn -static inline pte_t pte_next_pfn(pte_t pte) +#define pte_advance_pfn pte_advance_pfn +static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) { - return pfn_pte(pte_pfn(pte) + 1, pte_pgprot(pte)); + return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte)); } static inline void set_ptes(struct mm_struct *mm, @@ -370,7 +370,7 @@ static inline void set_ptes(struct mm_struct *mm, if (--nr == 0) break; ptep++; - pte = pte_next_pfn(pte); + pte = pte_advance_pfn(pte, 1); } } #define set_ptes set_ptes From patchwork Thu Feb 15 10:31:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557855 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 3D786C48BEB for ; Thu, 15 Feb 2024 10:32:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA1628D001A; Thu, 15 Feb 2024 05:32:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C78638D0001; Thu, 15 Feb 2024 05:32:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3F148D001A; Thu, 15 Feb 2024 05:32:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AA3108D0001 for ; Thu, 15 Feb 2024 05:32:39 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 55C20120F15 for ; Thu, 15 Feb 2024 10:32:39 +0000 (UTC) X-FDA: 81793674438.26.357CAB3 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf25.hostedemail.com (Postfix) with ESMTP id D4053A0008 for ; Thu, 15 Feb 2024 10:32:37 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.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=1707993157; a=rsa-sha256; cv=none; b=wwHWeRoqmdHv3wza23bDKT5tu037WmPGRZ+dX/Bj3LSxPO9j2TZZqgVnQ5Nh7mxupHCHi6 GLvi1TmZAPCrCKUCPbE1r5QJygWbt951XigdwdX/mLXCW/0p87UFYh7ooXHmeFV5aPMiVk AWHBZ0j+yxBSqVesELTtaeKEEl5eaBo= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.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=1707993157; 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=Vt7HWCdphupCFZjtUl00kIkT38SYH1HEWXlJCnuRk3c=; b=HHljABbpVCxwRXg8OphLsG+/fso5gesWiv5GiDqgvXblGAyA5CSzqrmEP+TAmcMmdBaCMk qrOwyPDwZnR4XgxjXLB8MLEqsm5XeSpxks6WF/oWis8rLECMrCXaP66ajeyRU+r0VJxRUL Zu9GuQhJ3YSFGtBb9yu9z+r8BthnzUs= 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 DDA7C1515; Thu, 15 Feb 2024 02:33:17 -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 D34703F7B4; Thu, 15 Feb 2024 02:32:33 -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 05/18] x86/mm: Convert pte_next_pfn() to pte_advance_pfn() Date: Thu, 15 Feb 2024 10:31:52 +0000 Message-Id: <20240215103205.2607016-6-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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: D4053A0008 X-Stat-Signature: oibzr1tmybx7aihazsueyxbqyqriy53w X-HE-Tag: 1707993157-678113 X-HE-Meta: U2FsdGVkX1/Bl2elvuxz0w4yH+jy/spUa2o4gbiiTIugbBevSIHQX7gdZ+wGcnnxWwAOkYJH75rRlHJbK1JgKijIsshD4ICDewv5jjrQgHsHpI17NFnUUo71eCqNv3Uw/kyRkkTOG4gyt0iW0IYFhU92GZoxytHMWcaonEqEUPHuO/28pwCy+TV9nPOJ70SbcDYf3mMdgS3BWo1xzTGUFvN0LPFaHpqsdY0hpeUGzDHbgogMxZlvxVl+DcrLxsT1qg6V6Qs3aiQhW712kP1KDhsBoPvLSCCZFoTdhZGrg9O2xuqEcIndZfVLBdOzDQ2i5e3BN607FKkfRFb/J/ybwpduTo+rvpiVwVGiYc0EjOGMZaa2CoZZ8Fi6o/Dj+MaEdtHPD552Jc5V09MuK9wPyt4CPDIuxcrCjhiOefwyM3VMEihCtoLmvbTPa06oY4M4753WDUcLJbF37nA/+LtN+aG4HvzmxShBMCBeXPZRsY/SfGkY2LlufpHTYgembtnsUOv/uDuNkcYB6c1teSgNifOmhbvbcBIAEpOmHZ6h0FXOYASHQOolz16oRw8r5m0bwMMvcGWyOlM41hsY2/spL7VcNZdP7BX60ci3RPg5MqwZYj8VEFLzHaN4tXswNLZGombZtbGz/xud/Fx1Bbs//LBqJHsNIA7MkmzHDBU6FQpAQeCp9EWA0nzyMjkKh0aDKOQpEDdT2MLmGQB0ldxnUJ+MmJAHB9wh6wbNVYFFLVSVN88nfxTjYeOlb039P0CBHFvNIHMCdznE0WahJd+BJnsz+U8cS76kLFa52MIszEUSkchH7DAgmgXqV8FF7VzxbhcZpOl3PnSQGiMfGSJNDL1vMgkTkZBCz26P2GT0Ra+8RDFKOskUieiAturMdky/MMKR+iq5biAKo4AwitOZn413NbG4muEhihDl0vDAwbEVg56lWCovGGhZyVYTVoFWga477ph6SXCrV2jKV1y VpQ== 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: Core-mm needs to be able to advance the pfn by an arbitrary amount, so override the new pte_advance_pfn() API to do so. Signed-off-by: Ryan Roberts Reviewed-by: David Hildenbrand --- arch/x86/include/asm/pgtable.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index b50b2ef63672..69ed0ea0641b 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -955,13 +955,13 @@ static inline int pte_same(pte_t a, pte_t b) return a.pte == b.pte; } -static inline pte_t pte_next_pfn(pte_t pte) +static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) { if (__pte_needs_invert(pte_val(pte))) - return __pte(pte_val(pte) - (1UL << PFN_PTE_SHIFT)); - return __pte(pte_val(pte) + (1UL << PFN_PTE_SHIFT)); + return __pte(pte_val(pte) - (nr << PFN_PTE_SHIFT)); + return __pte(pte_val(pte) + (nr << PFN_PTE_SHIFT)); } -#define pte_next_pfn pte_next_pfn +#define pte_advance_pfn pte_advance_pfn static inline int pte_present(pte_t a) { From patchwork Thu Feb 15 10:31: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: 13557856 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 7C2C8C4829E for ; Thu, 15 Feb 2024 10:32:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E0738D001B; Thu, 15 Feb 2024 05:32:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 091518D0001; Thu, 15 Feb 2024 05:32:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9B308D001B; Thu, 15 Feb 2024 05:32:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DF5618D0001 for ; Thu, 15 Feb 2024 05:32:42 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AD8BE80FB1 for ; Thu, 15 Feb 2024 10:32:42 +0000 (UTC) X-FDA: 81793674564.02.087F6BB Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 1F5201C0022 for ; Thu, 15 Feb 2024 10:32: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=1707993161; 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=L5R0Hb3vr2XhKlRaGDOJDdSoNMHQdu7Je9L2b+7crXk=; b=YrZ2YqJnzFolYgeuUwAq9Gm25/cgZ9q1KYCxFitjffmzrBOzTdJ5fJxyitPE9T6RlmZHxj rTRcgyOtYP8VhfUOKxGSMDJK2tcmN5DmJDYWKKDsAYX+5Kj+XYOHKBFDvvq7qtFeGABpk6 CYnhDmY6ZFfQBujSJZUDYuiOdihwLdk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707993161; a=rsa-sha256; cv=none; b=pYjf8x5OCLLFXI9YmgfCjS1xdYctauCCUojY78n/mrr84jKgbMdgV/kKOyj+Uloths2IxZ mXSmiZeigEccPNWK4K2+bKHJf1tsYapV+qObHwIcWLYrwodJ5h8l7pitsxGpKOrz0Z/8Go SPtD36B3ZUGf+QS0PrtYvpQx6FjvRGE= 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 4DA741516; Thu, 15 Feb 2024 02:33:21 -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 442213F7B4; Thu, 15 Feb 2024 02:32:37 -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 06/18] mm: Tidy up pte_next_pfn() definition Date: Thu, 15 Feb 2024 10:31:53 +0000 Message-Id: <20240215103205.2607016-7-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: 1F5201C0022 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: chzpnw9mknixfsybdxnzba9tibosoiuc X-HE-Tag: 1707993160-743886 X-HE-Meta: U2FsdGVkX18DTekH2zeg73dR9RgEgmYE8EHPbv2I2m1Y6Vp7e+Hz0B8xE9THR8L0zly2ys+D6mMbWN5FNlEr/MiHdB58iS3u+WP2hbba6anIuyjKmvFfwTZuGXJoANrUYJhw00RO+Y1DU5TDXGAUqfIDYmSu8CifmxjjkETRxv9hb81t3ln+VpbzCpKZ7aERsvhUZRhvj0UOP9ngCuSrqEJ2aguQCPyURdUcuIEcWjYE3il4yN/0xCX+dFgCQ1WaW12xwUyU2+8nzXeRJ/49uQEsRnym9I82+SUU8zMQgH2MhEY5SgepOIGm3h0gBVkMYIDI1ZqoDsQZzxhl2VJ5Nj7KOVUInaEw/i52OO+t7RcDO60RdFt3Z3+UUj5x4ku1itJlN0f+SXHVoX/ho/GQHkJXYDAaIUXYMw08w5oXSwiJ1aYFAe9a7fweW7LUD7S+wdRaD/VP4PgKxzbTW5saI7wKjwtssX2OfxcuyzNAyjCC6CcDGrQcrhuYCtrXpVQASDvrKycjDxWHWQNNysHN+poKb7CbZf+rBCt4HcXDxqURQZ9UlprkuJtfsnOouflXqla3Ag5N4ZKLLQRkT+xqfWY/QqoUfSvq5uEvYNCeNi8Ago6Sy8WLALUFIeHlgzi82kPPDa6sp70wLy1rBEUBEtqmeBerZmYJCtCdk489LVmvMt8pgsMeI89FIwNYc5VI/76VGC21r91AIslf/Gw6agbiTOZcxTZ6SWfsUqDs0HQ6LWiTtISbOt9avO8xhy6AZdw6mrmNIC1mJODjgc8faMi1saGTDVThq5RbTfzhfEVp4eJcMvlu8y3weyvzF7F1zM+XHmiaaKbjSaqZTO96ziCSdKbopbrMqPk/NSsstAo46538Qink1Z1BI1FcgQPTaIjtIk0H8v011sLiM3mXMBBhXdTD0zSvQ2pYodVzzCTpHsKjpN5ipwyiR2kL4M7PFWd/0obfqg0305qXUNK MSvKyzY/ NY9kuTJuePfC/FRqeN7pPzpUA1/0FcR1egFIiDHeCTO2+hknTDbGuWCO1fXL0h0M+v1PoZBpg1bTSUUYM5jU4y7e7TSUuq8OoFU9DApaA1LNQHseOZ68gLOKHFcppqLGm4xZ4G1pxjD4cwMNKTR99jni91+MqBCqGUq9G9oPITjh20hhDvHwkxBxcZuAFM7r0OWIsxqYzw1IpQ21Moa2jsqcDjubVThin9AmRU2mJsbvsM/ctkUJb1afM7C1VGDfYmo/mmaKi2oLUJbWkoV37rHhV5g== 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: Now that the all architecture overrides of pte_next_pfn() have been replaced with pte_advance_pfn(), we can simplify the definition of the generic pte_next_pfn() macro so that it is unconditionally defined. Signed-off-by: Ryan Roberts Acked-by: David Hildenbrand --- include/linux/pgtable.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index b7ac8358f2aa..bc005d84f764 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -212,7 +212,6 @@ static inline int pmd_dirty(pmd_t pmd) #define arch_flush_lazy_mmu_mode() do {} while (0) #endif -#ifndef pte_next_pfn #ifndef pte_advance_pfn static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) { @@ -221,7 +220,6 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) #endif #define pte_next_pfn(pte) pte_advance_pfn(pte, 1) -#endif #ifndef set_ptes /** From patchwork Thu Feb 15 10:31:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557857 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 287B1C48BEB for ; Thu, 15 Feb 2024 10:32:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AA6078D0001; Thu, 15 Feb 2024 05:32:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A2DF96B0083; Thu, 15 Feb 2024 05:32:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 880FA8D0001; Thu, 15 Feb 2024 05:32:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 75E406B0082 for ; Thu, 15 Feb 2024 05:32:46 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 50F541C199A for ; Thu, 15 Feb 2024 10:32:46 +0000 (UTC) X-FDA: 81793674732.07.19438E3 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf22.hostedemail.com (Postfix) with ESMTP id 9F420C000A for ; Thu, 15 Feb 2024 10:32:44 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf22.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=1707993164; 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=4BAlAPgOnjDFN2Hap4t525VVNCrDp7iVSl0knImXGnI=; b=1JWqvSGDk5Zpe4xvpOjs6smSncpBdgcAp9tII+KQFz43BXP+Ofibx93Yk0fsFej28RjpeO 6cZ08kJqspD0m5izjyndcEy6aDI+eYd/M5c/EoFwyXjb4h293pi5WMrTRUUOcaGRPYoadJ vf02zKasFVHLFmtxUIM76VMM8hy9bCU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf22.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=1707993164; a=rsa-sha256; cv=none; b=EvBglrAptxDbTs+/hdCT0YONhjJidlG5VBrvAoCKv1h3S/UdJlLMdcWf6BtsZGfzQBETmr l1ytlDixryfvpKZHgSlwJ24fW2yNaaCkzPpZ7nnTyaE2221STT5FrSTbI5XK1ehxlkIsic Ues+pxFuUsOXMUT7fEj1JjxctpNcZ04= 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 B2453152B; Thu, 15 Feb 2024 02:33:24 -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 A90E53F7B4; Thu, 15 Feb 2024 02:32:40 -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 07/18] arm64/mm: Convert READ_ONCE(*ptep) to ptep_get(ptep) Date: Thu, 15 Feb 2024 10:31:54 +0000 Message-Id: <20240215103205.2607016-8-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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9F420C000A X-Stat-Signature: s9qtdfxkwdjtmj8p6ry3c78djjyurao4 X-HE-Tag: 1707993164-971970 X-HE-Meta: U2FsdGVkX1+nbpGfw8cRXlzGCx9YsSnluFmqK6RlmULA4KDqTHiN2RvPq/HkjugydSWp3NdUKiXKxHZIZnY+p2Iazs/Y0m+v8rq7ep7J0oTuEyGK7Ssz4WzVSRYX1KXjmVrPeMw/+AsjwsgJUGiNtxGkwAuAGExxV0TI7dyTmuW17oA9HSmu9O49XR4Gi/4YMMSGmuRr4TwMARphm8evg8pWJMYUlzWMCOr/D7qjd8W/0Nl3hsDyPufnKBx3vghL6UNqDYvQvNJP58ToH810REypikag8B/BtZ9R70FcEWOfh0sXJnzDrcSrCtlfnRB7MhzGFFWjlhzRiI5lUDJBtqnNI9dGIJpg7EVysM5/acPxYNcFGc63D7Gd4krbAg+Qeewtx1JCeYEdqMfXt1SFlxvmnWV7SeLAZi/gRhNWpjEG8vZXY1j+omlAnN9pVM0jyrnpT0yjyXtvKoHNzACJiqKaOmqg8HSv/BuWMMtBxhPP48Suhh9QnN7bxsslf2Z6nOO7tiM8Ai9dlXQBDcjkD8FHI97goxb5ArJ4Q4WuCVjkGBDqRvuc9T2hu9Rf7ecbskLXxBVXtmAxAcXHbweRcPH2izg6mwYX8wCJHUuI8jAdoT6li/3zx7Hr3N3e2aKEb1SR9+4mnLF/9eQKVU+RAu5lHRRD2fplrRNWxHIDn33V95LqSF60mq0LwbN3F24LPN8e2wxNmgADtuq7r/dmXLKs7hXiBAgZxEsU1nVbbmoWtmgJnR37NEL0+kS86VyvBQ9j1ZW0/GcrKzPHL2WkVz97MOlQF0/HRrI2214j62QMblU31foHncP3ATw952TiDtccEzEiu42pbO9nIbPgQxCtE6ElpVVhLjDU1wokkjwoP/ceC608XVZxCT1aEgD3eUbBvGq2uTSlOrjpmEB/mXM0mxfT/UCYx2DZKxojJgEwtGaa1VELglh2xy7iLsYkDWX3JEjslfKhIBQnAtD EIVklPj4 v3sfG+fQ3fTOCsJdpCyMn/9RBGdRsA43jC/GZxD3MI/Vmub/yqMWfYjwZ+1aREiclu6I9mrbxKnorpg+/vJ1A+nhsLi3eIwGdjwZ6A3Wx71BXbI3K+xnsmGjnAuDBi1p0p1ppIfECYLP4oWfC8e7VSyFNJ4RYB22/pl+T+S6sz7VSIyT3kEF26TxiuWByJ5FZ+rPokbEt3MNnU9Q+8dUQeGJ0h9IClar/xAuGI2roXWg1jGMFideSlAfGsZFKGCkjpIkJqpJDom5ZSyiXFbvAFXi/4vSDKZaBsXRFeXzW2aAd8EQ= 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: There are a number of places in the arch code that read a pte by using the READ_ONCE() macro. Refactor these call sites to instead use the ptep_get() helper, which itself is a READ_ONCE(). Generated code should be the same. This will benefit us when we shortly introduce the transparent contpte support. In this case, ptep_get() will become more complex so we now have all the code abstracted through it. Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 12 +++++++++--- arch/arm64/kernel/efi.c | 2 +- arch/arm64/mm/fault.c | 4 ++-- arch/arm64/mm/hugetlbpage.c | 6 +++--- arch/arm64/mm/kasan_init.c | 2 +- arch/arm64/mm/mmu.c | 12 ++++++------ arch/arm64/mm/pageattr.c | 4 ++-- arch/arm64/mm/trans_pgd.c | 2 +- 8 files changed, 25 insertions(+), 19 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index b6d3e9e0a946..de034ca40bad 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -275,6 +275,12 @@ static inline void set_pte(pte_t *ptep, pte_t pte) } } +#define ptep_get ptep_get +static inline pte_t ptep_get(pte_t *ptep) +{ + return READ_ONCE(*ptep); +} + extern void __sync_icache_dcache(pte_t pteval); bool pgattr_change_is_safe(u64 old, u64 new); @@ -302,7 +308,7 @@ static inline void __check_safe_pte_update(struct mm_struct *mm, pte_t *ptep, if (!IS_ENABLED(CONFIG_DEBUG_VM)) return; - old_pte = READ_ONCE(*ptep); + old_pte = ptep_get(ptep); if (!pte_valid(old_pte) || !pte_valid(pte)) return; @@ -904,7 +910,7 @@ static inline int __ptep_test_and_clear_young(pte_t *ptep) { pte_t old_pte, pte; - pte = READ_ONCE(*ptep); + pte = ptep_get(ptep); do { old_pte = pte; pte = pte_mkold(pte); @@ -986,7 +992,7 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addres { pte_t old_pte, pte; - pte = READ_ONCE(*ptep); + pte = ptep_get(ptep); do { old_pte = pte; pte = pte_wrprotect(pte); diff --git a/arch/arm64/kernel/efi.c b/arch/arm64/kernel/efi.c index 0228001347be..d0e08e93b246 100644 --- a/arch/arm64/kernel/efi.c +++ b/arch/arm64/kernel/efi.c @@ -103,7 +103,7 @@ static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data) { struct set_perm_data *spd = data; const efi_memory_desc_t *md = spd->md; - pte_t pte = READ_ONCE(*ptep); + pte_t pte = ptep_get(ptep); if (md->attribute & EFI_MEMORY_RO) pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 55f6455a8284..a254761fa1bd 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -191,7 +191,7 @@ static void show_pte(unsigned long addr) if (!ptep) break; - pte = READ_ONCE(*ptep); + pte = ptep_get(ptep); pr_cont(", pte=%016llx", pte_val(pte)); pte_unmap(ptep); } while(0); @@ -214,7 +214,7 @@ int ptep_set_access_flags(struct vm_area_struct *vma, pte_t entry, int dirty) { pteval_t old_pteval, pteval; - pte_t pte = READ_ONCE(*ptep); + pte_t pte = ptep_get(ptep); if (pte_same(pte, entry)) return 0; diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 6720ec8d50e7..2892f925ed66 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -485,7 +485,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, size_t pgsize; pte_t pte; - if (!pte_cont(READ_ONCE(*ptep))) { + if (!pte_cont(ptep_get(ptep))) { ptep_set_wrprotect(mm, addr, ptep); return; } @@ -510,7 +510,7 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, size_t pgsize; int ncontig; - if (!pte_cont(READ_ONCE(*ptep))) + if (!pte_cont(ptep_get(ptep))) return ptep_clear_flush(vma, addr, ptep); ncontig = find_num_contig(mm, addr, ptep, &pgsize); @@ -543,7 +543,7 @@ pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr * when the permission changes from executable to non-executable * in cases where cpu is affected with errata #2645198. */ - if (pte_user_exec(READ_ONCE(*ptep))) + if (pte_user_exec(ptep_get(ptep))) return huge_ptep_clear_flush(vma, addr, ptep); } return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep); diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index 4c7ad574b946..c2a9f4f6c7dd 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -113,7 +113,7 @@ static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr, memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE); next = addr + PAGE_SIZE; set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL)); - } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep))); + } while (ptep++, addr = next, addr != end && pte_none(ptep_get(ptep))); } static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr, diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 3a27d887f7dd..343629a17042 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -173,7 +173,7 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, ptep = pte_set_fixmap_offset(pmdp, addr); do { - pte_t old_pte = READ_ONCE(*ptep); + pte_t old_pte = ptep_get(ptep); set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot)); @@ -182,7 +182,7 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, * only allow updates to the permission attributes. */ BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), - READ_ONCE(pte_val(*ptep)))); + pte_val(ptep_get(ptep)))); phys += PAGE_SIZE; } while (ptep++, addr += PAGE_SIZE, addr != end); @@ -852,7 +852,7 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, do { ptep = pte_offset_kernel(pmdp, addr); - pte = READ_ONCE(*ptep); + pte = ptep_get(ptep); if (pte_none(pte)) continue; @@ -985,7 +985,7 @@ static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, do { ptep = pte_offset_kernel(pmdp, addr); - pte = READ_ONCE(*ptep); + pte = ptep_get(ptep); /* * This is just a sanity check here which verifies that @@ -1004,7 +1004,7 @@ static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, */ ptep = pte_offset_kernel(pmdp, 0UL); for (i = 0; i < PTRS_PER_PTE; i++) { - if (!pte_none(READ_ONCE(ptep[i]))) + if (!pte_none(ptep_get(&ptep[i]))) return; } @@ -1473,7 +1473,7 @@ pte_t ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr, pte * when the permission changes from executable to non-executable * in cases where cpu is affected with errata #2645198. */ - if (pte_user_exec(READ_ONCE(*ptep))) + if (pte_user_exec(ptep_get(ptep))) return ptep_clear_flush(vma, addr, ptep); } return ptep_get_and_clear(vma->vm_mm, addr, ptep); diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c index 924843f1f661..73a5e8f82586 100644 --- a/arch/arm64/mm/pageattr.c +++ b/arch/arm64/mm/pageattr.c @@ -36,7 +36,7 @@ bool can_set_direct_map(void) static int change_page_range(pte_t *ptep, unsigned long addr, void *data) { struct page_change_data *cdata = data; - pte_t pte = READ_ONCE(*ptep); + pte_t pte = ptep_get(ptep); pte = clear_pte_bit(pte, cdata->clear_mask); pte = set_pte_bit(pte, cdata->set_mask); @@ -245,5 +245,5 @@ bool kernel_page_present(struct page *page) return true; ptep = pte_offset_kernel(pmdp, addr); - return pte_valid(READ_ONCE(*ptep)); + return pte_valid(ptep_get(ptep)); } diff --git a/arch/arm64/mm/trans_pgd.c b/arch/arm64/mm/trans_pgd.c index 7b14df3c6477..f71ab4704cce 100644 --- a/arch/arm64/mm/trans_pgd.c +++ b/arch/arm64/mm/trans_pgd.c @@ -33,7 +33,7 @@ static void *trans_alloc(struct trans_pgd_info *info) static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr) { - pte_t pte = READ_ONCE(*src_ptep); + pte_t pte = ptep_get(src_ptep); if (pte_valid(pte)) { /* From patchwork Thu Feb 15 10:31:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557858 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 CADB1C48BEF for ; Thu, 15 Feb 2024 10:32:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 417308D001C; Thu, 15 Feb 2024 05:32:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C4F56B0083; Thu, 15 Feb 2024 05:32:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 266FC8D001C; Thu, 15 Feb 2024 05:32:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0E7076B0082 for ; Thu, 15 Feb 2024 05:32:50 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DC6C61C199C for ; Thu, 15 Feb 2024 10:32:49 +0000 (UTC) X-FDA: 81793674858.14.D2623A2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf14.hostedemail.com (Postfix) with ESMTP id 2357610000D for ; Thu, 15 Feb 2024 10:32:47 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf14.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=1707993168; 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=ZeD0LRgPYyPlqU6eixwfCpwvc0jLGbSMmuxIvFt/HH8=; b=qRqIW2SH2Brn/X13CmVfC28rg7pwBrodPHSMQjiXmQ9bEcWcCe07sj8PjdNC7E9TxsgwW9 omeUFu7UVNt883VCWpW1w2+84jbjvHs5Beg/2s53UEu3bJn4fJ6oFBzJtjqnrh3Q+wmlVF 0XnmTC/s1JJGvE1Kdpg9HyddIjTaBuo= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf14.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=1707993168; a=rsa-sha256; cv=none; b=XiBKQcHKtFybITIcEvGdiNWrGK9HduwAnrk5ZjJ2Vs+sZhoWPP0JVIbgnultPsgqzylmzA UgNdZQStiTp3TgEVynGXe97lr5g5DcWDUo/Cj55cNAbdYuMnwBuoIK2z/BEeUbDD8+zDLz jB0HSq3CJ8T/1zQo1RM4/obPjRqE7Jk= 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 222B7153B; Thu, 15 Feb 2024 02:33:28 -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 18FC43F7B4; Thu, 15 Feb 2024 02:32:43 -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 08/18] arm64/mm: Convert set_pte_at() to set_ptes(..., 1) Date: Thu, 15 Feb 2024 10:31:55 +0000 Message-Id: <20240215103205.2607016-9-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: 2357610000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: sy94u3jdpr7um1f7bq3dmmyfniqyq3ya X-HE-Tag: 1707993167-393255 X-HE-Meta: U2FsdGVkX18+pAykQ7t37kX7J/VTQhWPmxEoqcASGQfvt/rpKbY/svgSGuACX/8HdVfwl+OqmB6HGqjRu5TH0bpVO/uMrQ3tTWUaAf57+5x4R+Riha0y0m4A9CJs6uSlzfyL8qLxfyquuTlBNGxj9JR1tk5HLMxjJIqi2Puz99IuqQspzLBa6NZrWHjfG7/M8ok5NaGAI2tU2XGnCunMcMU5EBeGbcebGYxPkwnlt1F8gm44tpjT3CGv2uLH+JpMBiMVD/SkyQCgdAKNzen0A5RNvxAMBfCSCD23ZoTPHinRl6/eVE90C1bbsWBpiWE7ETTkZAFNu0edC6nvqCNUFNKD749t5G16/mxx54DKpooXeqDl7j2ng/vQkhb3Wc9BkYWU0XX9w8lc59OnGhs82ChneKjOxlz0LaJOpbAiONkrKKyyQNq37gILqbPAySzGJLJJexgLoZwYJZYyWSyYEQYN1qfXIwzPj1CVtSEUeg/QWyaJa7QKqB5BAsfAXD1aHowLYu2MCtOGdOslvfjZ+Wn1mjblLiMqcqDs3jLRSFROfVN2aclKZqUBJcVkJ96H4sjkjFK2lLPnxdHg+zvCKGMKnliVmx7K/rXLmD6K9CZ0meuVWxwoCDrwAkMGiO2/1o3IepY4PKD5qio28aqeFrt3JIRhqUCzfMJs0aLe3XwlCwAwBi2L87fMPrNfp//dyuf/SPCP0sml/KOFQbX9w2Xplca2KzbWii6KeZstlw7HQ0OJVmmwv+lQcmHoScG5YUo4a3o1h178dqNYgeXWgxHPxxWI9OIZ+Cg8xIsBy/s20eVaV1dFYWp3tGNisQZq98dXwx2yZB5ctf5XwCPmyn9XMeFUJOQz+0MypRQGapzy8eauSDihLhNDGHNcGma41+rzPW8ImkeB3BmQmEzQ6Vgi7QZYbbgjAbxuAk6QzwZXjG/JPm0UXAQ80burFM5uY4H1JMCKxZYiSJgYZkl QuEdnVPD NG+C7xZElHfqJ+Np+y4sAf2dt546F0ycpq3ZchtEOshmAThfpNv0iCm+sn2n608cuP/rR71tD2jSJOGZbwy4sqsZalxTr/7E/ubW1ioB7yi9iDBW+/graywDrjfWuNLT7Und/sTWGJXW1NoBHA3K0EFMcbJUhVdOHqIVD2QUL7AYM/ZKMaWd3anCwoUMVq151NviXRgFQ+mhO8raZt6ubih3CMThloKt3zMuSq//PNnNGbZG3ANCGketOs6Y4iW1OVlzT8DYYDo5TJb3ZG7fzjb6Jcxq6urKYDdE2PDDC2+ZHN5U= 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: Since set_ptes() was introduced, set_pte_at() has been implemented as a generic macro around set_ptes(..., 1). So this change should continue to generate the same code. However, making this change prepares us for the transparent contpte support. It means we can reroute set_ptes() to __set_ptes(). Since set_pte_at() is a generic macro, there will be no equivalent __set_pte_at() to reroute to. Note that a couple of calls to set_pte_at() remain in the arch code. This is intentional, since those call sites are acting on behalf of core-mm and should continue to call into the public set_ptes() rather than the arch-private __set_ptes(). Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 2 +- arch/arm64/kernel/mte.c | 2 +- arch/arm64/kvm/guest.c | 2 +- arch/arm64/mm/fault.c | 2 +- arch/arm64/mm/hugetlbpage.c | 10 +++++----- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index de034ca40bad..9a2df85eb493 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1084,7 +1084,7 @@ static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio) #endif /* CONFIG_ARM64_MTE */ /* - * On AArch64, the cache coherency is handled via the set_pte_at() function. + * On AArch64, the cache coherency is handled via the set_ptes() function. */ static inline void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index a41ef3213e1e..59bfe2e96f8f 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -67,7 +67,7 @@ int memcmp_pages(struct page *page1, struct page *page2) /* * If the page content is identical but at least one of the pages is * tagged, return non-zero to avoid KSM merging. If only one of the - * pages is tagged, set_pte_at() may zero or change the tags of the + * pages is tagged, set_ptes() may zero or change the tags of the * other page via mte_sync_tags(). */ if (page_mte_tagged(page1) || page_mte_tagged(page2)) diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index aaf1d4939739..6e0df623c8e9 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -1072,7 +1072,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, } else { /* * Only locking to serialise with a concurrent - * set_pte_at() in the VMM but still overriding the + * set_ptes() in the VMM but still overriding the * tags, hence ignoring the return value. */ try_page_mte_tagging(page); diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index a254761fa1bd..3235e23309ec 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -205,7 +205,7 @@ static void show_pte(unsigned long addr) * * It needs to cope with hardware update of the accessed/dirty state by other * agents in the system and can safely skip the __sync_icache_dcache() call as, - * like set_pte_at(), the PTE is never changed from no-exec to exec here. + * like set_ptes(), the PTE is never changed from no-exec to exec here. * * Returns whether or not the PTE actually changed. */ diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 2892f925ed66..27f6160890d1 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -247,12 +247,12 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, if (!pte_present(pte)) { for (i = 0; i < ncontig; i++, ptep++, addr += pgsize) - set_pte_at(mm, addr, ptep, pte); + set_ptes(mm, addr, ptep, pte, 1); return; } if (!pte_cont(pte)) { - set_pte_at(mm, addr, ptep, pte); + set_ptes(mm, addr, ptep, pte, 1); return; } @@ -263,7 +263,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, clear_flush(mm, addr, ptep, pgsize, ncontig); for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); + set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); } pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, @@ -471,7 +471,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, hugeprot = pte_pgprot(pte); for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); + set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); return 1; } @@ -500,7 +500,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pfn = pte_pfn(pte); for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - set_pte_at(mm, addr, ptep, pfn_pte(pfn, hugeprot)); + set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); } pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, From patchwork Thu Feb 15 10:31:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557859 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 D51CFC48BEB for ; Thu, 15 Feb 2024 10:32:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B03A6B0088; Thu, 15 Feb 2024 05:32:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E8458D001E; Thu, 15 Feb 2024 05:32:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 488D88D001D; Thu, 15 Feb 2024 05:32:53 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 343436B0088 for ; Thu, 15 Feb 2024 05:32:53 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 13A17120F1B for ; Thu, 15 Feb 2024 10:32:53 +0000 (UTC) X-FDA: 81793675026.20.86A1134 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf21.hostedemail.com (Postfix) with ESMTP id 7AAC91C0019 for ; Thu, 15 Feb 2024 10:32:51 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf21.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=1707993171; a=rsa-sha256; cv=none; b=pJoGn7iFqyy84l6pIWpkbniVo14TE5EAtnbws+XJnC5BnjlzBjziXStqt2QUYeoaJ89lI4 7VXoQ5fWT/+Hg7BAZ7oiQy4e3m23t5ljvlMz96e+ogaoqS3qcsxj06cotDUG/CCXLvHgW8 ROctppPz3KKy63SZPYF/D8IntYUPA+8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf21.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=1707993171; 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=NNqJIU65wxbHkraYUk5NQY8zX49rVnFHXTzrcPivxfA=; b=xaWoJ1CgpeN5uEH5YxIZij4A0LRU6s0NHqx64wZbMDynJiRGZz7r47bld6oZB8xMyiqDAZ ysC0YaNU6SHkwTvMmSVxZARvdfsd4JOgL+JNRpJfxnyZ39o98wsplI3lg58oM0SY2jlqMg 6NXdTVuoSRjCKrt13uxATvlILcXM2q4= 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 86F1E1570; Thu, 15 Feb 2024 02:33:31 -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 7D7603F7B4; Thu, 15 Feb 2024 02:32:47 -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 09/18] arm64/mm: Convert ptep_clear() to ptep_get_and_clear() Date: Thu, 15 Feb 2024 10:31:56 +0000 Message-Id: <20240215103205.2607016-10-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-Server: rspam08 X-Rspamd-Queue-Id: 7AAC91C0019 X-Stat-Signature: o9sn1uf5xcxztdfz1kbsg5nii7fwzbdp X-Rspam-User: X-HE-Tag: 1707993171-329934 X-HE-Meta: U2FsdGVkX188JuuG6Lj2XuoZAY9WsJZwxkBjnDcfuETsDd/NfzD4HbCahq2WZ48chUI8XBPSyY2/w0I6v3WTXlOLv3c8hkCohkb2LQdVSrhhckxbP8mimUDaJVjXsZtfKoaa9nfJkTwWahio3W8Tg9jM/2ugS+24NIYYhC5fssA6EjuQ5LnQ8s7zIotJE6tJdxVLIDncq/ZrvmrM7Mybsc0NhZYXeajvfn7bJqziKjq6mGQbfSpDX2qNfY5fbjOkNyPG7SEO0fAldG8pHU+hRNjY4XiPIjFW7i9a7k9G0in35jlBzfQzDCI5Oyz5FM8O/0JpQpd3hv78JXmpPekyyS99l8hWt+NcdYu+Xq77TGyqmoI+GuMWhGxhTx9S5X0pxPQcQgMAp3odSuLA0vcY+88cxHq9rKUsP9aHsE4afQ0rhSVu3yD+XBE93sFwE4B77VT/0nra6zhswvposT0sW4vLCc/zX1HMqRROz6PdsPrOIJQZxEogMpEsnHCegvQgzqXngv8wqwp8PCp2KDdo3SrvHNWi5AkZxcfwX8hTXaI3kZ0mjLCSHy2lAiL6Z5Ry+tz0fEqVuqbAxET3bny0jGNFh13SLV4xI3CjVhyzoDRb7g5gxmTHCDWQ9ZMYefqNZ1NNit8yRZJhkrbdIZwMrkbOG1kytIU9HME6QzA8dP7dOD2msFnsURsOIYxX5Km908IzsNDzModp41THHa0zdKGhCxJs4NtjWFCiGso9uRyksRGOS5c5uS10kksR5KlqGAz7QwSHVnf51CZBsvjtlaoycVb9fLlWGMb+SY8gRwwVoo2pAcySAdeuuAMkjt4eD6dDkqQ/XRNY8ATnqrV2qHM4FGboo/+bpcB6JZtDKCR069knJfsZ15qpeXBoua6RDDGSJPBp4/O+FylOdAhzok+bBh03nwenGXNqrXC4diFBNG+rdsGJAuhmlKqcztMiyJkdQXQoMtKLvuMH1j/ GkY+hrE2 695qj3SVrA7OSLAYkIZnIvixcfP0gZLm/WI0+/Qzo0QZ8WSTL2MTE9wX1mABF7Bt9tKDF5LjopWgOPF2yq+E+2HYnHvKoCMSP0bKZN8CKz2bn0u/usQ7afad+1LOtQzndcKds2oCEcHyAfRBkiMUZmWJw6QhDwBCHpZXGj2j7CHv8Ibb/fZEA7UPMS5hdUygTCe8z400hwz0tSLuwt3GcdT8d0SfrL8UhX85i7gxlGnbd9S4ehcc506vpMGD6v3uw/7TOGNGfe2DHNts8zT/cY2hYcKvVycAqrSl8SrqEb3CtVM4btLvuhG7iETbwya6u9kL7cM4h0zbB4vZjZWcbeJaLfnt3XlQeIfaL 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: ptep_clear() is a generic wrapper around the arch-implemented ptep_get_and_clear(). We are about to convert ptep_get_and_clear() into a public version and private version (__ptep_get_and_clear()) to support the transparent contpte work. We won't have a private version of ptep_clear() so let's convert it to directly call ptep_get_and_clear(). Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/mm/hugetlbpage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 27f6160890d1..48e8b429879d 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -229,7 +229,7 @@ static void clear_flush(struct mm_struct *mm, unsigned long i, saddr = addr; for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) - ptep_clear(mm, addr, ptep); + ptep_get_and_clear(mm, addr, ptep); flush_tlb_range(&vma, saddr, addr); } From patchwork Thu Feb 15 10:31:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557860 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 3476EC48BEF for ; Thu, 15 Feb 2024 10:32:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BB73D6B0089; Thu, 15 Feb 2024 05:32:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B682F8D001D; Thu, 15 Feb 2024 05:32:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9461F8D000E; Thu, 15 Feb 2024 05:32:56 -0500 (EST) 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 7B7066B0089 for ; Thu, 15 Feb 2024 05:32:56 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5DE1F8014A for ; Thu, 15 Feb 2024 10:32:56 +0000 (UTC) X-FDA: 81793675152.28.C232A7C Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf02.hostedemail.com (Postfix) with ESMTP id CCDF380017 for ; Thu, 15 Feb 2024 10:32:54 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.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=1707993175; 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=i0bpuL9kL8NtPuhBKTPcA0JZgz3FoRCYzdhThhi+DB8=; b=axyS5hxx67n/N8unAc1ulOMK3aU1aVrlCyZWgW88wfO5QzKifX3T9wh879q6L1+jxOgtYc 9MplSn5xPy4Jwl3l2ZfwIcP+mG1HGI0xTgj5AbiaZjB+J8Xvn7yEi7jxYv0GQ7BqrgRI3n /DzMwUT/KsrAMRq93SLl5Lwr1hUWaP4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707993175; a=rsa-sha256; cv=none; b=cVYsY+hKnI/71ma5bxDFaHWVlqx8y6kKP7wcb53ivpi3SBC8fCGrSrvnK8Em4moxLAtuI0 UxPMZIOy2x17579Caporv4LbBbzxxtokqkhMUnzeZzwiA1SkGkwdq1wix29JeNfs6W5mhL qmhYWEkB4Ed6IqgRAtgcnrGmgMJlD30= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.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 1344C1576; Thu, 15 Feb 2024 02:33:35 -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 E30303F7B4; Thu, 15 Feb 2024 02:32:50 -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 10/18] arm64/mm: New ptep layer to manage contig bit Date: Thu, 15 Feb 2024 10:31:57 +0000 Message-Id: <20240215103205.2607016-11-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: CCDF380017 X-Rspam-User: X-Stat-Signature: cotojnjb7a34roth9bbg6jmptow8qwkq X-Rspamd-Server: rspam03 X-HE-Tag: 1707993174-12466 X-HE-Meta: U2FsdGVkX1+scG+CF6Nc4EHcrNAViBRRO5J1r2IuF7PXwtzYFPBsa0TuPWlJaQgHil8j/j4CCxfIdLA9BsOhGRyNQdqvUnH5Zyqi32ZFzN6mTuz5JFyAA/ukc/xkrPFVixvUbmIUb3C+hpo6whMWt9LhjqNxyIK0TbWQi2GW/Wu6MMZH0x4Izg9vAgHxJExkouQ0IMBQprur5PWhFdrERc+BQwFJeeZZdhCXTB81W4hPKYObF7Q7lIVnhT5RjNm+fVBZcGey8/LNe0jicjTvIYBb5yEkveU8GRpvI/Dr4DGwyPx6osqqbwS1EEafBl2gjKE8tWEa3bsPFE7FL1Xrd4mGQgEMYoNqc9vFBGjs+GsKU7JAleceKo6u9rNbYocOscz8YWsHEvlBSzWs04aE1mTVx6RFZFQrzqonb7kVjiXXlt6uvn43enMi72e0mM9P+PMKLtREpbmjvbFITEfOSfoSbHg3P0z/Iw/NeMA2VPS7nO1/Rw+9j2JSIMDH1lT9xDreRMC4Ao6lwjLdhKopN+c99p4pZGX8qWi1ylFqyvRNdF5tR3yqxNIjZzHy0hjDvWz3lQgice4K8y/ixdfIejE+cQcE/SEs6eiCjt6TU4G4rq3XCcdCTDHZss02l6zqLAVPKDmBTxF6XDn3UV/SW2apXPsUpfyvByGr/o538exZu84sSG6GNwIUkManEJy9l3fF51lX77rQlphjmvmJKxHX3TnsDEDvDq/o2wQRRmqZVbt+48W8OxQNcUze9JXB56ilWG1amktXi97Dg9UZqVOICswC2AjXvfrvOExdunkhy6yKgim5agdMZUOQPTbQMV7bb/i/D16LSisrru6uGCGSYlu/CAjLvVd99gJHE5ZvgVH+oVHNK+0n7N9QrapQoscVn+eEOHU9DqeHnNZrK6PZHy+WQl/sDQl+ZenpFIg= 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: Create a new layer for the in-table PTE manipulation APIs. For now, The existing API is prefixed with double underscore to become the arch-private API and the public API is just a simple wrapper that calls the private API. The public API implementation will subsequently be used to transparently manipulate the contiguous bit where appropriate. But since there are already some contig-aware users (e.g. hugetlb, kernel mapper), we must first ensure those users use the private API directly so that the future contig-bit manipulations in the public API do not interfere with those existing uses. The following APIs are treated this way: - ptep_get - set_pte - set_ptes - pte_clear - ptep_get_and_clear - ptep_test_and_clear_young - ptep_clear_flush_young - ptep_set_wrprotect - ptep_set_access_flags Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 83 +++++++++++++++++--------------- arch/arm64/kernel/efi.c | 4 +- arch/arm64/kernel/mte.c | 2 +- arch/arm64/kvm/guest.c | 2 +- arch/arm64/mm/fault.c | 12 ++--- arch/arm64/mm/fixmap.c | 4 +- arch/arm64/mm/hugetlbpage.c | 40 +++++++-------- arch/arm64/mm/kasan_init.c | 6 +-- arch/arm64/mm/mmu.c | 14 +++--- arch/arm64/mm/pageattr.c | 6 +-- arch/arm64/mm/trans_pgd.c | 6 +-- 11 files changed, 93 insertions(+), 86 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 9a2df85eb493..7336d40a893a 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -93,7 +93,8 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys) __pte(__phys_to_pte_val((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)) #define pte_none(pte) (!pte_val(pte)) -#define pte_clear(mm,addr,ptep) set_pte(ptep, __pte(0)) +#define __pte_clear(mm, addr, ptep) \ + __set_pte(ptep, __pte(0)) #define pte_page(pte) (pfn_to_page(pte_pfn(pte))) /* @@ -137,7 +138,7 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys) * so that we don't erroneously return false for pages that have been * remapped as PROT_NONE but are yet to be flushed from the TLB. * Note that we can't make any assumptions based on the state of the access - * flag, since ptep_clear_flush_young() elides a DSB when invalidating the + * flag, since __ptep_clear_flush_young() elides a DSB when invalidating the * TLB. */ #define pte_accessible(mm, pte) \ @@ -261,7 +262,7 @@ static inline pte_t pte_mkdevmap(pte_t pte) return set_pte_bit(pte, __pgprot(PTE_DEVMAP | PTE_SPECIAL)); } -static inline void set_pte(pte_t *ptep, pte_t pte) +static inline void __set_pte(pte_t *ptep, pte_t pte) { WRITE_ONCE(*ptep, pte); @@ -275,8 +276,7 @@ static inline void set_pte(pte_t *ptep, pte_t pte) } } -#define ptep_get ptep_get -static inline pte_t ptep_get(pte_t *ptep) +static inline pte_t __ptep_get(pte_t *ptep) { return READ_ONCE(*ptep); } @@ -308,7 +308,7 @@ static inline void __check_safe_pte_update(struct mm_struct *mm, pte_t *ptep, if (!IS_ENABLED(CONFIG_DEBUG_VM)) return; - old_pte = ptep_get(ptep); + old_pte = __ptep_get(ptep); if (!pte_valid(old_pte) || !pte_valid(pte)) return; @@ -317,7 +317,7 @@ static inline void __check_safe_pte_update(struct mm_struct *mm, pte_t *ptep, /* * Check for potential race with hardware updates of the pte - * (ptep_set_access_flags safely changes valid ptes without going + * (__ptep_set_access_flags safely changes valid ptes without going * through an invalid entry). */ VM_WARN_ONCE(!pte_young(pte), @@ -363,23 +363,22 @@ static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) return pfn_pte(pte_pfn(pte) + nr, pte_pgprot(pte)); } -static inline void set_ptes(struct mm_struct *mm, - unsigned long __always_unused addr, - pte_t *ptep, pte_t pte, unsigned int nr) +static inline void __set_ptes(struct mm_struct *mm, + unsigned long __always_unused addr, + pte_t *ptep, pte_t pte, unsigned int nr) { page_table_check_ptes_set(mm, ptep, pte, nr); __sync_cache_and_tags(pte, nr); for (;;) { __check_safe_pte_update(mm, ptep, pte); - set_pte(ptep, pte); + __set_pte(ptep, pte); if (--nr == 0) break; ptep++; pte = pte_advance_pfn(pte, 1); } } -#define set_ptes set_ptes /* * Huge pte definitions. @@ -546,7 +545,7 @@ static inline void __set_pte_at(struct mm_struct *mm, { __sync_cache_and_tags(pte, nr); __check_safe_pte_update(mm, ptep, pte); - set_pte(ptep, pte); + __set_pte(ptep, pte); } static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr, @@ -860,8 +859,7 @@ static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot) return pte_pmd(pte_modify(pmd_pte(pmd), newprot)); } -#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS -extern int ptep_set_access_flags(struct vm_area_struct *vma, +extern int __ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep, pte_t entry, int dirty); @@ -871,7 +869,8 @@ static inline int pmdp_set_access_flags(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp, pmd_t entry, int dirty) { - return ptep_set_access_flags(vma, address, (pte_t *)pmdp, pmd_pte(entry), dirty); + return __ptep_set_access_flags(vma, address, (pte_t *)pmdp, + pmd_pte(entry), dirty); } static inline int pud_devmap(pud_t pud) @@ -905,12 +904,13 @@ static inline bool pud_user_accessible_page(pud_t pud) /* * Atomic pte/pmd modifications. */ -#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG -static inline int __ptep_test_and_clear_young(pte_t *ptep) +static inline int __ptep_test_and_clear_young(struct vm_area_struct *vma, + unsigned long address, + pte_t *ptep) { pte_t old_pte, pte; - pte = ptep_get(ptep); + pte = __ptep_get(ptep); do { old_pte = pte; pte = pte_mkold(pte); @@ -921,18 +921,10 @@ static inline int __ptep_test_and_clear_young(pte_t *ptep) return pte_young(pte); } -static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, - unsigned long address, - pte_t *ptep) -{ - return __ptep_test_and_clear_young(ptep); -} - -#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH -static inline int ptep_clear_flush_young(struct vm_area_struct *vma, +static inline int __ptep_clear_flush_young(struct vm_area_struct *vma, unsigned long address, pte_t *ptep) { - int young = ptep_test_and_clear_young(vma, address, ptep); + int young = __ptep_test_and_clear_young(vma, address, ptep); if (young) { /* @@ -955,12 +947,11 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma, unsigned long address, pmd_t *pmdp) { - return ptep_test_and_clear_young(vma, address, (pte_t *)pmdp); + return __ptep_test_and_clear_young(vma, address, (pte_t *)pmdp); } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ -#define __HAVE_ARCH_PTEP_GET_AND_CLEAR -static inline pte_t ptep_get_and_clear(struct mm_struct *mm, +static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, unsigned long address, pte_t *ptep) { pte_t pte = __pte(xchg_relaxed(&pte_val(*ptep), 0)); @@ -984,15 +975,15 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ /* - * ptep_set_wrprotect - mark read-only while trasferring potential hardware + * __ptep_set_wrprotect - mark read-only while trasferring potential hardware * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit. */ -#define __HAVE_ARCH_PTEP_SET_WRPROTECT -static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) +static inline void __ptep_set_wrprotect(struct mm_struct *mm, + unsigned long address, pte_t *ptep) { pte_t old_pte, pte; - pte = ptep_get(ptep); + pte = __ptep_get(ptep); do { old_pte = pte; pte = pte_wrprotect(pte); @@ -1006,7 +997,7 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addres static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long address, pmd_t *pmdp) { - ptep_set_wrprotect(mm, address, (pte_t *)pmdp); + __ptep_set_wrprotect(mm, address, (pte_t *)pmdp); } #define pmdp_establish pmdp_establish @@ -1084,7 +1075,7 @@ static inline void arch_swap_restore(swp_entry_t entry, struct folio *folio) #endif /* CONFIG_ARM64_MTE */ /* - * On AArch64, the cache coherency is handled via the set_ptes() function. + * On AArch64, the cache coherency is handled via the __set_ptes() function. */ static inline void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, @@ -1136,6 +1127,22 @@ extern pte_t ptep_modify_prot_start(struct vm_area_struct *vma, extern void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t old_pte, pte_t new_pte); + +#define ptep_get __ptep_get +#define set_pte __set_pte +#define set_ptes __set_ptes +#define pte_clear __pte_clear +#define __HAVE_ARCH_PTEP_GET_AND_CLEAR +#define ptep_get_and_clear __ptep_get_and_clear +#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG +#define ptep_test_and_clear_young __ptep_test_and_clear_young +#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH +#define ptep_clear_flush_young __ptep_clear_flush_young +#define __HAVE_ARCH_PTEP_SET_WRPROTECT +#define ptep_set_wrprotect __ptep_set_wrprotect +#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS +#define ptep_set_access_flags __ptep_set_access_flags + #endif /* !__ASSEMBLY__ */ #endif /* __ASM_PGTABLE_H */ diff --git a/arch/arm64/kernel/efi.c b/arch/arm64/kernel/efi.c index d0e08e93b246..9afcc690fe73 100644 --- a/arch/arm64/kernel/efi.c +++ b/arch/arm64/kernel/efi.c @@ -103,7 +103,7 @@ static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data) { struct set_perm_data *spd = data; const efi_memory_desc_t *md = spd->md; - pte_t pte = ptep_get(ptep); + pte_t pte = __ptep_get(ptep); if (md->attribute & EFI_MEMORY_RO) pte = set_pte_bit(pte, __pgprot(PTE_RDONLY)); @@ -111,7 +111,7 @@ static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data) pte = set_pte_bit(pte, __pgprot(PTE_PXN)); else if (system_supports_bti_kernel() && spd->has_bti) pte = set_pte_bit(pte, __pgprot(PTE_GP)); - set_pte(ptep, pte); + __set_pte(ptep, pte); return 0; } diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 59bfe2e96f8f..dcdcccd40891 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -67,7 +67,7 @@ int memcmp_pages(struct page *page1, struct page *page2) /* * If the page content is identical but at least one of the pages is * tagged, return non-zero to avoid KSM merging. If only one of the - * pages is tagged, set_ptes() may zero or change the tags of the + * pages is tagged, __set_ptes() may zero or change the tags of the * other page via mte_sync_tags(). */ if (page_mte_tagged(page1) || page_mte_tagged(page2)) diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index 6e0df623c8e9..629145fd3161 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -1072,7 +1072,7 @@ int kvm_vm_ioctl_mte_copy_tags(struct kvm *kvm, } else { /* * Only locking to serialise with a concurrent - * set_ptes() in the VMM but still overriding the + * __set_ptes() in the VMM but still overriding the * tags, hence ignoring the return value. */ try_page_mte_tagging(page); diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 3235e23309ec..9a1c66183d16 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -191,7 +191,7 @@ static void show_pte(unsigned long addr) if (!ptep) break; - pte = ptep_get(ptep); + pte = __ptep_get(ptep); pr_cont(", pte=%016llx", pte_val(pte)); pte_unmap(ptep); } while(0); @@ -205,16 +205,16 @@ static void show_pte(unsigned long addr) * * It needs to cope with hardware update of the accessed/dirty state by other * agents in the system and can safely skip the __sync_icache_dcache() call as, - * like set_ptes(), the PTE is never changed from no-exec to exec here. + * like __set_ptes(), the PTE is never changed from no-exec to exec here. * * Returns whether or not the PTE actually changed. */ -int ptep_set_access_flags(struct vm_area_struct *vma, - unsigned long address, pte_t *ptep, - pte_t entry, int dirty) +int __ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long address, pte_t *ptep, + pte_t entry, int dirty) { pteval_t old_pteval, pteval; - pte_t pte = ptep_get(ptep); + pte_t pte = __ptep_get(ptep); if (pte_same(pte, entry)) return 0; diff --git a/arch/arm64/mm/fixmap.c b/arch/arm64/mm/fixmap.c index c0a3301203bd..bfc02568805a 100644 --- a/arch/arm64/mm/fixmap.c +++ b/arch/arm64/mm/fixmap.c @@ -121,9 +121,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep = fixmap_pte(addr); if (pgprot_val(flags)) { - set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags)); + __set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags)); } else { - pte_clear(&init_mm, addr, ptep); + __pte_clear(&init_mm, addr, ptep); flush_tlb_kernel_range(addr, addr+PAGE_SIZE); } } diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 48e8b429879d..0f0e10bb0a95 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -145,14 +145,14 @@ pte_t huge_ptep_get(pte_t *ptep) { int ncontig, i; size_t pgsize; - pte_t orig_pte = ptep_get(ptep); + pte_t orig_pte = __ptep_get(ptep); if (!pte_present(orig_pte) || !pte_cont(orig_pte)) return orig_pte; ncontig = num_contig_ptes(page_size(pte_page(orig_pte)), &pgsize); for (i = 0; i < ncontig; i++, ptep++) { - pte_t pte = ptep_get(ptep); + pte_t pte = __ptep_get(ptep); if (pte_dirty(pte)) orig_pte = pte_mkdirty(orig_pte); @@ -177,11 +177,11 @@ static pte_t get_clear_contig(struct mm_struct *mm, unsigned long pgsize, unsigned long ncontig) { - pte_t orig_pte = ptep_get(ptep); + pte_t orig_pte = __ptep_get(ptep); unsigned long i; for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) { - pte_t pte = ptep_get_and_clear(mm, addr, ptep); + pte_t pte = __ptep_get_and_clear(mm, addr, ptep); /* * If HW_AFDBM is enabled, then the HW could turn on @@ -229,7 +229,7 @@ static void clear_flush(struct mm_struct *mm, unsigned long i, saddr = addr; for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) - ptep_get_and_clear(mm, addr, ptep); + __ptep_get_and_clear(mm, addr, ptep); flush_tlb_range(&vma, saddr, addr); } @@ -247,12 +247,12 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, if (!pte_present(pte)) { for (i = 0; i < ncontig; i++, ptep++, addr += pgsize) - set_ptes(mm, addr, ptep, pte, 1); + __set_ptes(mm, addr, ptep, pte, 1); return; } if (!pte_cont(pte)) { - set_ptes(mm, addr, ptep, pte, 1); + __set_ptes(mm, addr, ptep, pte, 1); return; } @@ -263,7 +263,7 @@ void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, clear_flush(mm, addr, ptep, pgsize, ncontig); for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); + __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); } pte_t *huge_pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma, @@ -393,7 +393,7 @@ void huge_pte_clear(struct mm_struct *mm, unsigned long addr, ncontig = num_contig_ptes(sz, &pgsize); for (i = 0; i < ncontig; i++, addr += pgsize, ptep++) - pte_clear(mm, addr, ptep); + __pte_clear(mm, addr, ptep); } pte_t huge_ptep_get_and_clear(struct mm_struct *mm, @@ -401,10 +401,10 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, { int ncontig; size_t pgsize; - pte_t orig_pte = ptep_get(ptep); + pte_t orig_pte = __ptep_get(ptep); if (!pte_cont(orig_pte)) - return ptep_get_and_clear(mm, addr, ptep); + return __ptep_get_and_clear(mm, addr, ptep); ncontig = find_num_contig(mm, addr, ptep, &pgsize); @@ -424,11 +424,11 @@ static int __cont_access_flags_changed(pte_t *ptep, pte_t pte, int ncontig) { int i; - if (pte_write(pte) != pte_write(ptep_get(ptep))) + if (pte_write(pte) != pte_write(__ptep_get(ptep))) return 1; for (i = 0; i < ncontig; i++) { - pte_t orig_pte = ptep_get(ptep + i); + pte_t orig_pte = __ptep_get(ptep + i); if (pte_dirty(pte) != pte_dirty(orig_pte)) return 1; @@ -452,7 +452,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, pte_t orig_pte; if (!pte_cont(pte)) - return ptep_set_access_flags(vma, addr, ptep, pte, dirty); + return __ptep_set_access_flags(vma, addr, ptep, pte, dirty); ncontig = find_num_contig(mm, addr, ptep, &pgsize); dpfn = pgsize >> PAGE_SHIFT; @@ -471,7 +471,7 @@ int huge_ptep_set_access_flags(struct vm_area_struct *vma, hugeprot = pte_pgprot(pte); for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); + __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); return 1; } @@ -485,8 +485,8 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, size_t pgsize; pte_t pte; - if (!pte_cont(ptep_get(ptep))) { - ptep_set_wrprotect(mm, addr, ptep); + if (!pte_cont(__ptep_get(ptep))) { + __ptep_set_wrprotect(mm, addr, ptep); return; } @@ -500,7 +500,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pfn = pte_pfn(pte); for (i = 0; i < ncontig; i++, ptep++, addr += pgsize, pfn += dpfn) - set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); + __set_ptes(mm, addr, ptep, pfn_pte(pfn, hugeprot), 1); } pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, @@ -510,7 +510,7 @@ pte_t huge_ptep_clear_flush(struct vm_area_struct *vma, size_t pgsize; int ncontig; - if (!pte_cont(ptep_get(ptep))) + if (!pte_cont(__ptep_get(ptep))) return ptep_clear_flush(vma, addr, ptep); ncontig = find_num_contig(mm, addr, ptep, &pgsize); @@ -543,7 +543,7 @@ pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma, unsigned long addr * when the permission changes from executable to non-executable * in cases where cpu is affected with errata #2645198. */ - if (pte_user_exec(ptep_get(ptep))) + if (pte_user_exec(__ptep_get(ptep))) return huge_ptep_clear_flush(vma, addr, ptep); } return huge_ptep_get_and_clear(vma->vm_mm, addr, ptep); diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index c2a9f4f6c7dd..9ee16cfce587 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -112,8 +112,8 @@ static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr, if (!early) memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE); next = addr + PAGE_SIZE; - set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL)); - } while (ptep++, addr = next, addr != end && pte_none(ptep_get(ptep))); + __set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL)); + } while (ptep++, addr = next, addr != end && pte_none(__ptep_get(ptep))); } static void __init kasan_pmd_populate(pud_t *pudp, unsigned long addr, @@ -271,7 +271,7 @@ static void __init kasan_init_shadow(void) * so we should make sure that it maps the zero page read-only. */ for (i = 0; i < PTRS_PER_PTE; i++) - set_pte(&kasan_early_shadow_pte[i], + __set_pte(&kasan_early_shadow_pte[i], pfn_pte(sym_to_pfn(kasan_early_shadow_page), PAGE_KERNEL_RO)); diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 343629a17042..6208c7541f87 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -173,16 +173,16 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, ptep = pte_set_fixmap_offset(pmdp, addr); do { - pte_t old_pte = ptep_get(ptep); + pte_t old_pte = __ptep_get(ptep); - set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot)); + __set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot)); /* * After the PTE entry has been populated once, we * only allow updates to the permission attributes. */ BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), - pte_val(ptep_get(ptep)))); + pte_val(__ptep_get(ptep)))); phys += PAGE_SIZE; } while (ptep++, addr += PAGE_SIZE, addr != end); @@ -852,12 +852,12 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, do { ptep = pte_offset_kernel(pmdp, addr); - pte = ptep_get(ptep); + pte = __ptep_get(ptep); if (pte_none(pte)) continue; WARN_ON(!pte_present(pte)); - pte_clear(&init_mm, addr, ptep); + __pte_clear(&init_mm, addr, ptep); flush_tlb_kernel_range(addr, addr + PAGE_SIZE); if (free_mapped) free_hotplug_page_range(pte_page(pte), @@ -985,7 +985,7 @@ static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, do { ptep = pte_offset_kernel(pmdp, addr); - pte = ptep_get(ptep); + pte = __ptep_get(ptep); /* * This is just a sanity check here which verifies that @@ -1004,7 +1004,7 @@ static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr, */ ptep = pte_offset_kernel(pmdp, 0UL); for (i = 0; i < PTRS_PER_PTE; i++) { - if (!pte_none(ptep_get(&ptep[i]))) + if (!pte_none(__ptep_get(&ptep[i]))) return; } diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c index 73a5e8f82586..0c4e3ecf989d 100644 --- a/arch/arm64/mm/pageattr.c +++ b/arch/arm64/mm/pageattr.c @@ -36,12 +36,12 @@ bool can_set_direct_map(void) static int change_page_range(pte_t *ptep, unsigned long addr, void *data) { struct page_change_data *cdata = data; - pte_t pte = ptep_get(ptep); + pte_t pte = __ptep_get(ptep); pte = clear_pte_bit(pte, cdata->clear_mask); pte = set_pte_bit(pte, cdata->set_mask); - set_pte(ptep, pte); + __set_pte(ptep, pte); return 0; } @@ -245,5 +245,5 @@ bool kernel_page_present(struct page *page) return true; ptep = pte_offset_kernel(pmdp, addr); - return pte_valid(ptep_get(ptep)); + return pte_valid(__ptep_get(ptep)); } diff --git a/arch/arm64/mm/trans_pgd.c b/arch/arm64/mm/trans_pgd.c index f71ab4704cce..5139a28130c0 100644 --- a/arch/arm64/mm/trans_pgd.c +++ b/arch/arm64/mm/trans_pgd.c @@ -33,7 +33,7 @@ static void *trans_alloc(struct trans_pgd_info *info) static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr) { - pte_t pte = ptep_get(src_ptep); + pte_t pte = __ptep_get(src_ptep); if (pte_valid(pte)) { /* @@ -41,7 +41,7 @@ static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr) * read only (code, rodata). Clear the RDONLY bit from * the temporary mappings we use during restore. */ - set_pte(dst_ptep, pte_mkwrite_novma(pte)); + __set_pte(dst_ptep, pte_mkwrite_novma(pte)); } else if ((debug_pagealloc_enabled() || is_kfence_address((void *)addr)) && !pte_none(pte)) { /* @@ -55,7 +55,7 @@ static void _copy_pte(pte_t *dst_ptep, pte_t *src_ptep, unsigned long addr) */ BUG_ON(!pfn_valid(pte_pfn(pte))); - set_pte(dst_ptep, pte_mkpresent(pte_mkwrite_novma(pte))); + __set_pte(dst_ptep, pte_mkpresent(pte_mkwrite_novma(pte))); } } From patchwork Thu Feb 15 10:31:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557861 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 BBEBDC48BF0 for ; Thu, 15 Feb 2024 10:33:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 418D88D001D; Thu, 15 Feb 2024 05:33:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A0408D000E; Thu, 15 Feb 2024 05:33:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F1F08D001D; Thu, 15 Feb 2024 05:33:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 0B0048D000E for ; Thu, 15 Feb 2024 05:33:00 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D47E0C080F for ; Thu, 15 Feb 2024 10:32:59 +0000 (UTC) X-FDA: 81793675278.08.777F2D7 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf15.hostedemail.com (Postfix) with ESMTP id 50894A000F for ; Thu, 15 Feb 2024 10:32:58 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; spf=pass (imf15.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=1707993178; 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=4J1dTrrIzJ+DpallLluT2b35txLH0YV5OvrN+DtNofk=; b=122e5Aa2JWLqtbrxft72U/zdLQsfLxCpVJA3NwAc6cjoe4Vp9mIpXCbZ8B4E2jXk+vpxs7 1SiZeS5w2bjmpqx/q3whBSNFtQ690ZgekeDZzx4aq8gSnvYAfYFsMBwim1Tq8AhdwDxtmM XqmNYJQqzJOzkEClVPoiuikITgrnXaE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707993178; a=rsa-sha256; cv=none; b=ut0YQlJtOCwJHfnUqevHdTCjgtb+icdalRlgpfhYG51Ddf30T5cexicN4OSdGEs+j6Ywq9 GkVvwP+7IQsbusqMqRVWePuZi9ZX07gCMIw4vZzuzSjZbn5wk18FVXuxDBPv43gY804TTq i5I5HrBGM3jE6nrbv1h6qc9SqG8BOfs= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; spf=pass (imf15.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 79677DA7; Thu, 15 Feb 2024 02:33:38 -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 6E5073F7B4; Thu, 15 Feb 2024 02:32:54 -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 11/18] arm64/mm: Split __flush_tlb_range() to elide trailing DSB Date: Thu, 15 Feb 2024 10:31:58 +0000 Message-Id: <20240215103205.2607016-12-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: 50894A000F X-Rspam-User: X-Stat-Signature: jo8whq9azk4dsbgdhia3xhf93wg4hw6k X-Rspamd-Server: rspam03 X-HE-Tag: 1707993178-559821 X-HE-Meta: U2FsdGVkX19uGRgvXN5lzZuQed1nDYgMdmJXUg4tL/GdUDVS+DBAiKrnoi3/53DMSQgrVfBB+cIY7pQaGMN82z0CiAnfmSJ05kPYP38MpPUIssA3/gMFKOqFn510MWVzU4tPNFe/vpyBrlxk4Sm+/VCQnnusQOczlKVhIXc4uwcij6alhakyCJP2+WXkl28s/hYIFBD5KMroNP6MnCwjOJNFESqEogqYdWEIgC4TrkNVpXOw0vwa+CmdYepAoB7QXzPar8Hqu4yrLgdo5d2otpWQZHqVJx8TlQJyW6KLsKB4IuHZhxqo+Qoz7vHqQ7qZ/zf6hsu7fK6huvI0TM96g3cYaHpHjNwUclwtkE3goBMw1M7EgGuHRwxyrAPsNVLeVQvthrVpyaGoaOJNukfElAQqgmCyLd1AQBs0rX5gy+Q64pIiRIj9FLHO7jNaCMXO7/RBHjKCcTRp+nGZZ2ireibfqyNVniN7xLQmsTcWU1oKdGGbhVCHcFkFG236CWVFZjgDfFHGfwOm2l4IETqZDOy/807V1Y4Xh5vHKTEVFIR500blVWrELY7RApH5pOP+4Fr/Q6H65H/QqOxNDDrtCOD/i94Z33Gom7IDDHSbU2Z2jW8B7BcmxWLZZ1EqrpgybFQgBQDrzQsk209dPjSdQHdJ5TcwG7BTJA7oqBiOPfCaY05vljYkJp7LuRw+iD10YZzM5Qfckmgt818P9N16nHD+xoSNz6AK+G4mWpmEWZrQ/TQ2e5OU7rLiJyyZKsd8LTNF1Tzx+XJ4qyZL3z04I6wkEu5MPY70N/bTPnMvaqhbc6Xqa0Z4g8RHtmY5GbICmzB3zEfdztTGBA3dMCPG7P21TQzL6UCMI4Bc8Hvt+wEjgMO/5U4gdtPdw3kckBxgb7wcW+WPSNNkB6z0NV828ogoop5tjXHANZ/mEbEHF0Out0D+xo2vCUpLJkP5PTnyjb7j2jGBCPy4be3lH61 fERbHdom /H1qs+JGEP2qEKDdKMXRGgRVRgkfKn5Xslyxw/NFC81bGW1zA+qjnmCuNv57YUX1G2rZdidISoHwe4ppFmCapD4uj3ekIPdFE6EdwxruWgA1KT/px+CFXift4i+FXllQBu07hB43UDv8RRRTQluPievS1i9F+GZL/u7iwXggiAk2DxCC/8yPLE9fioKyquFueYjiVDNQW4InJ4F+r6TYgT+ZDJ4m0usf9arfw8VUuoHXuEv3fV7Xx5xpjdjGf3wxzoN0oBtoP6ossVUK1iZ7gg9wrk+f3FbQYHOjQhp8gUqLofsaXfqSPfUbwkMYDyuF3xIvCDggv6rrAAW0= 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: Split __flush_tlb_range() into __flush_tlb_range_nosync() + __flush_tlb_range(), in the same way as the existing flush_tlb_page() arrangement. This allows calling __flush_tlb_range_nosync() to elide the trailing DSB. Forthcoming "contpte" code will take advantage of this when clearing the young bit from a contiguous range of ptes. Ordering between dsb and mmu_notifier_arch_invalidate_secondary_tlbs() has changed, but now aligns with the ordering of __flush_tlb_page(). It has been discussed that __flush_tlb_page() may be wrong though. Regardless, both will be resolved separately if needed. Reviewed-by: David Hildenbrand Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/tlbflush.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/tlbflush.h b/arch/arm64/include/asm/tlbflush.h index 1deb5d789c2e..3b0e8248e1a4 100644 --- a/arch/arm64/include/asm/tlbflush.h +++ b/arch/arm64/include/asm/tlbflush.h @@ -422,7 +422,7 @@ do { \ #define __flush_s2_tlb_range_op(op, start, pages, stride, tlb_level) \ __flush_tlb_range_op(op, start, pages, stride, 0, tlb_level, false, kvm_lpa2_is_enabled()); -static inline void __flush_tlb_range(struct vm_area_struct *vma, +static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma, unsigned long start, unsigned long end, unsigned long stride, bool last_level, int tlb_level) @@ -456,10 +456,19 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, __flush_tlb_range_op(vae1is, start, pages, stride, asid, tlb_level, true, lpa2_is_enabled()); - dsb(ish); mmu_notifier_arch_invalidate_secondary_tlbs(vma->vm_mm, start, end); } +static inline void __flush_tlb_range(struct vm_area_struct *vma, + unsigned long start, unsigned long end, + unsigned long stride, bool last_level, + int tlb_level) +{ + __flush_tlb_range_nosync(vma, start, end, stride, + last_level, tlb_level); + dsb(ish); +} + static inline void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end) { From patchwork Thu Feb 15 10:31:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557862 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 82EF4C48BF1 for ; Thu, 15 Feb 2024 10:33:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA3788D001E; Thu, 15 Feb 2024 05:33:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C2EE28D000E; Thu, 15 Feb 2024 05:33:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A319E8D001E; Thu, 15 Feb 2024 05:33:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8E1B78D000E for ; Thu, 15 Feb 2024 05:33:03 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 617B5120F62 for ; Thu, 15 Feb 2024 10:33:03 +0000 (UTC) X-FDA: 81793675446.15.A36E7B4 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf25.hostedemail.com (Postfix) with ESMTP id BBD72A0012 for ; Thu, 15 Feb 2024 10:33:01 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.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=1707993181; 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=ZETcGOj0NxuKbgywJQOZ6Pcbnw3jIAmPBbmCU1QaI8w=; b=fXB+gytlbPZT8jVznojBdK1QzhgKoJfwUyUXU1cYLnZA5IQvOBjq+LDy7OtXDZu6vCg1Vd YFy9j9hHqhV+ZS4leHxK9sTUiS6mgV6fwBXgcgrYT8JKX+Esz8OgbDEllQiJeA2rK7b9Bo AWtn8jLaChpI1ni2gsUnPoio4EGujBg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707993181; a=rsa-sha256; cv=none; b=Ck5YBDzqJ8kTOXcKbRT65B5Xj81XMsg55vNIuRWq+pceI5WjE5C2GyP5Lh6kQtcsDcTh6f dJnVd1EpNv13HEziVIut5NFqprXWkhCdtMF7Gl2kQdOOZDQFa0nohZQqfRh9jis4tVhzKY rRHHycBzP8VpUTgsSZTY51NcDPQBNKM= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.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 DD6651FB; Thu, 15 Feb 2024 02:33:41 -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 D373C3F7B4; Thu, 15 Feb 2024 02:32:57 -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 12/18] arm64/mm: Wire up PTE_CONT for user mappings Date: Thu, 15 Feb 2024 10:31:59 +0000 Message-Id: <20240215103205.2607016-13-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-Stat-Signature: 1drzp84u8geyyz4j35ktxsd1x4tptxsz X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: BBD72A0012 X-Rspam-User: X-HE-Tag: 1707993181-376575 X-HE-Meta: U2FsdGVkX1/P7IIUlneoeOVFVx9w1EL1xrienZGfsFRoI3vcFlQk5abW+JvGFz0jLLjKquyX492BCoyiAiHvlZaeNUU3LT4GOcuNYVy8eFsOugW5Mm5lK//JW/rq62ikNdC1Ng+J+vnsOyDLqzaMpU2ivfD23vmkhuE/MGwrdfxjs772frhgwkzy9BTh4XEDfxeDF/4VluznBd3uOwWh4p92OXDHszOxyPTgzYysDqN+bQF3qVwIfc8HHUdzWveFw7aihaNFQqea+S/MXkGRoSO+VJ3d6rbOSJKC2Kxhqt4SVfMQdaKEZ7Rs7qRg8zhkCidP/OjJRoMx83Hq5rasxVqBdhnlpakwfOCN3wIn8TlXZfnnG3ZSxDtDrWUZD9ZzcttOX7SiNNmonzoaR2UcM5MLRpIYZjGBimJKwSpHParEMvlNLcbzW3AC+ga7tZy9NZGxWDBgBvDjXz3/Xhk0fWXll42DbEboNJACuz4fUJNXo7K+rm+mDmkvYAPKSvvBkFBiNkFLLzKaO4vnBIg2VoWVniYmoE0eUhKDS0sfvA+WqXoBpbfX5mKi6rHJ5SQg6AE3YYWmR7mg0FFGmUmF1mPGDDGh/w8HHDL+vNgYWvoW+lVcd67D9QaiUEO6fthXIfHkS/9Xc6kgxFBoQgr4LFdASFHORLjnQwDxX4Do5DdfTl7f/LXLWXy5NLqhWXaLCUPtpHqxcjs/nqQbzDZrevPB2q0jj6a7hdbwlz4zrZD6567LL/c3LmUvRRnVL8Psp27G9+MK0KgJ7bvtVz8vy2ZYc+Ryz6WemXznoU2emfFj0KqCav0wqgUSpv2dSeEga1UvHb3M5Zixt2q03joc5ZVoixTQBfO89hY5G9jPdTdKCuZRErrSYfOxzkA8/mljCeEokYIuJkB0UmTgM47hNLYEmMs9Gta7WfmiqG2rpHJ8wTkYLxvPfX7x99tM1zI6DozmIY4k7RVP8lXYSaQ +Y/CqXf5 /QfTGXpOKTxnzpj8yZP6XOSDZEPQk8kjq5JWZN01ccymY/Yzje4IiJRkiJAF58myr5AS9Yt4fQ+cHwl+fr9adRcBfmAL5/gYc1KwmNDQCi+klwU9+DkTkbxfVC+nAe07O+9pehfzRTSy+Qi+7YNJDeBV+3t/fUixq1TxP5C0zADS+/oNAY3N1i7iTlSaGi+VFXObdNLJZ3RPR/afxsxsVwX1HQd7hSd9rIZnauhfPuDY7Yp+UL32dnC0PFCvLL44WpfbW0C8SCQOy0dLpQ97D1kEhK5YFSjmQIypz4D5iy4zcPQK8ylZro4eMIT1qfFaxuk8Cd5J+GFDZ6sfQwRQmE/OX0VnCnXDwJT/rVCD3eEIHmyU= 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 ptep API sufficiently refactored, we can now introduce a new "contpte" API layer, which transparently manages the PTE_CONT bit for user mappings. In this initial implementation, only suitable batches of PTEs, set via set_ptes(), are mapped with the PTE_CONT bit. Any subsequent modification of individual PTEs will cause an "unfold" operation to repaint the contpte block as individual PTEs before performing the requested operation. While, a modification of a single PTE could cause the block of PTEs to which it belongs to become eligible for "folding" into a contpte entry, "folding" is not performed in this initial implementation due to the costs of checking the requirements are met. Due to this, contpte mappings will degrade back to normal pte mappings over time if/when protections are changed. This will be solved in a future patch. Since a contpte block only has a single access and dirty bit, the semantic here changes slightly; when getting a pte (e.g. ptep_get()) that is part of a contpte mapping, the access and dirty information are pulled from the block (so all ptes in the block return the same access/dirty info). When changing the access/dirty info on a pte (e.g. ptep_set_access_flags()) that is part of a contpte mapping, this change will affect the whole contpte block. This is works fine in practice since we guarantee that only a single folio is mapped by a contpte block, and the core-mm tracks access/dirty information per folio. In order for the public functions, which used to be pure inline, to continue to be callable by modules, export all the contpte_* symbols that are now called by those public inline functions. The feature is enabled/disabled with the ARM64_CONTPTE Kconfig parameter at build time. It defaults to enabled as long as its dependency, TRANSPARENT_HUGEPAGE is also enabled. The core-mm depends upon TRANSPARENT_HUGEPAGE to be able to allocate large folios, so if its not enabled, then there is no chance of meeting the physical contiguity requirement for contpte mappings. Acked-by: Ard Biesheuvel Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Reviewed-by: Catalin Marinas --- arch/arm64/Kconfig | 9 + arch/arm64/include/asm/pgtable.h | 167 ++++++++++++++++++ arch/arm64/mm/Makefile | 1 + arch/arm64/mm/contpte.c | 285 +++++++++++++++++++++++++++++++ include/linux/efi.h | 5 + 5 files changed, 467 insertions(+) create mode 100644 arch/arm64/mm/contpte.c diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index e8275a40afbd..5a7ac1f37bdc 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2229,6 +2229,15 @@ config UNWIND_PATCH_PAC_INTO_SCS select UNWIND_TABLES select DYNAMIC_SCS +config ARM64_CONTPTE + bool "Contiguous PTE mappings for user memory" if EXPERT + depends on TRANSPARENT_HUGEPAGE + default y + help + When enabled, user mappings are configured using the PTE contiguous + bit, for any mappings that meet the size and alignment requirements. + This reduces TLB pressure and improves performance. + endmenu # "Kernel Features" menu "Boot options" diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 7336d40a893a..831099cfc96b 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -133,6 +133,10 @@ static inline pteval_t __phys_to_pte_val(phys_addr_t phys) */ #define pte_valid_not_user(pte) \ ((pte_val(pte) & (PTE_VALID | PTE_USER | PTE_UXN)) == (PTE_VALID | PTE_UXN)) +/* + * Returns true if the pte is valid and has the contiguous bit set. + */ +#define pte_valid_cont(pte) (pte_valid(pte) && pte_cont(pte)) /* * Could the pte be present in the TLB? We must check mm_tlb_flush_pending * so that we don't erroneously return false for pages that have been @@ -1128,6 +1132,167 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t old_pte, pte_t new_pte); +#ifdef CONFIG_ARM64_CONTPTE + +/* + * The contpte APIs are used to transparently manage the contiguous bit in ptes + * where it is possible and makes sense to do so. The PTE_CONT bit is considered + * a private implementation detail of the public ptep API (see below). + */ +extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte); +extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte); +extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep); +extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte, unsigned int nr); +extern int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep); +extern int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep); +extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + pte_t entry, int dirty); + +static inline void contpte_try_unfold(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ + if (unlikely(pte_valid_cont(pte))) + __contpte_try_unfold(mm, addr, ptep, pte); +} + +/* + * The below functions constitute the public API that arm64 presents to the + * core-mm to manipulate PTE entries within their page tables (or at least this + * is the subset of the API that arm64 needs to implement). These public + * versions will automatically and transparently apply the contiguous bit where + * it makes sense to do so. Therefore any users that are contig-aware (e.g. + * hugetlb, kernel mapper) should NOT use these APIs, but instead use the + * private versions, which are prefixed with double underscore. All of these + * APIs except for ptep_get_lockless() are expected to be called with the PTL + * held. Although the contiguous bit is considered private to the + * implementation, it is deliberately allowed to leak through the getters (e.g. + * ptep_get()), back to core code. This is required so that pte_leaf_size() can + * provide an accurate size for perf_get_pgtable_size(). But this leakage means + * its possible a pte will be passed to a setter with the contiguous bit set, so + * we explicitly clear the contiguous bit in those cases to prevent accidentally + * setting it in the pgtable. + */ + +#define ptep_get ptep_get +static inline pte_t ptep_get(pte_t *ptep) +{ + pte_t pte = __ptep_get(ptep); + + if (likely(!pte_valid_cont(pte))) + return pte; + + return contpte_ptep_get(ptep, pte); +} + +#define ptep_get_lockless ptep_get_lockless +static inline pte_t ptep_get_lockless(pte_t *ptep) +{ + pte_t pte = __ptep_get(ptep); + + if (likely(!pte_valid_cont(pte))) + return pte; + + return contpte_ptep_get_lockless(ptep); +} + +static inline void set_pte(pte_t *ptep, pte_t pte) +{ + /* + * We don't have the mm or vaddr so cannot unfold contig entries (since + * it requires tlb maintenance). set_pte() is not used in core code, so + * this should never even be called. Regardless do our best to service + * any call and emit a warning if there is any attempt to set a pte on + * top of an existing contig range. + */ + pte_t orig_pte = __ptep_get(ptep); + + WARN_ON_ONCE(pte_valid_cont(orig_pte)); + __set_pte(ptep, pte_mknoncont(pte)); +} + +#define set_ptes set_ptes +static inline void set_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte, unsigned int nr) +{ + pte = pte_mknoncont(pte); + + if (likely(nr == 1)) { + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + __set_ptes(mm, addr, ptep, pte, 1); + } else { + contpte_set_ptes(mm, addr, ptep, pte, nr); + } +} + +static inline void pte_clear(struct mm_struct *mm, + unsigned long addr, pte_t *ptep) +{ + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + __pte_clear(mm, addr, ptep); +} + +#define __HAVE_ARCH_PTEP_GET_AND_CLEAR +static inline pte_t ptep_get_and_clear(struct mm_struct *mm, + unsigned long addr, pte_t *ptep) +{ + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + return __ptep_get_and_clear(mm, addr, ptep); +} + +#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG +static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep) +{ + pte_t orig_pte = __ptep_get(ptep); + + if (likely(!pte_valid_cont(orig_pte))) + return __ptep_test_and_clear_young(vma, addr, ptep); + + return contpte_ptep_test_and_clear_young(vma, addr, ptep); +} + +#define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH +static inline int ptep_clear_flush_young(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep) +{ + pte_t orig_pte = __ptep_get(ptep); + + if (likely(!pte_valid_cont(orig_pte))) + return __ptep_clear_flush_young(vma, addr, ptep); + + return contpte_ptep_clear_flush_young(vma, addr, ptep); +} + +#define __HAVE_ARCH_PTEP_SET_WRPROTECT +static inline void ptep_set_wrprotect(struct mm_struct *mm, + unsigned long addr, pte_t *ptep) +{ + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + __ptep_set_wrprotect(mm, addr, ptep); +} + +#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS +static inline int ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + pte_t entry, int dirty) +{ + pte_t orig_pte = __ptep_get(ptep); + + entry = pte_mknoncont(entry); + + if (likely(!pte_valid_cont(orig_pte))) + return __ptep_set_access_flags(vma, addr, ptep, entry, dirty); + + return contpte_ptep_set_access_flags(vma, addr, ptep, entry, dirty); +} + +#else /* CONFIG_ARM64_CONTPTE */ + #define ptep_get __ptep_get #define set_pte __set_pte #define set_ptes __set_ptes @@ -1143,6 +1308,8 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS #define ptep_set_access_flags __ptep_set_access_flags +#endif /* CONFIG_ARM64_CONTPTE */ + #endif /* !__ASSEMBLY__ */ #endif /* __ASM_PGTABLE_H */ diff --git a/arch/arm64/mm/Makefile b/arch/arm64/mm/Makefile index dbd1bc95967d..60454256945b 100644 --- a/arch/arm64/mm/Makefile +++ b/arch/arm64/mm/Makefile @@ -3,6 +3,7 @@ obj-y := dma-mapping.o extable.o fault.o init.o \ cache.o copypage.o flush.o \ ioremap.o mmap.o pgd.o mmu.o \ context.o proc.o pageattr.o fixmap.o +obj-$(CONFIG_ARM64_CONTPTE) += contpte.o obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o obj-$(CONFIG_PTDUMP_CORE) += ptdump.o obj-$(CONFIG_PTDUMP_DEBUGFS) += ptdump_debugfs.o diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c new file mode 100644 index 000000000000..6d7f40667fa2 --- /dev/null +++ b/arch/arm64/mm/contpte.c @@ -0,0 +1,285 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 ARM Ltd. + */ + +#include +#include +#include +#include + +static inline bool mm_is_user(struct mm_struct *mm) +{ + /* + * Don't attempt to apply the contig bit to kernel mappings, because + * dynamically adding/removing the contig bit can cause page faults. + * These racing faults are ok for user space, since they get serialized + * on the PTL. But kernel mappings can't tolerate faults. + */ + if (unlikely(mm_is_efi(mm))) + return false; + return mm != &init_mm; +} + +static inline pte_t *contpte_align_down(pte_t *ptep) +{ + return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES); +} + +static void contpte_convert(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ + struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); + unsigned long start_addr; + pte_t *start_ptep; + int i; + + start_ptep = ptep = contpte_align_down(ptep); + start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE); + pte = pfn_pte(ALIGN_DOWN(pte_pfn(pte), CONT_PTES), pte_pgprot(pte)); + + for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE) { + pte_t ptent = __ptep_get_and_clear(mm, addr, ptep); + + if (pte_dirty(ptent)) + pte = pte_mkdirty(pte); + + if (pte_young(ptent)) + pte = pte_mkyoung(pte); + } + + __flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, true, 3); + + __set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES); +} + +void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ + /* + * We have already checked that the ptes are contiguous in + * contpte_try_unfold(), so just check that the mm is user space. + */ + if (!mm_is_user(mm)) + return; + + pte = pte_mknoncont(pte); + contpte_convert(mm, addr, ptep, pte); +} +EXPORT_SYMBOL(__contpte_try_unfold); + +pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte) +{ + /* + * Gather access/dirty bits, which may be populated in any of the ptes + * of the contig range. We are guaranteed to be holding the PTL, so any + * contiguous range cannot be unfolded or otherwise modified under our + * feet. + */ + + pte_t pte; + int i; + + ptep = contpte_align_down(ptep); + + for (i = 0; i < CONT_PTES; i++, ptep++) { + pte = __ptep_get(ptep); + + if (pte_dirty(pte)) + orig_pte = pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte = pte_mkyoung(orig_pte); + } + + return orig_pte; +} +EXPORT_SYMBOL(contpte_ptep_get); + +pte_t contpte_ptep_get_lockless(pte_t *orig_ptep) +{ + /* + * Gather access/dirty bits, which may be populated in any of the ptes + * of the contig range. We may not be holding the PTL, so any contiguous + * range may be unfolded/modified/refolded under our feet. Therefore we + * ensure we read a _consistent_ contpte range by checking that all ptes + * in the range are valid and have CONT_PTE set, that all pfns are + * contiguous and that all pgprots are the same (ignoring access/dirty). + * If we find a pte that is not consistent, then we must be racing with + * an update so start again. If the target pte does not have CONT_PTE + * set then that is considered consistent on its own because it is not + * part of a contpte range. + */ + + pgprot_t orig_prot; + unsigned long pfn; + pte_t orig_pte; + pgprot_t prot; + pte_t *ptep; + pte_t pte; + int i; + +retry: + orig_pte = __ptep_get(orig_ptep); + + if (!pte_valid_cont(orig_pte)) + return orig_pte; + + orig_prot = pte_pgprot(pte_mkold(pte_mkclean(orig_pte))); + ptep = contpte_align_down(orig_ptep); + pfn = pte_pfn(orig_pte) - (orig_ptep - ptep); + + for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) { + pte = __ptep_get(ptep); + prot = pte_pgprot(pte_mkold(pte_mkclean(pte))); + + if (!pte_valid_cont(pte) || + pte_pfn(pte) != pfn || + pgprot_val(prot) != pgprot_val(orig_prot)) + goto retry; + + if (pte_dirty(pte)) + orig_pte = pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte = pte_mkyoung(orig_pte); + } + + return orig_pte; +} +EXPORT_SYMBOL(contpte_ptep_get_lockless); + +void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte, unsigned int nr) +{ + unsigned long next; + unsigned long end; + unsigned long pfn; + pgprot_t prot; + + /* + * The set_ptes() spec guarantees that when nr > 1, the initial state of + * all ptes is not-present. Therefore we never need to unfold or + * otherwise invalidate a range before we set the new ptes. + * contpte_set_ptes() should never be called for nr < 2. + */ + VM_WARN_ON(nr == 1); + + if (!mm_is_user(mm)) + return __set_ptes(mm, addr, ptep, pte, nr); + + end = addr + (nr << PAGE_SHIFT); + pfn = pte_pfn(pte); + prot = pte_pgprot(pte); + + do { + next = pte_cont_addr_end(addr, end); + nr = (next - addr) >> PAGE_SHIFT; + pte = pfn_pte(pfn, prot); + + if (((addr | next | (pfn << PAGE_SHIFT)) & ~CONT_PTE_MASK) == 0) + pte = pte_mkcont(pte); + else + pte = pte_mknoncont(pte); + + __set_ptes(mm, addr, ptep, pte, nr); + + addr = next; + ptep += nr; + pfn += nr; + + } while (addr != end); +} +EXPORT_SYMBOL(contpte_set_ptes); + +int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep) +{ + /* + * ptep_clear_flush_young() technically requires us to clear the access + * flag for a _single_ pte. However, the core-mm code actually tracks + * access/dirty per folio, not per page. And since we only create a + * contig range when the range is covered by a single folio, we can get + * away with clearing young for the whole contig range here, so we avoid + * having to unfold. + */ + + int young = 0; + int i; + + ptep = contpte_align_down(ptep); + addr = ALIGN_DOWN(addr, CONT_PTE_SIZE); + + for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE) + young |= __ptep_test_and_clear_young(vma, addr, ptep); + + return young; +} +EXPORT_SYMBOL(contpte_ptep_test_and_clear_young); + +int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep) +{ + int young; + + young = contpte_ptep_test_and_clear_young(vma, addr, ptep); + + if (young) { + /* + * See comment in __ptep_clear_flush_young(); same rationale for + * eliding the trailing DSB applies here. + */ + addr = ALIGN_DOWN(addr, CONT_PTE_SIZE); + __flush_tlb_range_nosync(vma, addr, addr + CONT_PTE_SIZE, + PAGE_SIZE, true, 3); + } + + return young; +} +EXPORT_SYMBOL(contpte_ptep_clear_flush_young); + +int contpte_ptep_set_access_flags(struct vm_area_struct *vma, + unsigned long addr, pte_t *ptep, + pte_t entry, int dirty) +{ + unsigned long start_addr; + pte_t orig_pte; + int i; + + /* + * Gather the access/dirty bits for the contiguous range. If nothing has + * changed, its a noop. + */ + orig_pte = pte_mknoncont(ptep_get(ptep)); + if (pte_val(orig_pte) == pte_val(entry)) + return 0; + + /* + * We can fix up access/dirty bits without having to unfold the contig + * range. But if the write bit is changing, we must unfold. + */ + if (pte_write(orig_pte) == pte_write(entry)) { + /* + * For HW access management, we technically only need to update + * the flag on a single pte in the range. But for SW access + * management, we need to update all the ptes to prevent extra + * faults. Avoid per-page tlb flush in __ptep_set_access_flags() + * and instead flush the whole range at the end. + */ + ptep = contpte_align_down(ptep); + start_addr = addr = ALIGN_DOWN(addr, CONT_PTE_SIZE); + + for (i = 0; i < CONT_PTES; i++, ptep++, addr += PAGE_SIZE) + __ptep_set_access_flags(vma, addr, ptep, entry, 0); + + if (dirty) + __flush_tlb_range(vma, start_addr, addr, + PAGE_SIZE, true, 3); + } else { + __contpte_try_unfold(vma->vm_mm, addr, ptep, orig_pte); + __ptep_set_access_flags(vma, addr, ptep, entry, dirty); + } + + return 1; +} +EXPORT_SYMBOL(contpte_ptep_set_access_flags); diff --git a/include/linux/efi.h b/include/linux/efi.h index c74f47711f0b..57da15e7429c 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -692,6 +692,11 @@ extern struct efi { extern struct mm_struct efi_mm; +static inline bool mm_is_efi(struct mm_struct *mm) +{ + return IS_ENABLED(CONFIG_EFI) && mm == &efi_mm; +} + static inline int efi_guidcmp (efi_guid_t left, efi_guid_t right) { From patchwork Thu Feb 15 10:32:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557863 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 C4F01C4829E for ; Thu, 15 Feb 2024 10:33:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 528578D001F; Thu, 15 Feb 2024 05:33:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B15B8D000E; Thu, 15 Feb 2024 05:33:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2B74F8D001F; Thu, 15 Feb 2024 05:33:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1178C8D000E for ; Thu, 15 Feb 2024 05:33:08 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DC1BD1A067B for ; Thu, 15 Feb 2024 10:33:07 +0000 (UTC) X-FDA: 81793675614.19.A780688 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf19.hostedemail.com (Postfix) with ESMTP id 3CF6A1A0018 for ; Thu, 15 Feb 2024 10:33:05 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf19.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=1707993185; 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=CrpX24hJ21lNTKd4YCqgFb/8kYDx8R2PLZ109arTQ9Q=; b=aqLBzknrrG3HVKLe3KP/auye+o0tEFVVtmR6jNMAneJN5QbK9NseSJQeFjoBOsx0SPMrnl Jh41Kv3omRaIMub3Yshq0AUNT4VUqNe/em0VTY5ary4P5mCV+sDl8o53iB7GsNKl3sb0ql 6dbl9q/41mwkFDt2WmN7GAcgtEgRens= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf19.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=1707993185; a=rsa-sha256; cv=none; b=olU5GrHZJJktx/AIwlA3scl1dE2YjAuT8Em+qaYKiq+u4cCrlsARpcz0rF+4vHC4Hxq/XF w9/Hzb0PJITgoHaN1SqWAVBdJ15qmQZ2Mb8UiuWJrvBwB7Aluxz1Tcs9Q5Z/29VOy4OGFB u+tHl58gViM7dcsu9lFIUBv6/AKGqAU= 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 4D79D1595; Thu, 15 Feb 2024 02:33:45 -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 44B7C3F7B4; Thu, 15 Feb 2024 02:33:01 -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 13/18] arm64/mm: Implement new wrprotect_ptes() batch API Date: Thu, 15 Feb 2024 10:32:00 +0000 Message-Id: <20240215103205.2607016-14-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-Server: rspam09 X-Rspamd-Queue-Id: 3CF6A1A0018 X-Stat-Signature: w95fieawtomq3uk6totw48yifjajn6ow X-Rspam-User: X-HE-Tag: 1707993185-439266 X-HE-Meta: U2FsdGVkX1/xdbkfuljv9P+P7Ab8rJoFf9uYsOii39BriR7dJNehV+OAsoS0yX68G5vFgbVRu2NCI7zt9Fl+KryDzF2yYlVi7P56PY3ewz3p3WBEZAI6Stm+R87Wjj0IYJw214JyJ/uA7y5dRkp/4MJQPba/ZbKVBleStNfhya9LHwI3xOUiwZANzpUw2aGYuYrQYHcYyOGMV77+whjjMjIP4KyrJhyxASpmRCRSb+Di49gYSiM2DnzM4q58tg6KPTFwW9E+k30vakQ96UI09KJWKhCbRHvThZZdTpBFiJTzpMTQwOKeIPzetLNM9QHjkQrWQvU82PyMEoPiJKB482uxva+9nwqoRts9cncAvzJfhuhyD1blZ7mPBaVl0Z1FgW5Xoh+dSQwPLux+WaKwT+8NSTbm3Vys+QuNsVwk25s1esOkwD+IfKHMEVLGgIP6dq0keMHekBNxhuW/ay8gSnETvBdwEwLvb0QKh3emDrxHY/JtbHwcdvnHq5pIpXjnFv+ecdibK6rOLf9rkpY8gAy5JuwbtOsjC4cxk+Aezf+H5k0Crs/YILm97N3v0RMskPIqsBQMwlmo4v9thB/Ih1oXys2tn19YJIIAhrfYR3Cml6ugkFkW3Em6/H6aO06b3T5F+AxlTcvJIDcMGAKvS08CqsyFM2CmbE0kFr/qD9Ik1JjmEDc9F2qOsGdNHx4+grFOBIuRazNO5Gy2fCCgKDZrKAZkOwaVH28OEz0DB770/qelle7PuTnrzHcqsiu5O02gmUiGicpZq7PdO6MNA8OgtFxW2+RmSVTwqKfeG00Q9I5IocpPJGVsfV0AB28XoQTg6Wigm0WPjwcVPULKKTeMvL5aqaKkmoOmAAVkDUyE5CC5zxtRnbNYWN5Rq2hDATvPMfo5T2x7CqNZXAt8V0aDtCXklC5ijQCR5woP+NufLXbUNygAwHgG/UlOXkGe+Vvd+SMnaDTm/Uiph2O BG6A074m 1L7dzL1hhfK6+dt/TDK0DMEzihMJWJfgmSZ709QyxgYrfnNg0babRD9070I9rgbgep2Stc//bmoq6HAhva2OycZEsfo3peHx0k2OI5d2AX+EImI/9sLjh+T7u82JzkbwuKghW9PPZ7tASg4mks4gpcSFQGwRIL7shMmuVI2WdKlpNrIkTZfleVH2+YdIECX5NsEcly8BDrLGir7Wn2NddTfcpWFGWbXDMJEQBg8PM6A6012m5e8S4CDYWiGk+7h3vI2BxM+6u+fAWcbgONv2h+GwhF5iu/Pj0pnXLOJJVs8rYC3c= 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: Optimize the contpte implementation to fix some of the fork performance regression introduced by the initial contpte commit. Subsequent patches will solve it entirely. During fork(), any private memory in the parent must be write-protected. Previously this was done 1 PTE at a time. But the core-mm supports batched wrprotect via the new wrprotect_ptes() API. So let's implement that API and for fully covered contpte mappings, we no longer need to unfold the contpte. This has 2 benefits: - reduced unfolding, reduces the number of tlbis that must be issued. - The memory remains contpte-mapped ("folded") in the parent, so it continues to benefit from the more efficient use of the TLB after the fork. The optimization to wrprotect a whole contpte block without unfolding is possible thanks to the tightening of the Arm ARM in respect to the definition and behaviour when 'Misprogramming the Contiguous bit'. See section D21194 at https://developer.arm.com/documentation/102105/ja-07/ Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 61 ++++++++++++++++++++++++++------ arch/arm64/mm/contpte.c | 38 ++++++++++++++++++++ 2 files changed, 89 insertions(+), 10 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 831099cfc96b..8643227c318b 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -978,16 +978,12 @@ static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, } #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ -/* - * __ptep_set_wrprotect - mark read-only while trasferring potential hardware - * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit. - */ -static inline void __ptep_set_wrprotect(struct mm_struct *mm, - unsigned long address, pte_t *ptep) +static inline void ___ptep_set_wrprotect(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + pte_t pte) { - pte_t old_pte, pte; + pte_t old_pte; - pte = __ptep_get(ptep); do { old_pte = pte; pte = pte_wrprotect(pte); @@ -996,6 +992,25 @@ static inline void __ptep_set_wrprotect(struct mm_struct *mm, } while (pte_val(pte) != pte_val(old_pte)); } +/* + * __ptep_set_wrprotect - mark read-only while trasferring potential hardware + * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit. + */ +static inline void __ptep_set_wrprotect(struct mm_struct *mm, + unsigned long address, pte_t *ptep) +{ + ___ptep_set_wrprotect(mm, address, ptep, __ptep_get(ptep)); +} + +static inline void __wrprotect_ptes(struct mm_struct *mm, unsigned long address, + pte_t *ptep, unsigned int nr) +{ + unsigned int i; + + for (i = 0; i < nr; i++, address += PAGE_SIZE, ptep++) + __ptep_set_wrprotect(mm, address, ptep); +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_PMDP_SET_WRPROTECT static inline void pmdp_set_wrprotect(struct mm_struct *mm, @@ -1149,6 +1164,8 @@ extern int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); extern int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); +extern void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr); extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty); @@ -1268,12 +1285,35 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma, return contpte_ptep_clear_flush_young(vma, addr, ptep); } +#define wrprotect_ptes wrprotect_ptes +static inline void wrprotect_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + if (likely(nr == 1)) { + /* + * Optimization: wrprotect_ptes() can only be called for present + * ptes so we only need to check contig bit as condition for + * unfold, and we can remove the contig bit from the pte we read + * to avoid re-reading. This speeds up fork() which is sensitive + * for order-0 folios. Equivalent to contpte_try_unfold(). + */ + pte_t orig_pte = __ptep_get(ptep); + + if (unlikely(pte_cont(orig_pte))) { + __contpte_try_unfold(mm, addr, ptep, orig_pte); + orig_pte = pte_mknoncont(orig_pte); + } + ___ptep_set_wrprotect(mm, addr, ptep, orig_pte); + } else { + contpte_wrprotect_ptes(mm, addr, ptep, nr); + } +} + #define __HAVE_ARCH_PTEP_SET_WRPROTECT static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { - contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); - __ptep_set_wrprotect(mm, addr, ptep); + wrprotect_ptes(mm, addr, ptep, 1); } #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS @@ -1305,6 +1345,7 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, #define ptep_clear_flush_young __ptep_clear_flush_young #define __HAVE_ARCH_PTEP_SET_WRPROTECT #define ptep_set_wrprotect __ptep_set_wrprotect +#define wrprotect_ptes __wrprotect_ptes #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS #define ptep_set_access_flags __ptep_set_access_flags diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 6d7f40667fa2..bedb58524535 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -26,6 +26,26 @@ static inline pte_t *contpte_align_down(pte_t *ptep) return PTR_ALIGN_DOWN(ptep, sizeof(*ptep) * CONT_PTES); } +static void contpte_try_unfold_partial(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + /* + * Unfold any partially covered contpte block at the beginning and end + * of the range. + */ + + if (ptep != contpte_align_down(ptep) || nr < CONT_PTES) + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + + if (ptep + nr != contpte_align_down(ptep + nr)) { + unsigned long last_addr = addr + PAGE_SIZE * (nr - 1); + pte_t *last_ptep = ptep + nr - 1; + + contpte_try_unfold(mm, last_addr, last_ptep, + __ptep_get(last_ptep)); + } +} + static void contpte_convert(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) { @@ -238,6 +258,24 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, } EXPORT_SYMBOL(contpte_ptep_clear_flush_young); +void contpte_wrprotect_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr) +{ + /* + * If wrprotecting an entire contig range, we can avoid unfolding. Just + * set wrprotect and wait for the later mmu_gather flush to invalidate + * the tlb. Until the flush, the page may or may not be wrprotected. + * After the flush, it is guaranteed wrprotected. If it's a partial + * range though, we must unfold, because we can't have a case where + * CONT_PTE is set but wrprotect applies to a subset of the PTEs; this + * would cause it to continue to be unpredictable after the flush. + */ + + contpte_try_unfold_partial(mm, addr, ptep, nr); + __wrprotect_ptes(mm, addr, ptep, nr); +} +EXPORT_SYMBOL(contpte_wrprotect_ptes); + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty) From patchwork Thu Feb 15 10:32:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557864 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 9150FC4829E for ; Thu, 15 Feb 2024 10:33:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 96EDC8D0020; Thu, 15 Feb 2024 05:33:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8FA1D8D000E; Thu, 15 Feb 2024 05:33:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D4AF8D0020; Thu, 15 Feb 2024 05:33:10 -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 5747D8D000E for ; Thu, 15 Feb 2024 05:33:10 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 288B4A2690 for ; Thu, 15 Feb 2024 10:33:10 +0000 (UTC) X-FDA: 81793675740.25.E68C4E3 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf11.hostedemail.com (Postfix) with ESMTP id 81CB540002 for ; Thu, 15 Feb 2024 10:33:08 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; spf=pass (imf11.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=1707993188; 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=BCAtce4Ja9jmZhUdX/dylO55pAFHXc3eVtMKGJQHUkc=; b=TBiirKtuhw1fnB25EZN8ZmFkLawqnLs51bDFsA5lUpmv3Q6m4g1U7VZgvL09CXpNGfwbgY xJb4vS0Almt7oiUGcq6R/fBAL58tchXYLi73csSn1qfQKeoT3tLYDUoM6gpmR9z8VNpgaf l2OQC4ccn0BwcoDbtf93nepsotVPmNU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707993188; a=rsa-sha256; cv=none; b=EyA9AXWBFokpekNQRV7/qTowlmmgNapO/YXguZtGczfFZeZfcJx7gL1KNknC9Ibjt6pFgD 0fapHO2MGVsZ0aK0gqd4sScxQm7fiqG2eTjTHdYKbnJBNmZj+7GAD/QdyyUQMdKIZFpf7G TUS3SJA3sUjY2O5lH5Zt1vyCnTXC7A8= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; spf=pass (imf11.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 B0CA71596; Thu, 15 Feb 2024 02:33: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 A85623F7B4; Thu, 15 Feb 2024 02:33:04 -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 14/18] arm64/mm: Implement new [get_and_]clear_full_ptes() batch APIs Date: Thu, 15 Feb 2024 10:32:01 +0000 Message-Id: <20240215103205.2607016-15-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: 81CB540002 X-Rspam-User: X-Stat-Signature: bbs3uhzji8eenebznt8fad7cycscroc7 X-Rspamd-Server: rspam03 X-HE-Tag: 1707993188-147889 X-HE-Meta: U2FsdGVkX19oS5AL52uWx7vmSHMWqCN1yUBwFwywpKCMw4TOHrDwrzB4hsxvh9AjlukQrN8qGYrdVdcoG2LTWWixSOlQaLemDgKakrjc3a2tgoSIpYsZJTrtVgnj8LRQKkpu8SS5/bKji5H8WWa38qTt7P5IRemgozgQU4Ytcp+jnHpBrQdDkeiRmj/NCtftiaDalxOTOmICD8TBiHysGldI7FNwpxkpH0zAvIRniC4N8mqcxIA+u9fwUWdIiNPUh2EcZ5ATg1xiguaS0uNndR0H0HRBtCSEFHP/3y5QBZWrW+hDLwM2iwyvEov8NdF9Y3b+o2tXk89Wlul95IR1SsT4/U55Zni0+1TtYtlOtEfvXhkgAt6Eo3YpE8HWar8sfCaCf1W9nprMooTbzmlVybGVgCAa5D5IXVoldjAXM3gDqmIGhbp7S2xQWDmsEdO9b+c4c0blOOoQMSq6nqWGAlorse3wvTo5ROk5nq4vu2sua7CUsY0W3yO50Bvm/S29z8HPh2yWPDV2/ro4M+4pYy9bQogxtVAxZoWobqD5IXjaP2vGa14icCK3lJDgC//zFtAAUx7+BSi1c68dH90LHxgcJRkTCfk6hsv+3/3IbzL7+uciO9KeawLGplLps9Esl2gL8s2cV7ZlbDQjCBYcKOy3PjE6m4Rhk00/Zjcu1aYfE45ILvW86tKolHpBUizkmSujtLVs3Fc5WZ9uYTW2wBm7EDAYN351JfbxBEr6m4Bg4sSCBfrH225Ep2zFuOaqpbFiWmRHOwaR58UEQ5c+x5F0GnqR57jQjQYFjoZ70M0qQPwQLDqtvDlITBmxJy8ScH75B9QiVahPIe2RjE8HC0IFFXwOX2DWcoCW2e3x67DkwGiYozTjkA644vuDthbJdJb6rXt+vK27rCA9LQ3jJN/GLUbhrQnHLYNIM7XieAnOumJkOpUPxWkQhUCNbCwWnLI2E+CE4kJDCkQRMpI jyXkPZbK 5JmSGwqMdiRGp1G+kRW06sy7ct39r7vz5Zt6kc3LkRlDV0WUYgPGwGYoPyYjWae37p9PGT01otVwT6xrWHvKwe5t84RdluHSffudaNS9KPF7aVMVf2ZjvI0ikw/uufubWiwf3/Mo0dN2LOPzSXixT+k9zmn60Ndd12FKTBB1d3jqZYa7J5SG/IarxtvSWqrx6vxMfrApXj5QOiGEUfPRgOIkQmQUJ8eDKD1gEMFKFhXEzFCyBPDrABxXHpbppi/Hq2CE17sTE/2BV2TBaih4GpjCXTnjV1PqMQ+Urfs2CcN/oejc= 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: Optimize the contpte implementation to fix some of the exit/munmap/dontneed performance regression introduced by the initial contpte commit. Subsequent patches will solve it entirely. During exit(), munmap() or madvise(MADV_DONTNEED), mappings must be cleared. Previously this was done 1 PTE at a time. But the core-mm supports batched clear via the new [get_and_]clear_full_ptes() APIs. So let's implement those APIs and for fully covered contpte mappings, we no longer need to unfold the contpte. This significantly reduces unfolding operations, reducing the number of tlbis that must be issued. Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 67 ++++++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 17 ++++++++ 2 files changed, 84 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 8643227c318b..a8f1a35e3086 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -965,6 +965,37 @@ static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, return pte; } +static inline void __clear_full_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, int full) +{ + for (;;) { + __ptep_get_and_clear(mm, addr, ptep); + if (--nr == 0) + break; + ptep++; + addr += PAGE_SIZE; + } +} + +static inline pte_t __get_and_clear_full_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + pte_t pte, tmp_pte; + + pte = __ptep_get_and_clear(mm, addr, ptep); + while (--nr) { + ptep++; + addr += PAGE_SIZE; + tmp_pte = __ptep_get_and_clear(mm, addr, ptep); + if (pte_dirty(tmp_pte)) + pte = pte_mkdirty(pte); + if (pte_young(tmp_pte)) + pte = pte_mkyoung(pte); + } + return pte; +} + #ifdef CONFIG_TRANSPARENT_HUGEPAGE #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm, @@ -1160,6 +1191,11 @@ extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte); extern pte_t contpte_ptep_get_lockless(pte_t *orig_ptep); extern void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte, unsigned int nr); +extern void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, int full); +extern pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, int full); extern int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep); extern int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, @@ -1253,6 +1289,35 @@ static inline void pte_clear(struct mm_struct *mm, __pte_clear(mm, addr, ptep); } +#define clear_full_ptes clear_full_ptes +static inline void clear_full_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, int full) +{ + if (likely(nr == 1)) { + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + __clear_full_ptes(mm, addr, ptep, nr, full); + } else { + contpte_clear_full_ptes(mm, addr, ptep, nr, full); + } +} + +#define get_and_clear_full_ptes get_and_clear_full_ptes +static inline pte_t get_and_clear_full_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + pte_t pte; + + if (likely(nr == 1)) { + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + pte = __get_and_clear_full_ptes(mm, addr, ptep, nr, full); + } else { + pte = contpte_get_and_clear_full_ptes(mm, addr, ptep, nr, full); + } + + return pte; +} + #define __HAVE_ARCH_PTEP_GET_AND_CLEAR static inline pte_t ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) @@ -1337,6 +1402,8 @@ static inline int ptep_set_access_flags(struct vm_area_struct *vma, #define set_pte __set_pte #define set_ptes __set_ptes #define pte_clear __pte_clear +#define clear_full_ptes __clear_full_ptes +#define get_and_clear_full_ptes __get_and_clear_full_ptes #define __HAVE_ARCH_PTEP_GET_AND_CLEAR #define ptep_get_and_clear __ptep_get_and_clear #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index bedb58524535..50e0173dc5ee 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -212,6 +212,23 @@ void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, } EXPORT_SYMBOL(contpte_set_ptes); +void contpte_clear_full_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, int full) +{ + contpte_try_unfold_partial(mm, addr, ptep, nr); + __clear_full_ptes(mm, addr, ptep, nr, full); +} +EXPORT_SYMBOL(contpte_clear_full_ptes); + +pte_t contpte_get_and_clear_full_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + contpte_try_unfold_partial(mm, addr, ptep, nr); + return __get_and_clear_full_ptes(mm, addr, ptep, nr, full); +} +EXPORT_SYMBOL(contpte_get_and_clear_full_ptes); + int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) { 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); } /* From patchwork Thu Feb 15 10:32:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557866 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 66484C4829E for ; Thu, 15 Feb 2024 10:33:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 774598D0022; Thu, 15 Feb 2024 05:33:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6FCC28D000E; Thu, 15 Feb 2024 05:33:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FFBC8D0022; Thu, 15 Feb 2024 05:33:17 -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 356B58D000E for ; Thu, 15 Feb 2024 05:33:17 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 15A6280FC4 for ; Thu, 15 Feb 2024 10:33:17 +0000 (UTC) X-FDA: 81793676034.24.FAE8DED Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf27.hostedemail.com (Postfix) with ESMTP id 673A840020 for ; Thu, 15 Feb 2024 10:33:15 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf27.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=1707993195; a=rsa-sha256; cv=none; b=0aFBquuYjObQ1+B+QB6ASiIiBs1x24v6O2AV7M7h+n5GcBubYe+WXJnoPoe5/kbiOGxlgM n8wiUkMHa46quJizdF5BUGRTOsLnFoz7213GZ+3+oTrKqw1e7jzJIGuEwhQR8YLYAr7HvF nX4BIfMhlnmhWPW4y/GpqXX+LCa26Wk= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf27.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=1707993195; 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=RBDSb1ynAOyzexiyHHWXWWE26cJncrKBIu2RWSUrpHs=; b=Miiw20A7Tuc4T4FPzU5IEYvM0yzjRsMJBgdtUOAKtKhXVYKrmcJG9o3pXQ8iN8pa6jIXBF suZSuHLPtYCOTEcnDrDepyFFfYqcfKnLXXwb0Hh/6Ga0UwPH/ggYUmu4LjEXltXZP5Y6xK 1mX4cwenJwWZtIKRLBz3UWAnjMkGV6Q= 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 871BF15BF; Thu, 15 Feb 2024 02:33:55 -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 7B9B93F7B4; Thu, 15 Feb 2024 02:33:11 -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 16/18] arm64/mm: Implement pte_batch_hint() Date: Thu, 15 Feb 2024 10:32:03 +0000 Message-Id: <20240215103205.2607016-17-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-Server: rspam08 X-Rspamd-Queue-Id: 673A840020 X-Stat-Signature: momodpndugdcdcak9w9xs88j7bnjwia5 X-Rspam-User: X-HE-Tag: 1707993195-786397 X-HE-Meta: U2FsdGVkX1/keZAjVT6f93ztl7FT+THUsRsEOFWyPEYDgLrxZPznmr7zHIdRDv2EFV5Fd1WEJNq0GQQVM7gseYoCMaYFi1gYKzso4bnBF3c9cpyiMhPfq40dIcjxHAU1JMWYXEHpYbnWxDHNfUozezoVe3cfYNcDsoGdi1hSnL0lagR6mjX74WTCdCnDC4XNwLo9TH+WT8mFAtbo4ExD+8XQpjrc+bYD4EApj2TfTR1CsX8GdPhi3YM9zemSDL/T8FlhgtapfuwltyXxvur9M3ODC9F8cSs8a57cEo/47D6+/12codsqYPzDe6HwWlMGQ1FSqh9VMFlA9KzIEkpcmrbdkaQ/f2I0w66n8xSZLdGUKEHbF9F01yr3flHn+Ti8tACiacPSlkkzrt4uwlw98OLMwTPJdT7s20Rsny+ye3NbzdLilUgp8R1EanI6d2zi3c5+z1jM6gchlJU3dR4oB6t8e23CIQn1/y4J5Aa5IhbVSaG4HQ8t2q8FTMKWQvb7k9QaI8UUIqpRQMrKIp9xDIgcCKtzQje+jSIIIkyNZeINNWWFq0olaLo8l2S6FETUbwLg08LnOo+9yKbVgwPpvDPr9Vt5Nv3Z3EFPSdlXOvm/pace0wvdYmB8r+pc3C0X1TLRQjrX153da/CSTl01DQ2fanYQUpwwd7oXFav/uYMCpfvn6Rpy1k7bceeTuR34NXwxtnROZkxyeWyI+nV+Izw+sAgD6iLnIkG92AC6cA9PDH2FEHjktiGy4MmU4dAfsBI9BZ2Hb0Crd18nEBUbTetBuWoipa9bdPzFQE0ce9mqwRH0FIG44946/BHMZF89zuHKpRfP7tY2eegLtZ6gNkhzIbIGFa9opfw6ZxhF6bzsQwQmOTUScrwio7u7LIfYi3awY+eWCxXc/lzc9mqC/y2+T0z3bazklDbzQ3smWwAXwNErpl9VR/crt8Cc6F2SUGbILk9A8Lv/NtYtzQl B23dSlRo UuGXKOYPIc36wkF4sgu1PBAhWzJr6vy51GSowzTzBKINvtaHkKgFes/ABon1kC5+T2fmVid+Y2nOk1hLQA2Ic26JNbFScASbBCl8x48bCeXka2GVX58rxcofcfi4zF2zWQ8yd1GvqMxuH1zinfE44TIdhPSSphHYNbAlELq95gZDUG59y4+ccCsxdCw57nAFbLikjeBjk2QrQKB/mOjL3YI7Y6wnaTHkVqajmR+F6MRtbpGuC45BQTEFis5qsgVspzbKRf8AJKXX92vIZy0srM192bgZxm9yIiMF4P1Hzv64+ZxuKA0F4wt55P1nxQbKsK/OI 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: When core code iterates over a range of ptes and calls ptep_get() for each of them, if the range happens to cover contpte mappings, the number of pte reads becomes amplified by a factor of the number of PTEs in a contpte block. This is because for each call to ptep_get(), the implementation must read all of the ptes in the contpte block to which it belongs to gather the access and dirty bits. This causes a hotspot for fork(), as well as operations that unmap memory such as munmap(), exit and madvise(MADV_DONTNEED). Fortunately we can fix this by implementing pte_batch_hint() which allows their iterators to skip getting the contpte tail ptes when gathering the batch of ptes to operate on. This results in the number of PTE reads returning to 1 per pte. Acked-by: Mark Rutland Reviewed-by: David Hildenbrand Tested-by: John Hubbard Signed-off-by: Ryan Roberts Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index a8f1a35e3086..d759a20d2929 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1213,6 +1213,15 @@ static inline void contpte_try_unfold(struct mm_struct *mm, unsigned long addr, __contpte_try_unfold(mm, addr, ptep, pte); } +#define pte_batch_hint pte_batch_hint +static inline unsigned int pte_batch_hint(pte_t *ptep, pte_t pte) +{ + if (!pte_valid_cont(pte)) + return 1; + + return CONT_PTES - (((unsigned long)ptep >> 3) & (CONT_PTES - 1)); +} + /* * The below functions constitute the public API that arm64 presents to the * core-mm to manipulate PTE entries within their page tables (or at least this From patchwork Thu Feb 15 10:32:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557867 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 5073BC48BEB for ; Thu, 15 Feb 2024 10:33:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D403E8D0023; Thu, 15 Feb 2024 05:33:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CC8858D000E; Thu, 15 Feb 2024 05:33:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF4FB8D0023; Thu, 15 Feb 2024 05:33:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 98F038D000E for ; Thu, 15 Feb 2024 05:33:20 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7ACC8120F15 for ; Thu, 15 Feb 2024 10:33:20 +0000 (UTC) X-FDA: 81793676160.06.F48F580 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf26.hostedemail.com (Postfix) with ESMTP id E68F914000A for ; Thu, 15 Feb 2024 10:33:18 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; spf=pass (imf26.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=1707993199; 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=PAVJHyGMAUnF7AjViDJ+K3AaQu9PJj0R+gebBFLO5LA=; b=GyN/hYixhActmGzhoJvRdSLk9jBYO/D66e8F0xUFrFYiGZrBqPvZ7JdnbYbZAnJdkqPXwv U4w7ifmCVOPMW+okhA+yyz7XA4zlHa/nEohgaT16i0bmj1P/rh4nmzSWV593KobwCndemr h9ioCVQ7EAgXNgtfwKJQRdQRDvCfwNU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707993199; a=rsa-sha256; cv=none; b=UH34bsajCi5VNsjUAxMMMYCJih6R/doa4PZbH2ryaTM79hXojiaYrInW8iOLHBe+4f44cG KDveUiAkviZMWeyLtPxL1mVq94uCekIgTJVUZnuIlPuJoFn/5wZWcov7TtZmst8ltI48wk T+2nfg5V5aWviu3AIulSpDILN36lCCc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; spf=pass (imf26.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 EBA3215DB; Thu, 15 Feb 2024 02:33:58 -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 E26CB3F7B4; Thu, 15 Feb 2024 02:33:14 -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 17/18] arm64/mm: __always_inline to improve fork() perf Date: Thu, 15 Feb 2024 10:32:04 +0000 Message-Id: <20240215103205.2607016-18-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-Stat-Signature: fgqt4tdys8crip1zeqxziesmgub5ofgs X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: E68F914000A X-Rspam-User: X-HE-Tag: 1707993198-722197 X-HE-Meta: U2FsdGVkX19DS5oa3qzJ5LAhEbutmBMGbC2W4PXfCJ1nm/cCRUwqeuaKGtHvoWkQAdEyNOQ23RZQZiBgrrSPdUQ5Jx3qjqdX8d8tQWjnGec5YRJgy06h5+PiiXb/3sKEcrB8dvpFOer+rB/ZKvTTDQRf1S6WAoByKaPGoceJYW3wcvcq+L/e1lS8/KO4K2ADgLizEPxiuBxX2tlq/mMwGaXAXIKvNBBbPGMjLajC7WmJXlHXZVTmTReTOoKyZNQNj18uKrsTVrsDk31iPvTT1GJJRC06fv0MR2dEX9ZjlHdnPR9un7aEYQSg6B0UrYLjOqmRe8RyzeS3abNPcURT+v4k6lC9EKdWNJeWnI1mw641Y4jDIFRoiZjnomtNw11p2YufRDc1JJZFc8CSFhkiyP04UmWymEj1td4Ht+FEM0ct/xesOLqgjyk+0IBFSJyrSeiTVh5NWrdw2uKPlnp6gux1IOX8ImL1qLllpXRgRtRdXsjJKDwrx6lkNnuPQx84DHgIpJYgE2Gt72ton8GCDNmjoAvUDDEB4J7kNguwx4BwhiMHeauGGLGV/ri8FNZGxORh1VLoQ/LRT9SxdWsuQUf5+SwYobZHSXJv+C33ymNenb9KmvRq+6kbLlCNn2jRHePE8zljD9E69a8s5gGQglvWbKlm9/8/9jlPdUQ7vghitpI7gZja9lt+x8MIReOP11gPGJuN2fNWIv053etSP+DQXLxZdgOdpOOpxilyC42u7yMwvhZ8xBFK0R8LDEa8FjfOl01ehAO1qR13lLtDtInaQv/PqncBlEJrwFrf8ZZYc5j68B8hqbdi+UegrXvDHeFS909d4jPCORuJSHmH5Sl5uOQCKQWe3mocdymcTNYEnOP1Nj8ErN7ov8sWWjJmJ/a9a4Xs3PMedyLKZs4rPxK/Wvhgek+n0afXEK/36LaCfEHo3c0RNPBXPVl8xeobbNlQYcoDzsrlr8ahdGK pL1+ZUMl FYumG7kOEEDrk7F+g+bYErk4lMT7MRiRJddCX+KGdVfRgS/CvVKXkLiWG4IoY5WYIFdBRnHWLU6kEEO+q8f6JLRIDk2KfqCZ3GtJ8pkNiQWZyGjv3YdWy1f3f2CazwvCuSWoF541EA1gYfA94wI6Li81zNv2XUMr3XKYU3nIT0QytoeHaZCJin7pK5jQ0ycnri/2lqhWM1ENKpHN95vE0GJL1LyNcXSCrfJjysN+4k0lZLsMKpSru8rzoT9dxag6icrpeq5f9+ecDCWMuUaFcOftpZg== 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: As set_ptes() and wrprotect_ptes() become a bit more complex, the compiler may choose not to inline them. But this is critical for fork() performance. So mark the functions, along with contpte_try_unfold() which is called by them, as __always_inline. This is worth ~1% on the fork() microbenchmark with order-0 folios (the common case). Acked-by: Mark Rutland Signed-off-by: Ryan Roberts Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index d759a20d2929..8310875133ff 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1206,8 +1206,8 @@ extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty); -static inline void contpte_try_unfold(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, pte_t pte) +static __always_inline void contpte_try_unfold(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, pte_t pte) { if (unlikely(pte_valid_cont(pte))) __contpte_try_unfold(mm, addr, ptep, pte); @@ -1278,7 +1278,7 @@ static inline void set_pte(pte_t *ptep, pte_t pte) } #define set_ptes set_ptes -static inline void set_ptes(struct mm_struct *mm, unsigned long addr, +static __always_inline void set_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte, unsigned int nr) { pte = pte_mknoncont(pte); @@ -1360,8 +1360,8 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma, } #define wrprotect_ptes wrprotect_ptes -static inline void wrprotect_ptes(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, unsigned int nr) +static __always_inline void wrprotect_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, unsigned int nr) { if (likely(nr == 1)) { /* From patchwork Thu Feb 15 10:32:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13557868 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 ACE61C4829E for ; Thu, 15 Feb 2024 10:33:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43F9B8D0024; Thu, 15 Feb 2024 05:33:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C6F48D000E; Thu, 15 Feb 2024 05:33:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21A228D0024; Thu, 15 Feb 2024 05:33:24 -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 025368D000E for ; Thu, 15 Feb 2024 05:33:24 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D081DA07AE for ; Thu, 15 Feb 2024 10:33:23 +0000 (UTC) X-FDA: 81793676286.09.312803A Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf16.hostedemail.com (Postfix) with ESMTP id 3A3BD180017 for ; Thu, 15 Feb 2024 10:33:22 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; spf=pass (imf16.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=1707993202; a=rsa-sha256; cv=none; b=RawWUb/4xsW05UowJTw5uF7h3E7Y7JFh7g4QfxSbqUB7Paex92jQEQ2xK4lz8rKIPR6nwz aZnlOdN+p5UTjqmRCddYhCmNcAssz1SxgINc2j5Pds5yk19G4/GLo0Hx+Om3QRVvk34nOa WJkiShVTMmQYX17uaShuH5d8Sh2CCXM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; spf=pass (imf16.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=1707993202; 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=Zz5h8gFoLcQ5F5jny0TZcnql6XjfJNjA5U+ixiuRaqk=; b=iEZwWbfgqhQDFXdbUXc0qTarrffcUAcsdzjWTEnO2oCzieDgWCDt7qYfQSGk9q69dCwbU8 PqckH0JLf4YxZ8AxoIfq6t9mnAAAOUHsTVh4yInYqo4OgcBW3k1FVI138qBX/90kVL2lRD dp8A8kwCLE/QYT8Npi7Xk2U8ddknPmY= 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 5B7551650; Thu, 15 Feb 2024 02:34:02 -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 527B93F7B4; Thu, 15 Feb 2024 02:33:18 -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 18/18] arm64/mm: Automatically fold contpte mappings Date: Thu, 15 Feb 2024 10:32:05 +0000 Message-Id: <20240215103205.2607016-19-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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3A3BD180017 X-Stat-Signature: qo7xt15sft8iptksgjei9y36mw43p4oj X-HE-Tag: 1707993202-195522 X-HE-Meta: U2FsdGVkX1+D2ZskTCmlX7t6njMQZLTtESj7oaBVXRc8z3aDcQU1P90k/4BYesKEyI0EwYP1xiqWgpVeyJJr8ZTqJeWUzCdriE31vVHPPB1o3R+zM65w2BlUWnY6uRkFNBatDB1zHh20ApjrLonzosgDlJxcmdBZ30IRIPepaiQJouNOTRvIqwFM3269t7ZRmz7RmH4+1V38MZMOvWWI1myt72MM7XaPbNvuLXdZNHsbzwD9HstMIVlhtuXjpVtL7XvhxMgFOQUrx0Jv+3rRGlUr/th1vj8GMydMxbvJafLxjEilsr9uPWkq+RizR909u+u5rorKy/43vDIPHpQ0joK2s0P9tOOrj8Cj6YvKq0gTKffq40MZ19qSfCmsZw+HZLlkcgWGE1hsRG+hapSdk0MmVJNQ7oCYX5MZpS/XeBZouW6G1/B7qb9Ke9PqvMyN6W2u3Id7+zdHIM2eglgCHyTsH9hR7qgnPEDKIXsQWnQVciqhYqG1etgtAlkzt20CREzcs9Ibsh8cn8POp6dDDHgd1+nRsF6RWX4XcDWLisPepTddTWdGq9pm+jtrUtVqLoPuY9fOvuJ/bmoB9DYwxecW1ZdgtUkHyi9JsWeXfr82l0CjbaUS+1jJy1asX8vK9x5A0pHMik5vGSCZiPKgAbL1Lfv7YyMg6oykphhojTqMVVjsXzECTOFyZD8+/7uw7+72lLcJbe33DdOfBiO0xjx1hbTpzhLqkl6OwQLFc0HvjZXQee/L1I1BsYz+WUIp3nsWjPjfz6kZeMArbCMIi14x9Kz01C3sTH6GwWflOw9mGHuNDbm8dHWiA87eujg6mc7nBnIoC/5g4EGTDZuofZJeYVSthVaKnfBDWbGkT7xgRsaVpyhjTz6dc7yEmzvesZNViyt05pOmjKlyqiD0i7EfK+2Ak39Zf/X5VYGavwAME5Iv1KJss8ru+eVtrZDzX9dPk2I7kYQcO4SZUfE ebdQtycZ GpA1kgJ5aSOIuaLbnx3sSQjmaEKyO8otJKCYfBSPTDCX8o5UNxP0iWttSyJOI4+rjqy+mnQau5PhZZ+7HQExi4qUsJxRJSL2GaMvfD1HZY3uXWbrkhWTP5s+nR+8XvCpL95fSi7wQOZvyBY11EI9rnCcrAZ9Amp6c4LbNM8FgKBOLIHd+8eYHHOGpQQfpf0winwPEbu8G7ConKfAKTcNT7QWWZG5TT21T77kruXOt5DhKf1g= 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: There are situations where a change to a single PTE could cause the contpte block in which it resides to become foldable (i.e. could be repainted with the contiguous bit). Such situations arise, for example, when user space temporarily changes protections, via mprotect, for individual pages, such can be the case for certain garbage collectors. We would like to detect when such a PTE change occurs. However this can be expensive due to the amount of checking required. Therefore only perform the checks when an indiviual PTE is modified via mprotect (ptep_modify_prot_commit() -> set_pte_at() -> set_ptes(nr=1)) and only when we are setting the final PTE in a contpte-aligned block. Signed-off-by: Ryan Roberts Acked-by: Mark Rutland Acked-by: Catalin Marinas --- arch/arm64/include/asm/pgtable.h | 26 +++++++++++++ arch/arm64/mm/contpte.c | 64 ++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 8310875133ff..401087e8a43d 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -1185,6 +1185,8 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, * where it is possible and makes sense to do so. The PTE_CONT bit is considered * a private implementation detail of the public ptep API (see below). */ +extern void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte); extern void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte); extern pte_t contpte_ptep_get(pte_t *ptep, pte_t orig_pte); @@ -1206,6 +1208,29 @@ extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty); +static __always_inline void contpte_try_fold(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, pte_t pte) +{ + /* + * Only bother trying if both the virtual and physical addresses are + * aligned and correspond to the last entry in a contig range. The core + * code mostly modifies ranges from low to high, so this is the likely + * the last modification in the contig range, so a good time to fold. + * We can't fold special mappings, because there is no associated folio. + */ + + const unsigned long contmask = CONT_PTES - 1; + bool valign = ((addr >> PAGE_SHIFT) & contmask) == contmask; + + if (unlikely(valign)) { + bool palign = (pte_pfn(pte) & contmask) == contmask; + + if (unlikely(palign && + pte_valid(pte) && !pte_cont(pte) && !pte_special(pte))) + __contpte_try_fold(mm, addr, ptep, pte); + } +} + static __always_inline void contpte_try_unfold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) { @@ -1286,6 +1311,7 @@ static __always_inline void set_ptes(struct mm_struct *mm, unsigned long addr, if (likely(nr == 1)) { contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); __set_ptes(mm, addr, ptep, pte, 1); + contpte_try_fold(mm, addr, ptep, pte); } else { contpte_set_ptes(mm, addr, ptep, pte, nr); } diff --git a/arch/arm64/mm/contpte.c b/arch/arm64/mm/contpte.c index 50e0173dc5ee..16788f07716d 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -73,6 +73,70 @@ static void contpte_convert(struct mm_struct *mm, unsigned long addr, __set_ptes(mm, start_addr, start_ptep, pte, CONT_PTES); } +void __contpte_try_fold(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ + /* + * We have already checked that the virtual and pysical addresses are + * correctly aligned for a contpte mapping in contpte_try_fold() so the + * remaining checks are to ensure that the contpte range is fully + * covered by a single folio, and ensure that all the ptes are valid + * with contiguous PFNs and matching prots. We ignore the state of the + * access and dirty bits for the purpose of deciding if its a contiguous + * range; the folding process will generate a single contpte entry which + * has a single access and dirty bit. Those 2 bits are the logical OR of + * their respective bits in the constituent pte entries. In order to + * ensure the contpte range is covered by a single folio, we must + * recover the folio from the pfn, but special mappings don't have a + * folio backing them. Fortunately contpte_try_fold() already checked + * that the pte is not special - we never try to fold special mappings. + * Note we can't use vm_normal_page() for this since we don't have the + * vma. + */ + + unsigned long folio_start, folio_end; + unsigned long cont_start, cont_end; + pte_t expected_pte, subpte; + struct folio *folio; + struct page *page; + unsigned long pfn; + pte_t *orig_ptep; + pgprot_t prot; + + int i; + + if (!mm_is_user(mm)) + return; + + page = pte_page(pte); + folio = page_folio(page); + folio_start = addr - (page - &folio->page) * PAGE_SIZE; + folio_end = folio_start + folio_nr_pages(folio) * PAGE_SIZE; + cont_start = ALIGN_DOWN(addr, CONT_PTE_SIZE); + cont_end = cont_start + CONT_PTE_SIZE; + + if (folio_start > cont_start || folio_end < cont_end) + return; + + pfn = ALIGN_DOWN(pte_pfn(pte), CONT_PTES); + prot = pte_pgprot(pte_mkold(pte_mkclean(pte))); + expected_pte = pfn_pte(pfn, prot); + orig_ptep = ptep; + ptep = contpte_align_down(ptep); + + for (i = 0; i < CONT_PTES; i++) { + subpte = pte_mkold(pte_mkclean(__ptep_get(ptep))); + if (!pte_same(subpte, expected_pte)) + return; + expected_pte = pte_advance_pfn(expected_pte, 1); + ptep++; + } + + pte = pte_mkcont(pte); + contpte_convert(mm, addr, orig_ptep, pte); +} +EXPORT_SYMBOL(__contpte_try_fold); + void __contpte_try_unfold(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pte) {