Skip to contentSkip to conversation

workqueues: shrink cpu_populated_map when CPU dies

PATCH In review Oleg Nesterov 2 participants 4 messages Feb 16, 2008 – Feb 18, 2008 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 workqueues: shrink cpu_populated_map when CPU dies

When cpu_populated_map was introduced, it was supposed that cwq->thread can survive after CPU_DEAD, that is why we never shrink cpu_populated_map.

This is not very nice, we can safely remove the already dead CPU from the map. The only required change is that destroy_workqueue() must hold the hotplug lock until it destroys all cwq->thread's, to protect the cpu_populated_map. We could make the local copy of cpu mask and drop the lock, but sizeof(cpumask_t) may be very large.

Also, fix the comment near queue_work(). Unless _cpu_down() happens we do guarantee the cpu-affinity of the work_struct, and we have users which rely on this.

kernel/workqueue.c renamed from kernel/workqueue.c~2_WQ_CPU_MAP +9 -4
@@ -158,8 +158,8 @@ static void __queue_work(struct cpu_work
  *
  * Returns 0 if @work was already on a queue, non-zero otherwise.
  *
- * We queue the work to the CPU it was submitted, but there is no
- * guarantee that it will be processed by that CPU.
+ * We queue the work to the CPU it was submitted, but if CPU dies
+ * it can be processed by another CPU.
  */
 int queue_work(struct workqueue_struct *wq, struct work_struct *work)
 {
@@ -813,12 +813,12 @@ void destroy_workqueue(struct workqueue_
 	spin_lock(&workqueue_lock);
 	list_del(&wq->list);
 	spin_unlock(&workqueue_lock);
-	put_online_cpus();
 
 	for_each_cpu_mask(cpu, *cpu_map) {
 		cwq = per_cpu_ptr(wq->cpu_wq, cpu);
 		cleanup_workqueue_thread(cwq, cpu);
 	}
+	put_online_cpus();
 
 	free_percpu(wq->cpu_wq);
 	kfree(wq);
@@ -836,7 +836,6 @@ static int __devinit workqueue_cpu_callb
 	action &= ~CPU_TASKS_FROZEN;
 
 	switch (action) {
-
 	case CPU_UP_PREPARE:
 		cpu_set(cpu, cpu_populated_map);
 	}
@@ -864,6 +863,12 @@ static int __devinit workqueue_cpu_callb
 		}
 	}
 
+	switch (action) {
+	case CPU_UP_CANCELED:
+	case CPU_DEAD:
+		cpu_clear(cpu, cpu_populated_map);
+	}
+
 	return NOTIFY_OK;
 }
 
#1 lore raw

Hi Oleg,

This patch looks OK to me. But while reading this I got some doubts in nearby places, so BTW 2 small questions:

1) ... workqueue_cpu_callback(...)

{
	...
        list_for_each_entry(wq, &workqueues, list) {
                cwq = per_cpu_ptr(wq->cpu_wq, cpu);

                switch (action) {
                case CPU_UP_PREPARE:
		...

It looks like not all CPU_ cases are served here: shouldn't list_for_each_entry() be omitted for them?

2) ... __create_workqueue_key(...)

{
	...
        if (singlethread) {
		...
        } else {
                get_online_cpus();
                spin_lock(&workqueue_lock);
                list_add(&wq->list, &workqueues);

Shouldn't this list_add() be done after all these inits below?

                spin_unlock(&workqueue_lock);

                for_each_possible_cpu(cpu) {
                        cwq = init_cpu_workqueue(wq, cpu);
			...
                }
		...

Thanks,
Jarek P.

Quoted 14 lines · Oleg Nesterov

When cpu_populated_map was introduced, it was supposed that cwq->thread can survive after CPU_DEAD, that is why we never shrink cpu_populated_map.

This is not very nice, we can safely remove the already dead CPU from the map. The only required change is that destroy_workqueue() must hold the hotplug lock until it destroys all cwq->thread's, to protect the cpu_populated_map. We could make the local copy of cpu mask and drop the lock, but sizeof(cpumask_t) may be very large.

Also, fix the comment near queue_work(). Unless _cpu_down() happens we do guarantee the cpu-affinity of the work_struct, and we have users which rely on this.

#2 lore raw
Quoted 2 lines · Jarek Poplawski

This patch looks OK to me.

Thanks for looking at this!

Quoted 13 lines

But while reading this I got some doubts
in nearby places, so BTW 2 small questions:

1) ... workqueue_cpu_callback(...)

{
	...
        list_for_each_entry(wq, &workqueues, list) {
                cwq = per_cpu_ptr(wq->cpu_wq, cpu);

                switch (action) {
                case CPU_UP_PREPARE:
		...
Quoted 2 lines

It looks like not all CPU_ cases are served here: shouldn't list_for_each_entry() be omitted for them?

Yes, but this is harmless. cpu-hotplug callbacks are not time-critical, and cpu_down/cpu_up happens not often, and LIST_HEAD(workqueues) is not very long, so ...

Quoted 12 lines

2) ... __create_workqueue_key(...)

{
	...
        if (singlethread) {
		...
        } else {
                get_online_cpus();
                spin_lock(&workqueue_lock);
                list_add(&wq->list, &workqueues);

Shouldn't this list_add() be done after all these inits below?

Quoted 7 lines
                spin_unlock(&workqueue_lock);

                for_each_possible_cpu(cpu) {
                        cwq = init_cpu_workqueue(wq, cpu);
			...
                }
		...

This doesn't matter. Please note that get_online_cpus() blocks cpu_up/cpu_down, they take cpu_hotplug_begin().

Oleg.

#3 lore raw
Quoted 1 line · Oleg Nesterov

On 02/17, Jarek Poplawski wrote:

...

Quoted 1 line
Quoted 1 line

1) ... workqueue_cpu_callback(...)

...

Quoted 5 lines

Yes, but this is harmless. cpu-hotplug callbacks are not time-critical, and cpu_down/cpu_up happens not often, and LIST_HEAD(workqueues) is not very long, so ...

Quoted 1 line

2) ... __create_workqueue_key(...)

...

Quoted 3 lines
Quoted 1 line

Shouldn't this list_add() be done after all these inits below?

This doesn't matter. Please note that get_online_cpus() blocks cpu_up/cpu_down, they take cpu_hotplug_begin().

You are completely right. It looks like this was only about ..."look". (But adding only "default:" in 1) would make it look nicer to me...)

Regards,
Jarek P.