worker_set/clr_flags() assume that if none of NOT_RUNNING flags is set the worker must be contributing to nr_running which is only true if the worker is actually running. As when called from self, it is guaranteed that the worker is running, those functions can be safely used from the worker itself and they aren't necessary from other places anyway. Make the following changes to fix the bug. * Make worker_set/clr_flags() whine if not called from self. * Convert all places which called those functions from other tasks to manipulate flags directly. * Make trustee_thread() directly clear nr_running after setting WORKER_ROGUE on all workers. This is the only place where nr_running manipulation is necessary outside of workers themselves. * While at it, add sanity check for nr_running in worker_enter_idle().
Showing the changes to kernel/workqueue.c (1 of 1 file). Show all
Unified · Split
| @@ -601,7 +601,7 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, · 600 unmodified lines | ||
| 601 | 601 | |
| 602 | 602 | /** |
| 603 | 603 | * worker_set_flags - set worker flags and adjust nr_running accordingly |
| 604 | − * @worker: worker to set flags for | |
| 604 | + * @worker: self | |
| 605 | 605 | * @flags: flags to set |
| 606 | 606 | * @wakeup: wakeup an idle worker if necessary |
| 607 | 607 | * |
| @@ -609,14 +609,16 @@ struct task_struct *wq_worker_sleeping(struct task_struct *task, · 1 unmodified line | ||
| 609 | 609 | * nr_running becomes zero and @wakeup is %true, an idle worker is |
| 610 | 610 | * woken up. |
| 611 | 611 | * |
| 612 | − * LOCKING: | |
| 613 | − * spin_lock_irq(gcwq->lock). | |
| 612 | + * CONTEXT: | |
| 613 | + * spin_lock_irq(gcwq->lock) | |
| 614 | 614 | */ |
| 615 | 615 | static inline void worker_set_flags(struct worker *worker, unsigned int flags, |
| 616 | 616 | bool wakeup) |
| 617 | 617 | { |
| 618 | 618 | struct global_cwq *gcwq = worker->gcwq; |
| 619 | 619 | |
| 620 | + WARN_ON_ONCE(worker->task != current); | |
| 621 | + | |
| 620 | 622 | /* |
| 621 | 623 | * If transitioning into NOT_RUNNING, adjust nr_running and |
| 622 | 624 | * wake up an idle worker as necessary if requested by |
| @@ -639,19 +641,21 @@ static inline void worker_set_flags(struct worker *worker, unsigned int flags, · 16 unmodified lines | ||
| 639 | 641 | |
| 640 | 642 | /** |
| 641 | 643 | * worker_clr_flags - clear worker flags and adjust nr_running accordingly |
| 642 | − * @worker: worker to set flags for | |
| 644 | + * @worker: self | |
| 643 | 645 | * @flags: flags to clear |
| 644 | 646 | * |
| 645 | 647 | * Clear @flags in @worker->flags and adjust nr_running accordingly. |
| 646 | 648 | * |
| 647 | − * LOCKING: | |
| 648 | − * spin_lock_irq(gcwq->lock). | |
| 649 | + * CONTEXT: | |
| 650 | + * spin_lock_irq(gcwq->lock) | |
| 649 | 651 | */ |
| 650 | 652 | static inline void worker_clr_flags(struct worker *worker, unsigned int flags) |
| 651 | 653 | { |
| 652 | 654 | struct global_cwq *gcwq = worker->gcwq; |
| 653 | 655 | unsigned int oflags = worker->flags; |
| 654 | 656 | |
| 657 | + WARN_ON_ONCE(worker->task != current); | |
| 658 | + | |
| 655 | 659 | worker->flags &= ~flags; |
| 656 | 660 | |
| 657 | 661 | /* if transitioning out of NOT_RUNNING, increment nr_running */ |
| @@ -1073,7 +1077,8 @@ static void worker_enter_idle(struct worker *worker) · 415 unmodified lines | ||
| 1073 | 1077 | BUG_ON(!list_empty(&worker->entry) && |
| 1074 | 1078 | (worker->hentry.next || worker->hentry.pprev)); |
| 1075 | 1079 | |
| 1076 | − worker_set_flags(worker, WORKER_IDLE, false); | |
| 1080 | + /* can't use worker_set_flags(), also called from start_worker() */ | |
| 1081 | + worker->flags |= WORKER_IDLE; | |
| 1077 | 1082 | gcwq->nr_idle++; |
| 1078 | 1083 | worker->last_active = jiffies; |
| 1079 | 1084 | |
| @@ -1086,6 +1091,10 @@ static void worker_enter_idle(struct worker *worker) · 6 unmodified lines | ||
| 1086 | 1091 | jiffies + IDLE_WORKER_TIMEOUT); |
| 1087 | 1092 | } else |
| 1088 | 1093 | wake_up_all(&gcwq->trustee_wait); |
| 1094 | + | |
| 1095 | + /* sanity check nr_running */ | |
| 1096 | + WARN_ON_ONCE(gcwq->nr_workers == gcwq->nr_idle && | |
| 1097 | + atomic_read(get_gcwq_nr_running(gcwq->cpu))); | |
| 1089 | 1098 | } |
| 1090 | 1099 | |
| 1091 | 1100 | /** |
| @@ -1270,7 +1279,7 @@ fail: · 178 unmodified lines | ||
| 1270 | 1279 | */ |
| 1271 | 1280 | static void start_worker(struct worker *worker) |
| 1272 | 1281 | { |
| 1273 | − worker_set_flags(worker, WORKER_STARTED, false); | |
| 1282 | + worker->flags |= WORKER_STARTED; | |
| 1274 | 1283 | worker->gcwq->nr_workers++; |
| 1275 | 1284 | worker_enter_idle(worker); |
| 1276 | 1285 | wake_up_process(worker->task); |
| @@ -1300,7 +1309,7 @@ static void destroy_worker(struct worker *worker) · 23 unmodified lines | ||
| 1300 | 1309 | gcwq->nr_idle--; |
| 1301 | 1310 | |
| 1302 | 1311 | list_del_init(&worker->entry); |
| 1303 | − worker_set_flags(worker, WORKER_DIE, false); | |
| 1312 | + worker->flags |= WORKER_DIE; | |
| 1304 | 1313 | |
| 1305 | 1314 | spin_unlock_irq(&gcwq->lock); |
| 1306 | 1315 | |
| @@ -2979,10 +2988,10 @@ static int __cpuinit trustee_thread(void *__gcwq) · 1672 unmodified lines | ||
| 2979 | 2988 | gcwq->flags |= GCWQ_MANAGING_WORKERS; |
| 2980 | 2989 | |
| 2981 | 2990 | list_for_each_entry(worker, &gcwq->idle_list, entry) |
| 2982 | − worker_set_flags(worker, WORKER_ROGUE, false); | |
| 2991 | + worker->flags |= WORKER_ROGUE; | |
| 2983 | 2992 | |
| 2984 | 2993 | for_each_busy_worker(worker, i, pos, gcwq) |
| 2985 | − worker_set_flags(worker, WORKER_ROGUE, false); | |
| 2994 | + worker->flags |= WORKER_ROGUE; | |
| 2986 | 2995 | |
| 2987 | 2996 | /* |
| 2988 | 2997 | * Call schedule() so that we cross rq->lock and thus can |
| @@ -2995,12 +3004,12 @@ static int __cpuinit trustee_thread(void *__gcwq) · 6 unmodified lines | ||
| 2995 | 3004 | spin_lock_irq(&gcwq->lock); |
| 2996 | 3005 | |
| 2997 | 3006 | /* |
| 2998 | − * Sched callbacks are disabled now. gcwq->nr_running should | |
| 2999 | − * be zero and will stay that way, making need_more_worker() | |
| 3000 | − * and keep_working() always return true as long as the | |
| 3001 | − * worklist is not empty. | |
| 3007 | + * Sched callbacks are disabled now. Zap nr_running. After | |
| 3008 | + * this, nr_running stays zero and need_more_worker() and | |
| 3009 | + * keep_working() are always true as long as the worklist is | |
| 3010 | + * not empty. | |
| 3002 | 3011 | */ |
| 3003 | − WARN_ON_ONCE(atomic_read(get_gcwq_nr_running(gcwq->cpu)) != 0); | |
| 3012 | + atomic_set(get_gcwq_nr_running(gcwq->cpu), 0); | |
| 3004 | 3013 | |
| 3005 | 3014 | spin_unlock_irq(&gcwq->lock); |
| 3006 | 3015 | del_timer_sync(&gcwq->idle_timer); |
| @@ -3046,7 +3055,7 @@ static int __cpuinit trustee_thread(void *__gcwq) · 39 unmodified lines | ||
| 3046 | 3055 | worker = create_worker(gcwq, false); |
| 3047 | 3056 | spin_lock_irq(&gcwq->lock); |
| 3048 | 3057 | if (worker) { |
| 3049 | − worker_set_flags(worker, WORKER_ROGUE, false); | |
| 3058 | + worker->flags |= WORKER_ROGUE; | |
| 3050 | 3059 | start_worker(worker); |
| 3051 | 3060 | } |
| 3052 | 3061 | } |
| @@ -3085,8 +3094,8 @@ static int __cpuinit trustee_thread(void *__gcwq) · 32 unmodified lines | ||
| 3085 | 3094 | * operations. Use a separate flag to mark that |
| 3086 | 3095 | * rebinding is scheduled. |
| 3087 | 3096 | */ |
| 3088 | − worker_set_flags(worker, WORKER_REBIND, false); | |
| 3089 | − worker_clr_flags(worker, WORKER_ROGUE); | |
| 3097 | + worker->flags |= WORKER_REBIND; | |
| 3098 | + worker->flags &= ~WORKER_ROGUE; | |
| 3090 | 3099 | |
| 3091 | 3100 | /* queue rebind_work, wq doesn't matter, use the default one */ |
| 3092 | 3101 | if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, |