bpf, arm64: Call bpf_jit_binary_pack_finalize() in bpf_jit_free()
Subsystems (3)
PATCH 1/1 bpf, arm64: Call bpf_jit_binary_pack_finalize() in bpf_jit_free()
The current implementation seems incorrect and does NOT match the comment above, use bpf_jit_binary_pack_finalize() instead.
--- arch/arm64/net/bpf_jit_comp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/net/bpf_jit_comp.c +1 -2
@@ -3064,8 +3064,7 @@ void bpf_jit_free(struct bpf_prog *prog)* before freeing it. */ if (jit_data) { -bpf_arch_text_copy (&jit_data->ro_header->size, &jit_data->header->size, -sizeof (jit_data->header->size)); +bpf_jit_binary_pack_finalize (jit_data->ro_header, jit_data->header);kfree (jit_data); } prog->bpf_func -=cfi_get_offset ();
Signature · 1 line
2.27.0
Puranjay Mohan
maintainerQuoted 14 lines · Hengqi Chen
The current implementation seems incorrect and does NOT match the comment above, use
bpf_jit_binary_pack_finalize()instead.Fixes 1dad391daef1 ("bpf, arm64: use bpf_prog_pack for memory management")Signed-off-by Hengqi Chen <[email protected]>--- arch/arm64/net/bpf_jit_comp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)arch/arm64/net/bpf_jit_comp.c +0
-0
arch/arm64/net/bpf_jit_comp.c void bpf_jit_free(struct bpf_prog *prog) · line 3068
Quoted 7 lines
arch/arm64/net/bpf_jit_comp.c +1
-2@@ -3064,8 +3064,7 @@ void bpf_jit_free(struct bpf_prog *prog)* before freeing it. */ if (jit_data) { -bpf_arch_text_copy (&jit_data->ro_header->size, &jit_data->header->size, -sizeof (jit_data->header->size)); +bpf_jit_binary_pack_finalize (jit_data->ro_header, jit_data->header);kfree (jit_data);
Thanks for this patch!
So, this is fixing a bug because bpf_jit_binary_pack_finalize() will do kvfree(rw_header); but without it currently, jit_data->header is never freed.
But I think we shouldn't use bpf_jit_binary_pack_finalize() here as it copies the whole rw_header to ro_header using bpf_arch_text_copy() which is an expensive operation (patch_map/unmap in loop + flush_icache_range()) and not needed here because we are going to free ro_header anyway.
We only need to copy jit_data->header->size to jit_data->ro_header->size because this size is later used by bpf_jit_binary_pack_free(), see comment above bpf_jit_binary_pack_free().
How I suggest we should fix the code and the comment:
-- >8 --
arch/arm64/net/bpf_jit_comp.c +4 -2
@@ -3093,12 +3093,14 @@ void bpf_jit_free(struct bpf_prog *prog)/* * If we fail the final pass of JIT (from jit_subprogs), -* the program may not be finalized yet. Call finalize here -* before freeing it. +* the program may not be finalized yet. Copy the header size +* from rw_header to ro_header before freeing the ro_header +* with bpf_jit_binary_pack_free(). */ if (jit_data) {bpf_arch_text_copy (&jit_data->ro_header->size, &jit_data->header->size,sizeof (jit_data->header->size)); +kvfree (jit_data->header);kfree (jit_data); } prog->bpf_func -=cfi_get_offset ();
-- 8< --
Song,
Do you think this optimization is worth it or should we just call bpf_jit_binary_pack_finalize() here like this patch is doing?
Thanks,
Puranjay
Song Liu
reviewerSorry for the late reply.
On Thu, Aug 28, 2025 at 5:10 AM Puranjay Mohan <[email protected]> wrote:
[...]
Quoted 44 lines
Thanks for this patch!
So, this is fixing a bug because
bpf_jit_binary_pack_finalize()will dokvfree(rw_header); but without it currently,jit_data->headeris never freed.But I think we shouldn't use
bpf_jit_binary_pack_finalize()here as it copies the wholerw_headertoro_headerusingbpf_arch_text_copy()which is an expensive operation (patch_map/unmap in loop +flush_icache_range()) and not needed here because we are going to freero_headeranyway.We only need to copy
jit_data->header->sizetojit_data->ro_header->sizebecause this size is later used bybpf_jit_binary_pack_free(), see comment abovebpf_jit_binary_pack_free().How I suggest we should fix the code and the comment:
-- >8 --
arch/arm64/net/bpf_jit_comp.c +4
-3@@ -3093,12 +3093,14 @@ void bpf_jit_free(struct bpf_prog *prog)/* * If we fail the final pass of JIT (from jit_subprogs), -* the program may not be finalized yet. Call finalize here -* before freeing it. +* the program may not be finalized yet. Copy the header size +* from rw_header to ro_header before freeing the ro_header +* with bpf_jit_binary_pack_free(). */ if (jit_data) {bpf_arch_text_copy (&jit_data->ro_header->size, &jit_data->header->size,sizeof (jit_data->header->size)); +kvfree (jit_data->header);kfree (jit_data); } prog->bpf_func -=cfi_get_offset (); -- 8< --
Quoted 4 lines
Song,
Do you think this optimization is worth it or should we just call
bpf_jit_binary_pack_finalize()here like this patch is doing?
This is a good optimization. However, given this is not a hot path, I don't have a strong preference either way. At the moment, most other architectures use bpf_jit_binary_pack_finalize(), so it is good to just use bpf_jit_binary_pack_finalize and keep the logic consistent.
In the longer term, we can consider refactoring bpf_jit_free so that multiple architectures can share code. After this patch, bpf_jit_free for x86_64 and arm64 are very similar to each other. There are likely some opportunities to reduce code duplications.
Thanks,
Song
Quoted 50 lines · Song Liu
Sorry for the late reply.
On Thu, Aug 28, 2025 at 5:10 AM Puranjay Mohan <[email protected]> wrote:
[...]Quoted 46 lines
Thanks for this patch!
So, this is fixing a bug because
bpf_jit_binary_pack_finalize()will dokvfree(rw_header); but without it currently,jit_data->headeris never freed.But I think we shouldn't use
bpf_jit_binary_pack_finalize()here as it copies the wholerw_headertoro_headerusingbpf_arch_text_copy()which is an expensive operation (patch_map/unmap in loop +flush_icache_range()) and not needed here because we are going to freero_headeranyway.We only need to copy
jit_data->header->sizetojit_data->ro_header->sizebecause this size is later used bybpf_jit_binary_pack_free(), see comment abovebpf_jit_binary_pack_free().How I suggest we should fix the code and the comment:
-- >8 --
arch/arm64/net/bpf_jit_comp.c +4
-3@@ -3093,12 +3093,14 @@ void bpf_jit_free(struct bpf_prog *prog)/* * If we fail the final pass of JIT (from jit_subprogs), -* the program may not be finalized yet. Call finalize here -* before freeing it. +* the program may not be finalized yet. Copy the header size +* from rw_header to ro_header before freeing the ro_header +* with bpf_jit_binary_pack_free(). */ if (jit_data) {bpf_arch_text_copy (&jit_data->ro_header->size, &jit_data->header->size,sizeof (jit_data->header->size)); +kvfree (jit_data->header);kfree (jit_data); } prog->bpf_func -=cfi_get_offset (); -- 8< --Song,
Quoted 8 lines
Quoted 2 lines
Do you think this optimization is worth it or should we just call
bpf_jit_binary_pack_finalize()here like this patch is doing?This is a good optimization. However, given this is not a hot path, I don't have a strong preference either way. At the moment, most other architectures use
bpf_jit_binary_pack_finalize(), so it is good to just usebpf_jit_binary_pack_finalizeand keep the logic consistent.
So, in that case we can merge this patch.
Thanks,
Puranjay
Quoted 62 lines · Puranjay Mohan
Quoted 50 lines · Song Liu
Sorry for the late reply.
On Thu, Aug 28, 2025 at 5:10 AM Puranjay Mohan <[email protected]> wrote:
[...]Quoted 46 lines
Thanks for this patch!
So, this is fixing a bug because
bpf_jit_binary_pack_finalize()will dokvfree(rw_header); but without it currently,jit_data->headeris never freed.But I think we shouldn't use
bpf_jit_binary_pack_finalize()here as it copies the wholerw_headertoro_headerusingbpf_arch_text_copy()which is an expensive operation (patch_map/unmap in loop +flush_icache_range()) and not needed here because we are going to freero_headeranyway.We only need to copy
jit_data->header->sizetojit_data->ro_header->sizebecause this size is later used bybpf_jit_binary_pack_free(), see comment abovebpf_jit_binary_pack_free().How I suggest we should fix the code and the comment:
-- >8 --
arch/arm64/net/bpf_jit_comp.c +4
-3@@ -3093,12 +3093,14 @@ void bpf_jit_free(struct bpf_prog *prog)/* * If we fail the final pass of JIT (from jit_subprogs), -* the program may not be finalized yet. Call finalize here -* before freeing it. +* the program may not be finalized yet. Copy the header size +* from rw_header to ro_header before freeing the ro_header +* with bpf_jit_binary_pack_free(). */ if (jit_data) {bpf_arch_text_copy (&jit_data->ro_header->size, &jit_data->header->size,sizeof (jit_data->header->size)); +kvfree (jit_data->header);kfree (jit_data); } prog->bpf_func -=cfi_get_offset (); -- 8< --Song,
Quoted 8 lines
Quoted 2 lines
Do you think this optimization is worth it or should we just call
bpf_jit_binary_pack_finalize()here like this patch is doing?This is a good optimization. However, given this is not a hot path, I don't have a strong preference either way. At the moment, most other architectures use
bpf_jit_binary_pack_finalize(), so it is good to just usebpf_jit_binary_pack_finalizeand keep the logic consistent.
Quoted 3 lines
So, in that case we can merge this patch.
Acked-by Puranjay Mohan <[email protected]>
It's out of patchwork.
Hengqi,
pls repost.
Quoted 64 lines · Alexei Starovoitov
Quoted 62 lines · Puranjay Mohan
Quoted 58 lines · Song Liu
Sorry for the late reply.
On Thu, Aug 28, 2025 at 5:10 AM Puranjay Mohan <[email protected]> wrote:
[...]Quoted 44 lines
Thanks for this patch!
So, this is fixing a bug because
bpf_jit_binary_pack_finalize()will dokvfree(rw_header); but without it currently,jit_data->headeris never freed.But I think we shouldn't use
bpf_jit_binary_pack_finalize()here as it copies the wholerw_headertoro_headerusingbpf_arch_text_copy()which is an expensive operation (patch_map/unmap in loop +flush_icache_range()) and not needed here because we are going to freero_headeranyway.We only need to copy
jit_data->header->sizetojit_data->ro_header->sizebecause this size is later used bybpf_jit_binary_pack_free(), see comment abovebpf_jit_binary_pack_free().How I suggest we should fix the code and the comment:
-- >8 --
arch/arm64/net/bpf_jit_comp.c +4
-3@@ -3093,12 +3093,14 @@ void bpf_jit_free(struct bpf_prog *prog)/* * If we fail the final pass of JIT (from jit_subprogs), -* the program may not be finalized yet. Call finalize here -* before freeing it. +* the program may not be finalized yet. Copy the header size +* from rw_header to ro_header before freeing the ro_header +* with bpf_jit_binary_pack_free(). */ if (jit_data) {bpf_arch_text_copy (&jit_data->ro_header->size, &jit_data->header->size,sizeof (jit_data->header->size)); +kvfree (jit_data->header);kfree (jit_data); } prog->bpf_func -=cfi_get_offset (); -- 8< --Quoted 4 lines
Song,
Do you think this optimization is worth it or should we just call
bpf_jit_binary_pack_finalize()here like this patch is doing?This is a good optimization. However, given this is not a hot path, I don't have a strong preference either way. At the moment, most other architectures use
bpf_jit_binary_pack_finalize(), so it is good to just usebpf_jit_binary_pack_finalizeand keep the logic consistent.
Quoted 7 lines
Quoted 3 lines
So, in that case we can merge this patch.
Acked-by Puranjay Mohan <[email protected]>It's out of patchwork.
Hengqi,
pls repost.
OK, just resend.