From patchwork Thu Oct 10 03:50:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bibo Mao X-Patchwork-Id: 13829446 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 CB2FACF0437 for ; Thu, 10 Oct 2024 03:51:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2CDFA6B0089; Wed, 9 Oct 2024 23:50:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 234AC6B008A; Wed, 9 Oct 2024 23:50:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F26186B008C; Wed, 9 Oct 2024 23:50:57 -0400 (EDT) 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 C8F276B008A for ; Wed, 9 Oct 2024 23:50:57 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 00F56A1B4F for ; Thu, 10 Oct 2024 03:50:51 +0000 (UTC) X-FDA: 82656316554.12.5462E31 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by imf13.hostedemail.com (Postfix) with ESMTP id 30E8820002 for ; Thu, 10 Oct 2024 03:50:53 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.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=1728532105; 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=rd4ZkujWkFK413qElnLYwYdjOhfxwEaEsEBX+Qwnza0=; b=Uscam//ILFFkVNlpiueSqBpZNZRBvYMpPdKtVoF20abT7VqmTxn5XinT3gjuBKNTze/5m9 bSCjDia/s3TL9z/nNnkmL9v8VYrO/CIX0NGqnNgDYN5AQJ+hDCWq8u9ZG1duJwIWlsQRwJ JVhrwU/zdhuRaUzGFCaSbK0rUlLqiR0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728532105; a=rsa-sha256; cv=none; b=UfsxE5vGmBe0BY9iCtSWcUB9yzs0sfYZW0cLuNGCXnc/0ay4DtYdYb0GHnqVfwotxuEC3N tNOeaoraQwOvzP71U76TLmuMDr0zho00gBx2KwkP8AA6+nOwS43QhbBQ46AR7ZloruHOAp tV3E6FbevbolnizymtnrTCUU8hd1/8I= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf13.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 _____8CxrrMaTwdnxrsRAA--.26960S3; Thu, 10 Oct 2024 11:50:50 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowMDx7tUZTwdnFP8hAA--.52915S3; Thu, 10 Oct 2024 11:50:49 +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 1/4] LoongArch: Set pte entry with PAGE_GLOBAL for kernel space Date: Thu, 10 Oct 2024 11:50:45 +0800 Message-Id: <20241010035048.3422527-2-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20241010035048.3422527-1-maobibo@loongson.cn> References: <20241010035048.3422527-1-maobibo@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: qMiowMDx7tUZTwdnFP8hAA--.52915S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 30E8820002 X-Stat-Signature: mf8cafz1hm7mbu33au8dgpcukfs9rbm7 X-Rspam-User: X-HE-Tag: 1728532253-423611 X-HE-Meta: U2FsdGVkX18sf8tzWetFN8qR2RbjnGUEiIG8ZLrW9wk7TkXvIn7fh9TuLSEyx57TTdBkpUOfVYm63/dEKY5sNWUrIjwm4ye5gEwhhNvxekMVdVZgu3UZ4+HZAtVSEpldiah8pGYR/Bik6IyJA8xHafsB04WhgamwPaiv97tYqdhbty1FSjYU8f1ghGtCE0hkZl2UjREVDcjzE5PJKgx/06mmKRtxtrASyM3fENGEdo4jxZVjheO+zCh0AV8jyOI8urxC8SIxrdZO5A1+jkVei7JExVWNDOc+4RoXQNy3Ws1950y9Hb2czEyQL6v7iSKGLkEt/KQhei1uC4Jixq8o0aqYpUJ9bTB0wphgYJHjwUfmnl81+GcqXCcrVVu2Viky/NbPNrk8Dd+CBqDJqqN0PExKl6plZkYAeKVCLZhW8bOFooevXKg6YF9dvacWs1Xevzh7bEX3oxb3xgMn2kASDrUYOoH0NCsa1QlTr+LkXSAWy7DiNvf3aEqcAzTUjXDiMvV+4Q3nsNnrZ6VNMM9Gd52ZeCn6Eym7aJvAbHVl57SOuPIneXkPrKNFMuNZsQJ4fTzgKiYra20bOXfNxHhmPGdAL/uRxXC+kkcn+BbY8HJ7n7Vd/6yhd/PeksLmlU1Y8rqFqake3DZfQRaACdcK6CUNq7ctD3VurXa+T1E0umSDmuGg3Rg7st/Wkt+7BjqR8YqUyYg/Ar5Yh/KkQalCtTyGoVBJM0sr8WGBTof2zSuyEvuHzZgFTFyGOsmgz59em8u6FQmS7wkD0ophhNMf+Vdt4yBkvxpGkltzxtkyW6VlnVWBtqRJZ5Ctuig6keUwzw4MBcstp6xMKpXZgwSf8dtCX/jKO8tZnS6rH8lIXX6CqRu2G7TGiJTsssZtqWKJhkq7RuqHlYGXxqj4DOlXhWoN7WOjq6g80fHXH2ZXSkkLNQSOOjQllxR+BFrmwXuzP9zWozyEwRVKKGMcy+v gzaFnMwl U2+PHPCO0hKzl9jpa3pfrP767/NXagpgiPTpPjZo2cAGc19HibnXzd41244f+fj3XcF0i3XdEP18NcnvmbUHMGCRxLISogUy36kJoQOrKf6PNgadCWfnyMNAmIwgA265bKjk1+lDxx4shqulgmAje9N/ZeA== 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 for one TLB entry on LoongArch system. For kernel space, it requires both two pte entries with PAGE_GLOBAL 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. Here function kernel_pte_init() is added, it can be used to init pte table when it is created, so the default inital pte is PAGE_GLOBAL rather than zero at beginning. Kernel space areas includes fixmap, percpu, vmalloc and kasan 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 ++++++++++++++++++++++ 5 files changed, 42 insertions(+), 2 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;