From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796374922; b=5c7TUdKzzuUPX0Qt+sEWH5w+9LZ+PMiBIB69hDVGsiZCJ6Xca+L2mq1o43XJYI/ANn6C m1W8qvQawwvEL/tz9UIZub5cALOVVA9Efc9+3qBTmlbIHlfZEV2gZ/WEfGwhHH037b1is aAuh7JB+nSzBqDir+1WerYn6o5No9R3ZtD+sSGtvuUybXR2xT/MooERbJPCOW4qr5fAP7 FFqx6/Up6u5ETDIZjVr2DFgcfctS3A1AytCBiIpYJBDOyAmSL15yeciB1lCLeZcbg59HZ GC7fTGGK8nt5VqgfxRpLv8cmjx04V2qP0Wd62scUXnpwmx6HjvAyoPOpyjzCyGabWVQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796374922; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=gx3H7b1ABR5OTxvfqUAm3qmfv708INsi0+c0FLcTrP8=; b=FaB9KHor29HL1p+NcnsbFrYtlppjGSLfuZzNb+P3ong56lKeyVLwZqtsQUzJ5iZW1qLw hz3LYTarczDewgFG5JiDtc3EbZt8oBWbyeIwbNN3S/Yhz4tDlDQCN0mLy83q9blxLhu2Y B/den+9Yp+ZhWv22XmBqUnplJPMmUboKCo4DSuEAjvKr99g80WUY4ogSK4BQbiV8yFHAU UHrixPcpa6R2JVJDP3RvpcgfK4rGv0EDvS9cY6+aLi90IwMkk0BOfBuIrtBCdoBCzi9wW up3401YzO1Qj+0bWGfe+ckh/gsF6uWyewvFM5rANnYbk/syU1rWAlPDfI21FfgZS5fg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=z4qedSSu; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EAC27606A69E; Fri, 4 Dec 2026 09:02:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=z4qedSSu Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CDF41F00A08; Fri, 4 Dec 2026 09:02:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796374921; bh=gx3H7b1ABR5OTxvfqUAm3qmfv708INsi0+c0FLcTrP8=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=z4qedSSuDW+36VhEhKoBbApUc9Q9PF6/k7EmINo8zUKagpYD0FeTm70S+h6KbL8bN XYEt3+9K0hrQSvhvU1NxEvJUAZgJC+V7Z4/9f2grIt/i0qRUYRBLtXHWsT9dOzWOcY kcXpZU42mb6Ahce0b8tdwQSDhX8+Azfib1l13M5w= Date: Fri, 4 Dec 2026 17:47:58 +0900 From: Greg Kroah-Hartman To: Jiayi Zhang Cc: Jiri Slaby , Ilpo =?iso-8859-1?Q?J=E4rvinen?= , Andy Shevchenko , linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+0479f67e520e4910d370@syzkaller.appspotmail.com, syzbot+2430ce6b487fe0119d03@syzkaller.appspotmail.com, syzkaller-bugs@googlegroups.com Subject: Re: [PATCH] tty: n_tty: hold termios_rwsem in n_tty_lookahead_flow_ctrl() Message-ID: <2026120429-sharper-approval-9326@gregkh> References: <20261202180854.4154340-1-zhangjiayi1014@gmail.com> X-Mailing-List: linux-serial@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261202180854.4154340-1-zhangjiayi1014@gmail.com> On Wed, Dec 02, 2026 at 06:08:54PM +0000, Jiayi Zhang wrote: > syzbot reports a data race between the XON/XOFF lookahead and TCSETS: > > BUG: KCSAN: data-race in n_tty_lookahead_flow_ctrl / tty_set_termios > > write to 0xffff88811a4798e0 of 44 bytes by task 9666 on cpu 1: > tty_set_termios+0xc0/0x900 drivers/tty/tty_ioctl.c:335 > set_termios+0x36e/0x4b0 drivers/tty/tty_ioctl.c:516 > ... > read to 0xffff88811a4798fa of 1 bytes by task 9026 on cpu 0: > n_tty_is_char_flow_ctrl drivers/tty/n_tty.c:1196 [inline] > n_tty_receive_char_flow_ctrl drivers/tty/n_tty.c:1217 [inline] > n_tty_lookahead_flow_ctrl+0x1b0/0x2d0 drivers/tty/n_tty.c:1492 > tty_port_default_lookahead_buf+0x91/0xc0 drivers/tty/tty_port.c:59 > lookahead_bufs drivers/tty/tty_buffer.c:435 [inline] > flush_to_ldisc+0x28b/0x350 drivers/tty/tty_buffer.c:505 > > and a second one between the lookahead and a hangup: > > BUG: KCSAN: data-race in n_tty_flush_buffer / n_tty_lookahead_flow_ctrl > > read-write to 0xffffc90002eed270 of 8 bytes by task 368 on cpu 1: > n_tty_lookahead_flow_ctrl+0x52/0x2d0 drivers/tty/n_tty.c:1483 > tty_port_default_lookahead_buf+0x91/0xc0 drivers/tty/tty_port.c:59 > ... > write to 0xffffc90002eed270 of 8 bytes by task 19255 on cpu 0: > reset_buffer_flags drivers/tty/n_tty.c:322 [inline] > n_tty_flush_buffer+0xa8/0x180 drivers/tty/n_tty.c:350 > tty_ldisc_hangup+0x77/0x480 drivers/tty/tty_ldisc.c:699 > ... > > n_tty protects the termios and its buffer state with tty->termios_rwsem. > tty_set_termios() replaces tty->termios and n_tty_flush_buffer() resets > ldata->lookahead_count with the semaphore held for writing, and > n_tty_receive_buf_common() holds it for reading while it processes > received characters. n_tty_lookahead_flow_ctrl() reads the termios > (I_IXON(), START_CHAR(), STOP_CHAR()) and updates lookahead_count > without the semaphore. So the lookahead can see a TCSETS half done and > start or stop the tty on a character that is no longer a flow control > character. Its update of lookahead_count can also undo the reset done > by a concurrent n_tty_flush_buffer(). For tcflush(TCIFLUSH), tty_ioctl() > first empties the flip buffer, and the ldisc flush then resets the > count, so a lost reset keeps counting characters that were discarded. > The receive path then treats that many new characters as already > handled by the lookahead and skips their flow control action. > > Take termios_rwsem for reading in n_tty_lookahead_flow_ctrl(), as > n_tty_receive_buf_common() does. Both are called from flush_to_ldisc() > with port->buf.lock held, so this adds no new lock order. > > Fixes: 6bb6fa6908eb ("tty: Implement lookahead to process XON/XOFF timely") > Reported-by: syzbot+0479f67e520e4910d370@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=0479f67e520e4910d370 > Reported-by: syzbot+2430ce6b487fe0119d03@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=2430ce6b487fe0119d03 > Signed-off-by: Jiayi Zhang > --- > Both syzbot reports are still in moderation and were not sent to the > list; the Closes: links show them. > > Tested in QEMU on 946d9ed1aa02 with the syzbot C reproducer and the > kernel config of the first report. The second report has no reproducer > and was not tested. The reproducer runs for 5 seconds and does not > turn KCSAN on (the config does not set CONFIG_KCSAN_EARLY_ENABLE), so > for the test it ran for 9 minutes, wrote "on" to > /sys/kernel/debug/kcsan, and filtered four unrelated races > (move_linked_works, queue_work_on, process_one_work, ksys_read). > Without the patch 5 of 5 instances reported this race within seconds; > with the patch none of 5 did. Three of the patched instances later > reported a race between copy_from_read_buf() and > n_tty_receive_buf_common(), which this patch does not touch. > > AI tools assisted with the analysis, the code and the changelog. > > drivers/tty/n_tty.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c > index e6a0f5b..725d6ed 100644 > --- a/drivers/tty/n_tty.c > +++ b/drivers/tty/n_tty.c > @@ -1480,6 +1480,8 @@ static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp, > struct n_tty_data *ldata = tty->disc_data; > u8 flag = TTY_NORMAL; > > + guard(rwsem_read)(&tty->termios_rwsem); > + > ldata->lookahead_count += count; > > if (!I_IXON(tty)) > -- > 2.53.0 > > What changed to cause this to be needed now? And why the "private" syzbot reports? thanks, greg k-h