From patchwork Fri Apr 27 15:16:55 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 155510 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 16168B6FA5 for ; Sat, 28 Apr 2012 01:23:43 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932142Ab2D0PWK (ORCPT ); Fri, 27 Apr 2012 11:22:10 -0400 Received: from mail-pz0-f51.google.com ([209.85.210.51]:50093 "EHLO mail-pz0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932115Ab2D0PWI (ORCPT ); Fri, 27 Apr 2012 11:22:08 -0400 Received: by mail-pz0-f51.google.com with SMTP id z8so1149168dad.10 for ; Fri, 27 Apr 2012 08:22:08 -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=Fb3RIDsTO4rwKexoftczE2mm3qLjRq+TaVAu5QGDDds=; b=vtIyQ3NElOXr9sZA4NjBVbqUNPCpBrycfhzvpHR1NMVizqO7zd41MZHeTQAwqv4zWv dDbBTdWvVKvO6aMqdz1cPpA3rVDBNjNvPaHntNQKEVXFuDPFmz+Q0O81vcuEpdcSpnV5 N09JYTPUlucTXu1c4AEdNV5nm/TDeDBa2YFNBLR0tWhe9FTfUg9hEz+ibg9gJAFBFJ0B vtlkOMZ/Fr3in62qTyJIhodIGUU1BLR3cew/+3TpWxduPgy6XUZrfSGluzx5SyBuQ9a+ 9fm+JQaosq2xui/6MTAKOw+lDKQGXJnlchpyL5kPgkhUoOTewdhAQacH5KVWahiPfTL5 1zuQ== Received: by 10.68.240.135 with SMTP id wa7mr24357952pbc.7.1335540128062; Fri, 27 Apr 2012 08:22:08 -0700 (PDT) Received: from localhost.localdomain ([221.221.26.142]) by mx.google.com with ESMTPS id 2sm6743917pbw.57.2012.04.27.08.21.59 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 27 Apr 2012 08:22:06 -0700 (PDT) From: Jiang Liu To: Yinghai Lu , Kenji Kaneshige , Bjorn Helgaas , Don Dutile , Greg KH Cc: Jiang Liu , Keping Chen , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Jiang Liu Subject: [PATCH v2 14/19] PCI: fix race windows when shutting down cpcihp controller Date: Fri, 27 Apr 2012 23:16:55 +0800 Message-Id: <1335539820-11232-15-git-send-email-jiang.liu@huawei.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1335539820-11232-1-git-send-email-jiang.liu@huawei.com> References: <1335539820-11232-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 From: Jiang Liu When cpci_hp_stop() is called to disabled cpcihp controller, it will disable interrupt for that controller. But there's small window for event_thread() to reenable the interrupt again. So stop the worker thread before disabling the interrupt. If check_slots() returns error, ther working thread (cpci_thread) will exit. Later when cpci_stop_thread() or cpci_hp_intr() tries to access cpci_thread, it may have already been destroyed. So hold a reference count to cpci_thread to avoid invalid memory access. Signed-off-by: Jiang Liu --- drivers/pci/hotplug/cpci_hotplug_core.c | 27 +++++++++++++++++---------- 1 files changed, 17 insertions(+), 10 deletions(-) diff --git a/drivers/pci/hotplug/cpci_hotplug_core.c b/drivers/pci/hotplug/cpci_hotplug_core.c index 7898023..68e43c7 100644 --- a/drivers/pci/hotplug/cpci_hotplug_core.c +++ b/drivers/pci/hotplug/cpci_hotplug_core.c @@ -60,7 +60,6 @@ static atomic_t extracting; int cpci_debug; static struct cpci_hp_controller *controller; static struct task_struct *cpci_thread; -static int thread_finished; static int enable_slot(struct hotplug_slot *slot); static int disable_slot(struct hotplug_slot *slot); @@ -341,7 +340,8 @@ cpci_hp_intr(int irq, void *data) controller->ops->disable_irq(); /* Trigger processing by the event thread */ - wake_up_process(cpci_thread); + if (cpci_thread) + wake_up_process(cpci_thread); return IRQ_HANDLED; } @@ -508,7 +508,6 @@ event_thread(void *data) msleep(500); } else if (rc < 0) { dbg("%s - error checking slots", __func__); - thread_finished = 1; goto out; } } while (atomic_read(&extracting) && !kthread_should_stop()); @@ -540,7 +539,6 @@ poll_thread(void *data) msleep(500); } else if (rc < 0) { dbg("%s - error checking slots", __func__); - thread_finished = 1; goto out; } } while (atomic_read(&extracting) && !kthread_should_stop()); @@ -562,15 +560,24 @@ cpci_start_thread(void) err("Can't start up our thread"); return PTR_ERR(cpci_thread); } - thread_finished = 0; + get_task_struct(cpci_thread); + return 0; } static void cpci_stop_thread(void) { - kthread_stop(cpci_thread); - thread_finished = 1; + struct task_struct *tp; + + if (cpci_thread) { + local_irq_disable(); + tp = cpci_thread; + cpci_thread = NULL; + local_irq_enable(); + kthread_stop(tp); + put_task_struct(tp); + } } int @@ -627,8 +634,7 @@ cpci_hp_unregister_controller(struct cpci_hp_controller *old_controller) int status = 0; if (controller) { - if (!thread_finished) - cpci_stop_thread(); + cpci_stop_thread(); if (controller->irq) free_irq(controller->irq, controller->dev_id); controller = NULL; @@ -680,12 +686,13 @@ cpci_hp_stop(void) { if (!controller) return -ENODEV; + cpci_stop_thread(); if (controller->irq) { /* Stop enum interrupt processing */ dbg("%s - disabling irq", __func__); controller->ops->disable_irq(); + synchronize_irq(controller->irq); } - cpci_stop_thread(); return 0; }