From patchwork Wed Aug 16 10:55:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714321 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 97EF4C04E69 for ; Wed, 16 Aug 2023 10:56:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243026AbjHPK4H (ORCPT ); Wed, 16 Aug 2023 06:56:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243690AbjHPKzh (ORCPT ); Wed, 16 Aug 2023 06:55:37 -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 0B55C13E; Wed, 16 Aug 2023 03:55:36 -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 8DBED64FFF; Wed, 16 Aug 2023 10:55:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF26FC433C8; Wed, 16 Aug 2023 10:55:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183335; bh=f0NgofJBUmSnyOLLGLK1G2U+0vgQxZe+PafjYAMnUsM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IFFno7RU8fFroRTlwyEv3dE4wmWHidBnWnAJI52ytxIlo9XxakgTjA+X9oEe1YDmU VU5OpHYDSbL15lY55l4UPHmzjMwx3/GGRmORxWqU+H+yGT8rYNqUt+1VDhl31dXIOz seXik9e5dJVoSpekD9EhKHJlYZoK20CLZBsPM28Mdjq9hrWSqYVoIHBpwOXgEmYoPE hr8zW01IHvSZTI65sICvPQz50zq4WRf6VtN08eI3sSexLCm8GPgHMQZFqTatTUg4ug qsum1XLGYaIKdvthtOG02RcY8TNOPKbw48ynoHcDHcXz4DPiFQLch6ZxrGopT7YGps P/AiSAFjvejfg== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 01/10] tty: tty_buffer: switch data type to u8 Date: Wed, 16 Aug 2023 12:55:21 +0200 Message-ID: <20230816105530.3335-2-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org There is no reason to have tty_buffer::data typed as unsigned long. Switch to u8, but preserve the ulong alignment using __aligned. This allows for the cast removal from char_buf_ptr(). And for use of struct_size() in the allocation in tty_buffer_alloc() -- in the next patch. Signed-off-by: Jiri Slaby (SUSE) --- include/linux/tty_buffer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/tty_buffer.h b/include/linux/tty_buffer.h index e45cba81d0e9..31125e3be3c5 100644 --- a/include/linux/tty_buffer.h +++ b/include/linux/tty_buffer.h @@ -19,12 +19,12 @@ struct tty_buffer { unsigned int read; bool flags; /* Data points here */ - unsigned long data[]; + u8 data[] __aligned(sizeof(unsigned long)); }; static inline u8 *char_buf_ptr(struct tty_buffer *b, unsigned int ofs) { - return ((u8 *)b->data) + ofs; + return b->data + ofs; } static inline u8 *flag_buf_ptr(struct tty_buffer *b, unsigned int ofs) From patchwork Wed Aug 16 10:55:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714572 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 2677CC001E0 for ; Wed, 16 Aug 2023 10:56:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243996AbjHPK4H (ORCPT ); Wed, 16 Aug 2023 06:56:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243950AbjHPKzi (ORCPT ); Wed, 16 Aug 2023 06:55:38 -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 81A1213E; Wed, 16 Aug 2023 03:55:37 -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 1683363FBB; Wed, 16 Aug 2023 10:55:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6A750C433C9; Wed, 16 Aug 2023 10:55:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183336; bh=rtvLwjn0+HFhIw5kcAsqn47YQB2JGor/QQpglZNQ9tI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jC0W+wWJkaOWqIbHWSW6Y4kUN+1oTd+M02FRGAiS0Ldxkja9Kxph2XDww81mVBbFA 3XwOv9EV0NoInFjgnLV9wM92Rtzp0roWIIfQoMM89WJhJ5wH9qorFAarPdtJpq/JVw kTmgZiPtFhF96vhq4kBYa/7Z62qLrltVYEAhnqEP9IB1pp3OBBnlieypUb51Fms8JN kn/3VD1EHovoxyH3d9fIL+qq6avcLP3mm580Sdg0VqQMk4NTb8Riu6yotEI0kW1ekx QN8DDNFT1bFYWzX9xyt6vfUFoYiKJF0GOfbxIfyqO0HthCHzYs2NcXAUYNGzamxbW8 gLSylhT1aAktg== From: "Jiri Slaby (SUSE)" To: gregkh@linuxfoundation.org Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org, "Jiri Slaby (SUSE)" Subject: [PATCH 02/10] tty: tty_buffer: use struct_size() in tty_buffer_alloc() Date: Wed, 16 Aug 2023 12:55:22 +0200 Message-ID: <20230816105530.3335-3-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org Now, that tty_buffer::data has the right type, use struct_size() for size calculation. struct_size() makes the code less error-prone and more readable. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/tty_buffer.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index 684d099cbe11..c94df1a2d7f8 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -177,8 +177,7 @@ static struct tty_buffer *tty_buffer_alloc(struct tty_port *port, size_t size) */ if (atomic_read(&port->buf.mem_used) > port->buf.mem_limit) return NULL; - p = kmalloc(sizeof(struct tty_buffer) + 2 * size, - GFP_ATOMIC | __GFP_NOWARN); + p = kmalloc(struct_size(p, data, 2 * size), GFP_ATOMIC | __GFP_NOWARN); if (p == NULL) return NULL; From patchwork Wed Aug 16 10:55:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714319 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 27D6EC04FE2 for ; Wed, 16 Aug 2023 10:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244135AbjHPK4H (ORCPT ); Wed, 16 Aug 2023 06:56:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243981AbjHPKzk (ORCPT ); Wed, 16 Aug 2023 06:55:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02B801985; Wed, 16 Aug 2023 03:55:39 -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 9436B64FFF; Wed, 16 Aug 2023 10:55:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA42CC433CA; Wed, 16 Aug 2023 10:55:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183338; bh=9arzAMjp1tDpddEsEvYS7yWfWPnFC38pD7wPryrXV6U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hT8HrzQeM68z30dQTxo3lwOP5IVFa8Qpw7BkhH9n5VQ1wdhNg7B0l2G5ErRaksiB8 dyWdcDZQJ9WwY1+ZPDSlGWvZ+Jby/Grqg4WAiOYsoD4Fg+T1p78qCzRdi7T0W7/wpf qApSVv1AiAfQIHjIigToM5IUug0a7cqSmBKxahdVGUPoak+HYBPEKdzUWCwLj7/Flx f+BA11qP3+D93tK7+EcqR7tL1ND5RcyjMLxSnU4g3BqUJgr8u+30fp8Z0jmRFZkiay nk4f76YgGYE45glBqTujPXb5HWTgAQGmtRUgXwTrYnqwwW7uJlZcG06/20qcBOyuyl f3ij3RvGKbP2A== 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/10] tty: tty_buffer: unify tty_insert_flip_string_{fixed_flag,flags}() Date: Wed, 16 Aug 2023 12:55:23 +0200 Message-ID: <20230816105530.3335-4-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org They both do the same except for flags. One mem-copies the flags from the caller, the other mem-sets to one flag given by the caller. This can be unified with a simple if in the unified function. Signed-off-by: Jiri Slaby (SUSE) --- Documentation/driver-api/tty/tty_buffer.rst | 6 +- drivers/tty/tty_buffer.c | 70 +++++---------------- include/linux/tty_flip.h | 46 ++++++++++++-- 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/Documentation/driver-api/tty/tty_buffer.rst b/Documentation/driver-api/tty/tty_buffer.rst index a39d4781e0d2..774dc119c312 100644 --- a/Documentation/driver-api/tty/tty_buffer.rst +++ b/Documentation/driver-api/tty/tty_buffer.rst @@ -15,10 +15,12 @@ Flip Buffer Management ====================== .. kernel-doc:: drivers/tty/tty_buffer.c - :identifiers: tty_prepare_flip_string tty_insert_flip_string_fixed_flag - tty_insert_flip_string_flags __tty_insert_flip_char + :identifiers: tty_prepare_flip_string __tty_insert_flip_char tty_flip_buffer_push tty_ldisc_receive_buf +.. kernel-doc:: include/linux/tty_flip.h + :identifiers: tty_insert_flip_string_fixed_flag tty_insert_flip_string_flags + ---- Other Functions diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index c94df1a2d7f8..94a88dc05a54 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -303,82 +303,42 @@ int tty_buffer_request_room(struct tty_port *port, size_t size) } EXPORT_SYMBOL_GPL(tty_buffer_request_room); -/** - * tty_insert_flip_string_fixed_flag - add characters to the tty buffer - * @port: tty port - * @chars: characters - * @flag: flag value for each character - * @size: size - * - * Queue a series of bytes to the tty buffering. All the characters passed are - * marked with the supplied flag. - * - * Returns: the number added. - */ -int tty_insert_flip_string_fixed_flag(struct tty_port *port, const u8 *chars, - u8 flag, size_t size) +int __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, + const u8 *flags, bool mutable_flags, + size_t size) { + bool need_flags = mutable_flags || flags[0] != TTY_NORMAL; int copied = 0; - bool flags = flag != TTY_NORMAL; do { int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); - int space = __tty_buffer_request_room(port, goal, flags); + int space = __tty_buffer_request_room(port, goal, need_flags); struct tty_buffer *tb = port->buf.tail; if (unlikely(space == 0)) break; - memcpy(char_buf_ptr(tb, tb->used), chars, space); - if (tb->flags) - memset(flag_buf_ptr(tb, tb->used), flag, space); - tb->used += space; - copied += space; - chars += space; - /* There is a small chance that we need to split the data over - * several buffers. If this is the case we must loop. - */ - } while (unlikely(size > copied)); - return copied; -} -EXPORT_SYMBOL(tty_insert_flip_string_fixed_flag); -/** - * tty_insert_flip_string_flags - add characters to the tty buffer - * @port: tty port - * @chars: characters - * @flags: flag bytes - * @size: size - * - * Queue a series of bytes to the tty buffering. For each character the flags - * array indicates the status of the character. - * - * Returns: the number added. - */ -int tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, - const u8 *flags, size_t size) -{ - int copied = 0; + memcpy(char_buf_ptr(tb, tb->used), chars, space); - do { - int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); - int space = tty_buffer_request_room(port, goal); - struct tty_buffer *tb = port->buf.tail; + if (mutable_flags) { + memcpy(flag_buf_ptr(tb, tb->used), flags, space); + flags += space; + } else if (tb->flags) { + memset(flag_buf_ptr(tb, tb->used), flags[0], space); + } - if (unlikely(space == 0)) - break; - memcpy(char_buf_ptr(tb, tb->used), chars, space); - memcpy(flag_buf_ptr(tb, tb->used), flags, space); tb->used += space; copied += space; chars += space; - flags += space; + /* There is a small chance that we need to split the data over * several buffers. If this is the case we must loop. */ } while (unlikely(size > copied)); + return copied; } -EXPORT_SYMBOL(tty_insert_flip_string_flags); +EXPORT_SYMBOL(__tty_insert_flip_string_flags); /** * __tty_insert_flip_char - add one character to the tty buffer diff --git a/include/linux/tty_flip.h b/include/linux/tty_flip.h index d33aed2172c7..8b781f709605 100644 --- a/include/linux/tty_flip.h +++ b/include/linux/tty_flip.h @@ -10,14 +10,52 @@ struct tty_ldisc; int tty_buffer_set_limit(struct tty_port *port, int limit); unsigned int tty_buffer_space_avail(struct tty_port *port); int tty_buffer_request_room(struct tty_port *port, size_t size); -int tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, - const u8 *flags, size_t size); -int tty_insert_flip_string_fixed_flag(struct tty_port *port, const u8 *chars, - u8 flag, size_t size); +int __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, + const u8 *flags, bool mutable_flags, + size_t size); int tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size); void tty_flip_buffer_push(struct tty_port *port); int __tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag); +/** + * tty_insert_flip_string_fixed_flag - add characters to the tty buffer + * @port: tty port + * @chars: characters + * @flag: flag value for each character + * @size: size + * + * Queue a series of bytes to the tty buffering. All the characters passed are + * marked with the supplied flag. + * + * Returns: the number added. + */ +static inline int tty_insert_flip_string_fixed_flag(struct tty_port *port, + const u8 *chars, u8 flag, + size_t size) +{ + return __tty_insert_flip_string_flags(port, chars, &flag, false, size); +} + +/** + * tty_insert_flip_string_flags - add characters to the tty buffer + * @port: tty port + * @chars: characters + * @flags: flag bytes + * @size: size + * + * Queue a series of bytes to the tty buffering. For each character the flags + * array indicates the status of the character. + * + * Returns: the number added. + */ +static inline int tty_insert_flip_string_flags(struct tty_port *port, + const u8 *chars, const u8 *flags, + size_t size) +{ + return __tty_insert_flip_string_flags(port, chars, flags, true, size); +} + + static inline int tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag) { struct tty_buffer *tb = port->buf.tail; From patchwork Wed Aug 16 10:55:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714571 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 EAF6FC04FE0 for ; Wed, 16 Aug 2023 10:56:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244143AbjHPK4H (ORCPT ); Wed, 16 Aug 2023 06:56:07 -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 S244002AbjHPKzl (ORCPT ); Wed, 16 Aug 2023 06:55:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B3A81984; Wed, 16 Aug 2023 03:55:40 -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 281A06555F; Wed, 16 Aug 2023 10:55:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7381DC433C9; Wed, 16 Aug 2023 10:55:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183339; bh=Y8jrcsirIkNz5+yJ0roZSoi6QKBn7sM9JY7d66gE/H8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nWho2sN2qAh/5RsypM+m0TKtkt73PKKWl6h0liiAY3Q4ry4rRR7kk7wqPxJG8VL2S us+OiPmNypy9EmV0HMwTN0mz/+KS8c0IAI1HC5uapjZ1boINuv5NuHMWmx6d6+jmoC 3xwnogGnK1Vbeu5y4qiw/lOMl34E/mLtLDcjWLFtWuG2in3/OmzI/BzJ9SmKW7Z5vD OuhoXZFxYQcpKBe9W/0V2AfxL0WWqFDEv4wOVcOaUE6Nr7wBFBdZPVIYuw+NLhCCEL a6HSNI956p6eDtvtYqOX8Q5+sX6LahZO8bId10bMMQ+xiovyTvgXjN3UrxPFLAgcew 54W1tbse5r6wQ== 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/10] tty: tty_buffer: warn if losing flags in __tty_insert_flip_string_flags() Date: Wed, 16 Aug 2023 12:55:24 +0200 Message-ID: <20230816105530.3335-5-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org And add a WARN_ON_ONCE(need_flags) to make sure we are not losing flags in __tty_insert_flip_string_flags(). Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/tty_buffer.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index 94a88dc05a54..c101b4ab737e 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -325,6 +325,9 @@ int __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, flags += space; } else if (tb->flags) { memset(flag_buf_ptr(tb, tb->used), flags[0], space); + } else { + /* tb->flags should be available once requested */ + WARN_ON_ONCE(need_flags); } tb->used += space; From patchwork Wed Aug 16 10:55:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714570 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 D9741C04A94 for ; Wed, 16 Aug 2023 10:56:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244159AbjHPK4I (ORCPT ); Wed, 16 Aug 2023 06:56:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244007AbjHPKzn (ORCPT ); Wed, 16 Aug 2023 06:55:43 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1477F1984; Wed, 16 Aug 2023 03:55: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 A64B064FFF; Wed, 16 Aug 2023 10:55:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 08249C433C7; Wed, 16 Aug 2023 10:55:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183341; bh=bQUbntuVjuhv84eUfQ4u1fCZj9wEJvWe7HvkbIm42oc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UbbghvJxgjg9AwRxAtkDuW2q2OsKG4Lt4NHz3/3X0C+UX4aSUTaD3hHyV6Jbi0uYs P2fzsf/jZuWL94dJ6yAcdV32YHPXmkSrpr9rlQbpEUsKOnaX48p68s2aCxiRLs8Ztg IXDM9qZ6uw1z6VIWYvdsFs6Ewd0JKwupS79yZkoFtoSvE9gXaTl0DxS6RFxoE9W8Wh 5RvxbKfnfQvd2VFT+rbGecTZe1aDIZAY0o52rfrqrcVjTgDb7S50QeTG7dP8UwkiLB JfnfrjeFeQ13j4PArnwiN5loY6FGB4WQbnh7b4BgqdJmeXSD2fY4qe0/c3fR0dlsb8 3WH1OwVcGynTQ== 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/10] tty: tty_buffer: switch insert functions to size_t Date: Wed, 16 Aug 2023 12:55:25 +0200 Message-ID: <20230816105530.3335-6-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org All the functions accept size_t as a size argument. They finally return the same size (or less). It is quite unexpected that they return a signed value and can confuse users to check for negative values. Instead, return the same size_t as accepted to make clear we return values >= 0, where zero in fact means failure. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/tty_buffer.c | 12 ++++++------ include/linux/tty_flip.h | 24 ++++++++++++------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index c101b4ab737e..598891e53031 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -303,16 +303,16 @@ int tty_buffer_request_room(struct tty_port *port, size_t size) } EXPORT_SYMBOL_GPL(tty_buffer_request_room); -int __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, - const u8 *flags, bool mutable_flags, - size_t size) +size_t __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, + const u8 *flags, bool mutable_flags, + size_t size) { bool need_flags = mutable_flags || flags[0] != TTY_NORMAL; - int copied = 0; + size_t copied = 0; do { - int goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); - int space = __tty_buffer_request_room(port, goal, need_flags); + size_t goal = min_t(size_t, size - copied, TTY_BUFFER_PAGE); + size_t space = __tty_buffer_request_room(port, goal, need_flags); struct tty_buffer *tb = port->buf.tail; if (unlikely(space == 0)) diff --git a/include/linux/tty_flip.h b/include/linux/tty_flip.h index 8b781f709605..569747364ae5 100644 --- a/include/linux/tty_flip.h +++ b/include/linux/tty_flip.h @@ -10,9 +10,9 @@ struct tty_ldisc; int tty_buffer_set_limit(struct tty_port *port, int limit); unsigned int tty_buffer_space_avail(struct tty_port *port); int tty_buffer_request_room(struct tty_port *port, size_t size); -int __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, - const u8 *flags, bool mutable_flags, - size_t size); +size_t __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, + const u8 *flags, bool mutable_flags, + size_t size); int tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size); void tty_flip_buffer_push(struct tty_port *port); int __tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag); @@ -29,9 +29,9 @@ int __tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag); * * Returns: the number added. */ -static inline int tty_insert_flip_string_fixed_flag(struct tty_port *port, - const u8 *chars, u8 flag, - size_t size) +static inline size_t tty_insert_flip_string_fixed_flag(struct tty_port *port, + const u8 *chars, u8 flag, + size_t size) { return __tty_insert_flip_string_flags(port, chars, &flag, false, size); } @@ -48,15 +48,15 @@ static inline int tty_insert_flip_string_fixed_flag(struct tty_port *port, * * Returns: the number added. */ -static inline int tty_insert_flip_string_flags(struct tty_port *port, - const u8 *chars, const u8 *flags, - size_t size) +static inline size_t tty_insert_flip_string_flags(struct tty_port *port, + const u8 *chars, + const u8 *flags, size_t size) { return __tty_insert_flip_string_flags(port, chars, flags, true, size); } -static inline int tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag) +static inline size_t tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag) { struct tty_buffer *tb = port->buf.tail; int change; @@ -71,8 +71,8 @@ static inline int tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag) return __tty_insert_flip_char(port, ch, flag); } -static inline int tty_insert_flip_string(struct tty_port *port, - const u8 *chars, size_t size) +static inline size_t tty_insert_flip_string(struct tty_port *port, + const u8 *chars, size_t size) { return tty_insert_flip_string_fixed_flag(port, chars, TTY_NORMAL, size); } From patchwork Wed Aug 16 10:55:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714567 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 8885AC0729B for ; Wed, 16 Aug 2023 10:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244166AbjHPK4I (ORCPT ); Wed, 16 Aug 2023 06:56:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244019AbjHPKzo (ORCPT ); Wed, 16 Aug 2023 06:55:44 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A41C81984; Wed, 16 Aug 2023 03:55: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 3EF8F64143; Wed, 16 Aug 2023 10:55:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86644C433C9; Wed, 16 Aug 2023 10:55:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183342; bh=K3LGKa2ZBj92Ono8nsGKS9bVk7dhEAszS/T9RyhEzzI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Mx/BgIO79cD7ufaBwQj8Hv3NtIscu7Y6hpXUH1fjtmWcrSLr57lIy5Ele1K+Jy9WG RC/772MpwV/7JIli+WSTF9UuGdAFFBfFCdMQ+ynGF9FmpPdwVCNWHMJFU5kBwUTep4 GLdPZgRdwVAnq9NEugJ3kbF5sV0+Jafzv8dUpBro8SJnYonKH4OIpFkUgOQDkFqj9W KjPyG97VMQnvj8YaVnWrjX4xVDKuhjxNJoEoAQksm3CwOdWNpXtqfKVux74SWgi93Y O3Q5Q9Ya1ARrY3mxqzgcLOqebIOiBiR3G91NPqrMbCy556JgvyhsEoSNGsvth92eZb JzCmfDcgGRSYA== 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/10] tty: tty_buffer: let tty_prepare_flip_string() return size_t Date: Wed, 16 Aug 2023 12:55:26 +0200 Message-ID: <20230816105530.3335-7-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org The same as in the previous patch, tty_prepare_flip_string() accepts size_t as an size argument. It returns the same size (or less). It is unexpected that it returns a signed value and can confuse users to check for negative values. Instead, return the same size_t as accepted to make clear we return values >= 0, where zero in fact means failure. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/tty_buffer.c | 5 +++-- include/linux/tty_flip.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index 598891e53031..4f84466498f7 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -383,9 +383,9 @@ EXPORT_SYMBOL(__tty_insert_flip_char); * Returns: the length available and buffer pointer (@chars) to the space which * is now allocated and accounted for as ready for normal characters. */ -int tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size) +size_t tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size) { - int space = __tty_buffer_request_room(port, size, false); + size_t space = __tty_buffer_request_room(port, size, false); if (likely(space)) { struct tty_buffer *tb = port->buf.tail; @@ -395,6 +395,7 @@ int tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size) memset(flag_buf_ptr(tb, tb->used), TTY_NORMAL, space); tb->used += space; } + return space; } EXPORT_SYMBOL_GPL(tty_prepare_flip_string); diff --git a/include/linux/tty_flip.h b/include/linux/tty_flip.h index 569747364ae5..efd03d9c11f8 100644 --- a/include/linux/tty_flip.h +++ b/include/linux/tty_flip.h @@ -13,7 +13,7 @@ int tty_buffer_request_room(struct tty_port *port, size_t size); size_t __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, const u8 *flags, bool mutable_flags, size_t size); -int tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size); +size_t tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size); void tty_flip_buffer_push(struct tty_port *port); int __tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag); From patchwork Wed Aug 16 10:55:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714318 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 69558C07E8A for ; Wed, 16 Aug 2023 10:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244170AbjHPK4J (ORCPT ); Wed, 16 Aug 2023 06:56:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244113AbjHPKzq (ORCPT ); Wed, 16 Aug 2023 06:55:46 -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 2C23313E; Wed, 16 Aug 2023 03:55:45 -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 B1FA864FFF; Wed, 16 Aug 2023 10:55:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 10BB9C433CA; Wed, 16 Aug 2023 10:55:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183344; bh=BTplnn7EuC4UzkRD7loDFaPokm16ysj8tGKZLoZSkcg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c9J1eLhh0K2OgPBGL5wyV4iSoeO+gbdgIACG7mORSBB0F22o1mGrxixfvTK6IAeyR I69Q5FEE7+0mT7rO2Dt7NNBTyuz+/YV7MWmILvGUs+udpxQ2trH1t/J6XDFC3NyOx2 Zp2FeKR5epl64hLtMaHRdsZ3OKFDsjY+ASoC5a0TKHvr568gScFTDUfunBHJMYAliR Pv24rECuYbblzKJdin+FYYTxG7/+ud/cZk5URiB1SeSu8ZGR27xTV4lVIInSsZhf4J r2PJAZz+LIT9+rs8BnlDWaJXljdWHbRBGyPbfzfo2ajkSUrrPgxMwP3jhllf5exM9K NrwJvAwb2Q2IA== 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/10] tty: tty_buffer: use __tty_insert_flip_string_flags() in tty_insert_flip_char() Date: Wed, 16 Aug 2023 12:55:27 +0200 Message-ID: <20230816105530.3335-8-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org Use __tty_insert_flip_string_flags() for the slow path of tty_insert_flip_char(). The former is generic enough, so there is no reason to reimplement the injection once again. So now we have a single function stuffing into tty buffers. Signed-off-by: Jiri Slaby (SUSE) --- Documentation/driver-api/tty/tty_buffer.rst | 3 ++- drivers/tty/tty_buffer.c | 26 --------------------- include/linux/tty_flip.h | 12 +++++++--- 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/Documentation/driver-api/tty/tty_buffer.rst b/Documentation/driver-api/tty/tty_buffer.rst index 774dc119c312..4b5ca1776d4f 100644 --- a/Documentation/driver-api/tty/tty_buffer.rst +++ b/Documentation/driver-api/tty/tty_buffer.rst @@ -15,11 +15,12 @@ Flip Buffer Management ====================== .. kernel-doc:: drivers/tty/tty_buffer.c - :identifiers: tty_prepare_flip_string __tty_insert_flip_char + :identifiers: tty_prepare_flip_string tty_flip_buffer_push tty_ldisc_receive_buf .. kernel-doc:: include/linux/tty_flip.h :identifiers: tty_insert_flip_string_fixed_flag tty_insert_flip_string_flags + tty_insert_flip_char ---- diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index 4f84466498f7..e162318d6c31 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -343,32 +343,6 @@ size_t __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, } EXPORT_SYMBOL(__tty_insert_flip_string_flags); -/** - * __tty_insert_flip_char - add one character to the tty buffer - * @port: tty port - * @ch: character - * @flag: flag byte - * - * Queue a single byte @ch to the tty buffering, with an optional flag. This is - * the slow path of tty_insert_flip_char(). - */ -int __tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag) -{ - struct tty_buffer *tb; - bool flags = flag != TTY_NORMAL; - - if (!__tty_buffer_request_room(port, 1, flags)) - return 0; - - tb = port->buf.tail; - if (tb->flags) - *flag_buf_ptr(tb, tb->used) = flag; - *char_buf_ptr(tb, tb->used++) = ch; - - return 1; -} -EXPORT_SYMBOL(__tty_insert_flip_char); - /** * tty_prepare_flip_string - make room for characters * @port: tty port diff --git a/include/linux/tty_flip.h b/include/linux/tty_flip.h index efd03d9c11f8..af4fce98f64e 100644 --- a/include/linux/tty_flip.h +++ b/include/linux/tty_flip.h @@ -15,7 +15,6 @@ size_t __tty_insert_flip_string_flags(struct tty_port *port, const u8 *chars, size_t size); size_t tty_prepare_flip_string(struct tty_port *port, u8 **chars, size_t size); void tty_flip_buffer_push(struct tty_port *port); -int __tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag); /** * tty_insert_flip_string_fixed_flag - add characters to the tty buffer @@ -55,7 +54,14 @@ static inline size_t tty_insert_flip_string_flags(struct tty_port *port, return __tty_insert_flip_string_flags(port, chars, flags, true, size); } - +/** + * tty_insert_flip_char - add one character to the tty buffer + * @port: tty port + * @ch: character + * @flag: flag byte + * + * Queue a single byte @ch to the tty buffering, with an optional flag. + */ static inline size_t tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag) { struct tty_buffer *tb = port->buf.tail; @@ -68,7 +74,7 @@ static inline size_t tty_insert_flip_char(struct tty_port *port, u8 ch, u8 flag) *char_buf_ptr(tb, tb->used++) = ch; return 1; } - return __tty_insert_flip_char(port, ch, flag); + return __tty_insert_flip_string_flags(port, &ch, &flag, false, 1); } static inline size_t tty_insert_flip_string(struct tty_port *port, From patchwork Wed Aug 16 10:55:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714569 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 3770BC04FE1 for ; Wed, 16 Aug 2023 10:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244192AbjHPK4J (ORCPT ); Wed, 16 Aug 2023 06:56:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244119AbjHPKzr (ORCPT ); Wed, 16 Aug 2023 06:55:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A64261984; Wed, 16 Aug 2023 03:55:46 -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 3F6EE63FBB; Wed, 16 Aug 2023 10:55:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EC36C433C9; Wed, 16 Aug 2023 10:55:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183345; bh=gDvRR64IFGgEzqSspbkO9kJwr/Hf7Tn7Fvxm8rbBCB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hkGYpNqRHxbM+E6Ovm/uyjkJvUEkiJThsfg74hFdwDsp38j05x0Pwnp5qpZfYoOHL pFmv/EiEMngoI2fYL8OQfvhDa/Fds73rI217YwW3FUR4YDB0s37lvLjh65qmiU3v7s Gwux/QKuVdiFlGLUYfNi9QUX7V114HeU5MJs94kDB7bW4iYdci4yH1hNpbeQ7iZl+W oW/KfxCUv5fc5bgHCQZ8wN590xKlUi8oDnSn9o46Zv75Is9xNAVqrYt0BdGIp7xlBD qTQt0dUWOuz72E9iQ9aIMu/IQReg7cQGI8eY/DmbLH61qSXXnRbbhdnyDXpRj3Sz9n 8AXXO0q8g6Iaw== 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/10] tty: tty_buffer: better types in __tty_buffer_request_room() Date: Wed, 16 Aug 2023 12:55:28 +0200 Message-ID: <20230816105530.3335-9-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org * use bool for 'change' as it holds a result of a boolean. * use size_t for 'left', so it is the same as 'size' which it is compared to. Both are supposed to contain an unsigned value. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/tty_buffer.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index e162318d6c31..414bb7f9155f 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -263,7 +263,8 @@ static int __tty_buffer_request_room(struct tty_port *port, size_t size, { struct tty_bufhead *buf = &port->buf; struct tty_buffer *b, *n; - int left, change; + size_t left; + bool change; b = buf->tail; if (!b->flags) From patchwork Wed Aug 16 10:55:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714568 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 A77B6C07E8B for ; Wed, 16 Aug 2023 10:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244201AbjHPK4J (ORCPT ); Wed, 16 Aug 2023 06:56:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244123AbjHPKzt (ORCPT ); Wed, 16 Aug 2023 06:55:49 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3446613E; Wed, 16 Aug 2023 03:55:48 -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 C26E464FFF; Wed, 16 Aug 2023 10:55:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 22BBCC433CA; Wed, 16 Aug 2023 10:55:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183347; bh=aK6+S+7t1XjbCYZWYAvH3oq+O1bDCKqIuzMFLq9adSI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WUO/agXF5SJIYVd1b6FJjwrV7WG6+KA44ikB9ADlRtGzhz8Y3j3RBJR65ub6k3WX8 bqS2qtSZs7TnVKFDUK2wUf/MbpfcsRpn57Ier2lyVswAvOmYjj8WIIKcg867g933yI Ou94V3Bxt6le/PVQPvgImqDtW15uouZ/dTIiJTrOkl5xQGaM9T3mhEFS+4tjkIJJjs DX65uX66bXc1z+Ej5vCkYWZ4mDry+v2ztAN7eQGWxJNElUFsfDDDtkV8kDei3trSnB OJTqM22wtZDw0rorE3So9zuV98cBWFsKWqPxa3rsrEGRLkRBSKN+/CUCX3mPKOWhSD qbXmTWls0LSAA== 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/10] tty: tty_buffer: initialize variables in initializers already Date: Wed, 16 Aug 2023 12:55:29 +0200 Message-ID: <20230816105530.3335-10-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org It makes the code both more compact, and more understandable. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/tty_buffer.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index 414bb7f9155f..44c0adaec850 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -262,17 +262,10 @@ static int __tty_buffer_request_room(struct tty_port *port, size_t size, bool flags) { struct tty_bufhead *buf = &port->buf; - struct tty_buffer *b, *n; - size_t left; - bool change; + struct tty_buffer *n, *b = buf->tail; + size_t left = (b->flags ? 1 : 2) * b->size - b->used; + bool change = !b->flags && flags; - b = buf->tail; - if (!b->flags) - left = 2 * b->size - b->used; - else - left = b->size - b->used; - - change = !b->flags && flags; if (change || left < size) { /* This is the slow path - looking for new buffers to use */ n = tty_buffer_alloc(port, size); From patchwork Wed Aug 16 10:55:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 714317 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 9817BC07E8C for ; Wed, 16 Aug 2023 10:56:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244205AbjHPK4K (ORCPT ); Wed, 16 Aug 2023 06:56:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244125AbjHPKzu (ORCPT ); Wed, 16 Aug 2023 06:55:50 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEF3E13E; Wed, 16 Aug 2023 03:55:49 -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 4AFE564C77; Wed, 16 Aug 2023 10:55:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0BDFC433C8; Wed, 16 Aug 2023 10:55:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692183348; bh=VEJwLY7hoPdNX4XnvykJ3RU6LGaAEAvuCCAn2+uGWxk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f2XDJqTl/pi4RpIiL4mEcVSC4RFSFu6dF9VdWLJNMGOyt1fMTbKbHwaOaU15B4Rs8 NpLR5QsCB3Szt52ZVkdxT9ju8/sxRQp8yZ7eyZufL0ja6nDcq4IF/mBlbzanRcaoMg 3YHR9LyFfb8cmb5tuzWKcb4imdnlivnrntNKaqWX4bmY92Z+BV0ceawAVY78ev2ucL wftcYbM/6RR2wVbf3LiW41SX32BBkut/iNUFfb/mrVmne1G3qmQy6j4jkp5WHdC0m7 /w6TFbX/MT5a2mcSHyofaE34a+ryKAjro+N0UFW5/S5FC6/hgv2/WeKERpxLuxnmjW wlVtmqR8WfEIQ== 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/10] tty: tty_buffer: invert conditions in __tty_buffer_request_room() Date: Wed, 16 Aug 2023 12:55:30 +0200 Message-ID: <20230816105530.3335-11-jirislaby@kernel.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230816105530.3335-1-jirislaby@kernel.org> References: <20230816105530.3335-1-jirislaby@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-serial@vger.kernel.org We are used to handle "bad" states in the 'if's in the kernel. Refactor (invert the two conditions in) __tty_buffer_request_room(), so that the code returns from the fast paths immediately instead of postponing to the heavy end of the function. Signed-off-by: Jiri Slaby (SUSE) --- drivers/tty/tty_buffer.c | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/drivers/tty/tty_buffer.c b/drivers/tty/tty_buffer.c index 44c0adaec850..5f6d0cf67571 100644 --- a/drivers/tty/tty_buffer.c +++ b/drivers/tty/tty_buffer.c @@ -266,28 +266,28 @@ static int __tty_buffer_request_room(struct tty_port *port, size_t size, size_t left = (b->flags ? 1 : 2) * b->size - b->used; bool change = !b->flags && flags; - if (change || left < size) { - /* This is the slow path - looking for new buffers to use */ - n = tty_buffer_alloc(port, size); - if (n != NULL) { - n->flags = flags; - buf->tail = n; - /* - * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() - * ensures they see all buffer data. - */ - smp_store_release(&b->commit, b->used); - /* - * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() - * ensures the latest commit value can be read before the head - * is advanced to the next buffer. - */ - smp_store_release(&b->next, n); - } else if (change) - size = 0; - else - size = left; - } + if (!change && left >= size) + return size; + + /* This is the slow path - looking for new buffers to use */ + n = tty_buffer_alloc(port, size); + if (n == NULL) + return change ? 0 : left; + + n->flags = flags; + buf->tail = n; + /* + * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() + * ensures they see all buffer data. + */ + smp_store_release(&b->commit, b->used); + /* + * Paired w/ acquire in flush_to_ldisc() and lookahead_bufs() + * ensures the latest commit value can be read before the head + * is advanced to the next buffer. + */ + smp_store_release(&b->next, n); + return size; }