Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp875587pxk; Thu, 17 Sep 2020 20:01:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgu3ba7kYalg7j4C+yhccPTg6ovJyKemkhIWCoKXA9PV3VHszYsGI4PScjRiV56INUeoH3 X-Received: by 2002:a50:fd19:: with SMTP id i25mr36645776eds.142.1600398087946; Thu, 17 Sep 2020 20:01:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398087; cv=none; d=google.com; s=arc-20160816; b=YeR2Q21OLhE1tu9an8MaqKO3VLwUvX+VD5Flc/x5s07RlGK7yBk0YiHltceXdqHmXu ZjT13n/nG7egndtAdsUu5yAOSPZC9wsjyuwEmTs1HI8+FkWOoLMtblzJIFokJKVMkEqf y2REqGXX1Gaswg4m8f0xvqfOEZz3Hg7Nb0KlM/iLiIfEF8uwfI7zEV/ZWDLgABmwmvQz ogkH05jN8dwj2OG4xT+iaoX7/zEKdUCkOzZ4KCIVlEpoqbTCdCSl0f3z97rjkUGW/ASU Wm9T22o7OkTeueol5TjrxvCUxOeWi49T2QlvfdDXfLc6EYPisUD3g/yaExh4bcmkIks4 CpBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SjEVReh6T9D3YJLINaFT75CrpNAWVZyHJm5g9cU35bM=; b=WCgmOcjbGBBG22TxbJe/QxsqJTZ7GZdWlxulr7QiGGykKQMgxHyO2TxZT3n23jNazI 1T22lTXUmJ297k9PwKbdLSY0i3xn8X06vdUkni7UEEZxcZ0qyAynbhBys96v/ljBH5ed omJRmQFdAAib3CB52R8Hvuc1IFg7WEXQp4VHOnFezUHRjusN40H/V9Df74SrB0ZvoAEt QkHtvXO0OqkyOi08/LiLftRWnj1S9dtobv75/kpFFKLIJlrsvG0NxPzYAIo9EYM97TGZ wtGUI+iQlMGklWWRAGYac21Nk+m50utSoDjyMI9v0GP2lrwHARuNkjcTTkYZfGDPNOqa l+nQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mCv2uBGO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d20si1120917ejb.704.2020.09.17.20.01.04; Thu, 17 Sep 2020 20:01:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mCv2uBGO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730295AbgIRC6z (ORCPT + 99 others); Thu, 17 Sep 2020 22:58:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:55092 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727699AbgIRCGE (ORCPT ); Thu, 17 Sep 2020 22:06:04 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5FA342389E; Fri, 18 Sep 2020 02:06:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394763; bh=dre4NXCynnFaoBq1haLtq3U9LcZuowiLHESGSCjxQXk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mCv2uBGODrMsOrcWg36Ea43Dr7j9VDQhScwQcEo/4ysaNN8vs9xUi4hGtUD9clMqf Fz2hlypy4gjIqynRkCb3zxUUrorHtS7oRDQowqKCcPThXiRI/e6EKS8OUNCIkcSb7H XY4Oqg4xz7tOb1qZ1IN5peUeMDWkIk5FzhIBli2k= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Stephane Eranian , Ian Rogers , Jiri Olsa , Arnaldo Carvalho de Melo , Alexander Shishkin , Mark Rutland , Namhyung Kim , Peter Zijlstra , Sasha Levin Subject: [PATCH AUTOSEL 5.4 239/330] perf stat: Force error in fallback on :k events Date: Thu, 17 Sep 2020 21:59:39 -0400 Message-Id: <20200918020110.2063155-239-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stephane Eranian [ Upstream commit bec49a9e05db3dbdca696fa07c62c52638fb6371 ] When it is not possible for a non-privilege perf command to monitor at the kernel level (:k), the fallback code forces a :u. That works if the event was previously monitoring both levels. But if the event was already constrained to kernel only, then it does not make sense to restrict it to user only. Given the code works by exclusion, a kernel only event would have: attr->exclude_user = 1 The fallback code would add: attr->exclude_kernel = 1 In the end the end would not monitor in either the user level or kernel level. In other words, it would count nothing. An event programmed to monitor kernel only cannot be switched to user only without seriously warning the user. This patch forces an error in this case to make it clear the request cannot really be satisfied. Behavior with paranoid 1: $ sudo bash -c "echo 1 > /proc/sys/kernel/perf_event_paranoid" $ perf stat -e cycles:k sleep 1 Performance counter stats for 'sleep 1': 1,520,413 cycles:k 1.002361664 seconds time elapsed 0.002480000 seconds user 0.000000000 seconds sys Old behavior with paranoid 2: $ sudo bash -c "echo 2 > /proc/sys/kernel/perf_event_paranoid" $ perf stat -e cycles:k sleep 1 Performance counter stats for 'sleep 1': 0 cycles:ku 1.002358127 seconds time elapsed 0.002384000 seconds user 0.000000000 seconds sys New behavior with paranoid 2: $ sudo bash -c "echo 2 > /proc/sys/kernel/perf_event_paranoid" $ perf stat -e cycles:k sleep 1 Error: You may not have permission to collect stats. Consider tweaking /proc/sys/kernel/perf_event_paranoid, which controls use of the performance events system by unprivileged users (without CAP_PERFMON or CAP_SYS_ADMIN). The current value is 2: -1: Allow use of (almost) all events by all users Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK >= 0: Disallow ftrace function tracepoint by users without CAP_PERFMON or CAP_SYS_ADMIN Disallow raw tracepoint access by users without CAP_SYS_PERFMON or CAP_SYS_ADMIN >= 1: Disallow CPU event access by users without CAP_PERFMON or CAP_SYS_ADMIN >= 2: Disallow kernel profiling by users without CAP_PERFMON or CAP_SYS_ADMIN To make this setting permanent, edit /etc/sysctl.conf too, e.g.: kernel.perf_event_paranoid = -1 v2 of this patch addresses the review feedback from jolsa@redhat.com. Signed-off-by: Stephane Eranian Reviewed-by: Ian Rogers Acked-by: Jiri Olsa Tested-by: Arnaldo Carvalho de Melo Cc: Alexander Shishkin Cc: Jiri Olsa Cc: Mark Rutland Cc: Namhyung Kim Cc: Peter Zijlstra Link: http://lore.kernel.org/lkml/20200414161550.225588-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/evsel.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index dfc982baecab4..12b1755b136d3 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2358,6 +2358,10 @@ bool perf_evsel__fallback(struct evsel *evsel, int err, char *new_name; const char *sep = ":"; + /* If event has exclude user then don't exclude kernel. */ + if (evsel->core.attr.exclude_user) + return false; + /* Is there already the separator in the name. */ if (strchr(name, '/') || strchr(name, ':')) -- 2.25.1