Skip to contentSkip to conversation

drivers: base: Set mod->async_probe_requested if needed

PATCH In review Bart Van Assche 2 participants 5 messages Apr 7 – Apr 29

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

PATCH 1/1 drivers: base: Set mod->async_probe_requested if needed

If PROBE_PREFER_ASYNCHRONOUS is set for a device driver, and if loading other kernel modules depends on probing of that device driver to complete, e.g. because it is a storage driver, and if mod->async_probe_requested has not been set, then the async_synchronize_full() call in do_init_module() introduces a delay. Fix this by setting mod->async_probe_requested if PROBE_PREFER_ASYNCHRONOUS has been set. This patch reduces the Pixel 10 boot time by 100 ms.

Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Daniel Gomez <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Aaron Tomlin <[email protected]>
Cc Igor Pylypiv <[email protected]>
Cc Chung-Kai Mei <[email protected]>
---
 drivers/base/module.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/base/module.c +3 -0
index 218aaa096455..e58fc189d389 100644
@@ -39,6 +39,9 @@ int module_add_driver(struct module *mod, const struct device_driver *drv)
 	if (!drv)
 		return 0;
 
+	if (mod && drv->probe_type == PROBE_PREFER_ASYNCHRONOUS)
+		mod->async_probe_requested = true;
+
 	if (mod)
 		mk = &mod->mkobj;
 	else if (drv->mod_name) {
#1 lore raw
Quoted 27 lines · Bart Van Assche

If PROBE_PREFER_ASYNCHRONOUS is set for a device driver, and if loading other kernel modules depends on probing of that device driver to complete, e.g. because it is a storage driver, and if mod->async_probe_requested has not been set, then the async_synchronize_full() call in do_init_module() introduces a delay. Fix this by setting mod->async_probe_requested if PROBE_PREFER_ASYNCHRONOUS has been set. This patch reduces the Pixel 10 boot time by 100 ms.

Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Daniel Gomez <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Aaron Tomlin <[email protected]>
Cc Igor Pylypiv <[email protected]>
Cc Chung-Kai Mei <[email protected]>
---
  drivers/base/module.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/base/module.c +0 -0
index 218aaa096455..e58fc189d389 100644

drivers/base/module.c int module_add_driver(struct module *mod, const struct device_driver *drv) · line 47

Quoted 9 lines
drivers/base/module.c +3 -0
@@ -39,6 +39,9 @@ int module_add_driver(struct module *mod, const struct device_driver *drv)
  	if (!drv)
  		return 0;
 
+	if (mod && drv->probe_type == PROBE_PREFER_ASYNCHRONOUS)
+		mod->async_probe_requested = true;
+
  	if (mod)
  		mk = &mod->mkobj;
  	else if (drv->mod_name) {

(replying to my own email)

The above patch still applies fine on Linux kernel v7.1-rc1. Please help with reviewing the above patch.

Thanks,

Bart.

maintainer
#2 lore raw
In reply to Bart Van Assche
Quoted 9 lines · Bart Van Assche

If PROBE_PREFER_ASYNCHRONOUS is set for a device driver, and if loading other kernel modules depends on probing of that device driver to complete, e.g. because it is a storage driver, and if mod->async_probe_requested has not been set, then the async_synchronize_full() call in do_init_module() introduces a delay. Fix this by setting mod->async_probe_requested if PROBE_PREFER_ASYNCHRONOUS has been set. This patch reduces the Pixel 10 boot time by 100 ms.

Quoted 8 lines
Cc Luis Chamberlain <[email protected]>
Cc Petr Pavlu <[email protected]>
Cc Daniel Gomez <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Aaron Tomlin <[email protected]>
Cc Igor Pylypiv <[email protected]>
Cc Chung-Kai Mei <[email protected]>

Why does this have Cc: stable? I think this is just an improvement and not a regression? If it is a regression, what's the commit that is fixed?

Quoted 10 lines
---
 drivers/base/module.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/base/module.c +0 -0
index 218aaa096455..e58fc189d389 100644

drivers/base/module.c int module_add_driver(struct module *mod, const struct device_driver *drv) · line 43

Quoted 5 lines
drivers/base/module.c +2 -0
@@ -39,6 +39,9 @@ int module_add_driver(struct module *mod, const struct device_driver *drv)
 	if (!drv)
 		return 0;
 
+	if (mod && drv->probe_type == PROBE_PREFER_ASYNCHRONOUS)
+		mod->async_probe_requested = true;

What if userspace did explicitly pass async_probe=0?

Quoted 4 lines
drivers/base/module.c +1 -0
+
 	if (mod)
 		mk = &mod->mkobj;
 	else if (drv->mod_name) {
#3 lore raw
Quoted 1 line · Danilo Krummrich

What if userspace did explicitly pass async_probe=0?

Does this mean that what the user has configured should take precedence, as in the untested patch below?

Thanks,

Bart.

drivers/base/module.c +4 -0
index 218aaa096455..86f1f21a3d23 100644
@@ -39,6 +39,10 @@ int module_add_driver(struct module *mod, const struct device_driver *drv)
 	if (!drv)
 		return 0;
 
+	if (mod && drv->probe_type == PROBE_PREFER_ASYNCHRONOUS &&
+	    mod->async_probe_requested == PROBE_TYPE_NOT_SET)
+		mod->async_probe_requested = PROBE_ASYNCHRONOUSLY;
+
 	if (mod)
 		mk = &mod->mkobj;
 	else if (drv->mod_name) {
include/linux/module.h +8 -2
index 7566815fabbe..a946ad88c925 100644
@@ -394,6 +394,12 @@ struct klp_modinfo {
 };
 #endif
 
+enum async_probe_type {
+	PROBE_TYPE_NOT_SET = 0,
+	PROBE_SYNCHRONOUSLY,
+	PROBE_ASYNCHRONOUSLY,
+} __packed;
+
 struct module {
 	enum module_state state;
 
@@ -442,7 +448,7 @@ struct module {
 	bool sig_ok;
 #endif
 
-	bool async_probe_requested;
+	enum async_probe_type async_probe_requested;
 
 	/* Exception table */
 	unsigned int num_exentries;
@@ -760,7 +766,7 @@ extern void print_modules(void);
 
 static inline bool module_requested_async_probing(struct module *module)
 {
-	return module && module->async_probe_requested;
+	return module && module->async_probe_requested == PROBE_ASYNCHRONOUSLY;
 }
 
 static inline bool is_livepatch_module(struct module *mod)
kernel/module/main.c +25 -3
index 46dd8d25a605..9e2d9c9f65fa 100644
@@ -3065,8 +3065,30 @@ void flush_module_init_free_work(void)
 #undef MODULE_PARAM_PREFIX
 #define MODULE_PARAM_PREFIX "module."
 /* Default value for module->async_probe_requested */
-static bool async_probe;
-module_param(async_probe, bool, 0644);
+static enum async_probe_type async_probe;
+
+static int async_probe_set(const char *val, const struct kernel_param *kp)
+{
+	bool async;
+	int ret;
+
+	ret = kstrtobool(val, &async);
+	if (ret)
+		return ret;
+	async_probe = async ? PROBE_ASYNCHRONOUSLY : PROBE_SYNCHRONOUSLY;
+	return 0;
+}
+
+static int async_probe_get(char *buffer, const struct kernel_param *kp)
+{
+	return sysfs_emit(buffer, "%d", async_probe == PROBE_ASYNCHRONOUSLY);
+}
+
+static struct kernel_param_ops async_probe_ops = {
+	.set = async_probe_set,
+	.get = async_probe_get,
+};
+module_param_cb(async_probe, &async_probe_ops, &async_probe, 0644);
 
 /*
  * This is where the real work happens.
@@ -3135,7 +3157,7 @@ static noinline int do_init_module(struct module *mod)
 	 * See commit 0fdff3ec6d87 ("async, kmod: warn on synchronous
 	 * request_module() from async workers") for more details.
 	 */
-	if (!mod->async_probe_requested)
+	if (mod->async_probe_requested != PROBE_ASYNCHRONOUSLY)
 		async_synchronize_full();
 
 	ftrace_free_mem(mod, mod->mem[MOD_INIT_TEXT].base,
@@ -3366,12 +3388,16 @@ static int prepare_coming_module(struct module *mod)
 static int unknown_module_param_cb(char *param, char *val, const char 

*modname,
void *arg)
{
+ bool async_probe_requested;
struct module *mod = arg;
int ret;

if (strcmp(param, "async_probe") == 0) {
- if (kstrtobool(val, &mod->async_probe_requested))
- mod->async_probe_requested = true;
+ if (kstrtobool(val, &async_probe_requested))
+ async_probe_requested = true;
+ mod->async_probe_requested = async_probe_requested ?
+ PROBE_ASYNCHRONOUSLY :
+ PROBE_SYNCHRONOUSLY;
return 0;
}

maintainer
#4 lore raw

(Cc: Luis)

Quoted 5 lines · Bart Van Assche
Quoted 1 line · Danilo Krummrich

What if userspace did explicitly pass async_probe=0?

Does this mean that what the user has configured should take precedence, as in the untested patch below?

Yes, but my concern actually goes beyond that (sorry for not expressing this properly right away).

I think the whole reason the async_probe module parameter exists in the first place is because userspace may rely on the devices being handled by the module to be available directly after returning from the syscall.

Even worse, with this patch we would make this user facing behavior dependent on an implementation detail of the driver, i.e. whether it chooses to opt into PROBE_PREFER_ASYNCHRONOUS.