linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next v2] locking/percpu-rwsem: fix a task_struct refcount
@ 2020-03-30 21:30 Qian Cai
  2020-04-06 13:31 ` Qian Cai
  2020-04-08 12:20 ` [tip: locking/urgent] locking/percpu-rwsem: Fix " tip-bot2 for Qian Cai
  0 siblings, 2 replies; 4+ messages in thread
From: Qian Cai @ 2020-03-30 21:30 UTC (permalink / raw)
  To: peterz, mingo; +Cc: will, dbueso, juri.lelli, longman, linux-kernel, Qian Cai

The commit 7f26482a872c ("locking/percpu-rwsem: Remove the embedded
rwsem") introduced some task_struct memory leaks due to messing up with
a task_struct refcount. At the beginning of
percpu_rwsem_wake_function(), it calls get_task_struct(), but if the
trylock failed, it will remain in the waitqueue. However, it will run
percpu_rwsem_wake_function() again with get_task_struct() to increase
the refcount but then only call put_task_struct() once the trylock
succeeded.

Fix it by adjusting percpu_rwsem_wake_function() a bit to guard against
when percpu_rwsem_wait() observing !private, terminating the wait and
doing a quick exit() while percpu_rwsem_wake_function() then doing
wake_up_process(p) as a use-after-free.

Fixes: 7f26482a872c ("locking/percpu-rwsem: Remove the embedded rwsem")
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Qian Cai <cai@lca.pw>
---
 kernel/locking/percpu-rwsem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/locking/percpu-rwsem.c b/kernel/locking/percpu-rwsem.c
index a008a1ba21a7..8bbafe3e5203 100644
--- a/kernel/locking/percpu-rwsem.c
+++ b/kernel/locking/percpu-rwsem.c
@@ -118,14 +118,15 @@ static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entry,
 				      unsigned int mode, int wake_flags,
 				      void *key)
 {
-	struct task_struct *p = get_task_struct(wq_entry->private);
 	bool reader = wq_entry->flags & WQ_FLAG_CUSTOM;
 	struct percpu_rw_semaphore *sem = key;
+	struct task_struct *p;
 
 	/* concurrent against percpu_down_write(), can get stolen */
 	if (!__percpu_rwsem_trylock(sem, reader))
 		return 1;
 
+	p = get_task_struct(wq_entry->private);
 	list_del_init(&wq_entry->entry);
 	smp_store_release(&wq_entry->private, NULL);
 
-- 
2.21.0 (Apple Git-122.2)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH -next v2] locking/percpu-rwsem: fix a task_struct refcount
  2020-03-30 21:30 [PATCH -next v2] locking/percpu-rwsem: fix a task_struct refcount Qian Cai
@ 2020-04-06 13:31 ` Qian Cai
  2020-04-06 14:09   ` Peter Zijlstra
  2020-04-08 12:20 ` [tip: locking/urgent] locking/percpu-rwsem: Fix " tip-bot2 for Qian Cai
  1 sibling, 1 reply; 4+ messages in thread
From: Qian Cai @ 2020-04-06 13:31 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Will Deacon, dbueso, juri.lelli, Waiman Long, linux-kernel



> On Mar 30, 2020, at 5:30 PM, Qian Cai <cai@lca.pw> wrote:
> 
> The commit 7f26482a872c ("locking/percpu-rwsem: Remove the embedded
> rwsem") introduced some task_struct memory leaks due to messing up with
> a task_struct refcount. At the beginning of
> percpu_rwsem_wake_function(), it calls get_task_struct(), but if the
> trylock failed, it will remain in the waitqueue. However, it will run
> percpu_rwsem_wake_function() again with get_task_struct() to increase
> the refcount but then only call put_task_struct() once the trylock
> succeeded.
> 
> Fix it by adjusting percpu_rwsem_wake_function() a bit to guard against
> when percpu_rwsem_wait() observing !private, terminating the wait and
> doing a quick exit() while percpu_rwsem_wake_function() then doing
> wake_up_process(p) as a use-after-free.
> 
> Fixes: 7f26482a872c ("locking/percpu-rwsem: Remove the embedded rwsem")

Peter, Ingo, can you take a look at this patch when you have a chance?

For some reasons Ingo had decided to send a pull request which is now merged
even though I had informed the commit was broken a few days earlier, it makes no
sense to leave known memory leaks in mainline like this.

> Suggested-by: Peter Zijlstra <peterz@infradead.org>
> Signed-off-by: Qian Cai <cai@lca.pw>
> ---
> kernel/locking/percpu-rwsem.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/locking/percpu-rwsem.c b/kernel/locking/percpu-rwsem.c
> index a008a1ba21a7..8bbafe3e5203 100644
> --- a/kernel/locking/percpu-rwsem.c
> +++ b/kernel/locking/percpu-rwsem.c
> @@ -118,14 +118,15 @@ static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entry,
> 				      unsigned int mode, int wake_flags,
> 				      void *key)
> {
> -	struct task_struct *p = get_task_struct(wq_entry->private);
> 	bool reader = wq_entry->flags & WQ_FLAG_CUSTOM;
> 	struct percpu_rw_semaphore *sem = key;
> +	struct task_struct *p;
> 
> 	/* concurrent against percpu_down_write(), can get stolen */
> 	if (!__percpu_rwsem_trylock(sem, reader))
> 		return 1;
> 
> +	p = get_task_struct(wq_entry->private);
> 	list_del_init(&wq_entry->entry);
> 	smp_store_release(&wq_entry->private, NULL);
> 
> -- 
> 2.21.0 (Apple Git-122.2)
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH -next v2] locking/percpu-rwsem: fix a task_struct refcount
  2020-04-06 13:31 ` Qian Cai
@ 2020-04-06 14:09   ` Peter Zijlstra
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Zijlstra @ 2020-04-06 14:09 UTC (permalink / raw)
  To: Qian Cai
  Cc: Ingo Molnar, Will Deacon, dbueso, juri.lelli, Waiman Long, linux-kernel

On Mon, Apr 06, 2020 at 09:31:18AM -0400, Qian Cai wrote:
> 
> 
> > On Mar 30, 2020, at 5:30 PM, Qian Cai <cai@lca.pw> wrote:
> > 
> > The commit 7f26482a872c ("locking/percpu-rwsem: Remove the embedded
> > rwsem") introduced some task_struct memory leaks due to messing up with
> > a task_struct refcount. At the beginning of
> > percpu_rwsem_wake_function(), it calls get_task_struct(), but if the
> > trylock failed, it will remain in the waitqueue. However, it will run
> > percpu_rwsem_wake_function() again with get_task_struct() to increase
> > the refcount but then only call put_task_struct() once the trylock
> > succeeded.
> > 
> > Fix it by adjusting percpu_rwsem_wake_function() a bit to guard against
> > when percpu_rwsem_wait() observing !private, terminating the wait and
> > doing a quick exit() while percpu_rwsem_wake_function() then doing
> > wake_up_process(p) as a use-after-free.
> > 
> > Fixes: 7f26482a872c ("locking/percpu-rwsem: Remove the embedded rwsem")
> 
> Peter, Ingo, can you take a look at this patch when you have a chance?

I have it queued for urgent, should probably hit tip soon.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [tip: locking/urgent] locking/percpu-rwsem: Fix a task_struct refcount
  2020-03-30 21:30 [PATCH -next v2] locking/percpu-rwsem: fix a task_struct refcount Qian Cai
  2020-04-06 13:31 ` Qian Cai
@ 2020-04-08 12:20 ` tip-bot2 for Qian Cai
  1 sibling, 0 replies; 4+ messages in thread
From: tip-bot2 for Qian Cai @ 2020-04-08 12:20 UTC (permalink / raw)
  To: linux-tip-commits; +Cc: Peter Zijlstra, Qian Cai, Ingo Molnar, x86, LKML

The following commit has been merged into the locking/urgent branch of tip:

Commit-ID:     d22cc7f67d55ebf2d5be865453971c783e9fb21a
Gitweb:        https://git.kernel.org/tip/d22cc7f67d55ebf2d5be865453971c783e9fb21a
Author:        Qian Cai <cai@lca.pw>
AuthorDate:    Mon, 30 Mar 2020 17:30:02 -04:00
Committer:     Ingo Molnar <mingo@kernel.org>
CommitterDate: Wed, 08 Apr 2020 12:05:06 +02:00

locking/percpu-rwsem: Fix a task_struct refcount

The following commit:

  7f26482a872c ("locking/percpu-rwsem: Remove the embedded rwsem")

introduced task_struct memory leaks due to messing up the task_struct
refcount.

At the beginning of percpu_rwsem_wake_function(), it calls get_task_struct(),
but if the trylock failed, it will remain in the waitqueue. However, it
will run percpu_rwsem_wake_function() again with get_task_struct() to
increase the refcount but then only call put_task_struct() once the trylock
succeeded.

Fix it by adjusting percpu_rwsem_wake_function() a bit to guard against
when percpu_rwsem_wait() observing !private, terminating the wait and
doing a quick exit() while percpu_rwsem_wake_function() then doing
wake_up_process(p) as a use-after-free.

Fixes: 7f26482a872c ("locking/percpu-rwsem: Remove the embedded rwsem")
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Qian Cai <cai@lca.pw>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Link: https://lkml.kernel.org/r/20200330213002.2374-1-cai@lca.pw
---
 kernel/locking/percpu-rwsem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kernel/locking/percpu-rwsem.c b/kernel/locking/percpu-rwsem.c
index a008a1b..8bbafe3 100644
--- a/kernel/locking/percpu-rwsem.c
+++ b/kernel/locking/percpu-rwsem.c
@@ -118,14 +118,15 @@ static int percpu_rwsem_wake_function(struct wait_queue_entry *wq_entry,
 				      unsigned int mode, int wake_flags,
 				      void *key)
 {
-	struct task_struct *p = get_task_struct(wq_entry->private);
 	bool reader = wq_entry->flags & WQ_FLAG_CUSTOM;
 	struct percpu_rw_semaphore *sem = key;
+	struct task_struct *p;
 
 	/* concurrent against percpu_down_write(), can get stolen */
 	if (!__percpu_rwsem_trylock(sem, reader))
 		return 1;
 
+	p = get_task_struct(wq_entry->private);
 	list_del_init(&wq_entry->entry);
 	smp_store_release(&wq_entry->private, NULL);
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-04-08 12:21 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-30 21:30 [PATCH -next v2] locking/percpu-rwsem: fix a task_struct refcount Qian Cai
2020-04-06 13:31 ` Qian Cai
2020-04-06 14:09   ` Peter Zijlstra
2020-04-08 12:20 ` [tip: locking/urgent] locking/percpu-rwsem: Fix " tip-bot2 for Qian Cai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).