bpf: Refactor active lock management
Kumar Kartikeya Dwivedi
maintainerPATCH 1/1 bpf: Refactor active lock management
When bpf_spin_lock was introduced originally, there was deliberation on whether to use an array of lock IDs, but since bpf_spin_lock is limited to holding a single lock at any given time, we've been using a single ID to identify the held lock.
In preparation for introducing spin locks that can be taken multiple times, introduce support for acquiring multiple lock IDs. For this purpose, reuse the acquired_refs array and store both lock and pointer references. We tag the entry with REF_TYPE_PTR or REF_TYPE_BPF_LOCK to disambiguate and find the relevant entry. The ptr field is used to track the map_ptr or btf (for bpf_obj_new allocations) to ensure locks can be matched with protected fields within the same "allocation", i.e. bpf_obj_new object or map value.
The struct active_lock is changed to a boolean as the state is part of the acquired_refs array, and we only need active_lock as a cheap way of detecting lock presence.
--- include/linux/bpf_verifier.h | 34 ++++++--- kernel/bpf/verifier.c | 139 +++++++++++++++++++++++++---------- 2 files changed, 122 insertions(+), 51 deletions(-)
include/linux/bpf_verifier.h +22 -12
@@ -266,6 +266,10 @@ struct bpf_stack_state { };struct bpf_reference_state { +/* Each reference object has a type. Ensure REF_TYPE_PTR is zero to +* default to pointer reference on zero initialization of a state. +*/ +enum {REF_TYPE_PTR =0 ,REF_TYPE_BPF_LOCK } type;/* Track each reference created with a unique id, even if the same * instruction creates the reference multiple times (eg, via CALL). */
@@ -274,17 +278,23 @@ struct bpf_reference_state {* is used purely to inform the user of a reference leak. */ int insn_idx; -/* There can be a case like: -* main (frame 0) -* cb (frame 1) -* func (frame 3) -* cb (frame 4) -* Hence for frame 4, if callback_ref just stored boolean, it would be -* impossible to distinguish nested callback refs. Hence store the -* frameno and compare that to callback_ref in check_reference_leak when -* exiting a callback function. -*/ -int callback_ref; +union { +/* There can be a case like: +* main (frame 0) +* cb (frame 1) +* func (frame 3) +* cb (frame 4) +* Hence for frame 4, if callback_ref just stored boolean, it would be +* impossible to distinguish nested callback refs. Hence store the +* frameno and compare that to callback_ref in check_reference_leak when +* exiting a callback function. +*/ +int callback_ref; +/* Use to keep track of the source object of a lock, to ensure +* it matches on unlock. +*/ +void *ptr; + }; };struct bpf_retval_range {
@@ -434,7 +444,7 @@ struct bpf_verifier_state {u32 insn_idx;u32 curframe; -struct bpf_active_lock active_lock; +bool active_lock;bool speculative;bool active_rcu_lock;u32 active_preempt_lock;
kernel/bpf/verifier.c +100 -39
276 lines not on this page · raw message
--
2.43.5