[LTP] [PATCH v3] madvise11.c:Check loadable module before rmmod

Wei Gao wegao@suse.com
Sun Mar 12 01:44:20 CET 2023


Following fail msg will popup if we try to rmmod buildin module:
rmmod: ERROR: Module hwpoison_inject is builtin

So need add extra check.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/kernel/syscalls/madvise/madvise11.c | 26 ++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/testcases/kernel/syscalls/madvise/madvise11.c b/testcases/kernel/syscalls/madvise/madvise11.c
index 7e291d571..18e120115 100644
--- a/testcases/kernel/syscalls/madvise/madvise11.c
+++ b/testcases/kernel/syscalls/madvise/madvise11.c
@@ -33,6 +33,7 @@
 #define NUM_LOOPS	5
 #define NUM_PAGES	32
 #define NUM_PAGES_OFFSET	5
+#define MAX_BUF 4094
 
 /* Needed module to online back memory pages */
 #define HW_MODULE	"hwpoison_inject"
@@ -291,6 +292,29 @@ static void unpoison_this_pfn(unsigned long pfn, int fd)
 	SAFE_WRITE(0, fd, pfn_str, strlen(pfn_str));
 }
 
+static int is_builtin(const char *modname)
+{
+	char command[MAX_BUF];
+	char line[MAX_BUF];
+
+	sprintf(command, "modinfo %s | grep '^filename'", modname);
+
+	FILE *fp = popen(command, "r");
+
+	if (fp == NULL)
+		tst_brk(TBROK, "Popen command %s failed", command);
+
+	if (fgets(line, MAX_BUF, fp) != NULL) {
+		if (strstr(line, "builtin")) {
+			pclose(fp);
+			return 1;
+		}
+	}
+
+	pclose(fp);
+	return 0;
+}
+
 /* Find and open the <debugfs>/hwpoison/unpoison-pfn special file */
 static int open_unpoison_pfn(void)
 {
@@ -300,7 +324,7 @@ static int open_unpoison_pfn(void)
 	struct mntent *mnt;
 	FILE *mntf;
 
-	if (!find_in_file("/proc/modules", HW_MODULE))
+	if (!find_in_file("/proc/modules", HW_MODULE) && !is_builtin(HW_MODULE))
 		hwpoison_probe = 1;
 
 	/* probe hwpoison only if it isn't already there */
-- 
2.35.3



More information about the ltp mailing list