From: Florian Weimer <fweimer@redhat.com>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
x86@kernel.org, Nadia Heninger <nadiah@cs.ucsd.edu>,
Thomas Ristenpart <ristenpart@cornell.edu>,
Theodore Ts'o <tytso@mit.edu>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
Subject: Re: [PATCH RFC v1] random: implement getrandom() in vDSO
Date: Fri, 29 Jul 2022 22:19:05 +0200 [thread overview]
Message-ID: <87a68r4qpy.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <20220729145525.1729066-1-Jason@zx2c4.com> (Jason A. Donenfeld's message of "Fri, 29 Jul 2022 16:55:25 +0200")
* Jason A. Donenfeld:
> diff --git a/lib/vdso/getrandom.c b/lib/vdso/getrandom.c
> new file mode 100644
> index 000000000000..3ffc900f31ff
> --- /dev/null
> +++ b/lib/vdso/getrandom.c
> +static struct getrandom_state *find_free_bucket(struct getrandom_state *buckets)
> +{
> + unsigned int start = 0, i;
> +
> + if (getcpu(&start, NULL, NULL) == 0)
> + start %= NUM_BUCKETS;
getcpu is not available everywhere. Userspace/libc should probably
provide a CPU number hint as an additional argument during the vDSO
call. We can load that easily enough from rseq. That's going to be
faster on x86, too (the LSL instruction is quite slow). The only
advantage of using getcpu like this is that it's compatible with a libc
that isn't rseq-enabled.
> + for (i = start;;) {
> + struct getrandom_state *state = &buckets[i];
> +
> + if (cmpxchg(&state->in_use, false, true) == false)
> + return state;
> +
> + i = i == NUM_BUCKETS - 1 ? 0 : i + 1;
> + if (i == start)
> + break;
> + }
Surely this scales very badly once the number of buckets is smaller than
the system processor count?
> +static ssize_t __always_inline
> +__cvdso_getrandom(void **state, void *buffer, size_t len, unsigned long flags)
> +more_batch:
> + batch_len = min_t(size_t, sizeof(s->batch) - s->pos, len);
> + if (batch_len) {
> + memcpy_and_zero(buffer, s->batch, batch_len);
> + s->pos += batch_len;
> + buffer += batch_len;
> + len -= batch_len;
> + if (!len) {
> + WRITE_ONCE(s->in_use, false);
> + return ret;
> + }
> + }
I expect the main performance benefit comes from not doing any ChaCha20
work except on batch boundaries, not the vDSO acceleration as such.
Maybe that's something that should be tried first within the system call
implementation. (Even for getrandom with a small buffer, it's not the
system call overhead that dominates, but something related to ChaCha20.)
Thanks,
Florian
next prev parent reply other threads:[~2022-07-29 20:19 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-29 14:55 [PATCH RFC v1] random: implement getrandom() in vDSO Jason A. Donenfeld
2022-07-29 20:19 ` Florian Weimer [this message]
2022-07-29 22:06 ` Jason A. Donenfeld
2022-07-30 15:48 ` Linus Torvalds
2022-07-30 23:45 ` Jason A. Donenfeld
2022-07-31 0:23 ` Jason A. Donenfeld
2022-07-31 1:31 ` [PATCH RFC v2] " Jason A. Donenfeld
2022-08-01 8:48 ` Florian Weimer
2022-08-01 12:49 ` Jason A. Donenfeld
2022-08-01 13:29 ` Jason A. Donenfeld
2022-08-01 13:00 ` Jason A. Donenfeld
2022-08-01 20:48 ` Thomas Gleixner
2022-08-01 23:41 ` Jason A. Donenfeld
2022-08-02 0:12 ` Jason A. Donenfeld
2022-08-01 19:30 ` [PATCH RFC v1] " Thomas Gleixner
2022-08-01 23:16 ` Jason A. Donenfeld
2022-08-02 13:46 ` Thomas Gleixner
2022-08-02 13:59 ` Jason A. Donenfeld
2022-08-02 15:14 ` Thomas Gleixner
2022-08-02 15:26 ` Jason A. Donenfeld
2022-08-02 22:27 ` Thomas Gleixner
2022-08-04 15:23 ` Jason A. Donenfeld
2022-08-04 16:08 ` Jeffrey Walton
2022-08-04 23:11 ` Thomas Gleixner
2022-08-17 8:20 ` Peter Zijlstra
2022-08-05 8:36 ` Florian Weimer
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=87a68r4qpy.fsf@oldenburg.str.redhat.com \
--to=fweimer@redhat.com \
--cc=Jason@zx2c4.com \
--cc=adhemerval.zanella@linaro.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nadiah@cs.ucsd.edu \
--cc=ristenpart@cornell.edu \
--cc=tytso@mit.edu \
--cc=vincenzo.frascino@arm.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).