From patchwork Wed Jun 30 23:15:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12352939 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.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, URIBL_BLOCKED,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 B2B42C11F66 for ; Wed, 30 Jun 2021 23:17:51 +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 7BBD46120C for ; Wed, 30 Jun 2021 23:17:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7BBD46120C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@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=RoeLWtrJcRnvuYABoxIZzjVI4WrWmpUjsw6+dM0CvNs=; b=wEV3r+WmCwzTGkdiObWpbjrHEQ 2RMQC5r/q8fEYwPGGlFLDFd9FBeA4xh6aGkaxHYq7EvTv2quVGP2nAGRouDpGDYlc/zTSCpnqZieT 2M34VpYEg9Y96Ej/PTVbUkZvi8gwI3qerXnKoOBNAoDGEezSV2mts6rMd0PRjpMovI7KqkvHUcytt uw6XVbdmA641UdHzzGFoj/lFLHtuVV6fzNE8nIIRaxpNMm+QdKrH2/BArM93xOU9dpJ5xqbjlNxuc kZT53EUNtiXQSF8SIfiMJbFPzdK8N6dLJInt73Apjdzgc2maZae1eV8LXc+tLujKq0mv2xc9m4/Zd nsutZYIQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lyjQx-00FV2k-HX; Wed, 30 Jun 2021 23:15:55 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lyjQU-00FUxc-Vs for linux-arm-kernel@lists.infradead.org; Wed, 30 Jun 2021 23:15:28 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id k11-20020a056902070bb029055a2303fc2dso3224150ybt.11 for ; Wed, 30 Jun 2021 16:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=oE0TFuP/AzeCniPqPzYdW2mcM9nTuA31fDRzFaNu/Ag=; b=RAgK0wdMYgEWytP9QMLwTQlwlbow5y/N80u/fWOSrshm8O4LE7IvdVATfOj6V/p1ch YOca2XL2+jecaz2Ogm9WrpMz/TVVsjMnTic02dPMZ3WVy5v0Lgs62Chix2Bs1dB1NmH7 lhMdw93FgUlIWiyEv0jHDAgPtkQIb2AvTifGwY2Wnx5MNOgr19IP2OXbWKY1jy9NQFWd kpTP1p25ZMexoa8OS8Tvq4UhSW8Can1NWS1aBcEke2AzUUL1kJS/7qWoeAxje1JeUG4y R812Zrf9ZsRlZaYKvNseu8z9qFKISaX+7ZyjlKWYPH5tDKDqYN5Lj3Nt7xYGEab30vzz XIww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=oE0TFuP/AzeCniPqPzYdW2mcM9nTuA31fDRzFaNu/Ag=; b=iKlIn0cI32XOjbOK1/UZAxQr7K7q7aAtHEmeapJ9jsX3xQWaGKZAJoGzbzMzG7GNDA JStvaANrX7n3oebiljONJcIHHTu31R7xdK9Ph1vWkR8tME3m1BrbozV4Bo0i3JRtoB6G FweO2eR5By/UmD/EaI/ZQrS8w2OkGxOLEzWUYlcha9ReH5E26mhCPBwo6EYtaQLRCpr5 fYfTrW7oz+wlgnTpQRxv/PE0C61YUEd2ks+Rm1KZ3nErcup8f+bPFgyyIhyYf1AY0yXn UfbqQEAbP+LzCeohaxm8IMHJH5lSOoz7UMTAq9TCg08LpZUKyH/Y3Yu9hxvi8MuSgU6F iprw== X-Gm-Message-State: AOAM532rqElbHS41eZneDjmbDJej7NukgFTuEEIrshBR2s9cPxq+5pNo XKRnkgSmFweomePStW7SXiZV3EA= X-Google-Smtp-Source: ABdhPJzaiuganhym9CJIoZv93+wn9iQ9HT5JDyIXzjqHrjbMOdM0Kknbfnm7RvY4rnvwQwFOyLqR/7o= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:8b00:104d:c249:d343]) (user=pcc job=sendgmr) by 2002:a25:1804:: with SMTP id 4mr49826526yby.157.1625094925305; Wed, 30 Jun 2021 16:15:25 -0700 (PDT) Date: Wed, 30 Jun 2021 16:15:08 -0700 In-Reply-To: <20210630231509.3773172-1-pcc@google.com> Message-Id: <20210630231509.3773172-4-pcc@google.com> Mime-Version: 1.0 References: <20210630231509.3773172-1-pcc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v8 3/4] arm64: mte: introduce a per-CPU tag checking mode preference From: Peter Collingbourne To: Catalin Marinas , Vincenzo Frascino , Will Deacon Cc: Peter Collingbourne , Evgenii Stepanov , Szabolcs Nagy , Tejas Belagod , linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210630_161527_074617_E17B52DC X-CRM114-Status: GOOD ( 16.76 ) 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 Add a per-CPU sysfs node, mte_tcf_preferred, that allows the preferred tag checking mode to be configured. The current possible values are async and sync. Link: https://linux-review.googlesource.com/id/I7493dcd533a2785a1437b16c3f6b50919f840854 Signed-off-by: Peter Collingbourne Reviewed-by: Catalin Marinas --- arch/arm64/kernel/mte.c | 77 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 7b8c241be24e..9c82e27b30f9 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -26,6 +27,8 @@ u64 gcr_kernel_excl __ro_after_init; static bool report_fault_once = true; +static DEFINE_PER_CPU_READ_MOSTLY(u64, mte_tcf_preferred); + #ifdef CONFIG_KASAN_HW_TAGS /* Whether the MTE asynchronous mode is enabled. */ DEFINE_STATIC_KEY_FALSE(mte_async_mode); @@ -214,17 +217,24 @@ void mte_thread_init_user(void) void mte_update_sctlr_user(struct task_struct *task) { + /* + * This can only be called on the current or next task since the CPU + * must match where the thread is going to run. + */ unsigned long sctlr = task->thread.sctlr_user; - unsigned long pref = MTE_CTRL_TCF_ASYNC; unsigned long mte_ctrl = task->thread.mte_ctrl; - unsigned long resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl; + unsigned long pref, resolved_mte_tcf; + preempt_disable(); + pref = __this_cpu_read(mte_tcf_preferred); + resolved_mte_tcf = (mte_ctrl & pref) ? pref : mte_ctrl; sctlr &= ~SCTLR_EL1_TCF0_MASK; if (resolved_mte_tcf & MTE_CTRL_TCF_ASYNC) sctlr |= SCTLR_EL1_TCF0_ASYNC; else if (resolved_mte_tcf & MTE_CTRL_TCF_SYNC) sctlr |= SCTLR_EL1_TCF0_SYNC; task->thread.sctlr_user = sctlr; + preempt_enable(); } void mte_thread_switch(struct task_struct *next) @@ -441,3 +451,66 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request, return ret; } + +static ssize_t mte_tcf_preferred_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + switch (per_cpu(mte_tcf_preferred, dev->id)) { + case MTE_CTRL_TCF_ASYNC: + return sysfs_emit(buf, "async\n"); + case MTE_CTRL_TCF_SYNC: + return sysfs_emit(buf, "sync\n"); + default: + return sysfs_emit(buf, "???\n"); + } +} + +static void sync_sctlr(void *arg) +{ + mte_update_sctlr_user(current); + set_task_sctlr_el1(current->thread.sctlr_user); +} + +static ssize_t mte_tcf_preferred_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + ssize_t ret = 0; + u64 tcf; + + if (sysfs_streq(buf, "async")) + tcf = MTE_CTRL_TCF_ASYNC; + else if (sysfs_streq(buf, "sync")) + tcf = MTE_CTRL_TCF_SYNC; + else + return -EINVAL; + + device_lock(dev); + per_cpu(mte_tcf_preferred, dev->id) = tcf; + + if (cpu_online(dev->id)) + ret = smp_call_function_single(dev->id, sync_sctlr, NULL, 0); + if (ret == 0) + ret = count; + device_unlock(dev); + + return ret; +} +static DEVICE_ATTR_RW(mte_tcf_preferred); + +static int register_mte_tcf_preferred_sysctl(void) +{ + unsigned int cpu; + + if (!system_supports_mte()) + return 0; + + for_each_possible_cpu(cpu) { + per_cpu(mte_tcf_preferred, cpu) = MTE_CTRL_TCF_ASYNC; + device_create_file(get_cpu_device(cpu), + &dev_attr_mte_tcf_preferred); + } + + return 0; +} +subsys_initcall(register_mte_tcf_preferred_sysctl);