[PATCH 2/2] softirq: redefine the type of kernel_stat.softirqs[] as unsigned long

From: thunder . leizhen
Date: Mon Jul 24 2023 - 09:23:51 EST


From: Zhen Lei <thunder.leizhen@xxxxxxxxxx>

As commit aa0ce5bbc2db ("softirq: introduce statistics for softirq")
mentioned, the number of one softirq can exceed 100 per second. At this
rate, the 32-bit sum will overflow after 1.5 years. This problem can be
avoided if the type of 'softirqs[]' is changed to u64, but the atomicity
of the counting operation cannot be guaranteed on 32-bit processors.
Changing to unsigned long can safely solve the problem on 64-bit
processors, and it is the same as the type of 'irqs_sum'.

Signed-off-by: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
---
fs/proc/softirqs.c | 2 +-
fs/proc/stat.c | 2 +-
include/linux/kernel_stat.h | 8 ++++----
kernel/rcu/tree.h | 2 +-
kernel/rcu/tree_stall.h | 6 +++---
5 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/fs/proc/softirqs.c b/fs/proc/softirqs.c
index f4616083faef3bb..985be6904d748ab 100644
--- a/fs/proc/softirqs.c
+++ b/fs/proc/softirqs.c
@@ -20,7 +20,7 @@ static int show_softirqs(struct seq_file *p, void *v)
for (i = 0; i < NR_SOFTIRQS; i++) {
seq_printf(p, "%12s:", softirq_to_name[i]);
for_each_possible_cpu(j)
- seq_printf(p, " %10u", kstat_softirqs_cpu(i, j));
+ seq_printf(p, " %10lu", kstat_softirqs_cpu(i, j));
seq_putc(p, '\n');
}
return 0;
diff --git a/fs/proc/stat.c b/fs/proc/stat.c
index 84aac577a50cabb..7d40d98471d5ab2 100644
--- a/fs/proc/stat.c
+++ b/fs/proc/stat.c
@@ -116,7 +116,7 @@ static int show_stat(struct seq_file *p, void *v)
sum += arch_irq_stat_cpu(i);

for (j = 0; j < NR_SOFTIRQS; j++) {
- unsigned int softirq_stat = kstat_softirqs_cpu(j, i);
+ unsigned long softirq_stat = kstat_softirqs_cpu(j, i);

per_softirq_sums[j] += softirq_stat;
sum_softirq += softirq_stat;
diff --git a/include/linux/kernel_stat.h b/include/linux/kernel_stat.h
index 9935f7ecbfb9e31..b6c5723a1149cd6 100644
--- a/include/linux/kernel_stat.h
+++ b/include/linux/kernel_stat.h
@@ -40,7 +40,7 @@ struct kernel_cpustat {

struct kernel_stat {
unsigned long irqs_sum;
- unsigned int softirqs[NR_SOFTIRQS];
+ unsigned long softirqs[NR_SOFTIRQS];
};

DECLARE_PER_CPU(struct kernel_stat, kstat);
@@ -63,15 +63,15 @@ static inline void kstat_incr_softirqs_this_cpu(unsigned int irq)
__this_cpu_inc(kstat.softirqs[irq]);
}

-static inline unsigned int kstat_softirqs_cpu(unsigned int irq, int cpu)
+static inline unsigned long kstat_softirqs_cpu(unsigned int irq, int cpu)
{
return kstat_cpu(cpu).softirqs[irq];
}

-static inline unsigned int kstat_cpu_softirqs_sum(int cpu)
+static inline unsigned long kstat_cpu_softirqs_sum(int cpu)
{
int i;
- unsigned int sum = 0;
+ unsigned long sum = 0;

for (i = 0; i < NR_SOFTIRQS; i++)
sum += kstat_softirqs_cpu(i, cpu);
diff --git a/kernel/rcu/tree.h b/kernel/rcu/tree.h
index 192536916f9a607..ce51640cd34f66b 100644
--- a/kernel/rcu/tree.h
+++ b/kernel/rcu/tree.h
@@ -268,7 +268,7 @@ struct rcu_data {
unsigned long rcuc_activity;

/* 7) Diagnostic data, including RCU CPU stall warnings. */
- unsigned int softirq_snap; /* Snapshot of softirq activity. */
+ unsigned long softirq_snap; /* Snapshot of softirq activity. */
/* ->rcu_iw* fields protected by leaf rcu_node ->lock. */
struct irq_work rcu_iw; /* Check for non-irq activity. */
bool rcu_iw_pending; /* Is ->rcu_iw pending? */
diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h
index 0ade7f9dcaa18da..d5b22700abcc385 100644
--- a/kernel/rcu/tree_stall.h
+++ b/kernel/rcu/tree_stall.h
@@ -446,7 +446,7 @@ static void print_cpu_stat_info(int cpu)
rsr.cputime_system = kcpustat_field(kcsp, CPUTIME_SYSTEM, cpu);

pr_err("\t hardirqs softirqs csw/system\n");
- pr_err("\t number: %8ld %10d %12lld\n",
+ pr_err("\t number: %8ld %10ld %12lld\n",
kstat_cpu_irqs_sum(cpu) - rsrp->nr_hardirqs,
kstat_cpu_softirqs_sum(cpu) - rsrp->nr_softirqs,
nr_context_switches_cpu(cpu) - rsrp->nr_csw);
@@ -498,7 +498,7 @@ static void print_cpu_stall_info(int cpu)
rcuc_starved = rcu_is_rcuc_kthread_starving(rdp, &j);
if (rcuc_starved)
sprintf(buf, " rcuc=%ld jiffies(starved)", j);
- pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%u/%u fqs=%ld%s%s\n",
+ pr_err("\t%d-%c%c%c%c: (%lu %s) idle=%04x/%ld/%#lx softirq=%lu/%lu fqs=%ld%s%s\n",
cpu,
"O."[!!cpu_online(cpu)],
"o."[!!(rdp->grpmask & rdp->mynode->qsmaskinit)],
@@ -575,7 +575,7 @@ static void rcu_check_gp_kthread_expired_fqs_timer(void)
data_race(rcu_state.gp_flags),
gp_state_getname(RCU_GP_WAIT_FQS), RCU_GP_WAIT_FQS,
data_race(READ_ONCE(gpk->__state)));
- pr_err("\tPossible timer handling issue on cpu=%d timer-softirq=%u\n",
+ pr_err("\tPossible timer handling issue on cpu=%d timer-softirq=%lu\n",
cpu, kstat_softirqs_cpu(TIMER_SOFTIRQ, cpu));
}
}
--
2.25.1