Skip to content

Commit 84226b0

Browse files
FedeDPAndreagit97
andcommitted
fix(driver/modern_bpf): avoid bpf_loop() helper.
We can't use bpf_loop() helper since the `bpf_core_enum_value_exists` check triggers a verifier failure on kernels prior to 5.13 that hadn't got `PTR_TO_FUNC` support. See https://lore.kernel.org/bpf/CAGQdkDt9zyQwr5JyftXqL=OLKscNcqUtEteY4hvOkx2S4GdEkQ@mail.gmail.com/T/#u. Instead, loop up to 16 messages. Signed-off-by: Federico Di Pierro <[email protected]> Co-authored-by: Andrea Terzolo <[email protected]>
1 parent 67975da commit 84226b0

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

Diff for: driver/modern_bpf/helpers/base/shared_size.h

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
/* Maximum number of `iovec` structures that we can analyze. */
2727
#define MAX_IOVCNT 32
2828

29+
/* Maximum number of supported sendmmsg/recvmmsg messages */
30+
#define MAX_SENDMMSG_RECVMMSG_SIZE 16
31+
2932
/* Maximum number of `pollfd` structures that we can analyze. */
3033
#define MAX_POLLFD 16
3134

Diff for: driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/recvmmsg.bpf.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef struct {
4242
void *ctx;
4343
} recvmmsg_data_t;
4444

45-
static long handle_exit(uint32_t index, void *ctx) {
45+
static __always_inline long handle_exit(uint32_t index, void *ctx) {
4646
recvmmsg_data_t *data = (recvmmsg_data_t *)ctx;
4747
struct mmsghdr mmh = {0};
4848
if(bpf_probe_read_user((void *)&mmh,
@@ -159,14 +159,17 @@ int BPF_PROG(recvmmsg_x, struct pt_regs *regs, long ret) {
159159
.ctx = ctx,
160160
};
161161

162-
if(bpf_core_enum_value_exists(enum bpf_func_id, BPF_FUNC_loop)) {
163-
uint32_t nr_loops = ret < 1024 ? ret : 1024;
164-
bpf_loop(nr_loops, handle_exit, &data, 0);
165-
} else {
166-
for(int i = 0; i < ret && i < MAX_IOVCNT; i++) {
167-
handle_exit(i, &data);
168-
}
162+
// We can't use bpf_loop() helper since the below check triggers a verifier failure:
163+
// see
164+
// https://lore.kernel.org/bpf/CAGQdkDt9zyQwr5JyftXqL=OLKscNcqUtEteY4hvOkx2S4GdEkQ@mail.gmail.com/T/#u
165+
/*if(bpf_core_enum_value_exists(enum bpf_func_id, BPF_FUNC_loop)) {
166+
uint32_t nr_loops = ret < 1024 ? ret : 1024;
167+
bpf_loop(nr_loops, handle_exit, &data, 0);
168+
} else {*/
169+
for(int i = 0; i < ret && i < MAX_SENDMMSG_RECVMMSG_SIZE; i++) {
170+
handle_exit(i, &data);
169171
}
172+
//}
170173

171174
return 0;
172175
}

Diff for: driver/modern_bpf/programs/tail_called/events/syscall_dispatched_events/sendmmsg.bpf.c

+11-8
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ typedef struct {
4242
void *ctx;
4343
} sendmmsg_exit_t;
4444

45-
static long handle_exit(uint32_t index, void *ctx) {
45+
static __always_inline long handle_exit(uint32_t index, void *ctx) {
4646
sendmmsg_exit_t *data = (sendmmsg_exit_t *)ctx;
4747
struct mmsghdr mmh = {0};
4848

@@ -152,14 +152,17 @@ int BPF_PROG(sendmmsg_x, struct pt_regs *regs, long ret) {
152152
.ctx = ctx,
153153
};
154154

155-
if(bpf_core_enum_value_exists(enum bpf_func_id, BPF_FUNC_loop)) {
156-
uint32_t nr_loops = ret < 1024 ? ret : 1024;
157-
bpf_loop(nr_loops, handle_exit, &data, 0);
158-
} else {
159-
for(int i = 0; i < ret && i < MAX_IOVCNT; i++) {
160-
handle_exit(i, &data);
161-
}
155+
// We can't use bpf_loop() helper since the below check triggers a verifier failure:
156+
// see
157+
// https://lore.kernel.org/bpf/CAGQdkDt9zyQwr5JyftXqL=OLKscNcqUtEteY4hvOkx2S4GdEkQ@mail.gmail.com/T/#u
158+
/*if(bpf_core_enum_value_exists(enum bpf_func_id, BPF_FUNC_loop)) {
159+
uint32_t nr_loops = ret < 1024 ? ret : 1024;
160+
bpf_loop(nr_loops, handle_exit, &data, 0);
161+
} else {*/
162+
for(int i = 0; i < ret && i < MAX_SENDMMSG_RECVMMSG_SIZE; i++) {
163+
handle_exit(i, &data);
162164
}
165+
//}
163166

164167
return 0;
165168
}

0 commit comments

Comments
 (0)