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; 581da177e4SLinus Torvalds 594690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 6036c8b586SIngo Molnar struct task_struct *thread; 611da177e4SLinus Torvalds } ____cacheline_aligned; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* 641da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 651da177e4SLinus Torvalds * per-CPU workqueues: 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds struct workqueue_struct { 6897e37d7bSTejun Heo unsigned int flags; /* I: WQ_* flags */ 694690c4abSTejun Heo struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */ 704690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 714690c4abSTejun Heo const char *name; /* I: workqueue name */ 724e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 734e6045f1SJohannes Berg struct lockdep_map lockdep_map; 744e6045f1SJohannes Berg #endif 751da177e4SLinus Torvalds }; 761da177e4SLinus Torvalds 77dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 78dc186ad7SThomas Gleixner 79dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 80dc186ad7SThomas Gleixner 81dc186ad7SThomas Gleixner /* 82dc186ad7SThomas Gleixner * fixup_init is called when: 83dc186ad7SThomas Gleixner * - an active object is initialized 84dc186ad7SThomas Gleixner */ 85dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 86dc186ad7SThomas Gleixner { 87dc186ad7SThomas Gleixner struct work_struct *work = addr; 88dc186ad7SThomas Gleixner 89dc186ad7SThomas Gleixner switch (state) { 90dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 91dc186ad7SThomas Gleixner cancel_work_sync(work); 92dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 93dc186ad7SThomas Gleixner return 1; 94dc186ad7SThomas Gleixner default: 95dc186ad7SThomas Gleixner return 0; 96dc186ad7SThomas Gleixner } 97dc186ad7SThomas Gleixner } 98dc186ad7SThomas Gleixner 99dc186ad7SThomas Gleixner /* 100dc186ad7SThomas Gleixner * fixup_activate is called when: 101dc186ad7SThomas Gleixner * - an active object is activated 102dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 103dc186ad7SThomas Gleixner */ 104dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 105dc186ad7SThomas Gleixner { 106dc186ad7SThomas Gleixner struct work_struct *work = addr; 107dc186ad7SThomas Gleixner 108dc186ad7SThomas Gleixner switch (state) { 109dc186ad7SThomas Gleixner 110dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 111dc186ad7SThomas Gleixner /* 112dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 113dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 114dc186ad7SThomas Gleixner * is tracked in the object tracker. 115dc186ad7SThomas Gleixner */ 11622df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 117dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 118dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 119dc186ad7SThomas Gleixner return 0; 120dc186ad7SThomas Gleixner } 121dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 122dc186ad7SThomas Gleixner return 0; 123dc186ad7SThomas Gleixner 124dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 125dc186ad7SThomas Gleixner WARN_ON(1); 126dc186ad7SThomas Gleixner 127dc186ad7SThomas Gleixner default: 128dc186ad7SThomas Gleixner return 0; 129dc186ad7SThomas Gleixner } 130dc186ad7SThomas Gleixner } 131dc186ad7SThomas Gleixner 132dc186ad7SThomas Gleixner /* 133dc186ad7SThomas Gleixner * fixup_free is called when: 134dc186ad7SThomas Gleixner * - an active object is freed 135dc186ad7SThomas Gleixner */ 136dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 137dc186ad7SThomas Gleixner { 138dc186ad7SThomas Gleixner struct work_struct *work = addr; 139dc186ad7SThomas Gleixner 140dc186ad7SThomas Gleixner switch (state) { 141dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 142dc186ad7SThomas Gleixner cancel_work_sync(work); 143dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 144dc186ad7SThomas Gleixner return 1; 145dc186ad7SThomas Gleixner default: 146dc186ad7SThomas Gleixner return 0; 147dc186ad7SThomas Gleixner } 148dc186ad7SThomas Gleixner } 149dc186ad7SThomas Gleixner 150dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 151dc186ad7SThomas Gleixner .name = "work_struct", 152dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 153dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 154dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 155dc186ad7SThomas Gleixner }; 156dc186ad7SThomas Gleixner 157dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 158dc186ad7SThomas Gleixner { 159dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 160dc186ad7SThomas Gleixner } 161dc186ad7SThomas Gleixner 162dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 163dc186ad7SThomas Gleixner { 164dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 165dc186ad7SThomas Gleixner } 166dc186ad7SThomas Gleixner 167dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 168dc186ad7SThomas Gleixner { 169dc186ad7SThomas Gleixner if (onstack) 170dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 171dc186ad7SThomas Gleixner else 172dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 173dc186ad7SThomas Gleixner } 174dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 175dc186ad7SThomas Gleixner 176dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 177dc186ad7SThomas Gleixner { 178dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 179dc186ad7SThomas Gleixner } 180dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 181dc186ad7SThomas Gleixner 182dc186ad7SThomas Gleixner #else 183dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 184dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 185dc186ad7SThomas Gleixner #endif 186dc186ad7SThomas Gleixner 18795402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 18895402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 1891da177e4SLinus Torvalds static LIST_HEAD(workqueues); 1901da177e4SLinus Torvalds 1913af24433SOleg Nesterov static int singlethread_cpu __read_mostly; 192e7577c50SRusty Russell static const struct cpumask *cpu_singlethread_map __read_mostly; 19314441960SOleg Nesterov /* 19414441960SOleg Nesterov * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD 19514441960SOleg Nesterov * flushes cwq->worklist. This means that flush_workqueue/wait_on_work 19614441960SOleg Nesterov * which comes in between can't use for_each_online_cpu(). We could 19714441960SOleg Nesterov * use cpu_possible_map, the cpumask below is more a documentation 19814441960SOleg Nesterov * than optimization. 19914441960SOleg Nesterov */ 200e7577c50SRusty Russell static cpumask_var_t cpu_populated_map __read_mostly; 201f756d5e2SNathan Lynch 2021da177e4SLinus Torvalds /* If it's single threaded, it isn't in the list of workqueues. */ 20397e37d7bSTejun Heo static inline bool is_wq_single_threaded(struct workqueue_struct *wq) 2041da177e4SLinus Torvalds { 20597e37d7bSTejun Heo return wq->flags & WQ_SINGLE_THREAD; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 208e7577c50SRusty Russell static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq) 209b1f4ec17SOleg Nesterov { 2106cc88bc4SDavid Howells return is_wq_single_threaded(wq) 211e7577c50SRusty Russell ? cpu_singlethread_map : cpu_populated_map; 212b1f4ec17SOleg Nesterov } 213b1f4ec17SOleg Nesterov 2144690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, 2154690c4abSTejun Heo struct workqueue_struct *wq) 216a848e3b6SOleg Nesterov { 2176cc88bc4SDavid Howells if (unlikely(is_wq_single_threaded(wq))) 218a848e3b6SOleg Nesterov cpu = singlethread_cpu; 219a848e3b6SOleg Nesterov return per_cpu_ptr(wq->cpu_wq, cpu); 220a848e3b6SOleg Nesterov } 221a848e3b6SOleg Nesterov 2224594bf15SDavid Howells /* 2234594bf15SDavid Howells * Set the workqueue on which a work item is to be run 2244594bf15SDavid Howells * - Must *only* be called if the pending flag is set 2254594bf15SDavid Howells */ 226ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work, 2274690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 2284690c4abSTejun Heo unsigned long extra_flags) 229365970a1SDavid Howells { 2304594bf15SDavid Howells BUG_ON(!work_pending(work)); 2314594bf15SDavid Howells 2324690c4abSTejun Heo atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) | 23322df02bbSTejun Heo WORK_STRUCT_PENDING | extra_flags); 234365970a1SDavid Howells } 235365970a1SDavid Howells 2364d707b9fSOleg Nesterov /* 2374d707b9fSOleg Nesterov * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued. 2384d707b9fSOleg Nesterov */ 2394d707b9fSOleg Nesterov static inline void clear_wq_data(struct work_struct *work) 2404d707b9fSOleg Nesterov { 2414690c4abSTejun Heo atomic_long_set(&work->data, work_static(work)); 2424d707b9fSOleg Nesterov } 2434d707b9fSOleg Nesterov 244*64166699STejun Heo static inline struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) 245365970a1SDavid Howells { 246*64166699STejun Heo return (void *)(atomic_long_read(&work->data) & 247*64166699STejun Heo WORK_STRUCT_WQ_DATA_MASK); 248365970a1SDavid Howells } 249365970a1SDavid Howells 2504690c4abSTejun Heo /** 2514690c4abSTejun Heo * insert_work - insert a work into cwq 2524690c4abSTejun Heo * @cwq: cwq @work belongs to 2534690c4abSTejun Heo * @work: work to insert 2544690c4abSTejun Heo * @head: insertion point 2554690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 2564690c4abSTejun Heo * 2574690c4abSTejun Heo * Insert @work into @cwq after @head. 2584690c4abSTejun Heo * 2594690c4abSTejun Heo * CONTEXT: 2604690c4abSTejun Heo * spin_lock_irq(cwq->lock). 2614690c4abSTejun Heo */ 262b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 2634690c4abSTejun Heo struct work_struct *work, struct list_head *head, 2644690c4abSTejun Heo unsigned int extra_flags) 265b89deed3SOleg Nesterov { 2664690c4abSTejun Heo /* we own @work, set data and link */ 2674690c4abSTejun Heo set_wq_data(work, cwq, extra_flags); 2684690c4abSTejun Heo 2696e84d644SOleg Nesterov /* 2706e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 2716e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 2726e84d644SOleg Nesterov */ 2736e84d644SOleg Nesterov smp_wmb(); 2744690c4abSTejun Heo 2751a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 276b89deed3SOleg Nesterov wake_up(&cwq->more_work); 277b89deed3SOleg Nesterov } 278b89deed3SOleg Nesterov 2794690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 2801da177e4SLinus Torvalds struct work_struct *work) 2811da177e4SLinus Torvalds { 2824690c4abSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2831da177e4SLinus Torvalds unsigned long flags; 2841da177e4SLinus Torvalds 285dc186ad7SThomas Gleixner debug_work_activate(work); 2861da177e4SLinus Torvalds spin_lock_irqsave(&cwq->lock, flags); 2874690c4abSTejun Heo BUG_ON(!list_empty(&work->entry)); 2884690c4abSTejun Heo insert_work(cwq, work, &cwq->worklist, 0); 2891da177e4SLinus Torvalds spin_unlock_irqrestore(&cwq->lock, flags); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2920fcb78c2SRolf Eike Beer /** 2930fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 2940fcb78c2SRolf Eike Beer * @wq: workqueue to use 2950fcb78c2SRolf Eike Beer * @work: work to queue 2960fcb78c2SRolf Eike Beer * 297057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 2981da177e4SLinus Torvalds * 29900dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 30000dfcaf7SOleg Nesterov * it can be processed by another CPU. 3011da177e4SLinus Torvalds */ 3027ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 3031da177e4SLinus Torvalds { 304ef1ca236SOleg Nesterov int ret; 3051da177e4SLinus Torvalds 306ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 307a848e3b6SOleg Nesterov put_cpu(); 308ef1ca236SOleg Nesterov 3091da177e4SLinus Torvalds return ret; 3101da177e4SLinus Torvalds } 311ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 3121da177e4SLinus Torvalds 313c1a220e7SZhang Rui /** 314c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 315c1a220e7SZhang Rui * @cpu: CPU number to execute work on 316c1a220e7SZhang Rui * @wq: workqueue to use 317c1a220e7SZhang Rui * @work: work to queue 318c1a220e7SZhang Rui * 319c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 320c1a220e7SZhang Rui * 321c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 322c1a220e7SZhang Rui * can't go away. 323c1a220e7SZhang Rui */ 324c1a220e7SZhang Rui int 325c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 326c1a220e7SZhang Rui { 327c1a220e7SZhang Rui int ret = 0; 328c1a220e7SZhang Rui 32922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 3304690c4abSTejun Heo __queue_work(cpu, wq, work); 331c1a220e7SZhang Rui ret = 1; 332c1a220e7SZhang Rui } 333c1a220e7SZhang Rui return ret; 334c1a220e7SZhang Rui } 335c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 336c1a220e7SZhang Rui 3376d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 3381da177e4SLinus Torvalds { 33952bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 340ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); 3411da177e4SLinus Torvalds 3424690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 3450fcb78c2SRolf Eike Beer /** 3460fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 3470fcb78c2SRolf Eike Beer * @wq: workqueue to use 348af9997e4SRandy Dunlap * @dwork: delayable work to queue 3490fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 3500fcb78c2SRolf Eike Beer * 351057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 3520fcb78c2SRolf Eike Beer */ 3537ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 35452bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 3551da177e4SLinus Torvalds { 35652bad64dSDavid Howells if (delay == 0) 35763bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 3581da177e4SLinus Torvalds 35963bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 3601da177e4SLinus Torvalds } 361ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 3621da177e4SLinus Torvalds 3630fcb78c2SRolf Eike Beer /** 3640fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 3650fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 3660fcb78c2SRolf Eike Beer * @wq: workqueue to use 367af9997e4SRandy Dunlap * @dwork: work to queue 3680fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 3690fcb78c2SRolf Eike Beer * 370057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 3710fcb78c2SRolf Eike Beer */ 3727a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 37352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 3747a6bc1cdSVenkatesh Pallipadi { 3757a6bc1cdSVenkatesh Pallipadi int ret = 0; 37652bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 37752bad64dSDavid Howells struct work_struct *work = &dwork->work; 3787a6bc1cdSVenkatesh Pallipadi 37922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 3807a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 3817a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 3827a6bc1cdSVenkatesh Pallipadi 3838a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 3848a3e77ccSAndrew Liu 385ed7c0feeSOleg Nesterov /* This stores cwq for the moment, for the timer_fn */ 3864690c4abSTejun Heo set_wq_data(work, get_cwq(raw_smp_processor_id(), wq), 0); 3877a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 38852bad64dSDavid Howells timer->data = (unsigned long)dwork; 3897a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 39063bc0362SOleg Nesterov 39163bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 3927a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 39363bc0362SOleg Nesterov else 39463bc0362SOleg Nesterov add_timer(timer); 3957a6bc1cdSVenkatesh Pallipadi ret = 1; 3967a6bc1cdSVenkatesh Pallipadi } 3977a6bc1cdSVenkatesh Pallipadi return ret; 3987a6bc1cdSVenkatesh Pallipadi } 399ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 4001da177e4SLinus Torvalds 401a62428c0STejun Heo /** 402a62428c0STejun Heo * process_one_work - process single work 403a62428c0STejun Heo * @cwq: cwq to process work for 404a62428c0STejun Heo * @work: work to process 405a62428c0STejun Heo * 406a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 407a62428c0STejun Heo * process a single work including synchronization against and 408a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 409a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 410a62428c0STejun Heo * call this function to process a work. 411a62428c0STejun Heo * 412a62428c0STejun Heo * CONTEXT: 413a62428c0STejun Heo * spin_lock_irq(cwq->lock) which is released and regrabbed. 414a62428c0STejun Heo */ 415a62428c0STejun Heo static void process_one_work(struct cpu_workqueue_struct *cwq, 416a62428c0STejun Heo struct work_struct *work) 4171da177e4SLinus Torvalds { 4186bb49e59SDavid Howells work_func_t f = work->func; 4194e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 4204e6045f1SJohannes Berg /* 421a62428c0STejun Heo * It is permissible to free the struct work_struct from 422a62428c0STejun Heo * inside the function that is called from it, this we need to 423a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 424a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 425a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 4264e6045f1SJohannes Berg */ 4274e6045f1SJohannes Berg struct lockdep_map lockdep_map = work->lockdep_map; 4284e6045f1SJohannes Berg #endif 429a62428c0STejun Heo /* claim and process */ 430dc186ad7SThomas Gleixner debug_work_deactivate(work); 431b89deed3SOleg Nesterov cwq->current_work = work; 432a62428c0STejun Heo list_del_init(&work->entry); 433a62428c0STejun Heo 434f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 4351da177e4SLinus Torvalds 436365970a1SDavid Howells BUG_ON(get_wq_data(work) != cwq); 43723b2e599SOleg Nesterov work_clear_pending(work); 4383295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 4393295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 44065f27f38SDavid Howells f(work); 4413295f0efSIngo Molnar lock_map_release(&lockdep_map); 4423295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 4431da177e4SLinus Torvalds 444d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 445d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 446d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 447a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 448d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 449d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 450d5abe669SPeter Zijlstra debug_show_held_locks(current); 451d5abe669SPeter Zijlstra dump_stack(); 452d5abe669SPeter Zijlstra } 453d5abe669SPeter Zijlstra 454f293ea92SOleg Nesterov spin_lock_irq(&cwq->lock); 455a62428c0STejun Heo 456a62428c0STejun Heo /* we're done with it, release */ 457b89deed3SOleg Nesterov cwq->current_work = NULL; 4581da177e4SLinus Torvalds } 459a62428c0STejun Heo 460a62428c0STejun Heo static void run_workqueue(struct cpu_workqueue_struct *cwq) 461a62428c0STejun Heo { 462a62428c0STejun Heo spin_lock_irq(&cwq->lock); 463a62428c0STejun Heo while (!list_empty(&cwq->worklist)) { 464a62428c0STejun Heo struct work_struct *work = list_entry(cwq->worklist.next, 465a62428c0STejun Heo struct work_struct, entry); 466a62428c0STejun Heo process_one_work(cwq, work); 467a62428c0STejun Heo } 468f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4714690c4abSTejun Heo /** 4724690c4abSTejun Heo * worker_thread - the worker thread function 4734690c4abSTejun Heo * @__cwq: cwq to serve 4744690c4abSTejun Heo * 4754690c4abSTejun Heo * The cwq worker thread function. 4764690c4abSTejun Heo */ 4771da177e4SLinus Torvalds static int worker_thread(void *__cwq) 4781da177e4SLinus Torvalds { 4791da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq = __cwq; 4803af24433SOleg Nesterov DEFINE_WAIT(wait); 4811da177e4SLinus Torvalds 48297e37d7bSTejun Heo if (cwq->wq->flags & WQ_FREEZEABLE) 48383144186SRafael J. Wysocki set_freezable(); 4841da177e4SLinus Torvalds 4853af24433SOleg Nesterov for (;;) { 4863af24433SOleg Nesterov prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); 48714441960SOleg Nesterov if (!freezing(current) && 48814441960SOleg Nesterov !kthread_should_stop() && 48914441960SOleg Nesterov list_empty(&cwq->worklist)) 4901da177e4SLinus Torvalds schedule(); 4913af24433SOleg Nesterov finish_wait(&cwq->more_work, &wait); 4921da177e4SLinus Torvalds 49385f4186aSOleg Nesterov try_to_freeze(); 49485f4186aSOleg Nesterov 49514441960SOleg Nesterov if (kthread_should_stop()) 4963af24433SOleg Nesterov break; 4973af24433SOleg Nesterov 4981da177e4SLinus Torvalds run_workqueue(cwq); 4991da177e4SLinus Torvalds } 5003af24433SOleg Nesterov 5011da177e4SLinus Torvalds return 0; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 504fc2e4d70SOleg Nesterov struct wq_barrier { 505fc2e4d70SOleg Nesterov struct work_struct work; 506fc2e4d70SOleg Nesterov struct completion done; 507fc2e4d70SOleg Nesterov }; 508fc2e4d70SOleg Nesterov 509fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 510fc2e4d70SOleg Nesterov { 511fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 512fc2e4d70SOleg Nesterov complete(&barr->done); 513fc2e4d70SOleg Nesterov } 514fc2e4d70SOleg Nesterov 5154690c4abSTejun Heo /** 5164690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 5174690c4abSTejun Heo * @cwq: cwq to insert barrier into 5184690c4abSTejun Heo * @barr: wq_barrier to insert 5194690c4abSTejun Heo * @head: insertion point 5204690c4abSTejun Heo * 5214690c4abSTejun Heo * Insert barrier @barr into @cwq before @head. 5224690c4abSTejun Heo * 5234690c4abSTejun Heo * CONTEXT: 5244690c4abSTejun Heo * spin_lock_irq(cwq->lock). 5254690c4abSTejun Heo */ 52683c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 5271a4d9b0aSOleg Nesterov struct wq_barrier *barr, struct list_head *head) 528fc2e4d70SOleg Nesterov { 529dc186ad7SThomas Gleixner /* 530dc186ad7SThomas Gleixner * debugobject calls are safe here even with cwq->lock locked 531dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 532dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 533dc186ad7SThomas Gleixner * might deadlock. 534dc186ad7SThomas Gleixner */ 535dc186ad7SThomas Gleixner INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); 53622df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 537fc2e4d70SOleg Nesterov init_completion(&barr->done); 53883c22520SOleg Nesterov 539dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 5404690c4abSTejun Heo insert_work(cwq, &barr->work, head, 0); 541fc2e4d70SOleg Nesterov } 542fc2e4d70SOleg Nesterov 54314441960SOleg Nesterov static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) 5441da177e4SLinus Torvalds { 5452355b70fSLai Jiangshan int active = 0; 546fc2e4d70SOleg Nesterov struct wq_barrier barr; 5471da177e4SLinus Torvalds 5482355b70fSLai Jiangshan WARN_ON(cwq->thread == current); 5492355b70fSLai Jiangshan 55083c22520SOleg Nesterov spin_lock_irq(&cwq->lock); 55183c22520SOleg Nesterov if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { 5521a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, &cwq->worklist); 55383c22520SOleg Nesterov active = 1; 55483c22520SOleg Nesterov } 55583c22520SOleg Nesterov spin_unlock_irq(&cwq->lock); 5561da177e4SLinus Torvalds 557dc186ad7SThomas Gleixner if (active) { 558fc2e4d70SOleg Nesterov wait_for_completion(&barr.done); 559dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 560dc186ad7SThomas Gleixner } 56114441960SOleg Nesterov 56214441960SOleg Nesterov return active; 56383c22520SOleg Nesterov } 5641da177e4SLinus Torvalds 5650fcb78c2SRolf Eike Beer /** 5661da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 5670fcb78c2SRolf Eike Beer * @wq: workqueue to flush 5681da177e4SLinus Torvalds * 5691da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 5701da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 5711da177e4SLinus Torvalds * 572fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 573fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 5741da177e4SLinus Torvalds */ 5757ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 5761da177e4SLinus Torvalds { 577e7577c50SRusty Russell const struct cpumask *cpu_map = wq_cpu_map(wq); 578cce1a165SOleg Nesterov int cpu; 579b1f4ec17SOleg Nesterov 580f293ea92SOleg Nesterov might_sleep(); 5813295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 5823295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 583aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 58489ada679SChristoph Lameter flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); 5851da177e4SLinus Torvalds } 586ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 5871da177e4SLinus Torvalds 588db700897SOleg Nesterov /** 589db700897SOleg Nesterov * flush_work - block until a work_struct's callback has terminated 590db700897SOleg Nesterov * @work: the work which is to be flushed 591db700897SOleg Nesterov * 592a67da70dSOleg Nesterov * Returns false if @work has already terminated. 593a67da70dSOleg Nesterov * 594db700897SOleg Nesterov * It is expected that, prior to calling flush_work(), the caller has 595db700897SOleg Nesterov * arranged for the work to not be requeued, otherwise it doesn't make 596db700897SOleg Nesterov * sense to use this function. 597db700897SOleg Nesterov */ 598db700897SOleg Nesterov int flush_work(struct work_struct *work) 599db700897SOleg Nesterov { 600db700897SOleg Nesterov struct cpu_workqueue_struct *cwq; 601db700897SOleg Nesterov struct list_head *prev; 602db700897SOleg Nesterov struct wq_barrier barr; 603db700897SOleg Nesterov 604db700897SOleg Nesterov might_sleep(); 605db700897SOleg Nesterov cwq = get_wq_data(work); 606db700897SOleg Nesterov if (!cwq) 607db700897SOleg Nesterov return 0; 608db700897SOleg Nesterov 6093295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 6103295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 611a67da70dSOleg Nesterov 612db700897SOleg Nesterov spin_lock_irq(&cwq->lock); 613db700897SOleg Nesterov if (!list_empty(&work->entry)) { 614db700897SOleg Nesterov /* 615db700897SOleg Nesterov * See the comment near try_to_grab_pending()->smp_rmb(). 616db700897SOleg Nesterov * If it was re-queued under us we are not going to wait. 617db700897SOleg Nesterov */ 618db700897SOleg Nesterov smp_rmb(); 619db700897SOleg Nesterov if (unlikely(cwq != get_wq_data(work))) 6204690c4abSTejun Heo goto already_gone; 621db700897SOleg Nesterov prev = &work->entry; 622db700897SOleg Nesterov } else { 623db700897SOleg Nesterov if (cwq->current_work != work) 6244690c4abSTejun Heo goto already_gone; 625db700897SOleg Nesterov prev = &cwq->worklist; 626db700897SOleg Nesterov } 627db700897SOleg Nesterov insert_wq_barrier(cwq, &barr, prev->next); 628db700897SOleg Nesterov 6294690c4abSTejun Heo spin_unlock_irq(&cwq->lock); 630db700897SOleg Nesterov wait_for_completion(&barr.done); 631dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 632db700897SOleg Nesterov return 1; 6334690c4abSTejun Heo already_gone: 6344690c4abSTejun Heo spin_unlock_irq(&cwq->lock); 6354690c4abSTejun Heo return 0; 636db700897SOleg Nesterov } 637db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 638db700897SOleg Nesterov 6396e84d644SOleg Nesterov /* 6401f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 6416e84d644SOleg Nesterov * so this work can't be re-armed in any way. 6426e84d644SOleg Nesterov */ 6436e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 6446e84d644SOleg Nesterov { 6456e84d644SOleg Nesterov struct cpu_workqueue_struct *cwq; 6461f1f642eSOleg Nesterov int ret = -1; 6476e84d644SOleg Nesterov 64822df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 6491f1f642eSOleg Nesterov return 0; 6506e84d644SOleg Nesterov 6516e84d644SOleg Nesterov /* 6526e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 6536e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 6546e84d644SOleg Nesterov */ 6556e84d644SOleg Nesterov 6566e84d644SOleg Nesterov cwq = get_wq_data(work); 6576e84d644SOleg Nesterov if (!cwq) 6586e84d644SOleg Nesterov return ret; 6596e84d644SOleg Nesterov 6606e84d644SOleg Nesterov spin_lock_irq(&cwq->lock); 6616e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 6626e84d644SOleg Nesterov /* 6636e84d644SOleg Nesterov * This work is queued, but perhaps we locked the wrong cwq. 6646e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 6656e84d644SOleg Nesterov * insert_work()->wmb(). 6666e84d644SOleg Nesterov */ 6676e84d644SOleg Nesterov smp_rmb(); 6686e84d644SOleg Nesterov if (cwq == get_wq_data(work)) { 669dc186ad7SThomas Gleixner debug_work_deactivate(work); 6706e84d644SOleg Nesterov list_del_init(&work->entry); 6716e84d644SOleg Nesterov ret = 1; 6726e84d644SOleg Nesterov } 6736e84d644SOleg Nesterov } 6746e84d644SOleg Nesterov spin_unlock_irq(&cwq->lock); 6756e84d644SOleg Nesterov 6766e84d644SOleg Nesterov return ret; 6776e84d644SOleg Nesterov } 6786e84d644SOleg Nesterov 6796e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, 680b89deed3SOleg Nesterov struct work_struct *work) 681b89deed3SOleg Nesterov { 682b89deed3SOleg Nesterov struct wq_barrier barr; 683b89deed3SOleg Nesterov int running = 0; 684b89deed3SOleg Nesterov 685b89deed3SOleg Nesterov spin_lock_irq(&cwq->lock); 686b89deed3SOleg Nesterov if (unlikely(cwq->current_work == work)) { 6871a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, cwq->worklist.next); 688b89deed3SOleg Nesterov running = 1; 689b89deed3SOleg Nesterov } 690b89deed3SOleg Nesterov spin_unlock_irq(&cwq->lock); 691b89deed3SOleg Nesterov 692dc186ad7SThomas Gleixner if (unlikely(running)) { 693b89deed3SOleg Nesterov wait_for_completion(&barr.done); 694dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 695dc186ad7SThomas Gleixner } 696b89deed3SOleg Nesterov } 697b89deed3SOleg Nesterov 6986e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work) 699b89deed3SOleg Nesterov { 700b89deed3SOleg Nesterov struct cpu_workqueue_struct *cwq; 70128e53bddSOleg Nesterov struct workqueue_struct *wq; 702e7577c50SRusty Russell const struct cpumask *cpu_map; 703b1f4ec17SOleg Nesterov int cpu; 704b89deed3SOleg Nesterov 705f293ea92SOleg Nesterov might_sleep(); 706f293ea92SOleg Nesterov 7073295f0efSIngo Molnar lock_map_acquire(&work->lockdep_map); 7083295f0efSIngo Molnar lock_map_release(&work->lockdep_map); 7094e6045f1SJohannes Berg 710b89deed3SOleg Nesterov cwq = get_wq_data(work); 711b89deed3SOleg Nesterov if (!cwq) 7123af24433SOleg Nesterov return; 713b89deed3SOleg Nesterov 71428e53bddSOleg Nesterov wq = cwq->wq; 71528e53bddSOleg Nesterov cpu_map = wq_cpu_map(wq); 71628e53bddSOleg Nesterov 717aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 7184690c4abSTejun Heo wait_on_cpu_work(get_cwq(cpu, wq), work); 7196e84d644SOleg Nesterov } 7206e84d644SOleg Nesterov 7211f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work, 7221f1f642eSOleg Nesterov struct timer_list* timer) 7231f1f642eSOleg Nesterov { 7241f1f642eSOleg Nesterov int ret; 7251f1f642eSOleg Nesterov 7261f1f642eSOleg Nesterov do { 7271f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 7281f1f642eSOleg Nesterov if (!ret) 7291f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 7301f1f642eSOleg Nesterov wait_on_work(work); 7311f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 7321f1f642eSOleg Nesterov 7334d707b9fSOleg Nesterov clear_wq_data(work); 7341f1f642eSOleg Nesterov return ret; 7351f1f642eSOleg Nesterov } 7361f1f642eSOleg Nesterov 7376e84d644SOleg Nesterov /** 7386e84d644SOleg Nesterov * cancel_work_sync - block until a work_struct's callback has terminated 7396e84d644SOleg Nesterov * @work: the work which is to be flushed 7406e84d644SOleg Nesterov * 7411f1f642eSOleg Nesterov * Returns true if @work was pending. 7421f1f642eSOleg Nesterov * 7436e84d644SOleg Nesterov * cancel_work_sync() will cancel the work if it is queued. If the work's 7446e84d644SOleg Nesterov * callback appears to be running, cancel_work_sync() will block until it 7456e84d644SOleg Nesterov * has completed. 7466e84d644SOleg Nesterov * 7476e84d644SOleg Nesterov * It is possible to use this function if the work re-queues itself. It can 7486e84d644SOleg Nesterov * cancel the work even if it migrates to another workqueue, however in that 7496e84d644SOleg Nesterov * case it only guarantees that work->func() has completed on the last queued 7506e84d644SOleg Nesterov * workqueue. 7516e84d644SOleg Nesterov * 7526e84d644SOleg Nesterov * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not 7536e84d644SOleg Nesterov * pending, otherwise it goes into a busy-wait loop until the timer expires. 7546e84d644SOleg Nesterov * 7556e84d644SOleg Nesterov * The caller must ensure that workqueue_struct on which this work was last 7566e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 7576e84d644SOleg Nesterov */ 7581f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work) 7596e84d644SOleg Nesterov { 7601f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 761b89deed3SOleg Nesterov } 76228e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 763b89deed3SOleg Nesterov 7646e84d644SOleg Nesterov /** 765f5a421a4SOleg Nesterov * cancel_delayed_work_sync - reliably kill off a delayed work. 7666e84d644SOleg Nesterov * @dwork: the delayed work struct 7676e84d644SOleg Nesterov * 7681f1f642eSOleg Nesterov * Returns true if @dwork was pending. 7691f1f642eSOleg Nesterov * 7706e84d644SOleg Nesterov * It is possible to use this function if @dwork rearms itself via queue_work() 7716e84d644SOleg Nesterov * or queue_delayed_work(). See also the comment for cancel_work_sync(). 7726e84d644SOleg Nesterov */ 7731f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork) 7746e84d644SOleg Nesterov { 7751f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 7766e84d644SOleg Nesterov } 777f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 7781da177e4SLinus Torvalds 7796e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly; 7801da177e4SLinus Torvalds 7810fcb78c2SRolf Eike Beer /** 7820fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 7830fcb78c2SRolf Eike Beer * @work: job to be done 7840fcb78c2SRolf Eike Beer * 7855b0f437dSBart Van Assche * Returns zero if @work was already on the kernel-global workqueue and 7865b0f437dSBart Van Assche * non-zero otherwise. 7875b0f437dSBart Van Assche * 7885b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 7895b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 7905b0f437dSBart Van Assche * workqueue otherwise. 7910fcb78c2SRolf Eike Beer */ 7927ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 7931da177e4SLinus Torvalds { 7941da177e4SLinus Torvalds return queue_work(keventd_wq, work); 7951da177e4SLinus Torvalds } 796ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 7971da177e4SLinus Torvalds 798c1a220e7SZhang Rui /* 799c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 800c1a220e7SZhang Rui * @cpu: cpu to put the work task on 801c1a220e7SZhang Rui * @work: job to be done 802c1a220e7SZhang Rui * 803c1a220e7SZhang Rui * This puts a job on a specific cpu 804c1a220e7SZhang Rui */ 805c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 806c1a220e7SZhang Rui { 807c1a220e7SZhang Rui return queue_work_on(cpu, keventd_wq, work); 808c1a220e7SZhang Rui } 809c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 810c1a220e7SZhang Rui 8110fcb78c2SRolf Eike Beer /** 8120fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 81352bad64dSDavid Howells * @dwork: job to be done 81452bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 8150fcb78c2SRolf Eike Beer * 8160fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 8170fcb78c2SRolf Eike Beer * workqueue. 8180fcb78c2SRolf Eike Beer */ 8197ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 82082f67cd9SIngo Molnar unsigned long delay) 8211da177e4SLinus Torvalds { 82252bad64dSDavid Howells return queue_delayed_work(keventd_wq, dwork, delay); 8231da177e4SLinus Torvalds } 824ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 8251da177e4SLinus Torvalds 8260fcb78c2SRolf Eike Beer /** 8278c53e463SLinus Torvalds * flush_delayed_work - block until a dwork_struct's callback has terminated 8288c53e463SLinus Torvalds * @dwork: the delayed work which is to be flushed 8298c53e463SLinus Torvalds * 8308c53e463SLinus Torvalds * Any timeout is cancelled, and any pending work is run immediately. 8318c53e463SLinus Torvalds */ 8328c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork) 8338c53e463SLinus Torvalds { 8348c53e463SLinus Torvalds if (del_timer_sync(&dwork->timer)) { 8354690c4abSTejun Heo __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq, 8364690c4abSTejun Heo &dwork->work); 8378c53e463SLinus Torvalds put_cpu(); 8388c53e463SLinus Torvalds } 8398c53e463SLinus Torvalds flush_work(&dwork->work); 8408c53e463SLinus Torvalds } 8418c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work); 8428c53e463SLinus Torvalds 8438c53e463SLinus Torvalds /** 8440fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 8450fcb78c2SRolf Eike Beer * @cpu: cpu to use 84652bad64dSDavid Howells * @dwork: job to be done 8470fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 8480fcb78c2SRolf Eike Beer * 8490fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 8500fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 8510fcb78c2SRolf Eike Beer */ 8521da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 85352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 8541da177e4SLinus Torvalds { 85552bad64dSDavid Howells return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); 8561da177e4SLinus Torvalds } 857ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 8581da177e4SLinus Torvalds 859b6136773SAndrew Morton /** 860b6136773SAndrew Morton * schedule_on_each_cpu - call a function on each online CPU from keventd 861b6136773SAndrew Morton * @func: the function to call 862b6136773SAndrew Morton * 863b6136773SAndrew Morton * Returns zero on success. 864b6136773SAndrew Morton * Returns -ve errno on failure. 865b6136773SAndrew Morton * 866b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 867b6136773SAndrew Morton */ 86865f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 86915316ba8SChristoph Lameter { 87015316ba8SChristoph Lameter int cpu; 87165a64464SAndi Kleen int orig = -1; 872b6136773SAndrew Morton struct work_struct *works; 87315316ba8SChristoph Lameter 874b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 875b6136773SAndrew Morton if (!works) 87615316ba8SChristoph Lameter return -ENOMEM; 877b6136773SAndrew Morton 87895402b38SGautham R Shenoy get_online_cpus(); 87993981800STejun Heo 88093981800STejun Heo /* 88193981800STejun Heo * When running in keventd don't schedule a work item on 88293981800STejun Heo * itself. Can just call directly because the work queue is 88393981800STejun Heo * already bound. This also is faster. 88493981800STejun Heo */ 88593981800STejun Heo if (current_is_keventd()) 88693981800STejun Heo orig = raw_smp_processor_id(); 88793981800STejun Heo 88815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 8899bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 8909bfb1839SIngo Molnar 8919bfb1839SIngo Molnar INIT_WORK(work, func); 89293981800STejun Heo if (cpu != orig) 8938de6d308SOleg Nesterov schedule_work_on(cpu, work); 89415316ba8SChristoph Lameter } 89593981800STejun Heo if (orig >= 0) 89693981800STejun Heo func(per_cpu_ptr(works, orig)); 89793981800STejun Heo 89893981800STejun Heo for_each_online_cpu(cpu) 8998616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 90093981800STejun Heo 90195402b38SGautham R Shenoy put_online_cpus(); 902b6136773SAndrew Morton free_percpu(works); 90315316ba8SChristoph Lameter return 0; 90415316ba8SChristoph Lameter } 90515316ba8SChristoph Lameter 906eef6a7d5SAlan Stern /** 907eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 908eef6a7d5SAlan Stern * 909eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 910eef6a7d5SAlan Stern * completion. 911eef6a7d5SAlan Stern * 912eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 913eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 914eef6a7d5SAlan Stern * will lead to deadlock: 915eef6a7d5SAlan Stern * 916eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 917eef6a7d5SAlan Stern * a lock held by your code or its caller. 918eef6a7d5SAlan Stern * 919eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 920eef6a7d5SAlan Stern * 921eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 922eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 923eef6a7d5SAlan Stern * what locks they need, which you have no control over. 924eef6a7d5SAlan Stern * 925eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 926eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 927eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 928eef6a7d5SAlan Stern * cancel_work_sync() instead. 929eef6a7d5SAlan Stern */ 9301da177e4SLinus Torvalds void flush_scheduled_work(void) 9311da177e4SLinus Torvalds { 9321da177e4SLinus Torvalds flush_workqueue(keventd_wq); 9331da177e4SLinus Torvalds } 934ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds /** 9371fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 9381fa44ecaSJames Bottomley * @fn: the function to execute 9391fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 9401fa44ecaSJames Bottomley * be available when the work executes) 9411fa44ecaSJames Bottomley * 9421fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 9431fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 9441fa44ecaSJames Bottomley * 9451fa44ecaSJames Bottomley * Returns: 0 - function was executed 9461fa44ecaSJames Bottomley * 1 - function was scheduled for execution 9471fa44ecaSJames Bottomley */ 94865f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 9491fa44ecaSJames Bottomley { 9501fa44ecaSJames Bottomley if (!in_interrupt()) { 95165f27f38SDavid Howells fn(&ew->work); 9521fa44ecaSJames Bottomley return 0; 9531fa44ecaSJames Bottomley } 9541fa44ecaSJames Bottomley 95565f27f38SDavid Howells INIT_WORK(&ew->work, fn); 9561fa44ecaSJames Bottomley schedule_work(&ew->work); 9571fa44ecaSJames Bottomley 9581fa44ecaSJames Bottomley return 1; 9591fa44ecaSJames Bottomley } 9601fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 9611fa44ecaSJames Bottomley 9621da177e4SLinus Torvalds int keventd_up(void) 9631da177e4SLinus Torvalds { 9641da177e4SLinus Torvalds return keventd_wq != NULL; 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds int current_is_keventd(void) 9681da177e4SLinus Torvalds { 9691da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq; 970d243769dSHugh Dickins int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ 9711da177e4SLinus Torvalds int ret = 0; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds BUG_ON(!keventd_wq); 9741da177e4SLinus Torvalds 97589ada679SChristoph Lameter cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); 9761da177e4SLinus Torvalds if (current == cwq->thread) 9771da177e4SLinus Torvalds ret = 1; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds return ret; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9833af24433SOleg Nesterov static struct cpu_workqueue_struct * 9843af24433SOleg Nesterov init_cpu_workqueue(struct workqueue_struct *wq, int cpu) 9851da177e4SLinus Torvalds { 98689ada679SChristoph Lameter struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); 9873af24433SOleg Nesterov 9883af24433SOleg Nesterov cwq->wq = wq; 9893af24433SOleg Nesterov spin_lock_init(&cwq->lock); 9903af24433SOleg Nesterov INIT_LIST_HEAD(&cwq->worklist); 9913af24433SOleg Nesterov init_waitqueue_head(&cwq->more_work); 9923af24433SOleg Nesterov 9933af24433SOleg Nesterov return cwq; 9943af24433SOleg Nesterov } 9953af24433SOleg Nesterov 9963af24433SOleg Nesterov static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 9973af24433SOleg Nesterov { 9983af24433SOleg Nesterov struct workqueue_struct *wq = cwq->wq; 9996cc88bc4SDavid Howells const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d"; 10003af24433SOleg Nesterov struct task_struct *p; 10013af24433SOleg Nesterov 10023af24433SOleg Nesterov p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); 10033af24433SOleg Nesterov /* 10043af24433SOleg Nesterov * Nobody can add the work_struct to this cwq, 10053af24433SOleg Nesterov * if (caller is __create_workqueue) 10063af24433SOleg Nesterov * nobody should see this wq 10073af24433SOleg Nesterov * else // caller is CPU_UP_PREPARE 10083af24433SOleg Nesterov * cpu is not on cpu_online_map 10093af24433SOleg Nesterov * so we can abort safely. 10103af24433SOleg Nesterov */ 10113af24433SOleg Nesterov if (IS_ERR(p)) 10123af24433SOleg Nesterov return PTR_ERR(p); 10133af24433SOleg Nesterov cwq->thread = p; 10143af24433SOleg Nesterov 10153af24433SOleg Nesterov return 0; 10163af24433SOleg Nesterov } 10173af24433SOleg Nesterov 101806ba38a9SOleg Nesterov static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 101906ba38a9SOleg Nesterov { 102006ba38a9SOleg Nesterov struct task_struct *p = cwq->thread; 102106ba38a9SOleg Nesterov 102206ba38a9SOleg Nesterov if (p != NULL) { 102306ba38a9SOleg Nesterov if (cpu >= 0) 102406ba38a9SOleg Nesterov kthread_bind(p, cpu); 102506ba38a9SOleg Nesterov wake_up_process(p); 102606ba38a9SOleg Nesterov } 102706ba38a9SOleg Nesterov } 102806ba38a9SOleg Nesterov 10294e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name, 103097e37d7bSTejun Heo unsigned int flags, 1031eb13ba87SJohannes Berg struct lock_class_key *key, 1032eb13ba87SJohannes Berg const char *lock_name) 10333af24433SOleg Nesterov { 10343af24433SOleg Nesterov struct workqueue_struct *wq; 10353af24433SOleg Nesterov struct cpu_workqueue_struct *cwq; 10363af24433SOleg Nesterov int err = 0, cpu; 10373af24433SOleg Nesterov 10383af24433SOleg Nesterov wq = kzalloc(sizeof(*wq), GFP_KERNEL); 10393af24433SOleg Nesterov if (!wq) 10404690c4abSTejun Heo goto err; 10413af24433SOleg Nesterov 10423af24433SOleg Nesterov wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); 10434690c4abSTejun Heo if (!wq->cpu_wq) 10444690c4abSTejun Heo goto err; 10453af24433SOleg Nesterov 104697e37d7bSTejun Heo wq->flags = flags; 10473af24433SOleg Nesterov wq->name = name; 1048eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 1049cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 10503af24433SOleg Nesterov 105197e37d7bSTejun Heo if (flags & WQ_SINGLE_THREAD) { 10523af24433SOleg Nesterov cwq = init_cpu_workqueue(wq, singlethread_cpu); 10533af24433SOleg Nesterov err = create_workqueue_thread(cwq, singlethread_cpu); 105406ba38a9SOleg Nesterov start_workqueue_thread(cwq, -1); 10553af24433SOleg Nesterov } else { 10563da1c84cSOleg Nesterov cpu_maps_update_begin(); 10576af8bf3dSOleg Nesterov /* 10586af8bf3dSOleg Nesterov * We must place this wq on list even if the code below fails. 10596af8bf3dSOleg Nesterov * cpu_down(cpu) can remove cpu from cpu_populated_map before 10606af8bf3dSOleg Nesterov * destroy_workqueue() takes the lock, in that case we leak 10616af8bf3dSOleg Nesterov * cwq[cpu]->thread. 10626af8bf3dSOleg Nesterov */ 106395402b38SGautham R Shenoy spin_lock(&workqueue_lock); 10643af24433SOleg Nesterov list_add(&wq->list, &workqueues); 106595402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 10666af8bf3dSOleg Nesterov /* 10676af8bf3dSOleg Nesterov * We must initialize cwqs for each possible cpu even if we 10686af8bf3dSOleg Nesterov * are going to call destroy_workqueue() finally. Otherwise 10696af8bf3dSOleg Nesterov * cpu_up() can hit the uninitialized cwq once we drop the 10706af8bf3dSOleg Nesterov * lock. 10716af8bf3dSOleg Nesterov */ 10723af24433SOleg Nesterov for_each_possible_cpu(cpu) { 10733af24433SOleg Nesterov cwq = init_cpu_workqueue(wq, cpu); 10743af24433SOleg Nesterov if (err || !cpu_online(cpu)) 10753af24433SOleg Nesterov continue; 10763af24433SOleg Nesterov err = create_workqueue_thread(cwq, cpu); 107706ba38a9SOleg Nesterov start_workqueue_thread(cwq, cpu); 10783af24433SOleg Nesterov } 10793da1c84cSOleg Nesterov cpu_maps_update_done(); 10803af24433SOleg Nesterov } 10813af24433SOleg Nesterov 10823af24433SOleg Nesterov if (err) { 10833af24433SOleg Nesterov destroy_workqueue(wq); 10843af24433SOleg Nesterov wq = NULL; 10853af24433SOleg Nesterov } 10863af24433SOleg Nesterov return wq; 10874690c4abSTejun Heo err: 10884690c4abSTejun Heo if (wq) { 10894690c4abSTejun Heo free_percpu(wq->cpu_wq); 10904690c4abSTejun Heo kfree(wq); 10914690c4abSTejun Heo } 10924690c4abSTejun Heo return NULL; 10933af24433SOleg Nesterov } 10944e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key); 10953af24433SOleg Nesterov 10961e35eaa2SOleg Nesterov static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq) 10973af24433SOleg Nesterov { 10983af24433SOleg Nesterov /* 10993da1c84cSOleg Nesterov * Our caller is either destroy_workqueue() or CPU_POST_DEAD, 11003da1c84cSOleg Nesterov * cpu_add_remove_lock protects cwq->thread. 11013af24433SOleg Nesterov */ 110214441960SOleg Nesterov if (cwq->thread == NULL) 110314441960SOleg Nesterov return; 110414441960SOleg Nesterov 11053295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 11063295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 11074e6045f1SJohannes Berg 110813c22168SOleg Nesterov flush_cpu_workqueue(cwq); 110914441960SOleg Nesterov /* 11103da1c84cSOleg Nesterov * If the caller is CPU_POST_DEAD and cwq->worklist was not empty, 111113c22168SOleg Nesterov * a concurrent flush_workqueue() can insert a barrier after us. 111213c22168SOleg Nesterov * However, in that case run_workqueue() won't return and check 111313c22168SOleg Nesterov * kthread_should_stop() until it flushes all work_struct's. 111414441960SOleg Nesterov * When ->worklist becomes empty it is safe to exit because no 111514441960SOleg Nesterov * more work_structs can be queued on this cwq: flush_workqueue 111614441960SOleg Nesterov * checks list_empty(), and a "normal" queue_work() can't use 111714441960SOleg Nesterov * a dead CPU. 111814441960SOleg Nesterov */ 111914441960SOleg Nesterov kthread_stop(cwq->thread); 112014441960SOleg Nesterov cwq->thread = NULL; 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 11233af24433SOleg Nesterov /** 11243af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 11253af24433SOleg Nesterov * @wq: target workqueue 11263af24433SOleg Nesterov * 11273af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 11283af24433SOleg Nesterov */ 11293af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 11303af24433SOleg Nesterov { 1131e7577c50SRusty Russell const struct cpumask *cpu_map = wq_cpu_map(wq); 11323af24433SOleg Nesterov int cpu; 11333af24433SOleg Nesterov 11343da1c84cSOleg Nesterov cpu_maps_update_begin(); 113595402b38SGautham R Shenoy spin_lock(&workqueue_lock); 11363af24433SOleg Nesterov list_del(&wq->list); 113795402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 11383af24433SOleg Nesterov 1139aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 11401e35eaa2SOleg Nesterov cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu)); 11413da1c84cSOleg Nesterov cpu_maps_update_done(); 11423af24433SOleg Nesterov 11433af24433SOleg Nesterov free_percpu(wq->cpu_wq); 11443af24433SOleg Nesterov kfree(wq); 11453af24433SOleg Nesterov } 11463af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 11473af24433SOleg Nesterov 11489c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, 11491da177e4SLinus Torvalds unsigned long action, 11501da177e4SLinus Torvalds void *hcpu) 11511da177e4SLinus Torvalds { 11523af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 11533af24433SOleg Nesterov struct cpu_workqueue_struct *cwq; 11541da177e4SLinus Torvalds struct workqueue_struct *wq; 115580b5184cSAkinobu Mita int err = 0; 11561da177e4SLinus Torvalds 11578bb78442SRafael J. Wysocki action &= ~CPU_TASKS_FROZEN; 11588bb78442SRafael J. Wysocki 11591da177e4SLinus Torvalds switch (action) { 11603af24433SOleg Nesterov case CPU_UP_PREPARE: 1161e7577c50SRusty Russell cpumask_set_cpu(cpu, cpu_populated_map); 11623af24433SOleg Nesterov } 11638448502cSOleg Nesterov undo: 11641da177e4SLinus Torvalds list_for_each_entry(wq, &workqueues, list) { 11653af24433SOleg Nesterov cwq = per_cpu_ptr(wq->cpu_wq, cpu); 11663af24433SOleg Nesterov 11673af24433SOleg Nesterov switch (action) { 11683af24433SOleg Nesterov case CPU_UP_PREPARE: 116980b5184cSAkinobu Mita err = create_workqueue_thread(cwq, cpu); 117080b5184cSAkinobu Mita if (!err) 11711da177e4SLinus Torvalds break; 117295402b38SGautham R Shenoy printk(KERN_ERR "workqueue [%s] for %i failed\n", 117395402b38SGautham R Shenoy wq->name, cpu); 11748448502cSOleg Nesterov action = CPU_UP_CANCELED; 117580b5184cSAkinobu Mita err = -ENOMEM; 11768448502cSOleg Nesterov goto undo; 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds case CPU_ONLINE: 117906ba38a9SOleg Nesterov start_workqueue_thread(cwq, cpu); 11801da177e4SLinus Torvalds break; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds case CPU_UP_CANCELED: 118306ba38a9SOleg Nesterov start_workqueue_thread(cwq, -1); 11843da1c84cSOleg Nesterov case CPU_POST_DEAD: 11851e35eaa2SOleg Nesterov cleanup_workqueue_thread(cwq); 11861da177e4SLinus Torvalds break; 11871da177e4SLinus Torvalds } 11883af24433SOleg Nesterov } 11891da177e4SLinus Torvalds 119000dfcaf7SOleg Nesterov switch (action) { 119100dfcaf7SOleg Nesterov case CPU_UP_CANCELED: 11923da1c84cSOleg Nesterov case CPU_POST_DEAD: 1193e7577c50SRusty Russell cpumask_clear_cpu(cpu, cpu_populated_map); 119400dfcaf7SOleg Nesterov } 119500dfcaf7SOleg Nesterov 119680b5184cSAkinobu Mita return notifier_from_errno(err); 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 11992d3854a3SRusty Russell #ifdef CONFIG_SMP 12008ccad40dSRusty Russell 12012d3854a3SRusty Russell struct work_for_cpu { 12026b44003eSAndrew Morton struct completion completion; 12032d3854a3SRusty Russell long (*fn)(void *); 12042d3854a3SRusty Russell void *arg; 12052d3854a3SRusty Russell long ret; 12062d3854a3SRusty Russell }; 12072d3854a3SRusty Russell 12086b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 12092d3854a3SRusty Russell { 12106b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 12112d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 12126b44003eSAndrew Morton complete(&wfc->completion); 12136b44003eSAndrew Morton return 0; 12142d3854a3SRusty Russell } 12152d3854a3SRusty Russell 12162d3854a3SRusty Russell /** 12172d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 12182d3854a3SRusty Russell * @cpu: the cpu to run on 12192d3854a3SRusty Russell * @fn: the function to run 12202d3854a3SRusty Russell * @arg: the function arg 12212d3854a3SRusty Russell * 122231ad9081SRusty Russell * This will return the value @fn returns. 122331ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 12246b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 12252d3854a3SRusty Russell */ 12262d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 12272d3854a3SRusty Russell { 12286b44003eSAndrew Morton struct task_struct *sub_thread; 12296b44003eSAndrew Morton struct work_for_cpu wfc = { 12306b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 12316b44003eSAndrew Morton .fn = fn, 12326b44003eSAndrew Morton .arg = arg, 12336b44003eSAndrew Morton }; 12342d3854a3SRusty Russell 12356b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 12366b44003eSAndrew Morton if (IS_ERR(sub_thread)) 12376b44003eSAndrew Morton return PTR_ERR(sub_thread); 12386b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 12396b44003eSAndrew Morton wake_up_process(sub_thread); 12406b44003eSAndrew Morton wait_for_completion(&wfc.completion); 12412d3854a3SRusty Russell return wfc.ret; 12422d3854a3SRusty Russell } 12432d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 12442d3854a3SRusty Russell #endif /* CONFIG_SMP */ 12452d3854a3SRusty Russell 1246c12920d1SOleg Nesterov void __init init_workqueues(void) 12471da177e4SLinus Torvalds { 1248e7577c50SRusty Russell alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL); 1249e7577c50SRusty Russell 1250e7577c50SRusty Russell cpumask_copy(cpu_populated_map, cpu_online_mask); 1251e7577c50SRusty Russell singlethread_cpu = cpumask_first(cpu_possible_mask); 1252e7577c50SRusty Russell cpu_singlethread_map = cpumask_of(singlethread_cpu); 12531da177e4SLinus Torvalds hotcpu_notifier(workqueue_cpu_callback, 0); 12541da177e4SLinus Torvalds keventd_wq = create_workqueue("events"); 12551da177e4SLinus Torvalds BUG_ON(!keventd_wq); 12561da177e4SLinus Torvalds } 1257