Commit ba39486d authored by Jose E. Marchesi's avatar Jose E. Marchesi Committed by Alexei Starovoitov

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: default avatarJose E. Marchesi <jose.marchesi@oracle.com>
Cc: david.faust@oracle.com
Cc: cupertino.miranda@oracle.com
Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Link: https://lore.kernel.org/r/20240511212243.23477-1-jose.marchesi@oracle.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 6a2f786e
...@@ -29,6 +29,7 @@ static inline void *bpf_iter_num_new(struct bpf_iter_num *it, int i, int j) { re ...@@ -29,6 +29,7 @@ static inline void *bpf_iter_num_new(struct bpf_iter_num *it, int i, int j) { re
static inline void bpf_iter_num_destroy(struct bpf_iter_num *it) {} static inline void bpf_iter_num_destroy(struct bpf_iter_num *it) {}
static inline bool bpf_iter_num_next(struct bpf_iter_num *it) { return true; } static inline bool bpf_iter_num_next(struct bpf_iter_num *it) { return true; }
#define cond_break ({}) #define cond_break ({})
#define can_loop true
#endif #endif
/* Safely walk link list elements. Deletion of elements is allowed. */ /* Safely walk link list elements. Deletion of elements is allowed. */
...@@ -36,8 +37,7 @@ static inline bool bpf_iter_num_next(struct bpf_iter_num *it) { return true; } ...@@ -36,8 +37,7 @@ static inline bool bpf_iter_num_next(struct bpf_iter_num *it) { return true; }
for (void * ___tmp = (pos = list_entry_safe((head)->first, \ for (void * ___tmp = (pos = list_entry_safe((head)->first, \
typeof(*(pos)), member), \ typeof(*(pos)), member), \
(void *)0); \ (void *)0); \
pos && ({ ___tmp = (void *)pos->member.next; 1; }); \ pos && ({ ___tmp = (void *)pos->member.next; 1; }) && can_loop; \
cond_break, \
pos = list_entry_safe((void __arena *)___tmp, typeof(*(pos)), member)) pos = list_entry_safe((void __arena *)___tmp, typeof(*(pos)), member))
static inline void list_add_head(arena_list_node_t *n, arena_list_head_t *h) static inline void list_add_head(arena_list_node_t *n, arena_list_head_t *h)
......
...@@ -326,19 +326,48 @@ l_true: \ ...@@ -326,19 +326,48 @@ l_true: \
}) })
#endif #endif
/*
* Note that cond_break can only be portably used in the body of a breakable
* construct, whereas can_loop can be used anywhere.
*/
#ifdef __BPF_FEATURE_MAY_GOTO #ifdef __BPF_FEATURE_MAY_GOTO
#define can_loop \
({ __label__ l_break, l_continue; \
bool ret = true; \
asm volatile goto("may_goto %l[l_break]" \
:::: l_break); \
goto l_continue; \
l_break: ret = false; \
l_continue:; \
ret; \
})
#define cond_break \ #define cond_break \
({ __label__ l_break, l_continue; \ ({ __label__ l_break, l_continue; \
asm volatile goto("may_goto %l[l_break]" \ asm volatile goto("may_goto %l[l_break]" \
:::: l_break); \ :::: l_break); \
goto l_continue; \ goto l_continue; \
l_break: break; \ l_break: break; \
l_continue:; \ l_continue:; \
}) })
#else #else
#define can_loop \
({ __label__ l_break, l_continue; \
bool ret = true; \
asm volatile goto("1:.byte 0xe5; \
.byte 0; \
.long ((%l[l_break] - 1b - 8) / 8) & 0xffff; \
.short 0" \
:::: l_break); \
goto l_continue; \
l_break: ret = false; \
l_continue:; \
ret; \
})
#define cond_break \ #define cond_break \
({ __label__ l_break, l_continue; \ ({ __label__ l_break, l_continue; \
asm volatile goto("1:.byte 0xe5; \ asm volatile goto("1:.byte 0xe5; \
.byte 0; \ .byte 0; \
.long ((%l[l_break] - 1b - 8) / 8) & 0xffff; \ .long ((%l[l_break] - 1b - 8) / 8) & 0xffff; \
.short 0" \ .short 0" \
......
...@@ -49,7 +49,7 @@ int arena_list_add(void *ctx) ...@@ -49,7 +49,7 @@ int arena_list_add(void *ctx)
list_head = &global_head; list_head = &global_head;
for (i = zero; i < cnt; cond_break, i++) { for (i = zero; i < cnt && can_loop; i++) {
struct elem __arena *n = bpf_alloc(sizeof(*n)); struct elem __arena *n = bpf_alloc(sizeof(*n));
test_val++; test_val++;
......
...@@ -318,7 +318,7 @@ int cond_break1(const void *ctx) ...@@ -318,7 +318,7 @@ int cond_break1(const void *ctx)
unsigned long i; unsigned long i;
unsigned int sum = 0; unsigned int sum = 0;
for (i = zero; i < ARR_SZ; cond_break, i++) for (i = zero; i < ARR_SZ && can_loop; i++)
sum += i; sum += i;
for (i = zero; i < ARR_SZ; i++) { for (i = zero; i < ARR_SZ; i++) {
barrier_var(i); barrier_var(i);
...@@ -336,12 +336,11 @@ int cond_break2(const void *ctx) ...@@ -336,12 +336,11 @@ int cond_break2(const void *ctx)
int i, j; int i, j;
int sum = 0; int sum = 0;
for (i = zero; i < 1000; cond_break, i++) for (i = zero; i < 1000 && can_loop; i++)
for (j = zero; j < 1000; j++) { for (j = zero; j < 1000; j++) {
sum += i + j; sum += i + j;
cond_break; cond_break;
} }
return sum; return sum;
} }
...@@ -349,7 +348,7 @@ static __noinline int loop(void) ...@@ -349,7 +348,7 @@ static __noinline int loop(void)
{ {
int i, sum = 0; int i, sum = 0;
for (i = zero; i <= 1000000; i++, cond_break) for (i = zero; i <= 1000000 && can_loop; i++)
sum += i; sum += i;
return sum; return sum;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment