From patchwork Wed Dec 10 18:38:20 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: 42097 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f71.google.com (mail-wg0-f71.google.com [74.125.82.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id B31F626C75 for ; Wed, 10 Dec 2014 18:41:40 +0000 (UTC) Received: by mail-wg0-f71.google.com with SMTP id l18sf2342318wgh.2 for ; Wed, 10 Dec 2014 10:41:40 -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:cc: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=c+ErNdIjQiV2mXtUmI2mFNnfWdRAr6tG/JGDJvo/BnI=; b=HCz6Dw6OanpzcChx97U5lo7w1L6vHysm5ZFQB/a/hbnKg/LPKaj7ReV3tdLuOjU77O McTUNiy3sdJWiBrOpvZSOTBK7ty5aWQVOExvMVOGbbRWtofnuyq0VV2flKp/QsoRzZww VU6EP2fdkkyz8bse9VdgyKwwRyVhy2x0GOU0egzw6vqjIinlw6xRl/V/Q+cEIUOrg1i5 LuGkmZd6LLgIxe+iNwIVtuvPhTYDvrDU9jkSCogFY/yWuvwm8nUIsslC4XHlpTExFpHp zpVKEKCoH77TMBFPG9tS/T4ZV22TV+OSvJtIUjPablJyg1Kgz77w2PIMI1bjD12pbKnf f3UQ== X-Gm-Message-State: ALoCoQnIdd7DIORnFhG5GjFZ4UZm0lcSZnNhlv1ylHZQxeGcp70PYGoR8SEhCLuKfZO6MoXWkKls X-Received: by 10.180.98.39 with SMTP id ef7mr5696283wib.6.1418236900059; Wed, 10 Dec 2014 10:41:40 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.42.197 with SMTP id q5ls215994lal.39.gmail; Wed, 10 Dec 2014 10:41:39 -0800 (PST) X-Received: by 10.112.166.101 with SMTP id zf5mr5800138lbb.42.1418236899794; Wed, 10 Dec 2014 10:41:39 -0800 (PST) Received: from mail-lb0-x229.google.com (mail-lb0-x229.google.com. [2a00:1450:4010:c04::229]) by mx.google.com with ESMTPS id tz3si321813lbb.66.2014.12.10.10.41.39 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 10 Dec 2014 10:41:39 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c04::229 as permitted sender) client-ip=2a00:1450:4010:c04::229; Received: by mail-lb0-f169.google.com with SMTP id p9so2864618lbv.0 for ; Wed, 10 Dec 2014 10:41:39 -0800 (PST) X-Received: by 10.112.170.36 with SMTP id aj4mr5519449lbc.3.1418236899697; Wed, 10 Dec 2014 10:41:39 -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 ru5csp292732lbb; Wed, 10 Dec 2014 10:41:38 -0800 (PST) X-Received: by 10.66.166.39 with SMTP id zd7mr9814248pab.11.1418236897835; Wed, 10 Dec 2014 10:41:37 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q5si7928948pdm.153.2014.12.10.10.41.37 for ; Wed, 10 Dec 2014 10:41:37 -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 S932833AbaLJSl1 (ORCPT + 27 others); Wed, 10 Dec 2014 13:41:27 -0500 Received: from nm42-vm2.bullet.mail.gq1.yahoo.com ([67.195.87.153]:59210 "EHLO nm42-vm2.bullet.mail.gq1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932414AbaLJSlZ (ORCPT ); Wed, 10 Dec 2014 13:41:25 -0500 Received: from [127.0.0.1] by nm42.bullet.mail.gq1.yahoo.com with NNFMP; 10 Dec 2014 18:41:24 -0000 Received: from [98.137.12.58] by nm42.bullet.mail.gq1.yahoo.com with NNFMP; 10 Dec 2014 18:38:23 -0000 Received: from [66.196.81.170] by tm3.bullet.mail.gq1.yahoo.com with NNFMP; 10 Dec 2014 18:38:23 -0000 Received: from [68.142.230.75] by tm16.bullet.mail.bf1.yahoo.com with NNFMP; 10 Dec 2014 18:38:23 -0000 Received: from [127.0.0.1] by smtp232.mail.bf1.yahoo.com with NNFMP; 10 Dec 2014 18:38:23 -0000 X-Yahoo-Newman-Id: 549234.47065.bm@smtp232.mail.bf1.yahoo.com X-Yahoo-Newman-Property: ymail-4 X-YMail-OSG: 3rcE7SYVM1nHIMOc2NrC9i0qJzyoDwR0tHOwKVKy85CYE3E ZCkpRa09opNLohZMa6BJrrApEKjXSyAR8Zyoah6MSRfQb5PLZPTWALcG9iz2 ed0WchZqCfvjKN1EJx9OeIJdR4gA0HYIecmJNz3qk9fmVDG0iWB5VaTUM_f0 EkMQrkwaRgXThsAgeXhWemm3E0Ndjr1vqw1XdaaqRSzsTDaG6t_mCP2h9uAz tEDKg1beG94mjETlVRrijtkP1inExXvjXAh7pYMArXiAd.7m8B1cgJHoN.mm _kfxRVAD4w6q9K0rBnrL4iFUcQC0TiqRiN4onbpcvcqazY8PpsY8OcmiuXoM Vcu3AarvEkoC6YoIAKNcBbn5.e9eWU8Ckgs1JZlucih9y1n5rwDKL0qt8L9e 4YbM21e._vYfsX.fUZjmxXgV9_2GwLLIXBN0PL6RfkMEiKQQv50qLUz8hGh3 fbdBGk_1lNmLkPr8RRb7Ba2mH_d044zyAGdnnwZZ4U0GJoxf2.MKjrQlDVNT b0H7GhRQ3hUqJO8UF6oU.otxMN2qnrIKAfs4wOHcSIYkZbdX.dstUsPVawpi EUeorL3NXhToXghX5yQOnyOJ76AV4JcYp X-Yahoo-SMTP: erUHay2swBAHy3fhG3khiNpFvMLWTM_N Message-ID: <5488931C.4040308@yahoo.ca> Date: Wed, 10 Dec 2014 13:38:20 -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: gregkh@linuxfoundation.org, jslaby@suse.cz, jmmahler@gmail.com CC: 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:c04::229 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 for 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; }