Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2688605pxb; Tue, 24 Aug 2021 05:26:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVcRxH+1TM6eJo6dH0tUV/rEwT7oGDSJ40cPGCPbEeXRH0BaTRRB4wusiQbsD+941aOuoF X-Received: by 2002:a05:6638:d43:: with SMTP id d3mr483308jak.138.1629807982607; Tue, 24 Aug 2021 05:26:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629807982; cv=none; d=google.com; s=arc-20160816; b=oWc1h8BfEi95rHZoZAkZJLnL4sJwukXdD1vUOnWtMIz0mrqAX2CVtSWDekTC+s2nYQ No7Yu3RJX47tkpbLKt1148Ol7zi1hkMqPZWsJug5g+ladeE+A/2ACdiprhjWU4k86Jmr FSJ5oUs1uyIHdfqrEyulnZKWeKReksdWilQmgY9EWE83DOfeQDZQN3LP0hnYt3lG3+1n KMfH+P8nCWqBhG6NX8/yp7kaTkDBH9awSisQAuc/CjGrMEHQLkU03USuQXdHA2rHerSN qR9tr2QWMLvT5TdXbuPVjxrtieA4Tb692XKIM4UruswPGvQplGHOI6jBjf7tqJ62n7Bf z3HQ== 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:message-id:subject:cc:to:from:date :dkim-signature; bh=AxtYts/zKFQC/k3PTK7LFXJXmWEpk6FG11lEhzUaPGw=; b=KNMGxLgYPfxjgkdZBa+uAn28Defuem0cLhTnT441OCOQLz0R5KXjExRurbqrZTSTqb szNAm8S9yuLFHzsJaXe2c8/P4l116kyT8e/yUoV9HXxBKPHZSUN02pPIXvZFH+M1aUMD Bsg6vyqO4jZDO/5NWnaIpF8U90ruU91UU2gi5+2GkRXBXHAkHtcccaJclqi8oOYo2wo5 vgGMetlvRXpl0d+PIwnNhYosOy+qUvCqZ4rkxl1b/bHAElCPqaM0NrYPtQVW158zL5KV cZmXX+4tZ+SBevTKTBr8nWEspuRMfLHO/8nWWLqyxgq7tQADx+jKo316yyfix2WeQGcC xueg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eKhRQQn9; 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 r10si17841561jap.64.2021.08.24.05.26.11; Tue, 24 Aug 2021 05:26:22 -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=@kernel.org header.s=k20201202 header.b=eKhRQQn9; 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 S237268AbhHXMZv (ORCPT + 99 others); Tue, 24 Aug 2021 08:25:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:58174 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230132AbhHXMZv (ORCPT ); Tue, 24 Aug 2021 08:25:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id B73016127B; Tue, 24 Aug 2021 12:25:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1629807907; bh=Edj68RKjcXX2r4keA5K6DEuxfBNmsQDCDQXdGfLJ09I=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=eKhRQQn91hAhG6loFGwgvUTfuk90J2t693MaoSzkx7rKuyyuInsQaMgZU5awAgMiz yFcJR6cf0jhhpE3vB4dGD9mLksEZzdr9I43oJSix1x0HOnWbmOZno+jd5ADXAhOGGw 7VBsLN+b3JDEPIAjmTsNi7pKwnFm4uEa/5d+I/GkW3Y2DHZWzSNnsAGKr2+6tje504 gdzarHSnOqsLqPhNt7yJfwf9JTDdZiL33VNsYfFYESXJXz9WctTg+j1c1a6yEUbDsV 61jwiPYWc7G6JK/UAycvuK3BgJF/MSVl2HltSH/g+u4GP57GgpVtVUtg+V/PoVe45Y CkH0VXc58XDaQ== Date: Tue, 24 Aug 2021 07:25:04 -0500 From: Bjorn Helgaas 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: <20210824122504.GA3452187@bjorn-Precision-5520> 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 "Fix a bug ..." is not a very useful subject line. It doesn't say anything about what the patch *does*. It doesn't hint at a locking change. 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. It's unfortunate that slots are not better integrated into the PCI core. I'm sorry your driver even has to worry about this. > > 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 A lore link to Dan's report would be useful here. > 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; > > /* > * 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_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 >