Skip to contentSkip to conversation

staging: lustre: add error handling for try_module_get

PATCH In review Zhouyang Jia 4 participants 5 messages Jun 12, 2018 – Jun 13, 2018 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: lustre: add error handling for try_module_get

When try_module_get fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling try_module_get.

---
 drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +4 -1
index 7086678..72a42bd 100644
@@ -2422,7 +2422,10 @@ ksocknal_base_startup(void)
 
 	/* flag lists/ptrs/locks initialised */
 	ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
-	try_module_get(THIS_MODULE);
+	if (!try_module_get(THIS_MODULE)) {
+		CERROR("%s: cannot get module\n", __func__);
+		goto failed;
+	}
 
 	ksocknal_data.ksnd_sched_info = cfs_percpt_alloc(lnet_cpt_table(),
 							 sizeof(*info));
Signature · 1 line
2.7.4
#1 lore raw
Quoted 16 lines · Zhouyang Jia

When try_module_get fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling try_module_get.

---
 drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +0 -0
index 7086678..72a42bd 100644
@@ -2422,7 +2422,10 @@ ksocknal_base_startup(void)
 

drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c ksocknal_base_startup(void) · line 2431

Quoted 10 lines
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +4 -1
 	/* flag lists/ptrs/locks initialised */
 	ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
-	try_module_get(THIS_MODULE);
+	if (!try_module_get(THIS_MODULE)) {
+		CERROR("%s: cannot get module\n", __func__);
+		goto failed;
+	}
 
 	ksocknal_data.ksnd_sched_info = cfs_percpt_alloc(lnet_cpt_table(),
 							 sizeof(*info));

Thanks for the patch....
I agree that this is probably a bug, but the code is still buggy after you patch, just in a different way.
Try following through the code and see what happens when you 'goto failed'.

NeilBrown

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180612/9407950d/attachment.sig>

#2 lore raw
In reply to Zhouyang Jia
Quoted 8 lines · Zhouyang Jia

When try_module_get fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling try_module_get.

---
 drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c | 5 ++++-

This patch does not apply to Linus's tree. Always be sure to work against linux-next to catch things like this.

thanks,

greg k-h

#3 lore raw
In reply to Zhouyang Jia

From: Zhouyang Jia

Quoted 6 lines

Sent: 12 June 2018 05:49

When try_module_get fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling try_module_get.

...

drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c ksocknal_base_startup(void) · line 2428

Quoted 10 lines

+++ b/drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c

drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +4 -1
@@ -2422,7 +2422,10 @@ ksocknal_base_startup(void)
 
 	/* flag lists/ptrs/locks initialised */
 	ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
-	try_module_get(THIS_MODULE);
+	if (!try_module_get(THIS_MODULE)) {
+		CERROR("%s: cannot get module\n", __func__);
+		goto failed;
+	}

Can try_module_get(THIS_MODULE) ever fail?
Since you are running code in 'THIS_MODULE' the caller must have a reference that can't go away.
So try_module_get() just increments the count that is already greater than zero.

Similarly module_put(THIS_MODULE) must never be able to release the last reference.
Any such calls that aren't in error paths after try_module_get() are probably buggy.

David

#4 lore raw
Quoted 11 lines · David Laight

From: Zhouyang Jia

Quoted 6 lines

Sent: 12 June 2018 05:49

When try_module_get fails, the lack of error-handling code may cause unexpected results.

This patch adds error-handling code after calling try_module_get.

...

Quoted 3 lines

+++ b/drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c

(quoted diff) +0 -0
@@ -2422,7 +2422,10 @@ ksocknal_base_startup(void)
 
Quoted 10 lines

drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c ksocknal_base_startup(void) · line 2428

Quoted 7 lines
drivers/staging/lustre/lnet/klnds/socklnd/socklnd.c +4 -1
 	/* flag lists/ptrs/locks initialised */
 	ksocknal_data.ksnd_init = SOCKNAL_INIT_DATA;
-	try_module_get(THIS_MODULE);
+	if (!try_module_get(THIS_MODULE)) {
+		CERROR("%s: cannot get module\n", __func__);
+		goto failed;
+	}

Can try_module_get(THIS_MODULE) ever fail?

Yes.

Quoted 2 lines

Since you are running code in 'THIS_MODULE' the caller must have a reference that can't go away.

Not necessarily, though it does usually work that way.

try_module_get() can fail while the exit function is running, but it is safe to run code in the module until the exit function completes. So if the exit function takes a lock, then other code can safely run code in the module while holding the lock, but not holding a reference to the module. If this code calls try_module_get(), it could fail.

That is exactly what is happening here. ksoclnd_exit() calls lnet_unregister_lnd() which takes the_lnet.ln_lnd_mutex.

ksocknal_base_startup() is called from ksocknal_startup() which is the_ksocklnd.lnd_startup and is called, from lnet_startup_lndni(), with that lock held.

Quoted 5 lines

So try_module_get() just increments the count that is already greater than zero.

Similarly module_put(THIS_MODULE) must never be able to release the last reference.

It can if a suitable lock is held.

Quoted 2 lines

Any such calls that aren't in error paths after try_module_get() are probably buggy.

Being in an error path doesn't make it safe. module_put(THIS_MODULE) can only be safe if a lock is held which prevents the exit function from completing. Some code outside the module must release the lock.

Having said that, I don't really like this approach. I much prefer for the module reference to be taken and put outside of the module - it seems less error-prone.

NeilBrown
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://lists.lustre.org/pipermail/lustre-devel-lustre.org/attachments/20180613/d4ecae9b/attachment.sig>