From patchwork Wed Oct 30 10:12:27 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gu Zheng X-Patchwork-Id: 3113771 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id EC1A89F2B7 for ; Wed, 30 Oct 2013 10:18:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0F9AB201ED for ; Wed, 30 Oct 2013 10:18:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D5378201EC for ; Wed, 30 Oct 2013 10:18:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751730Ab3J3KS3 (ORCPT ); Wed, 30 Oct 2013 06:18:29 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:4222 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751667Ab3J3KS2 (ORCPT ); Wed, 30 Oct 2013 06:18:28 -0400 X-IronPort-AV: E=Sophos;i="4.93,599,1378828800"; d="scan'208";a="8899884" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 30 Oct 2013 18:15:05 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id r9UAIPk9007100; Wed, 30 Oct 2013 18:18:25 +0800 Received: from [10.167.226.100] ([10.167.226.100]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2013103018164401-2645955 ; Wed, 30 Oct 2013 18:16:44 +0800 Message-ID: <5270DB8B.6070600@cn.fujitsu.com> Date: Wed, 30 Oct 2013 18:12:27 +0800 From: Gu Zheng User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110930 Thunderbird/7.0.1 MIME-Version: 1.0 To: Bjorn Helgaas CC: "linux-pci@vger.kernel.org" , linux-kernel Subject: [PATCH] pci-sysfs: fix a potential deadlock when concurrent remove&rescan pci device via sysfs X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/10/30 18:16:44, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/10/30 18:16:44, Serialize complete at 2013/10/30 18:16:44 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP There is a potential deadlock situation when we manipulate pci device (remove&rescan) via the pci-sysfs user interfaces simultaneously. Privious patch: https://lkml.org/lkml/2012/12/27/10 Related bug reports on bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=60672 https://bugzilla.kernel.org/show_bug.cgi?id=60695 deadlock info described as following: *dmesg ifno*: (snip) 1000e 0000:1c:00.0: eth9: Intel(R) PRO/1000 Network Connection sd 13:2:0:0: [sdb] Attached SCSI disk e1000e 0000:1c:00.0: eth9: MAC: 0, PHY: 4, PBA No: D50228-005 e1000e 0000:1c:00.1: Disabling ASPM L1 e1000e 0000:1c:00.1: Interrupt Throttling Rate (ints/sec) set to dynamic conservative mode e1000e 0000:1c:00.1: irq 143 for MSI/MSI-X e1000e 0000:1c:00.1: eth10: (PCI Express:2.5GT/s:Width x4) 00:15:17:cd:96:bf e1000e 0000:1c:00.1: eth10: Intel(R) PRO/1000 Network Connection e1000e 0000:1c:00.1: eth10: MAC: 0, PHY: 4, PBA No: D50228-005 INFO: task bash:62982 blocked for more than 120 seconds. "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. bash D 0000000000000000 0 62982 62978 0x00000080 ffff88038b277db8 0000000000000082 ffff88038b277fd8 0000000000013940 ffff88038b276010 0000000000013940 0000000000013940 0000000000013940 ffff88038b277fd8 0000000000013940 ffff880377449e30 ffff8806e822c670 Call Trace: [] schedule+0x29/0x70 [] schedule_preempt_disabled+0xe/0x10 [] __mutex_lock_slowpath+0xd3/0x150 [] mutex_lock+0x2b/0x50 [] dev_rescan_store+0x5c/0x80 [] dev_attr_store+0x20/0x30 [] sysfs_write_file+0xef/0x170 [] vfs_write+0xc8/0x190 [] sys_write+0x51/0x90 [] system_call_fastpath+0x16/0x1b INFO: task bash:64141 blocked for more than 120 seconds. "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. bash D ffffffff81610460 0 64141 64136 0x00000080 ffff8803540e9db8 0000000000000086 ffff8803540e9fd8 0000000000013940 ffff8803540e8010 0000000000013940 0000000000013940 0000000000013940 ffff8803540e9fd8 0000000000013940 ffff8807db338a10 ffff8806f09abc60 Call Trace: [] schedule+0x29/0x70 [] schedule_preempt_disabled+0xe/0x10 [] __mutex_lock_slowpath+0xd3/0x150 [] mutex_lock+0x2b/0x50 [] dev_rescan_store+0x5c/0x80 [] dev_attr_store+0x20/0x30 [] sysfs_write_file+0xef/0x170 [] vfs_write+0xc8/0x190 [] sys_write+0x51/0x90 [] system_call_fastpath+0x16/0x1b INFO: task kworker/u:3:64451 blocked for more than 120 seconds. "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. kworker/u:3 D ffffffff81610460 0 64451 2 0x00000080 ffff8807d51b7a30 0000000000000046 ffff8807d51b7fd8 0000000000013940 ffff8807d51b6010 0000000000013940 0000000000013940 0000000000013940 ffff8807d51b7fd8 0000000000013940 ffff8807db339420 ffff88037744b250 Call Trace: [] schedule+0x29/0x70 [] schedule_timeout+0x19d/0x220 [] ? __slab_free+0x1f2/0x2f0 [] wait_for_common+0x11e/0x190 [] ? try_to_wake_up+0x2c0/0x2c0 [] wait_for_completion+0x1d/0x20 [] sysfs_addrm_finish+0xb8/0xd0 [] ? sysfs_schedule_callback+0x1e0/0x1e0 [] sysfs_hash_and_remove+0x60/0xb0 [] sysfs_remove_file+0x39/0x50 [] device_remove_file+0x17/0x20 [] bus_remove_device+0xdc/0x180 [] device_del+0x120/0x1d0 [] device_unregister+0x22/0x60 [] pci_stop_bus_device+0x94/0xa0 [] pci_stop_bus_device+0x40/0xa0 [] pci_stop_bus_device+0x40/0xa0 [] pci_stop_bus_device+0x40/0xa0 [] pci_stop_and_remove_bus_device+0x16/0x30 [] remove_callback+0x29/0x40 [] sysfs_schedule_callback_work+0x24/0x70 [] process_one_work+0x179/0x4b0 [] worker_thread+0x12e/0x330 [] ? manage_workers+0x110/0x110 [] kthread+0x9e/0xb0 [] kernel_thread_helper+0x4/0x10 [] ? kthread_freezable_should_stop+0x70/0x70 [] ? gs_change+0x13/0x13 path1: sysfs remove device: | path2: sysfs rescan device: sysfs_schedule_callback_work() | sysfs_write_file() remove_callback() | flush_write_buffer() *1* mutex_lock(&pci_remove_rescan_mutex)|*2* sysfs_get_active(attr_sd) ... | dev_attr_store() device_remove_file() | dev_rescan_store() ... |*4* mutex_lock(&pci_remove_rescan_mutex) *3* sysfs_deactivate(sd) | ... wait_for_completion() |*5* sysfs_put_active(attr_sd) *6* mutex_unlock(&pci_remove_rescan_mutex) If path1 first holds the pci_remove_rescan_mutex at *1*, then another path called path2 actived and runs to *2* before path1 runs to *3*, we now runs to a deadlock situation: Path1 holds the mutex waiting path2 to decrease sysfs_dirent's s_active counter at *5*, but path2 is blocked at *4* when trying to get the pci_remove_rescan_mutex. The mutex won't be put by path1 until it reach *6*, but it's now blocked at *3*. So we use mutex_try_lock to avoid this deadlock, and additional wait/restart_syscall steps are used to make the fail path of try_lock more friendly to user space task. Signed-off-by: Gu Zheng --- drivers/pci/pci-sysfs.c | 48 +++++++++++++++++++++++++++++++++++++++------- 1 files changed, 40 insertions(+), 8 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 7128cfd..0dac6f4 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "pci.h" static int sysfs_initialized; /* = 0 */ @@ -285,6 +286,25 @@ msi_bus_store(struct device *dev, struct device_attribute *attr, } static DEFINE_MUTEX(pci_remove_rescan_mutex); + +static void pci_remove_rescan_lock(void) +{ + mutex_lock(&pci_remove_rescan_mutex); +} + +static int pci_remove_rescan_lock_sysfs(void) +{ + if (mutex_trylock(&pci_remove_rescan_mutex)) + return 0; + /* Avoid busy looping (20 ms of sleep should do). */ + msleep(20); + return restart_syscall(); +} + +static void pci_remove_rescan_unlock(void) +{ + mutex_unlock(&pci_remove_rescan_mutex); +} static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, size_t count) { @@ -295,10 +315,14 @@ static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf, return -EINVAL; if (val) { - mutex_lock(&pci_remove_rescan_mutex); + int ret; + + ret = pci_remove_rescan_lock_sysfs(); + if (ret) + return ret; while ((b = pci_find_next_bus(b)) != NULL) pci_rescan_bus(b); - mutex_unlock(&pci_remove_rescan_mutex); + pci_remove_rescan_unlock(); } return count; } @@ -319,9 +343,13 @@ dev_rescan_store(struct device *dev, struct device_attribute *attr, return -EINVAL; if (val) { - mutex_lock(&pci_remove_rescan_mutex); + int ret; + + ret = pci_remove_rescan_lock_sysfs(); + if (ret) + return ret; pci_rescan_bus(pdev->bus); - mutex_unlock(&pci_remove_rescan_mutex); + pci_remove_rescan_unlock(); } return count; } @@ -332,9 +360,9 @@ static void remove_callback(struct device *dev) { struct pci_dev *pdev = to_pci_dev(dev); - mutex_lock(&pci_remove_rescan_mutex); + pci_remove_rescan_lock(); pci_stop_and_remove_bus_device(pdev); - mutex_unlock(&pci_remove_rescan_mutex); + pci_remove_rescan_unlock(); } static ssize_t @@ -370,12 +398,16 @@ dev_bus_rescan_store(struct device *dev, struct device_attribute *attr, return -EINVAL; if (val) { - mutex_lock(&pci_remove_rescan_mutex); + int ret; + + ret = pci_remove_rescan_lock_sysfs(); + if (ret) + return ret; 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_remove_rescan_unlock(); } return count; }