Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp4691019iog; Wed, 22 Jun 2022 04:04:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uKWfLidW6fM2TsF5OrfhwWzpeP5LwYWc2aq19EhfUO1Gro21ilOaGxuejcTHVf3LFWVNRh X-Received: by 2002:a17:903:245:b0:168:efbc:c10 with SMTP id j5-20020a170903024500b00168efbc0c10mr33422737plh.71.1655895881437; Wed, 22 Jun 2022 04:04:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655895881; cv=none; d=google.com; s=arc-20160816; b=OCQ8ieLpB4+BzXBZAN1o+RLIHN5go+aiXv768iOpiZCB2hhNaqxJEoNBSxV3JHO82Y UnADQg6zCOGclzlKEhi3pEIfoSuYjR82Ax8OJ32eX0LVHjSghawxa77LuyBVeLxmhP2L 108NLwnuEYB0SL8vI3FWfBwYS/kmOR6gbxRb0Voysw5KfNtDsin2214BRpYZTytX8Vv/ tRtOruPYdS1ucfoq0QK60td5n1x2DgxQvCXaItuBjFrXrgV/G/UONzlFwkGOUIHiKDra OuslWBPC3sptKar1jO4LFBTP5u9TEgrs/G6RglhZvfI7P7iTLeeHKLPsNGFpVVBciAzV 8vEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=3mqUcadCW6UR/1/YwZ43oaYtk6UULq2iLUpsNrIk+9o=; b=M9ie5cmfUQmVyY+zaNpyCeJst0GsU0Fz6Jn1kYsbaCxxJ9nYTeIJhE14pKPzKhuKPt Uj/LZddue9pYxvZq0q9O5VLcyycfHcMGxRioDxNNquvjeZNWkk81z6nN45fqii9bEnLg US1ppR7ovps4VttODIgM/Y4AXum0j2oxm4Mc416yAf+TJo0hXnhGdfDXdesFhN0OcG46 n/vdz8GST7W1Taap0qB8TYVMQ/4hSd7fNKmrr5kAa4rBqdleVmKHZGNq2mb8pw3oUJPD 7aT5lfDMA6LG26vcKy+RHOvIG5UFjUbF2SpPD7HBdHZ86xrzalYHfmF2Kdh4zwwe1+JP IcPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=omymolps; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s10-20020a056a00194a00b005251e17bcb2si11461914pfk.254.2022.06.22.04.04.24; Wed, 22 Jun 2022 04:04:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@zx2c4.com header.s=20210105 header.b=omymolps; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zx2c4.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235871AbiFVKyt (ORCPT + 99 others); Wed, 22 Jun 2022 06:54:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355885AbiFVKyq (ORCPT ); Wed, 22 Jun 2022 06:54:46 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A21233BBE9; Wed, 22 Jun 2022 03:54:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 44D21B81D54; Wed, 22 Jun 2022 10:54:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41430C34114; Wed, 22 Jun 2022 10:54:41 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="omymolps" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1655895279; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=3mqUcadCW6UR/1/YwZ43oaYtk6UULq2iLUpsNrIk+9o=; b=omymolps9xNQlf2LXOo+XfnFHXJ6m9eFEKaXy7ep8RPyg1XN00c2EFoGxnR++wLBdtm2j1 0l2CJUc2nEzjGCPTkCiocjYfyiA4S3VtI4TbQrblIyx1zQ584XfOLm89YkpPRf63k0TPI5 BkELDKnc7vg5O0azcmhSoqa6FUKsYRc= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id 4bcfdeaf (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO); Wed, 22 Jun 2022 10:54:39 +0000 (UTC) From: "Jason A. Donenfeld" To: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , stable@vger.kernel.org, Michael Ellerman Subject: [PATCH] powerpc/kvm: don't crash on missing rng, and use darn Date: Wed, 22 Jun 2022 12:54:35 +0200 Message-Id: <20220622105435.203922-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Fixes: e928e9cb3601 ("KVM: PPC: Book3S HV: Add fast real-mode H_RANDOM implementation.") Signed-off-by: Jason A. Donenfeld --- 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; 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; } -- 2.35.1