Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp5114578ybb; Tue, 24 Mar 2020 11:12:44 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvGQJU9vA9FzPJSWpbn7aYo3yUS4xWBnjtfahUe8CoBSN7V0X9qf4+b5NnMsRPdvjQ4yBhg X-Received: by 2002:a9d:3f49:: with SMTP id m67mr5432642otc.315.1585073564613; Tue, 24 Mar 2020 11:12:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585073564; cv=none; d=google.com; s=arc-20160816; b=U2Lvi9quRnTbyAyS9A/HrlF+0jIgXs0xOHkoNbQ/KvTizHsg+RRjVJjHbNkF914eew Xx/bIRJkbrjdr3RA73mve5gxlocp8tBRF++VNYfsDn25c8T+TtIDbH4IsQ30YyVTjBzk IRVejEEOcE/iHzeq5ovNBjzIsuwEduKKKRIPlNGo9FxcWUzpInMaXdqCsY0iPlW2QcVX upfLrwmb8qDWaUbcd8CrkoD4qF3F1vhvOnSs8px5vPMbQ0IOT6GxyUhPXodJv/jdn+Oc ZOSeDJj1sh5GmGF73/kFw5APqP0Okv/aIoY9YGNTfjOlwTAHrQAfeHw5xr76zuPYlqlu MRRw== 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=A1s+GlgKu1KUfIn178l7zMrQpVDU7CgjURg8+2s0rx0=; b=S727VZIT4VQlwelc5jbgf7id2kdGapyLrxoUvs1pH05Kzqz2yIHxCmGsO8rcg05gHt q/MNtJ+hvGgviTcs8w/o0NAHiBqlsjQiXBCSpPHZ+ZYMMt4B/UXHDCDw6LTTOcbsPuXF /vTLgJYYytnoSIX5V5eicJ4vwGXmt+pVLMuuwhkg48Gi6Q7gpTcV3GnoM2UeLJZSQ4gs A6zaiQ3Q/XAdxRW+1MFikYRCyzrXOQMrqtDhXmYSI/5zfZjn7UMSrXtpli1mqCnl3+IK +/7bmvtIGQxmSutc6QrvSulnKZP/aQIVGlDwAhFzWWX5fkE6iRY0wgxC51OC11Vq4Dgs efjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SVxEUhNe; 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 d20si9475071oti.311.2020.03.24.11.12.26; Tue, 24 Mar 2020 11:12:44 -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=SVxEUhNe; 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 S1727509AbgCXSL3 (ORCPT + 99 others); Tue, 24 Mar 2020 14:11:29 -0400 Received: from mail-pg1-f202.google.com ([209.85.215.202]:48982 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727257AbgCXSL3 (ORCPT ); Tue, 24 Mar 2020 14:11:29 -0400 Received: by mail-pg1-f202.google.com with SMTP id f14so14225680pgj.15 for ; Tue, 24 Mar 2020 11:11:26 -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=A1s+GlgKu1KUfIn178l7zMrQpVDU7CgjURg8+2s0rx0=; b=SVxEUhNeN50/tJOoGdm1YPrsE8PoywLqO3Yl9kMDqrYxa36vAeD+6wR7wR7J7QOWyE ip2oVxNRex/Mi8jHuiDXdDUp84P8MPhhEQsVhOwxnmAdJzd07bqVaS0roszymXjJBMCS 96A3iWFVeKuVRUAM1V7ui9moS+JO1uSeCz/0OII7G8rGpMK5s7UQ+ebypdumuZMUmMV0 ClvmegIw6r+n3RrV26cxyzWgKhA8NS/49BubQKJuwpt/+Kdz8jkiLZTWmy3l/b7cFON7 y76F4LzO6V9x8KQZxBTWyrLKMWoiEDzloS/oV645tpUpbVheztN+ecPGg6M24lvixRNS S7Yg== 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=A1s+GlgKu1KUfIn178l7zMrQpVDU7CgjURg8+2s0rx0=; b=XrFKw+AhCDm7jCpg5YEd8xlWu5934sItKttbqJvbK1BixS8VNrjIOnyC2bSsqfWI7u 3znq9MGrOs1elNJVfh6aCuwN29XV1XjuQfEcmQGHMj189Qg/uPzR6LJwtLNnN+HkVpHQ n6aAtr/FzcGTaa6ia4aGfxswfMXErGvz19w1GF59cLx0jKDTQvv4etjzvkXHOtpYCqML vPGjbUwMX4KhiI0l7YbWTomkqXEOQx4kdUPmMUtCmdtpk28Kz+IoruKZZl1uNrXFXy6j TiGtNb+PqqnhVbNX2M/w9MVTYPMpYrB9xkb+Na7RuYD5QV0tx2hpw0fDhD91/TgQzbFd 9lQw== X-Gm-Message-State: ANhLgQ35QTqqI9tzabvygwscGzruXUduhMx0HN8WHn/u3maZC7G067ev U7wjYQ7yVqSWe4c1UAoQA8q5TdnTCtfJLKhJjxuEwB8OfQz+PxRMfPZq9TC1skP6/ZVKSQrMo1+ hNwjFdNwjdgk94qxeqMPOtzmLoNUKjX2FV+iXHBdofeP+tOKrpnRnZyaHKBGpMFiLSmd8XA+U X-Received: by 2002:a63:e70d:: with SMTP id b13mr28393578pgi.8.1585073486180; Tue, 24 Mar 2020 11:11:26 -0700 (PDT) Date: Tue, 24 Mar 2020 11:10:20 -0700 Message-Id: <20200324181020.229914-1-eranian@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.1.696.g5e7596f4ac-goog Subject: [PATCH] perf/tools: fix perf_evsel__fallback() for paranoid=2 and -b From: Stephane Eranian To: linux-kernel@vger.kernel.org Cc: peterz@infradead.org, mingo@elte.hu, acme@redhat.com, jolsa@redhat.com, irogers@google.com 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 When perf_event_paranoid=2, regular users are prevented from sampling at the kernel level. However, it the user passes an event without a privilege level, the tool will force :u when it detects paranoid>1. This works well, except when branch sampling is requested. It has a more stringent requirement especially with exclude_hv. $ perf record ls [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.001 MB /tmp/perf.data ] But: $ perf record -b ls 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). The current value is 2: -1: Allow use of (almost) all events by all users >= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK >= 1: Disallow CPU event access by users without CAP_SYS_ADMIN >= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN To make this setting permanent, edit /etc/sysctl.conf too, e.g.: kernel.perf_event_paranoid = -1 The problem is that in the fallback cod only exclude_kernel is checked and if set, then exclude_hv is not forced to 1. When branch sampling is enabled exclude_hv must be set. This patch fixes the bug in the fallback code by considering the value of exclude_hv and not just exclude_kernel. We prefer this approach to give a chance to exclude_hv=0. Signed-off-by: Stephane Eranian --- tools/perf/util/evsel.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 816d930d774e7..db0e6112992e5 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2424,7 +2424,8 @@ bool perf_evsel__fallback(struct evsel *evsel, int err, zfree(&evsel->name); return true; - } else if (err == EACCES && !evsel->core.attr.exclude_kernel && + } else if (err == EACCES && + (!evsel->core.attr.exclude_kernel || !evsel->core.attr.exclude_hv) && (paranoid = perf_event_paranoid()) > 1) { const char *name = perf_evsel__name(evsel); char *new_name; -- 2.25.1.696.g5e7596f4ac-goog