Commit 7a851ec
bpf: Search for kptrs in prog BTF structs
Currently btf_parse_fields is used in two places to create struct
btf_record's for structs: when looking at mapval type, and when looking
at any struct in program BTF. The former looks for kptr fields while the
latter does not. This patch modifies the btf_parse_fields call made when
looking at prog BTF struct types to search for kptrs as well.
Before this series there was no reason to search for kptrs in non-mapval
types: a referenced kptr needs some owner to guarantee resource cleanup,
and map values were the only owner that supported this. If a struct with
a kptr field were to have some non-kptr-aware owner, the kptr field
might not be properly cleaned up and result in resources leaking. Only
searching for kptr fields in mapval was a simple way to avoid this
problem.
In practice, though, searching for BPF_KPTR when populating
struct_meta_tab does not expose us to this risk, as struct_meta_tab is
only accessed through btf_find_struct_meta helper, and that helper is
only called in contexts where recognizing the kptr field is safe:
* PTR_TO_BTF_ID reg w/ MEM_ALLOC flag
* Such a reg is a local kptr and must be free'd via bpf_obj_drop,
which will correctly handle kptr field
* When handling specific kfuncs which either expect MEM_ALLOC input or
return MEM_ALLOC output (obj_{new,drop}, percpu_obj_{new,drop},
list+rbtree funcs, refcount_acquire)
* Will correctly handle kptr field for same reasons as above
* When looking at kptr pointee type
* Called by functions which implement "correct kptr resource
handling"
* In btf_check_and_fixup_fields
* Helper that ensures no ownership loops for lists and rbtrees,
doesn't care about kptr field existence
So we should be able to find BPF_KPTR fields in all prog BTF structs
without leaking resources.
Further patches in the series will build on this change to support
kptr_xchg into non-mapval local kptr. Without this change there would be
no kptr field found in such a type.
Acked-by: Martin KaFai Lau <martin.lau@kernel.org>
Acked-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Dave Marchevsky <davemarchevsky@fb.com>
Signed-off-by: Amery Hung <amery.hung@bytedance.com>
Link: https://lore.kernel.org/r/20240813212424.2871455-3-amery.hung@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>1 parent c5ef534 commit 7a851ec
1 file changed
Lines changed: 52 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5512 | 5512 | | |
5513 | 5513 | | |
5514 | 5514 | | |
5515 | | - | |
5516 | | - | |
5517 | | - | |
5518 | | - | |
5519 | | - | |
5520 | | - | |
5521 | | - | |
5522 | 5515 | | |
| 5516 | + | |
5523 | 5517 | | |
5524 | 5518 | | |
5525 | 5519 | | |
5526 | 5520 | | |
5527 | 5521 | | |
5528 | | - | |
| 5522 | + | |
| 5523 | + | |
| 5524 | + | |
| 5525 | + | |
| 5526 | + | |
5529 | 5527 | | |
5530 | 5528 | | |
5531 | 5529 | | |
5532 | 5530 | | |
5533 | 5531 | | |
| 5532 | + | |
| 5533 | + | |
5534 | 5534 | | |
5535 | 5535 | | |
5536 | 5536 | | |
5537 | | - | |
| 5537 | + | |
| 5538 | + | |
| 5539 | + | |
| 5540 | + | |
| 5541 | + | |
| 5542 | + | |
| 5543 | + | |
| 5544 | + | |
| 5545 | + | |
| 5546 | + | |
| 5547 | + | |
| 5548 | + | |
| 5549 | + | |
| 5550 | + | |
| 5551 | + | |
| 5552 | + | |
| 5553 | + | |
| 5554 | + | |
| 5555 | + | |
| 5556 | + | |
| 5557 | + | |
| 5558 | + | |
| 5559 | + | |
| 5560 | + | |
| 5561 | + | |
| 5562 | + | |
| 5563 | + | |
| 5564 | + | |
| 5565 | + | |
| 5566 | + | |
| 5567 | + | |
| 5568 | + | |
| 5569 | + | |
| 5570 | + | |
| 5571 | + | |
| 5572 | + | |
5538 | 5573 | | |
5539 | 5574 | | |
5540 | | - | |
| 5575 | + | |
5541 | 5576 | | |
5542 | | - | |
| 5577 | + | |
5543 | 5578 | | |
5544 | | - | |
5545 | 5579 | | |
5546 | 5580 | | |
5547 | 5581 | | |
| |||
5551 | 5585 | | |
5552 | 5586 | | |
5553 | 5587 | | |
5554 | | - | |
5555 | | - | |
5556 | | - | |
5557 | | - | |
5558 | 5588 | | |
5559 | 5589 | | |
5560 | 5590 | | |
5561 | 5591 | | |
5562 | 5592 | | |
5563 | 5593 | | |
5564 | | - | |
| 5594 | + | |
5565 | 5595 | | |
5566 | 5596 | | |
5567 | 5597 | | |
| |||
5580 | 5610 | | |
5581 | 5611 | | |
5582 | 5612 | | |
5583 | | - | |
| 5613 | + | |
| 5614 | + | |
5584 | 5615 | | |
5585 | 5616 | | |
5586 | 5617 | | |
| |||
5589 | 5620 | | |
5590 | 5621 | | |
5591 | 5622 | | |
| 5623 | + | |
5592 | 5624 | | |
5593 | 5625 | | |
5594 | 5626 | | |
| 5627 | + | |
| 5628 | + | |
5595 | 5629 | | |
5596 | 5630 | | |
5597 | 5631 | | |
| |||
0 commit comments