]> git.itanic.dy.fi Git - linux-stable/commit
random: add helpers for random numbers with given floor or range
authorJason A. Donenfeld <Jason@zx2c4.com>
Thu, 20 Oct 2022 05:19:35 +0000 (23:19 -0600)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 18 Nov 2022 01:15:12 +0000 (02:15 +0100)
commit7f576b2593a978451416424e75f69ad1e3ae4efe
treeaec27836d4be841b9aa495e2fb577b06d1f964fb
parente9a688bcb19348862afe30d7c85bc37c4c293471
random: add helpers for random numbers with given floor or range

Now that we have get_random_u32_below(), it's nearly trivial to make
inline helpers to compute get_random_u32_above() and
get_random_u32_inclusive(), which will help clean up open coded loops
and manual computations throughout the tree.

One snag is that in order to make get_random_u32_inclusive() operate on
closed intervals, we have to do some (unlikely) special case handling if
get_random_u32_inclusive(0, U32_MAX) is called. The least expensive way
of doing this is actually to adjust the slowpath of
get_random_u32_below() to have its undefined 0 result just return the
output of get_random_u32(). We can make this basically free by calling
get_random_u32() before the branch, so that the branch latency gets
interleaved.

Cc: stable@vger.kernel.org # to ease future backports that use this api
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
drivers/char/random.c
include/linux/random.h