Skip to contentSkip to conversation

: timers: Enabling timer migration to cpu0

RFC PATCH In review Arun R Bharadwaj 2 participants 5 messages Sep 16, 2008

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

RFC PATCH 1/1 : timers: Enabling timer migration to cpu0

The implentation details of this as follows:
A sysfs entry is created at /sys/devices/system/cpu/cpuX/timer_migration. By setting this to 1, timer migration is enabled for that cpu.
An important thing to note here is cpu-pinned timers. Timers can be pinned to a particular cpu using the function add_timer_on(). So, such timers should not be migrated. Since the last 3 bits of the tvec_base is guaranteed to be 0, and since the last bit is being used to indicate deferrable timers, I'm using the second last bit to indicate cpu-pinned timers. The implementation of functions to manage the TBASE_PINNED_FLAG is similar to those which manage the TBASE_DEFERRABLE_FLAG.

---

drivers/base/cpu.c +21 -0
Index: linux-2.6.26/drivers/base/cpu.c
===================================================================
@@ -13,6 +13,7 @@
 
 #include "base.h"
 
+DEFINE_PER_CPU(int, enable_timer_migration);
 struct sysdev_class cpu_sysdev_class = {
 	.name = "cpu",
 };
@@ -20,6 +21,21 @@
 
 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
 
+#ifdef CONFIG_TIMER_MIGRATION
+static ssize_t timer_migration_show(struct sys_device *dev, char *buf)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	return sprintf(buf, "%u\n", per_cpu(enable_timer_migration, cpu->sysdev.id));
+}
+static ssize_t timer_migration_store(struct sys_device *dev, const char *buf, size_t count)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	per_cpu(enable_timer_migration, cpu->sysdev.id) = buf[0] - 48;
+	return count;
+}
+static SYSDEV_ATTR(timer_migration, 0666, timer_migration_show, timer_migration_store);
+#endif
+
 #ifdef CONFIG_HOTPLUG_CPU
 static ssize_t show_online(struct sys_device *dev, char *buf)
 {
@@ -175,6 +191,11 @@
 	if (!error)
 		error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+	if (!error)
+		error = sysdev_create_file(&cpu->sysdev, &attr_timer_migration);
+#endif
 	return error;
 }
 
init/Kconfig +6 -0
Index: linux-2.6.26/init/Kconfig
===================================================================
@@ -923,3 +923,9 @@
 	  designed for best read-side performance on non-realtime
 	  systems.  Classic RCU is the default.  Note that the
 	  PREEMPT_RCU symbol is used to select/deselect this option.
+
+config TIMER_MIGRATION
+	bool
+	default y
+	help
+	  This option enables migration of non cpu-affine timers to cpu0.
include/linux/timer.h +5 -0
Index: linux-2.6.26/include/linux/timer.h
===================================================================
@@ -5,6 +5,7 @@
 #include <linux/ktime.h>
 #include <linux/stddef.h>
 #include <linux/debugobjects.h>
+#include <linux/percpu.h>
 
 struct tvec_base;
 
@@ -187,3 +188,7 @@
 unsigned long round_jiffies_relative(unsigned long j);
 
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+DECLARE_PER_CPU(int, enable_timer_migration);
+#endif
kernel/timer.c +22 -3
Index: linux-2.6.26/kernel/timer.c
===================================================================
@@ -37,6 +37,7 @@
 #include <linux/delay.h>
 #include <linux/tick.h>
 #include <linux/kallsyms.h>
+#include <linux/timer.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -87,8 +88,9 @@
  * the new flag to indicate whether the timer is deferrable
  */
 #define TBASE_DEFERRABLE_FLAG		(0x1)
+#define TBASE_PINNED_FLAG               (0x2)
 
-/* Functions below help us manage 'deferrable' flag */
+/* Functions below help us manage 'deferrable' flag and 'cpu-pinned-timer' flag */
 static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
 {
 	return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);
@@ -96,7 +98,7 @@
 
 static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
 {
-	return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
+	return (struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG & ~TBASE_PINNED_FLAG);
 }
 
 static inline void timer_set_deferrable(struct timer_list *timer)
@@ -105,11 +107,21 @@
 				       TBASE_DEFERRABLE_FLAG));
 }
 
+static inline unsigned int tbase_get_pinned(struct tvec_base *base)
+{
+	return ((unsigned int)(unsigned long)base & TBASE_PINNED_FLAG);
+}
+
 static inline void
 timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
 {
 	timer->base = (struct tvec_base *)((unsigned long)(new_base) |
-				      tbase_get_deferrable(timer->base));
+		   tbase_get_deferrable(timer->base) | tbase_get_pinned(timer->base));
+}
+
+static inline void timer_set_pinned(struct timer_list *timer)
+{
+	timer->base = ((struct tvec_base *)((unsigned long)(timer->base) | TBASE_PINNED_FLAG));
 }
 
 /**
@@ -540,6 +552,12 @@
 
 	new_base = __get_cpu_var(tvec_bases);
 
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration) && !tbase_get_pinned(timer->base)) {
+		new_base = per_cpu(tvec_bases, 0);
+	}
+	#endif
+
 	if (base != new_base) {
 		/*
 		 * We are trying to schedule the timer on the local CPU.
@@ -579,6 +597,7 @@
 	struct tvec_base *base = per_cpu(tvec_bases, cpu);
 	unsigned long flags;
 
+	timer_set_pinned(timer);
 	timer_stats_timer_set_start_info(timer);
 	BUG_ON(timer_pending(timer) || !timer->function);
 	spin_lock_irqsave(&base->lock, flags);
kernel/hrtimer.c +6 -0
Index: linux-2.6.26/kernel/hrtimer.c
===================================================================
@@ -200,6 +200,12 @@
 	struct hrtimer_cpu_base *new_cpu_base;
 
 	new_cpu_base = &__get_cpu_var(hrtimer_bases);
+
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration))
+		new_cpu_base = &per_cpu(hrtimer_bases, 0);
+	#endif
+
 	new_base = &new_cpu_base->clock_base[base->index];
 
 	if (base != new_base) {
#1 lore raw
Quoted 1 line · Arun R Bharadwaj

The implentation details of this as follows:

80 char lines please

Quoted 6 lines

A sysfs entry is created
at /sys/devices/system/cpu/cpuX/timer_migration. By setting this to 1, timer migration is enabled for that cpu.
An important thing to note here is cpu-pinned timers. Timers can be pinned to a particular cpu using the function add_timer_on(). So, such timers should not be migrated.

You utterly fail to mention hrtimers even though you do touch those as well, what's more - there are hrtimers that need to be run on the cpu that queues them - you don't seem to provide anything like that.

Quoted 8 lines

Since the last 3 bits of the tvec_base is guaranteed to be 0, and since the last bit is being used to indicate deferrable timers, I'm using the second last bit to indicate cpu-pinned timers. The implementation of functions to manage the TBASE_PINNED_FLAG is similar to those which manage the TBASE_DEFERRABLE_FLAG.

---

please add --show-c-function to your diff

Quoted 20 lines

Index: linux-2.6.26/drivers/base/cpu.c
===================================================================

drivers/base/cpu.c +5 -0
@@ -13,6 +13,7 @@
 
 #include "base.h"
 
+DEFINE_PER_CPU(int, enable_timer_migration);
 struct sysdev_class cpu_sysdev_class = {
 	.name = "cpu",
 };
@@ -20,6 +21,21 @@
 
 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
 
+#ifdef CONFIG_TIMER_MIGRATION
+static ssize_t timer_migration_show(struct sys_device *dev, char *buf)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);

drivers/base/cpu.c line 37

Quoted 10 lines
drivers/base/cpu.c +10 -0
+	return sprintf(buf, "%u\n", per_cpu(enable_timer_migration, cpu->sysdev.id));
+}
+static ssize_t timer_migration_store(struct sys_device *dev, const char *buf, size_t count)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	per_cpu(enable_timer_migration, cpu->sysdev.id) = buf[0] - 48;
+	return count;
+}
+static SYSDEV_ATTR(timer_migration, 0666, timer_migration_show, timer_migration_store);
+#endif

Why bother with the CONFIG_ variable - its so little code and it adds to the Kconfig space.

Quoted 20 lines
drivers/base/cpu.c +5 -0
 #ifdef CONFIG_HOTPLUG_CPU
 static ssize_t show_online(struct sys_device *dev, char *buf)
 {
@@ -175,6 +191,11 @@
 	if (!error)
 		error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+	if (!error)
+		error = sysdev_create_file(&cpu->sysdev, &attr_timer_migration);
+#endif
 	return error;
 }
 

Index: linux-2.6.26/init/Kconfig
===================================================================

init/Kconfig line 931

Quoted 9 lines
init/Kconfig +6 -0
@@ -923,3 +923,9 @@
 	  designed for best read-side performance on non-realtime
 	  systems.  Classic RCU is the default.  Note that the
 	  PREEMPT_RCU symbol is used to select/deselect this option.
+
+config TIMER_MIGRATION
+	bool
+	default y
+	help
+	  This option enables migration of non cpu-affine timers to cpu0.

this will earn you a place in the hall of kconfig-help-text of shame.

Quoted 44 lines

Index: linux-2.6.26/include/linux/timer.h
===================================================================

include/linux/timer.h +5 -0
@@ -5,6 +5,7 @@
 #include <linux/ktime.h>
 #include <linux/stddef.h>
 #include <linux/debugobjects.h>
+#include <linux/percpu.h>
 
 struct tvec_base;
 
@@ -187,3 +188,7 @@
 unsigned long round_jiffies_relative(unsigned long j);
 
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+DECLARE_PER_CPU(int, enable_timer_migration);
+#endif

Index: linux-2.6.26/kernel/timer.c
===================================================================

kernel/timer.c +3 -1
@@ -37,6 +37,7 @@
 #include <linux/delay.h>
 #include <linux/tick.h>
 #include <linux/kallsyms.h>
+#include <linux/timer.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -87,8 +88,9 @@
  * the new flag to indicate whether the timer is deferrable
  */
 #define TBASE_DEFERRABLE_FLAG		(0x1)
+#define TBASE_PINNED_FLAG               (0x2)
 
-/* Functions below help us manage 'deferrable' flag */
+/* Functions below help us manage 'deferrable' flag and 'cpu-pinned-timer' flag */
 static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
 {
 	return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);

kernel/timer.c line 102

Quoted 6 lines
kernel/timer.c +1 -1
@@ -96,7 +98,7 @@
 
 static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
 {
-	return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
+	return (struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG & ~TBASE_PINNED_FLAG);
 }

I'm pretty sure this line is too long.

kernel/timer.c line 113

Quoted 9 lines
kernel/timer.c +4 -0
 static inline void timer_set_deferrable(struct timer_list *timer)
@@ -105,11 +107,21 @@
 				       TBASE_DEFERRABLE_FLAG));
 }
 
+static inline unsigned int tbase_get_pinned(struct tvec_base *base)
+{
+	return ((unsigned int)(unsigned long)base & TBASE_PINNED_FLAG);
+}

Why explicitly cast to unsigned int?

kernel/timer.c line 125

Quoted 12 lines
kernel/timer.c +6 -1
 static inline void
 timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
 {
 	timer->base = (struct tvec_base *)((unsigned long)(new_base) |
-				      tbase_get_deferrable(timer->base));
+		   tbase_get_deferrable(timer->base) | tbase_get_pinned(timer->base));
+}
+
+static inline void timer_set_pinned(struct timer_list *timer)
+{
+	timer->base = ((struct tvec_base *)((unsigned long)(timer->base) | TBASE_PINNED_FLAG));
 }

<80 ?

kernel/timer.c line 559

Quoted 10 lines
kernel/timer.c +5 -0
 /**
@@ -540,6 +552,12 @@
 
 	new_base = __get_cpu_var(tvec_bases);
 
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration) && !tbase_get_pinned(timer->base)) {
+		new_base = per_cpu(tvec_bases, 0);
+	}
+	#endif

We don't indent preprocessor directives like that, also we don't use braces on single statement blocks.

Quoted 16 lines
kernel/timer.c +1 -0
 	if (base != new_base) {
 		/*
 		 * We are trying to schedule the timer on the local CPU.
@@ -579,6 +597,7 @@
 	struct tvec_base *base = per_cpu(tvec_bases, cpu);
 	unsigned long flags;
 
+	timer_set_pinned(timer);
 	timer_stats_timer_set_start_info(timer);
 	BUG_ON(timer_pending(timer) || !timer->function);
 	spin_lock_irqsave(&base->lock, flags);

Index: linux-2.6.26/kernel/hrtimer.c
===================================================================

kernel/hrtimer.c line 207

Quoted 8 lines
kernel/hrtimer.c +5 -0
@@ -200,6 +200,12 @@
 	struct hrtimer_cpu_base *new_cpu_base;
 
 	new_cpu_base = &__get_cpu_var(hrtimer_bases);
+
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration))
+		new_cpu_base = &per_cpu(hrtimer_bases, 0);
+	#endif

idem

and NAK - this will actually break stuff.

Quoted 3 lines
kernel/hrtimer.c +0 -0
 	new_base = &new_cpu_base->clock_base[base->index];
 
 	if (base != new_base) {
#2 lore raw
In reply to Arun R Bharadwaj
Quoted 1 line · Arun R Bharadwaj

The implentation details of this as follows:

80 char lines please

Quoted 6 lines

A sysfs entry is created
at /sys/devices/system/cpu/cpuX/timer_migration. By setting this to 1, timer migration is enabled for that cpu.
An important thing to note here is cpu-pinned timers. Timers can be pinned to a particular cpu using the function add_timer_on(). So, such timers should not be migrated.

You utterly fail to mention hrtimers even though you do touch those as well, what's more - there are hrtimers that need to be run on the cpu that queues them - you don't seem to provide anything like that.

Quoted 8 lines

Since the last 3 bits of the tvec_base is guaranteed to be 0, and since the last bit is being used to indicate deferrable timers, I'm using the second last bit to indicate cpu-pinned timers. The implementation of functions to manage the TBASE_PINNED_FLAG is similar to those which manage the TBASE_DEFERRABLE_FLAG.

---

please add --show-c-function to your diff

Quoted 20 lines

Index: linux-2.6.26/drivers/base/cpu.c
===================================================================

drivers/base/cpu.c +5 -0
@@ -13,6 +13,7 @@
 
 #include "base.h"
 
+DEFINE_PER_CPU(int, enable_timer_migration);
 struct sysdev_class cpu_sysdev_class = {
 	.name = "cpu",
 };
@@ -20,6 +21,21 @@
 
 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
 
+#ifdef CONFIG_TIMER_MIGRATION
+static ssize_t timer_migration_show(struct sys_device *dev, char *buf)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);

drivers/base/cpu.c line 37

Quoted 10 lines
drivers/base/cpu.c +10 -0
+	return sprintf(buf, "%u\n", per_cpu(enable_timer_migration, cpu->sysdev.id));
+}
+static ssize_t timer_migration_store(struct sys_device *dev, const char *buf, size_t count)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	per_cpu(enable_timer_migration, cpu->sysdev.id) = buf[0] - 48;
+	return count;
+}
+static SYSDEV_ATTR(timer_migration, 0666, timer_migration_show, timer_migration_store);
+#endif

Why bother with the CONFIG_ variable - its so little code and it adds to the Kconfig space.

Quoted 20 lines
drivers/base/cpu.c +5 -0
 #ifdef CONFIG_HOTPLUG_CPU
 static ssize_t show_online(struct sys_device *dev, char *buf)
 {
@@ -175,6 +191,11 @@
 	if (!error)
 		error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+	if (!error)
+		error = sysdev_create_file(&cpu->sysdev, &attr_timer_migration);
+#endif
 	return error;
 }
 

Index: linux-2.6.26/init/Kconfig
===================================================================

init/Kconfig line 931

Quoted 9 lines
init/Kconfig +6 -0
@@ -923,3 +923,9 @@
 	  designed for best read-side performance on non-realtime
 	  systems.  Classic RCU is the default.  Note that the
 	  PREEMPT_RCU symbol is used to select/deselect this option.
+
+config TIMER_MIGRATION
+	bool
+	default y
+	help
+	  This option enables migration of non cpu-affine timers to cpu0.

this will earn you a place in the hall of kconfig-help-text of shame.

Quoted 44 lines

Index: linux-2.6.26/include/linux/timer.h
===================================================================

include/linux/timer.h +5 -0
@@ -5,6 +5,7 @@
 #include <linux/ktime.h>
 #include <linux/stddef.h>
 #include <linux/debugobjects.h>
+#include <linux/percpu.h>
 
 struct tvec_base;
 
@@ -187,3 +188,7 @@
 unsigned long round_jiffies_relative(unsigned long j);
 
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+DECLARE_PER_CPU(int, enable_timer_migration);
+#endif

Index: linux-2.6.26/kernel/timer.c
===================================================================

kernel/timer.c +3 -1
@@ -37,6 +37,7 @@
 #include <linux/delay.h>
 #include <linux/tick.h>
 #include <linux/kallsyms.h>
+#include <linux/timer.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -87,8 +88,9 @@
  * the new flag to indicate whether the timer is deferrable
  */
 #define TBASE_DEFERRABLE_FLAG		(0x1)
+#define TBASE_PINNED_FLAG               (0x2)
 
-/* Functions below help us manage 'deferrable' flag */
+/* Functions below help us manage 'deferrable' flag and 'cpu-pinned-timer' flag */
 static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
 {
 	return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);

kernel/timer.c line 102

Quoted 6 lines
kernel/timer.c +1 -1
@@ -96,7 +98,7 @@
 
 static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
 {
-	return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
+	return (struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG & ~TBASE_PINNED_FLAG);
 }

I'm pretty sure this line is too long.

kernel/timer.c line 113

Quoted 9 lines
kernel/timer.c +4 -0
 static inline void timer_set_deferrable(struct timer_list *timer)
@@ -105,11 +107,21 @@
 				       TBASE_DEFERRABLE_FLAG));
 }
 
+static inline unsigned int tbase_get_pinned(struct tvec_base *base)
+{
+	return ((unsigned int)(unsigned long)base & TBASE_PINNED_FLAG);
+}

Why explicitly cast to unsigned int?

kernel/timer.c line 125

Quoted 12 lines
kernel/timer.c +6 -1
 static inline void
 timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
 {
 	timer->base = (struct tvec_base *)((unsigned long)(new_base) |
-				      tbase_get_deferrable(timer->base));
+		   tbase_get_deferrable(timer->base) | tbase_get_pinned(timer->base));
+}
+
+static inline void timer_set_pinned(struct timer_list *timer)
+{
+	timer->base = ((struct tvec_base *)((unsigned long)(timer->base) | TBASE_PINNED_FLAG));
 }

<80 ?

kernel/timer.c line 559

Quoted 10 lines
kernel/timer.c +5 -0
 /**
@@ -540,6 +552,12 @@
 
 	new_base = __get_cpu_var(tvec_bases);
 
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration) && !tbase_get_pinned(timer->base)) {
+		new_base = per_cpu(tvec_bases, 0);
+	}
+	#endif

We don't indent preprocessor directives like that, also we don't use braces on single statement blocks.

Quoted 16 lines
kernel/timer.c +1 -0
 	if (base != new_base) {
 		/*
 		 * We are trying to schedule the timer on the local CPU.
@@ -579,6 +597,7 @@
 	struct tvec_base *base = per_cpu(tvec_bases, cpu);
 	unsigned long flags;
 
+	timer_set_pinned(timer);
 	timer_stats_timer_set_start_info(timer);
 	BUG_ON(timer_pending(timer) || !timer->function);
 	spin_lock_irqsave(&base->lock, flags);

Index: linux-2.6.26/kernel/hrtimer.c
===================================================================

kernel/hrtimer.c line 207

Quoted 8 lines
kernel/hrtimer.c +5 -0
@@ -200,6 +200,12 @@
 	struct hrtimer_cpu_base *new_cpu_base;
 
 	new_cpu_base = &__get_cpu_var(hrtimer_bases);
+
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration))
+		new_cpu_base = &per_cpu(hrtimer_bases, 0);
+	#endif

idem

and NAK - this will actually break stuff.

Quoted 3 lines
kernel/hrtimer.c +0 -0
 	new_base = &new_cpu_base->clock_base[base->index];
 
 	if (base != new_base) {
#3 lore raw
Quoted 234 lines · Peter Zijlstra
Quoted 2 lines · Arun R Bharadwaj

The implentation details of this as follows:

80 char lines please

Quoted 7 lines

A sysfs entry is created
at /sys/devices/system/cpu/cpuX/timer_migration. By setting this to 1, timer migration is enabled for that cpu.
An important thing to note here is cpu-pinned timers. Timers can be pinned to a particular cpu using the function add_timer_on(). So, such timers should not be migrated.

You utterly fail to mention hrtimers even though you do touch those as well, what's more - there are hrtimers that need to be run on the cpu that queues them - you don't seem to provide anything like that.

Quoted 9 lines

Since the last 3 bits of the tvec_base is guaranteed to be 0, and since the last bit is being used to indicate deferrable timers, I'm using the second last bit to indicate cpu-pinned timers. The implementation of functions to manage the TBASE_PINNED_FLAG is similar to those which manage the TBASE_DEFERRABLE_FLAG.

---

please add --show-c-function to your diff

Quoted 21 lines

Index: linux-2.6.26/drivers/base/cpu.c
===================================================================

drivers/base/cpu.c +6 -0
@@ -13,6 +13,7 @@
 
 #include "base.h"
 
+DEFINE_PER_CPU(int, enable_timer_migration);
 struct sysdev_class cpu_sysdev_class = {
 	.name = "cpu",
 };
@@ -20,6 +21,21 @@
 
 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
 
+#ifdef CONFIG_TIMER_MIGRATION
+static ssize_t timer_migration_show(struct sys_device *dev, char *buf)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	return sprintf(buf, "%u\n", per_cpu(enable_timer_migration, cpu->sysdev.id));

drivers/base/cpu.c line 38

Quoted 10 lines
drivers/base/cpu.c +9 -0
+}
+static ssize_t timer_migration_store(struct sys_device *dev, const char *buf, size_t count)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	per_cpu(enable_timer_migration, cpu->sysdev.id) = buf[0] - 48;
+	return count;
+}
+static SYSDEV_ATTR(timer_migration, 0666, timer_migration_show, timer_migration_store);
+#endif
 

Why bother with the CONFIG_ variable - its so little code and it adds to the Kconfig space.

Quoted 21 lines
drivers/base/cpu.c +5 -0
 #ifdef CONFIG_HOTPLUG_CPU
 static ssize_t show_online(struct sys_device *dev, char *buf)
 {
@@ -175,6 +191,11 @@
 	if (!error)
 		error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+	if (!error)
+		error = sysdev_create_file(&cpu->sysdev, &attr_timer_migration);
+#endif
 	return error;
 }
 

Index: linux-2.6.26/init/Kconfig
===================================================================

init/Kconfig line 931

Quoted 9 lines
init/Kconfig +6 -0
@@ -923,3 +923,9 @@
 	  designed for best read-side performance on non-realtime
 	  systems.  Classic RCU is the default.  Note that the
 	  PREEMPT_RCU symbol is used to select/deselect this option.
+
+config TIMER_MIGRATION
+	bool
+	default y
+	help
+	  This option enables migration of non cpu-affine timers to cpu0.

this will earn you a place in the hall of kconfig-help-text of shame.

Quoted 44 lines

Index: linux-2.6.26/include/linux/timer.h
===================================================================

include/linux/timer.h +5 -0
@@ -5,6 +5,7 @@
 #include <linux/ktime.h>
 #include <linux/stddef.h>
 #include <linux/debugobjects.h>
+#include <linux/percpu.h>
 
 struct tvec_base;
 
@@ -187,3 +188,7 @@
 unsigned long round_jiffies_relative(unsigned long j);
 
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+DECLARE_PER_CPU(int, enable_timer_migration);
+#endif

Index: linux-2.6.26/kernel/timer.c
===================================================================

kernel/timer.c +3 -1
@@ -37,6 +37,7 @@
 #include <linux/delay.h>
 #include <linux/tick.h>
 #include <linux/kallsyms.h>
+#include <linux/timer.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -87,8 +88,9 @@
  * the new flag to indicate whether the timer is deferrable
  */
 #define TBASE_DEFERRABLE_FLAG		(0x1)
+#define TBASE_PINNED_FLAG               (0x2)
 
-/* Functions below help us manage 'deferrable' flag */
+/* Functions below help us manage 'deferrable' flag and 'cpu-pinned-timer' flag */
 static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
 {
 	return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);

kernel/timer.c line 103

Quoted 7 lines
kernel/timer.c +1 -1
@@ -96,7 +98,7 @@
 
 static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
 {
-	return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
+	return (struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG & ~TBASE_PINNED_FLAG);
 }
 

I'm pretty sure this line is too long.

kernel/timer.c line 114

Quoted 10 lines
kernel/timer.c +4 -0
 static inline void timer_set_deferrable(struct timer_list *timer)
@@ -105,11 +107,21 @@
 				       TBASE_DEFERRABLE_FLAG));
 }
 
+static inline unsigned int tbase_get_pinned(struct tvec_base *base)
+{
+	return ((unsigned int)(unsigned long)base & TBASE_PINNED_FLAG);
+}
 

Why explicitly cast to unsigned int?

kernel/timer.c line 126

Quoted 13 lines
kernel/timer.c +6 -1
 static inline void
 timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
 {
 	timer->base = (struct tvec_base *)((unsigned long)(new_base) |
-				      tbase_get_deferrable(timer->base));
+		   tbase_get_deferrable(timer->base) | tbase_get_pinned(timer->base));
+}
+
+static inline void timer_set_pinned(struct timer_list *timer)
+{
+	timer->base = ((struct tvec_base *)((unsigned long)(timer->base) | TBASE_PINNED_FLAG));
 }
 

<80 ?

kernel/timer.c line 560

Quoted 11 lines
kernel/timer.c +5 -0
 /**
@@ -540,6 +552,12 @@
 
 	new_base = __get_cpu_var(tvec_bases);
 
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration) && !tbase_get_pinned(timer->base)) {
+		new_base = per_cpu(tvec_bases, 0);
+	}
+	#endif
 

We don't indent preprocessor directives like that, also we don't use braces on single statement blocks.

Quoted 16 lines
kernel/timer.c +1 -0
 	if (base != new_base) {
 		/*
 		 * We are trying to schedule the timer on the local CPU.
@@ -579,6 +597,7 @@
 	struct tvec_base *base = per_cpu(tvec_bases, cpu);
 	unsigned long flags;
 
+	timer_set_pinned(timer);
 	timer_stats_timer_set_start_info(timer);
 	BUG_ON(timer_pending(timer) || !timer->function);
 	spin_lock_irqsave(&base->lock, flags);

Index: linux-2.6.26/kernel/hrtimer.c
===================================================================

kernel/hrtimer.c line 208

Quoted 9 lines
kernel/hrtimer.c +5 -0
@@ -200,6 +200,12 @@
 	struct hrtimer_cpu_base *new_cpu_base;
 
 	new_cpu_base = &__get_cpu_var(hrtimer_bases);
+
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration))
+		new_cpu_base = &per_cpu(hrtimer_bases, 0);
+	#endif
 

idem

Quoted 9 lines

and NAK - this will actually break stuff.

Quoted 4 lines
kernel/hrtimer.c +0 -0
 	new_base = &new_cpu_base->clock_base[base->index];
 
 	if (base != new_base) {

Thanks for pointing out all my blunders. I'll soon clean up my patch and post it again.

#4 lore raw
In reply to Peter Zijlstra
Quoted 234 lines · Peter Zijlstra
Quoted 2 lines · Arun R Bharadwaj

The implentation details of this as follows:

80 char lines please

Quoted 7 lines

A sysfs entry is created
at /sys/devices/system/cpu/cpuX/timer_migration. By setting this to 1, timer migration is enabled for that cpu.
An important thing to note here is cpu-pinned timers. Timers can be pinned to a particular cpu using the function add_timer_on(). So, such timers should not be migrated.

You utterly fail to mention hrtimers even though you do touch those as well, what's more - there are hrtimers that need to be run on the cpu that queues them - you don't seem to provide anything like that.

Quoted 9 lines

Since the last 3 bits of the tvec_base is guaranteed to be 0, and since the last bit is being used to indicate deferrable timers, I'm using the second last bit to indicate cpu-pinned timers. The implementation of functions to manage the TBASE_PINNED_FLAG is similar to those which manage the TBASE_DEFERRABLE_FLAG.

---

please add --show-c-function to your diff

Quoted 21 lines

Index: linux-2.6.26/drivers/base/cpu.c
===================================================================

drivers/base/cpu.c +6 -0
@@ -13,6 +13,7 @@
 
 #include "base.h"
 
+DEFINE_PER_CPU(int, enable_timer_migration);
 struct sysdev_class cpu_sysdev_class = {
 	.name = "cpu",
 };
@@ -20,6 +21,21 @@
 
 static DEFINE_PER_CPU(struct sys_device *, cpu_sys_devices);
 
+#ifdef CONFIG_TIMER_MIGRATION
+static ssize_t timer_migration_show(struct sys_device *dev, char *buf)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	return sprintf(buf, "%u\n", per_cpu(enable_timer_migration, cpu->sysdev.id));

drivers/base/cpu.c line 38

Quoted 10 lines
drivers/base/cpu.c +9 -0
+}
+static ssize_t timer_migration_store(struct sys_device *dev, const char *buf, size_t count)
+{
+	struct cpu *cpu = container_of(dev, struct cpu, sysdev);
+	per_cpu(enable_timer_migration, cpu->sysdev.id) = buf[0] - 48;
+	return count;
+}
+static SYSDEV_ATTR(timer_migration, 0666, timer_migration_show, timer_migration_store);
+#endif
 

Why bother with the CONFIG_ variable - its so little code and it adds to the Kconfig space.

Quoted 21 lines
drivers/base/cpu.c +5 -0
 #ifdef CONFIG_HOTPLUG_CPU
 static ssize_t show_online(struct sys_device *dev, char *buf)
 {
@@ -175,6 +191,11 @@
 	if (!error)
 		error = sysdev_create_file(&cpu->sysdev, &attr_crash_notes);
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+	if (!error)
+		error = sysdev_create_file(&cpu->sysdev, &attr_timer_migration);
+#endif
 	return error;
 }
 

Index: linux-2.6.26/init/Kconfig
===================================================================

init/Kconfig line 931

Quoted 9 lines
init/Kconfig +6 -0
@@ -923,3 +923,9 @@
 	  designed for best read-side performance on non-realtime
 	  systems.  Classic RCU is the default.  Note that the
 	  PREEMPT_RCU symbol is used to select/deselect this option.
+
+config TIMER_MIGRATION
+	bool
+	default y
+	help
+	  This option enables migration of non cpu-affine timers to cpu0.

this will earn you a place in the hall of kconfig-help-text of shame.

Quoted 44 lines

Index: linux-2.6.26/include/linux/timer.h
===================================================================

include/linux/timer.h +5 -0
@@ -5,6 +5,7 @@
 #include <linux/ktime.h>
 #include <linux/stddef.h>
 #include <linux/debugobjects.h>
+#include <linux/percpu.h>
 
 struct tvec_base;
 
@@ -187,3 +188,7 @@
 unsigned long round_jiffies_relative(unsigned long j);
 
 #endif
+
+#ifdef CONFIG_TIMER_MIGRATION
+DECLARE_PER_CPU(int, enable_timer_migration);
+#endif

Index: linux-2.6.26/kernel/timer.c
===================================================================

kernel/timer.c +3 -1
@@ -37,6 +37,7 @@
 #include <linux/delay.h>
 #include <linux/tick.h>
 #include <linux/kallsyms.h>
+#include <linux/timer.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -87,8 +88,9 @@
  * the new flag to indicate whether the timer is deferrable
  */
 #define TBASE_DEFERRABLE_FLAG		(0x1)
+#define TBASE_PINNED_FLAG               (0x2)
 
-/* Functions below help us manage 'deferrable' flag */
+/* Functions below help us manage 'deferrable' flag and 'cpu-pinned-timer' flag */
 static inline unsigned int tbase_get_deferrable(struct tvec_base *base)
 {
 	return ((unsigned int)(unsigned long)base & TBASE_DEFERRABLE_FLAG);

kernel/timer.c line 103

Quoted 7 lines
kernel/timer.c +1 -1
@@ -96,7 +98,7 @@
 
 static inline struct tvec_base *tbase_get_base(struct tvec_base *base)
 {
-	return ((struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG));
+	return (struct tvec_base *)((unsigned long)base & ~TBASE_DEFERRABLE_FLAG & ~TBASE_PINNED_FLAG);
 }
 

I'm pretty sure this line is too long.

kernel/timer.c line 114

Quoted 10 lines
kernel/timer.c +4 -0
 static inline void timer_set_deferrable(struct timer_list *timer)
@@ -105,11 +107,21 @@
 				       TBASE_DEFERRABLE_FLAG));
 }
 
+static inline unsigned int tbase_get_pinned(struct tvec_base *base)
+{
+	return ((unsigned int)(unsigned long)base & TBASE_PINNED_FLAG);
+}
 

Why explicitly cast to unsigned int?

kernel/timer.c line 126

Quoted 13 lines
kernel/timer.c +6 -1
 static inline void
 timer_set_base(struct timer_list *timer, struct tvec_base *new_base)
 {
 	timer->base = (struct tvec_base *)((unsigned long)(new_base) |
-				      tbase_get_deferrable(timer->base));
+		   tbase_get_deferrable(timer->base) | tbase_get_pinned(timer->base));
+}
+
+static inline void timer_set_pinned(struct timer_list *timer)
+{
+	timer->base = ((struct tvec_base *)((unsigned long)(timer->base) | TBASE_PINNED_FLAG));
 }
 

<80 ?

kernel/timer.c line 560

Quoted 11 lines
kernel/timer.c +5 -0
 /**
@@ -540,6 +552,12 @@
 
 	new_base = __get_cpu_var(tvec_bases);
 
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration) && !tbase_get_pinned(timer->base)) {
+		new_base = per_cpu(tvec_bases, 0);
+	}
+	#endif
 

We don't indent preprocessor directives like that, also we don't use braces on single statement blocks.

Quoted 16 lines
kernel/timer.c +1 -0
 	if (base != new_base) {
 		/*
 		 * We are trying to schedule the timer on the local CPU.
@@ -579,6 +597,7 @@
 	struct tvec_base *base = per_cpu(tvec_bases, cpu);
 	unsigned long flags;
 
+	timer_set_pinned(timer);
 	timer_stats_timer_set_start_info(timer);
 	BUG_ON(timer_pending(timer) || !timer->function);
 	spin_lock_irqsave(&base->lock, flags);

Index: linux-2.6.26/kernel/hrtimer.c
===================================================================

kernel/hrtimer.c line 208

Quoted 9 lines
kernel/hrtimer.c +5 -0
@@ -200,6 +200,12 @@
 	struct hrtimer_cpu_base *new_cpu_base;
 
 	new_cpu_base = &__get_cpu_var(hrtimer_bases);
+
+	#ifdef CONFIG_TIMER_MIGRATION
+	if (__get_cpu_var(enable_timer_migration))
+		new_cpu_base = &per_cpu(hrtimer_bases, 0);
+	#endif
 

idem

Quoted 9 lines

and NAK - this will actually break stuff.

Quoted 4 lines
kernel/hrtimer.c +0 -0
 	new_base = &new_cpu_base->clock_base[base->index];
 
 	if (base != new_base) {

Thanks for pointing out all my blunders. I'll soon clean up my patch and post it again.