From nobody Mon Feb 9 16:52:31 2026 Received: from mail-oa1-f74.google.com (mail-oa1-f74.google.com [209.85.160.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28912233136 for ; Fri, 25 Apr 2025 21:40:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745617239; cv=none; b=Y3iOHnkjcyq2Hr0cUp7tvWzgrthuf06XyIom6u2S3JdQzeC/xieEJSXBVpexuArRyKah/ZgyM/6ER0uX3387p2RxvuQrMyKrnzXgxEmf1m57ODcNrjYx+gRgPdBbjdpRYG/RnwzuKRaGUGcPDw3lKFbxf0wHyZoHjAF9E91YS4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745617239; c=relaxed/simple; bh=Mowkv5qqJ/F5GpiVJf300uqyE570+zdeev7heuBS7dE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=cyFIVfJrjHZel5g17BZfhdT0mSQLXIqkgKc4KZK7dUxlP4yMvrXYcZprS22fhLb4zHoiEgU2eWGSl2CKmOnoGSGlrkxznPLbJR6nLAiTNPBLQqIap++unwmP4VG82XdaZXlVUvvqR47UqgS/aYC+XL9pI7gau/wNlns7DclCzWc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=F0OtR/n/; arc=none smtp.client-ip=209.85.160.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F0OtR/n/" Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-2c72e6e51ceso836289fac.3 for ; Fri, 25 Apr 2025 14:40:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1745617236; x=1746222036; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=MCiBNIgyIq3tfdIckzwQPIYGTlpSc1MEqAHEJmYBsOU=; b=F0OtR/n/fQy9LRG4SVceUixQ2QlE6KkYITdo+2Pt7T6YykS5pU0Moo8Ahn1W953rHH qq7qQL5wP0Ade5NzXr9nqEG5JEkaP1KnIP277FFN7y5gOqmy59OFzAZAqd1MNcqqAmQQ YeFpUPWNcEiSnotllGFyOHZaaE+p+yDzjzxxauv/2TSkaAcwYeJap7oT3uhjdV1E80gH vLHjYK4JGUGbob3Lqj0M1GIICHKcw23LjQtMU1KIfbc/XgaqQ6xsbM6tEd/uQH9n8WDM C2lIBYTrFd5s5j9uDgBqz8KjvxsJEijmbSVfFiKa2aFEgkOCX4C4/dHBEaryP7SLqaT1 Gwrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745617236; x=1746222036; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MCiBNIgyIq3tfdIckzwQPIYGTlpSc1MEqAHEJmYBsOU=; b=IKo17OO+NWxcVTWmb/7NKFv+4ZK0lPRmkLaSGFOO1oxOhk1jlOuHXmJr26TBDMlW1t xZPkKr5Ws2m8Qr3MYoUPeF33KdK3hN5jr/1uG0Xrz599rRqOxOBG69Iw023y05TqbAUo xulkPLq+4NqQK1+KC1MpANhmXl6tJ+T8AZQE1YralYjre0kDEd17jEVw25vnZgEsgBIW bIw5qAr4EOKcKgOHJcDI9RJo5mz0oYORxn+LBhgFRieeObb/e8WOkwOlMfLB0gtS8CrY H8QPNoNC0ZjL4b9meTzBg9xtlNKvokBv/JDxz/4GGQTxOsYvxhotlyQXJ/lpBy3wbiVM aPEw== X-Forwarded-Encrypted: i=1; AJvYcCXAwGKs/jgj89/g0vzziSeRDGJabGY0xT1248jEDaqquq/cGjiZo5IEpZt7Ljny1T7Bg9IFILmOZYe/SXI=@vger.kernel.org X-Gm-Message-State: AOJu0YyFT7UyQMyu/nXecYFRHUXZW0ISTs0rxl01v2Z5lDchR6hTHN2J 1cXKzRyi0FifYD9Xy3OtQGTNzX0PwHwbJL15zqwzydzOKKr98CPw9+nbP/oqf409HDD+irGckgu WeFUavw== X-Google-Smtp-Source: AGHT+IEa2FLM10jcrrl+uJoBC09nP4IVEXI2lkCp+TFUaATjK6bauLtKrDXR6U2I/yDoVJ3h22f3O4wgOzrr X-Received: from oabwc14.prod.google.com ([2002:a05:6871:a50e:b0:2bc:6267:d082]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:a1a7:b0:2d0:3078:e72f with SMTP id 586e51a60fabf-2d99dc404cdmr2537739fac.26.1745617236166; Fri, 25 Apr 2025 14:40:36 -0700 (PDT) Date: Fri, 25 Apr 2025 14:40:05 -0700 In-Reply-To: <20250425214008.176100-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250425214008.176100-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.850.g28803427d3-goog Message-ID: <20250425214008.176100-8-irogers@google.com> Subject: [PATCH v3 07/10] perf trace: Switch user option to use BPF filter From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Dapeng Mi , Thomas Richter , Veronika Molnarova , Hao Ge , Howard Chu , Weilin Wang , Levi Yun , "Dr. David Alan Gilbert" , Dominique Martinet , Xu Yang , Tengda Wu , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The existing --uid option finds user processes by scanning /proc. Scanning /proc is inherently racy due to processes being in /proc but then exiting before perf does the perf_event_open. When the perf_event_open fails perf will terminate reporting errors which is a disappointing user experience. Scanning /proc when perf starts also cannot inform perf of new user processes starting. The ability to filter perf events with BPF isn't new, and has been in the perf tool for 10 years: https://lore.kernel.org/all/1444826502-49291-8-git-send-email-wangnan0@huaw= ei.com/ An ability to do filtering on the command line with a BPF program that's part of perf was added 2 years ago: https://lore.kernel.org/all/20230314234237.3008956-1-namhyung@kernel.org/ This was then extended to support uids as a way of filtering: https://lore.kernel.org/all/20240524205227.244375-1-irogers@google.com/ This change switches the perf trace --uid option to use the BPF filter code to avoid the inherent race and existing failures. To ensure all processes are considered by the filter, the change forces system-wide mode. Using BPF has permission issues in loading the BPF program not present in scanning /proc. As the scanning approach would miss new programs and fail due to the race, this is considered preferable. The change also avoids opening a perf event per PID, which is less overhead in the kernel. Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 6ac51925ea42..1f7d2b3d8b3d 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -240,6 +240,7 @@ struct trace { struct ordered_events data; u64 last; } oe; + const char *uid_str; }; =20 static void trace__load_vmlinux_btf(struct trace *trace __maybe_unused) @@ -4401,8 +4402,8 @@ static int trace__run(struct trace *trace, int argc, = const char **argv) evlist__add(evlist, pgfault_min); } =20 - /* Enable ignoring missing threads when -u/-p option is defined. */ - trace->opts.ignore_missing_thread =3D trace->opts.target.uid !=3D UINT_MA= X || trace->opts.target.pid; + /* Enable ignoring missing threads when -p option is defined. */ + trace->opts.ignore_missing_thread =3D trace->opts.target.pid; =20 if (trace->sched && evlist__add_newtp(evlist, "sched", "sched_stat_runtime", trace__sched= _stat_runtime)) @@ -5420,8 +5421,7 @@ int cmd_trace(int argc, const char **argv) "child tasks do not inherit counters"), OPT_CALLBACK('m', "mmap-pages", &trace.opts.mmap_pages, "pages", "number of mmap data pages", evlist__parse_mmap_pages), - OPT_STRING('u', "uid", &trace.opts.target.uid_str, "user", - "user to profile"), + OPT_STRING('u', "uid", &trace.uid_str, "user", "user to profile"), OPT_CALLBACK(0, "duration", &trace, "float", "show only events with duration > N.M ms", trace__set_duration), @@ -5762,11 +5762,19 @@ int cmd_trace(int argc, const char **argv) goto out_close; } =20 - err =3D target__parse_uid(&trace.opts.target); - if (err) { - target__strerror(&trace.opts.target, err, bf, sizeof(bf)); - fprintf(trace.output, "%s", bf); - goto out_close; + if (trace.uid_str) { + uid_t uid =3D parse_uid(trace.uid_str); + + if (uid =3D=3D UINT_MAX) { + ui__error("Invalid User: %s", trace.uid_str); + err =3D -EINVAL; + goto out_close; + } + err =3D parse_uid_filter(trace.evlist, uid); + if (err) + goto out_close; + + trace.opts.target.system_wide =3D true; } =20 if (!argc && target__none(&trace.opts.target)) --=20 2.49.0.850.g28803427d3-goog