Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4585471pxj; Wed, 12 May 2021 08:42:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6T0ijO6E8NRB+PU5a6Udd/yh7vrIwF/OjiYvDHgrmQMzPthXJIZhlk5qtIajCjfNabz0g X-Received: by 2002:aa7:dc0b:: with SMTP id b11mr44930463edu.124.1620834162837; Wed, 12 May 2021 08:42:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620834162; cv=none; d=google.com; s=arc-20160816; b=pcGawBQvddO0rrT5vSMfIvCAw+lc5rbimPjFRX6XWw5w2mfdYzXP1NqrKsNdz00Awu 1WwdUXzzneTG9X2MySSNwZl0Ec4coFBO9371plfcKF1YvYr6wLS4eAnfxlVd5cNHER7Q Po3P/ryIr2aQ/EToJWuOncS+m34soPuW63Xx75xGe20Enu1YdK6cVCYLdZ23WAFVlAsH nNSi9jwPjWeRTxCupTenKFVmZy1w6LYQQL3anHVKk3c1ePRhdjCU1ecXWb2Qz4QIlnOx C52m8+HTJYUKhCONQOVcRBnnEatH36MzkIqV8GhfknOxqkqs4xYF2YNu9HJQNN4VqZyC Ku+Q== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=sl+GUfLzs9Z2G7OCf+MtXsFtwMUSIvVcmULJCO4oqeY=; b=M9lblzfgeriIlHeg9lpf0V1DTKWKiqLQx/Xp3onMjw7EgLs++tV46TM5HUkNJuhx6j 4jphw6+8Y7f+KfytHvmBrkmd40qCQ0vpSBeaEEOUEt35fFJH6hvdJd/OfVPGsaq4vROP 7j8BNETc4J3/+B+UHs9fKmTn88qI8KoQ3bAKHde9vIq72wZyuDy/To1SECcK/iKJ5nRQ zs1dPztyFynvmQoJUwmAodNPvvTMLmbgvW0yJPhMk52+WijTKy68OvvbCjP0uMzxHtbq heGCtdBjqdAzoGO6ZLXod4BtUfQl4ifXJOrWF5k03bhUx/a4BlwxrSIjoTyogaEjRAG5 v6AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WNY1gVkZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gx22si156673ejc.568.2021.05.12.08.42.18; Wed, 12 May 2021 08:42:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WNY1gVkZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236406AbhELPhw (ORCPT + 99 others); Wed, 12 May 2021 11:37:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:46998 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234409AbhELPQr (ORCPT ); Wed, 12 May 2021 11:16:47 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 19FD761953; Wed, 12 May 2021 15:06:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620832006; bh=NWBABis9kwq/xVJTBqMof2aIpaLXz0DgLt6qwp4b5tk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WNY1gVkZxUKyA4OhCuUSb5HYmnYcW7uOxLXEJuKUcXAxtuQPU/BF8jMBtR17udF23 WZKZ7zIielRzR8hXAPSGvxX2XY7zfUhLEJgcZUmJ+ogP4ilb9ttSlHx4Cb9yiCDwa5 7yrCcDYxdNs6vn32KYzFNC6Ai5JtbJUgbJ5xD3/I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hao Sun , Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.10 102/530] KVM: Stop looking for coalesced MMIO zones if the bus is destroyed Date: Wed, 12 May 2021 16:43:32 +0200 Message-Id: <20210512144823.151969569@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 5d3c4c79384af06e3c8e25b7770b6247496b4417 upstream. Abort the walk of coalesced MMIO zones if kvm_io_bus_unregister_dev() fails to allocate memory for the new instance of the bus. If it can't instantiate a new bus, unregister_dev() destroys all devices _except_ the target device. But, it doesn't tell the caller that it obliterated the bus and invoked the destructor for all devices that were on the bus. In the coalesced MMIO case, this can result in a deleted list entry dereference due to attempting to continue iterating on coalesced_zones after future entries (in the walk) have been deleted. Opportunistically add curly braces to the for-loop, which encompasses many lines but sneaks by without braces due to the guts being a single if statement. Fixes: f65886606c2d ("KVM: fix memory leak in kvm_io_bus_unregister_dev()") Cc: stable@vger.kernel.org Reported-by: Hao Sun Signed-off-by: Sean Christopherson Message-Id: <20210412222050.876100-3-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- include/linux/kvm_host.h | 4 ++-- virt/kvm/coalesced_mmio.c | 19 +++++++++++++++++-- virt/kvm/kvm_main.c | 10 +++++----- 3 files changed, 24 insertions(+), 9 deletions(-) --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -190,8 +190,8 @@ int kvm_io_bus_read(struct kvm_vcpu *vcp int len, void *val); int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, int len, struct kvm_io_device *dev); -void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, - struct kvm_io_device *dev); +int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, + struct kvm_io_device *dev); struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr); --- a/virt/kvm/coalesced_mmio.c +++ b/virt/kvm/coalesced_mmio.c @@ -174,21 +174,36 @@ int kvm_vm_ioctl_unregister_coalesced_mm struct kvm_coalesced_mmio_zone *zone) { struct kvm_coalesced_mmio_dev *dev, *tmp; + int r; if (zone->pio != 1 && zone->pio != 0) return -EINVAL; mutex_lock(&kvm->slots_lock); - list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) + list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) { if (zone->pio == dev->zone.pio && coalesced_mmio_in_range(dev, zone->addr, zone->size)) { - kvm_io_bus_unregister_dev(kvm, + r = kvm_io_bus_unregister_dev(kvm, zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev); kvm_iodevice_destructor(&dev->dev); + + /* + * On failure, unregister destroys all devices on the + * bus _except_ the target device, i.e. coalesced_zones + * has been modified. No need to restart the walk as + * there aren't any zones left. + */ + if (r) + break; } + } mutex_unlock(&kvm->slots_lock); + /* + * Ignore the result of kvm_io_bus_unregister_dev(), from userspace's + * perspective, the coalesced MMIO is most definitely unregistered. + */ return 0; } --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4342,15 +4342,15 @@ int kvm_io_bus_register_dev(struct kvm * } /* Caller must hold slots_lock. */ -void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, - struct kvm_io_device *dev) +int kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, + struct kvm_io_device *dev) { int i, j; struct kvm_io_bus *new_bus, *bus; bus = kvm_get_bus(kvm, bus_idx); if (!bus) - return; + return 0; for (i = 0; i < bus->dev_count; i++) if (bus->range[i].dev == dev) { @@ -4358,7 +4358,7 @@ void kvm_io_bus_unregister_dev(struct kv } if (i == bus->dev_count) - return; + return 0; new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1), GFP_KERNEL_ACCOUNT); @@ -4383,7 +4383,7 @@ void kvm_io_bus_unregister_dev(struct kv } kfree(bus); - return; + return new_bus ? 0 : -ENOMEM; } struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,