Re: x86/mm: Randomize per-cpu entry area

From: Jason A. Donenfeld
Date: Fri Oct 07 2022 - 10:45:04 EST


On Fri, Oct 07, 2022 at 10:42:36AM +0200, Peter Zijlstra wrote:
> + max_cea = (CPU_ENTRY_AREA_MAP_SIZE - PAGE_SIZE) / CPU_ENTRY_AREA_SIZE;
> +
> + /* O(sodding terrible) */
> + for_each_possible_cpu(i) {
> + unsigned int cea;
> +
> +again:
> + cea = prandom_u32_max(max_cea);

Just FYI, max_cea is 2274876 (at least in my test rig), which means the
values returned from prandom_u32_max() won't be uniformly distributed.
Right now the kernel doesn't have a function that does rejection
sampling to get uniform distribution, but I could add one if you need.
Alternatively, maybe you don't actually *need* this to be perfectly
distributed -- which seems to be a common perspective -- and so this is
good enough. Your call, but just wanted to make you aware.

Jason