Commit Graph

275 Commits

Author SHA1 Message Date
Andrew Halaney 810d81609d tty: invert return values of tty_{,un}throttle_safe()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 5b4f9cf3cc339565be1ecd64d608b6ffdfba7c1e
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Tue Sep 19 10:51:48 2023 +0200

    tty: invert return values of tty_{,un}throttle_safe()

    If tty_{,un}throttle_safe() returned true on success (similar to
    *_trylock()), it would make the conditions in callers more obvious. So
    perform the switch to these inverted values (and fix the callers).

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230919085156.1578-8-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:12 -04:00
Andrew Halaney f9c20f2371 tty: n_tty: use do-while in n_tty_check_{,un}throttle()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 043c8a7c01ec4b13aa0da11a86425241937b112c
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Tue Sep 19 10:51:46 2023 +0200

    tty: n_tty: use do-while in n_tty_check_{,un}throttle()

    This change gets rid of the complicated exit from the loops. It can be
    done much easier using do-while loops.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230919085156.1578-6-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:12 -04:00
Andrew Halaney 27aa96a1c6 tty: n_tty: invert the condition in copy_from_read_buf()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit c2b0fb9f69987ddec6bf21f95157603ea4c83aff
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Tue Sep 19 10:51:45 2023 +0200

    tty: n_tty: invert the condition in copy_from_read_buf()

    Make "no numbers available" a fast quit from the function. And do the
    heavy work outside the 'if'. This makes the code more understandable and
    conforming to the common kernel coding style.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20230919085156.1578-5-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:12 -04:00
Andrew Halaney 960de1d8ac tty: n_tty: use min3() in copy_from_read_buf()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 72369f2d493d4c0f4f0ed5a66b19c6912c4837ef
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Tue Sep 19 10:51:44 2023 +0200

    tty: n_tty: use min3() in copy_from_read_buf()

    n is a minimum of:
    * available chars in the ring buffer
    * available chars in the ring buffer till the end of the ring buffer
    * requested number (*nr)

    We can use min3() for that instead of two min()s.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230919085156.1578-4-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:12 -04:00
Andrew Halaney 4ad44de775 tty: n_tty: rename and retype 'retval' in n_tty_ioctl()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 1e619477a9c8d6e2ec05a53cda97558fdf9f440e
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Tue Sep 19 10:51:43 2023 +0200

    tty: n_tty: rename and retype 'retval' in n_tty_ioctl()

    The value stored to the current 'retval' is number of characters. It is
    both obtained and put to user as unsigned. So make its type unsigned.
    And provided it's not a "return value" per se, rename it to 'num'.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230919085156.1578-3-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:12 -04:00
Andrew Halaney b887f3403d tty: n_tty: use 'retval' instead of 'c'
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 4a2ad26613867a9a6c49abf7b9319e2a5f6671b0
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Tue Sep 19 10:51:42 2023 +0200

    tty: n_tty: use 'retval' instead of 'c'

    In n_tty_read(), there is a separate int variable 'c' and is used only
    to hold an int value returned from job_control(). There is also a
    'retval' variable typed ssize_t. So drop this single occurrence of 'c'
    and reuse 'retval' which is used on all other places to hold the value
    returned from n_tty_read().

    Note that 'retval' needs not be initialized now. Drop that.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230919085156.1578-2-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:12 -04:00
Andrew Halaney a23759574b tty: n_tty: deduplicate copy code in n_tty_receive_buf_real_raw()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit a84853c5954fe2c1b97db2f005ae156dc29ae233
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:47 2023 +0200

    tty: n_tty: deduplicate copy code in n_tty_receive_buf_real_raw()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-15-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:02 -04:00
Andrew Halaney 79172e1fb2 tty: n_tty: extract ECHO_OP processing to a separate function
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 2aa91851ffa7cdfc0a63330d273115d38324b585
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:46 2023 +0200

    tty: n_tty: extract ECHO_OP processing to a separate function

    __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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-14-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:02 -04:00
Andrew Halaney 65f6ade7b2 tty: n_tty: unify counts to size_t
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit e30364c70895c1ba90b28b85a82aa0d98aab8c81
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:45 2023 +0200

    tty: n_tty: unify counts to size_t

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-13-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:02 -04:00
Andrew Halaney aa597c64ca tty: n_tty: use u8 for chars and flags
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit b9b96b2089e9563a77a69e0fcfbedc5285ce890c
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:44 2023 +0200

    tty: n_tty: use u8 for chars and flags

    Unify with the tty layer and use u8 for both chars and flags.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-12-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:02 -04:00
Andrew Halaney bad1d2eaf6 tty: n_tty: simplify chars_in_buffer()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit d88c3c2675f9d41c15297fa001e79812f4dcc9dd
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:43 2023 +0200

    tty: n_tty: simplify chars_in_buffer()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-11-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:02 -04:00
Andrew Halaney 2d3ed8a7f1 tty: n_tty: remove unsigned char casts from character constants
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 046b44ab0f5a87ba5788606155c5a6761169616a
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:42 2023 +0200

    tty: n_tty: remove unsigned char casts from character constants

    We compile with -funsigned-char, so all character constants are already
    unsigned chars. Therefore, remove superfluous casts.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-10-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney 2c7b4ef6b8 tty: n_tty: move newline handling to a separate function
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 008304079da79ab8dd122fe9e581494d10c0a3cc
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:41 2023 +0200

    tty: n_tty: move newline handling to a separate function

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-9-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney d44a934d64 tty: n_tty: move canon handling to a separate function
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 102dc8aac8d04be7ec6ee030962f7a40dc6d9731
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:40 2023 +0200

    tty: n_tty: move canon handling to a separate function

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-8-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney 0365e2a25b tty: n_tty: use MASK() for masking out size bits
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 819287f0f335cf74d6486eb7f3d465b9668bc9d8
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:39 2023 +0200

    tty: n_tty: use MASK() for masking out size bits

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-7-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney bdb248219b tty: n_tty: make n_tty_data::num_overrun unsigned
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit c3b2b26f6eaad5d48fe6dd2d9bada07b193bdb61
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:38 2023 +0200

    tty: n_tty: make n_tty_data::num_overrun unsigned

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-6-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney 762db67982 tty: n_tty: use time_is_before_jiffies() in n_tty_receive_overrun()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 73276e3a1097ff58d9d167fc9a593cc249d1fd6f
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:37 2023 +0200

    tty: n_tty: use time_is_before_jiffies() in n_tty_receive_overrun()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-5-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney ac083ae63f tty: n_tty: use 'num' for writes' counts
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 68d90d5f7b68652287fbcbcfe245c2c8b667d021
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:36 2023 +0200

    tty: n_tty: use 'num' for writes' counts

    We have a separate misnomer 'c' to hold the retuned value from
    tty->ops->write(). Instead, use 'num' already defined on another place
    (and already properly typed).

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-4-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney c0c4207606 tty: n_tty: use output character directly
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit d414034ec901c3821f17bd44b8eaaa5820253417
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:35 2023 +0200

    tty: n_tty: use output character directly

    There is no point to use a local variable to store the character when we
    can pass it directly. This assignment comes from era when we used to do
    get_user(c, b). We no longer need this, so fix this.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-3-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney 08fbf24276 tty: n_tty: make flow of n_tty_receive_buf_common() a bool
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 0d029ab8a05b5a21af9425c02816f5d6de054b7e
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Sun Aug 27 09:41:34 2023 +0200

    tty: n_tty: make flow of n_tty_receive_buf_common() a bool

    The 'flow' parameter of n_tty_receive_buf_common() is meant to be a
    boolean value. So use bool and alter call sites accordingly.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230827074147.2287-2-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:26:01 -04:00
Andrew Halaney 0d2b49e6b3 tty: ldops: unify to u8
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 49b8220cee4aa3d7aaaf42fd7f316b7f8fb710da
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Thu Aug 10 11:15:05 2023 +0200

    tty: ldops: unify to u8

    Some hooks in struct tty_ldisc_ops still reference buffers by 'unsigned
    char'. Unify to 'u8' as the rest of the tty layer does.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Cc: Marcel Holtmann <marcel@holtmann.org>
    Cc: Johan Hedberg <johan.hedberg@gmail.com>
    Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
    Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Eric Dumazet <edumazet@google.com>
    Cc: Jakub Kicinski <kuba@kernel.org>
    Cc: Paolo Abeni <pabeni@redhat.com>
    Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
    Link: https://lore.kernel.org/r/20230810091510.13006-32-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:57 -04:00
Andrew Halaney 929d93677d tty: use u8 for flags
JIRA: https://issues.redhat.com/browse/RHEL-24205
Conflicts: Skipping the drivers/net/mctp/mctp-serial.c as it is
           not present in cs9

commit 892bc209f250fb49ddca31c74d2c7b1126a7a61a
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Thu Aug 10 11:14:51 2023 +0200

    tty: use u8 for flags

    This makes all those 'char's an explicit 'u8'. This is part of the
    continuing unification of chars and flags to be consistent u8.

    This approaches tty_port_default_receive_buf().

    Note that we do not change signedness as we compile with
    -funsigned-char.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Cc: William Hubbs <w.d.hubbs@gmail.com>
    Cc: Chris Brannon <chris@the-brannons.com>
    Cc: Kirk Reiser <kirk@reisers.ca>
    Cc: Samuel Thibault <samuel.thibault@ens-lyon.org>
    Cc: Marcel Holtmann <marcel@holtmann.org>
    Cc: Johan Hedberg <johan.hedberg@gmail.com>
    Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
    Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
    Cc: Arnd Bergmann <arnd@arndb.de>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Eric Dumazet <edumazet@google.com>
    Cc: Jakub Kicinski <kuba@kernel.org>
    Cc: Paolo Abeni <pabeni@redhat.com>
    Cc: Max Staudt <max@enpas.org>
    Cc: Wolfgang Grandegger <wg@grandegger.com>
    Cc: Marc Kleine-Budde <mkl@pengutronix.de>
    Cc: Dario Binacchi <dario.binacchi@amarulasolutions.com>
    Cc: Andreas Koensgen <ajk@comnets.uni-bremen.de>
    Cc: Jeremy Kerr <jk@codeconstruct.com.au>
    Cc: Matt Johnston <matt@codeconstruct.com.au>
    Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
    Cc: Liam Girdwood <lgirdwood@gmail.com>
    Cc: Mark Brown <broonie@kernel.org>
    Cc: Jaroslav Kysela <perex@perex.cz>
    Cc: Takashi Iwai <tiwai@suse.com>
    Acked-by: Mark Brown <broonie@kernel.org>
    Link: https://lore.kernel.org/r/20230810091510.13006-18-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:56 -04:00
Andrew Halaney 80058b320b tty: use u8 for chars
JIRA: https://issues.redhat.com/browse/RHEL-24205
Conflicts: Skipping the drivers/net/mctp/mctp-serial.c as it is
           not present in cs9

commit a8d9cd2318606627d3c0e4747dbd7bbc44c48e27
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Thu Aug 10 11:14:50 2023 +0200

    tty: use u8 for chars

    This makes all those 'unsigned char's an explicit 'u8'. This is part of
    the continuing unification of chars and flags to be consistent u8.

    This approaches tty_port_default_receive_buf(). Flags to be next.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Cc: William Hubbs <w.d.hubbs@gmail.com>
    Cc: Chris Brannon <chris@the-brannons.com>
    Cc: Kirk Reiser <kirk@reisers.ca>
    Cc: Samuel Thibault <samuel.thibault@ens-lyon.org>
    Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
    Cc: Arnd Bergmann <arnd@arndb.de>
    Cc: Max Staudt <max@enpas.org>
    Cc: Wolfgang Grandegger <wg@grandegger.com>
    Cc: Marc Kleine-Budde <mkl@pengutronix.de>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Eric Dumazet <edumazet@google.com>
    Cc: Jakub Kicinski <kuba@kernel.org>
    Cc: Paolo Abeni <pabeni@redhat.com>
    Cc: Dario Binacchi <dario.binacchi@amarulasolutions.com>
    Cc: Andreas Koensgen <ajk@comnets.uni-bremen.de>
    Cc: Jeremy Kerr <jk@codeconstruct.com.au>
    Cc: Matt Johnston <matt@codeconstruct.com.au>
    Cc: Liam Girdwood <lgirdwood@gmail.com>
    Cc: Mark Brown <broonie@kernel.org>
    Cc: Jaroslav Kysela <perex@perex.cz>
    Cc: Takashi Iwai <tiwai@suse.com>
    Cc: Peter Ujfalusi <peter.ujfalusi@gmail.com>
    Acked-by: Mark Brown <broonie@kernel.org>
    Link: https://lore.kernel.org/r/20230810091510.13006-17-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:56 -04:00
Andrew Halaney 129989dbb4 tty: make tty_ldisc_ops::*buf*() hooks operate on size_t
JIRA: https://issues.redhat.com/browse/RHEL-24205
Conflicts: Skipping the drivers/net/mctp/mctp-serial.c as it is
           not present in cs9

commit e8161447bb0ce2d59277e9276012dd1c6f357850
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Thu Aug 10 11:14:49 2023 +0200

    tty: make tty_ldisc_ops::*buf*() hooks operate on size_t

    Count passed to tty_ldisc_ops::receive_buf*(), ::lookahead_buf(), and
    returned from ::receive_buf2() is expected to be size_t. So set it to
    size_t to unify with the rest of the code.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Cc: William Hubbs <w.d.hubbs@gmail.com>
    Cc: Chris Brannon <chris@the-brannons.com>
    Cc: Kirk Reiser <kirk@reisers.ca>
    Cc: Samuel Thibault <samuel.thibault@ens-lyon.org>
    Cc: Marcel Holtmann <marcel@holtmann.org>
    Cc: Johan Hedberg <johan.hedberg@gmail.com>
    Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
    Cc: Dmitry Torokhov <dmitry.torokhov@gmail.com>
    Cc: Arnd Bergmann <arnd@arndb.de>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Eric Dumazet <edumazet@google.com>
    Cc: Jakub Kicinski <kuba@kernel.org>
    Cc: Paolo Abeni <pabeni@redhat.com>
    Cc: Max Staudt <max@enpas.org>
    Cc: Wolfgang Grandegger <wg@grandegger.com>
    Cc: Marc Kleine-Budde <mkl@pengutronix.de>
    Cc: Dario Binacchi <dario.binacchi@amarulasolutions.com>
    Cc: Andreas Koensgen <ajk@comnets.uni-bremen.de>
    Cc: Jeremy Kerr <jk@codeconstruct.com.au>
    Cc: Matt Johnston <matt@codeconstruct.com.au>
    Cc: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
    Cc: Liam Girdwood <lgirdwood@gmail.com>
    Cc: Mark Brown <broonie@kernel.org>
    Cc: Jaroslav Kysela <perex@perex.cz>
    Cc: Takashi Iwai <tiwai@suse.com>
    Acked-by: Mark Brown <broonie@kernel.org>
    Link: https://lore.kernel.org/r/20230810091510.13006-16-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:56 -04:00
Andrew Halaney adc5a0901b n_tty: make many tty parameters const
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 5bedcf70c6be530c6c4a7f78a820dd19566996e0
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Wed Jul 12 08:42:16 2023 +0200

    n_tty: make many tty parameters const

    In many n_tty functions, the 'tty' parameter is used to either obtain
    'ldata', or test the tty flags. So mark 'tty' in them const to make
    obvious that it is only read.

    Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230712064216.12150-5-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:46 -04:00
Andrew Halaney 5648181a36 n_tty: pass ldata to canon_skip_eof() directly
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 32042446c030b677f3bf10081876d9bd078740a2
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Wed Jul 12 08:42:15 2023 +0200

    n_tty: pass ldata to canon_skip_eof() directly

    'tty' is not needed in canon_skip_eof(), so we can pass 'ldata' directly
    instead.

    Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230712064216.12150-4-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:46 -04:00
Andrew Halaney 71da906853 n_tty: simplify and sanitize zero_buffer()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit f6f847ff8d6662977fd7f7199152b87ef029e704
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Wed Jul 12 08:42:14 2023 +0200

    n_tty: simplify and sanitize zero_buffer()

    * 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) <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230712064216.12150-3-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:46 -04:00
Andrew Halaney 3905494743 n_tty: drop fp from n_tty_receive_buf_real_raw()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit b30a3d396b4c4fccc73a4922d594bb589afb5bee
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Wed Jul 12 08:42:13 2023 +0200

    n_tty: drop fp from n_tty_receive_buf_real_raw()

    The 'fp' parameter of n_tty_receive_buf_real_raw() is unused, so drop
    it.

    Signed-off-by: Jiri Slaby (SUSE) <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230712064216.12150-2-jirislaby@kernel.org
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:46 -04:00
Andrew Halaney d86dbd3992 tty: fix hang on tty device with no_room set
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 4903fde8047a28299d1fc79c1a0dcc255e928f12
Author: Hui Li <caelli@tencent.com>
Date:   Thu Apr 6 10:44:50 2023 +0800

    tty: fix hang on tty device with no_room set

    It is possible to hang pty devices in this case, the reader was
    blocking at epoll on master side, the writer was sleeping at
    wait_woken inside n_tty_write on slave side, and the write buffer
    on tty_port was full, we found that the reader and writer would
    never be woken again and blocked forever.

    The problem was caused by a race between reader and kworker:
    n_tty_read(reader):  n_tty_receive_buf_common(kworker):
    copy_from_read_buf()|
                        |room = N_TTY_BUF_SIZE - (ldata->read_head - tail)
                        |room <= 0
    n_tty_kick_worker() |
                        |ldata->no_room = true

    After writing to slave device, writer wakes up kworker to flush
    data on tty_port to reader, and the kworker finds that reader
    has no room to store data so room <= 0 is met. At this moment,
    reader consumes all the data on reader buffer and calls
    n_tty_kick_worker to check ldata->no_room which is false and
    reader quits reading. Then kworker sets ldata->no_room=true
    and quits too.

    If write buffer is not full, writer will wake kworker to flush data
    again after following writes, but if write buffer is full and writer
    goes to sleep, kworker will never be woken again and tty device is
    blocked.

    This problem can be solved with a check for read buffer size inside
    n_tty_receive_buf_common, if read buffer is empty and ldata->no_room
    is true, a call to n_tty_kick_worker is necessary to keep flushing
    data to reader.

    Cc: <stable@vger.kernel.org>
    Fixes: 42458f41d0 ("n_tty: Ensure reader restarts worker for next reader")
    Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Signed-off-by: Hui Li <caelli@tencent.com>
    Message-ID: <1680749090-14106-1-git-send-email-caelli@tencent.com>
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:45 -04:00
Andrew Halaney 0132b95765 n_tty: Reindent if condition
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 37e8b08ada22f5cd4a8e94f6e783d00cd6e6d7e6
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Thu Mar 9 10:20:34 2023 +0200

    n_tty: Reindent if condition

    Align if condition to make it easier to read.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20230309082035.14880-8-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:36 -04:00
Andrew Halaney 8c2a9b5452 n_tty: Cleanup includes
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 8ed012d1be12c183e173c9e0933b34a7073a9c8a
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Thu Mar 9 10:20:33 2023 +0200

    n_tty: Cleanup includes

    n_tty uses:
            - bitmap_zero() from linux/bitmap.h
            - EXPORT_SYMBOL_GPL() from linux/export.h
            - jiffies, time_after() from linux/jiffies.h

    Add includes.

    n_tty uses nothing from:
            - linux/audit.h
            - linux/interrupt.h
            - linux/major.h
            - linux/mm.h
            - linux/module.h
            - linux/timer.h

    Remove those includes.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20230309082035.14880-7-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:36 -04:00
Andrew Halaney 48872560cf n_tty: Use DIV_ROUND_UP() in room calculation
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 7e26c84d02d17e7412e6f2471916fd415654a1ba
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Thu Mar 9 10:20:32 2023 +0200

    n_tty: Use DIV_ROUND_UP() in room calculation

    When PARMRK is set, a character can result in up to 3 chars in the read
    buffer. Receive code calculates for how many characters there (at
    least) is room. Convert an opencoded rounding in the calculation to use
    DIV_ROUND_UP().

    Note: the room variable is decremented afterwards by one which ensures
    the characters will fit into the buffer for real so the code is okay
    despite rounding upwards.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20230309082035.14880-6-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:36 -04:00
Andrew Halaney b8e18b03e3 n_tty: Sort includes alphabetically
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 9db1be84054b7445f911b9efe9bc3c924ffdb543
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Thu Mar 9 10:20:31 2023 +0200

    n_tty: Sort includes alphabetically

    Sort includes in n_tty alphabetically to make it easier to see if an
    include is among the list or not.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20230309082035.14880-5-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:36 -04:00
Andrew Halaney 2470433bde n_tty: Convert no_space_left to space_left boolean
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit b8abba0eb1f5b8a05e864cf00f246e1c066775a2
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Thu Mar 9 10:20:28 2023 +0200

    n_tty: Convert no_space_left to space_left boolean

    The no_space_left variable is only assigned with 0 and 1.

    Change its type to boolean and move negation from its name into the
    check.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20230309082035.14880-2-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:36 -04:00
Andrew Halaney e6cdeb0ed6 n_tty: Rename tail to old_tail in n_tty_read()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 947d66b68f3c4e7cf8f3f3500807b9d2a0de28ce
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Fri Nov 11 16:25:02 2022 +0200

    n_tty: Rename tail to old_tail in n_tty_read()

    The local tail variable in n_tty_read() is used for one purpose, it
    keeps the old tail. Thus, rename it appropriately to improve code
    readability.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Reviewed-by: Jiri Slaby <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/22b37499-ff9a-7fc1-f6e0-58411328d122@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:18 -04:00
Andrew Halaney 7251bb834d tty: Make ldisc ->set_termios() old ktermios const
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 8b7d2d95cf82f8ca034ac65d0f39a2b3359b680f
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Tue Aug 16 14:57:36 2022 +0300

    tty: Make ldisc ->set_termios() old ktermios const

    There should be no reason to adjust old ktermios which is going to get
    discarded anyway.

    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20220816115739.10928-6-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:05 -04:00
Andrew Halaney e051d53b4c tty: Use flow-control char function on closing path
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 65534736d9a5cab5340ae8819e1394b6325e8390
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Mon Jun 6 18:36:52 2022 +0300

    tty: Use flow-control char function on closing path

    Use n_tty_receive_char_flow_ctrl also on the closing path. This makes
    the code cleaner and consistent.

    However, there a small change of regression!

    The earlier closing path has a small difference compared with the
    normal receive path. If START_CHAR and STOP_CHAR are equal, their
    precedence is different depending on which path a character is
    processed. I don't know whether this difference was intentional or
    not, and if equal START_CHAR and STOP_CHAR is actually used anywhere.
    But it feels not so useful corner case.

    While this change would logically belong to those earlier changes,
    having a separate patch for this is useful. If this regresses, bisect
    can pinpoint this change rather than the large patch. Also, this
    change is not necessary to minimal fix for the issue addressed in
    the previous patch.

    Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20220606153652.63554-3-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:56 -04:00
Andrew Halaney 8876a373cc tty: Implement lookahead to process XON/XOFF timely
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 6bb6fa6908ebd3cb4e14cd4f0ce272ec885d2eb0
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Mon Jun 6 18:36:51 2022 +0300

    tty: Implement lookahead to process XON/XOFF timely

    When tty is not read from, XON/XOFF may get stuck into an
    intermediate buffer. As those characters are there to do software
    flow-control, it is not very useful. In the case where neither end
    reads from ttys, the receiving ends might not be able receive the
    XOFF characters and just keep sending more data to the opposite
    direction. This problem is almost guaranteed to occur with DMA
    which sends data in large chunks.

    If TTY is slow to process characters, that is, eats less than given
    amount in receive_buf, invoke lookahead for the rest of the chars
    to process potential XON/XOFF characters.

    We need to keep track of how many characters have been processed by the
    lookahead to avoid processing the flow control char again on the normal
    path. Bookkeeping occurs parallel on two layers (tty_buffer and n_tty)
    to avoid passing the lookahead_count through the whole call chain.

    When a flow-control char is processed, two things must occur:
      a) it must not be treated as normal char
      b) if not yet processed, flow-control actions need to be taken
    The return value of n_tty_receive_char_flow_ctrl() tells caller a), and
    b) is kept internal to n_tty_receive_char_flow_ctrl().

    If characters were previous looked ahead, __receive_buf() makes two
    calls to the appropriate n_tty_receive_buf_* function. First call is
    made with lookahead_done=true for the characters that were subject to
    lookahead earlier and then with lookahead=false for the new characters.
    Either of the calls might be skipped when it has no characters to
    handle.

    Reported-by: Gilles Buloz <gilles.buloz@kontron.com>
    Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20220606153652.63554-2-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:56 -04:00
Andrew Halaney ccf3ab2f72 tty: Rework receive flow control char logic
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 25e02ba60f0fbe65ba07553b5b2b8867726273c4
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Tue Apr 26 17:49:33 2022 +0300

    tty: Rework receive flow control char logic

    Add a helper to check if the character is a flow control one. This
    rework prepares for adding lookahead done check cleanly to
    n_tty_receive_char_flow_ctrl() between n_tty_is_char_flow_ctrl() and
    the actions taken on the flow control characters.

    No functional changes intended.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20220426144935.54893-2-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:48 -04:00
Andrew Halaney 768381ec73 tty: Add function for handling flow control chars
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit ec66b8cf03e5a63de6332c989b0ceebe4ba2937e
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Mon Apr 11 12:48:55 2022 +0300

    tty: Add function for handling flow control chars

    Move receive path flow control character handling to own function.

    No functional changes.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20220411094859.10894-2-ilpo.jarvinen@linux.intel.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:40 -04:00
Andrew Halaney e4af521064 tty: n_tty: Restore EOF push handling behavior
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 65a8b287023da68c4550deab5c764e6891cf1caf
Author: Daniel Gibson <daniel@gibson.sh>
Date:   Wed Mar 30 01:58:10 2022 +0200

    tty: n_tty: Restore EOF push handling behavior

    TTYs in ICANON mode have a special case that allows "pushing" a line
    without a regular EOL character (like newline), by using EOF (the EOT
    character - ASCII 0x4) as a pseudo-EOL. It is silently discarded, so
    the reader of the PTS will receive the line *without* EOF or any other
    terminating character.

    This special case has an edge case: What happens if the readers buffer
    is the same size as the line (without EOF)? Will they be able to tell
    if the whole line is received, i.e. if the next read() will return more
    of the same line or the next line?

    There are two possibilities,  that both have (dis)advantages:

    1. The next read() returns 0. FreeBSD (13.0) and OSX (10.11) do this.
       Advantage: The reader can interpret this as "the line is over".
       Disadvantage: read() returning 0 means EOF, the reader could also
       interpret it as "there's no more data" and stop reading or even
       close the PT.

    2. The next read() returns the next line, the EOF is silently discarded.
       Solaris (or at least OpenIndiana 2021.10) does this, Linux has done
       do this since commit 40d5e0905a ("n_tty: Fix EOF push handling");
       this behavior was recently broken by commit 359303076163 ("tty:
       n_tty: do not look ahead for EOL character past the end of the buffer").
       Advantage: read() won't return 0 (EOF), reader less likely to be
       confused (and things like `while(read(..)>0)` don't break)
       Disadvantage: The reader can't really know if the read() continues
       the last line (that filled the whole read buffer) or starts a
       new line.

    As both options are defensible (and are used by other Unix-likes), it's
    best to stick to the "old" behavior since "n_tty: Fix EOF push handling"
    of 2013, i.e. silently discard that EOF.

    This patch - that I actually got from Linus for testing and only
    modified slightly - restores that behavior by skipping an EOF
    character if it's the next character after reading is done.

    Based on a patch from Linus Torvalds.

    Link: https://bugzilla.kernel.org/show_bug.cgi?id=215611
    Fixes: 359303076163 ("tty: n_tty: do not look ahead for EOL character past the end of the buffer")
    Cc: Peter Hurley <peter@hurleysoftware.com>
    Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
    Cc: Jiri Slaby <jirislaby@kernel.org>
    Reviewed-and-tested-by: Daniel Gibson <daniel@gibson.sh>
    Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
    Signed-off-by: Daniel Gibson <daniel@gibson.sh>
    Link: https://lore.kernel.org/r/20220329235810.452513-2-daniel@gibson.sh
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:37 -04:00
Andrew Halaney 1267675d57 tty: n_tty: do not look ahead for EOL character past the end of the buffer
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 3593030761630e09200072a4bd06468892c27be3
Author: Linus Torvalds <torvalds@linux-foundation.org>
Date:   Tue Feb 15 15:28:00 2022 -0800

    tty: n_tty: do not look ahead for EOL character past the end of the buffer

    Daniel Gibson reports that the n_tty code gets line termination wrong in
    very specific cases:

     "If you feed a line with exactly 64 chars + terminating newline, and
      directly afterwards (without reading) another line into a pseudo
      terminal, the the first read() on the other side will return the 64
      char line *without* terminating newline, and the next read() will
      return the missing terminating newline AND the complete next line (if
      it fits in the buffer)"

    and bisected the behavior to commit 3b830a9c34 ("tty: convert
    tty_ldisc_ops 'read()' function to take a kernel pointer").

    Now, digging deeper, it turns out that the behavior isn't exactly new:
    what changed in commit 3b830a9c34 was that the tty line discipline
    .read() function is now passed an intermediate kernel buffer rather than
    the final user space buffer.

    And that intermediate kernel buffer is 64 bytes in size - thus that
    special case with exactly 64 bytes plus terminating newline.

    The same problem did exist before, but historically the boundary was not
    the 64-byte chunk, but the user-supplied buffer size, which is obviously
    generally bigger (and potentially bigger than N_TTY_BUF_SIZE, which
    would hide the issue entirely).

    The reason is that the n_tty canon_copy_from_read_buf() code would look
    ahead for the EOL character one byte further than it would actually
    copy.  It would then decide that it had found the terminator, and unmark
    it as an EOL character - which in turn explains why the next read
    wouldn't then be terminated by it.

    Now, the reason it did all this in the first place is related to some
    historical and pretty obscure EOF behavior, see commit ac8f3bf883
    ("n_tty: Fix poll() after buffer-limited eof push read") and commit
    40d5e0905a ("n_tty: Fix EOF push handling").

    And the reason for the EOL confusion is that we treat EOF as a special
    EOL condition, with the EOL character being NUL (aka "__DISABLED_CHAR"
    in the kernel sources).

    So that EOF look-ahead also affects the normal EOL handling.

    This patch just removes the look-ahead that causes problems, because EOL
    is much more critical than the historical "EOF in the middle of a line
    that coincides with the end of the buffer" handling ever was.

    Now, it is possible that we should indeed re-introduce the "look at next
    character to see if it's a EOF" behavior, but if so, that should be done
    not at the kernel buffer chunk boundary in canon_copy_from_read_buf(),
    but at a higher level, when we run out of the user buffer.

    In particular, the place to do that would be at the top of
    'n_tty_read()', where we check if it's a continuation of a previously
    started read, and there is no more buffer space left, we could decide to
    just eat the __DISABLED_CHAR at that point.

    But that would be a separate patch, because I suspect nobody actually
    cares, and I'd like to get a report about it before bothering.

    Fixes: 3b830a9c34 ("tty: convert tty_ldisc_ops 'read()' function to take a kernel pointer")
    Fixes: ac8f3bf883 ("n_tty: Fix  poll() after buffer-limited eof push read")
    Fixes: 40d5e0905a ("n_tty: Fix EOF push handling")
    Link: https://bugzilla.kernel.org/show_bug.cgi?id=215611
    Reported-and-tested-by: Daniel Gibson <metalcaedes@gmail.com>
    Cc: Peter Hurley <peter@hurleysoftware.com>
    Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
    Cc: Jiri Slaby <jirislaby@kernel.org>
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:30 -04:00
Andrew Halaney 20ed287c01 n_tty: wake up poll(POLLRDNORM) on receiving data
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit c816b2e65b0e86b95011418cad334f0524fc33b8
Author: TATSUKAWA KOSUKE (立川 江介) <tatsu-ab1@nec.com>
Date:   Wed Jan 26 23:35:02 2022 +0000

    n_tty: wake up poll(POLLRDNORM) on receiving data

    The poll man page says POLLRDNORM is equivalent to POLLIN when used as
    an event.
    $ man poll
    <snip>
                  POLLRDNORM
                         Equivalent to POLLIN.

    However, in n_tty driver, POLLRDNORM does not return until timeout even
    if there is terminal input, whereas POLLIN returns.

    The following test program works until kernel-3.17, but the test stops
    in poll() after commit 57087d5154 ("tty: Fix spurious poll() wakeups").

    [Steps to run test program]
      $ cc -o test-pollrdnorm test-pollrdnorm.c
      $ ./test-pollrdnorm
      foo          <-- Type in something from the terminal followed by [RET].
                       The string should be echoed back.

      ------------------------< test-pollrdnorm.c >------------------------
      #include <stdio.h>
      #include <errno.h>
      #include <poll.h>
      #include <unistd.h>

      void main(void)
      {
            int             n;
            unsigned char   buf[8];
            struct pollfd   fds[1] = {{ 0, POLLRDNORM, 0 }};

            n = poll(fds, 1, -1);
            if (n < 0)
                    perror("poll");
            n = read(0, buf, 8);
            if (n < 0)
                    perror("read");
            if (n > 0)
                    write(1, buf, n);
      }
      ------------------------------------------------------------------------

    The attached patch fixes this problem.  Many calls to
    wake_up_interruptible_poll() in the kernel source code already specify
    "POLLIN | POLLRDNORM".

    Fixes: 57087d5154 ("tty: Fix spurious poll() wakeups")
    Cc: stable@vger.kernel.org
    Signed-off-by: Kosuke Tatsukawa <tatsu-ab1@nec.com>
    Link: https://lore.kernel.org/r/TYCPR01MB81901C0F932203D30E452B3EA5209@TYCPR01MB8190.jpnprd01.prod.outlook.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:29 -04:00
Andrew Halaney abb21705ba tty: reformat kernel-doc in n_tty.c
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 98629663bff8c2831f9cfd2b4e67537b24a48daa
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Fri Nov 26 09:16:04 2021 +0100

    tty: reformat kernel-doc in n_tty.c

    Kernel-doc is a bit strict about some formatting. So fix these:
    1) When there is a tab in comments, it thinks the line is a continuation
       one. So the description of the functions end up as descriptions of
       the last parameter described. Remove the tabs.

    2) Remove newlines before parameters description and after the comments.
       This was not wrong per se, only inconsistent with the rest of the
       file.

    3) Add periods to the end of sentences where appropriate.

    4) Add "()" to function names and "%" to constants, so that they are
       properly highlighted.

    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20211126081611.11001-17-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:23 -04:00
Andrew Halaney 946ae460f5 tty: fix kernel-doc in n_tty.c
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit c66453ce8af8bac78a72ba4e21fd9a86720127d7
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Fri Nov 26 09:16:03 2021 +0100

    tty: fix kernel-doc in n_tty.c

    * process_echoes doc was a misnomer
    * isig and n_tty_receive_char docs were misplaced
    * n_tty_read parameters were incorrect (from pre-cookie times)

    So fix all the warnings at once:
    624: warning: expecting prototype for process_echoes(). Prototype was for __process_echoes() instead
    1110: warning: expecting prototype for isig(). Prototype was for __isig() instead
    1264: warning: expecting prototype for n_tty_receive_char(). Prototype was for n_tty_receive_char_special() instead
    2067: warning: Excess function parameter 'buf' description in 'n_tty_read'
    624: warning: expecting prototype for process_echoes(). Prototype was for __process_echoes() instead
    1110: warning: expecting prototype for isig(). Prototype was for __isig() instead
    1264: warning: expecting prototype for n_tty_receive_char(). Prototype was for n_tty_receive_char_special() instead
    2067: warning: Function parameter or member 'kbuf' not described in 'n_tty_read'
    2067: warning: Function parameter or member 'cookie' not described in 'n_tty_read'
    2067: warning: Function parameter or member 'offset' not described in 'n_tty_read'
    2067: warning: Excess function parameter 'buf' description in 'n_tty_read'

    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20211126081611.11001-16-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:23 -04:00
Andrew Halaney 5a47545be3 tty: remove flags from struct tty_ldisc_ops
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit b468e688240b58ece498c430c377bb81b78a41f1
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Tue Sep 14 11:11:20 2021 +0200

    tty: remove flags from struct tty_ldisc_ops

    The last user was apparently removed by commit a352def21a (tty: Ldisc
    revamp) in 2008. So remove the field completely, the only setter
    (n_tty_inherit_ops) and also its only possible value
    (LDISC_FLAG_DEFINED).

    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20210914091134.17426-2-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:15 -04:00
Patrick Talbert 47e00132d8 Merge: lpfc cs9 (rhel9.1) update
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/862

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2063879

Signed-off-by: Dick Kennedy <dkennedy@redhat.com>

Approved-by: Jarod Wilson <jarod@redhat.com>
Approved-by: John W. Linville <linville@redhat.com>
Approved-by: Ewan D. Milne <emilne@redhat.com>

Conflicts:
- arch/s390/kvm/kvm-s390.c: context differs due to MR !762.

Signed-off-by: Patrick Talbert <ptalbert@redhat.com>
2022-06-17 11:30:18 +02:00
Dick Kennedy db8469c33d all: replace find_next{,_zero}_bit with find_first{,_zero}_bit where appropriate
Bugzilla: http://bugzilla.redhat.com/2063879

commit b5c7e7ec7d3418af2544452b45cc67297c857a86
Author: Yury Norov <yury.norov@gmail.com>
Date:   Sat Aug 14 14:17:03 2021 -0700

    all: replace find_next{,_zero}_bit with find_first{,_zero}_bit where appropriate

    find_first{,_zero}_bit is a more effective analogue of 'next' version if
    start == 0. This patch replaces 'next' with 'first' where things look
    trivial.

    Signed-off-by: Yury Norov <yury.norov@gmail.com>
    Tested-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Signed-off-by: Dick Kennedy <dkennedy@redhat.com>
2022-05-11 15:07:36 -04:00
Gopal Tiwari 4558535265 tty: remove file from tty_ldisc_ops::ioctl and compat_ioctl
Bugzilla: http://bugzilla.redhat.com/2066188

commit d78328bcc4d0e677f2ff83f4ae1f43c933fbd143
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Mon Nov 22 10:45:29 2021 +0100

    tty: remove file from tty_ldisc_ops::ioctl and compat_ioctl

    After the previous patches, noone needs 'file' parameter in neither
    ioctl hook from tty_ldisc_ops. So remove 'file' from both of them.

    Cc: Marcel Holtmann <marcel@holtmann.org>
    Cc: Johan Hedberg <johan.hedberg@gmail.com>
    Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
    Cc: Wolfgang Grandegger <wg@grandegger.com>
    Cc: Marc Kleine-Budde <mkl@pengutronix.de>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Jakub Kicinski <kuba@kernel.org>
    Cc: Andreas Koensgen <ajk@comnets.uni-bremen.de>
    Cc: Paul Mackerras <paulus@samba.org>
    Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com> [NFC]
    Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
    Acked-by: Marc Kleine-Budde <mkl@pengutronix.de>
    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20211122094529.24171-1-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-04-27 11:49:03 +05:30
Gopal Tiwari 65a94cc327 tty: remove file from n_tty_ioctl_helper
Bugzilla: http://bugzilla.redhat.com/2066188

commit 7c783601a3bc22a54cce0fb650259a983c8cafba
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Tue Sep 14 11:11:24 2021 +0200

    tty: remove file from n_tty_ioctl_helper

    After the previous patch, there are no users of 'file' in
    n_tty_ioctl_helper. So remove it also from there.

    Cc: Marcel Holtmann <marcel@holtmann.org>
    Cc: Johan Hedberg <johan.hedberg@gmail.com>
    Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
    Cc: Paul Mackerras <paulus@samba.org>
    Cc: "David S. Miller" <davem@davemloft.net>
    Cc: Jakub Kicinski <kuba@kernel.org>
    Cc: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
    Acked-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20210914091134.17426-6-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Gopal Tiwari <gtiwari@redhat.com>
2022-04-27 11:48:56 +05:30