Skip to contentSkip to conversation

perf bpf-filter: Fix sample flag check with ||

PATCH In review Namhyung Kim 3 participants 8 messages Aug 11, 2023 – Aug 23, 2023 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf bpf-filter: Fix sample flag check with ||

For logical OR operator, the actual sample_flags are in the 'groups' list so it needs to check entries in the list instead. Otherwise it would show the following error message.

  $ sudo perf record -a -e cycles:p --filter 'period > 100 || weight > 0' sleep 1
  Error: cycles:p event does not have sample flags 0
  failed to set filter "BPF" on event cycles:p with 2 (No such file or directory)

Actually it should warn on 'weight' is used without WEIGHT flag.

Error: cycles:p event does not have PERF_SAMPLE_WEIGHT
Hint: please add -W option to perf record
failed to set filter "BPF" on event cycles:p with 2 (No such file or directory)

Fixes 4310551b76e0 ("perf bpf filter: Show warning for missing sample flags")
---
 tools/perf/util/bpf-filter.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/perf/util/bpf-filter.c +10 -0
index 47f01df658d9..b51544996046 100644
@@ -62,6 +62,16 @@ static int check_sample_flags(struct evsel *evsel, struct perf_bpf_filter_expr *
 	if (evsel->core.attr.sample_type & expr->sample_flags)
 		return 0;
 
+	if (expr->op == PBF_OP_GROUP_BEGIN) {
+		struct perf_bpf_filter_expr *group;
+
+		list_for_each_entry(group, &expr->groups, list) {
+			if (check_sample_flags(evsel, group) < 0)
+				return -1;
+		}
+		return 0;
+	}
+
 	info = get_sample_info(expr->sample_flags);
 	if (info == NULL) {
 		pr_err("Error: %s event does not have sample flags %lx\n",
Signature · 1 line
2.41.0.640.ga95def55d0-goog
author maintainer
#1 lore raw

PATCH 2/2 perf test: Add perf record sample filtering test

  $ sudo ./perf test 'sample filter' -v
   94: perf record sample filtering (by BPF) tests                     :
  --- start ---
  test child forked, pid 3817527
  Checking BPF-filter privilege
  Basic bpf-filter test
  Basic bpf-filter test [Success]
  Failing bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Failing bpf-filter test [Success]
  Group bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE
  Group bpf-filter test [Success]
  test child finished with 0
  ---- end ----
  perf record sample filtering (by BPF) tests: Ok
---
 tools/perf/tests/shell/record_bpf_filter.sh | 128 ++++++++++++++++++++
 1 file changed, 128 insertions(+)
 create mode 100755 tools/perf/tests/shell/record_bpf_filter.sh
tools/perf/tests/shell/record_bpf_filter.sh new file +128 -0
new file mode 100755
index 000000000000..e76ea861b92c
@@ -0,0 +1,128 @@
+#!/bin/sh
+# perf record sample filtering (by BPF) tests
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+err=0
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+
+cleanup() {
+  rm -f "${perfdata}"
+  rm -f "${perfdata}".old
+  trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+  cleanup
+  exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+test_bpf_filter_priv() {
+  echo "Checking BPF-filter privilege"
+
+  if [ "$(id -u)" != 0 ]
+  then
+    echo "bpf-filter test [Skipped permission]"
+    err=2
+    return
+  fi
+  if ! perf record -e task-clock --filter 'period > 1' \
+	  -o /dev/null --quiet true 2>&1
+  then
+    echo "bpf-filter test [Skipped missing BPF support]"
+    err=2
+    return
+  fi
+}
+
+test_bpf_filter_basic() {
+  echo "Basic bpf-filter test"
+
+  if ! perf record -e task-clock -c 10000 --filter 'ip < 0xffffffff00000000' \
+	  -o "${perfdata}" true 2> /dev/null
+  then
+    echo "Basic bpf-filter test [Failed record]"
+    err=1
+    return
+  fi
+  if perf script -i "${perfdata}" -F ip | grep 'ffffffff[0-9a-f]*'
+  then
+    echo "Basic bpf-filter test [Failed invalid output]"
+    err=1
+    return
+  fi
+  echo "Basic bpf-filter test [Success]"
+}
+
+test_bpf_filter_fail() {
+  echo "Failing bpf-filter test"
+
+  # 'cpu' requires PERF_SAMPLE_CPU flag
+  if ! perf record -e task-clock --filter 'cpu > 0' \
+	  -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU
+  then
+    echo "Failing bpf-filter test [Failed forbidden CPU]"
+    err=1
+    return
+  fi
+
+  if ! perf record --sample-cpu -e task-clock --filter 'cpu > 0' \
+	  -o /dev/null true 2>/dev/null
+  then
+    echo "Failing bpf-filter test [Failed should succeed]"
+    err=1
+    return
+  fi
+
+  echo "Failing bpf-filter test [Success]"
+}
+
+test_bpf_filter_group() {
+  echo "Group bpf-filter test"
+
+  if ! perf record -e task-clock --filter 'period > 1000 || ip > 0' \
+	  -o /dev/null true 2>/dev/null
+  then
+    echo "Group bpf-filter test [Failed should succeed]"
+    err=1
+    return
+  fi
+
+  if ! perf record -e task-clock --filter 'cpu > 0 || ip > 0' \
+	  -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU
+  then
+    echo "Group bpf-filter test [Failed forbidden CPU]"
+    err=1
+    return
+  fi
+
+  if ! perf record -e task-clock --filter 'period > 0 || code_pgsz > 4096' \
+	  -o /dev/null true 2>&1 | grep PERF_SAMPLE_CODE_PAGE_SIZE
+  then
+    echo "Group bpf-filter test [Failed forbidden CODE_PAGE_SIZE]"
+    err=1
+    return
+  fi
+
+  echo "Group bpf-filter test [Success]"
+}
+
+
+test_bpf_filter_priv
+
+if [ $err = 0 ]; then
+  test_bpf_filter_basic
+fi
+
+if [ $err = 0 ]; then
+  test_bpf_filter_fail
+fi
+
+if [ $err = 0 ]; then
+  test_bpf_filter_group
+fi
+
+cleanup
+exit $err
Signature · 1 line
2.41.0.640.ga95def55d0-goog
reviewer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] perf test: Add perf record sample filtering test

Quoted 19 lines · Namhyung Kim
  $ sudo ./perf test 'sample filter' -v
   94: perf record sample filtering (by BPF) tests                     :
  --- start ---
  test child forked, pid 3817527
  Checking BPF-filter privilege
  Basic bpf-filter test
  Basic bpf-filter test [Success]
  Failing bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Failing bpf-filter test [Success]
  Group bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE
  Group bpf-filter test [Success]
  test child finished with 0
  ---- end ----
  perf record sample filtering (by BPF) tests: Ok
Quoted 2 lines

Thanks,
Ian

Quoted 142 lines
---
 tools/perf/tests/shell/record_bpf_filter.sh | 128 ++++++++++++++++++++
 1 file changed, 128 insertions(+)
 create mode 100755 tools/perf/tests/shell/record_bpf_filter.sh
tools/perf/tests/shell/record_bpf_filter.sh new file +128 -1
new file mode 100755
index 000000000000..e76ea861b92c
@@ -0,0 +1,128 @@
+#!/bin/sh
+# perf record sample filtering (by BPF) tests
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+err=0
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+
+cleanup() {
+  rm -f "${perfdata}"
+  rm -f "${perfdata}".old
+  trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+  cleanup
+  exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+test_bpf_filter_priv() {
+  echo "Checking BPF-filter privilege"
+
+  if [ "$(id -u)" != 0 ]
+  then
+    echo "bpf-filter test [Skipped permission]"
+    err=2
+    return
+  fi
+  if ! perf record -e task-clock --filter 'period > 1' \
+         -o /dev/null --quiet true 2>&1
+  then
+    echo "bpf-filter test [Skipped missing BPF support]"
+    err=2
+    return
+  fi
+}
+
+test_bpf_filter_basic() {
+  echo "Basic bpf-filter test"
+
+  if ! perf record -e task-clock -c 10000 --filter 'ip < 0xffffffff00000000' \
+         -o "${perfdata}" true 2> /dev/null
+  then
+    echo "Basic bpf-filter test [Failed record]"
+    err=1
+    return
+  fi
+  if perf script -i "${perfdata}" -F ip | grep 'ffffffff[0-9a-f]*'
+  then
+    echo "Basic bpf-filter test [Failed invalid output]"
+    err=1
+    return
+  fi
+  echo "Basic bpf-filter test [Success]"
+}
+
+test_bpf_filter_fail() {
+  echo "Failing bpf-filter test"
+
+  # 'cpu' requires PERF_SAMPLE_CPU flag
+  if ! perf record -e task-clock --filter 'cpu > 0' \
+         -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU
+  then
+    echo "Failing bpf-filter test [Failed forbidden CPU]"
+    err=1
+    return
+  fi
+
+  if ! perf record --sample-cpu -e task-clock --filter 'cpu > 0' \
+         -o /dev/null true 2>/dev/null
+  then
+    echo "Failing bpf-filter test [Failed should succeed]"
+    err=1
+    return
+  fi
+
+  echo "Failing bpf-filter test [Success]"
+}
+
+test_bpf_filter_group() {
+  echo "Group bpf-filter test"
+
+  if ! perf record -e task-clock --filter 'period > 1000 || ip > 0' \
+         -o /dev/null true 2>/dev/null
+  then
+    echo "Group bpf-filter test [Failed should succeed]"
+    err=1
+    return
+  fi
+
+  if ! perf record -e task-clock --filter 'cpu > 0 || ip > 0' \
+         -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU
+  then
+    echo "Group bpf-filter test [Failed forbidden CPU]"
+    err=1
+    return
+  fi
+
+  if ! perf record -e task-clock --filter 'period > 0 || code_pgsz > 4096' \
+         -o /dev/null true 2>&1 | grep PERF_SAMPLE_CODE_PAGE_SIZE
+  then
+    echo "Group bpf-filter test [Failed forbidden CODE_PAGE_SIZE]"
+    err=1
+    return
+  fi
+
+  echo "Group bpf-filter test [Success]"
+}
+
+
+test_bpf_filter_priv
+
+if [ $err = 0 ]; then
+  test_bpf_filter_basic
+fi
+
+if [ $err = 0 ]; then
+  test_bpf_filter_fail
+fi
+
+if [ $err = 0 ]; then
+  test_bpf_filter_group
+fi
+
+cleanup
+exit $err
--

2.41.0.640.ga95def55d0-goog

#3 lore raw

Re: [PATCH 2/2] perf test: Add perf record sample filtering test

In reply to Namhyung Kim

Em Thu, Aug 10, 2023 at 07:58:22PM -0700, Namhyung Kim escreveu:

Quoted 9 lines
  $ sudo ./perf test 'sample filter' -v
   94: perf record sample filtering (by BPF) tests                     :
  --- start ---
  test child forked, pid 3817527
  Checking BPF-filter privilege
  Basic bpf-filter test
  Basic bpf-filter test [Success]
  Failing bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
Quoted 8 lines
  Failing bpf-filter test [Success]
  Group bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE
  Group bpf-filter test [Success]
  test child finished with 0
  ---- end ----
  perf record sample filtering (by BPF) tests: Ok

[root@five ~]# perf test -v "by BPF"
91: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 64165
Checking BPF-filter privilege
Basic bpf-filter test
ffffffff97f4f688
ffffffff97f73859
ffffffff97412ce6
ffffffff976da215
ffffffff973a92bf
ffffffff97376ad7
ffffffff97f73859
ffffffff97f7400c
ffffffff97365116
ffffffff97f73859
ffffffff97f5320c
ffffffff973ea351
ffffffff97143774
ffffffff9740f730
ffffffff9736944f
ffffffff973e593d
ffffffff976e36c8
ffffffffc12cf19b
ffffffff976c79d5
ffffffff9737d254
ffffffff9737d254
ffffffff9737d254
ffffffff9737d254
ffffffff9737d254
ffffffff9737ca5f
ffffffff9737d254
ffffffff9737d254
ffffffff9737d339
ffffffff9737dcf3
ffffffff9737dcf3
ffffffff9737d254
ffffffff973e442a
ffffffff973713b8
ffffffff973ea561
ffffffff973712ee
ffffffff973713b8
ffffffff973713b8
ffffffff973e43fa
ffffffff971fa17e
ffffffff971fa17e
ffffffff973ac8ee
ffffffff97f73859
ffffffff9741082c
ffffffff973ea34a
ffffffff974148c6
ffffffff974c97d5
ffffffff97394b51
ffffffff973916af
ffffffff9737b5e3
ffffffff976cf825
ffffffff9737c58d
ffffffff9788ab15
ffffffff9732af89
ffffffff97096ef3
ffffffff973e92fd
ffffffff9730a991
ffffffff9739e2c5
ffffffff9731cdc2
ffffffff97f363c4
ffffffff97f3915c
ffffffff97f5d754
ffffffff9718a181
ffffffff9709b1a5
ffffffff973710e4
ffffffff97f52b27
ffffffff9738b905
ffffffff971fdca5
ffffffff9737dbc4
ffffffff971b4e04
Basic bpf-filter test [Failed invalid output] test child finished with -1
---- end ----
perf record sample filtering (by BPF) tests: FAILED! [root@five ~]#

[root@five ~]# uname -a
Linux five 6.2.15-100.fc36.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 11 16:51:53 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux [root@five ~]#

Quoted 144 lines
---
 tools/perf/tests/shell/record_bpf_filter.sh | 128 ++++++++++++++++++++
 1 file changed, 128 insertions(+)
 create mode 100755 tools/perf/tests/shell/record_bpf_filter.sh
tools/perf/tests/shell/record_bpf_filter.sh new file +128 -0
new file mode 100755
index 000000000000..e76ea861b92c
@@ -0,0 +1,128 @@
+#!/bin/sh
+# perf record sample filtering (by BPF) tests
+# SPDX-License-Identifier: GPL-2.0
+
+set -e
+
+err=0
+perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
+
+cleanup() {
+  rm -f "${perfdata}"
+  rm -f "${perfdata}".old
+  trap - EXIT TERM INT
+}
+
+trap_cleanup() {
+  cleanup
+  exit 1
+}
+trap trap_cleanup EXIT TERM INT
+
+test_bpf_filter_priv() {
+  echo "Checking BPF-filter privilege"
+
+  if [ "$(id -u)" != 0 ]
+  then
+    echo "bpf-filter test [Skipped permission]"
+    err=2
+    return
+  fi
+  if ! perf record -e task-clock --filter 'period > 1' \
+	  -o /dev/null --quiet true 2>&1
+  then
+    echo "bpf-filter test [Skipped missing BPF support]"
+    err=2
+    return
+  fi
+}
+
+test_bpf_filter_basic() {
+  echo "Basic bpf-filter test"
+
+  if ! perf record -e task-clock -c 10000 --filter 'ip < 0xffffffff00000000' \
+	  -o "${perfdata}" true 2> /dev/null
+  then
+    echo "Basic bpf-filter test [Failed record]"
+    err=1
+    return
+  fi
+  if perf script -i "${perfdata}" -F ip | grep 'ffffffff[0-9a-f]*'
+  then
+    echo "Basic bpf-filter test [Failed invalid output]"
+    err=1
+    return
+  fi
+  echo "Basic bpf-filter test [Success]"
+}
+
+test_bpf_filter_fail() {
+  echo "Failing bpf-filter test"
+
+  # 'cpu' requires PERF_SAMPLE_CPU flag
+  if ! perf record -e task-clock --filter 'cpu > 0' \
+	  -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU
+  then
+    echo "Failing bpf-filter test [Failed forbidden CPU]"
+    err=1
+    return
+  fi
+
+  if ! perf record --sample-cpu -e task-clock --filter 'cpu > 0' \
+	  -o /dev/null true 2>/dev/null
+  then
+    echo "Failing bpf-filter test [Failed should succeed]"
+    err=1
+    return
+  fi
+
+  echo "Failing bpf-filter test [Success]"
+}
+
+test_bpf_filter_group() {
+  echo "Group bpf-filter test"
+
+  if ! perf record -e task-clock --filter 'period > 1000 || ip > 0' \
+	  -o /dev/null true 2>/dev/null
+  then
+    echo "Group bpf-filter test [Failed should succeed]"
+    err=1
+    return
+  fi
+
+  if ! perf record -e task-clock --filter 'cpu > 0 || ip > 0' \
+	  -o /dev/null true 2>&1 | grep PERF_SAMPLE_CPU
+  then
+    echo "Group bpf-filter test [Failed forbidden CPU]"
+    err=1
+    return
+  fi
+
+  if ! perf record -e task-clock --filter 'period > 0 || code_pgsz > 4096' \
+	  -o /dev/null true 2>&1 | grep PERF_SAMPLE_CODE_PAGE_SIZE
+  then
+    echo "Group bpf-filter test [Failed forbidden CODE_PAGE_SIZE]"
+    err=1
+    return
+  fi
+
+  echo "Group bpf-filter test [Success]"
+}
+
+
+test_bpf_filter_priv
+
+if [ $err = 0 ]; then
+  test_bpf_filter_basic
+fi
+
+if [ $err = 0 ]; then
+  test_bpf_filter_fail
+fi
+
+if [ $err = 0 ]; then
+  test_bpf_filter_group
+fi
+
+cleanup
+exit $err
Signature · 1 line
2.41.0.640.ga95def55d0-goog
Signature · 1 line
- Arnaldo
#4 lore raw

Re: [PATCH 2/2] perf test: Add perf record sample filtering test

Em Tue, Aug 15, 2023 at 02:30:18PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 19 lines

Em Thu, Aug 10, 2023 at 07:58:22PM -0700, Namhyung Kim escreveu:

Quoted 9 lines
  $ sudo ./perf test 'sample filter' -v
   94: perf record sample filtering (by BPF) tests                     :
  --- start ---
  test child forked, pid 3817527
  Checking BPF-filter privilege
  Basic bpf-filter test
  Basic bpf-filter test [Success]
  Failing bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
Quoted 8 lines
  Failing bpf-filter test [Success]
  Group bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE
  Group bpf-filter test [Success]
  test child finished with 0
  ---- end ----
  perf record sample filtering (by BPF) tests: Ok
Quoted 13 lines

[root@five ~]# perf test -v "by BPF"
91: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 64165
Checking BPF-filter privilege
Basic bpf-filter test
ffffffff97f4f688
ffffffff97f73859
ffffffff97412ce6
ffffffff976da215
ffffffff973a92bf
ffffffff97376ad7
ffffffff97f73859

<SNIP

Quoted 9 lines

ffffffff971fdca5
ffffffff9737dbc4
ffffffff971b4e04
Basic bpf-filter test [Failed invalid output] test child finished with -1
---- end ----
perf record sample filtering (by BPF) tests: FAILED! [root@five ~]#

Quoted 3 lines

[root@five ~]# uname -a
Linux five 6.2.15-100.fc36.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 11 16:51:53 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux [root@five ~]#

Above was on a AMD Ryzen 5950X, the following was on a lenovo t480s, Intel notebook:

[root@quaco ~]# uname -a
Linux quaco 6.4.7-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jul 27 20:01:18 UTC 2023 x86_64 GNU/Linux [root@quaco ~]# perf test "filter"

 30: Filter hist entries                                             : Ok
 36: Filter fds with revents mask in a fdarray                       : Ok
 67: dlfilter C API                                                  : Ok
 91: perf record sample filtering (by BPF) tests                     : Ok

[root@quaco ~]# perf test -v "by BPF"
91: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 273609
Checking BPF-filter privilege
Basic bpf-filter test
Basic bpf-filter test [Success]
Failing bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU Failing bpf-filter test [Success]
Group bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU
Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE Group bpf-filter test [Success]
test child finished with 0
---- end ----
perf record sample filtering (by BPF) tests: Ok [root@quaco ~]#

author maintainer
#5 lore raw

Re: [PATCH 2/2] perf test: Add perf record sample filtering test

Hi Arnaldo,

Quoted 44 lines · Arnaldo Carvalho de Melo

Em Tue, Aug 15, 2023 at 02:30:18PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 19 lines

Em Thu, Aug 10, 2023 at 07:58:22PM -0700, Namhyung Kim escreveu:

Quoted 9 lines
  $ sudo ./perf test 'sample filter' -v
   94: perf record sample filtering (by BPF) tests                     :
  --- start ---
  test child forked, pid 3817527
  Checking BPF-filter privilege
  Basic bpf-filter test
  Basic bpf-filter test [Success]
  Failing bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
Quoted 8 lines
  Failing bpf-filter test [Success]
  Group bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE
  Group bpf-filter test [Success]
  test child finished with 0
  ---- end ----
  perf record sample filtering (by BPF) tests: Ok
Quoted 13 lines

[root@five ~]# perf test -v "by BPF"
91: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 64165
Checking BPF-filter privilege
Basic bpf-filter test
ffffffff97f4f688
ffffffff97f73859
ffffffff97412ce6
ffffffff976da215
ffffffff973a92bf
ffffffff97376ad7
ffffffff97f73859

<SNIP

Quoted 9 lines

ffffffff971fdca5
ffffffff9737dbc4
ffffffff971b4e04
Basic bpf-filter test [Failed invalid output] test child finished with -1
---- end ----
perf record sample filtering (by BPF) tests: FAILED! [root@five ~]#

Quoted 6 lines
Quoted 3 lines

[root@five ~]# uname -a
Linux five 6.2.15-100.fc36.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 11 16:51:53 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux [root@five ~]#

Above was on a AMD Ryzen 5950X, the following was on a lenovo t480s, Intel notebook:

Thanks for the test. I think it's a matter of the kernel version rather than the CPU vendor. 6.1 or before will fail the check in the beginning but 6.2 kernel lacks a feature to set sample flags for some fields and silently accept all samples. IIRC it's added in v6.3.

Probably I need to add a version check in the error path.

Thanks,
Namhyung

Quoted 26 lines

[root@quaco ~]# uname -a
Linux quaco 6.4.7-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jul 27 20:01:18 UTC 2023 x86_64 GNU/Linux [root@quaco ~]# perf test "filter"

 30: Filter hist entries                                             : Ok
 36: Filter fds with revents mask in a fdarray                       : Ok
 67: dlfilter C API                                                  : Ok
 91: perf record sample filtering (by BPF) tests                     : Ok

[root@quaco ~]# perf test -v "by BPF"
91: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 273609
Checking BPF-filter privilege
Basic bpf-filter test
Basic bpf-filter test [Success]
Failing bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU Failing bpf-filter test [Success]
Group bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU
Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE Group bpf-filter test [Success]
test child finished with 0
---- end ----
perf record sample filtering (by BPF) tests: Ok [root@quaco ~]#

#6 lore raw

Re: [PATCH 2/2] perf test: Add perf record sample filtering test

Em Tue, Aug 22, 2023 at 05:50:26PM -0700, Namhyung Kim escreveu:

Quoted 55 lines

Hi Arnaldo,

Quoted 44 lines · Arnaldo Carvalho de Melo

Em Tue, Aug 15, 2023 at 02:30:18PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 19 lines

Em Thu, Aug 10, 2023 at 07:58:22PM -0700, Namhyung Kim escreveu:

Quoted 9 lines
  $ sudo ./perf test 'sample filter' -v
   94: perf record sample filtering (by BPF) tests                     :
  --- start ---
  test child forked, pid 3817527
  Checking BPF-filter privilege
  Basic bpf-filter test
  Basic bpf-filter test [Success]
  Failing bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
Quoted 8 lines
  Failing bpf-filter test [Success]
  Group bpf-filter test
  Error: task-clock event does not have PERF_SAMPLE_CPU
  Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE
  Group bpf-filter test [Success]
  test child finished with 0
  ---- end ----
  perf record sample filtering (by BPF) tests: Ok
Quoted 13 lines

[root@five ~]# perf test -v "by BPF"
91: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 64165
Checking BPF-filter privilege
Basic bpf-filter test
ffffffff97f4f688
ffffffff97f73859
ffffffff97412ce6
ffffffff976da215
ffffffff973a92bf
ffffffff97376ad7
ffffffff97f73859

<SNIP

Quoted 9 lines

ffffffff971fdca5
ffffffff9737dbc4
ffffffff971b4e04
Basic bpf-filter test [Failed invalid output] test child finished with -1
---- end ----
perf record sample filtering (by BPF) tests: FAILED! [root@five ~]#

Quoted 6 lines
Quoted 3 lines

[root@five ~]# uname -a
Linux five 6.2.15-100.fc36.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 11 16:51:53 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux [root@five ~]#

Above was on a AMD Ryzen 5950X, the following was on a lenovo t480s, Intel notebook:

Quoted 7 lines

Thanks for the test. I think it's a matter of the kernel version rather than the CPU vendor. 6.1 or before will fail the check in the beginning but 6.2 kernel lacks a feature to set sample flags for some fields and silently accept all samples. IIRC it's added in v6.3.

Probably I need to add a version check in the error path.

Yeah, we need to do a wider testing of all things enabled BUILD_BPF_SKEL=1, in more kernels, architectures, distributions.

Thanks for the feedback,

- Arnaldo

Quoted 29 lines

Thanks,
Namhyung

Quoted 26 lines

[root@quaco ~]# uname -a
Linux quaco 6.4.7-200.fc38.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jul 27 20:01:18 UTC 2023 x86_64 GNU/Linux [root@quaco ~]# perf test "filter"

 30: Filter hist entries                                             : Ok
 36: Filter fds with revents mask in a fdarray                       : Ok
 67: dlfilter C API                                                  : Ok
 91: perf record sample filtering (by BPF) tests                     : Ok

[root@quaco ~]# perf test -v "by BPF"
91: perf record sample filtering (by BPF) tests :
--- start ---
test child forked, pid 273609
Checking BPF-filter privilege
Basic bpf-filter test
Basic bpf-filter test [Success]
Failing bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU Failing bpf-filter test [Success]
Group bpf-filter test
Error: task-clock event does not have PERF_SAMPLE_CPU
Error: task-clock event does not have PERF_SAMPLE_CODE_PAGE_SIZE Group bpf-filter test [Success]
test child finished with 0
---- end ----
perf record sample filtering (by BPF) tests: Ok [root@quaco ~]#

Signature · 1 line
- Arnaldo
reviewer Reviewed-by on v1 1/2
#7 lore raw
In reply to Namhyung Kim
Quoted 9 lines · Namhyung Kim

For logical OR operator, the actual sample_flags are in the 'groups' list so it needs to check entries in the list instead. Otherwise it would show the following error message.

  $ sudo perf record -a -e cycles:p --filter 'period > 100 || weight > 0' sleep 1
  Error: cycles:p event does not have sample flags 0
  failed to set filter "BPF" on event cycles:p with 2 (No such file or directory)
Quoted 8 lines

Actually it should warn on 'weight' is used without WEIGHT flag.

Error: cycles:p event does not have PERF_SAMPLE_WEIGHT
Hint: please add -W option to perf record
failed to set filter "BPF" on event cycles:p with 2 (No such file or directory)

Fixes 4310551b76e0 ("perf bpf filter: Show warning for missing sample flags")

Thanks,
Ian

Quoted 28 lines
---
 tools/perf/util/bpf-filter.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/perf/util/bpf-filter.c +10 -1
index 47f01df658d9..b51544996046 100644
@@ -62,6 +62,16 @@ static int check_sample_flags(struct evsel *evsel, struct perf_bpf_filter_expr *
        if (evsel->core.attr.sample_type & expr->sample_flags)
                return 0;
 
+       if (expr->op == PBF_OP_GROUP_BEGIN) {
+               struct perf_bpf_filter_expr *group;
+
+               list_for_each_entry(group, &expr->groups, list) {
+                       if (check_sample_flags(evsel, group) < 0)
+                               return -1;
+               }
+               return 0;
+       }
+
        info = get_sample_info(expr->sample_flags);
        if (info == NULL) {
                pr_err("Error: %s event does not have sample flags %lx\n",
--

2.41.0.640.ga95def55d0-goog