From d12b18195cedd629e94b5bafecd7c9509484c89f Mon Sep 17 00:00:00 2001 From: James Simmons Date: Sun, 4 Feb 2024 21:53:44 -0500 Subject: [PATCH] LU-17418 libcfs: support debug setup for libcfs modules Work was landed to make Lustre ensure key libcfs components were initialized for both a module build and a build directly into the kernel. This change resulted in an defect that allows you to crash a node when you only load libcfs.ko and run a user land tool to set a debugfs setting of libcfs. The debug handling is critical to load before anything. Update Lustre to handle both a module and builtin setup for Lustre. When lustre is built into the kernel we can't control if libcfs_init() is called first so have libcfs_setup() handle setting up the debug handling. When built as a module have libcfs_init() setup the debug handling instead. For both cases libcfs_debug_init() is always called so make sure we only initialize it only once. Add a test to validate this fix. Fixes: f3494a6e9 ("LU-9859 libcfs: refactor libcfs initialization.") Test-Parameters: trivial testlist=conf-sanity env=ONLY="5j" Change-Id: If4a229e43b9e06a723546c03eb2b787ba0b16f5a Signed-off-by: James Simmons Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/53825 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Neil Brown Reviewed-by: Andreas Dilger Reviewed-by: Oleg Drokin --- libcfs/libcfs/debug.c | 7 +++++++ libcfs/libcfs/module.c | 11 ++++++++++- lustre/tests/conf-sanity.sh | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) mode change 100644 => 100755 lustre/tests/conf-sanity.sh diff --git a/libcfs/libcfs/debug.c b/libcfs/libcfs/debug.c index cb67d26..9cc4c6a 100644 --- a/libcfs/libcfs/debug.c +++ b/libcfs/libcfs/debug.c @@ -642,11 +642,17 @@ static void libcfs_unregister_panic_notifier(void) &libcfs_panic_notifier); } +static bool debug_started; + int libcfs_debug_init(unsigned long bufsize) { unsigned int max = libcfs_debug_mb; int rc = 0; + if (debug_started) + return 0; + + debug_started = true; if (libcfs_console_max_delay <= 0 || /* not set by user or */ libcfs_console_min_delay <= 0 || /* set to invalid values */ libcfs_console_min_delay >= libcfs_console_max_delay) { @@ -679,6 +685,7 @@ int libcfs_debug_cleanup(void) kernel_param_lock(THIS_MODULE); cfs_tracefile_exit(); kernel_param_unlock(THIS_MODULE); + debug_started = false; return 0; } diff --git a/libcfs/libcfs/module.c b/libcfs/libcfs/module.c index fb7e861..d21cbb2 100644 --- a/libcfs/libcfs/module.c +++ b/libcfs/libcfs/module.c @@ -667,7 +667,7 @@ static void *debugfs_state; int libcfs_setup(void) { - int rc = -EINVAL; + int rc = 0; mutex_lock(&libcfs_startup); if (libcfs_active) @@ -720,9 +720,18 @@ static int __init libcfs_init(void) { int rc; + mutex_lock(&libcfs_startup); + rc = libcfs_debug_init(5 * 1024 * 1024); + mutex_unlock(&libcfs_startup); + if (rc < 0) { + pr_err("LustreError: libcfs_debug_init: rc = %d\n", rc); + return rc; + } + rc = cfs_arch_init(); if (rc < 0) { CERROR("cfs_arch_init: error %d\n", rc); + libcfs_debug_cleanup(); return rc; } diff --git a/lustre/tests/conf-sanity.sh b/lustre/tests/conf-sanity.sh old mode 100644 new mode 100755 index af68d16..e154feb --- a/lustre/tests/conf-sanity.sh +++ b/lustre/tests/conf-sanity.sh @@ -548,6 +548,15 @@ test_5i() { } run_test 5i "start mdt failure at mdt_quota_init()" +test_5j() { + unload_modules + load_module ../libcfs/libcfs/libcfs || + error "Failed to load modules libcfs rc = $?" + $LCTL set_param debug=all + rmmod -v libcfs +} +run_test 5j "verify libcfs doesn't crash when setting debug flags" + test_6() { setup manual_umount_client -- 1.8.3.1