From patchwork Fri Feb 26 11:46:26 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amit Shah X-Patchwork-Id: 46323 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from bilbo.ozlabs.org (localhost [127.0.0.1]) by ozlabs.org (Postfix) with ESMTP id E3304B7080 for ; Fri, 26 Feb 2010 22:48:03 +1100 (EST) Received: by ozlabs.org (Postfix) id D6D90B7CEC; Fri, 26 Feb 2010 22:47:54 +1100 (EST) Delivered-To: linuxppc-dev@ozlabs.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by ozlabs.org (Postfix) with ESMTP id 73D2AB7080 for ; Fri, 26 Feb 2010 22:47:53 +1100 (EST) Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o1QBljsZ000610 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 26 Feb 2010 06:47:45 -0500 Received: from localhost (dhcp1-127.pnq.redhat.com [10.65.193.127]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o1QBlheK006530; Fri, 26 Feb 2010 06:47:44 -0500 From: Amit Shah To: linux-kernel@vger.kernel.org Subject: [PATCH] hvc_console: Fix a race between hvc_close and hvc_remove Date: Fri, 26 Feb 2010 17:16:26 +0530 Message-Id: <1267184786-4377-1-git-send-email-amit.shah@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 Cc: Amit Shah , linuxppc-dev@ozlabs.org, Rusty Russell , Alan Cox X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Alan pointed out a race in the code where hvc_remove is invoked. The recent virtio_console work is the first user of hvc_remove(). Alan describes it thus: The hvc_console assumes that a close and remove call can't occur at the same time. In addition tty_hangup(tty) is problematic as tty_hangup is asynchronous itself.... So this can happen hvc_close hvc_remove hung up ? - no lock tty = hp->tty unlock lock hp->tty = NULL unlock notify del kref_put the hvc struct close completes tty is destroyed tty_hangup dead tty tty->ops will be NULL NULL->... This patch adds some tty krefs and also converts to using tty_vhangup() before putting the tty kref. Reported-by: Alan Cox Signed-off-by: Amit Shah CC: Alan Cox CC: linuxppc-dev@ozlabs.org CC: Rusty Russell --- I can't be sure if this is all that's needed. tty people, please take a look! drivers/char/hvc_console.c | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-) diff --git a/drivers/char/hvc_console.c b/drivers/char/hvc_console.c index d8dac58..3983e32 100644 --- a/drivers/char/hvc_console.c +++ b/drivers/char/hvc_console.c @@ -106,6 +106,7 @@ static struct hvc_struct *hvc_get_by_index(int index) spin_lock_irqsave(&hp->lock, flags); if (hp->index == index) { kref_get(&hp->kref); + tty_kref_get(hp->tty); spin_unlock_irqrestore(&hp->lock, flags); spin_unlock(&hvc_structs_lock); return hp; @@ -390,6 +391,7 @@ static void hvc_close(struct tty_struct *tty, struct file * filp) } kref_put(&hp->kref, destroy_hvc_struct); + tty_kref_put(tty); } static void hvc_hangup(struct tty_struct *tty) @@ -806,6 +808,7 @@ int hvc_remove(struct hvc_struct *hp) unsigned long flags; struct tty_struct *tty; + tty_kref_get(hp->tty); spin_lock_irqsave(&hp->lock, flags); tty = hp->tty; @@ -830,7 +833,9 @@ int hvc_remove(struct hvc_struct *hp) * cleaned up the hvc_struct. */ if (tty) - tty_hangup(tty); + tty_vhangup(tty); + + tty_kref_put(hp->tty); return 0; } EXPORT_SYMBOL_GPL(hvc_remove);