From: Fabiano Rosas <farosas@linux.ibm.com>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>,
linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>, stable@vger.kernel.org
Subject: Re: [PATCH] powerpc/kvm: don't crash on missing rng, and use darn
Date: Fri, 24 Jun 2022 10:43:15 -0300 [thread overview]
Message-ID: <87a6a2qirw.fsf@linux.ibm.com> (raw)
In-Reply-To: <20220622105435.203922-1-Jason@zx2c4.com>
"Jason A. Donenfeld" <Jason@zx2c4.com> writes:
> On POWER8 systems that don't have ibm,power-rng available, a guest that
> ignores the KVM_CAP_PPC_HWRNG flag and calls H_RANDOM anyway will
> dereference a NULL pointer. And on machines with darn instead of
> ibm,power-rng, H_RANDOM won't work at all.
>
> This patch kills two birds with one stone, by routing H_RANDOM calls to
> ppc_md.get_random_seed, and doing the real mode check inside of it.
>
> Cc: stable@vger.kernel.org # v4.1+
> Cc: Michael Ellerman <mpe@ellerman.id.au>
> Fixes: e928e9cb3601 ("KVM: PPC: Book3S HV: Add fast real-mode H_RANDOM implementation.")
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
>
> This patch must be applied ontop of:
> 1) https://github.com/linuxppc/linux/commit/f3eac426657d985b97c92fa5f7ae1d43f04721f3
> 2) https://lore.kernel.org/all/20220622102532.173393-1-Jason@zx2c4.com/
>
>
> arch/powerpc/include/asm/archrandom.h | 5 ----
> arch/powerpc/kvm/book3s_hv_builtin.c | 5 ++--
> arch/powerpc/platforms/powernv/rng.c | 33 +++++++--------------------
> 3 files changed, 11 insertions(+), 32 deletions(-)
>
> diff --git a/arch/powerpc/include/asm/archrandom.h b/arch/powerpc/include/asm/archrandom.h
> index 11d4815841ab..3af27bb84a3d 100644
> --- a/arch/powerpc/include/asm/archrandom.h
> +++ b/arch/powerpc/include/asm/archrandom.h
> @@ -38,12 +38,7 @@ static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
> #endif /* CONFIG_ARCH_RANDOM */
>
> #ifdef CONFIG_PPC_POWERNV
> -int pnv_hwrng_present(void);
> int pnv_get_random_long(unsigned long *v);
> -int pnv_get_random_real_mode(unsigned long *v);
> -#else
> -static inline int pnv_hwrng_present(void) { return 0; }
> -static inline int pnv_get_random_real_mode(unsigned long *v) { return 0; }
> #endif
>
> #endif /* _ASM_POWERPC_ARCHRANDOM_H */
> diff --git a/arch/powerpc/kvm/book3s_hv_builtin.c b/arch/powerpc/kvm/book3s_hv_builtin.c
> index 799d40c2ab4f..1c6672826db5 100644
> --- a/arch/powerpc/kvm/book3s_hv_builtin.c
> +++ b/arch/powerpc/kvm/book3s_hv_builtin.c
> @@ -176,13 +176,14 @@ EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode);
>
> int kvmppc_hwrng_present(void)
> {
> - return pnv_hwrng_present();
> + return ppc_md.get_random_seed != NULL;
> }
> EXPORT_SYMBOL_GPL(kvmppc_hwrng_present);
>
> long kvmppc_rm_h_random(struct kvm_vcpu *vcpu)
> {
> - if (pnv_get_random_real_mode(&vcpu->arch.regs.gpr[4]))
> + if (ppc_md.get_random_seed &&
> + ppc_md.get_random_seed(&vcpu->arch.regs.gpr[4]))
> return H_SUCCESS;
This is the same as arch_get_random_seed_long, perhaps you could use it
instead.
Otherwise, the archrandom.h include is not needed anymore and could be
replaced with #include <asm/machdep.h> for ppc_md.
>
> return H_HARDWARE;
> diff --git a/arch/powerpc/platforms/powernv/rng.c b/arch/powerpc/platforms/powernv/rng.c
> index 868bb9777425..c748567cd47e 100644
> --- a/arch/powerpc/platforms/powernv/rng.c
> +++ b/arch/powerpc/platforms/powernv/rng.c
> @@ -29,15 +29,6 @@ struct pnv_rng {
>
> static DEFINE_PER_CPU(struct pnv_rng *, pnv_rng);
>
> -int pnv_hwrng_present(void)
> -{
> - struct pnv_rng *rng;
> -
> - rng = get_cpu_var(pnv_rng);
> - put_cpu_var(rng);
> - return rng != NULL;
> -}
> -
> static unsigned long rng_whiten(struct pnv_rng *rng, unsigned long val)
> {
> unsigned long parity;
> @@ -58,17 +49,6 @@ static unsigned long rng_whiten(struct pnv_rng *rng, unsigned long val)
> return val;
> }
>
> -int pnv_get_random_real_mode(unsigned long *v)
> -{
> - struct pnv_rng *rng;
> -
> - rng = raw_cpu_read(pnv_rng);
> -
> - *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
> -
> - return 1;
> -}
> -
> static int pnv_get_random_darn(unsigned long *v)
> {
> unsigned long val;
> @@ -105,11 +85,14 @@ int pnv_get_random_long(unsigned long *v)
> {
> struct pnv_rng *rng;
>
> - rng = get_cpu_var(pnv_rng);
> -
> - *v = rng_whiten(rng, in_be64(rng->regs));
> -
> - put_cpu_var(rng);
> + if (mfmsr() & MSR_DR) {
> + rng = raw_cpu_read(pnv_rng);
> + *v = rng_whiten(rng, __raw_rm_readq(rng->regs_real));
> + } else {
> + rng = get_cpu_var(pnv_rng);
> + *v = rng_whiten(rng, in_be64(rng->regs));
> + put_cpu_var(rng);
> + }
>
> return 1;
> }
next prev parent reply other threads:[~2022-06-24 13:44 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-22 10:54 [PATCH] powerpc/kvm: don't crash on missing rng, and use darn Jason A. Donenfeld
2022-06-22 10:54 ` Jason A. Donenfeld
2022-06-24 13:43 ` Fabiano Rosas [this message]
2022-06-24 14:14 ` Jason A. Donenfeld
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=87a6a2qirw.fsf@linux.ibm.com \
--to=farosas@linux.ibm.com \
--cc=Jason@zx2c4.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=stable@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.