From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
Thomas Gleixner <tglx@linutronix.de>,
Theodore Ts'o <tytso@mit.edu>,
Dominik Brodowski <linux@dominikbrodowski.net>,
Sultan Alsawaf <sultan@kerneltoast.com>
Subject: Re: [PATCH] random: ensure mix_interrupt_randomness() is consistent
Date: Fri, 11 Feb 2022 09:16:05 +0100 [thread overview]
Message-ID: <YgYbRa+5cC0ivWrK@linutronix.de> (raw)
In-Reply-To: <20220211011446.392673-1-Jason@zx2c4.com>
On 2022-02-11 02:14:46 [+0100], Jason A. Donenfeld wrote:
> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Theodore Ts'o <tytso@mit.edu>
> Cc: Dominik Brodowski <linux@dominikbrodowski.net>
> Cc: Sultan Alsawaf <sultan@kerneltoast.com>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
> drivers/char/random.c | 42 ++++++++++++++++++++++++++++++++++--------
> 1 file changed, 34 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/char/random.c b/drivers/char/random.c
> index 9c779f1bda34..caaf3c33bb38 100644
> --- a/drivers/char/random.c
> +++ b/drivers/char/random.c
> @@ -1210,14 +1211,39 @@ static u32 get_reg(struct fast_pool *f, struct pt_regs *regs)
> static void mix_interrupt_randomness(struct work_struct *work)
> {
> struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix);
> - u32 pool[ARRAY_SIZE(fast_pool->pool32)];
> + unsigned long pool[ARRAY_SIZE(fast_pool->pool_long)];
> + unsigned int count_snapshot;
> + size_t i;
>
> - /* Copy the pool to the stack so that the mixer always has a consistent view. */
> - memcpy(pool, fast_pool->pool32, sizeof(pool));
> + /* Check to see if we're running on the wrong CPU due to hotplug. */
> + migrate_disable();
> + if (fast_pool != this_cpu_ptr(&irq_randomness)) {
I am not sure that acquire and release semantic is needed and if so a
comment would probably be helpful to explain why.
But I'm trying to avoid the migrate_disable(), so:
To close the racy with losing the workqueue bit, wouldn't it be
sufficient to set it to zero via atomic_cmpxchg()? Also if the counter
before the memcpy() and after (at cmpxchg time) didn't change then the
pool wasn't modified. So basically
do {
counter = atomic_read(&fast_pool->count); // no need to cast
memcpy(pool, fast_pool->pool_long, ARRAY_SIZE(pool));
} while (atomic_cmpxchg(&fast_pool->count, counter, 0) != counter);
then it also shouldn't matter if we are _accidentally_ on the wrong CPU.
> + migrate_enable();
> + /*
> + * If we are unlucky enough to have been moved to another CPU,
> + * then we set our count to zero atomically so that when the
> + * CPU comes back online, it can enqueue work again.
> + */
> + atomic_set_release(&fast_pool->count, 0);
> + return;
> + }
> +
> + /*
> + * Copy the pool to the stack so that the mixer always has a
> + * consistent view. It's extremely unlikely but possible that
> + * this 2 or 4 word read is interrupted by an irq, but in case
> + * it is, we double check that count stays the same.
> + */
> + do {
> + count_snapshot = (unsigned int)atomic_read(&fast_pool->count);
> + for (i = 0; i < ARRAY_SIZE(pool); ++i)
> + pool[i] = READ_ONCE(fast_pool->pool_long[i]);
Why do you avoid memcpy()? Since it is a small memcpy, I'm sure the
compile will inline the register moves.
Sebastian
next prev parent reply other threads:[~2022-02-11 8:16 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-09 12:56 [PATCH v4 0/2] random: PREEMPT_RT fixes Jason A. Donenfeld
2022-02-09 12:56 ` [PATCH v4 1/2] random: remove batched entropy locking Jason A. Donenfeld
2022-02-10 16:24 ` Sebastian Andrzej Siewior
2022-02-21 2:30 ` Eric Biggers
2022-02-09 12:56 ` [PATCH v4 2/2] random: defer fast pool mixing to worker Jason A. Donenfeld
2022-02-10 18:04 ` Sebastian Andrzej Siewior
2022-02-11 0:42 ` Jason A. Donenfeld
2022-02-11 1:14 ` [PATCH] random: ensure mix_interrupt_randomness() is consistent Jason A. Donenfeld
2022-02-11 8:16 ` Sebastian Andrzej Siewior [this message]
2022-02-11 10:48 ` Jason A. Donenfeld
2022-02-11 14:51 ` Sebastian Andrzej Siewior
2022-02-11 16:19 ` Jason A. Donenfeld
2022-02-11 16:24 ` Sebastian Andrzej Siewior
2022-02-11 13:04 ` Jason A. Donenfeld
2022-02-11 7:09 ` [PATCH v4 2/2] random: defer fast pool mixing to worker Sebastian Andrzej Siewior
2022-02-11 8:25 ` Dominik Brodowski
2022-02-11 14:18 ` Sebastian Andrzej Siewior
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=YgYbRa+5cC0ivWrK@linutronix.de \
--to=bigeasy@linutronix.de \
--cc=Jason@zx2c4.com \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@dominikbrodowski.net \
--cc=sultan@kerneltoast.com \
--cc=tglx@linutronix.de \
--cc=tytso@mit.edu \
/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).