From patchwork Fri Jul 26 23:51:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13743510 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ED1C8C3DA7F for ; Sat, 27 Jul 2024 00:14:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To: From:Subject:Message-ID:References:Mime-Version:In-Reply-To:Date: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=oqsp5dWmLf1ceGtEyEpwn2+TSiYr9kgkgYyCIEXijOU=; b=sF4srqXOQYSOb963FZNMxA1j6w jG0CYeWSNTIJgBzfOH2/83WBAOkHXWPkzmAhs4ujfTtVbj0++lyUBj9ldrmqB1mbNBztXKdfMQQMi deXqXi9VYokh13Dp3lfH+XogjvrXGyNNVKVRIaURPVI9Yt7tP90HCRRDY4KICvjPdQxMESGZkFZMO TmXTr9gxCiLDqGAh/R29z9/+/aPqm+epdlvtSjLjqGuoLBZL/i0AlY4hewYKlmriRalc6yHMPuZnT wvnXkj5W7eYpQ3mMK5N+Ub+liesY1Kni1a9AkLfbsgndRkXbM8PMPSGEbcBaOW3wxnvgzGq9ytcSK 4SsuZzpw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXV4y-00000005b7x-3N8j; Sat, 27 Jul 2024 00:14:32 +0000 Received: from mail-pg1-x54a.google.com ([2607:f8b0:4864:20::54a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sXUkP-00000005PcN-1TRu for linux-arm-kernel@lists.infradead.org; Fri, 26 Jul 2024 23:53:19 +0000 Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-7a28f78c67aso1346206a12.1 for ; Fri, 26 Jul 2024 16:53:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722037996; x=1722642796; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=oqsp5dWmLf1ceGtEyEpwn2+TSiYr9kgkgYyCIEXijOU=; b=WU1l/bGUgOlXergvUUEZAmStGB6e2lkbKY6g92ISVXZNY42sPSvTXwyx4umZVR8Y5v cdUylVL646oU9Igaur6rLaAq+m+GjE3yC0Yndyoif6kELo/pbSHKQBBfE9fMRmNeyzpN 2KZqn9K9lJrLjKDT8UKj9hEHZwih+bPlJAaF0VM+5J+p9R+s9kMswvBsVKQY+oni2/Eh fV0P91Corujow+DoDd7B82IT0/FlkYHfcUHmGji4+X//an9dQmlz5VM32+N7NrVOYMYB TVyZqNP50eEFVj7FNpwUg65FLJWOnTk2U4rKlABQmnwRVmSCebEzlM1kJbtiSGrQLfOM ONAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722037996; x=1722642796; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oqsp5dWmLf1ceGtEyEpwn2+TSiYr9kgkgYyCIEXijOU=; b=S+GrxJJQd7g3VqCjn5gIOmBd1BkH/EBw0y7oDbKF4uVti3Buk+J0/ZXY++Kk2m+JtK doN50tVyysN0IfzRUR1nNp/cuoyZqVznVnri1IbNiAo5tyoDpHw/ffTCHZHyGTYTGNFN aHNlcFinmp2WMnSI4V/9mq7HRfhZ45VifyMCsgGrCcw4G+GOBgVn+os7SMdw4kp6xPpc 5u73n1ojbJYYRTgsmV3h2KMozM1ZctN1tUxyApBa7pwXpCILHb6Km1+06ylOc+1Cx/EV uc+vkdbxRkJcSRSFyGyBI4QsgnpxaAWM1N17/P3c89WYAZZbCLPiv0GRB25iIfiZJQVZ m4Gw== X-Forwarded-Encrypted: i=1; AJvYcCXfTRNCHzZChc1dVDorR8AMtvzFvyiP80wqFin3kTtuSCzou8+VPyveVjKKpv5c/Ur2Nin3ql2inIY+5/5BPUn/6kZtxtfXcnWZwmqNptidfgUlEkk= X-Gm-Message-State: AOJu0YxqQXNyOaN3oFPAQN45EAslhofDQSg5SBj+qp16HO6LUgTvFxTp mvm4JFXpOllrvmymXDVijgC0127yHk/U0AYbqLpTrkj5on5DgYqVia5BFqUSD9d7GFjvXNQ18c6 8Qw== X-Google-Smtp-Source: AGHT+IGPnVvSSR8GC4je1DUQj4K0eIO0wDIdrfABFjI4ae5Nt/lU6oCOfX0avnzwArksU/NKeR8FWd7ZKxM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:5a9:b0:6be:8aa5:bffb with SMTP id 41be03b00d2f7-7ac8e0bb8cdmr3560a12.4.1722037995764; Fri, 26 Jul 2024 16:53:15 -0700 (PDT) Date: Fri, 26 Jul 2024 16:51:28 -0700 In-Reply-To: <20240726235234.228822-1-seanjc@google.com> Mime-Version: 1.0 References: <20240726235234.228822-1-seanjc@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240726235234.228822-20-seanjc@google.com> Subject: [PATCH v12 19/84] KVM: Explicitly initialize all fields at the start of kvm_vcpu_map() From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , David Stevens X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240726_165317_512848_F6159A0A X-CRM114-Status: GOOD ( 12.37 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Sean Christopherson Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Explicitly initialize the entire kvm_host_map structure when mapping a pfn, as some callers declare their struct on the stack, i.e. don't zero-initialize the struct, which makes the map->hva in kvm_vcpu_unmap() *very* suspect. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 21ff0f4fa02c..67a50b87bb87 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3088,32 +3088,24 @@ void kvm_release_pfn(kvm_pfn_t pfn, bool dirty) int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) { - kvm_pfn_t pfn; - void *hva = NULL; - struct page *page = KVM_UNMAPPED_PAGE; - - pfn = gfn_to_pfn(vcpu->kvm, gfn); - if (is_error_noslot_pfn(pfn)) - return -EINVAL; - - if (pfn_valid(pfn)) { - page = pfn_to_page(pfn); - hva = kmap(page); -#ifdef CONFIG_HAS_IOMEM - } else { - hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); -#endif - } - - if (!hva) - return -EFAULT; - - map->page = page; - map->hva = hva; - map->pfn = pfn; + map->page = KVM_UNMAPPED_PAGE; + map->hva = NULL; map->gfn = gfn; - return 0; + map->pfn = gfn_to_pfn(vcpu->kvm, gfn); + if (is_error_noslot_pfn(map->pfn)) + return -EINVAL; + + if (pfn_valid(map->pfn)) { + map->page = pfn_to_page(map->pfn); + map->hva = kmap(map->page); +#ifdef CONFIG_HAS_IOMEM + } else { + map->hva = memremap(pfn_to_hpa(map->pfn), PAGE_SIZE, MEMREMAP_WB); +#endif + } + + return map->hva ? 0 : -EFAULT; } EXPORT_SYMBOL_GPL(kvm_vcpu_map);