From patchwork Mon Oct 14 03:58:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: bibo mao X-Patchwork-Id: 13834138 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 9B83AD1A429 for ; Mon, 14 Oct 2024 03:59:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 204D26B0088; Sun, 13 Oct 2024 23:59:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B6896B0089; Sun, 13 Oct 2024 23:59:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED57B6B008A; Sun, 13 Oct 2024 23:59:05 -0400 (EDT) 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 CAA346B0088 for ; Sun, 13 Oct 2024 23:59:05 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B58F4C0AFD for ; Mon, 14 Oct 2024 03:58:57 +0000 (UTC) X-FDA: 82670852040.27.85E6D52 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by imf23.hostedemail.com (Postfix) with ESMTP id 8F2CA140005 for ; Mon, 14 Oct 2024 03:58:59 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf23.hostedemail.com: domain of maobibo@loongson.cn designates 114.242.206.163 as permitted sender) smtp.mailfrom=maobibo@loongson.cn ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728878203; 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=p7I8M5tc4Tck4U2HYdN2nAH2O+CY+L0GJMEbYTmhcQU=; b=cGvW9bVwn9ZX8j1sIeAwu8P7CuWxRsRpqJu3AE+XBQA/RnkdhPuWOxS7Ki27uDnxG4sHi7 WBn/uNWn9ZP7SNTh5TBQKapRTJ4LFyag+q9+ipNy0NiYRF609hvGYqxWAHd7RxItpycqHS pYza5wym00gwTMxxO039BtzV5TVFFg4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728878203; a=rsa-sha256; cv=none; b=gDUL+uHXaglSg24LfxFNDstr4liUcFSrFQfQcXYwXge0oI1EZvFRht8THh4je4or7rjRGz CW76f7DCJx324dqxnMyXGWdRieitiUi6jDGLfccvlkeIhDw3L9ODjXngcrRDcftHEXEwlM O+OC66Pim3LMtEvLTOHz5BftfrC9cZM= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf23.hostedemail.com: domain of maobibo@loongson.cn designates 114.242.206.163 as permitted sender) smtp.mailfrom=maobibo@loongson.cn Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8DxhbABlwxnfwIaAA--.37523S3; Mon, 14 Oct 2024 11:58:57 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowMBxXuT_lgxnc6EoAA--.1717S3; Mon, 14 Oct 2024 11:58:56 +0800 (CST) From: Bibo Mao To: Huacai Chen , Andrey Ryabinin , Andrew Morton Cc: David Hildenbrand , Barry Song , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org Subject: [PATCH v2 1/3] LoongArch: Set initial pte entry with PAGE_GLOBAL for kernel space Date: Mon, 14 Oct 2024 11:58:53 +0800 Message-Id: <20241014035855.1119220-2-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20241014035855.1119220-1-maobibo@loongson.cn> References: <20241014035855.1119220-1-maobibo@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowMBxXuT_lgxnc6EoAA--.1717S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Rspamd-Queue-Id: 8F2CA140005 X-Stat-Signature: sufsc4x3pnimw3m1se43uutsrscbnjsu X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728878339-20535 X-HE-Meta: U2FsdGVkX19jweRpVhmFV1r1UMDfku+A5olYRIpvUEF6ZK/PGxaWQMavtN2yjNmtVuw+2JfztAWAp/ojowcW/+fF8XJOUlUJYBE9cpZppdnEjumCZiofSdpZqML7NUEIAxZUOBuj1G5ltX/w93WBGLTY4EhEXLWe2W2PHhmfgFDNlIXjPcCn5GF9JF4LfkQpxRFr52DiYg98Kpg/v/KgR9cd6Zcq+J1k+zJ36/mAhMLdZHMlUUXsGQ0MGgSaDcdZDrEC4LDfDt9FHa3RP83T9EzAKEmKz8iGTB6lp5prW+/H4sMpjnzu1TB+UmcswL08VzEJnJOj+R3jrphQb9e1S70bDac2OfQFptndVoEYcT2JcCL8lVSoeX16MSs4SXvzpkrgPA/zH2drrM0eRzOT/I9M7+iSphUAAjDp+eMy1GdbzDqGae5zJjyRQW4EacU/irTR+Ae3cz0u1fOA2MeZ1782imijOD05H2TZuCbm2x5zZf8mzEhXRcn1FaxX3nO0XYdpIG9Y2BQ777rudYvs/JLPFrcLgCRtsmI0+iA9Lfp1XoGchE1ji1mxxUyoYIXc1hRPbtYRsfN48XzFX+Sax26BNIobLcWg36s5SeXLVbwj6i5+A4k0rBN0Ex+wfoSoFZQmXsq3ndbc/nRKB9FToYetMbLh76dbn7Z9KmogL6yVgzuwLuv5JiXQyW6kQCb56DqdJJcxnGaSL/vcoPXGv/aNQh0pLrPJ5NARmVqHK/INiWbfASIA024MScvtPEl5qcx50m5W3EEWuDH0XAtlFbVDMM7xqVDAS4KiWnDO7NdxjbsW8fqrv0ta+tyxHVqLauK2ySwJeb3OQ5qzXukInu5pm9OJXU3/LfcycjeqhwZ87f4FiacZ5EwYXLclBYy3pAMnnKa2ajxazRHmt577ou1rpYBYirQBrApZIfTJpr7utU9O+BwI+JIGFnN31ouO1tiZHeC7BP+ai0pRMiP VddcMLU9 tEjlNemuOGFMdGTuPXTkZuFl5g8EonW0nlBm7rtW1YznCNr34RGTekMj+InaH6kWrk41FRWM0yPFjffPSuajSLiXJJM6XMV3NS4PZv6zZSsVLSjFj4YhovChlayIYvbJdGpg3C7WUVPfMcWRmDLdZ+BxxHg== 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: Unlike general architectures, there are two pages in one TLB entry on LoongArch system. For kernel space, it requires both two pte entries with PAGE_GLOBAL bit set, else HW treats it as non-global tlb, there will be potential problems if tlb entry for kernel space is not global. Such as fail to flush kernel tlb with function local_flush_tlb_kernel_range() which only flush tlb with global bit. With function kernel_pte_init() added, it can be used to init pte table when it is created for kernel address space, and the default initial pte value is PAGE_GLOBAL rather than zero at beginning. Kernel address space areas includes fixmap, percpu, vmalloc, kasan and vmemmap areas set default pte entry with PAGE_GLOBAL set. Signed-off-by: Bibo Mao --- arch/loongarch/include/asm/pgalloc.h | 13 +++++++++++++ arch/loongarch/include/asm/pgtable.h | 1 + arch/loongarch/mm/init.c | 4 +++- arch/loongarch/mm/kasan_init.c | 4 +++- arch/loongarch/mm/pgtable.c | 22 ++++++++++++++++++++++ include/linux/mm.h | 1 + mm/kasan/init.c | 8 +++++++- mm/sparse-vmemmap.c | 5 +++++ 8 files changed, 55 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/include/asm/pgalloc.h b/arch/loongarch/include/asm/pgalloc.h index 4e2d6b7ca2ee..b2698c03dc2c 100644 --- a/arch/loongarch/include/asm/pgalloc.h +++ b/arch/loongarch/include/asm/pgalloc.h @@ -10,8 +10,21 @@ #define __HAVE_ARCH_PMD_ALLOC_ONE #define __HAVE_ARCH_PUD_ALLOC_ONE +#define __HAVE_ARCH_PTE_ALLOC_ONE_KERNEL #include +static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm) +{ + pte_t *pte; + + pte = (pte_t *) __get_free_page(GFP_KERNEL); + if (!pte) + return NULL; + + kernel_pte_init(pte); + return pte; +} + static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte) { diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/include/asm/pgtable.h index 9965f52ef65b..22e3a8f96213 100644 --- a/arch/loongarch/include/asm/pgtable.h +++ b/arch/loongarch/include/asm/pgtable.h @@ -269,6 +269,7 @@ extern void set_pmd_at(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp, pm extern void pgd_init(void *addr); extern void pud_init(void *addr); extern void pmd_init(void *addr); +extern void kernel_pte_init(void *addr); /* * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that diff --git a/arch/loongarch/mm/init.c b/arch/loongarch/mm/init.c index 8a87a482c8f4..9f26e933a8a3 100644 --- a/arch/loongarch/mm/init.c +++ b/arch/loongarch/mm/init.c @@ -198,9 +198,11 @@ pte_t * __init populate_kernel_pte(unsigned long addr) if (!pmd_present(pmdp_get(pmd))) { pte_t *pte; - pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE); + pte = memblock_alloc_raw(PAGE_SIZE, PAGE_SIZE); if (!pte) panic("%s: Failed to allocate memory\n", __func__); + + kernel_pte_init(pte); pmd_populate_kernel(&init_mm, pmd, pte); } diff --git a/arch/loongarch/mm/kasan_init.c b/arch/loongarch/mm/kasan_init.c index 427d6b1aec09..34988573b0d5 100644 --- a/arch/loongarch/mm/kasan_init.c +++ b/arch/loongarch/mm/kasan_init.c @@ -152,6 +152,8 @@ static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr, phys_addr_t page_phys = early ? __pa_symbol(kasan_early_shadow_page) : kasan_alloc_zeroed_page(node); + if (!early) + kernel_pte_init(__va(page_phys)); next = addr + PAGE_SIZE; set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL)); } while (ptep++, addr = next, addr != end && __pte_none(early, ptep_get(ptep))); @@ -287,7 +289,7 @@ void __init kasan_init(void) set_pte(&kasan_early_shadow_pte[i], pfn_pte(__phys_to_pfn(__pa_symbol(kasan_early_shadow_page)), PAGE_KERNEL_RO)); - memset(kasan_early_shadow_page, 0, PAGE_SIZE); + kernel_pte_init(kasan_early_shadow_page); csr_write64(__pa_symbol(swapper_pg_dir), LOONGARCH_CSR_PGDH); local_flush_tlb_all(); diff --git a/arch/loongarch/mm/pgtable.c b/arch/loongarch/mm/pgtable.c index eb6a29b491a7..228ffc1db0a3 100644 --- a/arch/loongarch/mm/pgtable.c +++ b/arch/loongarch/mm/pgtable.c @@ -38,6 +38,28 @@ pgd_t *pgd_alloc(struct mm_struct *mm) } EXPORT_SYMBOL_GPL(pgd_alloc); +void kernel_pte_init(void *addr) +{ + unsigned long *p, *end; + unsigned long entry; + + entry = (unsigned long)_PAGE_GLOBAL; + p = (unsigned long *)addr; + end = p + PTRS_PER_PTE; + + do { + p[0] = entry; + p[1] = entry; + p[2] = entry; + p[3] = entry; + p[4] = entry; + p += 8; + p[-3] = entry; + p[-2] = entry; + p[-1] = entry; + } while (p != end); +} + void pgd_init(void *addr) { unsigned long *p, *end; diff --git a/include/linux/mm.h b/include/linux/mm.h index ecf63d2b0582..6909fe059a2c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3818,6 +3818,7 @@ void *sparse_buffer_alloc(unsigned long size); struct page * __populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap); +void kernel_pte_init(void *addr); void pmd_init(void *addr); void pud_init(void *addr); pgd_t *vmemmap_pgd_populate(unsigned long addr, int node); diff --git a/mm/kasan/init.c b/mm/kasan/init.c index 89895f38f722..ac607c306292 100644 --- a/mm/kasan/init.c +++ b/mm/kasan/init.c @@ -106,6 +106,10 @@ static void __ref zero_pte_populate(pmd_t *pmd, unsigned long addr, } } +void __weak __meminit kernel_pte_init(void *addr) +{ +} + static int __ref zero_pmd_populate(pud_t *pud, unsigned long addr, unsigned long end) { @@ -126,8 +130,10 @@ static int __ref zero_pmd_populate(pud_t *pud, unsigned long addr, if (slab_is_available()) p = pte_alloc_one_kernel(&init_mm); - else + else { p = early_alloc(PAGE_SIZE, NUMA_NO_NODE); + kernel_pte_init(p); + } if (!p) return -ENOMEM; diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index edcc7a6b0f6f..c0388b2e959d 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -184,6 +184,10 @@ static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node) return p; } +void __weak __meminit kernel_pte_init(void *addr) +{ +} + pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node) { pmd_t *pmd = pmd_offset(pud, addr); @@ -191,6 +195,7 @@ pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node) void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node); if (!p) return NULL; + kernel_pte_init(p); pmd_populate_kernel(&init_mm, pmd, p); } return pmd;