From patchwork Fri Dec 9 21:06:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashok Raj X-Patchwork-Id: 9469021 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1AE2F60586 for ; Fri, 9 Dec 2016 21:06:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EE0B22863F for ; Fri, 9 Dec 2016 21:06:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D0A6928697; Fri, 9 Dec 2016 21:06:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 499FC2863F for ; Fri, 9 Dec 2016 21:06:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751064AbcLIVGb (ORCPT ); Fri, 9 Dec 2016 16:06:31 -0500 Received: from mga09.intel.com ([134.134.136.24]:34567 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750827AbcLIVGa (ORCPT ); Fri, 9 Dec 2016 16:06:30 -0500 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP; 09 Dec 2016 13:06:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,325,1477983600"; d="scan'208";a="38647602" Received: from otc-nc-03.jf.intel.com ([10.54.39.29]) by orsmga004.jf.intel.com with ESMTP; 09 Dec 2016 13:06:29 -0800 From: Ashok Raj To: linux-pci@vger.kernel.org, Bjorn Helgaas Cc: Ashok Raj , linux-kernel@vger.kernel.org, Keith Busch Subject: [PATCH] pciehp: Fix race condition handling surprise link-down Date: Fri, 9 Dec 2016 13:06:04 -0800 Message-Id: <1481317564-18045-1-git-send-email-ashok.raj@intel.com> X-Mailer: git-send-email 2.7.4 Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Changes from v1: Address comments from Bjorn: Added p_slot->lock mutex around changes to p_slot->state Updated commit message to call out mutex names A surprise link down may retrain very quickly, causing the same slot to generate a link up event before handling the link down completes. Since the link is active, the power off work queued from the first link down will cause a second down event when the power is disabled. The second down event should be ignored because the slot is already powering off; however, the "link up" event sets the slot state to POWERON before the event to handle this is enqueued, making the second down event believe it needs to do something. This creates a constant link up and down event cycle. This patch fixes that by setting the p_slot->state only when the work to handle the power event is executing, protected by the p_slot->hotplug_lock. To: Bjorn Helgass Cc: linux-kernel@vger.kernel.org Cc: Keith Busch Signed-off-by: Ashok Raj Reviewed-by: Keith Busch --- drivers/pci/hotplug/pciehp_ctrl.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/pci/hotplug/pciehp_ctrl.c b/drivers/pci/hotplug/pciehp_ctrl.c index ec0b4c1..4cf4772 100644 --- a/drivers/pci/hotplug/pciehp_ctrl.c +++ b/drivers/pci/hotplug/pciehp_ctrl.c @@ -182,6 +182,9 @@ static void pciehp_power_thread(struct work_struct *work) switch (info->req) { case DISABLE_REQ: mutex_lock(&p_slot->hotplug_lock); + mutex_lock(&p_slot->lock); + p_slot->state = POWEROFF_STATE; + mutex_unlock(&p_slot->lock); pciehp_disable_slot(p_slot); mutex_unlock(&p_slot->hotplug_lock); mutex_lock(&p_slot->lock); @@ -190,6 +193,9 @@ static void pciehp_power_thread(struct work_struct *work) break; case ENABLE_REQ: mutex_lock(&p_slot->hotplug_lock); + mutex_lock(&p_slot->lock); + p_slot->state = POWERON_STATE; + mutex_unlock(&p_slot->lock); ret = pciehp_enable_slot(p_slot); mutex_unlock(&p_slot->hotplug_lock); if (ret) @@ -209,8 +215,6 @@ static void pciehp_queue_power_work(struct slot *p_slot, int req) { struct power_work_info *info; - p_slot->state = (req == ENABLE_REQ) ? POWERON_STATE : POWEROFF_STATE; - info = kmalloc(sizeof(*info), GFP_KERNEL); if (!info) { ctrl_err(p_slot->ctrl, "no memory to queue %s request\n",