Skip to contentSkip to conversation

RFC: ksmbd: Create module_kobject if builtin

PATCH In review Linus Walleij 2 participants 4 messages Oct 23, 2025 – Oct 24, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 RFC: ksmbd: Create module_kobject if builtin

Adding a call to lookup_or_create_module_kobject() to ksmbd makes /sys/modules/ksmbd appear even when ksmbd is compiled into the kernel.

This is nice when you boot a custom kernel on OpenWrt because the startup script does things such as:

[ ! -e /sys/module/ksmbd ] && modprobe ksmbd 2> /dev/null if [ ! -e /sys/module/ksmbd ]; then
logger -p daemon.error -t 'ksmbd' "modprobe of ksmbd " "module failed, can\'t start ksmbd!"
exit 1
fi

which makes the script not work with a compiled-in ksmbd.

Since I actually turn off modules and compile all my modules into the kernel, I can't change the script to just check cat /lib/modules/$(uname -r)/modules.builtin | grep ksmbd
either: no /lib/modules directory.

An option would be to change the script to proceed and just assume the module is compiled in but it feels wrong.

If this approach is acceptable I am happy to generalize this to something that any module that wants a /sys/modules/foo file can use to get just that.

I can think of other ways to just create a dummy dir in /sys/modules but it is probably unwise if someone would later actually add a parameter or version string to the module and get unpredictable bugs. It's probably better like this if we do this thing.

---
Changes in v2:
- Adding MODULE_VERSION() just to get /sys/module/ksmbd was wrong.
- Try this other approach to call lookup_or_create_module_kobject()
if and only if ksmbd is compiled in.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 fs/smb/server/server.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
fs/smb/server/server.c +16 -0
index 40420544cc25a2f7767695641e85d126022a30f9..f12ec149899664efdc9587db749dc8a1d6ccfdae 100644
@@ -588,8 +588,24 @@ static int __init ksmbd_server_init(void)
 	if (ret)
 		goto err_crypto_destroy;
 
+	if (IS_BUILTIN(CONFIG_SMB_SERVER)) {
+		static struct module_kobject *mk;
+
+		/*
+		 * All this does is assure that /sys/module/ksmbd comes
+		 * into existence if ksmbd is compiled in so that userspace
+		 * can rely on its existence to identify that ksmbd is
+		 * available.
+		 */
+		mk = lookup_or_create_module_kobject(KBUILD_MODNAME);
+		if (!mk)
+			goto err_workqueue_destroy;
+	}
+
 	return 0;
 
+err_workqueue_destroy:
+	ksmbd_workqueue_destroy();
 err_crypto_destroy:
 	ksmbd_crypto_destroy();
 err_release_inode_hash:

---
base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
change-id: 20250531-ksmbd-sysfs-module-efaf2d1a86ca

Best regards,

Signature · 1 line
Linus Walleij <[email protected]>
#1 lore raw
Quoted 3 lines · Linus Walleij

Adding a call to lookup_or_create_module_kobject() to ksmbd makes /sys/modules/ksmbd appear even when ksmbd is compiled into the kernel.

Ick, no, please do not. Otherwise you will see this pop up in all drivers/modules for the same "reason".

Quoted 11 lines

This is nice when you boot a custom kernel on OpenWrt because the startup script does things such as:

[ ! -e /sys/module/ksmbd ] && modprobe ksmbd 2> /dev/null if [ ! -e /sys/module/ksmbd ]; then
logger -p daemon.error -t 'ksmbd' "modprobe of ksmbd " "module failed, can\'t start ksmbd!"
exit 1
fi

which makes the script not work with a compiled-in ksmbd.

Please fix the start up script to not do this. Don't work around broken userspace by changing the kernel.

Quoted 4 lines

Since I actually turn off modules and compile all my modules into the kernel, I can't change the script to just check cat /lib/modules/$(uname -r)/modules.builtin | grep ksmbd
either: no /lib/modules directory.

Then do something else to determine if ksmbd is in the running kernel, don't rely on a modules sysfs file like that.

Quoted 2 lines

An option would be to change the script to proceed and just assume the module is compiled in but it feels wrong.

Agreed, surely there is some other way to "know" if that kernel feature is present or not, right? Mount a filesystem? Something else?

Quoted 3 lines

If this approach is acceptable I am happy to generalize this to something that any module that wants a /sys/modules/foo file can use to get just that.

No, please do not do that. Just fix userspace.

thanks,

greg k-h

#2 lore raw
Quoted 7 lines · Greg Kroah-Hartman
Quoted 4 lines

Since I actually turn off modules and compile all my modules into the kernel, I can't change the script to just check cat /lib/modules/$(uname -r)/modules.builtin | grep ksmbd
either: no /lib/modules directory.

Then do something else to determine if ksmbd is in the running kernel, don't rely on a modules sysfs file like that.

There really isn't a good way for a script to detect if a module is compiled-in that I know of.

You did suggest simply create this file also for compiled-in module, always:
https://lore.kernel.org/all/2025060254-unscathed-junior-1b43@gregkh/

It just seemed a bit cluttery, but maybe the $subject approach is even more cluttery by creating special cases.

Yours,
Linus Walleij

#3 lore raw
Quoted 8 lines · Linus Walleij
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines

Since I actually turn off modules and compile all my modules into the kernel, I can't change the script to just check cat /lib/modules/$(uname -r)/modules.builtin | grep ksmbd
either: no /lib/modules directory.

Quoted 5 lines
Quoted 2 lines

Then do something else to determine if ksmbd is in the running kernel, don't rely on a modules sysfs file like that.

There really isn't a good way for a script to detect if a module is compiled-in that I know of.

That should be a per-feature type of thing. Surely ksmbd has some sort of user/kernel api that can be tested if it is present or not?

Quoted 6 lines

You did suggest simply create this file also for compiled-in module, always:
https://lore.kernel.org/all/2025060254-unscathed-junior-1b43@gregkh/

It just seemed a bit cluttery, but maybe the $subject approach is even more cluttery by creating special cases.

Yes, let's not special-case this, either make it so that every module name shows up in sysfs (if it has a parameter or not), or keep it as-is. Don't do module-specific hacks like you are proposing here.

thanks,

greg k-h