All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v5 0/3] Kernel module detection (own implementation)
@ 2021-01-22 14:54 Petr Vorel
  2021-01-22 14:54 ` [LTP] [PATCH v5 1/3] tst_check_driver(): Fix kernel module detection on BusyBox Petr Vorel
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Petr Vorel @ 2021-01-22 14:54 UTC (permalink / raw)
  To: ltp

Hi,

hopefully the latest version.

Changes v4->v5:
* return code is -1 instead of 1 (should be safe, thus I kept it in the
same commit, but can move to separate commit)
* update doc in the header

NOTE: there is a bit of mess in return codes in the C API
(-1 vs 1 returned on error), but that's for another effort.

Kind regards,
Petr

diff --git include/tst_kernel.h include/tst_kernel.h
index 71ab9466b..9e17bb71e 100644
--- include/tst_kernel.h
+++ include/tst_kernel.h
@@ -13,9 +13,11 @@ int tst_kernel_bits(void);
 /**
  * Checks support for the kernel driver.
  *
- * @param name The name of the driver.
- * @return Returns 0 if the kernel has the driver or modprobe is missing.
+ * @param driver The name of the driver.
+ * @return Returns 0 if the kernel has the driver,
+ * -1 when driver is missing or config file not available.
+ * On Android *always* 0 (always expect the driver is available).
  */
-int tst_check_driver(const char *name);
+int tst_check_driver(const char *driver);
 
 #endif	/* TST_KERNEL_H__ */
diff --git lib/tst_kernel.c lib/tst_kernel.c
index b5caf7b20..c908bb04c 100644
--- lib/tst_kernel.c
+++ lib/tst_kernel.c
@@ -139,7 +140,7 @@ static int tst_check_driver_(const char *driver)
 		!tst_search_driver(driver, "modules.builtin"))
 		return 0;
 
-	return 1;
+	return -1;
 }
 
 int tst_check_driver(const char *driver)
@@ -157,7 +158,7 @@ int tst_check_driver(const char *driver)
 	if (!tst_check_driver_(driver))
 		return 0;
 
-	int ret = 1;
+	int ret = -1;
 
 	if (strrchr(driver, '-') || strrchr(driver, '_')) {
 		char *driver2 = strdup(driver);



Petr Vorel (3):
  tst_check_driver(): Fix kernel module detection on BusyBox
  zram: Fix module detection on BusyBox
  tst_net.sh: Require veth for netns (again)

 include/tst_kernel.h                          |   8 +-
 lib/tst_kernel.c                              | 105 ++++++++++++++++--
 .../kernel/device-drivers/zram/zram_lib.sh    |   6 +-
 testcases/lib/tst_net.sh                      |   1 +
 4 files changed, 101 insertions(+), 19 deletions(-)

-- 
2.30.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v5 1/3] tst_check_driver(): Fix kernel module detection on BusyBox
  2021-01-22 14:54 [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel
@ 2021-01-22 14:54 ` Petr Vorel
  2021-01-22 14:54 ` [LTP] [PATCH v5 2/3] zram: Fix " Petr Vorel
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2021-01-22 14:54 UTC (permalink / raw)
  To: ltp

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}.

Also treat '-' and '_' in module name as the same (follow kmod implementation).

Changed return code on error from 1 to -1 (no update needed on callers
of the function). Document return code and params in the header.

On Android still assume all drivers are available because modules.* files
might not be available. We could search for modules in /lib/modules or
/vendor/lib/modules or /system/lib/modules (old aosp), 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").

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 include/tst_kernel.h |   8 ++--
 lib/tst_kernel.c     | 105 ++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 98 insertions(+), 15 deletions(-)

diff --git a/include/tst_kernel.h b/include/tst_kernel.h
index 71ab9466b..9e17bb71e 100644
--- a/include/tst_kernel.h
+++ b/include/tst_kernel.h
@@ -13,9 +13,11 @@ int tst_kernel_bits(void);
 /**
  * Checks support for the kernel driver.
  *
- * @param name The name of the driver.
- * @return Returns 0 if the kernel has the driver or modprobe is missing.
+ * @param driver The name of the driver.
+ * @return Returns 0 if the kernel has the driver,
+ * -1 when driver is missing or config file not available.
+ * On Android *always* 0 (always expect the driver is available).
  */
-int tst_check_driver(const char *name);
+int tst_check_driver(const char *driver);
 
 #endif	/* TST_KERNEL_H__ */
diff --git a/lib/tst_kernel.c b/lib/tst_kernel.c
index 57fa4b2be..c908bb04c 100644
--- a/lib/tst_kernel.c
+++ b/lib/tst_kernel.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
+ * Copyright (c) 2020-2021 Petr Vorel <pvorel@suse.cz>
  *
  * 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,97 @@ 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;
+
+		/* driver found */
+		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;
 }
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v5 2/3] zram: Fix module detection on BusyBox
  2021-01-22 14:54 [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel
  2021-01-22 14:54 ` [LTP] [PATCH v5 1/3] tst_check_driver(): Fix kernel module detection on BusyBox Petr Vorel
@ 2021-01-22 14:54 ` Petr Vorel
  2021-01-22 14:54 ` [LTP] [PATCH v5 3/3] tst_net.sh: Require veth for netns (again) Petr Vorel
  2021-01-25  8:39 ` [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel
  3 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2021-01-22 14:54 UTC (permalink / raw)
  To: ltp

BusyBox 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.

Reported-by: Leo Yu-Chi Liang <ycliang@andestech.com>
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 testcases/kernel/device-drivers/zram/zram_lib.sh | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/testcases/kernel/device-drivers/zram/zram_lib.sh b/testcases/kernel/device-drivers/zram/zram_lib.sh
index 3f4d1d55f..bdbf2453a 100755
--- a/testcases/kernel/device-drivers/zram/zram_lib.sh
+++ b/testcases/kernel/device-drivers/zram/zram_lib.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 # Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
-# Copyright (c) 2019 Petr Vorel <pvorel@suse.cz>
+# Copyright (c) 2019-2021 Petr Vorel <pvorel@suse.cz>
 # Author: Alexey Kodanev <alexey.kodanev@oracle.com>
 
 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"
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v5 3/3] tst_net.sh: Require veth for netns (again)
  2021-01-22 14:54 [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel
  2021-01-22 14:54 ` [LTP] [PATCH v5 1/3] tst_check_driver(): Fix kernel module detection on BusyBox Petr Vorel
  2021-01-22 14:54 ` [LTP] [PATCH v5 2/3] zram: Fix " Petr Vorel
@ 2021-01-22 14:54 ` Petr Vorel
  2021-01-25  8:39 ` [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel
  3 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2021-01-22 14:54 UTC (permalink / raw)
  To: ltp

new 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".

Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 testcases/lib/tst_net.sh | 1 +
 1 file changed, 1 insertion(+)

diff --git a/testcases/lib/tst_net.sh b/testcases/lib/tst_net.sh
index ef9354903..1ddef2fea 100644
--- a/testcases/lib/tst_net.sh
+++ b/testcases/lib/tst_net.sh
@@ -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
-- 
2.30.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH v5 0/3] Kernel module detection (own implementation)
  2021-01-22 14:54 [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel
                   ` (2 preceding siblings ...)
  2021-01-22 14:54 ` [LTP] [PATCH v5 3/3] tst_net.sh: Require veth for netns (again) Petr Vorel
@ 2021-01-25  8:39 ` Petr Vorel
  3 siblings, 0 replies; 5+ messages in thread
From: Petr Vorel @ 2021-01-25  8:39 UTC (permalink / raw)
  To: ltp

Hi,

patchset merged.
@Sandeep: feel free to contribute Android specific patch.

Kind regards,
Petr

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-01-25  8:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-22 14:54 [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel
2021-01-22 14:54 ` [LTP] [PATCH v5 1/3] tst_check_driver(): Fix kernel module detection on BusyBox Petr Vorel
2021-01-22 14:54 ` [LTP] [PATCH v5 2/3] zram: Fix " Petr Vorel
2021-01-22 14:54 ` [LTP] [PATCH v5 3/3] tst_net.sh: Require veth for netns (again) Petr Vorel
2021-01-25  8:39 ` [LTP] [PATCH v5 0/3] Kernel module detection (own implementation) Petr Vorel

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.