Skip to contentSkip to conversation

Add do_not_call_when_idle option to timer and workqueue

RFC PATCH In review Venkatesh Pallipadi 4 participants 6 messages Nov 22, 2006

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

RFC PATCH 1/1 Add do_not_call_when_idle option to timer and workqueue

Add a new flag to timers to allow "Do not call when Idle" mode. Export this sort of soft timer over workqueues and use it in ondemand governor.

This patch avoids the periodic ondemand timer invocations in Dynamic Tick kernels.

This can be used in various other kernel timers that end up setting up unnecessary timers during idle.

include/linux/timer.h +4 -0
Index: linux-2.6.19-rc-mm/include/linux/timer.h
===================================================================
@@ -8,6 +8,8 @@
 
 struct tvec_t_base_s;
 
+#define TIMER_FLAG_NOT_IN_IDLE	(0x1)
+
 struct timer_list {
 	struct list_head entry;
 	unsigned long expires;
@@ -16,6 +18,7 @@
 	unsigned long data;
 
 	struct tvec_t_base_s *base;
+	int	flags;
 #ifdef CONFIG_TIMER_STATS
 	void *start_site;
 	char start_comm[16];
@@ -30,6 +33,7 @@
 		.expires = (_expires),				\
 		.data = (_data),				\
 		.base = &boot_tvec_bases,			\
+		.flags = 0,					\
 	}
 
 #define DEFINE_TIMER(_name, _function, _expires, _data)		\
include/linux/workqueue.h +2 -0
Index: linux-2.6.19-rc-mm/include/linux/workqueue.h
===================================================================
@@ -65,6 +65,8 @@
 extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq, struct work_struct *work, unsigned long delay));
 extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
 	struct work_struct *work, unsigned long delay);
+extern int queue_soft_delayed_work_on(int cpu, struct workqueue_struct *wq,
+	struct work_struct *work, unsigned long delay);
 extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq));
 
 extern int FASTCALL(schedule_work(struct work_struct *work));
kernel/timer.c +2 -0
Index: linux-2.6.19-rc-mm/kernel/timer.c
===================================================================
@@ -639,6 +639,8 @@
 	j = base->timer_jiffies & TVR_MASK;
 	do {
 		list_for_each_entry(nte, base->tv1.vec + j, entry) {
+			if (nte->flags & TIMER_FLAG_NOT_IN_IDLE)
+				continue;
 			expires = nte->expires;
 			found = nte;
 			if (j < (base->timer_jiffies & TVR_MASK))
kernel/workqueue.c +14 -0
Index: linux-2.6.19-rc-mm/kernel/workqueue.c
===================================================================
@@ -196,6 +196,20 @@
 }
 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
 
+int queue_soft_delayed_work_on(int cpu, struct workqueue_struct *wq,
+			struct work_struct *work, unsigned long delay)
+{
+	int ret;
+	struct timer_list *timer = &work->timer;
+	ret = queue_delayed_work_on(cpu, wq, work, delay);
+	if (ret) {
+		timer->flags |= TIMER_FLAG_NOT_IN_IDLE;
+	}
+	return ret;
+}
+
+EXPORT_SYMBOL_GPL(queue_soft_delayed_work_on);
+
 static void run_workqueue(struct cpu_workqueue_struct *cwq)
 {
 	unsigned long flags;
drivers/cpufreq/cpufreq_ondemand.c +2 -2
Index: linux-2.6.19-rc-mm/drivers/cpufreq/cpufreq_ondemand.c
===================================================================
@@ -446,7 +448,7 @@
 	                        	dbs_info->freq_lo,
 	                        	CPUFREQ_RELATION_H);
 	}
-	queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
+	queue_soft_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
 }
 
 static inline void dbs_timer_init(unsigned int cpu)
@@ -456,9 +458,9 @@
 	int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
 	delay -= jiffies % delay;
 
 	ondemand_powersave_bias_init();
 	INIT_WORK(&dbs_info->work, do_dbs_timer, NULL);
-	queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
+	queue_soft_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
 }
 
 static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
#1 lore raw
Quoted 26 lines · On Tue, 21 Nov 2006 16:28:45 -0800 Venkatesh Pallipadi…

Add a new flag to timers to allow "Do not call when Idle" mode. Export this sort of soft timer over workqueues and use it in ondemand governor.

This patch avoids the periodic ondemand timer invocations in Dynamic Tick kernels.

This can be used in various other kernel timers that end up setting up unnecessary timers during idle.

Index: linux-2.6.19-rc-mm/include/linux/timer.h
===================================================================

include/linux/timer.h +2 -0
@@ -8,6 +8,8 @@
 
 struct tvec_t_base_s;
 
+#define TIMER_FLAG_NOT_IN_IDLE	(0x1)
+
 struct timer_list {
 	struct list_head entry;
 	unsigned long expires;

include/linux/timer.h line 22

Quoted 5 lines
include/linux/timer.h +1 -0
@@ -16,6 +18,7 @@
 	unsigned long data;
 
 	struct tvec_t_base_s *base;
+	int	flags;
 #ifdef CONFIG_TIMER_STATS

Adding a new field to the timer_list is somewhat of a hit - this is going to make an awful lot of data structures a bit larger. Some of which we allocate a large number of.

I think we could justfy getting nasty and using the LSB of timer_list.function for this..

Quoted 15 lines
include/linux/timer.h +1 -0
 	void *start_site;
 	char start_comm[16];
@@ -30,6 +33,7 @@
 		.expires = (_expires),				\
 		.data = (_data),				\
 		.base = &boot_tvec_bases,			\
+		.flags = 0,					\
 	}
 
 #define DEFINE_TIMER(_name, _function, _expires, _data)		\

Index: linux-2.6.19-rc-mm/include/linux/workqueue.h
===================================================================

include/linux/workqueue.h line 69

Quoted 5 lines
include/linux/workqueue.h +2 -0
@@ -65,6 +65,8 @@
 extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq, struct work_struct *work, unsigned long delay));
 extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
 	struct work_struct *work, unsigned long delay);
+extern int queue_soft_delayed_work_on(int cpu, struct workqueue_struct *wq,
+	struct work_struct *work, unsigned long delay);

I don't think that's a well-chosen name. What does the "soft" mean?

Also, this is a new timer API capability, but it is only exposed via the workqueue API, and then only via a part of it.

A complete implementation would expose the new capability via extensions to the timer API, and would then (in a separate patch) convert the workqueue API to use those extensions. (And in fact the third patch would convert cpufreq to use the new workqueue capabilities...)

Quoted 25 lines
include/linux/workqueue.h +0 -0
 extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq));
 
 extern int FASTCALL(schedule_work(struct work_struct *work));

Index: linux-2.6.19-rc-mm/kernel/timer.c
===================================================================

kernel/timer.c +2 -0
@@ -639,6 +639,8 @@
 	j = base->timer_jiffies & TVR_MASK;
 	do {
 		list_for_each_entry(nte, base->tv1.vec + j, entry) {
+			if (nte->flags & TIMER_FLAG_NOT_IN_IDLE)
+				continue;
 			expires = nte->expires;
 			found = nte;
 			if (j < (base->timer_jiffies & TVR_MASK))

Index: linux-2.6.19-rc-mm/kernel/workqueue.c
===================================================================

kernel/workqueue.c +1 -0
@@ -196,6 +196,20 @@
 }
 EXPORT_SYMBOL_GPL(queue_delayed_work_on);
 
+int queue_soft_delayed_work_on(int cpu, struct workqueue_struct *wq,

kernel/workqueue.c line 209

Quoted 10 lines
kernel/workqueue.c +10 -0
+			struct work_struct *work, unsigned long delay)
+{
+	int ret;
+	struct timer_list *timer = &work->timer;
+	ret = queue_delayed_work_on(cpu, wq, work, delay);
+	if (ret) {
+		timer->flags |= TIMER_FLAG_NOT_IN_IDLE;
+	}
+	return ret;
+}

See, here we have workqueue code poking around in timer_list internals.

Also, it's weird and conceivably racy to go altering the timer _after_ scheduling the work. What happens if the timer goes off before we set the bit? And suppose the workqueue callback function frees the storage at *timer?

All these problems will go away if the APIs are done fully, and correctly.

#2 lore raw
Quoted 25 lines · Andrew Morton
Quoted 14 lines

Index: linux-2.6.19-rc-mm/include/linux/timer.h
===================================================================

include/linux/timer.h +2 -0
@@ -8,6 +8,8 @@
 
 struct tvec_t_base_s;
 
+#define TIMER_FLAG_NOT_IN_IDLE	(0x1)
+
 struct timer_list {
 	struct list_head entry;
 	unsigned long expires;

include/linux/timer.h line 23

Quoted 6 lines
include/linux/timer.h +1 -0
@@ -16,6 +18,7 @@
 	unsigned long data;
 
 	struct tvec_t_base_s *base;
+	int	flags;
 #ifdef CONFIG_TIMER_STATS
 

Adding a new field to the timer_list is somewhat of a hit - this is going to make an awful lot of data structures a bit larger. Some of which we allocate a large number of.

Quoted 4 lines

I think we could justfy getting nasty and using the LSB of timer_list.function for this..

The lsb of a function pointer is used in variable length instruction processors (such as x86 when optimizing for size). The msb is constant though.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#3 lore raw
Quoted 2 lines · Avi Kivity

The lsb of a function pointer is used in variable length instruction processors (such as x86 when optimizing for size). The msb is constant though.

x86 aligns the start of functions to 4 bytes even when optimizing for size though...

#4 lore raw
In reply to Andrew Morton
Quoted 18 lines · Andrew Morton

include/linux/timer.h line 22

Quoted 10 lines · On Tue, 21 Nov 2006 16:28:45 -0800 Venkatesh Pallipadi…

struct timer_list {

include/linux/timer.h +1 -0
 	struct list_head entry;
 	unsigned long expires;
@@ -16,6 +18,7 @@
 	unsigned long data;
 
 	struct tvec_t_base_s *base;
+	int	flags;
 #ifdef CONFIG_TIMER_STATS

Adding a new field to the timer_list is somewhat of a hit - this is going to make an awful lot of data structures a bit larger. Some of which we allocate a large number of.

Quoted 2 lines

I think we could justfy getting nasty and using the LSB of timer_list.function for this..

That is a clever idea... Is that going to work in all architectures with all compiler flags?

Quoted 14 lines

include/linux/workqueue.h line 69

Quoted 11 lines
include/linux/timer.h +0 -0
 #define DEFINE_TIMER(_name, _function, _expires, _data)		\

Index: linux-2.6.19-rc-mm/include/linux/workqueue.h
===================================================================

include/linux/workqueue.h +2 -0
@@ -65,6 +65,8 @@
 extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq, struct work_struct *work, unsigned long delay));
 extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
 	struct work_struct *work, unsigned long delay);
+extern int queue_soft_delayed_work_on(int cpu, struct workqueue_struct *wq,
+	struct work_struct *work, unsigned long delay);

I don't think that's a well-chosen name. What does the "soft" mean?

Quoted 8 lines

Also, this is a new timer API capability, but it is only exposed via the workqueue API, and then only via a part of it.

A complete implementation would expose the new capability via extensions to the timer API, and would then (in a separate patch) convert the workqueue API to use those extensions. (And in fact the third patch would convert cpufreq to use the new workqueue capabilities...)

I agree with all API related comments. I will clean it up in the next refresh of the patch. I wanted to get the comments on this mechanism in general and wanted to be doubly sure that I am not breaking anything in cascading timer by doing something like this. Looks like the mechanism is OK.
So, I will work on a cleaner patch and repost.

Thanks,
Venki

#5 lore raw
Quoted 19 lines · On Wed, 22 Nov 2006 09:13:24 -0800 Venkatesh Pallipadi…
Quoted 18 lines · Andrew Morton

include/linux/timer.h line 22

Quoted 10 lines · On Tue, 21 Nov 2006 16:28:45 -0800 Venkatesh Pallipadi…

struct timer_list {

include/linux/timer.h +1 -0
 	struct list_head entry;
 	unsigned long expires;
@@ -16,6 +18,7 @@
 	unsigned long data;
 
 	struct tvec_t_base_s *base;
+	int	flags;
 #ifdef CONFIG_TIMER_STATS

Adding a new field to the timer_list is somewhat of a hit - this is going to make an awful lot of data structures a bit larger. Some of which we allocate a large number of.

Quoted 5 lines
Quoted 2 lines

I think we could justfy getting nasty and using the LSB of timer_list.function for this..

That is a clever idea... Is that going to work in all architectures with all compiler flags?

Don't know. Possibly not. Other options are list.next, list.prev and base. None of them are pleasant.