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 B97B1C61DA4 for ; Thu, 9 Mar 2023 10:34:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 260D66B0071; Thu, 9 Mar 2023 05:34:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 210876B0072; Thu, 9 Mar 2023 05:34:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D86A280001; Thu, 9 Mar 2023 05:34:12 -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 F2B8A6B0071 for ; Thu, 9 Mar 2023 05:34:11 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C0D4E1A0726 for ; Thu, 9 Mar 2023 10:34:11 +0000 (UTC) X-FDA: 80548999902.22.0329D07 Received: from mail-vs1-f54.google.com (mail-vs1-f54.google.com [209.85.217.54]) by imf30.hostedemail.com (Postfix) with ESMTP id 05B4080015 for ; Thu, 9 Mar 2023 10:34:09 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="d7bLR//u"; spf=pass (imf30.hostedemail.com: domain of elver@google.com designates 209.85.217.54 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678358050; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4DQp9yJyPkEGrEdSLXE5G+gqDSEdWsLMEj9kD3k6bFk=; b=AO2WFqRYP33Wb9IBNp+v9eYsrgQXLopbnj5fGaIFFSQ3D+q0SdOIUvr9cTPdY6aoUgcl99 0iJCX3vB2P+yh/pKE/9PBYnOsoKsZY+uUCmZ5CvbTJ589+VLn2e7Wirv2bzQ9pnNq/R10d WCK6dZNZ0AWk26TmXMIQLh+Y15IRve8= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b="d7bLR//u"; spf=pass (imf30.hostedemail.com: domain of elver@google.com designates 209.85.217.54 as permitted sender) smtp.mailfrom=elver@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678358050; a=rsa-sha256; cv=none; b=kKcH+A4dUJzoyviUrhWOg3FqU0ojslPxcHi3W7Gi4tq7ekv0SQhVyZqJCThQqiYGC9NWQB bzTN83Q+mqZvZe+Tf+sukwi2stFVPBIEb4bCDvXLIxddHTgKMu6StqhT/5szPqTBOik4md Gqi2AXrACfLxkiZ2/q35BuCvQs3AuwM= Received: by mail-vs1-f54.google.com with SMTP id o6so1152359vsq.10 for ; Thu, 09 Mar 2023 02:34:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678358049; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=4DQp9yJyPkEGrEdSLXE5G+gqDSEdWsLMEj9kD3k6bFk=; b=d7bLR//uD7dgFTpepKqns2NpSELuocztK3HgA03x9o77F7ntqz9lNzMZTwW5fJwcLF eUOIA2YEAdOxnn+NNL2BTXybRKadsfNeMdno7+WgD7BsmeQJi4h3nDzclzdDfob2VP1/ V5vihdc1EY2zuSyNyfUsygof1HwE2zkw4SqKPIn4C6nVoJLFtfX3RXkUVZ7hv7DwEy62 8hnHF/mpnBnZtnDpEnNFwDWZmUJMbIosV6nyVhRK5cRN96x6Qhwq8j+BxBoiZrUDSVPq C9Ts8KlGiXR+wBwuAqmlUjnYC8/qDzAi34gQyt6t7Odan1hlva+yywYOXWxlt1vnvN0V SVKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678358049; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4DQp9yJyPkEGrEdSLXE5G+gqDSEdWsLMEj9kD3k6bFk=; b=KSJ2lZTMtXoJRU/DVOCuvuEPWVRWjJA2aSh46ARaglnbAlXSK6PGZyawuzIUCQLf+S nPTTf1J7gYi44krpcXvvgPu8qvlVQwdNgYs3SCCYqTc2ZH6OOnATcCZrV1b+QYHx/3KZ V7DHv9wrTmXKK9vNNB4Lnb3t9IIS9yMmrh7q5Z9no1qJ9SyJadlA+VJ1pysoH3kJ1xj3 vD3g44a0eVidx6U7GCT6mSQ/1uo02q3lc2/hdbnP9bgsdUTNBiRwxj7LNu8QyNjfWp4r oVtnObojkCfnXqVTa2fRfUVpY4lgKMtDKtWgbfUV6tg5fXY/ehTHDh0SFMiq5QnC1298 y49g== X-Gm-Message-State: AO0yUKWnSNzmyCLKCDvbSAdKtFTDjOM6mwfkJ40D2tRUjv5Gqc3n3CPd 3Ck20fMoL2EN3hr47ccT36nxzGSk7WhH+E20DOKU4w== X-Google-Smtp-Source: AK7set9cKf8W7/SkNKtgYOj7Ef15d129jECAf2lr6Fp2om0lIPw+G2KJX74I1ki9PYeiDQpG3Ob+tEbMVCgfdIAZ0jk= X-Received: by 2002:a67:ce0a:0:b0:416:e50f:8215 with SMTP id s10-20020a67ce0a000000b00416e50f8215mr14184989vsl.4.1678358048733; Thu, 09 Mar 2023 02:34:08 -0800 (PST) MIME-Version: 1.0 References: <1678349122-19279-1-git-send-email-quic_zhenhuah@quicinc.com> In-Reply-To: <1678349122-19279-1-git-send-email-quic_zhenhuah@quicinc.com> From: Marco Elver Date: Thu, 9 Mar 2023 11:33:30 +0100 Message-ID: Subject: Re: [PATCH] mm,kfence: decouple kfence from page granularity mapping judgement To: Zhenhua Huang Cc: catalin.marinas@arm.com, will@kernel.org, glider@google.com, dvyukov@google.com, akpm@linux-foundation.org, robin.murphy@arm.com, mark.rutland@arm.com, jianyong.wu@arm.com, james.morse@arm.com, linux-arm-kernel@lists.infradead.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, quic_pkondeti@quicinc.com, quic_guptap@quicinc.com, quic_tingweiz@quicinc.com Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: id54bdhun7fi9p8ecr5814ypc73x6orz X-Rspamd-Queue-Id: 05B4080015 X-HE-Tag: 1678358049-755764 X-HE-Meta: U2FsdGVkX19QyeKuN5xIWjwN6mO70Zu3Ck3TkBk5f2k3cqerOPZk23GyIp+2qng6esj9gYJWaQaQIGrm6Woz+QaLgh9OwqvWYzI+9VTdhWGpInBVoInkRJKWPfHePdDvQC1656dlk0y84eaOudKXTW6UcbVoPOPjR7bXxZ+V+Ffqy3WNSp0EjcpfKgUB3UP//9rXf1nxlNoUdlIdC9i5nw+lf0cJtzesCCbujUc+bvr7FyzoQlejbIGb/VX+5o579tD6As9/NXmuy1if5FLtwL8Ky8L5JTDR1JRFbvgk3MGZWriPyAuiXi4FavrwnVzrnJxHL8F9Lnxtqhy3zqq7ph5PB8laxKKILS9mVoAGVIjQ8fM01Vj8mPbbvIFwy/cCeQ0wo8MY2lGMSUwzqSeAfzsDD1+NjGcvyFsY7zNWRqVc/nHAhT7zqDCfNX/K/xrDGiiw/n369oYF0gDwvcZEDzjo8wGOdxI6UZQk8zAxp0zthgxGr6LU7K+WKY3oA2+FLbOh2FcLA4VQaVDoMhNnLUcXinq/uWfKhDRkQ963cqTIjEwjGgeetDQaZkQrpO+gYOf8DC8g1jvtdfac11ZcXLW4gfZo9JZCoajaod8QaWKbLfWwutDovu4qhnrBKmfaciTTSnQISZiJmhvw6eCu/C23mv5ePSw/VYL2XG1YwTDBxlzR6wi0VVpsyqfXniMg/3Ki70/kYZAiHqRdAsgJXwTc0AaxinwOTU/OFDd5YSpkhdKhGZThe1+RI58Xpnvp4meE+5RzkGJiLUsUgf8uZ4nGJklSLHeK6nlDHOvlFuADqif/iI305QlBKR0+T5YuEQ0d62as1LT5WLO5ONcXOR7YnbBdtD7UaebQl0Tr2GIdqKH+dE1NxPi1rwzsMpSZi8uURT6OVTfdD8wIb7Xv/knVv5sZhtaoWOG7K4Dw+V89+aNrfgVkrcUlV4pIZhg5KxOV+G3aioUuRidftdP xFehtINg DUXDlIuOFLUCnilpSrDhTYltVQ5p/KC1qKYEmUdsB/oTnt8E55bDmlI5oHqVQcweVtp2I+2kF5vPeaczH1Iiar1ELAQxIbTktNhGhKuq2r5TlUZreFkhayr4Yixjqx6JfGyzSOeIJzIm/YKswnRbDM3uFJdWkrWQnaHO7WE04D1+vtKMCchN+TmlRMrTPnj4QwNKdQf9T+1VjtoN1VMChRl0PnJD1mszp9TPUx12bVD7sKiHQbxlMkPZf8a5YwW3HRt1UL/q9kniUQVY+OCMNq9xrvviMjNPUiuJos7ThPe2T6LMWl8vu0LDSWE2afIdHv1GgMD0epiRgTUAwuYsIFlAuuGzAN+S80Ij+yqEeJryi+RH11SCTQ3Ngv9t0kqozlqiKM2E4StyO0b9FdIueVpibckalQuUiLeeSXiUsAotIows= 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 Thu, 9 Mar 2023 at 09:05, Zhenhua Huang wrote: > > Kfence only needs its pool to be mapped as page granularity, previous > judgement was a bit over protected. Decouple it from judgement and do > page granularity mapping for kfence pool only [1]. > > To implement this, also relocate the kfence pool allocation before the > linear mapping setting up, kfence_alloc_pool is to allocate phys addr, > __kfence_pool is to be set after linear mapping set up. > > LINK: [1] https://lore.kernel.org/linux-arm-kernel/1675750519-1064-1-git-send-email-quic_zhenhuah@quicinc.com/T/ > Suggested-by: Mark Rutland > Signed-off-by: Zhenhua Huang > --- > arch/arm64/mm/mmu.c | 24 ++++++++++++++++++++++++ > arch/arm64/mm/pageattr.c | 5 ++--- > include/linux/kfence.h | 10 ++++++++-- > init/main.c | 1 - > mm/kfence/core.c | 18 ++++++++++++++---- > 5 files changed, 48 insertions(+), 10 deletions(-) > > diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c > index 6f9d889..bd79691 100644 > --- a/arch/arm64/mm/mmu.c > +++ b/arch/arm64/mm/mmu.c > @@ -24,6 +24,7 @@ > #include > #include > #include > +#include > > #include > #include > @@ -532,6 +533,9 @@ static void __init map_mem(pgd_t *pgdp) > phys_addr_t kernel_end = __pa_symbol(__init_begin); > phys_addr_t start, end; > int flags = NO_EXEC_MAPPINGS; > +#ifdef CONFIG_KFENCE > + phys_addr_t kfence_pool = 0; > +#endif > u64 i; > > /* > @@ -564,6 +568,12 @@ static void __init map_mem(pgd_t *pgdp) > } > #endif > > +#ifdef CONFIG_KFENCE > + kfence_pool = kfence_alloc_pool(); > + if (kfence_pool) > + memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE); > +#endif > + > /* map all the memory banks */ > for_each_mem_range(i, &start, &end) { > if (start >= end) > @@ -608,6 +618,20 @@ static void __init map_mem(pgd_t *pgdp) > } > } > #endif > + > + /* Kfence pool needs page-level mapping */ > +#ifdef CONFIG_KFENCE > + if (kfence_pool) { > + __map_memblock(pgdp, kfence_pool, > + kfence_pool + KFENCE_POOL_SIZE, > + pgprot_tagged(PAGE_KERNEL), > + NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS); > + memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE); > + /* kfence_pool really mapped now */ > + kfence_set_pool(kfence_pool); > + } > +#endif > + > } > > void mark_rodata_ro(void) > diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c > index 79dd201..61156d0 100644 > --- a/arch/arm64/mm/pageattr.c > +++ b/arch/arm64/mm/pageattr.c > @@ -22,12 +22,11 @@ bool rodata_full __ro_after_init = IS_ENABLED(CONFIG_RODATA_FULL_DEFAULT_ENABLED > bool can_set_direct_map(void) > { > /* > - * rodata_full, DEBUG_PAGEALLOC and KFENCE require linear map to be > + * rodata_full and DEBUG_PAGEALLOC require linear map to be > * mapped at page granularity, so that it is possible to > * protect/unprotect single pages. > */ > - return (rodata_enabled && rodata_full) || debug_pagealloc_enabled() || > - IS_ENABLED(CONFIG_KFENCE); > + return (rodata_enabled && rodata_full) || debug_pagealloc_enabled(); > } > > static int change_page_range(pte_t *ptep, unsigned long addr, void *data) > diff --git a/include/linux/kfence.h b/include/linux/kfence.h > index 726857a..0252e74 100644 > --- a/include/linux/kfence.h > +++ b/include/linux/kfence.h > @@ -61,7 +61,12 @@ static __always_inline bool is_kfence_address(const void *addr) > /** > * kfence_alloc_pool() - allocate the KFENCE pool via memblock > */ > -void __init kfence_alloc_pool(void); > +phys_addr_t __init kfence_alloc_pool(void); > + > +/** > + * kfence_set_pool() - KFENCE pool mapped and can be used > + */ > +void __init kfence_set_pool(phys_addr_t addr); > > /** > * kfence_init() - perform KFENCE initialization at boot time > @@ -223,7 +228,8 @@ bool __kfence_obj_info(struct kmem_obj_info *kpp, void *object, struct slab *sla > #else /* CONFIG_KFENCE */ > > static inline bool is_kfence_address(const void *addr) { return false; } > -static inline void kfence_alloc_pool(void) { } > +static inline phys_addr_t kfence_alloc_pool(void) { return (phys_addr_t)NULL; } > +static inline void kfence_set_pool(phys_addr_t addr) { } > static inline void kfence_init(void) { } > static inline void kfence_shutdown_cache(struct kmem_cache *s) { } > static inline void *kfence_alloc(struct kmem_cache *s, size_t size, gfp_t flags) { return NULL; } > diff --git a/init/main.c b/init/main.c > index 4425d17..9aaf217 100644 > --- a/init/main.c > +++ b/init/main.c > @@ -839,7 +839,6 @@ static void __init mm_init(void) > */ > page_ext_init_flatmem(); > init_mem_debugging_and_hardening(); > - kfence_alloc_pool(); This breaks other architectures. > report_meminit(); > kmsan_init_shadow(); > stack_depot_early_init(); > diff --git a/mm/kfence/core.c b/mm/kfence/core.c > index 5349c37..dd5cdd5 100644 > --- a/mm/kfence/core.c > +++ b/mm/kfence/core.c > @@ -809,15 +809,25 @@ static void toggle_allocation_gate(struct work_struct *work) > > /* === Public interface ===================================================== */ > > -void __init kfence_alloc_pool(void) > +phys_addr_t __init kfence_alloc_pool(void) > { You could just return here: if (__kfence_pool) return; /* Initialized earlier by arch init code. */ ... and see my comments below. > + phys_addr_t kfence_pool; > if (!kfence_sample_interval) > - return; > + return 0; > > - __kfence_pool = memblock_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); > + kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE); > > - if (!__kfence_pool) > + if (!kfence_pool) { > pr_err("failed to allocate pool\n"); > + return 0; > + } > + > + return kfence_pool; > +} > + > +void __init kfence_set_pool(phys_addr_t addr) > +{ > + __kfence_pool = phys_to_virt(addr); > } I would suggest leaving kfence_alloc_pool() to return nothing (with the addition above), and just set __kfence_pool as before. __kfence_pool itself is exported by include/linux/kfence.h, so if you call kfence_alloc_pool() in arm64 earlier, you can access __kfence_pool to get the allocated pool. Because at that point, KFENCE isn't yet running, that only happens after kfence_init() much later. With these changes, you should be able to make arm64 work the way you want, and not break other architectures where we don't need arch init code to allocate the pool. Thanks, -- Marco