From patchwork Tue Aug 27 14:51:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Clark X-Patchwork-Id: 13779631 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id C7D57C54734 for ; Tue, 27 Aug 2024 14:52:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=6O7GfpMrqTz0lBd8UHCZ0XN6KCNGlbZvNorb5xx4MUM=; b=t4Sn6RiVg6pkNOgQsdMyrJup60 RVN8WT2O6ZK1Eu4ixwbV/LQLAp0kTXOQ6ugwQPUncstHtZEesLNFHT5cTGnfHyrLoWV7Z481Pb5PU 9+0l6cRd4PmQazfzYwA7z3wvDSXfVzdMFZR5NS7mmwAyYujEfpO54xA8Z7kQkoV3LNleaOLx+CL3O M7AU+sSTQNvwke5FpPsZcipnBfHJZXxhKmhlRtBRmw4paD9o1aOINgh22kLCaTUdFm6Bj0yLTWdnK ZHWHDnufnSLuFEK8AN6X2vP28D13jgyBPo+6KzD2tkWSCjZ1hn/zC9h/DBAFKfJEy1pux5gRjCcWY L5YaY+DQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sixYc-0000000Bjrr-3FsW; Tue, 27 Aug 2024 14:52:30 +0000 Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sixXo-0000000BjiC-1Y1l for linux-arm-kernel@lists.infradead.org; Tue, 27 Aug 2024 14:51:41 +0000 Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-5befe420fc2so6940648a12.3 for ; Tue, 27 Aug 2024 07:51:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1724770299; x=1725375099; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6O7GfpMrqTz0lBd8UHCZ0XN6KCNGlbZvNorb5xx4MUM=; b=rFamnZlvWjCTlvT1I4oleWoZeRH1l5iG+cnzcS92y98sXlkrjaMwDHg7/WJzXubQEJ 2KYf3BWoSUVkNnGWEZNohY8/xIpN8d+q58mSsWMT0Du86fSUkVW4tEb3PoHDzXkCXtGb X5vH5mNljMVeO0d/PQNn81g6SYfNvrtUyOH78BRtPPMXy0IqnMQF4hHQ9O+TO6//ggtC dPezn+pVS9mXc+juUI7y2lzZr/beIv5EIhYWd/yjZRFThtBkB67Ie4/YRjOpE+xDLDOD Pq14d3t2LalofScLbWdLOqv6izQYe1pzwNro1HuwgLRHupsUzb8VUpJLlL6pXdERdpbP XECw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724770299; x=1725375099; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6O7GfpMrqTz0lBd8UHCZ0XN6KCNGlbZvNorb5xx4MUM=; b=CtxjRRXOFD7PeFf4YY1Si5xrB9sWQqU7gHhjeN2LXAUKVZII4eftfgsdLeP9S80gM6 G6EApOG65ScejNHtiE419ibSPpuydt/LU383FII9ntqSpzlu8DxZ6UgpIdMs6JqqrndJ RYsBvkwO+B8m8EtfJnDG+mv16S6t18+NORdmU5g2vvZGVw2irxmTq1PTBGyde33+rQUr YmMnTFvBXf33KFtpoSZZ6Ou1yqKErPpbRVl/A5nEq5leXvbsN/pU95fetojvt3/pcduj MHaWHCg8XC3mBCfsRnLiztBWHYED/GjyuvfUCNwL/C9ExjKGohiZKSKE51sZmCZM7l5f UinA== X-Gm-Message-State: AOJu0YxvswMc6ukCixiiMmN4O0rnRvzkd2GQW6W4HsardzL5vc91cUbx LxM8MQq1/r8BsyZHftLV3RO3FJPFwRVyltvVfgvjuDNlhm7EG2qG95uPUE1s7j+kZYivftqNsQs v5+I= X-Google-Smtp-Source: AGHT+IGgaD4kXyKN+cc4pBxFMEALMVM9k502KHZkWzUp+EoGr7FiXWJQecknaSUi5WgoQnItfi7y7w== X-Received: by 2002:a17:907:9496:b0:a7a:ab1a:2d64 with SMTP id a640c23a62f3a-a86a54ddc58mr922313066b.58.1724770298234; Tue, 27 Aug 2024 07:51:38 -0700 (PDT) Received: from localhost.localdomain ([89.47.253.130]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a86e590a6f2sm117727666b.188.2024.08.27.07.51.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 07:51:37 -0700 (PDT) From: James Clark To: linux-arm-kernel@lists.infradead.org, will@kernel.org Cc: peterz@infradead.org, James Clark , Al Grant , Mark Rutland , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH v3] drivers/perf: arm_spe: Use perf_allow_kernel() for permissions Date: Tue, 27 Aug 2024 15:51:12 +0100 Message-Id: <20240827145113.1224604-1-james.clark@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240827_075140_456943_0D91FE2D X-CRM114-Status: GOOD ( 17.94 ) 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 Use perf_allow_kernel() for 'pa_enable' (physical addresses), 'pct_enable' (physical timestamps) and context IDs. This means that perf_event_paranoid is now taken into account and LSM hooks can be used, which is more consistent with other perf_event_open calls. For example PERF_SAMPLE_PHYS_ADDR uses perf_allow_kernel() rather than just perfmon_capable(). This also indirectly fixes the following error message which is misleading because perf_event_paranoid is not taken into account by perfmon_capable(): $ perf record -e arm_spe/pa_enable/ Error: Access to performance monitoring and observability operations is limited. Consider adjusting /proc/sys/kernel/perf_event_paranoid setting ... Suggested-by: Al Grant Signed-off-by: James Clark --- Changes since v2: * Change commit message to better reflect the actual changes to all the options Changes since v1: * Export perf_allow_kernel() instead of sysctl_perf_event_paranoid drivers/perf/arm_spe_pmu.c | 9 ++++----- include/linux/perf_event.h | 8 +------- kernel/events/core.c | 9 +++++++++ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/drivers/perf/arm_spe_pmu.c b/drivers/perf/arm_spe_pmu.c index 9100d82bfabc..3569050f9cf3 100644 --- a/drivers/perf/arm_spe_pmu.c +++ b/drivers/perf/arm_spe_pmu.c @@ -41,7 +41,7 @@ /* * Cache if the event is allowed to trace Context information. - * This allows us to perform the check, i.e, perfmon_capable(), + * This allows us to perform the check, i.e, perf_allow_kernel(), * in the context of the event owner, once, during the event_init(). */ #define SPE_PMU_HW_FLAGS_CX 0x00001 @@ -50,7 +50,7 @@ static_assert((PERF_EVENT_FLAG_ARCH & SPE_PMU_HW_FLAGS_CX) == SPE_PMU_HW_FLAGS_C static void set_spe_event_has_cx(struct perf_event *event) { - if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR) && perfmon_capable()) + if (IS_ENABLED(CONFIG_PID_IN_CONTEXTIDR) && !perf_allow_kernel(&event->attr)) event->hw.flags |= SPE_PMU_HW_FLAGS_CX; } @@ -745,9 +745,8 @@ static int arm_spe_pmu_event_init(struct perf_event *event) set_spe_event_has_cx(event); reg = arm_spe_event_to_pmscr(event); - if (!perfmon_capable() && - (reg & (PMSCR_EL1_PA | PMSCR_EL1_PCT))) - return -EACCES; + if (reg & (PMSCR_EL1_PA | PMSCR_EL1_PCT)) + return perf_allow_kernel(&event->attr); return 0; } diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 1a8942277dda..e336306b8c08 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1602,13 +1602,7 @@ static inline int perf_is_paranoid(void) return sysctl_perf_event_paranoid > -1; } -static inline int perf_allow_kernel(struct perf_event_attr *attr) -{ - if (sysctl_perf_event_paranoid > 1 && !perfmon_capable()) - return -EACCES; - - return security_perf_event_open(attr, PERF_SECURITY_KERNEL); -} +int perf_allow_kernel(struct perf_event_attr *attr); static inline int perf_allow_cpu(struct perf_event_attr *attr) { diff --git a/kernel/events/core.c b/kernel/events/core.c index aa3450bdc227..ae7d63c0c593 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -13351,6 +13351,15 @@ const struct perf_event_attr *perf_event_attrs(struct perf_event *event) return &event->attr; } +int perf_allow_kernel(struct perf_event_attr *attr) +{ + if (sysctl_perf_event_paranoid > 1 && !perfmon_capable()) + return -EACCES; + + return security_perf_event_open(attr, PERF_SECURITY_KERNEL); +} +EXPORT_SYMBOL_GPL(perf_allow_kernel); + /* * Inherit an event from parent task to child task. *