Skip to contentSkip to conversation

tty/serial: create debugfs interface for UART register tracing

PATCH Superseded Dan Raymond 3 participants 9 messages Aug 24, 2023 – Sep 13, 2023

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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'

 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 | 530 +++++++++++++++++++++++++++
 drivers/tty/serial/8250/Kconfig      |   9 +
 drivers/tty/serial/8250/Makefile     |   1 +
 include/linux/serial_core.h          |   1 +
 8 files changed, 557 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 +530 -0

530 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
maintainer
#1 lore raw
Quoted 22 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.

---
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

Quoted 3 lines

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'

Better! But please, look into using the tracing infrastructure and functionality first. Only if that is somehow not workable at all should we regress into using stuff like debugfs for this.

thanks,

greg k-h

#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.

 drivers/tty/serial/8250/8250_debug.c | 530 +++++++++++++++++++++++++++

My gosh. Why trace points and trace events can't be used for that?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 4 lines · Andy Shevchenko

Warning: This email is from an unusual correspondent.
Warning: Make sure this is someone you trust.

On Thu, Aug 24, 2023 at 02:59:01PM -0600, Dan Raymond wrote:

Quoted 9 lines
Quoted 6 lines

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.

 drivers/tty/serial/8250/8250_debug.c | 530 +++++++++++++++++++++++++++

My gosh. Why trace points and trace events can't be used for that?

I don't understand your post. Are you saying there is already a way to trace UART register reads/writes without this patch? Can you elaborate?

maintainer
#5 lore raw
Quoted 10 lines · Dan Raymond
Quoted 9 lines · Andy Shevchenko

Warning: This email is from an unusual correspondent.
Warning: Make sure this is someone you trust.

Quoted 4 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.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line
 drivers/tty/serial/8250/8250_debug.c | 530 +++++++++++++++++++++++++++

My gosh. Why trace points and trace events can't be used for that?

I don't understand your post. Are you saying there is already a way to trace UART register reads/writes without this patch? Can you elaborate?

By adding trace points and trace events, instead of a custom api like you did. I asked you to look into this already :)

thanks,

greg k-h

#6 lore raw
In reply to Dan Raymond
Quoted 10 lines · Dan Raymond
Quoted 9 lines · Andy Shevchenko

Warning: This email is from an unusual correspondent.
Warning: Make sure this is someone you trust.

Quoted 4 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.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line
 drivers/tty/serial/8250/8250_debug.c | 530 +++++++++++++++++++++++++++

My gosh. Why trace points and trace events can't be used for that?

I don't understand your post. Are you saying there is already a way to trace UART register reads/writes without this patch? Can you elaborate?

Besides what Greg an I were telling you (another type of infrastructure), as a matter of fact you may trace any IO on some architectures (at least x86), it's called mmiotracer (I have used it like 5 years ago or so to trace UART).

Below is the excerpt from my old shell script

rebind() {
	local drvdir="/sys/bus/$1/drivers/$2"
	local devdir="/sys/bus/$1/devices/$3"
	[ -d "$drvdir" -a -d "$devdir" ] || return
	echo "$3" > "$drvdir/unbind"
	echo "$3" > "$drvdir/bind"
}
mmiotrace() {
	echo mmiotrace > /sys/kernel/tracing/current_tracer
	echo 1 > /sys/kernel/tracing/tracing_on

	rebind platform 'dw-apb-uart'   '80860F0A:00'   # BYT ttyS1 (ACPI)
	rebind platform 'dw-apb-uart'   '8086228A:00'   # BSW ttyS1 (ACPI)
	rebind pci      '8250_mid'      '0000:00:04.2'  # Edison ttyS1
	rebind pci      'intel-lpss'    '0000:00:18.1'  # BXT LH ttyS1
	rebind pci      'intel-lpss'    '0000:00:1e.0'  # SKL ttyS1
	rebind pci      '8250_lpss'     '0000:00:1e.3'  # BSW ttyS1 (PCI)
}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
In reply to Dan Raymond
Quoted 38 lines · Dan Raymond
Quoted 26 lines
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

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.

 drivers/tty/serial/8250/8250_debug.c | 530 +++++++++++++++++++++++++++

My gosh. Why trace points and trace events can't be used for that?

I don't understand your post. Are you saying there is already a way to trace UART register reads/writes without this patch? Can you elaborate?

Besides what Greg an I were telling you (another type of infrastructure), as a matter of fact you may trace any IO on some architectures (at least x86), it's called mmiotracer (I have used it like 5 years ago or so to trace UART).

Below is the excerpt from my old shell script

rebind() {
	local drvdir="/sys/bus/$1/drivers/$2"
	local devdir="/sys/bus/$1/devices/$3"
	[ -d "$drvdir" -a -d "$devdir" ] || return
	echo "$3" > "$drvdir/unbind"
	echo "$3" > "$drvdir/bind"
}
Quoted 11 lines
mmiotrace() {
	echo mmiotrace > /sys/kernel/tracing/current_tracer
	echo 1 > /sys/kernel/tracing/tracing_on

	rebind platform 'dw-apb-uart'   '80860F0A:00'   # BYT ttyS1 (ACPI)
	rebind platform 'dw-apb-uart'   '8086228A:00'   # BSW ttyS1 (ACPI)
	rebind pci      '8250_mid'      '0000:00:04.2'  # Edison ttyS1
	rebind pci      'intel-lpss'    '0000:00:18.1'  # BXT LH ttyS1
	rebind pci      'intel-lpss'    '0000:00:1e.0'  # SKL ttyS1
	rebind pci      '8250_lpss'     '0000:00:1e.3'  # BSW ttyS1 (PCI)
}
Quoted 4 lines

Correct me if I'm wrong but I don't think mmiotrace will work for me. My environment comprises an Intel Celeron M connected to an 852GM north bridge, an ICH4 south bridge, and a SCH3114 Super I/O controller. My UART device is ttyS1 and it uses port I/O not memory-mapped I/O.

Correct, mmiotrace (as suggested by its name) works only on MMIO. For IO there is no such mechanism as there are no exceptions when you access IO ports.

So, in such case the only instrumentation can help, but I suggest to do it generally as you may miss the writes or reads that are not going via serial_*() APIs.

Look how lib/trace_readwrite.c is done.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
In reply to Andy Shevchenko
Quoted 26 lines
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines

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.

 drivers/tty/serial/8250/8250_debug.c | 530 +++++++++++++++++++++++++++

My gosh. Why trace points and trace events can't be used for that?

I don't understand your post. Are you saying there is already a way to trace UART register reads/writes without this patch? Can you elaborate?

Besides what Greg an I were telling you (another type of infrastructure), as a matter of fact you may trace any IO on some architectures (at least x86), it's called mmiotracer (I have used it like 5 years ago or so to trace UART).

Below is the excerpt from my old shell script

rebind() {
	local drvdir="/sys/bus/$1/drivers/$2"
	local devdir="/sys/bus/$1/devices/$3"
	[ -d "$drvdir" -a -d "$devdir" ] || return
	echo "$3" > "$drvdir/unbind"
	echo "$3" > "$drvdir/bind"
}
Quoted 11 lines
mmiotrace() {
	echo mmiotrace > /sys/kernel/tracing/current_tracer
	echo 1 > /sys/kernel/tracing/tracing_on

	rebind platform 'dw-apb-uart'   '80860F0A:00'   # BYT ttyS1 (ACPI)
	rebind platform 'dw-apb-uart'   '8086228A:00'   # BSW ttyS1 (ACPI)
	rebind pci      '8250_mid'      '0000:00:04.2'  # Edison ttyS1
	rebind pci      'intel-lpss'    '0000:00:18.1'  # BXT LH ttyS1
	rebind pci      'intel-lpss'    '0000:00:1e.0'  # SKL ttyS1
	rebind pci      '8250_lpss'     '0000:00:1e.3'  # BSW ttyS1 (PCI)
}

Correct me if I'm wrong but I don't think mmiotrace will work for me. My environment comprises an Intel Celeron M connected to an 852GM north bridge, an ICH4 south bridge, and a SCH3114 Super I/O controller. My UART device is ttyS1 and it uses port I/O not memory-mapped I/O.

# find /sys/devices -name '*ttyS*'
/sys/devices/platform/serial8250/tty/ttyS2
/sys/devices/platform/serial8250/tty/ttyS3
/sys/devices/pnp0/00:03/tty/ttyS0
/sys/devices/pnp0/00:04/tty/ttyS1
# ls -l /sys/bus/pnp/drivers/serial
total 0
lrwxrwxrwx    1 root     root             0 Sep 11 14:19 00:03 -> ../../../../devices/pnp0/00:03
lrwxrwxrwx    1 root     root             0 Sep 11 14:19 00:04 -> ../../../../devices/pnp0/00:04
--w-------    1 root     root          4096 Sep 11 14:19 bind
--w-------    1 root     root          4096 Sep 11 14:19 uevent
--w-------    1 root     root          4096 Sep 11 14:19 unbind
# mount -t tracefs tracefs /sys/kernel/tracing
# echo mmiotrace > /sys/kernel/tracing/current_tracer
# echo 00:04 > /sys/bus/pnp/drivers/serial/unbind
# echo 00:04 > /sys/bus/pnp/drivers/serial/bind
# dmesg | tail -5
[   36.438305] random: crng init done
[   97.736362] mmiotrace: enabled.
[  113.964953] serial 00:04: disabled
[  117.469704] serial 00:04: activated
[  117.469986] 00:04: ttyS1 at I/O 0x2f8 (irq = 3, base_baud = 115200) is a 16550A

(perform UART transaction)

# cat /sys/kernel/tracing/trace
# tracer: mmiotrace
#
# entries-in-buffer/entries-written: 0/0   #P:1
#
#                                _-----=> irqs-off/BH-disabled
#                               / _----=> need-resched
#                              | / _---=> hardirq/softirq
#                              || / _--=> preempt-depth
#                              ||| / _-=> migrate-disable
#                              |||| /     delay
#           TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
#              | |         |   |||||     |         |
#2 lore raw
In reply to Greg KH
Quoted 7 lines · Greg KH

Better! But please, look into using the tracing infrastructure and functionality first. Only if that is somehow not workable at all should we regress into using stuff like debugfs for this.

thanks,

greg k-h

I took some timing measurements to compare the overhead using different tracing techniques. The measurements were taken by executing the rdtsc instruction at the entry and exit of the serial_in() and serial_out() functions in 'struct uart_port'. My test environment is a Celeron M clocked at 1GHz so each clock cycle is 1 nanosecond. Also note that the rdtsc instruction takes 43 clock cycles so that should be subtracted from each measurement to get the actual time. For each of the numbers below 100 measurements were taken and averaged.

1183 cycles using io_serial_in (no tracing)
1192 cycles using io_serial_out (no tracing)
1382 cycles using serial_in_wrapper and rdtsc (this patch) 1564 cycles using serial_out_wrapper and rdtsc (this patch) 1484 cycles using serial_in_wrapper and ktime_get_boottime 1980 cycles using serial_out_wrapper and ktime_get_boottime 2484 cycles using serial_in_wrapper and trace_portio_read 4411 cycles using serial_out_wrapper and trace_portio_write

The last two measurements used the existing kernel tracing infrastructure. TRACE_EVENT() macros were created and tracepoints were used to generate trace events that could be collected from /sys/kernel/tracing/trace.

My first observation is that the I/O instructions themselves (inb/outb) are fairly slow on this architecture so even with no tracing each operation takes around 1200 nanoseconds.

The tracing technique used in this patch adds between 200 and 350 nanoseconds of overhead to that. Swapping ktime_get_boottime() for rdtsc() adds another 100 to 200 nanoseconds. Using the existing kernel trace infrastructure incurs 1300 to 3200 nanoseconds of overhead.

Memory usage was difficult to determine exactly. The technique in this patch uses fixed length records that are 8 bytes each (6 bytes for the timestamp, 1 byte for the register offset, and 1 byte for the register data). But the memory used by the kernel trace infrastructure varied depending on how many records were collected. When I set the buffer size to 64 kilobytes (by writing 64 to /sys/kernel/tracing/buffer_size_kb) I was able to collect 4062 records. That averages to a little over 16 bytes per record.