Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp4006090pxb; Mon, 30 Aug 2021 16:15:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/aCmwhiMrpBCpkaDZgMD90/k7zwxiwjiQkxYZfAXDNVfSCCWp6PbT+tXrjuALpf3dJDo3 X-Received: by 2002:a17:906:e88:: with SMTP id p8mr21457329ejf.103.1630365339134; Mon, 30 Aug 2021 16:15:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630365339; cv=none; d=google.com; s=arc-20160816; b=cUhiccfh8paZ5QXTbLm6hHT8V/3SjSRtC2MPcJQCWzYyUObhdtw0Ai3mV6gz1v2ass 9C0DLM8OmMHEglhj30VC2efxkOabD1zUVthbUD33duRsGDHIMIEkiDqdWVXeJzWHecsD 7gEEwERUWLBAqvVPKJx3eKlm4PIjhPX3UabObTV/1OXlwFbe3VUwTomm0kx1ckAFge6d m8jFg/3KZJFpS7AntD/lz6FwaeJ/lJgnjl8d1a/Q6HyhuC8EGrRjhLcsqQrU4/hO+n4S zs1ZnyzY4TCBQ9oVnAIq4LinndOI+3BusDc9IQ5GysVEuzxVkgLdj2Ffg9ZIJWTxalpd Vf/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 :message-id:date:subject:cc:to:from:dkim-signature:dkim-filter; bh=y8kbnU6wSCpgwXOq1MQQIkVnRwqj3xH6aJeaz2aubBY=; b=rvvwgJNU12acuSEIrqdlORm4uMUaytr1oLgsGQH5AEMF6o9iTiuDsJj90IUdIky6Xk w34aqzrLOIyN9Ls62Pk5BqF841reArSLLNO/cdXu6SEmdBmhsga8khc9wZ/iXdMsHUGV aiyq+eRbA7K1XNXgEWP24r1qhnhRVk5ek0Y54KW7f26gvN+pvjkC38al4WpvBVRrXVBG U77gxcYxiCcWPr1ECAzsgI/LD0wtlUEYYexaKEhBAMPPfUhgIhb422F12U7AKUcuCJeP urC6pCHtnZecB7ic7ZT1R7GYnb+jD2y/7mDsrEXMkbbLElGIQ6b5LjEWEIDGx94aa2IY 9UCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxonhyperv.com header.s=default header.b=sSkVi6OX; 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=linuxonhyperv.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a14si12808994edx.0.2021.08.30.16.15.15; Mon, 30 Aug 2021 16:15:39 -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=@linuxonhyperv.com header.s=default header.b=sSkVi6OX; 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=linuxonhyperv.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239030AbhH3XOa (ORCPT + 99 others); Mon, 30 Aug 2021 19:14:30 -0400 Received: from linux.microsoft.com ([13.77.154.182]:48426 "EHLO linux.microsoft.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238898AbhH3XO3 (ORCPT ); Mon, 30 Aug 2021 19:14:29 -0400 Received: by linux.microsoft.com (Postfix, from userid 1004) id 24BE820B90FF; Mon, 30 Aug 2021 16:13:35 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 24BE820B90FF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxonhyperv.com; s=default; t=1630365215; bh=y8kbnU6wSCpgwXOq1MQQIkVnRwqj3xH6aJeaz2aubBY=; h=From:To:Cc:Subject:Date:From; b=sSkVi6OXSgiAj4hA98ukq34ASL/5U3pAIYzzvNkQswfVkKvxpySXEA+VX3YMrLCoP T/JnEMwesI348tJ9hy53ZX/p4571zO7va6B2YqKgEIHRf7kGDWNscy0J0whfdNoVG4 yFIDkJzZ8RdZQMYlsUFSPb/JCmOYvXoh0TFGg6ME= From: longli@linuxonhyperv.com To: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org Cc: Long Li , "K. Y. Srinivasan" , Haiyang Zhang , Stephen Hemminger , Wei Liu , Dexuan Cui , Lorenzo Pieralisi , Rob Herring , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Bjorn Helgaas , Michael Kelley Subject: [Patch v2] PCI: hv: Fix sleep while in non-sleep context when removing child devices from the bus Date: Mon, 30 Aug 2021 16:13:27 -0700 Message-Id: <1630365207-20616-1-git-send-email-longli@linuxonhyperv.com> X-Mailer: git-send-email 1.8.3.1 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 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 Reported-by: Dan Carpenter Link: https://lore.kernel.org/linux-hyperv/20210823152130.GA21501@kili/ Signed-off-by: Long Li --- Changes in v2: Made patch title more specific on what the patch does. Added link to the original bug report. Changed to list_for_each_entry_safe for iterating the removed list. drivers/pci/controller/pci-hyperv.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/pci/controller/pci-hyperv.c b/drivers/pci/controller/pci-hyperv.c index a53bd8728d0d..fc1a29acadbb 100644 --- a/drivers/pci/controller/pci-hyperv.c +++ b/drivers/pci/controller/pci-hyperv.c @@ -3229,9 +3229,17 @@ 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. */ + struct list_head removed; + + /* Move all present children to the list on stack */ + INIT_LIST_HEAD(&removed); 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 */ + list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) { list_del(&hpdev->list_entry); if (hpdev->pci_slot) pci_destroy_slot(hpdev->pci_slot); @@ -3239,7 +3247,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