From patchwork Wed Aug 7 15:51:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Clark X-Patchwork-Id: 13756475 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 B96C9C3DA7F for ; Wed, 7 Aug 2024 15:52:57 +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=oRFdCUk81EgAuZOPUgff1QAMGIx+EiDXi2BrG6XIJyg=; b=dSwRo8x1QU4WEQnXnTpvvFkKmY DqcZdURh4S5D1DIe8V9v+Yhtp/0C+9Za+iTzx6UaZtNI0wwKWh8GwAz5fmiyAVYyjJhMMSN3c7wME KoW4a8Ens4x8mZiV1GXYZnQRn7lPkudVjLu9W51ewpz6Q5dCrJZRiGXuZKoz8bTlbDBTzFf04QsUC FH74RRNkjh4Dy5obveRkggW0E9OtgPBJ4Nwxf3as3/d/wRnfBGYH7xQiCL1xPVQw1HdwPyUFBel+o KzGvyB8+Vc4XnNggFqQ/FZIJvFcXTIBeaXaQeoTOMEEeP4As1Y68Hrw5iGh/yxDFwQdr3eP9jaTTb OxJ21+rA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbiy0-00000005aDI-3cgF; Wed, 07 Aug 2024 15:52:48 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbixR-00000005a5q-1Ws9 for linux-arm-kernel@lists.infradead.org; Wed, 07 Aug 2024 15:52:15 +0000 Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-367940c57ddso1087131f8f.3 for ; Wed, 07 Aug 2024 08:52:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1723045931; x=1723650731; 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=oRFdCUk81EgAuZOPUgff1QAMGIx+EiDXi2BrG6XIJyg=; b=BXVJ73unDBiVr66uTKsu/UiiS0bqQHS82Esp9N+VzOyd0egw8dv98pKHUpd48loJ2s 3tchropbtTCrTC1bvOLc74FE+N6YIecMUyfCOSEBnYLaHLQ2Y3dIf3OpQqu4Qj46vg01 O2V0uWmzSaI2tkcishwYU3t2Guh2VrLweQ6iXYi4fA1p76neFhJ2Bby9QWlGJMobimLU PtyN3RrAwyn2hXgCrfmRAby4EpT/veBJwGM0TrqIPBIbhKKgFsluHT8O+aG4Zl8ZGsPN YUZ62UuE6iLrXYbCoce/erAqdm340Tazii5m1ejTeSNUTKt9HKe+SOqDlJ3T7CChDUYl WFIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723045931; x=1723650731; 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=oRFdCUk81EgAuZOPUgff1QAMGIx+EiDXi2BrG6XIJyg=; b=LeYOopj/x/g6Kjq4eb3TuGcNILqPXyl1Mwc/pHpO0p7PsOmF7lvdPbUu4+TgZTdFjo czq+fkZ/t4bu0p3K9SQZ5r5+fgvoEsA3rYYboBwJCK6sAOcat6/88GDcPAsKIVFmAg/q eSqd5WbWZOQe6R6quTj6qFzyU5cKQzfP2NgoG4MAtq4pvU++gBeK92WaVfPXuokVxncJ 7W8sOl/ZyY8tOfF2IqbCwTRWHjkmrxg3SpsRMa6aPdn5M8LmFuUc+hmRDFuZzZUdkAOa jBUvY9cLqtP9d44v8g6p6QRxP9MAWqsOhzZWckU1RXP8Ol6SL92el8V4/VATwIS5qtcy H0Zw== X-Gm-Message-State: AOJu0YxJjFXjzVzKGkhfm9HEECUAY3Ctb81l/HrbXS5FecJHHuTrydjV RL/NGEpnA2SzptxK3XypFxLgUTin/KPGOSjkKUQ/Z9+z0LOjEwpjzeGY2EU38PVtUCFAuTkco5z P1c0= X-Google-Smtp-Source: AGHT+IEEeoPN6j8nzeZeQO+PeuL8PISW2uSCLYbhFOCXh07DB880ZpSmwybG7OY/YQmH+1Lm/6eO9A== X-Received: by 2002:a5d:4e90:0:b0:368:591:9bc1 with SMTP id ffacd0b85a97d-36bbc1bd311mr11569869f8f.46.1723045930947; Wed, 07 Aug 2024 08:52:10 -0700 (PDT) Received: from localhost.localdomain ([89.47.253.130]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36bbd0261c2sm16428839f8f.57.2024.08.07.08.52.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 08:52:10 -0700 (PDT) From: James Clark To: linux-arm-kernel@lists.infradead.org, peterz@infradead.org Cc: James Clark , Al Grant , Will Deacon , 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 v2] drivers/perf: arm_spe: Use perf_allow_kernel() for permissions Date: Wed, 7 Aug 2024 16:51:53 +0100 Message-Id: <20240807155153.2714025-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-20240807_085213_435831_C26B55F6 X-CRM114-Status: GOOD ( 17.18 ) 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 For other PMUs, PERF_SAMPLE_PHYS_ADDR requires perf_allow_kernel() rather than just perfmon_capable(). Because PMSCR_EL1_PA is another form of physical address, make it consistent and use perf_allow_kernel() for SPE as well. PMSCR_EL1_PCT and PMSCR_EL1_CX also get the same change. This improves consistency and 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 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. *