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 D2A33EB64DA for ; Wed, 19 Jul 2023 15:28:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4FB1A28006E; Wed, 19 Jul 2023 11:28:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4AA4728004C; Wed, 19 Jul 2023 11:28:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 34C6828006E; Wed, 19 Jul 2023 11:28:07 -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 25FCD28004C for ; Wed, 19 Jul 2023 11:28:07 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E2AB0160196 for ; Wed, 19 Jul 2023 15:28:06 +0000 (UTC) X-FDA: 81028742172.27.2DA1AEE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id 95DBD1C0027 for ; Wed, 19 Jul 2023 15:28:04 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z97JdpsC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.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=1689780484; a=rsa-sha256; cv=none; b=6oYyluzErdmMa7djCKFxsEMVVOP409Fi/vn2bLlG3ieBm/qidHpRso7rErOohhbA64bylB LXOAlKQ6rUv9NclFAARfNoV2LNM6mPHdfH+EvXAGamsdpkHRSbz1QFfssDfVVzHfJXnRfH AdGvjxTWhv693Nvhhxrnl+zGFiPOh1Y= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Z97JdpsC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf20.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=1689780484; 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=KFt4BkJ2H2so/ro+TZYvoG+vX9g4beBLicnUoRPY/08=; b=cHT2W4sfFtjZ10PIZciYDERfAwcIby2ooz5cSKAajmnfSb8TwBGjyWoun8llkPy8Dih3De 1njnLDCLTllem0a+1XCqI4rtmS583rTpLI5SqZ2RNroUe8Z5KXx+jT6qfPkFu2pxjQMzQs 8P07QfWwBfoYz4fZhxa8isZLpRMuhQw= 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 70A3E61761 for ; Wed, 19 Jul 2023 15:28:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7A8CC433CB for ; Wed, 19 Jul 2023 15:28:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689780482; bh=VuCIRy30W6NNEm320JJ7plTAIxo9dj/XurZfZt2DCh4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Z97JdpsC8B5qqtam1ATSM/L8vaw3Oon+cL4aUIeI52X0HqPcdH0owYllEQJ9JB6fW vnOJob3VZgRQ/9vaHzS01vaNJUqaHuiWEUs5mQQVBo7beFj2I3Fi5Ey7Urci7rtJD+ AUC7MrSLRGOJv9OthL9WcUFLcFMZpDSEedikNWoNGRGQGc/p9dVdDd2OdN/yRM9Gbp Tp9OyxvjEj///bIafSmQOFrh0eC8c0uXxOfK1t8ntxJ6pw38hwW5B0vifev70vH/Ls cJN+2qv3HngSZmGAfkRQ5vhJtz6Ilc2xnVOFGaBt0yTlq51sw+w7X/ZF0QhlenhgyE NCaf5G2KRoJ+w== Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-51bece5d935so9946012a12.1 for ; Wed, 19 Jul 2023 08:28:02 -0700 (PDT) X-Gm-Message-State: ABy/qLaDiCE0AXV6ffLe+AOhX93+H0zCPGR3TTcBl01aqqIVby01APo8 khxqwDZmO/q0NxgJT+/hlmxkSQCoB+S1VmGZFbg= X-Google-Smtp-Source: APBJJlHG0pkNH/YXhGZaVStnyW7GW9Y2592NfB65Ub2jKLxCDPnKF7iV3fhEYdGxGKrF64EsYUhie/NQYmuSetP6bGo= X-Received: by 2002:a05:6402:2037:b0:51e:293b:e1ce with SMTP id ay23-20020a056402203700b0051e293be1cemr2706511edb.31.1689780480858; Wed, 19 Jul 2023 08:28:00 -0700 (PDT) MIME-Version: 1.0 References: <20230719082732.2189747-1-lienze@kylinos.cn> <20230719082732.2189747-5-lienze@kylinos.cn> In-Reply-To: <20230719082732.2189747-5-lienze@kylinos.cn> From: Huacai Chen Date: Wed, 19 Jul 2023 23:27:50 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 4/4] 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: rspam06 X-Rspamd-Queue-Id: 95DBD1C0027 X-Stat-Signature: b988kun6zf4t51ojzmwitjredsx4c6a9 X-HE-Tag: 1689780484-365915 X-HE-Meta: U2FsdGVkX1+Xx4M8suUhx/h2a+AwrWr1fDKVOtcAgWFuUixQUVulsLyvjR5jm9o4zECArhAMKsVOUJ4sOm03ERPms8fHXeYGGCk/S/bbYgwcPbBeSlDXeiUmNNyEz3HKOBbC1bvFAgpeKI9RX5yz7FsjbnBpauQtHJVkvke4f6xd7BRRCk1rb45tlSjCFP/4q9jQ35FynEgP2jub0u/U7snNkWNgM4mOidKOf018zz+pdIqFYxlekHtlOB5Nfcm42UTvWtTPbaQi37kx9WvpHpfyYko6W8y/Amgj+VqYF6Kws4N9kHrl7qCG/5NwINYysKgRGIVnzNroBsvRa3CeI5mn+5bnqWCIHvZHRUD7Q67qnt/Wqa6TSZ0dHMvbWpPsq0s6raatyV1rpK121HGclY/s3wf3grJqdku923rn2J8+BlnZ/R9JKP65ZbJjLGrf3XZ9BtPK0R2+hQRPKSqcj7l2oA9g+pgHNUf/PAXPnqSkGH5G6DFGwwg9REPFzGGmfBGpw6ytJEj/k7lLoncFMsUExwNLIY29EmS1TFJmRHy6B3/qDtoURZw4Cdc1iI/JXeEJZ7ynSS/eUFssU8OuR4Z++W/mWF/SyaWtr44/oC/k2QcfMdQ+pr50xLWa58g6t66urJ91YjjCtYiW8PeZJeRp0G1iLFRLaS5Egrl3oLoqvQ1R5egBGp9YaMqBThaH7t0igxAmlVrKWNAYTDvP50WQpOcYfMqvgFSrmMptYJ3fH7qNDx4+XwpZnzPb/NzYEG/YeEH3pXzYHhkR2sr0Ba4sbJ5kNPr1rV3LnFCmlnyoA70M6YL80zO/oKk3qnDPd0K42VeaQU9St8eUCMxS1XWPidZ/tyBXhlrJ+F/OwXB6FskVNdb4Lb6KbXbcNalEUva8/1zJZFPZF6tDx74ZEzjwfm2ZIzAkdI8BYi74Dwd+EuxW9+GkA8z657nH87x6URFayD0UeMe7Kn3SU2C GIO1XGft KpL+PKPEEUeGAz2Y7QqyoEFVCwWKqZ13cO2OCkmJ7XZTBfnCWEgJhIF7SfrmSSd9v+cl5shR4w/tgr5Ttz937EsITnon+ZJGKY0o2DIqXVmngo+kRAIMafAlOPTpi68k3iV4+JTeyZm1AKxRCeNL7R7NGKYQp9NPIhljm0hIUsm+us1O0HlfpuSD/haTL1BhUrELxO2LxSNk6a+h8Yu+X2frUoCtbmBWxpbJgqcBXQBnrrZfxJtF5VVPQF1mGcwTbc0kBGQTaEia/ogMaQxC+8igBulXgcr3MOxD+oU29D1LX8ERLFihP2inKFBl+2JEvif7izwrwnS09vSTd/6vyDNxoEMyiN3i6o1xgEiNvY6aZIDWHzcYK3S+TXP6AC3wlM80pNbZUX5q553jAQWxcmyVG6Q== 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: Hi, Enze, On Wed, Jul 19, 2023 at 4:34=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 using 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 | 6 +++ > arch/loongarch/mm/fault.c | 22 ++++++---- > 4 files changed, 83 insertions(+), 8 deletions(-) > create mode 100644 arch/loongarch/include/asm/kfence.h > > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig > index 5411e3a4eb88..db27729003d3 100644 > --- a/arch/loongarch/Kconfig > +++ b/arch/loongarch/Kconfig > @@ -93,6 +93,7 @@ config LOONGARCH > select HAVE_ARCH_JUMP_LABEL > select HAVE_ARCH_JUMP_LABEL_RELATIVE > select HAVE_ARCH_KASAN > + select HAVE_ARCH_KFENCE if 64BIT "if 64BIT" can be dropped here. > 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..2a85acc2bc70 > --- /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 char *arch_kfence_init_pool(void) > +{ > + char *__kfence_pool_orig =3D __kfence_pool; I prefer kfence_pool than __kfence_pool_orig here. > + 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 NULL; > + > + __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_orig)= , > + PAGE_KERNEL); > + if (err) { > + free_vm_area(area); > + return NULL; > + } > + > + return __kfence_pool; > +} > + > +/* 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 0fc074b8bd48..5a9c81298fe3 100644 > --- a/arch/loongarch/include/asm/pgtable.h > +++ b/arch/loongarch/include/asm/pgtable.h > @@ -85,7 +85,13 @@ extern unsigned long zero_page_mask; > #define MODULES_VADDR (vm_map_base + PCI_IOSIZE + (2 * PAGE_SIZE)) > #define MODULES_END (MODULES_VADDR + SZ_256M) > > +#ifdef CONFIG_KFENCE > +#define KFENCE_AREA_START MODULES_END > +#define KFENCE_AREA_END (KFENCE_AREA_START + SZ_512M) Why you choose 512M here? > +#define VMALLOC_START KFENCE_AREA_END > +#else > #define VMALLOC_START MODULES_END > +#endif I don't like to put KFENCE_AREA between module and vmalloc range (it may cause some problems), can we put it after vmemmap? Huacai > > #ifndef CONFIG_KASAN > #define VMALLOC_END \ > 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 > >