Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp654019rwd; Thu, 25 May 2023 01:51:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6geDsJJZ5g8Degj41v70SLSFpDNfhzZXZ3ev0P7Bx97ReHzNJpGqTAlFhWb+97NYmbcDdT X-Received: by 2002:a17:902:c402:b0:1ac:b259:87ea with SMTP id k2-20020a170902c40200b001acb25987eamr1399828plk.0.1685004666050; Thu, 25 May 2023 01:51:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685004666; cv=none; d=google.com; s=arc-20160816; b=WwMN2a0kxqqQlNNI3To4XS23qzdvJ8xPB8SQOeTbBWJBlHjFvc4/m5CiyamFVb1na7 ev13bb50H4pnyNmx1IzHwjOVZW8MpAwTu0Kvw8t9MjUv/fO/vWEgR15h3J8iRWEp0zq1 Ov3CcJMq8PLbDlnnQnh3Q8zMYKvLv7xSNzd8DDkcTTV7fGfXzWws7URO4hJBJVOitzlT 4ILvkhJNv3/wp6IrpRJIeGNN1YUh9vj/ogJop2STtRBS2kJz9fku7Ck1V8smZFg4XFy8 CWtbDixJUvUDulyEle89Ou1PV9PM8RurGEHzADE99HIjS12mR/pj1sfNvjR+PNiS3FEW QxKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=jC/Z9/a7TBT+akj3mSqt9Jm720nsVhQdGExkjYm6Cdo=; b=IxrGqUsqtqxFbv/Gca3Fxk/5GvT6YOV2xH12vnDtIyRrPMXZAhPk5p9wWOCjcYnQGD pSTjIFjHGrfIZLX0/Whaun5l2pe1JKx+SP1URTAFrfwvaW/phVicErqEWD9jXUOaEavg eOWYPIxmqqN/MCLzhZ7C9iaIRJsNYzHjc/mUzcs2aDbgUW4KRhGXcdcjWMqNLv0jXT8F aoZhn8griDnI+o776wim1ZJ48E4PIKxe5ojUq31Q1v7r/u1miRH/B9RGb6R3B267XtFD qtL+qDnsqhCJqNgA8ARLFZQdNpfqX85gG1Xr7WWjTRAokG4zWhBTKFgmr93nzXZZCLUm mb0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=J35245Wq; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d15-20020a170903230f00b00198ff2d6543si1114007plh.117.2023.05.25.01.50.51; Thu, 25 May 2023 01:51:06 -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=@kernel.org header.s=k20201202 header.b=J35245Wq; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239763AbjEYI1s (ORCPT + 99 others); Thu, 25 May 2023 04:27:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235794AbjEYI1r (ORCPT ); Thu, 25 May 2023 04:27:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A6EAC186; Thu, 25 May 2023 01:27:45 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 3805A63C3E; Thu, 25 May 2023 08:27:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BF57C433EF; Thu, 25 May 2023 08:27:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685003264; bh=TXzkAJJtr+rb8+qhlaMHIqTqmmEXt7KMrw8HfElDAJE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=J35245Wq98yYPRUnLLCiT44fAp7tz9SYlr6TWHMK2i4IvPOJAmr2x/gsAVhHmh0BW M5pefjyGo5aDKkPtKyHYK6omGZDyqWCWRhEI/G7GsEJE2IKx6+NuSI+uX3Bm1Calrc tc5MpwpSqLCg4Gp69lLvO+fxKwk2QqI+lQ5OItzhqQYasd4Pm+U0mvMPOsNJAsQs+6 ybY/dfxk49hudYozaRGRRWD8OtxgaJme91Q+zWxenywCQNcu9qNo79Q7UHAeD9h5iF 4NJejm1VsfleZXpbjxCKYfiusxtEcKiTJj67zeKAp2l5b35mW4qAsxfZjWLuGbWXs/ unNc/oob7IUBA== Date: Thu, 25 May 2023 10:27:35 +0200 From: Lorenzo Pieralisi To: Dexuan Cui Cc: bhelgaas@google.com, davem@davemloft.net, edumazet@google.com, haiyangz@microsoft.com, jakeo@microsoft.com, kuba@kernel.org, kw@linux.com, kys@microsoft.com, leon@kernel.org, linux-pci@vger.kernel.org, mikelley@microsoft.com, pabeni@redhat.com, robh@kernel.org, saeedm@nvidia.com, wei.liu@kernel.org, longli@microsoft.com, boqun.feng@gmail.com, ssengar@microsoft.com, helgaas@kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-rdma@vger.kernel.org, netdev@vger.kernel.org, josete@microsoft.com, stable@vger.kernel.org Subject: Re: [PATCH v3 5/6] PCI: hv: Add a per-bus mutex state_lock Message-ID: References: <20230420024037.5921-1-decui@microsoft.com> <20230420024037.5921-6-decui@microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230420024037.5921-6-decui@microsoft.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 Wed, Apr 19, 2023 at 07:40:36PM -0700, Dexuan Cui wrote: > In the case of fast device addition/removal, it's possible that > hv_eject_device_work() can start to run before create_root_hv_pci_bus() > starts to run; as a result, the pci_get_domain_bus_and_slot() in > hv_eject_device_work() can return a 'pdev' of NULL, and > hv_eject_device_work() can remove the 'hpdev', and immediately send a > message PCI_EJECTION_COMPLETE to the host, and the host immediately > unassigns the PCI device from the guest; meanwhile, > create_root_hv_pci_bus() and the PCI device driver can be probing the > dead PCI device and reporting timeout errors. > > Fix the issue by adding a per-bus mutex 'state_lock' and grabbing the > mutex before powering on the PCI bus in hv_pci_enter_d0(): when > hv_eject_device_work() starts to run, it's able to find the 'pdev' and call > pci_stop_and_remove_bus_device(pdev): if the PCI device driver has > loaded, the PCI device driver's probe() function is already called in > create_root_hv_pci_bus() -> pci_bus_add_devices(), and now > hv_eject_device_work() -> pci_stop_and_remove_bus_device() is able > to call the PCI device driver's remove() function and remove the device > reliably; if the PCI device driver hasn't loaded yet, the function call > hv_eject_device_work() -> pci_stop_and_remove_bus_device() is able to > remove the PCI device reliably and the PCI device driver's probe() > function won't be called; if the PCI device driver's probe() is already > running (e.g., systemd-udev is loading the PCI device driver), it must > be holding the per-device lock, and after the probe() finishes and releases > the lock, hv_eject_device_work() -> pci_stop_and_remove_bus_device() is > able to proceed to remove the device reliably. > > Fixes: 4daace0d8ce8 ("PCI: hv: Add paravirtual PCI front-end for Microsoft Hyper-V VMs") > Signed-off-by: Dexuan Cui > Reviewed-by: Michael Kelley > Cc: stable@vger.kernel.org > --- > > v2: > Removed the "debug code". > Fixed the "goto out" in hv_pci_resume() [Michael Kelley] > Added Cc:stable > > v3: > Added Michael's Reviewed-by. > > drivers/pci/controller/pci-hyperv.c | 29 ++++++++++++++++++++++++++--- > 1 file changed, 26 insertions(+), 3 deletions(-) Acked-by: Lorenzo Pieralisi > diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c > index 48feab095a144..3ae2f99dea8c2 100644 > --- a/drivers/pci/controller/pci-hyperv.c > +++ b/drivers/pci/controller/pci-hyperv.c > @@ -489,7 +489,10 @@ struct hv_pcibus_device { > struct fwnode_handle *fwnode; > /* Protocol version negotiated with the host */ > enum pci_protocol_version_t protocol_version; > + > + struct mutex state_lock; > enum hv_pcibus_state state; > + > struct hv_device *hdev; > resource_size_t low_mmio_space; > resource_size_t high_mmio_space; > @@ -2512,6 +2515,8 @@ static void pci_devices_present_work(struct work_struct *work) > if (!dr) > return; > > + mutex_lock(&hbus->state_lock); > + > /* First, mark all existing children as reported missing. */ > spin_lock_irqsave(&hbus->device_list_lock, flags); > list_for_each_entry(hpdev, &hbus->children, list_entry) { > @@ -2593,6 +2598,8 @@ static void pci_devices_present_work(struct work_struct *work) > break; > } > > + mutex_unlock(&hbus->state_lock); > + > kfree(dr); > } > > @@ -2741,6 +2748,8 @@ static void hv_eject_device_work(struct work_struct *work) > hpdev = container_of(work, struct hv_pci_dev, wrk); > hbus = hpdev->hbus; > > + mutex_lock(&hbus->state_lock); > + > /* > * Ejection can come before or after the PCI bus has been set up, so > * attempt to find it and tear down the bus state, if it exists. This > @@ -2777,6 +2786,8 @@ static void hv_eject_device_work(struct work_struct *work) > put_pcichild(hpdev); > put_pcichild(hpdev); > /* hpdev has been freed. Do not use it any more. */ > + > + mutex_unlock(&hbus->state_lock); > } > > /** > @@ -3562,6 +3573,7 @@ static int hv_pci_probe(struct hv_device *hdev, > return -ENOMEM; > > hbus->bridge = bridge; > + mutex_init(&hbus->state_lock); > hbus->state = hv_pcibus_init; > hbus->wslot_res_allocated = -1; > > @@ -3670,9 +3682,11 @@ static int hv_pci_probe(struct hv_device *hdev, > if (ret) > goto free_irq_domain; > > + mutex_lock(&hbus->state_lock); > + > ret = hv_pci_enter_d0(hdev); > if (ret) > - goto free_irq_domain; > + goto release_state_lock; > > ret = hv_pci_allocate_bridge_windows(hbus); > if (ret) > @@ -3690,12 +3704,15 @@ static int hv_pci_probe(struct hv_device *hdev, > if (ret) > goto free_windows; > > + mutex_unlock(&hbus->state_lock); > return 0; > > free_windows: > hv_pci_free_bridge_windows(hbus); > exit_d0: > (void) hv_pci_bus_exit(hdev, true); > +release_state_lock: > + mutex_unlock(&hbus->state_lock); > free_irq_domain: > irq_domain_remove(hbus->irq_domain); > free_fwnode: > @@ -3945,20 +3962,26 @@ static int hv_pci_resume(struct hv_device *hdev) > if (ret) > goto out; > > + mutex_lock(&hbus->state_lock); > + > ret = hv_pci_enter_d0(hdev); > if (ret) > - goto out; > + goto release_state_lock; > > ret = hv_send_resources_allocated(hdev); > if (ret) > - goto out; > + goto release_state_lock; > > prepopulate_bars(hbus); > > hv_pci_restore_msi_state(hbus); > > hbus->state = hv_pcibus_installed; > + mutex_unlock(&hbus->state_lock); > return 0; > + > +release_state_lock: > + mutex_unlock(&hbus->state_lock); > out: > vmbus_close(hdev->channel); > return ret; > -- > 2.25.1 >