[PATCH] libcfs: Fix a sleep-in-atomic bug in cfs_percpt_lock and cfs_percpt_unlock

From: Jia-Ju Bai
Date: Wed May 31 2017 - 04:18:43 EST


The driver may sleep under a spin lock, and the function call path is:
cfs_percpt_lock/cfs_percpt_unlock (acquire the lock by spin_lock)
LASSERT
lbug_with_loc
libcfs_debug_dumplog
schedule and kthread_run --> may sleep

To fix it, all "LASSERT" is placed out of the spin_lock and spin_unlock.

Signed-off-by: Jia-Ju Bai <baijiaju1990@xxxxxxx>
---
drivers/staging/lustre/lnet/libcfs/libcfs_lock.c | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/lustre/lnet/libcfs/libcfs_lock.c b/drivers/staging/lustre/lnet/libcfs/libcfs_lock.c
index 1967b97c..a2ce092f 100644
--- a/drivers/staging/lustre/lnet/libcfs/libcfs_lock.c
+++ b/drivers/staging/lustre/lnet/libcfs/libcfs_lock.c
@@ -113,9 +113,10 @@ struct cfs_percpt_lock *

/* exclusive lock request */
for (i = 0; i < ncpt; i++) {
+ if (!i)
+ LASSERT(!pcl->pcl_locked);
spin_lock(pcl->pcl_locks[i]);
if (!i) {
- LASSERT(!pcl->pcl_locked);
/* nobody should take private lock after this
* so I wouldn't starve for too long time
*/
@@ -141,11 +142,11 @@ struct cfs_percpt_lock *
}

for (i = ncpt - 1; i >= 0; i--) {
- if (!i) {
- LASSERT(pcl->pcl_locked);
+ if (!i)
pcl->pcl_locked = 0;
- }
spin_unlock(pcl->pcl_locks[i]);
+ if (!i)
+ LASSERT(pcl->pcl_locked);
}
}
EXPORT_SYMBOL(cfs_percpt_unlock);
--
1.7.9.5