From patchwork Mon Apr 16 16:29:08 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 152938 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 6128BB700D for ; Tue, 17 Apr 2012 02:33:48 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753479Ab2DPQdr (ORCPT ); Mon, 16 Apr 2012 12:33:47 -0400 Received: from mail-pz0-f52.google.com ([209.85.210.52]:35016 "EHLO mail-pz0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751474Ab2DPQdr (ORCPT ); Mon, 16 Apr 2012 12:33:47 -0400 Received: by mail-pz0-f52.google.com with SMTP id e40so7133149dak.11 for ; Mon, 16 Apr 2012 09:33:47 -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=R120BOZrdysvqPUmu5mSl0xubufr+sbHL8UB9H7FkEQ=; b=b7+vkficXDAeHnHjS0kq2zRm+xbdCqS+gt37GaEXGVsHA2nNp3aY0Lg+mur+y+NoBO 6PFF/qZzgq7r+oIz1qa1Hj0dSbc7GHSuNxFJs972jxDNMh8gJd9+E/lDByicE8TtAk5P Exlaz+arfD1gwo83SNPj+hw69c9EC6CLCH2s3bLj9XMXtxJrNcwV9vOW9xOwBXm135k+ Jk4vaqyt9MEgzZ3fxQHZ+Hi5p5BDorx7fE1hDOpumUvc06jEqhUqcTIyEfxOXbdHx1o+ oU84zjRSZNJwGfpMGZbh9omgpg5dyzW00ev62/jFjSmOadWPQA3rZ/64l+r5OyHK71Jo SzkA== Received: by 10.68.226.8 with SMTP id ro8mr28892497pbc.72.1334594026957; Mon, 16 Apr 2012 09:33:46 -0700 (PDT) Received: from localhost.localdomain ([221.221.22.162]) by mx.google.com with ESMTPS id v1sm18106794pbk.10.2012.04.16.09.33.41 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 16 Apr 2012 09:33:46 -0700 (PDT) From: Jiang Liu To: Yinghai Lu , Kenji Kaneshige , Bjorn Helgaas , Scott Murray Cc: Jiang Liu , Jiang Liu , Keping Chen , linux-pci@vger.kernel.org Subject: [PATCH RFC 14/17] PCI: fix race windows when shutting down cpcihp controller Date: Tue, 17 Apr 2012 00:29:08 +0800 Message-Id: <1334593751-5916-15-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 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; }