[RFC PATCH bpf-next 2/2] Export irq_time_read for BPF module usage

Jianlin Lv posted 2 patches 7 months, 3 weeks ago
[RFC PATCH bpf-next 2/2] Export irq_time_read for BPF module usage
Posted by Jianlin Lv 7 months, 3 weeks ago
From: Jianlin Lv <iecedge@gmail.com>

Move irq_time_read function to kernel/sched/core.c and export for
external use when CONFIG_IRQ_TIME_ACCOUNTING is enabled.

Signed-off-by: Jianlin Lv <iecedge@gmail.com>
---
 include/linux/sched.h |  4 ++++
 kernel/sched/core.c   | 22 ++++++++++++++++++++++
 kernel/sched/sched.h  | 19 -------------------
 3 files changed, 26 insertions(+), 19 deletions(-)

diff --git a/include/linux/sched.h b/include/linux/sched.h
index f96ac1982893..3b83ac99b533 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -2281,4 +2281,8 @@ static __always_inline void alloc_tag_restore(struct alloc_tag *tag, struct allo
 #define alloc_tag_restore(_tag, _old)		do {} while (0)
 #endif
 
+#ifdef CONFIG_IRQ_TIME_ACCOUNTING
+extern inline u64 irq_time_read(int cpu);
+#endif
+
 #endif
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index cfaca3040b2f..c840d1ffdaca 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -10747,3 +10747,25 @@ void sched_enq_and_set_task(struct sched_enq_and_set_ctx *ctx)
 		set_next_task(rq, ctx->p);
 }
 #endif	/* CONFIG_SCHED_CLASS_EXT */
+
+#ifdef CONFIG_IRQ_TIME_ACCOUNTING
+/*
+ * Returns the irqtime minus the softirq time computed by ksoftirqd.
+ * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
+ * and never move forward.
+ */
+inline u64 irq_time_read(int cpu)
+{
+	struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
+	unsigned int seq;
+	u64 total;
+
+	do {
+		seq = __u64_stats_fetch_begin(&irqtime->sync);
+		total = irqtime->total;
+	} while (__u64_stats_fetch_retry(&irqtime->sync, seq));
+
+	return total;
+}
+EXPORT_SYMBOL(irq_time_read);
+#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h
index 47972f34ea70..d2fd3772114e 100644
--- a/kernel/sched/sched.h
+++ b/kernel/sched/sched.h
@@ -3209,25 +3209,6 @@ static inline int irqtime_enabled(void)
 	return sched_clock_irqtime;
 }
 
-/*
- * Returns the irqtime minus the softirq time computed by ksoftirqd.
- * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
- * and never move forward.
- */
-static inline u64 irq_time_read(int cpu)
-{
-	struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
-	unsigned int seq;
-	u64 total;
-
-	do {
-		seq = __u64_stats_fetch_begin(&irqtime->sync);
-		total = irqtime->total;
-	} while (__u64_stats_fetch_retry(&irqtime->sync, seq));
-
-	return total;
-}
-
 #else
 
 static inline int irqtime_enabled(void)
-- 
2.34.1
Re: [RFC PATCH bpf-next 2/2] Export irq_time_read for BPF module usage
Posted by Peter Zijlstra 7 months, 3 weeks ago
On Tue, Apr 22, 2025 at 09:47:27PM +0800, Jianlin Lv wrote:
> From: Jianlin Lv <iecedge@gmail.com>
> 
> Move irq_time_read function to kernel/sched/core.c and export for
> external use when CONFIG_IRQ_TIME_ACCOUNTING is enabled.
> 
> Signed-off-by: Jianlin Lv <iecedge@gmail.com>
> ---
>  include/linux/sched.h |  4 ++++
>  kernel/sched/core.c   | 22 ++++++++++++++++++++++
>  kernel/sched/sched.h  | 19 -------------------
>  3 files changed, 26 insertions(+), 19 deletions(-)
> 
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index f96ac1982893..3b83ac99b533 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -2281,4 +2281,8 @@ static __always_inline void alloc_tag_restore(struct alloc_tag *tag, struct allo
>  #define alloc_tag_restore(_tag, _old)		do {} while (0)
>  #endif
>  
> +#ifdef CONFIG_IRQ_TIME_ACCOUNTING
> +extern inline u64 irq_time_read(int cpu);
> +#endif
> +
>  #endif
> diff --git a/kernel/sched/core.c b/kernel/sched/core.c
> index cfaca3040b2f..c840d1ffdaca 100644
> --- a/kernel/sched/core.c
> +++ b/kernel/sched/core.c
> @@ -10747,3 +10747,25 @@ void sched_enq_and_set_task(struct sched_enq_and_set_ctx *ctx)
>  		set_next_task(rq, ctx->p);
>  }
>  #endif	/* CONFIG_SCHED_CLASS_EXT */
> +
> +#ifdef CONFIG_IRQ_TIME_ACCOUNTING
> +/*
> + * Returns the irqtime minus the softirq time computed by ksoftirqd.
> + * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
> + * and never move forward.
> + */
> +inline u64 irq_time_read(int cpu)
> +{
> +	struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
> +	unsigned int seq;
> +	u64 total;
> +
> +	do {
> +		seq = __u64_stats_fetch_begin(&irqtime->sync);
> +		total = irqtime->total;
> +	} while (__u64_stats_fetch_retry(&irqtime->sync, seq));
> +
> +	return total;
> +}
> +EXPORT_SYMBOL(irq_time_read);

_GPL(), but as I've argued in the earlier email, I don't think you want
this. I think you want access to clock_task instead.