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 X-Spam-Level: X-Spam-Status: No, score=-14.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3AADBC433EF for ; Fri, 3 Sep 2021 22:14:15 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id ED56761051 for ; Fri, 3 Sep 2021 22:14:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org ED56761051 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=DQRJglGjEWKJZlmOr+uB7L0avzP8ueaMyv9zjDCLH6Q=; b=d/Qlj1YtTfF6Og HNN8VQ207N9ssGl5IrTEKGpCWdYTyynJLqbpx+MCnDAN3ZTZ+CYyRO2QNcJj3xabV3x/TSD2eyK3u FR8lkmRP2D2PdCnDQ+XnoSzHQ0zQhu4S5uhHi0VDBN7jGDyevFjoZz8spz6E4ZCf3SIzNoRxjlfIG OdYiCl2mFwRgyzYhFFkcPjsijMflaCFTkVoQ6bBj3l2m/qx1+b4DmPaQS3sI/fnRHaizb+de+AAu8 4LR/dJOxlxSYND9MR/TL9lKqUdINfOVeQbmXkhuLFmrajPf/AQr5cx+6sw11vRFG1lFG6vERmA96D OytjAe8DabjdK1LGuN1Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mMHQB-00D56g-2f; Fri, 03 Sep 2021 22:12:27 +0000 Received: from mail-io1-xd33.google.com ([2607:f8b0:4864:20::d33]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mMHQ6-00D567-O5 for linux-arm-kernel@lists.infradead.org; Fri, 03 Sep 2021 22:12:24 +0000 Received: by mail-io1-xd33.google.com with SMTP id f6so535248iox.0 for ; Fri, 03 Sep 2021 15:12:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=y0vsFwFY/FLdVuOVTnh70gJFGSLtapvFHiYeuWRhSSA=; b=pCUg97UfkzZaegG+W9DIAeBDDYV+qMaZRP0BV570nfGYUMvCBAbGZA1AardXWTlOIt qiZU54SqKPabO7SMdYcF7F8fQNtbT7AKxmmvuoy/4f9WAN/xyys9kcA9alF4f7mXh318 80aPKmqkL3CImJwxRQ3WmEQm1hM6u5+Ht17Ar4qcVWFDt/mA7mJV52qcZhD6gVTXby8J ryL2eTXhB2366NC6IbaEWlunUC5AaqWCsubQwOIDe8Ur8SRjVAGMSTl1BV7jE6v1PTSa Vbp/ryEBDpg+uZXGiGdDP9VDTnhiWNztT1v54j8vhuPgW2yK5hAtndNndhSGpcMfF2wz Nxbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=y0vsFwFY/FLdVuOVTnh70gJFGSLtapvFHiYeuWRhSSA=; b=cNUXtEIcxBAnD9j1+hG3eiZWHCsgHR42BdRAbeX116VOHrcpOP39hFMihzbCR/RJO0 jpMOQ63j5nItXWDDYBP6Et1Cs0hBwqnQqBcYQKeutpxlisaBmX+vM6Zlb8n68sZBEyoW LmfdzJfBa6Bj9hMupjPOSI1BJSIZLgJzMBLrHxkeD9m8z9wTiFj7ZlTZUrBDrb7+aRtl Xu4rYee6ro7qtbUSymVRba5aLve17G7QbBjWh8YiMHk1Uz+eG2O9in+/6qa3/E17XZot rwY9cPPK/QhdMjFsPA1o3bCiseTS11L4aCVDlLy6iH0RJAmiL90rtUuQKnCEsZi2Yfec fwFw== X-Gm-Message-State: AOAM533Mqk+GuwfgecKvcq5hCLUAhGc8mhy24UrG98MF9QvRf/QUtGA+ dXHQgfGrtodkJWaVlDghChKHD249HLuqfktA/l0ojw== X-Google-Smtp-Source: ABdhPJyW5Zckv1NqAPip+Q9ZbebCvMK0ifBe0WoqkNsXWe2AWcUb4lF5VXI2F5JH1kzTi98bpeQfZHtZvqCgcAEkey8= X-Received: by 2002:a5e:dc0b:: with SMTP id b11mr844824iok.91.1630707138077; Fri, 03 Sep 2021 15:12:18 -0700 (PDT) MIME-Version: 1.0 References: <20210827030748.587710-1-pcc@google.com> In-Reply-To: From: Peter Collingbourne Date: Fri, 3 Sep 2021 15:12:06 -0700 Message-ID: Subject: Re: [PATCH] arm64: kasan: mte: move GCR_EL1 switch to task switch when KASAN disabled To: Andrey Konovalov Cc: Catalin Marinas , Vincenzo Frascino , Will Deacon , Evgenii Stepanov , Linux ARM X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210903_151222_836702_F87F1290 X-CRM114-Status: GOOD ( 32.71 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Sun, Aug 29, 2021 at 7:10 AM Andrey Konovalov wrote: > > On Fri, Aug 27, 2021 at 5:07 AM Peter Collingbourne wrote: > > > > It is not necessary to write to GCR_EL1 on every kernel entry and > > exit when HW tag-based KASAN is disabled because the kernel will not > > execute any IRG instructions in that mode. Since accessing GCR_EL1 > > can be expensive on some microarchitectures, avoid doing so by moving > > the access to task switch when HW tag-based KASAN is disabled. > > > > Signed-off-by: Peter Collingbourne > > --- > > arch/arm64/kernel/entry.S | 10 +++++----- > > arch/arm64/kernel/mte.c | 24 +++++++++++++++++++++--- > > include/linux/kasan.h | 9 +++++++-- > > 3 files changed, 33 insertions(+), 10 deletions(-) > > > > diff --git a/arch/arm64/kernel/entry.S b/arch/arm64/kernel/entry.S > > index 923ee2ac85fd..e12ed892cde8 100644 > > --- a/arch/arm64/kernel/entry.S > > +++ b/arch/arm64/kernel/entry.S > > @@ -178,9 +178,9 @@ alternative_else_nop_endif > > > > .macro mte_set_kernel_gcr, tmp, tmp2 > > #ifdef CONFIG_KASAN_HW_TAGS > > -alternative_if_not ARM64_MTE > > +alternative_cb kasan_hw_tags_enable > > b 1f > > -alternative_else_nop_endif > > +alternative_cb_end > > mov \tmp, KERNEL_GCR_EL1 > > msr_s SYS_GCR_EL1, \tmp > > 1: > > @@ -188,10 +188,10 @@ alternative_else_nop_endif > > .endm > > > > .macro mte_set_user_gcr, tsk, tmp, tmp2 > > -#ifdef CONFIG_ARM64_MTE > > -alternative_if_not ARM64_MTE > > +#ifdef CONFIG_KASAN_HW_TAGS > > +alternative_cb kasan_hw_tags_enable > > b 1f > > -alternative_else_nop_endif > > +alternative_cb_end > > ldr \tmp, [\tsk, #THREAD_MTE_CTRL] > > > > mte_set_gcr \tmp, \tmp2 > > diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c > > index 9d314a3bad3b..0e8edb5d5861 100644 > > --- a/arch/arm64/kernel/mte.c > > +++ b/arch/arm64/kernel/mte.c > > @@ -162,7 +162,7 @@ void mte_check_tfsr_el1(void) > > } > > #endif > > > > -static void mte_update_sctlr_user(struct task_struct *task) > > +static void mte_update_sctlr_user_and_gcr_excl(struct task_struct *task) > > { > > /* > > * This must be called with preemption disabled and can only be called > > @@ -182,6 +182,24 @@ static void mte_update_sctlr_user(struct task_struct *task) > > else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC) > > sctlr |= SCTLR_EL1_TCF0_SYNC; > > task->thread.sctlr_user = sctlr; > > + > > + /* > > + * SYS_GCR_EL1 will be set to current->thread.mte_ctrl value by > > + * mte_set_user_gcr() in kernel_exit, but only if KASAN is enabled. > > + */ > > + if (!kasan_hw_tags_enabled()) > > + write_sysreg_s(((mte_ctrl >> MTE_CTRL_GCR_USER_EXCL_SHIFT) & > > + SYS_GCR_EL1_EXCL_MASK) | SYS_GCR_EL1_RRND, > > + SYS_GCR_EL1); > > +} > > + > > +void __init kasan_hw_tags_enable(struct alt_instr *alt, __le32 *origptr, > > + __le32 *updptr, int nr_inst) > > +{ > > + BUG_ON(nr_inst != 1); /* Branch -> NOP */ > > + > > + if (kasan_hw_tags_enabled()) > > + *updptr = cpu_to_le32(aarch64_insn_gen_nop()); > > } > > > > void mte_thread_init_user(void) > > @@ -199,7 +217,7 @@ void mte_thread_init_user(void) > > > > void mte_thread_switch(struct task_struct *next) > > { > > - mte_update_sctlr_user(next); > > + mte_update_sctlr_user_and_gcr_excl(next); > > > > /* > > * Check if an async tag exception occurred at EL1. > > @@ -244,7 +262,7 @@ long set_mte_ctrl(struct task_struct *task, unsigned long arg) > > task->thread.mte_ctrl = mte_ctrl; > > if (task == current) { > > preempt_disable(); > > - mte_update_sctlr_user(task); > > + mte_update_sctlr_user_and_gcr_excl(task); > > update_sctlr_el1(task->thread.sctlr_user); > > preempt_enable(); > > } > > diff --git a/include/linux/kasan.h b/include/linux/kasan.h > > index dd874a1ee862..de5f5913374d 100644 > > --- a/include/linux/kasan.h > > +++ b/include/linux/kasan.h > > @@ -89,7 +89,7 @@ static __always_inline bool kasan_enabled(void) > > return static_branch_likely(&kasan_flag_enabled); > > } > > > > -static inline bool kasan_has_integrated_init(void) > > +static inline bool kasan_hw_tags_enabled(void) > > { > > return kasan_enabled(); > > } > > @@ -104,7 +104,7 @@ static inline bool kasan_enabled(void) > > return IS_ENABLED(CONFIG_KASAN); > > } > > > > -static inline bool kasan_has_integrated_init(void) > > +static inline bool kasan_hw_tags_enabled(void) > > { > > return false; > > } > > @@ -125,6 +125,11 @@ static __always_inline void kasan_free_pages(struct page *page, > > > > #endif /* CONFIG_KASAN_HW_TAGS */ > > > > +static inline bool kasan_has_integrated_init(void) > > +{ > > + return kasan_hw_tags_enabled(); > > +} > > + > > #ifdef CONFIG_KASAN > > > > struct kasan_cache { > > -- > > 2.33.0.259.gc128427fd7-goog > > > > Acked-by: Andrey Konovalov > > for non-arm-specific KASAN parts. Hi Catalin, Would you be able to take a look please? Do you think it's possible that we could get this in before the end of the merge window? Peter _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel