From patchwork Thu Oct 10 18:23:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13830864 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 CC61ECFC5F4 for ; Thu, 10 Oct 2024 18:50: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-Transfer-Encoding:Content-Type:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IMgO/JDFhWAWu7Wkr7+zBWYfApHhI977J8rKw7ML51Y=; b=zGNSb8RJBtPp6Y z9qrDP4v3bMI3BDXDw++e2DgbQ/9XJnjzQhjCfFnkfUwqR8RkHp7NOeIRegEMMcMKY9ZjSJOgYYzZ E8Ely61Ai2FobIt+9t640HBJRbtN51e/fTDAkv2HeUR2aXvHCsLsBGJdaULmN62VwHrW9buIyH3Y/ HLaUSOwVM9ME1/dYJkr0+WOvMN0CS9s40jI/jC/3f53ApQ0cFDjdxZiZ/xTVv5bYSYaxVQGXtubUc mgQijYvcc3GdbVJd4JvBgkbU1KCF3oXmnSh1XfMFtWOi7urWrgnx1mGw9FBbZf49AkY7MFmUo3aUd xrCXN7cTwePT+90kiz9A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1syyF8-0000000DzwI-32gB; Thu, 10 Oct 2024 18:50:34 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1syxr0-0000000DqBP-1tf7 for linux-arm-kernel@lists.infradead.org; Thu, 10 Oct 2024 18:25:42 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-e290b8b69f8so2137090276.2 for ; Thu, 10 Oct 2024 11:25:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728584737; x=1729189537; darn=lists.infradead.org; h=content-transfer-encoding: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=IMgO/JDFhWAWu7Wkr7+zBWYfApHhI977J8rKw7ML51Y=; b=hgOyYvQjo5Iks6jmfsYRhhZ653J3E4BLCYk0mKm4K/P7+ujw/kMs5SrTQfCdhu/xHN Db+HLZdiVwPcx0XNx4loznLbHChfVsgKxjMvnZidzMISqKM77GaWqQSFWintuPV3lTQX 5jNj8pMXtm3F6/AV4ag8agFO4lZ7LmFFe8bcXThkRc/3+NJnsaMCjPEnLChuYTVFTwIB CFbpbYCm8WEOCdmDdlAYfY1rbx6mzZGqqXQreWnDdlkTfbmZbXswSTw+o5ATOZGDgKiM JIQWUJLrwQQWH02X0iNjAYXuGRrfA3GmkUw6L3I4X7spPpuDh3EtI/himbevDZ/o1cjm qkzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728584737; x=1729189537; h=content-transfer-encoding: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=IMgO/JDFhWAWu7Wkr7+zBWYfApHhI977J8rKw7ML51Y=; b=NdP94P1NFPhlSkCALwER2Z55bSJlpC4tzofpgpouu3Xn8uGQCXi3yNWzy2zXvu+n+L yATcNuTijWl3drYGBSY2/MZljYdoC1xYo0sO+9MjlsiNNcAtF7iXy/9VofhkcCO4TFH3 BzP18JiL6OJmQR/l5Q25Tr82UQa5QT+Oaqm1+6VueHvbHqbhoOxEbUlxXBsx1izKYKJ5 8CdpPzDvETgAtYfsfpEakaeA8wQc0zNPd68kKQ++HuitfUuYvaS1vj/q9ifuTh7CrfRX zlmjCukBqFaa/dDZQxpuAskU8lehrx1pa3yESodR8FYQJCkp6hh/SEG78t1VoJWIvEvU jiIA== X-Forwarded-Encrypted: i=1; AJvYcCV/dRhihC9Y7VvOfq6hsoOsiTMhr3PW14us8DT4KwPrZ7czFUSdKr5e0fTd8TRh/SFDHmvcvjHNx8GwGjQUOqd7@lists.infradead.org X-Gm-Message-State: AOJu0YyFoOhzj+fpNudORdXoJ3MH7HjpUZFj3nLYyyL6GFVKkXEu04Fk VWCFk2dOBZvsDuVEEVOSFcjA3dabk9er5NN6uSzS33x9EnSdT2nxplWYF9iUc9IXSw6lZkAYVw0 qSA== X-Google-Smtp-Source: AGHT+IGlSN4X6DbrP5DPPe6+MPgoNlxMoOT3GZzpJV2jr+xnXsAJlzTjTlsnBYkwN+CsyuU9UJbLGW89hxk= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:9d:3983:ac13:c240]) (user=seanjc job=sendgmr) by 2002:a25:c70a:0:b0:e28:eae7:f84a with SMTP id 3f1490d57ef6-e28fe0feaecmr5507276.0.1728584736880; Thu, 10 Oct 2024 11:25:36 -0700 (PDT) Date: Thu, 10 Oct 2024 11:23:23 -0700 In-Reply-To: <20241010182427.1434605-1-seanjc@google.com> Mime-Version: 1.0 References: <20241010182427.1434605-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc1.288.g06298d1525-goog Message-ID: <20241010182427.1434605-22-seanjc@google.com> Subject: [PATCH v13 21/85] 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, " =?utf-8?q?Alex_Benn=C3=A9e?= " , Yan Zhao , David Matlack , David Stevens , Andrew Jones X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241010_112538_686639_6CA201A4 X-CRM114-Status: GOOD ( 11.59 ) 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. Tested-by: Alex Bennée 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 c7691bc40389..f1c9a781315c 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3067,32 +3067,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);