From patchwork Wed Dec 10 18:26:34 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: 42095 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f70.google.com (mail-wg0-f70.google.com [74.125.82.70]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 93C9526C75 for ; Wed, 10 Dec 2014 18:31:15 +0000 (UTC) Received: by mail-wg0-f70.google.com with SMTP id b13sf2332600wgh.5 for ; Wed, 10 Dec 2014 10:31:14 -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=evON4YrMp4f13VKGyPsn8QxmjHARoNROjAQZa6KNNtU=; b=apT900N40nXjSJoHgqkX/Ato5eXU4j6nqPuEoGyO8WTlP5OC3YTUfCsBvwoKBebw36 uYuAB1smjmivSfOSm5oXE9sJKVonPvl3Oj3+SOWRarqTNdR9NhdgJkGmzupWHZqeANdr 5T/8d2Oz6pOrgk3csfbv1qsnbZ7RIbV4jSghfSdbERMklC6TFwT2xbthS2H7gfPVVxBZ hqxUmpuAfgeSMg8u2jJUJxYgdYSslQN1j5qB4qVC3TefLoJhTH9k9daHnqiJWUAI3k8U 4pV8hMCVfbPsfk70YrL4/QA6u4Bt/Wetj3dXO7VeokjR4E/sQ7Ss1kraeOavjcjXnyvg 5jfA== X-Gm-Message-State: ALoCoQkd4mBHV29osxeKqjrzYo1oUIGSKiUeHcAEdzkyjTSJz1gZlMGDhnmyG8PVr1frv4wxAjFp X-Received: by 10.112.32.163 with SMTP id k3mr106904lbi.17.1418236274852; Wed, 10 Dec 2014 10:31:14 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.7.193 with SMTP id l1ls224437laa.79.gmail; Wed, 10 Dec 2014 10:31:14 -0800 (PST) X-Received: by 10.152.164.232 with SMTP id yt8mr5550882lab.7.1418236274677; Wed, 10 Dec 2014 10:31:14 -0800 (PST) Received: from mail-la0-x232.google.com (mail-la0-x232.google.com. [2a00:1450:4010:c03::232]) by mx.google.com with ESMTPS id to1si5384377lbb.133.2014.12.10.10.31.14 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 10 Dec 2014 10:31:14 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::232 as permitted sender) client-ip=2a00:1450:4010:c03::232; Received: by mail-la0-f50.google.com with SMTP id pn19so2980979lab.9 for ; Wed, 10 Dec 2014 10:31:14 -0800 (PST) X-Received: by 10.152.23.9 with SMTP id i9mr5470131laf.81.1418236274587; Wed, 10 Dec 2014 10:31:14 -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 ru5csp290192lbb; Wed, 10 Dec 2014 10:31:13 -0800 (PST) X-Received: by 10.68.246.164 with SMTP id xx4mr9873396pbc.64.1418236272779; Wed, 10 Dec 2014 10:31:12 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ex2si7762500pbb.231.2014.12.10.10.31.11 for ; Wed, 10 Dec 2014 10:31:12 -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 S932805AbaLJSbJ (ORCPT + 27 others); Wed, 10 Dec 2014 13:31:09 -0500 Received: from nm44-vm6.bullet.mail.gq1.yahoo.com ([67.195.87.29]:37287 "EHLO nm44-vm6.bullet.mail.gq1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932479AbaLJSbH (ORCPT ); Wed, 10 Dec 2014 13:31:07 -0500 Received: from [127.0.0.1] by nm44.bullet.mail.gq1.yahoo.com with NNFMP; 10 Dec 2014 18:31:07 -0000 Received: from [216.39.60.181] by nm44.bullet.mail.gq1.yahoo.com with NNFMP; 10 Dec 2014 18:28:09 -0000 Received: from [66.196.81.173] by tm17.bullet.mail.gq1.yahoo.com with NNFMP; 10 Dec 2014 18:28:08 -0000 Received: from [68.142.230.69] by tm19.bullet.mail.bf1.yahoo.com with NNFMP; 10 Dec 2014 18:28:08 -0000 Received: from [127.0.0.1] by smtp226.mail.bf1.yahoo.com with NNFMP; 10 Dec 2014 18:28:08 -0000 X-Yahoo-Newman-Id: 768424.35416.bm@smtp226.mail.bf1.yahoo.com X-Yahoo-Newman-Property: ymail-4 X-YMail-OSG: BQIAeIsVM1kDVhzZWTBvhgMJd4PB1GDAxygVHqYIg8Pv4rk 3_nDJ_seAXRC.Bh2.RbM9i9Eu.78wjtnx1ZYzjdnOi5F0FHpwTy9L0RIKBCo hpusTz0P9iFb6GAiylASrqj8YtLRCDiWXSiodHXkdgrU3X0wUNAffM1nyXh8 llAhlRCcxm5mvWpSS2wmq5A.PufOP9cpgShtREzrSq8IvaM80zA0kvlTT.NC ZBqRJkHGRLQNtBzTH87OmtHPLVXrqAFby7uhX07i6Vm7lnUasId9skGbcaWs HleO5xgZ2o5.ZUjp8pLTyB_y6G1TabED6WLefbDbQcOGN77_1lTIc.._BhlU obfIirPWOVH9CeftylIB3.Hw73A6TGCjap0Bfqm3achdoyoQOl7ys9eqHj9z rZ5Y9Q2ZCBHO2HTF58E0NZ4tafciZFkDfeX5C_Kz3p8aVsdX4zzcKTshCoSe OXmBGS2NNWd7nYGPZ9cDnWIsxkaHHQzTl8icowpyrG9REJ4rHOZHf1dYDsl9 ub4_KkM4jrH9tcd.MufBEgAZ5.JmwmbZW_eRQraRNqUw70UqipELNF3DO4CS psfxX4trkvDXO53Un0uyytsQXI1W_99urO6g- X-Yahoo-SMTP: erUHay2swBAHy3fhG3khiNpFvMLWTM_N Message-ID: <5488905A.3060606@yahoo.ca> Date: Wed, 10 Dec 2014 13:26:34 -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, dudenis2000@yahoo.ca Subject: [PATCH] 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::232 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; }