Skip to content

Commit 7cbe3da

Browse files
eddyz87qmonnet
authored andcommitted
bpftool: __bpf_fastcall for kfuncs marked with special decl_tag
Generate __attribute__((bpf_fastcall)) for kfuncs marked with "bpf_fastcall" decl tag. E.g. for the following BTF: $ bpftool btf dump file vmlinux ... [A] FUNC 'bpf_rdonly_cast' type_id=... ... [B] DECL_TAG 'bpf_kfunc' type_id=A component_idx=-1 [C] DECL_TAG 'bpf_fastcall' type_id=A component_idx=-1 Generate the following vmlinux.h: #ifndef __VMLINUX_H__ #define __VMLINUX_H__ ... #ifndef __bpf_fastcall #if __has_attribute(bpf_fastcall) #define __bpf_fastcall __attribute__((bpf_fastcall)) #else #define __bpf_fastcall #endif #endif ... __bpf_fastcall extern void *bpf_rdonly_cast(...) ...; The "bpf_fastcall" / "bpf_kfunc" tags pair would generated by pahole when constructing vmlinux BTF. While at it, sort printed kfuncs by name for better vmlinux.h stability. Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 2cdc7ff commit 7cbe3da

File tree

1 file changed

+88
-10
lines changed

1 file changed

+88
-10
lines changed

src/btf.c

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
22
/* Copyright (C) 2019 Facebook */
33

4+
#ifndef _GNU_SOURCE
5+
#define _GNU_SOURCE
6+
#endif
47
#include <errno.h>
58
#include <fcntl.h>
69
#include <linux/err.h>
710
#include <stdbool.h>
811
#include <stdio.h>
12+
#include <stdlib.h>
913
#include <string.h>
1014
#include <unistd.h>
1115
#include <linux/btf.h>
@@ -21,6 +25,7 @@
2125
#include "main.h"
2226

2327
#define KFUNC_DECL_TAG "bpf_kfunc"
28+
#define FASTCALL_DECL_TAG "bpf_fastcall"
2429

2530
static const char * const btf_kind_str[NR_BTF_KINDS] = {
2631
[BTF_KIND_UNKN] = "UNKNOWN",
@@ -464,47 +469,113 @@ static int dump_btf_raw(const struct btf *btf,
464469
return 0;
465470
}
466471

472+
struct ptr_array {
473+
__u32 cnt;
474+
__u32 cap;
475+
const void **elems;
476+
};
477+
478+
static int ptr_array_push(const void *ptr, struct ptr_array *arr)
479+
{
480+
__u32 new_cap;
481+
void *tmp;
482+
483+
if (arr->cnt == arr->cap) {
484+
new_cap = (arr->cap ?: 16) * 2;
485+
tmp = realloc(arr->elems, sizeof(*arr->elems) * new_cap);
486+
if (!tmp)
487+
return -ENOMEM;
488+
arr->elems = tmp;
489+
arr->cap = new_cap;
490+
}
491+
arr->elems[arr->cnt++] = ptr;
492+
return 0;
493+
}
494+
495+
static void ptr_array_free(struct ptr_array *arr)
496+
{
497+
free(arr->elems);
498+
}
499+
500+
static int cmp_kfuncs(const void *pa, const void *pb, void *ctx)
501+
{
502+
struct btf *btf = ctx;
503+
const struct btf_type *a = *(void **)pa;
504+
const struct btf_type *b = *(void **)pb;
505+
506+
return strcmp(btf__str_by_offset(btf, a->name_off),
507+
btf__str_by_offset(btf, b->name_off));
508+
}
509+
467510
static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf)
468511
{
469512
LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts);
470-
int cnt = btf__type_cnt(btf);
471-
int i;
513+
__u32 cnt = btf__type_cnt(btf), i, j;
514+
struct ptr_array fastcalls = {};
515+
struct ptr_array kfuncs = {};
516+
int err = 0;
472517

473518
printf("\n/* BPF kfuncs */\n");
474519
printf("#ifndef BPF_NO_KFUNC_PROTOTYPES\n");
475520

476521
for (i = 1; i < cnt; i++) {
477522
const struct btf_type *t = btf__type_by_id(btf, i);
523+
const struct btf_type *ft;
478524
const char *name;
479-
int err;
480525

481526
if (!btf_is_decl_tag(t))
482527
continue;
483528

484529
if (btf_decl_tag(t)->component_idx != -1)
485530
continue;
486531

487-
name = btf__name_by_offset(btf, t->name_off);
488-
if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)))
532+
ft = btf__type_by_id(btf, t->type);
533+
if (!btf_is_func(ft))
489534
continue;
490535

491-
t = btf__type_by_id(btf, t->type);
492-
if (!btf_is_func(t))
493-
continue;
536+
name = btf__name_by_offset(btf, t->name_off);
537+
if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)) == 0) {
538+
err = ptr_array_push(ft, &kfuncs);
539+
if (err)
540+
goto out;
541+
}
542+
543+
if (strncmp(name, FASTCALL_DECL_TAG, sizeof(FASTCALL_DECL_TAG)) == 0) {
544+
err = ptr_array_push(ft, &fastcalls);
545+
if (err)
546+
goto out;
547+
}
548+
}
549+
550+
/* Sort kfuncs by name for improved vmlinux.h stability */
551+
qsort_r(kfuncs.elems, kfuncs.cnt, sizeof(*kfuncs.elems), cmp_kfuncs, (void *)btf);
552+
for (i = 0; i < kfuncs.cnt; i++) {
553+
const struct btf_type *t = kfuncs.elems[i];
494554

495555
printf("extern ");
496556

557+
/* Assume small amount of fastcall kfuncs */
558+
for (j = 0; j < fastcalls.cnt; j++) {
559+
if (fastcalls.elems[j] == t) {
560+
printf("__bpf_fastcall ");
561+
break;
562+
}
563+
}
564+
497565
opts.field_name = btf__name_by_offset(btf, t->name_off);
498566
err = btf_dump__emit_type_decl(d, t->type, &opts);
499567
if (err)
500-
return err;
568+
goto out;
501569

502570
printf(" __weak __ksym;\n");
503571
}
504572

505573
printf("#endif\n\n");
506574

507-
return 0;
575+
out:
576+
ptr_array_free(&fastcalls);
577+
ptr_array_free(&kfuncs);
578+
return err;
508579
}
509580

510581
static void __printf(2, 0) btf_dump_printf(void *ctx,
@@ -718,6 +789,13 @@ static int dump_btf_c(const struct btf *btf,
718789
printf("#ifndef __weak\n");
719790
printf("#define __weak __attribute__((weak))\n");
720791
printf("#endif\n\n");
792+
printf("#ifndef __bpf_fastcall\n");
793+
printf("#if __has_attribute(bpf_fastcall)\n");
794+
printf("#define __bpf_fastcall __attribute__((bpf_fastcall))\n");
795+
printf("#else\n");
796+
printf("#define __bpf_fastcall\n");
797+
printf("#endif\n");
798+
printf("#endif\n\n");
721799

722800
if (root_type_cnt) {
723801
for (i = 0; i < root_type_cnt; i++) {

0 commit comments

Comments
 (0)