From patchwork Thu Dec 14 07:39:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nanyong Sun X-Patchwork-Id: 13492437 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 83838C4332F for ; Thu, 14 Dec 2023 06:41:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B419E8D0098; Thu, 14 Dec 2023 01:41:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AEFA18D009C; Thu, 14 Dec 2023 01:41:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9DDF98D0098; Thu, 14 Dec 2023 01:41:55 -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 887DC8D009C for ; Thu, 14 Dec 2023 01:41:55 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 616D1C0183 for ; Thu, 14 Dec 2023 06:41:55 +0000 (UTC) X-FDA: 81564478590.03.213E73F Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf11.hostedemail.com (Postfix) with ESMTP id D816F4000B for ; Thu, 14 Dec 2023 06:41:52 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=sunnanyong@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702536113; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OgJlxUQDILr7vk9eqhUikkWa51wWaYZkIB9AjYwmae4=; b=lWceXRmGTz7UZtXAyo3v7QqiT7ZtxJO39VSqwQG16Z64R63ZrVELYLfvmZkNs0k8F/Z6Ei XjYob2ZPtD2Co7MhpuP0EjsPiatA817rZniC5KhblBoWnhgmh1y0QNIeRlPmrMA+9x0vFX Ppr2UR6BRdQDprJcd7Ntf5v2pXLHFH8= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf11.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=sunnanyong@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702536113; a=rsa-sha256; cv=none; b=WwQmfuXzf6klRtnH2gThwb7c1YrDx+i295wbcbdEdZOtxfpfeYCQ5JVwkeVQOrWS816qr+ gDDvlZ8cWOmGx1MqIEkqjXvHtcN0gyd3IQ8aRImtL/j/P+TpY5Fn6voxylE59qCssU1fPY 1kJLgKxBnO/MyoywfQqp4x2RNgp0oBI= Received: from mail.maildlp.com (unknown [172.19.88.214]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4SrN5g0gYQz29fy0; Thu, 14 Dec 2023 14:40:39 +0800 (CST) Received: from kwepemm000003.china.huawei.com (unknown [7.193.23.66]) by mail.maildlp.com (Postfix) with ESMTPS id 5007C1A0190; Thu, 14 Dec 2023 14:41:47 +0800 (CST) Received: from huawei.com (10.175.113.32) by kwepemm000003.china.huawei.com (7.193.23.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 14 Dec 2023 14:41:46 +0800 From: Nanyong Sun To: , , , , , CC: , , , , , Subject: [PATCH 1/3] mm: HVO: introduce helper function to update and flush pgtable Date: Thu, 14 Dec 2023 15:39:10 +0800 Message-ID: <20231214073912.1938330-2-sunnanyong@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231214073912.1938330-1-sunnanyong@huawei.com> References: <20231214073912.1938330-1-sunnanyong@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm000003.china.huawei.com (7.193.23.66) X-Rspamd-Queue-Id: D816F4000B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 1rpgxtawgybt7uxs5aq1y6atpdr5dy1a X-HE-Tag: 1702536112-120567 X-HE-Meta: U2FsdGVkX19EjqGkxwgXn24fWDWgJXKFUZ/YptMP6tiJC5GJGZ8uRx+21acSgQnML9zkP+l0IqzQd/FgCC/q6HY8rCI2oIOo6Yst4HBXGurO3jUyX7dn1HOF1ScmyeMQ/cbUrpUxAouSMoy52TALnWmpHhECNl3d9DrhvSV5yW0tXKNdzucN1lgwrpS4khMLnQPYeKb1ohcjYs7tlLvbWQDtnfy2H7VF4MACt9UyBX/ZYUJEeOXaAeful2UPLsBNZyEsJTplIFRJDNlRy5QI4ly5BZ8lvO2W9yuQ7TpO8aFOorT6ksahsiftPlh41kNa/EboX2inHHLuFAfZVN5ORdqrbn6osewdKPqmHnv+45dLf8TaRRd8dPP58fhb1oBp8QJX0xuwSeFqQflp7dH9/veRZvEpMdArEVUPqkkgqYNxKGnepoJlVNupxyX7Px1M20kDyw4wzoz4+sN5h4TzVHkIQq0GsaArPN+6uFvDHyOTypk1pho2I6BZts77CPEQ93Gglr8LnGW8SoA8U54K/Im8wSk09S72DWt+4Q1dp1syXwTMJ35eCpWaK4ZHA35tjbU5YGpI1f9emU963RMSfiC2w3EWeYoN4Re1hsAVceyKJ7bJsoTAfHixBiNC3mmxr93tP6DQ2MPzE1az/L7p204Q1mKj+XVIHzFKk3q/yEnKS0ouoqdyFZQdkxjkQQZZAVQt9lDGIs461iuOvUoTp+BD1XLU66puilfReVg2Kma/dR2H1BRSTR9KwNKt/dbwBhcP2oQozbme8n6S0gBkVVHuFvSTjgRt7IWNuzpoZ8JKsFtBGAz0eCXN3/kcw6y2SEVkRsYmMDcvJR6ohIaMoVQXt831pgwL3qj6nfi5h2BDmQr7dvZdxJeId8cf2ebr8L6fRg9GdW15BJrir353vhkVw6FD5mSPIyw5Dove0dEQVXdAMdlVf9ZwLPJL7GusR1UcHCe+gk7TDBJou7K Md1F0OVq tXIXSrV9vyuWS3po3Q+YSLdGxU4Qrn9jUosCSfSm4GR0pHctGp0yBnNkg57Dsrtfq3wCxVGW9Pho92Lu/EOmePYaof9MccVKoNi8rS5BcMcXE0XZFKkXZJtRbA5Ad/itLoLyoVEbIfSltbPSrJozDGgB35g== 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: Add pmd/pte update and tlb flush helper function to update page table. This refactoring patch is designed to facilitate each architecture to implement its own special logic in preparation for the arm64 architecture to follow the necessary break-before-make sequence when updating page tables. Signed-off-by: Nanyong Sun Reviewed-by: Muchun Song --- mm/hugetlb_vmemmap.c | 55 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 87818ee7f01d..49e8b351def3 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -45,6 +45,37 @@ struct vmemmap_remap_walk { unsigned long flags; }; +#ifndef vmemmap_update_pmd +static inline void vmemmap_update_pmd(unsigned long start, + pmd_t *pmd, pte_t *pgtable) +{ + pmd_populate_kernel(&init_mm, pmd, pgtable); +} +#endif + +#ifndef vmemmap_update_pte +static inline void vmemmap_update_pte(unsigned long addr, + pte_t *pte, pte_t entry) +{ + set_pte_at(&init_mm, addr, pte, entry); +} +#endif + +#ifndef flush_tlb_vmemmap_all +static inline void flush_tlb_vmemmap_all(void) +{ + flush_tlb_all(); +} +#endif + +#ifndef flush_tlb_vmemmap_range +static inline void flush_tlb_vmemmap_range(unsigned long start, + unsigned long end) +{ + flush_tlb_kernel_range(start, end); +} +#endif + static int split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start, bool flush) { pmd_t __pmd; @@ -87,9 +118,9 @@ static int split_vmemmap_huge_pmd(pmd_t *pmd, unsigned long start, bool flush) /* Make pte visible before pmd. See comment in pmd_install(). */ smp_wmb(); - pmd_populate_kernel(&init_mm, pmd, pgtable); + vmemmap_update_pmd(start, pmd, pgtable); if (flush) - flush_tlb_kernel_range(start, start + PMD_SIZE); + flush_tlb_vmemmap_range(start, start + PMD_SIZE); } else { pte_free_kernel(&init_mm, pgtable); } @@ -217,7 +248,7 @@ static int vmemmap_remap_range(unsigned long start, unsigned long end, } while (pgd++, addr = next, addr != end); if (walk->remap_pte && !(walk->flags & VMEMMAP_REMAP_NO_TLB_FLUSH)) - flush_tlb_kernel_range(start, end); + flush_tlb_vmemmap_range(start, end); return 0; } @@ -263,15 +294,15 @@ static void vmemmap_remap_pte(pte_t *pte, unsigned long addr, /* * Makes sure that preceding stores to the page contents from - * vmemmap_remap_free() become visible before the set_pte_at() - * write. + * vmemmap_remap_free() become visible before the + * vmemmap_update_pte() write. */ smp_wmb(); } entry = mk_pte(walk->reuse_page, pgprot); list_add(&page->lru, walk->vmemmap_pages); - set_pte_at(&init_mm, addr, pte, entry); + vmemmap_update_pte(addr, pte, entry); } /* @@ -310,10 +341,10 @@ static void vmemmap_restore_pte(pte_t *pte, unsigned long addr, /* * Makes sure that preceding stores to the page contents become visible - * before the set_pte_at() write. + * before the vmemmap_update_pte() write. */ smp_wmb(); - set_pte_at(&init_mm, addr, pte, mk_pte(page, pgprot)); + vmemmap_update_pte(addr, pte, mk_pte(page, pgprot)); } /** @@ -576,7 +607,7 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h, } if (restored) - flush_tlb_all(); + flush_tlb_vmemmap_all(); if (!ret) ret = restored; return ret; @@ -744,7 +775,7 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l break; } - flush_tlb_all(); + flush_tlb_vmemmap_all(); list_for_each_entry(folio, folio_list, lru) { int ret = __hugetlb_vmemmap_optimize_folio(h, folio, @@ -760,7 +791,7 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l * allowing more vmemmap remaps to occur. */ if (ret == -ENOMEM && !list_empty(&vmemmap_pages)) { - flush_tlb_all(); + flush_tlb_vmemmap_all(); free_vmemmap_page_list(&vmemmap_pages); INIT_LIST_HEAD(&vmemmap_pages); __hugetlb_vmemmap_optimize_folio(h, folio, @@ -769,7 +800,7 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l } } - flush_tlb_all(); + flush_tlb_vmemmap_all(); free_vmemmap_page_list(&vmemmap_pages); } From patchwork Thu Dec 14 07:39:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nanyong Sun X-Patchwork-Id: 13492438 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 E0D00C4332F for ; Thu, 14 Dec 2023 06:42:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8823D8D009D; Thu, 14 Dec 2023 01:41:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 794B08D009C; Thu, 14 Dec 2023 01:41:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65D438D009D; Thu, 14 Dec 2023 01:41:56 -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 567398D009C for ; Thu, 14 Dec 2023 01:41:56 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2F4A3120483 for ; Thu, 14 Dec 2023 06:41:56 +0000 (UTC) X-FDA: 81564478632.30.729AE86 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf28.hostedemail.com (Postfix) with ESMTP id 8F77AC0009 for ; Thu, 14 Dec 2023 06:41:53 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=sunnanyong@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702536114; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RTOem4NWFP8g4Yoo/TlqaSw/FCROfYkCnwy3hQYUVc4=; b=JRNgPdlpSuvnXLEovHd5KJ6GSMtwTAJ3BB9RoQgEZXicJLG4cHmXLQ97SzZILiw3i3SoME +6vVD4lTc2CjOhsNwvXIIFO2FyE7OXs0uqmy1Nz+31HECWrL0gvx2hbO+tgYdiML6g6jnO YS/0As+tcU8KJYLbrRlvfKO47swqSSw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702536114; a=rsa-sha256; cv=none; b=01L/Ns5Sz+THbGk7pFxrSNr6CeNffA61qROHemcU/zix6+Ro5xtYiGEHee8LSwedEPcetu 3Coj5UCKP1CowqdOeyaOCgNAothKShgxBi3uodWQOzHtBgqDbBFUDDUgcaUO2x6hw27sUD i6zslRWch6yBb85dt7/sHIZCr0UlAtY= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=sunnanyong@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.17]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4SrN2q1Gx2z1FDx3; Thu, 14 Dec 2023 14:38:11 +0800 (CST) Received: from kwepemm000003.china.huawei.com (unknown [7.193.23.66]) by mail.maildlp.com (Postfix) with ESMTPS id 4017C1A0597; Thu, 14 Dec 2023 14:41:48 +0800 (CST) Received: from huawei.com (10.175.113.32) by kwepemm000003.china.huawei.com (7.193.23.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 14 Dec 2023 14:41:47 +0800 From: Nanyong Sun To: , , , , , CC: , , , , , Subject: [PATCH 2/3] arm64: mm: HVO: support BBM of vmemmap pgtable safely Date: Thu, 14 Dec 2023 15:39:11 +0800 Message-ID: <20231214073912.1938330-3-sunnanyong@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231214073912.1938330-1-sunnanyong@huawei.com> References: <20231214073912.1938330-1-sunnanyong@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm000003.china.huawei.com (7.193.23.66) X-Stat-Signature: ynep5tyz5amg198161q6k9hgd9s6xrim X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8F77AC0009 X-Rspam-User: X-HE-Tag: 1702536113-214239 X-HE-Meta: U2FsdGVkX1/MOfz4pak0I6kpAqrg81JG6bpUgLj/AGV8n+4k1uKvGIYAbwo7usV/8jr2FnYDbjNkXH6yBEEvQOizwJC/fdtn1GFOd7Ru7GGzI1oQA6gEL3ifGBkdj7BD9KDWQuKvEmMyArW6UF7FbqWl/JSDmgKEJMRnGyNRdBsT0SidbnaSx1HOc1M7e04deB5Wy9mVXjCzvGAVr213MFWZbp3Cl9WBcW6WLIbgTlyHfix9RnulwQyNuJH91shj2JkGmbkCGzFSlKMXg399Jc8nNYUfJMiOxp5KU9fotd93ZS1qo/2CIcRYU0EXSshSbbb62eE2ioWCmYnwLNCHwX8gUVKDr3p56a/qeYy2wQJ+6UAVp/mwTwuDEED0guAhjFXoAAmRqUMbJFFFptqJiHtxRx4txxJ947mrDCUEwoQ8H82bPwUxtqWWEkZ6si/LA4Whx0c6fWzxy56LpSdhe/aMN5u/exctp5rUCnNxxiJWwTov+iuImUT7To1ZLdWpsBQZTv66T0e61HS2yNTDbB65EX3eKKiGFdJipsTEgNUA1QT1GsttEhpizfjpv77zm6NnV+RQcWaT+1niUvHU+J9xIzEci/BqxpB1lZpAyJztJHyeAKV6dUEFKj0JUB9+tFh3k9ZKdU2rImtVLexvlywF7qX1qAUxo+hw0drNsNfmw4lvq9z5GD1XN+3X2ma4KT26LscSoMZqpl+ZWqzP9uSyHIV8ZEAW1OkbUeqytBx0Ti3jjbHudVhnyaHtUVD138zCVhX+8xy/xfbm19U+uALyZWt7k4RvxR1UkD6Jof1XXguSqgJyN7F+FcdLZ2CfHQWwNmbvwwa05LZdUFMfcrj+VTRE0n3JUoWcNGS5hmzjTNuCGlBsBZeOWbioOg7YWXwIFcZjbZKJzjoG+Pt/KmXZGZoy/E2YOv8HgxBCujSBWZZJA/cyXfxT/LXVm8ATG29paWdj0KD9qtp+9pK PFyaD+CP d55NKjpl8UWbKMMhnvwjdvsrHvPe25/0sJ0Y1GdGfna4SRzj3qR+PNVBHcKawAgzy6I1FjxQ4Wedv14Mt0V/10+XVCZ3bcdNYtUpavViPID/softPFd/s1T1UklkYfKE8PSB/0T4+PJMtmJuN+/LxtoqKbw== 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: Implement vmemmap_update_pmd and vmemmap_update_pte on arm64 to do BBM(break-before-make) logic when change the page table of vmemmap address, they will under the init_mm.page_table_lock. If a translation fault of vmemmap address concurrently happened after pte/pmd cleared, vmemmap page fault handler will acquire the init_mm.page_table_lock to wait for vmemmap update to complete, by then the virtual address is valid again, so PF can return and access can continue. In other case, do the traditional kernel fault. Implement flush_tlb_vmemmap_all and flush_tlb_vmemmap_range on arm64 with nothing to do because tlb already flushed in every single BBM. Signed-off-by: Nanyong Sun --- arch/arm64/include/asm/esr.h | 4 ++ arch/arm64/include/asm/mmu.h | 20 ++++++++ arch/arm64/mm/fault.c | 94 ++++++++++++++++++++++++++++++++++++ arch/arm64/mm/mmu.c | 28 +++++++++++ 4 files changed, 146 insertions(+) diff --git a/arch/arm64/include/asm/esr.h b/arch/arm64/include/asm/esr.h index ae35939f395b..1c63256efd25 100644 --- a/arch/arm64/include/asm/esr.h +++ b/arch/arm64/include/asm/esr.h @@ -116,6 +116,10 @@ #define ESR_ELx_FSC_SERROR (0x11) #define ESR_ELx_FSC_ACCESS (0x08) #define ESR_ELx_FSC_FAULT (0x04) +#define ESR_ELx_FSC_FAULT_L0 (0x04) +#define ESR_ELx_FSC_FAULT_L1 (0x05) +#define ESR_ELx_FSC_FAULT_L2 (0x06) +#define ESR_ELx_FSC_FAULT_L3 (0x07) #define ESR_ELx_FSC_PERM (0x0C) #define ESR_ELx_FSC_SEA_TTW0 (0x14) #define ESR_ELx_FSC_SEA_TTW1 (0x15) diff --git a/arch/arm64/include/asm/mmu.h b/arch/arm64/include/asm/mmu.h index 2fcf51231d6e..fcec5827f54f 100644 --- a/arch/arm64/include/asm/mmu.h +++ b/arch/arm64/include/asm/mmu.h @@ -76,5 +76,25 @@ extern bool kaslr_requires_kpti(void); #define INIT_MM_CONTEXT(name) \ .pgd = init_pg_dir, +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +void vmemmap_update_pmd(unsigned long start, pmd_t *pmd, pte_t *pgtable); +#define vmemmap_update_pmd vmemmap_update_pmd +void vmemmap_update_pte(unsigned long addr, pte_t *pte, pte_t entry); +#define vmemmap_update_pte vmemmap_update_pte + +static inline void flush_tlb_vmemmap_all(void) +{ + /* do nothing, already flushed tlb in every single BBM */ +} +#define flush_tlb_vmemmap_all flush_tlb_vmemmap_all + +static inline void flush_tlb_vmemmap_range(unsigned long start, + unsigned long end) +{ + /* do nothing, already flushed tlb in every single BBM */ +} +#define flush_tlb_vmemmap_range flush_tlb_vmemmap_range +#endif + #endif /* !__ASSEMBLY__ */ #endif diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 460d799e1296..7066a273c1e0 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -368,6 +368,97 @@ static bool is_el1_mte_sync_tag_check_fault(unsigned long esr) return false; } +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +static inline bool is_vmemmap_address(unsigned long addr) +{ + return (addr >= VMEMMAP_START) && (addr < VMEMMAP_END); +} + +static inline bool vmemmap_fault_may_fixup(unsigned long addr, + unsigned long esr) +{ + if (!is_vmemmap_address(addr)) + return false; + + /* + * Only try to handle translation fault level 2 or level 3, + * because hugetlb vmemmap optimize only clear pmd or pte. + */ + switch (esr & ESR_ELx_FSC) { + case ESR_ELx_FSC_FAULT_L2: + case ESR_ELx_FSC_FAULT_L3: + return true; + default: + return false; + } +} + +/* + * PMD mapped vmemmap should has been split as PTE mapped + * by HVO now, here we only check this case, other cases + * should fail. + * Also should check the addr is healthy enough that will not cause + * a level2 or level3 translation fault again after page fault + * handled with success, so we need check both bits[1:0] of PMD and + * PTE as ARM Spec mentioned below: + * A Translation fault is generated if bits[1:0] of a translation + * table descriptor identify the descriptor as either a Fault + * encoding or a reserved encoding. + */ +static inline bool vmemmap_addr_healthy(unsigned long addr) +{ + pgd_t *pgdp; + p4d_t *p4dp; + pud_t *pudp, pud; + pmd_t *pmdp, pmd; + pte_t *ptep, pte; + + pgdp = pgd_offset_k(addr); + if (pgd_none(READ_ONCE(*pgdp))) + return false; + + p4dp = p4d_offset(pgdp, addr); + if (p4d_none(READ_ONCE(*p4dp))) + return false; + + pudp = pud_offset(p4dp, addr); + pud = READ_ONCE(*pudp); + if (pud_none(pud)) + return false; + + pmdp = pmd_offset(pudp, addr); + pmd = READ_ONCE(*pmdp); + if (!pmd_table(pmd)) + return false; + + ptep = pte_offset_kernel(pmdp, addr); + pte = READ_ONCE(*ptep); + return (pte_val(pte) & PTE_TYPE_MASK) == PTE_TYPE_PAGE; +} + +static bool vmemmap_handle_page_fault(unsigned long addr, + unsigned long esr) +{ + bool ret = false; + + if (likely(!vmemmap_fault_may_fixup(addr, esr))) + return false; + + spin_lock(&init_mm.page_table_lock); + if (vmemmap_addr_healthy(addr)) + ret = true; + spin_unlock(&init_mm.page_table_lock); + + return ret; +} +#else +static inline bool vmemmap_handle_page_fault(unsigned long addr, + unsigned long esr) +{ + return false; +} +#endif /*CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */ + static bool is_translation_fault(unsigned long esr) { return (esr & ESR_ELx_FSC_TYPE) == ESR_ELx_FSC_FAULT; @@ -409,6 +500,9 @@ static void __do_kernel_fault(unsigned long addr, unsigned long esr, kfence_handle_page_fault(addr, esr & ESR_ELx_WNR, regs)) return; + if (vmemmap_handle_page_fault(addr, esr)) + return; + msg = "paging request"; } diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 15f6347d23b6..81a600ccac7c 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -1146,6 +1146,34 @@ int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node, return 1; } +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +/* + * In the window between the page table entry is cleared and filled + * with a new value, other threads have the opportunity to concurrently + * access the vmemmap area then page translation fault occur. + * Therefore, we need to ensure that the init_mm.page_table_lock is held + * to synchronize the vmemmap page fault handling which will wait for + * this lock to be released to ensure that the page table entry has been + * refreshed with a new valid value. + */ +void vmemmap_update_pmd(unsigned long start, pmd_t *pmd, pte_t *pgtable) +{ + lockdep_assert_held(&init_mm.page_table_lock); + pmd_clear(pmd); + flush_tlb_kernel_range(start, start + PMD_SIZE); + pmd_populate_kernel(&init_mm, pmd, pgtable); +} + +void vmemmap_update_pte(unsigned long addr, pte_t *pte, pte_t entry) +{ + spin_lock(&init_mm.page_table_lock); + pte_clear(&init_mm, addr, pte); + flush_tlb_kernel_range(addr, addr + PAGE_SIZE); + set_pte_at(&init_mm, addr, pte, entry); + spin_unlock(&init_mm.page_table_lock); +} +#endif + int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, struct vmem_altmap *altmap) { From patchwork Thu Dec 14 07:39:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nanyong Sun X-Patchwork-Id: 13492436 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 0E7EAC4167B for ; Thu, 14 Dec 2023 06:41:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 684FB8D009B; Thu, 14 Dec 2023 01:41:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6341C8D0098; Thu, 14 Dec 2023 01:41:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D5548D009C; Thu, 14 Dec 2023 01:41:55 -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 355AA8D009B for ; Thu, 14 Dec 2023 01:41:55 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id F3CF0A1CAD for ; Thu, 14 Dec 2023 06:41:54 +0000 (UTC) X-FDA: 81564478548.04.0D82E72 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf09.hostedemail.com (Postfix) with ESMTP id 9543314000A for ; Thu, 14 Dec 2023 06:41:52 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=sunnanyong@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702536113; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RbqiuXsHA2FdxHJz22f/UdGbBmiJ/DwpGUMHSWGjULA=; b=eR7BFszmxKrOSq6WlIHyJ1tdR+pdg3PS3/SzrRHt0aOrr+83aYDZTkvJerFXEmXOkND53J sQufR8AS2J8hKDEgIm/qUT2WzC7VdVUAxLL5QF70LzAvtWexeYwR4+KawRyTnaO8pgebbd 5vSe3w/cT3QLMIU1pZ5q5kGjE4hkggw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702536113; a=rsa-sha256; cv=none; b=WZorIZvNgudvUipdESiAx0pc4Xs2x8vErskNygJdihOyHAuxX2O5nL/N3wKYQjMw25na3E lcPk7BGYivqCjAmUqJN+wZwMF0gVBcR0X6B4BR7dh0EVBABCj0RiAn5IViYuNQ7crOOZ9N EgX0ACkUvUo6vyvhQM59fUWKST9/Qjc= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; spf=pass (imf09.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.190 as permitted sender) smtp.mailfrom=sunnanyong@huawei.com; dmarc=pass (policy=quarantine) header.from=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.112]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4SrN5h6bP5z29g0v; Thu, 14 Dec 2023 14:40:40 +0800 (CST) Received: from kwepemm000003.china.huawei.com (unknown [7.193.23.66]) by mail.maildlp.com (Postfix) with ESMTPS id 26D4514038F; Thu, 14 Dec 2023 14:41:49 +0800 (CST) Received: from huawei.com (10.175.113.32) by kwepemm000003.china.huawei.com (7.193.23.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Thu, 14 Dec 2023 14:41:48 +0800 From: Nanyong Sun To: , , , , , CC: , , , , , Subject: [PATCH 3/3] arm64: mm: Re-enable OPTIMIZE_HUGETLB_VMEMMAP Date: Thu, 14 Dec 2023 15:39:12 +0800 Message-ID: <20231214073912.1938330-4-sunnanyong@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231214073912.1938330-1-sunnanyong@huawei.com> References: <20231214073912.1938330-1-sunnanyong@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To kwepemm000003.china.huawei.com (7.193.23.66) X-Rspamd-Queue-Id: 9543314000A X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: i8jk9mu7tik8thshfeq9859z54cin7qr X-HE-Tag: 1702536112-694405 X-HE-Meta: U2FsdGVkX1+FSKxrgM+2lxGytrSTr+wXFxz4TUpe8OQoDiUqJu9Yo3PT3zknTNhs2PA35SNxcYYrlOpAuzK2hX87FHR3RL+zRgi+kPuSl9getSNs4QNuqW6Pae8ZYHjHWGtvZH0URYiMxYmJU9B+mCd+X4PhcNdQS616JxDHGGbfYeOIE0htuJNkhcmqNoxDvXLl7z9p1JVe0XmX72gLX4XOsstTsCIpr2FxQK3QyP8SM2AVs6BknQmgqDNpx2mObDNfa1B5rUKFFnmEJIT7NLJ9SSOQFg6oY3pAnWSbnb6I4NXrhb3E+8R/qUPrV6gYeaAWHI80CexqN+p8snwV1TgS9pBxXueQOGgBr2Q5wlpWD/idtDFtB9zyH2cozWAhai9ZjgP0kNizMCSrmDBZ4oZh1rUnfBOLNOn0YIgsL7HTi/AIaKAsfVv6ZQD7eUnSUmlUS1L1msz/5nHz+WycEs2fw4jOMdUyMiqcfW/yi1mx4heBp7SOIrmm+hR6t2x6JgzTyZFgIUpJ73w2xWFJec3/mCrpxE+wjmAZ5v5oNAABaw/6dK09kGDxhLdEYzGmQep0YfqgAFmlFBMnLru0qTChE+rCde58xTXleBUDMetgMtQ+1utujNPnshqmYoHIa6gYEXnuOUmkDNm2xRwLJVloohJ0xfOBqTckLxEioKKXFVELOO8JnqJiqU63QYdjA8/MZjtWCFZi+SCqBJ8+xVnDHVDyhu+Tk82YV6glpGg0TR0f0/hhflMNQFvASomH4MIXA+WuamTvAApcSGRNdkjtsP6pluvTVmeG14k0SK7cnBFox3KdP2GmwPSPQoOv9QR5UVkpZbkTLUX8p2jd7Wl7ar8kZwcozIHX0IBdhDFjnDQKiVMw7OqMllxhvPpsXqYvQ3g+ceJlSOnW3DBJMHXh4k04mFZhIQNzMee3y9Xyer99DTRvvd8Ctingq8PAqxvDeWo20jf33GK3Wav NkQMLyBS SL0VYj6BJaNmZInAQgIzRsbS1nGZWrX1OOTWCzfPVI5DwIeINmhNH28+5gWP4WkEWBBy4/+/+UD+nzhvDzyB6kjsMskhhO7cPZTpBIxsR3+D1vzM3GVwBeiX6SZeLE0f0VNDOyXx198x1ecCoZVdXvIrB6xcTAh5VZMfwwH4jkVyWQiB/qgvmBA0NJdeLxjwi/whT X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Now update of vmemmap page table can follow the rule of break-before-make safely for arm64 architecture, re-enable HVO on arm64. Signed-off-by: Nanyong Sun Reviewed-by: Muchun Song --- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7b071a00425d..43e3d5576fb2 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -103,6 +103,7 @@ config ARM64 select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_FRAME_POINTERS select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36) + select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANTS_NO_INSTR select ARCH_WANTS_THP_SWAP if ARM64_4K_PAGES