Skip to contentSkip to conversation

+ pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on.patch added to -mm tree

PATCH New [email protected] 5 participants 11 messages Jul 11, 2008 – Aug 6, 2008 Workqueue

lore
Share & tools
lore

[email protected]

author tree notice
#0 lore raw

The patch titled
pm: introduce new interfaces schedule_work_on() and queue_work_on()
has been added to the -mm tree. Its filename is
pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on.patch

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/SubmitChecklist when testing your code ***

See http://www.zip.com.au/~akpm/linux/patches/stuff/added-to-mm.txt to find out what to do about this

The current -mm tree may be found at http://userweb.kernel.org/~akpm/mmotm/

------------------------------------------------------
Subject: pm: introduce new interfaces schedule_work_on() and queue_work_on()
From: Zhang Rui <[email protected]>

This interface allows adding a job on a specific cpu.

Although a work struct on a cpu will be scheduled to other cpu if the cpu dies, there is a recursion if a work task tries to offline the cpu it's running on. we need to schedule the task to a specific cpu in this case.
http://bugzilla.kernel.org/show_bug.cgi?id=10897

---

 include/linux/workqueue.h |    3 ++
 kernel/workqueue.c        |   39 ++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
include/linux/workqueue.h renamed from include/linux/workqueue.h~pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on +3 -0
diff -puN include/linux/workqueue.h~pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on include/linux/workqueue.h
@@ -179,6 +179,8 @@ __create_workqueue_key(const char *name,
 extern void destroy_workqueue(struct workqueue_struct *wq);
 
 extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
+extern int queue_work_on(int cpu, struct workqueue_struct *wq,
+			struct work_struct *work);
 extern int queue_delayed_work(struct workqueue_struct *wq,
 			struct delayed_work *work, unsigned long delay);
 extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
@@ -188,6 +190,7 @@ extern void flush_workqueue(struct workq
 extern void flush_scheduled_work(void);
 
 extern int schedule_work(struct work_struct *work);
+extern int schedule_work_on(int cpu, struct work_struct *work);
 extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
 extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
 					unsigned long delay);
kernel/workqueue.c renamed from kernel/workqueue.c~pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on +39 -0
diff -puN kernel/workqueue.c~pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on kernel/workqueue.c
@@ -175,6 +175,32 @@ int queue_work(struct workqueue_struct *
 }
 EXPORT_SYMBOL_GPL(queue_work);
 
+/**
+ * queue_work_on - queue work on specific cpu
+ * @cpu: CPU number to execute work on
+ * @wq: workqueue to use
+ * @work: work to queue
+ *
+ * Returns 0 if @work was already on a queue, non-zero otherwise.
+ *
+ * We queue the work to a specific CPU
+ */
+int
+queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
+{
+	int ret = 0;
+
+	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
+		BUG_ON(!list_empty(&work->entry));
+		preempt_disable();
+		__queue_work(wq_per_cpu(wq, cpu), work);
+		preempt_enable();
+		ret = 1;
+	}
+	return ret;
+}
+EXPORT_SYMBOL_GPL(queue_work_on);
+
 static void delayed_work_timer_fn(unsigned long __data)
 {
 	struct delayed_work *dwork = (struct delayed_work *)__data;
@@ -553,6 +579,19 @@ int schedule_work(struct work_struct *wo
 }
 EXPORT_SYMBOL(schedule_work);
 
+/*
+ * schedule_work_on - put work task on a specific cpu
+ * @cpu: cpu to put the work task on
+ * @work: job to be done
+ *
+ * This puts a job on a specific cpu
+ */
+int schedule_work_on(int cpu, struct work_struct *work)
+{
+	return queue_work_on(cpu, keventd_wq, work);
+}
+EXPORT_SYMBOL(schedule_work_on);
+
 /**
  * schedule_delayed_work - put work task in global workqueue after delay
  * @dwork: job to be done

_

Patches currently in -mm which might be from [email protected] are

linux-next.patch
pm-ahci-speed-up-resume.patch pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on.patch pm-schedule-sysrq-poweroff-on-boot-cpu.patch dcdbas-use-memory_read_from_buffer.patch dell_rbu-use-memory_read_from_buffer.patch

#1 lore raw

PATCH 1/1 pm-introduce-new-interfaces-schedule_work_on-and-queue_work_on-cleanup

Review

Quoted 10 lines · Andrew Morton
(quoted diff) +9 -0
+queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
+{
+	int ret = 0;
+
+	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
+		BUG_ON(!list_empty(&work->entry));
+		preempt_disable();
+		__queue_work(wq_per_cpu(wq, cpu), work);
+		preempt_enable();

The comment above __queue_work() is wrong, we don't need to disable preemtion.

What it actually means is: the caller of __queue_work() must ensure we can't race with CPU_DEAD, but preempt_disable() can't help for queue_work_on(). CPU can die even before preempt_disable().

Remove preempt_disable() and update the comment.

kernel/workqueue.c renamed from kernel/workqueue.c~WQ_1_QWON_CLEANUP +2 -4
@@ -137,7 +137,6 @@ static void insert_work(struct cpu_workq
 	wake_up(&cwq->more_work);
 }
 
-/* Preempt must be disabled. */
 static void __queue_work(struct cpu_workqueue_struct *cwq,
 			 struct work_struct *work)
 {
@@ -180,7 +179,8 @@ EXPORT_SYMBOL_GPL(queue_work);
  *
  * Returns 0 if @work was already on a queue, non-zero otherwise.
  *
- * We queue the work to a specific CPU
+ * We queue the work to a specific CPU, the caller must ensure it
+ * can't go away.
  */
 int
 queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work)
@@ -189,9 +189,7 @@ queue_work_on(int cpu, struct workqueue_
 
 	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
 		BUG_ON(!list_empty(&work->entry));
-		preempt_disable();
 		__queue_work(wq_per_cpu(wq, cpu), work);
-		preempt_enable();
 		ret = 1;
 	}
 	return ret;
#2 lore raw

PATCH 1/1 workqueues: queue_work() can use queue_work_on()

queue_work() can use queue_work_on() to avoid the code duplication.

kernel/workqueue.c renamed from kernel/workqueue.c~WQ_2_QWON_FACTOR +4 -7
@@ -159,14 +159,11 @@ static void __queue_work(struct cpu_work
  */
 int queue_work(struct workqueue_struct *wq, struct work_struct *work)
 {
-	int ret = 0;
+	int ret;
+
+	ret = queue_work_on(get_cpu(), wq, work);
+	put_cpu();
 
-	if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) {
-		BUG_ON(!list_empty(&work->entry));
-		__queue_work(wq_per_cpu(wq, get_cpu()), work);
-		put_cpu();
-		ret = 1;
-	}
 	return ret;
 }
 EXPORT_SYMBOL_GPL(queue_work);
#3 lore raw

PATCH 1/1 workqueues: schedule_on_each_cpu() can use schedule_work_on()

In reply to Oleg Nesterov

schedule_on_each_cpu() can use schedule_work_on() to avoid the code duplication.

kernel/workqueue.c renamed from kernel/workqueue.c~WQ_3_SOEC_QWON +1 -2
@@ -689,8 +689,7 @@ int schedule_on_each_cpu(work_func_t fun
 		struct work_struct *work = per_cpu_ptr(works, cpu);
 
 		INIT_WORK(work, func);
-		set_bit(WORK_STRUCT_PENDING, work_data_bits(work));
-		__queue_work(per_cpu_ptr(keventd_wq->cpu_wq, cpu), work);
+		schedule_work_on(cpu, work);
 	}
 	for_each_online_cpu(cpu)
 		flush_work(per_cpu_ptr(works, cpu));
#4 lore raw
In reply to [email protected]

(Gautham cc'ed)

Quoted 10 lines · Andrew Morton

Subject: pm: introduce new interfaces schedule_work_on() and queue_work_on()
From: Zhang Rui <[email protected]>

This interface allows adding a job on a specific cpu.

Although a work struct on a cpu will be scheduled to other cpu if the cpu dies, there is a recursion if a work task tries to offline the cpu it's running on. we need to schedule the task to a specific cpu in this case.
http://bugzilla.kernel.org/show_bug.cgi?id=10897

So, this is used in http://bugzilla.kernel.org/attachment.cgi?id=16707

	--- linux-2.6.orig/kernel/power/poweroff.c	2008-06-30 16:01:35.000000000 +0800
	+++ linux-2.6/kernel/power/poweroff.c	2008-07-03 10:50:05.000000000 +0800
	@@ -25,7 +25,8 @@
	 
	 static void handle_poweroff(int key, struct tty_struct *tty)
	 {
	-	schedule_work(&poweroff_work);
	+	/* run sysrq poweroff on boot cpu */
	+	schedule_work_on(first_cpu(cpu_online_map), &poweroff_work);
	 }
	 
	 static struct sysrq_key_op	sysrq_poweroff_op = {

A couple of silly questions, I don't understand the low-level details.

This patch (and kernel_power_off() afaics) assumes that the boot cpu can't be cpu_down()'ed. Is it true in general? For example, grep shows that arch/s390/kernel/smp.c:topology_init()->smp_add_present_cpu() sets ->hotpluggable = 1 for all present CPUs?

Another question. I can't understand why first_cpu(cpu_online_map) is always the boot CPU on every arch. IOW, shouldn't boot_cpu_init() set some "boot_cpu = smp_processor_id()" which should be use instead of first_cpu(cpu_online_map) ?

Thanks,

Oleg.

#5 lore raw
Quoted 2 lines · Oleg Nesterov

(Gautham cc'ed)

Sorry for the delay... I'm a bit tied down to other things until aug 20th :(

Quoted 27 lines · Andrew Morton
Quoted 10 lines

Subject: pm: introduce new interfaces schedule_work_on() and queue_work_on()
From: Zhang Rui <[email protected]>

This interface allows adding a job on a specific cpu.

Although a work struct on a cpu will be scheduled to other cpu if the cpu dies, there is a recursion if a work task tries to offline the cpu it's running on. we need to schedule the task to a specific cpu in this case.
http://bugzilla.kernel.org/show_bug.cgi?id=10897

So, this is used in http://bugzilla.kernel.org/attachment.cgi?id=16707

	--- linux-2.6.orig/kernel/power/poweroff.c	2008-06-30 16:01:35.000000000 +0800
	+++ linux-2.6/kernel/power/poweroff.c	2008-07-03 10:50:05.000000000 +0800
	@@ -25,7 +25,8 @@
	 
	 static void handle_poweroff(int key, struct tty_struct *tty)
	 {
	-	schedule_work(&poweroff_work);
	+	/* run sysrq poweroff on boot cpu */
	+	schedule_work_on(first_cpu(cpu_online_map), &poweroff_work);
	 }
	 
	 static struct sysrq_key_op	sysrq_poweroff_op = {
Quoted 6 lines

A couple of silly questions, I don't understand the low-level details.

This patch (and kernel_power_off() afaics) assumes that the boot cpu can't be cpu_down()'ed. Is it true in general? For example, grep shows that arch/s390/kernel/smp.c:topology_init()->smp_add_present_cpu() sets ->hotpluggable = 1 for all present CPUs?

I tried this on a Power system sometime back and I was able to offline CPU0. What I am not sure however, is
if that was the boot-cpu.

On x86, I do remember reading somewhere why we cannot offline CPU0.

/me searches.

Yes, in arch/x86/kernel/topology.c

int __ref arch_register_cpu(int num)
{
/*
* CPU0 cannot be offlined due to several
* restrictions and assumptions in kernel. This basically
* doesnt add a control file, one cannot attempt to offline
* BSP.
*
* Also certain PCI quirks require not to enable hotplug control
* for all CPU's.
*/
if (num)
per_cpu(cpu_devices, num).cpu.hotpluggable = 1;
return register_cpu(&per_cpu(cpu_devices, num).cpu, num);
}

Quoted 6 lines

Another question. I can't understand why first_cpu(cpu_online_map) is always the boot CPU on every arch. IOW, shouldn't boot_cpu_init() set some "boot_cpu = smp_processor_id()" which should be use instead of first_cpu(cpu_online_map) ?

Not very sure about this one.

Quoted 4 lines

Thanks,

Oleg.

Signature · 2 lines
Thanks and Regards
gautham
#6 lore raw
Quoted 20 lines · Gautham R Shenoy
Quoted 18 lines · Oleg Nesterov

So, this is used in http://bugzilla.kernel.org/attachment.cgi?id=16707

	--- linux-2.6.orig/kernel/power/poweroff.c	2008-06-30 16:01:35.000000000 +0800
	+++ linux-2.6/kernel/power/poweroff.c	2008-07-03 10:50:05.000000000 +0800
	@@ -25,7 +25,8 @@
	 
	 static void handle_poweroff(int key, struct tty_struct *tty)
	 {
	-	schedule_work(&poweroff_work);
	+	/* run sysrq poweroff on boot cpu */
	+	schedule_work_on(first_cpu(cpu_online_map), &poweroff_work);
	 }
	 
	 static struct sysrq_key_op	sysrq_poweroff_op = {

A couple of silly questions, I don't understand the low-level details.

Quoted 7 lines
Quoted 4 lines

This patch (and kernel_power_off() afaics) assumes that the boot cpu can't be cpu_down()'ed. Is it true in general? For example, grep shows that arch/s390/kernel/smp.c:topology_init()->smp_add_present_cpu() sets ->hotpluggable = 1 for all present CPUs?

I tried this on a Power system sometime back and I was able to offline CPU0.

This means that

pm-schedule-sysrq-poweroff-on-boot-cpu.patch

is not 100% right. It is still possible to hang/deadlock if we race with cpu_down(first_cpu(cpu_online_map)).

The bug is mostly theoretical, but perhaps should be fixed anyway, handle_poweroff() can use kthread_run().

Oleg.

#7 lore raw
Quoted 36 lines · Oleg Nesterov
Quoted 4 lines · Gautham R Shenoy
Quoted 2 lines · Oleg Nesterov

So, this is used in

http://bugzilla.kernel.org/attachment.cgi?id=16707

Quoted 2 lines
Quoted 2 lines
    --- linux-2.6.orig/kernel/power/poweroff.c      2008-06-30

16:01:35.000000000 +0800

Quoted 1 line
Quoted 1 line
    +++ linux-2.6/kernel/power/poweroff.c   2008-07-03

10:50:05.000000000 +0800

Quoted 7 lines
Quoted 7 lines

@@ -25,7 +25,8 @@

static void handle_poweroff(int key, struct tty_struct *tty) {
- schedule_work(&poweroff_work);
+ /* run sysrq poweroff on boot cpu */
+ schedule_work_on(first_cpu(cpu_online_map),

&poweroff_work);

Quoted 5 lines
Quoted 5 lines
     }
    
     static struct sysrq_key_op     sysrq_poweroff_op = {

A couple of silly questions, I don't understand the low-level

details.

Quoted 2 lines
Quoted 2 lines

This patch (and kernel_power_off() afaics) assumes that the boot

cpu

Quoted 1 line
Quoted 1 line

can't be cpu_down()'ed. Is it true in general? For example, grep

shows

Quoted 5 lines
Quoted 2 lines

that arch/s390/kernel/smp.c:topology_init()->smp_add_present_cpu() sets ->hotpluggable = 1 for all present CPUs?

I tried this on a Power system sometime back and I was able to offline CPU0.

Quoted 6 lines

This means that

pm-schedule-sysrq-poweroff-on-boot-cpu.patch

is not 100% right. It is still possible to hang/deadlock if we race with cpu_down(first_cpu(cpu_online_map)).

Yes, you're right.
But then should we fix disable_nonboot_cpus as well?

int disable_nonboot_cpus(void)

{
        first_cpu = first_cpu(cpu_online_map);
	...
 
        for_each_online_cpu(cpu) {
                if (cpu == first_cpu)
                        continue;
                error = _cpu_down(cpu, 1);
		...
        }
	...
}

thanks,
rui

Quoted 3 lines

The bug is mostly theoretical, but perhaps should be fixed anyway, handle_poweroff() can use kthread_run().

#8 lore raw
Quoted 14 lines · Zhang Rui
Quoted 7 lines · Oleg Nesterov

This means that

pm-schedule-sysrq-poweroff-on-boot-cpu.patch

is not 100% right. It is still possible to hang/deadlock if we race with cpu_down(first_cpu(cpu_online_map)).

Yes, you're right.
But then should we fix disable_nonboot_cpus as well?

int disable_nonboot_cpus(void)

Quoted 12 lines
{
        first_cpu = first_cpu(cpu_online_map);
	...

        for_each_online_cpu(cpu) {
                if (cpu == first_cpu)
                        continue;
                error = _cpu_down(cpu, 1);
		...
        }
	...
}

Note that disable_nonboot_cpus() does first_cpu = first_cpu() under cpu_maps_update_begin(), so we can't race with cpu-hotplug.

However, this afaics means that its name is wrong, and printk("Disabling non-boot CPUs ...\n") is not right too. What it does is disable_all_but_one_cpus().

And, it is not clear why disable_nonboot_cpus() assumes that all but first_cpu(cpu_online_map) must have .hotpluggable == 1.

And, if one of the callers really need to preserve the boot CPU, I don't understand how it is guaranteed it must be first_cpu().

Oleg.

#9 lore raw

Hi!

Quoted 24 lines
Quoted 11 lines
Quoted 6 lines

This means that

pm-schedule-sysrq-poweroff-on-boot-cpu.patch

is not 100% right. It is still possible to hang/deadlock if we race with cpu_down(first_cpu(cpu_online_map)).

Yes, you're right.
But then should we fix disable_nonboot_cpus as well?

int disable_nonboot_cpus(void)

Quoted 12 lines
{
        first_cpu = first_cpu(cpu_online_map);
	...

        for_each_online_cpu(cpu) {
                if (cpu == first_cpu)
                        continue;
                error = _cpu_down(cpu, 1);
		...
        }
	...
}
Quoted 6 lines

Note that disable_nonboot_cpus() does first_cpu = first_cpu() under cpu_maps_update_begin(), so we can't race with cpu-hotplug.

However, this afaics means that its name is wrong, and printk("Disabling non-boot CPUs ...\n") is not right too. What it does is disable_all_but_one_cpus().

I thought that first cpu is defined to be boot cpu?

Quoted 2 lines

And, it is not clear why disable_nonboot_cpus() assumes that all but first_cpu(cpu_online_map) must have .hotpluggable == 1.

Where does it assume that?

It will fail if some CPUs can't be unplugged, and I'm afraid suspend can't work in such case...

Quoted 2 lines

And, if one of the callers really need to preserve the boot CPU, I don't understand how it is guaranteed it must be first_cpu().

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#10 lore raw
Quoted 25 lines · Pavel Machek
Quoted 25 lines
Quoted 24 lines
Quoted 7 lines

This means that

pm-schedule-sysrq-poweroff-on-boot-cpu.patch

is not 100% right. It is still possible to hang/deadlock if we race with cpu_down(first_cpu(cpu_online_map)).

Yes, you're right.
But then should we fix disable_nonboot_cpus as well?

int disable_nonboot_cpus(void)

{
        first_cpu = first_cpu(cpu_online_map);
	...

        for_each_online_cpu(cpu) {
                if (cpu == first_cpu)
                        continue;
                error = _cpu_down(cpu, 1);
		...
        }
	...
}
Quoted 8 lines
Quoted 6 lines

Note that disable_nonboot_cpus() does first_cpu = first_cpu() under cpu_maps_update_begin(), so we can't race with cpu-hotplug.

However, this afaics means that its name is wrong, and printk("Disabling non-boot CPUs ...\n") is not right too. What it does is disable_all_but_one_cpus().

I thought that first cpu is defined to be boot cpu?

I don't know, but I don't really understand this low-level code.

Is it documented? This is certainly true on x86, but I don't understand why this must be true on every arch.

Let's see. start_kernel() does smp_setup_processor_id(). Is it guaranteed that it chooses the lowest number from cpu_possible_map? This helper is only defined for voyager, but anyway it is not clear why start_kernel() must be always called on CPU 0. Otherwise, the next cpu_up() (from smp_init() or later) can add another CPU which becomes first_cpu(cpu_online_map).

But, from disable_nonboot_cpus's pov this doesn't matter. Even if the first cpu must be boot cpu, it can be (in general) cpu_down()'ed. In that case, when disable_nonboot_cpus() is called, first_cpu() returns another value.

Once again, I don't claim this all is wrong.

Quoted 7 lines
Quoted 2 lines

And, it is not clear why disable_nonboot_cpus() assumes that all but first_cpu(cpu_online_map) must have .hotpluggable == 1.

Where does it assume that?

It will fail if some CPUs can't be unplugged, and I'm afraid suspend can't work in such case...

Yes I see. But disable_nonboot_cpus() doesn't check .hotpluggable, it just takes CPU down regardless of .hotpluggable, is it always OK?

Oleg.