Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp3541949ybz; Mon, 20 Apr 2020 05:00:48 -0700 (PDT) X-Google-Smtp-Source: APiQypKWE8ygDBWj1iCHuXv533Ucl27sx6aArJwrVspIIz6/3zoQj+7SLqDggbGGm6c4+PZo2cIk X-Received: by 2002:aa7:dac3:: with SMTP id x3mr10271624eds.379.1587384047713; Mon, 20 Apr 2020 05:00:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587384047; cv=none; d=google.com; s=arc-20160816; b=0ipZrusz8JEXCGFoQNDFKdIn21pXfVpr0sz7M/QAa8UTmzdzlkojFUjz6y5IT8WbTk v0VyF6RsZwV4DiIYagozO2iOCYqXiVwGz1py9bkt2KfPquZ1AzUE5IUkea43ePUQTJAZ 27yGWrwHFeI7Tpjx7PuFPaGDTEAFk05VXXNAqfsnJVTGCJrYxURS07OeLOwhJ/b6odNz NUFULAn/4hOGu8YYtc5iXcH9iJEE9cTKZFutf/8z1Czbjg/ck/loN84wpzPDL0+rlAuC llwuhgVgVqlJ7+sNoUZ4i6VhZwbzH/bVk9Zl7Qy4uvCu1xqZm25MKc0jejwBBSZshkLL Dm4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SlIOJBiVc/KYglNVxlTYMYcouxI1I9vTvEAOmIC24P8=; b=n9PMQERDJ6nDJYaWbx4IvhW4i0TvhxGplfDx8v3TGmtNlug5Iwbs0CvD2OdgYFX0Wl EKqIC/k079PtGwfxPHxZFlwbdHOFhcr6rbz2+oXmRF4AsXLwOa7gqRMLC6TezP71KlDj W1T1oxvhV3VYijVFur183227v/u6JtYpNJ6F5VZ/MrorElfVQ6Rns/wR9dzWdOcyw6Xx ZEr7snF7k7bRmm59oNcsR+qbBTmKfiMuuoqth1N1x5Ywd4isd2sZOfz3GwIwPQq84r69 fPV5UHkz25POYZWIfZK2XC7MzbR/9gxE+PBCZwpvkmAOrTCQ4eR5Im83tUfJGTKt/5kt I52w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=2WG0qxW3; 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 l13si425467edi.509.2020.04.20.05.00.23; Mon, 20 Apr 2020 05:00:47 -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=2WG0qxW3; 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 S1727076AbgDTL4a (ORCPT + 99 others); Mon, 20 Apr 2020 07:56:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:40860 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727060AbgDTL43 (ORCPT ); Mon, 20 Apr 2020 07:56:29 -0400 Received: from quaco.ghostprotocols.net (unknown [179.97.37.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7936D2223D; Mon, 20 Apr 2020 11:56:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1587383788; bh=lmGTczQYs73nSnR9l2DZ9fp/5CLokuQYfrpr+ojkbZ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2WG0qxW3cuUVh3s0Wyj2B6alDMm+wWZ9o/YoY4dclbwOiirTgrYGc1yU9hjBoxAYC A79TY678sj3YJu/LSHg2yd+L7LMNiEbzihp0ajDHOYGEh18057qw03kGzqO+coirfu RSLBgeqKXdbSUUF+IO9FKBo5ytiTWkL4z1o5FWnA= From: Arnaldo Carvalho de Melo To: Ingo Molnar , Thomas Gleixner Cc: Jiri Olsa , Namhyung Kim , Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Stephane Eranian , Ian Rogers , Jiri Olsa , Arnaldo Carvalho de Melo , Alexander Shishkin , Mark Rutland , Peter Zijlstra Subject: [PATCH 42/60] perf stat: Force error in fallback on :k events Date: Mon, 20 Apr 2020 08:52:58 -0300 Message-Id: <20200420115316.18781-43-acme@kernel.org> X-Mailer: git-send-email 2.21.1 In-Reply-To: <20200420115316.18781-1-acme@kernel.org> References: <20200420115316.18781-1-acme@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stephane Eranian 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 --- 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 8300e8c7aea8..6a571d322bb2 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2427,6 +2427,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.21.1