Skip to content

Commit d551232

Browse files
Yonghong SongKernel Patches Daemon
authored andcommitted
selftests/bpf: Add check_lto_kernel() helper
Add check_lto_kernel() helper to detect whether the underlying kernel enabled CONFIG_LTO or not. The function check_lto_kernel() can be used by selftests to handle some lto-specific situations. The code is heavily borrowed from libbpf function bpf_object__read_kconfig_file(). Signed-off-by: Yonghong Song <[email protected]>
1 parent 9911e07 commit d551232

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

tools/testing/selftests/bpf/testing_helpers.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <stdlib.h>
66
#include <string.h>
77
#include <errno.h>
8+
#include <zlib.h>
9+
#include <sys/utsname.h>
810
#include <bpf/bpf.h>
911
#include <bpf/libbpf.h>
1012
#include "test_progs.h"
@@ -475,3 +477,48 @@ bool is_jit_enabled(void)
475477

476478
return enabled;
477479
}
480+
481+
int check_lto_kernel(void)
482+
{
483+
static int check_lto = 2;
484+
char buf[PATH_MAX];
485+
struct utsname uts;
486+
gzFile file;
487+
int len;
488+
489+
if (check_lto != 2)
490+
return check_lto;
491+
492+
uname(&uts);
493+
len = snprintf(buf, PATH_MAX, "/boot/config-%s", uts.release);
494+
if (len < 0) {
495+
check_lto = -EINVAL;
496+
goto out;
497+
} else if (len >= PATH_MAX) {
498+
check_lto = -ENAMETOOLONG;
499+
goto out;
500+
}
501+
502+
/* gzopen also accepts uncompressed files. */
503+
file = gzopen(buf, "re");
504+
if (!file)
505+
file = gzopen("/proc/config.gz", "re");
506+
507+
if (!file) {
508+
check_lto = -ENOENT;
509+
goto out;
510+
}
511+
512+
check_lto = 0;
513+
while (gzgets(file, buf, sizeof(buf))) {
514+
/* buf also contains '\n', skip it during comparison. */
515+
if (!strncmp(buf, "CONFIG_LTO=y", 12)) {
516+
check_lto = 1;
517+
break;
518+
}
519+
}
520+
521+
gzclose(file);
522+
out:
523+
return check_lto;
524+
}

tools/testing/selftests/bpf/testing_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ struct bpf_insn;
5555
int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);
5656
int testing_prog_flags(void);
5757
bool is_jit_enabled(void);
58+
int check_lto_kernel(void);
5859

5960
#endif /* __TESTING_HELPERS_H */

0 commit comments

Comments
 (0)