Skip to contentSkip to conversation

bpf: Skip same map struct_ops link updates

PATCH In review Paul Moses 2 participants 2 messages Dec 2 – Dec 3

Subsystems (4)
lore
Share & tools
lore

Paul Moses

author
#0 lore raw

PATCH 1/1 bpf: Skip same map struct_ops link updates

bpf_struct_ops_map_link_update() invokes the subsystem update callback even when the link already uses the new map. For TCP congestion control, tcp_update_congestion_control() then adds and removes the same list entry, which can corrupt tcp_cong_list.

Skip the callback when the maps are identical.

Fixes aef56f2e918b ("bpf: Update the struct_ops of a bpf_link.")

---
CAP_BPF/CAP_PERFMON required.
---
list_del corruption, ffff000001e82a78->prev is LIST_POISON2 (dead000000000122)

WARNING: lib/list_debug.c:61 at __list_del_entry_valid_or_report+0x100/0x154, CPU#1: fun2/47
CPU: 1 UID: 1000 PID: 47 Comm: fun2 Not tainted 7.4.0-rc4 #1 PREEMPT 
Hardware name: linux,dummy-virt (DT)
pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : __list_del_entry_valid_or_report+0x100/0x154
lr : __list_del_entry_valid_or_report+0xfc/0x154

sp : ffff800080ef3cf0
x29: ffff800080ef3cf0 x28: ffff0000011e6200 x27: 0000000000000000
x26: 0000000000000000 x25: 0000000000000000 x24: 0000000000000000
x23: 00000000020a0001 x22: dead000000000122 x21: dead000000000122
x20: ffff800080c158c0 x19: ffff000001e82a78 x18: 0000000000000000
x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
x14: 000000000000009c x13: 0000000000000003 x12: ffff80003ee6e000
x11: ffff0000bf9cc508 x10: 0000000000000001 x9 : ffff800080b86cd0 x8 : 0000000100000001 x7 : 505f5453494c2073 x6 : 0000000000000029 x5 : ffff800080c3bd8f x4 : 0000000000000000 x3 : ffff800080ef3988 x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000000

Call trace:
 __list_del_entry_valid_or_report+0x100/0x154 (P)
 tcp_unregister_congestion_control+0x24/0x5c
 bpf_tcp_ca_unreg+0xc/0x14
 bpf_struct_ops_map_link_dealloc+0x30/0x4c
 bpf_link_dealloc+0x34/0x40
 bpf_link_free+0xb0/0xbc
 bpf_link_put_direct+0x20/0x4c
 bpf_link_release+0x10/0x1c
 __fput+0x50/0x1cc
 fput_close_sync+0x38/0xec
 __arm64_sys_close+0x34/0x70
 do_el0_svc+0x88/0xcc
 el0_svc+0x20/0x54
 el0t_64_sync_handler+0x40/0xfc
 el0t_64_sync+0x154/0x158

...

Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000

Mem abort info:
ESR = 0x0000000086000006
EC = 0x21: IABT (current EL), IL = 32 bits
SET = 0, FnV = 0
EA = 0, S1PTW = 0
FSC = 0x06: level 2 translation fault
user pgtable: 4k pages, 48-bit VAs, pgdp=00000000412cf000
[0000000000000000] pgd=0800000041ec8403, p4d=0800000041ec8403, pud=0800000041ec9403, pmd=0000000000000000

Internal error: Oops: 0000000086000006 [#1]  SMP
CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Tainted: G        W           7.4.0-rc4 #1 PREEMPT 
Tainted: [W]=WARN
Hardware name: linux,dummy-virt (DT)
pstate: 20400005 (nzCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
pc : 0x0
lr : tcp_enter_loss+0x16c/0x2dc

sp : ffff800080cc3dc0
x29: ffff800080cc3dc0 x28: ffff800080cc3eb8 x27: 0000000000000000
x26: dead000000000122 x25: 000000000000001b x24: 0000000000000000
x23: ffff0000012981c0 x22: ffff800080c95b00 x21: 0000000000000060
x20: 0000000000000000 x19: ffff000001efd340 x18: 000000008cf6d33d
x17: 000000009de3d1c8 x16: 0000000037b1d58d x15: 000000001f98cc76
x14: 00000000fd3a0fac x13: 00000000b211991d x12: 00000000c73d125d
x11: 000000009518c73b x10: 0000000000000007 x9 : ffff000001e82a00 x8 : 0000000000000000 x7 : 00000000e1575f02 x6 : 00000000bde17e00 x5 : 000000000d8e05e2 x4 : 0000000077937876 x3 : 0000000037f0c725 x2 : 00000000863de794 x1 : ffff0000012981f0 x0 : ffff000001efd340

Call trace:
 0x0 (P)
 tcp_retransmit_timer+0x600/0x860
 tcp_write_timer_handler+0xf4/0x1e0
 tcp_write_timer+0x78/0x130
 call_timer_fn+0x20/0x80
 run_timer_softirq+0x290/0x2f4
 handle_softirqs+0x10c/0x250
 __do_softirq+0x10/0x18
 ____do_softirq+0xc/0x14
 call_on_irq_stack+0x30/0x4c
 do_softirq_own_stack+0x18/0x20
 irq_exit_rcu+0x40/0xc0
 el1_interrupt+0x3c/0x58
 el1h_64_irq_handler+0x14/0x1c
 el1h_64_irq+0x78/0x7c
 default_idle_call+0x20/0x28 (P)
 do_idle+0x5c/0xb4
 cpu_startup_entry+0x30/0x34
 secondary_start_kernel+0x128/0x148
 __secondary_switched+0x74/0x78

---

 kernel/bpf/bpf_struct_ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/bpf_struct_ops.c +1 -1
index 5461e0ebab2b..0985854d169a 100644
@@ -1409,7 +1409,7 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
 	mutex_lock(&update_mutex);
 	old_map = st_link->map;
 	err = bpf_struct_ops_link_update_check(new_map, old_map, expected_old_map);
-	if (err)
+	if (err || new_map == old_map)
 		goto err_out;
 
 	old_st_map = container_of(old_map, struct bpf_struct_ops_map, map);
Signature · 1 line
2.56.0.lol.gf7de81456940
#1 lore raw

Review static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map · line 1413

Quoted 9 lines · Paul Moses

Skip the callback when the maps are identical.

(quoted diff) +1 -1
@@ -1409,7 +1409,7 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
 	mutex_lock(&update_mutex);
 	old_map = st_link->map;
 	err = bpf_struct_ops_link_update_check(new_map, old_map, expected_old_map);
-	if (err)
+	if (err || new_map == old_map)
 		goto err_out;

This changes sched_ext too.
bpf_scx_update() always returns -EOPNOTSUPP. The patch skips it. BPF_LINK_UPDATE with the map that the link already has:

  struct_ops              bpf/master      with the patch
  ----------------------  --------------  --------------
  tcp_congestion_ops      0, broken list  0
  sched_ext_ops           -EOPNOTSUPP     0
  Qdisc_ops, hid_bpf_ops  -EOPNOTSUPP     -EOPNOTSUPP

sysrq-S disables the scheduler, but the link keeps the map. User space gets 0 for a scheduler that doesn't run.

The bug is in tcp_update_congestion_control(). Fix it there.

net/ipv4/tcp_cong.c +3 -0
index e3f462f..5658f24 100644
@@ -148,6 +148,9 @@ int tcp_update_congestion_control(struct tcp_congestion_ops *ca, struct tcp_cong
 	struct tcp_congestion_ops *existing;
 	int ret = 0;
 
+	if (ca == old_ca)
+		return 0;
+
 	ca->key = jhash(ca->name, sizeof(ca->name), strlen(ca->name));
 
 	spin_lock(&tcp_cong_list_lock);

pw-bot: cr