From patchwork Fri Jul 2 19:41:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 12356441 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, 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 5C9F0C07E95 for ; Fri, 2 Jul 2021 19:43: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 2F875613B9 for ; Fri, 2 Jul 2021 19:43:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2F875613B9 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=CjaDa07m/oK92dwHP2nQBSPrLP2Gc6yxZtnfu8dq9MU=; b=MF+sdmuiRWzir5VHLe8X2Oh0HQ 4T5yGiXoIornhOh/ByKZ6W7cQ3ZQZkjbdDML2wYFGHnvqTeDUZCABojYk4erYtHNu8pioGsJ3Uz9Y B/HtsIFyixDRhNFQxTCYmdZthFiXv8cpApiK58UOYs5EfEyYzT2uuvGlXVEnVz9Mo3XyzbwZNcm3S NFWk5zfL6PX/NyUWI02+M/yKMOMIzj8UPNBlMcTasEft1mLFQkD+9z6fsrXYhtQqcRssIBDSd0ovh t2bbu646FElz9K6pJ34sBSsOhC4oQk97H7rbCCr9IVUJZ1hU74aSCTOPYoJ45sXzHXrNJcl4ZaO3+ aVBETjKA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lzP2x-003tTG-W9; Fri, 02 Jul 2021 19:41:56 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lzP2W-003tR2-8r for linux-arm-kernel@lists.infradead.org; Fri, 02 Jul 2021 19:41:29 +0000 Received: by mail-yb1-xb49.google.com with SMTP id k11-20020a056902070bb029055a2303fc2dso11628169ybt.11 for ; Fri, 02 Jul 2021 12:41:26 -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=0T/joJC7xKhkdTN0wYLMgqa8Y1c1ZIjGppXYAMH2li8=; b=CMPdeCdA2C4NbHN7+HChMtawUGr7mP2D2mUNsR//daaCSww+/Ecm9mBwG1nWzJ5ABr Qz0Kg6+fB1Nbsfou2BMvqErLCNRUDkkjeL1KpB8kacmOaDlw5eanYwNXBnKJtuc4Eng0 +wXrFT40TcUfRSbJVODV2HuNwcWCb3naPK4agf2RFtF4dorakYp1O7xGIZOWe+RAQ4Kl xMTdcxzdbtiWUUhwIZzHr1ZyGH+L2HQPQf5OuWtjpUFBNwYQGLOw30gm0M+IWslzXdTo x3zFiiv0zU/9QBDSX3bZEICZ1iRA7zN43E1a2ItuNss6GwLffAIB57hSMp/zCl2SaBp5 /C6w== 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=0T/joJC7xKhkdTN0wYLMgqa8Y1c1ZIjGppXYAMH2li8=; b=it2v/uBWD5HsPvTOcdlUgX7yNZUK4fJtVIKh2+TpRAzYjcDUdB1J+dxe9AGbhS2O1Z cOFOkLMMYzbPU/7LTroTgWQPmG6WE0fjvvgcS4evxnPeET7dIvP5sFNVtJtXlQnNNnB2 Fja9T1BRdfHl8PoODNuy3q91ssfe+kbBlNvL52/3unXRj8shJVuZmHdKAh2EyT28Brf4 vBZA1PDisigj9AVBtk+4OPkWqSdZJhfrFJO3MfRUF4+ya5r0/9MUT4/HH7Phk2fNq/kz gh5JCLfvk8I/K+j0oSEZAKX1wMkDLgUTU124JzeH6m7kRPDOrDivqXBZP50fdVhB7Kk3 yy1Q== X-Gm-Message-State: AOAM530ZSqJwpyPGY5Jxa0slJggLQ94gQOsoQ2ra2iiei7qGlTT7BFAM XkksgiVLtln2Q2sZQdaibfpkWuI= X-Google-Smtp-Source: ABdhPJwkNkw+jvsYQ/QuXf0zg55ifVh1xlp3GuaEIKk/fI/qexdM3fOj0HMDHGFeldUbi9OumUcTpME= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2ce:200:7c5b:5407:a2db:c8fb]) (user=pcc job=sendgmr) by 2002:a25:6c04:: with SMTP id h4mr1444132ybc.122.1625254885348; Fri, 02 Jul 2021 12:41:25 -0700 (PDT) Date: Fri, 2 Jul 2021 12:41:09 -0700 In-Reply-To: <20210702194110.2045282-1-pcc@google.com> Message-Id: <20210702194110.2045282-4-pcc@google.com> Mime-Version: 1.0 References: <20210702194110.2045282-1-pcc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v9 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, Greg Kroah-Hartman X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210702_124128_357042_F36C60F5 X-CRM114-Status: GOOD ( 17.24 ) 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 53d89915029d..48f218e070cc 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); @@ -199,17 +202,24 @@ static void update_gcr_el1_excl(u64 excl) static 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_init_user(void) @@ -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);