Skip to contentSkip to conversation

serial: 8250: Fix runtime PM for start_tx() for RS485

PATCH Superseded Tony Lindgren 3 participants 12 messages Apr 11, 2022 TTY layer and serial drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 serial: 8250: Fix runtime PM for start_tx() for RS485

The early return from serial8250_start_tx() added by commit e490c9144cfa ("tty: Add software emulated RS485 support for 8250") failed to call serial8250_rpm_put_tx() that normally gets called on __stop_tx().

Likely this is a harmless issue as the RS485 using folks probably are not using runtime PM for the serial ports.

Fixes e490c9144cfa ("tty: Add software emulated RS485 support for 8250")
Cc Matwey V. Kornilov <[email protected]>
Cc Steffen Trumtrar <[email protected]>
Cc Uwe Kleine-König <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1681,8 +1681,10 @@ static void serial8250_start_tx(struct uart_port *port)
 		return;
 
 	if (em485 &&
-	    em485->active_timer == &em485->start_tx_timer)
+	    em485->active_timer == &em485->start_tx_timer) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}
 
 	if (em485)
 		start_tx_rs485(port);
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 serial: 8250: Fix runtime PM for start_tx() for empty buffer

Commit 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send") caused a regression where the drivers implementing runtime PM stopped idling.

We need to call serial8250_rpm_put_tx() on early exit, it normally gets called later on at __stop_tx().

Fixes 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send")
Cc Steffen Trumtrar <[email protected]>
Cc Uwe Kleine-König <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1677,8 +1677,10 @@ static void serial8250_start_tx(struct uart_port *port)
 
 	serial8250_rpm_get_tx(up);
 
-	if (!port->x_char && uart_circ_empty(&port->state->xmit))
+	if (!port->x_char && uart_circ_empty(&port->state->xmit)) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}
 
 	if (em485 &&
 	    em485->active_timer == &em485->start_tx_timer) {
Signature · 1 line
2.35.1
#2 lore raw

Re: [PATCH 2/2] serial: 8250: Fix runtime PM for start_tx() for empty buffer

Quoted 19 lines · Tony Lindgren

Commit 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send") caused a regression where the drivers implementing runtime PM stopped idling.

We need to call serial8250_rpm_put_tx() on early exit, it normally gets called later on at __stop_tx().

Fixes 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send")
Cc Steffen Trumtrar <[email protected]>
Cc Uwe Kleine-König <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1683

Quoted 8 lines
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1677,8 +1677,10 @@ static void serial8250_start_tx(struct uart_port *port)
 
 	serial8250_rpm_get_tx(up);
 
-	if (!port->x_char && uart_circ_empty(&port->state->xmit))
+	if (!port->x_char && uart_circ_empty(&port->state->xmit)) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

Assuming you don't need serial8250_rpm_get_tx() to check the condition, it would be easier to move the early return before the call to serial8250_rpm_get_tx().

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#3 lore raw

Re: [PATCH 2/2] serial: 8250: Fix runtime PM for start_tx() for empty buffer

Quoted 23 lines · Uwe Kleine-König
Quoted 22 lines · Tony Lindgren

Commit 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send") caused a regression where the drivers implementing runtime PM stopped idling.

We need to call serial8250_rpm_put_tx() on early exit, it normally gets called later on at __stop_tx().

Fixes 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send")
Cc Steffen Trumtrar <[email protected]>
Cc Uwe Kleine-König <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_port.c +0 -0
@@ -1677,8 +1677,10 @@ static void serial8250_start_tx(struct uart_port *port)
 
 	serial8250_rpm_get_tx(up);
 
Quoted 9 lines

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1683

Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +3 -1
-	if (!port->x_char && uart_circ_empty(&port->state->xmit))
+	if (!port->x_char && uart_circ_empty(&port->state->xmit)) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

Assuming you don't need serial8250_rpm_get_tx() to check the condition, it would be easier to move the early return before the call to serial8250_rpm_get_tx().

Yeah good suggestion, that should work.

Regards,

Tony

#4 lore raw

Re: [PATCH 2/2] serial: 8250: Fix runtime PM for start_tx() for empty buffer

In reply to Tony Lindgren
Quoted 19 lines · Tony Lindgren

Commit 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send") caused a regression where the drivers implementing runtime PM stopped idling.

We need to call serial8250_rpm_put_tx() on early exit, it normally gets called later on at __stop_tx().

Fixes 932d596378b0 ("serial: 8250: Return early in .start_tx() if there are no chars to send")
Cc Steffen Trumtrar <[email protected]>
Cc Uwe Kleine-König <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1683

Quoted 8 lines
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1677,8 +1677,10 @@ static void serial8250_start_tx(struct uart_port *port)
 
 	serial8250_rpm_get_tx(up);
 
-	if (!port->x_char && uart_circ_empty(&port->state->xmit))
+	if (!port->x_char && uart_circ_empty(&port->state->xmit)) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

Move this before the runtime pm get instead?

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1686

Quoted 3 lines
drivers/tty/serial/8250/8250_port.c +0 -0
 	if (em485 &&
 	    em485->active_timer == &em485->start_tx_timer) {

Johan

#5 lore raw

Re: [PATCH 2/2] serial: 8250: Fix runtime PM for start_tx() for empty buffer

Quoted 8 lines · Johan Hovold
Quoted 7 lines · Tony Lindgren
drivers/tty/serial/8250/8250_port.c +0 -0
@@ -1677,8 +1677,10 @@ static void serial8250_start_tx(struct uart_port *port)
 
 	serial8250_rpm_get_tx(up);
 
Quoted 7 lines

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1683

Quoted 5 lines
drivers/tty/serial/8250/8250_port.c +3 -1
-	if (!port->x_char && uart_circ_empty(&port->state->xmit))
+	if (!port->x_char && uart_circ_empty(&port->state->xmit)) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

Move this before the runtime pm get instead?

Yup good idea.

Tony

#6 lore raw
In reply to Tony Lindgren
Quoted 20 lines · Tony Lindgren

The early return from serial8250_start_tx() added by commit e490c9144cfa ("tty: Add software emulated RS485 support for 8250") failed to call serial8250_rpm_put_tx() that normally gets called on __stop_tx().

Likely this is a harmless issue as the RS485 using folks probably are not using runtime PM for the serial ports.

Fixes e490c9144cfa ("tty: Add software emulated RS485 support for 8250")
Cc Matwey V. Kornilov <[email protected]>
Cc Steffen Trumtrar <[email protected]>
Cc Uwe Kleine-König <[email protected]>
---
 drivers/tty/serial/8250/8250_port.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1687

Quoted 8 lines
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1681,8 +1681,10 @@ static void serial8250_start_tx(struct uart_port *port)
 		return;
 
 	if (em485 &&
-	    em485->active_timer == &em485->start_tx_timer)
+	    em485->active_timer == &em485->start_tx_timer) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

I was just taking a quick look at your report about this and also noticed this return statement.

The runtime PM implementation is a bit of mess as we've discussed elsewhere, but the change you propose here doesn't look right.

start_tx() can be deferred in the rs485 case, but that doesn't mean you should suspend the device here. In fact, that look like it would just break runtime PM (the parts that may work to some extent).

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1690

Quoted 3 lines
drivers/tty/serial/8250/8250_port.c +0 -0
 	if (em485)
 		start_tx_rs485(port);

Johan

#7 lore raw
Quoted 17 lines · Johan Hovold

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1687

Quoted 12 lines · Tony Lindgren
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1681,8 +1681,10 @@ static void serial8250_start_tx(struct uart_port *port)
 		return;
 
 	if (em485 &&
-	    em485->active_timer == &em485->start_tx_timer)
+	    em485->active_timer == &em485->start_tx_timer) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

I was just taking a quick look at your report about this and also noticed this return statement.

Quoted 2 lines

The runtime PM implementation is a bit of mess as we've discussed elsewhere, but the change you propose here doesn't look right.

Frankly "a bit of mess" applies "a bit more" than just the serial runtime PM :)

Quoted 3 lines

start_tx() can be deferred in the rs485 case, but that doesn't mean you should suspend the device here. In fact, that look like it would just break runtime PM (the parts that may work to some extent).

AFAIK there's currently nothing paired with the serial8250_rpm_get_tx(up) call at the beginning of serial8250_start_tx() for the early exit cases if start_tx_rs485() or __start_tx() won't get called.

Care to clarify a bit more what you have in mind?

Regards,

Tony

#8 lore raw
Quoted 18 lines · Tony Lindgren
Quoted 17 lines · Johan Hovold

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1687

Quoted 12 lines · Tony Lindgren
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1681,8 +1681,10 @@ static void serial8250_start_tx(struct uart_port *port)
 		return;
 
 	if (em485 &&
-	    em485->active_timer == &em485->start_tx_timer)
+	    em485->active_timer == &em485->start_tx_timer) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

I was just taking a quick look at your report about this and also noticed this return statement.

Quoted 5 lines
Quoted 2 lines

The runtime PM implementation is a bit of mess as we've discussed elsewhere, but the change you propose here doesn't look right.

Frankly "a bit of mess" applies "a bit more" than just the serial runtime PM :)

Heh. I'm afraid that's all too true. :)

Quoted 9 lines
Quoted 3 lines

start_tx() can be deferred in the rs485 case, but that doesn't mean you should suspend the device here. In fact, that look like it would just break runtime PM (the parts that may work to some extent).

AFAIK there's currently nothing paired with the serial8250_rpm_get_tx(up) call at the beginning of serial8250_start_tx() for the early exit cases if start_tx_rs485() or __start_tx() won't get called.

Care to clarify a bit more what you have in mind?

The problem is that that serial8250_rpm_put_tx() you're adding may suspend the device unconditionally (i.e. regardless of any previous calls to serial8250_rpm_get_tx()).

If rs485 tx is just being deferred you mustn't suspend the device before it has had a chance to start transmitting.

Johan

#9 lore raw
Quoted 35 lines · Johan Hovold
Quoted 18 lines · Tony Lindgren
Quoted 17 lines · Johan Hovold

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1687

Quoted 12 lines · Tony Lindgren
drivers/tty/serial/8250/8250_port.c +3 -1
@@ -1681,8 +1681,10 @@ static void serial8250_start_tx(struct uart_port *port)
 		return;
 
 	if (em485 &&
-	    em485->active_timer == &em485->start_tx_timer)
+	    em485->active_timer == &em485->start_tx_timer) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

I was just taking a quick look at your report about this and also noticed this return statement.

Quoted 5 lines
Quoted 2 lines

The runtime PM implementation is a bit of mess as we've discussed elsewhere, but the change you propose here doesn't look right.

Frankly "a bit of mess" applies "a bit more" than just the serial runtime PM :)

Heh. I'm afraid that's all too true. :)

Quoted 8 lines
Quoted 3 lines

start_tx() can be deferred in the rs485 case, but that doesn't mean you should suspend the device here. In fact, that look like it would just break runtime PM (the parts that may work to some extent).

AFAIK there's currently nothing paired with the serial8250_rpm_get_tx(up) call at the beginning of serial8250_start_tx() for the early exit cases if start_tx_rs485() or __start_tx() won't get called.

Quoted 8 lines
Quoted 1 line

Care to clarify a bit more what you have in mind?

The problem is that that serial8250_rpm_put_tx() you're adding may suspend the device unconditionally (i.e. regardless of any previous calls to serial8250_rpm_get_tx()).

If rs485 tx is just being deferred you mustn't suspend the device before it has had a chance to start transmitting.

Hmm I'm pretty sure rs485 has the runtime PM usage count is currently unbalanced. To me it seems em485->start_tx_timer calls start_tx() again from serial8250_em485_handle_start_tx().

Anyways, let's deal with the regression patch first, this can wait a bit.

Regards,

Tony

#10 lore raw
Quoted 21 lines · Tony Lindgren
Quoted 20 lines · Johan Hovold
Quoted 8 lines · Tony Lindgren
Quoted 7 lines · Johan Hovold
Quoted 6 lines · Tony Lindgren
drivers/tty/serial/8250/8250_port.c +0 -0
@@ -1681,8 +1681,10 @@ static void serial8250_start_tx(struct uart_port *port)
 		return;
 
Quoted 6 lines
Quoted 6 lines

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1687

Quoted 6 lines
drivers/tty/serial/8250/8250_port.c +3 -1
 	if (em485 &&
-	    em485->active_timer == &em485->start_tx_timer)
+	    em485->active_timer == &em485->start_tx_timer) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

The problem is that that serial8250_rpm_put_tx() you're adding may suspend the device unconditionally (i.e. regardless of any previous calls to serial8250_rpm_get_tx()).

Quoted 6 lines
Quoted 2 lines

If rs485 tx is just being deferred you mustn't suspend the device before it has had a chance to start transmitting.

Hmm I'm pretty sure rs485 has the runtime PM usage count is currently unbalanced. To me it seems em485->start_tx_timer calls start_tx() again from serial8250_em485_handle_start_tx().

It appears to call __start_tx(), but note that the only call to serial8250_rpm_get_tx() is in serial8250_start_tx() which this patch would have cancelled out.

Also note that the serial8250_rpm_get/set_tx() calls aren't supposed to be balanced. get() can be called multiple times and will only increment the PM usage counter once, while put() will decrement the counter whenever get() has been called once (and hence potentially suspend the device immediately).

Messy indeed.

Johan

#11 lore raw
Quoted 25 lines · Johan Hovold
Quoted 24 lines · Tony Lindgren
Quoted 22 lines · Johan Hovold
Quoted 8 lines · Tony Lindgren
Quoted 7 lines · Johan Hovold
Quoted 6 lines · Tony Lindgren
drivers/tty/serial/8250/8250_port.c +0 -0
@@ -1681,8 +1681,10 @@ static void serial8250_start_tx(struct uart_port *port)
 		return;
 
Quoted 6 lines
Quoted 6 lines

drivers/tty/serial/8250/8250_port.c static void serial8250_start_tx(struct uart_port *port) · line 1687

Quoted 6 lines
drivers/tty/serial/8250/8250_port.c +3 -1
 	if (em485 &&
-	    em485->active_timer == &em485->start_tx_timer)
+	    em485->active_timer == &em485->start_tx_timer) {
+		serial8250_rpm_put_tx(up);
 		return;
+	}

The problem is that that serial8250_rpm_put_tx() you're adding may suspend the device unconditionally (i.e. regardless of any previous calls to serial8250_rpm_get_tx()).

If rs485 tx is just being deferred you mustn't suspend the device before it has had a chance to start transmitting.

Quoted 7 lines
Quoted 3 lines

Hmm I'm pretty sure rs485 has the runtime PM usage count is currently unbalanced. To me it seems em485->start_tx_timer calls start_tx() again from serial8250_em485_handle_start_tx().

It appears to call __start_tx(), but note that the only call to serial8250_rpm_get_tx() is in serial8250_start_tx() which this patch would have cancelled out.

OK

Quoted 7 lines

Also note that the serial8250_rpm_get/set_tx() calls aren't supposed to be balanced. get() can be called multiple times and will only increment the PM usage counter once, while put() will decrement the counter whenever get() has been called once (and hence potentially suspend the device immediately).

Messy indeed.

Yeah that is not nice.

I'll send a patch to prepare things for runtime PM that will hopefully make things a bit easier as discussed earlier.

Regards,

Tony