Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: Introduce rqspinlock kfuncs

Introduce four new kfuncs, bpf_res_spin_lock, and bpf_res_spin_unlock,
and their irqsave/irqrestore variants, which wrap the rqspinlock APIs.
bpf_res_spin_lock returns a conditional result, depending on whether the
lock was acquired (NULL is returned when lock acquisition succeeds,
non-NULL upon failure). The memory pointed to by the returned pointer
upon failure can be dereferenced after the NULL check to obtain the
error code.

Instead of using the old bpf_spin_lock type, introduce a new type with
the same layout, and the same alignment, but a different name to avoid
type confusion.

Preemption is disabled upon successful lock acquisition, however IRQs
are not. Special kfuncs can be introduced later to allow disabling IRQs
when taking a spin lock. Resilient locks are safe against AA deadlocks,
hence not disabling IRQs currently does not allow violation of kernel
safety.

__irq_flag annotation is used to accept IRQ flags for the IRQ-variants,
with the same semantics as existing bpf_local_irq_{save, restore}.

These kfuncs will require additional verifier-side support in subsequent
commits, to allow programs to hold multiple locks at the same time.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20250316040541.108729-23-memxor@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Kumar Kartikeya Dwivedi and committed by
Alexei Starovoitov
97eb35f3 47979314

+82
+7
include/asm-generic/rqspinlock.h
··· 23 23 }; 24 24 }; 25 25 26 + /* Even though this is same as struct rqspinlock, we need to emit a distinct 27 + * type in BTF for BPF programs. 28 + */ 29 + struct bpf_res_spin_lock { 30 + u32 val; 31 + }; 32 + 26 33 struct qspinlock; 27 34 #ifdef CONFIG_QUEUED_SPINLOCKS 28 35 typedef struct qspinlock rqspinlock_t;
+1
include/linux/bpf.h
··· 30 30 #include <linux/static_call.h> 31 31 #include <linux/memcontrol.h> 32 32 #include <linux/cfi.h> 33 + #include <asm/rqspinlock.h> 33 34 34 35 struct bpf_verifier_env; 35 36 struct bpf_verifier_log;
+74
kernel/bpf/rqspinlock.c
··· 15 15 16 16 #include <linux/smp.h> 17 17 #include <linux/bug.h> 18 + #include <linux/bpf.h> 19 + #include <linux/err.h> 18 20 #include <linux/cpumask.h> 19 21 #include <linux/percpu.h> 20 22 #include <linux/hardirq.h> ··· 663 661 EXPORT_SYMBOL_GPL(resilient_queued_spin_lock_slowpath); 664 662 665 663 #endif /* CONFIG_QUEUED_SPINLOCKS */ 664 + 665 + __bpf_kfunc_start_defs(); 666 + 667 + __bpf_kfunc int bpf_res_spin_lock(struct bpf_res_spin_lock *lock) 668 + { 669 + int ret; 670 + 671 + BUILD_BUG_ON(sizeof(rqspinlock_t) != sizeof(struct bpf_res_spin_lock)); 672 + BUILD_BUG_ON(__alignof__(rqspinlock_t) != __alignof__(struct bpf_res_spin_lock)); 673 + 674 + preempt_disable(); 675 + ret = res_spin_lock((rqspinlock_t *)lock); 676 + if (unlikely(ret)) { 677 + preempt_enable(); 678 + return ret; 679 + } 680 + return 0; 681 + } 682 + 683 + __bpf_kfunc void bpf_res_spin_unlock(struct bpf_res_spin_lock *lock) 684 + { 685 + res_spin_unlock((rqspinlock_t *)lock); 686 + preempt_enable(); 687 + } 688 + 689 + __bpf_kfunc int bpf_res_spin_lock_irqsave(struct bpf_res_spin_lock *lock, unsigned long *flags__irq_flag) 690 + { 691 + u64 *ptr = (u64 *)flags__irq_flag; 692 + unsigned long flags; 693 + int ret; 694 + 695 + preempt_disable(); 696 + local_irq_save(flags); 697 + ret = res_spin_lock((rqspinlock_t *)lock); 698 + if (unlikely(ret)) { 699 + local_irq_restore(flags); 700 + preempt_enable(); 701 + return ret; 702 + } 703 + *ptr = flags; 704 + return 0; 705 + } 706 + 707 + __bpf_kfunc void bpf_res_spin_unlock_irqrestore(struct bpf_res_spin_lock *lock, unsigned long *flags__irq_flag) 708 + { 709 + u64 *ptr = (u64 *)flags__irq_flag; 710 + unsigned long flags = *ptr; 711 + 712 + res_spin_unlock((rqspinlock_t *)lock); 713 + local_irq_restore(flags); 714 + preempt_enable(); 715 + } 716 + 717 + __bpf_kfunc_end_defs(); 718 + 719 + BTF_KFUNCS_START(rqspinlock_kfunc_ids) 720 + BTF_ID_FLAGS(func, bpf_res_spin_lock, KF_RET_NULL) 721 + BTF_ID_FLAGS(func, bpf_res_spin_unlock) 722 + BTF_ID_FLAGS(func, bpf_res_spin_lock_irqsave, KF_RET_NULL) 723 + BTF_ID_FLAGS(func, bpf_res_spin_unlock_irqrestore) 724 + BTF_KFUNCS_END(rqspinlock_kfunc_ids) 725 + 726 + static const struct btf_kfunc_id_set rqspinlock_kfunc_set = { 727 + .owner = THIS_MODULE, 728 + .set = &rqspinlock_kfunc_ids, 729 + }; 730 + 731 + static __init int rqspinlock_register_kfuncs(void) 732 + { 733 + return register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &rqspinlock_kfunc_set); 734 + } 735 + late_initcall(rqspinlock_register_kfuncs);