Commit Graph

112 Commits

Author SHA1 Message Date
Andrew Halaney 79428ce113 tty: tty_buffer: use bool for 'restart' in tty_buffer_unlock_exclusive()
JIRA: https://issues.redhat.com/browse/RHEL-24205

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

    tty: tty_buffer: use bool for 'restart' in tty_buffer_unlock_exclusive()

    It's a boolean value, so no need for 'int' there.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230919085156.1578-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:12 -04:00
Andrew Halaney 36f5ce2b5f tty: tty_buffer: invert conditions in __tty_buffer_request_room()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit ebee41c8490aeb3ef2e7d25f5e97ce45c996da4d
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:30 2023 +0200

    tty: tty_buffer: invert conditions in __tty_buffer_request_room()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:25:59 -04:00
Andrew Halaney 587c90a654 tty: tty_buffer: initialize variables in initializers already
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 035197c908b57804b9407cf119d2c2fe5934ee00
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:29 2023 +0200

    tty: tty_buffer: initialize variables in initializers already

    It makes the code both more compact, and more understandable.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:25:58 -04:00
Andrew Halaney 38fe6caac5 tty: tty_buffer: better types in __tty_buffer_request_room()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 64365743b366ae756d4e9c16a0b639960f417493
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:28 2023 +0200

    tty: tty_buffer: better types in __tty_buffer_request_room()

    * 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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:25:58 -04:00
Andrew Halaney de05bb7628 tty: tty_buffer: use __tty_insert_flip_string_flags() in tty_insert_flip_char()
JIRA: https://issues.redhat.com/browse/RHEL-24205
Conflicts: Skipped the Documentation/ bits as getting them in order is
           proving difficult

commit b49a0ff7328f5f9acfd027906f4c7d313a225361
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:27 2023 +0200

    tty: tty_buffer: use __tty_insert_flip_string_flags() in tty_insert_flip_char()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:25:58 -04:00
Andrew Halaney 6e835694e9 tty: tty_buffer: let tty_prepare_flip_string() return size_t
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 2ce2983c24c11beaeb6ef6da93bf7228679bd08b
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:26 2023 +0200

    tty: tty_buffer: let tty_prepare_flip_string() return size_t

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:25:58 -04:00
Andrew Halaney 5a8fd40086 tty: tty_buffer: switch insert functions to size_t
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 6144922e17677204cbead4ee544699af69785a84
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:25 2023 +0200

    tty: tty_buffer: switch insert functions to size_t

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:25:58 -04:00
Andrew Halaney f0477f53ee tty: tty_buffer: warn if losing flags in __tty_insert_flip_string_flags()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 4a8d99a409d3d194527674a896362992a171cd7b
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:24 2023 +0200

    tty: tty_buffer: warn if losing flags in __tty_insert_flip_string_flags()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:58 -04:00
Andrew Halaney 76d4f49057 tty: tty_buffer: unify tty_insert_flip_string_{fixed_flag,flags}()
JIRA: https://issues.redhat.com/browse/RHEL-24205
Conflicts: Skipped the Documentation/ bits as getting them in order is
           proving difficult

commit c26405fd289b74225b111b8f23e9e7eae1a02513
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:23 2023 +0200

    tty: tty_buffer: unify tty_insert_flip_string_{fixed_flag,flags}()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:58 -04:00
Andrew Halaney 851c7d359c tty: tty_buffer: use struct_size() in tty_buffer_alloc()
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 46bc78c81b65044fa7bd4781e6501b756b3a86bc
Author: Jiri Slaby (SUSE) <jirislaby@kernel.org>
Date:   Wed Aug 16 12:55:22 2023 +0200

    tty: tty_buffer: use struct_size() in tty_buffer_alloc()

    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)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230816105530.3335-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:58 -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 587240a937 tty: switch count in tty_ldisc_receive_buf() to size_t
JIRA: https://issues.redhat.com/browse/RHEL-24205

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

    tty: switch count in tty_ldisc_receive_buf() to size_t

    It comes from both paste_selection() and tty_port_default_receive_buf()
    as unsigned (int and size_t respectively). Switch to size_t to converge
    to that eventually.

    Return the count as size_t too (the two callers above expect that).

    Switch paste_selection()'s type of 'count' too, so that the returned and
    passed type match.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230810091510.13006-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:25:56 -04:00
Andrew Halaney 178584173e tty: switch receive_buf() counts to size_t
JIRA: https://issues.redhat.com/browse/RHEL-24205

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

    tty: switch receive_buf() counts to size_t

    'size_t' is what receive_buf() expects and returns while handling count.
    So switch to 'size_t'.

    This renders both local 'count' and 'rcvd' in flush_to_ldisc() to be
    size_t too.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Link: https://lore.kernel.org/r/20230810091510.13006-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:25:55 -04:00
Andrew Halaney 275e3282ad tty: make tty_port_client_operations operate with u8
JIRA: https://issues.redhat.com/browse/RHEL-24205

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

    tty: make tty_port_client_operations operate with u8

    The parameters are already unsigned chars. So make them explicitly u8s,
    as the rest is going to be unified to u8 eventually too.

    Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
    Cc: Rob Herring <robh@kernel.org>
    Link: https://lore.kernel.org/r/20230810091510.13006-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:25:55 -04:00
Andrew Halaney 848c566cf9 tty: Convert tty_buffer flags to bool
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 2e2b4b896159f9d47d063ccf4ed0a7af9a40f1c5
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Wed Oct 19 13:55:03 2022 +0300

    tty: Convert tty_buffer flags to bool

    The struct tty_buffer has flags which is only used for storing TTYB_NORMAL.
    There is also a few quite confusing operations for checking the presense
    of TTYB_NORMAL. Simplify things by converting flags to bool.

    Despite the name remaining the same, the meaning of "flags" is altered
    slightly by this change. Previously it referred to flags of the buffer
    (only TTYB_NORMAL being used as a flag). After this change, flags tell
    whether the buffer contains/should be allocated with flags array along
    with character data array. It is much more suitable name that
    TTYB_NORMAL was for this purpose, thus the name remains.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20221019105504.16800-1-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:18 -04:00
Andrew Halaney 947c502426 tty: Cleanup tty buffer align mask
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 15730dc45dc7432713c7af9ee5abad76872f6405
Author: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Date:   Wed Oct 19 13:55:04 2022 +0300

    tty: Cleanup tty buffer align mask

    Don't use decimal for mask. Don't use literal for aligning.

    Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Link: https://lore.kernel.org/r/20221019105504.16800-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:17 -04:00
Andrew Halaney f1b05b534f tty: Fix lookahead_buf crash with serdev
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 56c14fb4086b2de6921dd70251b19b364b909ea1
Author: Vincent Whitchurch <vincent.whitchurch@axis.com>
Date:   Thu Aug 18 13:50:26 2022 +0200

    tty: Fix lookahead_buf crash with serdev

    Do not follow a NULL pointer if the tty_port_client_operations does not
    implement the ->lookahead_buf() callback, which is the case with
    serdev's ttyport.

    Reported-by: Hans de Goede <hdegoede@redhat.com>
    Fixes: 6bb6fa6908ebd3 ("tty: Implement lookahead to process XON/XOFF timely")
    Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
    Signed-off-by: Vincent Whitchurch <vincent.whitchurch@axis.com>
    Link: https://lore.kernel.org/r/20220818115026.2237893-1-vincent.whitchurch@axis.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:25:06 -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 197b4fe6dd tty: fix deadlock caused by calling printk() under tty_port->lock
JIRA: https://issues.redhat.com/browse/RHEL-24205

commit 6b9dbedbe3499fef862c4dff5217cf91f34e43b3
Author: Qi Zheng <zhengqi.arch@bytedance.com>
Date:   Thu May 12 20:38:37 2022 -0700

    tty: fix deadlock caused by calling printk() under tty_port->lock

    pty_write() invokes kmalloc() which may invoke a normal printk() to print
    failure message.  This can cause a deadlock in the scenario reported by
    syz-bot below:

           CPU0              CPU1                    CPU2
           ----              ----                    ----
                             lock(console_owner);
                                                     lock(&port_lock_key);
      lock(&port->lock);
                             lock(&port_lock_key);
                                                     lock(&port->lock);
      lock(console_owner);

    As commit dbdda842fe ("printk: Add console owner and waiter logic to
    load balance console writes") said, such deadlock can be prevented by
    using printk_deferred() in kmalloc() (which is invoked in the section
    guarded by the port->lock).  But there are too many printk() on the
    kmalloc() path, and kmalloc() can be called from anywhere, so changing
    printk() to printk_deferred() is too complicated and inelegant.

    Therefore, this patch chooses to specify __GFP_NOWARN to kmalloc(), so
    that printk() will not be called, and this deadlock problem can be
    avoided.

    Syzbot reported the following lockdep error:

    ======================================================
    WARNING: possible circular locking dependency detected
    5.4.143-00237-g08ccc19a-dirty #10 Not tainted
    ------------------------------------------------------
    syz-executor.4/29420 is trying to acquire lock:
    ffffffff8aedb2a0 (console_owner){....}-{0:0}, at: console_trylock_spinning kernel/printk/printk.c:1752 [inline]
    ffffffff8aedb2a0 (console_owner){....}-{0:0}, at: vprintk_emit+0x2ca/0x470 kernel/printk/printk.c:2023

    but task is already holding lock:
    ffff8880119c9158 (&port->lock){-.-.}-{2:2}, at: pty_write+0xf4/0x1f0 drivers/tty/pty.c:120

    which lock already depends on the new lock.

    the existing dependency chain (in reverse order) is:

    -> #2 (&port->lock){-.-.}-{2:2}:
           __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline]
           _raw_spin_lock_irqsave+0x35/0x50 kernel/locking/spinlock.c:159
           tty_port_tty_get drivers/tty/tty_port.c:288 [inline]                     <-- lock(&port->lock);
           tty_port_default_wakeup+0x1d/0xb0 drivers/tty/tty_port.c:47
           serial8250_tx_chars+0x530/0xa80 drivers/tty/serial/8250/8250_port.c:1767
           serial8250_handle_irq.part.0+0x31f/0x3d0 drivers/tty/serial/8250/8250_port.c:1854
           serial8250_handle_irq drivers/tty/serial/8250/8250_port.c:1827 [inline]  <-- lock(&port_lock_key);
           serial8250_default_handle_irq+0xb2/0x220 drivers/tty/serial/8250/8250_port.c:1870
           serial8250_interrupt+0xfd/0x200 drivers/tty/serial/8250/8250_core.c:126
           __handle_irq_event_percpu+0x109/0xa50 kernel/irq/handle.c:156
           [...]

    -> #1 (&port_lock_key){-.-.}-{2:2}:
           __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline]
           _raw_spin_lock_irqsave+0x35/0x50 kernel/locking/spinlock.c:159
           serial8250_console_write+0x184/0xa40 drivers/tty/serial/8250/8250_port.c:3198
                                                                                    <-- lock(&port_lock_key);
           call_console_drivers kernel/printk/printk.c:1819 [inline]
           console_unlock+0x8cb/0xd00 kernel/printk/printk.c:2504
           vprintk_emit+0x1b5/0x470 kernel/printk/printk.c:2024                     <-- lock(console_owner);
           vprintk_func+0x8d/0x250 kernel/printk/printk_safe.c:394
           printk+0xba/0xed kernel/printk/printk.c:2084
           register_console+0x8b3/0xc10 kernel/printk/printk.c:2829
           univ8250_console_init+0x3a/0x46 drivers/tty/serial/8250/8250_core.c:681
           console_init+0x49d/0x6d3 kernel/printk/printk.c:2915
           start_kernel+0x5e9/0x879 init/main.c:713
           secondary_startup_64+0xa4/0xb0 arch/x86/kernel/head_64.S:241

    -> #0 (console_owner){....}-{0:0}:
           [...]
           lock_acquire+0x127/0x340 kernel/locking/lockdep.c:4734
           console_trylock_spinning kernel/printk/printk.c:1773 [inline]            <-- lock(console_owner);
           vprintk_emit+0x307/0x470 kernel/printk/printk.c:2023
           vprintk_func+0x8d/0x250 kernel/printk/printk_safe.c:394
           printk+0xba/0xed kernel/printk/printk.c:2084
           fail_dump lib/fault-inject.c:45 [inline]
           should_fail+0x67b/0x7c0 lib/fault-inject.c:144
           __should_failslab+0x152/0x1c0 mm/failslab.c:33
           should_failslab+0x5/0x10 mm/slab_common.c:1224
           slab_pre_alloc_hook mm/slab.h:468 [inline]
           slab_alloc_node mm/slub.c:2723 [inline]
           slab_alloc mm/slub.c:2807 [inline]
           __kmalloc+0x72/0x300 mm/slub.c:3871
           kmalloc include/linux/slab.h:582 [inline]
           tty_buffer_alloc+0x23f/0x2a0 drivers/tty/tty_buffer.c:175
           __tty_buffer_request_room+0x156/0x2a0 drivers/tty/tty_buffer.c:273
           tty_insert_flip_string_fixed_flag+0x93/0x250 drivers/tty/tty_buffer.c:318
           tty_insert_flip_string include/linux/tty_flip.h:37 [inline]
           pty_write+0x126/0x1f0 drivers/tty/pty.c:122                              <-- lock(&port->lock);
           n_tty_write+0xa7a/0xfc0 drivers/tty/n_tty.c:2356
           do_tty_write drivers/tty/tty_io.c:961 [inline]
           tty_write+0x512/0x930 drivers/tty/tty_io.c:1045
           __vfs_write+0x76/0x100 fs/read_write.c:494
           [...]

    other info that might help us debug this:

    Chain exists of:
      console_owner --> &port_lock_key --> &port->lock

    Link: https://lkml.kernel.org/r/20220511061951.1114-2-zhengqi.arch@bytedance.com
    Link: https://lkml.kernel.org/r/20220510113809.80626-2-zhengqi.arch@bytedance.com
    Fixes: b6da31b2c0 ("tty: Fix data race in tty_insert_flip_string_fixed_flag")
    Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com>
    Acked-by: Jiri Slaby <jirislaby@kernel.org>
    Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
    Cc: Akinobu Mita <akinobu.mita@gmail.com>
    Cc: Vlastimil Babka <vbabka@suse.cz>
    Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
    Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Signed-off-by: Andrew Halaney <ahalaney@redhat.com>
2024-05-09 11:24:46 -04:00
Andrew Halaney 33df46efcd tty: reformat kernel-doc in tty_buffer.c
JIRA: https://issues.redhat.com/browse/RHEL-24205
Conflicts: Context diff due to
           6eb2a5642d ("tty: use new tty_insert_flip_string_and_push_buffer() in pty_write()")

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

    tty: reformat kernel-doc in tty_buffer.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-15-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
Steve Best 9c76cbe7cd tty: tty_buffer: Fix the softlockup issue in flush_to_ldisc
JIRA: https://issues.redhat.com/browse/RHEL-32592
CVE: CVE-2021-47185

commit 3968ddcf05fb4b9409cd1859feb06a5b0550a1c1
Author: Guanghui Feng <guanghuifeng@linux.alibaba.com>
Date:   Mon Oct 11 22:08:24 2021 +0800

    tty: tty_buffer: Fix the softlockup issue in flush_to_ldisc

    When running ltp testcase(ltp/testcases/kernel/pty/pty04.c) with arm64, there is a soft lockup,
    which look like this one:

      Workqueue: events_unbound flush_to_ldisc
      Call trace:
       dump_backtrace+0x0/0x1ec
       show_stack+0x24/0x30
       dump_stack+0xd0/0x128
       panic+0x15c/0x374
       watchdog_timer_fn+0x2b8/0x304
       __run_hrtimer+0x88/0x2c0
       __hrtimer_run_queues+0xa4/0x120
       hrtimer_interrupt+0xfc/0x270
       arch_timer_handler_phys+0x40/0x50
       handle_percpu_devid_irq+0x94/0x220
       __handle_domain_irq+0x88/0xf0
       gic_handle_irq+0x84/0xfc
       el1_irq+0xc8/0x180
       slip_unesc+0x80/0x214 [slip]
       tty_ldisc_receive_buf+0x64/0x80
       tty_port_default_receive_buf+0x50/0x90
       flush_to_ldisc+0xbc/0x110
       process_one_work+0x1d4/0x4b0
       worker_thread+0x180/0x430
       kthread+0x11c/0x120

    In the testcase pty04, The first process call the write syscall to send
    data to the pty master. At the same time, the workqueue will do the
    flush_to_ldisc to pop data in a loop until there is no more data left.
    When the sender and workqueue running in different core, the sender sends
    data fastly in full time which will result in workqueue doing work in loop
    for a long time and occuring softlockup in flush_to_ldisc with kernel
    configured without preempt. So I add need_resched check and cond_resched
    in the flush_to_ldisc loop to avoid it.

    Signed-off-by: Guanghui Feng <guanghuifeng@linux.alibaba.com>
    Link: https://lore.kernel.org/r/1633961304-24759-1-git-send-email-guanghuifeng@linux.alibaba.com
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Signed-off-by: Steve Best <sbest@redhat.com>
2024-04-17 03:14:59 -04:00
Rafael Aquini 6eb2a5642d tty: use new tty_insert_flip_string_and_push_buffer() in pty_write()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2078867
CVE: CVE-2022-1462
Conflicts: as documented on the backport notes section.

This patch is a backport of the following upstream commit:
commit a501ab75e7624d133a5a3c7ec010687c8b961d23
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Thu Jul 7 10:25:58 2022 +0200

    tty: use new tty_insert_flip_string_and_push_buffer() in pty_write()

    There is a race in pty_write(). pty_write() can be called in parallel
    with e.g. ioctl(TIOCSTI) or ioctl(TCXONC) which also inserts chars to
    the buffer. Provided, tty_flip_buffer_push() in pty_write() is called
    outside the lock, it can commit inconsistent tail. This can lead to out
    of bounds writes and other issues. See the Link below.

    To fix this, we have to introduce a new helper called
    tty_insert_flip_string_and_push_buffer(). It does both
    tty_insert_flip_string() and tty_flip_buffer_commit() under the port
    lock. It also calls queue_work(), but outside the lock. See
    71a174b39f (pty: do tty_flip_buffer_push without port->lock in
    pty_write) for the reasons.

    Keep the helper internal-only (in drivers' tty.h). It is not intended to
    be used widely.

    Link: https://seclists.org/oss-sec/2022/q2/155
    Fixes: 71a174b39f (pty: do tty_flip_buffer_push without port->lock in pty_write)
    Cc: 一只狗 <chennbnbnb@gmail.com>
    Cc: Dan Carpenter <dan.carpenter@oracle.com>
    Suggested-by: Hillf Danton <hdanton@sina.com>
    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20220707082558.9250-2-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

RHEL BACKPORT NOTES:
* drivers/tty/tty_buffer.c: minor context diff due to RHEL9 missing upstream
  commit bc17b7236b47 ("tty: reformat kernel-doc in tty_buffer.c"), which
  is not a hard dependency for this port

Signed-off-by: Rafael Aquini <aquini@redhat.com>
2022-09-07 19:15:14 -04:00
Rafael Aquini 8c7e7669f0 tty: extract tty_flip_buffer_commit() from tty_flip_buffer_push()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2078867
CVE: CVE-2022-1462
Conflicts: as documented on the backport notes section.

This patch is a backport of the following upstream commit:
commit 716b10580283fda66f2b88140e3964f8a7f9da89
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Thu Jul 7 10:25:57 2022 +0200

    tty: extract tty_flip_buffer_commit() from tty_flip_buffer_push()

    We will need this new helper in the next patch.

    Cc: Hillf Danton <hdanton@sina.com>
    Cc: 一只狗 <chennbnbnb@gmail.com>
    Cc: Dan Carpenter <dan.carpenter@oracle.com>
    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20220707082558.9250-1-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

RHEL BACKPORT NOTES:
* minor context diff due to RHEL9 missing upstream commit bc17b7236b47
  ("tty: reformat kernel-doc in tty_buffer.c"), which is not a hard
  dependency for this port

Signed-off-by: Rafael Aquini <aquini@redhat.com>
2022-09-07 19:15:13 -04:00
Rafael Aquini c7aca03728 tty: drop tty_schedule_flip()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2078867
CVE: CVE-2022-1462
Conflicts: as documented on the backport notes section.

This patch is a backport of the following upstream commit:
commit 5db96ef23bda6c2a61a51693c85b78b52d03f654
Author: Jiri Slaby <jirislaby@kernel.org>
Date:   Mon Nov 22 12:16:48 2021 +0100

    tty: drop tty_schedule_flip()

    Since commit a9c3f68f3c (tty: Fix low_latency BUG) in 2014,
    tty_flip_buffer_push() is only a wrapper to tty_schedule_flip(). All
    users were converted in the previous patches, so remove
    tty_schedule_flip() completely while inlining its body into
    tty_flip_buffer_push().

    One less exported function.

    Reviewed-by: Johan Hovold <johan@kernel.org>
    Signed-off-by: Jiri Slaby <jslaby@suse.cz>
    Link: https://lore.kernel.org/r/20211122111648.30379-4-jslaby@suse.cz
    Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

RHEL BACKPORT NOTES:
* include/linux/tty_flip.h: minor fuzz due to RHEL9 missing upstream commit
  7894193436b6 ("tty: remove extern from functions in tty headers") and friends,
  which are not required dependencies for this port

Signed-off-by: Rafael Aquini <aquini@redhat.com>
2022-09-07 19:15:13 -04:00
Lee Jones a1c0da88f1 tty: tty_buffer: Fix incorrectly documented function __tty_buffer_request_room()
Fixes the following W=1 kernel build warning(s):

 drivers/tty/tty_buffer.c:259: warning: expecting prototype for tty_buffer_request_room(). Prototype was for __tty_buffer_request_room() instead

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jiri Slaby <jirislaby@kernel.org>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
Link: https://lore.kernel.org/r/20210520121906.3468725-6-lee.jones@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-05-20 17:06:15 +02:00
Xiaofei Tan 80e3fce103 tty: tty_buffer: Fix coding style issues of block comments
Fix coding style issues of block comments, reported by checkpatch.pl.
Besides, add a period at the end of the sentenses.

Signed-off-by: Xiaofei Tan <tanxiaofei@huawei.com>
Link: https://lore.kernel.org/r/1620811585-18582-6-git-send-email-tanxiaofei@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-05-13 18:29:12 +02:00
Xiaofei Tan dadc104948 tty: tty_buffer: Remove the repeated word 'the'
Remove the repeated word 'the' following advice of checkpatch.pl
Besides, add a period at the end of comment sentence.

Signed-off-by: Xiaofei Tan <tanxiaofei@huawei.com>
Link: https://lore.kernel.org/r/1620811585-18582-5-git-send-email-tanxiaofei@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-05-13 18:29:12 +02:00
Xiaofei Tan 993c67b1fe tty: tty_buffer: Add a blank line after declarations
Add a blank line after declarations, reported by checkpatch.pl.

Signed-off-by: Xiaofei Tan <tanxiaofei@huawei.com>
Link: https://lore.kernel.org/r/1620811585-18582-4-git-send-email-tanxiaofei@huawei.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-05-13 18:29:12 +02:00
Jiri Slaby 9a33fbf9d2 tty: make tty_buffer_space_avail return uint
tty_buffer_space_avail returns values >= 0, so make it clear by the
return type.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Cc: Joel Stanley <joel@jms.id.au>
Link: https://lore.kernel.org/r/20210505091928.22010-25-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-05-13 18:29:11 +02:00
Jiri Slaby 0f3dcf3b5d tty: make fp of tty_ldisc_ops::receive_buf{,2} const
Char pointer (cp) passed to tty_ldisc_ops::receive_buf{,2} is const.
There is no reason for flag pointer (fp) not to be too. So switch it in
the definition and all uses.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
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: Jakub Kicinski <kuba@kernel.org>
Cc: Wolfgang Grandegger <wg@grandegger.com>
Cc: Marc Kleine-Budde <mkl@pengutronix.de>
Cc: Andreas Koensgen <ajk@comnets.uni-bremen.de>
Cc: Paul Mackerras <paulus@samba.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>
Cc: Peter Ujfalusi <peter.ujfalusi@gmail.com>
Link: https://lore.kernel.org/r/20210505091928.22010-12-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-05-13 16:57:16 +02:00
Greg Kroah-Hartman 6c80c0b94b tty: move some internal tty lock enums and functions out of tty.h
Move the TTY_LOCK_* enums and tty_ldisc lock functions out of the global
tty.h into the local header file to clean things up.

Cc: Jiri Slaby <jirislaby@kernel.org>
Link: https://lore.kernel.org/r/20210408125134.3016837-10-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2021-04-15 10:26:58 +02:00
Lee Jones a776f10d12 tty: tty_buffer: Add missing description for 'limit'
Fixes the following W=1 kernel build warning(s):

 drivers/tty/tty_buffer.c:592: warning: Function parameter or member 'limit' not described in 'tty_buffer_set_limit'

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Jiri Slaby <jirislaby@kernel.org>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
Link: https://lore.kernel.org/r/20201104193549.4026187-6-lee.jones@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-11-06 10:49:27 +01:00
Jiri Slaby fa44195455 tty: fix kernel-doc
With W=1, the kernel-doc checker complains quite a lot in the tty layer.
Over the time, many documented parameters were renamed, removed or
switched from tty to tty_port and similar. Some were mistyped in the doc
too.

So fix all these in the tty core. (But do not add the missing ones which
the checker complains about too. Not now.) The rest in the tty layer
will follow in the next patches.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Link: https://lore.kernel.org/r/20200818085655.12071-4-jslaby@suse.cz
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2020-08-18 13:51:18 +02:00
Manfred Schlaegl 7ab57b76eb tty: increase the default flip buffer limit to 2*640K
We increase the default limit for buffer memory allocation by a factor of
10 to 640K to prevent data loss when using fast serial interfaces.

For example when using RS485 without flow-control at speeds of 1Mbit/s
an upwards we've run into problems such as applications being too slow
to read out this buffer (on embedded devices based on imx53 or imx6).

If you want to write transmitted data to a slow SD card and thus have
realtime requirements, this limit can become a problem.

That shouldn't be the case and 640K buffers fix such problems for us.

This value is a maximum limit for allocation only. It has no effect
on systems that currently run fine. When transmission is slow enough
applications and hardware can keep up and increasing this limit
doesn't change anything.

It only _allows_ to allocate more than 2*64K in cases we currently fail to
allocate memory despite having some.

Signed-off-by: Manfred Schlaegl <manfred.schlaegl@ginzinger.com>
Signed-off-by: Martin Kepplinger <martin.kepplinger@ginzinger.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-01-30 12:03:58 +01:00
Linus Torvalds c9a8e5fce0 tty: wipe buffer.
After we are done with the tty buffer, zero it out.

Reported-by: aszlig <aszlig@nix.build>
Tested-by: Milan Broz <gmazyland@gmail.com>
Tested-by: Daniel Zatovic <daniel.zatovic@gmail.com>
Tested-by: aszlig <aszlig@nix.build>
Cc: Willy Tarreau <w@1wt.eu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-10-11 19:50:00 +02:00
Jiri Slaby feacbecb39 TTY: tty_buffer, warn on leaks
When we leak some tty buffer, warn about that. For that we need to
account the memory used also in the tty_buffer_free_all function. On
other locations, the accounting is handled correctly.

Note that we do not account the free list, as that was accounted in
tty_buffer_free before put on the free list.

I have been using this patch for ages, so let's see if anybody else
encounters any issues.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-09-18 16:07:25 +02:00
Greg Kroah-Hartman e3b3d0f549 tty: add SPDX identifiers to all remaining files in drivers/tty/
It's good to have SPDX identifiers in all files to make it easier to
audit the kernel tree for correct licenses.

Update the drivers/tty files files with the correct SPDX license
identifier based on the license text in the file itself.  The SPDX
identifier is a legally binding shorthand, which can be used instead of
the full boiler plate text.

This work is based on a script and data from Thomas Gleixner, Philippe
Ombredanne, and Kate Stewart.

Cc: Jiri Slaby <jslaby@suse.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Chris Metcalf <cmetcalf@mellanox.com>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: David Sterba <dsterba@suse.com>
Cc: James Hogan <jhogan@kernel.org>
Cc: Rob Herring <robh@kernel.org>
Cc: Eric Anholt <eric@anholt.net>
Cc: Stefan Wahren <stefan.wahren@i2se.com>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: Ray Jui <rjui@broadcom.com>
Cc: Scott Branden <sbranden@broadcom.com>
Cc: bcm-kernel-feedback-list@broadcom.com
Cc: "James E.J. Bottomley" <jejb@parisc-linux.org>
Cc: Helge Deller <deller@gmx.de>
Cc: Joachim Eastwood <manabian@gmail.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: Tobias Klauser <tklauser@distanz.ch>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Vineet Gupta <vgupta@synopsys.com>
Cc: Richard Genoud <richard.genoud@gmail.com>
Cc: Alexander Shiyan <shc_work@mail.ru>
Cc: Baruch Siach <baruch@tkos.co.il>
Cc: "Maciej W. Rozycki" <macro@linux-mips.org>
Cc: "Uwe Kleine-König" <kernel@pengutronix.de>
Cc: Pat Gefre <pfg@sgi.com>
Cc: "Guilherme G. Piccoli" <gpiccoli@linux.vnet.ibm.com>
Cc: Jason Wessel <jason.wessel@windriver.com>
Cc: Vladimir Zapolskiy <vz@mleia.com>
Cc: Sylvain Lemieux <slemieux.tyco@gmail.com>
Cc: Carlo Caione <carlo@caione.org>
Cc: Kevin Hilman <khilman@baylibre.com>
Cc: Liviu Dudau <liviu.dudau@arm.com>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Andy Gross <andy.gross@linaro.org>
Cc: David Brown <david.brown@linaro.org>
Cc: "Andreas Färber" <afaerber@suse.de>
Cc: Kevin Cernekee <cernekee@gmail.com>
Cc: Laxman Dewangan <ldewangan@nvidia.com>
Cc: Thierry Reding <thierry.reding@gmail.com>
Cc: Jonathan Hunter <jonathanh@nvidia.com>
Cc: Barry Song <baohua@kernel.org>
Cc: Patrice Chotard <patrice.chotard@st.com>
Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com>
Cc: Alexandre Torgue <alexandre.torgue@st.com>
Cc: "David S. Miller" <davem@davemloft.net>
Cc: Peter Korsgaard <jacmet@sunsite.dk>
Cc: Timur Tabi <timur@tabi.org>
Cc: Tony Prisk <linux@prisktech.co.nz>
Cc: Michal Simek <michal.simek@xilinx.com>
Cc: "Sören Brinkmann" <soren.brinkmann@xilinx.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Kate Stewart <kstewart@linuxfoundation.org>
Cc: Philippe Ombredanne <pombredanne@nexb.com>
Cc: Jiri Slaby <jslaby@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-08 13:08:12 +01:00
Johan Hovold e7e51dcf3b tty: fix tty_ldisc_receive_buf() documentation
The tty_ldisc_receive_buf() helper returns the number of bytes
processed so drop the bogus "not" from the kernel doc comment.

Fixes: 8d082cd300 ("tty: Unify receive_buf() code paths")
Signed-off-by: Johan Hovold <johan@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-04 12:08:53 +01:00
Arnd Bergmann 8a5a90a2a4 tty: fix __tty_insert_flip_char regression
Sergey noticed a small but fatal mistake in __tty_insert_flip_char,
leading to an oops in an interrupt handler when using any serial
port.

The problem is that I accidentally took the tty_buffer pointer
before calling __tty_buffer_request_room(), which replaces the
buffer. This moves the pointer lookup to the right place after
allocating the new buffer space.

Fixes: 979990c628 ("tty: improve tty_insert_flip_char() fast path")
Reported-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Tested-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-08-02 06:58:17 -07:00
Arnd Bergmann 065ea0a7af tty: improve tty_insert_flip_char() slow path
While working on improving the fast path of tty_insert_flip_char(),
I noticed that by calling tty_buffer_request_room(), we needlessly
move to the separate flag buffer mode for the tty, even when all
characters use TTY_NORMAL as the flag.

This changes the code to call __tty_buffer_request_room() with the
correct flag, which will then allocate a regular buffer when it rounds
out of space but no special flags have been used. I'm guessing that
this is the behavior that Peter Hurley intended when he introduced
the compacted flip buffers.

Fixes: acc0f67f30 ("tty: Halve flip buffer GFP_ATOMIC memory consumption")
Cc: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-07-30 07:52:19 -07:00
Arnd Bergmann 979990c628 tty: improve tty_insert_flip_char() fast path
kernelci.org reports a crazy stack usage for the VT code when CONFIG_KASAN
is enabled:

drivers/tty/vt/keyboard.c: In function 'kbd_keycode':
drivers/tty/vt/keyboard.c:1452:1: error: the frame size of 2240 bytes is larger than 2048 bytes [-Werror=frame-larger-than=]

The problem is that tty_insert_flip_char() gets inlined many times into
kbd_keycode(), and also into other functions, and each copy requires 128
bytes for stack redzone to check for a possible out-of-bounds access on
the 'ch' and 'flags' arguments that are passed into
tty_insert_flip_string_flags as a variable-length string.

This introduces a new __tty_insert_flip_char() function for the slow
path, which receives the two arguments by value. This completely avoids
the problem and the stack usage goes back down to around 100 bytes.

Without KASAN, this is also slightly better, as we don't have to
spill the arguments to the stack but can simply pass 'ch' and 'flag'
in registers, saving a few bytes in .text for each call site.

This should be backported to linux-4.0 or later, which first introduced
the stack sanitizer in the kernel.

Cc: stable@vger.kernel.org
Fixes: c420f167db ("kasan: enable stack instrumentation")
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-07-30 07:52:19 -07:00
Rob Herring c3485ee0d5 tty_port: Add port client functions
Introduce a client (upward direction) operations struct for tty_port
clients. Initially supported operations are for receiving data and write
wake-up. This will allow for having clients other than an ldisc.

Convert the calls to the ldisc to use the client ops as the default
operations.

Signed-off-by: Rob Herring <robh@kernel.org>
Reviewed-By: Sebastian Reichel <sre@kernel.org>
Tested-By: Sebastian Reichel <sre@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-02-03 10:17:02 +01:00
Rob Herring c92d781f1a tty: constify tty_ldisc_receive_buf buffer pointer
This is needed to work with the client operations which uses const ptrs.

Really, the flags pointer could be const, too, but this would be a tree
wide fix.

Signed-off-by: Rob Herring <robh@kernel.org>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-By: Sebastian Reichel <sre@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-01-19 17:22:34 +01:00
Brian Bloniarz 0f40fbbcc3 Fix OpenSSH pty regression on close
OpenSSH expects the (non-blocking) read() of pty master to return
EAGAIN only if it has received all of the slave-side output after
it has received SIGCHLD. This used to work on pre-3.12 kernels.

This fix effectively forces non-blocking read() and poll() to
block for parallel i/o to complete for all ttys. It also unwinds
these changes:

1) f8747d4a46
   tty: Fix pty master read() after slave closes

2) 52bce7f8d4
   pty, n_tty: Simplify input processing on final close

3) 1a48632ffe
   pty: Fix input race when closing

Inspired by analysis and patch from Marc Aurele La France <tsi@tuyoix.net>

Reported-by: Volth <openssh@volth.com>
Reported-by: Marc Aurele La France <tsi@tuyoix.net>
BugLink: https://bugzilla.mindrot.org/show_bug.cgi?id=52
BugLink: https://bugzilla.mindrot.org/show_bug.cgi?id=2492
Signed-off-by: Brian Bloniarz <brian.bloniarz@gmail.com>
Reviewed-by: Peter Hurley <peter@hurleysoftware.com>
Cc: stable <stable@vger.kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-05-01 13:22:54 -07:00
Peter Hurley 8d082cd300 tty: Unify receive_buf() code paths
Instead of two distinct code branches for receive_buf() handling,
use tty_ldisc_receive_buf() as the single code path.

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2016-01-28 14:13:44 -08:00
Peter Hurley 9ce119f318 tty: Fix GPF in flush_to_ldisc()
A line discipline which does not define a receive_buf() method can
can cause a GPF if data is ever received [1]. Oddly, this was known
to the author of n_tracesink in 2011, but never fixed.

[1] GPF report
    BUG: unable to handle kernel NULL pointer dereference at           (null)
    IP: [<          (null)>]           (null)
    PGD 3752d067 PUD 37a7b067 PMD 0
    Oops: 0010 [#1] SMP KASAN
    Modules linked in:
    CPU: 2 PID: 148 Comm: kworker/u10:2 Not tainted 4.4.0-rc2+ #51
    Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
    Workqueue: events_unbound flush_to_ldisc
    task: ffff88006da94440 ti: ffff88006db60000 task.ti: ffff88006db60000
    RIP: 0010:[<0000000000000000>]  [<          (null)>]           (null)
    RSP: 0018:ffff88006db67b50  EFLAGS: 00010246
    RAX: 0000000000000102 RBX: ffff88003ab32f88 RCX: 0000000000000102
    RDX: 0000000000000000 RSI: ffff88003ab330a6 RDI: ffff88003aabd388
    RBP: ffff88006db67c48 R08: ffff88003ab32f9c R09: ffff88003ab31fb0
    R10: ffff88003ab32fa8 R11: 0000000000000000 R12: dffffc0000000000
    R13: ffff88006db67c20 R14: ffffffff863df820 R15: ffff88003ab31fb8
    FS:  0000000000000000(0000) GS:ffff88006dc00000(0000) knlGS:0000000000000000
    CS:  0010 DS: 0000 ES: 0000 CR0: 000000008005003b
    CR2: 0000000000000000 CR3: 0000000037938000 CR4: 00000000000006e0
    Stack:
     ffffffff829f46f1 ffff88006da94bf8 ffff88006da94bf8 0000000000000000
     ffff88003ab31fb0 ffff88003aabd438 ffff88003ab31ff8 ffff88006430fd90
     ffff88003ab32f9c ffffed0007557a87 1ffff1000db6cf78 ffff88003ab32078
    Call Trace:
     [<ffffffff8127cf91>] process_one_work+0x8f1/0x17a0 kernel/workqueue.c:2030
     [<ffffffff8127df14>] worker_thread+0xd4/0x1180 kernel/workqueue.c:2162
     [<ffffffff8128faaf>] kthread+0x1cf/0x270 drivers/block/aoe/aoecmd.c:1302
     [<ffffffff852a7c2f>] ret_from_fork+0x3f/0x70 arch/x86/entry/entry_64.S:468
    Code:  Bad RIP value.
    RIP  [<          (null)>]           (null)
     RSP <ffff88006db67b50>
    CR2: 0000000000000000
    ---[ end trace a587f8947e54d6ea ]---

Reported-by: Dmitry Vyukov <dvyukov@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-12-12 23:05:28 -08:00
Peter Hurley e052c6d15c tty: Use unbound workqueue for all input workers
The commonly accepted wisdom that scheduling work on the same cpu
that handled interrupt i/o benefits from cache-locality is only
true if the cpu is idle (since bound kworkers are often the highest
vruntime and thus the lowest priority).

Measurements of scheduling via the unbound queue show lowered
worst-case latency responses of up to 5x over bound workqueue, without
increase in average latency or throughput.

pty i/o test measurements show >3x (!) reduced total running time; tests
previously taking ~8s now complete in <2.5s.

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-10-17 21:32:21 -07:00
Peter Hurley e176058f0d tty: Abstract tty buffer work
Introduce API functions to restart and cancel tty buffer work, rather
than manipulate buffer work directly.

Signed-off-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-10-17 21:32:21 -07:00
Dmitry Vyukov facd885c75 tty: fix data race on tty_buffer.commit
Race on buffer data happens when newly committed data is
picked up by an old flush work in the following scenario:
__tty_buffer_request_room does a plain write of tail->commit,
no barriers were executed before that.
At this point flush_to_ldisc reads this new value of commit,
and reads buffer data, no barriers in between.
The committed buffer data is not necessary visible to flush_to_ldisc.

Similar bug happens when tty_schedule_flip commits data.

Update commit with smp_store_release and read commit with
smp_load_acquire, as it is commit that signals data readiness.
This is orthogonal to the existing synchronization on tty_buffer.next,
which is required to not dismiss a buffer with unconsumed data.

The data race was found with KernelThreadSanitizer (KTSAN).

Signed-off-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: Peter Hurley <peter@hurleysoftware.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2015-10-04 19:03:40 +01:00