lib/random: catch unseeded and out-of-range Park-Miller state
52fb4630 ("CodeSamples/lib: Avoid degenerate seeds in Park-Miller RNG") made an
unseeded thread harmless by defaulting randseed to 1 rather than 0, so that
myrandom() could not sit in the absorbing state. That fixes the pathology, but
it turns a thread that forgot to seed into a thread that silently draws the
same sequence as every other thread that forgot to seed. That is a quiet
failure, and an easy one to introduce: randseed is __thread, so seeding main()
does not seed the workers, and every harness that spawns threads has to
remember to seed each one.
It is also a failure with teeth. Earlier in this series, seeding
existence_3skiplist_uperf's updaters is worth 1.24x at 8 updaters and 1.42x at
32, because correlated tower heights make the updaters reach into the upper
skiplist levels in lockstep and contend there together. The benchmark gives no
hint that anything is wrong. It just runs slower.
So make the failure loud instead. Return randseed to being zero-initialized,
and have myrandom() reject any state outside the recurrence's [1, 2^31 - 2]:
zero, which is what an unseeded thread has, and which is the absorbing state;
2^31 - 1, which is a fixed point that returns the same value forever; and
anything else a stray write might leave behind. One range check covers them
all.
This is why the preceding patches seed the threads in
existence_3skiplist_uperf, skiplisttorture, routetorture, singleton and
logtorture: with this patch, an unseeded draw aborts, so every thread that
draws must seed first. I built every program under CodeSamples that uses this
generator and ran each across its modes; with those patches in place, none of
the 150 program/mode combinations trips this check. (logtorture only trips
under --stresstest, not --smoketest, which is what makes this class of bug
worth catching mechanically.)
The check costs nothing measurable. Rotations in 1 s in
existence_3skiplist_uperf, best of 5, unchecked vs checked: 1454948 vs 1457950
at 8 updaters, 2656892 vs 2683963 at 32.
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 file changed