bpf: Convert queue_stack map to rqspinlock

Replace all usage of raw_spinlock_t in queue_stack_maps.c with
rqspinlock. This is a map type with a set of open syzbot reports
reproducing possible deadlocks. Prior attempt to fix the issues
was at [0], but was dropped in favor of this approach.

Make sure we return the -EBUSY error in case of possible deadlocks or
timeouts, just to make sure user space or BPF programs relying on the
error code to detect problems do not break.

With these changes, the map should be safe to access in any context,
including NMIs.

[0]: https://lore.kernel.org/all/20240429165658.1305969-1-sidchintamaneni@gmail.com

Reported-by: syzbot+8bdfc2c53fb2b63e1871@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/0000000000004c3fc90615f37756@google.com
Reported-by: syzbot+252bc5c744d0bba917e1@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/000000000000c80abd0616517df9@google.com
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20250410153142.2064340-1-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by Kumar Kartikeya Dwivedi and committed by Alexei Starovoitov 2f41503d 92b90f78

+12 -23
+12 -23
kernel/bpf/queue_stack_maps.c
··· 9 9 #include <linux/slab.h> 10 10 #include <linux/btf_ids.h> 11 11 #include "percpu_freelist.h" 12 + #include <asm/rqspinlock.h> 12 13 13 14 #define QUEUE_STACK_CREATE_FLAG_MASK \ 14 15 (BPF_F_NUMA_NODE | BPF_F_ACCESS_MASK) 15 16 16 17 struct bpf_queue_stack { 17 18 struct bpf_map map; 18 - raw_spinlock_t lock; 19 + rqspinlock_t lock; 19 20 u32 head, tail; 20 21 u32 size; /* max_entries + 1 */ 21 22 ··· 79 78 80 79 qs->size = size; 81 80 82 - raw_spin_lock_init(&qs->lock); 81 + raw_res_spin_lock_init(&qs->lock); 83 82 84 83 return &qs->map; 85 84 } ··· 99 98 int err = 0; 100 99 void *ptr; 101 100 102 - if (in_nmi()) { 103 - if (!raw_spin_trylock_irqsave(&qs->lock, flags)) 104 - return -EBUSY; 105 - } else { 106 - raw_spin_lock_irqsave(&qs->lock, flags); 107 - } 101 + if (raw_res_spin_lock_irqsave(&qs->lock, flags)) 102 + return -EBUSY; 108 103 109 104 if (queue_stack_map_is_empty(qs)) { 110 105 memset(value, 0, qs->map.value_size); ··· 117 120 } 118 121 119 122 out: 120 - raw_spin_unlock_irqrestore(&qs->lock, flags); 123 + raw_res_spin_unlock_irqrestore(&qs->lock, flags); 121 124 return err; 122 125 } 123 126 ··· 130 133 void *ptr; 131 134 u32 index; 132 135 133 - if (in_nmi()) { 134 - if (!raw_spin_trylock_irqsave(&qs->lock, flags)) 135 - return -EBUSY; 136 - } else { 137 - raw_spin_lock_irqsave(&qs->lock, flags); 138 - } 136 + if (raw_res_spin_lock_irqsave(&qs->lock, flags)) 137 + return -EBUSY; 139 138 140 139 if (queue_stack_map_is_empty(qs)) { 141 140 memset(value, 0, qs->map.value_size); ··· 150 157 qs->head = index; 151 158 152 159 out: 153 - raw_spin_unlock_irqrestore(&qs->lock, flags); 160 + raw_res_spin_unlock_irqrestore(&qs->lock, flags); 154 161 return err; 155 162 } 156 163 ··· 196 203 if (flags & BPF_NOEXIST || flags > BPF_EXIST) 197 204 return -EINVAL; 198 205 199 - if (in_nmi()) { 200 - if (!raw_spin_trylock_irqsave(&qs->lock, irq_flags)) 201 - return -EBUSY; 202 - } else { 203 - raw_spin_lock_irqsave(&qs->lock, irq_flags); 204 - } 206 + if (raw_res_spin_lock_irqsave(&qs->lock, irq_flags)) 207 + return -EBUSY; 205 208 206 209 if (queue_stack_map_is_full(qs)) { 207 210 if (!replace) { ··· 216 227 qs->head = 0; 217 228 218 229 out: 219 - raw_spin_unlock_irqrestore(&qs->lock, irq_flags); 230 + raw_res_spin_unlock_irqrestore(&qs->lock, irq_flags); 220 231 return err; 221 232 } 222 233