From patchwork Wed Dec 20 05:18:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nanyong Sun X-Patchwork-Id: 13499491 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 A6ADBC3DA6E for ; Wed, 20 Dec 2023 04:21:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41FBD8D0006; Tue, 19 Dec 2023 23:21:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3AB968D0001; Tue, 19 Dec 2023 23:21:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 187DD8D0006; Tue, 19 Dec 2023 23:21:49 -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 094938D0001 for ; Tue, 19 Dec 2023 23:21:49 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D0BDC4035B for ; Wed, 20 Dec 2023 04:21:48 +0000 (UTC) X-FDA: 81585898296.05.C3A6E86 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf14.hostedemail.com (Postfix) with ESMTP id 2E06B100007 for ; Wed, 20 Dec 2023 04:21:45 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.187 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=1703046107; 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=sdpGPIry7Om9J5JJWjA8qr98spmVPAsS0vg4JpIjNWw=; b=wfmR2XSSdYivjewD9wAOF16NfYdBKdQNZcRIN5NkGFa7hl/W2EgLpuNtWT2EbRZKWYjIZi 8WT/bMCPOyYu+6W0RSqaGgrH4Lep6mdvQqMwT8FiakC7cajigxhKg+RtLuF95CPN6XrEeQ RT4dTlj/gzMYDMk67Tj1pL+hLsXsMDE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703046107; a=rsa-sha256; cv=none; b=VaVqY+9bpc2e4B1S5dv3VaGnPSpEUtxVYwUCrJmQNyEaVDBLVEOj7xbfErk+WYS4hggnqE Vd/5wnewu0mw/8HseR8yGVfocLyBBdg2oLa+wKn7+jbxOEW9C/cAd2uAfJNrZS+gVjZ7Aj 1VH6jWuBd2MCvbTltPwLHJwYSl7Ty1k= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.187 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.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4Sw0kF2gX3zsSDx; Wed, 20 Dec 2023 12:21:25 +0800 (CST) Received: from kwepemm000003.china.huawei.com (unknown [7.193.23.66]) by mail.maildlp.com (Postfix) with ESMTPS id 0F7BB18007A; Wed, 20 Dec 2023 12:21:42 +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; Wed, 20 Dec 2023 12:21:41 +0800 From: Nanyong Sun To: , , , , , CC: , , , , , Subject: [PATCH v2 1/3] mm: HVO: introduce helper function to update and flush pgtable Date: Wed, 20 Dec 2023 13:18:53 +0800 Message-ID: <20231220051855.47547-2-sunnanyong@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231220051855.47547-1-sunnanyong@huawei.com> References: <20231220051855.47547-1-sunnanyong@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm000003.china.huawei.com (7.193.23.66) X-Rspamd-Queue-Id: 2E06B100007 X-Rspam-User: X-Stat-Signature: j7xmxmg4kj7yuntxoc6gp55znnddn5fk X-Rspamd-Server: rspam03 X-HE-Tag: 1703046105-91407 X-HE-Meta: U2FsdGVkX1/m53cUc7VrHFOkJs09AVIz2stLnWvU6GulBVctlMj9D6ShPmYlJD2usVc786275LR7mJCJSRWWX3VWVl6FxN6+L8u9Sj/1xaPSAeoe0hMu5HLyzhiXj7WmxiOiS/ynXNTn+czdtsBzQQeUy83+hekoKxNzSlObPEfwRSRWfW0flANAlGAq7GUtSBo+m3lRsJeccY1btsxYHAXOjo0O84EcK9hrF57QuMdpdDDPW4DGhSpgQgH1get0An3sBWahVbq/c1RTKESzWtrTkWXXNn2+GLIv3UtqbQk5n2vDEbjAl53kIRgaesYbBezZCdhPs3zNKFLjCXe1BYwb/K7/yvi1tVayStY0is5Hm8/sz/82x8ziIZQnF0JgGrzz3QxUNPjocGtxRcz5/FpFWwSdqE3BNdALPb7X+X+2zxZ34NntCrLbmbTVs5TQVkaOq7eQn8qy06lRqCeW/90aKIbiZflTitYkMENbLH/BwnuMKkWmJuUdlG1+nWPmi0rToQNqZ3ynKL83ptYtwMthfb+G3DLbM5lJE1ANGXzutCRx2WHcFcIs2y2AP0+v7LNtgdZBR3BI4glT/9Uc9y4e9yyTxrcLOHjbUgIkgCh7JXzmDE3kpWmBNsRpk7vnp4PCEqFRFcKBUm2wiSjJ0h7+qQYE0oAy4V0+ZNvDYJpORQqOAmLVJoiE4ppubuqRYW9mPYSMUwldulCzRgghpyM/Wlp64IUJD97qroeq8c1gcS6F4/EsDwazkvhlzr2r8ci05809UG6ZizowBotfI3nxogF6gH88b8Pen9WXOZk3RqTg91z9JwYPsAkoE8esaobt7R9dt5BsKJYxm+i+NmweEvAJ3NRPVSg3RHlCPZBHgdTwHjUf1dfzGVAXB5SNoivEHrRNsDSgmW/Wfetz8iuYTL6A5FW2myW/yR8755iqxB4JUqCMTfaN6oJQy/JLCXSSx6EF7cgiGPxFNML 2g3xsSjj EdnoYyxw12v7akybktCTlAHIkf6v3WbKk++H3kgow+S/KXXaMlQbpJEk9aRiQa375Y6VxURgbmRL3Xx5Ak4MzCZ/1lfmgxTDODMUkAKYRT3z1ngXNXlIk1OFqh/Th1RxV8ZTAskzVYO2LjUCNnVULAwWG93tzaVXF88WesfF0Kt7VEM7r/ub/T/TOd5E7wSrI0VsLo0ilZd4Zkgs= 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..2187e5410a94 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 addr, + pmd_t *pmdp, pte_t *ptep) +{ + pmd_populate_kernel(&init_mm, pmdp, ptep); +} +#endif + +#ifndef vmemmap_update_pte +static inline void vmemmap_update_pte(unsigned long addr, + pte_t *ptep, pte_t pte) +{ + set_pte_at(&init_mm, addr, ptep, pte); +} +#endif + +#ifndef vmemmap_flush_tlb_all +static inline void vmemmap_flush_tlb_all(void) +{ + flush_tlb_all(); +} +#endif + +#ifndef vmemmap_flush_tlb_range +static inline void vmemmap_flush_tlb_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); + vmemmap_flush_tlb_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); + vmemmap_flush_tlb_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(); + vmemmap_flush_tlb_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(); + vmemmap_flush_tlb_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(); + vmemmap_flush_tlb_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(); + vmemmap_flush_tlb_all(); free_vmemmap_page_list(&vmemmap_pages); } From patchwork Wed Dec 20 05:18:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nanyong Sun X-Patchwork-Id: 13499493 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 EDA23C3DA6E for ; Wed, 20 Dec 2023 04:21:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A9BA8D0008; Tue, 19 Dec 2023 23:21:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 531EE8D0007; Tue, 19 Dec 2023 23:21:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 336C98D0008; Tue, 19 Dec 2023 23:21:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1E87E8D0007 for ; Tue, 19 Dec 2023 23:21:50 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E27AF804BD for ; Wed, 20 Dec 2023 04:21:49 +0000 (UTC) X-FDA: 81585898338.27.DF80F34 Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) by imf09.hostedemail.com (Postfix) with ESMTP id 34BE214000E for ; Wed, 20 Dec 2023 04:21:46 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.191 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=1703046107; 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=4f6q8e8ZnWQBBBGZG3cmUvrK1WMxuvb6B/5nkofrYoE=; b=THRTZg7YoYCFQMpMR90k1CMYB5MTnuo4ilGBq09rvrfABaqPWBwwnvQ6oSUJbP4BIPxgG7 Q5t0740DVmMqGdqmOgnPgHDBcLMH1Pegln5mR1FwyuVRMxaEQTAFJPdWDD+4ovMjnciW6K cFrxIHRJ+KliVtQcINnPKvLKt0dJGzU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf09.hostedemail.com: domain of sunnanyong@huawei.com designates 45.249.212.191 as permitted sender) smtp.mailfrom=sunnanyong@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1703046108; a=rsa-sha256; cv=none; b=hzc1SwakReH36mSBPhKmaxbyY28awfTX2KDi1TAja6QRYNEaPFW1FK7L04QNi1Bp8hyxlK QEjGZQ33JZRJCDnerPZSK96XUoh6GXOQHmFqcUQM+Td758NpSWu7YYzHADW73E678ptepx HWhtvBpBu9O39LZHSOpvCohUNLfQy7o= Received: from mail.maildlp.com (unknown [172.19.163.44]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Sw0j95d38z1fyjT; Wed, 20 Dec 2023 12:20:29 +0800 (CST) Received: from kwepemm000003.china.huawei.com (unknown [7.193.23.66]) by mail.maildlp.com (Postfix) with ESMTPS id EA10B140412; Wed, 20 Dec 2023 12:21:42 +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; Wed, 20 Dec 2023 12:21:41 +0800 From: Nanyong Sun To: , , , , , CC: , , , , , Subject: [PATCH v2 2/3] arm64: mm: HVO: support BBM of vmemmap pgtable safely Date: Wed, 20 Dec 2023 13:18:54 +0800 Message-ID: <20231220051855.47547-3-sunnanyong@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231220051855.47547-1-sunnanyong@huawei.com> References: <20231220051855.47547-1-sunnanyong@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm000003.china.huawei.com (7.193.23.66) X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 34BE214000E X-Stat-Signature: 819mj3nhtx9os7u9kfu45b135takjb9b X-HE-Tag: 1703046106-439270 X-HE-Meta: U2FsdGVkX1+/js8/l6OD0HAosb56pULE80//lNm6QvLAL1Q3VFku9rJzNbGEx4TvV0vsM95wNYH7Df5Ki3Y/Mym57R76pQ9+KzrXhiY7tTTq5c2rfpDl4EoZHDNoufSDhm+WMsmRxsPE0Obv5APkZSPN3haIDkBZfn2DXKqftuglApoSE854yxWcYqixAOgIuxyR/xB5a9yoJGwPYaFzzirlLdnFDPSRPwwX14IB/o58EHbplumNAPzurw1plX0q142e6tq5XoUrqKrZYNvKypzkdeBBKroV2cQTFuEhQJVdXB1FNY+ohM+EV9s6FoRk+sA7Za/l3wAza4FHT2yaV1mlnRdoQ9Neks6b+2pfeYk7cb0FlMp3PwpTv1mG/63V0H99qX4xXlcfxAsomsHibJdyosC1VV9CNYIia3q8tvc7D9JcILehPYo8Pz5zw1Tl0O0sgxRAXiTTLJF0DHJPivcGtYDzdU+eNAJqsIz8638zDZUMZeq2MuVaWgNBVK49M7gfClMMrKGbTJTrmuu7XIK0KL+Pe9xRJbbWHVcnfp76zQqYvur7ikkcbRr77z8C5YZhvCqJw6PR+7HoiXFH2W7obx8opRW0RcsHaJCXYhh+juQSyT2cIc3vgdNws00inkNR59ifj7zwV6XKQIfVGbUXodngLPxIEYcr4rnfexTATbEBF967VpRs1JxkPr8Tbzl8ZIUCM86QdVH+aYXNZ6C1hr29kqqdvzNDOq2njW6Iaurnanq2Wro7wgiqD5jT8IZjqva4L+zcRsxap4m2MqVh231GpEh9yj+dOf/dNARzxU41fDwGBLbJrv08ugXnRwbdlwG9HX2WbDqdkKe/8a9Iz5k1/E6QAwxsMXUwRPd8timnuuHI+q+HpIa/D+Rf9y42IAoq//2Ln77GVGefEn2mh4RvGZ3dhtZlNGLwpUfB47kUOVULavgYiOcbjzIc1UTCInvZyXCKNiLd9Ae Rh6avz8/ SdX0xE6feM/kplUeR2RnTGcabwopdPy70GM50nkBrAu12Dd4tEMGoqwY8Jw9aiwTVe/jiGWhkijK0o4Ju0zIp0pVOSgTa1Neg5zNr57L/K1gSDe6KcCt8ws3516/2b/HMrdj3wfLpAPaaR0191WDpRGeHpg== 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 vmemmap_flush_tlb_all/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 | 78 ++++++++++++++++++++++++++++++++++-- arch/arm64/mm/mmu.c | 28 +++++++++++++ 4 files changed, 127 insertions(+), 3 deletions(-) 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..b553bc37c925 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 addr, pmd_t *pmdp, pte_t *ptep); +#define vmemmap_update_pmd vmemmap_update_pmd +void vmemmap_update_pte(unsigned long addr, pte_t *ptep, pte_t pte); +#define vmemmap_update_pte vmemmap_update_pte + +static inline void vmemmap_flush_tlb_all(void) +{ + /* do nothing, already flushed tlb in every single BBM */ +} +#define vmemmap_flush_tlb_all vmemmap_flush_tlb_all + +static inline void vmemmap_flush_tlb_range(unsigned long start, + unsigned long end) +{ + /* do nothing, already flushed tlb in every single BBM */ +} +#define vmemmap_flush_tlb_range vmemmap_flush_tlb_range +#endif + #endif /* !__ASSEMBLY__ */ #endif diff --git a/arch/arm64/mm/fault.c b/arch/arm64/mm/fault.c index 460d799e1296..21ac2cba66a3 100644 --- a/arch/arm64/mm/fault.c +++ b/arch/arm64/mm/fault.c @@ -368,6 +368,75 @@ static bool is_el1_mte_sync_tag_check_fault(unsigned long esr) return false; } +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +static inline bool vmemmap_fault_may_fixup(unsigned long addr, + unsigned long esr) +{ + if (addr < VMEMMAP_START || addr >= VMEMMAP_END) + 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) +{ + pmd_t *pmdp, pmd; + pte_t *ptep, pte; + + pmdp = pmd_off_k(addr); + pmd = pmdp_get(pmdp); + if (!pmd_table(pmd)) + return false; + + ptep = pte_offset_kernel(pmdp, addr); + pte = ptep_get(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; + + if (likely(!vmemmap_fault_may_fixup(addr, esr))) + return false; + + spin_lock(&init_mm.page_table_lock); + ret = vmemmap_addr_healthy(addr); + 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; @@ -405,9 +474,12 @@ static void __do_kernel_fault(unsigned long addr, unsigned long esr, } else if (addr < PAGE_SIZE) { msg = "NULL pointer dereference"; } else { - if (is_translation_fault(esr) && - kfence_handle_page_fault(addr, esr & ESR_ELx_WNR, regs)) - return; + if (is_translation_fault(esr)) { + if (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..e475495b6851 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 addr, pmd_t *pmdp, pte_t *ptep) +{ + lockdep_assert_held(&init_mm.page_table_lock); + pmd_clear(pmdp); + flush_tlb_kernel_range(addr, addr + PMD_SIZE); + pmd_populate_kernel(&init_mm, pmdp, ptep); +} + +void vmemmap_update_pte(unsigned long addr, pte_t *ptep, pte_t pte) +{ + spin_lock(&init_mm.page_table_lock); + pte_clear(&init_mm, addr, ptep); + flush_tlb_kernel_range(addr, addr + PAGE_SIZE); + set_pte_at(&init_mm, addr, ptep, pte); + 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 Wed Dec 20 05:18:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nanyong Sun X-Patchwork-Id: 13499494 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 8BD99C3DA6E for ; Wed, 20 Dec 2023 04:21:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BC6738D0009; Tue, 19 Dec 2023 23:21:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B4EED8D0007; Tue, 19 Dec 2023 23:21:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9ED968D0009; Tue, 19 Dec 2023 23:21:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 89BF98D0007 for ; Tue, 19 Dec 2023 23:21:50 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 63B324017D for ; Wed, 20 Dec 2023 04:21:50 +0000 (UTC) X-FDA: 81585898380.29.554DCC7 Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf17.hostedemail.com (Postfix) with ESMTP id 1B9824000F for ; Wed, 20 Dec 2023 04:21:47 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf17.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=1703046108; a=rsa-sha256; cv=none; b=ShKZAOHixYKjbNzDU2P4990nBIcaq+VMnaXdRBfTQ7IalFd4080x+yyAA97wS34ZjIAx7H YeCKP+FupEMCbDteTuEXGGZ1G4CEia+iEkOGIaCCtFxlLwMTa5LDSEN86g2Eb9/J6EGzQj m53MLUZhWRwp8VDG6sx9XW5XA8hkADc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf17.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=1703046108; 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=7EHEyYnASjLqkXz0Wx5vgCLPHOJjY4wdawhn5jGqED8=; b=iDJaUTJsGXJF5kE5meG1OawNYnQlv3MbbD3vWSZW9JYuHnE4OQp3TviCxvNsW+55dle+ec 12dO9ZoNvtr5wRsj67oePzoJouQ94jKr9/7JMth0KNgBJ10fobH+0w0T0Y48ueWWG7nC4M u0KuzTnzNkBbeYR4sXOAcnjOBegd1is= Received: from mail.maildlp.com (unknown [172.19.88.234]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4Sw0jB30t1z29fmG; Wed, 20 Dec 2023 12:20:30 +0800 (CST) Received: from kwepemm000003.china.huawei.com (unknown [7.193.23.66]) by mail.maildlp.com (Postfix) with ESMTPS id C22CD140499; Wed, 20 Dec 2023 12:21:43 +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; Wed, 20 Dec 2023 12:21:42 +0800 From: Nanyong Sun To: , , , , , CC: , , , , , Subject: [PATCH v2 3/3] arm64: mm: Re-enable OPTIMIZE_HUGETLB_VMEMMAP Date: Wed, 20 Dec 2023 13:18:55 +0800 Message-ID: <20231220051855.47547-4-sunnanyong@huawei.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231220051855.47547-1-sunnanyong@huawei.com> References: <20231220051855.47547-1-sunnanyong@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To kwepemm000003.china.huawei.com (7.193.23.66) X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 1B9824000F X-Stat-Signature: mcctbb81k838x6bscumnxtc8sahoxuec X-Rspam-User: X-HE-Tag: 1703046107-720140 X-HE-Meta: U2FsdGVkX19UoBCx2NQHgRAG1QxmUEQqFiVo2REZrfV8eKQHdFeJrVAV5K/zloEkPpTzKgWZMdnqO9XspjlAlFaXHm4ygllr4xYufEh4odOPAYaz4t0zH5en4liu6aMjffbyxD5bgTpWAdtQxInvztDhUqASE/Yyha1VwqSbFHW5wi2VoQP4ejNcuRJ7aAlKL/Y9dlDXhfqDw2zL4whOG5tooP8lK3N5iGq8DFAo8Ci2lm0KgY1insy/dTtpQn4BvQw8dNIfFYPi/oe7L3x6q/Sti7494mHhBA84luxWl8k8ZW8iQt+FQhZ2NQUm6pRBKiBcXBhMUfyB/LIig2+eVJsT3GCG8YtSdCXA1TBveROmX2pcTzlse/WuMofqwsXovDwCpgXq+rzae9Hyu7UpxNPwqAz07QDVbM6GCwJPiHltZZi4au7chEdFlo64dArUkQnc5JQysBTFWR3Z2cv/Ka8mYhHhl9NdAXGMKPjck8Uhw8+ZWo1VvXNzKNJcIAbyoZOOV9HC5h4SKHlM0yx4hdOAcTw3T/tzDN3DFpndKW2Abp82l2jRxgPQxzo7Gw6fcpCWidYKOGwiV97eRhn7VF5tYK+v/hKQYNTyUyXF21R9leThK5GhM+TDKhH2rCXL1++P9xMiUkQMRgcBbTG1MDRGoXx5W+9o6ukjCLsy3rXDTP8E72DMn59n7g8W0D9ifIEePjDBFqPY/b6xX/7v2LNAsSld7JiV/kPl7FtDA9IQHVRZhhRUhEY+1Q1cBHXudseDCQhj0Q4dxPUE5794xZ2kYf2q8GR4M9kFDFJhNdPRIhX7JiUlH9kY/fPdgaAGNyeopIR2Qy4o1WA3TFOOYoMx/2rYRHE4jFJQR8hRRXrM/T0z3/CtgtUso4t1LxtfuqnjdrZBMdU= 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