Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp312279ybe; Tue, 3 Sep 2019 23:27:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqyehU1Dwd6JfelcYM2FxSzrbMkuGGTM7BRQG26d9rJj6vKoNMONlWKWOqp5w1HkcirmZemR X-Received: by 2002:a62:640e:: with SMTP id y14mr43517698pfb.222.1567578449754; Tue, 03 Sep 2019 23:27:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567578449; cv=none; d=google.com; s=arc-20160816; b=jkTf6defK/BThafRjr2T5M8X0yp+cUWKIf+fId//olu76grG08WL+vMlcHzvtKe/NL dz0dZeG5gFP97AiNMh6xV7tg6oRGLG2VGKHfH8FIRopps+mnykh+lv4LAU1T5F1ntBSn ZnCub9fhC6k3fFnlCD7VSXYcybizmulNOn7BwDzMp4Ifkb3izm3j/Ss2GFiUB8ujZywy LZNURY2g+gCi4uLrpfIYuuFmz8cS3+Uxi4Es6XiCOpl1SjPM8Eo6B/1PtOsZAbPefrM0 dekwhIUM9WTJZyvWaVuzYzPIi2MaNfEjyeqxuMYSnFffb16wvbkurTChG9HBHk3UmMLO 4lQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=Tbk9iHA1BcIv4CnYIU8mmP8LfRBc9qlMqqjj2iAmjRM=; b=QUymtAo3ZThPbSY4rMyO/1V0aEtrvg2W1Y3WXPMLKsiQY9Zs6dw4IVEXmtfRCm8jVH GJ8+BirrNiuw6jQuWhFwXKqOtMUHI9zTmtsTv1+R0Ofg9pbuvNtZFQUmfza9yJh3SaMT zLFe4yyUP9QR+OnR6PUNH3M2f0KGFgy/09BqXBOxxDOv+PJPCHB8f7yDsZ8uUuhNXYTH diera0GJuNIu3Yu+DqAS8d1caDMG69XALGoVWPDufT+xIyXsIBE0jWR+Yyxp4Yalg8ZO HTrgtP9v6Z/RfmfUj4cTvR0r3ysA7uowNgYH8U92n2I6YZWaS6XT3MIPxJkQtwmTU/oe tMHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=rVpRcpMy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t132si12402297pgc.288.2019.09.03.23.27.13; Tue, 03 Sep 2019 23:27:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=rVpRcpMy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728108AbfIDG0O (ORCPT + 99 others); Wed, 4 Sep 2019 02:26:14 -0400 Received: from mail-pg1-f202.google.com ([209.85.215.202]:33045 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725877AbfIDG0N (ORCPT ); Wed, 4 Sep 2019 02:26:13 -0400 Received: by mail-pg1-f202.google.com with SMTP id a21so12550433pgv.0 for ; Tue, 03 Sep 2019 23:26:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=Tbk9iHA1BcIv4CnYIU8mmP8LfRBc9qlMqqjj2iAmjRM=; b=rVpRcpMywls/Rm6WnSUx4nLg8V3ztZoD8XZiwDNwzQ+pBlG3Dt8lGxRb+HXjSp4vB1 e+huemO29QOZ84dq1PbhwJGO5BwlRo7cjeZFiwfKGraZF2g+nhjVLCugyeDn8ciR+8s5 80YTsOla4s0Dqn9kPQtwgtzrUL8k5MtZxymPaB73lHHSqZyqTXMcF/kH3gxER/1qvomw ElWP4Iy7No2Ns/DHp2hOCqrg89DG5LISSyqjsuQac/1enLT8yusovFDWPJhFIpT+Ahax A2SmETEYGoJ0inQeSk6rC5HWu8mTf48Wcs0VImtA9WKvUFdViey0xBNoNzP8f8/gb1nX gLWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=Tbk9iHA1BcIv4CnYIU8mmP8LfRBc9qlMqqjj2iAmjRM=; b=a1OPfkFRqugds1gH70BtUL4wpDIvRmIknBhw8WYHohajLT2vsseTv6ymfpkD4yV8qF UUYlK9f6i73RFg1QWhrveWBPeRD0I0ox6N04TYPoAvLOdPyRgZjTlaMd/f0x7jRJdyID IV0ZoZ45z6KXwRlAMXMlQoMNUWz25Tq03GvWWPdLwo9SBwurSrikR/9gCni1Hz31J7IA zTvrQb7c1UzGKIQ7qLu+udU9pjA4TvRnaac0hYj1QYnV0wUVBFEN418fnJ+qiRyD/bqg bN6sbSVr1WVQslz5EmQb/VnOz9AXBgdI6jm1j6gyAap02X9fw5AJCIiOhDaDRp026RFS 9Y/Q== X-Gm-Message-State: APjAAAW9BJH+qgrHZ3PF9RMU6P00imYaJ5lVyifJlg9XEoo+up2sZSS+ Kk1fWhJ8Tt1XbTGUU84J9Mu1ttc2hpea4maCEQWCyvEOdTwUC6dX9AzsIh0mNqIfXlrk4Sit+h7 PU2TLurtX9wXcPuHlZjV5Ollsg5okP0xg4uaclqmnL31+IH/nyNZ84n5N5J+2Rlpzv9v8w2Fl X-Received: by 2002:a63:711c:: with SMTP id m28mr31760438pgc.396.1567578372659; Tue, 03 Sep 2019 23:26:12 -0700 (PDT) Date: Tue, 3 Sep 2019 23:26:03 -0700 Message-Id: <20190904062603.90165-1-eranian@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.23.0.187.g17f5b7556c-goog Subject: [PATCH] perf record: fix priv level with branch sampling for paranoid=2 From: Stephane Eranian To: linux-kernel@vger.kernel.org Cc: peterz@infradead.org, mingo@elte.hu, acme@redhat.com, jolsa@redhat.com, namhyung@kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Now that the default perf_events paranoid level is set to 2, a regular user cannot monitor kernel level activity anymore. As such, with the following cmdline: $ perf record -e cycles date The perf tool first tries cycles:uk but then falls back to cycles:u as can be seen in the perf report --header-only output: cmdline : /export/hda3/tmp/perf.tip record -e cycles ls event : name = cycles:u, , id = { 436186, ... } This is okay as long as there is way to learn the priv level was changed internally by the tool. But consider a similar example: $ perf record -b -e cycles date 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_SYS_ADMIN). ... Why is that treated differently given that the branch sampling inherits the priv level of the first event in this case, i.e., cycles:u? It turns out that the branch sampling code is more picky and also checks exclude_hv. In the fallback path, perf record is setting exclude_kernel = 1, but it does not change exclude_hv. This does not seem to match the restriction imposed by paranoid = 2. This patch fixes the problem by forcing exclude_hv = 1 in the fallback for paranoid=2. With this in place: $ perf record -b -e cycles date cmdline : /export/hda3/tmp/perf.tip record -b -e cycles ls event : name = cycles:u, , id = { 436847, ... } And the command succeeds as expected. Signed-off-by: Stephane Eranian --- tools/perf/util/evsel.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 85825384f9e8..3cbe06fdf7f7 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2811,9 +2811,11 @@ bool perf_evsel__fallback(struct evsel *evsel, int err, if (evsel->name) free(evsel->name); evsel->name = new_name; - scnprintf(msg, msgsize, -"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid); + scnprintf(msg, msgsize, "kernel.perf_event_paranoid=%d, trying " + "to fall back to excluding kernel and hypervisor " + " samples", paranoid); evsel->core.attr.exclude_kernel = 1; + evsel->core.attr.exclude_hv = 1; return true; } -- 2.23.0.187.g17f5b7556c-goog