Skip to contentSkip to conversation

fixes for bpf_prog_pack

PATCH Superseded Song Liu 3 participants 6 messages Mar 2, 2022

Subsystems (6)
Networking Network drivers x86 BPF +2 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Song Liu

author reviewer
#0 lore raw

cover fixes for bpf_prog_pack

Two fixes for bpf_prog_pack.

Song Liu (2):
x86: disable HAVE_ARCH_HUGE_VMALLOC on 32-bit x86 bpf, x86: set header->size properly before freeing it

 arch/x86/Kconfig            | 2 +-
 arch/x86/net/bpf_jit_comp.c | 6 +++++-
 kernel/bpf/core.c           | 7 ++++---
 3 files changed, 10 insertions(+), 5 deletions(-)

--
2.30.2

author reviewer
#3 lore raw

PATCH 2/2 bpf, x86: set header->size properly before freeing it

On do_jit failure path, the header is freed by bpf_jit_binary_pack_free. While bpf_jit_binary_pack_free doesn't require proper ro_header->size, bpf_prog_pack_free still uses it. Set header->size in bpf_int_jit_compile before calling bpf_jit_binary_pack_free.

Fixes 1022a5498f6f ("bpf, x86_64: Use bpf_jit_binary_pack_alloc")
Fixes 33c9805860e5 ("bpf: Introduce bpf_jit_binary_pack_[alloc|finalize|free]")
---
 arch/x86/net/bpf_jit_comp.c | 6 +++++-
 kernel/bpf/core.c           | 7 ++++---
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/net/bpf_jit_comp.c +5 -1
index c7db0fe4de2f..b923d81ff6f9 100644
@@ -2330,8 +2330,12 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 		if (proglen <= 0) {
 out_image:
 			image = NULL;
-			if (header)
+			if (header) {
+				/* set header->size for bpf_arch_text_copy */
+				bpf_arch_text_copy(&header->size, &rw_header->size,
+						   sizeof(rw_header->size));
 				bpf_jit_binary_pack_free(header, rw_header);
+			}
 			prog = orig_prog;
 			goto out_addrs;
 		}
kernel/bpf/core.c +4 -3
index ebb0193d07f0..da587e4619e0 100644
@@ -1112,13 +1112,14 @@ int bpf_jit_binary_pack_finalize(struct bpf_prog *prog,
  *   1) when the program is freed after;
  *   2) when the JIT engine fails (before bpf_jit_binary_pack_finalize).
  * For case 2), we need to free both the RO memory and the RW buffer.
- * Also, ro_header->size in 2) is not properly set yet, so rw_header->size
- * is used for uncharge.
+ *
+ * If bpf_jit_binary_pack_free is called before _finalize (jit error),
+ * it is necessary to set ro_header->size properly before freeing it.
  */
 void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
 			      struct bpf_binary_header *rw_header)
 {
-	u32 size = rw_header ? rw_header->size : ro_header->size;
+	u32 size = ro_header->size;
 
 	bpf_prog_pack_free(ro_header);
 	kvfree(rw_header);
Signature · 1 line
2.30.2
author reviewer
#1 lore raw

PATCH 1/2 x86: disable HAVE_ARCH_HUGE_VMALLOC on 32-bit x86

In reply to Song Liu

kernel test robot reported kernel BUG like:

[ 44.587744][ T1] kernel BUG at arch/x86/mm/physaddr.c:76!
[ 44.590151][ T1] __vmalloc_area_node (mm/vmalloc.c:622 mm/vmalloc.c:2995)
[ 44.590151][ T1] __vmalloc_node_range (mm/vmalloc.c:3108)
[ 44.590151][ T1] __vmalloc_node (mm/vmalloc.c:3157)

which is triggered with HAVE_ARCH_HUGE_VMALLOC on 32-bit x86. Since BPF only uses HAVE_ARCH_HUGE_VMALLOC for x86_64, turn it off for 32-bit x86.

Fixes fac54e2bfb5b ("x86/Kconfig: Select HAVE_ARCH_HUGE_VMALLOC with HAVE_ARCH_HUGE_VMAP")
---
 arch/x86/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/Kconfig +1 -1
index 995f2dc28631..9b356da6f46b 100644
@@ -158,7 +158,7 @@ config X86
 	select HAVE_ALIGNED_STRUCT_PAGE		if SLUB
 	select HAVE_ARCH_AUDITSYSCALL
 	select HAVE_ARCH_HUGE_VMAP		if X86_64 || X86_PAE
-	select HAVE_ARCH_HUGE_VMALLOC		if HAVE_ARCH_HUGE_VMAP
+	select HAVE_ARCH_HUGE_VMALLOC		if X86_64
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_ARCH_KASAN			if X86_64
Signature · 1 line
2.30.2
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next 1/2] x86: disable HAVE_ARCH_HUGE_VMALLOC on 32-bit x86

Quoted 7 lines · Song Liu

kernel test robot reported kernel BUG like:

[ 44.587744][ T1] kernel BUG at arch/x86/mm/physaddr.c:76!
[ 44.590151][ T1] __vmalloc_area_node (mm/vmalloc.c:622 mm/vmalloc.c:2995)
[ 44.590151][ T1] __vmalloc_node_range (mm/vmalloc.c:3108)
[ 44.590151][ T1] __vmalloc_node (mm/vmalloc.c:3157)
Quoted 6 lines

which is triggered with HAVE_ARCH_HUGE_VMALLOC on 32-bit x86. Since BPF only uses HAVE_ARCH_HUGE_VMALLOC for x86_64, turn it off for 32-bit x86.

Fixes fac54e2bfb5b ("x86/Kconfig: Select HAVE_ARCH_HUGE_VMALLOC with HAVE_ARCH_HUGE_VMAP")
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH bpf-next 2/2] bpf, x86: set header->size properly before freeing it

In reply to Song Liu
Quoted 9 lines · Song Liu

On do_jit failure path, the header is freed by bpf_jit_binary_pack_free. While bpf_jit_binary_pack_free doesn't require proper ro_header->size, bpf_prog_pack_free still uses it. Set header->size in bpf_int_jit_compile before calling bpf_jit_binary_pack_free.

Fixes 1022a5498f6f ("bpf, x86_64: Use bpf_jit_binary_pack_alloc")
Fixes 33c9805860e5 ("bpf: Introduce bpf_jit_binary_pack_[alloc|finalize|free]")

LGTM with a nit below related to comments.

Quoted 10 lines
---
  arch/x86/net/bpf_jit_comp.c | 6 +++++-
  kernel/bpf/core.c           | 7 ++++---
  2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/net/bpf_jit_comp.c +0 -0
index c7db0fe4de2f..b923d81ff6f9 100644

arch/x86/net/bpf_jit_comp.c struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) · line 2334

Quoted 6 lines
arch/x86/net/bpf_jit_comp.c +2 -1
@@ -2330,8 +2330,12 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
  		if (proglen <= 0) {
  out_image:
  			image = NULL;
-			if (header)
+			if (header) {
+				/* set header->size for bpf_arch_text_copy */

This comment is confusing. Setting header->size is not for bpf_arch_text_copy. Probably you mean 'by bpf_arch_text_copy? I think this comment is not necessary.

Quoted 12 lines
arch/x86/net/bpf_jit_comp.c +3 -0
+				bpf_arch_text_copy(&header->size, &rw_header->size,
+						   sizeof(rw_header->size));
  				bpf_jit_binary_pack_free(header, rw_header);
+			}
  			prog = orig_prog;
  			goto out_addrs;
  		}
kernel/bpf/core.c +0 -0
index ebb0193d07f0..da587e4619e0 100644

kernel/bpf/core.c int bpf_jit_binary_pack_finalize(struct bpf_prog *prog, · line 1116

Quoted 7 lines
kernel/bpf/core.c +2 -2
@@ -1112,13 +1112,14 @@ int bpf_jit_binary_pack_finalize(struct bpf_prog *prog,
   *   1) when the program is freed after;
   *   2) when the JIT engine fails (before bpf_jit_binary_pack_finalize).
   * For case 2), we need to free both the RO memory and the RW buffer.
- * Also, ro_header->size in 2) is not properly set yet, so rw_header->size
- * is used for uncharge.
+ *
+ * If bpf_jit_binary_pack_free is called before _finalize (jit error),

Do you mean bpf_jit_binary_pack_free() is called before calling bpf_jit_binary_pack_finalize()? This seems not the case.

Quoted 10 lines
kernel/bpf/core.c +2 -1
+ * it is necessary to set ro_header->size properly before freeing it.
   */
  void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
  			      struct bpf_binary_header *rw_header)
  {
-	u32 size = rw_header ? rw_header->size : ro_header->size;
+	u32 size = ro_header->size;
 
  	bpf_prog_pack_free(ro_header);
  	kvfree(rw_header);
#5 lore raw

Re: [PATCH bpf-next 2/2] bpf, x86: set header->size properly before freeing it

Quoted 30 lines · Yonghong Song
Quoted 8 lines · Song Liu

On do_jit failure path, the header is freed by bpf_jit_binary_pack_free. While bpf_jit_binary_pack_free doesn't require proper ro_header->size, bpf_prog_pack_free still uses it. Set header->size in bpf_int_jit_compile before calling bpf_jit_binary_pack_free.

Fixes 1022a5498f6f ("bpf, x86_64: Use bpf_jit_binary_pack_alloc")
Fixes 33c9805860e5 ("bpf: Introduce bpf_jit_binary_pack_[alloc|finalize|free]")

LGTM with a nit below related to comments.

Quoted 10 lines
---
 arch/x86/net/bpf_jit_comp.c | 6 +++++-
 kernel/bpf/core.c           | 7 ++++---
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/net/bpf_jit_comp.c +0 -0
index c7db0fe4de2f..b923d81ff6f9 100644
@@ -2330,8 +2330,12 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
 		if (proglen <= 0) {
Quoted 8 lines

arch/x86/net/bpf_jit_comp.c struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog) · line 2334

Quoted 5 lines
arch/x86/net/bpf_jit_comp.c +2 -1
 out_image:
 			image = NULL;
-			if (header)
+			if (header) {
+				/* set header->size for bpf_arch_text_copy */

This comment is confusing. Setting header->size is not for bpf_arch_text_copy. Probably you mean 'by bpf_arch_text_copy? I think this comment is not necessary.

I meant to say set header->size for bpf_jit_binary_pack_free(). I guess it is not really necessary. Let me remove it.

Quoted 15 lines
Quoted 15 lines
arch/x86/net/bpf_jit_comp.c +3 -0
+				bpf_arch_text_copy(&header->size, &rw_header->size,
+						   sizeof(rw_header->size));
 				bpf_jit_binary_pack_free(header, rw_header);
+			}
 			prog = orig_prog;
 			goto out_addrs;
 		}
kernel/bpf/core.c +0 -0
index ebb0193d07f0..da587e4619e0 100644
@@ -1112,13 +1112,14 @@ int bpf_jit_binary_pack_finalize(struct bpf_prog *prog,
  *   1) when the program is freed after;
  *   2) when the JIT engine fails (before bpf_jit_binary_pack_finalize).
Quoted 8 lines

kernel/bpf/core.c int bpf_jit_binary_pack_finalize(struct bpf_prog *prog, · line 1116

Quoted 5 lines
kernel/bpf/core.c +2 -2
  * For case 2), we need to free both the RO memory and the RW buffer.
- * Also, ro_header->size in 2) is not properly set yet, so rw_header->size
- * is used for uncharge.
+ *
+ * If bpf_jit_binary_pack_free is called before _finalize (jit error),

Do you mean bpf_jit_binary_pack_free() is called before calling bpf_jit_binary_pack_finalize()? This seems not the case.

Now I see this is a little confusing. Let me rephrase it in v2.

Thanks,
Song

Quoted 10 lines
Quoted 10 lines
kernel/bpf/core.c +2 -1
+ * it is necessary to set ro_header->size properly before freeing it.
  */
 void bpf_jit_binary_pack_free(struct bpf_binary_header *ro_header,
 			      struct bpf_binary_header *rw_header)
 {
-	u32 size = rw_header ? rw_header->size : ro_header->size;
+	u32 size = ro_header->size;
   	bpf_prog_pack_free(ro_header);
 	kvfree(rw_header);