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> 36fb39125fSZhaolei #define CREATE_TRACE_POINTS 37fb39125fSZhaolei #include <trace/events/workqueue.h> 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds /* 404690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 414690c4abSTejun Heo * 424690c4abSTejun Heo * I: Set during initialization and read-only afterwards. 434690c4abSTejun Heo * 444690c4abSTejun Heo * L: cwq->lock protected. Access with cwq->lock held. 454690c4abSTejun Heo * 464690c4abSTejun Heo * W: workqueue_lock protected. 474690c4abSTejun Heo */ 484690c4abSTejun Heo 494690c4abSTejun Heo /* 50f756d5e2SNathan Lynch * The per-CPU workqueue (if single thread, we always use the first 51f756d5e2SNathan Lynch * possible cpu). 521da177e4SLinus Torvalds */ 531da177e4SLinus Torvalds struct cpu_workqueue_struct { 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds spinlock_t lock; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds struct list_head worklist; 581da177e4SLinus Torvalds wait_queue_head_t more_work; 593af24433SOleg Nesterov struct work_struct *current_work; 601da177e4SLinus Torvalds 614690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 6236c8b586SIngo Molnar struct task_struct *thread; 631da177e4SLinus Torvalds } ____cacheline_aligned; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* 661da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 671da177e4SLinus Torvalds * per-CPU workqueues: 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds struct workqueue_struct { 7097e37d7bSTejun Heo unsigned int flags; /* I: WQ_* flags */ 714690c4abSTejun Heo struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */ 724690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 734690c4abSTejun Heo const char *name; /* I: workqueue name */ 744e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 754e6045f1SJohannes Berg struct lockdep_map lockdep_map; 764e6045f1SJohannes Berg #endif 771da177e4SLinus Torvalds }; 781da177e4SLinus Torvalds 79dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 80dc186ad7SThomas Gleixner 81dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 82dc186ad7SThomas Gleixner 83dc186ad7SThomas Gleixner /* 84dc186ad7SThomas Gleixner * fixup_init is called when: 85dc186ad7SThomas Gleixner * - an active object is initialized 86dc186ad7SThomas Gleixner */ 87dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 88dc186ad7SThomas Gleixner { 89dc186ad7SThomas Gleixner struct work_struct *work = addr; 90dc186ad7SThomas Gleixner 91dc186ad7SThomas Gleixner switch (state) { 92dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 93dc186ad7SThomas Gleixner cancel_work_sync(work); 94dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 95dc186ad7SThomas Gleixner return 1; 96dc186ad7SThomas Gleixner default: 97dc186ad7SThomas Gleixner return 0; 98dc186ad7SThomas Gleixner } 99dc186ad7SThomas Gleixner } 100dc186ad7SThomas Gleixner 101dc186ad7SThomas Gleixner /* 102dc186ad7SThomas Gleixner * fixup_activate is called when: 103dc186ad7SThomas Gleixner * - an active object is activated 104dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 105dc186ad7SThomas Gleixner */ 106dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 107dc186ad7SThomas Gleixner { 108dc186ad7SThomas Gleixner struct work_struct *work = addr; 109dc186ad7SThomas Gleixner 110dc186ad7SThomas Gleixner switch (state) { 111dc186ad7SThomas Gleixner 112dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 113dc186ad7SThomas Gleixner /* 114dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 115dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 116dc186ad7SThomas Gleixner * is tracked in the object tracker. 117dc186ad7SThomas Gleixner */ 11822df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 119dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 120dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 121dc186ad7SThomas Gleixner return 0; 122dc186ad7SThomas Gleixner } 123dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 124dc186ad7SThomas Gleixner return 0; 125dc186ad7SThomas Gleixner 126dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 127dc186ad7SThomas Gleixner WARN_ON(1); 128dc186ad7SThomas Gleixner 129dc186ad7SThomas Gleixner default: 130dc186ad7SThomas Gleixner return 0; 131dc186ad7SThomas Gleixner } 132dc186ad7SThomas Gleixner } 133dc186ad7SThomas Gleixner 134dc186ad7SThomas Gleixner /* 135dc186ad7SThomas Gleixner * fixup_free is called when: 136dc186ad7SThomas Gleixner * - an active object is freed 137dc186ad7SThomas Gleixner */ 138dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 139dc186ad7SThomas Gleixner { 140dc186ad7SThomas Gleixner struct work_struct *work = addr; 141dc186ad7SThomas Gleixner 142dc186ad7SThomas Gleixner switch (state) { 143dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 144dc186ad7SThomas Gleixner cancel_work_sync(work); 145dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 146dc186ad7SThomas Gleixner return 1; 147dc186ad7SThomas Gleixner default: 148dc186ad7SThomas Gleixner return 0; 149dc186ad7SThomas Gleixner } 150dc186ad7SThomas Gleixner } 151dc186ad7SThomas Gleixner 152dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 153dc186ad7SThomas Gleixner .name = "work_struct", 154dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 155dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 156dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 157dc186ad7SThomas Gleixner }; 158dc186ad7SThomas Gleixner 159dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 160dc186ad7SThomas Gleixner { 161dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 162dc186ad7SThomas Gleixner } 163dc186ad7SThomas Gleixner 164dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 165dc186ad7SThomas Gleixner { 166dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 167dc186ad7SThomas Gleixner } 168dc186ad7SThomas Gleixner 169dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 170dc186ad7SThomas Gleixner { 171dc186ad7SThomas Gleixner if (onstack) 172dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 173dc186ad7SThomas Gleixner else 174dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 175dc186ad7SThomas Gleixner } 176dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 177dc186ad7SThomas Gleixner 178dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 179dc186ad7SThomas Gleixner { 180dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 181dc186ad7SThomas Gleixner } 182dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 183dc186ad7SThomas Gleixner 184dc186ad7SThomas Gleixner #else 185dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 186dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 187dc186ad7SThomas Gleixner #endif 188dc186ad7SThomas Gleixner 18995402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 19095402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 1911da177e4SLinus Torvalds static LIST_HEAD(workqueues); 1921da177e4SLinus Torvalds 1933af24433SOleg Nesterov static int singlethread_cpu __read_mostly; 194e7577c50SRusty Russell static const struct cpumask *cpu_singlethread_map __read_mostly; 19514441960SOleg Nesterov /* 19614441960SOleg Nesterov * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD 19714441960SOleg Nesterov * flushes cwq->worklist. This means that flush_workqueue/wait_on_work 19814441960SOleg Nesterov * which comes in between can't use for_each_online_cpu(). We could 19914441960SOleg Nesterov * use cpu_possible_map, the cpumask below is more a documentation 20014441960SOleg Nesterov * than optimization. 20114441960SOleg Nesterov */ 202e7577c50SRusty Russell static cpumask_var_t cpu_populated_map __read_mostly; 203f756d5e2SNathan Lynch 2041da177e4SLinus Torvalds /* If it's single threaded, it isn't in the list of workqueues. */ 20597e37d7bSTejun Heo static inline bool is_wq_single_threaded(struct workqueue_struct *wq) 2061da177e4SLinus Torvalds { 20797e37d7bSTejun Heo return wq->flags & WQ_SINGLE_THREAD; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 210e7577c50SRusty Russell static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq) 211b1f4ec17SOleg Nesterov { 2126cc88bc4SDavid Howells return is_wq_single_threaded(wq) 213e7577c50SRusty Russell ? cpu_singlethread_map : cpu_populated_map; 214b1f4ec17SOleg Nesterov } 215b1f4ec17SOleg Nesterov 2164690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, 2174690c4abSTejun Heo struct workqueue_struct *wq) 218a848e3b6SOleg Nesterov { 2196cc88bc4SDavid Howells if (unlikely(is_wq_single_threaded(wq))) 220a848e3b6SOleg Nesterov cpu = singlethread_cpu; 221a848e3b6SOleg Nesterov return per_cpu_ptr(wq->cpu_wq, cpu); 222a848e3b6SOleg Nesterov } 223a848e3b6SOleg Nesterov 2244594bf15SDavid Howells /* 2254594bf15SDavid Howells * Set the workqueue on which a work item is to be run 2264594bf15SDavid Howells * - Must *only* be called if the pending flag is set 2274594bf15SDavid Howells */ 228ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work, 2294690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 2304690c4abSTejun Heo unsigned long extra_flags) 231365970a1SDavid Howells { 2324594bf15SDavid Howells BUG_ON(!work_pending(work)); 2334594bf15SDavid Howells 2344690c4abSTejun Heo atomic_long_set(&work->data, (unsigned long)cwq | work_static(work) | 23522df02bbSTejun Heo WORK_STRUCT_PENDING | extra_flags); 236365970a1SDavid Howells } 237365970a1SDavid Howells 2384d707b9fSOleg Nesterov /* 2394d707b9fSOleg Nesterov * Clear WORK_STRUCT_PENDING and the workqueue on which it was queued. 2404d707b9fSOleg Nesterov */ 2414d707b9fSOleg Nesterov static inline void clear_wq_data(struct work_struct *work) 2424d707b9fSOleg Nesterov { 2434690c4abSTejun Heo atomic_long_set(&work->data, work_static(work)); 2444d707b9fSOleg Nesterov } 2454d707b9fSOleg Nesterov 246ed7c0feeSOleg Nesterov static inline 247ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) 248365970a1SDavid Howells { 249a08727baSLinus Torvalds return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK); 250365970a1SDavid Howells } 251365970a1SDavid Howells 2524690c4abSTejun Heo /** 2534690c4abSTejun Heo * insert_work - insert a work into cwq 2544690c4abSTejun Heo * @cwq: cwq @work belongs to 2554690c4abSTejun Heo * @work: work to insert 2564690c4abSTejun Heo * @head: insertion point 2574690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 2584690c4abSTejun Heo * 2594690c4abSTejun Heo * Insert @work into @cwq after @head. 2604690c4abSTejun Heo * 2614690c4abSTejun Heo * CONTEXT: 2624690c4abSTejun Heo * spin_lock_irq(cwq->lock). 2634690c4abSTejun Heo */ 264b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 2654690c4abSTejun Heo struct work_struct *work, struct list_head *head, 2664690c4abSTejun Heo unsigned int extra_flags) 267b89deed3SOleg Nesterov { 268e1d8aa9fSFrederic Weisbecker trace_workqueue_insertion(cwq->thread, work); 269e1d8aa9fSFrederic Weisbecker 2704690c4abSTejun Heo /* we own @work, set data and link */ 2714690c4abSTejun Heo set_wq_data(work, cwq, extra_flags); 2724690c4abSTejun Heo 2736e84d644SOleg Nesterov /* 2746e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 2756e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 2766e84d644SOleg Nesterov */ 2776e84d644SOleg Nesterov smp_wmb(); 2784690c4abSTejun Heo 2791a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 280b89deed3SOleg Nesterov wake_up(&cwq->more_work); 281b89deed3SOleg Nesterov } 282b89deed3SOleg Nesterov 2834690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 2841da177e4SLinus Torvalds struct work_struct *work) 2851da177e4SLinus Torvalds { 2864690c4abSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2871da177e4SLinus Torvalds unsigned long flags; 2881da177e4SLinus Torvalds 289dc186ad7SThomas Gleixner debug_work_activate(work); 2901da177e4SLinus Torvalds spin_lock_irqsave(&cwq->lock, flags); 2914690c4abSTejun Heo BUG_ON(!list_empty(&work->entry)); 2924690c4abSTejun Heo insert_work(cwq, work, &cwq->worklist, 0); 2931da177e4SLinus Torvalds spin_unlock_irqrestore(&cwq->lock, flags); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds 2960fcb78c2SRolf Eike Beer /** 2970fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 2980fcb78c2SRolf Eike Beer * @wq: workqueue to use 2990fcb78c2SRolf Eike Beer * @work: work to queue 3000fcb78c2SRolf Eike Beer * 301057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 3021da177e4SLinus Torvalds * 30300dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 30400dfcaf7SOleg Nesterov * it can be processed by another CPU. 3051da177e4SLinus Torvalds */ 3067ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 3071da177e4SLinus Torvalds { 308ef1ca236SOleg Nesterov int ret; 3091da177e4SLinus Torvalds 310ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 311a848e3b6SOleg Nesterov put_cpu(); 312ef1ca236SOleg Nesterov 3131da177e4SLinus Torvalds return ret; 3141da177e4SLinus Torvalds } 315ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 3161da177e4SLinus Torvalds 317c1a220e7SZhang Rui /** 318c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 319c1a220e7SZhang Rui * @cpu: CPU number to execute work on 320c1a220e7SZhang Rui * @wq: workqueue to use 321c1a220e7SZhang Rui * @work: work to queue 322c1a220e7SZhang Rui * 323c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 324c1a220e7SZhang Rui * 325c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 326c1a220e7SZhang Rui * can't go away. 327c1a220e7SZhang Rui */ 328c1a220e7SZhang Rui int 329c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 330c1a220e7SZhang Rui { 331c1a220e7SZhang Rui int ret = 0; 332c1a220e7SZhang Rui 33322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 3344690c4abSTejun Heo __queue_work(cpu, wq, work); 335c1a220e7SZhang Rui ret = 1; 336c1a220e7SZhang Rui } 337c1a220e7SZhang Rui return ret; 338c1a220e7SZhang Rui } 339c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 340c1a220e7SZhang Rui 3416d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 3421da177e4SLinus Torvalds { 34352bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 344ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); 3451da177e4SLinus Torvalds 3464690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3490fcb78c2SRolf Eike Beer /** 3500fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 3510fcb78c2SRolf Eike Beer * @wq: workqueue to use 352af9997e4SRandy Dunlap * @dwork: delayable work to queue 3530fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 3540fcb78c2SRolf Eike Beer * 355057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 3560fcb78c2SRolf Eike Beer */ 3577ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 35852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 3591da177e4SLinus Torvalds { 36052bad64dSDavid Howells if (delay == 0) 36163bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 3621da177e4SLinus Torvalds 36363bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 3641da177e4SLinus Torvalds } 365ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 3661da177e4SLinus Torvalds 3670fcb78c2SRolf Eike Beer /** 3680fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 3690fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 3700fcb78c2SRolf Eike Beer * @wq: workqueue to use 371af9997e4SRandy Dunlap * @dwork: work to queue 3720fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 3730fcb78c2SRolf Eike Beer * 374057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 3750fcb78c2SRolf Eike Beer */ 3767a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 37752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 3787a6bc1cdSVenkatesh Pallipadi { 3797a6bc1cdSVenkatesh Pallipadi int ret = 0; 38052bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 38152bad64dSDavid Howells struct work_struct *work = &dwork->work; 3827a6bc1cdSVenkatesh Pallipadi 38322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 3847a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 3857a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 3867a6bc1cdSVenkatesh Pallipadi 3878a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 3888a3e77ccSAndrew Liu 389ed7c0feeSOleg Nesterov /* This stores cwq for the moment, for the timer_fn */ 3904690c4abSTejun Heo set_wq_data(work, get_cwq(raw_smp_processor_id(), wq), 0); 3917a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 39252bad64dSDavid Howells timer->data = (unsigned long)dwork; 3937a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 39463bc0362SOleg Nesterov 39563bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 3967a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 39763bc0362SOleg Nesterov else 39863bc0362SOleg Nesterov add_timer(timer); 3997a6bc1cdSVenkatesh Pallipadi ret = 1; 4007a6bc1cdSVenkatesh Pallipadi } 4017a6bc1cdSVenkatesh Pallipadi return ret; 4027a6bc1cdSVenkatesh Pallipadi } 403ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 4041da177e4SLinus Torvalds 405*a62428c0STejun Heo /** 406*a62428c0STejun Heo * process_one_work - process single work 407*a62428c0STejun Heo * @cwq: cwq to process work for 408*a62428c0STejun Heo * @work: work to process 409*a62428c0STejun Heo * 410*a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 411*a62428c0STejun Heo * process a single work including synchronization against and 412*a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 413*a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 414*a62428c0STejun Heo * call this function to process a work. 415*a62428c0STejun Heo * 416*a62428c0STejun Heo * CONTEXT: 417*a62428c0STejun Heo * spin_lock_irq(cwq->lock) which is released and regrabbed. 418*a62428c0STejun Heo */ 419*a62428c0STejun Heo static void process_one_work(struct cpu_workqueue_struct *cwq, 420*a62428c0STejun Heo struct work_struct *work) 4211da177e4SLinus Torvalds { 4226bb49e59SDavid Howells work_func_t f = work->func; 4234e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 4244e6045f1SJohannes Berg /* 425*a62428c0STejun Heo * It is permissible to free the struct work_struct from 426*a62428c0STejun Heo * inside the function that is called from it, this we need to 427*a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 428*a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 429*a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 4304e6045f1SJohannes Berg */ 4314e6045f1SJohannes Berg struct lockdep_map lockdep_map = work->lockdep_map; 4324e6045f1SJohannes Berg #endif 433*a62428c0STejun Heo /* claim and process */ 434e1d8aa9fSFrederic Weisbecker trace_workqueue_execution(cwq->thread, work); 435dc186ad7SThomas Gleixner debug_work_deactivate(work); 436b89deed3SOleg Nesterov cwq->current_work = work; 437*a62428c0STejun Heo list_del_init(&work->entry); 438*a62428c0STejun Heo 439f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 4401da177e4SLinus Torvalds 441365970a1SDavid Howells BUG_ON(get_wq_data(work) != cwq); 44223b2e599SOleg Nesterov work_clear_pending(work); 4433295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 4443295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 44565f27f38SDavid Howells f(work); 4463295f0efSIngo Molnar lock_map_release(&lockdep_map); 4473295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 4481da177e4SLinus Torvalds 449d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 450d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 451d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 452*a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 453d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 454d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 455d5abe669SPeter Zijlstra debug_show_held_locks(current); 456d5abe669SPeter Zijlstra dump_stack(); 457d5abe669SPeter Zijlstra } 458d5abe669SPeter Zijlstra 459f293ea92SOleg Nesterov spin_lock_irq(&cwq->lock); 460*a62428c0STejun Heo 461*a62428c0STejun Heo /* we're done with it, release */ 462b89deed3SOleg Nesterov cwq->current_work = NULL; 4631da177e4SLinus Torvalds } 464*a62428c0STejun Heo 465*a62428c0STejun Heo static void run_workqueue(struct cpu_workqueue_struct *cwq) 466*a62428c0STejun Heo { 467*a62428c0STejun Heo spin_lock_irq(&cwq->lock); 468*a62428c0STejun Heo while (!list_empty(&cwq->worklist)) { 469*a62428c0STejun Heo struct work_struct *work = list_entry(cwq->worklist.next, 470*a62428c0STejun Heo struct work_struct, entry); 471*a62428c0STejun Heo process_one_work(cwq, work); 472*a62428c0STejun Heo } 473f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 4764690c4abSTejun Heo /** 4774690c4abSTejun Heo * worker_thread - the worker thread function 4784690c4abSTejun Heo * @__cwq: cwq to serve 4794690c4abSTejun Heo * 4804690c4abSTejun Heo * The cwq worker thread function. 4814690c4abSTejun Heo */ 4821da177e4SLinus Torvalds static int worker_thread(void *__cwq) 4831da177e4SLinus Torvalds { 4841da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq = __cwq; 4853af24433SOleg Nesterov DEFINE_WAIT(wait); 4861da177e4SLinus Torvalds 48797e37d7bSTejun Heo if (cwq->wq->flags & WQ_FREEZEABLE) 48883144186SRafael J. Wysocki set_freezable(); 4891da177e4SLinus Torvalds 4903af24433SOleg Nesterov for (;;) { 4913af24433SOleg Nesterov prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); 49214441960SOleg Nesterov if (!freezing(current) && 49314441960SOleg Nesterov !kthread_should_stop() && 49414441960SOleg Nesterov list_empty(&cwq->worklist)) 4951da177e4SLinus Torvalds schedule(); 4963af24433SOleg Nesterov finish_wait(&cwq->more_work, &wait); 4971da177e4SLinus Torvalds 49885f4186aSOleg Nesterov try_to_freeze(); 49985f4186aSOleg Nesterov 50014441960SOleg Nesterov if (kthread_should_stop()) 5013af24433SOleg Nesterov break; 5023af24433SOleg Nesterov 5031da177e4SLinus Torvalds run_workqueue(cwq); 5041da177e4SLinus Torvalds } 5053af24433SOleg Nesterov 5061da177e4SLinus Torvalds return 0; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 509fc2e4d70SOleg Nesterov struct wq_barrier { 510fc2e4d70SOleg Nesterov struct work_struct work; 511fc2e4d70SOleg Nesterov struct completion done; 512fc2e4d70SOleg Nesterov }; 513fc2e4d70SOleg Nesterov 514fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 515fc2e4d70SOleg Nesterov { 516fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 517fc2e4d70SOleg Nesterov complete(&barr->done); 518fc2e4d70SOleg Nesterov } 519fc2e4d70SOleg Nesterov 5204690c4abSTejun Heo /** 5214690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 5224690c4abSTejun Heo * @cwq: cwq to insert barrier into 5234690c4abSTejun Heo * @barr: wq_barrier to insert 5244690c4abSTejun Heo * @head: insertion point 5254690c4abSTejun Heo * 5264690c4abSTejun Heo * Insert barrier @barr into @cwq before @head. 5274690c4abSTejun Heo * 5284690c4abSTejun Heo * CONTEXT: 5294690c4abSTejun Heo * spin_lock_irq(cwq->lock). 5304690c4abSTejun Heo */ 53183c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 5321a4d9b0aSOleg Nesterov struct wq_barrier *barr, struct list_head *head) 533fc2e4d70SOleg Nesterov { 534dc186ad7SThomas Gleixner /* 535dc186ad7SThomas Gleixner * debugobject calls are safe here even with cwq->lock locked 536dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 537dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 538dc186ad7SThomas Gleixner * might deadlock. 539dc186ad7SThomas Gleixner */ 540dc186ad7SThomas Gleixner INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); 54122df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 542fc2e4d70SOleg Nesterov init_completion(&barr->done); 54383c22520SOleg Nesterov 544dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 5454690c4abSTejun Heo insert_work(cwq, &barr->work, head, 0); 546fc2e4d70SOleg Nesterov } 547fc2e4d70SOleg Nesterov 54814441960SOleg Nesterov static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) 5491da177e4SLinus Torvalds { 5502355b70fSLai Jiangshan int active = 0; 551fc2e4d70SOleg Nesterov struct wq_barrier barr; 5521da177e4SLinus Torvalds 5532355b70fSLai Jiangshan WARN_ON(cwq->thread == current); 5542355b70fSLai Jiangshan 55583c22520SOleg Nesterov spin_lock_irq(&cwq->lock); 55683c22520SOleg Nesterov if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { 5571a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, &cwq->worklist); 55883c22520SOleg Nesterov active = 1; 55983c22520SOleg Nesterov } 56083c22520SOleg Nesterov spin_unlock_irq(&cwq->lock); 5611da177e4SLinus Torvalds 562dc186ad7SThomas Gleixner if (active) { 563fc2e4d70SOleg Nesterov wait_for_completion(&barr.done); 564dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 565dc186ad7SThomas Gleixner } 56614441960SOleg Nesterov 56714441960SOleg Nesterov return active; 56883c22520SOleg Nesterov } 5691da177e4SLinus Torvalds 5700fcb78c2SRolf Eike Beer /** 5711da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 5720fcb78c2SRolf Eike Beer * @wq: workqueue to flush 5731da177e4SLinus Torvalds * 5741da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 5751da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 5761da177e4SLinus Torvalds * 577fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 578fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 5791da177e4SLinus Torvalds */ 5807ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 5811da177e4SLinus Torvalds { 582e7577c50SRusty Russell const struct cpumask *cpu_map = wq_cpu_map(wq); 583cce1a165SOleg Nesterov int cpu; 584b1f4ec17SOleg Nesterov 585f293ea92SOleg Nesterov might_sleep(); 5863295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 5873295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 588aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 58989ada679SChristoph Lameter flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); 5901da177e4SLinus Torvalds } 591ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 5921da177e4SLinus Torvalds 593db700897SOleg Nesterov /** 594db700897SOleg Nesterov * flush_work - block until a work_struct's callback has terminated 595db700897SOleg Nesterov * @work: the work which is to be flushed 596db700897SOleg Nesterov * 597a67da70dSOleg Nesterov * Returns false if @work has already terminated. 598a67da70dSOleg Nesterov * 599db700897SOleg Nesterov * It is expected that, prior to calling flush_work(), the caller has 600db700897SOleg Nesterov * arranged for the work to not be requeued, otherwise it doesn't make 601db700897SOleg Nesterov * sense to use this function. 602db700897SOleg Nesterov */ 603db700897SOleg Nesterov int flush_work(struct work_struct *work) 604db700897SOleg Nesterov { 605db700897SOleg Nesterov struct cpu_workqueue_struct *cwq; 606db700897SOleg Nesterov struct list_head *prev; 607db700897SOleg Nesterov struct wq_barrier barr; 608db700897SOleg Nesterov 609db700897SOleg Nesterov might_sleep(); 610db700897SOleg Nesterov cwq = get_wq_data(work); 611db700897SOleg Nesterov if (!cwq) 612db700897SOleg Nesterov return 0; 613db700897SOleg Nesterov 6143295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 6153295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 616a67da70dSOleg Nesterov 617db700897SOleg Nesterov spin_lock_irq(&cwq->lock); 618db700897SOleg Nesterov if (!list_empty(&work->entry)) { 619db700897SOleg Nesterov /* 620db700897SOleg Nesterov * See the comment near try_to_grab_pending()->smp_rmb(). 621db700897SOleg Nesterov * If it was re-queued under us we are not going to wait. 622db700897SOleg Nesterov */ 623db700897SOleg Nesterov smp_rmb(); 624db700897SOleg Nesterov if (unlikely(cwq != get_wq_data(work))) 6254690c4abSTejun Heo goto already_gone; 626db700897SOleg Nesterov prev = &work->entry; 627db700897SOleg Nesterov } else { 628db700897SOleg Nesterov if (cwq->current_work != work) 6294690c4abSTejun Heo goto already_gone; 630db700897SOleg Nesterov prev = &cwq->worklist; 631db700897SOleg Nesterov } 632db700897SOleg Nesterov insert_wq_barrier(cwq, &barr, prev->next); 633db700897SOleg Nesterov 6344690c4abSTejun Heo spin_unlock_irq(&cwq->lock); 635db700897SOleg Nesterov wait_for_completion(&barr.done); 636dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 637db700897SOleg Nesterov return 1; 6384690c4abSTejun Heo already_gone: 6394690c4abSTejun Heo spin_unlock_irq(&cwq->lock); 6404690c4abSTejun Heo return 0; 641db700897SOleg Nesterov } 642db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 643db700897SOleg Nesterov 6446e84d644SOleg Nesterov /* 6451f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 6466e84d644SOleg Nesterov * so this work can't be re-armed in any way. 6476e84d644SOleg Nesterov */ 6486e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 6496e84d644SOleg Nesterov { 6506e84d644SOleg Nesterov struct cpu_workqueue_struct *cwq; 6511f1f642eSOleg Nesterov int ret = -1; 6526e84d644SOleg Nesterov 65322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 6541f1f642eSOleg Nesterov return 0; 6556e84d644SOleg Nesterov 6566e84d644SOleg Nesterov /* 6576e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 6586e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 6596e84d644SOleg Nesterov */ 6606e84d644SOleg Nesterov 6616e84d644SOleg Nesterov cwq = get_wq_data(work); 6626e84d644SOleg Nesterov if (!cwq) 6636e84d644SOleg Nesterov return ret; 6646e84d644SOleg Nesterov 6656e84d644SOleg Nesterov spin_lock_irq(&cwq->lock); 6666e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 6676e84d644SOleg Nesterov /* 6686e84d644SOleg Nesterov * This work is queued, but perhaps we locked the wrong cwq. 6696e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 6706e84d644SOleg Nesterov * insert_work()->wmb(). 6716e84d644SOleg Nesterov */ 6726e84d644SOleg Nesterov smp_rmb(); 6736e84d644SOleg Nesterov if (cwq == get_wq_data(work)) { 674dc186ad7SThomas Gleixner debug_work_deactivate(work); 6756e84d644SOleg Nesterov list_del_init(&work->entry); 6766e84d644SOleg Nesterov ret = 1; 6776e84d644SOleg Nesterov } 6786e84d644SOleg Nesterov } 6796e84d644SOleg Nesterov spin_unlock_irq(&cwq->lock); 6806e84d644SOleg Nesterov 6816e84d644SOleg Nesterov return ret; 6826e84d644SOleg Nesterov } 6836e84d644SOleg Nesterov 6846e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, 685b89deed3SOleg Nesterov struct work_struct *work) 686b89deed3SOleg Nesterov { 687b89deed3SOleg Nesterov struct wq_barrier barr; 688b89deed3SOleg Nesterov int running = 0; 689b89deed3SOleg Nesterov 690b89deed3SOleg Nesterov spin_lock_irq(&cwq->lock); 691b89deed3SOleg Nesterov if (unlikely(cwq->current_work == work)) { 6921a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, cwq->worklist.next); 693b89deed3SOleg Nesterov running = 1; 694b89deed3SOleg Nesterov } 695b89deed3SOleg Nesterov spin_unlock_irq(&cwq->lock); 696b89deed3SOleg Nesterov 697dc186ad7SThomas Gleixner if (unlikely(running)) { 698b89deed3SOleg Nesterov wait_for_completion(&barr.done); 699dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 700dc186ad7SThomas Gleixner } 701b89deed3SOleg Nesterov } 702b89deed3SOleg Nesterov 7036e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work) 704b89deed3SOleg Nesterov { 705b89deed3SOleg Nesterov struct cpu_workqueue_struct *cwq; 70628e53bddSOleg Nesterov struct workqueue_struct *wq; 707e7577c50SRusty Russell const struct cpumask *cpu_map; 708b1f4ec17SOleg Nesterov int cpu; 709b89deed3SOleg Nesterov 710f293ea92SOleg Nesterov might_sleep(); 711f293ea92SOleg Nesterov 7123295f0efSIngo Molnar lock_map_acquire(&work->lockdep_map); 7133295f0efSIngo Molnar lock_map_release(&work->lockdep_map); 7144e6045f1SJohannes Berg 715b89deed3SOleg Nesterov cwq = get_wq_data(work); 716b89deed3SOleg Nesterov if (!cwq) 7173af24433SOleg Nesterov return; 718b89deed3SOleg Nesterov 71928e53bddSOleg Nesterov wq = cwq->wq; 72028e53bddSOleg Nesterov cpu_map = wq_cpu_map(wq); 72128e53bddSOleg Nesterov 722aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 7234690c4abSTejun Heo wait_on_cpu_work(get_cwq(cpu, wq), work); 7246e84d644SOleg Nesterov } 7256e84d644SOleg Nesterov 7261f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work, 7271f1f642eSOleg Nesterov struct timer_list* timer) 7281f1f642eSOleg Nesterov { 7291f1f642eSOleg Nesterov int ret; 7301f1f642eSOleg Nesterov 7311f1f642eSOleg Nesterov do { 7321f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 7331f1f642eSOleg Nesterov if (!ret) 7341f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 7351f1f642eSOleg Nesterov wait_on_work(work); 7361f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 7371f1f642eSOleg Nesterov 7384d707b9fSOleg Nesterov clear_wq_data(work); 7391f1f642eSOleg Nesterov return ret; 7401f1f642eSOleg Nesterov } 7411f1f642eSOleg Nesterov 7426e84d644SOleg Nesterov /** 7436e84d644SOleg Nesterov * cancel_work_sync - block until a work_struct's callback has terminated 7446e84d644SOleg Nesterov * @work: the work which is to be flushed 7456e84d644SOleg Nesterov * 7461f1f642eSOleg Nesterov * Returns true if @work was pending. 7471f1f642eSOleg Nesterov * 7486e84d644SOleg Nesterov * cancel_work_sync() will cancel the work if it is queued. If the work's 7496e84d644SOleg Nesterov * callback appears to be running, cancel_work_sync() will block until it 7506e84d644SOleg Nesterov * has completed. 7516e84d644SOleg Nesterov * 7526e84d644SOleg Nesterov * It is possible to use this function if the work re-queues itself. It can 7536e84d644SOleg Nesterov * cancel the work even if it migrates to another workqueue, however in that 7546e84d644SOleg Nesterov * case it only guarantees that work->func() has completed on the last queued 7556e84d644SOleg Nesterov * workqueue. 7566e84d644SOleg Nesterov * 7576e84d644SOleg Nesterov * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not 7586e84d644SOleg Nesterov * pending, otherwise it goes into a busy-wait loop until the timer expires. 7596e84d644SOleg Nesterov * 7606e84d644SOleg Nesterov * The caller must ensure that workqueue_struct on which this work was last 7616e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 7626e84d644SOleg Nesterov */ 7631f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work) 7646e84d644SOleg Nesterov { 7651f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 766b89deed3SOleg Nesterov } 76728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 768b89deed3SOleg Nesterov 7696e84d644SOleg Nesterov /** 770f5a421a4SOleg Nesterov * cancel_delayed_work_sync - reliably kill off a delayed work. 7716e84d644SOleg Nesterov * @dwork: the delayed work struct 7726e84d644SOleg Nesterov * 7731f1f642eSOleg Nesterov * Returns true if @dwork was pending. 7741f1f642eSOleg Nesterov * 7756e84d644SOleg Nesterov * It is possible to use this function if @dwork rearms itself via queue_work() 7766e84d644SOleg Nesterov * or queue_delayed_work(). See also the comment for cancel_work_sync(). 7776e84d644SOleg Nesterov */ 7781f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork) 7796e84d644SOleg Nesterov { 7801f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 7816e84d644SOleg Nesterov } 782f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 7831da177e4SLinus Torvalds 7846e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly; 7851da177e4SLinus Torvalds 7860fcb78c2SRolf Eike Beer /** 7870fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 7880fcb78c2SRolf Eike Beer * @work: job to be done 7890fcb78c2SRolf Eike Beer * 7905b0f437dSBart Van Assche * Returns zero if @work was already on the kernel-global workqueue and 7915b0f437dSBart Van Assche * non-zero otherwise. 7925b0f437dSBart Van Assche * 7935b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 7945b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 7955b0f437dSBart Van Assche * workqueue otherwise. 7960fcb78c2SRolf Eike Beer */ 7977ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 7981da177e4SLinus Torvalds { 7991da177e4SLinus Torvalds return queue_work(keventd_wq, work); 8001da177e4SLinus Torvalds } 801ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 8021da177e4SLinus Torvalds 803c1a220e7SZhang Rui /* 804c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 805c1a220e7SZhang Rui * @cpu: cpu to put the work task on 806c1a220e7SZhang Rui * @work: job to be done 807c1a220e7SZhang Rui * 808c1a220e7SZhang Rui * This puts a job on a specific cpu 809c1a220e7SZhang Rui */ 810c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 811c1a220e7SZhang Rui { 812c1a220e7SZhang Rui return queue_work_on(cpu, keventd_wq, work); 813c1a220e7SZhang Rui } 814c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 815c1a220e7SZhang Rui 8160fcb78c2SRolf Eike Beer /** 8170fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 81852bad64dSDavid Howells * @dwork: job to be done 81952bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 8200fcb78c2SRolf Eike Beer * 8210fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 8220fcb78c2SRolf Eike Beer * workqueue. 8230fcb78c2SRolf Eike Beer */ 8247ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 82582f67cd9SIngo Molnar unsigned long delay) 8261da177e4SLinus Torvalds { 82752bad64dSDavid Howells return queue_delayed_work(keventd_wq, dwork, delay); 8281da177e4SLinus Torvalds } 829ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 8301da177e4SLinus Torvalds 8310fcb78c2SRolf Eike Beer /** 8328c53e463SLinus Torvalds * flush_delayed_work - block until a dwork_struct's callback has terminated 8338c53e463SLinus Torvalds * @dwork: the delayed work which is to be flushed 8348c53e463SLinus Torvalds * 8358c53e463SLinus Torvalds * Any timeout is cancelled, and any pending work is run immediately. 8368c53e463SLinus Torvalds */ 8378c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork) 8388c53e463SLinus Torvalds { 8398c53e463SLinus Torvalds if (del_timer_sync(&dwork->timer)) { 8404690c4abSTejun Heo __queue_work(get_cpu(), get_wq_data(&dwork->work)->wq, 8414690c4abSTejun Heo &dwork->work); 8428c53e463SLinus Torvalds put_cpu(); 8438c53e463SLinus Torvalds } 8448c53e463SLinus Torvalds flush_work(&dwork->work); 8458c53e463SLinus Torvalds } 8468c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work); 8478c53e463SLinus Torvalds 8488c53e463SLinus Torvalds /** 8490fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 8500fcb78c2SRolf Eike Beer * @cpu: cpu to use 85152bad64dSDavid Howells * @dwork: job to be done 8520fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 8530fcb78c2SRolf Eike Beer * 8540fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 8550fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 8560fcb78c2SRolf Eike Beer */ 8571da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 85852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 8591da177e4SLinus Torvalds { 86052bad64dSDavid Howells return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); 8611da177e4SLinus Torvalds } 862ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 8631da177e4SLinus Torvalds 864b6136773SAndrew Morton /** 865b6136773SAndrew Morton * schedule_on_each_cpu - call a function on each online CPU from keventd 866b6136773SAndrew Morton * @func: the function to call 867b6136773SAndrew Morton * 868b6136773SAndrew Morton * Returns zero on success. 869b6136773SAndrew Morton * Returns -ve errno on failure. 870b6136773SAndrew Morton * 871b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 872b6136773SAndrew Morton */ 87365f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 87415316ba8SChristoph Lameter { 87515316ba8SChristoph Lameter int cpu; 87665a64464SAndi Kleen int orig = -1; 877b6136773SAndrew Morton struct work_struct *works; 87815316ba8SChristoph Lameter 879b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 880b6136773SAndrew Morton if (!works) 88115316ba8SChristoph Lameter return -ENOMEM; 882b6136773SAndrew Morton 88395402b38SGautham R Shenoy get_online_cpus(); 88493981800STejun Heo 88593981800STejun Heo /* 88693981800STejun Heo * When running in keventd don't schedule a work item on 88793981800STejun Heo * itself. Can just call directly because the work queue is 88893981800STejun Heo * already bound. This also is faster. 88993981800STejun Heo */ 89093981800STejun Heo if (current_is_keventd()) 89193981800STejun Heo orig = raw_smp_processor_id(); 89293981800STejun Heo 89315316ba8SChristoph Lameter for_each_online_cpu(cpu) { 8949bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 8959bfb1839SIngo Molnar 8969bfb1839SIngo Molnar INIT_WORK(work, func); 89793981800STejun Heo if (cpu != orig) 8988de6d308SOleg Nesterov schedule_work_on(cpu, work); 89915316ba8SChristoph Lameter } 90093981800STejun Heo if (orig >= 0) 90193981800STejun Heo func(per_cpu_ptr(works, orig)); 90293981800STejun Heo 90393981800STejun Heo for_each_online_cpu(cpu) 9048616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 90593981800STejun Heo 90695402b38SGautham R Shenoy put_online_cpus(); 907b6136773SAndrew Morton free_percpu(works); 90815316ba8SChristoph Lameter return 0; 90915316ba8SChristoph Lameter } 91015316ba8SChristoph Lameter 911eef6a7d5SAlan Stern /** 912eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 913eef6a7d5SAlan Stern * 914eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 915eef6a7d5SAlan Stern * completion. 916eef6a7d5SAlan Stern * 917eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 918eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 919eef6a7d5SAlan Stern * will lead to deadlock: 920eef6a7d5SAlan Stern * 921eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 922eef6a7d5SAlan Stern * a lock held by your code or its caller. 923eef6a7d5SAlan Stern * 924eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 925eef6a7d5SAlan Stern * 926eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 927eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 928eef6a7d5SAlan Stern * what locks they need, which you have no control over. 929eef6a7d5SAlan Stern * 930eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 931eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 932eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 933eef6a7d5SAlan Stern * cancel_work_sync() instead. 934eef6a7d5SAlan Stern */ 9351da177e4SLinus Torvalds void flush_scheduled_work(void) 9361da177e4SLinus Torvalds { 9371da177e4SLinus Torvalds flush_workqueue(keventd_wq); 9381da177e4SLinus Torvalds } 939ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds /** 9421fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 9431fa44ecaSJames Bottomley * @fn: the function to execute 9441fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 9451fa44ecaSJames Bottomley * be available when the work executes) 9461fa44ecaSJames Bottomley * 9471fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 9481fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 9491fa44ecaSJames Bottomley * 9501fa44ecaSJames Bottomley * Returns: 0 - function was executed 9511fa44ecaSJames Bottomley * 1 - function was scheduled for execution 9521fa44ecaSJames Bottomley */ 95365f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 9541fa44ecaSJames Bottomley { 9551fa44ecaSJames Bottomley if (!in_interrupt()) { 95665f27f38SDavid Howells fn(&ew->work); 9571fa44ecaSJames Bottomley return 0; 9581fa44ecaSJames Bottomley } 9591fa44ecaSJames Bottomley 96065f27f38SDavid Howells INIT_WORK(&ew->work, fn); 9611fa44ecaSJames Bottomley schedule_work(&ew->work); 9621fa44ecaSJames Bottomley 9631fa44ecaSJames Bottomley return 1; 9641fa44ecaSJames Bottomley } 9651fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 9661fa44ecaSJames Bottomley 9671da177e4SLinus Torvalds int keventd_up(void) 9681da177e4SLinus Torvalds { 9691da177e4SLinus Torvalds return keventd_wq != NULL; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds int current_is_keventd(void) 9731da177e4SLinus Torvalds { 9741da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq; 975d243769dSHugh Dickins int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ 9761da177e4SLinus Torvalds int ret = 0; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds BUG_ON(!keventd_wq); 9791da177e4SLinus Torvalds 98089ada679SChristoph Lameter cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); 9811da177e4SLinus Torvalds if (current == cwq->thread) 9821da177e4SLinus Torvalds ret = 1; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds return ret; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9883af24433SOleg Nesterov static struct cpu_workqueue_struct * 9893af24433SOleg Nesterov init_cpu_workqueue(struct workqueue_struct *wq, int cpu) 9901da177e4SLinus Torvalds { 99189ada679SChristoph Lameter struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); 9923af24433SOleg Nesterov 9933af24433SOleg Nesterov cwq->wq = wq; 9943af24433SOleg Nesterov spin_lock_init(&cwq->lock); 9953af24433SOleg Nesterov INIT_LIST_HEAD(&cwq->worklist); 9963af24433SOleg Nesterov init_waitqueue_head(&cwq->more_work); 9973af24433SOleg Nesterov 9983af24433SOleg Nesterov return cwq; 9993af24433SOleg Nesterov } 10003af24433SOleg Nesterov 10013af24433SOleg Nesterov static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 10023af24433SOleg Nesterov { 10033af24433SOleg Nesterov struct workqueue_struct *wq = cwq->wq; 10046cc88bc4SDavid Howells const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d"; 10053af24433SOleg Nesterov struct task_struct *p; 10063af24433SOleg Nesterov 10073af24433SOleg Nesterov p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); 10083af24433SOleg Nesterov /* 10093af24433SOleg Nesterov * Nobody can add the work_struct to this cwq, 10103af24433SOleg Nesterov * if (caller is __create_workqueue) 10113af24433SOleg Nesterov * nobody should see this wq 10123af24433SOleg Nesterov * else // caller is CPU_UP_PREPARE 10133af24433SOleg Nesterov * cpu is not on cpu_online_map 10143af24433SOleg Nesterov * so we can abort safely. 10153af24433SOleg Nesterov */ 10163af24433SOleg Nesterov if (IS_ERR(p)) 10173af24433SOleg Nesterov return PTR_ERR(p); 10183af24433SOleg Nesterov cwq->thread = p; 10193af24433SOleg Nesterov 1020e1d8aa9fSFrederic Weisbecker trace_workqueue_creation(cwq->thread, cpu); 1021e1d8aa9fSFrederic Weisbecker 10223af24433SOleg Nesterov return 0; 10233af24433SOleg Nesterov } 10243af24433SOleg Nesterov 102506ba38a9SOleg Nesterov static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 102606ba38a9SOleg Nesterov { 102706ba38a9SOleg Nesterov struct task_struct *p = cwq->thread; 102806ba38a9SOleg Nesterov 102906ba38a9SOleg Nesterov if (p != NULL) { 103006ba38a9SOleg Nesterov if (cpu >= 0) 103106ba38a9SOleg Nesterov kthread_bind(p, cpu); 103206ba38a9SOleg Nesterov wake_up_process(p); 103306ba38a9SOleg Nesterov } 103406ba38a9SOleg Nesterov } 103506ba38a9SOleg Nesterov 10364e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name, 103797e37d7bSTejun Heo unsigned int flags, 1038eb13ba87SJohannes Berg struct lock_class_key *key, 1039eb13ba87SJohannes Berg const char *lock_name) 10403af24433SOleg Nesterov { 10413af24433SOleg Nesterov struct workqueue_struct *wq; 10423af24433SOleg Nesterov struct cpu_workqueue_struct *cwq; 10433af24433SOleg Nesterov int err = 0, cpu; 10443af24433SOleg Nesterov 10453af24433SOleg Nesterov wq = kzalloc(sizeof(*wq), GFP_KERNEL); 10463af24433SOleg Nesterov if (!wq) 10474690c4abSTejun Heo goto err; 10483af24433SOleg Nesterov 10493af24433SOleg Nesterov wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); 10504690c4abSTejun Heo if (!wq->cpu_wq) 10514690c4abSTejun Heo goto err; 10523af24433SOleg Nesterov 105397e37d7bSTejun Heo wq->flags = flags; 10543af24433SOleg Nesterov wq->name = name; 1055eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 1056cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 10573af24433SOleg Nesterov 105897e37d7bSTejun Heo if (flags & WQ_SINGLE_THREAD) { 10593af24433SOleg Nesterov cwq = init_cpu_workqueue(wq, singlethread_cpu); 10603af24433SOleg Nesterov err = create_workqueue_thread(cwq, singlethread_cpu); 106106ba38a9SOleg Nesterov start_workqueue_thread(cwq, -1); 10623af24433SOleg Nesterov } else { 10633da1c84cSOleg Nesterov cpu_maps_update_begin(); 10646af8bf3dSOleg Nesterov /* 10656af8bf3dSOleg Nesterov * We must place this wq on list even if the code below fails. 10666af8bf3dSOleg Nesterov * cpu_down(cpu) can remove cpu from cpu_populated_map before 10676af8bf3dSOleg Nesterov * destroy_workqueue() takes the lock, in that case we leak 10686af8bf3dSOleg Nesterov * cwq[cpu]->thread. 10696af8bf3dSOleg Nesterov */ 107095402b38SGautham R Shenoy spin_lock(&workqueue_lock); 10713af24433SOleg Nesterov list_add(&wq->list, &workqueues); 107295402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 10736af8bf3dSOleg Nesterov /* 10746af8bf3dSOleg Nesterov * We must initialize cwqs for each possible cpu even if we 10756af8bf3dSOleg Nesterov * are going to call destroy_workqueue() finally. Otherwise 10766af8bf3dSOleg Nesterov * cpu_up() can hit the uninitialized cwq once we drop the 10776af8bf3dSOleg Nesterov * lock. 10786af8bf3dSOleg Nesterov */ 10793af24433SOleg Nesterov for_each_possible_cpu(cpu) { 10803af24433SOleg Nesterov cwq = init_cpu_workqueue(wq, cpu); 10813af24433SOleg Nesterov if (err || !cpu_online(cpu)) 10823af24433SOleg Nesterov continue; 10833af24433SOleg Nesterov err = create_workqueue_thread(cwq, cpu); 108406ba38a9SOleg Nesterov start_workqueue_thread(cwq, cpu); 10853af24433SOleg Nesterov } 10863da1c84cSOleg Nesterov cpu_maps_update_done(); 10873af24433SOleg Nesterov } 10883af24433SOleg Nesterov 10893af24433SOleg Nesterov if (err) { 10903af24433SOleg Nesterov destroy_workqueue(wq); 10913af24433SOleg Nesterov wq = NULL; 10923af24433SOleg Nesterov } 10933af24433SOleg Nesterov return wq; 10944690c4abSTejun Heo err: 10954690c4abSTejun Heo if (wq) { 10964690c4abSTejun Heo free_percpu(wq->cpu_wq); 10974690c4abSTejun Heo kfree(wq); 10984690c4abSTejun Heo } 10994690c4abSTejun Heo return NULL; 11003af24433SOleg Nesterov } 11014e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key); 11023af24433SOleg Nesterov 11031e35eaa2SOleg Nesterov static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq) 11043af24433SOleg Nesterov { 11053af24433SOleg Nesterov /* 11063da1c84cSOleg Nesterov * Our caller is either destroy_workqueue() or CPU_POST_DEAD, 11073da1c84cSOleg Nesterov * cpu_add_remove_lock protects cwq->thread. 11083af24433SOleg Nesterov */ 110914441960SOleg Nesterov if (cwq->thread == NULL) 111014441960SOleg Nesterov return; 111114441960SOleg Nesterov 11123295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 11133295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 11144e6045f1SJohannes Berg 111513c22168SOleg Nesterov flush_cpu_workqueue(cwq); 111614441960SOleg Nesterov /* 11173da1c84cSOleg Nesterov * If the caller is CPU_POST_DEAD and cwq->worklist was not empty, 111813c22168SOleg Nesterov * a concurrent flush_workqueue() can insert a barrier after us. 111913c22168SOleg Nesterov * However, in that case run_workqueue() won't return and check 112013c22168SOleg Nesterov * kthread_should_stop() until it flushes all work_struct's. 112114441960SOleg Nesterov * When ->worklist becomes empty it is safe to exit because no 112214441960SOleg Nesterov * more work_structs can be queued on this cwq: flush_workqueue 112314441960SOleg Nesterov * checks list_empty(), and a "normal" queue_work() can't use 112414441960SOleg Nesterov * a dead CPU. 112514441960SOleg Nesterov */ 1126e1d8aa9fSFrederic Weisbecker trace_workqueue_destruction(cwq->thread); 112714441960SOleg Nesterov kthread_stop(cwq->thread); 112814441960SOleg Nesterov cwq->thread = NULL; 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11313af24433SOleg Nesterov /** 11323af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 11333af24433SOleg Nesterov * @wq: target workqueue 11343af24433SOleg Nesterov * 11353af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 11363af24433SOleg Nesterov */ 11373af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 11383af24433SOleg Nesterov { 1139e7577c50SRusty Russell const struct cpumask *cpu_map = wq_cpu_map(wq); 11403af24433SOleg Nesterov int cpu; 11413af24433SOleg Nesterov 11423da1c84cSOleg Nesterov cpu_maps_update_begin(); 114395402b38SGautham R Shenoy spin_lock(&workqueue_lock); 11443af24433SOleg Nesterov list_del(&wq->list); 114595402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 11463af24433SOleg Nesterov 1147aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 11481e35eaa2SOleg Nesterov cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu)); 11493da1c84cSOleg Nesterov cpu_maps_update_done(); 11503af24433SOleg Nesterov 11513af24433SOleg Nesterov free_percpu(wq->cpu_wq); 11523af24433SOleg Nesterov kfree(wq); 11533af24433SOleg Nesterov } 11543af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 11553af24433SOleg Nesterov 11569c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, 11571da177e4SLinus Torvalds unsigned long action, 11581da177e4SLinus Torvalds void *hcpu) 11591da177e4SLinus Torvalds { 11603af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 11613af24433SOleg Nesterov struct cpu_workqueue_struct *cwq; 11621da177e4SLinus Torvalds struct workqueue_struct *wq; 116380b5184cSAkinobu Mita int err = 0; 11641da177e4SLinus Torvalds 11658bb78442SRafael J. Wysocki action &= ~CPU_TASKS_FROZEN; 11668bb78442SRafael J. Wysocki 11671da177e4SLinus Torvalds switch (action) { 11683af24433SOleg Nesterov case CPU_UP_PREPARE: 1169e7577c50SRusty Russell cpumask_set_cpu(cpu, cpu_populated_map); 11703af24433SOleg Nesterov } 11718448502cSOleg Nesterov undo: 11721da177e4SLinus Torvalds list_for_each_entry(wq, &workqueues, list) { 11733af24433SOleg Nesterov cwq = per_cpu_ptr(wq->cpu_wq, cpu); 11743af24433SOleg Nesterov 11753af24433SOleg Nesterov switch (action) { 11763af24433SOleg Nesterov case CPU_UP_PREPARE: 117780b5184cSAkinobu Mita err = create_workqueue_thread(cwq, cpu); 117880b5184cSAkinobu Mita if (!err) 11791da177e4SLinus Torvalds break; 118095402b38SGautham R Shenoy printk(KERN_ERR "workqueue [%s] for %i failed\n", 118195402b38SGautham R Shenoy wq->name, cpu); 11828448502cSOleg Nesterov action = CPU_UP_CANCELED; 118380b5184cSAkinobu Mita err = -ENOMEM; 11848448502cSOleg Nesterov goto undo; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds case CPU_ONLINE: 118706ba38a9SOleg Nesterov start_workqueue_thread(cwq, cpu); 11881da177e4SLinus Torvalds break; 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds case CPU_UP_CANCELED: 119106ba38a9SOleg Nesterov start_workqueue_thread(cwq, -1); 11923da1c84cSOleg Nesterov case CPU_POST_DEAD: 11931e35eaa2SOleg Nesterov cleanup_workqueue_thread(cwq); 11941da177e4SLinus Torvalds break; 11951da177e4SLinus Torvalds } 11963af24433SOleg Nesterov } 11971da177e4SLinus Torvalds 119800dfcaf7SOleg Nesterov switch (action) { 119900dfcaf7SOleg Nesterov case CPU_UP_CANCELED: 12003da1c84cSOleg Nesterov case CPU_POST_DEAD: 1201e7577c50SRusty Russell cpumask_clear_cpu(cpu, cpu_populated_map); 120200dfcaf7SOleg Nesterov } 120300dfcaf7SOleg Nesterov 120480b5184cSAkinobu Mita return notifier_from_errno(err); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12072d3854a3SRusty Russell #ifdef CONFIG_SMP 12088ccad40dSRusty Russell 12092d3854a3SRusty Russell struct work_for_cpu { 12106b44003eSAndrew Morton struct completion completion; 12112d3854a3SRusty Russell long (*fn)(void *); 12122d3854a3SRusty Russell void *arg; 12132d3854a3SRusty Russell long ret; 12142d3854a3SRusty Russell }; 12152d3854a3SRusty Russell 12166b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 12172d3854a3SRusty Russell { 12186b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 12192d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 12206b44003eSAndrew Morton complete(&wfc->completion); 12216b44003eSAndrew Morton return 0; 12222d3854a3SRusty Russell } 12232d3854a3SRusty Russell 12242d3854a3SRusty Russell /** 12252d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 12262d3854a3SRusty Russell * @cpu: the cpu to run on 12272d3854a3SRusty Russell * @fn: the function to run 12282d3854a3SRusty Russell * @arg: the function arg 12292d3854a3SRusty Russell * 123031ad9081SRusty Russell * This will return the value @fn returns. 123131ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 12326b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 12332d3854a3SRusty Russell */ 12342d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 12352d3854a3SRusty Russell { 12366b44003eSAndrew Morton struct task_struct *sub_thread; 12376b44003eSAndrew Morton struct work_for_cpu wfc = { 12386b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 12396b44003eSAndrew Morton .fn = fn, 12406b44003eSAndrew Morton .arg = arg, 12416b44003eSAndrew Morton }; 12422d3854a3SRusty Russell 12436b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 12446b44003eSAndrew Morton if (IS_ERR(sub_thread)) 12456b44003eSAndrew Morton return PTR_ERR(sub_thread); 12466b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 12476b44003eSAndrew Morton wake_up_process(sub_thread); 12486b44003eSAndrew Morton wait_for_completion(&wfc.completion); 12492d3854a3SRusty Russell return wfc.ret; 12502d3854a3SRusty Russell } 12512d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 12522d3854a3SRusty Russell #endif /* CONFIG_SMP */ 12532d3854a3SRusty Russell 1254c12920d1SOleg Nesterov void __init init_workqueues(void) 12551da177e4SLinus Torvalds { 1256e7577c50SRusty Russell alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL); 1257e7577c50SRusty Russell 1258e7577c50SRusty Russell cpumask_copy(cpu_populated_map, cpu_online_mask); 1259e7577c50SRusty Russell singlethread_cpu = cpumask_first(cpu_possible_mask); 1260e7577c50SRusty Russell cpu_singlethread_map = cpumask_of(singlethread_cpu); 12611da177e4SLinus Torvalds hotcpu_notifier(workqueue_cpu_callback, 0); 12621da177e4SLinus Torvalds keventd_wq = create_workqueue("events"); 12631da177e4SLinus Torvalds BUG_ON(!keventd_wq); 12641da177e4SLinus Torvalds } 1265