linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: x86@kernel.org, Jann Horn <jannh@google.com>,
	Natalie Silvanovich <natashenka@google.com>,
	Seth Jenkins <sethjenkins@google.com>,
	Kees Cook <keescook@chromium.org>,
	linux-kernel@vger.kernel.org
Subject: Re: x86/mm: Randomize per-cpu entry area
Date: Fri, 7 Oct 2022 17:14:55 +0200	[thread overview]
Message-ID: <Y0BCb7mkLv75BqYK@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <Y0A7ZAjtDcq2PJaX@zx2c4.com>

On Fri, Oct 07, 2022 at 08:44:52AM -0600, Jason A. Donenfeld wrote:
> 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

Yeah, that sounds about right, just over 21 bits.

> 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.

I figured it was better than '% max_cea'.

> 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.

First iteration had a LFSR to 'uniformly' and uniquely distribute the
CEAs around, but someone took offence to that :-). Anyway, I think
anything non-obvious is good enough in this case.

      reply	other threads:[~2022-10-07 15:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-07  8:42 x86/mm: Randomize per-cpu entry area Peter Zijlstra
2022-10-07 14:44 ` Jason A. Donenfeld
2022-10-07 15:14   ` Peter Zijlstra [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y0BCb7mkLv75BqYK@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=Jason@zx2c4.com \
    --cc=jannh@google.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=natashenka@google.com \
    --cc=sethjenkins@google.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).