Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp803974lqb; Fri, 24 May 2024 13:56:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWufp9cXAhrofhWBVrnn2BbMTkv7x6Si6cUTcpQH3mQZ6Pe3QDLnRxizDGoVMaYlcfsRI6pe05iM6Je4Sl/HZ0pp2UzZdcbozg3HQbX5A== X-Google-Smtp-Source: AGHT+IEJxFrBDdb7jgP/CKJNEweje0flZV5n2FZdlLOuV2MyarvemKOWzne7908Uo7XPO9yz9uNZ X-Received: by 2002:a17:902:e54d:b0:1e3:cfc5:589e with SMTP id d9443c01a7336-1f4498e1c90mr38174395ad.64.1716584195929; Fri, 24 May 2024 13:56:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716584195; cv=pass; d=google.com; s=arc-20160816; b=hfkzDOiRQLry1bgJg06apDqzfvsl7qDe3jYiaDIvpTAgm32vu4Fqk3ssZ7ZGYwBqfT m9UsA+ZAo/ta/vpVmfLO1KenWDzO/CzIY5QTpWhqpqoGeViBtylCn1ZvtJnqIAo44Bgr On/MZTzxhGIJUgEZMQO4cnoF4i3Ps8ZJrfrF52Ng7n/DUUridccwOpLLKT037XckFetR WLPdhZL2O5quF4zbXaeRRu1sZxC0D+mCt0lZmz7u5+zA3EnTXyVpeFvSA/GViXux5KvE gut/e6K+o+zUw8KVELzJCMKn2aF8fuk5hV7q5ZOygb0KqUI9UwFIAVylZCNTM2E6uFyQ ZaRQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=oawNFUCmaLFxsRTNXlYLk/GWjHdQloVnoR4MnORxCVI=; fh=ogrKi0DhmjihW56SQyQKepX0qklN+QDvh5poQk3Lbuk=; b=V9hIbPOym3Pg5swXVG52ouSJlPRBZKV/RokY3Cib5e41B9z48jgUl/9yC08lgb4ZRr z3erTaxleS1OWsV2Z3lx3i77Ek3AEqCMcQM4S2kJk/v4bKT+2hp3lhxM+XPet7xM+T5v 8MUZ7HFzcO25/6yA3lmiZjqv1iRXzVLeZdTgxsNPNIfQA1RjhTS4c4M6znY1QNQbZjwB me0LvxIuvbJWbMqG6nEP5pWU/r05ofC6m7a5uY5Le69ubeAJEMsjyCFDhe1WQwTiRoTm zqbjRSK+Y2ZaZpSX3q3ocCvHVXYlV/PwbhrkYrtO1UTmcND3+NnMooJgQ5xZpdVezB2Y /7Rg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=S0LgooAL; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-189125-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189125-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d9443c01a7336-1f44c79255esi19205505ad.87.2024.05.24.13.56.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 13:56:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-189125-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=S0LgooAL; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-189125-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-189125-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 8BA5228186B for ; Fri, 24 May 2024 20:56:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E7D0A130491; Fri, 24 May 2024 20:52:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S0LgooAL" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 6C7B48627C for ; Fri, 24 May 2024 20:52:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716583971; cv=none; b=qbeIPh0L3V4s1p5FtVJseI0x2if/ASPFWz4Fqsdgqlbj6RLqXRcpNmX7kLdwgE7dwVY0OH+EGI8GsaDMApPFWygxUNmAIZftj4llaJ/QHVI7CeobuagL9PdfrdpxNpthVLnqGmMkr66uHH5Hjo1YkSdifJ1Zv/4W7FTbUnybMYg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716583971; c=relaxed/simple; bh=3L8J4mFnFcR04GC0s7Reyzu6OTdUhfWKdzrb2tiRPSg=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=umFVMarSM6jrLFGcYMcYvVZMe0KYsqdlAHlUFBtIVL+aEOi715ULucCzkHb2LuIv7IbZmUnV4u4PZpMW9ADeL8CKTGsk4chQetYrGKol16BH2Adt/Q7XXV14BAhGLshumqNjRYwwRVTVzb0Wdn/y9weSQfzZlz9KjcX3ENmNw9Q= 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=S0LgooAL; arc=none smtp.client-ip=209.85.128.201 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 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62a0841402aso10961247b3.2 for ; Fri, 24 May 2024 13:52:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1716583969; x=1717188769; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=oawNFUCmaLFxsRTNXlYLk/GWjHdQloVnoR4MnORxCVI=; b=S0LgooALtT5RGWwR7kLw5Z6kKhN7I3O4pUJjcqpAC32gJseFHVkOmkxEdDni6Fy+0U oYWM8BExpJhPOayn4AlbqSV0eFZlpMK6bhO+Owc00XLiYUsZI8aTFkL8Wsnbf69UhaOD 5+WL4YNJsxb/rDw+kWF+wgSwEJrgyLrACg0LwaDTS751fdcbz/OJtnHQeI7Y5ibv1e/1 f5YWk5PUKvstW6TLcReG5julW1a9OQr9bUUMQG6seWj+bh0VxG/I89S3D6i/mWn2ETAt PI/JOhAqySIMh2IZsT0aJ4JWziRw8xteeEx9/JIPourJhO4F2bRaBrHurFy/ABX5f5mo unQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716583969; x=1717188769; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oawNFUCmaLFxsRTNXlYLk/GWjHdQloVnoR4MnORxCVI=; b=vEB2o5aKipvG6ogc2deiLGA29DR7F+DuxJjdww+/n4Rg5EhwDRULTsrorAN0g17p3A YpO2zkB6aaMcjXVWi2wg5qMv1LkiF5jhLJNg/wBd46np3jSG8Io+PyJI2nOf/vYHCVhk oYwsSxFnetCCEei1E89ESXvcOkDV4o/vHHVqB0465iRLAEoA8egyWOY3to88dlb11fca UzNB4IjRIp4wgCbmv2iD4tWyD0o3K2qJxUHdnxA8cFTgf+EMUfV5+4LUY5Gl6JNxEu5e aW1O5bsZX+4wsUTB5laGUZhhKro6H3EiKKFwQ9YhLzMGRwX3POgVw5RafTjlM+XF+K+v BsTg== X-Forwarded-Encrypted: i=1; AJvYcCUfjwayGS2lDV13QPUG2zZeYcq3K+8nPC7g16vSkQGAjtc38GVgPTCJ6jUBXi2scwBcUcU0FLcEDRy1j8lRkvXVrMaZ+wLSzQezAYkc X-Gm-Message-State: AOJu0Yx3l88QqeCWYqOfuk2f9RmM/USHan9/IGVzN5MH70KiWSSssXvZ AyCb7ZMa0sG04mG5L4u7Izob3ig7I7gmsauL9ndAoNVe6xlEQ6TziTEKqBaWbXY1QZggzy+wZwp SYODM9w== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:b0b5:95af:a29:375e]) (user=irogers job=sendgmr) by 2002:a05:6902:c0d:b0:de5:5304:3206 with SMTP id 3f1490d57ef6-df77223c631mr324959276.11.1716583969292; Fri, 24 May 2024 13:52:49 -0700 (PDT) Date: Fri, 24 May 2024 13:52:26 -0700 In-Reply-To: <20240524205227.244375-1-irogers@google.com> Message-Id: <20240524205227.244375-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240524205227.244375-1-irogers@google.com> X-Mailer: git-send-email 2.45.1.288.g0e0cd299f1-goog Subject: [PATCH v3 2/3] perf bpf filter: Add uid and gid terms 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 , Changbin Du , Yang Jihong , Andrii Nakryiko , John Fastabend , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Allow the BPF filter to use the uid and gid terms determined by the bpf_get_current_uid_gid BPF helper. For example, the following will record the cpu-clock event system wide discarding samples that don't belong to the current user. $ perf record -e cpu-clock --filter "uid == $(id -u)" -a sleep 0.1 Signed-off-by: Ian Rogers --- tools/perf/Documentation/perf-record.txt | 2 +- tools/perf/util/bpf-filter.c | 5 +++++ tools/perf/util/bpf-filter.l | 2 ++ tools/perf/util/bpf_skel/sample-filter.h | 3 +++ tools/perf/util/bpf_skel/sample_filter.bpf.c | 4 ++++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/perf/Documentation/perf-record.txt b/tools/perf/Documentation/perf-record.txt index 6015fdd08fb6..059bc40c5ee1 100644 --- a/tools/perf/Documentation/perf-record.txt +++ b/tools/perf/Documentation/perf-record.txt @@ -200,7 +200,7 @@ OPTIONS ip, id, tid, pid, cpu, time, addr, period, txn, weight, phys_addr, code_pgsz, data_pgsz, weight1, weight2, weight3, ins_lat, retire_lat, p_stage_cyc, mem_op, mem_lvl, mem_snoop, mem_remote, mem_lock, - mem_dtlb, mem_blk, mem_hops + mem_dtlb, mem_blk, mem_hops, uid, gid The can be one of: ==, !=, >, >=, <, <=, & diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c index f10148623a8e..04f98b6bb291 100644 --- a/tools/perf/util/bpf-filter.c +++ b/tools/perf/util/bpf-filter.c @@ -63,6 +63,11 @@ static int check_sample_flags(struct evsel *evsel, struct perf_bpf_filter_expr * (evsel->core.attr.sample_type & (1 << (expr->term - PBF_TERM_SAMPLE_START)))) return 0; + if (expr->term == PBF_TERM_UID || expr->term == PBF_TERM_GID) { + /* Not dependent on the sample_type as computed from a BPF helper. */ + return 0; + } + if (expr->op == PBF_OP_GROUP_BEGIN) { struct perf_bpf_filter_expr *group; diff --git a/tools/perf/util/bpf-filter.l b/tools/perf/util/bpf-filter.l index 62c959813466..2a7c839f3fae 100644 --- a/tools/perf/util/bpf-filter.l +++ b/tools/perf/util/bpf-filter.l @@ -95,6 +95,8 @@ mem_lock { return sample_part(PBF_TERM_DATA_SRC, 5); } mem_dtlb { return sample_part(PBF_TERM_DATA_SRC, 6); } mem_blk { return sample_part(PBF_TERM_DATA_SRC, 7); } mem_hops { return sample_part(PBF_TERM_DATA_SRC, 8); } +uid { return sample(PBF_TERM_UID); } +gid { return sample(PBF_TERM_GID); } "==" { return operator(PBF_OP_EQ); } "!=" { return operator(PBF_OP_NEQ); } diff --git a/tools/perf/util/bpf_skel/sample-filter.h b/tools/perf/util/bpf_skel/sample-filter.h index 25f780022951..350efa121026 100644 --- a/tools/perf/util/bpf_skel/sample-filter.h +++ b/tools/perf/util/bpf_skel/sample-filter.h @@ -47,6 +47,9 @@ enum perf_bpf_filter_term { PBF_TERM_CODE_PAGE_SIZE = PBF_TERM_SAMPLE_START + 23, /* SAMPLE_CODE_PAGE_SIZE = 1U << 23 */ PBF_TERM_WEIGHT_STRUCT = PBF_TERM_SAMPLE_START + 24, /* SAMPLE_WEIGHT_STRUCT = 1U << 24 */ PBF_TERM_SAMPLE_END = PBF_TERM_WEIGHT_STRUCT, + /* Terms computed from BPF helpers. */ + PBF_TERM_UID, + PBF_TERM_GID, }; /* BPF map entry for filtering */ diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util/bpf_skel/sample_filter.bpf.c index 5ac1778ff66e..f59985101973 100644 --- a/tools/perf/util/bpf_skel/sample_filter.bpf.c +++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c @@ -140,6 +140,10 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx, } /* return the whole word */ return kctx->data->data_src.val; + case PBF_TERM_UID: + return bpf_get_current_uid_gid() & 0xFFFFFFFF; + case PBF_TERM_GID: + return bpf_get_current_uid_gid() >> 32; case PBF_TERM_NONE: case __PBF_UNUSED_TERM4: case __PBF_UNUSED_TERM5: -- 2.45.1.288.g0e0cd299f1-goog