Skip to contentSkip to conversation

kernel/irq/proc: use seq_put_decimal_ull_width() for decimal values

PATCH Applied David Wang 4 participants 21 messages Nov 8, 2024 – Dec 3, 2024 IRQ

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

David Wang

author
#0 lore raw

PATCH 1/13 kernel/irq/proc: use seq_put_decimal_ull_width() for decimal values

seq_printf() is costy, on a system with m interrupts and n CPUs, there would be m*n decimal values yield via seq_printf() when reading /proc/interrupts, the cost parsing format strings grows with number of CPU. Profiling on a x86 8-core system indicates seq_printf() takes ~47% samples of show_interrupts(), and replace seq_printf() with seq_put_decimal_ull_width() could have near 30% performance gain.

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

---
 kernel/irq/proc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/irq/proc.c +4 -2
index 9081ada81c3d..988ce781e813 100644
@@ -494,9 +494,11 @@ int show_interrupts(struct seq_file *p, void *v)
 	if (!desc->action || irq_desc_is_chained(desc) || !desc->kstat_irqs)
 		goto outsparse;
 
-	seq_printf(p, "%*d: ", prec, i);
+	seq_printf(p, "%*d:", prec, i);
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0);
+		seq_put_decimal_ull_width(p, " ",
+					  desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0,
+					  10);
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {
Signature · 1 line
2.39.2
merge bot Applied
#1 lore raw

[tip: irq/core] genirq/proc: Use seq_put_decimal_ull_width() for decimal values

The following commit has been merged into the irq/core branch of tip:

The following commit has been merged into the irq/core branch of tip:

Commit-ID:     f9ed1f7c2e26fcd19781774e310a6236d7525c11
Gitweb:        https://git.kernel.org/tip/f9ed1f7c2e26fcd19781774e310a6236d7525c11
Author:        David Wang <[email protected]>
AuthorDate:    Sat, 09 Nov 2024 00:07:17 +08:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 13 Nov 2024 17:36:35 +01:00

genirq/proc: Use seq_put_decimal_ull_width() for decimal values

seq_printf() is more expensive than seq_put_decimal_ull_width() due to the format string parsing costs.

Profiling on a x86 8-core system indicates seq_printf() takes ~47% samples of show_interrupts(). Replacing it with seq_put_decimal_ull_width() yields almost 30% performance gain.

[ tglx: Massaged changelog and fixed up coding style ]

---
 kernel/irq/proc.c |  9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
kernel/irq/proc.c +6 -3
index d226282..f36c33b 100644
@@ -495,9 +495,12 @@ int show_interrupts(struct seq_file *p, void *v)
 	if (!desc->action || irq_desc_is_chained(desc) || !desc->kstat_irqs)
 		goto outsparse;
 
-	seq_printf(p, "%*d: ", prec, i);
-	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0);
+	seq_printf(p, "%*d:", prec, i);
+	for_each_online_cpu(j) {
+		unsigned int cnt = desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0;
+
+		seq_put_decimal_ull_width(p, " ", cnt, 10);
+	}
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {
#2 lore raw
In reply to David Wang
Quoted 2 lines · David Wang

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

I've applied this, but ...

looking at a 256 CPU machine. /proc/interrupts provides data for 560 interrupts, which amounts to ~1.6MB data size.

There are 560 * 256 = 143360 interrupt count fields. 140615 of these fields are zero, which means 140615 * 11 bytes. That's 96% of the overall data size. The actually useful information is less than 50KB if properly condensed.

I'm really amused that people spend a lot of time to improve the performance of /proc/interrupts instead of actually sitting down and implementing a proper new interface for this purpose, which would make both the kernel and the tools faster by probably several orders of magnitude.

Thanks,

tglx

author
#3 lore raw

Hi,

Quoted 14 lines · Thomas Gleixner
Quoted 2 lines · David Wang

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

I've applied this, but ...

looking at a 256 CPU machine. /proc/interrupts provides data for 560 interrupts, which amounts to ~1.6MB data size.

There are 560 * 256 = 143360 interrupt count fields. 140615 of these fields are zero, which means 140615 * 11 bytes. That's 96% of the overall data size. The actually useful information is less than 50KB if properly condensed.

Quoted 5 lines

I'm really amused that people spend a lot of time to improve the performance of /proc/interrupts instead of actually sitting down and implementing a proper new interface for this purpose, which would make both the kernel and the tools faster by probably several orders of magnitude.

That's a great idea~.
I tried to make changes and verify the performance, the result is good, only that kernel side improvement is not that big, but still significant.

Here is what I did, (draft codes are at the end):
Created two new /proc entry for comparision:
1. /proc/interruptsp, non-zero value only, arch-independent irqs without description

	$ cat /proc/interruptsp 
	IRQ CPU counter # positive only
	0 0 40
	38 5 23
	39 0 81
	40 1 6
	41 2 111
	...
	$ cat /proc/interruptsp  | wc
	     18      57     181
	$ strace -e read -T cat /proc/interruptsp > /dev/null
	...
	read(3, "IRQ CPU counter # positive only\n"..., 131072) = 181 <0.000144>
	read(3, "", 131072)                     = 0 <0.000009>
	$ time ./interruptsp  # 1 million rounds of open/read(all)/close;
	real	1m54.727s
	user	0m0.368s
	sys	1m54.309s

2. /proc/interruptso, same with old format, except arch-dependent irqs are removed

	$ cat /proc/interruptso | wc
	     32     388    4439
	$ strace -e read -T cat /proc/interruptso > /dev/null
	...
	read(3, "            CPU0       CPU1     "..., 131072) = 4005 <0.000071>
	read(3, "  88:          0          0     "..., 131072) = 434 <0.000111>
	read(3, "", 131072)                     = 0 <0.000009>
        $ time ./interruptso # 1 million rounds of open/read(all)/close;
	real	2m19.284s
	user	0m0.400s
	sys	2m18.756s

The size is indeed tens of times shorter, and would have huge improvement for those applications parsing the whole content; But as for kernel side improvement, strace and stress test indicates the improvement is not that huge, well, but still significant ~40%.

The bottleneck seems to be mtree_load called by irq_to_desc, based on a simple profiling (not sure whether this is expected or not):

	show_interruptsp(74.724% 845541/1131554)
	    mtree_load(56.596% 478544/845541)
	    __rcu_read_unlock(5.914% 50004/845541)
	    __rcu_read_lock(3.056% 25840/845541)
	    irq_to_desc(2.151% 18184/845541)
	    seq_put_decimal_ull_width(1.211% 10243/845541)
		...

I think the improvement worth pursuing. Maybe a new interface for "active" interrupts, say /proc/activeinterrupts?, and the old /proc/interrupts can serve as a table for available ids/cpus/descriptions.

Do you plan to work on this? If not, I can take time on it.

Draft codes:

	int show_interruptsp(struct seq_file *p, void *v)
	{

		int i = *(loff_t *) v, j;
		struct irq_desc *desc;

		if (i >= ACTUAL_NR_IRQS)
			return 0;

		/* print header and calculate the width of the first column */
		if (i == 0) {
			seq_puts(p, "IRQ CPU counter # positive only\n");
		}

		rcu_read_lock();
		desc = irq_to_desc(i);
		if (!desc || irq_settings_is_hidden(desc))
			goto outsparse;

		if (!desc->action || irq_desc_is_chained(desc) || !desc->kstat_irqs)
			goto outsparse;

		for_each_online_cpu(j) {
			unsigned int cnt = desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0;
			if (cnt > 0) {
				seq_put_decimal_ull(p, "", i);
				seq_put_decimal_ull(p, " ", j);
				seq_put_decimal_ull(p, " ", cnt);
				seq_putc(p, '\n');
			}
		}

	outsparse:
		rcu_read_unlock();
		return 0;
	}

	int show_interruptso(struct seq_file *p, void *v)
	{
		static int prec;

		int i = *(loff_t *) v, j;
		struct irqaction *action;
		struct irq_desc *desc;
		unsigned long flags;

		if (i >= ACTUAL_NR_IRQS) <<---return when arch-independent irqs are done.
			return 0;   
		...

Thanks~
David

#4 lore raw
In reply to David Wang

Hi David,

Quoted 11 lines · David Wang

seq_printf() is costy, on a system with m interrupts and n CPUs, there would be m*n decimal values yield via seq_printf() when reading /proc/interrupts, the cost parsing format strings grows with number of CPU. Profiling on a x86 8-core system indicates seq_printf() takes ~47% samples of show_interrupts(), and replace seq_printf() with seq_put_decimal_ull_width() could have near 30% performance gain.

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

Thanks for your patch, which is now commit f9ed1f7c2e26fcd1 ("genirq/proc: Use seq_put_decimal_ull_width() for decimal values") in irqchip/irq/core.

This removes a space after the last CPU column, causing the values in this column to be concatenated to the values in the next column.

E.g. on Koelsch (R-Car M-W), the output changes from:

	       CPU0       CPU1
     27:       1871       2017 GIC-0  27 Level     arch_timer
     29:        646          0 GIC-0 205 Level     e60b0000.i2c
     30:          0          0 GIC-0 174 Level     ffca0000.timer
     31:          0          0 GIC-0  36 Level     e6050000.gpio
     32:          0          0 GIC-0  37 Level     e6051000.gpio
     [...]

to

	       CPU0       CPU1
     27:       1966       1900GIC-0  27 Level     arch_timer
     29:        580          0GIC-0 205 Level     e60b0000.i2c
     30:          0          0GIC-0 174 Level     ffca0000.timer
     31:          0          0GIC-0  36 Level     e6050000.gpio
     32:          0          0GIC-0  37 Level     e6051000.gpio
     [...]

making the output hard to read, and probably breaking scripts that parse its contents.

Reverting the commit fixes the issue for me.

Quoted 6 lines
kernel/irq/proc.c +0 -0
@@ -494,9 +494,11 @@ int show_interrupts(struct seq_file *p, void *v)
 	if (!desc->action || irq_desc_is_chained(desc) || !desc->kstat_irqs)
 		goto outsparse;
 

kernel/irq/proc.c int show_interrupts(struct seq_file *p, void *v) · line 504

Quoted 10 lines
kernel/irq/proc.c +4 -2
-	seq_printf(p, "%*d: ", prec, i);
+	seq_printf(p, "%*d:", prec, i);
 	for_each_online_cpu(j)
-		seq_printf(p, "%10u ", desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0);
+		seq_put_decimal_ull_width(p, " ",
+					  desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, j) : 0,
+					  10);
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

#5 lore raw
Quoted 20 lines · Geert Uytterhoeven

E.g. on Koelsch (R-Car M-W), the output changes from:

CPU0 CPU1

      27:       1871       2017 GIC-0  27 Level     arch_timer
      29:        646          0 GIC-0 205 Level     e60b0000.i2c
      30:          0          0 GIC-0 174 Level     ffca0000.timer
      31:          0          0 GIC-0  36 Level     e6050000.gpio
      32:          0          0 GIC-0  37 Level     e6051000.gpio
      [...]

to

CPU0 CPU1

      27:       1966       1900GIC-0  27 Level     arch_timer
      29:        580          0GIC-0 205 Level     e60b0000.i2c
      30:          0          0GIC-0 174 Level     ffca0000.timer
      31:          0          0GIC-0  36 Level     e6050000.gpio
      32:          0          0GIC-0  37 Level     e6051000.gpio
      [...]
Quoted 4 lines

making the output hard to read, and probably breaking scripts that parse its contents.

Reverting the commit fixes the issue for me.

Interestingly enough the generic version and quite some of the chip specific print functions have a leading space, but GIC does not.

The below should restore the original state.

Thanks,

tglx
---

kernel/irq/proc.c +1 -0
index f36c33bd2da4..9b715ce8cf2e 100644
@@ -501,6 +501,7 @@ int show_interrupts(struct seq_file *p, void *v)
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {
author Acked-by
#6 lore raw
Quoted 40 lines · Thomas Gleixner
Quoted 20 lines · Geert Uytterhoeven

E.g. on Koelsch (R-Car M-W), the output changes from:

CPU0 CPU1

      27:       1871       2017 GIC-0  27 Level     arch_timer
      29:        646          0 GIC-0 205 Level     e60b0000.i2c
      30:          0          0 GIC-0 174 Level     ffca0000.timer
      31:          0          0 GIC-0  36 Level     e6050000.gpio
      32:          0          0 GIC-0  37 Level     e6051000.gpio
      [...]

to

CPU0 CPU1

      27:       1966       1900GIC-0  27 Level     arch_timer
      29:        580          0GIC-0 205 Level     e60b0000.i2c
      30:          0          0GIC-0 174 Level     ffca0000.timer
      31:          0          0GIC-0  36 Level     e6050000.gpio
      32:          0          0GIC-0  37 Level     e6051000.gpio
      [...]
Quoted 4 lines

making the output hard to read, and probably breaking scripts that parse its contents.

Reverting the commit fixes the issue for me.

Interestingly enough the generic version and quite some of the chip specific print functions have a leading space, but GIC does not.

The below should restore the original state.

Thanks,

tglx
---

kernel/irq/proc.c +0 -0
index f36c33bd2da4..9b715ce8cf2e 100644

kernel/irq/proc.c int show_interrupts(struct seq_file *p, void *v) · line 507

Quoted 7 lines
kernel/irq/proc.c +1 -0
@@ -501,6 +501,7 @@ int show_interrupts(struct seq_file *p, void *v)
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {

LGTM.

Thanks
David

author
#7 lore raw
In reply to Thomas Gleixner
Quoted 40 lines · Thomas Gleixner
Quoted 20 lines · Geert Uytterhoeven

E.g. on Koelsch (R-Car M-W), the output changes from:

CPU0 CPU1

      27:       1871       2017 GIC-0  27 Level     arch_timer
      29:        646          0 GIC-0 205 Level     e60b0000.i2c
      30:          0          0 GIC-0 174 Level     ffca0000.timer
      31:          0          0 GIC-0  36 Level     e6050000.gpio
      32:          0          0 GIC-0  37 Level     e6051000.gpio
      [...]

to

CPU0 CPU1

      27:       1966       1900GIC-0  27 Level     arch_timer
      29:        580          0GIC-0 205 Level     e60b0000.i2c
      30:          0          0GIC-0 174 Level     ffca0000.timer
      31:          0          0GIC-0  36 Level     e6050000.gpio
      32:          0          0GIC-0  37 Level     e6051000.gpio
      [...]
Quoted 4 lines

making the output hard to read, and probably breaking scripts that parse its contents.

Reverting the commit fixes the issue for me.

Interestingly enough the generic version and quite some of the chip specific print functions have a leading space, but GIC does not.

The below should restore the original state.

Thanks,

tglx
---

kernel/irq/proc.c +0 -0
index f36c33bd2da4..9b715ce8cf2e 100644

kernel/irq/proc.c int show_interrupts(struct seq_file *p, void *v) · line 507

Quoted 7 lines
kernel/irq/proc.c +1 -0
@@ -501,6 +501,7 @@ int show_interrupts(struct seq_file *p, void *v)
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {

On second thought, considering other paths have already had a leading space,
maybe it is more clean to just add a leading space before irq_print_chip:

        raw_spin_lock_irqsave(&desc->lock, flags);
        if (desc->irq_data.chip) {
-               if (desc->irq_data.chip->irq_print_chip)
+               if (desc->irq_data.chip->irq_print_chip) {
+                       seq_putc(p, ' ');
                        desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
-               else if (desc->irq_data.chip->name)
+               } else if (desc->irq_data.chip->name)
                        seq_printf(p, " %8s", desc->irq_data.chip->name);
                else
                        seq_printf(p, " %8s", "-");

David

#8 lore raw
Quoted 19 lines · David Wang

kernel/irq/proc.c int show_interrupts(struct seq_file *p, void *v) · line 507

Quoted 12 lines · Thomas Gleixner
kernel/irq/proc.c +1 -0
index f36c33bd2da4..9b715ce8cf2e 100644
@@ -501,6 +501,7 @@ int show_interrupts(struct seq_file *p, void *v)
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {

On second thought, considering other paths have already had a leading space,
maybe it is more clean to just add a leading space before irq_print_chip:

        raw_spin_lock_irqsave(&desc->lock, flags);
        if (desc->irq_data.chip) {

Review

Quoted 9 lines
(quoted diff) +3 -2
-               if (desc->irq_data.chip->irq_print_chip)
+               if (desc->irq_data.chip->irq_print_chip) {
+                       seq_putc(p, ' ');
                        desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
-               else if (desc->irq_data.chip->name)
+               } else if (desc->irq_data.chip->name)
                        seq_printf(p, " %8s", desc->irq_data.chip->name);
                else
                        seq_printf(p, " %8s", "-");

I rather keep the seq_putc() and remove the trailing space from the other prints.

author
#9 lore raw
Quoted 30 lines · Thomas Gleixner
Quoted 19 lines · David Wang

kernel/irq/proc.c int show_interrupts(struct seq_file *p, void *v) · line 507

Quoted 12 lines · Thomas Gleixner
kernel/irq/proc.c +1 -0
index f36c33bd2da4..9b715ce8cf2e 100644
@@ -501,6 +501,7 @@ int show_interrupts(struct seq_file *p, void *v)
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {

On second thought, considering other paths have already had a leading space,
maybe it is more clean to just add a leading space before irq_print_chip:

        raw_spin_lock_irqsave(&desc->lock, flags);
        if (desc->irq_data.chip) {

Review

Quoted 9 lines
(quoted diff) +3 -2
-               if (desc->irq_data.chip->irq_print_chip)
+               if (desc->irq_data.chip->irq_print_chip) {
+                       seq_putc(p, ' ');
                        desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
-               else if (desc->irq_data.chip->name)
+               } else if (desc->irq_data.chip->name)
                        seq_printf(p, " %8s", desc->irq_data.chip->name);
                else
                        seq_printf(p, " %8s", "-");
Quoted 2 lines

I rather keep the seq_putc() and remove the trailing space from the other prints.

Agree, code would align better this way and separating spaces will be consistent.

David

#10 lore raw
In reply to Thomas Gleixner

Hi Thomas,

Quoted 34 lines · Thomas Gleixner
Quoted 20 lines · Geert Uytterhoeven

E.g. on Koelsch (R-Car M-W), the output changes from:

             CPU0       CPU1
      27:       1871       2017 GIC-0  27 Level     arch_timer
      29:        646          0 GIC-0 205 Level     e60b0000.i2c
      30:          0          0 GIC-0 174 Level     ffca0000.timer
      31:          0          0 GIC-0  36 Level     e6050000.gpio
      32:          0          0 GIC-0  37 Level     e6051000.gpio
      [...]

to

             CPU0       CPU1
      27:       1966       1900GIC-0  27 Level     arch_timer
      29:        580          0GIC-0 205 Level     e60b0000.i2c
      30:          0          0GIC-0 174 Level     ffca0000.timer
      31:          0          0GIC-0  36 Level     e6050000.gpio
      32:          0          0GIC-0  37 Level     e6051000.gpio
      [...]
Quoted 4 lines

making the output hard to read, and probably breaking scripts that parse its contents.

Reverting the commit fixes the issue for me.

Interestingly enough the generic version and quite some of the chip specific print functions have a leading space, but GIC does not.

The below should restore the original state.

kernel/irq/proc.c int show_interrupts(struct seq_file *p, void *v) · line 507

Quoted 7 lines
kernel/irq/proc.c +1 -0
@@ -501,6 +501,7 @@ int show_interrupts(struct seq_file *p, void *v)
 
                seq_put_decimal_ull_width(p, " ", cnt, 10);
        }
+       seq_putc(p, ' ');
 
        raw_spin_lock_irqsave(&desc->lock, flags);
        if (desc->irq_data.chip) {

Thanks, that does the trick!

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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
#11 lore raw

PATCH 1/1 genirq/proc: Add missing space separator back

The recent conversion of show_interrupts() to seq_put_decimal_ull_width() caused a formatting regression as it drops a previosuly existing space separator.

Add it back by unconditionally inserting a space after the interrupt counts and removing the extra leading space from the chip name prints.

Fixes f9ed1f7c2e26 ("genirq/proc: Use seq_put_decimal_ull_width() for decimal values")
---
 kernel/irq/proc.c |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/irq/proc.c +4 -3
@@ -501,17 +501,18 @@ int show_interrupts(struct seq_file *p,
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {
 		if (desc->irq_data.chip->irq_print_chip)
 			desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
 		else if (desc->irq_data.chip->name)
-			seq_printf(p, " %8s", desc->irq_data.chip->name);
+			seq_printf(p, "%8s", desc->irq_data.chip->name);
 		else
-			seq_printf(p, " %8s", "-");
+			seq_printf(p, "%8s", "-");
 	} else {
-		seq_printf(p, " %8s", "None");
+		seq_printf(p, "%8s", "None");
 	}
 	if (desc->irq_data.domain)
 		seq_printf(p, " %*lu", prec, desc->irq_data.hwirq);
author Reviewed-by on v1 1/1
#12 lore raw

Re:[PATCH] genirq/proc: Add missing space separator back

Quoted 30 lines · Thomas Gleixner

The recent conversion of show_interrupts() to seq_put_decimal_ull_width() caused a formatting regression as it drops a previosuly existing space separator.

Add it back by unconditionally inserting a space after the interrupt counts and removing the extra leading space from the chip name prints.

Fixes f9ed1f7c2e26 ("genirq/proc: Use seq_put_decimal_ull_width() for decimal values")

---
kernel/irq/proc.c | 7 ++++---

1 file changed, 4 insertions(+), 3 deletions(-)
kernel/irq/proc.c +1 -1
@@ -501,17 +501,18 @@ int show_interrupts(struct seq_file *p,
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {
 		if (desc->irq_data.chip->irq_print_chip)
 			desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
 		else if (desc->irq_data.chip->name)
-			seq_printf(p, " %8s", desc->irq_data.chip->name);

kernel/irq/proc.c int show_interrupts(struct seq_file *p, · line 518

Quoted 10 lines
kernel/irq/proc.c +3 -2
+			seq_printf(p, "%8s", desc->irq_data.chip->name);
 		else
-			seq_printf(p, " %8s", "-");
+			seq_printf(p, "%8s", "-");
 	} else {
-		seq_printf(p, " %8s", "None");
+		seq_printf(p, "%8s", "None");
 	}
 	if (desc->irq_data.domain)
 		seq_printf(p, " %*lu", prec, desc->irq_data.hwirq);

And again, sorry for the regression...

Thanks
David

#13 lore raw

Re: [PATCH] genirq/proc: Add missing space separator back

In reply to Thomas Gleixner

Hi Thomas,

Quoted 4 lines · Thomas Gleixner

The recent conversion of show_interrupts() to seq_put_decimal_ull_width() caused a formatting regression as it drops a previosuly existing space separator.

Quoted 8 lines

Add it back by unconditionally inserting a space after the interrupt counts and removing the extra leading space from the chip name prints.

Fixes f9ed1f7c2e26 ("genirq/proc: Use seq_put_decimal_ull_width() for decimal values")

Thanks for your patch!

kernel/irq/proc.c int show_interrupts(struct seq_file *p, · line 509

Quoted 12 lines
kernel/irq/proc.c +1 -0
@@ -501,17 +501,18 @@ int show_interrupts(struct seq_file *p,
 
                seq_put_decimal_ull_width(p, " ", cnt, 10);
        }
+       seq_putc(p, ' ');
 
        raw_spin_lock_irqsave(&desc->lock, flags);
        if (desc->irq_data.chip) {
                if (desc->irq_data.chip->irq_print_chip)
                        desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);

You should also remove the leading space from the few .irq_print_chip() callbacks that print such a space. According to

git grep -lw irq_print_chip | xargs git grep -W "seq_.*\" "

and filterng out the false positives:

    arch/powerpc/sysdev/fsl_msi.c:  seq_printf(p, " fsl-msi-%d", cascade_virq);
    drivers/bus/moxtet.c:   seq_printf(p, " moxtet-%s.%i#%i",
mox_module_name(id), pos->idx,
    drivers/irqchip/irq-partition-percpu.c: seq_printf(p, " %5s-%lu",
chip->name, data->hwirq);
    drivers/soc/qcom/smp2p.c:       seq_printf(p, " %8s",

dev_name(entry->smp2p->dev));

kernel/irq/proc.c int show_interrupts(struct seq_file *p, · line 518

Quoted 12 lines
kernel/irq/proc.c +3 -3
                else if (desc->irq_data.chip->name)
-                       seq_printf(p, " %8s", desc->irq_data.chip->name);
+                       seq_printf(p, "%8s", desc->irq_data.chip->name);
                else
-                       seq_printf(p, " %8s", "-");
+                       seq_printf(p, "%8s", "-");
        } else {
-               seq_printf(p, " %8s", "None");
+               seq_printf(p, "%8s", "None");
        }
        if (desc->irq_data.domain)
                seq_printf(p, " %*lu", prec, desc->irq_data.hwirq);

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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
#14 lore raw

Re: [PATCH] genirq/proc: Add missing space separator back

Quoted 8 lines · Geert Uytterhoeven

You should also remove the leading space from the few .irq_print_chip() callbacks that print such a space. According to

git grep -lw irq_print_chip | xargs git grep -W "seq_.*\" "

and filterng out the false positives:

Quoted 7 lines
    arch/powerpc/sysdev/fsl_msi.c:  seq_printf(p, " fsl-msi-%d", cascade_virq);
    drivers/bus/moxtet.c:   seq_printf(p, " moxtet-%s.%i#%i",
mox_module_name(id), pos->idx,
    drivers/irqchip/irq-partition-percpu.c: seq_printf(p, " %5s-%lu",
chip->name, data->hwirq);
    drivers/soc/qcom/smp2p.c:       seq_printf(p, " %8s",

dev_name(entry->smp2p->dev));

Care to send a patch?

Thanks,

tglx

#15 lore raw

[tip: irq/urgent] genirq/proc: Add missing space separator back

In reply to Thomas Gleixner
The following commit has been merged into the irq/urgent branch of tip:

The following commit has been merged into the irq/urgent branch of tip:

Commit-ID:     9d9f204bdf7243bfc2c6a023d63c63f7cbf8ef0b
Gitweb:        https://git.kernel.org/tip/9d9f204bdf7243bfc2c6a023d63c63f7cbf8ef0b
Author:        Thomas Gleixner <[email protected]>
AuthorDate:    Tue, 03 Dec 2024 11:40:43 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Tue, 03 Dec 2024 14:59:34 +01:00

genirq/proc: Add missing space separator back

The recent conversion of show_interrupts() to seq_put_decimal_ull_width() caused a formatting regression as it drops a previosuly existing space separator.

Add it back by unconditionally inserting a space after the interrupt counts and removing the extra leading space from the chip name prints.

---
 kernel/irq/proc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/irq/proc.c +4 -3
index f36c33b..8e29809 100644
@@ -501,17 +501,18 @@ int show_interrupts(struct seq_file *p, void *v)
 
 		seq_put_decimal_ull_width(p, " ", cnt, 10);
 	}
+	seq_putc(p, ' ');
 
 	raw_spin_lock_irqsave(&desc->lock, flags);
 	if (desc->irq_data.chip) {
 		if (desc->irq_data.chip->irq_print_chip)
 			desc->irq_data.chip->irq_print_chip(&desc->irq_data, p);
 		else if (desc->irq_data.chip->name)
-			seq_printf(p, " %8s", desc->irq_data.chip->name);
+			seq_printf(p, "%8s", desc->irq_data.chip->name);
 		else
-			seq_printf(p, " %8s", "-");
+			seq_printf(p, "%8s", "-");
 	} else {
-		seq_printf(p, " %8s", "None");
+		seq_printf(p, "%8s", "None");
 	}
 	if (desc->irq_data.domain)
 		seq_printf(p, " %*lu", prec, desc->irq_data.hwirq);
author
#16 lore raw
In reply to Geert Uytterhoeven

Hi,

Quoted 42 lines · Geert Uytterhoeven

Hi David,

Quoted 11 lines · David Wang

seq_printf() is costy, on a system with m interrupts and n CPUs, there would be m*n decimal values yield via seq_printf() when reading /proc/interrupts, the cost parsing format strings grows with number of CPU. Profiling on a x86 8-core system indicates seq_printf() takes ~47% samples of show_interrupts(), and replace seq_printf() with seq_put_decimal_ull_width() could have near 30% performance gain.

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

Thanks for your patch, which is now commit f9ed1f7c2e26fcd1 ("genirq/proc: Use seq_put_decimal_ull_width() for decimal values") in irqchip/irq/core.

This removes a space after the last CPU column, causing the values in this column to be concatenated to the values in the next column.

E.g. on Koelsch (R-Car M-W), the output changes from:

	       CPU0       CPU1
     27:       1871       2017 GIC-0  27 Level     arch_timer
     29:        646          0 GIC-0 205 Level     e60b0000.i2c
     30:          0          0 GIC-0 174 Level     ffca0000.timer
     31:          0          0 GIC-0  36 Level     e6050000.gpio
     32:          0          0 GIC-0  37 Level     e6051000.gpio
     [...]

to

	       CPU0       CPU1
     27:       1966       1900GIC-0  27 Level     arch_timer
     29:        580          0GIC-0 205 Level     e60b0000.i2c
     30:          0          0GIC-0 174 Level     ffca0000.timer
     31:          0          0GIC-0  36 Level     e6050000.gpio
     32:          0          0GIC-0  37 Level     e6051000.gpio
     [...]
Quoted 2 lines

making the output hard to read, and probably breaking scripts that parse its contents.

Thanks for reporting this, I was considering the spaces and checked it on my system, I thought "all" descriptions have leading spaces and it's ok to remove the extra one. But I did not check all the "irq_print_chip" codes, now when
checking the code, there are many GPIO drivers' implementations with no leading spaces. (The behavior is not consistent cross driver implementations though...)

Sorry for the regression, and thanks for catching this.

David

author
#17 lore raw
Quoted 44 lines · David Wang

Hi,

Quoted 42 lines · Geert Uytterhoeven

Hi David,

Quoted 11 lines · David Wang

seq_printf() is costy, on a system with m interrupts and n CPUs, there would be m*n decimal values yield via seq_printf() when reading /proc/interrupts, the cost parsing format strings grows with number of CPU. Profiling on a x86 8-core system indicates seq_printf() takes ~47% samples of show_interrupts(), and replace seq_printf() with seq_put_decimal_ull_width() could have near 30% performance gain.

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

Thanks for your patch, which is now commit f9ed1f7c2e26fcd1 ("genirq/proc: Use seq_put_decimal_ull_width() for decimal values") in irqchip/irq/core.

This removes a space after the last CPU column, causing the values in this column to be concatenated to the values in the next column.

E.g. on Koelsch (R-Car M-W), the output changes from:

	       CPU0       CPU1
     27:       1871       2017 GIC-0  27 Level     arch_timer
     29:        646          0 GIC-0 205 Level     e60b0000.i2c
     30:          0          0 GIC-0 174 Level     ffca0000.timer
     31:          0          0 GIC-0  36 Level     e6050000.gpio
     32:          0          0 GIC-0  37 Level     e6051000.gpio
     [...]

to

	       CPU0       CPU1
     27:       1966       1900GIC-0  27 Level     arch_timer
     29:        580          0GIC-0 205 Level     e60b0000.i2c
     30:          0          0GIC-0 174 Level     ffca0000.timer
     31:          0          0GIC-0  36 Level     e6050000.gpio
     32:          0          0GIC-0  37 Level     e6051000.gpio
     [...]
Quoted 8 lines
Quoted 2 lines

making the output hard to read, and probably breaking scripts that parse its contents.

Thanks for reporting this, I was considering the spaces and checked it on my system, I thought "all" descriptions have leading spaces and it's ok to remove the extra one. But I did not check all the "irq_print_chip" codes, now when
checking the code, there are many GPIO drivers' implementations with no leading spaces. (The behavior is not consistent cross driver implementations though...)

Several drivers use dev_name as format string for seq_printf, would this raise security concerns?

       drivers/gpio/gpio-xgs-iproc.c:	seq_printf(p, dev_name(chip->dev));
        drivers/gpio/gpio-mlxbf2.c:	seq_printf(p, dev_name(gs->dev));
        drivers/gpio/gpio-omap.c:	seq_printf(p, dev_name(bank->dev));
        drivers/gpio/gpio-hlwd.c:	seq_printf(p, dev_name(hlwd->dev));
        drivers/gpio/gpio-aspeed.c:	seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pca953x.c:	seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra186.c:	seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra.c:	seq_printf(s, dev_name(chip->parent));
        drivers/gpio/gpio-ep93xx.c:	seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-aspeed-sgpio.c:	seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pl061.c:	seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-visconti.c:	seq_printf(p, dev_name(priv->dev));
Quoted 5 lines

Sorry for the regression, and thanks for catching this.

David

David

#18 lore raw

Hi David,

Quoted 53 lines · David Wang
Quoted 41 lines · David Wang
Quoted 40 lines · Geert Uytterhoeven
Quoted 11 lines · David Wang

seq_printf() is costy, on a system with m interrupts and n CPUs, there would be m*n decimal values yield via seq_printf() when reading /proc/interrupts, the cost parsing format strings grows with number of CPU. Profiling on a x86 8-core system indicates seq_printf() takes ~47% samples of show_interrupts(), and replace seq_printf() with seq_put_decimal_ull_width() could have near 30% performance gain.

The improvement has pratical significance, considering many monitoring tools would read /proc/interrupts periodically.

Thanks for your patch, which is now commit f9ed1f7c2e26fcd1 ("genirq/proc: Use seq_put_decimal_ull_width() for decimal values") in irqchip/irq/core.

This removes a space after the last CPU column, causing the values in this column to be concatenated to the values in the next column.

E.g. on Koelsch (R-Car M-W), the output changes from:

            CPU0       CPU1
     27:       1871       2017 GIC-0  27 Level     arch_timer
     29:        646          0 GIC-0 205 Level     e60b0000.i2c
     30:          0          0 GIC-0 174 Level     ffca0000.timer
     31:          0          0 GIC-0  36 Level     e6050000.gpio
     32:          0          0 GIC-0  37 Level     e6051000.gpio
     [...]

to

            CPU0       CPU1
     27:       1966       1900GIC-0  27 Level     arch_timer
     29:        580          0GIC-0 205 Level     e60b0000.i2c
     30:          0          0GIC-0 174 Level     ffca0000.timer
     31:          0          0GIC-0  36 Level     e6050000.gpio
     32:          0          0GIC-0  37 Level     e6051000.gpio
     [...]
Quoted 8 lines
Quoted 2 lines

making the output hard to read, and probably breaking scripts that parse its contents.

Thanks for reporting this, I was considering the spaces and checked it on my system, I thought "all" descriptions have leading spaces and it's ok to remove the extra one. But I did not check all the "irq_print_chip" codes, now when
checking the code, there are many GPIO drivers' implementations with no leading spaces. (The behavior is not consistent cross driver implementations though...)

Several drivers use dev_name as format string for seq_printf, would this raise security concerns?

Quoted 12 lines
       drivers/gpio/gpio-xgs-iproc.c:   seq_printf(p, dev_name(chip->dev));
        drivers/gpio/gpio-mlxbf2.c:     seq_printf(p, dev_name(gs->dev));
        drivers/gpio/gpio-omap.c:       seq_printf(p, dev_name(bank->dev));
        drivers/gpio/gpio-hlwd.c:       seq_printf(p, dev_name(hlwd->dev));
        drivers/gpio/gpio-aspeed.c:     seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pca953x.c:    seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra186.c:   seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra.c:      seq_printf(s, dev_name(chip->parent));
        drivers/gpio/gpio-ep93xx.c:     seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-aspeed-sgpio.c:       seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pl061.c:      seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-visconti.c:   seq_printf(p, dev_name(priv->dev));

In theory, yes. But I guess it's hard to sneak a percent sign in these device names.

But given the above, all of them should probably be updated to print an initial space?

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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
author
#19 lore raw
Quoted 19 lines · Geert Uytterhoeven

Hi David,

Quoted 15 lines

Several drivers use dev_name as format string for seq_printf, would this raise security concerns?

       drivers/gpio/gpio-xgs-iproc.c:   seq_printf(p, dev_name(chip->dev));
        drivers/gpio/gpio-mlxbf2.c:     seq_printf(p, dev_name(gs->dev));
        drivers/gpio/gpio-omap.c:       seq_printf(p, dev_name(bank->dev));
        drivers/gpio/gpio-hlwd.c:       seq_printf(p, dev_name(hlwd->dev));
        drivers/gpio/gpio-aspeed.c:     seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pca953x.c:    seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra186.c:   seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra.c:      seq_printf(s, dev_name(chip->parent));
        drivers/gpio/gpio-ep93xx.c:     seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-aspeed-sgpio.c:       seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pl061.c:      seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-visconti.c:   seq_printf(p, dev_name(priv->dev));
Quoted 2 lines

In theory, yes. But I guess it's hard to sneak a percent sign in these device names.

Yes, it is just theoretical... (Would be a wonderful story if someone manage it somehow :) ) Anyway, I send out another patch for further discussion.

Quoted 4 lines

But given the above, all of them should probably be updated to print an initial space?

Oh, no, I did not mean to adding leading space for those in irq_print_chip() I mentioned those just because of the format string thing.

Add leading space in those irq_print_chip() is kind of strange...
With Thomas's patch, irq_print_chip() needs not worry about the leading space issue.

Quoted 10 lines

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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

Thanks~
David

#20 lore raw

Hi David,

Quoted 22 lines · David Wang
Quoted 15 lines · Geert Uytterhoeven
Quoted 14 lines

Several drivers use dev_name as format string for seq_printf, would this raise security concerns?

       drivers/gpio/gpio-xgs-iproc.c:   seq_printf(p, dev_name(chip->dev));
        drivers/gpio/gpio-mlxbf2.c:     seq_printf(p, dev_name(gs->dev));
        drivers/gpio/gpio-omap.c:       seq_printf(p, dev_name(bank->dev));
        drivers/gpio/gpio-hlwd.c:       seq_printf(p, dev_name(hlwd->dev));
        drivers/gpio/gpio-aspeed.c:     seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pca953x.c:    seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra186.c:   seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-tegra.c:      seq_printf(s, dev_name(chip->parent));
        drivers/gpio/gpio-ep93xx.c:     seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-aspeed-sgpio.c:       seq_printf(p, dev_name(gpio->dev));
        drivers/gpio/gpio-pl061.c:      seq_printf(p, dev_name(gc->parent));
        drivers/gpio/gpio-visconti.c:   seq_printf(p, dev_name(priv->dev));
Quoted 2 lines

In theory, yes. But I guess it's hard to sneak a percent sign in these device names.

Yes, it is just theoretical... (Would be a wonderful story if someone manage it somehow :) ) Anyway, I send out another patch for further discussion.

Quoted 8 lines
Quoted 3 lines

But given the above, all of them should probably be updated to print an initial space?

Oh, no, I did not mean to adding leading space for those in irq_print_chip() I mentioned those just because of the format string thing.

Add leading space in those irq_print_chip() is kind of strange...
With Thomas's patch, irq_print_chip() needs not worry about the leading space issue.

Sure, but there's still a slight misalignment if you have multiple irqchips of different types:

153:          0          0 GIC-0 300 Level     feb00000.display
155:          0          0  da9063-irq   1 Level     ALARM
183:          1          0      irqc   0 Level     ee700000.ethernet-ffffffff:01
184:          0          0 GIC-0 197 Level     ee100000.mmc
185:         52          0 GIC-0 199 Level     ee140000.mmc
186:          0          0 GIC-0 200 Level     ee160000.mmc
187:          0          0  gpio-rcar   6 Edge      ee100000.mmc cd

I have just sent out a fix for another preexisting misalignment on ARM
https://lore.kernel.org/96f61cafee969c59796ac06c1410195fa0f1ba0b.1732096154.git.geert+renesas@glider.be

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

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