Skip to contentSkip to conversation

bpf: do not inline bpf_get_smp_processor_id() with CONFIG_SMP disabled

PATCH In review Andrea Righi 4 participants 6 messages Dec 16, 2024

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

PATCH 1/1 bpf: do not inline bpf_get_smp_processor_id() with CONFIG_SMP disabled

Calling bpf_get_smp_processor_id() in a kernel with CONFIG_SMP disabled can trigger the following bug, as pcpu_hot is unavailable:

[    8.471774] BUG: unable to handle page fault for address: 00000000936a290c
[    8.471849] #PF: supervisor read access in kernel mode
[    8.471881] #PF: error_code(0x0000) - not-present page

Fix by preventing the inlining of bpf_get_smp_processor_id() when CONFIG_SMP disabled.

Fixes 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper")
---
 kernel/bpf/verifier.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/verifier.c +1 -1
index f7f892a52a37..d85413f1a784 100644
@@ -21272,7 +21272,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 			goto next_insn;
 		}
 
-#if defined(CONFIG_X86_64) && !defined(CONFIG_UML)
+#if defined(CONFIG_SMP) && defined(CONFIG_X86_64) && !defined(CONFIG_UML)
 		/* Implement bpf_get_smp_processor_id() inline. */
 		if (insn->imm == BPF_FUNC_get_smp_processor_id &&
 		    verifier_inlines_helper_call(env, insn->imm)) {
Signature · 1 line
2.47.1
maintainer
#1 lore raw
Quoted 7 lines · Andrea Righi

Calling bpf_get_smp_processor_id() in a kernel with CONFIG_SMP disabled can trigger the following bug, as pcpu_hot is unavailable:

[    8.471774] BUG: unable to handle page fault for address: 00000000936a290c
[    8.471849] #PF: supervisor read access in kernel mode
[    8.471881] #PF: error_code(0x0000) - not-present page
Quoted 5 lines

Fix by preventing the inlining of bpf_get_smp_processor_id() when CONFIG_SMP disabled.

Fixes 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper")

lgtm, but can't we instead do sth like this :

kernel/bpf/verifier.c +5 -1
index f7f892a52a37..761c70899754 100644
@@ -21281,11 +21281,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 			 * changed in some incompatible and hard to support
 			 * way, it's fine to back out this inlining logic
 			 */
+#ifdef CONFIG_SMP
 			insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
 			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
 			insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
 			cnt = 3;
-
+#else
+			BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0),
+			cnt = 1;
+#endif
 			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
 			if (!new_prog)
 				return -ENOMEM;

Thanks,
Daniel

#2 lore raw
Quoted 27 lines · Daniel Borkmann
Quoted 7 lines · Andrea Righi

Calling bpf_get_smp_processor_id() in a kernel with CONFIG_SMP disabled can trigger the following bug, as pcpu_hot is unavailable:

[    8.471774] BUG: unable to handle page fault for address: 00000000936a290c
[    8.471849] #PF: supervisor read access in kernel mode
[    8.471881] #PF: error_code(0x0000) - not-present page
Quoted 5 lines

Fix by preventing the inlining of bpf_get_smp_processor_id() when CONFIG_SMP disabled.

Fixes 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper")

lgtm, but can't we instead do sth like this :

kernel/bpf/verifier.c +1 -0
index f7f892a52a37..761c70899754 100644
@@ -21281,11 +21281,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 			 * changed in some incompatible and hard to support
 			 * way, it's fine to back out this inlining logic
 			 */
+#ifdef CONFIG_SMP
 			insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
 			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 21295

Quoted 10 lines
kernel/bpf/verifier.c +4 -1
 			insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
 			cnt = 3;
-
+#else
+			BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0),
+			cnt = 1;
+#endif
 			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
 			if (!new_prog)
 				return -ENOMEM;

That works as well (just tested) and it's probably better since we're basically inlining the return 0. Do you want me to send a v2 with this?

Thanks,
-Andrea

maintainer
#3 lore raw
Quoted 33 lines · Andrea Righi
Quoted 32 lines · Daniel Borkmann
Quoted 7 lines · Andrea Righi

Calling bpf_get_smp_processor_id() in a kernel with CONFIG_SMP disabled can trigger the following bug, as pcpu_hot is unavailable:

[    8.471774] BUG: unable to handle page fault for address: 00000000936a290c
[    8.471849] #PF: supervisor read access in kernel mode
[    8.471881] #PF: error_code(0x0000) - not-present page
Quoted 5 lines

Fix by preventing the inlining of bpf_get_smp_processor_id() when CONFIG_SMP disabled.

Fixes 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper")

lgtm, but can't we instead do sth like this :

kernel/bpf/verifier.c +3 -1
index f7f892a52a37..761c70899754 100644
@@ -21281,11 +21281,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
 			 * changed in some incompatible and hard to support
 			 * way, it's fine to back out this inlining logic
 			 */
+#ifdef CONFIG_SMP
 			insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
 			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
 			insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
 			cnt = 3;
-
+#else
+			BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0),
Quoted 8 lines

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 21295

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+			cnt = 1;
+#endif
 			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
 			if (!new_prog)
 				return -ENOMEM;

That works as well (just tested) and it's probably better since we're basically inlining the return 0. Do you want me to send a v2 with this?

I think both Andrea's and Daniel's versions of the fix are good. Note, however, that I missed one more configuration variable when making bpf_get_smp_processor_id() inlinable: CONFIG_DEBUG_PREEMPT.

Helper body:

    BPF_CALL_0(bpf_get_smp_processor_id)
    {
    	return smp_processor_id();
    }

smp_processor_id definition:

    #ifdef CONFIG_DEBUG_PREEMPT
      extern unsigned int debug_smp_processor_id(void);
    # define smp_processor_id() debug_smp_processor_id()
    #else
    # define smp_processor_id() __smp_processor_id()
    #endif

Thanks,
Eduard.

#4 lore raw
Quoted 55 lines · Eduard Zingerman
Quoted 33 lines · Andrea Righi
Quoted 32 lines · Daniel Borkmann
Quoted 7 lines · Andrea Righi

Calling bpf_get_smp_processor_id() in a kernel with CONFIG_SMP disabled can trigger the following bug, as pcpu_hot is unavailable:

[    8.471774] BUG: unable to handle page fault for address: 00000000936a290c
[    8.471849] #PF: supervisor read access in kernel mode
[    8.471881] #PF: error_code(0x0000) - not-present page
Quoted 5 lines

Fix by preventing the inlining of bpf_get_smp_processor_id() when CONFIG_SMP disabled.

Fixes 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper")

lgtm, but can't we instead do sth like this :

kernel/bpf/verifier.c +3 -1
index f7f892a52a37..761c70899754 100644
@@ -21281,11 +21281,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
                     * changed in some incompatible and hard to support
                     * way, it's fine to back out this inlining logic
                     */
+#ifdef CONFIG_SMP
                    insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
                    insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
                    insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
                    cnt = 3;
-
+#else
+                   BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0),
Quoted 8 lines

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 21295

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+                   cnt = 1;
+#endif
                    new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
                    if (!new_prog)
                            return -ENOMEM;

That works as well (just tested) and it's probably better since we're basically inlining the return 0. Do you want me to send a v2 with this?

I think both Andrea's and Daniel's versions of the fix are good. Note, however, that I missed one more configuration variable when making bpf_get_smp_processor_id() inlinable: CONFIG_DEBUG_PREEMPT.

Helper body:

    BPF_CALL_0(bpf_get_smp_processor_id)
    {
        return smp_processor_id();
    }
Quoted 8 lines

smp_processor_id definition:

    #ifdef CONFIG_DEBUG_PREEMPT
      extern unsigned int debug_smp_processor_id(void);
    # define smp_processor_id() debug_smp_processor_id()
    #else
    # define smp_processor_id() __smp_processor_id()
    #endif

No. It's fine as-is.
We inline raw_smp_processor_id().

maintainer
#5 lore raw
In reply to Andrea Righi
Quoted 33 lines · Andrea Righi
Quoted 32 lines · Daniel Borkmann
Quoted 7 lines · Andrea Righi

Calling bpf_get_smp_processor_id() in a kernel with CONFIG_SMP disabled can trigger the following bug, as pcpu_hot is unavailable:

[    8.471774] BUG: unable to handle page fault for address: 00000000936a290c
[    8.471849] #PF: supervisor read access in kernel mode
[    8.471881] #PF: error_code(0x0000) - not-present page
Quoted 5 lines

Fix by preventing the inlining of bpf_get_smp_processor_id() when CONFIG_SMP disabled.

Fixes 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper")

lgtm, but can't we instead do sth like this :

kernel/bpf/verifier.c +3 -1
index f7f892a52a37..761c70899754 100644
@@ -21281,11 +21281,15 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
  			 * changed in some incompatible and hard to support
  			 * way, it's fine to back out this inlining logic
  			 */
+#ifdef CONFIG_SMP
  			insn_buf[0] = BPF_MOV32_IMM(BPF_REG_0, (u32)(unsigned long)&pcpu_hot.cpu_number);
  			insn_buf[1] = BPF_MOV64_PERCPU_REG(BPF_REG_0, BPF_REG_0);
  			insn_buf[2] = BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_0, 0);
  			cnt = 3;
-
+#else
+			BPF_ALU32_REG(BPF_XOR, BPF_REG_0, BPF_REG_0),
Quoted 8 lines

kernel/bpf/verifier.c static int do_misc_fixups(struct bpf_verifier_env *env) · line 21295

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+			cnt = 1;
+#endif
  			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
  			if (!new_prog)
  				return -ENOMEM;

That works as well (just tested) and it's probably better since we're basically inlining the return 0. Do you want me to send a v2 with this?

Yes, pls send v2, I think this is better than explicitly calling the helper under !CONFIG_SMP.

Thanks,
Daniel