Skip to contentSkip to conversation

klist: del waiter from klist_remove_waiters before wakeup waitting process

PATCH In review wangbiao 3 participants 3 messages May 3, 2013 – May 7, 2013 Library code

lore
Share & tools
lore

wangbiao

author
#0 lore raw

PATCH 1/1 klist: del waiter from klist_remove_waiters before wakeup waitting process

From: "wang, biao" <[email protected]>
Date: Fri, 3 May 2013 14:18:34 +0800
Subject: [PATCH] klist: del waiter from klist_remove_waiters before wakeup
waitting process

There is a race between klist_remove and klist_release. klist_remove uses a local var waiter saved on stack. When klist_release calls wake_up_process(waiter->process) to wake up the waiter, waiter might run immediately and reuse the stack. Then, klist_release calls list_del(&waiter->list) to change previous
wait data and cause prior waiter thread corrupt.

The patch fixes it against kernel 3.9.

---
 lib/klist.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
lib/klist.c +1 -1
index 0874e41..358a368 100644
@@ -193,10 +193,10 @@ static void klist_release(struct kref *kref)
 		if (waiter->node != n)
 			continue;
 
+		list_del(&waiter->list);
 		waiter->woken = 1;
 		mb();
 		wake_up_process(waiter->process);
-		list_del(&waiter->list);
 	}
 	spin_unlock(&klist_remove_lock);
 	knode_set_klist(n, NULL);
Signature · 1 line
1.7.6
Acked-by on v1 1/1
#1 lore raw
Quoted 5 lines · wangbiao

From: "wang, biao" <[email protected]>
Date: Fri, 3 May 2013 14:18:34 +0800
Subject: [PATCH] klist: del waiter from klist_remove_waiters before wakeup
waitting process

Quoted 8 lines

There is a race between klist_remove and klist_release. klist_remove uses a local var waiter saved on stack. When klist_release calls wake_up_process(waiter->process) to wake up the waiter, waiter might run immediately and reuse the stack. Then, klist_release calls list_del(&waiter->list) to change previous
wait data and cause prior waiter thread corrupt.

The patch fixes it against kernel 3.9.

I've never seen that code before in my life; but after a quick look you appear to be completely right.

Quoted 1 line
Quoted 25 lines
---
 lib/klist.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
lib/klist.c +1 -1
index 0874e41..358a368 100644
@@ -193,10 +193,10 @@ static void klist_release(struct kref *kref)
 		if (waiter->node != n)
 			continue;
 
+		list_del(&waiter->list);
 		waiter->woken = 1;
 		mb();
 		wake_up_process(waiter->process);
-		list_del(&waiter->list);
 	}
 	spin_unlock(&klist_remove_lock);
 	knode_set_klist(n, NULL);
Signature · 1 line
1.7.6
#2 lore raw

Andrew,

Would you like to merge the patch to your MM tree?

Yanmin

Quoted 56 lines

-----Original Message-----
From: Peter Zijlstra [mailto:[email protected]]
Sent: Friday, May 03, 2013 6:42 PM
To: Wang, Biao
Cc: [email protected]; [email protected]; [email protected]; Zhang, Yanmin; [email protected]
Subject: Re: [PATCH] klist: del waiter from klist_remove_waiters before wakeup waitting process

Quoted 5 lines · wangbiao

From: "wang, biao" <[email protected]>
Date: Fri, 3 May 2013 14:18:34 +0800
Subject: [PATCH] klist: del waiter from klist_remove_waiters before wakeup waitting process

Quoted 8 lines

There is a race between klist_remove and klist_release. klist_remove uses a local var waiter saved on stack. When klist_release calls wake_up_process(waiter->process) to wake up the waiter, waiter might run immediately and reuse the stack. Then, klist_release calls list_del(&waiter->list) to change previous wait data and cause prior waiter thread corrupt.

The patch fixes it against kernel 3.9.

I've never seen that code before in my life; but after a quick look you appear to be completely right.

Quoted 1 line
Quoted 24 lines
---
 lib/klist.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
lib/klist.c +1 -2
@@ -193,10 +193,10 @@ static void klist_release(struct kref *kref)
 		if (waiter->node != n)
 			continue;
 
+		list_del(&waiter->list);
 		waiter->woken = 1;
 		mb();
 		wake_up_process(waiter->process);
-		list_del(&waiter->list);
 	}
 	spin_unlock(&klist_remove_lock);
 	knode_set_klist(n, NULL);
--

1.7.6