Re: Why does bpf_probe_read also release relocation information?
From: Yonghong Song <hidden>
Date: 2021-05-24 02:34:16
On 5/23/21 7:24 PM, Shuyi Cheng wrote:
Hello everyone, I would like to ask a question. The question is: Why does bpf_probe_read not use __builtin_preserve_access_index and also release relocation information? The following document is from: https://github.com/libbpf/libbpf/blob/57375504c6c9766d23f178c40f71bf5e8df9363d/src/libbpf_internal.h#L414 * Such relocation is emitted when using __builtin_preserve_access_index() * Clang built-in, passing expression that captures field address, e.g.: * * bpf_probe_read(&dst, sizeof(dst), * __builtin_preserve_access_index(&src->a.b.c)); * * In this case Clang will emit field relocation recording necessary data to * be able to find offset of embedded `a.b.c` field within `src` struct. This document clearly explains the function of __builtin_preserve_access_index. However, I did a small test. The test results show that only using bpf_probe_read and not using __builtin_preserve_access_index will also be relocated.The specific test content is as follows: // The bpf program. SEC("kprobe/kfree_skb") int BPF_PROG(kprobe__kfree_skb,struct sk_buff *skb) { unsigned char *data; bpf_probe_read(&data,sizeof(data),&skb->data); return 0; } // The debug log. libbpf: CO-RE relocating [0] struct sk_buff: found target candidate [3057] struct sk_buff in [vmlinux] libbpf: CO-RE relocating [0] struct sk_buff: found target candidate [23925] struct sk_buff in [vmlinux] libbpf: prog 'kprobe__kfree_skb': relo #0: matching candidate #0 [3057] struct sk_buff.data (0:77 @ offset 240) libbpf: prog 'kprobe__kfree_skb': relo #0: matching candidate #1 [23925] struct sk_buff.data (0:77 @ offset 240) libbpf: prog 'kprobe__kfree_skb': relo #0: patched insn #0 (ALU/ALU64) imm 240 -> 240
Did you include "vmlinux.h" in your program? The "vmlinux.h" contains #ifndef BPF_NO_PRESERVE_ACCESS_INDEX #pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record) #endif which will put preserve_access_index to all record (struct/union) definitions. So the above "sk_buff" member access will be relocated automatically by libbpf.
Thanks in advance for your help, Cheng