diff mbox series

[v3,bpf-next,1/5] bpf: Let callers of btf_parse_kptr() track life cycle of prog btf

Message ID 20240809005131.3916464-2-amery.hung@bytedance.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Support bpf_kptr_xchg into local kptr | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 42 this patch: 42
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 9 maintainers not CCed: kpsingh@kernel.org haoluo@google.com john.fastabend@gmail.com jolsa@kernel.org yonghong.song@linux.dev martin.lau@linux.dev song@kernel.org eddyz87@gmail.com sdf@fomichev.me
netdev/build_clang success Errors and warnings before: 43 this patch: 43
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 45 this patch: 45
netdev/checkpatch warning WARNING: From:/Signed-off-by: email address mismatch: 'From: Amery Hung <ameryhung@gmail.com>' != 'Signed-off-by: Amery Hung <amery.hung@bytedance.com>'
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 fail Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18

Commit Message

Amery Hung Aug. 9, 2024, 12:51 a.m. UTC
btf_parse_kptr() and btf_record_free() do btf_get() and btf_put()
respectively when working on btf_record in program and map if there are
kptr fields. If the kptr is from program BTF, since both callers has
already tracked the life cycle of program BTF, it is safe to remove the
btf_get() and btf_put().

This change prevents memory leak of program BTF later when we start
searching for kptr fields when building btf_record for program. It can
happen when the btf fd is closed. The btf_put() corresponding to the
btf_get() in btf_parse_kptr() was supposed to be called by
btf_record_free() in btf_free_struct_meta_tab() in btf_free(). However,
it will never happen since the invocation of btf_free() depends on the
refcount of the btf to become 0 in the first place.

Signed-off-by: Amery Hung <amery.hung@bytedance.com>
---
 kernel/bpf/btf.c     | 2 +-
 kernel/bpf/syscall.c | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)

Comments

Martin KaFai Lau Aug. 9, 2024, 11:06 p.m. UTC | #1
On 8/8/24 5:51 PM, Amery Hung wrote:
> btf_parse_kptr() and btf_record_free() do btf_get() and btf_put()
> respectively when working on btf_record in program and map if there are
> kptr fields. If the kptr is from program BTF, since both callers has
> already tracked the life cycle of program BTF, it is safe to remove the
> btf_get() and btf_put().
> 
> This change prevents memory leak of program BTF later when we start
> searching for kptr fields when building btf_record for program. It can
> happen when the btf fd is closed. The btf_put() corresponding to the
> btf_get() in btf_parse_kptr() was supposed to be called by
> btf_record_free() in btf_free_struct_meta_tab() in btf_free(). However,
> it will never happen since the invocation of btf_free() depends on the
> refcount of the btf to become 0 in the first place.
> 
> Signed-off-by: Amery Hung <amery.hung@bytedance.com>

Acked-by: Martin KaFai Lau <martin.lau@kernel.org>

Need to fix the checkpatch warning though:

WARNING: From:/Signed-off-by: email address mismatch: 'From: Amery Hung 
<ameryhung@gmail.com>' != 'Signed-off-by: Amery Hung <amery.hung@bytedance.com>'
Martin KaFai Lau Aug. 10, 2024, 1:29 a.m. UTC | #2
On 8/9/24 4:06 PM, Martin KaFai Lau wrote:
> On 8/8/24 5:51 PM, Amery Hung wrote:
>> btf_parse_kptr() and btf_record_free() do btf_get() and btf_put()
>> respectively when working on btf_record in program and map if there are
>> kptr fields. If the kptr is from program BTF, since both callers has
>> already tracked the life cycle of program BTF, it is safe to remove the
>> btf_get() and btf_put().
>>
>> This change prevents memory leak of program BTF later when we start
>> searching for kptr fields when building btf_record for program. It can
>> happen when the btf fd is closed. The btf_put() corresponding to the
>> btf_get() in btf_parse_kptr() was supposed to be called by
>> btf_record_free() in btf_free_struct_meta_tab() in btf_free(). However,
>> it will never happen since the invocation of btf_free() depends on the
>> refcount of the btf to become 0 in the first place.
>>
>> Signed-off-by: Amery Hung <amery.hung@bytedance.com>
> 
> Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
> 
> Need to fix the checkpatch warning though:
> 
> WARNING: From:/Signed-off-by: email address mismatch: 'From: Amery Hung 
> <ameryhung@gmail.com>' != 'Signed-off-by: Amery Hung <amery.hung@bytedance.com>'
> 

There is a veristat failure also:

https://github.com/kernel-patches/bpf/actions/runs/10311824065/job/28546213338

|File                  |Program                         |Verdict                |States Diff (%)|
|----------------------|--------------------------------|-----------------------|---------------|
|local_kptr_stash.bpf.o|refcount_acquire_without_unstash|success -> failure (!!)|-100.00 %      |
|local_kptr_stash.bpf.o|stash_local_with_root           |success -> failure (!!)|-100.00 %      |
|local_kptr_stash.bpf.o|stash_plain                     |success -> failure (!!)|-100.00 %      |
|local_kptr_stash.bpf.o|stash_rb_nodes                  |success -> failure (!!)|-100.00 %      |
|local_kptr_stash.bpf.o|stash_refcounted_node           |success -> failure (!!)|-100.00 %      |
|local_kptr_stash.bpf.o|stash_test_ref_kfunc            |success -> failure (!!)|-100.00 %      |
|local_kptr_stash.bpf.o|unstash_rb_node                 |success -> failure (!!)|-100.00 %      |
Hou Tao Aug. 10, 2024, 10:14 a.m. UTC | #3
On 8/9/2024 8:51 AM, Amery Hung wrote:
> btf_parse_kptr() and btf_record_free() do btf_get() and btf_put()
> respectively when working on btf_record in program and map if there are
> kptr fields. If the kptr is from program BTF, since both callers has
> already tracked the life cycle of program BTF, it is safe to remove the
> btf_get() and btf_put().
>
> This change prevents memory leak of program BTF later when we start
> searching for kptr fields when building btf_record for program. It can
> happen when the btf fd is closed. The btf_put() corresponding to the
> btf_get() in btf_parse_kptr() was supposed to be called by
> btf_record_free() in btf_free_struct_meta_tab() in btf_free(). However,
> it will never happen since the invocation of btf_free() depends on the
> refcount of the btf to become 0 in the first place.
>
> Signed-off-by: Amery Hung <amery.hung@bytedance.com>

Acked-by: Hou Tao <houtao1@huawei.com>
diff mbox series

Patch

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 95426d5b634e..deacf9d7b276 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3759,6 +3759,7 @@  static int btf_find_field(const struct btf *btf, const struct btf_type *t,
 	return -EINVAL;
 }
 
+/* Callers have to ensure the life cycle of btf if it is program BTF */
 static int btf_parse_kptr(const struct btf *btf, struct btf_field *field,
 			  struct btf_field_info *info)
 {
@@ -3787,7 +3788,6 @@  static int btf_parse_kptr(const struct btf *btf, struct btf_field *field,
 		field->kptr.dtor = NULL;
 		id = info->kptr.type_id;
 		kptr_btf = (struct btf *)btf;
-		btf_get(kptr_btf);
 		goto found_dtor;
 	}
 	if (id < 0)
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 869265852d51..4003e1025264 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -550,7 +550,8 @@  void btf_record_free(struct btf_record *rec)
 		case BPF_KPTR_PERCPU:
 			if (rec->fields[i].kptr.module)
 				module_put(rec->fields[i].kptr.module);
-			btf_put(rec->fields[i].kptr.btf);
+			if (btf_is_kernel(rec->fields[i].kptr.btf))
+				btf_put(rec->fields[i].kptr.btf);
 			break;
 		case BPF_LIST_HEAD:
 		case BPF_LIST_NODE:
@@ -596,7 +597,8 @@  struct btf_record *btf_record_dup(const struct btf_record *rec)
 		case BPF_KPTR_UNREF:
 		case BPF_KPTR_REF:
 		case BPF_KPTR_PERCPU:
-			btf_get(fields[i].kptr.btf);
+			if (btf_is_kernel(fields[i].kptr.btf))
+				btf_get(fields[i].kptr.btf);
 			if (fields[i].kptr.module && !try_module_get(fields[i].kptr.module)) {
 				ret = -ENXIO;
 				goto free;