Skip to contentSkip to conversation

Fix bpf qdisc bugs and clean up

PATCH Applied Amery Hung 3 participants 8 messages May 2, 2025

Subsystems (6)
lore
Share & tools
lore

Amery Hung

author
#0 lore raw

v2 cover Fix bpf qdisc bugs and clean up

This patchset fixes the following bugs in bpf qdisc and clean up the selftest.

- A null-pointer dereference can happen in qdisc_watchdog_cancel() if the timer is not initialized when 1) .init is not defined by user so init prologue is not generated. 2) .init fails and qdisc_create() calls .destroy

- bpf qdisc fails to attach to mq/mqprio when being set as the default qdisc due to failed qdisc_lookup() in init prologue

v2
- Rebase to bpf-next/net
- Fix erroneous commit messages
- Fix and simplify selftests cleanup
v1: https://lore.kernel.org/bpf/[email protected]/

Amery Hung (5):
bpf: net_sched: Fix bpf qdisc init prologue when set as default qdisc selftests/bpf: Test setting and creating bpf qdisc as default qdisc
bpf: net_sched: Make some Qdisc_ops ops mandatory selftests/bpf: Test attaching a bpf qdisc with incomplete operators selftests/bpf: Cleanup bpf qdisc selftests

 net/sched/bpf_qdisc.c                         |  24 +++-
 .../selftests/bpf/prog_tests/bpf_qdisc.c      | 120 +++++++++++++-----
 .../selftests/bpf/progs/bpf_qdisc_common.h    |   6 -
 .../bpf/progs/bpf_qdisc_fail__incompl_ops.c   |  41 ++++++
 .../selftests/bpf/progs/bpf_qdisc_fifo.c      |   9 ++
 .../selftests/bpf/progs/bpf_qdisc_fq.c        |   6 +
 6 files changed, 161 insertions(+), 45 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c
Signature · 1 line
2.47.1
author
#1 lore raw

PATCH v2 1/5 bpf: net_sched: Fix bpf qdisc init prologue when set as default qdisc

Allow .init to proceed if qdisc_lookup() returns NULL as it only happens when called by qdisc_create_dflt() in mq/mqprio_init and the parent qdisc has not been added to qdisc_hash yet. In qdisc_create(), the caller, __tc_modify_qdisc(), would have made sure the parent qdisc already exist.

In addition, call qdisc_watchdog_init() whether .init succeeds or not to prevent null-pointer dereference. In qdisc_create() and qdisc_create_dflt(), if .init fails, .destroy will be called. As a result, the destroy epilogue could call qdisc_watchdog_cancel() with an uninitialized timer, causing null-pointer deference in hrtimer_cancel().

Fixes c8240344956e ("bpf: net_sched: Support implementation of Qdisc_ops in bpf")
---
 net/sched/bpf_qdisc.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
net/sched/bpf_qdisc.c +7 -5
index 9f32b305636f..a8efc3ff2b7e 100644
@@ -234,18 +234,20 @@ __bpf_kfunc int bpf_qdisc_init_prologue(struct Qdisc *sch,
 	struct net_device *dev = qdisc_dev(sch);
 	struct Qdisc *p;
 
+	qdisc_watchdog_init(&q->watchdog, sch);
+
 	if (sch->parent != TC_H_ROOT) {
+		/* If qdisc_lookup() returns NULL, it means .init is called by
+		 * qdisc_create_dflt() in mq/mqprio_init and the parent qdisc
+		 * has not been added to qdisc_hash yet.
+		 */
 		p = qdisc_lookup(dev, TC_H_MAJ(sch->parent));
-		if (!p)
-			return -ENOENT;
-
-		if (!(p->flags & TCQ_F_MQROOT)) {
+		if (p && !(p->flags & TCQ_F_MQROOT)) {
 			NL_SET_ERR_MSG(extack, "BPF qdisc only supported on root or mq");
 			return -EINVAL;
 		}
 	}
 
-	qdisc_watchdog_init(&q->watchdog, sch);
 	return 0;
 }
 
Signature · 1 line
2.47.1
author
#2 lore raw

PATCH v2 2/5 selftests/bpf: Test setting and creating bpf qdisc as default qdisc

In reply to Amery Hung

First, test that bpf qdisc can be set as default qdisc. Then, attach an mq qdisc to see if bpf qdisc can be successfully created and grafted.

The test is a sequential test as net.core.default_qdisc is global.

---
 .../selftests/bpf/prog_tests/bpf_qdisc.c      | 61 +++++++++++++++++++
 .../selftests/bpf/progs/bpf_qdisc_fifo.c      |  3 +
 2 files changed, 64 insertions(+)
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +61 -0
index c9a54177c84e..a22008fd31d2 100644
@@ -159,6 +159,62 @@ static void test_qdisc_attach_to_non_root(void)
 	bpf_qdisc_fifo__destroy(fifo_skel);
 }
 
+static int get_default_qdisc(char *qdisc_name)
+{
+	FILE *f;
+	int num;
+
+	f = fopen("/proc/sys/net/core/default_qdisc", "r");
+	if (!f)
+		return -errno;
+
+	num = fscanf(f, "%s", qdisc_name);
+	fclose(f);
+
+	return num == 1 ? 0 : -EFAULT;
+}
+
+static void test_default_qdisc_attach_to_mq(void)
+{
+	char default_qdisc[IFNAMSIZ] = {};
+	struct bpf_qdisc_fifo *fifo_skel;
+	struct netns_obj *netns = NULL;
+	int err;
+
+	fifo_skel = bpf_qdisc_fifo__open_and_load();
+	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
+		return;
+
+	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
+		goto out;
+
+	err = get_default_qdisc(default_qdisc);
+	if (!ASSERT_OK(err, "read sysctl net.core.default_qdisc"))
+		goto out;
+
+	err = write_sysctl("/proc/sys/net/core/default_qdisc", "bpf_fifo");
+	if (!ASSERT_OK(err, "write sysctl net.core.default_qdisc"))
+		goto out;
+
+	netns = netns_new("bpf_qdisc_ns", true);
+	if (!ASSERT_OK_PTR(netns, "netns_new"))
+		goto out;
+
+	SYS(out, "ip link add veth0 type veth peer veth1");
+	SYS(out, "tc qdisc add dev veth0 root handle 1: mq");
+
+	ASSERT_EQ(fifo_skel->bss->init_called, true, "init_called");
+
+	SYS(out, "tc qdisc delete dev veth0 root mq");
+out:
+	if (netns)
+		netns_free(netns);
+	if (default_qdisc[0])
+		write_sysctl("/proc/sys/net/core/default_qdisc", default_qdisc);
+
+	bpf_qdisc_fifo__destroy(fifo_skel);
+}
+
 void test_bpf_qdisc(void)
 {
 	struct netns_obj *netns;
@@ -178,3 +234,8 @@ void test_bpf_qdisc(void)
 
 	netns_free(netns);
 }
+
+void serial_test_bpf_qdisc_default(void)
+{
+	test_default_qdisc_attach_to_mq();
+}
tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c +3 -0
index 0c7cfb82dae1..571fa7233ec0 100644
@@ -14,6 +14,8 @@ struct skb_node {
 private(A) struct bpf_spin_lock q_fifo_lock;
 private(A) struct bpf_list_head q_fifo __contains(skb_node, node);
 
+bool init_called;
+
 SEC("struct_ops/bpf_fifo_enqueue")
 int BPF_PROG(bpf_fifo_enqueue, struct sk_buff *skb, struct Qdisc *sch,
 	     struct bpf_sk_buff_ptr *to_free)
@@ -77,6 +79,7 @@ int BPF_PROG(bpf_fifo_init, struct Qdisc *sch, struct nlattr *opt,
 	     struct netlink_ext_ack *extack)
 {
 	sch->limit = 1000;
+	init_called = true;
 	return 0;
 }
 
Signature · 1 line
2.47.1
author
#3 lore raw

PATCH v2 3/5 bpf: net_sched: Make some Qdisc_ops ops mandatory

In reply to Amery Hung

The patch makes all currently supported Qdisc_ops (i.e., .enqueue, .dequeue, .init, .reset, and .destroy) mandatory.

Make .init, .reset and .destroy mandatory as bpf qdisc relies on prologue and epilogue to check attach points and correctly initialize/cleanup resources. The prologue/epilogue will only be generated for an struct_ops operator only if users implement the operator.

Make .enqueue and .dequeue mandatory as bpf qdisc infra does not provide a default data path.

Fixes c8240344956e ("bpf: net_sched: Support implementation of Qdisc_ops in bpf")
---
 net/sched/bpf_qdisc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
net/sched/bpf_qdisc.c +12 -0
index a8efc3ff2b7e..7ea8b54b2ab1 100644
@@ -395,6 +395,17 @@ static void bpf_qdisc_unreg(void *kdata, struct bpf_link *link)
 	return unregister_qdisc(kdata);
 }
 
+static int bpf_qdisc_validate(void *kdata)
+{
+	struct Qdisc_ops *ops = (struct Qdisc_ops *)kdata;
+
+	if (!ops->enqueue || !ops->dequeue || !ops->init ||
+	    !ops->reset || !ops->destroy)
+		return -EINVAL;
+
+	return 0;
+}
+
 static int Qdisc_ops__enqueue(struct sk_buff *skb__ref, struct Qdisc *sch,
 			      struct sk_buff **to_free)
 {
@@ -432,6 +443,7 @@ static struct bpf_struct_ops bpf_Qdisc_ops = {
 	.verifier_ops = &bpf_qdisc_verifier_ops,
 	.reg = bpf_qdisc_reg,
 	.unreg = bpf_qdisc_unreg,
+	.validate = bpf_qdisc_validate,
 	.init_member = bpf_qdisc_init_member,
 	.init = bpf_qdisc_init,
 	.name = "Qdisc_ops",
Signature · 1 line
2.47.1
author
#4 lore raw

PATCH v2 4/5 selftests/bpf: Test attaching a bpf qdisc with incomplete operators

In reply to Amery Hung

Implement .destroy in bpf_fq and bpf_fifo as it is now mandatory.

Test attaching a bpf qdisc with a missing operator .init. This is not allowed as bpf qdisc qdisc_watchdog_cancel() could have been called with an uninitialized timer.

---
 .../selftests/bpf/prog_tests/bpf_qdisc.c      | 19 +++++++++
 .../bpf/progs/bpf_qdisc_fail__incompl_ops.c   | 41 +++++++++++++++++++
 .../selftests/bpf/progs/bpf_qdisc_fifo.c      |  6 +++
 .../selftests/bpf/progs/bpf_qdisc_fq.c        |  6 +++
 4 files changed, 72 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +19 -0
index a22008fd31d2..53154544b28c 100644
@@ -7,6 +7,7 @@
 #include "network_helpers.h"
 #include "bpf_qdisc_fifo.skel.h"
 #include "bpf_qdisc_fq.skel.h"
+#include "bpf_qdisc_fail__incompl_ops.skel.h"
 
 #define LO_IFINDEX 1
 
@@ -159,6 +160,22 @@ static void test_qdisc_attach_to_non_root(void)
 	bpf_qdisc_fifo__destroy(fifo_skel);
 }
 
+static void test_incompl_ops(void)
+{
+	struct bpf_qdisc_fail__incompl_ops *skel;
+	struct bpf_link *link;
+
+	skel = bpf_qdisc_fail__incompl_ops__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "bpf_qdisc_fifo__open_and_load"))
+		return;
+
+	link = bpf_map__attach_struct_ops(skel->maps.test);
+	if (!ASSERT_ERR_PTR(link, "bpf_map__attach_struct_ops"))
+		bpf_link__destroy(link);
+
+	bpf_qdisc_fail__incompl_ops__destroy(skel);
+}
+
 static int get_default_qdisc(char *qdisc_name)
 {
 	FILE *f;
@@ -231,6 +248,8 @@ void test_bpf_qdisc(void)
 		test_qdisc_attach_to_mq();
 	if (test__start_subtest("attach to non root"))
 		test_qdisc_attach_to_non_root();
+	if (test__start_subtest("incompl_ops"))
+		test_incompl_ops();
 
 	netns_free(netns);
 }
tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c new file +41 -0

41 lines not on this page · raw message

tools/testing/selftests/bpf/progs/bpf_qdisc_fifo.c +6 -0
index 571fa7233ec0..bb91b04113a6 100644
@@ -109,12 +109,18 @@ void BPF_PROG(bpf_fifo_reset, struct Qdisc *sch)
 	sch->q.qlen = 0;
 }
 
+SEC("struct_ops/bpf_fifo_destroy")
+void BPF_PROG(bpf_fifo_destroy, struct Qdisc *sch)
+{
+}
+
 SEC(".struct_ops")
 struct Qdisc_ops fifo = {
 	.enqueue   = (void *)bpf_fifo_enqueue,
 	.dequeue   = (void *)bpf_fifo_dequeue,
 	.init      = (void *)bpf_fifo_init,
 	.reset     = (void *)bpf_fifo_reset,
+	.destroy   = (void *)bpf_fifo_destroy,
 	.id        = "bpf_fifo",
 };
 
tools/testing/selftests/bpf/progs/bpf_qdisc_fq.c +6 -0
index 7c110a156224..72c9f4aefbcf 100644
@@ -740,11 +740,17 @@ int BPF_PROG(bpf_fq_init, struct Qdisc *sch, struct nlattr *opt,
 	return 0;
 }
 
+SEC("struct_ops/bpf_fq_destroy")
+void BPF_PROG(bpf_fq_destroy, struct Qdisc *sch)
+{
+}
+
 SEC(".struct_ops")
 struct Qdisc_ops fq = {
 	.enqueue   = (void *)bpf_fq_enqueue,
 	.dequeue   = (void *)bpf_fq_dequeue,
 	.reset     = (void *)bpf_fq_reset,
 	.init      = (void *)bpf_fq_init,
+	.destroy   = (void *)bpf_fq_destroy,
 	.id        = "bpf_fq",
 };
Signature · 1 line
2.47.1
reviewer Applied
#5 lore raw

Re: [PATCH bpf-next/net v2 4/5] selftests/bpf: Test attaching a bpf qdisc with incomplete operators

tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c line 28

Quoted 4 lines · Amery Hung
tools/testing/selftests/bpf/progs/bpf_qdisc_fail__incompl_ops.c new file +4 -0
+SEC("struct_ops/bpf_qdisc_test_enqueue")
+SEC("struct_ops/bpf_qdisc_test_dequeue")
+SEC("struct_ops/bpf_qdisc_test_reset")
+SEC("struct_ops/bpf_qdisc_test_destroy")

I removed all the "/bpf_qdisc_test_xxx" part. Only SEC("struct_ops") is needed. A similar cleanup was done in bpf_cubic/dctcp.c.

Review

Quoted 2 lines
(quoted diff) +2 -0
+SEC("struct_ops/bpf_fifo_destroy")
+SEC("struct_ops/bpf_fq_destroy")

Removed the "/bpf_xxx" part from these new ones also.

The other existing bpf_qdisc_fifo/fq SEC was an overlook and it should be cleaned up in a followup.

Applied. Thanks.

author
#6 lore raw

PATCH v2 5/5 selftests/bpf: Cleanup bpf qdisc selftests

In reply to Amery Hung

Some cleanups:
- Remove unnecessary kfuncs declaration
- Use _ns in the test name to run tests in a separate net namespace
- Call skeleton __attach() instead of bpf_map__attach_struct_ops() to simplify tests.

---
 .../selftests/bpf/prog_tests/bpf_qdisc.c      | 50 ++++---------------
 .../selftests/bpf/progs/bpf_qdisc_common.h    |  6 ---
 2 files changed, 11 insertions(+), 45 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +11 -39
index 53154544b28c..c4d797f95da9 100644
@@ -50,42 +50,32 @@ static void do_test(char *qdisc)
 static void test_fifo(void)
 {
 	struct bpf_qdisc_fifo *fifo_skel;
-	struct bpf_link *link;
 
 	fifo_skel = bpf_qdisc_fifo__open_and_load();
 	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
 		return;
 
-	link = bpf_map__attach_struct_ops(fifo_skel->maps.fifo);
-	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
-		bpf_qdisc_fifo__destroy(fifo_skel);
-		return;
-	}
+	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
+		goto out;
 
 	do_test("bpf_fifo");
-
-	bpf_link__destroy(link);
+out:
 	bpf_qdisc_fifo__destroy(fifo_skel);
 }
 
 static void test_fq(void)
 {
 	struct bpf_qdisc_fq *fq_skel;
-	struct bpf_link *link;
 
 	fq_skel = bpf_qdisc_fq__open_and_load();
 	if (!ASSERT_OK_PTR(fq_skel, "bpf_qdisc_fq__open_and_load"))
 		return;
 
-	link = bpf_map__attach_struct_ops(fq_skel->maps.fq);
-	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
-		bpf_qdisc_fq__destroy(fq_skel);
-		return;
-	}
+	if (!ASSERT_OK(bpf_qdisc_fq__attach(fq_skel), "bpf_qdisc_fq__attach"))
+		goto out;
 
 	do_test("bpf_fq");
-
-	bpf_link__destroy(link);
+out:
 	bpf_qdisc_fq__destroy(fq_skel);
 }
 
@@ -97,18 +87,14 @@ static void test_qdisc_attach_to_mq(void)
 			    .handle = 0x11 << 16,
 			    .qdisc = "bpf_fifo");
 	struct bpf_qdisc_fifo *fifo_skel;
-	struct bpf_link *link;
 	int err;
 
 	fifo_skel = bpf_qdisc_fifo__open_and_load();
 	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
 		return;
 
-	link = bpf_map__attach_struct_ops(fifo_skel->maps.fifo);
-	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
-		bpf_qdisc_fifo__destroy(fifo_skel);
-		return;
-	}
+	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
+		goto out;
 
 	SYS(out, "ip link add veth0 type veth peer veth1");
 	hook.ifindex = if_nametoindex("veth0");
@@ -121,7 +107,6 @@ static void test_qdisc_attach_to_mq(void)
 
 	SYS(out, "tc qdisc delete dev veth0 root mq");
 out:
-	bpf_link__destroy(link);
 	bpf_qdisc_fifo__destroy(fifo_skel);
 }
 
@@ -133,18 +118,14 @@ static void test_qdisc_attach_to_non_root(void)
 			    .handle = 0x11 << 16,
 			    .qdisc = "bpf_fifo");
 	struct bpf_qdisc_fifo *fifo_skel;
-	struct bpf_link *link;
 	int err;
 
 	fifo_skel = bpf_qdisc_fifo__open_and_load();
 	if (!ASSERT_OK_PTR(fifo_skel, "bpf_qdisc_fifo__open_and_load"))
 		return;
 
-	link = bpf_map__attach_struct_ops(fifo_skel->maps.fifo);
-	if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops")) {
-		bpf_qdisc_fifo__destroy(fifo_skel);
-		return;
-	}
+	if (!ASSERT_OK(bpf_qdisc_fifo__attach(fifo_skel), "bpf_qdisc_fifo__attach"))
+		goto out;
 
 	SYS(out, "tc qdisc add dev lo root handle 1: htb");
 	SYS(out_del_htb, "tc class add dev lo parent 1: classid 1:1 htb rate 75Kbit");
@@ -156,7 +137,6 @@ static void test_qdisc_attach_to_non_root(void)
 out_del_htb:
 	SYS(out, "tc qdisc delete dev lo root htb");
 out:
-	bpf_link__destroy(link);
 	bpf_qdisc_fifo__destroy(fifo_skel);
 }
 
@@ -232,14 +212,8 @@ static void test_default_qdisc_attach_to_mq(void)
 	bpf_qdisc_fifo__destroy(fifo_skel);
 }
 
-void test_bpf_qdisc(void)
+void test_ns_bpf_qdisc(void)
 {
-	struct netns_obj *netns;
-
-	netns = netns_new("bpf_qdisc_ns", true);
-	if (!ASSERT_OK_PTR(netns, "netns_new"))
-		return;
-
 	if (test__start_subtest("fifo"))
 		test_fifo();
 	if (test__start_subtest("fq"))
@@ -250,8 +224,6 @@ void test_bpf_qdisc(void)
 		test_qdisc_attach_to_non_root();
 	if (test__start_subtest("incompl_ops"))
 		test_incompl_ops();
-
-	netns_free(netns);
 }
 
 void serial_test_bpf_qdisc_default(void)
tools/testing/selftests/bpf/progs/bpf_qdisc_common.h +0 -6
index 7e7f2fe04f22..3754f581b328 100644
@@ -14,12 +14,6 @@
 
 struct bpf_sk_buff_ptr;
 
-u32 bpf_skb_get_hash(struct sk_buff *p) __ksym;
-void bpf_kfree_skb(struct sk_buff *p) __ksym;
-void bpf_qdisc_skb_drop(struct sk_buff *p, struct bpf_sk_buff_ptr *to_free) __ksym;
-void bpf_qdisc_watchdog_schedule(struct Qdisc *sch, u64 expire, u64 delta_ns) __ksym;
-void bpf_qdisc_bstats_update(struct Qdisc *sch, const struct sk_buff *skb) __ksym;
-
 static struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
 {
 	return (struct qdisc_skb_cb *)skb->cb;
Signature · 1 line
2.47.1
#7 lore raw
In reply to Amery Hung
This series was applied to bpf/bpf-next.git (net) by Martin KaFai Lau <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (net)
by Martin KaFai Lau <[email protected]>:

Quoted 9 lines · On Fri, 2 May 2025 13:16:19 -0700 you wrote:

This patchset fixes the following bugs in bpf qdisc and clean up the selftest.

- A null-pointer dereference can happen in qdisc_watchdog_cancel() if the timer is not initialized when 1) .init is not defined by user so init prologue is not generated. 2) .init fails and qdisc_create() calls .destroy

[...]

Here is the summary with links:
- [bpf-next/net,v2,1/5] bpf: net_sched: Fix bpf qdisc init prologue when set as default qdisc

    https://git.kernel.org/bpf/bpf-next/c/659b3b2c4885
  - [bpf-next/net,v2,2/5] selftests/bpf: Test setting and creating bpf qdisc as default qdisc
    (no matching commit)
  - [bpf-next/net,v2,3/5] bpf: net_sched: Make some Qdisc_ops ops mandatory
    https://git.kernel.org/bpf/bpf-next/c/64d6e3b9df1b
  - [bpf-next/net,v2,4/5] selftests/bpf: Test attaching a bpf qdisc with incomplete operators
    (no matching commit)
  - [bpf-next/net,v2,5/5] selftests/bpf: Cleanup bpf qdisc selftests
    https://git.kernel.org/bpf/bpf-next/c/2f9838e25790

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html