Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3887612pxv; Mon, 19 Jul 2021 11:09:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmV/p0z+A4+q1PTc/JATbTLmVI70KBAtCvmSY04gWl1Foyrri4QWB1x04I8sRAgrmplCD3 X-Received: by 2002:a17:906:2716:: with SMTP id z22mr27980962ejc.372.1626718175047; Mon, 19 Jul 2021 11:09:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626718175; cv=none; d=google.com; s=arc-20160816; b=zwNMj4HycaJI5JnOtZ3Fdftudx3y70z9Tga+Omjh7TNgszmHOpqpntfZegAwxEF8r9 qx7aWAyW0V5Zj67UWftxc+k87YcKkRDIgs+KkABS8LpE7abYJoyfn6M6ah4CNk4RD7hh wGCS18+4G1pLF9lQkHs6I8b79IJrG5EfyI+z7Zs/J/4yogx/abjp+mJXyuRSzSnPbn0e 1IzAo3+ttuuNmspLBaZo0cOZf3jAqMLqhnhVZs5N+4+JICoAeJYfantP+MIVZWn9Ho00 RNkGsq6TkXGzZhrCoSMSZh0nLgs2Zs2yZf2yQRN6aHgzAxGLqACkvgdKcuadzGavfal9 kS/A== 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=8tN9BxlxJ4QzWDZBoW4DiO/QSOC3QRZHfC52mL1I5Gg=; b=Yz66GGQciWjIjD1gFjo/m88UsZGKUlH8CNt+h1tDyPxJ5Op53bYqRAuyZZCTzOB1F4 rXpXwAa0Y1FZ5QRtyEFqCPjBRWWUCXdFl2IF3YU5q9aIzzsTr8Vg3K3nbbU6OSF+yGN1 0KtCosmOSCLIEU8LwmZxWX7WezLWuc8Y/k8LV0i6bh3ELy5kcDSo4r87ZYuSWDLoVT7W VFSctABndpcDX9Q+nI+pIIekOnpgP9S/vQC6FakeDNopyMOZlxi9ZBMYl2WSSLWfqY/g 90J8uUO+I0TiU/Vg8hHRD2RFB/b9RU6MiBvlORGq3zePwYgFTIPKkVOflsd0uVsVQAjS Mj4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uji8KdRO; 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 cf9si21310803edb.132.2021.07.19.11.09.07; Mon, 19 Jul 2021 11:09:35 -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=uji8KdRO; 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 S1354811AbhGSRX1 (ORCPT + 99 others); Mon, 19 Jul 2021 13:23:27 -0400 Received: from mail.kernel.org ([198.145.29.99]:45036 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348911AbhGSPog (ORCPT ); Mon, 19 Jul 2021 11:44:36 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DC1DD61445; Mon, 19 Jul 2021 16:23:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626711801; bh=1r6CmX60uloS2lVWieV9kv75mRMWxdwHYkAAr3S3LKM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uji8KdRO2H6FuyrNfDT88XFeKoX4KvpWwkafVUEZyAtVMYcXxEk3elrhleoEC+7MH /B5Zftrh2JX5U0ZmOD016UpQxaRvWf8Ph7d2QUCLTwa/vlR99h9wAaFmhGK0sR4kbg Yejx8fku4chNPCvl6hcgxB6uHY/sLglXSTrKeNxA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Long Li , Lorenzo Pieralisi , Michael Kelley , Sasha Levin Subject: [PATCH 5.12 137/292] PCI: hv: Fix a race condition when removing the device Date: Mon, 19 Jul 2021 16:53:19 +0200 Message-Id: <20210719144946.990615405@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210719144942.514164272@linuxfoundation.org> References: <20210719144942.514164272@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: Long Li [ Upstream commit 94d22763207ac6633612b8d8e0ca4fba0f7aa139 ] On removing the device, any work item (hv_pci_devices_present() or hv_pci_eject_device()) scheduled on workqueue hbus->wq may still be running and race with hv_pci_remove(). This can happen because the host may send PCI_EJECT or PCI_BUS_RELATIONS(2) and decide to rescind the channel immediately after that. Fix this by flushing/destroying the workqueue of hbus before doing hbus remove. Link: https://lore.kernel.org/r/1620806800-30983-1-git-send-email-longli@linuxonhyperv.com Signed-off-by: Long Li Signed-off-by: Lorenzo Pieralisi Reviewed-by: Michael Kelley Signed-off-by: Sasha Levin --- drivers/pci/controller/pci-hyperv.c | 30 ++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c index 7479edf3676c..fd30f70c7560 100644 --- a/drivers/pci/controller/pci-hyperv.c +++ b/drivers/pci/controller/pci-hyperv.c @@ -444,7 +444,6 @@ enum hv_pcibus_state { hv_pcibus_probed, hv_pcibus_installed, hv_pcibus_removing, - hv_pcibus_removed, hv_pcibus_maximum }; @@ -3247,8 +3246,9 @@ static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs) struct pci_packet teardown_packet; u8 buffer[sizeof(struct pci_message)]; } pkt; - struct hv_dr_state *dr; struct hv_pci_compl comp_pkt; + struct hv_pci_dev *hpdev, *tmp; + unsigned long flags; int ret; /* @@ -3260,9 +3260,16 @@ static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs) if (!keep_devs) { /* Delete any children which might still exist. */ - dr = kzalloc(sizeof(*dr), GFP_KERNEL); - if (dr && hv_pci_start_relations_work(hbus, dr)) - kfree(dr); + spin_lock_irqsave(&hbus->device_list_lock, flags); + list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) { + list_del(&hpdev->list_entry); + if (hpdev->pci_slot) + pci_destroy_slot(hpdev->pci_slot); + /* For the two refs got in new_pcichild_device() */ + put_pcichild(hpdev); + put_pcichild(hpdev); + } + spin_unlock_irqrestore(&hbus->device_list_lock, flags); } ret = hv_send_resources_released(hdev); @@ -3305,13 +3312,23 @@ static int hv_pci_remove(struct hv_device *hdev) hbus = hv_get_drvdata(hdev); if (hbus->state == hv_pcibus_installed) { + tasklet_disable(&hdev->channel->callback_event); + hbus->state = hv_pcibus_removing; + tasklet_enable(&hdev->channel->callback_event); + destroy_workqueue(hbus->wq); + hbus->wq = NULL; + /* + * At this point, no work is running or can be scheduled + * on hbus-wq. We can't race with hv_pci_devices_present() + * or hv_pci_eject_device(), it's safe to proceed. + */ + /* Remove the bus from PCI's point of view. */ pci_lock_rescan_remove(); pci_stop_root_bus(hbus->pci_bus); hv_pci_remove_slots(hbus); pci_remove_root_bus(hbus->pci_bus); pci_unlock_rescan_remove(); - hbus->state = hv_pcibus_removed; } ret = hv_pci_bus_exit(hdev, false); @@ -3326,7 +3343,6 @@ static int hv_pci_remove(struct hv_device *hdev) irq_domain_free_fwnode(hbus->sysdata.fwnode); put_hvpcibus(hbus); wait_for_completion(&hbus->remove_event); - destroy_workqueue(hbus->wq); hv_put_dom_num(hbus->sysdata.domain); -- 2.30.2