From patchwork Fri Sep 24 01:06:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12513821 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=-18.2 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 0B68CC433EF for ; Fri, 24 Sep 2021 01:10: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 CB0EC61211 for ; Fri, 24 Sep 2021 01:10:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org CB0EC61211 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:Mime-Version: Message-Id:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=/1O2XYmCDN9lDMt58ONVEfoH0kwxpgxTzKYdGcAr7WY=; b=jyV akYuBwHOspqlEiUTSgyI7v455GlIYKzSMuIMBxBE8VfI4IxkA+uYzo3xLQHO3iQGrsvEz8ZD0IxFZ Q76np46LrHw5lCMcT0Q4njZx3LRGXwiSRpjR9+ul0fyxUBAB2e7usbzG0T5Pb2W7/zjKRJ3QMXGcV j/KjSDdYSg1fV6OQ3cLO5fbBe2hA65QMLIR4iZAlv4iLzm+bMaeoao5rPBynZIlTzvnBVYtfKjXYo dRJK2Ppw9Ezrw6fi6bcMH3CLv2f7MA6GRE/Fe10nc5cHLL0PR/ZGSU7JaNx6YqDrp4jN4kFhsTmoh oXL0i2SaA9mG4FIbNlu0mZitGNCm3HQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mTZgd-00CnwU-SY; Fri, 24 Sep 2021 01:07:36 +0000 Received: from mail-qk1-x74a.google.com ([2607:f8b0:4864:20::74a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mTZgZ-00Cnvw-SM for linux-arm-kernel@lists.infradead.org; Fri, 24 Sep 2021 01:07:33 +0000 Received: by mail-qk1-x74a.google.com with SMTP id ay30-20020a05620a179e00b00433294fbf97so30382649qkb.3 for ; Thu, 23 Sep 2021 18:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=rhzuRBlj9eU9uvDmJyCbKMg9oFZSqkucfy75BP9pIts=; b=F/1cShoGIZRWmQRTJb2TwqMJAFCmh0HzCkfl4h3ev8jhoEGZkktmtpLxQ8bltfEJ/C twVQZs9vrn4RM0wZ9yLCE/YmMlzDJ7DG/xPtNLTMq8gP8HEnkz7/I872apvvk9faM6mn QEOglKKDj8xRmlqpuKz/g3V6QIG6dwrdHTL9qyh0w3G6fMT4FvhrjFAHhIH5kxbbmYgP WYBfTdZRcOEd5/glpcwoTrKtQcrGRmSnj4T8/LQFllw3CLDXyOdIO/LfOyW91pqodFZP aQiTcQDpxHpuct0Dbcz4IPrNYwzsUk158jp42/albGUYXL7JwLl7dKOUNNaylvZTQQnm RZOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=rhzuRBlj9eU9uvDmJyCbKMg9oFZSqkucfy75BP9pIts=; b=eRzETctuITCwgNhlkaaCCPSfPlLKy1m+3R8xfA6oRMgZPY0Ecu8RkuEOadOTSjNhm+ L6KxP7j58DDFFWijccyqQjUtBZb/QQgNWZRzGHEGZLkfUSGuS0DsXIVeVeKh48r8GN14 QWNx1+hxtPefAJya8ydgtwRaAf4ayjvxcQSw5GilkQhW6ASMsHee/AVtpNCYXvISJMlu kL5wE8fsb7KBB8U7uKjsM8cexxPyjhXrVaDQn/kwvhD41kAizgIeRQa1/Hp7mo0bBDw/ maVDx0ZHDGdy82oRRX5cCEi3SYKfc4zutY+/M0NmSbyKpzNjKJZ3kvh1on1R+hNhfvVJ uoVQ== X-Gm-Message-State: AOAM530JRQ16IjDYCnD/ZTYYDVdkczW9/QSqc+OE9EA4JEVA1X5UXMg3 Vo1Ckuh8jLdbE1hYW8wwPESc39E= X-Google-Smtp-Source: ABdhPJxKYxVWOJzcJf0X39xUUku0Bl8VJWge0g5D05nVlOTvUpIFtFIM3aNxJxWskoyOHY810VcEfBw= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:eafa:bd6f:107d:7c0a]) (user=pcc job=sendgmr) by 2002:ad4:4aad:: with SMTP id i13mr7325759qvx.27.1632445649963; Thu, 23 Sep 2021 18:07:29 -0700 (PDT) Date: Thu, 23 Sep 2021 18:06:55 -0700 Message-Id: <20210924010655.2886918-1-pcc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.685.g46640cef36-goog Subject: [PATCH v4] 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-20210923_180731_946636_6D362246 X-CRM114-Status: GOOD ( 18.23 ) 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 Reviewed-by: Catalin Marinas --- v4: - move the gcr update into a separate function v2: - add prerequisite patch to fix booting on non-MTE hardware arch/arm64/kernel/entry.S | 10 +++++----- arch/arm64/kernel/mte.c | 26 ++++++++++++++++++++++++++ include/linux/kasan.h | 9 +++++++-- 3 files changed, 38 insertions(+), 7 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 e5e801bc5312..0cdae086966e 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -179,6 +179,30 @@ static void mte_update_sctlr_user(struct task_struct *task) task->thread.sctlr_user = sctlr; } +static void mte_update_gcr_excl(struct task_struct *task) +{ + /* + * 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()) + return; + + write_sysreg_s( + ((task->thread.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) { if (!system_supports_mte()) @@ -198,6 +222,7 @@ void mte_thread_switch(struct task_struct *next) return; mte_update_sctlr_user(next); + mte_update_gcr_excl(next); /* * Check if an async tag exception occurred at EL1. @@ -243,6 +268,7 @@ long set_mte_ctrl(struct task_struct *task, unsigned long arg) if (task == current) { preempt_disable(); mte_update_sctlr_user(task); + mte_update_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 {