Skip to contentSkip to conversation

s390: Remove vtimer infrastructure

PATCH In review Heiko Carstens 3 participants 7 messages Oct 6 – Oct 8 s390

lore
Share & tools
lore

Heiko Carstens

author maintainer
#0 lore raw

v2 cover s390: Remove vtimer infrastructure

v2:
- Use for_each_possible_cpu() instead of for_each_online_cpu() in appldata_total_cpu_time_ns() to make sure the reported cpu time is monotonic increasing [Sashiko]

- Rework locking in __appldata_timer_setup() to make sure appldata_timer_active always reflects if a work is still active or not [Sashiko]

- Use mod_delayed_work() [Sashiko]

- Add cpu hotplug handler to recalculate interval if a new CPU is added, avoiding potential long delays between an expired timer and when the callback is run

- Avoid using scoped_guard(), etc. - keep coding style consistent within the file

v1:
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 | 171 ++++++++++++++---------
 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, 119 insertions(+), 301 deletions(-)
 delete mode 100644 arch/s390/include/asm/vtimer.h
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH v2 1/2 s390/appldata: Emulate virtual timer with delayed work

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.

This is slightly more expensive, but allows to subsequently remove the entire vtimer infrastructure.

---
 arch/s390/appldata/appldata_base.c | 171 ++++++++++++++++++-----------
 1 file changed, 107 insertions(+), 64 deletions(-)
arch/s390/appldata/appldata_base.c +107 -64
index 9cba4633c3f3..836c51da18f4 100644
@@ -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,14 +76,34 @@ 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_possible_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));
+	mod_delayed_work(system_percpu_wq, &appldata_work, delay);
 }
 
 /*
@@ -106,9 +113,27 @@ static void appldata_timer_function(unsigned long data)
  */
 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;
+	bool expired = false;
 
+	now = appldata_total_cpu_time_ns();
+	mutex_lock(&appldata_timer_lock);
+	if (appldata_timer_active) {
+		interval = appldata_interval;
+		elapsed = now - appldata_cputime_start;
+		if (elapsed < interval) {
+			appldata_schedule_work(interval - elapsed);
+		} else {
+			appldata_cputime_start = now;
+			appldata_schedule_work(interval);
+			expired = true;
+		}
+	}
+	mutex_unlock(&appldata_timer_lock);
+	if (!expired)
+		return;
 	mutex_lock(&appldata_ops_mutex);
 	list_for_each(lh, &appldata_ops_list) {
 		ops = list_entry(lh, struct appldata_ops, list);
@@ -162,33 +187,52 @@ int appldata_diag(char record_nr, u16 function, unsigned long buffer,
 #define APPLDATA_MOD_TIMER	2
 
 /*
- * __appldata_vtimer_setup()
+ * __appldata_timer_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_timer_setup(int cmd)
 {
-	u64 timer_interval = (u64) appldata_interval * 1000 * TOD_MICRO;
+	static DEFINE_MUTEX(appldata_cancel_lock);
+	u64 now, elapsed, remaining;
 
 	switch (cmd) {
 	case APPLDATA_ADD_TIMER:
-		if (appldata_timer_active)
-			break;
-		appldata_timer.expires = timer_interval;
-		add_virt_timer_periodic(&appldata_timer);
-		appldata_timer_active = 1;
+		mutex_lock(&appldata_cancel_lock);
+		mutex_lock(&appldata_timer_lock);
+		if (!appldata_timer_active) {
+			appldata_cputime_start = appldata_total_cpu_time_ns();
+			appldata_schedule_work(appldata_interval);
+			appldata_timer_active = 1;
+		}
+		mutex_unlock(&appldata_timer_lock);
+		mutex_unlock(&appldata_cancel_lock);
 		break;
 	case APPLDATA_DEL_TIMER:
-		del_virt_timer(&appldata_timer);
-		if (!appldata_timer_active)
+		mutex_lock(&appldata_cancel_lock);
+		mutex_lock(&appldata_timer_lock);
+		if (!appldata_timer_active) {
+			mutex_unlock(&appldata_timer_lock);
+			mutex_unlock(&appldata_cancel_lock);
 			break;
+		}
 		appldata_timer_active = 0;
+		mutex_unlock(&appldata_timer_lock);
+		cancel_delayed_work_sync(&appldata_work);
+		mutex_unlock(&appldata_cancel_lock);
 		break;
 	case APPLDATA_MOD_TIMER:
-		if (!appldata_timer_active)
-			break;
-		mod_virt_timer_periodic(&appldata_timer, timer_interval);
+		mutex_lock(&appldata_timer_lock);
+		if (appldata_timer_active) {
+			now = appldata_total_cpu_time_ns();
+			elapsed = now - appldata_cputime_start;
+			remaining = appldata_interval - elapsed;
+			if (elapsed >= appldata_interval)
+				remaining = 1;
+			appldata_schedule_work(remaining);
+		}
+		mutex_unlock(&appldata_timer_lock);
+		break;
 	}
 }
 
@@ -215,12 +259,10 @@ 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);
+		__appldata_timer_setup(APPLDATA_ADD_TIMER);
 	else
-		__appldata_vtimer_setup(APPLDATA_DEL_TIMER);
-	spin_unlock(&appldata_timer_lock);
+		__appldata_timer_setup(APPLDATA_DEL_TIMER);
 	return 0;
 }
 
@@ -234,7 +276,7 @@ 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 = appldata_interval / NSEC_PER_MSEC;
 	int rc;
 	struct ctl_table ctl_entry = {
 		.procname	= ctl->procname,
@@ -247,10 +289,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);
+	mutex_lock(&appldata_timer_lock);
+	appldata_interval = interval * NSEC_PER_MSEC;
+	mutex_unlock(&appldata_timer_lock);
+	__appldata_timer_setup(APPLDATA_MOD_TIMER);
 	return 0;
 }
 
@@ -392,19 +434,20 @@ void appldata_unregister_ops(struct appldata_ops *ops)
 
 /******************************* init / exit *********************************/
 
-/*
- * appldata_init()
- *
- * init timer, register /proc entries
- */
+static int appldata_cpu_online(unsigned int cpu)
+{
+	__appldata_timer_setup(APPLDATA_MOD_TIMER);
+	return 0;
+}
+
 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;
+	int rc;
+
+	rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "s390/appldata:online",
+				       appldata_cpu_online, NULL);
+	if (rc < 0)
+		return rc;
 	register_sysctl(appldata_proc_name, appldata_table);
 	return 0;
 }
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH v2 2/2 s390/vtime: Remove vtimer infrastructure

In reply to Heiko Carstens

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
index da116a93d3b6..22e494b22192 100644
@@ -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
deleted file mode 100644
index e601adaa6320..000000000000
@@ -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
index 416650ae4871..4c73d2528fcb 100644
@@ -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
index 32499cad86f0..f7c262a17c03 100644
@@ -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
index 2b989bebd220..dd8a88cc9c4b 100644
@@ -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
#2 lore raw

Re: [PATCH v2 1/2] s390/appldata: Emulate virtual timer with delayed work

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

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.

This is slightly more expensive, but allows to subsequently remove the entire vtimer infrastructure.

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=1
#6 lore raw

Re: [PATCH v2 2/2] s390/vtime: Remove vtimer infrastructure

In reply to Heiko Carstens
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 3 lines

The only user is gone - remove the vtimer infrastructure.

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
Reviewed-by Tested-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] s390/appldata: Emulate virtual timer with delayed work

In reply to Heiko Carstens
Quoted 7 lines · On Tue, 6 Oct 2026 16:01:12 +0200 Heiko Carstens <[email protected]> wrote:

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.

Quoted 7 lines

This is slightly more expensive, but allows to subsequently remove the entire vtimer infrastructure.

---
 arch/s390/appldata/appldata_base.c | 171 ++++++++++++++++++-----------
 1 file changed, 107 insertions(+), 64 deletions(-)

Nice improvement, with minimum wall-clock interval 100ms. Specifying a 1ms CPU-time interval was possible, but did not make much sense in practice. On very idle systems, the resulting wall-clock interval would also have been around 100ms, but as soon as they do some work, it would have become far too frequent.

Also tested it, and on a busy system there is now no noticeable difference for interval values 1-100, while idle systems still behave like before.

author maintainer
#4 lore raw

Re: [PATCH v2 1/2] s390/appldata: Emulate virtual timer with delayed work

Quoted 24 lines · Gerald Schaefer
Quoted 7 lines · On Tue, 6 Oct 2026 16:01:12 +0200 Heiko Carstens <[email protected]> wrote:

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.

Quoted 7 lines

This is slightly more expensive, but allows to subsequently remove the entire vtimer infrastructure.

---
 arch/s390/appldata/appldata_base.c | 171 ++++++++++++++++++-----------
 1 file changed, 107 insertions(+), 64 deletions(-)

Nice improvement, with minimum wall-clock interval 100ms. Specifying a 1ms CPU-time interval was possible, but did not make much sense in practice. On very idle systems, the resulting wall-clock interval would also have been around 100ms, but as soon as they do some work, it would have become far too frequent.

Quoted 5 lines

Also tested it, and on a busy system there is now no noticeable difference for interval values 1-100, while idle systems still behave like before.

Thanks a lot! Applied :)