Skip to contentSkip to conversation

bpf: Use common error handling code in bpf_is_state_visited()

PATCH Superseded Markus Elfring 3 participants 5 messages Jun 17 – Jun 22

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Use common error handling code in bpf_is_state_visited()

From: Markus Elfring <[email protected]>
Date: Wed, 17 Jun 2026 21:15:08 +0200

Use an additional label so that a bit of exception handling can be better reused at the end of this function implementation.

This issue was detected by using the Coccinelle software.

---
 kernel/bpf/states.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
kernel/bpf/states.c +10 -10
index 5945956a7573..09f87e2bb1e6 100644
@@ -1566,21 +1566,16 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
 	/* add new state to the head of linked list */
 	new = &new_sl->state;
 	err = bpf_copy_verifier_state(new, cur);
-	if (err) {
-		bpf_free_verifier_state(new, false);
-		kfree(new_sl);
-		return err;
-	}
+	if (err)
+		goto free_verifier_state;
+
 	new->insn_idx = insn_idx;
 	verifier_bug_if(new->branches != 1, env,
 			"%s:branches_to_explore=%d insn %d",
 			__func__, new->branches, insn_idx);
 	err = maybe_enter_scc(env, new);
-	if (err) {
-		bpf_free_verifier_state(new, false);
-		kfree(new_sl);
-		return err;
-	}
+	if (err)
+		goto free_verifier_state;
 
 	cur->parent = new;
 	cur->first_insn_idx = insn_idx;
@@ -1588,4 +1583,9 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
 	bpf_clear_jmp_history(cur);
 	list_add(&new_sl->node, head);
 	return 0;
+
+free_verifier_state:
+	bpf_free_verifier_state(new, false);
+	kfree(new_sl);
+	return err;
 }
Signature · 1 line
2.54.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 17 Jun 2026 21:15:08 +0200

Use an additional label so that a bit of exception handling can be better reused at the end of this function implementation.

This issue was detected by using the Coccinelle software.

Can you resend with [PATCH bpf-next] in the title? Right now it's not celar which tree this is targeting so Patchwork is treating this as a netdev patch.

Quoted 45 lines
---
 kernel/bpf/states.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
kernel/bpf/states.c +10 -10
index 5945956a7573..09f87e2bb1e6 100644
@@ -1566,21 +1566,16 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
 	/* add new state to the head of linked list */
 	new = &new_sl->state;
 	err = bpf_copy_verifier_state(new, cur);
-	if (err) {
-		bpf_free_verifier_state(new, false);
-		kfree(new_sl);
-		return err;
-	}
+	if (err)
+		goto free_verifier_state;
+
 	new->insn_idx = insn_idx;
 	verifier_bug_if(new->branches != 1, env,
 			"%s:branches_to_explore=%d insn %d",
 			__func__, new->branches, insn_idx);
 	err = maybe_enter_scc(env, new);
-	if (err) {
-		bpf_free_verifier_state(new, false);
-		kfree(new_sl);
-		return err;
-	}
+	if (err)
+		goto free_verifier_state;
 
 	cur->parent = new;
 	cur->first_insn_idx = insn_idx;
@@ -1588,4 +1583,9 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
 	bpf_clear_jmp_history(cur);
 	list_add(&new_sl->node, head);
 	return 0;
+
+free_verifier_state:
+	bpf_free_verifier_state(new, false);
+	kfree(new_sl);
+	return err;
 }
#2 lore raw

PATCH RESEND 1/1 bpf: Use common error handling code in bpf_is_state_visited()

From: Markus Elfring <[email protected]>
Date: Wed, 17 Jun 2026 21:15:08 +0200

Use an additional label so that a bit of exception handling can be better reused at the end of this function implementation.

This issue was detected by using the Coccinelle software.

---
 kernel/bpf/states.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
kernel/bpf/states.c +10 -10
index 5945956a7573..09f87e2bb1e6 100644
@@ -1566,21 +1566,16 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
 	/* add new state to the head of linked list */
 	new = &new_sl->state;
 	err = bpf_copy_verifier_state(new, cur);
-	if (err) {
-		bpf_free_verifier_state(new, false);
-		kfree(new_sl);
-		return err;
-	}
+	if (err)
+		goto free_verifier_state;
+
 	new->insn_idx = insn_idx;
 	verifier_bug_if(new->branches != 1, env,
 			"%s:branches_to_explore=%d insn %d",
 			__func__, new->branches, insn_idx);
 	err = maybe_enter_scc(env, new);
-	if (err) {
-		bpf_free_verifier_state(new, false);
-		kfree(new_sl);
-		return err;
-	}
+	if (err)
+		goto free_verifier_state;
 
 	cur->parent = new;
 	cur->first_insn_idx = insn_idx;
@@ -1588,4 +1583,9 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
 	bpf_clear_jmp_history(cur);
 	list_add(&new_sl->node, head);
 	return 0;
+
+free_verifier_state:
+	bpf_free_verifier_state(new, false);
+	kfree(new_sl);
+	return err;
 }
Signature · 1 line
2.54.0
#3 lore raw
Quoted 47 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Wed, 17 Jun 2026 21:15:08 +0200

Use an additional label so that a bit of exception handling can be better reused at the end of this function implementation.

This issue was detected by using the Coccinelle software.

---
 kernel/bpf/states.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
kernel/bpf/states.c +5 -10
index 5945956a7573..09f87e2bb1e6 100644
@@ -1566,21 +1566,16 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
        /* add new state to the head of linked list */
        new = &new_sl->state;
        err = bpf_copy_verifier_state(new, cur);
-       if (err) {
-               bpf_free_verifier_state(new, false);
-               kfree(new_sl);
-               return err;
-       }
+       if (err)
+               goto free_verifier_state;
+
        new->insn_idx = insn_idx;
        verifier_bug_if(new->branches != 1, env,
                        "%s:branches_to_explore=%d insn %d",
                        __func__, new->branches, insn_idx);
        err = maybe_enter_scc(env, new);
-       if (err) {
-               bpf_free_verifier_state(new, false);
-               kfree(new_sl);
-               return err;
-       }
+       if (err)
+               goto free_verifier_state;
 
        cur->parent = new;
        cur->first_insn_idx = insn_idx;

kernel/bpf/states.c int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx) · line 1590

Quoted 8 lines
kernel/bpf/states.c +5 -0
@@ -1588,4 +1583,9 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
        bpf_clear_jmp_history(cur);
        list_add(&new_sl->node, head);
        return 0;
+
+free_verifier_state:
+       bpf_free_verifier_state(new, false);
+       kfree(new_sl);
+       return err;

This function is full of 'goto's already.
I prefer to keep the code as-is.

#4 lore raw
Quoted 4 lines
Quoted 4 lines

Use an additional label so that a bit of exception handling can be better reused at the end of this function implementation.

This issue was detected by using the Coccinelle software.

…

Quoted 1 line
Quoted 1 line

+++ b/kernel/bpf/states.c

…

Quoted 4 lines
Quoted 4 lines
kernel/bpf/states.c +0 -0
@@ -1588,4 +1583,9 @@ int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx)
        bpf_clear_jmp_history(cur);
        list_add(&new_sl->node, head);
        return 0;
Quoted 8 lines

kernel/bpf/states.c int bpf_is_state_visited(struct bpf_verifier_env *env, int insn_idx) · line 1590

Quoted 5 lines
kernel/bpf/states.c +5 -0
+
+free_verifier_state:
+       bpf_free_verifier_state(new, false);
+       kfree(new_sl);
+       return err;

This function is full of 'goto's already.
I prefer to keep the code as-is.

How does such a view fit to a known coding style recommendation?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.1#n526

How do you think about to increase the application of scope-based resource management?

Regards,
Markus