Skip to content

Commit 185da3d

Browse files
Jakob-Koschelanakryiko
authored andcommitted
bpf: Replace usage of supported with dedicated list iterator variable
To move the list iterator variable into the list_for_each_entry_*() macro in the future it should be avoided to use the list iterator variable after the loop body. To *never* use the list iterator variable after the loop it was concluded to use a separate iterator variable instead of a found boolean [1]. This removes the need to use the found variable (existed & supported) and simply checking if the variable was set, can determine if the break/goto was hit. [1] https://lore.kernel.org/all/CAHk-=wgRr_D8CB-D9Kg-c=EHreAsk5SqXPwr9Y7k9sA6cWXJ6w@mail.gmail.com/ Signed-off-by: Jakob Koschel <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 891663a commit 185da3d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

kernel/bpf/bpf_iter.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -330,35 +330,34 @@ static void cache_btf_id(struct bpf_iter_target_info *tinfo,
330330
bool bpf_iter_prog_supported(struct bpf_prog *prog)
331331
{
332332
const char *attach_fname = prog->aux->attach_func_name;
333+
struct bpf_iter_target_info *tinfo = NULL, *iter;
333334
u32 prog_btf_id = prog->aux->attach_btf_id;
334335
const char *prefix = BPF_ITER_FUNC_PREFIX;
335-
struct bpf_iter_target_info *tinfo;
336336
int prefix_len = strlen(prefix);
337-
bool supported = false;
338337

339338
if (strncmp(attach_fname, prefix, prefix_len))
340339
return false;
341340

342341
mutex_lock(&targets_mutex);
343-
list_for_each_entry(tinfo, &targets, list) {
344-
if (tinfo->btf_id && tinfo->btf_id == prog_btf_id) {
345-
supported = true;
342+
list_for_each_entry(iter, &targets, list) {
343+
if (iter->btf_id && iter->btf_id == prog_btf_id) {
344+
tinfo = iter;
346345
break;
347346
}
348-
if (!strcmp(attach_fname + prefix_len, tinfo->reg_info->target)) {
349-
cache_btf_id(tinfo, prog);
350-
supported = true;
347+
if (!strcmp(attach_fname + prefix_len, iter->reg_info->target)) {
348+
cache_btf_id(iter, prog);
349+
tinfo = iter;
351350
break;
352351
}
353352
}
354353
mutex_unlock(&targets_mutex);
355354

356-
if (supported) {
355+
if (tinfo) {
357356
prog->aux->ctx_arg_info_size = tinfo->reg_info->ctx_arg_info_size;
358357
prog->aux->ctx_arg_info = tinfo->reg_info->ctx_arg_info;
359358
}
360359

361-
return supported;
360+
return tinfo != NULL;
362361
}
363362

364363
const struct bpf_func_proto *
@@ -499,12 +498,11 @@ bool bpf_link_is_iter(struct bpf_link *link)
499498
int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
500499
struct bpf_prog *prog)
501500
{
501+
struct bpf_iter_target_info *tinfo = NULL, *iter;
502502
struct bpf_link_primer link_primer;
503-
struct bpf_iter_target_info *tinfo;
504503
union bpf_iter_link_info linfo;
505504
struct bpf_iter_link *link;
506505
u32 prog_btf_id, linfo_len;
507-
bool existed = false;
508506
bpfptr_t ulinfo;
509507
int err;
510508

@@ -530,14 +528,14 @@ int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
530528

531529
prog_btf_id = prog->aux->attach_btf_id;
532530
mutex_lock(&targets_mutex);
533-
list_for_each_entry(tinfo, &targets, list) {
534-
if (tinfo->btf_id == prog_btf_id) {
535-
existed = true;
531+
list_for_each_entry(iter, &targets, list) {
532+
if (iter->btf_id == prog_btf_id) {
533+
tinfo = iter;
536534
break;
537535
}
538536
}
539537
mutex_unlock(&targets_mutex);
540-
if (!existed)
538+
if (!tinfo)
541539
return -ENOENT;
542540

543541
link = kzalloc(sizeof(*link), GFP_USER | __GFP_NOWARN);

0 commit comments

Comments
 (0)