From patchwork Tue Sep 14 21:54:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12494943 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=-17.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, USER_AGENT_GIT 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 6723DC433F5 for ; Tue, 14 Sep 2021 21:56:56 +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 2E54960F21 for ; Tue, 14 Sep 2021 21:56:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2E54960F21 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:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=rnrNEZnXwFd1RV7j9mKSWbbnReb/ILUnhPnHTgTh8nw=; b=pYte1LJ9b8eMzPujwymg4r8Y2C eKc7tS5IcKinXNfcVVKLmEtWXPzAIl3Yd+yOqcwsGU7rcLMelhMOORHkC7HuQ42GOLG7snXGF0rvF JpNGFlLHZBax+V+q4lZTPkeAj6M3N3kDmiF8PnavGRFFNk3yKOC7UugqJpknqrCPlJYy9JCzDSm34 ctL4hsX3iKWk7R4bC2fGLnI/xJyeamdPOQuAzky8j3tfIqCYac+qJuWipfVaIosssz+hsR7MTlIXn nYPr7UeFBDLLSneBHkNMl9Miw99M8kZJHKgAkoRlkU1xxnw2WMW+huLyU0F+rgHerm149b8WbAsjL T/2uuZrg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mQGOO-007DKY-2c; Tue, 14 Sep 2021 21:55:04 +0000 Received: from mail-qv1-xf49.google.com ([2607:f8b0:4864:20::f49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mQGO1-007DIG-GU for linux-arm-kernel@lists.infradead.org; Tue, 14 Sep 2021 21:54:43 +0000 Received: by mail-qv1-xf49.google.com with SMTP id h25-20020a0cab19000000b0037a49d15c93so1024014qvb.22 for ; Tue, 14 Sep 2021 14:54:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=UzF2gV7QAYvK64sYW0fwZA147TPrDIulHLAuupzaM24=; b=gXJai2UAJYlWT6cZJXFVFqFJMp2CkahKc66t52XhH3rmAo6PbTRFtFFM6VwRc43hnr RAKmd9HHK4bTHqF9idQ18TCYMbXVNNAbluIL8y8OsRNKfVd9xyhBamMMFY8Q+eKxdPGC 0MIdfA/XFQq6N4UekFmsgSQKYTTPhWWYKRlBL5yYJ2CQSHqiqH2QzEgMLEJSnIdx7JvF FEITMWWEv9XIrn9Voba5HfI85sXGTpLoPr2gQO7My1G6FtHoEW2mRG05mS46zc45vCoV FspeDUxRxkLbPC3U63ropyxj3pAT/jQ3mEh5dX/XKB4ZfWhp6mlcJB2rnW10rh8ADY+z kWig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=UzF2gV7QAYvK64sYW0fwZA147TPrDIulHLAuupzaM24=; b=GILXrPiDDoS78LOcvMFVsnMnMpj56nCuLNSc3IGdowv7hLybmkShRPlv+RvGxh57Z/ YplmLhCIOdIZyCSe9JCGii3rDFs5Xrv/Zv/b3KGOzCfzNamXHZKAf+qoEpviaWveh0SH ED5tn9JBnatoLGWAOy67sO5vZNSB42tjBvxmBMtN0SYbBpMRgEGN4cBgT5XHXlgj0lqx V5+9i2ZxqCWht6ObhURMgYY9P3jWJ/mPuOEqLB58ZymipJgCzFRTIuj9Dtd+f+re5WkV eXuVou3xM4tHPsPygTxL3rPTMZZskwtPOWcjJC1yOCE6F38XC+NdcRXDC3bkwDfJ10y8 03zQ== X-Gm-Message-State: AOAM532Ep7gDsAznFszU3NKSlTKWK3SqvxhTayPvM0n8Bb5GG3WZrXhO tKWQM/tcFjLYddGmC6PO2kI/adM= X-Google-Smtp-Source: ABdhPJxCUHdC4GVQv9ahY6g6kIgHef2GMSBVjmFIPOecwgybq4css740CWFPDRurTTZ6tJcs30dkZ3U= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:42fd:ba5d:d2c8:ab5]) (user=pcc job=sendgmr) by 2002:a0c:db0a:: with SMTP id d10mr7918510qvk.28.1631656478866; Tue, 14 Sep 2021 14:54:38 -0700 (PDT) Date: Tue, 14 Sep 2021 14:54:29 -0700 In-Reply-To: <20210914215429.745849-1-pcc@google.com> Message-Id: <20210914215429.745849-2-pcc@google.com> Mime-Version: 1.0 References: <20210914215429.745849-1-pcc@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH v2 2/2] arm64: kasan: mte: move GCR_EL1 switch to task switch when KASAN disabled From: Peter Collingbourne To: Catalin Marinas , Vincenzo Frascino , Will Deacon , Andrey Konovalov Cc: Peter Collingbourne , Evgenii Stepanov , linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210914_145441_595766_2CE2553D X-CRM114-Status: GOOD ( 18.08 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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 Acked-by: Andrey Konovalov Link: https://linux-review.googlesource.com/id/I78e90d60612a94c24344526f476ac4ff216e10d2 --- v2: - add prerequisite patch to fix booting on non-MTE hardware 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 bc6d5a970a13..2f69ae43941d 100644 --- a/arch/arm64/kernel/entry.S +++ b/arch/arm64/kernel/entry.S @@ -168,9 +168,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: @@ -178,10 +178,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 8fc2d90eeb00..5a026a91b4bf 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) @@ -202,7 +220,7 @@ void mte_thread_switch(struct task_struct *next) if (!system_supports_mte()) return; - mte_update_sctlr_user(next); + mte_update_sctlr_user_and_gcr_excl(next); /* * Check if an async tag exception occurred at EL1. @@ -247,7 +265,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 {