Skip to contentSkip to conversation

bpf: make dmabuf iterator stay drained after NULL

PATCH In review Kang Chen 5 participants 16 messages Nov 10 – Nov 13

Subsystems (5)
lore
Share & tools
lore

Kang Chen

author
#0 lore raw

PATCH 1/1 bpf: make dmabuf iterator stay drained after NULL

bpf_iter_dmabuf_next() uses kit->dmabuf == NULL to mean "iteration has not started". dma_buf_iter_next() also returns NULL at the end of the list, so a next() call after exhaustion restarts from the list head.

The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted dmabuf iteration therefore hands the BPF program a kernel pointer while the verifier believes R0 == 0.

Use an invalid pointer value as the drained sentinel so the iterator state keeps its one-pointer ABI, and skip dma_buf_put() for it.

Fixes 6eab7ac7c5ee ("bpf: Add open coded dmabuf iterator")
---
 kernel/bpf/dmabuf_iter.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
kernel/bpf/dmabuf_iter.c +22 -1
index cd500248abd9..a2b7c38f9ae6 100644
@@ -156,6 +156,13 @@ struct bpf_iter_dmabuf_kern {
 	struct dma_buf *dmabuf;
 } __aligned(8);
 
+/*
+ * The iterator state is one pointer wide because struct bpf_iter_dmabuf is
+ * part of the generated vmlinux.h ABI.  Use an invalid pointer value as the
+ * "drained" sentinel instead of growing the state.
+ */
+#define DMABUF_ITER_DONE	((struct dma_buf *)1L)
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
@@ -173,11 +180,25 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
+	/*
+	 * The verifier assumes that an iterator which returned NULL keeps
+	 * returning NULL (see process_iter_next_call()).  Without this check
+	 * the NULL left by the last dma_buf_iter_next() would be interpreted
+	 * as "not started" and the iteration would restart from the list head.
+	 */
+	if (kit->dmabuf == DMABUF_ITER_DONE)
+		return NULL;
+
 	if (kit->dmabuf)
 		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
 	else
 		kit->dmabuf = dma_buf_iter_begin();
 
+	if (!kit->dmabuf) {
+		kit->dmabuf = DMABUF_ITER_DONE;
+		return NULL;
+	}
+
 	return kit->dmabuf;
 }
 
@@ -185,7 +206,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-	if (kit->dmabuf)
+	if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_DONE)
 		dma_buf_put(kit->dmabuf);
 }
 
Signature · 1 line
2.34.1
#1 lore raw
TL;DR: Nit only: the new comment in bpf_iter_dmabuf_next() describes the pre-patch restart behavior, not the code it sits on; the review suggests rewording it to explain why the sentinel exists.
Quoted 1 line

bpf: make dmabuf iterator stay drained after NULL

TL;DR: Nit only: the new comment in bpf_iter_dmabuf_next() describes the pre-patch restart behavior, not the code it sits on; the review suggests rewording it to explain why the sentinel exists.

Quoted 3 lines
kernel/bpf/dmabuf_iter.c +0 -0

[ ... ]

Quoted 13 lines
kernel/bpf/dmabuf_iter.c +9 -0
@@ -173,11 +180,25 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
+	/*
+	 * The verifier assumes that an iterator which returned NULL keeps
+	 * returning NULL (see process_iter_next_call()).  Without this check
+	 * the NULL left by the last dma_buf_iter_next() would be interpreted
+	 * as "not started" and the iteration would restart from the list head.
+	 */
+	if (kit->dmabuf == DMABUF_ITER_DONE)
+		return NULL;
+

kernel/bpf/dmabuf_iter.c __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it) · line 201

Quoted 10 lines
kernel/bpf/dmabuf_iter.c +5 -0
 	if (kit->dmabuf)
 		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
 	else
 		kit->dmabuf = dma_buf_iter_begin();
 
+	if (!kit->dmabuf) {
+		kit->dmabuf = DMABUF_ITER_DONE;
+		return NULL;
+	}
+

This isn't a bug, but is the second sentence of the comment above describing the pre-patch behavior rather than the code it sits on?

With this patch no NULL is ever left in kit->dmabuf, since the block above replaces it with DMABUF_ITER_DONE:

    if (!kit->dmabuf) {
            kit->dmabuf = DMABUF_ITER_DONE;
            return NULL;
    }

If only the "if (kit->dmabuf == DMABUF_ITER_DONE)" check were removed, the iteration would not restart from the list head. The sentinel would take the "if (kit->dmabuf)" branch and be passed to dma_buf_iter_next((struct dma_buf *)1).

The restart described is the bug being fixed, which is already in the changelog. Could the comment instead say why the sentinel exists, for
example: once begin()/next() returns NULL, park the iterator at DMABUF_ITER_DONE so it is not mistaken for a fresh (NULL) iterator, and keep returning NULL as process_iter_next_call() requires?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/42982427559

#2 lore raw
In reply to Kang Chen

Adding T.J. to comment.

Regards,
Christian.

Quoted 75 lines · Kang Chen

bpf_iter_dmabuf_next() uses kit->dmabuf == NULL to mean "iteration has not started". dma_buf_iter_next() also returns NULL at the end of the list, so a next() call after exhaustion restarts from the list head.

The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted dmabuf iteration therefore hands the BPF program a kernel pointer while the verifier believes R0 == 0.

Use an invalid pointer value as the drained sentinel so the iterator state keeps its one-pointer ABI, and skip dma_buf_put() for it.

Fixes 6eab7ac7c5ee ("bpf: Add open coded dmabuf iterator")
---
 kernel/bpf/dmabuf_iter.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
kernel/bpf/dmabuf_iter.c +22 -2
index cd500248abd9..a2b7c38f9ae6 100644
@@ -156,6 +156,13 @@ struct bpf_iter_dmabuf_kern {
        struct dma_buf *dmabuf;
 } __aligned(8);
 
+/*
+ * The iterator state is one pointer wide because struct bpf_iter_dmabuf is
+ * part of the generated vmlinux.h ABI.  Use an invalid pointer value as the
+ * "drained" sentinel instead of growing the state.
+ */
+#define DMABUF_ITER_DONE       ((struct dma_buf *)1L)
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
@@ -173,11 +180,25 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
+       /*
+        * The verifier assumes that an iterator which returned NULL keeps
+        * returning NULL (see process_iter_next_call()).  Without this check
+        * the NULL left by the last dma_buf_iter_next() would be interpreted
+        * as "not started" and the iteration would restart from the list head.
+        */
+       if (kit->dmabuf == DMABUF_ITER_DONE)
+               return NULL;
+
        if (kit->dmabuf)
                kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
        else
                kit->dmabuf = dma_buf_iter_begin();
 
+       if (!kit->dmabuf) {
+               kit->dmabuf = DMABUF_ITER_DONE;
+               return NULL;
+       }
+
        return kit->dmabuf;
 }
 
@@ -185,7 +206,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-       if (kit->dmabuf)
+       if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_DONE)
                dma_buf_put(kit->dmabuf);
 }
 
--

2.34.1

#3 lore raw
Quoted 5 lines · Christian König

Adding T.J. to comment.

Regards,
Christian.

Thanks I've had to aggressively filter emails, so for the most part I only see stuff where I'm on To: (and I still can't read them all). I would have expected get_maintainer.pl to add me here.

Quoted 77 lines · Kang Chen
Quoted 75 lines

bpf_iter_dmabuf_next() uses kit->dmabuf == NULL to mean "iteration has not started". dma_buf_iter_next() also returns NULL at the end of the list, so a next() call after exhaustion restarts from the list head.

The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted dmabuf iteration therefore hands the BPF program a kernel pointer while the verifier believes R0 == 0.

Use an invalid pointer value as the drained sentinel so the iterator state keeps its one-pointer ABI, and skip dma_buf_put() for it.

Fixes 6eab7ac7c5ee ("bpf: Add open coded dmabuf iterator")
---
 kernel/bpf/dmabuf_iter.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
kernel/bpf/dmabuf_iter.c +22 -2
index cd500248abd9..a2b7c38f9ae6 100644
@@ -156,6 +156,13 @@ struct bpf_iter_dmabuf_kern {
        struct dma_buf *dmabuf;
 } __aligned(8);
 
+/*
+ * The iterator state is one pointer wide because struct bpf_iter_dmabuf is
+ * part of the generated vmlinux.h ABI.  Use an invalid pointer value as the
+ * "drained" sentinel instead of growing the state.
+ */
+#define DMABUF_ITER_DONE       ((struct dma_buf *)1L)
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
@@ -173,11 +180,25 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
+       /*
+        * The verifier assumes that an iterator which returned NULL keeps
+        * returning NULL (see process_iter_next_call()).  Without this check
+        * the NULL left by the last dma_buf_iter_next() would be interpreted
+        * as "not started" and the iteration would restart from the list head.
+        */
+       if (kit->dmabuf == DMABUF_ITER_DONE)
+               return NULL;
+
        if (kit->dmabuf)
                kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
        else
                kit->dmabuf = dma_buf_iter_begin();
 
+       if (!kit->dmabuf) {
+               kit->dmabuf = DMABUF_ITER_DONE;
+               return NULL;
+       }
+
        return kit->dmabuf;
 }
 
@@ -185,7 +206,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-       if (kit->dmabuf)
+       if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_DONE)
                dma_buf_put(kit->dmabuf);
 }
 
--

2.34.1

#4 lore raw

PATCH 1/1 selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

In reply to Kang Chen

The BPF iterator docs in bpf_iterators.rst specify that the next function of an open-coded iterator should keep returning NULL after it has been returned once.

Add a subtest that checks this for the dmabuf iterator. It drains an iterator, calls bpf_iter_dmabuf_next() once more and checks that the result is NULL. The dma-bufs the test creates keep the list from being empty, so an iterator that starts over would return one of them.

The existing open_coded subtest uses bpf_for_each(), which stops at the first NULL and never calls next() on a drained iterator.

---
This depends on Kang Chen's fix and fails without it:
https://lore.kernel.org/bpf/[email protected]/
Kang, feel free to carry it as 2/2 in your v2.

 .../selftests/bpf/prog_tests/dmabuf_iter.c    | 19 +++++++++++++++++++
 .../testing/selftests/bpf/progs/dmabuf_iter.c | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c +19 -0
index 1a5dc1adae73..c1f81b5dccdc 100644
@@ -265,6 +265,23 @@ static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int m
 	} while (bpf_map_get_next_key(map_fd, key, key));
 }
 
+static void subtest_dmabuf_iter_check_next_after_drain(struct dmabuf_iter *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err, fd;
+
+	fd = bpf_program__fd(skel->progs.iter_dmabuf_next_after_drain);
+
+	err = bpf_prog_test_run_opts(fd, &topts);
+	if (!ASSERT_OK(err, "test_run_opts err"))
+		return;
+	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+		return;
+
+	/* The test buffers exist, so a restarted iteration would find one */
+	ASSERT_EQ(skel->data->next_after_drain, 0, "next_after_drain");
+}
+
 void test_dmabuf_iter(void)
 {
 	struct dmabuf_iter *skel = NULL;
@@ -315,6 +332,8 @@ void test_dmabuf_iter(void)
 	}
 	if (test__start_subtest("open_coded"))
 		subtest_dmabuf_iter_check_open_coded(skel, map_fd);
+	if (test__start_subtest("next_after_drain"))
+		subtest_dmabuf_iter_check_next_after_drain(skel);
 
 destroy:
 	destroy_test_buffers();
tools/testing/selftests/bpf/progs/dmabuf_iter.c +19 -0
index 9cbb7442646e..d5421b75c39c 100644
@@ -99,3 +99,22 @@ int iter_dmabuf_for_each(const void *ctx)
 
 	return 0;
 }
+
+/* Result of a bpf_iter_dmabuf_next() call after the iterator was drained */
+__u64 next_after_drain = -1;
+
+SEC("syscall")
+int iter_dmabuf_next_after_drain(const void *ctx)
+{
+	struct bpf_iter_dmabuf it;
+
+	bpf_iter_dmabuf_new(&it);
+	while (bpf_iter_dmabuf_next(&it))
+		;
+
+	/* Store the raw value; the verifier would remove a branch on it */
+	next_after_drain = (__u64)bpf_iter_dmabuf_next(&it);
+	bpf_iter_dmabuf_destroy(&it);
+
+	return 0;
+}
Signature · 1 line
2.56.0.567.gdb80ae8cd5-goog
Acked-by on v1 1/1
#5 lore raw

Re: [PATCH bpf] selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

Quoted 9 lines · T.J. Mercier

The BPF iterator docs in bpf_iterators.rst specify that the next function of an open-coded iterator should keep returning NULL after it has been returned once.

Add a subtest that checks this for the dmabuf iterator. It drains an iterator, calls bpf_iter_dmabuf_next() once more and checks that the result is NULL. The dma-bufs the test creates keep the list from being empty, so an iterator that starts over would return one of them.

Quoted 4 lines

The existing open_coded subtest uses bpf_for_each(), which stops at the first NULL and never calls next() on a drained iterator.

Let me know if you already have drm-misc commit rights.

Regards,
Christian.

Quoted 73 lines

---
This depends on Kang Chen's fix and fails without it:
https://lore.kernel.org/bpf/[email protected]/
Kang, feel free to carry it as 2/2 in your v2.

 .../selftests/bpf/prog_tests/dmabuf_iter.c    | 19 +++++++++++++++++++
 .../testing/selftests/bpf/progs/dmabuf_iter.c | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c +19 -0
index 1a5dc1adae73..c1f81b5dccdc 100644
@@ -265,6 +265,23 @@ static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int m
 	} while (bpf_map_get_next_key(map_fd, key, key));
 }
 
+static void subtest_dmabuf_iter_check_next_after_drain(struct dmabuf_iter *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err, fd;
+
+	fd = bpf_program__fd(skel->progs.iter_dmabuf_next_after_drain);
+
+	err = bpf_prog_test_run_opts(fd, &topts);
+	if (!ASSERT_OK(err, "test_run_opts err"))
+		return;
+	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+		return;
+
+	/* The test buffers exist, so a restarted iteration would find one */
+	ASSERT_EQ(skel->data->next_after_drain, 0, "next_after_drain");
+}
+
 void test_dmabuf_iter(void)
 {
 	struct dmabuf_iter *skel = NULL;
@@ -315,6 +332,8 @@ void test_dmabuf_iter(void)
 	}
 	if (test__start_subtest("open_coded"))
 		subtest_dmabuf_iter_check_open_coded(skel, map_fd);
+	if (test__start_subtest("next_after_drain"))
+		subtest_dmabuf_iter_check_next_after_drain(skel);
 
 destroy:
 	destroy_test_buffers();
tools/testing/selftests/bpf/progs/dmabuf_iter.c +19 -0
index 9cbb7442646e..d5421b75c39c 100644
@@ -99,3 +99,22 @@ int iter_dmabuf_for_each(const void *ctx)
 
 	return 0;
 }
+
+/* Result of a bpf_iter_dmabuf_next() call after the iterator was drained */
+__u64 next_after_drain = -1;
+
+SEC("syscall")
+int iter_dmabuf_next_after_drain(const void *ctx)
+{
+	struct bpf_iter_dmabuf it;
+
+	bpf_iter_dmabuf_new(&it);
+	while (bpf_iter_dmabuf_next(&it))
+		;
+
+	/* Store the raw value; the verifier would remove a branch on it */
+	next_after_drain = (__u64)bpf_iter_dmabuf_next(&it);
+	bpf_iter_dmabuf_destroy(&it);
+
+	return 0;
+}
#6 lore raw

Re: [PATCH bpf] selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

Quoted 14 lines · Christian König
Quoted 12 lines · T.J. Mercier

The BPF iterator docs in bpf_iterators.rst specify that the next function of an open-coded iterator should keep returning NULL after it has been returned once.

Add a subtest that checks this for the dmabuf iterator. It drains an iterator, calls bpf_iter_dmabuf_next() once more and checks that the result is NULL. The dma-bufs the test creates keep the list from being empty, so an iterator that starts over would return one of them.

The existing open_coded subtest uses bpf_for_each(), which stops at the first NULL and never calls next() on a drained iterator.

Quoted 8 lines
Quoted 1 line

Let me know if you already have drm-misc commit rights.

Regards,
Christian.

Thanks! Still waiting but no rush.

Quoted 74 lines
Quoted 73 lines

---
This depends on Kang Chen's fix and fails without it:
https://lore.kernel.org/bpf/[email protected]/
Kang, feel free to carry it as 2/2 in your v2.

 .../selftests/bpf/prog_tests/dmabuf_iter.c    | 19 +++++++++++++++++++
 .../testing/selftests/bpf/progs/dmabuf_iter.c | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c +19 -0
index 1a5dc1adae73..c1f81b5dccdc 100644
@@ -265,6 +265,23 @@ static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int m
      } while (bpf_map_get_next_key(map_fd, key, key));
 }
 
+static void subtest_dmabuf_iter_check_next_after_drain(struct dmabuf_iter *skel)
+{
+     LIBBPF_OPTS(bpf_test_run_opts, topts);
+     int err, fd;
+
+     fd = bpf_program__fd(skel->progs.iter_dmabuf_next_after_drain);
+
+     err = bpf_prog_test_run_opts(fd, &topts);
+     if (!ASSERT_OK(err, "test_run_opts err"))
+             return;
+     if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+             return;
+
+     /* The test buffers exist, so a restarted iteration would find one */
+     ASSERT_EQ(skel->data->next_after_drain, 0, "next_after_drain");
+}
+
 void test_dmabuf_iter(void)
 {
      struct dmabuf_iter *skel = NULL;
@@ -315,6 +332,8 @@ void test_dmabuf_iter(void)
      }
      if (test__start_subtest("open_coded"))
              subtest_dmabuf_iter_check_open_coded(skel, map_fd);
+     if (test__start_subtest("next_after_drain"))
+             subtest_dmabuf_iter_check_next_after_drain(skel);
 
 destroy:
      destroy_test_buffers();
tools/testing/selftests/bpf/progs/dmabuf_iter.c +19 -0
index 9cbb7442646e..d5421b75c39c 100644
@@ -99,3 +99,22 @@ int iter_dmabuf_for_each(const void *ctx)
 
      return 0;
 }
+
+/* Result of a bpf_iter_dmabuf_next() call after the iterator was drained */
+__u64 next_after_drain = -1;
+
+SEC("syscall")
+int iter_dmabuf_next_after_drain(const void *ctx)
+{
+     struct bpf_iter_dmabuf it;
+
+     bpf_iter_dmabuf_new(&it);
+     while (bpf_iter_dmabuf_next(&it))
+             ;
+
+     /* Store the raw value; the verifier would remove a branch on it */
+     next_after_drain = (__u64)bpf_iter_dmabuf_next(&it);
+     bpf_iter_dmabuf_destroy(&it);
+
+     return 0;
+}
#7 lore raw
In reply to Kang Chen
Quoted 11 lines · Kang Chen

bpf_iter_dmabuf_next() uses kit->dmabuf == NULL to mean "iteration has not started". dma_buf_iter_next() also returns NULL at the end of the list, so a next() call after exhaustion restarts from the list head.

The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted dmabuf iteration therefore hands the BPF program a kernel pointer while the verifier believes R0 == 0.

Quoted 5 lines

Use an invalid pointer value as the drained sentinel so the iterator state keeps its one-pointer ABI, and skip dma_buf_put() for it.

Fixes 6eab7ac7c5ee ("bpf: Add open coded dmabuf iterator")

Thanks for this fix. The cgroup (bpf_iter_css_next) and scx_dsq (bpf_iter_scx_dsq_next) iterators look like they also have this problem.

Quoted 9 lines
---
 kernel/bpf/dmabuf_iter.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
kernel/bpf/dmabuf_iter.c +0 -0
index cd500248abd9..a2b7c38f9ae6 100644

kernel/bpf/dmabuf_iter.c struct bpf_iter_dmabuf_kern { · line 163

Quoted 8 lines
kernel/bpf/dmabuf_iter.c +5 -0
@@ -156,6 +156,13 @@ struct bpf_iter_dmabuf_kern {
        struct dma_buf *dmabuf;
 } __aligned(8);
 
+/*
+ * The iterator state is one pointer wide because struct bpf_iter_dmabuf is
+ * part of the generated vmlinux.h ABI.  Use an invalid pointer value as the
+ * "drained" sentinel instead of growing the state.
+ */

This comment explains why this patch uses a sentinel instead of growing the state, but that doesn't help anyone reading this code in the future. This belongs in the commit message, not a code comment. The real constraint is that the size of bpf_iter_dmabuf is user-visible API, so it can't grow without breaking backwards compatibility.

kernel/bpf/dmabuf_iter.c struct bpf_iter_dmabuf_kern { · line 164

Quoted 1 line
kernel/bpf/dmabuf_iter.c +1 -0
+#define DMABUF_ITER_DONE       ((struct dma_buf *)1L)

I would prefer this to be DMABUF_ITER_START to mirror the kmem_cache iterator. Then the second if (!kit->dmabuf) block in bpf_iter_dmabuf_next() below could also be removed.

Quoted 5 lines
kernel/bpf/dmabuf_iter.c +1 -0
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)

kernel/bpf/dmabuf_iter.c __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it) · line 188

Quoted 9 lines
kernel/bpf/dmabuf_iter.c +6 -0
@@ -173,11 +180,25 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
+       /*
+        * The verifier assumes that an iterator which returned NULL keeps
+        * returning NULL (see process_iter_next_call()).  Without this check
+        * the NULL left by the last dma_buf_iter_next() would be interpreted
+        * as "not started" and the iteration would restart from the list head.
+        */

Same here, this is commit message material. If you want to add a code comment, referencing the rule from bpf_iterators.rst should be enough:

/* Once NULL is returned, subsequent next calls should keep returning NULL. */

Quoted 26 lines
kernel/bpf/dmabuf_iter.c +9 -2
+       if (kit->dmabuf == DMABUF_ITER_DONE)
+               return NULL;
+
        if (kit->dmabuf)
                kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
        else
                kit->dmabuf = dma_buf_iter_begin();
 
+       if (!kit->dmabuf) {
+               kit->dmabuf = DMABUF_ITER_DONE;
+               return NULL;
+       }
+
        return kit->dmabuf;
 }
 
@@ -185,7 +206,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-       if (kit->dmabuf)
+       if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_DONE)
                dma_buf_put(kit->dmabuf);
 }
 
--
Quoted 1 line

2.34.1

I think we should also add a test for this. I've sent one as a reply to this thread. It fails without your fix and passes with it. Feel free to add it as 2/2 in v2.

#8 lore raw
In reply to Kang Chen

Review

Quoted 6 lines · Kang Chen
(quoted diff) +6 -0
+/*
+ * The iterator state is one pointer wide because struct bpf_iter_dmabuf is
+ * part of the generated vmlinux.h ABI.  Use an invalid pointer value as the
+ * "drained" sentinel instead of growing the state.
+ */
+#define DMABUF_ITER_DONE	((struct dma_buf *)1L)

+1 to T.J.'s point re: DMABUF_ITER_START. bpf_iter_kmem_cache_next() does it that way. dma_buf_iter_next() leaves NULL at the end of the list. Let NULL mean drained. Then no need for the 2nd store in next() and for the long comments.

kernel/bpf/dmabuf_iter.c +11 -5
index cd50024..1bef87b 100644
@@ -156,6 +156,8 @@ struct bpf_iter_dmabuf_kern {
 	struct dma_buf *dmabuf;
 } __aligned(8);
 
+#define DMABUF_ITER_START	((struct dma_buf *)1L)
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
@@ -165,7 +167,7 @@ __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
 	BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
 	BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
 
-	kit->dmabuf = NULL;
+	kit->dmabuf = DMABUF_ITER_START;
 	return 0;
 }
 
@@ -173,10 +175,14 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-	if (kit->dmabuf)
-		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
-	else
+	/* Once NULL is returned, next() has to keep returning NULL */
+	if (!kit->dmabuf)
+		return NULL;
+
+	if (kit->dmabuf == DMABUF_ITER_START)
 		kit->dmabuf = dma_buf_iter_begin();
+	else
+		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
 
 	return kit->dmabuf;
 }
@@ -185,7 +191,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-	if (kit->dmabuf)
+	if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_START)
 		dma_buf_put(kit->dmabuf);
 }
 

Pls take T.J.'s selftest as patch 2 and tag the respin as [PATCH bpf v2].

Also you sent fixes for css and scx_dsq.
bpf_iter_task_vma_next() has the same problem.
It doesn't hold mmap_lock between the calls since
commit bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks"):

Pls fix it too.

pw-bot: cr

author
#9 lore raw

PATCH v2 1/2 bpf: make dmabuf iterator stay drained after NULL

In reply to Kang Chen

bpf_iter_dmabuf_next() uses kit->dmabuf == NULL to mean "iteration has not started". dma_buf_iter_next() also returns NULL at the end of the list, so a next() call after exhaustion restarts from the list head.

The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted dmabuf iteration therefore hands the BPF program a kernel pointer while the verifier believes R0 == 0.

NULL now means drained. Initialize the state to DMABUF_ITER_START and translate it to dma_buf_iter_begin() on the first next(). The iterator state is BPF-visible ABI (one pointer wide), so use an invalid pointer sentinel instead of growing it.

Fixes 6eab7ac7c5ee ("bpf: Add open coded dmabuf iterator")

---
v2:
- Use DMABUF_ITER_START; NULL now means drained.
- Move the ABI explanation to the commit message.

 kernel/bpf/dmabuf_iter.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
kernel/bpf/dmabuf_iter.c +12 -5
index cd500248abd9..390f6d730a67 100644
@@ -156,6 +156,9 @@ struct bpf_iter_dmabuf_kern {
 	struct dma_buf *dmabuf;
 } __aligned(8);
 
+/* NULL means drained; START means "not started yet". */
+#define DMABUF_ITER_START	((struct dma_buf *)1L)
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
@@ -165,7 +168,7 @@ __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
 	BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
 	BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
 
-	kit->dmabuf = NULL;
+	kit->dmabuf = DMABUF_ITER_START;
 	return 0;
 }
 
@@ -173,10 +176,14 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-	if (kit->dmabuf)
-		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
-	else
+	/* Once NULL is returned, next() has to keep returning NULL. */
+	if (!kit->dmabuf)
+		return NULL;
+
+	if (kit->dmabuf == DMABUF_ITER_START)
 		kit->dmabuf = dma_buf_iter_begin();
+	else
+		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
 
 	return kit->dmabuf;
 }
@@ -185,7 +192,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-	if (kit->dmabuf)
+	if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_START)
 		dma_buf_put(kit->dmabuf);
 }
 
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH v2 2/2 selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

The BPF iterator docs in bpf_iterators.rst specify that the next function of an open-coded iterator should keep returning NULL after it has been returned once.

Add a subtest that checks this for the dmabuf iterator. It drains an iterator, calls bpf_iter_dmabuf_next() once more and checks that the result is NULL. The dma-bufs the test creates keep the list from being empty, so an iterator that starts over would return one of them.

The existing open_coded subtest uses bpf_for_each(), which stops at the first NULL and never calls next() on a drained iterator.

The selftest was originally posted by T.J. Mercier in the v1 thread:

---
v2:
- New in v2; T.J. Mercier's selftest, credited in the commit message.

 .../selftests/bpf/prog_tests/dmabuf_iter.c    | 19 +++++++++++++++++++
 .../testing/selftests/bpf/progs/dmabuf_iter.c | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c +19 -0
index 1a5dc1adae73..c1f81b5dccdc 100644
@@ -265,6 +265,23 @@ static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int m
 	} while (bpf_map_get_next_key(map_fd, key, key));
 }
 
+static void subtest_dmabuf_iter_check_next_after_drain(struct dmabuf_iter *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err, fd;
+
+	fd = bpf_program__fd(skel->progs.iter_dmabuf_next_after_drain);
+
+	err = bpf_prog_test_run_opts(fd, &topts);
+	if (!ASSERT_OK(err, "test_run_opts err"))
+		return;
+	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+		return;
+
+	/* The test buffers exist, so a restarted iteration would find one */
+	ASSERT_EQ(skel->data->next_after_drain, 0, "next_after_drain");
+}
+
 void test_dmabuf_iter(void)
 {
 	struct dmabuf_iter *skel = NULL;
@@ -315,6 +332,8 @@ void test_dmabuf_iter(void)
 	}
 	if (test__start_subtest("open_coded"))
 		subtest_dmabuf_iter_check_open_coded(skel, map_fd);
+	if (test__start_subtest("next_after_drain"))
+		subtest_dmabuf_iter_check_next_after_drain(skel);
 
 destroy:
 	destroy_test_buffers();
tools/testing/selftests/bpf/progs/dmabuf_iter.c +19 -0
index 9cbb7442646e..d5421b75c39c 100644
@@ -99,3 +99,22 @@ int iter_dmabuf_for_each(const void *ctx)
 
 	return 0;
 }
+
+/* Result of a bpf_iter_dmabuf_next() call after the iterator was drained */
+__u64 next_after_drain = -1;
+
+SEC("syscall")
+int iter_dmabuf_next_after_drain(const void *ctx)
+{
+	struct bpf_iter_dmabuf it;
+
+	bpf_iter_dmabuf_new(&it);
+	while (bpf_iter_dmabuf_next(&it))
+		;
+
+	/* Store the raw value; the verifier would remove a branch on it */
+	next_after_drain = (__u64)bpf_iter_dmabuf_next(&it);
+	bpf_iter_dmabuf_destroy(&it);
+
+	return 0;
+}
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH bpf v2 2/2] selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

TL;DR: Commit message only: the test is credited to T.J. Mercier, but no From:, Co-developed-by: or Signed-off-by: names him; the review asks whether those tags should be added.
Quoted 1 line

selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

TL;DR: Commit message only: the test is credited to T.J. Mercier, but no From:, Co-developed-by: or Signed-off-by: names him; the review asks whether those tags should be added.

Quoted 12 lines

selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

The BPF iterator docs in bpf_iterators.rst specify that the next function of an open-coded iterator should keep returning NULL after it has been returned once.
Add a subtest that checks this for the dmabuf iterator. It drains an iterator, calls bpf_iter_dmabuf_next() once more and checks that the result is NULL. The dma-bufs the test creates keep the list from being empty, so an iterator that starts over would return one of them. The existing open_coded subtest uses bpf_for_each(), which stops at the first NULL and never calls next() on a drained iterator. The selftest was originally posted by T.J. Mercier in the v1 thread:

Quoted 2 lines

The changelog says the selftest was originally posted by T.J. Mercier, but the only trailer is the submitter's Signed-off-by. Nothing names the original author: there is no From:, no Co-developed-by: with a matching Signed-off-by:, and no Suggested-by:.

Documentation/process/submitting-patches.rst says Co-developed-by: is used to give attribution to co-authors, and that every Co-developed-by: must be immediately followed by a Signed-off-by: of the associated co-author.

Should this carry Co-developed-by: and Signed-off-by: tags from T.J. Mercier (or have him as the From: author), depending on what he agreed to in the v1 thread?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/43356758506

author
#12 lore raw

PATCH v3 1/2 bpf: make dmabuf iterator stay drained after NULL

In reply to Kang Chen

bpf_iter_dmabuf_next() uses kit->dmabuf == NULL to mean "iteration has not started". dma_buf_iter_next() also returns NULL at the end of the list, so a next() call after exhaustion restarts from the list head.

The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted dmabuf iteration therefore hands the BPF program a kernel pointer while the verifier believes R0 == 0.

NULL now means drained. Initialize the state to DMABUF_ITER_START and translate it to dma_buf_iter_begin() on the first next(). The iterator state is BPF-visible ABI (one pointer wide), so use an invalid pointer sentinel instead of growing it.

Fixes 6eab7ac7c5ee ("bpf: Add open coded dmabuf iterator")

---
v3:
- No change from v2; reposted with the updated 2/2 attribution.

 kernel/bpf/dmabuf_iter.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
kernel/bpf/dmabuf_iter.c +12 -5
index cd500248abd9..390f6d730a67 100644
@@ -156,6 +156,9 @@ struct bpf_iter_dmabuf_kern {
 	struct dma_buf *dmabuf;
 } __aligned(8);
 
+/* NULL means drained; START means "not started yet". */
+#define DMABUF_ITER_START	((struct dma_buf *)1L)
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
@@ -165,7 +168,7 @@ __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
 	BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
 	BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
 
-	kit->dmabuf = NULL;
+	kit->dmabuf = DMABUF_ITER_START;
 	return 0;
 }
 
@@ -173,10 +176,14 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-	if (kit->dmabuf)
-		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
-	else
+	/* Once NULL is returned, next() has to keep returning NULL. */
+	if (!kit->dmabuf)
+		return NULL;
+
+	if (kit->dmabuf == DMABUF_ITER_START)
 		kit->dmabuf = dma_buf_iter_begin();
+	else
+		kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
 
 	return kit->dmabuf;
 }
@@ -185,7 +192,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
 	struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-	if (kit->dmabuf)
+	if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_START)
 		dma_buf_put(kit->dmabuf);
 }
 
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH v3 2/2 selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

The BPF iterator docs in bpf_iterators.rst specify that the next function of an open-coded iterator should keep returning NULL after it has been returned once.

Add a subtest that checks this for the dmabuf iterator. It drains an iterator, calls bpf_iter_dmabuf_next() once more and checks that the result is NULL. The dma-bufs the test creates keep the list from being empty, so an iterator that starts over would return one of them.

The existing open_coded subtest uses bpf_for_each(), which stops at the first NULL and never calls next() on a drained iterator.

The selftest was originally posted by T.J. Mercier in the v1 thread:

---
v3:
- Add T.J. Mercier's Co-developed-by and matching Signed-off-by.
- No code change.

 .../selftests/bpf/prog_tests/dmabuf_iter.c    | 19 +++++++++++++++++++
 .../testing/selftests/bpf/progs/dmabuf_iter.c | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c +19 -0
index 1a5dc1adae73..c1f81b5dccdc 100644
@@ -265,6 +265,23 @@ static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int m
 	} while (bpf_map_get_next_key(map_fd, key, key));
 }
 
+static void subtest_dmabuf_iter_check_next_after_drain(struct dmabuf_iter *skel)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	int err, fd;
+
+	fd = bpf_program__fd(skel->progs.iter_dmabuf_next_after_drain);
+
+	err = bpf_prog_test_run_opts(fd, &topts);
+	if (!ASSERT_OK(err, "test_run_opts err"))
+		return;
+	if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+		return;
+
+	/* The test buffers exist, so a restarted iteration would find one */
+	ASSERT_EQ(skel->data->next_after_drain, 0, "next_after_drain");
+}
+
 void test_dmabuf_iter(void)
 {
 	struct dmabuf_iter *skel = NULL;
@@ -315,6 +332,8 @@ void test_dmabuf_iter(void)
 	}
 	if (test__start_subtest("open_coded"))
 		subtest_dmabuf_iter_check_open_coded(skel, map_fd);
+	if (test__start_subtest("next_after_drain"))
+		subtest_dmabuf_iter_check_next_after_drain(skel);
 
 destroy:
 	destroy_test_buffers();
tools/testing/selftests/bpf/progs/dmabuf_iter.c +19 -0
index 9cbb7442646e..d5421b75c39c 100644
@@ -99,3 +99,22 @@ int iter_dmabuf_for_each(const void *ctx)
 
 	return 0;
 }
+
+/* Result of a bpf_iter_dmabuf_next() call after the iterator was drained */
+__u64 next_after_drain = -1;
+
+SEC("syscall")
+int iter_dmabuf_next_after_drain(const void *ctx)
+{
+	struct bpf_iter_dmabuf it;
+
+	bpf_iter_dmabuf_new(&it);
+	while (bpf_iter_dmabuf_next(&it))
+		;
+
+	/* Store the raw value; the verifier would remove a branch on it */
+	next_after_drain = (__u64)bpf_iter_dmabuf_next(&it);
+	bpf_iter_dmabuf_destroy(&it);
+
+	return 0;
+}
Signature · 1 line
2.34.1
Acked-by Christian König on v3 2/2
#14 lore raw

Re: [PATCH bpf v3 2/2] selftests/bpf: Test that a drained dmabuf iterator keeps returning NULL

Quoted 13 lines · Kang Chen

The BPF iterator docs in bpf_iterators.rst specify that the next function of an open-coded iterator should keep returning NULL after it has been returned once.

Add a subtest that checks this for the dmabuf iterator. It drains an iterator, calls bpf_iter_dmabuf_next() once more and checks that the result is NULL. The dma-bufs the test creates keep the list from being empty, so an iterator that starts over would return one of them.

The existing open_coded subtest uses bpf_for_each(), which stops at the first NULL and never calls next() on a drained iterator.

Quoted 7 lines

The selftest was originally posted by T.J. Mercier in the v1 thread:

This should be:
From: T.J. Mercier <[email protected]>

<unmodified original commit message>

For example see:
https://lore.kernel.org/all/20261104-v2_20230123_tjmercier_google_com-v4-1-1819a017000b@redhat.com/
and: https://docs.kernel.org/process/submitting-patches.html#from-line

Quoted 76 lines

---
v3:
- Add T.J. Mercier's Co-developed-by and matching Signed-off-by.
- No code change.

 .../selftests/bpf/prog_tests/dmabuf_iter.c    | 19 +++++++++++++++++++
 .../testing/selftests/bpf/progs/dmabuf_iter.c | 19 +++++++++++++++++++
 2 files changed, 38 insertions(+)
tools/testing/selftests/bpf/prog_tests/dmabuf_iter.c +19 -0
index 1a5dc1adae73..c1f81b5dccdc 100644
@@ -265,6 +265,23 @@ static void subtest_dmabuf_iter_check_open_coded(struct dmabuf_iter *skel, int m
        } while (bpf_map_get_next_key(map_fd, key, key));
 }
 
+static void subtest_dmabuf_iter_check_next_after_drain(struct dmabuf_iter *skel)
+{
+       LIBBPF_OPTS(bpf_test_run_opts, topts);
+       int err, fd;
+
+       fd = bpf_program__fd(skel->progs.iter_dmabuf_next_after_drain);
+
+       err = bpf_prog_test_run_opts(fd, &topts);
+       if (!ASSERT_OK(err, "test_run_opts err"))
+               return;
+       if (!ASSERT_OK(topts.retval, "test_run_opts retval"))
+               return;
+
+       /* The test buffers exist, so a restarted iteration would find one */
+       ASSERT_EQ(skel->data->next_after_drain, 0, "next_after_drain");
+}
+
 void test_dmabuf_iter(void)
 {
        struct dmabuf_iter *skel = NULL;
@@ -315,6 +332,8 @@ void test_dmabuf_iter(void)
        }
        if (test__start_subtest("open_coded"))
                subtest_dmabuf_iter_check_open_coded(skel, map_fd);
+       if (test__start_subtest("next_after_drain"))
+               subtest_dmabuf_iter_check_next_after_drain(skel);
 
 destroy:
        destroy_test_buffers();
tools/testing/selftests/bpf/progs/dmabuf_iter.c +19 -1
index 9cbb7442646e..d5421b75c39c 100644
@@ -99,3 +99,22 @@ int iter_dmabuf_for_each(const void *ctx)
 
        return 0;
 }
+
+/* Result of a bpf_iter_dmabuf_next() call after the iterator was drained */
+__u64 next_after_drain = -1;
+
+SEC("syscall")
+int iter_dmabuf_next_after_drain(const void *ctx)
+{
+       struct bpf_iter_dmabuf it;
+
+       bpf_iter_dmabuf_new(&it);
+       while (bpf_iter_dmabuf_next(&it))
+               ;
+
+       /* Store the raw value; the verifier would remove a branch on it */
+       next_after_drain = (__u64)bpf_iter_dmabuf_next(&it);
+       bpf_iter_dmabuf_destroy(&it);
+
+       return 0;
+}
--

2.34.1

Reviewed-by on v3 1/2
#15 lore raw
In reply to Kang Chen
Quoted 11 lines · Kang Chen

bpf_iter_dmabuf_next() uses kit->dmabuf == NULL to mean "iteration has not started". dma_buf_iter_next() also returns NULL at the end of the list, so a next() call after exhaustion restarts from the list head.

The verifier relies on the iterator contract that once next() returns NULL it keeps returning NULL: process_iter_next_call() marks the iterator DRAINED and treats the result of later next() calls as the constant zero. A restarted dmabuf iteration therefore hands the BPF program a kernel pointer while the verifier believes R0 == 0.

Quoted 7 lines

NULL now means drained. Initialize the state to DMABUF_ITER_START and translate it to dma_buf_iter_begin() on the first next(). The iterator state is BPF-visible ABI (one pointer wide), so use an invalid pointer sentinel instead of growing it.

Fixes 6eab7ac7c5ee ("bpf: Add open coded dmabuf iterator")
Quoted 60 lines

---
v3:
- No change from v2; reposted with the updated 2/2 attribution.

 kernel/bpf/dmabuf_iter.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
kernel/bpf/dmabuf_iter.c +12 -6
index cd500248abd9..390f6d730a67 100644
@@ -156,6 +156,9 @@ struct bpf_iter_dmabuf_kern {
        struct dma_buf *dmabuf;
 } __aligned(8);
 
+/* NULL means drained; START means "not started yet". */
+#define DMABUF_ITER_START      ((struct dma_buf *)1L)
+
 __bpf_kfunc_start_defs();
 
 __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
@@ -165,7 +168,7 @@ __bpf_kfunc int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it)
        BUILD_BUG_ON(sizeof(*kit) > sizeof(*it));
        BUILD_BUG_ON(__alignof__(*kit) != __alignof__(*it));
 
-       kit->dmabuf = NULL;
+       kit->dmabuf = DMABUF_ITER_START;
        return 0;
 }
 
@@ -173,10 +176,14 @@ __bpf_kfunc struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-       if (kit->dmabuf)
-               kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
-       else
+       /* Once NULL is returned, next() has to keep returning NULL. */
+       if (!kit->dmabuf)
+               return NULL;
+
+       if (kit->dmabuf == DMABUF_ITER_START)
                kit->dmabuf = dma_buf_iter_begin();
+       else
+               kit->dmabuf = dma_buf_iter_next(kit->dmabuf);
 
        return kit->dmabuf;
 }
@@ -185,7 +192,7 @@ __bpf_kfunc void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it)
 {
        struct bpf_iter_dmabuf_kern *kit = (void *)it;
 
-       if (kit->dmabuf)
+       if (kit->dmabuf && kit->dmabuf != DMABUF_ITER_START)
                dma_buf_put(kit->dmabuf);
 }
 
--

2.34.1