[PATCH] genirq: move clear of kstat_irqs to free_desc()

Luigi Rizzo posted 1 patch 3 weeks, 5 days ago
There is a newer version of this series
kernel/irq/irqdesc.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH] genirq: move clear of kstat_irqs to free_desc()
Posted by Luigi Rizzo 3 weeks, 5 days ago
desc_set_defaults() has a loop to clear the per-cpu counters kstats_irq.

This is only needed in free_desc(), which is used with non-sparse IRQs
so that the irq_desc can be recycled. For newly allocated irq_desc,
the memory comes from alloc_percpu() and is already zeroed out.

Move the loop to free_desc() to avoid wasting time unnecessarily.

This is especially important on large servers with 100+ CPUs, because
each write results in a cache miss, and the write buffer can only have
so many outstanding transactions.

Below is an example of cost on a host with 480 CPUs, taken with
local_irq_save()/restore() around the code to avoid interference.
Measurements taken with kstats
https://github.com/luigirizzo/lr-cstats/tree/main/kstats

BUCKET 	SAMPLES	 AVG TIME(ns)  PERCENTILE

 40           3         2432   0.000366
 41           3         3000   0.000732
 42          24         3241   0.003662
 43          33         3971   0.007690
 44         963         4742   0.125244
 45        1071         5545   0.255981
 46         494         6644   0.316284
 47         352         7661   0.359252
 48         816         9447   0.458862
 49        2214        11493   0.729125
 50        1440        13027   0.904907
 51         428        15219   0.957153
 52         275        18211   0.990722
 53          69        21396   0.999145
 54           4        26125   0.999633
 55           1        28996   0.999755
 56           2        37253   1.000000

Signed-off-by: Luigi Rizzo <lrizzo@google.com>
---
 kernel/irq/irqdesc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/kernel/irq/irqdesc.c b/kernel/irq/irqdesc.c
index f8e4e13dbe339..fa1e3c7838aa7 100644
--- a/kernel/irq/irqdesc.c
+++ b/kernel/irq/irqdesc.c
@@ -134,8 +134,6 @@ static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
 	desc->tot_count = 0;
 	desc->name = NULL;
 	desc->owner = owner;
-	for_each_possible_cpu(cpu)
-		*per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
 	desc_smp_init(desc, node, affinity);
 }
 
@@ -622,8 +620,11 @@ static void free_desc(unsigned int irq)
 {
 	struct irq_desc *desc = irq_to_desc(irq);
 
-	scoped_guard(raw_spinlock_irqsave, &desc->lock)
+	scoped_guard(raw_spinlock_irqsave, &desc->lock) {
 		desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
+		for_each_possible_cpu(cpu)
+			*per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
+	}
 	delete_irq_desc(irq);
 }
 
-- 
2.52.0.457.g6b5491de43-goog
Re: [PATCH] genirq: move clear of kstat_irqs to free_desc()
Posted by kernel test robot 3 weeks, 5 days ago
Hi Luigi,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/irq/core]
[also build test ERROR on linus/master v6.19-rc5 next-20260109]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Luigi-Rizzo/genirq-move-clear-of-kstat_irqs-to-free_desc/20260112-031857
base:   tip/irq/core
patch link:    https://lore.kernel.org/r/20260111191710.1993867-1-lrizzo%40google.com
patch subject: [PATCH] genirq: move clear of kstat_irqs to free_desc()
config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20260112/202601121654.B4jBkJbI-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260112/202601121654.B4jBkJbI-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601121654.B4jBkJbI-lkp@intel.com/

All errors (new ones prefixed by >>):

   kernel/irq/irqdesc.c:118:6: warning: unused variable 'cpu' [-Wunused-variable]
     118 |         int cpu;
         |             ^~~
   kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
     625 |                 for_each_possible_cpu(cpu)
         |                                       ^
   kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
   kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
   kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
   kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
   kernel/irq/irqdesc.c:626:35: error: use of undeclared identifier 'cpu'
     626 |                         *per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
         |                                                        ^
>> kernel/irq/irqdesc.c:626:4: error: indirection requires pointer operand ('unsigned long' invalid)
     626 |                         *per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   1 warning and 7 errors generated.


vim +626 kernel/irq/irqdesc.c

   618	
   619	static void free_desc(unsigned int irq)
   620	{
   621		struct irq_desc *desc = irq_to_desc(irq);
   622	
   623		scoped_guard(raw_spinlock_irqsave, &desc->lock) {
   624			desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
 > 625			for_each_possible_cpu(cpu)
 > 626				*per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
   627		}
   628		delete_irq_desc(irq);
   629	}
   630	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH] genirq: move clear of kstat_irqs to free_desc()
Posted by Luigi Rizzo 3 weeks, 5 days ago
On Sun, Jan 11, 2026 at 8:17 PM Luigi Rizzo <lrizzo@google.com> wrote:
>
> desc_set_defaults() has a loop to clear the per-cpu counters kstats_irq.
>
> This is only needed in free_desc(), which is used with non-sparse IRQs
> so that the irq_desc can be recycled. For newly allocated irq_desc,
> the memory comes from alloc_percpu() and is already zeroed out.
>
> Move the loop to free_desc() to avoid wasting time unnecessarily.

...

ah I forgot to move the "int cpu;" line, just sent a corrected [PATCH v2]

cheers
luigi
Re: [PATCH] genirq: move clear of kstat_irqs to free_desc()
Posted by kernel test robot 3 weeks, 5 days ago
Hi Luigi,

kernel test robot noticed the following build errors:

[auto build test ERROR on tip/irq/core]
[also build test ERROR on linus/master v6.19-rc4 next-20260109]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Luigi-Rizzo/genirq-move-clear-of-kstat_irqs-to-free_desc/20260112-031857
base:   tip/irq/core
patch link:    https://lore.kernel.org/r/20260111191710.1993867-1-lrizzo%40google.com
patch subject: [PATCH] genirq: move clear of kstat_irqs to free_desc()
config: hexagon-allnoconfig (https://download.01.org/0day-ci/archive/20260112/202601120627.Dd67Hehl-lkp@intel.com/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 9b8addffa70cee5b2acc5454712d9cf78ce45710)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260112/202601120627.Dd67Hehl-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601120627.Dd67Hehl-lkp@intel.com/

All errors (new ones prefixed by >>):

   kernel/irq/irqdesc.c:118:6: warning: unused variable 'cpu' [-Wunused-variable]
     118 |         int cpu;
         |             ^~~
>> kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
     625 |                 for_each_possible_cpu(cpu)
         |                                       ^~~
>> kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
     625 |                 for_each_possible_cpu(cpu)
         |                                       ^~~
>> kernel/irq/irqdesc.c:625:25: error: use of undeclared identifier 'cpu'
     625 |                 for_each_possible_cpu(cpu)
         |                                       ^~~
   kernel/irq/irqdesc.c:626:35: error: use of undeclared identifier 'cpu'
     626 |                         *per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
         |                                                        ^~~
   1 warning and 4 errors generated.


vim +/cpu +625 kernel/irq/irqdesc.c

   618	
   619	static void free_desc(unsigned int irq)
   620	{
   621		struct irq_desc *desc = irq_to_desc(irq);
   622	
   623		scoped_guard(raw_spinlock_irqsave, &desc->lock) {
   624			desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
 > 625			for_each_possible_cpu(cpu)
   626				*per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
   627		}
   628		delete_irq_desc(irq);
   629	}
   630	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki