From patchwork Mon Apr 16 16:28:57 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 152927 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id ABE00B700D for ; Tue, 17 Apr 2012 02:32:29 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752854Ab2DPQc3 (ORCPT ); Mon, 16 Apr 2012 12:32:29 -0400 Received: from mail-pz0-f52.google.com ([209.85.210.52]:37352 "EHLO mail-pz0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752442Ab2DPQc2 (ORCPT ); Mon, 16 Apr 2012 12:32:28 -0400 Received: by mail-pz0-f52.google.com with SMTP id e40so7132053dak.11 for ; Mon, 16 Apr 2012 09:32:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=aW15IR1AfjqWblHT+tgqtE8pZsfsZTXO2vYN4LrpHeQ=; b=BuFnb4WjRnjsVn72vpyHI3crE2ugkMCbomUnimsRqVlQpTqjrbRFos5yKnzS/HqrdE 1CviRVGAaO0cKHmnvoPHRZdNVwiy0yPSFSXzwS5BSyVbKagTdATqQP4ayacuSwCAJHAt L2GbOI4Sq+o4DNAlno6pVvzQIWCRbD3woRH8T3Hfkb4Wj7GQUtNzt74aFwWAckoaRZPa oVfiUMzWk3tboQbRyMse5UJjy2aID+pDc6S7gdaHmK34d+fZoWXaraB3FKudGk+H36BB QSpfYHM7uicqyQa/GIB2rv+zBSibQmWwqa0A6aZLbng59IrMFhCGrgAqlh/nPBYE98HX FaZA== Received: by 10.68.237.71 with SMTP id va7mr12572245pbc.51.1334593948445; Mon, 16 Apr 2012 09:32:28 -0700 (PDT) Received: from localhost.localdomain ([221.221.22.162]) by mx.google.com with ESMTPS id v1sm18106794pbk.10.2012.04.16.09.32.22 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 16 Apr 2012 09:32:27 -0700 (PDT) From: Jiang Liu To: Yinghai Lu , Kenji Kaneshige , Bjorn Helgaas Cc: Jiang Liu , Jiang Liu , Keping Chen , linux-pci@vger.kernel.org Subject: [PATCH RFC 03/17] PCI: replace pci_remove_rescan_mutex with the PCI hotplug lock Date: Tue, 17 Apr 2012 00:28:57 +0800 Message-Id: <1334593751-5916-4-git-send-email-jiang.liu@huawei.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1334593751-5916-1-git-send-email-jiang.liu@huawei.com> References: <1334593751-5916-1-git-send-email-jiang.liu@huawei.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org Replace pci_remove_rescan_mutex with the PCI hotplug lock, which is used to globally serialize all PCI hotplug operations. Signed-off-by: Jiang Liu --- drivers/pci/pci-sysfs.c | 100 +++++++++++++++++++++++++++-------------------- drivers/pci/remove.c | 1 + 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index a55e248..ecf197d 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -283,7 +283,31 @@ msi_bus_store(struct device *dev, struct device_attribute *attr, } #ifdef CONFIG_HOTPLUG -static DEFINE_MUTEX(pci_remove_rescan_mutex); +/* + * Schedule a device callback to avoid deadlock in case of + * 1) An attribute method tries to deregister the sysfs file itself + * 2) Another thread is trying to remove the sysfs file, which have + * already held the PCI hotplug lock by invoking pci_hotplug_enter(). + */ +static int schedule_hp_callback(struct device *dev, + const char *buf, size_t count, + void (*func)(struct device *)) +{ + int ret = 0; + unsigned long val; + + if (kstrtoul(buf, 0, &val) < 0) + return -EINVAL; + + if (val) { + ret = device_schedule_callback(dev, func); + if (ret) + count = ret; + } + + return count; +} + static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, size_t count) { @@ -294,10 +318,10 @@ static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, return -EINVAL; if (val) { - mutex_lock(&pci_remove_rescan_mutex); + pci_hotplug_enter(); while ((b = pci_find_next_bus(b)) != NULL) pci_rescan_bus(b); - mutex_unlock(&pci_remove_rescan_mutex); + pci_hotplug_exit(); } return count; } @@ -307,72 +331,62 @@ struct bus_attribute pci_bus_attrs[] = { __ATTR_NULL }; -static ssize_t -dev_rescan_store(struct device *dev, struct device_attribute *attr, - const char *buf, size_t count) +static void dev_rescan_callback(struct device *dev) { - unsigned long val; struct pci_dev *pdev = to_pci_dev(dev); - if (strict_strtoul(buf, 0, &val) < 0) - return -EINVAL; - - if (val) { - mutex_lock(&pci_remove_rescan_mutex); + pci_hotplug_enter(); + /* Skip if the device has been stopped. */ + if (pdev->is_added && pdev->bus) pci_rescan_bus(pdev->bus); - mutex_unlock(&pci_remove_rescan_mutex); - } - return count; + pci_hotplug_exit(); +} + +static ssize_t +dev_rescan_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + return schedule_hp_callback(dev, buf, count, dev_rescan_callback); } static void remove_callback(struct device *dev) { struct pci_dev *pdev = to_pci_dev(dev); - mutex_lock(&pci_remove_rescan_mutex); - pci_stop_and_remove_bus_device(pdev); - mutex_unlock(&pci_remove_rescan_mutex); + pci_hotplug_enter(); + /* Skip if the bus has been removed. */ + if (pdev->bus_list.next) + pci_stop_and_remove_bus_device(pdev); + pci_hotplug_exit(); } static ssize_t remove_store(struct device *dev, struct device_attribute *dummy, const char *buf, size_t count) { - int ret = 0; - unsigned long val; - - if (strict_strtoul(buf, 0, &val) < 0) - return -EINVAL; - - /* An attribute cannot be unregistered by one of its own methods, - * so we have to use this roundabout approach. - */ - if (val) - ret = device_schedule_callback(dev, remove_callback); - if (ret) - count = ret; - return count; + return schedule_hp_callback(dev, buf, count, remove_callback); } -static ssize_t -dev_bus_rescan_store(struct device *dev, struct device_attribute *attr, - const char *buf, size_t count) +static void dev_bus_rescan_callback(struct device *dev) { - unsigned long val; struct pci_bus *bus = to_pci_bus(dev); - if (strict_strtoul(buf, 0, &val) < 0) - return -EINVAL; - - if (val) { - mutex_lock(&pci_remove_rescan_mutex); + pci_hotplug_enter(); + /* Skip if the bus has been stopped. */ + if (bus->is_added) { if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) pci_rescan_bus_bridge_resize(bus->self); else pci_rescan_bus(bus); - mutex_unlock(&pci_remove_rescan_mutex); } - return count; + pci_hotplug_exit(); +} + +static ssize_t +dev_bus_rescan_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + return schedule_hp_callback(dev, buf, count, dev_bus_rescan_callback); } #endif diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c index fd77e2b..0b4342b 100644 --- a/drivers/pci/remove.c +++ b/drivers/pci/remove.c @@ -72,6 +72,7 @@ void pci_remove_bus(struct pci_bus *pci_bus) if (!pci_bus->is_added) return; + pci_bus->is_added = 0; pci_remove_legacy_files(pci_bus); device_unregister(&pci_bus->dev); }