Re: [PATCH] Fix a race in pid generation that causes pids to bereused immediately.

From: Linus Torvalds
Date: Wed Jun 09 2010 - 11:40:00 EST

On Wed, 9 Jun 2010, Ingo Molnar wrote:

> * Salman <sqazi@xxxxxxxxxx> wrote:
> >
> > A program that repeatedly forks and waits is susceptible to having the
> > same pid repeated, especially when it competes with another instance of the
> > same program. This is really bad for bash implementation. Furthermore, many shell
> > scripts assume that pid numbers will not be used for some length of time.
> >
> > Thanks to Ted Tso for the key ideas of this implementation.
> Looks rather interesting. (Cleanliness-wise i'd suggest to split out the while
> loop into a helper function.)

I have to say that usually I can look at a patch and see what it does.

This time I had absolutely no clue.

There was a whole big context missing: that original load of "last_pid"
into "last" at the top of the function, far outside the patch. And in
this case I don't think splitting out the trivial cmpxchg() loop would
help that problem - that would just make the "load original" part of the
big picture be even further away from the final "replace if same" part,
and I think _that_ is a much more critical part of the subtleties there.

So I had to read the patch _and_ go read the code it patched, in order to
at all understand what it did. I think the patch explanation should have
done it, and I also think this would need a bit comment at the top.

[ In fact, I'd argue that the _old_ code would have needed a big comment
at the top about last_pid usage, but i somebody had done that, they'd
probably also have seen the race while explaning how the code worked ;]

So having looked at the patch and the code, I agree with the patch, but
I'd like some more explanation to go with it.

[ Or Ted's version: as mentioned, I don't think the complexity is actually
in the final cmpxchg loop itself, but in the bigger picture, so I don't
think the differences between Ted's and Salman's versions are that big ]

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at