Skip to content

Commit 21aeabb

Browse files
chenhengqianakryiko
authored andcommitted
selftests/bpf: Use vmlinux.h for BPF programs
Some of the bpf test progs still use linux/libc headers. Let's use vmlinux.h instead like the rest of test progs. This will also ease cross compiling. Signed-off-by: Hengqi Chen <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 78e097f commit 21aeabb

File tree

5 files changed

+11
-36
lines changed

5 files changed

+11
-36
lines changed

tools/testing/selftests/bpf/progs/loop1.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2019 Facebook
3-
#include <linux/sched.h>
4-
#include <linux/ptrace.h>
5-
#include <stdint.h>
6-
#include <stddef.h>
7-
#include <stdbool.h>
8-
#include <linux/bpf.h>
3+
#include "vmlinux.h"
94
#include <bpf/bpf_helpers.h>
105
#include <bpf/bpf_tracing.h>
116

tools/testing/selftests/bpf/progs/loop2.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2019 Facebook
3-
#include <linux/sched.h>
4-
#include <linux/ptrace.h>
5-
#include <stdint.h>
6-
#include <stddef.h>
7-
#include <stdbool.h>
8-
#include <linux/bpf.h>
3+
#include "vmlinux.h"
94
#include <bpf/bpf_helpers.h>
105
#include <bpf/bpf_tracing.h>
116

tools/testing/selftests/bpf/progs/loop3.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
// Copyright (c) 2019 Facebook
3-
#include <linux/sched.h>
4-
#include <linux/ptrace.h>
5-
#include <stdint.h>
6-
#include <stddef.h>
7-
#include <stdbool.h>
8-
#include <linux/bpf.h>
3+
#include "vmlinux.h"
94
#include <bpf/bpf_helpers.h>
105
#include <bpf/bpf_tracing.h>
116

tools/testing/selftests/bpf/progs/loop6.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
#include <linux/ptrace.h>
4-
#include <stddef.h>
5-
#include <linux/bpf.h>
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_core_read.h>
65
#include <bpf/bpf_helpers.h>
76
#include <bpf/bpf_tracing.h>
87
#include "bpf_misc.h"
@@ -26,12 +25,6 @@ char _license[] SEC("license") = "GPL";
2625
#define SG_CHAIN 0x01UL
2726
#define SG_END 0x02UL
2827

29-
struct scatterlist {
30-
unsigned long page_link;
31-
unsigned int offset;
32-
unsigned int length;
33-
};
34-
3528
#define sg_is_chain(sg) ((sg)->page_link & SG_CHAIN)
3629
#define sg_is_last(sg) ((sg)->page_link & SG_END)
3730
#define sg_chain_ptr(sg) \
@@ -62,7 +55,7 @@ static inline struct scatterlist *get_sgp(struct scatterlist **sgs, int i)
6255
return sgp;
6356
}
6457

65-
int config = 0;
58+
int run_once = 0;
6659
int result = 0;
6760

6861
SEC("kprobe/virtqueue_add_sgs")
@@ -73,14 +66,14 @@ int BPF_KPROBE(trace_virtqueue_add_sgs, void *unused, struct scatterlist **sgs,
7366
__u64 length1 = 0, length2 = 0;
7467
unsigned int i, n, len;
7568

76-
if (config != 0)
69+
if (run_once != 0)
7770
return 0;
7871

7972
for (i = 0; (i < VIRTIO_MAX_SGS) && (i < out_sgs); i++) {
8073
__sink(out_sgs);
8174
for (n = 0, sgp = get_sgp(sgs, i); sgp && (n < SG_MAX);
8275
sgp = __sg_next(sgp)) {
83-
bpf_probe_read_kernel(&len, sizeof(len), &sgp->length);
76+
len = BPF_CORE_READ(sgp, length);
8477
length1 += len;
8578
n++;
8679
}
@@ -90,13 +83,13 @@ int BPF_KPROBE(trace_virtqueue_add_sgs, void *unused, struct scatterlist **sgs,
9083
__sink(in_sgs);
9184
for (n = 0, sgp = get_sgp(sgs, i); sgp && (n < SG_MAX);
9285
sgp = __sg_next(sgp)) {
93-
bpf_probe_read_kernel(&len, sizeof(len), &sgp->length);
86+
len = BPF_CORE_READ(sgp, length);
9487
length2 += len;
9588
n++;
9689
}
9790
}
9891

99-
config = 1;
92+
run_once = 1;
10093
result = length2 - length1;
10194
return 0;
10295
}

tools/testing/selftests/bpf/progs/test_overhead.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2019 Facebook */
3-
#include <stdbool.h>
4-
#include <stddef.h>
5-
#include <linux/bpf.h>
6-
#include <linux/ptrace.h>
3+
#include "vmlinux.h"
74
#include <bpf/bpf_helpers.h>
85
#include <bpf/bpf_tracing.h>
96

0 commit comments

Comments
 (0)