Skip to contentSkip to conversation

tty/serial: create debugfs interface for UART register tracing

PATCH In review Dan Raymond 3 participants 4 messages Sep 16, 2023 – Sep 18, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 tty/serial: create debugfs interface for UART register tracing

Implement a UART register tracing facility using the debugfs. This can be used as a "serial port sniffer" to monitor UART traffic and line settings with timestamps at microsecond granularity. This can be useful for general serial port debugging or to debug the UART driver itself.

---
V1 -> V2:
- add Kconfig option to disable SERIAL_8250_DEBUG feature
- limit comments to 80 columns
- add defaults to documentation for debugfs files
- convert documentation to kernel-doc and link to Documentation/trace/index.rst
- switch user types (uint32_t) to kernel types (u32)
- reorganize reg_queue structure to prevent memory holes
- embed spinlock inside structure it protects
- reduce size of line[] buffer to avoid wasted memory
- use min_t() macro to select minimum of two integers
- simplify return statement from buffer_read()
- move comment for "round down to nearest power of 2"
- split double assignment of pointers into two lines
- use SZ_4K instead of 4096

V2 -> V3:
- change formatting of timestamp from "hh:mm:ss.mmmuuu" to "ssssssss.mmmuuu"
- create a new field in 'struct uart_port' instead of using 'private_data'

V3 -> V4:
- rename 'trace_buffer' to 'trace_pipe' and support blocking reads
- improve locking of register event queue data structure
- fix NULL pointer dereference when unbinding device

 Documentation/trace/index.rst        |   1 +
 Documentation/trace/serial.rst       |   5 +
 drivers/tty/serial/8250/8250.h       |   8 +
 drivers/tty/serial/8250/8250_core.c  |   2 +
 drivers/tty/serial/8250/8250_debug.c | 570 +++++++++++++++++++++++++++
 drivers/tty/serial/8250/Kconfig      |   9 +
 drivers/tty/serial/8250/Makefile     |   1 +
 include/linux/serial_core.h          |   1 +
 8 files changed, 597 insertions(+)
 create mode 100644 Documentation/trace/serial.rst
 create mode 100644 drivers/tty/serial/8250/8250_debug.c
Documentation/trace/index.rst +1 -0
index 2d73e8697523..2f85814204f0 100644
@@ -33,3 +33,4 @@ Linux Tracing Technologies
    coresight/index
    user_events
    rv/index
+   serial
Documentation/trace/serial.rst new file +5 -0
new file mode 100644
index 000000000000..c40c2a727f20
@@ -0,0 +1,5 @@
+UART Tracing
+------------
+
+.. kernel-doc:: drivers/tty/serial/8250/8250_debug.c
+   :doc: UART register tracing
drivers/tty/serial/8250/8250.h +8 -0
index 287153d32536..a0a224414d92 100644
@@ -403,3 +403,11 @@ static inline int serial_index(struct uart_port *port)
 {
 	return port->minor - 64;
 }
+
+#ifdef CONFIG_SERIAL_8250_DEBUG
+extern void uart_debug_create(struct uart_port *port);
+extern void uart_debug_destroy(struct uart_port *port);
+#else
+static inline void uart_debug_create(struct uart_port *port) { }
+static inline void uart_debug_destroy(struct uart_port *port) { }
+#endif
drivers/tty/serial/8250/8250_core.c +2 -0
index 94fbf0add2ce..1f7f8005e3ee 100644
@@ -1094,6 +1094,7 @@ int serial8250_register_8250_port(const struct uart_8250_port *up)
 			if (ret)
 				goto err;
 
+			uart_debug_create(&uart->port);
 			ret = uart->port.line;
 		} else {
 			dev_info(uart->port.dev,
@@ -1151,6 +1152,7 @@ void serial8250_unregister_port(int line)
 		spin_unlock_irqrestore(&uart->port.lock, flags);
 	}
 
+	uart_debug_destroy(&uart->port);
 	uart_remove_one_port(&serial8250_reg, &uart->port);
 	if (serial8250_isa_devs) {
 		uart->port.flags &= ~UPF_BOOT_AUTOCONF;
drivers/tty/serial/8250/8250_debug.c new file +570 -0

570 lines not on this page · raw message

drivers/tty/serial/8250/Kconfig +9 -0
index 7b3ffb7f50ab..9b7cf09500f7 100644
@@ -34,6 +34,15 @@ config SERIAL_8250
 	  Most people will say Y or M here, so that they can use serial mice,
 	  modems and similar devices connecting to the standard serial ports.
 
+config SERIAL_8250_DEBUG
+	bool "Debugging features for 8250/16550 serial ports"
+	depends on SERIAL_8250
+	default SERIAL_8250
+	help
+	  This builds debugging support that can be used with 8250/16550
+	  compatible UART controllers.  Currently this includes tracing of
+	  UART register I/O.
+
 config SERIAL_8250_DEPRECATED_OPTIONS
 	bool "Support 8250_core.* kernel options (DEPRECATED)"
 	depends on SERIAL_8250
drivers/tty/serial/8250/Makefile +1 -0
index bee908f99ea0..d0dc1c26d99c 100644
@@ -9,6 +9,7 @@ obj-$(CONFIG_SERIAL_8250)		+= 8250.o 8250_base.o
 8250-$(CONFIG_ALPHA_JENSEN)		+= 8250_alpha.o
 8250-$(CONFIG_SERIAL_8250_PNP)		+= 8250_pnp.o
 8250_base-y				:= 8250_port.o
+8250_base-$(CONFIG_SERIAL_8250_DEBUG)	+= 8250_debug.o
 8250_base-$(CONFIG_SERIAL_8250_DMA)	+= 8250_dma.o
 8250_base-$(CONFIG_SERIAL_8250_DWLIB)	+= 8250_dwlib.o
 8250_base-$(CONFIG_SERIAL_8250_FINTEK)	+= 8250_fintek.o
include/linux/serial_core.h +1 -0
index beb2ffd31fea..d44dfaf4c20c 100644
@@ -581,6 +581,7 @@ struct uart_port {
 	struct gpio_desc	*rs485_term_gpio;	/* enable RS485 bus termination */
 	struct serial_iso7816   iso7816;
 	void			*private_data;		/* generic platform data pointer */
+	void			*debug_data;		/* used for debug purposes only */
 };
 
 static inline int serial_port_in(struct uart_port *up, int offset)

base-commit: be8b93b5cc7d533eb8c9b0590cdac055ecafe13a

Signature · 1 line
2.25.1
#1 lore raw

I took Andy Shevchenko's suggestion and added port I/O tracing in a similar manner as the memory-mapped I/O tracing enabled with CONFIG_TRACE_MMIO_ACCESS. I'll be submitting another patch for that shortly. It works well and it provides another way to trace UART register accesses. For most purposes it is probably superior to the method used in this patch because it leverages the existing kernel tracing framework.

However, this patch might still be useful for debugging issues that are sensitive to timing because it uses less CPU resources. It also uses less memory so a larger trace can be collected. I did some more timing analysis and tried to minimize the impact of cache misses by reading the MSR register 100 times in a loop. I added this test to serial8250_do_get_mctrl() and ran 'cat /proc/tty/driver/serial' to exercise the test. Here are the results (trace overhead is measured in CPU cycles at 1GHz):

overhead   method
--------   ------------------------------------------
       0   serial_debugfs disabled
     158   serial_debugfs enabled without pipe reader
     367   serial_debugfs enabled with pipe reader
       2   portio_tracefs disabled
     461   portio_tracefs enabled but filtered
     713   portio_tracefs enabled and collected

I made a few changes to v4 of this patch to support pipe semantics similar to how /sys/kernel/tracing/trace_pipe works. You can now get unlimited trace depth by piping the trace buffer to disk as follows:

cat /sys/kernel/debug/ttyS1/trace_pipe > trace.txt &

The 'cat' process will run in the background and it will terminate when tracing is disabled. This doesn't come for free, however. The wake_up() call adds a fair amount of overhead when a pipe reader is present as you can see in the measurements above.

#2 lore raw
Quoted 31 lines · Dan Raymond

I took Andy Shevchenko's suggestion and added port I/O tracing in a similar manner as the memory-mapped I/O tracing enabled with CONFIG_TRACE_MMIO_ACCESS. I'll be submitting another patch for that shortly. It works well and it provides another way to trace UART register accesses. For most purposes it is probably superior to the method used in this patch because it leverages the existing kernel tracing framework.

However, this patch might still be useful for debugging issues that are sensitive to timing because it uses less CPU resources. It also uses less memory so a larger trace can be collected. I did some more timing analysis and tried to minimize the impact of cache misses by reading the MSR register 100 times in a loop. I added this test to serial8250_do_get_mctrl() and ran 'cat /proc/tty/driver/serial' to exercise the test. Here are the results (trace overhead is measured in CPU cycles at 1GHz):

overhead   method
--------   ------------------------------------------
       0   serial_debugfs disabled
     158   serial_debugfs enabled without pipe reader
     367   serial_debugfs enabled with pipe reader
       2   portio_tracefs disabled
     461   portio_tracefs enabled but filtered
     713   portio_tracefs enabled and collected

I made a few changes to v4 of this patch to support pipe semantics similar to how /sys/kernel/tracing/trace_pipe works. You can now get unlimited trace depth by piping the trace buffer to disk as follows:

Quoted 6 lines

cat /sys/kernel/debug/ttyS1/trace_pipe > trace.txt &

The 'cat' process will run in the background and it will terminate when tracing is disabled. This doesn't come for free, however. The wake_up() call adds a fair amount of overhead when a pipe reader is present as you can see in the measurements above.

Thanks!

In my opinion this work is good to have published, but still doesn't warrant to be part of the Linux kernel with the other method being worked.

Let's hear others' opinions, though...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw
In reply to Dan Raymond
Quoted 6 lines · Dan Raymond

Implement a UART register tracing facility using the debugfs. This can be used as a "serial port sniffer" to monitor UART traffic and line settings with timestamps at microsecond granularity. This can be useful for general serial port debugging or to debug the UART driver itself.

Again, please stick with the tracing infrastructure and use that, like all other kernel subsystems do. If each driver/subsystem were to create a tracing interface like this, it would be unmanageable. So unless there is some really really good reason to not use the trace infrastructure, this patch can not be accepted.

thanks,

greg k-h