Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4025194ybl; Mon, 3 Feb 2020 11:05:18 -0800 (PST) X-Google-Smtp-Source: APXvYqxTzlCXeN9KZEIrPVH+MXBfCnedbev5F4vPSoRZFwKP5LzON56BkveOKPTs2IMBuQEfCwlj X-Received: by 2002:aca:b4c3:: with SMTP id d186mr325181oif.131.1580756718564; Mon, 03 Feb 2020 11:05:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580756718; cv=none; d=google.com; s=arc-20160816; b=bDMZtWa6HL1oZJchON8dIi84I7B7cQJ6yk6e64GJcW1Rlx+3vJX+8hWThxtzHSlJa4 6I9lI8rVasC+9SbAkWpUe4k6UWrD1Q9pEbe/Ee/88MMjYQhX0Fmi4gbO40w80+yyH7gA Tz6E58J175Sxa61nvdGHNRZpFtq+0OjRT6i9suoSlOmAkIm3BIunLCuB9rRt/RXUHe9M sftLsU/ikc2MWSP164t1UQfawnh6dQNUI6WxAkWQgwcVgBl9yP/UcTGKwcDOvH176HVE qkrvPKWtBWdQIRuJPstqnp61Ifan5soKl6ySM6CAqaFSk7xILHRrOmxyEhWILtYsjpQl MCrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=xMAsgB4q4Gh8vVgv0gnKXray5mZORakdvq/WWo0nQq4=; b=saWLIVdg88tH4/rynYXDTqkMQOCPWFyH0QqNABm1Na8nSBtmYzUDjNQ2Hiz0LWl3hz GWapu++E7DxZQ3gDO/W/7ncmgk1fzb7tSjzC1rY+T+AQoWsn2HZSPe3LBCc/2TOtWOhU cua1oe4DUmpJ1lhlb9xsypNkcgKthQM5rzURwU6BJVUkDDb39PKrorfE4lC4uNwAhi3c Dg72PbQhUpYpQRqfurHsoIXor8h6xOEcpvNdeteF/19PtaB9xfbJz3cZo9HKtEhtf7Zw LRbyx4gejmaiqsyNa1buWuSfJVLw3VfGH4Y2JzVmDR5RB4kAq3CksV/9g3BiNMTNJe+5 kIpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dRQHmDFo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x198si8021238oif.108.2020.02.03.11.05.06; Mon, 03 Feb 2020 11:05:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dRQHmDFo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729764AbgBCQ3T (ORCPT + 99 others); Mon, 3 Feb 2020 11:29:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:41164 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729755AbgBCQ3Q (ORCPT ); Mon, 3 Feb 2020 11:29:16 -0500 Received: from localhost (unknown [104.132.45.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A0C5E20CC7; Mon, 3 Feb 2020 16:29:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580747356; bh=f6/Ccx65vDvsW4yhp/ZsExnlynZ3BNePDvYFyDnc58U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dRQHmDFo8bumOI4g6G2MXzzM5MWVQya3gthTUnwpi2L/mbasRVFYew6tW1QzFTUgX quQoO4Q0T82uJxoc+lEidoix1yGVTQ8r8nKKR7qTMt+VlBSOkfUtfCLoqVjgeofGuD GHKvNZJGX1yzKnbzUTck8XtAH7AMUz3X2a1jnr54= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+2eeef62ee31f9460ad65@syzkaller.appspotmail.com, Zhenzhong Duan , Arnd Bergmann Subject: [PATCH 4.14 49/89] ttyprintk: fix a potential deadlock in interrupt context issue Date: Mon, 3 Feb 2020 16:19:34 +0000 Message-Id: <20200203161923.502078294@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200203161916.847439465@linuxfoundation.org> References: <20200203161916.847439465@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zhenzhong Duan commit 9a655c77ff8fc65699a3f98e237db563b37c439b upstream. tpk_write()/tpk_close() could be interrupted when holding a mutex, then in timer handler tpk_write() may be called again trying to acquire same mutex, lead to deadlock. Google syzbot reported this issue with CONFIG_DEBUG_ATOMIC_SLEEP enabled: BUG: sleeping function called from invalid context at kernel/locking/mutex.c:938 in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 0, name: swapper/1 1 lock held by swapper/1/0: ... Call Trace: dump_stack+0x197/0x210 ___might_sleep.cold+0x1fb/0x23e __might_sleep+0x95/0x190 __mutex_lock+0xc5/0x13c0 mutex_lock_nested+0x16/0x20 tpk_write+0x5d/0x340 resync_tnc+0x1b6/0x320 call_timer_fn+0x1ac/0x780 run_timer_softirq+0x6c3/0x1790 __do_softirq+0x262/0x98c irq_exit+0x19b/0x1e0 smp_apic_timer_interrupt+0x1a3/0x610 apic_timer_interrupt+0xf/0x20 See link https://syzkaller.appspot.com/bug?extid=2eeef62ee31f9460ad65 for more details. Fix it by using spinlock in process context instead of mutex and having interrupt disabled in critical section. Reported-by: syzbot+2eeef62ee31f9460ad65@syzkaller.appspotmail.com Signed-off-by: Zhenzhong Duan Cc: Arnd Bergmann Cc: Greg Kroah-Hartman Link: https://lore.kernel.org/r/20200113034842.435-1-zhenzhong.duan@gmail.com Signed-off-by: Greg Kroah-Hartman --- drivers/char/ttyprintk.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) --- a/drivers/char/ttyprintk.c +++ b/drivers/char/ttyprintk.c @@ -18,10 +18,11 @@ #include #include #include +#include struct ttyprintk_port { struct tty_port port; - struct mutex port_write_mutex; + spinlock_t spinlock; }; static struct ttyprintk_port tpk_port; @@ -100,11 +101,12 @@ static int tpk_open(struct tty_struct *t static void tpk_close(struct tty_struct *tty, struct file *filp) { struct ttyprintk_port *tpkp = tty->driver_data; + unsigned long flags; - mutex_lock(&tpkp->port_write_mutex); + spin_lock_irqsave(&tpkp->spinlock, flags); /* flush tpk_printk buffer */ tpk_printk(NULL, 0); - mutex_unlock(&tpkp->port_write_mutex); + spin_unlock_irqrestore(&tpkp->spinlock, flags); tty_port_close(&tpkp->port, tty, filp); } @@ -116,13 +118,14 @@ static int tpk_write(struct tty_struct * const unsigned char *buf, int count) { struct ttyprintk_port *tpkp = tty->driver_data; + unsigned long flags; int ret; /* exclusive use of tpk_printk within this tty */ - mutex_lock(&tpkp->port_write_mutex); + spin_lock_irqsave(&tpkp->spinlock, flags); ret = tpk_printk(buf, count); - mutex_unlock(&tpkp->port_write_mutex); + spin_unlock_irqrestore(&tpkp->spinlock, flags); return ret; } @@ -172,7 +175,7 @@ static int __init ttyprintk_init(void) { int ret = -ENOMEM; - mutex_init(&tpk_port.port_write_mutex); + spin_lock_init(&tpk_port.spinlock); ttyprintk_driver = tty_alloc_driver(1, TTY_DRIVER_RESET_TERMIOS |