Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2634062pxb; Tue, 24 Aug 2021 04:04:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1ytUQEholJLdDGqxvxUzjiAEO3rKG0onrdnY0umkZ/hjjeZJTOucz7dImsmsPomnEO9fX X-Received: by 2002:a02:708f:: with SMTP id f137mr868768jac.68.1629803077183; Tue, 24 Aug 2021 04:04:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629803077; cv=none; d=google.com; s=arc-20160816; b=wHG3JFZp8aG/CGaSCBpMey52XGGg4NWx+jq1C6CXctUyUZQaTlV23blH0MiVwnqRQo Hpt36tCpgzbdcP50WqtwCvzA9/O9x7uUFoQvFaPbyO5FSOGXhXRENxlHdSihTFuEGMna /d6gSx5yfRK+BpPc6V+yCtgj0Du4fm7GqZo+eOgxJ5dlETt7j7ovQDMLNal9kOjlSmmb OCLJJnHsKOiZsBzZa0LT2e7UUVK3AbASY91ecjkbcKpfDLfFoV0+3xEmLR4C1S72c+1U rb+IUR0+nQFRxYyWOzjhCNq5SfB8lWox9xhR0BHK0sMqbchZZCuYHKs/MFZv2L450SiU 8csw== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=usZCgUnmy/C/PHJqn4fGun/TO/sNvOWBArrpSOrh8r4=; b=hJO/WEpMXm8so98XwArRgy+onr4HKm6lOKNM/owrd8EYxWIEDvzA4OecuahCJPhCKk 007dgc9kg0Z0FrBY+2DM9Ohx2fVO2NLWQec9vw/WZPsY64Fi/QZ5vuTFDvtVoi0C9vv1 BCACZkQRPuKKz2OhRkM3NSZuwlHLDnGZYRFAGvIgt3DPgFAKtmC2oIWmJ5fBdxAbd3xz gRjBW62ycybLqD0WOyHE6Cir0oWN24NhgY9klk1RqKxylL1YixWh1uf5myUtFUgZplLx R9TxJPpRaxWdw/Wb1xNosNxDG9X+XSvIRarsL2wtZfFC6AoY7NbTti3cwsRy7sc0SbyS KXvA== ARC-Authentication-Results: i=1; mx.google.com; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n3si17075522ilt.132.2021.08.24.04.04.25; Tue, 24 Aug 2021 04:04:37 -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; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236413AbhHXLC6 (ORCPT + 99 others); Tue, 24 Aug 2021 07:02:58 -0400 Received: from mail-wm1-f44.google.com ([209.85.128.44]:42937 "EHLO mail-wm1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236377AbhHXLC4 (ORCPT ); Tue, 24 Aug 2021 07:02:56 -0400 Received: by mail-wm1-f44.google.com with SMTP id k20-20020a05600c0b5400b002e87ad6956eso348296wmr.1; Tue, 24 Aug 2021 04:02:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=usZCgUnmy/C/PHJqn4fGun/TO/sNvOWBArrpSOrh8r4=; b=WteQERNJxIOAhkfrOpXlAu5/upcAvdfs6iBWklsaqBSgx6MMOrcOIXfHNWHwFXy1qF mTwoWJPccYlWLP6reEuy0SuZtQmqzERAN3V8jCBxgiFhwjkurjjNlDpQFEJ0WJkBK5Ay HAuwar4yKqletRe028hOXky00suoQtHlXhO/QAAPSAndHFKwt1n4DqpBL3dWob+2K7+U 2mC+pH0BgpBIQRVCacI0ny5+97Uj9ocUKC8bMbsGVk8NEd6Hh1JyHJQi4mB17dLI5cN+ hACGQTTCBLqnVHTuGaEtrJkcs+mBZRJMxLAjWQq9vcF3mOfQqfcBEtNlFPJl5BLWeZOv QwxA== X-Gm-Message-State: AOAM532vD7dvF2Tr8mIEz3dcz1O7VPn0OIha8TegLjx7ouALbF4zmOD9 8VM3Z52k7ay1bRgl1CQwUkU= X-Received: by 2002:a1c:f002:: with SMTP id a2mr3564259wmb.79.1629802931140; Tue, 24 Aug 2021 04:02:11 -0700 (PDT) Received: from liuwe-devbox-debian-v2 ([51.145.34.42]) by smtp.gmail.com with ESMTPSA id z19sm2416079wma.0.2021.08.24.04.02.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Aug 2021 04:02:10 -0700 (PDT) Date: Tue, 24 Aug 2021 11:02:08 +0000 From: Wei Liu To: longli@linuxonhyperv.com Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, Long Li , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Lorenzo Pieralisi , Rob Herring , Krzysztof =?utf-8?Q?Wilczy=C5=84ski?= , Bjorn Helgaas , Michael Kelley , Dan Carpenter Subject: Re: [PATCH] PCI: hv: Fix a bug on removing child devices on the bus Message-ID: <20210824110208.xd57oqm5rii4rr4n@liuwe-devbox-debian-v2> References: <1629789620-11049-1-git-send-email-longli@linuxonhyperv.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1629789620-11049-1-git-send-email-longli@linuxonhyperv.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 24, 2021 at 12:20:20AM -0700, longli@linuxonhyperv.com wrote: > From: Long Li > > In hv_pci_bus_exit, the code is holding a spinlock while calling > pci_destroy_slot(), which takes a mutex. > > This is not safe for spinlock. Fix this by moving the children to be > deleted to a list on the stack, and removing them after spinlock is > released. > > Fixes: 94d22763207a ("PCI: hv: Fix a race condition when removing the device") > > Cc: "K. Y. Srinivasan" > Cc: Haiyang Zhang > Cc: Stephen Hemminger > Cc: Wei Liu > Cc: Dexuan Cui > Cc: Lorenzo Pieralisi > Cc: Rob Herring > Cc: "Krzysztof WilczyƄski" > Cc: Bjorn Helgaas > Cc: Michael Kelley > Cc: Dan Carpenter > Reported-by: Dan Carpenter > Signed-off-by: Long Li > --- > drivers/pci/controller/pci-hyperv.c | 15 ++++++++++++--- > 1 file changed, 12 insertions(+), 3 deletions(-) > > diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c > index a53bd8728d0d..d4f3cce18957 100644 > --- a/drivers/pci/controller/pci-hyperv.c > +++ b/drivers/pci/controller/pci-hyperv.c > @@ -3220,6 +3220,7 @@ static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs) > struct hv_pci_dev *hpdev, *tmp; > unsigned long flags; > int ret; > + struct list_head removed; This can be moved to where it is needed -- the if(!keep_dev) branch -- to limit its scope. > > /* > * After the host sends the RESCIND_CHANNEL message, it doesn't > @@ -3229,9 +3230,18 @@ static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs) > return 0; > > if (!keep_devs) { > - /* Delete any children which might still exist. */ > + INIT_LIST_HEAD(&removed); > + > + /* Move all present children to the list on stack */ > spin_lock_irqsave(&hbus->device_list_lock, flags); > - list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) { > + list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry) > + list_move_tail(&hpdev->list_entry, &removed); > + spin_unlock_irqrestore(&hbus->device_list_lock, flags); > + > + /* Remove all children in the list */ > + while (!list_empty(&removed)) { > + hpdev = list_first_entry(&removed, struct hv_pci_dev, > + list_entry); list_for_each_entry_safe can also be used here, right? Wei. > list_del(&hpdev->list_entry); > if (hpdev->pci_slot) > pci_destroy_slot(hpdev->pci_slot); > @@ -3239,7 +3249,6 @@ static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs) > put_pcichild(hpdev); > put_pcichild(hpdev); > } > - spin_unlock_irqrestore(&hbus->device_list_lock, flags); > } > > ret = hv_send_resources_released(hdev); > -- > 2.25.1 >