From patchwork Mon Dec 18 10:50:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496565 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 DF76DC35274 for ; Mon, 18 Dec 2023 10:51:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7580E6B0089; Mon, 18 Dec 2023 05:51:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7070B6B008C; Mon, 18 Dec 2023 05:51:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A7CA6B0092; Mon, 18 Dec 2023 05:51:25 -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 47D636B0089 for ; Mon, 18 Dec 2023 05:51:25 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0F439121038 for ; Mon, 18 Dec 2023 10:51:25 +0000 (UTC) X-FDA: 81579622530.26.28DB535 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf19.hostedemail.com (Postfix) with ESMTP id 464EF1A0011 for ; Mon, 18 Dec 2023 10:51:23 +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=1702896683; 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=nHYd6pL2au0ylISSnRYYRDrN6i/JAMSJRS7tvhKFDUA=; b=j3OCoglEgHyKXxTQ8f5qP16xkcjY/Wq+FaJMYorMOEIZKKOGJw+IR3oCCAaR0JI8ixwGOW vO2oXIu35IeqwRI8zO3o3/MaeD+3AaR/zcgO7ktDPg6lTYlzWnV3o+3TaZV7gtlr7RpVTT YA4fLG3mVdbtP+68vtdI8v0VElj7Xxs= 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=1702896683; a=rsa-sha256; cv=none; b=HeT4Jj3qUzx6GGzr4VQTEdaPZofWk8MeksEK5pVed7t17vWfUdHyiYcIOuSjdvxkXA34Xf MaFnB3uBAFyZ9j66pIGoB/jqpUfXr9yu/m58xq1kqkjqfZn2EENmGkShh+6trS+v8nfxSh bcY+hwx1TxCttirKulkMpR3TJt42DsE= 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 754E92F4; Mon, 18 Dec 2023 02:52:06 -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 9BE873F738; Mon, 18 Dec 2023 02:51:18 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 01/16] mm: thp: Batch-collapse PMD with set_ptes() Date: Mon, 18 Dec 2023 10:50:45 +0000 Message-Id: <20231218105100.172635-2-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 464EF1A0011 X-Stat-Signature: taeaadrfpumgwkopazkm5knf3adep64r X-HE-Tag: 1702896682-619285 X-HE-Meta: U2FsdGVkX18JFI2jhaJUD2ahuW3lFLtt06yzgSMTbXkXxQwDdCa4ZbDC+i9b1iZypoiWv+lKo3OIaqnIrY8VMe0CiAMb0XPtE0VJiDA++x6KWn+2Mf6yS3uQf3Jmym9cnuGeaLkPhVjFTHIvsnNKJ/c0CSiUwFDmpvcpW1666C6UBX63+o+CLD9Z9PP/7HTzSiK9FBo0qYw9/XytJIs/tW22bteahVcRWnRLKOYhsXHdr9hFRrbVAcm0jlyPOuGuVlFX7xT+57KmKTvPzW9dq4tvKmcgI0C8Efd3ePwiBHUDm84mi+c8vJKQODLoxs6MpHYTOq4pNdp0WiqzRt3JFrhyfTJp8SVtcllWMqwj0gk97lE/p8KbS/s6LkjVrWgPcPvKrEF655kfQxK4e4F7/JzrUyCF7WN/m2R9dw97t/+hdvU5aC0fiT/jxaN8H/RgbNeNqkW4Gth5GAcWVa6FuKKg3fVuT9WuxslL06Dmj8g2iqbQoIjXRl8aV1z7GELG36hohsjfb3lcNFZiRymr251ekKXEA0Q3OT3f41VU6YaEVfwYV/Xo/ye7C9IVsh28y7U8GuQ6FOelSYKcrGrpMZ96f2SFJuzBWzCh0DZx2UP/DuBaapX2u/d8QjeUoBlGNskF8PM38u7IX2BVfjo9zuyEPHW1ShBqgyJ3yDoEO7fUioVVSYmKk1EYdIZ1M4ryA1ex8aNS48oHQR1y3Pa6MLz44vMBOQbLO4ke9fF1C8oK8+ON1nO9slYgRootBSDOcKDMvI9DmVgpjXzBCrAjAflCQvoV0+H/n81R+DuXLReJqDV63h4W1yDRCbTHzvSFMADJH4nCvw6TkVlWblAYZRCFygmh6oj8h+8idrwYg4eWE01om355/rv8msl6Bi7Lyr9NOBekybBMyCSl9PmAs6UJUmpb025YUyR9ocV0HwlPqE7xw1H/WR3dF5Nv02woACYW459uBZ/fx1ydDMd 3ui+h/zI 2iUkMpJsv7H1JsHPhWyVUkMMxrU1cTU9JsbTx0ItPPpagb3y6w3Z8hqjYW1baW6xHDb2JngLUH9K194TTacqc/9t6KVKsSSMiTSxGvEgXmD5qcdozaO844UdVi+U27vpcz3MrTw/WDI8O8S0iGZ6k/P2zd4kwSuoVBA0Li03jRHoidFa31shZWuyUI+8uuQm4DOtwbr7yQRmt/SRlf+xrX6CeVZkxQqPI7S44nA5HJ7W9Z1t05I+5Sd2FHUOwsLPEEAEq1vvNZa6E4UDxoEt2QxD5/Q== 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(). It also provides a future opportunity to batch-add the folio to the rmap using David's new batched rmap APIs. 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. Signed-off-by: Ryan Roberts Acked-by: David Hildenbrand --- mm/huge_memory.c | 59 ++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 6be1a380a298..fbf7e95ea983 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2535,15 +2535,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)); @@ -2562,28 +2563,36 @@ 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); + + VM_WARN_ON(!pte_none(ptep_get(pte + i))); + set_pte_at(mm, addr, pte + i, entry); + } + } 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, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { if (anon_exclusive) SetPageAnonExclusive(page + i); - 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); page_add_anon_rmap(page + i, vma, addr, RMAP_NONE); + VM_WARN_ON(!pte_none(ptep_get(pte + i))); } - VM_BUG_ON(!pte_none(ptep_get(pte))); - set_pte_at(mm, addr, pte, entry); - pte++; + + set_ptes(mm, haddr, pte, entry, HPAGE_PMD_NR); } - pte_unmap(pte - 1); + pte_unmap(pte); if (!pmd_migration) page_remove_rmap(page, vma, true); From patchwork Mon Dec 18 10:50:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496566 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 31970C35274 for ; Mon, 18 Dec 2023 10:51:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9E3F6B0092; Mon, 18 Dec 2023 05:51:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B25D26B0093; Mon, 18 Dec 2023 05:51:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A0B96B0095; Mon, 18 Dec 2023 05:51:28 -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 8661B6B0092 for ; Mon, 18 Dec 2023 05:51:28 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5C7C81405BD for ; Mon, 18 Dec 2023 10:51:28 +0000 (UTC) X-FDA: 81579622656.17.CB42142 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf25.hostedemail.com (Postfix) with ESMTP id 8E56CA0014 for ; Mon, 18 Dec 2023 10:51:26 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702896686; 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=HMNkpYltWDvuMPO5V4A9nNfVvksmNsk6NYV9JnDoT3k=; b=CykNu2paiXs7h6FbEO4//kpmUY84I7fMS8wAqcm2evzPa7VlCXlugtgVu6hosfhFHLAIIc JfcnVPDXbrz0AJzOoKTCXcldqH+4R4KOXjNMT1d0JbbzzCbIX7JTgHYIfasgq5Us/auZy9 loY0QfCpv7pJ7frraQ8izsBkupdLmLA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896686; a=rsa-sha256; cv=none; b=OfiTKpAOLhU6YJQTKmukACsLarba+5sPeG5LWwn8kPxJL4XO6wFo91s9LpRUVNPPtbC4vB PIGK03TZ02huR3QS9N+K/ZfPbu/tJDhpxOL3NxJUpdwExlBLcmdh4y2X+6YWctM+tc48Xb caFGHDKbw8bO+3VR9GvzQuvmuttiFk4= 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 31EB713D5; Mon, 18 Dec 2023 02:52:10 -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 3E0243F738; Mon, 18 Dec 2023 02:51:22 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 02/16] mm: Batch-copy PTE ranges during fork() Date: Mon, 18 Dec 2023 10:50:46 +0000 Message-Id: <20231218105100.172635-3-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 8E56CA0014 X-Stat-Signature: bmhubhqyfi3ut9hfuwgci6c7cpjg9taf X-HE-Tag: 1702896686-783959 X-HE-Meta: U2FsdGVkX19210kna2kUylX606Oi2jvcJJ2Ffk+/9KhD87muJykpqPaTt3kd3xpIQz/rcEHDG59UFA/GyyKzIBgUbw2cO6VlrPjxYoFq5hDdUuplFv2/CwcxeYUFqE6KU1lAF/ExYZfc4Cb3a7KC0/g07XdL1OELzmw6M2OrdvsGee8yqIfLVcFGXtv71/EBDisO2cMVLB9hRxeaagB+LhlTHrzYIYNN4qIRopBPgW+3xJWvlkQ3DptIwvsvMowMiO6XJxXrdTcNbOjmon0GABSiXF6CN66maGELOzhz3bGtwgPW8C5XvrTMqLvDLu7Y6/zhPGrSbwDVhn2m23Dp3I/RfvKM5B/rFcMLVDpzdAlUFMHTH3KD0g2OAn6xfGrIg6ETnGlN5JTupUgFlD0FHrZcnaFotFXv1AnUQdPQuO7BMo1tN9g5Ie/xbEw7w4p3vsJjZQLvnPw4Md45VdCg1yxJGJb4PNrqRBB0r9Pe7WQnr3LZAMbFyzwR9HOtXLPmGdHkLR2T/HkgukRCYxKF/F86EUlBc1dOdgohuRGLYa6wuSshJtoRDpSehBIroL2i/vZ6o6brBWwhJfkxRhQK6fwZvFsc5s2ZsXLN1MECqXSnDdIMK7YlTk05PXtNJ/SQihIDMQ+CJnyVwR8uh1BRxq72NIJWIvGcal2rOkDXtKxtVEFaMAU74wun1kBE+3FOCV8WcBY2Ny/K4NFSY+owTOdY+ahhnEQaS/mOUv39lSYiyIsXGNw27Mbt+kJ8cJ/X3Yi6CLQ40Ri/UQrrffLxdSCkW0IfwTXh8XaVvrU78bROMQ8P80UQjRQmibVPWaLL3z/ME9Wq7GhZMgCbzrUCBwIa7PpQWwcBwglHJLM1T0FHV+gc5ahtVuF2cj7g2pcaDXz5tZjdUuVUavZIbBfTsLd6udHMRysovdSP5wXzEni8Il9kCc0TM7d7uZ0BkevbWe6eJhpYTBYoPl8YaYF 8JZgeR+h aoDcj526+V6v92UGkeSQ3U47ZULxbK8vCu2HoTsmEUTzhC8O+xLEU3KmItPDZTer0edT60b4Qu52L3dYAqc1vsbf3Fmzs1bZwzTo7u0tNKskP/Maq+DhlebRWqYOMWpXYY1VuZFmaaEjIUUNsnNsW/pbl+HWJMFGMtOvxCTZCYv2YwdcRA5iqLdn89HlcUIC1OGGIaB4/UeI5oulKD4fWWMAun5KsaJ5rTQY4kbYpaE8/wWp1mrFmogj9yaU9mmm1rCgf 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: Convert copy_pte_range() to copy a batch of ptes in one go. A given batch is determined by the architecture with the new helper, pte_batch_remaining(), and maps a physically contiguous block of memory, all belonging to the same folio. A pte batch is then write-protected in one go in the parent using the new helper, ptep_set_wrprotects() and is set in one go in the child using the new helper, set_ptes_full(). The primary motivation for this change is to reduce the number of tlb maintenance operations that the arm64 backend has to perform during fork, as it is about to add transparent support for the "contiguous bit" in its ptes. By write-protecting the parent using the new ptep_set_wrprotects() (note the 's' at the end) function, the backend can avoid having to unfold contig ranges of PTEs, which is expensive, when all ptes in the range are being write-protected. Similarly, by using set_ptes_full() rather than set_pte_at() to set up ptes in the child, the backend does not need to fold a contiguous range once they are all populated - they can be initially populated as a contiguous range in the first place. This code is very performance sensitive, and a significant amount of effort has been put into not regressing performance for the order-0 folio case. By default, pte_batch_remaining() is compile constant 1, which enables the compiler to simplify the extra loops that are added for batching and produce code that is equivalent (and equally performant) as the previous implementation. This change addresses the core-mm refactoring only and a separate change will implement pte_batch_remaining(), ptep_set_wrprotects() and set_ptes_full() in the arm64 backend to realize the performance improvement as part of the work to enable contpte mappings. To ensure the arm64 is performant once implemented, this change is very careful to only call ptep_get() once per pte batch. The following microbenchmark results demonstate that there is no significant performance change after this patch. Fork is called in a tight loop in a process with 1G of populated memory and the time for the function to execute is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of order-0 folios and case where comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | Apple M2 VM | order-0 (pte-map) | order-9 (pte-map) | | fork |-------------------|-------------------| | microbench | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 1.1% | 0.0% | 1.2% | | after-change | -1.0% | 2.0% | -0.1% | 1.1% | | Ampere Altra | order-0 (pte-map) | order-9 (pte-map) | | fork |-------------------|-------------------| | microbench | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 1.0% | 0.0% | 0.1% | | after-change | -0.1% | 1.2% | -0.1% | 0.1% | Tested-by: John Hubbard Reviewed-by: Alistair Popple Signed-off-by: Ryan Roberts --- include/linux/pgtable.h | 80 +++++++++++++++++++++++++++++++++++ mm/memory.c | 92 ++++++++++++++++++++++++++--------------- 2 files changed, 139 insertions(+), 33 deletions(-) diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index af7639c3b0a3..db93fb81465a 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -205,6 +205,27 @@ static inline int pmd_young(pmd_t pmd) #define arch_flush_lazy_mmu_mode() do {} while (0) #endif +#ifndef pte_batch_remaining +/** + * pte_batch_remaining - Number of pages from addr to next batch boundary. + * @pte: Page table entry for the first page. + * @addr: Address of the first page. + * @end: Batch ceiling (e.g. end of vma). + * + * Some architectures (arm64) can efficiently modify a contiguous batch of ptes. + * In such cases, this function returns the remaining number of pages to the end + * of the current batch, as defined by addr. This can be useful when iterating + * over ptes. + * + * May be overridden by the architecture, else batch size is always 1. + */ +static inline unsigned int pte_batch_remaining(pte_t pte, unsigned long addr, + unsigned long end) +{ + return 1; +} +#endif + #ifndef set_ptes #ifndef pte_next_pfn @@ -246,6 +267,34 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr, #endif #define set_pte_at(mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1) +#ifndef set_ptes_full +/** + * set_ptes_full - Map consecutive pages to a contiguous range of addresses. + * @mm: Address space to map the pages into. + * @addr: Address to map the first page at. + * @ptep: Page table pointer for the first entry. + * @pte: Page table entry for the first page. + * @nr: Number of pages to map. + * @full: True if systematically setting all ptes and their previous values + * were known to be none (e.g. part of fork). + * + * Some architectures (arm64) can optimize the implementation if copying ptes + * batach-by-batch from the parent, where a batch is defined by + * pte_batch_remaining(). + * + * May be overridden by the architecture, else full is ignored and call is + * forwarded to set_ptes(). + * + * Context: The caller holds the page table lock. The pages all belong to the + * same folio. The PTEs are all in the same PMD. + */ +static inline void set_ptes_full(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte, unsigned int nr, int full) +{ + set_ptes(mm, addr, ptep, pte, nr); +} +#endif + #ifndef __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address, pte_t *ptep, @@ -622,6 +671,37 @@ static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addres } #endif +#ifndef ptep_set_wrprotects +struct mm_struct; +/** + * ptep_set_wrprotects - Write protect a consecutive set of pages. + * @mm: Address space that the pages are mapped into. + * @address: Address of first page to write protect. + * @ptep: Page table pointer for the first entry. + * @nr: Number of pages to write protect. + * @full: True if systematically wite protecting all ptes (e.g. part of fork). + * + * Some architectures (arm64) can optimize the implementation if + * write-protecting ptes batach-by-batch, where a batch is defined by + * pte_batch_remaining(). + * + * May be overridden by the architecture, else implemented as a loop over + * ptep_set_wrprotect(). + * + * Context: The caller holds the page table lock. The PTEs are all in the same + * PMD. + */ +static inline void ptep_set_wrprotects(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + unsigned int nr, int full) +{ + unsigned int i; + + for (i = 0; i < nr; i++, address += PAGE_SIZE, ptep++) + ptep_set_wrprotect(mm, address, ptep); +} +#endif + /* * On some architectures hardware does not set page access bit when accessing * memory page, it is responsibility of software setting this bit. It brings diff --git a/mm/memory.c b/mm/memory.c index 809746555827..111f8feeb56e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -929,42 +929,60 @@ copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma } /* - * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page - * is required to copy this pte. + * Copy set of contiguous ptes. Returns number of ptes copied if succeeded + * (always gte 1), or -EAGAIN if one preallocated page is required to copy the + * first pte. */ static inline int -copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, - pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, - struct folio **prealloc) +copy_present_ptes(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, + pte_t *dst_pte, pte_t *src_pte, pte_t pte, + unsigned long addr, unsigned long end, + int *rss, struct folio **prealloc) { struct mm_struct *src_mm = src_vma->vm_mm; unsigned long vm_flags = src_vma->vm_flags; - pte_t pte = ptep_get(src_pte); struct page *page; struct folio *folio; + int nr, i, ret; + + nr = pte_batch_remaining(pte, addr, end); page = vm_normal_page(src_vma, addr, pte); - if (page) + if (page) { folio = page_folio(page); + folio_ref_add(folio, nr); + } if (page && folio_test_anon(folio)) { - /* - * If this page may have been pinned by the parent process, - * copy the page immediately for the child so that we'll always - * guarantee the pinned page won't be randomly replaced in the - * future. - */ - folio_get(folio); - if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { - /* Page may be pinned, we have to copy. */ - folio_put(folio); - return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, - addr, rss, prealloc, page); + for (i = 0; i < nr; i++, page++) { + /* + * If this page may have been pinned by the parent + * process, copy the page immediately for the child so + * that we'll always guarantee the pinned page won't be + * randomly replaced in the future. + */ + if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { + if (i != 0) + break; + /* Page may be pinned, we have to copy. */ + folio_ref_sub(folio, nr); + ret = copy_present_page(dst_vma, src_vma, + dst_pte, src_pte, addr, + rss, prealloc, page); + return ret == 0 ? 1 : ret; + } + VM_BUG_ON(PageAnonExclusive(page)); } - rss[MM_ANONPAGES]++; + + if (unlikely(i < nr)) { + folio_ref_sub(folio, nr - i); + nr = i; + } + + rss[MM_ANONPAGES] += nr; } else if (page) { - folio_get(folio); - page_dup_file_rmap(page, false); - rss[mm_counter_file(page)]++; + for (i = 0; i < nr; i++) + page_dup_file_rmap(page + i, false); + rss[mm_counter_file(page)] += nr; } /* @@ -972,10 +990,9 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, * in the parent and the child */ if (is_cow_mapping(vm_flags) && pte_write(pte)) { - ptep_set_wrprotect(src_mm, addr, src_pte); + ptep_set_wrprotects(src_mm, addr, src_pte, nr, true); pte = pte_wrprotect(pte); } - VM_BUG_ON(page && folio_test_anon(folio) && PageAnonExclusive(page)); /* * If it's a shared mapping, mark it clean in @@ -988,8 +1005,8 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, if (!userfaultfd_wp(dst_vma)) pte = pte_clear_uffd_wp(pte); - set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); - return 0; + set_ptes_full(dst_vma->vm_mm, addr, dst_pte, pte, nr, true); + return nr; } static inline struct folio *folio_prealloc(struct mm_struct *src_mm, @@ -1030,6 +1047,7 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, int rss[NR_MM_COUNTERS]; swp_entry_t entry = (swp_entry_t){0}; struct folio *prealloc = NULL; + int nr_ptes; again: progress = 0; @@ -1060,6 +1078,8 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, arch_enter_lazy_mmu_mode(); do { + nr_ptes = 1; + /* * We are holding two locks at this point - either of them * could generate latencies in another task on another CPU. @@ -1095,16 +1115,21 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, * the now present pte. */ WARN_ON_ONCE(ret != -ENOENT); + ret = 0; } - /* copy_present_pte() will clear `*prealloc' if consumed */ - ret = copy_present_pte(dst_vma, src_vma, dst_pte, src_pte, - addr, rss, &prealloc); + /* copy_present_ptes() will clear `*prealloc' if consumed */ + nr_ptes = copy_present_ptes(dst_vma, src_vma, dst_pte, src_pte, + ptent, addr, end, rss, &prealloc); + /* * If we need a pre-allocated page for this pte, drop the * locks, allocate, and try again. */ - if (unlikely(ret == -EAGAIN)) + if (unlikely(nr_ptes == -EAGAIN)) { + ret = -EAGAIN; break; + } + if (unlikely(prealloc)) { /* * pre-alloc page cannot be reused by next time so as @@ -1115,8 +1140,9 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, folio_put(prealloc); prealloc = NULL; } - progress += 8; - } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end); + progress += 8 * nr_ptes; + } while (dst_pte += nr_ptes, src_pte += nr_ptes, + addr += PAGE_SIZE * nr_ptes, addr != end); arch_leave_lazy_mmu_mode(); pte_unmap_unlock(orig_src_pte, src_ptl); From patchwork Mon Dec 18 10:50:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496567 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 AF3F8C35274 for ; Mon, 18 Dec 2023 10:51:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BE0E6B0095; Mon, 18 Dec 2023 05:51:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 46E1E6B0096; Mon, 18 Dec 2023 05:51:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 311296B0098; Mon, 18 Dec 2023 05:51: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 216666B0095 for ; Mon, 18 Dec 2023 05:51:32 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DE22580BC4 for ; Mon, 18 Dec 2023 10:51:31 +0000 (UTC) X-FDA: 81579622782.05.44E332A Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 2EC092001E for ; Mon, 18 Dec 2023 10:51:29 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; spf=pass (imf03.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=1702896690; 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=bwKlKTVosArOXLPMMU2ssUe/7Xt/n6Zbrde3/LmyRvQ=; b=G41JSbNtY0Mqu5XVELFaSglL7HKJWqBNr3bbYngYqtNDEt/1iVwn3UO+oxlftih7lGXcLZ mn/Rn5sQM/dKh6rkRfFE7FTrc9emXD02Gi1A/JJeON7pI3wMXVF3VXT79516lQ4xkgZcL7 jGzjljzQ+cbq5rhLeBjZxPTwtnmwzb8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896690; a=rsa-sha256; cv=none; b=EYV1yXHLJNtnjj5T4R0v31/V5sdE9zR2xQxAwyiLUXFsTEpsK4FPE7pKdCU9QbwXIb/8dt 1oQj/G354ts20mqNe1R/VcvaAlg2XpYnKagPRZvh9ksP+2YDOKrCmyVvplcbaABt9Vkzfc nI50t1Jhxyv++xyL+4EqgenCySrTqRQ= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; spf=pass (imf03.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 CA9C81424; Mon, 18 Dec 2023 02:52:13 -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 EE4DF3F738; Mon, 18 Dec 2023 02:51:25 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 03/16] mm: Batch-clear PTE ranges during zap_pte_range() Date: Mon, 18 Dec 2023 10:50:47 +0000 Message-Id: <20231218105100.172635-4-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2EC092001E X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: e3x6dwmbqthcmsnbtgs43rwwx7qad3ya X-HE-Tag: 1702896689-912097 X-HE-Meta: U2FsdGVkX18xYSY+AXAACwX3tC3PuYzolScx5ufr1z3gP2u7n4B5UC0NaJ8XZhOrgthXjBjCM46tj145t3o4yhFQYV/kh5LE0WMLRj6Tuwltky8n7fsn1r9vvELYJlM61obU0sSOwLfU5JH4MvFNpCC+lqlcOPryNZteHBV9xt0wUCLOQI/xeewugvmr5pC640LdXvs5Tz3S1iatbxOJwaf/nUkLePr8gGiL5AClIhUQ3NS0P7Y8BxRqLTRTfoomfLDdq0AWUNCWTfxEr0HDn5CdPfujJAfl2CsCiFU6bQ+EcTefkP66ThegDYo3Mm/vfoRnmAxQ87bv+Mwgzv7o9jZXpGXkamoJg9jQI+G2QyK/Fgm1scs1D2u6LmknWxNsmfCJ6X9IBGEV/270L3803ZlINNWLHCH07/0v6hjOP46ICS9FrgDpGyV4C4fgJTNIfVrzAtJEs/KU3w7gIlrpKV54xXxfBxpSDJw26d1S3P9StzOKosrvU7vKwWXhRX1zrnkDAjoU0KTLShd76rKsmbUgAnOWcelLkA6LdFFUeiKYFlaDHnJMOwAQG24nmuH3Ah4Gr28XhsFoUko89LJAUeOq4to3HQ8eoPPbRvOQQnnSYPM9CD9xrG73AyN5SaFOJf25tjs3ajhcqn/fXsH0GhKTjyr15u1eCg9cNCNJq6BecpOnbF8hH0bzRGpKrn196kfWH6m4ZJ1hCVl0eoW+y7NfaWTwur1SAP8ET9nMT4C0nKf//H8VGhpDFwJRWs5SFLGWM/rMx2PfFzJmDDLXD1FqPXOFW12Pw/JH3mrYsuz7bTVByo0Sora7DlRvu4Qt45anH38JZxZis82ku5FUQQ6VhganT2XZZyWovMSUty5s3xXyYE2XtZNtSzmxSbuwBAr0iF+42JMQsh/860jpZBIdwYQugn0NHxz9+4jxkBkjjJ2Leepm7NiVUgj0k2q9E/MuWViNEqB0qC6dFu7 5LbFIAT3 emAu728hnOmrmRSajJp4x88T2oKSEv2FANcm3mi3qsqYkZrkVw/aMwoKHD+vCH+GMVzgRxsbPl6+TzoZnD6O6A5mqrM9MwzvO287tRf4W9Y6EkUrniZzNryGtKnrhIJtOsh+++tNEmwTDZxb1t+1Nq7MW4MZWRe55+WXxPTcbkMECOIPWAEzYC7KFjkk5hPTk4Lpafe5P89mEUhqUO/seK2Aszh9B/jbZ2aOSmDBqqFo9tcuYAJGRbYhKuo+EnTl9bKS6PXUydWHKBp7odLQxQhEVaa3WTzsJfnMR48NWKQSDHao= 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: Convert zap_pte_range() to copy a batch of ptes in one go. A given batch is determined by the architecture (see pte_batch_remaining()), and maps a physically contiguous block of memory, all belonging to the same folio. A pte batch is cleared using the new helper, clear_ptes(). The primary motivation for this change is to reduce the number of tlb maintenance operations that the arm64 backend has to perform during exit and other syscalls that cause zap_pte_range() (e.g. munmap, madvise(DONTNEED), etc.), as it is about to add transparent support for the "contiguous bit" in its ptes. By clearing ptes using the new clear_ptes() API, the backend doesn't have to perform an expensive unfold operation when a PTE being cleared is part of a contpte block. Instead it can just clear the whole block immediately. This code is very performance sensitive, and a significant amount of effort has been put into not regressing performance for the order-0 folio case. By default, pte_batch_remaining() always returns 1, which enables the compiler to simplify the extra loops that are added for batching and produce code that is equivalent (and equally performant) as the previous implementation. This change addresses the core-mm refactoring only, and introduces clear_ptes() with a default implementation that calls ptep_get_and_clear_full() for each pte in the range. Note that this API returns the pte at the beginning of the batch, but with the dirty and young bits set if ANY of the ptes in the cleared batch had those bits set; this information is applied to the folio by the core-mm. Given the batch is guaranteed to cover only a single folio, collapsing this state does not lose any useful information. A separate change will implement clear_ptes() in the arm64 backend to realize the performance improvement as part of the work to enable contpte mappings. The following microbenchmark results demonstate that there is no madvise(dontneed) performance regression (and actually an improvement in some cases) after this patch. madvise(dontneed) is called for each page of a 1G populated mapping and the total time is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of order-0 folios and case where comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | Apple M2 VM | order-0 (pte-map) | order-9 (pte-map) | | dontneed |-------------------|-------------------| | microbench | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 7.5% | 0.0% | 7.9% | | after-change | -9.6% | 3.1% | -4.9% | 8.1% | | Ampere Altra | order-0 (pte-map) | order-9 (pte-map) | | dontneed |-------------------|-------------------| | microbench | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 0.1% | 0.0% | 0.0% | | after-change | -0.2% | 0.1% | -0.1% | 0.0% | The following microbenchmark results demonstate that there is no munmap performance regression (and actually an improvement in some cases) after this patch. munmap is called for a 1G populated mapping and the time to execute the function is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of order-0 folios and case where comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | Apple M2 VM | order-0 (pte-map) | order-9 (pte-map) | | munmap |-------------------|-------------------| | microbench | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 3.8% | 0.0% | 6.4% | | after-change | -1.9% | 0.2% | -4.7% | 0.8% | | Ampere Altra | order-0 (pte-map) | order-9 (pte-map) | | munmap |-------------------|-------------------| | microbench | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 0.9% | 0.0% | 0.1% | | after-change | -0.2% | 0.6% | -3.2% | 0.2% | Tested-by: John Hubbard Signed-off-by: Ryan Roberts Reviewed-by: Alistair Popple --- include/asm-generic/tlb.h | 11 +++++++ include/linux/pgtable.h | 43 ++++++++++++++++++++++++++ mm/memory.c | 64 ++++++++++++++++++++++++++++----------- mm/mmu_gather.c | 15 +++++++++ 4 files changed, 115 insertions(+), 18 deletions(-) diff --git a/include/asm-generic/tlb.h b/include/asm-generic/tlb.h index 129a3a759976..1b25929d2000 100644 --- a/include/asm-generic/tlb.h +++ b/include/asm-generic/tlb.h @@ -75,6 +75,10 @@ * boolean indicating if the queue is (now) full and a call to * tlb_flush_mmu() is required. * + * tlb_reserve_space() attempts to preallocate space for nr pages and returns + * the minimum garanteed number of pages that can be queued without overflow, + * which may be more or less than requested. + * * tlb_remove_page() and tlb_remove_page_size() imply the call to * tlb_flush_mmu() when required and has no return value. * @@ -263,6 +267,7 @@ struct mmu_gather_batch { extern bool __tlb_remove_page_size(struct mmu_gather *tlb, struct encoded_page *page, int page_size); +extern unsigned int tlb_reserve_space(struct mmu_gather *tlb, unsigned int nr); #ifdef CONFIG_SMP /* @@ -273,6 +278,12 @@ extern bool __tlb_remove_page_size(struct mmu_gather *tlb, extern void tlb_flush_rmaps(struct mmu_gather *tlb, struct vm_area_struct *vma); #endif +#else +static inline unsigned int tlb_reserve_space(struct mmu_gather *tlb, + unsigned int nr) +{ + return 1; +} #endif /* diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h index db93fb81465a..170925379534 100644 --- a/include/linux/pgtable.h +++ b/include/linux/pgtable.h @@ -601,6 +601,49 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm, } #endif +#ifndef clear_ptes +struct mm_struct; +/** + * clear_ptes - Clear a consecutive range of ptes and return the previous value. + * @mm: Address space that the ptes map. + * @address: Address corresponding to the first pte to clear. + * @ptep: Page table pointer for the first entry. + * @nr: Number of ptes to clear. + * @full: True if systematically clearing all ptes for the address space. + * + * A batched version of ptep_get_and_clear_full(), which returns the old pte + * value for the first pte in the range, but with young and/or dirty set if any + * of the ptes in the range were young or dirty. + * + * May be overridden by the architecture, else implemented as a loop over + * ptep_get_and_clear_full(). + * + * Context: The caller holds the page table lock. The PTEs are all in the same + * PMD. + */ +static inline pte_t clear_ptes(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + unsigned int nr, int full) +{ + unsigned int i; + pte_t pte; + pte_t orig_pte = ptep_get_and_clear_full(mm, address, ptep, full); + + for (i = 1; i < nr; i++) { + address += PAGE_SIZE; + ptep++; + pte = ptep_get_and_clear_full(mm, address, ptep, full); + + if (pte_dirty(pte)) + orig_pte = pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte = pte_mkyoung(orig_pte); + } + + return orig_pte; +} +#endif /* * If two threads concurrently fault at the same page, the thread that diff --git a/mm/memory.c b/mm/memory.c index 111f8feeb56e..81b023cf3182 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1447,6 +1447,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, pte_t *start_pte; pte_t *pte; swp_entry_t entry; + int nr; tlb_change_page_size(tlb, PAGE_SIZE); init_rss_vec(rss); @@ -1459,6 +1460,9 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, do { pte_t ptent = ptep_get(pte); struct page *page; + int i; + + nr = 1; if (pte_none(ptent)) continue; @@ -1467,43 +1471,67 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, break; if (pte_present(ptent)) { - unsigned int delay_rmap; + unsigned int delay_rmap = 0; + struct folio *folio; + bool full = false; + + /* + * tlb_gather always has at least one slot so avoid call + * to tlb_reserve_space() when pte_batch_remaining() is + * a compile-time constant 1 (default). + */ + nr = pte_batch_remaining(ptent, addr, end); + if (unlikely(nr > 1)) + nr = min_t(int, nr, tlb_reserve_space(tlb, nr)); page = vm_normal_page(vma, addr, ptent); if (unlikely(!should_zap_page(details, page))) continue; - ptent = ptep_get_and_clear_full(mm, addr, pte, - tlb->fullmm); + ptent = clear_ptes(mm, addr, pte, nr, tlb->fullmm); arch_check_zapped_pte(vma, ptent); - tlb_remove_tlb_entry(tlb, pte, addr); - zap_install_uffd_wp_if_needed(vma, addr, pte, details, - ptent); + + for (i = 0; i < nr; i++) { + unsigned long subaddr = addr + PAGE_SIZE * i; + + tlb_remove_tlb_entry(tlb, &pte[i], subaddr); + zap_install_uffd_wp_if_needed(vma, subaddr, + &pte[i], details, ptent); + } if (unlikely(!page)) { ksm_might_unmap_zero_page(mm, ptent); continue; } - delay_rmap = 0; - if (!PageAnon(page)) { + folio = page_folio(page); + if (!folio_test_anon(folio)) { if (pte_dirty(ptent)) { - set_page_dirty(page); + folio_mark_dirty(folio); if (tlb_delay_rmap(tlb)) { delay_rmap = 1; force_flush = 1; } } if (pte_young(ptent) && likely(vma_has_recency(vma))) - mark_page_accessed(page); + folio_mark_accessed(folio); } - rss[mm_counter(page)]--; - if (!delay_rmap) { - page_remove_rmap(page, vma, false); - if (unlikely(page_mapcount(page) < 0)) - print_bad_pte(vma, addr, ptent, page); + rss[mm_counter(page)] -= nr; + for (i = 0; i < nr; i++, page++) { + if (!delay_rmap) { + page_remove_rmap(page, vma, false); + if (unlikely(page_mapcount(page) < 0)) + print_bad_pte(vma, addr, ptent, page); + } + + /* + * nr calculated based on available space, so + * can only be full on final iteration. + */ + VM_WARN_ON(full); + full = __tlb_remove_page(tlb, page, delay_rmap); } - if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { + if (unlikely(full)) { force_flush = 1; - addr += PAGE_SIZE; + addr += PAGE_SIZE * nr; break; } continue; @@ -1557,7 +1585,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, } pte_clear_not_present_full(mm, addr, pte, tlb->fullmm); zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); - } while (pte++, addr += PAGE_SIZE, addr != end); + } while (pte += nr, addr += PAGE_SIZE * nr, addr != end); add_mm_rss_vec(mm, rss); arch_leave_lazy_mmu_mode(); diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c index 4f559f4ddd21..39725756e6bf 100644 --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -47,6 +47,21 @@ static bool tlb_next_batch(struct mmu_gather *tlb) return true; } +unsigned int tlb_reserve_space(struct mmu_gather *tlb, unsigned int nr) +{ + struct mmu_gather_batch *batch = tlb->active; + unsigned int nr_alloc = batch->max - batch->nr; + + while (nr_alloc < nr) { + if (!tlb_next_batch(tlb)) + break; + nr_alloc += tlb->active->max; + } + + tlb->active = batch; + return nr_alloc; +} + #ifdef CONFIG_SMP static void tlb_flush_rmap_batch(struct mmu_gather_batch *batch, struct vm_area_struct *vma) { From patchwork Mon Dec 18 10:50:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496568 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 3589BC35274 for ; Mon, 18 Dec 2023 10:51:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C87D46B0098; Mon, 18 Dec 2023 05:51:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C10F76B0099; Mon, 18 Dec 2023 05:51:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AD9EE6B009A; Mon, 18 Dec 2023 05:51:35 -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 99A836B0098 for ; Mon, 18 Dec 2023 05:51:35 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6C9611A012E for ; Mon, 18 Dec 2023 10:51:35 +0000 (UTC) X-FDA: 81579622950.21.50B71A9 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id B7C421C0027 for ; Mon, 18 Dec 2023 10:51:33 +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=1702896693; 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=Tgbl19Y9zwf6Mq8Tb/Q5BDwkfU5WzhnvG6gc2ibDGlc=; b=B/eCowDzTEBgz5W1mIygAihsGa4+2c6foi3DAk+3uLdI5S9V1/vHlhya17g6qSkuLPlDbi uptFneSPKoHXkm0jaJagBmU4vaYoq3z6QbaaNoJN6WttjWLs/uCo4jgMjlBpP7LlXFq7Fc ZfhUJU3wB3I/12U9pguT22VwuaSyfqQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896693; a=rsa-sha256; cv=none; b=iIPDOmdy6symUXXnmYewH03Xwtu3A9wQSirM14l9d+kqWKQms9qmuMlaJrSnH6DQb9Qrmv DmL1/Iw4b08Xa8Lbyl3xpLQQTRYhz8bycyHAn6l1KXcJIk/fxpixHI6qLheteM88IsR2Vi zIVpXCXRvaTjyWXuKx++YJ8RM9CcWOg= 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 6C3A71FB; Mon, 18 Dec 2023 02:52: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 936353F738; Mon, 18 Dec 2023 02:51:29 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 04/16] arm64/mm: set_pte(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:48 +0000 Message-Id: <20231218105100.172635-5-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: B7C421C0027 X-Rspam-User: X-Stat-Signature: jugtr9cumcqosipxjukcy4ekamrw53i7 X-Rspamd-Server: rspam03 X-HE-Tag: 1702896693-766894 X-HE-Meta: U2FsdGVkX1+wPT0yOkssHfmL1vCWhO/EXy4VHAjUORqT1qhR1jpcPqP3vzymG4rTG1I5r+8KxabEq7LcnFtsviuJc3q/58ukGF+vAffaDsyAkCxyspb33BeBM2Wbms3ekgWnLUkhNPDp4B+gIHYQa2RXZOvYSLYUATS5cUiIYgrCh1l3i91Y/vnooqeFEGkLMdBx8VXOxbH10hL2uGSPSx3u5sbm37a69xXCFGSezXt1TfEpZvV28d9JcHYjx0M6p7QKjSm9EIuU4/Eq/G0weWyaBLWxh3UaKcchQpbu5Rw2s3o7uctNBm7RPOckRCIYjSJAedOVlaHEPyFgpnwLkw2T4q9Vhgt9Ls1ZlZbv3GXuQVKH/hLnOfckK8ZnFOxhuHSYVhSZQ54hsnUZ6r9A71uVx3fD28Qvx4tYCBPegsh5jJqxSKB2pGZaaVX75XCPgBsKAW94zdFAWlXsiErpmHb3iT8slSlyIIRTpK5uFP3IUUTv9FDB2Dg9IIWT/sfNJOCB1SSsrSFIGlXWoCZimH6dBULCfTIe/0ygAslxhSO7z3j+O0mRwyjNSFiM4vErlmMi4o61vCGO80QF1CIqLZsm90Ob8b63G2N/EGe/K+19x3qjuVBf3c2hhLjyisJFdszbSsg/fmCQ0kMqFNR17FBPf9Fg5ctbgbIMwQSoL2d4T0rBrYPjCH4FbSrlr5bhukovIaa1saYiCS8kvWuvw2/yt8ieThdX5GtJ5nwojCiZBmBWKkdGRzg+g5XNgBDdYYCvifeMt74/HMZ96eAc4oWsSWcOmlOm7VCt0OQciCKw0qBjWpfPZ5O5XMIqWJD6hO9rm/2DVchPujh5yNL15jCksLc9IVM5jhLIHKL/FLz2z9NplSyfnMU79S3u6cyqs4gQIvLX7mup4XMoOWxWS0MXoqz2YI4v/dZ4rD3/VP3as7GcfhcQUf2bvOlyOpLWaq9M+7Z0/cHG2msZVka r6/iL/7K rbubCiW/B26D4exfxv3k0k+oxT3gBjI2Zxmgt3h+lw9uyS+S7zWxBvaifuWBBAdVCKuzbMxvPQRLD6c/+8x2SXPiv/qxkOfshhD137KX6gAyKMhuO7co79lTwDYjQRxuqGcWRWO/IJkxJZRtxyd7rZyB4QuOu/rZ9YssnbqUiXg5dHNKJsx+ERoOXo7D00OzW02mm6lWiDOcRdwLg6vGEpuXZCKi0fuFMr2mNAhpoAshuuEXUQzBKJlevNrYftjdgIWW0vHW68nVA55smLBDftyW3Hz/FrKhEN8cF8wvsutQRQ1E= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 12 ++++++++---- arch/arm64/kernel/efi.c | 2 +- arch/arm64/mm/fixmap.c | 2 +- arch/arm64/mm/kasan_init.c | 4 ++-- arch/arm64/mm/mmu.c | 2 +- arch/arm64/mm/pageattr.c | 2 +- arch/arm64/mm/trans_pgd.c | 4 ++-- 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index b19a8aee684c..650d4f4bb6dc 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))) /* @@ -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); @@ -350,7 +351,7 @@ static inline void set_ptes(struct mm_struct *mm, for (;;) { __check_safe_pte_update(mm, ptep, pte); - set_pte(ptep, pte); + __set_pte(ptep, pte); if (--nr == 0) break; ptep++; @@ -534,7 +535,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, @@ -1118,6 +1119,9 @@ 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 set_pte __set_pte + #endif /* !__ASSEMBLY__ */ #endif /* __ASM_PGTABLE_H */ diff --git a/arch/arm64/kernel/efi.c b/arch/arm64/kernel/efi.c index 0228001347be..44288a12fc6c 100644 --- a/arch/arm64/kernel/efi.c +++ b/arch/arm64/kernel/efi.c @@ -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/mm/fixmap.c b/arch/arm64/mm/fixmap.c index c0a3301203bd..51cd4501816d 100644 --- a/arch/arm64/mm/fixmap.c +++ b/arch/arm64/mm/fixmap.c @@ -121,7 +121,7 @@ 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); flush_tlb_kernel_range(addr, addr+PAGE_SIZE); diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c index 555285ebd5af..5eade712e9e5 100644 --- a/arch/arm64/mm/kasan_init.c +++ b/arch/arm64/mm/kasan_init.c @@ -112,7 +112,7 @@ 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)); + __set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL)); } while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep))); } @@ -266,7 +266,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 15f6347d23b6..e884279b268e 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -178,7 +178,7 @@ static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end, do { pte_t old_pte = READ_ONCE(*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 diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c index 924843f1f661..a7996d8edf0a 100644 --- a/arch/arm64/mm/pageattr.c +++ b/arch/arm64/mm/pageattr.c @@ -41,7 +41,7 @@ static int change_page_range(pte_t *ptep, unsigned long addr, void *data) 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; } diff --git a/arch/arm64/mm/trans_pgd.c b/arch/arm64/mm/trans_pgd.c index 7b14df3c6477..230b607cf881 100644 --- a/arch/arm64/mm/trans_pgd.c +++ b/arch/arm64/mm/trans_pgd.c @@ -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 Mon Dec 18 10:50:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496569 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 DAF6FC35274 for ; Mon, 18 Dec 2023 10:51:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F4136B009A; Mon, 18 Dec 2023 05:51:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C9886B009B; Mon, 18 Dec 2023 05:51:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66BAA6B009C; Mon, 18 Dec 2023 05:51:39 -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 56E2E6B009A for ; Mon, 18 Dec 2023 05:51:39 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2BC47C1112 for ; Mon, 18 Dec 2023 10:51:39 +0000 (UTC) X-FDA: 81579623118.13.1AB41E3 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf28.hostedemail.com (Postfix) with ESMTP id 7DD5EC001F for ; Mon, 18 Dec 2023 10:51:37 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; 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; 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=1702896697; 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=TnFSlpEh5uzhY/eJyrzYI0R1Q6mihQVRrU2NPpRvEVc=; b=Re7JKpSJSDYo9TvH5ODiCh/xUsRY8ZwGXdZu5uSZw1HvG/lIBuEJLv65bNdBEwBv4oYATy tj1La6Jo4daFI9SQxLsyS8uGrd48909R9Y/3WrRAerCKK7EPNNx9zgPyCJu5gEYG98cGkI H1FwfJNPppv0Wk/eICC9K6hKRYR5nPk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; 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; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896697; a=rsa-sha256; cv=none; b=yXdtRAYpDiPD0eAcZZf0xoDmcswIevt6klTyd3qilivppIe/LFX9qxPVCWBp3yI46XffTb 9Z3pgaQ7SIOE5Q0dqqOLQe1H9xD/ZSW319vK62Drj2/K0yg7mzrhgiz/arcvvXRoJyVfoJ w0aPkQuyMFb/Yv0I6EbUR+wr4ae1ip0= 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 13D8C2F4; Mon, 18 Dec 2023 02:52: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 346023F738; Mon, 18 Dec 2023 02:51:33 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 05/16] arm64/mm: set_ptes()/set_pte_at(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:49 +0000 Message-Id: <20231218105100.172635-6-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7DD5EC001F X-Rspam-User: X-Stat-Signature: 3rper79e1tgmm9fa1pxjehkkqxbxoxae X-Rspamd-Server: rspam01 X-HE-Tag: 1702896697-86516 X-HE-Meta: U2FsdGVkX1+ybNkzwlqy0FR9/mo1Qi8SFdd34MRkWTrL27SZkqQplC2GmEL7MU8d3OtCCHHQOz2nm2ffAg19ms5JRODQ/Eu4UcSflh55PO+P56DKULdPSl54BCXZTmYa/Dg4DJ6YQk2Lvv8LU9/pKy/q2iiRdtnRjaf1P4KFLwHBjAj56HTXhU8+y7wrk9WyM0c9+LYs8XfV5cbkXRoU4Ke8g1BgoDtDJxw0n+vtkoKVbqLgBfYNihrWsbZbeBOuTf850TlGa/bVH6BUG9ZCWPtIGWspK9z3+z4G82YsvOFrNWCIJPXPM6w3ByL+jhWJqcMLj8D27e+QpFSW/6OqGCgaEGJVmV5SEGjlRsbCUv8+niAbrD1iDxntdZIe555UKajkZQ1zHlsRyEGRQYLJDYC5EIS7An5bVw8IIRv0uF50W5ztZAYHUtw/hu8CpXVEEm0GZreXEaaGTlBnCtGwu97MHFBFcbnyQvrZyjJxaEi2COGI+0sjJ+C2a04U4NgGbKeCZRaJWH0ERTANfYlHYv5UZ5zFP+MNsmvg0qO3AOQyGSIVSitKP0DB15ZaqADPnbNrlq+ge+HlHd4Lkc40gx/JNcCTT1zgojyJ+X9eLuiKxtFw8I43D7TjlVqMd5z30KgznfTVUmS3yJgScO137J2y+O2YVuK2gowA1buEmYomw+OVGCgWGBvyDSGBI4m6biamfradEhsXVjwVWKvB/KTmDXCpZRSp4kxfKkAEKZFI7HIbhVbbpLefzxKrk62CAvMI4QotoIABeBVYDas8xUV7Y0OaQygLt3v6xFnrIWLMCsNBr6fQ6fhVaL2gMnLZNMgR1KTyeJn2r0b3g7MdHEGB2bUGn7RR6qodS9Olmps3ttG3ty+x8Qu5K4DVl8p4D1pG58+evAL1kxqwKetBmRbSumOdiya70u2pS2Om4Bx6UnTpNjncWr1bt2Tr+JyH6XfLWCwzh++df2oHI2v IrZggN+L UICc/sKxHqmF6rFrwzTqDuRe0jrSmW1dJ8UdFG/kyQXVG55dvvpG5ckpsPR/dfDoVnUO9PIBgd94xwvyl1E6zi+sceRh0qZWfbEt7EJBy1Ix+RJOotsRPt5mes0mAmaNb7BUg6b8nVU8IoNUrq8W53j16iOsgKi4QP2o/lxvZIqbu78h/i56u9WghnSCGhu9kScID82cZ0YwIXAXB7A3NZC6/2hWknLwbo3InWTzKLNUH3xidO5uC2DGFtoNq6ZLD++GJffvempsc11/6VcJMbmCnwAXFLYmEh25owFMkz8QKWYc= 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. set_pte_at() is a core macro that forwards to set_ptes() (with nr=1). Instead of creating a __set_pte_at() internal macro, convert all arch users to use set_ptes()/__set_ptes() directly, as appropriate. Callers in hugetlb may benefit from calling __set_ptes() once for their whole range rather than managing their own loop. This is left for future improvement. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 10 +++++----- 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, 13 insertions(+), 13 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 650d4f4bb6dc..323ec91add60 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -342,9 +342,9 @@ static inline void __sync_cache_and_tags(pte_t pte, unsigned int nr_pages) mte_sync_tags(pte, nr_pages); } -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); @@ -358,7 +358,6 @@ static inline void set_ptes(struct mm_struct *mm, pte_val(pte) += PAGE_SIZE; } } -#define set_ptes set_ptes /* * Huge pte definitions. @@ -1067,7 +1066,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, @@ -1121,6 +1120,7 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, pte_t old_pte, pte_t new_pte); #define set_pte __set_pte +#define set_ptes __set_ptes #endif /* !__ASSEMBLY__ */ diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index a41ef3213e1e..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_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..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_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 460d799e1296..a287c1dea871 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 f5aae342632c..741cb53672fd 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -254,12 +254,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; } @@ -270,7 +270,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, @@ -478,7 +478,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; } @@ -507,7 +507,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 Mon Dec 18 10:50:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496570 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 4AC6DC35274 for ; Mon, 18 Dec 2023 10:51:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D49E56B009C; Mon, 18 Dec 2023 05:51:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CFCD16B009D; Mon, 18 Dec 2023 05:51:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9B2E6B009E; Mon, 18 Dec 2023 05:51:42 -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 A56526B009C for ; Mon, 18 Dec 2023 05:51:42 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 84E581A0D80 for ; Mon, 18 Dec 2023 10:51:42 +0000 (UTC) X-FDA: 81579623244.26.040A893 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 09E411C0016 for ; Mon, 18 Dec 2023 10:51:40 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702896701; 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=43xS6Y4SE6bF0+VyPS5TuDh+DNzmqC+J1CqyywgWfDk=; b=LTZggdVw26EA8tuNQ+/gQioWxVFPS+LpEHrUfg/b2AKpTz9e2G9isab/ZJwjqOAl7b7TQH R9FS25eJqAVcGYa4fDa0b9VFyJskUprOAfQR9FESX3VtbvSM/ehjB7ieg6Uxg0b8yo363t nxeTlT9Et6gJPDy8+8XloUvfZEkLEyo= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896701; a=rsa-sha256; cv=none; b=PlkvALqLhP5fxlj4jCfTPYj6xR6AIPK+lgMlKZ7xXznZsMQllX6g6y+VcK4AXvGjeBkaJd G4Tuqjx5tQNapvlAuoZrK6hGd68sUd7swPteDw6WGJetdbHbYAlcwL+Xo1y6RJW9tlCmsn z0lO4bRr9BNLfQvBUlU8RskjdcebKc8= 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 A9CE72F4; Mon, 18 Dec 2023 02:52: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 D00713F738; Mon, 18 Dec 2023 02:51:36 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 06/16] arm64/mm: pte_clear(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:50 +0000 Message-Id: <20231218105100.172635-7-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 09E411C0016 X-Stat-Signature: 8zmn4w6cgfr93si84n1oumcbfaeb8of1 X-HE-Tag: 1702896700-379949 X-HE-Meta: U2FsdGVkX1/Aqck/UwU4BhFV3u5NFg7FYsmwVSaCqQMilaIrBXY1dL1pym3E1C3UJ7P4lQYC/IvsjnLHncMfW6B2/PBjH1i5KdJ76GSke6TurTXvsuilkPd+y3YEY5zR2iORqgklCefSlDoIFIYX+gCS9HVHk6GGjhKNvLZ68E+SCWNJ6lC1aGsIXW+aWvLYyzfaKt01LfF24lvCPHrLUVAcCcpt9W3+xmeaKHRZjw4dkDeE5f4GL/IKT8F7tozB4OX3dh5j6zzIebPGitqFbza7Fh85m2JbJTmU3j28zXzCE8NIWRSVzv7wuSIkzb8ejewpRIMeBwCBWNENfjwIREm1/8S1RByggT+XQmbSfZQSKgQlkqjtXbOzATJs7NIDsepHTq5lb1CQXZsLj6++x1OyDCEt+wnZSGsC2wvjKDpg1B3JAvaFqqa7WDwRDfTnCWGDDtlLJxyZMQ8r2PieyQso+20JctClJejD6fKzzN5Z9PB8tioslbUTXwLxeK1B0n2Z6bkbeeys+GII7ImdtWeeDBmftydNwa3T5venaNMQwrckTYSYbIPhFKuFYZFvA6y41yKGfl9sUwn3+cmV9E3aW86fAzTX7uhD9LDNPnGUPn8SD5YLjVypmvNzueQIN7WD1tsnKz+u/rhgTJAX7cjpaTXDhskkgplRKh7HdAU6Q10dd0y5CifZYBDKW8GXijK3YJyCnp++ih+hnIXxRtiklMgzNILgNjjyf39pk9g18BQas+4BrZPxpLDtr6bNTMdgQs88nrGVXl/ZubGB8fnKmlp0YhZZUNGM6hDxKX+fMCIB0+V0CquIvgd58VU7UXC/OZMv5kgumKLysRf5Uhxo2E2gDGYNU4NQQ8KPL0FPRNh02GSayw6UnQEw9pt0QCquWmPzcFafp3txasV9mgADeVGEGSFzqBZzB2JDCAKR29OVN/UmEp5PPqDA9ocm6vzHLqCaOdI6uO0Bctc y4w== 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 3 ++- arch/arm64/mm/fixmap.c | 2 +- arch/arm64/mm/hugetlbpage.c | 2 +- arch/arm64/mm/mmu.c | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 323ec91add60..1464e990580a 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -93,7 +93,7 @@ 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) \ +#define __pte_clear(mm, addr, ptep) \ __set_pte(ptep, __pte(0)) #define pte_page(pte) (pfn_to_page(pte_pfn(pte))) @@ -1121,6 +1121,7 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, #define set_pte __set_pte #define set_ptes __set_ptes +#define pte_clear __pte_clear #endif /* !__ASSEMBLY__ */ diff --git a/arch/arm64/mm/fixmap.c b/arch/arm64/mm/fixmap.c index 51cd4501816d..bfc02568805a 100644 --- a/arch/arm64/mm/fixmap.c +++ b/arch/arm64/mm/fixmap.c @@ -123,7 +123,7 @@ void __set_fixmap(enum fixed_addresses idx, if (pgprot_val(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 741cb53672fd..510b2d4b89a9 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -400,7 +400,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, diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index e884279b268e..080e9b50f595 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -859,7 +859,7 @@ static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr, 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), From patchwork Mon Dec 18 10:50:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496571 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 1382CC35274 for ; Mon, 18 Dec 2023 10:51:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB24B6B009E; Mon, 18 Dec 2023 05:51:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A61C36B00A0; Mon, 18 Dec 2023 05:51:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92ACE6B00A1; Mon, 18 Dec 2023 05:51:46 -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 7FB586B009E for ; Mon, 18 Dec 2023 05:51:46 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5429AA25FF for ; Mon, 18 Dec 2023 10:51:46 +0000 (UTC) X-FDA: 81579623412.11.DA2E178 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 8CD1F18001D for ; Mon, 18 Dec 2023 10:51:44 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.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=1702896704; 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=p8TO3jSXlQIO2M/vTLXz8jOzA91p4UDOv06l4gwoTwE=; b=1mBXTKrwOnLr9oeqU1X+THwxCc3BljS810Eeza/mCP5Ag1AQavUwkUru8r3ufealpevROJ /w0J/IGQrNR8oh6EFNy8oNksbIO0KqI/6qX/2RnjxMF6QjJWWyrF9psL9tMm8168FQU9Dx u0SpAlivcXECKtb94NdFFEcMloBmpMk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.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=1702896704; a=rsa-sha256; cv=none; b=2gZHQeyfIY3qPQ/AbQ0+jvifWcEIeV9nWuRlLALLAcQ/LhwaHt69E58rubl8tz/QhbaYNq TNaN7bO230m+C1pnxehQJ14dGJqp2o8svSgCbmTuR4qlsxJbWqts4UpErOGW2MfX5DQbXJ wr1m24rRkIm27pEVK/jvmEoI6cxB6co= 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 4AD001FB; Mon, 18 Dec 2023 02:52: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 7132F3F738; Mon, 18 Dec 2023 02:51:40 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 07/16] arm64/mm: ptep_get_and_clear(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:51 +0000 Message-Id: <20231218105100.172635-8-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 8CD1F18001D X-Stat-Signature: ieczexd7trju8rboui1k9p46d97n6c31 X-Rspam-User: X-HE-Tag: 1702896704-859837 X-HE-Meta: U2FsdGVkX1/IuFmhqg9+nO5D78FGmq2g0gG8f1vv06Wgg2b7comNOmgnGiiPosWORG//XK6b7rvBtwCghbExoxoqmixt0rEFb28+X3+n6/X9IjEBUX4X/oVklODD6BEOpYufJaL3S1eFFpkHVpeY+LM9QAQK1O8krZNbeThGkArmYr1ZcSb8EvSM0yESDl4UEgMzxsMGuUMcMjRMlXXrbiRw1QL9mCRgoYLhxiTkffDoq2GWau9hBGIraVW2genITc7TNyD49++KKQeLVsQPWbUdLDOGwki02TB2paikcOZ6c40wuDhrQaQw0VA/FC2VPsS7Fu8EB8MnJU09cnfWgqWxP9z3InOG2g2VQpjXH82LfVHQiaMDMjEYWWoEkgqjMM5h9NiRTzYjiYJ2QwoZqYAfXiXAR2NyNHT896dGOHukhacdzjwKgq7w3NqjKjLlo8k487xDVuCjiFbKwXoZhRsEwE9LTmxTV/1xqWRjIpWyadkiQxDU8O+TX+Oqe8sj2g4SbUbCAFV0vWpTLVqDItTHkJRJ1yYKJrj6l9rZzSgJ8IAghrn1xzcB+6v4U9tHECUZDRQqzt8md2ZtJojTS3wePi4NAlqHj4udRDMezfC0Hu8DZ5Z8AjEkRCY/dgQZNJ75BdrCCVRvtDvOxD1m17k0PRClvGh6EJWA3rRj5MiIdc9n3rgbnfizTsM35S2U6eNzJ0i4BGQK3xCknFghiAFIX+KngDDrEsYNHXKLiNzpSdKomBbmq4hC49WsT2qFz4QkMXy1bcvfPqMBof7B4BUP7GRkzOOYrmq0vXHTKtkPrv7BbWfHrsXMuKLbHvhkSWIurWGd9iiL8uLJSNiq8OVLbJM2BJiuBg4t7TgHXc0mCGFZubL4zGvlP5opxIztGQ54LBL33tmTY6knaE4p2Nr1A0K+SiylIt5kb3irW2kDgT60w0SlWaUPYkZanEL/01MJQ1n6+7TJgeKBJzn KlJuX9BY cAje383hdkwm1Xtu2Bhnc1goc7F79TOW4v1r44/+4Ns9tM7cyo7q08ZXWlODD2SUshLudpmQFCKRxBu0lhhS5xrfXBEy6EYV4k8aZI4D+4Udm4hJD9JP/bvP1fZuQ1zbx9d7ZSO5cEVEuGPD6nNwBAhQeC3rsxVB6tgXxAU/xRvEnGJOb1O3l2qDiN6ZAJ6UmJTZ5KNKvlrGDsuElKWDwX82X+56YofsOe6zHgJNWXeEMyfvlKMe3r4ca23bf21hE/f1Tee32g2n3iD7UkxBCm82b8N6i59XjNlU4xwwEdU2RsdU= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 5 +++-- arch/arm64/mm/hugetlbpage.c | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 1464e990580a..994597a0bb0f 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -941,8 +941,7 @@ static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma, } #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)); @@ -1122,6 +1121,8 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, #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 #endif /* !__ASSEMBLY__ */ diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 510b2d4b89a9..c2a753541d13 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -188,7 +188,7 @@ static pte_t get_clear_contig(struct mm_struct *mm, 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 @@ -236,7 +236,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); } @@ -411,7 +411,7 @@ pte_t huge_ptep_get_and_clear(struct mm_struct *mm, 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); From patchwork Mon Dec 18 10:50:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496572 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 8290FC35274 for ; Mon, 18 Dec 2023 10:51:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 226F16B00A1; Mon, 18 Dec 2023 05:51:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1D64A6B00A2; Mon, 18 Dec 2023 05:51:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C6F46B00A3; Mon, 18 Dec 2023 05:51:50 -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 F0A506B00A1 for ; Mon, 18 Dec 2023 05:51:49 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D275D4107B for ; Mon, 18 Dec 2023 10:51:49 +0000 (UTC) X-FDA: 81579623538.28.D87BCB3 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf11.hostedemail.com (Postfix) with ESMTP id 24AC540005 for ; Mon, 18 Dec 2023 10:51:47 +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=1702896708; 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=OjvLCzI/KlO/iQQJYXpNOpyg2OgTuVIw6IDag87PmTA=; b=uRDgGihvKGWx5fSY+s24U81CdVg3PSXGePtn3qVR5a6FbiCmGWaIwqDwNLRYi0vXu+QoW8 Q7msaCHwjyowjSpsRLw67QFYSLhEcxhCFmPdOHxkwBGTb3rKsB+gfDjIGt4M6DlJmPTYgU tY/y+ndS2lYeJpY/ijNOj4RpXAhYYaM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896708; a=rsa-sha256; cv=none; b=tYLa/S5iBVrx+QBL3CUPEk4IvLyqawrGOCq+rNbJBGnvpIP4Jl1u12oJBs0lsFqxNEjLk7 ydxTHiit8LhFXTI449eRDt4FQ7twdUKegTCv4puvNxtxpPc3KKGFhID7Vnrg3+8XHyNtz9 THXfH6EOTp6hiSfVL7/OnhpUiD4gtts= 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 E02011FB; Mon, 18 Dec 2023 02:52: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 12C8F3F738; Mon, 18 Dec 2023 02:51:43 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 08/16] arm64/mm: ptep_test_and_clear_young(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:52 +0000 Message-Id: <20231218105100.172635-9-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 24AC540005 X-Rspam-User: X-Stat-Signature: qq3gqqdbufirjt1hinqqn3mbdqo8nwo7 X-Rspamd-Server: rspam03 X-HE-Tag: 1702896707-942529 X-HE-Meta: U2FsdGVkX1/Fyf4AZKpasAtgRpsXpvJ0TdlwcNPoD9J+vZuRPoLnhsUTcEP+RNBTgNzVI2jojHwjG5kXNQ5ounSE2uTpakvxGsyzDSh00MFQKY3uqh6lDiJOMXR/Uct0mPkBRnB8Tgl0tVF+AezWeyfHjTjU+iF6NcGjD4JSkFgQn2gmNCAzwxY9804toC9WK0CwNfQuw6Knef3B+C+uBov/IBnfpEoK52uKknuNPGpFcmQSi/gGZ+ONDMcJgNYpzgnGQf7zp9Q0X7ZhBZnTuaGTzwIDuBHHn1yJOxM5PWOBIQiIzM+UqXJy9daEUQNw59ot7IS0E1SquXx1bqWwjKsocfa1tqK67wxOfByFBFSvg5vQkz2IqAhedNuF/2qvxyPxZc/p3V01BqVEXFclMp3eia0CddL1c9b+DQ4brpuBYQel0d/yh/sxNyl8mm496+pPiMacZhoalfB1w57U7CrIRC6cLrYBzFif1FgWy32fgllV1z2Lq03ohLuDu8kG//F8YHGLMNf47PvEYjDjE5dPSz7plMHD1mgyKZx+hRt4jMY1OARsZDRJ9zxx97QyDd/VhQeiWOX1CUyOGk3pr5DfxvSTGtx9xtqVZLQJMI7yfUeNt/7bIRLu6MJVRJH6y8X0C81FIil3pPEaNzoc2bf85f+ZTvLLqQk+k0zocE1M5zs4aamFdfiVeRlmaed5Qr0JZY8AVDO+KcO9kpGxmPWVaWSoAhvphhYqFGMXBJeSVZIIkUqT+leH//3jSA5RlMxUpzl8pFa/sRkOgKFB4uYJqqe7zzIkiKhppmCLz/Ex36p3gtcRF6dC0h64gHBhK2zyfGV2kP0ofViR5nMWhIYjiyhv3xeWFZmRcYa13CXdu8RbddtJnkhddis1ABFH2NqcyHLrA5hkHy93pZoq4Fsp6YYl2+6FGFG/G6gn/++BO6wwWez978ofvC/AZMjNQDA6K5imNasPdnapJam ecCb0OMK 7FiFT99L3h/cDtZqEHTnDNWkqpRL5tvy2aGCR04APnNj3DGdwwPjvme/0UjqBkkgQcy0Tz2Yz13RYVV4NJeD0ZZnys8qgWhSodXfDOZPlZI5huucSBD8AXFLxrHB3kLwWbs/e1pi2vPzgOZWFjCbl1X5upbw9h6oX+U54U6QfwR0yNuLHrlGn8xh+WaU+UoiWAXhLd1027UvJfFTcj9/66BqHY5DUtAUg7a+dcPbNasfP4ahbJ622S6NfqTIrgote+UpYeiDdbY6WxJl4AxehWdsmnB0+sCOvZelEMjbKI3SXcQw= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 994597a0bb0f..9b4a9909fd5b 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -887,8 +887,9 @@ 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; @@ -903,18 +904,11 @@ 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, 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) { /* @@ -937,7 +931,7 @@ 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 */ @@ -1123,6 +1117,8 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, #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 #endif /* !__ASSEMBLY__ */ From patchwork Mon Dec 18 10:50:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496573 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 2EF43C35274 for ; Mon, 18 Dec 2023 10:51:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C38D56B00A3; Mon, 18 Dec 2023 05:51:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BE89B6B00A4; Mon, 18 Dec 2023 05:51:53 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB0C16B00A5; Mon, 18 Dec 2023 05:51:53 -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 9C3616B00A3 for ; Mon, 18 Dec 2023 05:51:53 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 754A71A0D08 for ; Mon, 18 Dec 2023 10:51:53 +0000 (UTC) X-FDA: 81579623706.15.8DFD84F Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf07.hostedemail.com (Postfix) with ESMTP id B245640018 for ; Mon, 18 Dec 2023 10:51:51 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; spf=pass (imf07.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=1702896711; a=rsa-sha256; cv=none; b=VNbB5q8KToskOCeV0GNd5SDRC5m7+P20u6He7Hw3WWyYZp7EdXM/n5GFXw6jWrhOvkNrEX VVmeIr+JggJOqq31tmSGd19VykuKLu3cZIyXoh00RfSH2tN0p8HQFm79Ae0CSxFqRMvqP+ 4/h0flInFQaXaW7vqJ3NwzZtxj3DKk4= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; spf=pass (imf07.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=1702896711; 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=80eWkD1e3ScDQ5SjUCye7wOJgIG+FP4dY1bE4B1pVFU=; b=fnUtxoJyn6YfHVIQ6knZYs2jx5ipEim8BEbnqvq3f66+roWPh6bgnDIeaTvUfYe9WqNU4Y tgmipr5oJaS4bmfI7XN5OHlbr27gElRx0UmraRT912wExVZ/0LWNiP+hazAmDAGJk15Q8m SeJ/t2oPf561O/nmTKP3IS0Q+Y7QVDQ= 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 843712F4; Mon, 18 Dec 2023 02:52: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 A89773F738; Mon, 18 Dec 2023 02:51:47 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 09/16] arm64/mm: ptep_clear_flush_young(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:53 +0000 Message-Id: <20231218105100.172635-10-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: B245640018 X-Stat-Signature: 3ojx1nf74sam5177hrktmc1mwpb4tpjq X-HE-Tag: 1702896711-429556 X-HE-Meta: U2FsdGVkX1+gjxteIXBm7tpB/Qq0tYeTE/Js+33YXOZJSZ4J4+ka7KvWlKa2C4Wc86Lrb/nHkHxES5DsSht3+cz0q+D0D0uMk323Imrr6M5c+WYjYrIearBtjBfWMG6cmHYBnRO1Xos+jteeXpwJBMlHfX+lHy2v3LI5USvqvnk6Kn0x7KW3vzK2nVD94eexQL3Ffyv56IXpKGcsctVTEebZQWydXb7mkJejClM0prUF2VL8Q4h6MwIljgVdofI9UWRVuNv/N89MH+7MxSw5pvlEEPNAQTbgqSNaFsaPLfjzBOWmynAfSWBxUTGf8waSu2wdNKUawvDyyPR/fcLUA+lwtUSxtLaJNEKaCHLmrygDxGqZuiJAeA7SR6j7XfKIPyA7f/vu14KfGbqM675JsUzkkhr3OjFcW/o9eZtsex7/GWEcRqqSpXdI6okjnTpXsc++3J7Nn07bGMN34lNyDw+7S62gVFXIvn3VNW2q9WmKEdOaGTgWy0QeR+K1rw1692B84JlLxgC1jx5LLc6C2SEAmln9JjfImu2S983a3M7X5U2ggWw2Am56IG7GuYOVAJz67VxO3glRIFEf/nZNgmEJOg+YdXLoz+wakKmOBYlZ9vYWJ3r6BPmxKA8B6Pt4NSvsXYJhlL8Dg5vBzdOF+FfdERZjhJbVLaL48uCyEkCx8o9oQs55nRfOlpemtfMDS5MwQ8vouM+adAQvjMFBQMlhuua4bbddBuRczZ9tIkT1VPVk6PdT/7tkt9BYTUcFybP9JBZqGqGRpS1VV56UQedDJrbjP35L6YVoRxLDSP5bi2goQyjAW8s56jAAZq/XMx07dUIpiVipGoEMvHkLG7WzFofBPfGVBSp35IeuEgkYCvxD2/5uWpwxB8hTfAZm8MpLahoOwAmOUpHDAhATy1F9Z2jg0BD+NHG8QVnMDsEQcwc20IgI29MaJ1wh+VyA4+hU+lj+e0FO1AuwxqQ 6i/7BaMy p17jylGGFBYhrraXQelWkbovg6+YCM3jNtP9p+dRvq/G/OdZOtYZyukJUWN4/gBzksUgR7H7ACV2ohHFfxhUgBxqwT5WNYJb1EPAxGsxF2Jns2cz3PObFe3+3dMCiMFETdUG5CPwYPP2e2Bl1a2w0cN8zlWJMH49bdeeMt0ILOFCAcRP/A/Wl1ZtoqwhCRxy5JvMffrqt72e9tSHnjURMTH5/bM07S5j3qDuKS27J2DSXhz9PveJLYtmt0fdpwLjm0CQU1bsQc6+KGBhnC661jYr5hJ5DeuoegnL6zmBqDCSjN/4= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 9b4a9909fd5b..fc1005222ee4 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -138,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) \ @@ -904,8 +904,7 @@ static inline int __ptep_test_and_clear_young(struct vm_area_struct *vma, return pte_young(pte); } -#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); @@ -1119,6 +1118,8 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, #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 #endif /* !__ASSEMBLY__ */ From patchwork Mon Dec 18 10:50:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496574 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 ED5BFC35274 for ; Mon, 18 Dec 2023 10:51:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B6B56B006E; Mon, 18 Dec 2023 05:51:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 88DA56B0075; Mon, 18 Dec 2023 05:51:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 755656B00A5; Mon, 18 Dec 2023 05:51:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 660846B006E for ; Mon, 18 Dec 2023 05:51:57 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4732D120CDF for ; Mon, 18 Dec 2023 10:51:57 +0000 (UTC) X-FDA: 81579623874.28.6379982 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 8663B2001A for ; Mon, 18 Dec 2023 10:51:55 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.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=1702896715; 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=L96YjzXNEj/7992DdZl7vC+6Lpqmu+YvM9dQCrXn2YQ=; b=Ln+1wSrKG3RmmoJfWeH22pnTFd7tFMipgCTW4Vd4pAAd2AQ/KXQn6VJfoMh7iFrQYj7WkS /Xlf9NnmL8nI4b8oHSll3viBhDiGUQwXTB1by9X5QgmV3W4Wz/Quk8fslL59PiXY4C5LuM MYNcPgyen2DEp0bd6NBBUNTfr8uTvb0= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.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=1702896715; a=rsa-sha256; cv=none; b=qPyVVhHlsz+wJTDcH7RcrHL6LA+3Y47A+Q9LN7IbAe7T8US0aHVYrOSz8R9fmG7wUaa+X0 G7B4zlG7xH8/MkcGI67X9i/VraTn/AEZz4Vt43KT4Od1EETEQy6ado1E/JnC5ZNGjVHWqG jqQtr+LkC3IlA/XmvAopFxB+nP505lU= 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 267FD13D5; Mon, 18 Dec 2023 02:52:39 -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 4C8F23F738; Mon, 18 Dec 2023 02:51:51 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 10/16] arm64/mm: ptep_set_wrprotect(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:54 +0000 Message-Id: <20231218105100.172635-11-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8663B2001A X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: rgkcjwsm9jqj3j1t7xeaziqessr39p49 X-HE-Tag: 1702896715-464740 X-HE-Meta: U2FsdGVkX1/Qa+uF61pchvBS0VsdOIp8jilhy7mo8XeYTYGe//0UD8/gVyUcbj2a+aLhZX2SQ1/Yl7waLqzWoyI64x7dJA9HZTBOG2lL1IASAt7ZrSQrXYyVf5uYsuYmdUQFxmQwZn0kbdodHjPJNI1mR/VTXTLMeC8XmVPaYmDDwTBISCIIt9v6I1m0YkZYIkjpbvz6kMjIjhPXuzH3Uikmw8KBXLjCAmKFB92h861CUL6ZMZoMBV+bh+X8/ZxQnlmCZyNzOZ/bnM+0NPShT43tkIYDylIAnWZ5DCSKdPFx+aqhh0O4t2qYVoxGjQDIO9sK210UuDuyEhMFCKYTSsEmtFd2tYc3QK61vzoxDb1lR59W+/PVjdNKk23E2nFTVQCmPPpkY+mgFnp+ZNbt3Nym+9X7vRR7N7juJNMy8lalPUqgYOmvEEtYS7UBhZAb5NUaK+0i9Mp6BAhASvrH2yjcB0KBlJ4wpm03BHqiMldFGSmaZzVgFyqVr4uz/7Mn8IYb6Xf176CpnfFwR1gRBgGDszgIz6pDt6h7+NvnjDThrl8EHGmtOAepae/8Jjy7HaITjJ4luyKMWDOqbwLAresqbe66auJSNTI/WyCJsG3w8e8GwcfBz05X369m2w/FmybICjZE+ubsNvtyk1tNxYTQ/+nz0CUXSAP9q2n8yOlHwJ+WoF49QilIUL1ylHlf/Pg3zgoU4j5a6RBdjhfTqM28oXEFWKccdqMH2u30n8pjVlrAUQimwdGNQiEzZ7cG1Tms8Kc4wLjk1uZ08K3WDTYbrsMfXPbbMbynFc1/av3oxn+uOJg0z1IagcIh7bfWz+Wna7q3KyJD1+k81fZwCtDl44qsbscUtnQ47X4pTEtgIryQ03FY8PZw3lVkdPiMbeMYSs/9DgcicXq7OtFe6NJEcTu1cCEarpvFgGDMDe5uTGRmVWbjKtgVHdvXKIUwSX4QizK9bqvzj1lHKeV kwZNsMaG gmqlcn2y3XxFR7bvZ5v0lDbsjTUKmFZviWY4KiCJ3ic57r1qKzmrDdlP6wvWnZbRlUwiR+EBnXkuaiy7BF0+5b8yWYq5sFTUBBVoh/S0hAsF/epX+GEgijmlzfMQqYPnrTEmVIFhU0frP1nodpJ/KaXgXaiMG9nCU/DuCq9t5HO+W7jeitCDAyKqQpxJ8x47z48fCf9qRqUFVT37eycLyBlTvTXWcUs4AuI5+NpFoFUtOJtsNjBo9WJThnHFzJ7PIfivWkDga8xqV+gexYpuLRmvx3zssqyweWlFDMX+MfE6jro8= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 10 ++++++---- arch/arm64/mm/hugetlbpage.c | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index fc1005222ee4..423cc32b2777 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -958,11 +958,11 @@ 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; @@ -980,7 +980,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 @@ -1120,6 +1120,8 @@ extern void ptep_modify_prot_commit(struct vm_area_struct *vma, #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 #endif /* !__ASSEMBLY__ */ diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index c2a753541d13..952462820d9d 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -493,7 +493,7 @@ void huge_ptep_set_wrprotect(struct mm_struct *mm, pte_t pte; if (!pte_cont(READ_ONCE(*ptep))) { - ptep_set_wrprotect(mm, addr, ptep); + __ptep_set_wrprotect(mm, addr, ptep); return; } From patchwork Mon Dec 18 10:50:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496575 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 9AB0DC46CA2 for ; Mon, 18 Dec 2023 10:52:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EA246B00A6; Mon, 18 Dec 2023 05:52:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 272F06B00A7; Mon, 18 Dec 2023 05:52:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 13C266B00A8; Mon, 18 Dec 2023 05:52:01 -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 F0D0B6B00A6 for ; Mon, 18 Dec 2023 05:52:00 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CCF11A0C4E for ; Mon, 18 Dec 2023 10:52:00 +0000 (UTC) X-FDA: 81579624000.16.FC240F6 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf21.hostedemail.com (Postfix) with ESMTP id 0F1651C0021 for ; Mon, 18 Dec 2023 10:51:58 +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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702896719; 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=ELvSMVT6SA8FQM2Gr1fpFkyswAIlVTQIG3RtOI3VLw0=; b=56Sjd4rBc3VBgDA8tmasPfSI41254oSOIeYPadiVou2dfD6jdVYepmG7dxSYiSQfnCsuD8 NkE6XbvOC/BaT5KJyBybLN16qQaAK2y2FIdbGN4s5X6qkE6ILM0Pv6xD5naL2YZ58ElbnH Lc9f2QMUy6QyeH/H7ILJXkfTcXQ9zNk= 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896719; a=rsa-sha256; cv=none; b=HCf4/Eg07QMwPOlB77PFG59JpGV0oz0LWfWrMMIKJpfBQXcZlALe/CyfnyAf6UvtLqGXZu TkG9Yn+2h3g7lDLdC8FMJBIgyjVd4LXTg4NruuP1B83uV8ga08S/J620zp6VEZQuTd/3FK 6Qkz+IFvMrhofsAJ+RKKgiNqKrWMpFM= 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 BD2611FB; Mon, 18 Dec 2023 02:52:42 -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 E2DB33F738; Mon, 18 Dec 2023 02:51:54 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 11/16] arm64/mm: ptep_set_access_flags(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:55 +0000 Message-Id: <20231218105100.172635-12-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0F1651C0021 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: kuhb4sxfhuijjqeh69ri984oc85a1x88 X-HE-Tag: 1702896718-363257 X-HE-Meta: U2FsdGVkX1/lEWNmztWhfHgoTnzKOgSsndzb1XcYAPwYVEkBiTVXINGKnviB8oI93X31HkzWyb7UIzfBepvb0z5U44nFH0AcrLGK3v5fwTiXUd3uGiOk7Ll7SqTsnhaAo4IFkK6gqFJn2LBlrm7NhD8CyYrZ0Yvb1mfxvnFZuEPOXD4FkarASWD8dSbyq5PhSqtPfdn6q6IuwGSk4GPKoSvuQobcfKmuWhxyfTQh3KvAynLQ/eZNycViKLzEkZYsV7GvKZLxdoAbMrsUJY3gGp1JIgTGRwuViywhKrVtWplKKStgZIKA+AXiJ1837IQoCQA2bQFUtNkLNrhHOjRorTpLbF8yWeonlFWzWdwt4Jls5hjG2x0r2i41eASaex9KTXt3TerF78t5lm0u7XPnZDJA7YwRcUWJPBrybRFz+fB9/gOveCVY+JHfrsYagNWhv8J+IzinhOQpyCx8IzrYfp9OOd12DAXuoytkXTIHqfTgplTxiVUd3yYeMCX7vIzCtBGl6/WBG9dKUtBzPbzCzRRSOhuELLgQ+C9YzMa1IQaJxZdDj0Ns8bQR7PVqQPLQArPzhjn2Oi/KJlSSICL9dIyGw6a3cYrlatrpEjP9HwLGTCdlYdlIPn0Vm9WZIdjh7jSmcLtPIkND+0kr2DLEHQa4iPs8Blzdd7jK0IhjS5nQglSVo6AfyowTD6hir2C1x71a+J6m7adVH9cyzXvf33A2hrT0vc3eh2l9uOB+CGi0Ejy3xJtVBgfl4/YG2KRKN2/09QTrl1r5TuVxCV+uSqp3o2o7J4N2wceJL0hFbhR0Jp7VeKA1eK2u9sRNQB+4X/NDC5Gwe5+jmAv7/FfoY7z3Dvs295q8w7CT86+3tMwNkGuEBAH5Cac4hB3EzkOTGSfc11r7o3tYPwcxzZ72ETQx1pdNbe601GdKFfAggebiTEmun3zFeK2pjONqzxDe8Zjr2D7oPZVWdL4qHMS BsoEF044 op9IV39Uq1mzRix0wSNPawdATZy9Cz20j6eDmxrCE0L5Bdr4Pvq7/5++8OmFcTyJZWN+wI2yNpEsvIE6b4gD5C+vwlymh3Xe6fP/HLi3hID3ITlJ4hR5qs4dZCSKt22auIttXZ7KdoPfBxtMeJn2njrjA5vfU2x79wjHV6b5R1+7r0+fOfwPTwro9cQ3iaFatXWRhgEqb6s0skpjXnUqJt6BR++erxF2BQVU4SriFGsFa9FUXJJ2d3eFJ+68iWeg7iWX/piP8kU+mq22aLBj2zeLDRSvXLoEE/yibLbYhs7DTe1s= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 10 ++++++---- arch/arm64/mm/fault.c | 6 +++--- arch/arm64/mm/hugetlbpage.c | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 423cc32b2777..85010c2d4dfa 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -312,7 +312,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), @@ -842,8 +842,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); @@ -853,7 +852,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) @@ -1122,6 +1122,8 @@ extern void ptep_modify_prot_commit(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 __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS +#define ptep_set_access_flags __ptep_set_access_flags #endif /* !__ASSEMBLY__ */ diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index a287c1dea871..7cebd9847aae 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -209,9 +209,9 @@ static void show_pte(unsigned long addr) * * 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 = READ_ONCE(*ptep); diff --git a/arch/arm64/mm/hugetlbpage.c b/arch/arm64/mm/hugetlbpage.c index 952462820d9d..627a9717e98c 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -459,7 +459,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; From patchwork Mon Dec 18 10:50:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496576 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 F2921C35274 for ; Mon, 18 Dec 2023 10:52:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8DD2F6B00A9; Mon, 18 Dec 2023 05:52:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 88EF46B00AA; Mon, 18 Dec 2023 05:52:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 72E486B00AB; Mon, 18 Dec 2023 05:52:04 -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 63CBA6B00A9 for ; Mon, 18 Dec 2023 05:52:04 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 468A71A0D80 for ; Mon, 18 Dec 2023 10:52:04 +0000 (UTC) X-FDA: 81579624168.02.7C616F2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf03.hostedemail.com (Postfix) with ESMTP id 9F91B20005 for ; Mon, 18 Dec 2023 10:52:02 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.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=1702896722; 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=aK2GgN2wmseeTtTSFeJsHcj3Zu5KeNbIGeBRlv4JN2k=; b=A50g7vXQzgo1Mbf4S3WDrxT7KLzRE5gykwKzpzJ1siq+X2CkACO5Te7wAFZPUqUJw5EsDt ETO2gkdBVGMmhrblk4OIqV5StoluoPLJ/3WlSDsCNnhMnR/Qu3hchk1y2zs6nrbBrSpIRr 3O1Y2lpPMwboCE6oTC7hIvzrbg309qs= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf03.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=1702896722; a=rsa-sha256; cv=none; b=SDoimasvvQ/ZQprKBj1D4Zxl8yKddfXpnqdH5MNKeVNAiO2THc7Gn08njM6vf59AcRu+bX UR+SyW76VYTqr2y33ZuaVnWbWL1ZYclNUsleGM8FHoCTqsyJVpsRwEkkqE3CzxHyDwqJUl OWCcbzDBm4sadM0NVJLHTo+XsQt6uBc= 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 5EA192F4; Mon, 18 Dec 2023 02:52:46 -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 852853F738; Mon, 18 Dec 2023 02:51:58 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 12/16] arm64/mm: ptep_get(): New layer to manage contig bit Date: Mon, 18 Dec 2023 10:50:56 +0000 Message-Id: <20231218105100.172635-13-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 9F91B20005 X-Stat-Signature: zsujgnimfszkq99oedsx7ucs785do98a X-Rspam-User: X-HE-Tag: 1702896722-112806 X-HE-Meta: U2FsdGVkX18Xkrp+KT0BfjsiFzqOBgGRQMTMoxIPAqZhnkKGKI6rKYMcMgwNV5B6jGz3zg57P0GjdCv5VpoAD1maGdkeLzl/nbArubkWZbmPvTopldehUbv3oTtRXSXwhxD8CrHbc7aZrDYhDsr6FZQG3la+YWV4OaHNAPoNneGYmDjlxsK2y0d0rZgK/tnocoV7IVYfauWyEV5DrFcfhEFCHggncfqYmPs4YIM7rhP+Fo5kuTX8Ty/WtL0S++JoKJHQHeWgG8Rrqbt33i5ex7SaHAezgZY72u940nb4dGa9eDj0bW4pPVaFH/C6fRkoucp2FyjUQ8t/mdYwThFq+7E64FTPcqLmoIC77rd4Cjkqps+pfMr/fb9R19EA3fLQDuQOkXsWxCtoKlMLvwY6YtivlL6HU5TOBOo29jWDtBhlaM035/ZaxNC9udXFSL0EXX07M0YWb1ts5+YrSextK1nXlbKdpWm1cLfY9POUyp58h8UcXzsHz0Pfby4ac06m7CXS8mOU11zM7w9CpdATGUO3BY/sKiMmUw24h/4BVJBpIC+bhfxgs3NGGXv4nOuiRkmlXHvLIT0aNq9ooUwvIEyD4cIG22iCP1KSAzVP4ESUdzLQ5ELSQPxDIb7qjFOqrwXSpaqHLv6itIB00sMYGjmXNsJOk2O5wNr2J0cqHheyfEhSSPWgpQjxeGQDt48HLxsXXziMe/QZINlVF5viOWGpxk20TseqNmnOd4CmOUn9J65157NI4BB1IglU7WX+5U8B91QInCfE2xC3XKeD1fbEja3UXI6JG+sLFVFwaGrklqEeGUqIgu8y806Rq41ZQMRlM33ByF0d1+W7l2o7zXf1n8OcgnscC4bDugTFPqPd3QKlhuhStdQ/DtB/6HvOt/hd+kXRVx50+yVb5yEcszLkXy1LaQS8GLJ9xpuPSdVzHK8T2TKJOLh+cnml+8iaOUm8xaFB+N57lpiZSGq ncaNZ/Z+ N/jfYKsVbBhbpF2xuTS4oEzxjWxC02kcnE2vbOk4tgUR0Y9jlty+6WD8mb3JEGUU2dRaFwd/KO8DxtKjQnn271ZKMMEICdgtL/HOcgEwoIBfXpU/enBSvMUxvoodea1irTUGLdpFCNTSlwN82PzXFKvgqKvr7EO0Gt0uK/0A4Attfx+y8nRBWJ3c+uvRZs+IsYjcTUTfddTbhQOaw8PfiAx5WJM08eAw0gQsKSRlsTwnTc0DNxQ6Ao431KAM3roKsVFq/LDuVs6mAJcG28ZxUSykpgVw+HvWofD76wqgKFnJ0HHo= 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. arm64 did not previously define an arch-specific ptep_get(), so override the default version in the arch code, and also define the private __ptep_get() version. Currently they both do the same thing that the default version does (READ_ONCE()). Some arch users (hugetlb) were already using ptep_get() so convert those to the private API. While other callsites were doing direct READ_ONCE(), so convert those to use the appropriate (public/private) API too. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 12 +++++++++--- arch/arm64/kernel/efi.c | 2 +- arch/arm64/mm/fault.c | 4 ++-- arch/arm64/mm/hugetlbpage.c | 18 +++++++++--------- 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, 31 insertions(+), 25 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 85010c2d4dfa..6930c14f062f 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -276,6 +276,11 @@ static inline void __set_pte(pte_t *ptep, pte_t pte) } } +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); @@ -303,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; @@ -893,7 +898,7 @@ static inline int __ptep_test_and_clear_young(struct vm_area_struct *vma, { pte_t old_pte, pte; - pte = READ_ONCE(*ptep); + pte = __ptep_get(ptep); do { old_pte = pte; pte = pte_mkold(pte); @@ -966,7 +971,7 @@ static inline void __ptep_set_wrprotect(struct mm_struct *mm, { pte_t old_pte, pte; - pte = READ_ONCE(*ptep); + pte = __ptep_get(ptep); do { old_pte = pte; pte = pte_wrprotect(pte); @@ -1111,6 +1116,7 @@ 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 diff --git a/arch/arm64/kernel/efi.c b/arch/arm64/kernel/efi.c index 44288a12fc6c..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 = 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 7cebd9847aae..d63f3a0a7251 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 627a9717e98c..52fb767607e0 100644 --- a/arch/arm64/mm/hugetlbpage.c +++ b/arch/arm64/mm/hugetlbpage.c @@ -152,14 +152,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); @@ -184,7 +184,7 @@ 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++) { @@ -408,7 +408,7 @@ 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); @@ -431,11 +431,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; @@ -492,7 +492,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; } @@ -517,7 +517,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); @@ -550,7 +550,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 5eade712e9e5..5274c317d775 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 080e9b50f595..784f1e312447 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -176,7 +176,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)); @@ -185,7 +185,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); @@ -854,7 +854,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; @@ -987,7 +987,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 @@ -1006,7 +1006,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; } @@ -1475,7 +1475,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 a7996d8edf0a..0c4e3ecf989d 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 230b607cf881..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 = READ_ONCE(*src_ptep); + pte_t pte = __ptep_get(src_ptep); if (pte_valid(pte)) { /* From patchwork Mon Dec 18 10:50:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496577 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 B6879C35274 for ; Mon, 18 Dec 2023 10:52:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 534026B00AB; Mon, 18 Dec 2023 05:52:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4BFB66B00AC; Mon, 18 Dec 2023 05:52:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 310DC6B00AD; Mon, 18 Dec 2023 05:52:08 -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 21BB16B00AB for ; Mon, 18 Dec 2023 05:52:08 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 07501A0D45 for ; Mon, 18 Dec 2023 10:52:08 +0000 (UTC) X-FDA: 81579624336.14.C2E9C19 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf02.hostedemail.com (Postfix) with ESMTP id 465328000E for ; Mon, 18 Dec 2023 10:52:06 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702896726; 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=3XWNUf0y+LoNhJb/OvSX6jZDQxYAWNT/5GVeo+DL1UM=; b=NL33FOs6H4nTWNFUMW7PEujJ2MhPz1gZCgXHD06pDyWYK3ZhcES/GD7K0kS2UHdvfl7oei hal8vPWfowZvPliS4Ge6NB9zX1d0lyEM54gQQ+85VAzeBNLA0Rfmn+055V+TGKuNbnbtue JFsB4RhsU8y/2cslEdg4LSqD3Wz7G7k= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896726; a=rsa-sha256; cv=none; b=diKaD0LEagImnio8Fa1r5qYq/XOGQpqrQ62RdwlXZFdTlCQAO3j99MsrzUBgbF3r9w1Qmv wozuk2utD4UD6Zv/FV7GPPc64/rS1kbaEXCAGFp6prSnXJpMeYRJ7y+xPVdVyhNB4g1Udl wrpwY4LLZb72Xa10Zii5ZytftgimVNg= 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 000181FB; Mon, 18 Dec 2023 02:52:49 -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 26B0F3F738; Mon, 18 Dec 2023 02:52:02 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 13/16] arm64/mm: Split __flush_tlb_range() to elide trailing DSB Date: Mon, 18 Dec 2023 10:50:57 +0000 Message-Id: <20231218105100.172635-14-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 465328000E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: hggwi7dooymgqak7xobqgizfzgpb69ez X-HE-Tag: 1702896726-715381 X-HE-Meta: U2FsdGVkX1+WBuo97Wh6hfEnVdveD9Cz4GwYDefDqxb8noFAA28YT76leN9zNNv87qbBJu4ARPcDximoJTX0Ny1X26HcuaykUQzRzio6ItZ6s0UuII1/VklPiHteWKTcjoUw56YeeBqYN15Zx4rgbIp6O8Bqz+zjKrenkHfkXb5ykYv+GyDOSDQf7xRu4dLy7Vr9V+GcVQhiIDG9Zind/YBhdyGCKpPWG4m2Jd43+6PgY2VZQ4AkDw1mX53PEIpa4Qa6kdw+ycJWbmxkSbNAIshcyY/pBtjtzjBpDXmbT9yBd979H5BMiLNtLzYuIcwcYKzARfLIToL9aTa/je+hoYvgnWBNZP6SJNrLBPF+Y6PyWxObOiALRAZSBprEd3VDaGbtVofjjcrUm7xniVqvenrgIwAKYX1xdYenkjvKMG2JBatKId2JMgjVdbWOirOh5CTx/TkDjEaVjmvru+oc7ixe6ghE2Ft14mzCmzMXnMk0oyw+SekMKd2aaCOnwGoV+0lXgvEeCIFYAdEYGvGMBDcPgviCQFLj1S1+WRMg47Ru5eZ4Mu5rziH51hUUhqVAlG2NdXem1WB54UmHHbqh1UfzPmy9utqndbat+G+Y4rh3TtZsHPL7naKLW3IJ59J56ZC8m5/Znxrwsq5KOBHFpi5FirIw+Y9gtqntp14xL+/ktD1N+Y03SBF0igUuGeWNaNw38dwIsMBBzkV8Gi9PkTKiwZIxJe7mPnoCSbZ6c6zfKQ+l6qXjBAi3xYGU89LF25NV1i3ETuqaJyNk54grEXgBgdV9Z5GF79dZl10i/Ca3tj0RJGDa+iajQ6wQFMqXD+Oc8R18QIBKA9ZUphqOaHdsN7/wFZJcI6zT4bfUqP04dKhHZn96g4gYmdElW6XXEJTytrIo3OL8yslMB/H3Ra/laIv59T6/5hkXZoCrlP9JYhGaKV3hvY6AucrRwaQ7WCKY9M0Q2WMUyPDh3Sk gSY7soNW 0xNYdNUrQeqeF4201CfUTJnQnSs292h6qnQRem0ncHMZ02CxmgDF7lLsgBFtoya+Yyc0lMRWQSpiAcm1cWLsGvrDbgcdNAtlkKCYNBIvE/oISKKI9aQsuxzBhd1jZNmkFwzl1X4IMno1AbMFgHoL3gPzj7R1QP05jjxFHEECsxWnqX/n5LDCWoxic7Knvn/Q7LwM4h/ybVJDfiVDcY/lzDmkwMyERxWHzlGa6HsPuIx8SB6u9GWeViWe2ixVOa2VwDvrjLxxuIZzlvt0oKU7F8tKemJWPb+O5QvPq/5hkj6FtMIc= 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- 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 bb2c2833a987..925ef3bdf9ed 100644 --- a/arch/arm64/include/asm/tlbflush.h +++ b/arch/arm64/include/asm/tlbflush.h @@ -399,7 +399,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) -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) @@ -431,10 +431,19 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, else __flush_tlb_range_op(vae1is, start, pages, stride, asid, tlb_level, true); - 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 Mon Dec 18 10:50:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496578 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 E5DD5C35274 for ; Mon, 18 Dec 2023 10:52:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81AC86B00AD; Mon, 18 Dec 2023 05:52:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C9E96B00AE; Mon, 18 Dec 2023 05:52:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 644076B00AF; Mon, 18 Dec 2023 05:52:12 -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 4F7186B00AD for ; Mon, 18 Dec 2023 05:52:12 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2A97E160D7F for ; Mon, 18 Dec 2023 10:52:12 +0000 (UTC) X-FDA: 81579624504.22.650F5E9 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf16.hostedemail.com (Postfix) with ESMTP id 64CC8180017 for ; Mon, 18 Dec 2023 10:52:10 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702896730; 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=j8NHhbxBUceulF2OYwyIFDA/Hi188nG8B9JKA9Sk7RA=; b=iDqTeh8wgyR7GOhB0Ku96kvVE61sG7XED2W6W/OBMG8ROyUdrbW8qHqz7Kcbiy40/XIlhM i6s7fSJyRppOu6HyVUGEoRzPfLJ1N0XvH9VhdlbU8SFUzwZsNPYyXdrEhszVB4L4tLZT5W OzwwdfdJRnN24ALBGsSvjJGw5DHGYsM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896730; a=rsa-sha256; cv=none; b=ef/hQ/HnKJaVd2giq5Mr/fcWe18w166glKeGIn9EfOyj3dkv88yfrVMjAN4n1gU/XtQv6h 9VPlznbIIbL2++pxix1oruwux63+cydEee/QMfuJQxWBrgb7Ip/JtcgpABICy64EwPZJEp WadlfFQ0xOWo2iEPLCyJQ84ZdwvokX0= 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 D7FF213D5; Mon, 18 Dec 2023 02:52:53 -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 BD3703F738; Mon, 18 Dec 2023 02:52:05 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 14/16] arm64/mm: Wire up PTE_CONT for user mappings Date: Mon, 18 Dec 2023 10:50:58 +0000 Message-Id: <20231218105100.172635-15-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: mg1sgdahrd7aeo96i4b1jr4wwtw3txwd X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 64CC8180017 X-HE-Tag: 1702896730-255931 X-HE-Meta: U2FsdGVkX1+JsFNipgH+amvr2nZaKp2yUxfYeqHJ+EvoocKC81M7J6R5UICKpLedWnWavKhSXfwEm40NN2IiY71VrWP+fZEiPWLAKAFAibeyoh9rsS2KQWncMCTenpXh1Geo8STMOAAsrylX783lTLrcTp7lTB07hiYORDfx6VinuqPUd0wia+H7Z4SnUif7oayzdlq3ZekGHCgXuvnqmzRQslZoCkt5S207jWnR4va3I4xnS3ujejFY+kgnfDdUf/TbhdcYkRQcZ9dq1e/I3lxKaFkg+sJG7+G9WoMt9rGllLvYEbUzRGgRvCHUN8eEasSWMd14Xt35x449hXu0Tr4xptQcGsTeNimkSuINILcu3EI3+tvsHKJt+KShNmjdWe5Zu9c542wkXQWB0FWF0SjDq7P8ZxY/3VwbKHOpknO1Fj4XSCosGrfem6pQa1/yGwgDfsRpkCxgltiP5W8z4/5dcIa/P/m5vnynYnj8mbjlHQ6lmGw0aARbEfZt6rcmpdrk8u/G3APoSdui2rRdhm8N+pHt6Yoww2YgwHCjV7qwkTcj0MAVQXhjQuNebhY+SRagP526pn9Qzr//qFdh5lfWbMMCgbY0EYYHlHDxfMVXKtJrKrwERqG5TPRf0QvFrvL5jyWFkSgrGvajBaEt+mpfskJHUgUnm+OmyoEmEXevQoNtrY5Hs8GuV+5J9jXlsbw+hZy3Nfa4G18AQZGz4PRzDmPA5zHpHCnQuouVzM5ns4tPjYTGsjEHYvvhGVowkREhOjA+eEnQXKjwGf/folhM8L5lAF8Ax+JhbNM4AMdH21KLToFlgb/drdtlcjzIoczV01vsY65RsAjlifjmolRmkpvJdZguhJDPw/B7QAXfVZfxnw8YyaZovJwh97Qwx3IQGIb88Tq7xBoRsw44ytOw4keqvNCmiRNfj+xkRCYO08w5AvDW0z3zXmsWwRO0Ckp5aGuQNaCEo9WD8uZ A4Mb7xvz KqPW3XtTI1hRk9dRf7YHE+TdBOqGMmpw/183UDqxDtsrD/xD0s1yQ5DAHcvZKcKuqxlBFy5f5oeYiHM1TrmjWwD51PmEfkKAgF1ITvwIEcZkR7IIrZpcv5ziqQJWINLz/VkKGm+TI30myZ5anjRM8Pg4q02tfyaG0Iuqh0nzxU+/mOSnhnBtgawZMvR4LgVAyIqqVWAUvIDwnhSlVNmSSLrciLhrzp/Gi4fhK8hTNT0e/3ckat0wuY+0a+ALX/SrdfcSJnHom/TKmSd6EsJN6MzlGo3NMZIJYqkeAf12ijFjTliQ= 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. Whenever it detects a set of PTEs that meet the requirements for a contiguous range, the PTEs are re-painted with the PTE_CONT bit. Use of contpte mappings is intended to be transparent to the core-mm, which continues to interact with individual ptes. 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. This initial change provides a baseline that can be optimized in future commits. That said, fold/unfold operations (which imply tlb invalidation) are avoided where possible with a few tricks for access/dirty bit management. Write-protect modifications for contpte mappings are currently non-optimal, and incure a regression in fork() performance. This will be addressed in follow-up changes. 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. Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/Kconfig | 10 +- arch/arm64/include/asm/pgtable.h | 184 +++++++++++++++ arch/arm64/mm/Makefile | 1 + arch/arm64/mm/contpte.c | 388 +++++++++++++++++++++++++++++++ 4 files changed, 582 insertions(+), 1 deletion(-) create mode 100644 arch/arm64/mm/contpte.c diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7b071a00425d..de76e484ff3a 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -2209,6 +2209,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" @@ -2318,4 +2327,3 @@ endmenu # "CPU Power Management" source "drivers/acpi/Kconfig" source "arch/arm64/kvm/Kconfig" - diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 6930c14f062f..e64120452301 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 @@ -1116,6 +1120,184 @@ 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_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); +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_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 = (((unsigned long)ptep >> 3) & contmask) == contmask; + bool palign = (pte_pfn(pte) & contmask) == contmask; + + if (valign && palign && + pte_valid(pte) && !pte_cont(pte) && !pte_special(pte)) + __contpte_try_fold(mm, addr, ptep, pte); +} + +static inline void contpte_try_unfold(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte) +{ + if (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. + */ + +#define ptep_get ptep_get +static inline pte_t ptep_get(pte_t *ptep) +{ + pte_t pte = __ptep_get(ptep); + + if (!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 (!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 or fold 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 (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); +} + +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 (!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 (!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); + contpte_try_fold(mm, addr, ptep, __ptep_get(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 (!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 @@ -1131,6 +1313,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..69c36749dd98 --- /dev/null +++ b/arch/arm64/mm/contpte.c @@ -0,0 +1,388 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2023 ARM Ltd. + */ + +#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. + */ + return mm != &init_mm; +} + +static inline pte_t *contpte_align_down(pte_t *ptep) +{ + return (pte_t *)(ALIGN_DOWN((unsigned long)ptep >> 3, CONT_PTES) << 3); +} + +static void ptep_clear_flush_range(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, int nr) +{ + struct vm_area_struct vma = TLB_FLUSH_VMA(mm, 0); + unsigned long start_addr = addr; + int i; + + for (i = 0; i < nr; i++, ptep++, addr += PAGE_SIZE) + __pte_clear(mm, addr, ptep); + + __flush_tlb_range(&vma, start_addr, addr, PAGE_SIZE, true, 3); +} + +static bool ptep_any_valid(pte_t *ptep, int nr) +{ + int i; + + for (i = 0; i < nr; i++, ptep++) { + if (pte_valid(__ptep_get(ptep))) + return true; + } + + return false; +} + +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_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_saddr; + unsigned long folio_eaddr; + unsigned long cont_saddr; + unsigned long cont_eaddr; + struct folio *folio; + struct page *page; + unsigned long pfn; + pte_t *orig_ptep; + pgprot_t prot; + pte_t subpte; + int i; + + if (!mm_is_user(mm)) + return; + + page = pte_page(pte); + folio = page_folio(page); + folio_saddr = addr - (page - &folio->page) * PAGE_SIZE; + folio_eaddr = folio_saddr + folio_nr_pages(folio) * PAGE_SIZE; + cont_saddr = ALIGN_DOWN(addr, CONT_PTE_SIZE); + cont_eaddr = cont_saddr + CONT_PTE_SIZE; + + if (folio_saddr > cont_saddr || folio_eaddr < cont_eaddr) + return; + + pfn = pte_pfn(pte) - ((addr - cont_saddr) >> PAGE_SHIFT); + prot = pte_pgprot(pte_mkold(pte_mkclean(pte))); + orig_ptep = ptep; + ptep = contpte_align_down(ptep); + + for (i = 0; i < CONT_PTES; i++, ptep++, pfn++) { + subpte = __ptep_get(ptep); + subpte = pte_mkold(pte_mkclean(subpte)); + + if (!pte_valid(subpte) || + pte_pfn(subpte) != pfn || + pgprot_val(pte_pgprot(subpte)) != pgprot_val(prot)) + return; + } + + 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) +{ + /* + * 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 guarranteed 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; + pte_t orig_pte; + + 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); + + /* + * If operating on a partial contiguous range then we must first + * unfold the contiguous range if it was previously folded. + * Otherwise we could end up with overlapping tlb entries. + */ + if (nr != CONT_PTES) + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + + /* + * If we are replacing ptes that were contiguous or if the new + * ptes are contiguous and any of the ptes being replaced are + * valid, we need to clear and flush the range to prevent + * overlapping tlb entries. + */ + orig_pte = __ptep_get(ptep); + if (pte_valid_cont(orig_pte) || + (pte_cont(pte) && ptep_any_valid(ptep, nr))) + ptep_clear_flush_range(mm, addr, ptep, nr); + + __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/fold the + * contig range. But if the write bit is changing, we need to go through + * the full unfold/fold cycle. + */ + 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); + contpte_try_fold(vma->vm_mm, addr, ptep, entry); + } + + return 1; +} +EXPORT_SYMBOL(contpte_ptep_set_access_flags); From patchwork Mon Dec 18 10:50:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496579 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 D6D48C46CCD for ; Mon, 18 Dec 2023 10:52:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7A58F6B00AF; Mon, 18 Dec 2023 05:52:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 754976B00B1; Mon, 18 Dec 2023 05:52:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 61FD56B00B0; Mon, 18 Dec 2023 05:52:15 -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 52DBB6B00AE for ; Mon, 18 Dec 2023 05:52:15 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 32B291C1B79 for ; Mon, 18 Dec 2023 10:52:15 +0000 (UTC) X-FDA: 81579624630.12.7A6D7A2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf27.hostedemail.com (Postfix) with ESMTP id 6C4A340013 for ; Mon, 18 Dec 2023 10:52:13 +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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702896733; 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=B2PjP7HqUpeEkMhkbIFHBWS4ku1Qcu2bCx3FQTTUYsI=; b=uefErjWf2HCvyflCWSIGEPsg7BFXAT4HuCI1NRtut6veru1nrNYpN8ZRpcoen5gfMnsMIW /WAZE81WhWxlUD+SAMIA6IIa1XQ1Wa/yLfR2bZ6RJpXUaKrSM6aw0ZME5s5K6IJ9e9dZij pmXs+JqCBGg2mYhSWtB8DrKE+jjUhY0= 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702896733; a=rsa-sha256; cv=none; b=kyuYOxdBRtBbmbIt3up1+ztF4Ikmp6JhFc9Vd9/KQWUZpiwlXOdEHD5EPBMjBavxrEcIcA 6sXHclW0SWrQ7r9IjivK3pxeqRjaW2OsZc8Vh+F/3+P+RLlaDxCvK4sQdz5Ubwhd782hNs ADMc07VZ8b5aQ+LECth96MvLzc9ezj4= 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 384F71FB; Mon, 18 Dec 2023 02:52:57 -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 5EF4F3F7C5; Mon, 18 Dec 2023 02:52:09 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 15/16] arm64/mm: Implement new helpers to optimize fork() Date: Mon, 18 Dec 2023 10:50:59 +0000 Message-Id: <20231218105100.172635-16-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 6C4A340013 X-Stat-Signature: 6hr1jc6fdxwzk3gj3hz3e5nqjwgwnh6j X-Rspam-User: X-HE-Tag: 1702896733-395555 X-HE-Meta: U2FsdGVkX181oMxAd1YbgX9BGQdgTaoh8m14YP6fop2pnQiV41G4VCjvVn956QVLg1iuv9yG+obOrlbWSDbkqmz9aAU2i2QoMgMQ+8sKoZG/+B4fNZ7Zm6P2Md/v4khNUxLNCffsbQpWsdD++q93vMIPEfwFrNI+dvZz8swoAAg4TgES6p0+Ck/GV/SStJkOh1vQ1dPSmgec0z7mZP60VY54Hqn4ACbBtociPgdYxsO38Brq+oRKwRocfwtHnlFyaJDVkSfTEcCw89gdjyomW73ZXiulONeJ8dteP2xt5wnsUNQvRXmZqkBiTpxzGLdLFaD9APYE2srKvQXd5npPHLKcSq1Ttva6ID/WnQ+HPIUSvil/R8+MwDU7tkImXYz6deLDeg8tJc6qXC1YIzIKsfCC9vjgelcim1IvxqLr8By0/STKscozar7pl9YinU+yTWxf+xh9B2akg2uF4s3+YHDqO5Fwh/RB2Gv1w2t+3aMujENtNtowsrbhSCa0U2M9aPyG38mhPXuzKVuz3tM6M6gN97ZO5rYOVT/p3kupwdA0ee624bI+Cm1UPx3y6iOWcS0hXZRDtMdymHJzLRjVjz1W/QC+/sPntWQ4HGPLQmpgPotwqfhbSMgmWUO0IJgaA3rh1YCvsjnkozXga/nLwdg6hQ+RQPAmazHKCuFDeVQDkTgUWVEES1fVD5/ERMM9uU8kymDEWGH8WC06S2M5jEJxQ0ZqmcpjfCN7ow4gWx4vEgQxFpjr5FIm4AEmdc+pjTRo00Xgwri8E51+VuDImIG00ExQfDyEi3swQFtv9no4HTnC5gv5/8JLw6Zu4pBhK4htUoC0KVPpLxGr1yXax1/gfk8Do3BwGjZT7zCAKCbiGfUO61sy2J8MANuRbVk095BAMGte5SmQ+X6n5D5qXT7kfeQBBI+Yz5VUBnldRvpcwWsLjYIpZDFIxmn15jkg1dNixu04o/7VIFPXsEc O82ZEqWZ cJ3Zy/mOzTgdkLMI4GnPQXSXJid4p2HeKt0+n3HKSJvPBFPY9BI9t2GwdiIGnmmCnFj/uWLtvE9i/Hj9hkaiZ4BLMiFP/7j4B7rDzYP3w4JcMPXq4PGAtWszAlqOi+0QBsmaSz3kemjoyCi+JN2JpUqjaRO1JZQ2/BlfG2uiPGl+1gj/68S6ToizaLnrbbYmVX4OMONvgeyTuUOFr2JESNQDYCnVlI0CRDvC5pueeGoYBjt/OmHqcFwtUsiv2PFHL9WfO 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 core-mm changes in place to batch-copy ptes during fork, we can take advantage of this in arm64 to greatly reduce the number of tlbis we have to issue, and recover the lost fork performance incured when adding support for transparent contiguous ptes. This optimization covers 2 cases: 2) The memory being CoWed is contpte-sized (or bigger) folios. We set wrprotect in the parent and set the ptes in the child for a whole contpte block in one hit. This means we can operate on the whole block and don't need to unfold/fold. 1) The memory being CoWed is all order-0 folios. No folding or unfolding occurs here, but the added cost of checking if we need to fold on every pte adds up. Given we are forking, we are just copying the ptes already in the parent, so we should be maintaining the single/contpte state into the child anyway, and any check for folding will always be false. Therefore, we can elide the fold check in set_ptes_full() and ptep_set_wrprotects() when full=1. 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/latest/ The following microbenchmark results demonstate the recovered (and overall improved) fork performance for large pte-mapped folios once this patch is applied. Fork is called in a tight loop in a process with 1G of populated memory and the time for the function to execute is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | fork | Apple M2 VM | Ampere Altra | | order-9 |-------------------|-------------------| | (pte-map) | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 1.2% | 0.0% | 0.1% | | before-change | 541.5% | 2.8% | 3654.4% | 0.0% | | after-change | -25.4% | 1.9% | -6.7% | 0.1% | Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 97 ++++++++++++++++++++++++++------ arch/arm64/mm/contpte.c | 47 ++++++++++++++++ 2 files changed, 128 insertions(+), 16 deletions(-) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index e64120452301..d4805f73b9db 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -966,16 +966,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); @@ -984,6 +980,26 @@ 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 __ptep_set_wrprotects(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + unsigned int nr, int full) +{ + 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, @@ -1139,6 +1155,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_set_wrprotects(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, int full); extern int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty); @@ -1170,6 +1188,17 @@ static inline void contpte_try_unfold(struct mm_struct *mm, unsigned long addr, __contpte_try_unfold(mm, addr, ptep, pte); } +#define pte_batch_remaining pte_batch_remaining +static inline unsigned int pte_batch_remaining(pte_t pte, unsigned long addr, + unsigned long end) +{ + if (!pte_valid_cont(pte)) + return 1; + + return min(CONT_PTES - ((addr >> PAGE_SHIFT) & (CONT_PTES - 1)), + (end - addr) >> PAGE_SHIFT); +} + /* * 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 @@ -1219,20 +1248,30 @@ static inline void set_pte(pte_t *ptep, pte_t 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) +#define set_ptes_full set_ptes_full +static inline void set_ptes_full(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte, unsigned int nr, + int full) { pte = pte_mknoncont(pte); if (nr == 1) { - contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + if (!full) + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); __set_ptes(mm, addr, ptep, pte, 1); - contpte_try_fold(mm, addr, ptep, pte); + if (!full) + contpte_try_fold(mm, addr, ptep, pte); } else contpte_set_ptes(mm, addr, ptep, pte, nr); } +#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) +{ + set_ptes_full(mm, addr, ptep, pte, nr, false); +} + static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep) { @@ -1272,13 +1311,38 @@ static inline int ptep_clear_flush_young(struct vm_area_struct *vma, return contpte_ptep_clear_flush_young(vma, addr, ptep); } +#define ptep_set_wrprotects ptep_set_wrprotects +static inline void ptep_set_wrprotects(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + if (nr == 1) { + /* + * Optimization: ptep_set_wrprotects() 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() with is very + * sensitive for order-0 folios. Should be equivalent to + * contpte_try_unfold() for this case. + */ + 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); + if (!full) + contpte_try_fold(mm, addr, ptep, __ptep_get(ptep)); + } else + contpte_set_wrprotects(mm, addr, ptep, nr, full); +} + #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); - contpte_try_fold(mm, addr, ptep, __ptep_get(ptep)); + ptep_set_wrprotects(mm, addr, ptep, 1, false); } #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS @@ -1310,6 +1374,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 ptep_set_wrprotects __ptep_set_wrprotects #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 69c36749dd98..72e672024785 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -339,6 +339,53 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma, } EXPORT_SYMBOL(contpte_ptep_clear_flush_young); +void contpte_set_wrprotects(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, unsigned int nr, int full) +{ + unsigned long next; + unsigned long end; + + if (!mm_is_user(mm)) + return __ptep_set_wrprotects(mm, addr, ptep, nr, full); + + end = addr + (nr << PAGE_SHIFT); + + do { + next = pte_cont_addr_end(addr, end); + nr = (next - addr) >> PAGE_SHIFT; + + /* + * 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 + * guarranteed wrprotected. If its 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. + */ + if (nr != CONT_PTES) + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + + __ptep_set_wrprotects(mm, addr, ptep, nr, full); + + addr = next; + ptep += nr; + + /* + * If applying to a partial contig range, the change could have + * made the range foldable. Use the last pte in the range we + * just set for comparison, since contpte_try_fold() only + * triggers when acting on the last pte in the contig range. + */ + if (nr != CONT_PTES) + contpte_try_fold(mm, addr - PAGE_SIZE, ptep - 1, + __ptep_get(ptep - 1)); + + } while (addr != end); +} +EXPORT_SYMBOL(contpte_set_wrprotects); + int contpte_ptep_set_access_flags(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep, pte_t entry, int dirty) From patchwork Mon Dec 18 10:51:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13496580 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 79567C46CCD for ; Mon, 18 Dec 2023 10:52:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 18A726B00B1; Mon, 18 Dec 2023 05:52:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 114216B00B2; Mon, 18 Dec 2023 05:52:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECF8F6B00B3; Mon, 18 Dec 2023 05:52:18 -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 DE97F6B00B1 for ; Mon, 18 Dec 2023 05:52:18 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B72BF140E5B for ; Mon, 18 Dec 2023 10:52:18 +0000 (UTC) X-FDA: 81579624756.02.791C86C Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf22.hostedemail.com (Postfix) with ESMTP id 1BFAAC001C for ; Mon, 18 Dec 2023 10:52:16 +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=1702896737; 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=Pl80kIjTjpL3O0EXepdffRGD/m7SMioiLS6gBNRB6iM=; b=XT2GH8DtNvlcbMmB2dWIxZ29pJ+zqe3QRkwtFhpgsvKokC5wF9yy8SvkLp20KfPdw3qGPh ORhWdBk+2iYL2F+/p062VpBuD5C5oMSQBYp28MxQ46ow3OSSG7vQuhB8kVJnzCJ3eIesFZ P363haAZv0tLaQvTAWh7EXxRxYDzd+Q= 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=1702896737; a=rsa-sha256; cv=none; b=G//ksPJK2+XzZ9MwICt4MybbVGjXPe4aVFCEcSj7d2k8cFxasxU4tiTUTqohCn9U12/zp3 qflNW9QLdX+vegvVUFqX5spxufb8sH+fW+D6iXQN/OdCT6eVCjnQr5C9Ww1kpyncXwZmPM /upduyjVnQJ4aXsXsJ9899Q9xuheohU= 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 D081E1424; Mon, 18 Dec 2023 02:53:00 -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 00DE33F738; Mon, 18 Dec 2023 02:52:12 -0800 (PST) From: Ryan Roberts To: Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Zenghui Yu , Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , Andrew Morton , Anshuman Khandual , Matthew Wilcox , Yu Zhao , Mark Rutland , David Hildenbrand , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi Cc: Ryan Roberts , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 16/16] arm64/mm: Implement clear_ptes() to optimize exit, munmap, dontneed Date: Mon, 18 Dec 2023 10:51:00 +0000 Message-Id: <20231218105100.172635-17-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231218105100.172635-1-ryan.roberts@arm.com> References: <20231218105100.172635-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1BFAAC001C X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: twzmqgur5s9b7rj9i44oacn31q7s38qf X-HE-Tag: 1702896736-719275 X-HE-Meta: U2FsdGVkX1/4XhelwtPo8fgBB5RTkPnhN2QuuLR/KtIiPpzggQ5tHGg05KLjljZawl+mL/pOo462F+bOYaoRNmgG/U/dOCQ1iksmdI+97GDKvz+9C6eXGYfz29JqoWBhJoeRB5LKBYD1ZjdvdZNcAdHiO3Or0kIwt6loQPQTVUkxLRyD73LGGKPObPd3Sb7gduvqWqwnXek9Z4d4QjT6EIoYKOkGEmzbLPBDg+n53ubn2gsvtctVoLBwDbSA1Nq5VeoNXsFBOZi3Xe9yYUJF19+kuv4DjDS7297Ag30SlgPp8QXpJj+gB1ks8f54wCm7ePMoq2Tf1Et0RcHzdGGHYBCMy753RtoKw7yRWk+2icaiGDFmiC/B5xHMrt6zO/yPL6xPs4Aw5fKXdt7vbHzaHlSYHrwsPYIeK2skDASKbqnsYS8WT6RPnHfbKsp2KH17XdWHbLEDmDwFkbWbptJQMKaSkjV+MBFXqiQ94GIcGoZa7i8JBrvk2ZtAu6ekjE9IZkOT68egptds/siprqsN/DNu10T6GgTVzeJsrAyX6eFa9ulb31m8Uok+S55aNZKZ43AameymLFTiBjN6TmZCJpVDprZPOE7bAjsfjh6dwFazl4K1ya2jh7AMkyU5vJJvjJbqbynRbwBnuKL5NswxCBc1KJC92P0mvsEvQAprHY009Yx2GC4eIFMOts4cQspyHBc2wxmn+oi+HJtzh/w9RuqAf6lSCVBwg+pTiuhD9nJcLx/U/yzbqg9Nxd/fzcqIlJ5M9SPVF3kgoPs/MkDmNJ5VdEhSqX+crVHEFJwq05mY7coc433ASKljIWcqD7x2RKkQG70fKczLP4Uk83inkgeAreOFEvbdHn/zJWSj2KSyOq9TKz1wD+DjkaKEK0vSzO5duFCP2oom3tRuIxQQBVDRWz4zPuwTaq4I9BLMgQn/PQqH/hTQ3CjzCpMNbDQZZeeCughOb30WAbnGzym HC+qwmvD ltbVrDNh4ziu/cvCYOCIqLXCp6jyImNFkCBqlF38Ov5BWA9fJlUtCK4KwjhTDUsyx1X4kMm5BE92XbaU3Qr+7829lEzdTuxTZDy979U2qmHNV/iHXl9JO6glZnWHOq6seNIy3Yja4g6WCQSlCGM7YcntqwT7oIpFvq0uMCEBAhTe4utLgrMA+oug8gNfxiVhFfetHmA3exsItT5XjapeyjsplMyfXoM6Flp4i5Wex5fqL3MOII9jKOKBvwRIjNzwxxYImv2qKgofEVcp86NIlvqYFmNlVdTgoEdrt/X+9zZU8U0o= 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 core-mm changes in place to batch-clear ptes during zap_pte_range(), we can take advantage of this in arm64 to greatly reduce the number of tlbis we have to issue, and recover the lost performance in exit, munmap and madvise(DONTNEED) incured when adding support for transparent contiguous ptes. If we are clearing a whole contpte range, we can elide first unfolding that range and save the tlbis. We just clear the whole range. The following microbenchmark results demonstate the effect of this change on madvise(DONTNEED) performance for large pte-mapped folios. madvise(dontneed) is called for each page of a 1G populated mapping and the total time is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | dontneed | Apple M2 VM | Ampere Altra | | order-9 |-------------------|-------------------| | (pte-map) | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 7.9% | 0.0% | 0.0% | | before-change | -1.3% | 7.0% | 13.0% | 0.0% | | after-change | -9.9% | 0.9% | 14.1% | 0.0% | The memory is initially all contpte-mapped and has to be unfolded (which requires tlbi for the whole block) when the first page is touched (since the test is madvise-ing 1 page at a time). Ampere Altra has high cost for tlbi; this is why cost increases there. The following microbenchmark results demonstate the recovery (and overall improvement) of munmap performance for large pte-mapped folios. munmap is called for a 1G populated mapping and the function runtime is measured. 100 iterations per run, 8 runs performed on both Apple M2 (VM) and Ampere Altra (bare metal). Tests performed for case where 1G memory is comprised of pte-mapped order-9 folios. Negative is faster, positive is slower, compared to baseline upon which the series is based: | munmap | Apple M2 VM | Ampere Altra | | order-9 |-------------------|-------------------| | (pte-map) | mean | stdev | mean | stdev | |---------------|---------|---------|---------|---------| | baseline | 0.0% | 6.4% | 0.0% | 0.1% | | before-change | 43.3% | 1.9% | 375.2% | 0.0% | | after-change | -6.0% | 1.4% | -0.6% | 0.2% | Tested-by: John Hubbard Signed-off-by: Ryan Roberts --- arch/arm64/include/asm/pgtable.h | 42 +++++++++++++++++++++++++++++ arch/arm64/mm/contpte.c | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index d4805f73b9db..f5bf059291c3 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -953,6 +953,29 @@ static inline pte_t __ptep_get_and_clear(struct mm_struct *mm, return pte; } +static inline pte_t __clear_ptes(struct mm_struct *mm, + unsigned long address, pte_t *ptep, + unsigned int nr, int full) +{ + pte_t orig_pte = __ptep_get_and_clear(mm, address, ptep); + unsigned int i; + pte_t pte; + + for (i = 1; i < nr; i++) { + address += PAGE_SIZE; + ptep++; + pte = __ptep_get_and_clear(mm, address, ptep); + + if (pte_dirty(pte)) + orig_pte = pte_mkdirty(orig_pte); + + if (pte_young(pte)) + orig_pte = pte_mkyoung(orig_pte); + } + + return orig_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, @@ -1151,6 +1174,8 @@ 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 pte_t contpte_clear_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, @@ -1279,6 +1304,22 @@ static inline void pte_clear(struct mm_struct *mm, __pte_clear(mm, addr, ptep); } +#define clear_ptes clear_ptes +static inline pte_t clear_ptes(struct mm_struct *mm, + unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + pte_t pte; + + if (nr == 1) { + contpte_try_unfold(mm, addr, ptep, __ptep_get(ptep)); + pte = __ptep_get_and_clear(mm, addr, ptep); + } else + pte = contpte_clear_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) @@ -1366,6 +1407,7 @@ 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_ptes __clear_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 72e672024785..6f2a15ac5163 100644 --- a/arch/arm64/mm/contpte.c +++ b/arch/arm64/mm/contpte.c @@ -293,6 +293,51 @@ void contpte_set_ptes(struct mm_struct *mm, unsigned long addr, } EXPORT_SYMBOL(contpte_set_ptes); +pte_t contpte_clear_ptes(struct mm_struct *mm, unsigned long addr, pte_t *ptep, + unsigned int nr, int full) +{ + /* + * If we cover a partial contpte block at the beginning or end of the + * batch, unfold if currently folded. This makes it safe to clear some + * of the entries while keeping others. contpte blocks in the middle of + * the range, which are fully covered don't need to be unfolded because + * we will clear the full block. + */ + + unsigned int i; + pte_t pte; + pte_t tail; + + if (!mm_is_user(mm)) + return __clear_ptes(mm, addr, ptep, nr, full); + + 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)) + contpte_try_unfold(mm, addr + PAGE_SIZE * (nr - 1), + ptep + nr - 1, + __ptep_get(ptep + nr - 1)); + + pte = __ptep_get_and_clear(mm, addr, ptep); + + for (i = 1; i < nr; i++) { + addr += PAGE_SIZE; + ptep++; + + tail = __ptep_get_and_clear(mm, addr, ptep); + + if (pte_dirty(tail)) + pte = pte_mkdirty(pte); + + if (pte_young(tail)) + pte = pte_mkyoung(pte); + } + + return pte; +} +EXPORT_SYMBOL(contpte_clear_ptes); + int contpte_ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep) {