From patchwork Fri Dec 5 20:16:47 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: 41986 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f197.google.com (mail-lb0-f197.google.com [209.85.217.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 47E8F26667 for ; Fri, 5 Dec 2014 20:25:19 +0000 (UTC) Received: by mail-lb0-f197.google.com with SMTP id n15sf887110lbi.8 for ; Fri, 05 Dec 2014 12:25:18 -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:date:from:reply-to:to:cc:message-id :in-reply-to:references:subject:mime-version:content-type :content-length:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=HH7ON8vs/9NoSci0vEzMLeuXJHs4GZ+mNydVBcvSSLo=; b=elMzOUWHbynaJK0AdC6r4SNGxgH7IZ6sXMeMcMnyQRMImAhbpNI2ccfmql392peRX9 aQZsDCuSdJf/9JoN/G3BFWFMU1Reb0DA2VWRzsFks9zCqKm8kN6ARgzLgPJ8TZ3q28Vs uC3rPUOsSym3aht4mpa9i46dYUBKvKrCQOd6xPA0ay1R40Zprvg2yYb/tO+KPYF3PqnJ utrrYgN21HdUFLBJ40xZlHHpjtU3aevh/b0PcCyshaePEovrXbmZkUO3BcG2ZDEFUS0u 4ezn9r/wy4kpz3l0H4LZYVTbeNZQzPZr1vnsR7H0smxnijxzO8MRKuIPEZEKaGMDCkAN efbQ== X-Gm-Message-State: ALoCoQkYKV6/F8CCNeEOJK//vC9XeK+2WwDkcDGVzIbt7fr3eLLA4nh3nJ/MNNg/G3irX/qX9Zpt X-Received: by 10.194.134.193 with SMTP id pm1mr844234wjb.4.1417811118062; Fri, 05 Dec 2014 12:25:18 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.45.33 with SMTP id j1ls461794lam.59.gmail; Fri, 05 Dec 2014 12:25:17 -0800 (PST) X-Received: by 10.152.37.6 with SMTP id u6mr4743085laj.74.1417811117579; Fri, 05 Dec 2014 12:25:17 -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 zl4si14166603lbb.125.2014.12.05.12.25.17 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 05 Dec 2014 12:25:17 -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 p9so1171791lbv.28 for ; Fri, 05 Dec 2014 12:25:17 -0800 (PST) X-Received: by 10.112.52.37 with SMTP id q5mr4627494lbo.32.1417811117392; Fri, 05 Dec 2014 12:25:17 -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.184.201 with SMTP id ew9csp278081lbc; Fri, 5 Dec 2014 12:25:15 -0800 (PST) X-Received: by 10.70.103.37 with SMTP id ft5mr31471372pdb.159.1417811115207; Fri, 05 Dec 2014 12:25:15 -0800 (PST) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r14si23166511pdj.233.2014.12.05.12.25.14 for ; Fri, 05 Dec 2014 12:25:15 -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 S1751529AbaLEUZL (ORCPT + 28 others); Fri, 5 Dec 2014 15:25:11 -0500 Received: from nm7.bullet.mail.bf1.yahoo.com ([98.139.212.166]:46009 "EHLO nm7.bullet.mail.bf1.yahoo.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750748AbaLEUZJ (ORCPT ); Fri, 5 Dec 2014 15:25:09 -0500 X-Greylist: delayed 499 seconds by postgrey-1.27 at vger.kernel.org; Fri, 05 Dec 2014 15:25:08 EST Received: from [98.139.212.151] by nm7.bullet.mail.bf1.yahoo.com with NNFMP; 05 Dec 2014 20:16:48 -0000 Received: from [98.139.212.204] by tm8.bullet.mail.bf1.yahoo.com with NNFMP; 05 Dec 2014 20:16:48 -0000 Received: from [127.0.0.1] by omp1013.mail.bf1.yahoo.com with NNFMP; 05 Dec 2014 20:16:48 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 145526.9823.bm@omp1013.mail.bf1.yahoo.com X-YMail-OSG: IPojfKoVM1kSIqwJqCtGzNjcOekn8PPKUk8wlxefZjvS_Hs_bXH6cqDiQvxkPN6 Q4l86SWB85hN1S0Tr3OiIdZujn_AMqpfxepvtj2gfnysVwIEZ3Oq5HsXRlpQrs4t2smV4Q4wcuYR DRW_nIrfhBvDImqP3.2nxB5SpiQ1F29Vn2UWDpWvWF6BxZW5r9Uxc5rkjtWl_fwi41XlZqSGoqaU oMmOUV_Jk6uhkRUYehweQpga1WPdn1J_MIglAv1Wl0qd3DMHaWQRwKpAGnkTmFSlV5kV0Ur_6uuA m2Idg0fDo.sdOxlftrKXP2FDGEXLFWI7_aodb_CjOnJNvYpBh_mDcN0j6jZBJcccIzXmjHOc1TpJ LfujiQ5TAHU.n3NKW7wmPSQGTCq5DKjiyaXLEIcQno4ejbV23juXfqfe7GPBAg7JaZl2x2bE7kTb 6ltWP8suU7qpglJ0h8gcpUGcu.pXOOe..s__WG8Cgbbxf Received: by 66.196.80.146; Fri, 05 Dec 2014 20:16:47 +0000 Date: Fri, 5 Dec 2014 20:16:47 +0000 (UTC) From: "'Denis Du' via Patchwork Forward" Reply-To: Denis Du To: "gregkh@linuxfoundation.org" , "jslaby@suse.cz" Cc: "linux-kernel@vger.kernel.org" Message-ID: <345838865.5722597.1417810607255.JavaMail.yahoo@jws10603.mail.bf1.yahoo.com> In-Reply-To: <950568161.5257116.1417730252670.JavaMail.yahoo@jws10613.mail.bf1.yahoo.com> References: <950568161.5257116.1417730252670.JavaMail.yahoo@jws10613.mail.bf1.yahoo.com> Subject: TTY layer need a lock to copy data to user space MIME-Version: 1.0 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 Hi, Guys: It was found that the 3.12 kernel tty layer will lose or corrupt data when have a full-duplex communication, especcially in high baudrate, 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. I did try to use the existed lock atomic_read_lock, but it doesn’t work. Denis Du --- drivers/tty/n_tty.c-old 2014-10-16 16:39:35.909350338 -0400 +++ drivers/tty/n_tty.c 2014-10-16 16:49:00.004930469 -0400 @@ -124,6 +124,7 @@ 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) @@ -1686,9 +1687,11 @@ char *fp, int count) { int room, n; + struct n_tty_data *ldata = tty->disc_data; down_read(&tty->termios_rwsem); + mutex_lock(&ldata->read_buf_lock); while (1) { room = receive_room(tty); n = min(count, room); @@ -1703,6 +1706,7 @@ tty->receive_room = room; n_tty_check_throttle(tty); + mutex_unlock(&ldata->read_buf_lock); up_read(&tty->termios_rwsem); } @@ -1713,7 +1717,7 @@ 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); @@ -1732,6 +1736,7 @@ tty->receive_room = room; n_tty_check_throttle(tty); + mutex_unlock(&ldata->read_buf_lock); up_read(&tty->termios_rwsem); return rcvd; @@ -1880,6 +1885,7 @@ 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); @@ -1945,6 +1951,8 @@ 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) { @@ -1960,6 +1968,7 @@ *b += n; *nr -= n; } + mutex_unlock(&ldata->read_buf_lock); return retval; } @@ -1990,6 +1999,8 @@ size_t tail; int ret, found = 0; bool eof_push = 0; + + mutex_lock(&ldata->read_buf_lock); /* N.B. avoid overrun if nr == 0 */ n = min(*nr, read_cnt(ldata)); @@ -2049,6 +2060,8 @@ ldata->line_start = ldata->read_tail; tty_audit_push(tty); } + + mutex_unlock(&ldata->read_buf_lock); return eof_push ? -EAGAIN : 0; }