Skip to contentSkip to conversation

Why does bpf_probe_read also release relocation information?

DISCUSSION Shuyi Cheng 2 participants 3 messages May 24, 2021 BPF

lore
Share & tools
lore
#0 lore raw

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

Thanks in advance for your help,
Cheng

#1 lore raw
Quoted 29 lines · Shuyi Cheng

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;
}

Quoted 6 lines

// 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.

Quoted 5 lines

Thanks in advance for your help,
Cheng

#2 lore raw
Quoted 38 lines · Yonghong Song
Quoted 28 lines · Shuyi Cheng

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;
}

Quoted 6 lines

// 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

Quoted 9 lines

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.

Yes, i inclue "vmlinux.h". I understand, Thank you very much for answering my doubts.:-)

Quoted 5 lines
Quoted 5 lines

Thanks in advance for your help,
Cheng