From patchwork Thu May 16 15:50:54 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 2578671 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 8C854DFB7B for ; Thu, 16 May 2013 15:53:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754764Ab3EPPvo (ORCPT ); Thu, 16 May 2013 11:51:44 -0400 Received: from mail-pd0-f180.google.com ([209.85.192.180]:49222 "EHLO mail-pd0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754704Ab3EPPvk (ORCPT ); Thu, 16 May 2013 11:51:40 -0400 Received: by mail-pd0-f180.google.com with SMTP id 10so1374990pdc.11 for ; Thu, 16 May 2013 08:51:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=Tub5WwMb2vCKwlQS5eJwhJchN/E0sgd1rTaGZx8nKI4=; b=cWm7RT1j3VqafjvMLskSct4s8Rp05o3kYqGWJLkxuGdsDnta91fMHU5uTsus2dp34L oXv0+i0ssyh4rgros8E2AD0jyz7Z69uPRrdYBbxf5woMtR/c6MpdjS1TOujPs4ziRChU EPKaBO6/BzRiW7E8dx17yHMFYdzjkVwHsDvUhqkK6K9SCyay7L52hodjP7pDCq898JWS Rssa/RvTu+swNJVhtjVjcrp7sCtFLtrr0kd1MydBJ0cpcPBYvFhrBVKpTnfgjAFoyLio o7MIshtrq/VgFr0nS/RbQzg7fbMoFdXwcktkZdtxGXJeRNj8lBaWd8qOySySxToanw8F 8wOA== X-Received: by 10.68.216.37 with SMTP id on5mr44935001pbc.35.1368719499444; Thu, 16 May 2013 08:51:39 -0700 (PDT) Received: from localhost.localdomain ([120.196.98.100]) by mx.google.com with ESMTPSA id ea15sm8026701pad.16.2013.05.16.08.51.35 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 16 May 2013 08:51:38 -0700 (PDT) From: Jiang Liu To: Bjorn Helgaas , Yinghai Lu Cc: Jiang Liu , "Rafael J . Wysocki" , Greg Kroah-Hartman , Gu Zheng , Toshi Kani , Myron Stowe , Yijing Wang , Jiang Liu , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v2, part3 06/11] PCI, sysfs: use PCI bus lock to serialize hotplug operations triggered by sysfs Date: Thu, 16 May 2013 23:50:54 +0800 Message-Id: <1368719459-24800-7-git-send-email-jiang.liu@huawei.com> X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1368719459-24800-1-git-send-email-jiang.liu@huawei.com> References: <1368719459-24800-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 Use PCI bus lock to serialize hotplug operations triggered by pci-sysfs, and remove the redundant local mutex pci_remove_rescan_mutex. This also fixes the bug reported by Gu Zheng as: echo -n 1 > /sys/bus/pci/devices/0000\:10\:00.0/remove ; echo -n 1 > /sys/bus/pci/devices/0000\:1a\:01.0/remove will cause kernel crash as bus get freed. [ 418.946462] CPU 4 [ 418.968377] Pid: 512, comm: kworker/u:2 Tainted: G W 3.8.0 #2 FUJITSU-SV PRIMEQUEST 1800E/SB [ 419.081763] RIP: 0010:[] [] pci_bus_read_config_word+0x5e/0x90 [ 420.494137] Call Trace: [ 420.523326] [] ? remove_callback+0x1f/0x40 [ 420.591984] [] pci_pme_active+0x4b/0x1c0 [ 420.658545] [] pci_stop_bus_device+0x57/0xb0 [ 420.729259] [] pci_stop_and_remove_bus_device+0x16/0x30 [ 420.811392] [] remove_callback+0x2b/0x40 [ 420.877955] [] sysfs_schedule_callback_work+0x26/0x70 https://bugzilla.kernel.org/show_bug.cgi?id=54411 Signed-off-by: Jiang Liu Reported-by: Gu Zheng Cc: linux-pci@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/pci/pci-sysfs.c | 52 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index fcc4bb2..91ff11e 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -284,7 +284,6 @@ msi_bus_store(struct device *dev, struct device_attribute *attr, return count; } -static DEFINE_MUTEX(pci_remove_rescan_mutex); static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, size_t count) { @@ -293,13 +292,15 @@ static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, if (strict_strtoul(buf, 0, &val) < 0) return -EINVAL; + if (!val) + return count; - if (val) { - mutex_lock(&pci_remove_rescan_mutex); - for_each_pci_root_bus(b) + for_each_pci_root_bus(b) + if (pci_bus_lock(b, PCI_BUS_STATE_STOPPING - 1, true) == 0) { pci_rescan_bus(b); - mutex_unlock(&pci_remove_rescan_mutex); - } + pci_bus_unlock(b, true); + } + return count; } @@ -312,27 +313,41 @@ static ssize_t dev_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + int ret; unsigned long val; struct pci_dev *pdev = to_pci_dev(dev); if (strict_strtoul(buf, 0, &val) < 0) return -EINVAL; + if (!val) + return count; + + do { + ret = pci_bus_lock_timeout(pdev->bus, + PCI_BUS_STATE_STOPPING - 1, true, HZ); + if (ret == 0) { + pci_rescan_bus(pdev->bus); + pci_bus_unlock(pdev->bus, true); + break; + } + /* + * Prevent a deadlock scenario that thread A waits for + * all sysfs files to be released while holding PCI bus + * locks, and Thread B tries to acquire PCI bus locks + * in a sysfs handler. These checks break the deadlock + * condition. + */ + if (pci_dev_get_state(pdev) >= PCI_DEV_STATE_STOPPING || + pci_bus_get_state(pdev->bus) >= PCI_BUS_STATE_STOPPING) + return -EBUSY; + } while (true); - if (val) { - mutex_lock(&pci_remove_rescan_mutex); - pci_rescan_bus(pdev->bus); - mutex_unlock(&pci_remove_rescan_mutex); - } return count; } 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_stop_and_remove_device(to_pci_dev(dev)); } static ssize_t @@ -366,12 +381,13 @@ dev_bus_rescan_store(struct device *dev, struct device_attribute *attr, return -EINVAL; if (val) { - mutex_lock(&pci_remove_rescan_mutex); + if (pci_bus_lock(bus, PCI_BUS_STATE_STOPPING - 1, true) < 0) + return -EBUSY; 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); + pci_bus_unlock(bus, true); } return count; }