linux-renesas-soc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] serial: sh-sci: Fix off-by-one error in FIFO threshold register setting
@ 2021-05-10 12:07 Geert Uytterhoeven
  2021-05-11  8:55 ` Wolfram Sang
  2021-05-11 10:07 ` Ulrich Hecht
  0 siblings, 2 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2021-05-10 12:07 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Ulrich Hecht, Wolfram Sang, Linh Phung
  Cc: linux-serial, linux-renesas-soc, Geert Uytterhoeven

The Receive FIFO Data Count Trigger field (RTRG[6:0]) in the Receive
FIFO Data Count Trigger Register (HSRTRGR) of HSCIF can only hold values
ranging from 0-127.  As the FIFO size is equal to 128 on HSCIF, the user
can write an out-of-range value, touching reserved bits.

Fix this by limiting the trigger value to the FIFO size minus one.
Reverse the order of the checks, to avoid rx_trig becoming zero if the
FIFO size is one.

Note that this change has no impact on other SCIF variants, as their
maximum supported trigger value is lower than the FIFO size anyway, and
the code below takes care of enforcing these limits.

Reported-by: Linh Phung <linh.phung.jy@renesas.com>
Fixes: a380ed461f66d1b8 ("serial: sh-sci: implement FIFO threshold register setting")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
---
Compile-tested only.

The BSP contains a different patch[1], which masks the value to write by
0x7f.  This is IMHO incorrect, as it would set the trigger value to zero
when 128 is requested.

[1] "serial: sh-sci: Using mask when writing to HSRTRGR"
    https://github.com/renesas-rcar/linux-bsp/commit/9915223f41c7d680aaaed12971601dc038ce76a3
---
 drivers/tty/serial/sh-sci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index ef37fdf37612f82f..4baf1316ea729931 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -1023,10 +1023,10 @@ static int scif_set_rtrg(struct uart_port *port, int rx_trig)
 {
 	unsigned int bits;
 
+	if (rx_trig >= port->fifosize)
+		rx_trig = port->fifosize - 1;
 	if (rx_trig < 1)
 		rx_trig = 1;
-	if (rx_trig >= port->fifosize)
-		rx_trig = port->fifosize;
 
 	/* HSCIF can be set to an arbitrary level. */
 	if (sci_getreg(port, HSRTRGR)->size) {
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] serial: sh-sci: Fix off-by-one error in FIFO threshold register setting
  2021-05-10 12:07 [PATCH] serial: sh-sci: Fix off-by-one error in FIFO threshold register setting Geert Uytterhoeven
@ 2021-05-11  8:55 ` Wolfram Sang
  2021-05-11  9:03   ` Geert Uytterhoeven
  2021-05-11 10:07 ` Ulrich Hecht
  1 sibling, 1 reply; 4+ messages in thread
From: Wolfram Sang @ 2021-05-11  8:55 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Greg Kroah-Hartman, Ulrich Hecht, Linh Phung, linux-serial,
	linux-renesas-soc

[-- Attachment #1: Type: text/plain, Size: 1324 bytes --]

On Mon, May 10, 2021 at 02:07:55PM +0200, Geert Uytterhoeven wrote:
> The Receive FIFO Data Count Trigger field (RTRG[6:0]) in the Receive
> FIFO Data Count Trigger Register (HSRTRGR) of HSCIF can only hold values
> ranging from 0-127.  As the FIFO size is equal to 128 on HSCIF, the user
> can write an out-of-range value, touching reserved bits.
> 
> Fix this by limiting the trigger value to the FIFO size minus one.
> Reverse the order of the checks, to avoid rx_trig becoming zero if the
> FIFO size is one.
> 
> Note that this change has no impact on other SCIF variants, as their
> maximum supported trigger value is lower than the FIFO size anyway, and
> the code below takes care of enforcing these limits.
> 
> Reported-by: Linh Phung <linh.phung.jy@renesas.com>
> Fixes: a380ed461f66d1b8 ("serial: sh-sci: implement FIFO threshold register setting")
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
> Compile-tested only.
> 
> The BSP contains a different patch[1], which masks the value to write by
> 0x7f.  This is IMHO incorrect, as it would set the trigger value to zero
> when 128 is requested.

Makes also sense to me to have the trigger at fifosize-1 to have one
spare byte to handle latencies.

Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] serial: sh-sci: Fix off-by-one error in FIFO threshold register setting
  2021-05-11  8:55 ` Wolfram Sang
@ 2021-05-11  9:03   ` Geert Uytterhoeven
  0 siblings, 0 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2021-05-11  9:03 UTC (permalink / raw)
  To: Wolfram Sang
  Cc: Greg Kroah-Hartman, Ulrich Hecht, Linh Phung,
	open list:SERIAL DRIVERS, Linux-Renesas

Hi Wolfram,

On Tue, May 11, 2021 at 10:55 AM Wolfram Sang
<wsa+renesas@sang-engineering.com> wrote:
> On Mon, May 10, 2021 at 02:07:55PM +0200, Geert Uytterhoeven wrote:
> > The Receive FIFO Data Count Trigger field (RTRG[6:0]) in the Receive
> > FIFO Data Count Trigger Register (HSRTRGR) of HSCIF can only hold values
> > ranging from 0-127.  As the FIFO size is equal to 128 on HSCIF, the user
> > can write an out-of-range value, touching reserved bits.
> >
> > Fix this by limiting the trigger value to the FIFO size minus one.
> > Reverse the order of the checks, to avoid rx_trig becoming zero if the
> > FIFO size is one.
> >
> > Note that this change has no impact on other SCIF variants, as their
> > maximum supported trigger value is lower than the FIFO size anyway, and
> > the code below takes care of enforcing these limits.
> >
> > Reported-by: Linh Phung <linh.phung.jy@renesas.com>
> > Fixes: a380ed461f66d1b8 ("serial: sh-sci: implement FIFO threshold register setting")
> > Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> > ---
> > Compile-tested only.
> >
> > The BSP contains a different patch[1], which masks the value to write by
> > 0x7f.  This is IMHO incorrect, as it would set the trigger value to zero
> > when 128 is requested.
>
> Makes also sense to me to have the trigger at fifosize-1 to have one
> spare byte to handle latencies.

Exactly, that's why the maximum value supported by the HSCIF
hardware is 127, not 128.

> Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Thanks!

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] serial: sh-sci: Fix off-by-one error in FIFO threshold register setting
  2021-05-10 12:07 [PATCH] serial: sh-sci: Fix off-by-one error in FIFO threshold register setting Geert Uytterhoeven
  2021-05-11  8:55 ` Wolfram Sang
@ 2021-05-11 10:07 ` Ulrich Hecht
  1 sibling, 0 replies; 4+ messages in thread
From: Ulrich Hecht @ 2021-05-11 10:07 UTC (permalink / raw)
  To: Geert Uytterhoeven, Greg Kroah-Hartman, Ulrich Hecht,
	Wolfram Sang, Linh Phung
  Cc: linux-serial, linux-renesas-soc


> On 05/10/2021 2:07 PM Geert Uytterhoeven <geert+renesas@glider.be> wrote:
> 
>  
> The Receive FIFO Data Count Trigger field (RTRG[6:0]) in the Receive
> FIFO Data Count Trigger Register (HSRTRGR) of HSCIF can only hold values
> ranging from 0-127.  As the FIFO size is equal to 128 on HSCIF, the user
> can write an out-of-range value, touching reserved bits.
> 
> Fix this by limiting the trigger value to the FIFO size minus one.
> Reverse the order of the checks, to avoid rx_trig becoming zero if the
> FIFO size is one.
> 
> Note that this change has no impact on other SCIF variants, as their
> maximum supported trigger value is lower than the FIFO size anyway, and
> the code below takes care of enforcing these limits.
> 
> Reported-by: Linh Phung <linh.phung.jy@renesas.com>
> Fixes: a380ed461f66d1b8 ("serial: sh-sci: implement FIFO threshold register setting")
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
> ---
> Compile-tested only.
> 
> The BSP contains a different patch[1], which masks the value to write by
> 0x7f.  This is IMHO incorrect, as it would set the trigger value to zero
> when 128 is requested.
> 
> [1] "serial: sh-sci: Using mask when writing to HSRTRGR"
>     https://github.com/renesas-rcar/linux-bsp/commit/9915223f41c7d680aaaed12971601dc038ce76a3
> ---
>  drivers/tty/serial/sh-sci.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
> index ef37fdf37612f82f..4baf1316ea729931 100644
> --- a/drivers/tty/serial/sh-sci.c
> +++ b/drivers/tty/serial/sh-sci.c
> @@ -1023,10 +1023,10 @@ static int scif_set_rtrg(struct uart_port *port, int rx_trig)
>  {
>  	unsigned int bits;
>  
> +	if (rx_trig >= port->fifosize)
> +		rx_trig = port->fifosize - 1;
>  	if (rx_trig < 1)
>  		rx_trig = 1;
> -	if (rx_trig >= port->fifosize)
> -		rx_trig = port->fifosize;
>  
>  	/* HSCIF can be set to an arbitrary level. */
>  	if (sci_getreg(port, HSRTRGR)->size) {
> -- 
> 2.25.1

Reviewed-by: Ulrich Hecht <uli+renesas@fpond.eu>

CU
Uli

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-11 10:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-10 12:07 [PATCH] serial: sh-sci: Fix off-by-one error in FIFO threshold register setting Geert Uytterhoeven
2021-05-11  8:55 ` Wolfram Sang
2021-05-11  9:03   ` Geert Uytterhoeven
2021-05-11 10:07 ` Ulrich Hecht

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).