From patchwork Wed Aug 16 10:58:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714315 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 65891C04FDF for ; Wed, 16 Aug 2023 11:00:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244227AbjHPK7w (ORCPT ); Wed, 16 Aug 2023 06:59:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244375AbjHPK7l (ORCPT ); Wed, 16 Aug 2023 06:59:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 969002D6B; Wed, 16 Aug 2023 03:59:12 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B6B3A6658C; Wed, 16 Aug 2023 10:58:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1D0BFC433C9; Wed, 16 Aug 2023 10:58:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183507; bh=4M+311SCFHjZNmmXmuQ/Cv4yRF1MEJ3AD4kmj9Cg3lk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PVCFNLPov+BxKElL+WEweAvekMzsgVZVuehO78//6u4KJGclRdgkTwas8CstpYjaU 5EoZLkbzfs6KkbGnHWsLBlc0rLayV3Vj3ybynQGEZf6neTdMEOI30JpAM2pmEs1zOC 83e1l7YVl3FhtScIUoWf60zJzqFx3MNSK8gBlQIX5+0PKsPagL9Qn7PxFjGbDEtXdN yxK9+8LyPMgYt4io0Ry58xAcRJtXy+v8ypS5NkPUhPgeG4mXrRC/Zg8Hz5WsUNWYsI oyN6TiVhmZ2H2b10XFqJn7vgmPfvDK3futPsDZwhUZv62rZ9K53juVRV1rVfzPMFO+ PIovGvTkiqfZQ== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 1/4] n_tty: drop fp from n_tty_receive_buf_real_raw() Date: Wed, 16 Aug 2023 12:58:05 +0200 Message-ID: <20230816105822.3685-2-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org The 'fp' parameter of n_tty_receive_buf_real_raw() is unused, so drop it. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 552e8a741562..1599012f20c8 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1502,7 +1502,7 @@ static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const unsigned cha static void n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp, - const char *fp, int count) + int count) { struct n_tty_data *ldata = tty->disc_data; size_t n, head; @@ -1597,7 +1597,7 @@ static void __receive_buf(struct tty_struct *tty, const unsigned char *cp, size_t la_count = min_t(size_t, ldata->lookahead_count, count); if (ldata->real_raw) - n_tty_receive_buf_real_raw(tty, cp, fp, count); + n_tty_receive_buf_real_raw(tty, cp, count); else if (ldata->raw || (L_EXTPROC(tty) && !preops)) n_tty_receive_buf_raw(tty, cp, fp, count); else if (tty->closing && !L_EXTPROC(tty)) { From patchwork Wed Aug 16 10:58:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714316 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1166FC001E0 for ; Wed, 16 Aug 2023 10:59:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230075AbjHPK7S (ORCPT ); Wed, 16 Aug 2023 06:59:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244348AbjHPK67 (ORCPT ); Wed, 16 Aug 2023 06:58:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC70C212B; Wed, 16 Aug 2023 03:58:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C0B9A665B6; Wed, 16 Aug 2023 10:58:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 25994C433C7; Wed, 16 Aug 2023 10:58:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183510; bh=CJX8SlHepZCFBu+LS4vVyP3RzICxchrVKsWPVGHPYpw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jQLi/dKO5eDtAfQ7NVObJGr/IZ+Iotd+HqX4/aGsyHk2O0kQ4dDH6zzWMaPUjDRBD K0Th3GCYdX5aBtiER4f9of3EHKdGUtqj0BtEvYbsY5PQPSn8hAKlzfot7WMVH/4Iln 67XMxEu1Cn9tPWaN8p/vqAOEQpJnEEsfwogR89/xBO3GK//mDSYFSZLqpdm742B0WU IJXZKafNxAw6z5osoWHLtKu3KVtClME8PkStSlQ7X+POnhoaxLQC/JnPXXqNdimrU5 eaCBgzWpixwvcHQ4BiyM6HKoXlQ+IqFs2WqckIePwPazNoSaBPjnN3qL3uXqyosN+Y 6GdKvJO02Vxww== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 2/4] n_tty: simplify and sanitize zero_buffer() Date: Wed, 16 Aug 2023 12:58:07 +0200 Message-ID: <20230816105822.3685-4-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org * Make 'tty' parameter const as we only look at tty flags here. * Make 'size' parameter of size_t type as everyone passes that and memset() (the consumer) expects size_t too. So be consistent. * Remove redundant local variables, place the content directly to the 'if'. * Use 0 instead of 0x00 in memset(). The former is more obvious. No functional changes expected. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 1599012f20c8..c1859ae263eb 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -158,13 +158,10 @@ static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i) } /* If we are not echoing the data, perhaps this is a secret so erase it */ -static void zero_buffer(struct tty_struct *tty, u8 *buffer, int size) +static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size) { - bool icanon = !!L_ICANON(tty); - bool no_echo = !L_ECHO(tty); - - if (icanon && no_echo) - memset(buffer, 0x00, size); + if (L_ICANON(tty) && !L_ECHO(tty)) + memset(buffer, 0, size); } static void tty_copy(struct tty_struct *tty, void *to, size_t tail, size_t n) From patchwork Wed Aug 16 10:58:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714312 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94A38C04FE0 for ; Wed, 16 Aug 2023 11:00:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244344AbjHPLAZ (ORCPT ); Wed, 16 Aug 2023 07:00:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244417AbjHPLAL (ORCPT ); Wed, 16 Aug 2023 07:00:11 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD04B26A5; Wed, 16 Aug 2023 03:59:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5E86B665C5; Wed, 16 Aug 2023 10:58:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B570AC433C7; Wed, 16 Aug 2023 10:58:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183514; bh=EWGu7CcKLTWJELHIckUZyO75GIdPiiaKcoynO1yju/A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D3zqzKDWxtK9qSFeomM1q+EaTFAaq4ZueHb3+DC/M1VHfOu3SE4MPqglkHOeSlrB2 gdQfg835K57wYNRN8yiJk4HIy0UgePSJw5r/999C0sZ0qMeTeKjAMK8nDGd6QZvfie Ka8NIVozbNs9D+BadhuhJv4GXRH/YQxKpJF93TX+4/IXwqBtd2q3uXPIGfi3dHC9kL 8xAAeV9fzmyFtq2a1STn7BBbhyIwOZmocs/e0QM6mC8MMIEBtL+fVPQX9zf4XZt3CE /0PeTIMFhzl7FvzfLFWgwn1ZaveJoYWj3rGDDUZ3M3ds06ZnxNnUkda060vVCnEi9g YmWvCGESdleNg== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 03/14] tty: n_tty: use 'retval' for writes' retvals Date: Wed, 16 Aug 2023 12:58:10 +0200 Message-ID: <20230816105822.3685-7-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org We have a separate misnomer 'c' to hold the retuned value from tty->ops->write(). Instead, use already defined and properly typed 'retval'. We have another variable 'num' to serve the same purpose in the OPOST branch. We can use this 'retval' too. But just clear it in case of EAGAIN. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index f6fa4dbdf78f..e293d87b5362 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -2335,7 +2335,6 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, { const u8 *b = buf; DEFINE_WAIT_FUNC(wait, woken_wake_function); - int c; ssize_t retval = 0; /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ @@ -2362,15 +2361,16 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, } if (O_OPOST(tty)) { while (nr > 0) { - ssize_t num = process_output_block(tty, b, nr); - if (num < 0) { - if (num == -EAGAIN) - break; - retval = num; - goto break_out; + retval = process_output_block(tty, b, nr); + if (retval == -EAGAIN) { + retval = 0; + break; } - b += num; - nr -= num; + if (retval < 0) + goto break_out; + + b += retval; + nr -= retval; if (nr == 0) break; if (process_output(*b, tty) < 0) @@ -2384,16 +2384,14 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file, while (nr > 0) { mutex_lock(&ldata->output_lock); - c = tty->ops->write(tty, b, nr); + retval = tty->ops->write(tty, b, nr); mutex_unlock(&ldata->output_lock); - if (c < 0) { - retval = c; + if (retval < 0) goto break_out; - } - if (!c) + if (!retval) break; - b += c; - nr -= c; + b += retval; + nr -= retval; } } if (!nr) From patchwork Wed Aug 16 10:58:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F30DAC001E0 for ; Wed, 16 Aug 2023 11:01:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244282AbjHPLAy (ORCPT ); Wed, 16 Aug 2023 07:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244369AbjHPLA3 (ORCPT ); Wed, 16 Aug 2023 07:00:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C18692712; Wed, 16 Aug 2023 04:00:09 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 75ECC665D3; Wed, 16 Aug 2023 10:58:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C85E6C433C7; Wed, 16 Aug 2023 10:58:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183517; bh=fNEfGkwMMbfLXMQISVUXDfoX7yFP8+G69JbMFx6WlHg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VFgR9U1TOLVZbZC4rdr5k5GiznGwYldRR2j0xT9LA4Kcf8oXPSoIlGGmUmHl0vq6P qBrlxPv4xGNP7fXDYOGuD/J70tz4iz/dpgowv456aoFVGH9SxPnSqmQ3QFgrWp2oHo RYozsyNyMn/lu4dgtbyBJdteB53RWvrB8OijAZ07Uizq9Mdea71ZPDmfO9gl6gXToi i55FuaL4oCnQso4nexq4DZwWcHJnuQcUVKVyaaYNcKxVCHHfKQdpK0kyUlEq2VIxrx /jkk6vH+cxoz84oDGNpf894hrZz7lBiUUQWKp0TswPki9DbZ7sFf7FqmK8zYZWG0WC ocy8ucjs5p5DA== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 04/14] tty: n_tty: use time_is_before_jiffies() in n_tty_receive_overrun() Date: Wed, 16 Aug 2023 12:58:12 +0200 Message-ID: <20230816105822.3685-9-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org The jiffies tests in n_tty_receive_overrun() are simplified ratelimiting (without locking). We could use struct ratelimit_state and the helpers, but to me, it occurs to be too complex for this use case. But the code currently tests both if the time passed (the first time_after()) and if jiffies wrapped around (the second time_after()). time_is_before_jiffies() takes care of both, provided overrun_time is initialized at the allocation time. So switch to time_is_before_jiffies(), the same what ratelimiting does. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index e293d87b5362..996cad23e385 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1173,8 +1173,7 @@ static void n_tty_receive_overrun(const struct tty_struct *tty) struct n_tty_data *ldata = tty->disc_data; ldata->num_overrun++; - if (time_after(jiffies, ldata->overrun_time + HZ) || - time_after(ldata->overrun_time, jiffies)) { + if (time_is_before_jiffies(ldata->overrun_time + HZ)) { tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun); ldata->overrun_time = jiffies; ldata->num_overrun = 0; From patchwork Wed Aug 16 10:58:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714313 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E478DC001E0 for ; Wed, 16 Aug 2023 11:00:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244191AbjHPLAX (ORCPT ); Wed, 16 Aug 2023 07:00:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244385AbjHPLAG (ORCPT ); Wed, 16 Aug 2023 07:00:06 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7689E2D61; Wed, 16 Aug 2023 03:59:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id EBC74665B9; Wed, 16 Aug 2023 10:58:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 524B6C433C9; Wed, 16 Aug 2023 10:58:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183519; bh=4VZ2evVSvUAzXqtmvQJZAhVFIJnE/OW4U6dWFy5R8X4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C7UiGgL4LwqR9DTYlgC5CnjHosHnly96ni4yenkymp3FX1B3glAii+AIveeTJBZw4 pxHW/BTixP4b53eC0f/+WjY5+DCwceg4lPFBe58hrjU15knpJChX4o6dIWDUbRN3az 9k9bivPInG17tCrRBzjROgYgyW3UK9SIBxeSc6cGMnTMY0/xewagAR90DxS2qXLfLn eMcfkZuwaOz2+qneHYD+njpoafowS+rGOU8NQjbw6xYEEAfqGShH3CIHRzx8CLT49O pcoUH2FjXzqYV1WlWahLOHBnNbEAWwchbKT4t914Xoh31PSdySduePKYKlpuhq6uIJ +VfI6IlMz2cXw== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 05/14] tty: n_tty: make n_tty_data::num_overrun unsigned Date: Wed, 16 Aug 2023 12:58:13 +0200 Message-ID: <20230816105822.3685-10-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org n_tty_data::num_overrun is unlikely to overflow in a second. But make it explicitly unsigned to avoid printing negative values. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 996cad23e385..0b6eeeb94920 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -99,7 +99,7 @@ struct n_tty_data { /* private to n_tty_receive_overrun (single-threaded) */ unsigned long overrun_time; - int num_overrun; + unsigned int num_overrun; /* non-atomic */ bool no_room; @@ -1174,7 +1174,7 @@ static void n_tty_receive_overrun(const struct tty_struct *tty) ldata->num_overrun++; if (time_is_before_jiffies(ldata->overrun_time + HZ)) { - tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun); + tty_warn(tty, "%u input overrun(s)\n", ldata->num_overrun); ldata->overrun_time = jiffies; ldata->num_overrun = 0; } From patchwork Wed Aug 16 10:58:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 21D51C04E69 for ; Wed, 16 Aug 2023 11:01:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244128AbjHPLAy (ORCPT ); Wed, 16 Aug 2023 07:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57454 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244397AbjHPLAe (ORCPT ); Wed, 16 Aug 2023 07:00:34 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B0F3211E; Wed, 16 Aug 2023 04:00:11 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 77A6C665DB; Wed, 16 Aug 2023 10:58:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CF279C433C7; Wed, 16 Aug 2023 10:58:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183520; bh=AQezLToqhD9r/UVRfPRanWBqyjKP7YFO2T4TPQp9hao=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VDAIkAn654zc8SmFZedhk/DUTO72cPdZu6fJ/zJq7cjegJIBmHVF/NMwTPdQaGbqW k8/QqtDGOQaVJbBZXlMaoaiGnAaHnlsYLN1aagH5o0LZv1YwGUti70tgImw8v2vPNb AIeS+6JX4VJHBV75mrhIo/Vg/P1NXGI1dm1AvllwasdUgcjZv48idxYrGd/wInrDDZ tajo4o7P29kDRbNFZvpiZRiZZcjK6uhIsaM1wllDuqMJCDfWCFO2t/m4Mxs0/m5SIO I6D96InzvrQ6o7920Abm4K8wzGHPlh2qO6/JTs8oeTndkU571OTn7TwYsQ7/PD5tjU KSQThGCQ9b43Q== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 06/14] tty: n_tty: use MASK() for masking out size bits Date: Wed, 16 Aug 2023 12:58:14 +0200 Message-ID: <20230816105822.3685-11-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org In n_tty, there is already a macro to mask out top bits from ring buffer counters. It is MASK() added some time ago. So use it more in the code to make it more readable. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 0b6eeeb94920..9e0ef5294b52 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -138,23 +138,23 @@ static inline size_t read_cnt(struct n_tty_data *ldata) static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i) { - return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)]; + return ldata->read_buf[MASK(i)]; } static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i) { - return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)]; + return &ldata->read_buf[MASK(i)]; } static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i) { smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */ - return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)]; + return ldata->echo_buf[MASK(i)]; } static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i) { - return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)]; + return &ldata->echo_buf[MASK(i)]; } /* If we are not echoing the data, perhaps this is a secret so erase it */ @@ -1359,7 +1359,7 @@ static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c, put_tty_queue(c, ldata); handle_newline: - set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags); + set_bit(MASK(ldata->read_head), ldata->read_flags); put_tty_queue(c, ldata); smp_store_release(&ldata->canon_head, ldata->read_head); kill_fasync(&tty->fasync, SIGIO, POLL_IN); @@ -1505,14 +1505,14 @@ n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp, struct n_tty_data *ldata = tty->disc_data; size_t n, head; - head = ldata->read_head & (N_TTY_BUF_SIZE - 1); + head = MASK(ldata->read_head); n = min_t(size_t, count, N_TTY_BUF_SIZE - head); memcpy(read_buf_addr(ldata, head), cp, n); ldata->read_head += n; cp += n; count -= n; - head = ldata->read_head & (N_TTY_BUF_SIZE - 1); + head = MASK(ldata->read_head); n = min_t(size_t, count, N_TTY_BUF_SIZE - head); memcpy(read_buf_addr(ldata, head), cp, n); ldata->read_head += n; @@ -1779,8 +1779,7 @@ static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old ldata->canon_head = ldata->read_tail; ldata->push = 0; } else { - set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1), - ldata->read_flags); + set_bit(MASK(ldata->read_head - 1), ldata->read_flags); ldata->canon_head = ldata->read_head; ldata->push = 1; } @@ -1941,7 +1940,7 @@ static bool copy_from_read_buf(const struct tty_struct *tty, size_t n; bool is_eof; size_t head = smp_load_acquire(&ldata->commit_head); - size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); + size_t tail = MASK(ldata->read_tail); n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail); n = min(*nr, n); @@ -2004,7 +2003,7 @@ static bool canon_copy_from_read_buf(const struct tty_struct *tty, canon_head = smp_load_acquire(&ldata->canon_head); n = min(*nr, canon_head - ldata->read_tail); - tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1); + tail = MASK(ldata->read_tail); size = min_t(size_t, tail + n, N_TTY_BUF_SIZE); n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n", @@ -2465,7 +2464,7 @@ static unsigned long inq_canon(struct n_tty_data *ldata) nr = head - tail; /* Skip EOF-chars.. */ while (MASK(head) != MASK(tail)) { - if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) && + if (test_bit(MASK(tail), ldata->read_flags) && read_buf(ldata, tail) == __DISABLED_CHAR) nr--; tail++; From patchwork Wed Aug 16 10:58:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714562 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6143C04FE2 for ; Wed, 16 Aug 2023 11:00:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244338AbjHPLAY (ORCPT ); Wed, 16 Aug 2023 07:00:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244391AbjHPLAH (ORCPT ); Wed, 16 Aug 2023 07:00:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F4642D6B; Wed, 16 Aug 2023 03:59:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0443D665E0; Wed, 16 Aug 2023 10:58:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5805EC433C8; Wed, 16 Aug 2023 10:58:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183522; bh=a18DAd2O+Ntjms6YOLNrKnYz9YereskZXRqhxKQrNgo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qPujYQe6FLaM0F8DYuJ2dozF5tYFbdAhGvzFaneHvFPvqNHC8fFHlWsFZXfFM2PSN PnFLQq8pjEyQ8IE7tmsD6a+u6Y2EoEPcYjE70F2pD756R/RimAXzgz92qh2fQXYdP4 mRRrlW3uT3O09Q8qCWMANZkfzOfbqncz4W+cczzAMTtl8uZJf0ojXFq4u5u0zcynf9 oPpDcraOMHKSqMQ8oyWY+NaHwzI4tr/euw1kwTyqvgYFmlonBhU74Trc18eM5eUZhb IEvFRkPSyFl03roLQzF2HwvWN+Ofqmlv3acQRKnhL5B9WOdgq3agz82bo1Qliu1ypJ /UdsjsPyrrjsA== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 07/14] tty: n_tty: move canon handling to a separate function Date: Wed, 16 Aug 2023 12:58:15 +0200 Message-ID: <20230816105822.3685-12-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org n_tty_receive_char_special() is already complicated enough. Split the canon handling to a separate function: n_tty_receive_char_canon(). Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 158 ++++++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 71 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 9e0ef5294b52..ae80d57c9f97 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1262,6 +1262,91 @@ static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, unsigned char c return true; } +static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c) +{ + struct n_tty_data *ldata = tty->disc_data; + + if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || + (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { + eraser(c, tty); + commit_echoes(tty); + + return true; + } + + if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { + ldata->lnext = 1; + if (L_ECHO(tty)) { + finish_erasing(ldata); + if (L_ECHOCTL(tty)) { + echo_char_raw('^', ldata); + echo_char_raw('\b', ldata); + commit_echoes(tty); + } + } + + return true; + } + + if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) { + size_t tail = ldata->canon_head; + + finish_erasing(ldata); + echo_char(c, tty); + echo_char_raw('\n', ldata); + while (MASK(tail) != MASK(ldata->read_head)) { + echo_char(read_buf(ldata, tail), tty); + tail++; + } + commit_echoes(tty); + + return true; + } + + if (c == '\n') { + if (L_ECHO(tty) || L_ECHONL(tty)) { + echo_char_raw('\n', ldata); + commit_echoes(tty); + } + goto handle_newline; + } + + if (c == EOF_CHAR(tty)) { + c = __DISABLED_CHAR; + goto handle_newline; + } + + if ((c == EOL_CHAR(tty)) || + (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { + /* + * XXX are EOL_CHAR and EOL2_CHAR echoed?!? + */ + if (L_ECHO(tty)) { + /* Record the column of first canon char. */ + if (ldata->canon_head == ldata->read_head) + echo_set_canon_col(ldata); + echo_char(c, tty); + commit_echoes(tty); + } + /* + * XXX does PARMRK doubling happen for + * EOL_CHAR and EOL2_CHAR? + */ + if (c == (unsigned char) '\377' && I_PARMRK(tty)) + put_tty_queue(c, ldata); + +handle_newline: + set_bit(MASK(ldata->read_head), ldata->read_flags); + put_tty_queue(c, ldata); + smp_store_release(&ldata->canon_head, ldata->read_head); + kill_fasync(&tty->fasync, SIGIO, POLL_IN); + wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); + return true; + } + + return false; +} + static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c, bool lookahead_done) { @@ -1296,77 +1381,8 @@ static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c, } else if (c == '\n' && I_INLCR(tty)) c = '\r'; - if (ldata->icanon) { - if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || - (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { - eraser(c, tty); - commit_echoes(tty); - return; - } - if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { - ldata->lnext = 1; - if (L_ECHO(tty)) { - finish_erasing(ldata); - if (L_ECHOCTL(tty)) { - echo_char_raw('^', ldata); - echo_char_raw('\b', ldata); - commit_echoes(tty); - } - } - return; - } - if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) { - size_t tail = ldata->canon_head; - - finish_erasing(ldata); - echo_char(c, tty); - echo_char_raw('\n', ldata); - while (MASK(tail) != MASK(ldata->read_head)) { - echo_char(read_buf(ldata, tail), tty); - tail++; - } - commit_echoes(tty); - return; - } - if (c == '\n') { - if (L_ECHO(tty) || L_ECHONL(tty)) { - echo_char_raw('\n', ldata); - commit_echoes(tty); - } - goto handle_newline; - } - if (c == EOF_CHAR(tty)) { - c = __DISABLED_CHAR; - goto handle_newline; - } - if ((c == EOL_CHAR(tty)) || - (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { - /* - * XXX are EOL_CHAR and EOL2_CHAR echoed?!? - */ - if (L_ECHO(tty)) { - /* Record the column of first canon char. */ - if (ldata->canon_head == ldata->read_head) - echo_set_canon_col(ldata); - echo_char(c, tty); - commit_echoes(tty); - } - /* - * XXX does PARMRK doubling happen for - * EOL_CHAR and EOL2_CHAR? - */ - if (c == (unsigned char) '\377' && I_PARMRK(tty)) - put_tty_queue(c, ldata); - -handle_newline: - set_bit(MASK(ldata->read_head), ldata->read_flags); - put_tty_queue(c, ldata); - smp_store_release(&ldata->canon_head, ldata->read_head); - kill_fasync(&tty->fasync, SIGIO, POLL_IN); - wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); - return; - } - } + if (ldata->icanon && n_tty_receive_char_canon(tty, c)) + return; if (L_ECHO(tty)) { finish_erasing(ldata); From patchwork Wed Aug 16 10:58:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E501C41513 for ; Wed, 16 Aug 2023 11:00:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244312AbjHPLAX (ORCPT ); Wed, 16 Aug 2023 07:00:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244383AbjHPLAF (ORCPT ); Wed, 16 Aug 2023 07:00:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAC442D67; Wed, 16 Aug 2023 03:59:42 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 791126654E; Wed, 16 Aug 2023 10:58:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4095C433C9; Wed, 16 Aug 2023 10:58:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183523; bh=4+49NCcHa+BmWKBNyb9RscCkwv+ohnVvNT5U/OTxTj4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y2aOxpumNEZkoq2+7OSjt8zdvSr/egTCdfOSnGz9vUJWxq9SDgK3D7xIJNmaXRLZu tsDI0qZ90oedJljuzAORTESC6h/TNvwyqgx/96yO8HANgqDG0qcYtNy1SY3l6VG7l+ ZJ28PRgBayWagSuQ63Uon7vs2GLXg1CWvyBb+e1CJGT9bPHYVTQfK9yolMYQBCYtWg GEQwM+Qftk+gqt/4rSzfuCv4BgjM0fiVmPlaX8XtV7rNL+kNheizmxUBMMA9YX4RVI ZuO/nzAnUVPgxa9aDKphZyTKwL0hg+tKl5rsuaANIKy0KRqxPxr53k8sIBMlXbd0HH dIgJCOpTWD76Q== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 08/14] tty: n_tty: move newline handling to a separate function Date: Wed, 16 Aug 2023 12:58:16 +0200 Message-ID: <20230816105822.3685-13-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org Currently, n_tty handles the newline in a label in n_tty_receive_char_canon(). That is invoked from two more places. Split this code to a separate function and avoid the label in this case. This makes the code flow more understandable. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index ae80d57c9f97..ab3e7c20fbef 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1262,6 +1262,17 @@ static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, unsigned char c return true; } +static void n_tty_receive_handle_newline(struct tty_struct *tty, u8 c) +{ + struct n_tty_data *ldata = tty->disc_data; + + set_bit(MASK(ldata->read_head), ldata->read_flags); + put_tty_queue(c, ldata); + smp_store_release(&ldata->canon_head, ldata->read_head); + kill_fasync(&tty->fasync, SIGIO, POLL_IN); + wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); +} + static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c) { struct n_tty_data *ldata = tty->disc_data; @@ -1308,12 +1319,16 @@ static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c) echo_char_raw('\n', ldata); commit_echoes(tty); } - goto handle_newline; + n_tty_receive_handle_newline(tty, c); + + return true; } if (c == EOF_CHAR(tty)) { c = __DISABLED_CHAR; - goto handle_newline; + n_tty_receive_handle_newline(tty, c); + + return true; } if ((c == EOL_CHAR(tty)) || @@ -1335,12 +1350,8 @@ static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c) if (c == (unsigned char) '\377' && I_PARMRK(tty)) put_tty_queue(c, ldata); -handle_newline: - set_bit(MASK(ldata->read_head), ldata->read_flags); - put_tty_queue(c, ldata); - smp_store_release(&ldata->canon_head, ldata->read_head); - kill_fasync(&tty->fasync, SIGIO, POLL_IN); - wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM); + n_tty_receive_handle_newline(tty, c); + return true; } From patchwork Wed Aug 16 10:58:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714559 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 931CAC04FDF for ; Wed, 16 Aug 2023 11:01:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244306AbjHPLAz (ORCPT ); Wed, 16 Aug 2023 07:00:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244410AbjHPLAf (ORCPT ); Wed, 16 Aug 2023 07:00:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C9CA2D70; Wed, 16 Aug 2023 04:00:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 061A3665EE; Wed, 16 Aug 2023 10:58:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CDF2C433C7; Wed, 16 Aug 2023 10:58:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183525; bh=0qQF4RO9wzHWQ+t9jAmzN2X8pLXu/bil8u2foxNEB5g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kHFk/vlcR+hy2TfJwNTfa0zchK3sa0tswlYaI2UjAoCKSttFfxkS9auLVdKqrgn8x cU/xced03uDAbgHFTqFroPKyuODJhGFTi8VYF9Ji4pGm1K/AE4GmFnVC0IZBk9n8ms gh6bUt4eG7loUSZ948+rc6Y+PAaa47BreW/D+85V0tBAyelOAgtk4aqw4RYHlO4cIR feanH4TcpnQTDFnqqlMjCvFl5DEfpSsZancXFw0jKJOOHA0SJbEGLKqG5/JABfc2B0 0lpDD99AqZw6f4tR8UKSw1WKAWkYb2BNe2ozRJXLt4gXpXaMPlkowdE4EME7OqCL+u BE+HHBe6QPEjQ== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 09/14] tty: n_tty: remove unsigned char casts from character constants Date: Wed, 16 Aug 2023 12:58:17 +0200 Message-ID: <20230816105822.3685-14-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org We compile with -funsigned-char, so all character constants are already unsigned chars. Therefore, remove superfluous casts. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index ab3e7c20fbef..875a2bbb51c3 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1347,7 +1347,7 @@ static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c) * XXX does PARMRK doubling happen for * EOL_CHAR and EOL2_CHAR? */ - if (c == (unsigned char) '\377' && I_PARMRK(tty)) + if (c == '\377' && I_PARMRK(tty)) put_tty_queue(c, ldata); n_tty_receive_handle_newline(tty, c); @@ -1409,7 +1409,7 @@ static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c, } /* PARMRK doubling check */ - if (c == (unsigned char) '\377' && I_PARMRK(tty)) + if (c == '\377' && I_PARMRK(tty)) put_tty_queue(c, ldata); put_tty_queue(c, ldata); @@ -1444,7 +1444,7 @@ static void n_tty_receive_char(struct tty_struct *tty, unsigned char c) commit_echoes(tty); } /* PARMRK doubling check */ - if (c == (unsigned char) '\377' && I_PARMRK(tty)) + if (c == '\377' && I_PARMRK(tty)) put_tty_queue(c, ldata); put_tty_queue(c, ldata); } From patchwork Wed Aug 16 10:58:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7360EC04A94 for ; Wed, 16 Aug 2023 11:01:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244335AbjHPLAz (ORCPT ); Wed, 16 Aug 2023 07:00:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244423AbjHPLAf (ORCPT ); Wed, 16 Aug 2023 07:00:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0A0F26A8; Wed, 16 Aug 2023 04:00:15 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 874DA6661E; Wed, 16 Aug 2023 10:58:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9E57C433C8; Wed, 16 Aug 2023 10:58:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183526; bh=wBnHhWo9xC8BiAE1Jfh8d+5rplf2+edVMoIbryon06M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fQ705NI9+vcmhsFFFl87Wl0PuDeOFUjgyoXnjcrmIk53fNn+jpUgnD3FHiAsE9VHb o2tXwhytnTwFI8WiAhuZyE5mDjlXrX/ecZPcUb+ZIORowVjN2NddDIH8W2w3img6vT py6tkAZKCoF8eAuCwk+0ZdSdQm2Kc0z3WGzuld5gVYQ2GoT1p2it6gKQkDizYBsPMp c9Jo7/aFQAAdvuiDR+lAt3IStd6RFnNpOLEtj0lpH+HXS8u68CIJyRjleAASo1Xw26 ZXEuBf/okZpMClTcLft5LKc+2zzXHBX8hmZ6HKARn4OobWJGADCDvAQleEZ17zJ7J7 XDUp2ljGfFLyg== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 10/14] tty: n_tty: simplify chars_in_buffer() Date: Wed, 16 Aug 2023 12:58:18 +0200 Message-ID: <20230816105822.3685-15-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org The 'if' in chars_in_buffer() is misleadingly inverted. And since the only difference is the head used for computation, cache the head using ternary operator. And use that in return directly. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 875a2bbb51c3..3815201c220b 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -219,13 +219,9 @@ static void n_tty_kick_worker(const struct tty_struct *tty) static ssize_t chars_in_buffer(const struct tty_struct *tty) { const struct n_tty_data *ldata = tty->disc_data; - ssize_t n = 0; + size_t head = ldata->icanon ? ldata->canon_head : ldata->commit_head; - if (!ldata->icanon) - n = ldata->commit_head - ldata->read_tail; - else - n = ldata->canon_head - ldata->read_tail; - return n; + return head - ldata->read_tail; } /** From patchwork Wed Aug 16 10:58:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714311 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7DE6AC04A94 for ; Wed, 16 Aug 2023 11:00:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244324AbjHPLAY (ORCPT ); Wed, 16 Aug 2023 07:00:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38912 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244390AbjHPLAG (ORCPT ); Wed, 16 Aug 2023 07:00:06 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 697E726A4; Wed, 16 Aug 2023 03:59:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0F36B665F0; Wed, 16 Aug 2023 10:58:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63411C433CB; Wed, 16 Aug 2023 10:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183528; bh=3w+cBELq9YllfEqHK1j2dq7W4bpsjG8RadsgkA+ku8U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X1/EvQvxd53DFX7XyZAr2GqmaO43WdJEj6pE71GoJHU1SpSfDLVqwXx6ZMq8rImWe jcza4GuCMZqjRvaJs1KKTfE3KuHIb6oRoRL5S4U4UsaNZsW42cRDuSdcOR03uVfDBU HMVW1aO9f/tDT5nTP2DauJP+27ZEZ2lsqllvNC1S9WhESRU9Xcp5ScjS2WDplCLM4t 1OcoeApdXF2505m3zAwdwQEKJJ7N23iXj54+T+7JHeTbwdU8RTEF7XfqcdJaV+tJXZ 5ZP7WG90tOkJLPfxTviKmc/d+BZ85m9T4Vhr+TVML5UQCwymV4JmmgCdcgYxrGrhnF 7F5SDiTX7nX+w== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 11/14] tty: n_tty: use u8 for chars and flags Date: Wed, 16 Aug 2023 12:58:19 +0200 Message-ID: <20230816105822.3685-16-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org Unify with the tty layer and use u8 for both chars and flags. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 72 ++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 3815201c220b..4c2f77996ad3 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -109,9 +109,9 @@ struct n_tty_data { unsigned char push:1; /* shared by producer and consumer */ - char read_buf[N_TTY_BUF_SIZE]; + u8 read_buf[N_TTY_BUF_SIZE]; DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE); - unsigned char echo_buf[N_TTY_BUF_SIZE]; + u8 echo_buf[N_TTY_BUF_SIZE]; /* consumer-published */ size_t read_tail; @@ -136,23 +136,23 @@ static inline size_t read_cnt(struct n_tty_data *ldata) return ldata->read_head - ldata->read_tail; } -static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i) +static inline u8 read_buf(struct n_tty_data *ldata, size_t i) { return ldata->read_buf[MASK(i)]; } -static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i) +static inline u8 *read_buf_addr(struct n_tty_data *ldata, size_t i) { return &ldata->read_buf[MASK(i)]; } -static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i) +static inline u8 echo_buf(struct n_tty_data *ldata, size_t i) { smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */ return ldata->echo_buf[MASK(i)]; } -static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i) +static inline u8 *echo_buf_addr(struct n_tty_data *ldata, size_t i) { return &ldata->echo_buf[MASK(i)]; } @@ -303,7 +303,7 @@ static void n_tty_check_unthrottle(struct tty_struct *tty) * * n_tty_receive_buf()/producer path: * caller holds non-exclusive %termios_rwsem */ -static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata) +static inline void put_tty_queue(u8 c, struct n_tty_data *ldata) { *read_buf_addr(ldata, ldata->read_head) = c; ldata->read_head++; @@ -377,7 +377,7 @@ static void n_tty_flush_buffer(struct tty_struct *tty) * character. We use this to correctly compute the on-screen size of the * character when printing. */ -static inline int is_utf8_continuation(unsigned char c) +static inline int is_utf8_continuation(u8 c) { return (c & 0xc0) == 0x80; } @@ -390,7 +390,7 @@ static inline int is_utf8_continuation(unsigned char c) * Returns: true if the utf8 character @c is a multibyte continuation character * and the terminal is in unicode mode. */ -static inline int is_continuation(unsigned char c, const struct tty_struct *tty) +static inline int is_continuation(u8 c, const struct tty_struct *tty) { return I_IUTF8(tty) && is_utf8_continuation(c); } @@ -414,7 +414,7 @@ static inline int is_continuation(unsigned char c, const struct tty_struct *tty) * Locking: should be called under the %output_lock to protect the column state * and space left in the buffer. */ -static int do_output_char(unsigned char c, struct tty_struct *tty, int space) +static int do_output_char(u8 c, struct tty_struct *tty, int space) { struct n_tty_data *ldata = tty->disc_data; int spaces; @@ -488,7 +488,7 @@ static int do_output_char(unsigned char c, struct tty_struct *tty, int space) * Locking: %output_lock to protect column state and space left (also, this is *called from n_tty_write() under the tty layer write lock). */ -static int process_output(unsigned char c, struct tty_struct *tty) +static int process_output(u8 c, struct tty_struct *tty) { struct n_tty_data *ldata = tty->disc_data; int space, retval; @@ -524,12 +524,12 @@ static int process_output(unsigned char c, struct tty_struct *tty) * called from n_tty_write() under the tty layer write lock). */ static ssize_t process_output_block(struct tty_struct *tty, - const unsigned char *buf, unsigned int nr) + const u8 *buf, unsigned int nr) { struct n_tty_data *ldata = tty->disc_data; int space; int i; - const unsigned char *cp; + const u8 *cp; mutex_lock(&ldata->output_lock); @@ -542,7 +542,7 @@ static ssize_t process_output_block(struct tty_struct *tty, nr = space; for (i = 0, cp = buf; i < nr; i++, cp++) { - unsigned char c = *cp; + u8 c = *cp; switch (c) { case '\n': @@ -609,7 +609,7 @@ static size_t __process_echoes(struct tty_struct *tty) struct n_tty_data *ldata = tty->disc_data; int space, old_space; size_t tail; - unsigned char c; + u8 c; old_space = space = tty_write_room(tty); @@ -617,7 +617,7 @@ static size_t __process_echoes(struct tty_struct *tty) while (MASK(ldata->echo_commit) != MASK(tail)) { c = echo_buf(ldata, tail); if (c == ECHO_OP_START) { - unsigned char op; + u8 op; bool space_left = true; /* @@ -818,7 +818,7 @@ static void flush_echoes(struct tty_struct *tty) * * Add a character or operation byte to the echo buffer. */ -static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata) +static inline void add_echo_byte(u8 c, struct n_tty_data *ldata) { *echo_buf_addr(ldata, ldata->echo_head) = c; smp_wmb(); /* Matches smp_rmb() in echo_buf(). */ @@ -889,7 +889,7 @@ static void echo_erase_tab(unsigned int num_chars, int after_tab, * * This variant does not treat control characters specially. */ -static void echo_char_raw(unsigned char c, struct n_tty_data *ldata) +static void echo_char_raw(u8 c, struct n_tty_data *ldata) { if (c == ECHO_OP_START) { add_echo_byte(ECHO_OP_START, ldata); @@ -910,7 +910,7 @@ static void echo_char_raw(unsigned char c, struct n_tty_data *ldata) * This variant tags control characters to be echoed as "^X" (where X is the * letter representing the control char). */ -static void echo_char(unsigned char c, const struct tty_struct *tty) +static void echo_char(u8 c, const struct tty_struct *tty) { struct n_tty_data *ldata = tty->disc_data; @@ -948,7 +948,7 @@ static inline void finish_erasing(struct n_tty_data *ldata) * Locking: n_tty_receive_buf()/producer path: * caller holds non-exclusive %termios_rwsem */ -static void eraser(unsigned char c, const struct tty_struct *tty) +static void eraser(u8 c, const struct tty_struct *tty) { struct n_tty_data *ldata = tty->disc_data; enum { ERASE, WERASE, KILL } kill_type; @@ -1188,7 +1188,7 @@ static void n_tty_receive_overrun(const struct tty_struct *tty) * caller holds non-exclusive %termios_rwsem */ static void n_tty_receive_parity_error(const struct tty_struct *tty, - unsigned char c) + u8 c) { struct n_tty_data *ldata = tty->disc_data; @@ -1206,7 +1206,7 @@ static void n_tty_receive_parity_error(const struct tty_struct *tty, } static void -n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) +n_tty_receive_signal_char(struct tty_struct *tty, int signal, u8 c) { isig(signal, tty); if (I_IXON(tty)) @@ -1218,7 +1218,7 @@ n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c) process_echoes(tty); } -static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, unsigned char c) +static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, u8 c) { return c == START_CHAR(tty) || c == STOP_CHAR(tty); } @@ -1238,7 +1238,7 @@ static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, unsigned char c) * Returns true if @c is consumed as flow-control character, the character * must not be treated as normal character. */ -static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, unsigned char c, +static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, u8 c, bool lookahead_done) { if (!n_tty_is_char_flow_ctrl(tty, c)) @@ -1354,7 +1354,7 @@ static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c) return false; } -static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c, +static void n_tty_receive_char_special(struct tty_struct *tty, u8 c, bool lookahead_done) { struct n_tty_data *ldata = tty->disc_data; @@ -1423,7 +1423,7 @@ static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c, * caller holds non-exclusive %termios_rwsem * publishes canon_head if canonical mode is active */ -static void n_tty_receive_char(struct tty_struct *tty, unsigned char c) +static void n_tty_receive_char(struct tty_struct *tty, u8 c) { struct n_tty_data *ldata = tty->disc_data; @@ -1445,7 +1445,7 @@ static void n_tty_receive_char(struct tty_struct *tty, unsigned char c) put_tty_queue(c, ldata); } -static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c, +static void n_tty_receive_char_closing(struct tty_struct *tty, u8 c, bool lookahead_done) { if (I_ISTRIP(tty)) @@ -1465,7 +1465,7 @@ static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c, } static void -n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag) +n_tty_receive_char_flagged(struct tty_struct *tty, u8 c, u8 flag) { switch (flag) { case TTY_BREAK: @@ -1479,13 +1479,13 @@ n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag) n_tty_receive_overrun(tty); break; default: - tty_err(tty, "unknown flag %d\n", flag); + tty_err(tty, "unknown flag %u\n", flag); break; } } static void -n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag) +n_tty_receive_char_lnext(struct tty_struct *tty, u8 c, u8 flag) { struct n_tty_data *ldata = tty->disc_data; @@ -1505,7 +1505,7 @@ static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp, const u8 *fp, size_t count) { struct n_tty_data *ldata = tty->disc_data; - unsigned char flag = TTY_NORMAL; + u8 flag = TTY_NORMAL; ldata->lookahead_count += count; @@ -1562,7 +1562,7 @@ static void n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp, int count, bool lookahead_done) { - char flag = TTY_NORMAL; + u8 flag = TTY_NORMAL; while (count--) { if (fp) @@ -1955,7 +1955,7 @@ static inline int input_available_p(const struct tty_struct *tty, int poll) * read_tail published */ static bool copy_from_read_buf(const struct tty_struct *tty, - unsigned char **kbp, + u8 **kbp, size_t *nr) { @@ -1968,7 +1968,7 @@ static bool copy_from_read_buf(const struct tty_struct *tty, n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail); n = min(*nr, n); if (n) { - unsigned char *from = read_buf_addr(ldata, tail); + u8 *from = read_buf_addr(ldata, tail); memcpy(*kbp, from, n); is_eof = n == 1 && *from == EOF_CHAR(tty); tty_audit_add_data(tty, from, n); @@ -2010,7 +2010,7 @@ static bool copy_from_read_buf(const struct tty_struct *tty, * read_tail published */ static bool canon_copy_from_read_buf(const struct tty_struct *tty, - unsigned char **kbp, + u8 **kbp, size_t *nr) { struct n_tty_data *ldata = tty->disc_data; @@ -2229,7 +2229,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf, while (nr) { /* First test for status change. */ if (packet && tty->link->ctrl.pktstatus) { - unsigned char cs; + u8 cs; if (kb != kbuf) break; spin_lock_irq(&tty->link->ctrl.lock); From patchwork Wed Aug 16 10:58:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F099AC04FE0 for ; Wed, 16 Aug 2023 11:01:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244363AbjHPLA5 (ORCPT ); Wed, 16 Aug 2023 07:00:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244468AbjHPLAi (ORCPT ); Wed, 16 Aug 2023 07:00:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 112E52D48; Wed, 16 Aug 2023 04:00:21 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 86D276656C; Wed, 16 Aug 2023 10:58:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DFBBEC433C9; Wed, 16 Aug 2023 10:58:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183530; bh=+zvqivP6B2y0YeXRf8c57g1f5SNCFX/cX6fgmE5cuY4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=biso20wzFk+X31qi9sul3aKfAn3WRDRjz5AzXSQ3A2Fczgf4tkpNAaBsC7BsTgfPf VfWxp6lpH1f5uKl8126yaZac6ILwe9ke/CyDPLbazFpbFZBlsTl9F7gAXVlm9ErKnV AuIPjJaX/Eld/w94rHfHxqlUZbyIar3f6j+4P3aMXHyx+hxVMX8RkEF8nnbpcM+mYL WsKLUfY0sEr+Byr4/GKy0YWlMatyhtxDSgFFFd3tas7W19QQTtZQc+YZLXXVoVXwfm kidp4WlGhhJig1OoTh8dYbmsF0Vc1GqM27AD2Df8+lG2QehwW67dIwDp8hN5iblkSH lAB9OHDnvU0yw== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 12/14] tty: n_tty: unify counts to size_t Date: Wed, 16 Aug 2023 12:58:20 +0200 Message-ID: <20230816105822.3685-17-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org Some count types are already 'size_t' for a long time. Some were switched to 'size_t' recently. Unify the rest with those now. This allows for some min_t()s to become min()s. And make one min() an explicit min_t() as we are comparing signed 'room' to unsigned 'count'. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 4c2f77996ad3..11becd2dda2d 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1523,27 +1523,27 @@ static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp, static void n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp, - int count) + size_t count) { struct n_tty_data *ldata = tty->disc_data; size_t n, head; head = MASK(ldata->read_head); - n = min_t(size_t, count, N_TTY_BUF_SIZE - head); + n = min(count, N_TTY_BUF_SIZE - head); memcpy(read_buf_addr(ldata, head), cp, n); ldata->read_head += n; cp += n; count -= n; head = MASK(ldata->read_head); - n = min_t(size_t, count, N_TTY_BUF_SIZE - head); + n = min(count, N_TTY_BUF_SIZE - head); memcpy(read_buf_addr(ldata, head), cp, n); ldata->read_head += n; } static void n_tty_receive_buf_raw(struct tty_struct *tty, const u8 *cp, const u8 *fp, - int count) + size_t count) { struct n_tty_data *ldata = tty->disc_data; u8 flag = TTY_NORMAL; @@ -1560,7 +1560,7 @@ n_tty_receive_buf_raw(struct tty_struct *tty, const u8 *cp, const u8 *fp, static void n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp, - int count, bool lookahead_done) + size_t count, bool lookahead_done) { u8 flag = TTY_NORMAL; @@ -1573,7 +1573,7 @@ n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp, } static void n_tty_receive_buf_standard(struct tty_struct *tty, const u8 *cp, - const u8 *fp, int count, + const u8 *fp, size_t count, bool lookahead_done) { struct n_tty_data *ldata = tty->disc_data; @@ -1612,11 +1612,11 @@ static void n_tty_receive_buf_standard(struct tty_struct *tty, const u8 *cp, } static void __receive_buf(struct tty_struct *tty, const u8 *cp, const u8 *fp, - int count) + size_t count) { struct n_tty_data *ldata = tty->disc_data; bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty)); - size_t la_count = min_t(size_t, ldata->lookahead_count, count); + size_t la_count = min(ldata->lookahead_count, count); if (ldata->real_raw) n_tty_receive_buf_real_raw(tty, cp, count); @@ -1687,11 +1687,11 @@ static void __receive_buf(struct tty_struct *tty, const u8 *cp, const u8 *fp, */ static size_t n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp, - int count, bool flow) + size_t count, bool flow) { struct n_tty_data *ldata = tty->disc_data; - size_t rcvd = 0; - int room, n, overflow; + size_t n, rcvd = 0; + int room, overflow; down_read(&tty->termios_rwsem); @@ -1724,7 +1724,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp, } else overflow = 0; - n = min(count, room); + n = min_t(size_t, count, room); if (!n) break; @@ -1954,9 +1954,8 @@ static inline int input_available_p(const struct tty_struct *tty, int poll) * caller holds non-exclusive %termios_rwsem; * read_tail published */ -static bool copy_from_read_buf(const struct tty_struct *tty, - u8 **kbp, - size_t *nr) +static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp, + size_t *nr) { struct n_tty_data *ldata = tty->disc_data; @@ -2009,8 +2008,7 @@ static bool copy_from_read_buf(const struct tty_struct *tty, * caller holds non-exclusive %termios_rwsem; * read_tail published */ -static bool canon_copy_from_read_buf(const struct tty_struct *tty, - u8 **kbp, +static bool canon_copy_from_read_buf(const struct tty_struct *tty, u8 **kbp, size_t *nr) { struct n_tty_data *ldata = tty->disc_data; From patchwork Wed Aug 16 10:58:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714558 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2DD7C04FE1 for ; Wed, 16 Aug 2023 11:01:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244356AbjHPLA4 (ORCPT ); Wed, 16 Aug 2023 07:00:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57494 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244471AbjHPLAj (ORCPT ); Wed, 16 Aug 2023 07:00:39 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAC2F2D4B; Wed, 16 Aug 2023 04:00:21 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 164CF66600; Wed, 16 Aug 2023 10:58:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68FADC433CA; Wed, 16 Aug 2023 10:58:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183531; bh=Y+IFfIONED7Np5i1ztZdbLOGiFw1ZaXyKdb8F+lMdmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DPxLouj+4PCwyYf4BCGlSFYFoLBqh44K0ErJvACCDimrxrddqCg3s5WKobksVeotd /HirbtcowWE8E8GTlwU+q1N2KDybgsCDG29Wx/zyoC5LoYlRHxxWY1n/uRQvwcrkx3 cQeS6ku+YN7CYZiwImeqLHgscM2jk9ZRIaCv2xw5W+jYIb9tqRGKVGnhNxLvmkWDeN Zmn/O9rInhGuHjkGnr1mPdnTDS4D3qTPCSAsz4+DxttyQoIxQicKHrlRnLSUerULiN XUoyNZ+0a73nfWgu8cq7gOjILN1zNFk1TmXbu7o6n92G3Dxs8UEWDKopgKF0MQR/sq rLH5c3F/b82IA== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 13/14] tty: n_tty: extract ECHO_OP processing to a separate function Date: Wed, 16 Aug 2023 12:58:21 +0200 Message-ID: <20230816105822.3685-18-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org __process_echoes() contains ECHO_OPs processing. It is stuffed in a while loop and the whole function is barely readable. Separate it to a new function: n_tty_process_echo_ops(). Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 194 ++++++++++++++++++++++---------------------- 1 file changed, 98 insertions(+), 96 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index 11becd2dda2d..d4d00c530c58 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -582,6 +582,100 @@ static ssize_t process_output_block(struct tty_struct *tty, return i; } +static int n_tty_process_echo_ops(struct tty_struct *tty, size_t *tail, + int space) +{ + struct n_tty_data *ldata = tty->disc_data; + u8 op; + + /* + * Since add_echo_byte() is called without holding output_lock, we + * might see only portion of multi-byte operation. + */ + if (MASK(ldata->echo_commit) == MASK(*tail + 1)) + return -ENODATA; + + /* + * If the buffer byte is the start of a multi-byte operation, get the + * next byte, which is either the op code or a control character value. + */ + op = echo_buf(ldata, *tail + 1); + + switch (op) { + case ECHO_OP_ERASE_TAB: { + unsigned int num_chars, num_bs; + + if (MASK(ldata->echo_commit) == MASK(*tail + 2)) + return -ENODATA; + + num_chars = echo_buf(ldata, *tail + 2); + + /* + * Determine how many columns to go back in order to erase the + * tab. This depends on the number of columns used by other + * characters within the tab area. If this (modulo 8) count is + * from the start of input rather than from a previous tab, we + * offset by canon column. Otherwise, tab spacing is normal. + */ + if (!(num_chars & 0x80)) + num_chars += ldata->canon_column; + num_bs = 8 - (num_chars & 7); + + if (num_bs > space) + return -ENOSPC; + + space -= num_bs; + while (num_bs--) { + tty_put_char(tty, '\b'); + if (ldata->column > 0) + ldata->column--; + } + *tail += 3; + break; + } + case ECHO_OP_SET_CANON_COL: + ldata->canon_column = ldata->column; + *tail += 2; + break; + + case ECHO_OP_MOVE_BACK_COL: + if (ldata->column > 0) + ldata->column--; + *tail += 2; + break; + + case ECHO_OP_START: + /* This is an escaped echo op start code */ + if (!space) + return -ENOSPC; + + tty_put_char(tty, ECHO_OP_START); + ldata->column++; + space--; + *tail += 2; + break; + + default: + /* + * If the op is not a special byte code, it is a ctrl char + * tagged to be echoed as "^X" (where X is the letter + * representing the control char). Note that we must ensure + * there is enough space for the whole ctrl pair. + */ + if (space < 2) + return -ENOSPC; + + tty_put_char(tty, '^'); + tty_put_char(tty, op ^ 0100); + ldata->column += 2; + space -= 2; + *tail += 2; + break; + } + + return space; +} + /** * __process_echoes - write pending echo characters * @tty: terminal device @@ -617,104 +711,12 @@ static size_t __process_echoes(struct tty_struct *tty) while (MASK(ldata->echo_commit) != MASK(tail)) { c = echo_buf(ldata, tail); if (c == ECHO_OP_START) { - u8 op; - bool space_left = true; - - /* - * Since add_echo_byte() is called without holding - * output_lock, we might see only portion of multi-byte - * operation. - */ - if (MASK(ldata->echo_commit) == MASK(tail + 1)) + int ret = n_tty_process_echo_ops(tty, &tail, space); + if (ret == -ENODATA) goto not_yet_stored; - /* - * If the buffer byte is the start of a multi-byte - * operation, get the next byte, which is either the - * op code or a control character value. - */ - op = echo_buf(ldata, tail + 1); - - switch (op) { - case ECHO_OP_ERASE_TAB: { - unsigned int num_chars, num_bs; - - if (MASK(ldata->echo_commit) == MASK(tail + 2)) - goto not_yet_stored; - num_chars = echo_buf(ldata, tail + 2); - - /* - * Determine how many columns to go back - * in order to erase the tab. - * This depends on the number of columns - * used by other characters within the tab - * area. If this (modulo 8) count is from - * the start of input rather than from a - * previous tab, we offset by canon column. - * Otherwise, tab spacing is normal. - */ - if (!(num_chars & 0x80)) - num_chars += ldata->canon_column; - num_bs = 8 - (num_chars & 7); - - if (num_bs > space) { - space_left = false; - break; - } - space -= num_bs; - while (num_bs--) { - tty_put_char(tty, '\b'); - if (ldata->column > 0) - ldata->column--; - } - tail += 3; - break; - } - case ECHO_OP_SET_CANON_COL: - ldata->canon_column = ldata->column; - tail += 2; - break; - - case ECHO_OP_MOVE_BACK_COL: - if (ldata->column > 0) - ldata->column--; - tail += 2; - break; - - case ECHO_OP_START: - /* This is an escaped echo op start code */ - if (!space) { - space_left = false; - break; - } - tty_put_char(tty, ECHO_OP_START); - ldata->column++; - space--; - tail += 2; - break; - - default: - /* - * If the op is not a special byte code, - * it is a ctrl char tagged to be echoed - * as "^X" (where X is the letter - * representing the control char). - * Note that we must ensure there is - * enough space for the whole ctrl pair. - * - */ - if (space < 2) { - space_left = false; - break; - } - tty_put_char(tty, '^'); - tty_put_char(tty, op ^ 0100); - ldata->column += 2; - space -= 2; - tail += 2; - } - - if (!space_left) + if (ret < 0) break; + space = ret; } else { if (O_OPOST(tty)) { int retval = do_output_char(c, tty, space); From patchwork Wed Aug 16 10:58:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jiri Slaby \(SUSE\)" X-Patchwork-Id: 714561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6A50C04FE1 for ; Wed, 16 Aug 2023 11:00:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244349AbjHPLA0 (ORCPT ); Wed, 16 Aug 2023 07:00:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244477AbjHPLAV (ORCPT ); Wed, 16 Aug 2023 07:00:21 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADB2D2D4B; Wed, 16 Aug 2023 04:00:05 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 96EF5665FB; Wed, 16 Aug 2023 10:58:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E660FC433C9; Wed, 16 Aug 2023 10:58:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183533; bh=ESSt5X2RRzZnpsf5+yWnfafc+UieZd3b3Vb0DvJ2wvQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YBgLjJ19B9relkf6oHCdOzSNkolQuM0T/Y1vfecX/XUUmB2lPLpERnq3Wodxo2L7k lAGd3NVUV05WsySW6kS8qOI54HSdL0748jTcUtxW7g1HutSw0q6zSltofE5zPS1PLc a87I4bH00FlLUmypXenFRHNNJUJG9hNxPVyXA2LEbzrLXZBw41gojwEyOB9XVjqMZt C+5ITKT/v2Nntf9M+c6pWA4a3qFKtgjWNWvxHysZJlR1s/8jFo9f5sOEoRPh3zhPET QbukmGiDnKp915asfd6W9Xx9EtPfCGFWXtFkv0otaCr36FqKE5bWHaZ0nQkvB16AHN Qq7SPGDCJ353Q== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 14/14] tty: n_tty: deduplicate copy code in n_tty_receive_buf_real_raw() Date: Wed, 16 Aug 2023 12:58:22 +0200 Message-ID: <20230816105822.3685-19-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105822.3685-1-jirislaby@kernel.org> References: <20230816105822.3685-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org The code is duplicated to perform the copy twice -- to handle buffer wrap-around. Instead of the duplication, roll this into the loop. (And add some blank lines around to have the code a bit more readable.) Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/n_tty.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c index d4d00c530c58..646c67e1547b 100644 --- a/drivers/tty/n_tty.c +++ b/drivers/tty/n_tty.c @@ -1528,19 +1528,18 @@ n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp, size_t count) { struct n_tty_data *ldata = tty->disc_data; - size_t n, head; - - head = MASK(ldata->read_head); - n = min(count, N_TTY_BUF_SIZE - head); - memcpy(read_buf_addr(ldata, head), cp, n); - ldata->read_head += n; - cp += n; - count -= n; - - head = MASK(ldata->read_head); - n = min(count, N_TTY_BUF_SIZE - head); - memcpy(read_buf_addr(ldata, head), cp, n); - ldata->read_head += n; + + /* handle buffer wrap-around by a loop */ + for (unsigned int i = 0; i < 2; i++) { + size_t head = MASK(ldata->read_head); + size_t n = min(count, N_TTY_BUF_SIZE - head); + + memcpy(read_buf_addr(ldata, head), cp, n); + + ldata->read_head += n; + cp += n; + count -= n; + } } static void