Skip to content

Commit ba39486

Browse files
jemarchAlexei Starovoitov
authored andcommitted
bpf: make list_for_each_entry portable
[Changes from V1: - The __compat_break has been abandoned in favor of a more readable can_loop macro that can be used anywhere, including loop conditions.] The macro list_for_each_entry is defined in bpf_arena_list.h as follows: #define list_for_each_entry(pos, head, member) \ for (void * ___tmp = (pos = list_entry_safe((head)->first, \ typeof(*(pos)), member), \ (void *)0); \ pos && ({ ___tmp = (void *)pos->member.next; 1; }); \ cond_break, \ pos = list_entry_safe((void __arena *)___tmp, typeof(*(pos)), member)) The macro cond_break, in turn, expands to a statement expression that contains a `break' statement. Compound statement expressions, and the subsequent ability of placing statements in the header of a `for' loop, are GNU extensions. Unfortunately, clang implements this GNU extension differently than GCC: - In GCC the `break' statement is bound to the containing "breakable" context in which the defining `for' appears. If there is no such context, GCC emits a warning: break statement without enclosing `for' o `switch' statement. - In clang the `break' statement is bound to the defining `for'. If the defining `for' is itself inside some breakable construct, then clang emits a -Wgcc-compat warning. This patch adds a new macro can_loop to bpf_experimental, that implements the same logic than cond_break but evaluates to a boolean expression. The patch also changes all the current instances of usage of cond_break withing the header of loop accordingly. Tested in bpf-next master. No regressions. Signed-off-by: Jose E. Marchesi <[email protected]> Cc: [email protected] Cc: [email protected] Cc: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 6a2f786 commit ba39486

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

tools/testing/selftests/bpf/bpf_arena_list.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ static inline void *bpf_iter_num_new(struct bpf_iter_num *it, int i, int j) { re
2929
static inline void bpf_iter_num_destroy(struct bpf_iter_num *it) {}
3030
static inline bool bpf_iter_num_next(struct bpf_iter_num *it) { return true; }
3131
#define cond_break ({})
32+
#define can_loop true
3233
#endif
3334

3435
/* Safely walk link list elements. Deletion of elements is allowed. */
3536
#define list_for_each_entry(pos, head, member) \
3637
for (void * ___tmp = (pos = list_entry_safe((head)->first, \
3738
typeof(*(pos)), member), \
3839
(void *)0); \
39-
pos && ({ ___tmp = (void *)pos->member.next; 1; }); \
40-
cond_break, \
40+
pos && ({ ___tmp = (void *)pos->member.next; 1; }) && can_loop; \
4141
pos = list_entry_safe((void __arena *)___tmp, typeof(*(pos)), member))
4242

4343
static inline void list_add_head(arena_list_node_t *n, arena_list_head_t *h)

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,48 @@ l_true: \
326326
})
327327
#endif
328328

329+
/*
330+
* Note that cond_break can only be portably used in the body of a breakable
331+
* construct, whereas can_loop can be used anywhere.
332+
*/
329333
#ifdef __BPF_FEATURE_MAY_GOTO
334+
#define can_loop \
335+
({ __label__ l_break, l_continue; \
336+
bool ret = true; \
337+
asm volatile goto("may_goto %l[l_break]" \
338+
:::: l_break); \
339+
goto l_continue; \
340+
l_break: ret = false; \
341+
l_continue:; \
342+
ret; \
343+
})
344+
330345
#define cond_break \
331346
({ __label__ l_break, l_continue; \
332-
asm volatile goto("may_goto %l[l_break]" \
347+
asm volatile goto("may_goto %l[l_break]" \
333348
:::: l_break); \
334349
goto l_continue; \
335350
l_break: break; \
336351
l_continue:; \
337352
})
338353
#else
354+
#define can_loop \
355+
({ __label__ l_break, l_continue; \
356+
bool ret = true; \
357+
asm volatile goto("1:.byte 0xe5; \
358+
.byte 0; \
359+
.long ((%l[l_break] - 1b - 8) / 8) & 0xffff; \
360+
.short 0" \
361+
:::: l_break); \
362+
goto l_continue; \
363+
l_break: ret = false; \
364+
l_continue:; \
365+
ret; \
366+
})
367+
339368
#define cond_break \
340369
({ __label__ l_break, l_continue; \
341-
asm volatile goto("1:.byte 0xe5; \
370+
asm volatile goto("1:.byte 0xe5; \
342371
.byte 0; \
343372
.long ((%l[l_break] - 1b - 8) / 8) & 0xffff; \
344373
.short 0" \

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int arena_list_add(void *ctx)
4949

5050
list_head = &global_head;
5151

52-
for (i = zero; i < cnt; cond_break, i++) {
52+
for (i = zero; i < cnt && can_loop; i++) {
5353
struct elem __arena *n = bpf_alloc(sizeof(*n));
5454

5555
test_val++;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ int cond_break1(const void *ctx)
318318
unsigned long i;
319319
unsigned int sum = 0;
320320

321-
for (i = zero; i < ARR_SZ; cond_break, i++)
321+
for (i = zero; i < ARR_SZ && can_loop; i++)
322322
sum += i;
323323
for (i = zero; i < ARR_SZ; i++) {
324324
barrier_var(i);
@@ -336,20 +336,19 @@ int cond_break2(const void *ctx)
336336
int i, j;
337337
int sum = 0;
338338

339-
for (i = zero; i < 1000; cond_break, i++)
339+
for (i = zero; i < 1000 && can_loop; i++)
340340
for (j = zero; j < 1000; j++) {
341341
sum += i + j;
342342
cond_break;
343-
}
344-
343+
}
345344
return sum;
346345
}
347346

348347
static __noinline int loop(void)
349348
{
350349
int i, sum = 0;
351350

352-
for (i = zero; i <= 1000000; i++, cond_break)
351+
for (i = zero; i <= 1000000 && can_loop; i++)
353352
sum += i;
354353

355354
return sum;

0 commit comments

Comments
 (0)