Kernel module detection (own implementation)
v4 cover Kernel module detection (own implementation)
Hi,
changes v3->v4:
* fix memory leaks (add missing free())
* optimize handling input string (avoid unnecessary fgets())
* add missing static
* new commit: re-add tst_require_drivers veth in tst_net.sh
I'm for merging this variant which works with text files. I haven't check the binary format yet, but it'd would most likely require using libkmod. Do we want that?
Kind regards,
Petr
Petr Vorel (3):
lib: Fix kernel module detection on BusyBox
zram: Fix module detection on BusyBox
tst_net.sh: Require veth for netns (again)
lib/tst_kernel.c | 104 ++++++++++++++++-- .../kernel/device-drivers/zram/zram_lib.sh | 6 +- testcases/lib/tst_net.sh | 1 + 3 files changed, 95 insertions(+), 16 deletions(-)
Signature · 1 line
2.30.0
PATCH v4 1/3 lib: Fix kernel module detection on BusyBox
BusyBox modprobe implementation does not support -n switch.
It does support -D, which could be used, *but* unless is busybox binary configured with CONFIG_MODPROBE_SMALL=y (IMHO the default).
We could use modinfo and grep output for 'filename:', but we agreed on ML that having our own implementation will be the best as it also does not require modinfo as external dependency.
Implementation searches for for module presence in /lib/modules/$(uname -r)/modules.{dep,builtin}. On Android expect files in /system/lib/modules directory.
Also treat '-' and '_' in module name as the same (follow kmod implementation).
On Android still assume all drivers are available because modules.* files might not be available. We could search modules in /system/lib/modules, but to to determine built-in drivers we need modules.builtin (it's required also by Busybox mod{info,probe} implementation).
This fixes many tests on BusyBox, e.g. *all* network tests (tests using tst_net.sh) after 305a78e4c ("tst_net.sh: Require veth for netns").
--- lib/tst_kernel.c | 104 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 12 deletions(-)
lib/tst_kernel.c +92 -12
@@ -1,5 +1,6 @@/* * Copyright (c) 2017 Cyril Hrubis <[email protected]> +* Copyright (c) 2020-2021 Petr Vorel <[email protected]> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -17,8 +18,11 @@#include <sys/personality.h> #include <sys/utsname.h> +#include <limits.h> +#include "test.h" #include "tst_kernel.h" +#include "old_safe_stdio.h" static int get_kernel_bits_from_uname (struct utsname *buf) {
@@ -81,20 +85,96 @@ int tst_kernel_bits(void)return kernel_bits; } -int tst_check_driver (const char *name) +static int tst_search_driver (const char *driver,const char *file) { -#ifndef __ANDROID__ -const char *const argv[] = {"modprobe" ,"-n" , name,NULL }; -int res =tst_cmd_ (NULL , argv,"/dev/null" ,"/dev/null" , -TST_CMD_PASS_RETVAL ); - -/* 255 - it looks like modprobe not available */ -return (res ==255 ) ?0 : res; -#else -/* Android modprobe may not have '-n', or properly installed -* module.*.bin files to determine built-in drivers. Assume -* all drivers are available. +struct stat st; +char buf[PATH_MAX ]; +char *path =NULL , *search =NULL , *sep =NULL ; +FILE *f; +int ret =-1 ; + +struct utsname uts; + +if (uname (&uts)) { +tst_brkm (TBROK |TERRNO ,NULL ,"uname() failed" ); +return -1 ; + } +SAFE_ASPRINTF (NULL , &path,"/lib/modules/%s/%s" , uts.release, file); + +if (stat (path, &st) || !(S_ISREG (st.st_mode) ||S_ISLNK (st.st_mode))) { +tst_resm (TWARN ,"expected file %s does not exist or not a file" , path); +return -1 ; + } + +if (access (path,R_OK )) { +tst_resm (TWARN ,"file %s cannot be read" , path); +return -1 ; + } + +SAFE_ASPRINTF (NULL , &search,"/%s.ko" , driver); + + f =SAFE_FOPEN (NULL , path,"r" ); + +while (fgets (buf,sizeof (buf), f)) { +/* Cut dependencies after : */ +if ((sep =strchr (buf,':' ))) + *sep =0 ; + +if (strstr (buf, search) !=NULL ) { + ret =0 ; +break ; + } + } + +SAFE_FCLOSE (NULL , f); +free (search); +free (path); + +return ret; +} + +static int tst_check_driver_ (const char *driver) +{ +if (!tst_search_driver (driver,"modules.dep" ) || + !tst_search_driver (driver,"modules.builtin" )) +return 0 ; + +return 1 ; +} + +int tst_check_driver (const char *driver) +{ +#ifdef __ANDROID__ +/* +* Android may not have properly installed modules.* files. We could +* search modules in /system/lib/modules, but to to determine built-in +* drivers we need modules.builtin. Therefore assume all drivers are +* available. */ return 0 ;#endif + +if (!tst_check_driver_ (driver)) +return 0 ; + +int ret =1 ; + +if (strrchr (driver,'-' ) ||strrchr (driver,'_' )) { +char *driver2 =strdup (driver); +char *ix = driver2; +char find ='-' , replace ='_' ; + +if (strrchr (driver,'_' )) { + find ='_' ; + replace ='-' ; + } + +while ((ix =strchr (ix, find))) + *ix++ = replace; + + ret =tst_check_driver_ (driver2); +free (driver2); + } + +return ret; }
Signature · 1 line
2.30.0
PATCH v4 1/3 lib: Fix kernel module detection on BusyBox
Hi!
Quoted 6 lines
lib/tst_kernel.c +6
-0+int tst_check_driver (const char *driver) +{ +#ifdef __ANDROID__ +/* +* Android may not have properly installed modules.* files. We could +* search modules in /system/lib/modules, but to to determine built-in
lib/tst_kernel.c int tst_kernel_bits(void) · line 160
Quoted 10 lines
lib/tst_kernel.c +7
-0+* drivers we need modules.builtin. Therefore assume all drivers are +* available. */ return 0 ;#endif + +if (!tst_check_driver_ (driver)) +return 0 ; + +int ret =1 ;
One last nit, shouldn't this be -1?
Since the tst_check_driver_() returns either 0 or -1.
Or should we change tst_check_driver_() to return 0 or 1 instead?
Other than that this version looks good,
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v4 1/3 lib: Fix kernel module detection on BusyBox
Hi,
Quoted 20 lines
Hi!
Quoted 6 lines
lib/tst_kernel.c +6
-0+int tst_check_driver (const char *driver) +{ +#ifdef __ANDROID__ +/* +* Android may not have properly installed modules.* files. We could +* search modules in /system/lib/modules, but to to determine built-in lib/tst_kernel.c int tst_kernel_bits(void) · line 160
Quoted 10 lines
lib/tst_kernel.c +7
-0+* drivers we need modules.builtin. Therefore assume all drivers are +* available. */ return 0 ;#endif + +if (!tst_check_driver_ (driver)) +return 0 ; + +int ret =1 ;One last nit, shouldn't this be -1?
Quoted 3 lines
Since the
tst_check_driver_()returns either 0 or -1.Or should we change
tst_check_driver_()to return 0 or 1 instead?
Oops, yes the value should be the same.
I prefer -1 as that's error in syscalls, but no strong opinion about it.
Quoted 3 lines
Other than that this version looks good,
Reviewed-by Cyril Hrubis <[email protected]>
Thanks a lot for your patient review!
Kind regards,
Petr
PATCH v4 2/3 zram: Fix module detection on BusyBox
In reply to Petr VorelBusyBox modinfo implementation does not exit with 0 when module not found. Our own API implementation used for module detection in tst_check_driver() was fixed in previous commit thus use it.
--- testcases/kernel/device-drivers/zram/zram_lib.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
testcases/kernel/device-drivers/zram/zram_lib.sh +2 -4
@@ -1,6 +1,6 @@ #!/bin/sh # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved. -# Copyright (c) 2019 Petr Vorel <[email protected]> +# Copyright (c) 2019-2021 Petr Vorel <[email protected]> # Author: Alexey Kodanev <[email protected]> dev_makeswap=-1
@@ -9,6 +9,7 @@ dev_mounted=-1 TST_NEEDS_TMPDIR=1 TST_SETUP="zram_load" TST_CLEANUP="zram_cleanup" +TST_NEEDS_DRIVERS="zram" . tst_test.sh zram_cleanup()
@@ -210,6 +211,3 @@ zram_mount() tst_res TPASS "mount of zram device(s) succeeded" } - -modinfo zram > /dev/null 2>&1 || - tst_brk TCONF "zram not configured in kernel"
Signature · 1 line
2.30.0
PATCH v4 3/3 tst_net.sh: Require veth for netns (again)
In reply to Petr Vorelnew error message is more informative:tcp_fastopen_run 1 TCONF: veth driver not available
then the old one:
Error: Unknown device type.tcp_fastopen_run 1 TBROK: ip li add name ltp_ns_veth1 type veth peer name ltp_ns_veth2 failed
NOTE: originally added in 305a78e4c and temporarily reverted in 7fe2ad11d due problems which were fixed in commit "lib: Fix kernel module detection on BusyBox".
--- testcases/lib/tst_net.sh | 1 + 1 file changed, 1 insertion(+)
testcases/lib/tst_net.sh +1 -0
@@ -106,6 +106,7 @@ init_ltp_netspace() tst_require_cmds ip tst_require_root + tst_require_drivers veth ROD ip li add name ltp_ns_veth1 type veth peer name ltp_ns_veth2 pid="$(ROD ns_create net,mnt)" mkdir -p /var/run/netns
Signature · 1 line
2.30.0