Re: [PATCH v4] do_wait: make PIDTYPE_PID case O(1) instead of O(n)

From: Oleg Nesterov
Date: Fri Mar 12 2021 - 11:42:14 EST


On 03/11, Jim Newsome wrote:
>
> +static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
> + struct task_struct *target)
> +{
> + struct task_struct *parent =
> + !ptrace ? target->real_parent : target->parent;
> +
> + return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
> + same_thread_group(current, parent));
> +}
> +
> +/*
> + * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
> + * and tracee lists to find the target task.
> + */
> +static int do_wait_pid(struct wait_opts *wo)
> +{
> + bool ptrace;
> + struct task_struct *target;
> + int retval;
> +
> + ptrace = false;
> +
> + /* A non-ptrace wait can only be performed on a thread group leader. */
> + target = pid_task(wo->wo_pid, PIDTYPE_TGID);
> +
> + if (target && is_effectively_child(wo, ptrace, target)) {
> + retval = wait_consider_task(wo, ptrace, target);
> + if (retval)
> + return retval;
> + }
> +
> + ptrace = true;
> +
> + /* A ptrace wait can be done on non-thread-group-leaders. */
> + if (!target)
> + target = pid_task(wo->wo_pid, PIDTYPE_PID);
> +
> + if (target && is_effectively_child(wo, ptrace, target)) {
> + retval = wait_consider_task(wo, ptrace, target);

No, this is not right... You need to check target->ptrace != 0.

I know that Eric suggests to not use thread_group_leader() and I won't argue
even if I don't really agree.

Up to you, but to me something like

do_wait_pid()
{
target = pid_task(wo->wo_pid, PIDTYPE_PID);

if (!target)
return 0;

if (thread_group_leader(target) &&
is_effectively_child(wo, 0, target) {
...
}

if (target->ptrace &&
is_effectively_child(wo, 1, target) {
...
}

return 0;

}

looks more simple/clean.

Oleg.