From: "T.J. Mercier" <tjmercier@google.com>
To: sumit.semwal@linaro.org, christian.koenig@amd.com,
ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org,
martin.lau@linux.dev, skhan@linuxfoundation.org
Cc: linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org,
linux-doc@vger.kernel.org, bpf@vger.kernel.org,
linux-kselftest@vger.kernel.org, android-mm@google.com,
simona@ffwll.ch, corbet@lwn.net, eddyz87@gmail.com,
song@kernel.org, yonghong.song@linux.dev,
john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me,
jolsa@kernel.org, mykolal@fb.com,
"T.J. Mercier" <tjmercier@google.com>
Subject: [PATCH 3/4] selftests/bpf: Add test for dmabuf_iter
Date: Mon, 14 Apr 2025 22:52:26 +0000 [thread overview]
Message-ID: <20250414225227.3642618-4-tjmercier@google.com> (raw)
In-Reply-To: <20250414225227.3642618-1-tjmercier@google.com>
This test creates a udmabuf and uses a BPF program that prints dmabuf
metadata with the new dmabuf_iter to verify it can be found.
Signed-off-by: T.J. Mercier <tjmercier@google.com>
---
tools/testing/selftests/bpf/config | 1 +
.../selftests/bpf/prog_tests/dmabuf_iter.c | 116 ++++++++++++++++++
.../testing/selftests/bpf/progs/dmabuf_iter.c | 31 +++++
3 files changed, 148 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c
create mode 100644 tools/testing/selftests/bpf/progs/dmabuf_iter.c
diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config
index c378d5d07e02..a791c60813df 100644
--- a/tools/testing/selftests/bpf/config
+++ b/tools/testing/selftests/bpf/config
@@ -106,6 +106,7 @@ CONFIG_SECURITY=y
CONFIG_SECURITYFS=y
CONFIG_SYN_COOKIES=y
CONFIG_TEST_BPF=m
+CONFIG_UDMABUF=y
CONFIG_USERFAULTFD=y
CONFIG_VSOCKETS=y
CONFIG_VXLAN=y
diff --git a/tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c b/tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c
new file mode 100644
index 000000000000..af215a4e0520
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c
@@ -0,0 +1,116 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Google */
+
+#include <test_progs.h>
+#include <bpf/libbpf.h>
+#include <bpf/btf.h>
+#include "dmabuf_iter.skel.h"
+
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <linux/dma-buf.h>
+#include <linux/udmabuf.h>
+
+
+static void subtest_dmabuf_iter_check_udmabuf(struct dmabuf_iter *skel)
+{
+ static const char test_buffer_name[] = "udmabuf_test_buffer_for_iter";
+ const size_t test_buffer_size = 10 * getpagesize();
+
+ ASSERT_LE(sizeof(test_buffer_name), DMA_BUF_NAME_LEN, "NAMETOOLONG");
+
+ int memfd = memfd_create("memfd_test", MFD_ALLOW_SEALING);
+ ASSERT_OK_FD(memfd, "memfd_create");
+
+ ASSERT_OK(ftruncate(memfd, test_buffer_size), "ftruncate");
+
+ ASSERT_OK(fcntl(memfd, F_ADD_SEALS, F_SEAL_SHRINK), "seal");
+
+ int dev_udmabuf = open("/dev/udmabuf", O_RDONLY);
+ ASSERT_OK_FD(dev_udmabuf, "open udmabuf");
+
+ struct udmabuf_create create;
+ create.memfd = memfd;
+ create.flags = UDMABUF_FLAGS_CLOEXEC;
+ create.offset = 0;
+ create.size = test_buffer_size;
+
+ int udmabuf = ioctl(dev_udmabuf, UDMABUF_CREATE, &create);
+ close(dev_udmabuf);
+ ASSERT_OK_FD(udmabuf, "udmabuf_create");
+
+ ASSERT_OK(ioctl(udmabuf, DMA_BUF_SET_NAME_B, test_buffer_name), "name");
+
+ int iter_fd = bpf_iter_create(bpf_link__fd(skel->links.dmabuf_collector));
+ ASSERT_OK_FD(iter_fd, "iter_create");
+
+ FILE *iter_file = fdopen(iter_fd, "r");
+ ASSERT_OK_PTR(iter_file, "fdopen");
+
+ char *line = NULL;
+ size_t linesize = 0;
+ bool found_test_udmabuf = false;
+ while (getline(&line, &linesize, iter_file) != -1) {
+ long inode, size;
+ char name[DMA_BUF_NAME_LEN], exporter[32];
+
+ int nelements = sscanf(line, "ino:%ld size:%ld name:%s exp_name:%s",
+ &inode, &size, name, exporter);
+
+ if (nelements == 4 && size == test_buffer_size &&
+ !strcmp(name, test_buffer_name) &&
+ !strcmp(exporter, "udmabuf")) {
+ found_test_udmabuf = true;
+ break;
+ }
+ }
+
+ ASSERT_TRUE(found_test_udmabuf, "found_test_buffer");
+
+ free(line);
+ fclose(iter_file);
+ close(iter_fd);
+ close(udmabuf);
+ close(memfd);
+}
+
+void test_dmabuf_iter(void)
+{
+ struct dmabuf_iter *skel = NULL;
+ char buf[256];
+ int iter_fd;
+
+ skel = dmabuf_iter__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "dmabuf_iter__open_and_load"))
+ return;
+
+ if (!ASSERT_OK(dmabuf_iter__attach(skel), "skel_attach"))
+ goto destroy;
+
+ iter_fd = bpf_iter_create(bpf_link__fd(skel->links.dmabuf_collector));
+ if (!ASSERT_GE(iter_fd, 0, "iter_create"))
+ goto destroy;
+
+ memset(buf, 0, sizeof(buf));
+ while (read(iter_fd, buf, sizeof(buf) > 0)) {
+ /* Read out all contents */
+ }
+
+ /* Next reads should return 0 */
+ ASSERT_EQ(read(iter_fd, buf, sizeof(buf)), 0, "read");
+
+ if (test__start_subtest("check_udmabuf"))
+ subtest_dmabuf_iter_check_udmabuf(skel);
+
+ close(iter_fd);
+
+destroy:
+ dmabuf_iter__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/dmabuf_iter.c b/tools/testing/selftests/bpf/progs/dmabuf_iter.c
new file mode 100644
index 000000000000..b2af14ceb609
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/dmabuf_iter.c
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Google LLC */
+#include <vmlinux.h>
+#include <bpf/bpf_core_read.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+SEC("iter/dmabuf")
+int dmabuf_collector(struct bpf_iter__dmabuf *ctx)
+{
+ struct seq_file *seq = ctx->meta->seq;
+ const struct dma_buf *dmabuf = ctx->dmabuf;
+
+ if (dmabuf) {
+ size_t size;
+ unsigned long inode;
+ const char *name, *exp_name;
+
+ if (bpf_core_read(&size, sizeof(size), &dmabuf->size) ||
+ BPF_CORE_READ_INTO(&inode, dmabuf, file, f_inode, i_ino) ||
+ bpf_core_read(&name, sizeof(name), &dmabuf->name) ||
+ bpf_core_read(&exp_name, sizeof(exp_name), &dmabuf->exp_name))
+ return 1;
+
+ BPF_SEQ_PRINTF(seq, "ino:%lu size:%llu name:%s exp_name:%s\n",
+ inode, size, name ? name : "", exp_name ? exp_name : "");
+ }
+
+ return 0;
+}
--
2.49.0.604.gff1f9ca942-goog
next prev parent reply other threads:[~2025-04-14 22:53 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-14 22:52 [PATCH 0/4] Replace CONFIG_DMABUF_SYSFS_STATS with BPF T.J. Mercier
2025-04-14 22:52 ` [PATCH 1/4] dma-buf: Rename and expose debugfs symbols T.J. Mercier
2025-04-14 22:52 ` [PATCH 2/4] bpf: Add dmabuf iterator T.J. Mercier
2025-04-16 2:48 ` kernel test robot
2025-04-16 16:28 ` T.J. Mercier
2025-04-16 22:02 ` Song Liu
2025-04-16 22:51 ` T.J. Mercier
2025-04-16 23:08 ` Song Liu
2025-04-16 23:40 ` T.J. Mercier
2025-04-17 1:25 ` Song Liu
2025-04-17 2:09 ` T.J. Mercier
2025-04-17 4:56 ` Song Liu
2025-04-17 16:04 ` T.J. Mercier
2025-04-17 20:26 ` Song Liu
2025-04-18 15:25 ` T.J. Mercier
2025-04-21 18:12 ` Song Liu
2025-04-21 17:58 ` Song Liu
2025-04-21 20:40 ` T.J. Mercier
2025-04-21 23:38 ` Alexei Starovoitov
2025-04-22 19:57 ` T.J. Mercier
2025-04-22 23:00 ` Alexei Starovoitov
2025-04-23 17:16 ` T.J. Mercier
2025-04-14 22:52 ` T.J. Mercier [this message]
2025-04-21 18:08 ` [PATCH 3/4] selftests/bpf: Add test for dmabuf_iter Song Liu
2025-04-14 22:52 ` [RFC PATCH 4/4] RFC: dma-buf: Remove DMA-BUF statistics T.J. Mercier
2025-04-15 9:03 ` [PATCH 0/4] Replace CONFIG_DMABUF_SYSFS_STATS with BPF Christian König
2025-04-15 18:13 ` T.J. Mercier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250414225227.3642618-4-tjmercier@google.com \
--to=tjmercier@google.com \
--cc=andrii@kernel.org \
--cc=android-mm@google.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=christian.koenig@amd.com \
--cc=corbet@lwn.net \
--cc=daniel@iogearbox.net \
--cc=dri-devel@lists.freedesktop.org \
--cc=eddyz87@gmail.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=linaro-mm-sig@lists.linaro.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=sdf@fomichev.me \
--cc=simona@ffwll.ch \
--cc=skhan@linuxfoundation.org \
--cc=song@kernel.org \
--cc=sumit.semwal@linaro.org \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox