From patchwork Wed Dec 10 18:32:31 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: 'Timothy Arceri' via Patchwork Forward X-Patchwork-Id: 42096 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id F285826C75 for ; Wed, 10 Dec 2014 18:41:27 +0000 (UTC) Received: by mail-lb0-f200.google.com with SMTP id f15sf2383688lbj.11 for ; Wed, 10 Dec 2014 10:41:27 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:message-id:date:from:user-agent :mime-version:to:subject:content-type:content-transfer-encoding :sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe:reply-to; bh=drj0cEa5uZpGImyQXzm+ypAqEUTmQDNIqcA7JOPW9bg=; b=mfC7Uu/I6LRCGxhkTg57+dVTxSKAL3fM/4/3fFww22KPt2NuTtTIF1vwm3GCDaCURU bn2PwCLXACQY029ta8Q1q/bYzgyuGBZoszEU4h6B0Xgz0C4aqaYnh7bauGOsSGo2tW7b +4gCYJOn4CihQIFs8/x1LsZ9WNmMa385o472AxC/lImjTq2bUiyxJ2B3N55vpItsfaAE axvYznVcmdGOuU3nke0PDF9oMYVS+UUtlEhi2eHFy8o0W2qfyws1ScbQY//Tz+9jFj1H L7XHPHKsmTO/u4f9YbZmsdFh54qsXk98Ic8MoWM+IqTpNAb6j7ZpyK2hOec4ZH3dv7yM l93g== X-Gm-Message-State: ALoCoQnnorn1vNhqYfEvfQJk3RR5nOPKgnnMjEsX9UvEppsnq/vjLMZoVk1fj5mk3mhFghHmYFYf X-Received: by 10.112.151.38 with SMTP id un6mr979113lbb.0.1418236886946; Wed, 10 Dec 2014 10:41:26 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.23.39 with SMTP id j7ls213665laf.92.gmail; Wed, 10 Dec 2014 10:41:26 -0800 (PST) X-Received: by 10.112.16.168 with SMTP id h8mr5539918lbd.41.1418236886512; Wed, 10 Dec 2014 10:41:26 -0800 (PST) Received: from mail-la0-x22a.google.com (mail-la0-x22a.google.com. [2a00:1450:4010:c03::22a]) by mx.google.com with ESMTPS id qi2si5484904lbb.47.2014.12.10.10.41.26 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 10 Dec 2014 10:41:26 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::22a as permitted sender) client-ip=2a00:1450:4010:c03::22a; Received: by mail-la0-f42.google.com with SMTP id gd6so2998460lab.1 for ; Wed, 10 Dec 2014 10:41:26 -0800 (PST) X-Received: by 10.152.87.100 with SMTP id w4mr5506671laz.71.1418236886379; Wed, 10 Dec 2014 10:41:26 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.142.69 with SMTP id ru5csp292675lbb; Wed, 10 Dec 2014 10:41:25 -0800 (PST) X-Received: by 10.70.102.145 with SMTP id fo17mr10081166pdb.48.1418236884553; Wed, 10 Dec 2014 10:41:24 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id xp4si8060671pbb.36.2014.12.10.10.41.23 for ; Wed, 10 Dec 2014 10:41:24 -0800 (PST) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932773AbaLJSlV (ORCPT + 27 others); Wed, 10 Dec 2014 13:41:21 -0500 Received: from nm32-vm7.bullet.mail.ne1.yahoo.com ([98.138.229.55]:48987 "EHLO nm32-vm7.bullet.mail.ne1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932414AbaLJSlU (ORCPT ); Wed, 10 Dec 2014 13:41:20 -0500 X-Greylist: delayed 361 seconds by postgrey-1.27 at vger.kernel.org; Wed, 10 Dec 2014 13:41:20 EST Received: from [127.0.0.1] by nm32.bullet.mail.ne1.yahoo.com with NNFMP; 10 Dec 2014 18:35:17 -0000 Received: from [98.138.100.115] by nm32.bullet.mail.ne1.yahoo.com with NNFMP; 10 Dec 2014 18:32:34 -0000 Received: from [98.139.215.143] by tm106.bullet.mail.ne1.yahoo.com with NNFMP; 10 Dec 2014 18:32:34 -0000 Received: from [98.139.211.193] by tm14.bullet.mail.bf1.yahoo.com with NNFMP; 10 Dec 2014 18:32:34 -0000 Received: from [127.0.0.1] by smtp202.mail.bf1.yahoo.com with NNFMP; 10 Dec 2014 18:32:33 -0000 X-Yahoo-Newman-Id: 980847.46211.bm@smtp202.mail.bf1.yahoo.com X-Yahoo-Newman-Property: ymail-4 X-YMail-OSG: csCznGQVM1nR0NBu71P6o85ogX9fZKbIZMAequzTf0aNKpE k1l.apg0ZA4Ta6x6LakDWiVhev.rwkhASe0SGH3VueT1iYUHlVa07MrHQHqS vrkkgByXS_BsBKOWrv4WT2tzJSoB6YWvX0JhksJKBI.vkI7zuVl6rKtvKWx1 IcIeV05FDNRw3hFoWQGSnEDjq1TCAC2EHfkWTh9SDVNEhCWoTGJWWEBK_Hy0 nXuakVnZpZvqgSndxHTBeQnLPKFHV8_4ZdiqkcXlslz3bLzxFf1kd4xWl8Ii 4zB9cylCbjY8CUS._lqqXtjhktgHNSZFoxVwxDIkbSlop0O17H2adZPvejJM VxAK1rvPbHvgDXMmT0pWjZnPnoEyWYeQsHnkXb6h_nJK4PW8tiGibiXwSONC tFe7qoo_Ws6jIuHVPLtOjkCaO6uW9enQM0yJMUbMwrqt0xLVUFKJdM8H9hPG _tjEXNy5p9BksKSQ6tH4Q_xMCU4umLJd7Ffy9hTBILguDAjxRTjs7GI5iZZt RVwXPhoPYBJHInBHdkHPV6CyxtO2adrvaQeWhRNM8fNNa5gHTzPGdGxw8vgb 8DjjS_fI4nInpxpWIfCWuXPDvnx9d4RkS X-Yahoo-SMTP: erUHay2swBAHy3fhG3khiNpFvMLWTM_N Message-ID: <548891BF.9020108@yahoo.ca> Date: Wed, 10 Dec 2014 13:32:31 -0500 From: "'Denis Du' via Patchwork Forward" User-Agent: Mozilla/5.0 (X11; Linux i686 on x86_64; rv:24.0) Gecko/20100101 Icedove/24.5.0 MIME-Version: 1.0 To: linux-kernel@vger.kernel.org Subject: [PATCH] TTY: Fix the missing lock for the TTY ldisc buffer X-Forwarded-Message-Id: Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Original-Sender: dudenis2000@yahoo.ca X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::22a as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (body hash did not verify) header.i=@; dmarc=fail (p=NONE dis=NONE) header.from=yahoo.ca Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , X-Original-From: Denis Du Reply-To: Denis Du Hi, Guys: It was found that the 3.12 kernel tty layer will lose or corrupt data when have a full-duplex communication, especially in high baud rate, for example 230k for my OMAP5 uart. Eventually I found there is lock missing between copy data to ldisc layer buffer and copy data from the same buffer to user space. I believe this issue existed since 3.8 kernel(since this kernel , it start to remove most of the spin-locks) and I didn't find any fix even through 3.17 kernel. This patch was tested to be works great with no any data loss again on 3.12 kernel. This patch was built fro the latest kernel, but I cannot test it.Somebody may give a test. I did try to use the existed lock atomic_read_lock, but it doesn’t work. Signed-off-by: Hui Du --- drivers/tty/n_tty.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 2e900a9..6e5c6ae 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -125,6 +125,7 @@ struct n_tty_data { struct mutex atomic_read_lock; struct mutex output_lock; + struct mutex read_buf_lock; }; static inline size_t read_cnt(struct n_tty_data *ldata) @@ -1691,7 +1692,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp, int room, n, rcvd = 0; down_read(&tty->termios_rwsem); - + mutex_lock(&ldata->read_buf_lock); while (1) { room = receive_room(tty); n = min(count, room); @@ -1710,6 +1711,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp, tty->receive_room = room; n_tty_check_throttle(tty); + mutex_unlock(&ldata->read_buf_lock); up_read(&tty->termios_rwsem); return rcvd; @@ -1876,6 +1878,7 @@ static int n_tty_open(struct tty_struct *tty) ldata->overrun_time = jiffies; mutex_init(&ldata->atomic_read_lock); mutex_init(&ldata->output_lock); + mutex_init(&ldata->read_buf_lock); tty->disc_data = ldata; reset_buffer_flags(tty->disc_data); @@ -1939,6 +1942,7 @@ static int copy_from_read_buf(struct tty_struct *tty, size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); retval = 0; + mutex_lock(&ldata->read_buf_lock); n = min(read_cnt(ldata), N_TTY_BUF_SIZE - tail); n = min(*nr, n); if (n) { @@ -1954,6 +1958,7 @@ static int copy_from_read_buf(struct tty_struct *tty, *b += n; *nr -= n; } + mutex_unlock(&ldata->read_buf_lock); return retval; } @@ -1992,6 +1997,7 @@ static int canon_copy_from_read_buf(struct tty_struct *tty, bool eof_push = 0; /* N.B. avoid overrun if nr == 0 */ + mutex_lock(&ldata->read_buf_lock); n = min(*nr, read_cnt(ldata)); if (!n) return 0; @@ -2052,6 +2058,7 @@ static int canon_copy_from_read_buf(struct tty_struct *tty, ldata->push = 0; tty_audit_push(tty); } + mutex_unlock(&ldata->read_buf_lock); return eof_push ? -EAGAIN : 0; }