Re: [PATCH bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage

4 messages, 3 authors, 2021-01-12 · open the first message on its own page

Re: [PATCH bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage

From: Yonghong Song <hidden>
Date: 2021-01-11 17:32:27


On 1/8/21 3:19 PM, Song Liu wrote:
quoted hunk
Task local storage is enabled for tracing programs. Add a test for it
without CONFIG_BPF_LSM.

Signed-off-by: Song Liu <redacted>
---
  .../bpf/prog_tests/test_task_local_storage.c  | 34 +++++++++++++++++
  .../selftests/bpf/progs/task_local_storage.c  | 37 +++++++++++++++++++
  2 files changed, 71 insertions(+)
  create mode 100644 tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
  create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage.c
diff --git a/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
new file mode 100644
index 0000000000000..7de7a154ebbe6
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
2020 -> 2021
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "task_local_storage.skel.h"
+
+static unsigned int duration;
+
+void test_test_task_local_storage(void)
+{
+	struct task_local_storage *skel;
+	const int count = 10;
+	int i, err;
+
+	skel = task_local_storage__open_and_load();
+
Extra line is unnecessary here.
+	if (CHECK(!skel, "skel_open_and_load", "skeleton open and load failed\n"))
+		return;
+
+	err = task_local_storage__attach(skel);
+
ditto.
+	if (CHECK(err, "skel_attach", "skeleton attach failed\n"))
+		goto out;
+
+	for (i = 0; i < count; i++)
+		usleep(1000);
Does a smaller usleep value will work? If it is, recommend to have a 
smaller value here to reduce test_progs running time.
quoted hunk
+	CHECK(skel->bss->value < count, "task_local_storage_value",
+	      "task local value too small\n");
+
+out:
+	task_local_storage__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/task_local_storage.c b/tools/testing/selftests/bpf/progs/task_local_storage.c
new file mode 100644
index 0000000000000..807255c5c162d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_local_storage.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
2020 -> 2021
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct local_data {
+	__u64 val;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+	__type(key, int);
+	__type(value, struct local_data);
+} task_storage_map SEC(".maps");
+
+int value = 0;
+
+SEC("tp_btf/sched_switch")
+int BPF_PROG(on_switch, bool preempt, struct task_struct *prev,
+	     struct task_struct *next)
+{
+	struct local_data *storage;
If it possible that we do some filtering based on test_progs pid
so below bpf_task_storage_get is only called for test_progs process?
This is more targeted and can avoid counter contributions from
other unrelated processes and make test_task_local_storage.c result
comparison more meaningful.
+
+	storage = bpf_task_storage_get(&task_storage_map,
+				       next, 0,
+				       BPF_LOCAL_STORAGE_GET_F_CREATE);
+	if (storage) {
+		storage->val++;
+		value = storage->val;
+	}
+	return 0;
+}

Re: [PATCH bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage

From: KP Singh <kpsingh@kernel.org>
Date: 2021-01-11 17:45:32

On Mon, Jan 11, 2021 at 6:31 PM Yonghong Song [off-list ref] wrote:


On 1/8/21 3:19 PM, Song Liu wrote:
quoted
Task local storage is enabled for tracing programs. Add a test for it
without CONFIG_BPF_LSM.
Can you also explain what the test does in the commit log?

It would also be nicer to have a somewhat more realistic selftest which
represents a simple tracing + task local storage use case.
quoted
Signed-off-by: Song Liu <redacted>
---
  .../bpf/prog_tests/test_task_local_storage.c  | 34 +++++++++++++++++
  .../selftests/bpf/progs/task_local_storage.c  | 37 +++++++++++++++++++
  2 files changed, 71 insertions(+)
  create mode 100644 tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
  create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage.c
diff --git a/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
new file mode 100644
index 0000000000000..7de7a154ebbe6
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
2020 -> 2021
quoted
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "task_local_storage.skel.h"
+
+static unsigned int duration;
+
+void test_test_task_local_storage(void)
+{
+     struct task_local_storage *skel;
+     const int count = 10;
+     int i, err;
+
+     skel = task_local_storage__open_and_load();
+
Extra line is unnecessary here.
quoted
+     if (CHECK(!skel, "skel_open_and_load", "skeleton open and load failed\n"))
+             return;
+
+     err = task_local_storage__attach(skel);
+
ditto.
quoted
+     if (CHECK(err, "skel_attach", "skeleton attach failed\n"))
+             goto out;
+
+     for (i = 0; i < count; i++)
+             usleep(1000);
Does a smaller usleep value will work? If it is, recommend to have a
smaller value here to reduce test_progs running time.
quoted
+     CHECK(skel->bss->value < count, "task_local_storage_value",
+           "task local value too small\n");
[...]
quoted
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
2020 -> 2021
quoted
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
[...]
quoted
+{
+     struct local_data *storage;
If it possible that we do some filtering based on test_progs pid
so below bpf_task_storage_get is only called for test_progs process?
This is more targeted and can avoid counter contributions from
other unrelated processes and make test_task_local_storage.c result
comparison more meaningful.
Indeed, have a look at the monitored_pid approach some of the LSM programs
do.
quoted
+
+     storage = bpf_task_storage_get(&task_storage_map,
+                                    next, 0,
+                                    BPF_LOCAL_STORAGE_GET_F_CREATE);
+     if (storage) {
+             storage->val++;
+             value = storage->val;
+     }
+     return 0;
+}

Re: [PATCH bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage

From: Song Liu <hidden>
Date: 2021-01-12 00:55:29

On Jan 11, 2021, at 9:44 AM, KP Singh [off-list ref] wrote:

On Mon, Jan 11, 2021 at 6:31 PM Yonghong Song [off-list ref] wrote:
quoted


On 1/8/21 3:19 PM, Song Liu wrote:
quoted
Task local storage is enabled for tracing programs. Add a test for it
without CONFIG_BPF_LSM.
Can you also explain what the test does in the commit log?

It would also be nicer to have a somewhat more realistic selftest which
represents a simple tracing + task local storage use case.
Let me try to make this more realistic. 

Thanks,
Song

[...]

Re: [PATCH bpf-next 2/4] selftests/bpf: add non-BPF_LSM test for task local storage

From: Song Liu <hidden>
Date: 2021-01-12 00:55:29

On Jan 11, 2021, at 9:30 AM, Yonghong Song [off-list ref] wrote:



On 1/8/21 3:19 PM, Song Liu wrote:
quoted
Task local storage is enabled for tracing programs. Add a test for it
without CONFIG_BPF_LSM.
Signed-off-by: Song Liu <redacted>
---
 .../bpf/prog_tests/test_task_local_storage.c  | 34 +++++++++++++++++
 .../selftests/bpf/progs/task_local_storage.c  | 37 +++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
 create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage.c
diff --git a/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c b/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
new file mode 100644
index 0000000000000..7de7a154ebbe6
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_task_local_storage.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
2020 -> 2021
quoted
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include "task_local_storage.skel.h"
+
+static unsigned int duration;
+
+void test_test_task_local_storage(void)
+{
+	struct task_local_storage *skel;
+	const int count = 10;
+	int i, err;
+
+	skel = task_local_storage__open_and_load();
+
Extra line is unnecessary here.
quoted
+	if (CHECK(!skel, "skel_open_and_load", "skeleton open and load failed\n"))
+		return;
+
+	err = task_local_storage__attach(skel);
+
ditto.
quoted
+	if (CHECK(err, "skel_attach", "skeleton attach failed\n"))
+		goto out;
+
+	for (i = 0; i < count; i++)
+		usleep(1000);
Does a smaller usleep value will work? If it is, recommend to have a smaller value here to reduce test_progs running time.
I thought 10ms total was acceptable. But yeah, smaller value should still work. 
quoted
+	CHECK(skel->bss->value < count, "task_local_storage_value",
+	      "task local value too small\n");
+
+out:
+	task_local_storage__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/task_local_storage.c b/tools/testing/selftests/bpf/progs/task_local_storage.c
new file mode 100644
index 0000000000000..807255c5c162d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/task_local_storage.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2020 Facebook */
2020 -> 2021
quoted
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct local_data {
+	__u64 val;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+	__uint(map_flags, BPF_F_NO_PREALLOC);
+	__type(key, int);
+	__type(value, struct local_data);
+} task_storage_map SEC(".maps");
+
+int value = 0;
+
+SEC("tp_btf/sched_switch")
+int BPF_PROG(on_switch, bool preempt, struct task_struct *prev,
+	     struct task_struct *next)
+{
+	struct local_data *storage;
If it possible that we do some filtering based on test_progs pid
so below bpf_task_storage_get is only called for test_progs process?
This is more targeted and can avoid counter contributions from
other unrelated processes and make test_task_local_storage.c result
comparison more meaningful.
Make sense. Will fix in the next version. 
quoted
+
+	storage = bpf_task_storage_get(&task_storage_map,
+				       next, 0,
+				       BPF_LOCAL_STORAGE_GET_F_CREATE);
+	if (storage) {
+		storage->val++;
+		value = storage->val;
+	}
+	return 0;
+}

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help