Skip to contentSkip to conversation

Expose do_timer CPU via sysctl to userspace as R/W

PATCH RESEND In review [email protected] 3 participants 4 messages Feb 19, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 Expose do_timer CPU via sysctl to userspace as R/W

From: Henrik Austad <[email protected]>

Looks like this got dropped by vger a few days ago, resending.

This allows everybody in a system to read which core is currently running do_timer() as well as letting root change this.

A few things to keep in mind

- This is intended as a debug-feature, except for testing whether or not the target CPU is present or not, the logic will not care -one- bit if moving the do_timer to that particular CPU is a good idea.
- It introduces more ifdeffery in the kernel.

On the positive side, it allows you to quickly change where the timer runs, which is nice if you have interrupts flooding the core where the timer is updated.

It is exposed via sysctl as

/proc/sys/kernel/time_tick_cpu
kernel:time_tick_cpu

Enable/disable via CONFIG_EXPOSE_TICK_CPU, disabling this will not lead to an increased image size.

CC Thomas Gleixner <[email protected]>
---
 include/linux/clocksource.h |   15 +++++++++++++++
 kernel/sysctl.c             |   13 +++++++++++++
 kernel/time/Kconfig         |   16 ++++++++++++++++
 kernel/time/tick-common.c   |   25 +++++++++++++++++++++++++
 kernel/time/tick-internal.h |    5 +++++
 kernel/time/timekeeping.c   |   42 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 116 insertions(+)
include/linux/clocksource.h +15 -0
index 67301a4..b61c893 100644
@@ -18,6 +18,10 @@
 #include <asm/div64.h>
 #include <asm/io.h>
 
+#ifdef CONFIG_EXPOSE_TICK_CPU
+#include <linux/sysctl.h>
+#endif
+
 /* clocksource cycle base type */
 typedef u64 cycle_t;
 struct clocksource;
@@ -359,3 +363,14 @@ static inline void clocksource_of_init(void) {}
 #endif
 
 #endif /* _LINUX_CLOCKSOURCE_H */
+#ifdef CONFIG_EXPOSE_TICK_CPU
+/*
+ * expose the CPU that handles the timer-tick and allow userspace to change it.
+ */
+extern int sysctl_tick_do_timer_cpu;
+extern int timekeeping_expose_timer_cpu(struct ctl_table *table,
+					int write,
+					void __user *buffer,
+					size_t *lenp,
+					loff_t *ppos);
+#endif
kernel/sysctl.c +13 -0
index 49e13e1..c6613f5 100644
@@ -64,6 +64,10 @@
 #include <linux/sched/sysctl.h>
 #include <linux/kexec.h>
 
+#ifdef CONFIG_EXPOSE_TICK_CPU
+#include <linux/clocksource.h>
+#endif
+
 #include <asm/uaccess.h>
 #include <asm/processor.h>
 
@@ -424,6 +428,15 @@ static struct ctl_table kern_table[] = {
 		.mode		= 0644,
 		.proc_handler	= sched_rr_handler,
 	},
+#ifdef CONFIG_EXPOSE_TICK_CPU
+	{
+		.procname  = "timer_tick_cpu",
+		.data      = &sysctl_tick_do_timer_cpu,
+		.maxlen	   = sizeof(int),
+		.mode	   = 0644,
+		.proc_handler	= timekeeping_expose_timer_cpu,
+	},
+#endif
 #ifdef CONFIG_SCHED_AUTOGROUP
 	{
 		.procname	= "sched_autogroup_enabled",
kernel/time/Kconfig +16 -0
index 3ce6e8c..45460de 100644
@@ -202,3 +202,19 @@ config HIGH_RES_TIMERS
 
 endmenu
 endif
+
+config EXPOSE_TICK_CPU
+         bool "Expose which CPU is responsible for timer-tick"
+	 default False
+	 help
+	   This option exposes which CPU is responsible from the
+	   periodic timer-tick. It also alows userspace to change the
+	   CPU, letting userspace move the peridic update away from a
+	   CPU.
+
+	   Note: if the CPU goes offline, the underlying machinery will
+	   move the handler away, and once the CPU is brought back
+	   online, there is absolutely *no* guarantee that the handling
+	   will be moved back to this CPU.
+
+
kernel/time/tick-common.c +25 -0
index 20b2fe3..54dbd7f 100644
@@ -347,6 +347,31 @@ void tick_handover_do_timer(int *cpup)
 	}
 }
 
+#ifdef CONFIG_EXPOSE_TICK_CPU
+/* part of the expose-tick sysctl interface for r/w which core to run
+ * the do_timer update */
+int tick_expose_cpu(void)
+{
+	return tick_do_timer_cpu;
+}
+int tick_set_cpu(int new_cpu)
+{
+	int old_cpu, current_cpu;
+	if (!cpu_present(new_cpu)) {
+		pr_err("Illegal value for tick_do_timer_cpu, requested CPU (%d) not present\n",
+			new_cpu);
+		return -EINVAL;
+	}
+
+	current_cpu = tick_do_timer_cpu;
+	if ((old_cpu = cmpxchg(&tick_do_timer_cpu, tick_do_timer_cpu, new_cpu)) != current_cpu) {
+		pr_err("Error updating timer-cpu!\n");
+		return -EBADE;
+	}
+	return 0;
+}
+#endif	/* CONFIG_EXPOSE_TICK_CPU */
+
 /*
  * Shutdown an event device on a given cpu:
  *
kernel/time/tick-internal.h +5 -0
index 8329669..6c960d6 100644
@@ -154,5 +154,10 @@ static inline int tick_device_is_functional(struct clock_event_device *dev)
 
 #endif
 
+#ifdef CONFIG_EXPOSE_TICK_CPU
+extern int tick_expose_cpu(void);
+extern int tick_set_cpu(int new_cpu);
+#endif
+
 extern void do_timer(unsigned long ticks);
 extern void update_wall_time(void);
kernel/time/timekeeping.c +42 -0

57 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 4 lines · On Wed, 19 Feb 2014, [email protected] wrote:

From: Henrik Austad <[email protected]>

Looks like this got dropped by vger a few days ago, resending.

Quoted 8 lines

This allows everybody in a system to read which core is currently running do_timer() as well as letting root change this.

A few things to keep in mind

- This is intended as a debug-feature, except for testing whether or not the target CPU is present or not, the logic will not care -one- bit if moving the do_timer to that particular CPU is a good idea.

Well at least it must check that the cpu is online and neither in nohz idle nor nohz full mode.

And we probably want to have that in sysfs with two files:

timekeeping/current_cpu
timekeeping/forced_cpu

The latter contains -1 when the system boots and if you write to it, the duty gets hard assigned to that core, which in turn makes it blocked from NOHZ idle and NOHZ full modes. If the core goes offline, then the value must got back to -1. Writing -1 to it undoes the hard assignment.

Quoted 1 line

- It introduces more ifdeffery in the kernel.

There is no reason to do that.

Thanks,

tglx

#2 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 7 lines · On Wed, 19 Feb 2014, [email protected] wrote:

From: Henrik Austad <[email protected]>

Looks like this got dropped by vger a few days ago, resending.

This allows everybody in a system to read which core is currently running do_timer() as well as letting root change this.

Quoted 8 lines
Quoted 5 lines

A few things to keep in mind

- This is intended as a debug-feature, except for testing whether or not the target CPU is present or not, the logic will not care -one- bit if moving the do_timer to that particular CPU is a good idea.

Well at least it must check that the cpu is online and neither in nohz idle nor nohz full mode.

The online-part should be taken care of by the cpu_present(), no?

But the nohz idle/nohz full should be checked, I agree.

Quoted 4 lines

And we probably want to have that in sysfs with two files:

timekeeping/current_cpu
timekeeping/forced_cpu

Fair enough, I can add that

Quoted 5 lines

The latter contains -1 when the system boots and if you write to it, the duty gets hard assigned to that core, which in turn makes it blocked from NOHZ idle and NOHZ full modes. If the core goes offline, then the value must got back to -1. Writing -1 to it undoes the hard assignment.

Yeah, sounds like a good approach.

Quoted 3 lines
Quoted 1 line

- It introduces more ifdeffery in the kernel.

There is no reason to do that.

probably not, as you can see, I wrapped everything in CONFIG_EXPOSE_TICK_CPU and added an option in Kconfig. Just drop all of that?

Thanks for the feedback! :)

Signature · 1 line
Henrik Austad
#3 lore raw
Quoted 1 line · Henrik Austad

The online-part should be taken care of by the cpu_present(), no?

No, cpu_present() only means there is a physical CPU present. It might be offline.

See the comment in include/linux/cpumask.h