mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kirill Tkhai <tkhai@yandex.ru>
To: Oleg Nesterov <oleg@redhat.com>,
	Kirill Tkhai <ktkhai@parallels.com>,
	Peter Zijlstra <peterz@infradead.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Ingo Molnar <mingo@redhat.com>,
	Vladimir Davydov <vdavydov@parallels.com>
Subject: Re: [PATCH] sched/numa: fix unsafe get_task_struct() in task_numa_assign()
Date: Sat, 18 Oct 2014 12:33:27 +0400	[thread overview]
Message-ID: <1011271413621207@web30j.yandex.ru> (raw)
In-Reply-To: <4323181413620101@web21o.yandex.ru>

18.10.2014, 12:15, "Kirill Tkhai" <tkhai@yandex.ru>:
> 18.10.2014, 01:40, "Oleg Nesterov" <oleg@redhat.com>:
>>  The lockless get_task_struct(tsk) is only safe if tsk == current
>>  and didn't pass exit_notify(), or if this tsk was found on a rcu
>>  protected list (say, for_each_process() or find_task_by_vpid()).
>>  IOW, it is only safe if release_task() was not called before we
>>  take rcu_read_lock(), in this case we can rely on the fact that
>>  delayed_put_pid() can not drop the (potentially) last reference
>>  until rcu_read_unlock().
>>
>>  And as Kirill pointed out task_numa_compare()->task_numa_assign()
>>  path does get_task_struct(dst_rq->curr) and this is not safe. The
>>  task_struct itself can't go away, but rcu_read_lock() can't save
>>  us from the final put_task_struct() in finish_task_switch(); this
>>  reference goes away without rcu gp.
>>
>>  Reported-by: Kirill Tkhai <ktkhai@parallels.com>
>>  Signed-off-by: Oleg Nesterov <oleg@redhat.com>
>>  ---
>>   kernel/sched/fair.c |    8 +++++++-
>>   1 files changed, 7 insertions(+), 1 deletions(-)
>>
>>  diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
>>  index 0090e8c..52049b9 100644
>>  --- a/kernel/sched/fair.c
>>  +++ b/kernel/sched/fair.c
>>  @@ -1158,7 +1158,13 @@ static void task_numa_compare(struct task_numa_env *env,
>>
>>           rcu_read_lock();
>>           cur = ACCESS_ONCE(dst_rq->curr);
>>  - if (cur->pid == 0) /* idle */
>>  + /*
>>  + * No need to move the exiting task, and this ensures that ->curr
>>  + * wasn't reaped and thus get_task_struct() in task_numa_assign()
>>  + * is safe; note that rcu_read_lock() can't protect from the final
>>  + * put_task_struct() after the last schedule().
>>  + */
>>  + if (is_idle_task(cur) || (cur->flags & PF_EXITING))
>>                   cur = NULL;
>>
>>           /*
>
> Oleg, I've looked once again, and now it's not good for me.
> Where is the guarantee this memory hasn't been allocated again?
> If so, PF_EXITING is not of the task we are interesting, but it's
> not a task's even.
>
> rcu_read_lock()                   ...                           ...
> cur = ACCESS_ONCE(dst_rq->curr);  ...                           ...
> <interrupt>                       rq->curr = next;              ...
> <interrupt>                           put_prev_task()           ...
> <interrupt>                               __put_prev_task       ...
> <interrupt>                                  kmem_cache_free()  ...
> <interrupt>                                  ...                <alocated again>
> <interrupt>                                  ...                memset(, 0, )
> <interrupt>                                  ...                ...
> if (cur->flags & PF_EXITING)                 ...                ...
>     <no>                                     ...                ...
> get_task_struct()                            ...                ...

How about this?

diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c
index b78280c..d46427e 100644
--- a/kernel/sched/fair.c
+++ b/kernel/sched/fair.c
@@ -1165,7 +1165,21 @@ static void task_numa_compare(struct task_numa_env *env,
 
 	rcu_read_lock();
 	cur = ACCESS_ONCE(dst_rq->curr);
-	if (cur->pid == 0) /* idle */
+	/*
+	 * No need to move the exiting task, and this ensures that ->curr
+	 * wasn't reaped and thus get_task_struct() in task_numa_assign()
+	 * is safe; note that rcu_read_lock() can't protect from the final
+	 * put_task_struct() after the last schedule().
+	 */
+	if (is_idle_task(cur) || (cur->flags & PF_EXITING))
+		cur = NULL;
+	/*
+	 * Check once again to be sure curr is still on dst_rq. Even if
+	 * it points on a new task, which is using the memory of freed
+	 * cur, it's OK, because we've locked RCU before
+	 * delayed_put_task_struct() callback is called to put its struct.
+	 */
+	if (cur != ACCESS_ONCE(dst_rq->curr))
 		cur = NULL;
 
 	/*

  reply	other threads:[~2014-10-18  8:33 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-15 12:31 [PATCH RFC] sched: Revert delayed_put_task_struct() and fix use after free Kirill Tkhai
2014-10-15 15:06 ` Oleg Nesterov
2014-10-15 19:40   ` Oleg Nesterov
2014-10-15 21:46     ` Kirill Tkhai
2014-10-15 22:02       ` Kirill Tkhai
2014-10-16  7:59       ` Peter Zijlstra
2014-10-16  8:16         ` Kirill Tkhai
2014-10-16  9:43           ` Peter Zijlstra
2014-10-16  9:50             ` Kirill Tkhai
2014-10-16  9:51               ` Kirill Tkhai
2014-10-16 10:04                 ` Kirill Tkhai
2014-10-17 21:34       ` Oleg Nesterov
2014-10-16  7:56     ` Peter Zijlstra
2014-10-16  8:01   ` Peter Zijlstra
2014-10-16 22:05     ` Oleg Nesterov
2014-10-17 21:36 ` [PATCH] sched/numa: fix unsafe get_task_struct() in task_numa_assign() Oleg Nesterov
2014-10-18  8:15   ` Kirill Tkhai
2014-10-18  8:33     ` Kirill Tkhai [this message]
2014-10-18 19:36       ` Peter Zijlstra
2014-10-18 21:18         ` Oleg Nesterov
2014-10-19  8:20         ` Kirill Tkhai
2014-10-18 20:56     ` Oleg Nesterov
2014-10-18 23:13       ` Kirill Tkhai
2014-10-19 19:24         ` Oleg Nesterov
2014-10-19 19:37           ` Oleg Nesterov
2014-10-19 19:43             ` Oleg Nesterov
2014-10-20  9:03               ` Kirill Tkhai
2014-10-20  9:13             ` Peter Zijlstra
2014-10-20 10:36               ` Kirill Tkhai
2014-10-20  9:00           ` Kirill Tkhai
2014-10-19 21:38         ` Peter Zijlstra
2014-10-20  8:56           ` Kirill Tkhai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1011271413621207@web30j.yandex.ru \
    --to=tkhai@yandex.ru \
    --cc=ktkhai@parallels.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=vdavydov@parallels.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®