From mboxrd@z Thu Jan 1 00:00:00 1970 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 EA1A6C0015E for ; Thu, 27 Jul 2023 01:26:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 398156B0072; Wed, 26 Jul 2023 21:26:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 347AF6B0074; Wed, 26 Jul 2023 21:26:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20F888D0001; Wed, 26 Jul 2023 21:26:26 -0400 (EDT) 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 12A696B0072 for ; Wed, 26 Jul 2023 21:26:26 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BC50340AB4 for ; Thu, 27 Jul 2023 01:26:25 +0000 (UTC) X-FDA: 81055651530.12.47AC63B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf24.hostedemail.com (Postfix) with ESMTP id C18AF180015 for ; Thu, 27 Jul 2023 01:26:22 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=n0mF130n; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of chenhuacai@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chenhuacai@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690421182; 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:dkim-signature; bh=kF2KsmExiWYczznuEgVtdNQkEJ9KvvlP0zAgsbXMSzU=; b=pbKBQAlR8z4kc2A/VtmmQNONr2fk+y2j8gaVawNcasZA3+fiUgRSNi9MtEE4NMMfnnaXiv RIDtD3yUbp7+vqw/ShMLvr+r6c6GvGjtFexlRWCyCsj8YLNVC+njy/z89kOPo+NmaTqSHk TSamNYweoyweLOFxvVV37v698/Cw3Eg= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=n0mF130n; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf24.hostedemail.com: domain of chenhuacai@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chenhuacai@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690421182; a=rsa-sha256; cv=none; b=Zj9bi6nc0NDok6pi2F8Zj1LueuQ0IGegJOIIb2qNIE0ysQuvP5PAfsWJM3s+mfY3OHKJIS 12v9bw6JBq3HnzcQlWqD2cre4U7fU31BEN6/u8R+eQet3XJgU88FO4OENq4Sw+HcOUD368 EsLygIu5LEC9fS4vLkhTex8NcfJVKSw= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id BE1AF61C9C for ; Thu, 27 Jul 2023 01:26:21 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2E80C433D9 for ; Thu, 27 Jul 2023 01:26:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690421180; bh=/c/O0qLl+PCHoWWgkI4YPL7GBy4s94WZytafKQrHBnI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=n0mF130nIJCIbPbl6RNQVDxGmXvsVfmp33GrICFW9oHZAyn8t3qAtzpYIZyo3wLpI SB1tW+CFUUvhPv9ywgdMpCSZPK/MhWEKkzkt1M/ZOz+W0VLu3c4Cs5bkCzNoGT1Nwl A38lsG/IEumyAPz1FemlDUWl8pw/9K+ipVl14gfiihFiaOVx3QqEA9Z0o+G8e+A7iU 8JlFR+1PEVXRJYJBXAh1OTRNcb+wMa09Xy5orzT3nvEjzwNJDKvCMJ9XkJvkIakB6y Xf/ShomYAYchPhag6Mo8MtljusFogMjdiaNFENzH85+x9B4qtquQd4qR9kbiCMufML etWUJpfPrgsjw== Received: by mail-ed1-f44.google.com with SMTP id 4fb4d7f45d1cf-5223fbd54c6so479965a12.3 for ; Wed, 26 Jul 2023 18:26:20 -0700 (PDT) X-Gm-Message-State: ABy/qLaPJwU1ozaRDLBfQ4io+a/WEz5BKbm84jL83okJJI9XPB3aVs1U 9UPa4DhqMNmauufvRP7o8tIQcUk5U+FGAISzKas= X-Google-Smtp-Source: APBJJlEc0IIYhics2+11RHI7FWA700u5cam4tcTp9l414TAQeG1c/um9bAT1gZDBWn6u4xsZUFFvJffAeC964ZVsTGA= X-Received: by 2002:a17:906:845c:b0:993:eddd:6df4 with SMTP id e28-20020a170906845c00b00993eddd6df4mr641132ejy.10.1690421178609; Wed, 26 Jul 2023 18:26:18 -0700 (PDT) MIME-Version: 1.0 References: <20230725061451.1231480-1-lienze@kylinos.cn> <20230725061451.1231480-5-lienze@kylinos.cn> In-Reply-To: <20230725061451.1231480-5-lienze@kylinos.cn> From: Huacai Chen Date: Thu, 27 Jul 2023 09:26:04 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 4/4 v2] LoongArch: Add KFENCE support To: Enze Li Cc: kernel@xen0n.name, loongarch@lists.linux.dev, glider@google.com, elver@google.com, akpm@linux-foundation.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, zhangqing@loongson.cn, yangtiezhu@loongson.cn, dvyukov@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C18AF180015 X-Stat-Signature: ro9f5srqznmiy94bg1zs5kdunee5hm4p X-HE-Tag: 1690421182-959847 X-HE-Meta: U2FsdGVkX18cYvzhzHYNUDzbAIIbvZs4tDX61eKxqVPWnyNCLH63/ScuO83rpuBkA6if7e5d28m6amIYI4l81kLl9mLLUjag2KpUCMqAIve0HeNMZiUUHXnUKrRGq5lxsdlpg1wrzv9c/Lw36UcxlbXklxWqMvjqzC3/La5ouXFAShGSdg1yvtdrGQow1SnJ7v6iJjKkQZiU/1MkqyAHWSDX27BzPF9Buf1FFne+5UCcuRL/GqXyDeeU/DQu9RD6W0jtHh2qYoM347MDjucVN1ubU64sC6zaRE+v8AcRvYNX7IX1ZumASEBJwKUoXlUbbhuf32cE8Zuh/+4ozCFcbpAvE/MDm/Y23E1qCdqVBU2RyQOpowALlg4AEhvRDdc3omJBrkGEBQsWrCeoCTKumE+JHX3UmhUG0Kj5M8+2bi+QIYXeyVbY07K6yzRXe2gCJQROMybzXCBdR6GiRaq3J6PQeFRkPZb/pbeot4nw7v7SiWxjPR7VJ4bnKeaWZPE+kzyhoN/ZcPTU9rrha64sb0hUofKzjLUg2Lp+ZYKoxWC4puC7J6OqvHlpH546h70sAmQaY3tj8EBk3gM0pDpOzcv5JLcyTgsQoKK3neCtrjPNgFFoxaZZPAJ0NYH1qhNJwKGO2G0kal2laPi0sJI7GJD2qCiA9pjV60+yHGYbuZpeQH67FEx1bY2bAHIAPTu1k7hPvUS/2S27BIz+7FAhNcIieDXxLmvWkkYILWnuykeGiYJtBLOvIceaKQX3GGWoLGr/L7n1w323gHEjrkECyU+tUQlfpypFOVM2xSXc3hmSqvrEogqu6mNM8ZuxgTd9gA/QutNksnx/1CqG1oVPm8Ww0XlGG+hGYhJw+20EQzKCFvD8yAnu21BJqZuJeuBQlintsEF0oDhHSViz1HQwuKgReHaX/ZZqj5jzk1LWDq3HKQwobn04WMyO5+ZiOp9ksSTMppr4dkEMP0ERfR0 2fHsa1MX O82mVoEzCzmZ0X+/akpIqAtSv5EVfakn9ym6EBqcxHN8HP8aHvkMqcj75C2EWXjT2or89vSBqns5KlRBCffkmc8/dZpztuXGUosDM9YM3kG/teLg1QLtbKh2PJcnpK1oZvX4jjoQSFUIxTHJVbWO+7nee9GCo/VUDwWVjlaszfGqflVLRpKwwFBLDZEOC4yXPKjypFmJS3jcbyHZDVsO59a42YgwCyx5JnUQPGv1eTdjnNvfY8jvgYLyNwL0PWkvR9uCqkWh4HpoQWWksaSVm3mxw6BuelpuXI41PPtzWkNmeDV09BAM2Pt7A9AlCzR59gj0eAeEeXXCKdap+T9hC/ZfxuZqDZ1L9jljTGvVRFlBVaTfZjaTv/qg4aleRjd73wEBKJh9dVhjCPrhQrKLiiup35w== 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: On Tue, Jul 25, 2023 at 2:15=E2=80=AFPM Enze Li wrote: > > The LoongArch architecture is quite different from other architectures. > When the allocating of KFENCE itself is done, it is mapped to the direct > mapping configuration window [1] by default on LoongArch. It means that > it is not possible to use the page table mapped mode which required by > the KFENCE system and therefore it should be remapped to the appropriate > region. > > This patch adds architecture specific implementation details for KFENCE. > In particular, this implements the required interface in . > > Tested this patch by running the testcases and all passed. > > [1] https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.= html#virtual-address-space-and-address-translation-mode > > Signed-off-by: Enze Li > --- > arch/loongarch/Kconfig | 1 + > arch/loongarch/include/asm/kfence.h | 62 ++++++++++++++++++++++++++++ > arch/loongarch/include/asm/pgtable.h | 14 ++++++- > arch/loongarch/mm/fault.c | 22 ++++++---- > 4 files changed, 90 insertions(+), 9 deletions(-) > create mode 100644 arch/loongarch/include/asm/kfence.h > > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig > index 70635ea3d1e4..5b63b16be49e 100644 > --- a/arch/loongarch/Kconfig > +++ b/arch/loongarch/Kconfig > @@ -91,6 +91,7 @@ config LOONGARCH > select HAVE_ARCH_AUDITSYSCALL > select HAVE_ARCH_JUMP_LABEL > select HAVE_ARCH_JUMP_LABEL_RELATIVE > + select HAVE_ARCH_KFENCE > select HAVE_ARCH_MMAP_RND_BITS if MMU > select HAVE_ARCH_SECCOMP_FILTER > select HAVE_ARCH_TRACEHOOK > diff --git a/arch/loongarch/include/asm/kfence.h b/arch/loongarch/include= /asm/kfence.h > new file mode 100644 > index 000000000000..fb39076fe4d7 > --- /dev/null > +++ b/arch/loongarch/include/asm/kfence.h > @@ -0,0 +1,62 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * KFENCE support for LoongArch. > + * > + * Author: Enze Li > + * Copyright (C) 2022-2023 KylinSoft Corporation. > + */ > + > +#ifndef _ASM_LOONGARCH_KFENCE_H > +#define _ASM_LOONGARCH_KFENCE_H > + > +#include > +#include > +#include > + > +static inline bool arch_kfence_init_pool(void) > +{ > + char *kfence_pool =3D __kfence_pool; > + struct vm_struct *area; > + int err; > + > + area =3D __get_vm_area_caller(KFENCE_POOL_SIZE, VM_IOREMAP, > + KFENCE_AREA_START, KFENCE_AREA_END, > + __builtin_return_address(0)); > + if (!area) > + return false; > + > + __kfence_pool =3D (char *)area->addr; > + err =3D ioremap_page_range((unsigned long)__kfence_pool, > + (unsigned long)__kfence_pool + KFENCE_PO= OL_SIZE, > + virt_to_phys((void *)kfence_pool), > + PAGE_KERNEL); > + if (err) { > + free_vm_area(area); > + return false; > + } > + > + return true; > +} > + > +/* Protect the given page and flush TLB. */ > +static inline bool kfence_protect_page(unsigned long addr, bool protect) > +{ > + pte_t *pte =3D virt_to_kpte(addr); > + > + if (WARN_ON(!pte) || pte_none(*pte)) > + return false; > + > + if (protect) > + set_pte(pte, __pte(pte_val(*pte) & ~(_PAGE_VALID | _PAGE_= PRESENT))); > + else > + set_pte(pte, __pte(pte_val(*pte) | (_PAGE_VALID | _PAGE_P= RESENT))); > + > + /* Flush this CPU's TLB. */ > + preempt_disable(); > + local_flush_tlb_one(addr); > + preempt_enable(); > + > + return true; > +} > + > +#endif /* _ASM_LOONGARCH_KFENCE_H */ > diff --git a/arch/loongarch/include/asm/pgtable.h b/arch/loongarch/includ= e/asm/pgtable.h > index 98a0c98de9d1..2702a6ba7122 100644 > --- a/arch/loongarch/include/asm/pgtable.h > +++ b/arch/loongarch/include/asm/pgtable.h > @@ -77,6 +77,13 @@ extern unsigned long zero_page_mask; > (virt_to_page((void *)(empty_zero_page + (((unsigned long)(vaddr)= ) & zero_page_mask)))) > #define __HAVE_COLOR_ZERO_PAGE > > +#ifdef CONFIG_KFENCE > +#define KFENCE_AREA_SIZE \ > + (((CONFIG_KFENCE_NUM_OBJECTS + 1) * 2 + 2) * PAGE_SIZE) Another question: Why define KFENCE_AREA_SIZE while there is already KFENCE_POOL_SIZE? And why is KFENCE_AREA_SIZE a little larger than KFENCE_POOL_SIZE? If we can reuse KFENCE_POOL_SIZE, KFENCE_AREA_START/KFENCE_AREA_END can be renamed to KFENCE_POOL_START/KFENCE_POOL_END. Huacai > +#else > +#define KFENCE_AREA_SIZE 0 > +#endif > + > /* > * TLB refill handlers may also map the vmalloc area into xkvrange. > * Avoid the first couple of pages so NULL pointer dereferences will > @@ -88,11 +95,16 @@ extern unsigned long zero_page_mask; > #define VMALLOC_START MODULES_END > #define VMALLOC_END \ > (vm_map_base + \ > - min(PTRS_PER_PGD * PTRS_PER_PUD * PTRS_PER_PMD * PTRS_PER_PTE * = PAGE_SIZE, (1UL << cpu_vabits)) - PMD_SIZE - VMEMMAP_SIZE) > + min(PTRS_PER_PGD * PTRS_PER_PUD * PTRS_PER_PMD * PTRS_PER_PTE * = PAGE_SIZE, (1UL << cpu_vabits)) - PMD_SIZE - VMEMMAP_SIZE - KFENCE_AREA_SIZ= E) > > #define vmemmap ((struct page *)((VMALLOC_END + PMD_SIZE)= & PMD_MASK)) > #define VMEMMAP_END ((unsigned long)vmemmap + VMEMMAP_SIZE - 1) > > +#ifdef CONFIG_KFENCE > +#define KFENCE_AREA_START VMEMMAP_END > +#define KFENCE_AREA_END (KFENCE_AREA_START + KFENCE_AREA_= SIZE) > +#endif > + > #define pte_ERROR(e) \ > pr_err("%s:%d: bad pte %016lx.\n", __FILE__, __LINE__, pte_val(e)= ) > #ifndef __PAGETABLE_PMD_FOLDED > diff --git a/arch/loongarch/mm/fault.c b/arch/loongarch/mm/fault.c > index da5b6d518cdb..c0319128b221 100644 > --- a/arch/loongarch/mm/fault.c > +++ b/arch/loongarch/mm/fault.c > @@ -23,6 +23,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -30,7 +31,8 @@ > > int show_unhandled_signals =3D 1; > > -static void __kprobes no_context(struct pt_regs *regs, unsigned long add= ress) > +static void __kprobes no_context(struct pt_regs *regs, unsigned long add= ress, > + unsigned long write) > { > const int field =3D sizeof(unsigned long) * 2; > > @@ -38,6 +40,9 @@ static void __kprobes no_context(struct pt_regs *regs, = unsigned long address) > if (fixup_exception(regs)) > return; > > + if (kfence_handle_page_fault(address, write, regs)) > + return; > + > /* > * Oops. The kernel tried to access some bad page. We'll have to > * terminate things with extreme prejudice. > @@ -51,14 +56,15 @@ static void __kprobes no_context(struct pt_regs *regs= , unsigned long address) > die("Oops", regs); > } > > -static void __kprobes do_out_of_memory(struct pt_regs *regs, unsigned lo= ng address) > +static void __kprobes do_out_of_memory(struct pt_regs *regs, unsigned lo= ng address, > + unsigned long write) > { > /* > * We ran out of memory, call the OOM killer, and return the user= space > * (which will retry the fault, or kill us if we got oom-killed). > */ > if (!user_mode(regs)) { > - no_context(regs, address); > + no_context(regs, address, write); > return; > } > pagefault_out_of_memory(); > @@ -69,7 +75,7 @@ static void __kprobes do_sigbus(struct pt_regs *regs, > { > /* Kernel mode? Handle exceptions or die */ > if (!user_mode(regs)) { > - no_context(regs, address); > + no_context(regs, address, write); > return; > } > > @@ -90,7 +96,7 @@ static void __kprobes do_sigsegv(struct pt_regs *regs, > > /* Kernel mode? Handle exceptions or die */ > if (!user_mode(regs)) { > - no_context(regs, address); > + no_context(regs, address, write); > return; > } > > @@ -149,7 +155,7 @@ static void __kprobes __do_page_fault(struct pt_regs = *regs, > */ > if (address & __UA_LIMIT) { > if (!user_mode(regs)) > - no_context(regs, address); > + no_context(regs, address, write); > else > do_sigsegv(regs, write, address, si_code); > return; > @@ -211,7 +217,7 @@ static void __kprobes __do_page_fault(struct pt_regs = *regs, > > if (fault_signal_pending(fault, regs)) { > if (!user_mode(regs)) > - no_context(regs, address); > + no_context(regs, address, write); > return; > } > > @@ -232,7 +238,7 @@ static void __kprobes __do_page_fault(struct pt_regs = *regs, > if (unlikely(fault & VM_FAULT_ERROR)) { > mmap_read_unlock(mm); > if (fault & VM_FAULT_OOM) { > - do_out_of_memory(regs, address); > + do_out_of_memory(regs, address, write); > return; > } else if (fault & VM_FAULT_SIGSEGV) { > do_sigsegv(regs, write, address, si_code); > -- > 2.34.1 > >