Re: [PATCH v2 2/2] proc: Ensure we see the exit of each process tid exactly

From: Linus Torvalds
Date: Thu Apr 23 2020 - 16:28:41 EST


On Thu, Apr 23, 2020 at 12:42 PM Eric W. Biederman
<ebiederm@xxxxxxxxxxxx> wrote:
>
> +void exchange_tids(struct task_struct *ntask, struct task_struct *otask)
> +{
> + /* pid_links[PIDTYPE_PID].next is always NULL */
> + struct pid *npid = READ_ONCE(ntask->thread_pid);
> + struct pid *opid = READ_ONCE(otask->thread_pid);
> +
> + rcu_assign_pointer(opid->tasks[PIDTYPE_PID].first, &ntask->pid_links[PIDTYPE_PID]);
> + rcu_assign_pointer(npid->tasks[PIDTYPE_PID].first, &otask->pid_links[PIDTYPE_PID]);
> + rcu_assign_pointer(ntask->thread_pid, opid);
> + rcu_assign_pointer(otask->thread_pid, npid);
> + WRITE_ONCE(ntask->pid_links[PIDTYPE_PID].pprev, &opid->tasks[PIDTYPE_PID].first);
> + WRITE_ONCE(otask->pid_links[PIDTYPE_PID].pprev, &npid->tasks[PIDTYPE_PID].first);
> + WRITE_ONCE(ntask->pid, pid_nr(opid));
> + WRITE_ONCE(otask->pid, pid_nr(npid));
> +}

This function is _very_ hard to read as written.

It really wants a helper function to do the swapping per hlist_head
and hlist_node, I think. And "opid/npid" is very hard to see, and the
naming doesn't make much sense (if it's an "exchange", then why is it
"old/new" - they're symmetric).

At least something like

struct hlist_head *old_pid_hlist = opid->tasks + PIDTYPE_PID;
struct hlist_head *new_pid_hlist = npid->tasks + PIDTYPE_PID;
struct hlist_node *old_pid_node = otask->pid_links + PIDTYPE_PID;
struct hlist_node *new_pid_node = ntask->pid_links + PIDTYPE_PID;

struct hlist_node *old_first_node = old_pid_hlist->first;
struct hlist_node *new_first_node = new_pid_hlist->first;

and then trying to group up the first/pprev/thread_pid/pid accesses
so that you them together, and using a helper function that does the
whole switch, so that you'd have

/* Move new node to old hlist, and update thread_pid/pid fields */
insert_pid_pointers(old_pid_hlist, new_pid_node, new_first_node);
rcu_assign_pointer(ntask->thread_pid, opid);
WRITE_ONCE(ntask->pid, pid_nr(opid));

/* Move old new to new hlist, and update thread_pid/pid fields */
insert_pid_pointers(new_pid_hlist, old_pid_node, old_first_node);
rcu_assign_pointer(otask->thread_pid, npid);
WRITE_ONCE(otask->pid, pid_nr(npid));

or something roughly like that.

(And the above still uses "old/new", which as mentioned sounds wrong
to me. Maybe it should just be "a_xyz" and "b_xyz"? Also note that I
did this in my MUA, so I could have gotten the names and types wrong
etc).

I think that would make it look at least _slightly_ less like random
line noise and easier to follow.

But maybe even a rcu_hlist_swap() helper? We have one for regular
lists. Do we really have to do it all written out, not do it with a
"remove and reinsert" model?

Linus