11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/workqueue.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Generic mechanism for defining kernel helper threads for running 51da177e4SLinus Torvalds * arbitrary tasks in process context. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Started by Ingo Molnar, Copyright (C) 2002 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * David Woodhouse <[email protected]> 12e1f8e874SFrancois Cami * Andrew Morton 131da177e4SLinus Torvalds * Kai Petzke <[email protected]> 141da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1589ada679SChristoph Lameter * 16cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/kernel.h> 211da177e4SLinus Torvalds #include <linux/sched.h> 221da177e4SLinus Torvalds #include <linux/init.h> 231da177e4SLinus Torvalds #include <linux/signal.h> 241da177e4SLinus Torvalds #include <linux/completion.h> 251da177e4SLinus Torvalds #include <linux/workqueue.h> 261da177e4SLinus Torvalds #include <linux/slab.h> 271da177e4SLinus Torvalds #include <linux/cpu.h> 281da177e4SLinus Torvalds #include <linux/notifier.h> 291da177e4SLinus Torvalds #include <linux/kthread.h> 301fa44ecaSJames Bottomley #include <linux/hardirq.h> 3146934023SChristoph Lameter #include <linux/mempolicy.h> 32341a5958SRafael J. Wysocki #include <linux/freezer.h> 33d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 34d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 354e6045f1SJohannes Berg #include <linux/lockdep.h> 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /* 384690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 394690c4abSTejun Heo * 404690c4abSTejun Heo * I: Set during initialization and read-only afterwards. 414690c4abSTejun Heo * 424690c4abSTejun Heo * L: cwq->lock protected. Access with cwq->lock held. 434690c4abSTejun Heo * 444690c4abSTejun Heo * W: workqueue_lock protected. 454690c4abSTejun Heo */ 464690c4abSTejun Heo 474690c4abSTejun Heo /* 48f756d5e2SNathan Lynch * The per-CPU workqueue (if single thread, we always use the first 49f756d5e2SNathan Lynch * possible cpu). 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds struct cpu_workqueue_struct { 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds spinlock_t lock; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds struct list_head worklist; 561da177e4SLinus Torvalds wait_queue_head_t more_work; 573af24433SOleg Nesterov struct work_struct *current_work; 58*1537663fSTejun Heo unsigned int cpu; 591da177e4SLinus Torvalds 604690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 6136c8b586SIngo Molnar struct task_struct *thread; 621da177e4SLinus Torvalds } ____cacheline_aligned; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 661da177e4SLinus Torvalds * per-CPU workqueues: 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds struct workqueue_struct { 6997e37d7bSTejun Heo unsigned int flags; /* I: WQ_* flags */ 704690c4abSTejun Heo struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */ 714690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 724690c4abSTejun Heo const char *name; /* I: workqueue name */ 734e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 744e6045f1SJohannes Berg struct lockdep_map lockdep_map; 754e6045f1SJohannes Berg #endif 761da177e4SLinus Torvalds }; 771da177e4SLinus Torvalds 78dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 79dc186ad7SThomas Gleixner 80dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 81dc186ad7SThomas Gleixner 82dc186ad7SThomas Gleixner /* 83dc186ad7SThomas Gleixner * fixup_init is called when: 84dc186ad7SThomas Gleixner * - an active object is initialized 85dc186ad7SThomas Gleixner */ 86dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 87dc186ad7SThomas Gleixner { 88dc186ad7SThomas Gleixner struct work_struct *work = addr; 89dc186ad7SThomas Gleixner 90dc186ad7SThomas Gleixner switch (state) { 91dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 92dc186ad7SThomas Gleixner cancel_work_sync(work); 93dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 94dc186ad7SThomas Gleixner return 1; 95dc186ad7SThomas Gleixner default: 96dc186ad7SThomas Gleixner return 0; 97dc186ad7SThomas Gleixner } 98dc186ad7SThomas Gleixner } 99dc186ad7SThomas Gleixner 100dc186ad7SThomas Gleixner /* 101dc186ad7SThomas Gleixner * fixup_activate is called when: 102dc186ad7SThomas Gleixner * - an active object is activated 103dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 104dc186ad7SThomas Gleixner */ 105dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 106dc186ad7SThomas Gleixner { 107dc186ad7SThomas Gleixner struct work_struct *work = addr; 108dc186ad7SThomas Gleixner 109dc186ad7SThomas Gleixner switch (state) { 110dc186ad7SThomas Gleixner 111dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 112dc186ad7SThomas Gleixner /* 113dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 114dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 115dc186ad7SThomas Gleixner * is tracked in the object tracker. 116dc186ad7SThomas Gleixner */ 11722df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 118dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 119dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 120dc186ad7SThomas Gleixner return 0; 121dc186ad7SThomas Gleixner } 122dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 123dc186ad7SThomas Gleixner return 0; 124dc186ad7SThomas Gleixner 125dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 126dc186ad7SThomas Gleixner WARN_ON(1); 127dc186ad7SThomas Gleixner 128dc186ad7SThomas Gleixner default: 129dc186ad7SThomas Gleixner return 0; 130dc186ad7SThomas Gleixner } 131dc186ad7SThomas Gleixner } 132dc186ad7SThomas Gleixner 133dc186ad7SThomas Gleixner /* 134dc186ad7SThomas Gleixner * fixup_free is called when: 135dc186ad7SThomas Gleixner * - an active object is freed 136dc186ad7SThomas Gleixner */ 137dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 138dc186ad7SThomas Gleixner { 139dc186ad7SThomas Gleixner struct work_struct *work = addr; 140dc186ad7SThomas Gleixner 141dc186ad7SThomas Gleixner switch (state) { 142dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 143dc186ad7SThomas Gleixner cancel_work_sync(work); 144dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 145dc186ad7SThomas Gleixner return 1; 146dc186ad7SThomas Gleixner default: 147dc186ad7SThomas Gleixner return 0; 148dc186ad7SThomas Gleixner } 149dc186ad7SThomas Gleixner } 150dc186ad7SThomas Gleixner 151dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 152dc186ad7SThomas Gleixner .name = "work_struct", 153dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 154dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 155dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 156dc186ad7SThomas Gleixner }; 157dc186ad7SThomas Gleixner 158dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 159dc186ad7SThomas Gleixner { 160dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 161dc186ad7SThomas Gleixner } 162dc186ad7SThomas Gleixner 163dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 164dc186ad7SThomas Gleixner { 165dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 166dc186ad7SThomas Gleixner } 167dc186ad7SThomas Gleixner 168dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 169dc186ad7SThomas Gleixner { 170dc186ad7SThomas Gleixner if (onstack) 171dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 172dc186ad7SThomas Gleixner else 173dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 174dc186ad7SThomas Gleixner } 175dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 176dc186ad7SThomas Gleixner 177dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 178dc186ad7SThomas Gleixner { 179dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 180dc186ad7SThomas Gleixner } 181dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 182dc186ad7SThomas Gleixner 183dc186ad7SThomas Gleixner #else 184dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 185dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 186dc186ad7SThomas Gleixner #endif 187dc186ad7SThomas Gleixner 18895402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 18995402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 1901da177e4SLinus Torvalds static LIST_HEAD(workqueues); 1911da177e4SLinus Torvalds 1923af24433SOleg Nesterov static int singlethread_cpu __read_mostly; 193b1f4ec17SOleg Nesterov 1944690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, 1954690c4abSTejun Heo struct workqueue_struct *wq) 196a848e3b6SOleg Nesterov { 197a848e3b6SOleg Nesterov return per_cpu_ptr(wq->cpu_wq, cpu); 198a848e3b6SOleg Nesterov } 199a848e3b6SOleg Nesterov 200*1537663fSTejun Heo static struct cpu_workqueue_struct *target_cwq(unsigned int cpu, 201*1537663fSTejun Heo struct workqueue_struct *wq) 202*1537663fSTejun Heo { 203*1537663fSTejun Heo if (unlikely(wq->flags & WQ_SINGLE_THREAD)) 204*1537663fSTejun Heo cpu = singlethread_cpu; 205*1537663fSTejun Heo return get_cwq(cpu, wq); 206*1537663fSTejun Heo } 207*1537663fSTejun Heo 2084594bf15SDavid Howells /* 2094594bf15SDavid Howells * Set the workqueue on which a work item is to be run 2104594bf15SDavid Howells * - Must *only* be called if the pending flag is set 2114594bf15SDavid Howells */ 212ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work, 2134690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 2144690c4abSTejun Heo unsigned long extra_flags) 215365970a1SDavid Howells { 2164594bf15SDavid Howells BUG_ON(!work_pending(work)); 2174594bf15SDavid Howells 2184690c4abSTejun Heo atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) | 21922df02bbSTejun Heo WORK_STRUCT_PENDING | extra_flags); 220365970a1SDavid Howells } 221365970a1SDavid Howells 2224d707b9fSOleg Nesterov /* 2234d707b9fSOleg Nesterov * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued. 2244d707b9fSOleg Nesterov */ 2254d707b9fSOleg Nesterov static inline void clear_wq_data(struct work_struct *work) 2264d707b9fSOleg Nesterov { 2274690c4abSTejun Heo atomic_long_set(&work->data, work_static(work)); 2284d707b9fSOleg Nesterov } 2294d707b9fSOleg Nesterov 23064166699STejun Heo static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) 231365970a1SDavid Howells { 23264166699STejun Heo return (void *)(atomic_long_read(&work->data) & 23364166699STejun Heo WORK_STRUCT_WQ_DATA_MASK); 234365970a1SDavid Howells } 235365970a1SDavid Howells 2364690c4abSTejun Heo /** 2374690c4abSTejun Heo * insert_work - insert a work into cwq 2384690c4abSTejun Heo * @cwq: cwq @work belongs to 2394690c4abSTejun Heo * @work: work to insert 2404690c4abSTejun Heo * @head: insertion point 2414690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 2424690c4abSTejun Heo * 2434690c4abSTejun Heo * Insert @work into @cwq after @head. 2444690c4abSTejun Heo * 2454690c4abSTejun Heo * CONTEXT: 2464690c4abSTejun Heo * spin_lock_irq(cwq->lock). 2474690c4abSTejun Heo */ 248b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 2494690c4abSTejun Heo struct work_struct *work, struct list_head *head, 2504690c4abSTejun Heo unsigned int extra_flags) 251b89deed3SOleg Nesterov { 2524690c4abSTejun Heo /* we own @work, set data and link */ 2534690c4abSTejun Heo set_wq_data(work, cwq, extra_flags); 2544690c4abSTejun Heo 2556e84d644SOleg Nesterov /* 2566e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 2576e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 2586e84d644SOleg Nesterov */ 2596e84d644SOleg Nesterov smp_wmb(); 2604690c4abSTejun Heo 2611a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 262b89deed3SOleg Nesterov wake_up(&cwq->more_work); 263b89deed3SOleg Nesterov } 264b89deed3SOleg Nesterov 2654690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 2661da177e4SLinus Torvalds struct work_struct *work) 2671da177e4SLinus Torvalds { 268*1537663fSTejun Heo struct cpu_workqueue_struct *cwq = target_cwq(cpu, wq); 2691da177e4SLinus Torvalds unsigned long flags; 2701da177e4SLinus Torvalds 271dc186ad7SThomas Gleixner debug_work_activate(work); 2721da177e4SLinus Torvalds spin_lock_irqsave(&cwq->lock, flags); 2734690c4abSTejun Heo BUG_ON(!list_empty(&work->entry)); 2744690c4abSTejun Heo insert_work(cwq, work, &cwq->worklist, 0); 2751da177e4SLinus Torvalds spin_unlock_irqrestore(&cwq->lock, flags); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 2780fcb78c2SRolf Eike Beer /** 2790fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 2800fcb78c2SRolf Eike Beer * @wq: workqueue to use 2810fcb78c2SRolf Eike Beer * @work: work to queue 2820fcb78c2SRolf Eike Beer * 283057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 2841da177e4SLinus Torvalds * 28500dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 28600dfcaf7SOleg Nesterov * it can be processed by another CPU. 2871da177e4SLinus Torvalds */ 2887ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 2891da177e4SLinus Torvalds { 290ef1ca236SOleg Nesterov int ret; 2911da177e4SLinus Torvalds 292ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 293a848e3b6SOleg Nesterov put_cpu(); 294ef1ca236SOleg Nesterov 2951da177e4SLinus Torvalds return ret; 2961da177e4SLinus Torvalds } 297ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 2981da177e4SLinus Torvalds 299c1a220e7SZhang Rui /** 300c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 301c1a220e7SZhang Rui * @cpu: CPU number to execute work on 302c1a220e7SZhang Rui * @wq: workqueue to use 303c1a220e7SZhang Rui * @work: work to queue 304c1a220e7SZhang Rui * 305c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 306c1a220e7SZhang Rui * 307c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 308c1a220e7SZhang Rui * can't go away. 309c1a220e7SZhang Rui */ 310c1a220e7SZhang Rui int 311c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 312c1a220e7SZhang Rui { 313c1a220e7SZhang Rui int ret = 0; 314c1a220e7SZhang Rui 31522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 3164690c4abSTejun Heo __queue_work(cpu, wq, work); 317c1a220e7SZhang Rui ret = 1; 318c1a220e7SZhang Rui } 319c1a220e7SZhang Rui return ret; 320c1a220e7SZhang Rui } 321c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 322c1a220e7SZhang Rui 3236d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 3241da177e4SLinus Torvalds { 32552bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 326ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); 3271da177e4SLinus Torvalds 3284690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3310fcb78c2SRolf Eike Beer /** 3320fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 3330fcb78c2SRolf Eike Beer * @wq: workqueue to use 334af9997e4SRandy Dunlap * @dwork: delayable work to queue 3350fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 3360fcb78c2SRolf Eike Beer * 337057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 3380fcb78c2SRolf Eike Beer */ 3397ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 34052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 3411da177e4SLinus Torvalds { 34252bad64dSDavid Howells if (delay == 0) 34363bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 3441da177e4SLinus Torvalds 34563bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 3461da177e4SLinus Torvalds } 347ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 3481da177e4SLinus Torvalds 3490fcb78c2SRolf Eike Beer /** 3500fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 3510fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 3520fcb78c2SRolf Eike Beer * @wq: workqueue to use 353af9997e4SRandy Dunlap * @dwork: work to queue 3540fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 3550fcb78c2SRolf Eike Beer * 356057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 3570fcb78c2SRolf Eike Beer */ 3587a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 35952bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 3607a6bc1cdSVenkatesh Pallipadi { 3617a6bc1cdSVenkatesh Pallipadi int ret = 0; 36252bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 36352bad64dSDavid Howells struct work_struct *work = &dwork->work; 3647a6bc1cdSVenkatesh Pallipadi 36522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 3667a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 3677a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 3687a6bc1cdSVenkatesh Pallipadi 3698a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 3708a3e77ccSAndrew Liu 371ed7c0feeSOleg Nesterov /* This stores cwq for the moment, for the timer_fn */ 372*1537663fSTejun Heo set_wq_data(work, target_cwq(raw_smp_processor_id(), wq), 0); 3737a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 37452bad64dSDavid Howells timer->data = (unsigned long)dwork; 3757a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 37663bc0362SOleg Nesterov 37763bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 3787a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 37963bc0362SOleg Nesterov else 38063bc0362SOleg Nesterov add_timer(timer); 3817a6bc1cdSVenkatesh Pallipadi ret = 1; 3827a6bc1cdSVenkatesh Pallipadi } 3837a6bc1cdSVenkatesh Pallipadi return ret; 3847a6bc1cdSVenkatesh Pallipadi } 385ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 3861da177e4SLinus Torvalds 387a62428c0STejun Heo /** 388a62428c0STejun Heo * process_one_work - process single work 389a62428c0STejun Heo * @cwq: cwq to process work for 390a62428c0STejun Heo * @work: work to process 391a62428c0STejun Heo * 392a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 393a62428c0STejun Heo * process a single work including synchronization against and 394a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 395a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 396a62428c0STejun Heo * call this function to process a work. 397a62428c0STejun Heo * 398a62428c0STejun Heo * CONTEXT: 399a62428c0STejun Heo * spin_lock_irq(cwq->lock) which is released and regrabbed. 400a62428c0STejun Heo */ 401a62428c0STejun Heo static void process_one_work(struct cpu_workqueue_struct *cwq, 402a62428c0STejun Heo struct work_struct *work) 4031da177e4SLinus Torvalds { 4046bb49e59SDavid Howells work_func_t f = work->func; 4054e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 4064e6045f1SJohannes Berg /* 407a62428c0STejun Heo * It is permissible to free the struct work_struct from 408a62428c0STejun Heo * inside the function that is called from it, this we need to 409a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 410a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 411a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 4124e6045f1SJohannes Berg */ 4134e6045f1SJohannes Berg struct lockdep_map lockdep_map = work->lockdep_map; 4144e6045f1SJohannes Berg #endif 415a62428c0STejun Heo /* claim and process */ 416dc186ad7SThomas Gleixner debug_work_deactivate(work); 417b89deed3SOleg Nesterov cwq->current_work = work; 418a62428c0STejun Heo list_del_init(&work->entry); 419a62428c0STejun Heo 420f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 4211da177e4SLinus Torvalds 422365970a1SDavid Howells BUG_ON(get_wq_data(work) != cwq); 42323b2e599SOleg Nesterov work_clear_pending(work); 4243295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 4253295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 42665f27f38SDavid Howells f(work); 4273295f0efSIngo Molnar lock_map_release(&lockdep_map); 4283295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 4291da177e4SLinus Torvalds 430d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 431d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 432d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 433a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 434d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 435d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 436d5abe669SPeter Zijlstra debug_show_held_locks(current); 437d5abe669SPeter Zijlstra dump_stack(); 438d5abe669SPeter Zijlstra } 439d5abe669SPeter Zijlstra 440f293ea92SOleg Nesterov spin_lock_irq(&cwq->lock); 441a62428c0STejun Heo 442a62428c0STejun Heo /* we're done with it, release */ 443b89deed3SOleg Nesterov cwq->current_work = NULL; 4441da177e4SLinus Torvalds } 445a62428c0STejun Heo 446a62428c0STejun Heo static void run_workqueue(struct cpu_workqueue_struct *cwq) 447a62428c0STejun Heo { 448a62428c0STejun Heo spin_lock_irq(&cwq->lock); 449a62428c0STejun Heo while (!list_empty(&cwq->worklist)) { 450a62428c0STejun Heo struct work_struct *work = list_entry(cwq->worklist.next, 451a62428c0STejun Heo struct work_struct, entry); 452a62428c0STejun Heo process_one_work(cwq, work); 453a62428c0STejun Heo } 454f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4574690c4abSTejun Heo /** 4584690c4abSTejun Heo * worker_thread - the worker thread function 4594690c4abSTejun Heo * @__cwq: cwq to serve 4604690c4abSTejun Heo * 4614690c4abSTejun Heo * The cwq worker thread function. 4624690c4abSTejun Heo */ 4631da177e4SLinus Torvalds static int worker_thread(void *__cwq) 4641da177e4SLinus Torvalds { 4651da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq = __cwq; 4663af24433SOleg Nesterov DEFINE_WAIT(wait); 4671da177e4SLinus Torvalds 46897e37d7bSTejun Heo if (cwq->wq->flags & WQ_FREEZEABLE) 46983144186SRafael J. Wysocki set_freezable(); 4701da177e4SLinus Torvalds 4713af24433SOleg Nesterov for (;;) { 4723af24433SOleg Nesterov prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); 47314441960SOleg Nesterov if (!freezing(current) && 47414441960SOleg Nesterov !kthread_should_stop() && 47514441960SOleg Nesterov list_empty(&cwq->worklist)) 4761da177e4SLinus Torvalds schedule(); 4773af24433SOleg Nesterov finish_wait(&cwq->more_work, &wait); 4781da177e4SLinus Torvalds 47985f4186aSOleg Nesterov try_to_freeze(); 48085f4186aSOleg Nesterov 48114441960SOleg Nesterov if (kthread_should_stop()) 4823af24433SOleg Nesterov break; 4833af24433SOleg Nesterov 484*1537663fSTejun Heo if (unlikely(!cpumask_equal(&cwq->thread->cpus_allowed, 485*1537663fSTejun Heo get_cpu_mask(cwq->cpu)))) 486*1537663fSTejun Heo set_cpus_allowed_ptr(cwq->thread, 487*1537663fSTejun Heo get_cpu_mask(cwq->cpu)); 4881da177e4SLinus Torvalds run_workqueue(cwq); 4891da177e4SLinus Torvalds } 4903af24433SOleg Nesterov 4911da177e4SLinus Torvalds return 0; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 494fc2e4d70SOleg Nesterov struct wq_barrier { 495fc2e4d70SOleg Nesterov struct work_struct work; 496fc2e4d70SOleg Nesterov struct completion done; 497fc2e4d70SOleg Nesterov }; 498fc2e4d70SOleg Nesterov 499fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 500fc2e4d70SOleg Nesterov { 501fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 502fc2e4d70SOleg Nesterov complete(&barr->done); 503fc2e4d70SOleg Nesterov } 504fc2e4d70SOleg Nesterov 5054690c4abSTejun Heo /** 5064690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 5074690c4abSTejun Heo * @cwq: cwq to insert barrier into 5084690c4abSTejun Heo * @barr: wq_barrier to insert 5094690c4abSTejun Heo * @head: insertion point 5104690c4abSTejun Heo * 5114690c4abSTejun Heo * Insert barrier @barr into @cwq before @head. 5124690c4abSTejun Heo * 5134690c4abSTejun Heo * CONTEXT: 5144690c4abSTejun Heo * spin_lock_irq(cwq->lock). 5154690c4abSTejun Heo */ 51683c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 5171a4d9b0aSOleg Nesterov struct wq_barrier *barr, struct list_head *head) 518fc2e4d70SOleg Nesterov { 519dc186ad7SThomas Gleixner /* 520dc186ad7SThomas Gleixner * debugobject calls are safe here even with cwq->lock locked 521dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 522dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 523dc186ad7SThomas Gleixner * might deadlock. 524dc186ad7SThomas Gleixner */ 525dc186ad7SThomas Gleixner INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); 52622df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 527fc2e4d70SOleg Nesterov init_completion(&barr->done); 52883c22520SOleg Nesterov 529dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 5304690c4abSTejun Heo insert_work(cwq, &barr->work, head, 0); 531fc2e4d70SOleg Nesterov } 532fc2e4d70SOleg Nesterov 53314441960SOleg Nesterov static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) 5341da177e4SLinus Torvalds { 5352355b70fSLai Jiangshan int active = 0; 536fc2e4d70SOleg Nesterov struct wq_barrier barr; 5371da177e4SLinus Torvalds 5382355b70fSLai Jiangshan WARN_ON(cwq->thread == current); 5392355b70fSLai Jiangshan 54083c22520SOleg Nesterov spin_lock_irq(&cwq->lock); 54183c22520SOleg Nesterov if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { 5421a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, &cwq->worklist); 54383c22520SOleg Nesterov active = 1; 54483c22520SOleg Nesterov } 54583c22520SOleg Nesterov spin_unlock_irq(&cwq->lock); 5461da177e4SLinus Torvalds 547dc186ad7SThomas Gleixner if (active) { 548fc2e4d70SOleg Nesterov wait_for_completion(&barr.done); 549dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 550dc186ad7SThomas Gleixner } 55114441960SOleg Nesterov 55214441960SOleg Nesterov return active; 55383c22520SOleg Nesterov } 5541da177e4SLinus Torvalds 5550fcb78c2SRolf Eike Beer /** 5561da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 5570fcb78c2SRolf Eike Beer * @wq: workqueue to flush 5581da177e4SLinus Torvalds * 5591da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 5601da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 5611da177e4SLinus Torvalds * 562fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 563fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 5641da177e4SLinus Torvalds */ 5657ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 5661da177e4SLinus Torvalds { 567cce1a165SOleg Nesterov int cpu; 568b1f4ec17SOleg Nesterov 569f293ea92SOleg Nesterov might_sleep(); 5703295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 5713295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 572*1537663fSTejun Heo for_each_possible_cpu(cpu) 573*1537663fSTejun Heo flush_cpu_workqueue(get_cwq(cpu, wq)); 5741da177e4SLinus Torvalds } 575ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 5761da177e4SLinus Torvalds 577db700897SOleg Nesterov /** 578db700897SOleg Nesterov * flush_work - block until a work_struct's callback has terminated 579db700897SOleg Nesterov * @work: the work which is to be flushed 580db700897SOleg Nesterov * 581a67da70dSOleg Nesterov * Returns false if @work has already terminated. 582a67da70dSOleg Nesterov * 583db700897SOleg Nesterov * It is expected that, prior to calling flush_work(), the caller has 584db700897SOleg Nesterov * arranged for the work to not be requeued, otherwise it doesn't make 585db700897SOleg Nesterov * sense to use this function. 586db700897SOleg Nesterov */ 587db700897SOleg Nesterov int flush_work(struct work_struct *work) 588db700897SOleg Nesterov { 589db700897SOleg Nesterov struct cpu_workqueue_struct *cwq; 590db700897SOleg Nesterov struct list_head *prev; 591db700897SOleg Nesterov struct wq_barrier barr; 592db700897SOleg Nesterov 593db700897SOleg Nesterov might_sleep(); 594db700897SOleg Nesterov cwq = get_wq_data(work); 595db700897SOleg Nesterov if (!cwq) 596db700897SOleg Nesterov return 0; 597db700897SOleg Nesterov 5983295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 5993295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 600a67da70dSOleg Nesterov 601db700897SOleg Nesterov spin_lock_irq(&cwq->lock); 602db700897SOleg Nesterov if (!list_empty(&work->entry)) { 603db700897SOleg Nesterov /* 604db700897SOleg Nesterov * See the comment near try_to_grab_pending()->smp_rmb(). 605db700897SOleg Nesterov * If it was re-queued under us we are not going to wait. 606db700897SOleg Nesterov */ 607db700897SOleg Nesterov smp_rmb(); 608db700897SOleg Nesterov if (unlikely(cwq != get_wq_data(work))) 6094690c4abSTejun Heo goto already_gone; 610db700897SOleg Nesterov prev = &work->entry; 611db700897SOleg Nesterov } else { 612db700897SOleg Nesterov if (cwq->current_work != work) 6134690c4abSTejun Heo goto already_gone; 614db700897SOleg Nesterov prev = &cwq->worklist; 615db700897SOleg Nesterov } 616db700897SOleg Nesterov insert_wq_barrier(cwq, &barr, prev->next); 617db700897SOleg Nesterov 6184690c4abSTejun Heo spin_unlock_irq(&cwq->lock); 619db700897SOleg Nesterov wait_for_completion(&barr.done); 620dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 621db700897SOleg Nesterov return 1; 6224690c4abSTejun Heo already_gone: 6234690c4abSTejun Heo spin_unlock_irq(&cwq->lock); 6244690c4abSTejun Heo return 0; 625db700897SOleg Nesterov } 626db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 627db700897SOleg Nesterov 6286e84d644SOleg Nesterov /* 6291f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 6306e84d644SOleg Nesterov * so this work can't be re-armed in any way. 6316e84d644SOleg Nesterov */ 6326e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 6336e84d644SOleg Nesterov { 6346e84d644SOleg Nesterov struct cpu_workqueue_struct *cwq; 6351f1f642eSOleg Nesterov int ret = -1; 6366e84d644SOleg Nesterov 63722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 6381f1f642eSOleg Nesterov return 0; 6396e84d644SOleg Nesterov 6406e84d644SOleg Nesterov /* 6416e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 6426e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 6436e84d644SOleg Nesterov */ 6446e84d644SOleg Nesterov 6456e84d644SOleg Nesterov cwq = get_wq_data(work); 6466e84d644SOleg Nesterov if (!cwq) 6476e84d644SOleg Nesterov return ret; 6486e84d644SOleg Nesterov 6496e84d644SOleg Nesterov spin_lock_irq(&cwq->lock); 6506e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 6516e84d644SOleg Nesterov /* 6526e84d644SOleg Nesterov * This work is queued, but perhaps we locked the wrong cwq. 6536e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 6546e84d644SOleg Nesterov * insert_work()->wmb(). 6556e84d644SOleg Nesterov */ 6566e84d644SOleg Nesterov smp_rmb(); 6576e84d644SOleg Nesterov if (cwq == get_wq_data(work)) { 658dc186ad7SThomas Gleixner debug_work_deactivate(work); 6596e84d644SOleg Nesterov list_del_init(&work->entry); 6606e84d644SOleg Nesterov ret = 1; 6616e84d644SOleg Nesterov } 6626e84d644SOleg Nesterov } 6636e84d644SOleg Nesterov spin_unlock_irq(&cwq->lock); 6646e84d644SOleg Nesterov 6656e84d644SOleg Nesterov return ret; 6666e84d644SOleg Nesterov } 6676e84d644SOleg Nesterov 6686e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, 669b89deed3SOleg Nesterov struct work_struct *work) 670b89deed3SOleg Nesterov { 671b89deed3SOleg Nesterov struct wq_barrier barr; 672b89deed3SOleg Nesterov int running = 0; 673b89deed3SOleg Nesterov 674b89deed3SOleg Nesterov spin_lock_irq(&cwq->lock); 675b89deed3SOleg Nesterov if (unlikely(cwq->current_work == work)) { 6761a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, cwq->worklist.next); 677b89deed3SOleg Nesterov running = 1; 678b89deed3SOleg Nesterov } 679b89deed3SOleg Nesterov spin_unlock_irq(&cwq->lock); 680b89deed3SOleg Nesterov 681dc186ad7SThomas Gleixner if (unlikely(running)) { 682b89deed3SOleg Nesterov wait_for_completion(&barr.done); 683dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 684dc186ad7SThomas Gleixner } 685b89deed3SOleg Nesterov } 686b89deed3SOleg Nesterov 6876e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work) 688b89deed3SOleg Nesterov { 689b89deed3SOleg Nesterov struct cpu_workqueue_struct *cwq; 69028e53bddSOleg Nesterov struct workqueue_struct *wq; 691b1f4ec17SOleg Nesterov int cpu; 692b89deed3SOleg Nesterov 693f293ea92SOleg Nesterov might_sleep(); 694f293ea92SOleg Nesterov 6953295f0efSIngo Molnar lock_map_acquire(&work->lockdep_map); 6963295f0efSIngo Molnar lock_map_release(&work->lockdep_map); 6974e6045f1SJohannes Berg 698b89deed3SOleg Nesterov cwq = get_wq_data(work); 699b89deed3SOleg Nesterov if (!cwq) 7003af24433SOleg Nesterov return; 701b89deed3SOleg Nesterov 70228e53bddSOleg Nesterov wq = cwq->wq; 70328e53bddSOleg Nesterov 704*1537663fSTejun Heo for_each_possible_cpu(cpu) 7054690c4abSTejun Heo wait_on_cpu_work(get_cwq(cpu, wq), work); 7066e84d644SOleg Nesterov } 7076e84d644SOleg Nesterov 7081f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work, 7091f1f642eSOleg Nesterov struct timer_list* timer) 7101f1f642eSOleg Nesterov { 7111f1f642eSOleg Nesterov int ret; 7121f1f642eSOleg Nesterov 7131f1f642eSOleg Nesterov do { 7141f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 7151f1f642eSOleg Nesterov if (!ret) 7161f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 7171f1f642eSOleg Nesterov wait_on_work(work); 7181f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 7191f1f642eSOleg Nesterov 7204d707b9fSOleg Nesterov clear_wq_data(work); 7211f1f642eSOleg Nesterov return ret; 7221f1f642eSOleg Nesterov } 7231f1f642eSOleg Nesterov 7246e84d644SOleg Nesterov /** 7256e84d644SOleg Nesterov * cancel_work_sync - block until a work_struct's callback has terminated 7266e84d644SOleg Nesterov * @work: the work which is to be flushed 7276e84d644SOleg Nesterov * 7281f1f642eSOleg Nesterov * Returns true if @work was pending. 7291f1f642eSOleg Nesterov * 7306e84d644SOleg Nesterov * cancel_work_sync() will cancel the work if it is queued. If the work's 7316e84d644SOleg Nesterov * callback appears to be running, cancel_work_sync() will block until it 7326e84d644SOleg Nesterov * has completed. 7336e84d644SOleg Nesterov * 7346e84d644SOleg Nesterov * It is possible to use this function if the work re-queues itself. It can 7356e84d644SOleg Nesterov * cancel the work even if it migrates to another workqueue, however in that 7366e84d644SOleg Nesterov * case it only guarantees that work->func() has completed on the last queued 7376e84d644SOleg Nesterov * workqueue. 7386e84d644SOleg Nesterov * 7396e84d644SOleg Nesterov * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not 7406e84d644SOleg Nesterov * pending, otherwise it goes into a busy-wait loop until the timer expires. 7416e84d644SOleg Nesterov * 7426e84d644SOleg Nesterov * The caller must ensure that workqueue_struct on which this work was last 7436e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 7446e84d644SOleg Nesterov */ 7451f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work) 7466e84d644SOleg Nesterov { 7471f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 748b89deed3SOleg Nesterov } 74928e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 750b89deed3SOleg Nesterov 7516e84d644SOleg Nesterov /** 752f5a421a4SOleg Nesterov * cancel_delayed_work_sync - reliably kill off a delayed work. 7536e84d644SOleg Nesterov * @dwork: the delayed work struct 7546e84d644SOleg Nesterov * 7551f1f642eSOleg Nesterov * Returns true if @dwork was pending. 7561f1f642eSOleg Nesterov * 7576e84d644SOleg Nesterov * It is possible to use this function if @dwork rearms itself via queue_work() 7586e84d644SOleg Nesterov * or queue_delayed_work(). See also the comment for cancel_work_sync(). 7596e84d644SOleg Nesterov */ 7601f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork) 7616e84d644SOleg Nesterov { 7621f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 7636e84d644SOleg Nesterov } 764f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 7651da177e4SLinus Torvalds 7666e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly; 7671da177e4SLinus Torvalds 7680fcb78c2SRolf Eike Beer /** 7690fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 7700fcb78c2SRolf Eike Beer * @work: job to be done 7710fcb78c2SRolf Eike Beer * 7725b0f437dSBart Van Assche * Returns zero if @work was already on the kernel-global workqueue and 7735b0f437dSBart Van Assche * non-zero otherwise. 7745b0f437dSBart Van Assche * 7755b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 7765b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 7775b0f437dSBart Van Assche * workqueue otherwise. 7780fcb78c2SRolf Eike Beer */ 7797ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 7801da177e4SLinus Torvalds { 7811da177e4SLinus Torvalds return queue_work(keventd_wq, work); 7821da177e4SLinus Torvalds } 783ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 7841da177e4SLinus Torvalds 785c1a220e7SZhang Rui /* 786c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 787c1a220e7SZhang Rui * @cpu: cpu to put the work task on 788c1a220e7SZhang Rui * @work: job to be done 789c1a220e7SZhang Rui * 790c1a220e7SZhang Rui * This puts a job on a specific cpu 791c1a220e7SZhang Rui */ 792c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 793c1a220e7SZhang Rui { 794c1a220e7SZhang Rui return queue_work_on(cpu, keventd_wq, work); 795c1a220e7SZhang Rui } 796c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 797c1a220e7SZhang Rui 7980fcb78c2SRolf Eike Beer /** 7990fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 80052bad64dSDavid Howells * @dwork: job to be done 80152bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 8020fcb78c2SRolf Eike Beer * 8030fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 8040fcb78c2SRolf Eike Beer * workqueue. 8050fcb78c2SRolf Eike Beer */ 8067ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 80782f67cd9SIngo Molnar unsigned long delay) 8081da177e4SLinus Torvalds { 80952bad64dSDavid Howells return queue_delayed_work(keventd_wq, dwork, delay); 8101da177e4SLinus Torvalds } 811ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 8121da177e4SLinus Torvalds 8130fcb78c2SRolf Eike Beer /** 8148c53e463SLinus Torvalds * flush_delayed_work - block until a dwork_struct's callback has terminated 8158c53e463SLinus Torvalds * @dwork: the delayed work which is to be flushed 8168c53e463SLinus Torvalds * 8178c53e463SLinus Torvalds * Any timeout is cancelled, and any pending work is run immediately. 8188c53e463SLinus Torvalds */ 8198c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork) 8208c53e463SLinus Torvalds { 8218c53e463SLinus Torvalds if (del_timer_sync(&dwork->timer)) { 8224690c4abSTejun Heo __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq, 8234690c4abSTejun Heo &dwork->work); 8248c53e463SLinus Torvalds put_cpu(); 8258c53e463SLinus Torvalds } 8268c53e463SLinus Torvalds flush_work(&dwork->work); 8278c53e463SLinus Torvalds } 8288c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work); 8298c53e463SLinus Torvalds 8308c53e463SLinus Torvalds /** 8310fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 8320fcb78c2SRolf Eike Beer * @cpu: cpu to use 83352bad64dSDavid Howells * @dwork: job to be done 8340fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 8350fcb78c2SRolf Eike Beer * 8360fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 8370fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 8380fcb78c2SRolf Eike Beer */ 8391da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 84052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 8411da177e4SLinus Torvalds { 84252bad64dSDavid Howells return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); 8431da177e4SLinus Torvalds } 844ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 8451da177e4SLinus Torvalds 846b6136773SAndrew Morton /** 847b6136773SAndrew Morton * schedule_on_each_cpu - call a function on each online CPU from keventd 848b6136773SAndrew Morton * @func: the function to call 849b6136773SAndrew Morton * 850b6136773SAndrew Morton * Returns zero on success. 851b6136773SAndrew Morton * Returns -ve errno on failure. 852b6136773SAndrew Morton * 853b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 854b6136773SAndrew Morton */ 85565f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 85615316ba8SChristoph Lameter { 85715316ba8SChristoph Lameter int cpu; 85865a64464SAndi Kleen int orig = -1; 859b6136773SAndrew Morton struct work_struct *works; 86015316ba8SChristoph Lameter 861b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 862b6136773SAndrew Morton if (!works) 86315316ba8SChristoph Lameter return -ENOMEM; 864b6136773SAndrew Morton 86595402b38SGautham R Shenoy get_online_cpus(); 86693981800STejun Heo 86793981800STejun Heo /* 86893981800STejun Heo * When running in keventd don't schedule a work item on 86993981800STejun Heo * itself. Can just call directly because the work queue is 87093981800STejun Heo * already bound. This also is faster. 87193981800STejun Heo */ 87293981800STejun Heo if (current_is_keventd()) 87393981800STejun Heo orig = raw_smp_processor_id(); 87493981800STejun Heo 87515316ba8SChristoph Lameter for_each_online_cpu(cpu) { 8769bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 8779bfb1839SIngo Molnar 8789bfb1839SIngo Molnar INIT_WORK(work, func); 87993981800STejun Heo if (cpu != orig) 8808de6d308SOleg Nesterov schedule_work_on(cpu, work); 88115316ba8SChristoph Lameter } 88293981800STejun Heo if (orig >= 0) 88393981800STejun Heo func(per_cpu_ptr(works, orig)); 88493981800STejun Heo 88593981800STejun Heo for_each_online_cpu(cpu) 8868616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 88793981800STejun Heo 88895402b38SGautham R Shenoy put_online_cpus(); 889b6136773SAndrew Morton free_percpu(works); 89015316ba8SChristoph Lameter return 0; 89115316ba8SChristoph Lameter } 89215316ba8SChristoph Lameter 893eef6a7d5SAlan Stern /** 894eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 895eef6a7d5SAlan Stern * 896eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 897eef6a7d5SAlan Stern * completion. 898eef6a7d5SAlan Stern * 899eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 900eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 901eef6a7d5SAlan Stern * will lead to deadlock: 902eef6a7d5SAlan Stern * 903eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 904eef6a7d5SAlan Stern * a lock held by your code or its caller. 905eef6a7d5SAlan Stern * 906eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 907eef6a7d5SAlan Stern * 908eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 909eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 910eef6a7d5SAlan Stern * what locks they need, which you have no control over. 911eef6a7d5SAlan Stern * 912eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 913eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 914eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 915eef6a7d5SAlan Stern * cancel_work_sync() instead. 916eef6a7d5SAlan Stern */ 9171da177e4SLinus Torvalds void flush_scheduled_work(void) 9181da177e4SLinus Torvalds { 9191da177e4SLinus Torvalds flush_workqueue(keventd_wq); 9201da177e4SLinus Torvalds } 921ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds /** 9241fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 9251fa44ecaSJames Bottomley * @fn: the function to execute 9261fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 9271fa44ecaSJames Bottomley * be available when the work executes) 9281fa44ecaSJames Bottomley * 9291fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 9301fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 9311fa44ecaSJames Bottomley * 9321fa44ecaSJames Bottomley * Returns: 0 - function was executed 9331fa44ecaSJames Bottomley * 1 - function was scheduled for execution 9341fa44ecaSJames Bottomley */ 93565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 9361fa44ecaSJames Bottomley { 9371fa44ecaSJames Bottomley if (!in_interrupt()) { 93865f27f38SDavid Howells fn(&ew->work); 9391fa44ecaSJames Bottomley return 0; 9401fa44ecaSJames Bottomley } 9411fa44ecaSJames Bottomley 94265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 9431fa44ecaSJames Bottomley schedule_work(&ew->work); 9441fa44ecaSJames Bottomley 9451fa44ecaSJames Bottomley return 1; 9461fa44ecaSJames Bottomley } 9471fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 9481fa44ecaSJames Bottomley 9491da177e4SLinus Torvalds int keventd_up(void) 9501da177e4SLinus Torvalds { 9511da177e4SLinus Torvalds return keventd_wq != NULL; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds int current_is_keventd(void) 9551da177e4SLinus Torvalds { 9561da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq; 957d243769dSHugh Dickins int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ 9581da177e4SLinus Torvalds int ret = 0; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds BUG_ON(!keventd_wq); 9611da177e4SLinus Torvalds 962*1537663fSTejun Heo cwq = get_cwq(cpu, keventd_wq); 9631da177e4SLinus Torvalds if (current == cwq->thread) 9641da177e4SLinus Torvalds ret = 1; 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds return ret; 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9703af24433SOleg Nesterov static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 9713af24433SOleg Nesterov { 9723af24433SOleg Nesterov struct workqueue_struct *wq = cwq->wq; 9733af24433SOleg Nesterov struct task_struct *p; 9743af24433SOleg Nesterov 975*1537663fSTejun Heo p = kthread_create(worker_thread, cwq, "%s/%d", wq->name, cpu); 9763af24433SOleg Nesterov /* 9773af24433SOleg Nesterov * Nobody can add the work_struct to this cwq, 9783af24433SOleg Nesterov * if (caller is __create_workqueue) 9793af24433SOleg Nesterov * nobody should see this wq 9803af24433SOleg Nesterov * else // caller is CPU_UP_PREPARE 9813af24433SOleg Nesterov * cpu is not on cpu_online_map 9823af24433SOleg Nesterov * so we can abort safely. 9833af24433SOleg Nesterov */ 9843af24433SOleg Nesterov if (IS_ERR(p)) 9853af24433SOleg Nesterov return PTR_ERR(p); 9863af24433SOleg Nesterov cwq->thread = p; 9873af24433SOleg Nesterov 9883af24433SOleg Nesterov return 0; 9893af24433SOleg Nesterov } 9903af24433SOleg Nesterov 99106ba38a9SOleg Nesterov static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 99206ba38a9SOleg Nesterov { 99306ba38a9SOleg Nesterov struct task_struct *p = cwq->thread; 99406ba38a9SOleg Nesterov 99506ba38a9SOleg Nesterov if (p != NULL) { 99606ba38a9SOleg Nesterov if (cpu >= 0) 99706ba38a9SOleg Nesterov kthread_bind(p, cpu); 99806ba38a9SOleg Nesterov wake_up_process(p); 99906ba38a9SOleg Nesterov } 100006ba38a9SOleg Nesterov } 100106ba38a9SOleg Nesterov 10024e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name, 100397e37d7bSTejun Heo unsigned int flags, 1004eb13ba87SJohannes Berg struct lock_class_key *key, 1005eb13ba87SJohannes Berg const char *lock_name) 10063af24433SOleg Nesterov { 1007*1537663fSTejun Heo bool singlethread = flags & WQ_SINGLE_THREAD; 10083af24433SOleg Nesterov struct workqueue_struct *wq; 10093af24433SOleg Nesterov int err = 0, cpu; 10103af24433SOleg Nesterov 10113af24433SOleg Nesterov wq = kzalloc(sizeof(*wq), GFP_KERNEL); 10123af24433SOleg Nesterov if (!wq) 10134690c4abSTejun Heo goto err; 10143af24433SOleg Nesterov 10153af24433SOleg Nesterov wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); 10164690c4abSTejun Heo if (!wq->cpu_wq) 10174690c4abSTejun Heo goto err; 10183af24433SOleg Nesterov 101997e37d7bSTejun Heo wq->flags = flags; 10203af24433SOleg Nesterov wq->name = name; 1021eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 1022cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 10233af24433SOleg Nesterov 10243da1c84cSOleg Nesterov cpu_maps_update_begin(); 10256af8bf3dSOleg Nesterov /* 10266af8bf3dSOleg Nesterov * We must initialize cwqs for each possible cpu even if we 10276af8bf3dSOleg Nesterov * are going to call destroy_workqueue() finally. Otherwise 10286af8bf3dSOleg Nesterov * cpu_up() can hit the uninitialized cwq once we drop the 10296af8bf3dSOleg Nesterov * lock. 10306af8bf3dSOleg Nesterov */ 10313af24433SOleg Nesterov for_each_possible_cpu(cpu) { 1032*1537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 1033*1537663fSTejun Heo 1034*1537663fSTejun Heo cwq->wq = wq; 1035*1537663fSTejun Heo cwq->cpu = cpu; 1036*1537663fSTejun Heo spin_lock_init(&cwq->lock); 1037*1537663fSTejun Heo INIT_LIST_HEAD(&cwq->worklist); 1038*1537663fSTejun Heo init_waitqueue_head(&cwq->more_work); 1039*1537663fSTejun Heo 1040*1537663fSTejun Heo if (err) 10413af24433SOleg Nesterov continue; 10423af24433SOleg Nesterov err = create_workqueue_thread(cwq, cpu); 1043*1537663fSTejun Heo if (cpu_online(cpu) && !singlethread) 104406ba38a9SOleg Nesterov start_workqueue_thread(cwq, cpu); 1045*1537663fSTejun Heo else 1046*1537663fSTejun Heo start_workqueue_thread(cwq, -1); 10473af24433SOleg Nesterov } 1048*1537663fSTejun Heo 1049*1537663fSTejun Heo spin_lock(&workqueue_lock); 1050*1537663fSTejun Heo list_add(&wq->list, &workqueues); 1051*1537663fSTejun Heo spin_unlock(&workqueue_lock); 1052*1537663fSTejun Heo 10533da1c84cSOleg Nesterov cpu_maps_update_done(); 10543af24433SOleg Nesterov 10553af24433SOleg Nesterov if (err) { 10563af24433SOleg Nesterov destroy_workqueue(wq); 10573af24433SOleg Nesterov wq = NULL; 10583af24433SOleg Nesterov } 10593af24433SOleg Nesterov return wq; 10604690c4abSTejun Heo err: 10614690c4abSTejun Heo if (wq) { 10624690c4abSTejun Heo free_percpu(wq->cpu_wq); 10634690c4abSTejun Heo kfree(wq); 10644690c4abSTejun Heo } 10654690c4abSTejun Heo return NULL; 10663af24433SOleg Nesterov } 10674e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key); 10683af24433SOleg Nesterov 10691e35eaa2SOleg Nesterov static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq) 10703af24433SOleg Nesterov { 10713af24433SOleg Nesterov /* 10723da1c84cSOleg Nesterov * Our caller is either destroy_workqueue() or CPU_POST_DEAD, 10733da1c84cSOleg Nesterov * cpu_add_remove_lock protects cwq->thread. 10743af24433SOleg Nesterov */ 107514441960SOleg Nesterov if (cwq->thread == NULL) 107614441960SOleg Nesterov return; 107714441960SOleg Nesterov 10783295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 10793295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 10804e6045f1SJohannes Berg 108113c22168SOleg Nesterov flush_cpu_workqueue(cwq); 108214441960SOleg Nesterov /* 10833da1c84cSOleg Nesterov * If the caller is CPU_POST_DEAD and cwq->worklist was not empty, 108413c22168SOleg Nesterov * a concurrent flush_workqueue() can insert a barrier after us. 108513c22168SOleg Nesterov * However, in that case run_workqueue() won't return and check 108613c22168SOleg Nesterov * kthread_should_stop() until it flushes all work_struct's. 108714441960SOleg Nesterov * When ->worklist becomes empty it is safe to exit because no 108814441960SOleg Nesterov * more work_structs can be queued on this cwq: flush_workqueue 108914441960SOleg Nesterov * checks list_empty(), and a "normal" queue_work() can't use 109014441960SOleg Nesterov * a dead CPU. 109114441960SOleg Nesterov */ 109214441960SOleg Nesterov kthread_stop(cwq->thread); 109314441960SOleg Nesterov cwq->thread = NULL; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10963af24433SOleg Nesterov /** 10973af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 10983af24433SOleg Nesterov * @wq: target workqueue 10993af24433SOleg Nesterov * 11003af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 11013af24433SOleg Nesterov */ 11023af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 11033af24433SOleg Nesterov { 11043af24433SOleg Nesterov int cpu; 11053af24433SOleg Nesterov 11063da1c84cSOleg Nesterov cpu_maps_update_begin(); 110795402b38SGautham R Shenoy spin_lock(&workqueue_lock); 11083af24433SOleg Nesterov list_del(&wq->list); 110995402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 11103da1c84cSOleg Nesterov cpu_maps_update_done(); 11113af24433SOleg Nesterov 1112*1537663fSTejun Heo for_each_possible_cpu(cpu) 1113*1537663fSTejun Heo cleanup_workqueue_thread(get_cwq(cpu, wq)); 1114*1537663fSTejun Heo 11153af24433SOleg Nesterov free_percpu(wq->cpu_wq); 11163af24433SOleg Nesterov kfree(wq); 11173af24433SOleg Nesterov } 11183af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 11193af24433SOleg Nesterov 11209c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, 11211da177e4SLinus Torvalds unsigned long action, 11221da177e4SLinus Torvalds void *hcpu) 11231da177e4SLinus Torvalds { 11243af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 11253af24433SOleg Nesterov struct cpu_workqueue_struct *cwq; 11261da177e4SLinus Torvalds struct workqueue_struct *wq; 11271da177e4SLinus Torvalds 11288bb78442SRafael J. Wysocki action &= ~CPU_TASKS_FROZEN; 11298bb78442SRafael J. Wysocki 11301da177e4SLinus Torvalds list_for_each_entry(wq, &workqueues, list) { 1131*1537663fSTejun Heo if (wq->flags & WQ_SINGLE_THREAD) 1132*1537663fSTejun Heo continue; 1133*1537663fSTejun Heo 1134*1537663fSTejun Heo cwq = get_cwq(cpu, wq); 11353af24433SOleg Nesterov 11363af24433SOleg Nesterov switch (action) { 11373da1c84cSOleg Nesterov case CPU_POST_DEAD: 1138*1537663fSTejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 1139*1537663fSTejun Heo lock_map_release(&cwq->wq->lockdep_map); 1140*1537663fSTejun Heo flush_cpu_workqueue(cwq); 11411da177e4SLinus Torvalds break; 11421da177e4SLinus Torvalds } 11433af24433SOleg Nesterov } 11441da177e4SLinus Torvalds 1145*1537663fSTejun Heo return notifier_from_errno(0); 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11482d3854a3SRusty Russell #ifdef CONFIG_SMP 11498ccad40dSRusty Russell 11502d3854a3SRusty Russell struct work_for_cpu { 11516b44003eSAndrew Morton struct completion completion; 11522d3854a3SRusty Russell long (*fn)(void *); 11532d3854a3SRusty Russell void *arg; 11542d3854a3SRusty Russell long ret; 11552d3854a3SRusty Russell }; 11562d3854a3SRusty Russell 11576b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 11582d3854a3SRusty Russell { 11596b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 11602d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 11616b44003eSAndrew Morton complete(&wfc->completion); 11626b44003eSAndrew Morton return 0; 11632d3854a3SRusty Russell } 11642d3854a3SRusty Russell 11652d3854a3SRusty Russell /** 11662d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 11672d3854a3SRusty Russell * @cpu: the cpu to run on 11682d3854a3SRusty Russell * @fn: the function to run 11692d3854a3SRusty Russell * @arg: the function arg 11702d3854a3SRusty Russell * 117131ad9081SRusty Russell * This will return the value @fn returns. 117231ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 11736b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 11742d3854a3SRusty Russell */ 11752d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 11762d3854a3SRusty Russell { 11776b44003eSAndrew Morton struct task_struct *sub_thread; 11786b44003eSAndrew Morton struct work_for_cpu wfc = { 11796b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 11806b44003eSAndrew Morton .fn = fn, 11816b44003eSAndrew Morton .arg = arg, 11826b44003eSAndrew Morton }; 11832d3854a3SRusty Russell 11846b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 11856b44003eSAndrew Morton if (IS_ERR(sub_thread)) 11866b44003eSAndrew Morton return PTR_ERR(sub_thread); 11876b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 11886b44003eSAndrew Morton wake_up_process(sub_thread); 11896b44003eSAndrew Morton wait_for_completion(&wfc.completion); 11902d3854a3SRusty Russell return wfc.ret; 11912d3854a3SRusty Russell } 11922d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 11932d3854a3SRusty Russell #endif /* CONFIG_SMP */ 11942d3854a3SRusty Russell 1195c12920d1SOleg Nesterov void __init init_workqueues(void) 11961da177e4SLinus Torvalds { 1197e7577c50SRusty Russell singlethread_cpu = cpumask_first(cpu_possible_mask); 11981da177e4SLinus Torvalds hotcpu_notifier(workqueue_cpu_callback, 0); 11991da177e4SLinus Torvalds keventd_wq = create_workqueue("events"); 12001da177e4SLinus Torvalds BUG_ON(!keventd_wq); 12011da177e4SLinus Torvalds } 1202