Skip to content

Commit 7b6e5bf

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
Merge branch 'refactor-lock-management'
Kumar Kartikeya Dwivedi says: ==================== Refactor lock management This set refactors lock management in the verifier in preparation for spin locks that can be acquired multiple times. In addition to this, unnecessary code special case reference leak logic for callbacks is also dropped, that is no longer necessary. See patches for details. Changelog: ---------- v5 -> v6 v5: https://lore.kernel.org/bpf/[email protected] * Move active_locks mutation to {acquire,release}_lock_state (Alexei) v4 -> v5 v4: https://lore.kernel.org/bpf/[email protected] * Make active_locks part of bpf_func_state (Alexei) * Remove unneeded in_callback_fn logic for references v3 -> v4 v3: https://lore.kernel.org/bpf/[email protected] * Address comments from Alexei * Drop struct bpf_active_lock definition * Name enum type, expand definition to multiple lines * s/REF_TYPE_BPF_LOCK/REF_TYPE_LOCK/g * Change active_lock type to int * Fix type of 'type' in acquire_lock_state * Filter by taking type explicitly in find_lock_state * WARN for default case in refsafe switch statement v2 -> v3 v2: https://lore.kernel.org/bpf/[email protected] * Rebase on bpf-next to resolve merge conflict v1 -> v2 v1: https://lore.kernel.org/bpf/[email protected] * Fix refsafe state comparison to check callback_ref and ptr separately. ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 937a1c2 + ae6e3a2 commit 7b6e5bf

File tree

3 files changed

+123
-86
lines changed

3 files changed

+123
-86
lines changed

include/linux/bpf_verifier.h

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,6 @@ enum bpf_reg_liveness {
4848
REG_LIVE_DONE = 0x8, /* liveness won't be updating this register anymore */
4949
};
5050

51-
/* For every reg representing a map value or allocated object pointer,
52-
* we consider the tuple of (ptr, id) for them to be unique in verifier
53-
* context and conside them to not alias each other for the purposes of
54-
* tracking lock state.
55-
*/
56-
struct bpf_active_lock {
57-
/* This can either be reg->map_ptr or reg->btf. If ptr is NULL,
58-
* there's no active lock held, and other fields have no
59-
* meaning. If non-NULL, it indicates that a lock is held and
60-
* id member has the reg->id of the register which can be >= 0.
61-
*/
62-
void *ptr;
63-
/* This will be reg->id */
64-
u32 id;
65-
};
66-
6751
#define ITER_PREFIX "bpf_iter_"
6852

6953
enum bpf_iter_state {
@@ -266,6 +250,13 @@ struct bpf_stack_state {
266250
};
267251

268252
struct bpf_reference_state {
253+
/* Each reference object has a type. Ensure REF_TYPE_PTR is zero to
254+
* default to pointer reference on zero initialization of a state.
255+
*/
256+
enum ref_state_type {
257+
REF_TYPE_PTR = 0,
258+
REF_TYPE_LOCK,
259+
} type;
269260
/* Track each reference created with a unique id, even if the same
270261
* instruction creates the reference multiple times (eg, via CALL).
271262
*/
@@ -274,17 +265,10 @@ struct bpf_reference_state {
274265
* is used purely to inform the user of a reference leak.
275266
*/
276267
int insn_idx;
277-
/* There can be a case like:
278-
* main (frame 0)
279-
* cb (frame 1)
280-
* func (frame 3)
281-
* cb (frame 4)
282-
* Hence for frame 4, if callback_ref just stored boolean, it would be
283-
* impossible to distinguish nested callback refs. Hence store the
284-
* frameno and compare that to callback_ref in check_reference_leak when
285-
* exiting a callback function.
286-
*/
287-
int callback_ref;
268+
/* Use to keep track of the source object of a lock, to ensure
269+
* it matches on unlock.
270+
*/
271+
void *ptr;
288272
};
289273

290274
struct bpf_retval_range {
@@ -332,6 +316,7 @@ struct bpf_func_state {
332316

333317
/* The following fields should be last. See copy_func_state() */
334318
int acquired_refs;
319+
int active_locks;
335320
struct bpf_reference_state *refs;
336321
/* The state of the stack. Each element of the array describes BPF_REG_SIZE
337322
* (i.e. 8) bytes worth of stack memory.
@@ -434,7 +419,6 @@ struct bpf_verifier_state {
434419
u32 insn_idx;
435420
u32 curframe;
436421

437-
struct bpf_active_lock active_lock;
438422
bool speculative;
439423
bool active_rcu_lock;
440424
u32 active_preempt_lock;

0 commit comments

Comments
 (0)