mbox series

[v3,0/4] tty: TX helpers

Message ID 20220906104805.23211-1-jslaby@suse.cz (mailing list archive)
Headers show
Series tty: TX helpers | expand

Message

Jiri Slaby Sept. 6, 2022, 10:48 a.m. UTC
This series introduces DEFINE_UART_PORT_TX_HELPER +
DEFINE_UART_PORT_TX_HELPER_LIMITED TX helpers. See PATCH 2/4 for the
details. Comments welcome.

Then it switches drivers to use them. First, to
DEFINE_UART_PORT_TX_HELPER() in 3/4 and then
DEFINE_UART_PORT_TX_HELPER_LIMITED() in 4/4.

The diffstat of patches 3+4 is as follows:
 26 files changed, 191 insertions(+), 823 deletions(-)
which appears to be nice.

Cc: Tobias Klauser <tklauser@distanz.ch>
Cc: Richard Genoud <richard.genoud@gmail.com>
Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: Claudiu Beznea <claudiu.beznea@microchip.com>
Cc: Vladimir Zapolskiy <vz@mleia.com>
Cc: Liviu Dudau <liviu.dudau@arm.com>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: NXP Linux Team <linux-imx@nxp.com>
Cc: "Andreas Färber" <afaerber@suse.de>
Cc: Manivannan Sadhasivam <mani@kernel.org>
Cc: Russell King <linux@armlinux.org.uk>
Cc: Florian Fainelli <f.fainelli@gmail.com>
Cc: bcm-kernel-feedback-list@broadcom.com
Cc: "Pali Rohár" <pali@kernel.org>
Cc: Kevin Cernekee <cernekee@gmail.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Orson Zhai <orsonzhai@gmail.com>
Cc: Baolin Wang <baolin.wang7@gmail.com>
Cc: Chunyan Zhang <zhang.lyra@gmail.com>
Cc: Patrice Chotard <patrice.chotard@foss.st.com>
Cc: linux-riscv@lists.infradead.org

Jiri Slaby (4):
  tty: serial: move and cleanup vt8500_tx_empty()
  tty: serial: introduce transmit helper generators
  tty: serial: use DEFINE_UART_PORT_TX_HELPER()
  tty: serial: use DEFINE_UART_PORT_TX_HELPER_LIMITED()

 Documentation/driver-api/serial/driver.rst |  3 +
 drivers/tty/serial/21285.c                 | 33 ++-------
 drivers/tty/serial/altera_jtaguart.c       | 42 +++--------
 drivers/tty/serial/altera_uart.c           | 37 ++--------
 drivers/tty/serial/amba-pl010.c            | 37 ++--------
 drivers/tty/serial/apbuart.c               | 36 ++--------
 drivers/tty/serial/atmel_serial.c          | 29 ++------
 drivers/tty/serial/bcm63xx_uart.c          | 47 +++----------
 drivers/tty/serial/fsl_lpuart.c            | 38 +++-------
 drivers/tty/serial/lantiq.c                | 44 ++++--------
 drivers/tty/serial/lpc32xx_hs.c            | 38 ++--------
 drivers/tty/serial/mcf.c                   | 27 ++------
 drivers/tty/serial/mpc52xx_uart.c          | 43 +-----------
 drivers/tty/serial/mps2-uart.c             | 29 +-------
 drivers/tty/serial/mux.c                   | 46 ++++--------
 drivers/tty/serial/mvebu-uart.c            | 40 ++---------
 drivers/tty/serial/mxs-auart.c             | 31 ++-------
 drivers/tty/serial/omap-serial.c           | 47 ++++---------
 drivers/tty/serial/owl-uart.c              | 35 +---------
 drivers/tty/serial/pxa.c                   | 39 +++--------
 drivers/tty/serial/rp2.c                   | 36 ++--------
 drivers/tty/serial/sa1100.c                | 49 +++++--------
 drivers/tty/serial/serial_txx9.c           | 37 ++--------
 drivers/tty/serial/sifive.c                | 45 ++----------
 drivers/tty/serial/sprd_serial.c           | 38 ++--------
 drivers/tty/serial/st-asc.c                | 50 ++-----------
 drivers/tty/serial/vt8500_serial.c         | 40 ++---------
 include/linux/serial_core.h                | 81 ++++++++++++++++++++++
 28 files changed, 275 insertions(+), 822 deletions(-)

Comments

Johan Hovold Sept. 6, 2022, 11:30 a.m. UTC | #1
On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> This series introduces DEFINE_UART_PORT_TX_HELPER +
> DEFINE_UART_PORT_TX_HELPER_LIMITED TX helpers. See PATCH 2/4 for the
> details. Comments welcome.
> 
> Then it switches drivers to use them. First, to
> DEFINE_UART_PORT_TX_HELPER() in 3/4 and then
> DEFINE_UART_PORT_TX_HELPER_LIMITED() in 4/4.
> 
> The diffstat of patches 3+4 is as follows:
>  26 files changed, 191 insertions(+), 823 deletions(-)
> which appears to be nice.

Not really. This is horrid. Quality can't be measured in LoC (only).

The resulting code is unreadable. And for no good reason.

[ And note that you're "saving" something like 20 lines per driver:

	 12 files changed, 84 insertions(+), 349 deletions(-)
]

NAK

Johan
Jiri Slaby Sept. 7, 2022, 7:19 a.m. UTC | #2
On 06. 09. 22, 13:30, Johan Hovold wrote:
> On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
>> This series introduces DEFINE_UART_PORT_TX_HELPER +
>> DEFINE_UART_PORT_TX_HELPER_LIMITED TX helpers. See PATCH 2/4 for the
>> details. Comments welcome.
>>
>> Then it switches drivers to use them. First, to
>> DEFINE_UART_PORT_TX_HELPER() in 3/4 and then
>> DEFINE_UART_PORT_TX_HELPER_LIMITED() in 4/4.
>>
>> The diffstat of patches 3+4 is as follows:
>>   26 files changed, 191 insertions(+), 823 deletions(-)
>> which appears to be nice.
> 
> Not really. This is horrid. Quality can't be measured in LoC (only).
> 
> The resulting code is unreadable. And for no good reason.

IMO, it's much more readable than the original ~ 30 various (and buggy 
-- see Ilpo's fixes) copies of this code. Apart from that, it makes 
further rework much easier (I have switch to kfifo in my mind for example).

> [ And note that you're "saving" something like 20 lines per driver:

It's not about saving, it's about deduplicating and unifying.

> 	 12 files changed, 84 insertions(+), 349 deletions(-)
> ]
> 
> NAK

I'd love to come up with something nicer. That would be a function in 
serial-core calling hooks like I had [1] for example. But provided all 
those CPU workarounds/thunks, it'd be quite expensive to call two 
functions per character.

Or creating a static inline (having ± the macro content) and the hooks 
as parameters and hope for optimizations to eliminate thunks (also 
suggested in the past [1]).

[1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/

thanks,
Ilpo Järvinen Sept. 7, 2022, 9:54 a.m. UTC | #3
On Tue, 6 Sep 2022, Jiri Slaby wrote:

> This series introduces DEFINE_UART_PORT_TX_HELPER +
> DEFINE_UART_PORT_TX_HELPER_LIMITED TX helpers. See PATCH 2/4 for the
> details. Comments welcome.
> 
> Then it switches drivers to use them. First, to
> DEFINE_UART_PORT_TX_HELPER() in 3/4 and then
> DEFINE_UART_PORT_TX_HELPER_LIMITED() in 4/4.
> 
> The diffstat of patches 3+4 is as follows:
>  26 files changed, 191 insertions(+), 823 deletions(-)
> which appears to be nice.
> 
> Cc: Tobias Klauser <tklauser@distanz.ch>
> Cc: Richard Genoud <richard.genoud@gmail.com>
> Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
> Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Cc: Claudiu Beznea <claudiu.beznea@microchip.com>
> Cc: Vladimir Zapolskiy <vz@mleia.com>
> Cc: Liviu Dudau <liviu.dudau@arm.com>
> Cc: Sudeep Holla <sudeep.holla@arm.com>
> Cc: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
> Cc: Shawn Guo <shawnguo@kernel.org>
> Cc: Sascha Hauer <s.hauer@pengutronix.de>
> Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
> Cc: Fabio Estevam <festevam@gmail.com>
> Cc: NXP Linux Team <linux-imx@nxp.com>
> Cc: "Andreas Färber" <afaerber@suse.de>
> Cc: Manivannan Sadhasivam <mani@kernel.org>
> Cc: Russell King <linux@armlinux.org.uk>
> Cc: Florian Fainelli <f.fainelli@gmail.com>
> Cc: bcm-kernel-feedback-list@broadcom.com
> Cc: "Pali Rohár" <pali@kernel.org>
> Cc: Kevin Cernekee <cernekee@gmail.com>
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Cc: Paul Walmsley <paul.walmsley@sifive.com>
> Cc: Orson Zhai <orsonzhai@gmail.com>
> Cc: Baolin Wang <baolin.wang7@gmail.com>
> Cc: Chunyan Zhang <zhang.lyra@gmail.com>
> Cc: Patrice Chotard <patrice.chotard@foss.st.com>
> Cc: linux-riscv@lists.infradead.org
> 
> Jiri Slaby (4):
>   tty: serial: move and cleanup vt8500_tx_empty()
>   tty: serial: introduce transmit helper generators
>   tty: serial: use DEFINE_UART_PORT_TX_HELPER()
>   tty: serial: use DEFINE_UART_PORT_TX_HELPER_LIMITED()
> 
>  Documentation/driver-api/serial/driver.rst |  3 +
>  drivers/tty/serial/21285.c                 | 33 ++-------
>  drivers/tty/serial/altera_jtaguart.c       | 42 +++--------
>  drivers/tty/serial/altera_uart.c           | 37 ++--------
>  drivers/tty/serial/amba-pl010.c            | 37 ++--------
>  drivers/tty/serial/apbuart.c               | 36 ++--------
>  drivers/tty/serial/atmel_serial.c          | 29 ++------
>  drivers/tty/serial/bcm63xx_uart.c          | 47 +++----------
>  drivers/tty/serial/fsl_lpuart.c            | 38 +++-------
>  drivers/tty/serial/lantiq.c                | 44 ++++--------
>  drivers/tty/serial/lpc32xx_hs.c            | 38 ++--------
>  drivers/tty/serial/mcf.c                   | 27 ++------
>  drivers/tty/serial/mpc52xx_uart.c          | 43 +-----------
>  drivers/tty/serial/mps2-uart.c             | 29 +-------
>  drivers/tty/serial/mux.c                   | 46 ++++--------
>  drivers/tty/serial/mvebu-uart.c            | 40 ++---------
>  drivers/tty/serial/mxs-auart.c             | 31 ++-------
>  drivers/tty/serial/omap-serial.c           | 47 ++++---------
>  drivers/tty/serial/owl-uart.c              | 35 +---------
>  drivers/tty/serial/pxa.c                   | 39 +++--------
>  drivers/tty/serial/rp2.c                   | 36 ++--------
>  drivers/tty/serial/sa1100.c                | 49 +++++--------
>  drivers/tty/serial/serial_txx9.c           | 37 ++--------
>  drivers/tty/serial/sifive.c                | 45 ++----------
>  drivers/tty/serial/sprd_serial.c           | 38 ++--------
>  drivers/tty/serial/st-asc.c                | 50 ++-----------
>  drivers/tty/serial/vt8500_serial.c         | 40 ++---------
>  include/linux/serial_core.h                | 81 ++++++++++++++++++++++
>  28 files changed, 275 insertions(+), 822 deletions(-)

For the whole series:

Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Ilpo Järvinen Sept. 7, 2022, 10:16 a.m. UTC | #4
On Wed, 7 Sep 2022, Jiri Slaby wrote:

> On 06. 09. 22, 13:30, Johan Hovold wrote:
> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> > > This series introduces DEFINE_UART_PORT_TX_HELPER +
> > > DEFINE_UART_PORT_TX_HELPER_LIMITED TX helpers. See PATCH 2/4 for the
> > > details. Comments welcome.
> > > 
> > > Then it switches drivers to use them. First, to
> > > DEFINE_UART_PORT_TX_HELPER() in 3/4 and then
> > > DEFINE_UART_PORT_TX_HELPER_LIMITED() in 4/4.
> > > 
> > > The diffstat of patches 3+4 is as follows:
> > >   26 files changed, 191 insertions(+), 823 deletions(-)
> > > which appears to be nice.
> > 
> > Not really. This is horrid. Quality can't be measured in LoC (only).
> > 
> > The resulting code is unreadable. And for no good reason.
> 
> IMO, it's much more readable than the original ~ 30 various (and buggy -- see
> Ilpo's fixes) copies of this code. Apart from that, it makes further rework
> much easier (I have switch to kfifo in my mind for example).
> 
> > [ And note that you're "saving" something like 20 lines per driver:
> 
> It's not about saving, it's about deduplicating and unifying.
>
> > 	 12 files changed, 84 insertions(+), 349 deletions(-)
> > ]
> > 
> > NAK
> 
> I'd love to come up with something nicer. That would be a function in
> serial-core calling hooks like I had [1] for example. But provided all those
> CPU workarounds/thunks, it'd be quite expensive to call two functions per
> character.
> 
> Or creating a static inline (having ± the macro content) and the hooks as
> parameters and hope for optimizations to eliminate thunks (also suggested in
> the past [1]).
> 
> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/

I second Jiri here.

Saving lines in drivers is not that important compared with all removing 
all the variants of the same thing that have crept there over the years.

I suspect the main reason for the variants is that everybody just used 
other drivers as examples and therefore we've a few "main" variant 
branches depending on which of the drivers was used as an example for the 
other. That is hardly a good enough reason to keep them different and as 
long as each driver keeps its own function for this, it will eventually 
lead to similar differentiation so e.g. a one-time band-aid similarization 
would not help in the long run.

Also, I don't understand why you see it unreadable when the actual code is 
out in the open in that macro. It's formatted much better than e.g. 
read_poll_timeout() if you want an example of something that is hardly 
readable ;-). I agree though there's a learning-curve, albeit small, that 
it actually creates a function but that doesn't seem to me as big of an 
obstacle you seem to think.
Arnd Bergmann Sept. 7, 2022, 11:59 a.m. UTC | #5
On Wed, Sep 7, 2022, at 12:16 PM, Ilpo Järvinen wrote:
> On Wed, 7 Sep 2022, Jiri Slaby wrote:
>> On 06. 09. 22, 13:30, Johan Hovold wrote:
>> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
>> > NAK
>> 
>> I'd love to come up with something nicer. That would be a function in
>> serial-core calling hooks like I had [1] for example. But provided all those
>> CPU workarounds/thunks, it'd be quite expensive to call two functions per
>> character.
>> 
>> Or creating a static inline (having ± the macro content) and the hooks as
>> parameters and hope for optimizations to eliminate thunks (also suggested in
>> the past [1]).
>> 
>> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/
>
> I second Jiri here.
>
> Saving lines in drivers is not that important compared with all removing 
> all the variants of the same thing that have crept there over the years.
>
> I suspect the main reason for the variants is that everybody just used 
> other drivers as examples and therefore we've a few "main" variant 
> branches depending on which of the drivers was used as an example for the 
> other. That is hardly a good enough reason to keep them different and as 
> long as each driver keeps its own function for this, it will eventually 
> lead to similar differentiation so e.g. a one-time band-aid similarization 
> would not help in the long run.
>
> Also, I don't understand why you see it unreadable when the actual code is 
> out in the open in that macro. It's formatted much better than e.g. 
> read_poll_timeout() if you want an example of something that is hardly 
> readable ;-). I agree though there's a learning-curve, albeit small, that 
> it actually creates a function but that doesn't seem to me as big of an 
> obstacle you seem to think.

I think it would help to replace the macro that defines
the function with a set of macros that can be used in
function bodies. This would avoid the __VA_ARGS__ stuff
and allow readers that are unfamiliar with tty drivers to
treat it as a function call.

So e.g. instead of 

static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
		true,
		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
		({}));

the altera_jtaguart driver would contain a function like

static int altera_jtaguart_do_tx_chars(struct uart_port *port,
                                       unsigned int count)
{
       char ch;

       return uart_port_tx_helper_limited(port, ch, count, true,
                writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
                ({}));
}

or some variation of that. It's a few more lines, but those
extra lines would help me understand what is actually going on
while still avoiding the usual bugs and duplication.

If the caller of that function is itself trivial (like
serial21285_tx_chars), then the intermediate function can
be omitted in order to save some of the extra complexity.

       Arnd
Ilpo Järvinen Sept. 7, 2022, 12:21 p.m. UTC | #6
On Wed, 7 Sep 2022, Arnd Bergmann wrote:

> On Wed, Sep 7, 2022, at 12:16 PM, Ilpo Järvinen wrote:
> > On Wed, 7 Sep 2022, Jiri Slaby wrote:
> >> On 06. 09. 22, 13:30, Johan Hovold wrote:
> >> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> >> > NAK
> >> 
> >> I'd love to come up with something nicer. That would be a function in
> >> serial-core calling hooks like I had [1] for example. But provided all those
> >> CPU workarounds/thunks, it'd be quite expensive to call two functions per
> >> character.
> >> 
> >> Or creating a static inline (having ± the macro content) and the hooks as
> >> parameters and hope for optimizations to eliminate thunks (also suggested in
> >> the past [1]).
> >> 
> >> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/
> >
> > I second Jiri here.
> >
> > Saving lines in drivers is not that important compared with all removing 
> > all the variants of the same thing that have crept there over the years.
> >
> > I suspect the main reason for the variants is that everybody just used 
> > other drivers as examples and therefore we've a few "main" variant 
> > branches depending on which of the drivers was used as an example for the 
> > other. That is hardly a good enough reason to keep them different and as 
> > long as each driver keeps its own function for this, it will eventually 
> > lead to similar differentiation so e.g. a one-time band-aid similarization 
> > would not help in the long run.
> >
> > Also, I don't understand why you see it unreadable when the actual code is 
> > out in the open in that macro. It's formatted much better than e.g. 
> > read_poll_timeout() if you want an example of something that is hardly 
> > readable ;-). I agree though there's a learning-curve, albeit small, that 
> > it actually creates a function but that doesn't seem to me as big of an 
> > obstacle you seem to think.
> 
> I think it would help to replace the macro that defines
> the function with a set of macros that can be used in
> function bodies. This would avoid the __VA_ARGS__ stuff
> and allow readers that are unfamiliar with tty drivers to
> treat it as a function call.
> 
> So e.g. instead of 
> 
> static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
> 		true,
> 		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> 		({}));
> 
> the altera_jtaguart driver would contain a function like
> 
> static int altera_jtaguart_do_tx_chars(struct uart_port *port,
>                                        unsigned int count)
> {
>        char ch;
> 
>        return uart_port_tx_helper_limited(port, ch, count, true,
>                 writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
>                 ({}));
> }
> 
> or some variation of that. It's a few more lines, but those
> extra lines would help me understand what is actually going on
> while still avoiding the usual bugs and duplication.
> 
> If the caller of that function is itself trivial (like
> serial21285_tx_chars), then the intermediate function can
> be omitted in order to save some of the extra complexity.

I'd be ok with that. There's still a small startle factor associated to 
passing that writel(...) as an argument to a "function" but it's the same 
for other things such as read_poll_timeout() so not an end of the world.
Greg Kroah-Hartman Sept. 7, 2022, 12:27 p.m. UTC | #7
On Wed, Sep 07, 2022 at 03:21:28PM +0300, Ilpo Järvinen wrote:
> On Wed, 7 Sep 2022, Arnd Bergmann wrote:
> 
> > On Wed, Sep 7, 2022, at 12:16 PM, Ilpo Järvinen wrote:
> > > On Wed, 7 Sep 2022, Jiri Slaby wrote:
> > >> On 06. 09. 22, 13:30, Johan Hovold wrote:
> > >> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> > >> > NAK
> > >> 
> > >> I'd love to come up with something nicer. That would be a function in
> > >> serial-core calling hooks like I had [1] for example. But provided all those
> > >> CPU workarounds/thunks, it'd be quite expensive to call two functions per
> > >> character.
> > >> 
> > >> Or creating a static inline (having ± the macro content) and the hooks as
> > >> parameters and hope for optimizations to eliminate thunks (also suggested in
> > >> the past [1]).
> > >> 
> > >> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/
> > >
> > > I second Jiri here.
> > >
> > > Saving lines in drivers is not that important compared with all removing 
> > > all the variants of the same thing that have crept there over the years.
> > >
> > > I suspect the main reason for the variants is that everybody just used 
> > > other drivers as examples and therefore we've a few "main" variant 
> > > branches depending on which of the drivers was used as an example for the 
> > > other. That is hardly a good enough reason to keep them different and as 
> > > long as each driver keeps its own function for this, it will eventually 
> > > lead to similar differentiation so e.g. a one-time band-aid similarization 
> > > would not help in the long run.
> > >
> > > Also, I don't understand why you see it unreadable when the actual code is 
> > > out in the open in that macro. It's formatted much better than e.g. 
> > > read_poll_timeout() if you want an example of something that is hardly 
> > > readable ;-). I agree though there's a learning-curve, albeit small, that 
> > > it actually creates a function but that doesn't seem to me as big of an 
> > > obstacle you seem to think.
> > 
> > I think it would help to replace the macro that defines
> > the function with a set of macros that can be used in
> > function bodies. This would avoid the __VA_ARGS__ stuff
> > and allow readers that are unfamiliar with tty drivers to
> > treat it as a function call.
> > 
> > So e.g. instead of 
> > 
> > static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
> > 		true,
> > 		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > 		({}));
> > 
> > the altera_jtaguart driver would contain a function like
> > 
> > static int altera_jtaguart_do_tx_chars(struct uart_port *port,
> >                                        unsigned int count)
> > {
> >        char ch;
> > 
> >        return uart_port_tx_helper_limited(port, ch, count, true,
> >                 writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> >                 ({}));
> > }
> > 
> > or some variation of that. It's a few more lines, but those
> > extra lines would help me understand what is actually going on
> > while still avoiding the usual bugs and duplication.
> > 
> > If the caller of that function is itself trivial (like
> > serial21285_tx_chars), then the intermediate function can
> > be omitted in order to save some of the extra complexity.
> 
> I'd be ok with that. There's still a small startle factor associated to 
> passing that writel(...) as an argument to a "function" but it's the same 
> for other things such as read_poll_timeout() so not an end of the world.

That's going to incure the function-pointer-indirection-call for every
character that Jiri's original submission had, so I don't think this is
a very viable solution, sorry.

thanks,

greg k-h
Ilpo Järvinen Sept. 7, 2022, 12:32 p.m. UTC | #8
On Wed, 7 Sep 2022, Greg Kroah-Hartman wrote:

> On Wed, Sep 07, 2022 at 03:21:28PM +0300, Ilpo Järvinen wrote:
> > On Wed, 7 Sep 2022, Arnd Bergmann wrote:
> > 
> > > On Wed, Sep 7, 2022, at 12:16 PM, Ilpo Järvinen wrote:
> > > > On Wed, 7 Sep 2022, Jiri Slaby wrote:
> > > >> On 06. 09. 22, 13:30, Johan Hovold wrote:
> > > >> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> > > >> > NAK
> > > >> 
> > > >> I'd love to come up with something nicer. That would be a function in
> > > >> serial-core calling hooks like I had [1] for example. But provided all those
> > > >> CPU workarounds/thunks, it'd be quite expensive to call two functions per
> > > >> character.
> > > >> 
> > > >> Or creating a static inline (having ± the macro content) and the hooks as
> > > >> parameters and hope for optimizations to eliminate thunks (also suggested in
> > > >> the past [1]).
> > > >> 
> > > >> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/
> > > >
> > > > I second Jiri here.
> > > >
> > > > Saving lines in drivers is not that important compared with all removing 
> > > > all the variants of the same thing that have crept there over the years.
> > > >
> > > > I suspect the main reason for the variants is that everybody just used 
> > > > other drivers as examples and therefore we've a few "main" variant 
> > > > branches depending on which of the drivers was used as an example for the 
> > > > other. That is hardly a good enough reason to keep them different and as 
> > > > long as each driver keeps its own function for this, it will eventually 
> > > > lead to similar differentiation so e.g. a one-time band-aid similarization 
> > > > would not help in the long run.
> > > >
> > > > Also, I don't understand why you see it unreadable when the actual code is 
> > > > out in the open in that macro. It's formatted much better than e.g. 
> > > > read_poll_timeout() if you want an example of something that is hardly 
> > > > readable ;-). I agree though there's a learning-curve, albeit small, that 
> > > > it actually creates a function but that doesn't seem to me as big of an 
> > > > obstacle you seem to think.
> > > 
> > > I think it would help to replace the macro that defines
> > > the function with a set of macros that can be used in
> > > function bodies. This would avoid the __VA_ARGS__ stuff
> > > and allow readers that are unfamiliar with tty drivers to
> > > treat it as a function call.
> > > 
> > > So e.g. instead of 
> > > 
> > > static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
> > > 		true,
> > > 		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > > 		({}));
> > > 
> > > the altera_jtaguart driver would contain a function like
> > > 
> > > static int altera_jtaguart_do_tx_chars(struct uart_port *port,
> > >                                        unsigned int count)
> > > {
> > >        char ch;
> > > 
> > >        return uart_port_tx_helper_limited(port, ch, count, true,
> > >                 writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > >                 ({}));
> > > }
> > > 
> > > or some variation of that. It's a few more lines, but those
> > > extra lines would help me understand what is actually going on
> > > while still avoiding the usual bugs and duplication.
> > > 
> > > If the caller of that function is itself trivial (like
> > > serial21285_tx_chars), then the intermediate function can
> > > be omitted in order to save some of the extra complexity.
> > 
> > I'd be ok with that. There's still a small startle factor associated to 
> > passing that writel(...) as an argument to a "function" but it's the same 
> > for other things such as read_poll_timeout() so not an end of the world.
> 
> That's going to incure the function-pointer-indirection-call for every
> character that Jiri's original submission had, so I don't think this is
> a very viable solution, sorry.

I don't think you got what Arnd meant. It must still be technically a 
#define because you cannot pass
	writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG)
as an argument to a real function like he did in the example above.
It's similar to how read_poll_timeout() and friends are #defines despite 
being lowercased.
Greg Kroah-Hartman Sept. 7, 2022, 12:36 p.m. UTC | #9
On Wed, Sep 07, 2022 at 03:32:44PM +0300, Ilpo Järvinen wrote:
> On Wed, 7 Sep 2022, Greg Kroah-Hartman wrote:
> 
> > On Wed, Sep 07, 2022 at 03:21:28PM +0300, Ilpo Järvinen wrote:
> > > On Wed, 7 Sep 2022, Arnd Bergmann wrote:
> > > 
> > > > On Wed, Sep 7, 2022, at 12:16 PM, Ilpo Järvinen wrote:
> > > > > On Wed, 7 Sep 2022, Jiri Slaby wrote:
> > > > >> On 06. 09. 22, 13:30, Johan Hovold wrote:
> > > > >> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> > > > >> > NAK
> > > > >> 
> > > > >> I'd love to come up with something nicer. That would be a function in
> > > > >> serial-core calling hooks like I had [1] for example. But provided all those
> > > > >> CPU workarounds/thunks, it'd be quite expensive to call two functions per
> > > > >> character.
> > > > >> 
> > > > >> Or creating a static inline (having ± the macro content) and the hooks as
> > > > >> parameters and hope for optimizations to eliminate thunks (also suggested in
> > > > >> the past [1]).
> > > > >> 
> > > > >> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/
> > > > >
> > > > > I second Jiri here.
> > > > >
> > > > > Saving lines in drivers is not that important compared with all removing 
> > > > > all the variants of the same thing that have crept there over the years.
> > > > >
> > > > > I suspect the main reason for the variants is that everybody just used 
> > > > > other drivers as examples and therefore we've a few "main" variant 
> > > > > branches depending on which of the drivers was used as an example for the 
> > > > > other. That is hardly a good enough reason to keep them different and as 
> > > > > long as each driver keeps its own function for this, it will eventually 
> > > > > lead to similar differentiation so e.g. a one-time band-aid similarization 
> > > > > would not help in the long run.
> > > > >
> > > > > Also, I don't understand why you see it unreadable when the actual code is 
> > > > > out in the open in that macro. It's formatted much better than e.g. 
> > > > > read_poll_timeout() if you want an example of something that is hardly 
> > > > > readable ;-). I agree though there's a learning-curve, albeit small, that 
> > > > > it actually creates a function but that doesn't seem to me as big of an 
> > > > > obstacle you seem to think.
> > > > 
> > > > I think it would help to replace the macro that defines
> > > > the function with a set of macros that can be used in
> > > > function bodies. This would avoid the __VA_ARGS__ stuff
> > > > and allow readers that are unfamiliar with tty drivers to
> > > > treat it as a function call.
> > > > 
> > > > So e.g. instead of 
> > > > 
> > > > static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
> > > > 		true,
> > > > 		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > > > 		({}));
> > > > 
> > > > the altera_jtaguart driver would contain a function like
> > > > 
> > > > static int altera_jtaguart_do_tx_chars(struct uart_port *port,
> > > >                                        unsigned int count)
> > > > {
> > > >        char ch;
> > > > 
> > > >        return uart_port_tx_helper_limited(port, ch, count, true,
> > > >                 writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > > >                 ({}));
> > > > }
> > > > 
> > > > or some variation of that. It's a few more lines, but those
> > > > extra lines would help me understand what is actually going on
> > > > while still avoiding the usual bugs and duplication.
> > > > 
> > > > If the caller of that function is itself trivial (like
> > > > serial21285_tx_chars), then the intermediate function can
> > > > be omitted in order to save some of the extra complexity.
> > > 
> > > I'd be ok with that. There's still a small startle factor associated to 
> > > passing that writel(...) as an argument to a "function" but it's the same 
> > > for other things such as read_poll_timeout() so not an end of the world.
> > 
> > That's going to incure the function-pointer-indirection-call for every
> > character that Jiri's original submission had, so I don't think this is
> > a very viable solution, sorry.
> 
> I don't think you got what Arnd meant. It must still be technically a 
> #define because you cannot pass
> 	writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG)
> as an argument to a real function like he did in the example above.
> It's similar to how read_poll_timeout() and friends are #defines despite 
> being lowercased.

Ok, no, I don't understand what Arnd meant here then :(

confused,

greg k-h
Ilpo Järvinen Sept. 7, 2022, 12:56 p.m. UTC | #10
On Wed, 7 Sep 2022, Greg Kroah-Hartman wrote:

> On Wed, Sep 07, 2022 at 03:32:44PM +0300, Ilpo Järvinen wrote:
> > On Wed, 7 Sep 2022, Greg Kroah-Hartman wrote:
> > 
> > > On Wed, Sep 07, 2022 at 03:21:28PM +0300, Ilpo Järvinen wrote:
> > > > On Wed, 7 Sep 2022, Arnd Bergmann wrote:
> > > > 
> > > > > On Wed, Sep 7, 2022, at 12:16 PM, Ilpo Järvinen wrote:
> > > > > > On Wed, 7 Sep 2022, Jiri Slaby wrote:
> > > > > >> On 06. 09. 22, 13:30, Johan Hovold wrote:
> > > > > >> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> > > > > >> > NAK
> > > > > >> 
> > > > > >> I'd love to come up with something nicer. That would be a function in
> > > > > >> serial-core calling hooks like I had [1] for example. But provided all those
> > > > > >> CPU workarounds/thunks, it'd be quite expensive to call two functions per
> > > > > >> character.
> > > > > >> 
> > > > > >> Or creating a static inline (having ± the macro content) and the hooks as
> > > > > >> parameters and hope for optimizations to eliminate thunks (also suggested in
> > > > > >> the past [1]).
> > > > > >> 
> > > > > >> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/
> > > > > >
> > > > > > I second Jiri here.
> > > > > >
> > > > > > Saving lines in drivers is not that important compared with all removing 
> > > > > > all the variants of the same thing that have crept there over the years.
> > > > > >
> > > > > > I suspect the main reason for the variants is that everybody just used 
> > > > > > other drivers as examples and therefore we've a few "main" variant 
> > > > > > branches depending on which of the drivers was used as an example for the 
> > > > > > other. That is hardly a good enough reason to keep them different and as 
> > > > > > long as each driver keeps its own function for this, it will eventually 
> > > > > > lead to similar differentiation so e.g. a one-time band-aid similarization 
> > > > > > would not help in the long run.
> > > > > >
> > > > > > Also, I don't understand why you see it unreadable when the actual code is 
> > > > > > out in the open in that macro. It's formatted much better than e.g. 
> > > > > > read_poll_timeout() if you want an example of something that is hardly 
> > > > > > readable ;-). I agree though there's a learning-curve, albeit small, that 
> > > > > > it actually creates a function but that doesn't seem to me as big of an 
> > > > > > obstacle you seem to think.
> > > > > 
> > > > > I think it would help to replace the macro that defines
> > > > > the function with a set of macros that can be used in
> > > > > function bodies. This would avoid the __VA_ARGS__ stuff
> > > > > and allow readers that are unfamiliar with tty drivers to
> > > > > treat it as a function call.
> > > > > 
> > > > > So e.g. instead of 
> > > > > 
> > > > > static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
> > > > > 		true,
> > > > > 		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > > > > 		({}));
> > > > > 
> > > > > the altera_jtaguart driver would contain a function like
> > > > > 
> > > > > static int altera_jtaguart_do_tx_chars(struct uart_port *port,
> > > > >                                        unsigned int count)
> > > > > {
> > > > >        char ch;
> > > > > 
> > > > >        return uart_port_tx_helper_limited(port, ch, count, true,
> > > > >                 writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > > > >                 ({}));
> > > > > }
> > > > > 
> > > > > or some variation of that. It's a few more lines, but those
> > > > > extra lines would help me understand what is actually going on
> > > > > while still avoiding the usual bugs and duplication.
> > > > > 
> > > > > If the caller of that function is itself trivial (like
> > > > > serial21285_tx_chars), then the intermediate function can
> > > > > be omitted in order to save some of the extra complexity.
> > > > 
> > > > I'd be ok with that. There's still a small startle factor associated to 
> > > > passing that writel(...) as an argument to a "function" but it's the same 
> > > > for other things such as read_poll_timeout() so not an end of the world.
> > > 
> > > That's going to incure the function-pointer-indirection-call for every
> > > character that Jiri's original submission had, so I don't think this is
> > > a very viable solution, sorry.
> > 
> > I don't think you got what Arnd meant. It must still be technically a 
> > #define because you cannot pass
> > 	writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG)
> > as an argument to a real function like he did in the example above.
> > It's similar to how read_poll_timeout() and friends are #defines despite 
> > being lowercased.
> 
> Ok, no, I don't understand what Arnd meant here then :(

I think he did just 2 things (compared with 2/4 of this series):

- __DEFINE_UART_PORT_TX_HELPER() doesn't create a function but just wraps 
the macro body with ({ }). Therefore, a driver must use it inside 
a function rather than the macro creating a new function with name.
- Use lowercase instead of uppercase (this is a trivial change)

When a driver is using these "function" that are just made look functions 
but are macros for real, it will not add function-pointer-indirection-call 
per character but pulls the whole loop from the macro into the function 
in the driver and injects those putchar, etc. directly into that loop. 
...It is just like read_poll_timeout() constructs the loop.
Arnd Bergmann Sept. 7, 2022, 1:47 p.m. UTC | #11
On Wed, Sep 7, 2022, at 2:56 PM, Ilpo Järvinen wrote:
> On Wed, 7 Sep 2022, Greg Kroah-Hartman wrote:
>> Ok, no, I don't understand what Arnd meant here then :(
>
> I think he did just 2 things (compared with 2/4 of this series):
>
> - __DEFINE_UART_PORT_TX_HELPER() doesn't create a function but just wraps 
> the macro body with ({ }). Therefore, a driver must use it inside 
> a function rather than the macro creating a new function with name.
> - Use lowercase instead of uppercase (this is a trivial change)
>
> When a driver is using these "function" that are just made look functions 
> but are macros for real, it will not add function-pointer-indirection-call 
> per character but pulls the whole loop from the macro into the function 
> in the driver and injects those putchar, etc. directly into that loop. 
> ...It is just like read_poll_timeout() constructs the loop.

Right, the example I was thinking of was wait_event(), which is
another common macro that takes expressions as arguments but gets
used like a function call. Same thing really.

Note that even with an always_inline function that takes a
function pointer argument, anything from gcc-4.7 up has no 
problem inlining through that, so it should not be any less
efficient though it might not help readability either.
See https://godbolt.org/z/o8GGG8TzM for an example of that.

     Arnd
Russell King (Oracle) Sept. 7, 2022, 1:52 p.m. UTC | #12
On Wed, Sep 07, 2022 at 02:36:37PM +0200, Greg Kroah-Hartman wrote:
> On Wed, Sep 07, 2022 at 03:32:44PM +0300, Ilpo Järvinen wrote:
> > On Wed, 7 Sep 2022, Greg Kroah-Hartman wrote:
> > 
> > > On Wed, Sep 07, 2022 at 03:21:28PM +0300, Ilpo Järvinen wrote:
> > > > On Wed, 7 Sep 2022, Arnd Bergmann wrote:
> > > > 
> > > > > On Wed, Sep 7, 2022, at 12:16 PM, Ilpo Järvinen wrote:
> > > > > > On Wed, 7 Sep 2022, Jiri Slaby wrote:
> > > > > >> On 06. 09. 22, 13:30, Johan Hovold wrote:
> > > > > >> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> > > > > >> > NAK
> > > > > >> 
> > > > > >> I'd love to come up with something nicer. That would be a function in
> > > > > >> serial-core calling hooks like I had [1] for example. But provided all those
> > > > > >> CPU workarounds/thunks, it'd be quite expensive to call two functions per
> > > > > >> character.
> > > > > >> 
> > > > > >> Or creating a static inline (having ± the macro content) and the hooks as
> > > > > >> parameters and hope for optimizations to eliminate thunks (also suggested in
> > > > > >> the past [1]).
> > > > > >> 
> > > > > >> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/
> > > > > >
> > > > > > I second Jiri here.
> > > > > >
> > > > > > Saving lines in drivers is not that important compared with all removing 
> > > > > > all the variants of the same thing that have crept there over the years.
> > > > > >
> > > > > > I suspect the main reason for the variants is that everybody just used 
> > > > > > other drivers as examples and therefore we've a few "main" variant 
> > > > > > branches depending on which of the drivers was used as an example for the 
> > > > > > other. That is hardly a good enough reason to keep them different and as 
> > > > > > long as each driver keeps its own function for this, it will eventually 
> > > > > > lead to similar differentiation so e.g. a one-time band-aid similarization 
> > > > > > would not help in the long run.
> > > > > >
> > > > > > Also, I don't understand why you see it unreadable when the actual code is 
> > > > > > out in the open in that macro. It's formatted much better than e.g. 
> > > > > > read_poll_timeout() if you want an example of something that is hardly 
> > > > > > readable ;-). I agree though there's a learning-curve, albeit small, that 
> > > > > > it actually creates a function but that doesn't seem to me as big of an 
> > > > > > obstacle you seem to think.
> > > > > 
> > > > > I think it would help to replace the macro that defines
> > > > > the function with a set of macros that can be used in
> > > > > function bodies. This would avoid the __VA_ARGS__ stuff
> > > > > and allow readers that are unfamiliar with tty drivers to
> > > > > treat it as a function call.
> > > > > 
> > > > > So e.g. instead of 
> > > > > 
> > > > > static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
> > > > > 		true,
> > > > > 		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > > > > 		({}));
> > > > > 
> > > > > the altera_jtaguart driver would contain a function like
> > > > > 
> > > > > static int altera_jtaguart_do_tx_chars(struct uart_port *port,
> > > > >                                        unsigned int count)
> > > > > {
> > > > >        char ch;
> > > > > 
> > > > >        return uart_port_tx_helper_limited(port, ch, count, true,
> > > > >                 writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
> > > > >                 ({}));
> > > > > }
> > > > > 
> > > > > or some variation of that. It's a few more lines, but those
> > > > > extra lines would help me understand what is actually going on
> > > > > while still avoiding the usual bugs and duplication.
> > > > > 
> > > > > If the caller of that function is itself trivial (like
> > > > > serial21285_tx_chars), then the intermediate function can
> > > > > be omitted in order to save some of the extra complexity.
> > > > 
> > > > I'd be ok with that. There's still a small startle factor associated to 
> > > > passing that writel(...) as an argument to a "function" but it's the same 
> > > > for other things such as read_poll_timeout() so not an end of the world.
> > > 
> > > That's going to incure the function-pointer-indirection-call for every
> > > character that Jiri's original submission had, so I don't think this is
> > > a very viable solution, sorry.
> > 
> > I don't think you got what Arnd meant. It must still be technically a 
> > #define because you cannot pass
> > 	writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG)
> > as an argument to a real function like he did in the example above.
> > It's similar to how read_poll_timeout() and friends are #defines despite 
> > being lowercased.
> 
> Ok, no, I don't understand what Arnd meant here then :(

Arnd's opening sentence when proposing the new thing was:

"I think it would help to replace the macro that defines
the function with a set of macros that can be used in
function bodies."

Given "a set of macros" and the quoted example, I take
"uart_port_tx_helper_limited" to be one of those macros - it has to be
a macro because - as others have pointed out - you can't pass
"writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG)" to a function.

Of course, it would have been nicer to see the definition of this
macro, because then we can understand what the "ch" argument is to
this macro, and how that relates to the macro argument that is
shown in the example as a writel().

Maybe a more complete example would help clear up the confusion?
Arnd?
Arnd Bergmann Sept. 7, 2022, 2:56 p.m. UTC | #13
On Wed, Sep 7, 2022, at 3:52 PM, Russell King (Oracle) wrote:
> On Wed, Sep 07, 2022 at 02:36:37PM +0200, Greg Kroah-Hartman wrote:
>
> Of course, it would have been nicer to see the definition of this
> macro, because then we can understand what the "ch" argument is to
> this macro, and how that relates to the macro argument that is
> shown in the example as a writel().

I pulled out the 'ch' variable from the macro to avoid having
the macro define local variables that are then passed to the
inner expressions. 

> Maybe a more complete example would help clear up the confusion?
> Arnd?

Here is a patch on top of the series that would implement the
uart_port_tx_helper_limited() and uart_port_tx_helper()
macros that can be used directly from drivers in place of defining
local functions, with the (alphabetically) first two drivers
converted to that.

I left the (now trivial) DEFINE_UART_PORT_TX_HELPER_LIMITED() and
DEFINE_UART_PORT_TX_HELPER() macros in place to keep it building,
but they would get removed if we decide to use something like
my suggested approach for all drivers.

   Arnd

diff --git a/drivers/tty/serial/21285.c b/drivers/tty/serial/21285.c
index 40cf1bb534f3..a0f5c59d6128 100644
--- a/drivers/tty/serial/21285.c
+++ b/drivers/tty/serial/21285.c
@@ -151,16 +151,14 @@ static irqreturn_t serial21285_rx_chars(int irq, void *dev_id)
 	return IRQ_HANDLED;
 }
 
-static DEFINE_UART_PORT_TX_HELPER_LIMITED(serial21285_do_tx_chars,
-		!(*CSR_UARTFLG & 0x20),
-		*CSR_UARTDR = ch,
-		({}));
-
 static irqreturn_t serial21285_tx_chars(int irq, void *dev_id)
 {
 	struct uart_port *port = dev_id;
+	unsigned int count = 256;
+	unsigned char ch;
 
-	serial21285_do_tx_chars(port, 256);
+	uart_port_tx_helper_limited(port, !(*CSR_UARTFLG & 0x20),
+				    *CSR_UARTDR = ch, ({}), count, ch);
 
 	return IRQ_HANDLED;
 }
diff --git a/drivers/tty/serial/altera_uart.c b/drivers/tty/serial/altera_uart.c
index 70c0ad431cf9..f81dd950cd39 100644
--- a/drivers/tty/serial/altera_uart.c
+++ b/drivers/tty/serial/altera_uart.c
@@ -246,10 +246,14 @@ static void altera_uart_rx_chars(struct altera_uart *pp)
 	tty_flip_buffer_push(&port->state->port);
 }
 
-static DEFINE_UART_PORT_TX_HELPER(altera_uart_tx_chars,
-		altera_uart_readl(port, ALTERA_UART_STATUS_REG) &
-		                ALTERA_UART_STATUS_TRDY_MSK,
-		altera_uart_writel(port, ch, ALTERA_UART_TXDATA_REG));
+static int altera_uart_tx_chars(struct uart_port *port)
+{
+	u8 ch;
+
+	return uart_port_tx_helper(port, 
+		altera_uart_readl(port, ALTERA_UART_STATUS_REG) & ALTERA_UART_STATUS_TRDY_MSK,
+		altera_uart_writel(port, ch, ALTERA_UART_TXDATA_REG), ch);
+}
 
 static irqreturn_t altera_uart_interrupt(int irq, void *data)
 {
diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h
index 7236fc76ba22..d48d2301d1b7 100644
--- a/include/linux/serial_core.h
+++ b/include/linux/serial_core.h
@@ -638,13 +638,11 @@ struct uart_driver {
 
 void uart_write_wakeup(struct uart_port *port);
 
-#define __DEFINE_UART_PORT_TX_HELPER(name, tx_ready, put_char, tx_done,	  \
-		for_test, for_post, ...)				  \
-unsigned int name(struct uart_port *port __VA_OPT__(,) __VA_ARGS__)	  \
-{									  \
+#define __uart_port_tx_helper(port, tx_ready, put_char, tx_done,	  \
+	       	for_test, for_post, ch)					  \
+({									  \
 	struct circ_buf *xmit = &port->state->xmit;			  \
 	unsigned int pending;						  \
-	u8 ch;								  \
 									  \
 	for (; (for_test) && (tx_ready); (for_post), port->icount.tx++) { \
 		if (port->x_char) {					  \
@@ -672,8 +670,15 @@ unsigned int name(struct uart_port *port __VA_OPT__(,) __VA_ARGS__)	  \
 			port->ops->stop_tx(port);			  \
 	}								  \
 									  \
-	return pending;							  \
-}
+	pending;							  \
+})
+
+#define uart_port_tx_helper_limited(port, tx_ready, put_char, tx_done, count, ch) \
+	__uart_port_tx_helper(port, tx_ready, put_char, tx_done, count, count--, ch)
+
+#define uart_port_tx_helper(port, tx_ready, put_char, ch)		  \
+	__uart_port_tx_helper(port, tx_ready, put_char, ({}), true, ({}), ch)
+
 
 /**
  * DEFINE_UART_PORT_TX_HELPER_LIMITED -- generate transmit helper for uart_port
@@ -703,9 +708,13 @@ unsigned int name(struct uart_port *port __VA_OPT__(,) __VA_ARGS__)	  \
  * For all of them, @port->lock is held, interrupts are locally disabled and
  * the expressions must not sleep.
  */
-#define DEFINE_UART_PORT_TX_HELPER_LIMITED(name, tx_ready, put_char, tx_done) \
-	__DEFINE_UART_PORT_TX_HELPER(name, tx_ready, put_char, tx_done,	      \
-			count, count--, unsigned int count)
+#define DEFINE_UART_PORT_TX_HELPER_LIMITED(name, tx_ready, put_char, tx_done)	\
+unsigned int name(struct uart_port *port, unsigned int count)			\
+{										\
+	u8 ch;									\
+	return uart_port_tx_helper_limited(port, tx_ready, put_char, tx_done,	\
+					   count, ch);				\
+}
 
 /**
  * DEFINE_UART_PORT_TX_HELPER -- generate transmit helper for uart_port
@@ -715,9 +724,12 @@ unsigned int name(struct uart_port *port __VA_OPT__(,) __VA_ARGS__)	  \
  *
  * See DEFINE_UART_PORT_TX_HELPER_LIMITED() for more details.
  */
-#define DEFINE_UART_PORT_TX_HELPER(name, tx_ready, put_char)		\
-	__DEFINE_UART_PORT_TX_HELPER(name, tx_ready, put_char, ({}),	\
-			true, ({}))
+#define DEFINE_UART_PORT_TX_HELPER(name, tx_ready, put_char, ...)	  \
+unsigned int name(struct uart_port *port __VA_OPT__(,) __VA_ARGS__)	  \
+{									  \
+	u8 ch;								  \
+	return uart_port_tx_helper(port, tx_ready, put_char, ch);	  \
+}
 
 /*
  * Baud rate helpers.
Jiri Slaby Sept. 9, 2022, 10:53 a.m. UTC | #14
On 07. 09. 22, 16:56, Arnd Bergmann wrote:
> On Wed, Sep 7, 2022, at 3:52 PM, Russell King (Oracle) wrote:
>> On Wed, Sep 07, 2022 at 02:36:37PM +0200, Greg Kroah-Hartman wrote:
>>
>> Of course, it would have been nicer to see the definition of this
>> macro, because then we can understand what the "ch" argument is to
>> this macro, and how that relates to the macro argument that is
>> shown in the example as a writel().
> 
> I pulled out the 'ch' variable from the macro to avoid having
> the macro define local variables that are then passed to the
> inner expressions.

Note that I had "port" and "ch" as a part of the macro parameters in 
[v2], but it didn't help the situation much.
>> Maybe a more complete example would help clear up the confusion?
>> Arnd?
> 
> Here is a patch on top of the series that would implement the
> uart_port_tx_helper_limited() and uart_port_tx_helper()
> macros that can be used directly from drivers in place of defining
> local functions, with the (alphabetically) first two drivers
> converted to that.

If there are no objections, I will push the patches this directorin. I 
like this more than [v2] or [v3] (the helper macros). Actually, I 
mentioned this wait_event() style in [v1], but I perhaps simplified the 
concept too much to completely eliminate the need of a wrapper function. 
And that made it too complicated/too hard to understand.

Except I'd drop the "_helper" part from the name. Originally (in [v1]), 
I had uart_port_tx() and uart_port_tx_limited() functions. In [v2+v3], I 
added _helper to avoid confusion as we were generating a helpers using 
the macros. Yes, technically, uart_port_tx() is still a helper, but I 
think it's superfluous to have it in the name now.

thanks,
Greg Kroah-Hartman Sept. 9, 2022, 11:06 a.m. UTC | #15
On Fri, Sep 09, 2022 at 12:53:04PM +0200, Jiri Slaby wrote:
> On 07. 09. 22, 16:56, Arnd Bergmann wrote:
> > On Wed, Sep 7, 2022, at 3:52 PM, Russell King (Oracle) wrote:
> > > On Wed, Sep 07, 2022 at 02:36:37PM +0200, Greg Kroah-Hartman wrote:
> > > 
> > > Of course, it would have been nicer to see the definition of this
> > > macro, because then we can understand what the "ch" argument is to
> > > this macro, and how that relates to the macro argument that is
> > > shown in the example as a writel().
> > 
> > I pulled out the 'ch' variable from the macro to avoid having
> > the macro define local variables that are then passed to the
> > inner expressions.
> 
> Note that I had "port" and "ch" as a part of the macro parameters in [v2],
> but it didn't help the situation much.
> > > Maybe a more complete example would help clear up the confusion?
> > > Arnd?
> > 
> > Here is a patch on top of the series that would implement the
> > uart_port_tx_helper_limited() and uart_port_tx_helper()
> > macros that can be used directly from drivers in place of defining
> > local functions, with the (alphabetically) first two drivers
> > converted to that.
> 
> If there are no objections, I will push the patches this directorin. I like
> this more than [v2] or [v3] (the helper macros). Actually, I mentioned this
> wait_event() style in [v1], but I perhaps simplified the concept too much to
> completely eliminate the need of a wrapper function. And that made it too
> complicated/too hard to understand.
> 
> Except I'd drop the "_helper" part from the name. Originally (in [v1]), I
> had uart_port_tx() and uart_port_tx_limited() functions. In [v2+v3], I added
> _helper to avoid confusion as we were generating a helpers using the macros.
> Yes, technically, uart_port_tx() is still a helper, but I think it's
> superfluous to have it in the name now.

No objection from me, thanks for doing this work!

greg k-h
Johan Hovold Sept. 9, 2022, 12:13 p.m. UTC | #16
On Wed, Sep 07, 2022 at 09:19:25AM +0200, Jiri Slaby wrote:
> On 06. 09. 22, 13:30, Johan Hovold wrote:
> > On Tue, Sep 06, 2022 at 12:48:01PM +0200, Jiri Slaby wrote:
> >> This series introduces DEFINE_UART_PORT_TX_HELPER +
> >> DEFINE_UART_PORT_TX_HELPER_LIMITED TX helpers. See PATCH 2/4 for the
> >> details. Comments welcome.
> >>
> >> Then it switches drivers to use them. First, to
> >> DEFINE_UART_PORT_TX_HELPER() in 3/4 and then
> >> DEFINE_UART_PORT_TX_HELPER_LIMITED() in 4/4.
> >>
> >> The diffstat of patches 3+4 is as follows:
> >>   26 files changed, 191 insertions(+), 823 deletions(-)
> >> which appears to be nice.
> > 
> > Not really. This is horrid. Quality can't be measured in LoC (only).
> > 
> > The resulting code is unreadable. And for no good reason.
> 
> IMO, it's much more readable than the original ~ 30 various (and buggy 
> -- see Ilpo's fixes) copies of this code. Apart from that, it makes 
> further rework much easier (I have switch to kfifo in my mind for example).

Sure, but you can't have that at the cost of something that is
unreadable. Arnd's suggestions seems to improve things somewhat in this
respect, though.
 
> > [ And note that you're "saving" something like 20 lines per driver:
> 
> It's not about saving, it's about deduplicating and unifying.

It was you who brought the diff stat that up and I put this in
parentheses for a reason.
 
> > 	 12 files changed, 84 insertions(+), 349 deletions(-)
> > ]
> > 
> > NAK
> 
> I'd love to come up with something nicer. That would be a function in 
> serial-core calling hooks like I had [1] for example. But provided all 
> those CPU workarounds/thunks, it'd be quite expensive to call two 
> functions per character.

Yeah, that was even worse so no need to reiterate that.

> Or creating a static inline (having ± the macro content) and the hooks 
> as parameters and hope for optimizations to eliminate thunks (also 
> suggested in the past [1]).
> 
> [1] https://lore.kernel.org/all/20220411105405.9519-1-jslaby@suse.cz/

If that works then that should improve readability as well.

Johan
Johan Hovold Sept. 9, 2022, 12:23 p.m. UTC | #17
On Wed, Sep 07, 2022 at 01:16:23PM +0300, Ilpo Järvinen wrote:

> Also, I don't understand why you see it unreadable when the actual code is 
> out in the open in that macro. It's formatted much better than e.g. 
> read_poll_timeout() if you want an example of something that is hardly 
> readable ;-). I agree though there's a learning-curve, albeit small, that 
> it actually creates a function but that doesn't seem to me as big of an 
> obstacle you seem to think.

There's a huge difference between the proposed macro with its
I-lost-count-of-how-many arguments and levels of indirection and
something like wait_event() which take one condition and has a
descriptive name:

	wait_event(device->misc_wait, atomic_read(&device->ap_pending_cnt) == 0);

vs

	static DEFINE_UART_PORT_TX_HELPER_LIMITED(altera_jtaguart_do_tx_chars,
		true,
		writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG),
		({}));

In the former case, you just need to look at the code to understand what
is going on, very much unlike in the latter case.

Johan
Johan Hovold Sept. 9, 2022, 12:41 p.m. UTC | #18
On Fri, Sep 09, 2022 at 12:53:04PM +0200, Jiri Slaby wrote:
> On 07. 09. 22, 16:56, Arnd Bergmann wrote:
> > On Wed, Sep 7, 2022, at 3:52 PM, Russell King (Oracle) wrote:
> >> On Wed, Sep 07, 2022 at 02:36:37PM +0200, Greg Kroah-Hartman wrote:
> >>
> >> Of course, it would have been nicer to see the definition of this
> >> macro, because then we can understand what the "ch" argument is to
> >> this macro, and how that relates to the macro argument that is
> >> shown in the example as a writel().
> > 
> > I pulled out the 'ch' variable from the macro to avoid having
> > the macro define local variables that are then passed to the
> > inner expressions.
> 
> Note that I had "port" and "ch" as a part of the macro parameters in 
> [v2], but it didn't help the situation much.
> >> Maybe a more complete example would help clear up the confusion?
> >> Arnd?
> > 
> > Here is a patch on top of the series that would implement the
> > uart_port_tx_helper_limited() and uart_port_tx_helper()
> > macros that can be used directly from drivers in place of defining
> > local functions, with the (alphabetically) first two drivers
> > converted to that.
> 
> If there are no objections, I will push the patches this directorin. I 
> like this more than [v2] or [v3] (the helper macros). Actually, I 
> mentioned this wait_event() style in [v1], but I perhaps simplified the 
> concept too much to completely eliminate the need of a wrapper function. 
> And that made it too complicated/too hard to understand.

This sounds much better. You also had some users that needed some
preamble which could now go in the same function (e.g.
altera_jtaguart_tx_chars()).

> Except I'd drop the "_helper" part from the name. Originally (in [v1]), 
> I had uart_port_tx() and uart_port_tx_limited() functions. In [v2+v3], I 
> added _helper to avoid confusion as we were generating a helpers using 
> the macros. Yes, technically, uart_port_tx() is still a helper, but I 
> think it's superfluous to have it in the name now.

That would also be an in improvement. For the altera example you could
end up with something like:

static void altera_jtaguart_tx_chars(struct altera_jtaguart *pp)
{
	struct uart_port *port = &pp->port;
	unsigned int space;

	space = (readl(port->membase + ALTERA_JTAGUART_CONTROL_REG) &
			ALTERA_JTAGUART_CONTROL_WSPACE_MSK) >>
			ALTERA_JTAGUART_CONTROL_WSPACE_OFF;

	uart_port_tx_chars(port, space,
			   writel(ch, port->membase + ALTERA_JTAGUART_DATA_REG));
}

which would be understandable.

If there are too many arguments to be passed in, then perhaps you can
explore Arnd's (and you own) suggestion to use inline helpers so that
the arguments are named.

Johan