s390: Remove vtimer infrastructure
Heiko Carstens
maintainercover s390: Remove vtimer infrastructure
s390's virtual timer infrastructure has only a single user and the semantics of the interface are not straightforward: a virtual timer fires if the sum of elapsed CPU time over all CPUs exceeds a specified value.
This is suboptimal since the number of active CPUs defines how frequently the timer fires. With many CPUs and short intervals this can be very frequent. Furthermore the required accounting in the vtime code uses a single 64-bit value to accumulate elapsed CPU time. In busy systems this can lead to cache line bouncing when the value is updated concurrently on several CPUs; which is the reason why other accounting mechanisms use per-CPU data structures for this purpose.
In practice this is not a problem with the appldata default interval of 10 seconds, however the interval is configurable via sysctl with a minimum value of 1ms.
Given that there is only a single user, emulate the virtual timer in appldata using delayed work: the work is scheduled for the minimum possible wall-clock time until the configured CPU-time interval elapses (remaining CPU time / number of online CPUs), with a minimum delay of 100ms. The work then reads per-CPU statistics of all online CPUs to check whether enough CPU time has elapsed, and if so runs the registered callbacks.
The calculation is more expensive, but avoids potential cache line bouncing problem. In addition it allows to remove the entire vtimer infrastructure code with a subsequent patch.
Thanks,
Heiko
Heiko Carstens (2):
s390/appldata: Emulate virtual timer with delayed work s390/vtime: Remove vtimer infrastructure
arch/s390/appldata/appldata_base.c | 150 +++++++++++--------- arch/s390/include/asm/vtime.h | 2 + arch/s390/include/asm/vtimer.h | 30 ---- arch/s390/kernel/process.c | 1 - arch/s390/kernel/smp.c | 2 +- arch/s390/kernel/time.c | 2 +- arch/s390/kernel/vtime.c | 212 ++--------------------------- 7 files changed, 94 insertions(+), 305 deletions(-) delete mode 100644 arch/s390/include/asm/vtimer.h
Signature · 1 line
2.53.0
Heiko Carstens
maintainerRFC PATCH 2/2 s390/vtime: Remove vtimer infrastructure
The only user is gone - remove the vtimer infrastructure.
--- arch/s390/include/asm/vtime.h | 2 + arch/s390/include/asm/vtimer.h | 30 ----- arch/s390/kernel/process.c | 1 - arch/s390/kernel/smp.c | 2 +- arch/s390/kernel/time.c | 2 +- arch/s390/kernel/vtime.c | 212 ++------------------------------- 6 files changed, 12 insertions(+), 237 deletions(-) delete mode 100644 arch/s390/include/asm/vtimer.h
arch/s390/include/asm/vtime.h +2 -0
@@ -8,6 +8,8 @@DECLARE_PER_CPU (u64, mt_cycles[8 ]); +void vtime_init (void ); +static inline void update_timer_sys (void ) {struct lowcore *lc =get_lowcore ();
arch/s390/include/asm/vtimer.h deleted +0 -30
@@ -1,30 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -/* -* Copyright IBM Corp. 2003, 2012 -* Virtual CPU timer -* -* Author(s): Jan Glauber <[email protected]> -*/ - -#ifndef _ASM_S390_TIMER_H -#define _ASM_S390_TIMER_H - -#define VTIMER_MAX_SLICE (0x7fffffffffffffffULL) - -struct vtimer_list { -struct list_head entry; -u64 expires; -u64 interval; -void (*function)(unsigned long ); -unsigned long data; -}; - -extern void init_virt_timer (struct vtimer_list *timer); -extern void add_virt_timer (struct vtimer_list *timer); -extern void add_virt_timer_periodic (struct vtimer_list *timer); -extern int mod_virt_timer (struct vtimer_list *timer,u64 expires); -extern int mod_virt_timer_periodic (struct vtimer_list *timer,u64 expires); -extern int del_virt_timer (struct vtimer_list *timer); -extern void vtime_init (void ); - -#endif /* _ASM_S390_TIMER_H */
arch/s390/kernel/process.c +0 -1
@@ -35,7 +35,6 @@#include <asm/cpu_mf.h> #include <asm/processor.h> #include <asm/ptrace.h> -#include <asm/vtimer.h> #include <asm/exec.h> #include <asm/fpu.h> #include <asm/irq.h>
arch/s390/kernel/smp.c +1 -1
@@ -48,7 +48,7 @@#include <asm/setup.h> #include <asm/irq.h> #include <asm/tlbflush.h> -#include <asm/vtimer.h> +#include <asm/vtime.h> #include <asm/abs_lowcore.h> #include <asm/sclp.h> #include <asm/debug.h>
arch/s390/kernel/time.c +1 -1
@@ -48,9 +48,9 @@#include <asm/vdso.h> #include <asm/irq.h> #include <asm/irq_regs.h> -#include <asm/vtimer.h> #include <asm/stp.h> #include <asm/cio.h> +#include <asm/vtime.h> #include "entry.h" union tod_clock __bootdata_preserved (tod_clock_base );
arch/s390/kernel/vtime.c +8 -204
268 lines not on this page · raw message
Signature · 1 line
2.53.0
Heiko Carstens
maintainerRFC PATCH 1/2 s390/appldata: Emulate virtual timer with delayed work
In reply to Heiko CarstensEmulate the virtual timer in appldata using delayed work: the work is scheduled for the minimum possible wall-clock time until the configured CPU-time interval elapses (remaining CPU time / number of online CPUs), with a minimum delay of 100ms. The work then reads per-CPU statistics of all online CPUs to check whether enough CPU time has elapsed, and if so runs the registered callbacks.
This slightly more expensive, but allows to subsequently remove the entire vtimer infrastructure.
--- arch/s390/appldata/appldata_base.c | 150 ++++++++++++++++------------- 1 file changed, 82 insertions(+), 68 deletions(-)
arch/s390/appldata/appldata_base.c +82 -68
@@ -28,19 +28,14 @@#include <linux/workqueue.h> #include <linux/uaccess.h> #include <linux/io.h> +#include <linux/kernel_stat.h> #include <asm/appldata.h> -#include <asm/vtimer.h> #include <asm/smp.h> #include "appldata.h" - -#define APPLDATA_CPU_INTERVAL 10000/* default (CPU) time for -sampling interval in -milliseconds */ - -#define TOD_MICRO 0x01000/* nr. of TOD clock units -for 1 microsecond */ +/* Default CPU time for sampling interval */ +#define APPLDATA_CPU_INTERVAL (10 * NSEC_PER_SEC)/* * /proc entries (sysctl)
@@ -64,22 +59,14 @@ static const struct ctl_table appldata_table[] = { }, }; -/* -* Timer -*/ -static struct vtimer_list appldata_timer; +static void appldata_work_fn (struct work_struct *work); +static DECLARE_DELAYED_WORK(appldata_work, appldata_work_fn); -static DEFINE_SPINLOCK (appldata_timer_lock); -static int appldata_interval =APPLDATA_CPU_INTERVAL ; +static DEFINE_MUTEX (appldata_timer_lock); +static u64 appldata_interval =APPLDATA_CPU_INTERVAL ;static int appldata_timer_active; -/* -* Work queue -*/ -static struct workqueue_struct *appldata_wq; -static void appldata_work_fn (struct work_struct *work); -static DECLARE_WORK(appldata_work, appldata_work_fn); - +static u64 appldata_cputime_start;/* * Ops list
@@ -89,34 +76,68 @@ static LIST_HEAD(appldata_ops_list);/*************************** timer, work, DIAG *******************************/ -/* -* appldata_timer_function() -* -* schedule work and reschedule timer -*/ -static void appldata_timer_function (unsigned long data) +static u64 appldata_total_cpu_time_ns (void ) { -queue_work (appldata_wq, (struct work_struct *) data); +u64 total =0 ; +int cpu; + +for_each_online_cpu (cpu) { + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_USER ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_NICE ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_SYSTEM ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_IRQ ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_SOFTIRQ ]; + } +return total; +} + +static void appldata_schedule_work (u64 remaining) +{ +unsigned int ncpus =num_online_cpus (); +unsigned long delay =HZ /10 ; + +/* +* At most ncpus CPUs consume CPU time simultaneously, so the +* minimum wall-clock time until the remaining CPU time elapses +* is remaining / ncpus. +* Make sure the work is not scheduled more than once per 100ms. +*/ + delay =max (delay,nsecs_to_jiffies (remaining / ncpus)); +schedule_delayed_work (&appldata_work, delay); }/* * appldata_work_fn() * -* call data gathering function for each (active) module +* Check whether the CPU-time interval has elapsed. If yes, run the +* data-gathering callbacks and reschedule for the next full interval. +* If no, only reschedule for the remaining time. */ static void appldata_work_fn (struct work_struct *work) { -struct list_head *lh; +u64 now, elapsed, interval;struct appldata_ops *ops; +struct list_head *lh; -mutex_lock (&appldata_ops_mutex); + now =appldata_total_cpu_time_ns (); + scoped_guard(mutex, &appldata_timer_lock) { +if (!appldata_timer_active) +return ; + interval = appldata_interval; + elapsed = now - appldata_cputime_start; +if (elapsed < interval) { +appldata_schedule_work (interval - elapsed); +return ; + } + appldata_cputime_start = now; +appldata_schedule_work (interval); + } +guard (mutex)(&appldata_ops_mutex);list_for_each (lh, &appldata_ops_list) { ops = list_entry(lh, struct appldata_ops, list); -if (ops->active ==1 ) { +if (ops->active) ops->callback (ops->data); - } } -mutex_unlock (&appldata_ops_mutex); }static struct appldata_product_id appldata_id = {
@@ -162,33 +183,37 @@ int appldata_diag(char record_nr, u16 function, unsigned long buffer,#define APPLDATA_MOD_TIMER 2/* -* __appldata_vtimer_setup() +* appldata_work_setup() * -* Add, delete or modify virtual timers on all online cpus. -* The caller needs to get the appldata_timer_lock spinlock. +* Add, delete or modify the appldata delayed work. */ -static void __appldata_vtimer_setup (int cmd) +static void appldata_work_setup (int cmd) { -u64 timer_interval = (u64 ) appldata_interval *1000 *TOD_MICRO ; +u64 now, elapsed, remaining;switch (cmd) {case APPLDATA_ADD_TIMER : +lockdep_assert_held (&appldata_timer_lock);if (appldata_timer_active)break ; - appldata_timer.expires = timer_interval; -add_virt_timer_periodic (&appldata_timer); + appldata_cputime_start =appldata_total_cpu_time_ns (); +appldata_schedule_work (appldata_interval); appldata_timer_active =1 ;break ;case APPLDATA_DEL_TIMER : -del_virt_timer (&appldata_timer); -if (!appldata_timer_active) -break ; +lockdep_assert_not_held (&appldata_timer_lock); appldata_timer_active =0 ; +cancel_delayed_work_sync (&appldata_work);break ;case APPLDATA_MOD_TIMER : +lockdep_assert_held (&appldata_timer_lock);if (!appldata_timer_active)break ; -mod_virt_timer_periodic (&appldata_timer, timer_interval); + now =appldata_total_cpu_time_ns (); + elapsed = now - appldata_cputime_start; + remaining = (elapsed < appldata_interval) ? (appldata_interval - elapsed) :1 ; +appldata_schedule_work (remaining); +break ; } }
@@ -215,12 +240,12 @@ appldata_timer_handler(const struct ctl_table *ctl, int write,if (rc <0 || !write)return rc; -spin_lock (&appldata_timer_lock); -if (timer_active) -__appldata_vtimer_setup (APPLDATA_ADD_TIMER ); -else -__appldata_vtimer_setup (APPLDATA_DEL_TIMER ); -spin_unlock (&appldata_timer_lock); +if (timer_active) { + scoped_guard(mutex, &appldata_timer_lock) +appldata_work_setup (APPLDATA_ADD_TIMER ); + }else { +appldata_work_setup (APPLDATA_DEL_TIMER ); + }return 0 ; }
@@ -234,11 +259,11 @@ static int appldata_interval_handler(const struct ctl_table *ctl, int write, void *buffer, size_t *lenp, loff_t *ppos) { - int interval = appldata_interval; + int interval_ms = appldata_interval / NSEC_PER_MSEC;int rc;struct ctl_table ctl_entry = { .procname = ctl->procname, - .data = &interval, + .data = &interval_ms, .maxlen =sizeof (int ), .extra1 =SYSCTL_ONE , };
@@ -247,10 +272,10 @@ appldata_interval_handler(const struct ctl_table *ctl, int write,if (rc <0 || !write)return rc; -spin_lock (&appldata_timer_lock); - appldata_interval = interval; -__appldata_vtimer_setup (APPLDATA_MOD_TIMER ); -spin_unlock (&appldata_timer_lock); + scoped_guard(mutex, &appldata_timer_lock) { + appldata_interval = interval_ms *NSEC_PER_MSEC ; +appldata_work_setup (APPLDATA_MOD_TIMER ); + }return 0 ; }
@@ -392,19 +417,8 @@ void appldata_unregister_ops(struct appldata_ops *ops)/******************************* init / exit *********************************/ -/* -* appldata_init() -* -* init timer, register /proc entries -*/ static int __init appldata_init (void ) { -init_virt_timer (&appldata_timer); - appldata_timer.function = appldata_timer_function; - appldata_timer.data = (unsigned long ) &appldata_work; - appldata_wq =alloc_ordered_workqueue ("appldata" ,0 ); -if (!appldata_wq) -return -ENOMEM ;register_sysctl (appldata_proc_name, appldata_table);return 0 ; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH 2/2] s390/vtime: Remove vtimer infrastructure
In reply to Heiko CarstensSashiko has reviewed this patch and found no issues. It looks great!
Quoted 3 lines
The only user is gone - remove the vtimer infrastructure.
Signed-off-by Heiko Carstens <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [RFC PATCH 1/2] s390/appldata: Emulate virtual timer with delayed work
In reply to Heiko CarstensThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] sysctl: race in timer deactivation allows permanent stall - [Medium] appldata: interval sysctl…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] sysctl: race in timer deactivation allows permanent stall
- [Medium] appldata: interval sysctl updates ignored due to schedule_delayed_work()
- [Medium] appldata: CPU offlining causes arithmetic underflow and premature execution --
commit 8e2ea62d851c3c34199ee4aaf1c6b3ad590787f1
Author: Heiko Carstens <[email protected]>
s390/appldata: Emulate virtual timer with delayed work
This commit emulates the virtual timer in appldata using delayed work to allow for the eventual removal of the vtimer infrastructure. The work is scheduled for the minimum wall-clock time until the configured CPU-time interval elapses, subsequently reading per-CPU statistics.
Quoted 4 lines
arch/s390/appldata/appldata_base.c +0
-0
[ ... ]
Quoted 4 lines
arch/s390/appldata/appldata_base.c +4
-0+static u64 appldata_total_cpu_time_ns (void ) +{ +u64 total =0 ; +int cpu;
arch/s390/appldata/appldata_base.c static LIST_HEAD(appldata_ops_list); · line 92
Quoted 10 lines
arch/s390/appldata/appldata_base.c +10
-0+ +for_each_online_cpu (cpu) { + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_USER ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_NICE ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_SYSTEM ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_IRQ ]; + total +=kcpustat_cpu (cpu).cpustat[CPUTIME_SOFTIRQ ]; + } +return total; +}
[Severity: Medium]
Is it safe to use for_each_online_cpu() here for a monotonic time counter?
If a CPU goes offline, won't the returned total time unexpectedly shrink? Later, when calculating the elapsed time:
elapsed = now - appldata_cputime_start;
If now shrinks to be smaller than appldata_cputime_start, elapsed will underflow to a massive unsigned value. This bypasses the elapsed < interval check in appldata_work_fn() and causes the callbacks to execute immediately.
[ ... ]
Quoted 5 lines
arch/s390/appldata/appldata_base.c +3
-1+static void appldata_schedule_work (u64 remaining) { -queue_work (appldata_wq, (struct work_struct *) data); +unsigned int ncpus =num_online_cpus (); +unsigned long delay =HZ /10 ;
arch/s390/appldata/appldata_base.c static LIST_HEAD(appldata_ops_list); · line 107
Quoted 10 lines
arch/s390/appldata/appldata_base.c +9
-0+ +/* +* At most ncpus CPUs consume CPU time simultaneously, so the +* minimum wall-clock time until the remaining CPU time elapses +* is remaining / ncpus. +* Make sure the work is not scheduled more than once per 100ms. +*/ + delay =max (delay,nsecs_to_jiffies (remaining / ncpus)); +schedule_delayed_work (&appldata_work, delay); }
[Severity: Medium]
Does this successfully update the interval if the work is already queued?
When a user updates the interval via /proc/sys/appldata/interval, the sysctl handler calls appldata_work_setup(APPLDATA_MOD_TIMER), which eventually calls this function.
Since schedule_delayed_work() is a no-op when a work item is already queued, it seems the requested interval change is ignored for the current cycle. Would mod_delayed_work() be required here instead to update the existing timer?
[ ... ]
Quoted 8 lines
arch/s390/appldata/appldata_base.c +0
-4@@ -215,12 +240,12 @@ appldata_timer_handler(const struct ctl_table *ctl, int write,if (rc <0 || !write)return rc; -spin_lock (&appldata_timer_lock); -if (timer_active) -__appldata_vtimer_setup (APPLDATA_ADD_TIMER ); -else
arch/s390/appldata/appldata_base.c appldata_timer_handler(const struct ctl_table *ctl, int write, · line 250
Quoted 10 lines
arch/s390/appldata/appldata_base.c +6
-2-__appldata_vtimer_setup (APPLDATA_DEL_TIMER ); -spin_unlock (&appldata_timer_lock); +if (timer_active) { + scoped_guard(mutex, &appldata_timer_lock) +appldata_work_setup (APPLDATA_ADD_TIMER ); + }else { +appldata_work_setup (APPLDATA_DEL_TIMER ); + }return 0 ; }
[Severity: Medium]
Is there a race condition here since the APPLDATA_DEL_TIMER path does not take the appldata_timer_lock?
If a sysctl write of 0 (DEL_TIMER) races with a concurrent sysctl write of 1 (ADD_TIMER), they could interleave. If DEL_TIMER executes locklessly and sets appldata_timer_active = 0 just before ADD_TIMER takes the lock and queues the work, DEL_TIMER might proceed to unconditionally cancel the newly queued work.
This would leave the timer in an active = 1 state without any delayed work queued, permanently breaking appldata gathering.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1