public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: patch: do_wait: make PIDTYPE_PID case O(1) instead of O(n)
       [not found] <b8591e67-1248-6c29-847e-d97e04b58210@torproject.org>
@ 2021-03-09  8:56 ` Oleg Nesterov
  2021-03-09 16:18   ` Jim Newsome
  0 siblings, 1 reply; 2+ messages in thread
From: Oleg Nesterov @ 2021-03-09  8:56 UTC (permalink / raw)
  To: Jim Newsome
  Cc: Andrew Morton, Eric W. Biederman, Christian Brauner, linux-kernel

Ah, and you forgot to CC lkml ;) let me resend my email.


Hi Jim,

Please do not use the attachments, just send the patch as plain text.
See Documentation/process/submitting-patches.rst

On 03/08, Jim Newsome wrote:
>
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
> @@ -1462,8 +1462,61 @@ static long do_wait(struct wait_opts *wo)
>  		goto notask;
>
>  	set_current_state(TASK_INTERRUPTIBLE);
> +
>  	read_lock(&tasklist_lock);
>  	tsk = current;
> +
> +	if (wo->wo_type == PIDTYPE_PID) {
> +		// Optimization for PIDTYPE_PID. No need to iterate through child and
> +		// tracee lists to find the target task.

I'd suggest to put this PIDTYPE_PID code into the new function.

> +
> +		struct task_struct *real_parent = NULL;
> +		struct task_struct *target = NULL;
> +		bool do_regular_wait, do_ptrace_wait;
> +
> +		// XXX: Do we need this? Or is the tasklist_lock sufficient?
> +		rcu_read_lock();

No, you don't need rcu lock, tasklist_lock is sufficient

> +		target = pid_task(wo->wo_pid, PIDTYPE_PID);
> +		if (!target) {
> +			rcu_read_unlock();
> +			goto notask;

This is wrong, you forgot to drop tasklist_lock.


> +		real_parent = !target->real_parent ? target->parent :
> +						     target->real_parent;

Hmm, I don't understand the line above... perhaps it connects to the
question below.

> +		if (!real_parent) {
> +			// XXX: Is it a kernel bug to get here? Or would this be
> +			// true of the init process?

Afaics, parent/real_parent can't be NULL if pid_task() succeeds.

> +		do_regular_wait = tsk == real_parent ||
> +				  (!(wo->wo_flags & __WNOTHREAD) &&
> +				   same_thread_group(tsk, real_parent));
> +		do_ptrace_wait = target->ptrace &&
> +				 (tsk == target->parent ||
> +				  (!(wo->wo_flags & __WNOTHREAD) &&
> +				   same_thread_group(tsk, target->parent)));
> +		rcu_read_unlock();
> +
> +		if (do_regular_wait) {
> +			retval =
> +				wait_consider_task(wo, /* ptrace= */ 0, target);
> +			if (retval) {
> +				goto end;
> +			}
> +		}
> +		if (do_ptrace_wait) {
> +			retval =
> +				wait_consider_task(wo, /* ptrace= */ 1, target);
> +			if (retval) {
> +				goto end;
> +			}
> +		}
> +		read_unlock(&tasklist_lock);
> +		goto notask;

This part looks correct at first glance...

Please redo and send V2 ;)

Oleg.


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

* Re: patch: do_wait: make PIDTYPE_PID case O(1) instead of O(n)
  2021-03-09  8:56 ` patch: do_wait: make PIDTYPE_PID case O(1) instead of O(n) Oleg Nesterov
@ 2021-03-09 16:18   ` Jim Newsome
  0 siblings, 0 replies; 2+ messages in thread
From: Jim Newsome @ 2021-03-09 16:18 UTC (permalink / raw)
  To: Oleg Nesterov
  Cc: Andrew Morton, Eric W. Biederman, Christian Brauner, linux-kernel

Thanks for the quick review!

On 3/9/21 02:56, Oleg Nesterov wrote:
> I'd suggest to put this PIDTYPE_PID code into the new function.

Done

> No, you don't need rcu lock, tasklist_lock is sufficient

Done

> This is wrong, you forgot to drop tasklist_lock.

Done

>> +		real_parent = !target->real_parent ? target->parent :
>> +						     target->real_parent;
> 
> Hmm, I don't understand the line above... perhaps it connects to the
> question below.

I'd mistakenly thought `real_parent` was NULL if the target wasn't being
traced (or was being traced by the real parent). It looks like indeed
this should always be set, though.

> Afaics, parent/real_parent can't be NULL if pid_task() succeeds.

Done

> Please redo and send V2 ;)

Sent with subject "[PATCH v2] do_wait: make PIDTYPE_PID case O(1)
instead of O(n)"

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

end of thread, other threads:[~2021-03-09 16:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <b8591e67-1248-6c29-847e-d97e04b58210@torproject.org>
2021-03-09  8:56 ` patch: do_wait: make PIDTYPE_PID case O(1) instead of O(n) Oleg Nesterov
2021-03-09 16:18   ` Jim Newsome

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox