linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -mm 0/3] proc: task->signal can't be NULL
@ 2010-03-22 18:41 Oleg Nesterov
  2010-03-23  2:18 ` Eric W. Biederman
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2010-03-22 18:41 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Alexey Dobriyan, Eric W. Biederman, Roland McGrath, linux-kernel

With the recent changes in -mm it is always safe to dereference
task->signal. It can't be NULL and it is pinned to task_struct.

fs/proc becomes the only valid user of signal->count which should
either die or become "int nr_threads".


Alexey, Eric.

Can't we kill this counter? Afaics, get_nr_threads() doesn't need to
be "precise", we probably can estimate the number of threads using
signal->live (yes sure, we can't use ->live as nr_threads).

Except: first_tid() uses get_nr_threads() for optimization. Is this
optimization really important? Afaics, it only helps in the unlikely
case, probably in that case the extra lockless while_each_thread()
doesn't hurt.

IOW, how about

	--- a/fs/proc/base.c
	+++ b/fs/proc/base.c
	@@ -3071,11 +3071,6 @@ static struct task_struct *first_tid(str
				goto found;
		}
	 
	-	/* If nr exceeds the number of threads there is nothing todo */
	-	pos = NULL;
	-	if (nr && nr >= get_nr_threads(leader))
	-		goto out;
	-
		/* If we haven't found our starting place yet start
		 * with the leader and walk nr threads forward.
		 */

?

Not that I think it is terribly important to kill this counter, and
probably signal->nr_threads can make sense anyway, so far I am just
curious.

Oleg.


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

* Re: [PATCH -mm 0/3] proc: task->signal can't be NULL
  2010-03-22 18:41 [PATCH -mm 0/3] proc: task->signal can't be NULL Oleg Nesterov
@ 2010-03-23  2:18 ` Eric W. Biederman
  2010-03-23 18:31   ` Oleg Nesterov
  0 siblings, 1 reply; 5+ messages in thread
From: Eric W. Biederman @ 2010-03-23  2:18 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Alexey Dobriyan, Roland McGrath, linux-kernel

Oleg Nesterov <oleg@redhat.com> writes:

> With the recent changes in -mm it is always safe to dereference
> task->signal. It can't be NULL and it is pinned to task_struct.
>
> fs/proc becomes the only valid user of signal->count which should
> either die or become "int nr_threads".
>
>
> Alexey, Eric.
>
> Can't we kill this counter? Afaics, get_nr_threads() doesn't need to
> be "precise", we probably can estimate the number of threads using
> signal->live (yes sure, we can't use ->live as nr_threads).
>
> Except: first_tid() uses get_nr_threads() for optimization. Is this
> optimization really important? Afaics, it only helps in the unlikely
> case, probably in that case the extra lockless while_each_thread()
> doesn't hurt.
>
> IOW, how about
>
> 	--- a/fs/proc/base.c
> 	+++ b/fs/proc/base.c
> 	@@ -3071,11 +3071,6 @@ static struct task_struct *first_tid(str
> 				goto found;
> 		}
> 	 
> 	-	/* If nr exceeds the number of threads there is nothing todo */
> 	-	pos = NULL;
> 	-	if (nr && nr >= get_nr_threads(leader))
> 	-		goto out;
> 	-
> 		/* If we haven't found our starting place yet start
> 		 * with the leader and walk nr threads forward.
> 		 */
>
> ?
>
> Not that I think it is terribly important to kill this counter, and
> probably signal->nr_threads can make sense anyway, so far I am just
> curious.

I think that was just a sanity check since it was easy.  I want to say
it prevents a DOS attack with user space passing unreasonably large
file position but that DOS attack is handled by ensuring we don't walk
through the list if threads more than once.

However:
proc_task_getattr uses get_nr_threads to get it's nlink count correct.

Not walking the thread list to get the number of threads seems like an
important cpu time saving measure.

Eric


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

* Re: [PATCH -mm 0/3] proc: task->signal can't be NULL
  2010-03-23  2:18 ` Eric W. Biederman
@ 2010-03-23 18:31   ` Oleg Nesterov
  2010-03-23 20:53     ` Eric W. Biederman
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2010-03-23 18:31 UTC (permalink / raw)
  To: Eric W. Biederman
  Cc: Andrew Morton, Alexey Dobriyan, Roland McGrath, linux-kernel

On 03/22, Eric W. Biederman wrote:
>
> Oleg Nesterov <oleg@redhat.com> writes:
>
> > Can't we kill this counter? Afaics, get_nr_threads() doesn't need to
> > be "precise", we probably can estimate the number of threads using
> > signal->live (yes sure, we can't use ->live as nr_threads).
> >
> > Except: first_tid() uses get_nr_threads() for optimization. Is this
> > optimization really important? Afaics, it only helps in the unlikely
> > case, probably in that case the extra lockless while_each_thread()
> > doesn't hurt.
> >
> > IOW, how about
> >
> > 	--- a/fs/proc/base.c
> > 	+++ b/fs/proc/base.c
> > 	@@ -3071,11 +3071,6 @@ static struct task_struct *first_tid(str
> > 				goto found;
> > 		}
> >
> > 	-	/* If nr exceeds the number of threads there is nothing todo */
> > 	-	pos = NULL;
> > 	-	if (nr && nr >= get_nr_threads(leader))
> > 	-		goto out;
> > 	-
> > 		/* If we haven't found our starting place yet start
> > 		 * with the leader and walk nr threads forward.
> > 		 */
> >
> > ?
> >
> > Not that I think it is terribly important to kill this counter, and
> > probably signal->nr_threads can make sense anyway, so far I am just
> > curious.
>
> I think that was just a sanity check since it was easy.  I want to say
> it prevents a DOS attack with user space passing unreasonably large
> file position but that DOS attack is handled by ensuring we don't walk
> through the list if threads more than once.

If a bad user passes the large f_pos > nr_threads then this check
eliminates the unneeded while_each_thread() loop, yes. But it can use
f_pos == nr_threads and provoke the same loop?

Or. just do rewinddir() + readdir(big_count). Now we walk through the
list and call proc_task_fill_cache() for each entry.

IOW, I don't understand how this check can help from the DOS pov.

> However:
> proc_task_getattr uses get_nr_threads to get it's nlink count correct.

Yes. But we don't need the exactly precise number here if we are
racing with fork/exit ?

> Not walking the thread list to get the number of threads seems like an
> important cpu time saving measure.

Not sure I understand... Also, first_tid() could use sig->sigcnt (the
reference counter) instead of sig->count. This is not the same, but I
think in practice this is fine.


OK. Let's keep this counter as "int nr_thread".

Besides, when I tried to re-implement get_nr_threads() using signal->live
I got the really ugly result ;)

Thanks.

Oleg.


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

* Re: [PATCH -mm 0/3] proc: task->signal can't be NULL
  2010-03-23 18:31   ` Oleg Nesterov
@ 2010-03-23 20:53     ` Eric W. Biederman
  2010-03-24 17:49       ` Oleg Nesterov
  0 siblings, 1 reply; 5+ messages in thread
From: Eric W. Biederman @ 2010-03-23 20:53 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Alexey Dobriyan, Roland McGrath, linux-kernel

Oleg Nesterov <oleg@redhat.com> writes:

> On 03/22, Eric W. Biederman wrote:
>>
>> Oleg Nesterov <oleg@redhat.com> writes:
>
> If a bad user passes the large f_pos > nr_threads then this check
> eliminates the unneeded while_each_thread() loop, yes. But it can use
> f_pos == nr_threads and provoke the same loop?
>
> Or. just do rewinddir() + readdir(big_count). Now we walk through the
> list and call proc_task_fill_cache() for each entry.
>
> IOW, I don't understand how this check can help from the DOS pov.

It can't.   I just want it to be able to ;)

>> However:
>> proc_task_getattr uses get_nr_threads to get it's nlink count correct.
>
> Yes. But we don't need the exactly precise number here if we are
> racing with fork/exit ?
>
>> Not walking the thread list to get the number of threads seems like an
>> important cpu time saving measure.
>
> Not sure I understand... Also, first_tid() could use sig->sigcnt (the
> reference counter) instead of sig->count. This is not the same, but I
> think in practice this is fine.

We need a value that can be computed in constant time, and is not correct
except when the number of threads is actively changing.

> OK. Let's keep this counter as "int nr_thread".
>
> Besides, when I tried to re-implement get_nr_threads() using signal->live
> I got the really ugly result ;)

Sounds good.

Eric

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

* Re: [PATCH -mm 0/3] proc: task->signal can't be NULL
  2010-03-23 20:53     ` Eric W. Biederman
@ 2010-03-24 17:49       ` Oleg Nesterov
  0 siblings, 0 replies; 5+ messages in thread
From: Oleg Nesterov @ 2010-03-24 17:49 UTC (permalink / raw)
  To: Eric W. Biederman
  Cc: Andrew Morton, Alexey Dobriyan, Roland McGrath, linux-kernel

On 03/23, Eric W. Biederman wrote:
>
> We need a value that can be computed in constant time, and is not correct
> except when the number of threads is actively changing.

Sure. I was thinking of something like

	int get_nr_threads(struct task_struct *tsk)
	{
		int nr = atomic_read(&task->signal->live);
		int reasonable_min = 1;

		rcu_read_lock();
		if (!thread_group_leader(tsk) && pid_alive(tsk) &&
		    tsk->group_leader->exit_state)
			reasonable_min = 2;
		rcu_read_unlock();

		return max(nr, reasonable_min);
	}

but as I said this doesn't look nice at all.

> > OK. Let's keep this counter as "int nr_thread".
> >
> > Besides, when I tried to re-implement get_nr_threads() using signal->live
> > I got the really ugly result ;)
>
> Sounds good.

OK, please see the "final" patch I am going to send...

Oleg.


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

end of thread, other threads:[~2010-03-24 17:51 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-22 18:41 [PATCH -mm 0/3] proc: task->signal can't be NULL Oleg Nesterov
2010-03-23  2:18 ` Eric W. Biederman
2010-03-23 18:31   ` Oleg Nesterov
2010-03-23 20:53     ` Eric W. Biederman
2010-03-24 17:49       ` Oleg Nesterov

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).