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 /* 38f756d5e2SNathan Lynch * The per-CPU workqueue (if single thread, we always use the first 39f756d5e2SNathan Lynch * possible cpu). 401da177e4SLinus Torvalds */ 411da177e4SLinus Torvalds struct cpu_workqueue_struct { 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds spinlock_t lock; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds struct list_head worklist; 461da177e4SLinus Torvalds wait_queue_head_t more_work; 473af24433SOleg Nesterov struct work_struct *current_work; 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds struct workqueue_struct *wq; 5036c8b586SIngo Molnar struct task_struct *thread; 511da177e4SLinus Torvalds } ____cacheline_aligned; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 551da177e4SLinus Torvalds * per-CPU workqueues: 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds struct workqueue_struct { 5889ada679SChristoph Lameter struct cpu_workqueue_struct *cpu_wq; 59cce1a165SOleg Nesterov struct list_head list; 601da177e4SLinus Torvalds const char *name; 61cce1a165SOleg Nesterov int singlethread; 62319c2a98SOleg Nesterov int freezeable; /* Freeze threads during suspend */ 630d557dc9SHeiko Carstens int rt; 644e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 654e6045f1SJohannes Berg struct lockdep_map lockdep_map; 664e6045f1SJohannes Berg #endif 671da177e4SLinus Torvalds }; 681da177e4SLinus Torvalds 6995402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 7095402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 711da177e4SLinus Torvalds static LIST_HEAD(workqueues); 721da177e4SLinus Torvalds 733af24433SOleg Nesterov static int singlethread_cpu __read_mostly; 74e7577c50SRusty Russell static const struct cpumask *cpu_singlethread_map __read_mostly; 7514441960SOleg Nesterov /* 7614441960SOleg Nesterov * _cpu_down() first removes CPU from cpu_online_map, then CPU_DEAD 7714441960SOleg Nesterov * flushes cwq->worklist. This means that flush_workqueue/wait_on_work 7814441960SOleg Nesterov * which comes in between can't use for_each_online_cpu(). We could 7914441960SOleg Nesterov * use cpu_possible_map, the cpumask below is more a documentation 8014441960SOleg Nesterov * than optimization. 8114441960SOleg Nesterov */ 82e7577c50SRusty Russell static cpumask_var_t cpu_populated_map __read_mostly; 83f756d5e2SNathan Lynch 841da177e4SLinus Torvalds /* If it's single threaded, it isn't in the list of workqueues. */ 856cc88bc4SDavid Howells static inline int is_wq_single_threaded(struct workqueue_struct *wq) 861da177e4SLinus Torvalds { 87cce1a165SOleg Nesterov return wq->singlethread; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 90e7577c50SRusty Russell static const struct cpumask *wq_cpu_map(struct workqueue_struct *wq) 91b1f4ec17SOleg Nesterov { 926cc88bc4SDavid Howells return is_wq_single_threaded(wq) 93e7577c50SRusty Russell ? cpu_singlethread_map : cpu_populated_map; 94b1f4ec17SOleg Nesterov } 95b1f4ec17SOleg Nesterov 96a848e3b6SOleg Nesterov static 97a848e3b6SOleg Nesterov struct cpu_workqueue_struct *wq_per_cpu(struct workqueue_struct *wq, int cpu) 98a848e3b6SOleg Nesterov { 996cc88bc4SDavid Howells if (unlikely(is_wq_single_threaded(wq))) 100a848e3b6SOleg Nesterov cpu = singlethread_cpu; 101a848e3b6SOleg Nesterov return per_cpu_ptr(wq->cpu_wq, cpu); 102a848e3b6SOleg Nesterov } 103a848e3b6SOleg Nesterov 1044594bf15SDavid Howells /* 1054594bf15SDavid Howells * Set the workqueue on which a work item is to be run 1064594bf15SDavid Howells * - Must *only* be called if the pending flag is set 1074594bf15SDavid Howells */ 108ed7c0feeSOleg Nesterov static inline void set_wq_data(struct work_struct *work, 109ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *cwq) 110365970a1SDavid Howells { 1114594bf15SDavid Howells unsigned long new; 112365970a1SDavid Howells 1134594bf15SDavid Howells BUG_ON(!work_pending(work)); 1144594bf15SDavid Howells 115ed7c0feeSOleg Nesterov new = (unsigned long) cwq | (1UL << WORK_STRUCT_PENDING); 116a08727baSLinus Torvalds new |= WORK_STRUCT_FLAG_MASK & *work_data_bits(work); 117a08727baSLinus Torvalds atomic_long_set(&work->data, new); 118365970a1SDavid Howells } 119365970a1SDavid Howells 120ed7c0feeSOleg Nesterov static inline 121ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *get_wq_data(struct work_struct *work) 122365970a1SDavid Howells { 123a08727baSLinus Torvalds return (void *) (atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK); 124365970a1SDavid Howells } 125365970a1SDavid Howells 126b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 1271a4d9b0aSOleg Nesterov struct work_struct *work, struct list_head *head) 128b89deed3SOleg Nesterov { 129b89deed3SOleg Nesterov set_wq_data(work, cwq); 1306e84d644SOleg Nesterov /* 1316e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 1326e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 1336e84d644SOleg Nesterov */ 1346e84d644SOleg Nesterov smp_wmb(); 1351a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 136b89deed3SOleg Nesterov wake_up(&cwq->more_work); 137b89deed3SOleg Nesterov } 138b89deed3SOleg Nesterov 1391da177e4SLinus Torvalds static void __queue_work(struct cpu_workqueue_struct *cwq, 1401da177e4SLinus Torvalds struct work_struct *work) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds unsigned long flags; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds spin_lock_irqsave(&cwq->lock, flags); 1451a4d9b0aSOleg Nesterov insert_work(cwq, work, &cwq->worklist); 1461da177e4SLinus Torvalds spin_unlock_irqrestore(&cwq->lock, flags); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1490fcb78c2SRolf Eike Beer /** 1500fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 1510fcb78c2SRolf Eike Beer * @wq: workqueue to use 1520fcb78c2SRolf Eike Beer * @work: work to queue 1530fcb78c2SRolf Eike Beer * 154057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 1551da177e4SLinus Torvalds * 15600dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 15700dfcaf7SOleg Nesterov * it can be processed by another CPU. 1581da177e4SLinus Torvalds */ 1597ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 1601da177e4SLinus Torvalds { 161ef1ca236SOleg Nesterov int ret; 1621da177e4SLinus Torvalds 163ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 164a848e3b6SOleg Nesterov put_cpu(); 165ef1ca236SOleg Nesterov 1661da177e4SLinus Torvalds return ret; 1671da177e4SLinus Torvalds } 168ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 1691da177e4SLinus Torvalds 170c1a220e7SZhang Rui /** 171c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 172c1a220e7SZhang Rui * @cpu: CPU number to execute work on 173c1a220e7SZhang Rui * @wq: workqueue to use 174c1a220e7SZhang Rui * @work: work to queue 175c1a220e7SZhang Rui * 176c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 177c1a220e7SZhang Rui * 178c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 179c1a220e7SZhang Rui * can't go away. 180c1a220e7SZhang Rui */ 181c1a220e7SZhang Rui int 182c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 183c1a220e7SZhang Rui { 184c1a220e7SZhang Rui int ret = 0; 185c1a220e7SZhang Rui 186c1a220e7SZhang Rui if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { 187c1a220e7SZhang Rui BUG_ON(!list_empty(&work->entry)); 188c1a220e7SZhang Rui __queue_work(wq_per_cpu(wq, cpu), work); 189c1a220e7SZhang Rui ret = 1; 190c1a220e7SZhang Rui } 191c1a220e7SZhang Rui return ret; 192c1a220e7SZhang Rui } 193c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 194c1a220e7SZhang Rui 1956d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 1961da177e4SLinus Torvalds { 19752bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 198ed7c0feeSOleg Nesterov struct cpu_workqueue_struct *cwq = get_wq_data(&dwork->work); 199ed7c0feeSOleg Nesterov struct workqueue_struct *wq = cwq->wq; 2001da177e4SLinus Torvalds 201a848e3b6SOleg Nesterov __queue_work(wq_per_cpu(wq, smp_processor_id()), &dwork->work); 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2040fcb78c2SRolf Eike Beer /** 2050fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 2060fcb78c2SRolf Eike Beer * @wq: workqueue to use 207af9997e4SRandy Dunlap * @dwork: delayable work to queue 2080fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 2090fcb78c2SRolf Eike Beer * 210057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 2110fcb78c2SRolf Eike Beer */ 2127ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 21352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 2141da177e4SLinus Torvalds { 21552bad64dSDavid Howells if (delay == 0) 21663bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 2171da177e4SLinus Torvalds 21863bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 2191da177e4SLinus Torvalds } 220ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 2211da177e4SLinus Torvalds 2220fcb78c2SRolf Eike Beer /** 2230fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 2240fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 2250fcb78c2SRolf Eike Beer * @wq: workqueue to use 226af9997e4SRandy Dunlap * @dwork: work to queue 2270fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 2280fcb78c2SRolf Eike Beer * 229057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 2300fcb78c2SRolf Eike Beer */ 2317a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 23252bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 2337a6bc1cdSVenkatesh Pallipadi { 2347a6bc1cdSVenkatesh Pallipadi int ret = 0; 23552bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 23652bad64dSDavid Howells struct work_struct *work = &dwork->work; 2377a6bc1cdSVenkatesh Pallipadi 238a08727baSLinus Torvalds if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) { 2397a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 2407a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 2417a6bc1cdSVenkatesh Pallipadi 2428a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 2438a3e77ccSAndrew Liu 244ed7c0feeSOleg Nesterov /* This stores cwq for the moment, for the timer_fn */ 245a848e3b6SOleg Nesterov set_wq_data(work, wq_per_cpu(wq, raw_smp_processor_id())); 2467a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 24752bad64dSDavid Howells timer->data = (unsigned long)dwork; 2487a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 24963bc0362SOleg Nesterov 25063bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 2517a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 25263bc0362SOleg Nesterov else 25363bc0362SOleg Nesterov add_timer(timer); 2547a6bc1cdSVenkatesh Pallipadi ret = 1; 2557a6bc1cdSVenkatesh Pallipadi } 2567a6bc1cdSVenkatesh Pallipadi return ret; 2577a6bc1cdSVenkatesh Pallipadi } 258ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 2591da177e4SLinus Torvalds 260858119e1SArjan van de Ven static void run_workqueue(struct cpu_workqueue_struct *cwq) 2611da177e4SLinus Torvalds { 262f293ea92SOleg Nesterov spin_lock_irq(&cwq->lock); 2631da177e4SLinus Torvalds while (!list_empty(&cwq->worklist)) { 2641da177e4SLinus Torvalds struct work_struct *work = list_entry(cwq->worklist.next, 2651da177e4SLinus Torvalds struct work_struct, entry); 2666bb49e59SDavid Howells work_func_t f = work->func; 2674e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2684e6045f1SJohannes Berg /* 2694e6045f1SJohannes Berg * It is permissible to free the struct work_struct 2704e6045f1SJohannes Berg * from inside the function that is called from it, 2714e6045f1SJohannes Berg * this we need to take into account for lockdep too. 2724e6045f1SJohannes Berg * To avoid bogus "held lock freed" warnings as well 2734e6045f1SJohannes Berg * as problems when looking into work->lockdep_map, 2744e6045f1SJohannes Berg * make a copy and use that here. 2754e6045f1SJohannes Berg */ 2764e6045f1SJohannes Berg struct lockdep_map lockdep_map = work->lockdep_map; 2774e6045f1SJohannes Berg #endif 2781da177e4SLinus Torvalds 279b89deed3SOleg Nesterov cwq->current_work = work; 2801da177e4SLinus Torvalds list_del_init(cwq->worklist.next); 281f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 2821da177e4SLinus Torvalds 283365970a1SDavid Howells BUG_ON(get_wq_data(work) != cwq); 28423b2e599SOleg Nesterov work_clear_pending(work); 2853295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 2863295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 28765f27f38SDavid Howells f(work); 2883295f0efSIngo Molnar lock_map_release(&lockdep_map); 2893295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 2901da177e4SLinus Torvalds 291d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 292d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 293d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 294d5abe669SPeter Zijlstra current->comm, preempt_count(), 295ba25f9dcSPavel Emelyanov task_pid_nr(current)); 296d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 297d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 298d5abe669SPeter Zijlstra debug_show_held_locks(current); 299d5abe669SPeter Zijlstra dump_stack(); 300d5abe669SPeter Zijlstra } 301d5abe669SPeter Zijlstra 302f293ea92SOleg Nesterov spin_lock_irq(&cwq->lock); 303b89deed3SOleg Nesterov cwq->current_work = NULL; 3041da177e4SLinus Torvalds } 305f293ea92SOleg Nesterov spin_unlock_irq(&cwq->lock); 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds static int worker_thread(void *__cwq) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq = __cwq; 3113af24433SOleg Nesterov DEFINE_WAIT(wait); 3121da177e4SLinus Torvalds 31383144186SRafael J. Wysocki if (cwq->wq->freezeable) 31483144186SRafael J. Wysocki set_freezable(); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds set_user_nice(current, -5); 3171da177e4SLinus Torvalds 3183af24433SOleg Nesterov for (;;) { 3193af24433SOleg Nesterov prepare_to_wait(&cwq->more_work, &wait, TASK_INTERRUPTIBLE); 32014441960SOleg Nesterov if (!freezing(current) && 32114441960SOleg Nesterov !kthread_should_stop() && 32214441960SOleg Nesterov list_empty(&cwq->worklist)) 3231da177e4SLinus Torvalds schedule(); 3243af24433SOleg Nesterov finish_wait(&cwq->more_work, &wait); 3251da177e4SLinus Torvalds 32685f4186aSOleg Nesterov try_to_freeze(); 32785f4186aSOleg Nesterov 32814441960SOleg Nesterov if (kthread_should_stop()) 3293af24433SOleg Nesterov break; 3303af24433SOleg Nesterov 3311da177e4SLinus Torvalds run_workqueue(cwq); 3321da177e4SLinus Torvalds } 3333af24433SOleg Nesterov 3341da177e4SLinus Torvalds return 0; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 337fc2e4d70SOleg Nesterov struct wq_barrier { 338fc2e4d70SOleg Nesterov struct work_struct work; 339fc2e4d70SOleg Nesterov struct completion done; 340fc2e4d70SOleg Nesterov }; 341fc2e4d70SOleg Nesterov 342fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 343fc2e4d70SOleg Nesterov { 344fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 345fc2e4d70SOleg Nesterov complete(&barr->done); 346fc2e4d70SOleg Nesterov } 347fc2e4d70SOleg Nesterov 34883c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 3491a4d9b0aSOleg Nesterov struct wq_barrier *barr, struct list_head *head) 350fc2e4d70SOleg Nesterov { 351fc2e4d70SOleg Nesterov INIT_WORK(&barr->work, wq_barrier_func); 352fc2e4d70SOleg Nesterov __set_bit(WORK_STRUCT_PENDING, work_data_bits(&barr->work)); 353fc2e4d70SOleg Nesterov 354fc2e4d70SOleg Nesterov init_completion(&barr->done); 35583c22520SOleg Nesterov 3561a4d9b0aSOleg Nesterov insert_work(cwq, &barr->work, head); 357fc2e4d70SOleg Nesterov } 358fc2e4d70SOleg Nesterov 35914441960SOleg Nesterov static int flush_cpu_workqueue(struct cpu_workqueue_struct *cwq) 3601da177e4SLinus Torvalds { 361*2355b70fSLai Jiangshan int active = 0; 362fc2e4d70SOleg Nesterov struct wq_barrier barr; 3631da177e4SLinus Torvalds 364*2355b70fSLai Jiangshan WARN_ON(cwq->thread == current); 365*2355b70fSLai Jiangshan 36683c22520SOleg Nesterov spin_lock_irq(&cwq->lock); 36783c22520SOleg Nesterov if (!list_empty(&cwq->worklist) || cwq->current_work != NULL) { 3681a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, &cwq->worklist); 36983c22520SOleg Nesterov active = 1; 37083c22520SOleg Nesterov } 37183c22520SOleg Nesterov spin_unlock_irq(&cwq->lock); 3721da177e4SLinus Torvalds 373d721304dSOleg Nesterov if (active) 374fc2e4d70SOleg Nesterov wait_for_completion(&barr.done); 37514441960SOleg Nesterov 37614441960SOleg Nesterov return active; 37783c22520SOleg Nesterov } 3781da177e4SLinus Torvalds 3790fcb78c2SRolf Eike Beer /** 3801da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 3810fcb78c2SRolf Eike Beer * @wq: workqueue to flush 3821da177e4SLinus Torvalds * 3831da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 3841da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 3851da177e4SLinus Torvalds * 386fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 387fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 3881da177e4SLinus Torvalds * 3891da177e4SLinus Torvalds * This function used to run the workqueues itself. Now we just wait for the 3901da177e4SLinus Torvalds * helper threads to do it. 3911da177e4SLinus Torvalds */ 3927ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 3931da177e4SLinus Torvalds { 394e7577c50SRusty Russell const struct cpumask *cpu_map = wq_cpu_map(wq); 395cce1a165SOleg Nesterov int cpu; 396b1f4ec17SOleg Nesterov 397f293ea92SOleg Nesterov might_sleep(); 3983295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 3993295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 400aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 40189ada679SChristoph Lameter flush_cpu_workqueue(per_cpu_ptr(wq->cpu_wq, cpu)); 4021da177e4SLinus Torvalds } 403ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 4041da177e4SLinus Torvalds 405db700897SOleg Nesterov /** 406db700897SOleg Nesterov * flush_work - block until a work_struct's callback has terminated 407db700897SOleg Nesterov * @work: the work which is to be flushed 408db700897SOleg Nesterov * 409a67da70dSOleg Nesterov * Returns false if @work has already terminated. 410a67da70dSOleg Nesterov * 411db700897SOleg Nesterov * It is expected that, prior to calling flush_work(), the caller has 412db700897SOleg Nesterov * arranged for the work to not be requeued, otherwise it doesn't make 413db700897SOleg Nesterov * sense to use this function. 414db700897SOleg Nesterov */ 415db700897SOleg Nesterov int flush_work(struct work_struct *work) 416db700897SOleg Nesterov { 417db700897SOleg Nesterov struct cpu_workqueue_struct *cwq; 418db700897SOleg Nesterov struct list_head *prev; 419db700897SOleg Nesterov struct wq_barrier barr; 420db700897SOleg Nesterov 421db700897SOleg Nesterov might_sleep(); 422db700897SOleg Nesterov cwq = get_wq_data(work); 423db700897SOleg Nesterov if (!cwq) 424db700897SOleg Nesterov return 0; 425db700897SOleg Nesterov 4263295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 4273295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 428a67da70dSOleg Nesterov 429db700897SOleg Nesterov prev = NULL; 430db700897SOleg Nesterov spin_lock_irq(&cwq->lock); 431db700897SOleg Nesterov if (!list_empty(&work->entry)) { 432db700897SOleg Nesterov /* 433db700897SOleg Nesterov * See the comment near try_to_grab_pending()->smp_rmb(). 434db700897SOleg Nesterov * If it was re-queued under us we are not going to wait. 435db700897SOleg Nesterov */ 436db700897SOleg Nesterov smp_rmb(); 437db700897SOleg Nesterov if (unlikely(cwq != get_wq_data(work))) 438db700897SOleg Nesterov goto out; 439db700897SOleg Nesterov prev = &work->entry; 440db700897SOleg Nesterov } else { 441db700897SOleg Nesterov if (cwq->current_work != work) 442db700897SOleg Nesterov goto out; 443db700897SOleg Nesterov prev = &cwq->worklist; 444db700897SOleg Nesterov } 445db700897SOleg Nesterov insert_wq_barrier(cwq, &barr, prev->next); 446db700897SOleg Nesterov out: 447db700897SOleg Nesterov spin_unlock_irq(&cwq->lock); 448db700897SOleg Nesterov if (!prev) 449db700897SOleg Nesterov return 0; 450db700897SOleg Nesterov 451db700897SOleg Nesterov wait_for_completion(&barr.done); 452db700897SOleg Nesterov return 1; 453db700897SOleg Nesterov } 454db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 455db700897SOleg Nesterov 4566e84d644SOleg Nesterov /* 4571f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 4586e84d644SOleg Nesterov * so this work can't be re-armed in any way. 4596e84d644SOleg Nesterov */ 4606e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 4616e84d644SOleg Nesterov { 4626e84d644SOleg Nesterov struct cpu_workqueue_struct *cwq; 4631f1f642eSOleg Nesterov int ret = -1; 4646e84d644SOleg Nesterov 4656e84d644SOleg Nesterov if (!test_and_set_bit(WORK_STRUCT_PENDING, work_data_bits(work))) 4661f1f642eSOleg Nesterov return 0; 4676e84d644SOleg Nesterov 4686e84d644SOleg Nesterov /* 4696e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 4706e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 4716e84d644SOleg Nesterov */ 4726e84d644SOleg Nesterov 4736e84d644SOleg Nesterov cwq = get_wq_data(work); 4746e84d644SOleg Nesterov if (!cwq) 4756e84d644SOleg Nesterov return ret; 4766e84d644SOleg Nesterov 4776e84d644SOleg Nesterov spin_lock_irq(&cwq->lock); 4786e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 4796e84d644SOleg Nesterov /* 4806e84d644SOleg Nesterov * This work is queued, but perhaps we locked the wrong cwq. 4816e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 4826e84d644SOleg Nesterov * insert_work()->wmb(). 4836e84d644SOleg Nesterov */ 4846e84d644SOleg Nesterov smp_rmb(); 4856e84d644SOleg Nesterov if (cwq == get_wq_data(work)) { 4866e84d644SOleg Nesterov list_del_init(&work->entry); 4876e84d644SOleg Nesterov ret = 1; 4886e84d644SOleg Nesterov } 4896e84d644SOleg Nesterov } 4906e84d644SOleg Nesterov spin_unlock_irq(&cwq->lock); 4916e84d644SOleg Nesterov 4926e84d644SOleg Nesterov return ret; 4936e84d644SOleg Nesterov } 4946e84d644SOleg Nesterov 4956e84d644SOleg Nesterov static void wait_on_cpu_work(struct cpu_workqueue_struct *cwq, 496b89deed3SOleg Nesterov struct work_struct *work) 497b89deed3SOleg Nesterov { 498b89deed3SOleg Nesterov struct wq_barrier barr; 499b89deed3SOleg Nesterov int running = 0; 500b89deed3SOleg Nesterov 501b89deed3SOleg Nesterov spin_lock_irq(&cwq->lock); 502b89deed3SOleg Nesterov if (unlikely(cwq->current_work == work)) { 5031a4d9b0aSOleg Nesterov insert_wq_barrier(cwq, &barr, cwq->worklist.next); 504b89deed3SOleg Nesterov running = 1; 505b89deed3SOleg Nesterov } 506b89deed3SOleg Nesterov spin_unlock_irq(&cwq->lock); 507b89deed3SOleg Nesterov 5083af24433SOleg Nesterov if (unlikely(running)) 509b89deed3SOleg Nesterov wait_for_completion(&barr.done); 510b89deed3SOleg Nesterov } 511b89deed3SOleg Nesterov 5126e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work) 513b89deed3SOleg Nesterov { 514b89deed3SOleg Nesterov struct cpu_workqueue_struct *cwq; 51528e53bddSOleg Nesterov struct workqueue_struct *wq; 516e7577c50SRusty Russell const struct cpumask *cpu_map; 517b1f4ec17SOleg Nesterov int cpu; 518b89deed3SOleg Nesterov 519f293ea92SOleg Nesterov might_sleep(); 520f293ea92SOleg Nesterov 5213295f0efSIngo Molnar lock_map_acquire(&work->lockdep_map); 5223295f0efSIngo Molnar lock_map_release(&work->lockdep_map); 5234e6045f1SJohannes Berg 524b89deed3SOleg Nesterov cwq = get_wq_data(work); 525b89deed3SOleg Nesterov if (!cwq) 5263af24433SOleg Nesterov return; 527b89deed3SOleg Nesterov 52828e53bddSOleg Nesterov wq = cwq->wq; 52928e53bddSOleg Nesterov cpu_map = wq_cpu_map(wq); 53028e53bddSOleg Nesterov 531aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 5326e84d644SOleg Nesterov wait_on_cpu_work(per_cpu_ptr(wq->cpu_wq, cpu), work); 5336e84d644SOleg Nesterov } 5346e84d644SOleg Nesterov 5351f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work, 5361f1f642eSOleg Nesterov struct timer_list* timer) 5371f1f642eSOleg Nesterov { 5381f1f642eSOleg Nesterov int ret; 5391f1f642eSOleg Nesterov 5401f1f642eSOleg Nesterov do { 5411f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 5421f1f642eSOleg Nesterov if (!ret) 5431f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 5441f1f642eSOleg Nesterov wait_on_work(work); 5451f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 5461f1f642eSOleg Nesterov 5471f1f642eSOleg Nesterov work_clear_pending(work); 5481f1f642eSOleg Nesterov return ret; 5491f1f642eSOleg Nesterov } 5501f1f642eSOleg Nesterov 5516e84d644SOleg Nesterov /** 5526e84d644SOleg Nesterov * cancel_work_sync - block until a work_struct's callback has terminated 5536e84d644SOleg Nesterov * @work: the work which is to be flushed 5546e84d644SOleg Nesterov * 5551f1f642eSOleg Nesterov * Returns true if @work was pending. 5561f1f642eSOleg Nesterov * 5576e84d644SOleg Nesterov * cancel_work_sync() will cancel the work if it is queued. If the work's 5586e84d644SOleg Nesterov * callback appears to be running, cancel_work_sync() will block until it 5596e84d644SOleg Nesterov * has completed. 5606e84d644SOleg Nesterov * 5616e84d644SOleg Nesterov * It is possible to use this function if the work re-queues itself. It can 5626e84d644SOleg Nesterov * cancel the work even if it migrates to another workqueue, however in that 5636e84d644SOleg Nesterov * case it only guarantees that work->func() has completed on the last queued 5646e84d644SOleg Nesterov * workqueue. 5656e84d644SOleg Nesterov * 5666e84d644SOleg Nesterov * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not 5676e84d644SOleg Nesterov * pending, otherwise it goes into a busy-wait loop until the timer expires. 5686e84d644SOleg Nesterov * 5696e84d644SOleg Nesterov * The caller must ensure that workqueue_struct on which this work was last 5706e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 5716e84d644SOleg Nesterov */ 5721f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work) 5736e84d644SOleg Nesterov { 5741f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 575b89deed3SOleg Nesterov } 57628e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 577b89deed3SOleg Nesterov 5786e84d644SOleg Nesterov /** 579f5a421a4SOleg Nesterov * cancel_delayed_work_sync - reliably kill off a delayed work. 5806e84d644SOleg Nesterov * @dwork: the delayed work struct 5816e84d644SOleg Nesterov * 5821f1f642eSOleg Nesterov * Returns true if @dwork was pending. 5831f1f642eSOleg Nesterov * 5846e84d644SOleg Nesterov * It is possible to use this function if @dwork rearms itself via queue_work() 5856e84d644SOleg Nesterov * or queue_delayed_work(). See also the comment for cancel_work_sync(). 5866e84d644SOleg Nesterov */ 5871f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork) 5886e84d644SOleg Nesterov { 5891f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 5906e84d644SOleg Nesterov } 591f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 5921da177e4SLinus Torvalds 5936e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly; 5941da177e4SLinus Torvalds 5950fcb78c2SRolf Eike Beer /** 5960fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 5970fcb78c2SRolf Eike Beer * @work: job to be done 5980fcb78c2SRolf Eike Beer * 5990fcb78c2SRolf Eike Beer * This puts a job in the kernel-global workqueue. 6000fcb78c2SRolf Eike Beer */ 6017ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 6021da177e4SLinus Torvalds { 6031da177e4SLinus Torvalds return queue_work(keventd_wq, work); 6041da177e4SLinus Torvalds } 605ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 6061da177e4SLinus Torvalds 607c1a220e7SZhang Rui /* 608c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 609c1a220e7SZhang Rui * @cpu: cpu to put the work task on 610c1a220e7SZhang Rui * @work: job to be done 611c1a220e7SZhang Rui * 612c1a220e7SZhang Rui * This puts a job on a specific cpu 613c1a220e7SZhang Rui */ 614c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 615c1a220e7SZhang Rui { 616c1a220e7SZhang Rui return queue_work_on(cpu, keventd_wq, work); 617c1a220e7SZhang Rui } 618c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 619c1a220e7SZhang Rui 6200fcb78c2SRolf Eike Beer /** 6210fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 62252bad64dSDavid Howells * @dwork: job to be done 62352bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 6240fcb78c2SRolf Eike Beer * 6250fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 6260fcb78c2SRolf Eike Beer * workqueue. 6270fcb78c2SRolf Eike Beer */ 6287ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 62982f67cd9SIngo Molnar unsigned long delay) 6301da177e4SLinus Torvalds { 63152bad64dSDavid Howells return queue_delayed_work(keventd_wq, dwork, delay); 6321da177e4SLinus Torvalds } 633ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 6341da177e4SLinus Torvalds 6350fcb78c2SRolf Eike Beer /** 6360fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 6370fcb78c2SRolf Eike Beer * @cpu: cpu to use 63852bad64dSDavid Howells * @dwork: job to be done 6390fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 6400fcb78c2SRolf Eike Beer * 6410fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 6420fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 6430fcb78c2SRolf Eike Beer */ 6441da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 64552bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 6461da177e4SLinus Torvalds { 64752bad64dSDavid Howells return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); 6481da177e4SLinus Torvalds } 649ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 6501da177e4SLinus Torvalds 651b6136773SAndrew Morton /** 652b6136773SAndrew Morton * schedule_on_each_cpu - call a function on each online CPU from keventd 653b6136773SAndrew Morton * @func: the function to call 654b6136773SAndrew Morton * 655b6136773SAndrew Morton * Returns zero on success. 656b6136773SAndrew Morton * Returns -ve errno on failure. 657b6136773SAndrew Morton * 658b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 659b6136773SAndrew Morton */ 66065f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 66115316ba8SChristoph Lameter { 66215316ba8SChristoph Lameter int cpu; 663b6136773SAndrew Morton struct work_struct *works; 66415316ba8SChristoph Lameter 665b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 666b6136773SAndrew Morton if (!works) 66715316ba8SChristoph Lameter return -ENOMEM; 668b6136773SAndrew Morton 66995402b38SGautham R Shenoy get_online_cpus(); 67015316ba8SChristoph Lameter for_each_online_cpu(cpu) { 6719bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 6729bfb1839SIngo Molnar 6739bfb1839SIngo Molnar INIT_WORK(work, func); 6748de6d308SOleg Nesterov schedule_work_on(cpu, work); 67515316ba8SChristoph Lameter } 6768616a89aSOleg Nesterov for_each_online_cpu(cpu) 6778616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 67895402b38SGautham R Shenoy put_online_cpus(); 679b6136773SAndrew Morton free_percpu(works); 68015316ba8SChristoph Lameter return 0; 68115316ba8SChristoph Lameter } 68215316ba8SChristoph Lameter 6831da177e4SLinus Torvalds void flush_scheduled_work(void) 6841da177e4SLinus Torvalds { 6851da177e4SLinus Torvalds flush_workqueue(keventd_wq); 6861da177e4SLinus Torvalds } 687ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds /** 6901fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 6911fa44ecaSJames Bottomley * @fn: the function to execute 6921fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 6931fa44ecaSJames Bottomley * be available when the work executes) 6941fa44ecaSJames Bottomley * 6951fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 6961fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 6971fa44ecaSJames Bottomley * 6981fa44ecaSJames Bottomley * Returns: 0 - function was executed 6991fa44ecaSJames Bottomley * 1 - function was scheduled for execution 7001fa44ecaSJames Bottomley */ 70165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 7021fa44ecaSJames Bottomley { 7031fa44ecaSJames Bottomley if (!in_interrupt()) { 70465f27f38SDavid Howells fn(&ew->work); 7051fa44ecaSJames Bottomley return 0; 7061fa44ecaSJames Bottomley } 7071fa44ecaSJames Bottomley 70865f27f38SDavid Howells INIT_WORK(&ew->work, fn); 7091fa44ecaSJames Bottomley schedule_work(&ew->work); 7101fa44ecaSJames Bottomley 7111fa44ecaSJames Bottomley return 1; 7121fa44ecaSJames Bottomley } 7131fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 7141fa44ecaSJames Bottomley 7151da177e4SLinus Torvalds int keventd_up(void) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds return keventd_wq != NULL; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds int current_is_keventd(void) 7211da177e4SLinus Torvalds { 7221da177e4SLinus Torvalds struct cpu_workqueue_struct *cwq; 723d243769dSHugh Dickins int cpu = raw_smp_processor_id(); /* preempt-safe: keventd is per-cpu */ 7241da177e4SLinus Torvalds int ret = 0; 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds BUG_ON(!keventd_wq); 7271da177e4SLinus Torvalds 72889ada679SChristoph Lameter cwq = per_cpu_ptr(keventd_wq->cpu_wq, cpu); 7291da177e4SLinus Torvalds if (current == cwq->thread) 7301da177e4SLinus Torvalds ret = 1; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds return ret; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7363af24433SOleg Nesterov static struct cpu_workqueue_struct * 7373af24433SOleg Nesterov init_cpu_workqueue(struct workqueue_struct *wq, int cpu) 7381da177e4SLinus Torvalds { 73989ada679SChristoph Lameter struct cpu_workqueue_struct *cwq = per_cpu_ptr(wq->cpu_wq, cpu); 7403af24433SOleg Nesterov 7413af24433SOleg Nesterov cwq->wq = wq; 7423af24433SOleg Nesterov spin_lock_init(&cwq->lock); 7433af24433SOleg Nesterov INIT_LIST_HEAD(&cwq->worklist); 7443af24433SOleg Nesterov init_waitqueue_head(&cwq->more_work); 7453af24433SOleg Nesterov 7463af24433SOleg Nesterov return cwq; 7473af24433SOleg Nesterov } 7483af24433SOleg Nesterov 7493af24433SOleg Nesterov static int create_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 7503af24433SOleg Nesterov { 7510d557dc9SHeiko Carstens struct sched_param param = { .sched_priority = MAX_RT_PRIO-1 }; 7523af24433SOleg Nesterov struct workqueue_struct *wq = cwq->wq; 7536cc88bc4SDavid Howells const char *fmt = is_wq_single_threaded(wq) ? "%s" : "%s/%d"; 7543af24433SOleg Nesterov struct task_struct *p; 7553af24433SOleg Nesterov 7563af24433SOleg Nesterov p = kthread_create(worker_thread, cwq, fmt, wq->name, cpu); 7573af24433SOleg Nesterov /* 7583af24433SOleg Nesterov * Nobody can add the work_struct to this cwq, 7593af24433SOleg Nesterov * if (caller is __create_workqueue) 7603af24433SOleg Nesterov * nobody should see this wq 7613af24433SOleg Nesterov * else // caller is CPU_UP_PREPARE 7623af24433SOleg Nesterov * cpu is not on cpu_online_map 7633af24433SOleg Nesterov * so we can abort safely. 7643af24433SOleg Nesterov */ 7653af24433SOleg Nesterov if (IS_ERR(p)) 7663af24433SOleg Nesterov return PTR_ERR(p); 7670d557dc9SHeiko Carstens if (cwq->wq->rt) 7680d557dc9SHeiko Carstens sched_setscheduler_nocheck(p, SCHED_FIFO, ¶m); 7693af24433SOleg Nesterov cwq->thread = p; 7703af24433SOleg Nesterov 7713af24433SOleg Nesterov return 0; 7723af24433SOleg Nesterov } 7733af24433SOleg Nesterov 77406ba38a9SOleg Nesterov static void start_workqueue_thread(struct cpu_workqueue_struct *cwq, int cpu) 77506ba38a9SOleg Nesterov { 77606ba38a9SOleg Nesterov struct task_struct *p = cwq->thread; 77706ba38a9SOleg Nesterov 77806ba38a9SOleg Nesterov if (p != NULL) { 77906ba38a9SOleg Nesterov if (cpu >= 0) 78006ba38a9SOleg Nesterov kthread_bind(p, cpu); 78106ba38a9SOleg Nesterov wake_up_process(p); 78206ba38a9SOleg Nesterov } 78306ba38a9SOleg Nesterov } 78406ba38a9SOleg Nesterov 7854e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name, 7864e6045f1SJohannes Berg int singlethread, 7874e6045f1SJohannes Berg int freezeable, 7880d557dc9SHeiko Carstens int rt, 789eb13ba87SJohannes Berg struct lock_class_key *key, 790eb13ba87SJohannes Berg const char *lock_name) 7913af24433SOleg Nesterov { 7923af24433SOleg Nesterov struct workqueue_struct *wq; 7933af24433SOleg Nesterov struct cpu_workqueue_struct *cwq; 7943af24433SOleg Nesterov int err = 0, cpu; 7953af24433SOleg Nesterov 7963af24433SOleg Nesterov wq = kzalloc(sizeof(*wq), GFP_KERNEL); 7973af24433SOleg Nesterov if (!wq) 7983af24433SOleg Nesterov return NULL; 7993af24433SOleg Nesterov 8003af24433SOleg Nesterov wq->cpu_wq = alloc_percpu(struct cpu_workqueue_struct); 8013af24433SOleg Nesterov if (!wq->cpu_wq) { 8023af24433SOleg Nesterov kfree(wq); 8033af24433SOleg Nesterov return NULL; 8043af24433SOleg Nesterov } 8053af24433SOleg Nesterov 8063af24433SOleg Nesterov wq->name = name; 807eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 808cce1a165SOleg Nesterov wq->singlethread = singlethread; 8093af24433SOleg Nesterov wq->freezeable = freezeable; 8100d557dc9SHeiko Carstens wq->rt = rt; 811cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 8123af24433SOleg Nesterov 8133af24433SOleg Nesterov if (singlethread) { 8143af24433SOleg Nesterov cwq = init_cpu_workqueue(wq, singlethread_cpu); 8153af24433SOleg Nesterov err = create_workqueue_thread(cwq, singlethread_cpu); 81606ba38a9SOleg Nesterov start_workqueue_thread(cwq, -1); 8173af24433SOleg Nesterov } else { 8183da1c84cSOleg Nesterov cpu_maps_update_begin(); 8196af8bf3dSOleg Nesterov /* 8206af8bf3dSOleg Nesterov * We must place this wq on list even if the code below fails. 8216af8bf3dSOleg Nesterov * cpu_down(cpu) can remove cpu from cpu_populated_map before 8226af8bf3dSOleg Nesterov * destroy_workqueue() takes the lock, in that case we leak 8236af8bf3dSOleg Nesterov * cwq[cpu]->thread. 8246af8bf3dSOleg Nesterov */ 82595402b38SGautham R Shenoy spin_lock(&workqueue_lock); 8263af24433SOleg Nesterov list_add(&wq->list, &workqueues); 82795402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 8286af8bf3dSOleg Nesterov /* 8296af8bf3dSOleg Nesterov * We must initialize cwqs for each possible cpu even if we 8306af8bf3dSOleg Nesterov * are going to call destroy_workqueue() finally. Otherwise 8316af8bf3dSOleg Nesterov * cpu_up() can hit the uninitialized cwq once we drop the 8326af8bf3dSOleg Nesterov * lock. 8336af8bf3dSOleg Nesterov */ 8343af24433SOleg Nesterov for_each_possible_cpu(cpu) { 8353af24433SOleg Nesterov cwq = init_cpu_workqueue(wq, cpu); 8363af24433SOleg Nesterov if (err || !cpu_online(cpu)) 8373af24433SOleg Nesterov continue; 8383af24433SOleg Nesterov err = create_workqueue_thread(cwq, cpu); 83906ba38a9SOleg Nesterov start_workqueue_thread(cwq, cpu); 8403af24433SOleg Nesterov } 8413da1c84cSOleg Nesterov cpu_maps_update_done(); 8423af24433SOleg Nesterov } 8433af24433SOleg Nesterov 8443af24433SOleg Nesterov if (err) { 8453af24433SOleg Nesterov destroy_workqueue(wq); 8463af24433SOleg Nesterov wq = NULL; 8473af24433SOleg Nesterov } 8483af24433SOleg Nesterov return wq; 8493af24433SOleg Nesterov } 8504e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key); 8513af24433SOleg Nesterov 8521e35eaa2SOleg Nesterov static void cleanup_workqueue_thread(struct cpu_workqueue_struct *cwq) 8533af24433SOleg Nesterov { 8543af24433SOleg Nesterov /* 8553da1c84cSOleg Nesterov * Our caller is either destroy_workqueue() or CPU_POST_DEAD, 8563da1c84cSOleg Nesterov * cpu_add_remove_lock protects cwq->thread. 8573af24433SOleg Nesterov */ 85814441960SOleg Nesterov if (cwq->thread == NULL) 85914441960SOleg Nesterov return; 86014441960SOleg Nesterov 8613295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 8623295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 8634e6045f1SJohannes Berg 86413c22168SOleg Nesterov flush_cpu_workqueue(cwq); 86514441960SOleg Nesterov /* 8663da1c84cSOleg Nesterov * If the caller is CPU_POST_DEAD and cwq->worklist was not empty, 86713c22168SOleg Nesterov * a concurrent flush_workqueue() can insert a barrier after us. 86813c22168SOleg Nesterov * However, in that case run_workqueue() won't return and check 86913c22168SOleg Nesterov * kthread_should_stop() until it flushes all work_struct's. 87014441960SOleg Nesterov * When ->worklist becomes empty it is safe to exit because no 87114441960SOleg Nesterov * more work_structs can be queued on this cwq: flush_workqueue 87214441960SOleg Nesterov * checks list_empty(), and a "normal" queue_work() can't use 87314441960SOleg Nesterov * a dead CPU. 87414441960SOleg Nesterov */ 87514441960SOleg Nesterov kthread_stop(cwq->thread); 87614441960SOleg Nesterov cwq->thread = NULL; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8793af24433SOleg Nesterov /** 8803af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 8813af24433SOleg Nesterov * @wq: target workqueue 8823af24433SOleg Nesterov * 8833af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 8843af24433SOleg Nesterov */ 8853af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 8863af24433SOleg Nesterov { 887e7577c50SRusty Russell const struct cpumask *cpu_map = wq_cpu_map(wq); 8883af24433SOleg Nesterov int cpu; 8893af24433SOleg Nesterov 8903da1c84cSOleg Nesterov cpu_maps_update_begin(); 89195402b38SGautham R Shenoy spin_lock(&workqueue_lock); 8923af24433SOleg Nesterov list_del(&wq->list); 89395402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 8943af24433SOleg Nesterov 895aa85ea5bSRusty Russell for_each_cpu(cpu, cpu_map) 8961e35eaa2SOleg Nesterov cleanup_workqueue_thread(per_cpu_ptr(wq->cpu_wq, cpu)); 8973da1c84cSOleg Nesterov cpu_maps_update_done(); 8983af24433SOleg Nesterov 8993af24433SOleg Nesterov free_percpu(wq->cpu_wq); 9003af24433SOleg Nesterov kfree(wq); 9013af24433SOleg Nesterov } 9023af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 9033af24433SOleg Nesterov 9049c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, 9051da177e4SLinus Torvalds unsigned long action, 9061da177e4SLinus Torvalds void *hcpu) 9071da177e4SLinus Torvalds { 9083af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 9093af24433SOleg Nesterov struct cpu_workqueue_struct *cwq; 9101da177e4SLinus Torvalds struct workqueue_struct *wq; 9118448502cSOleg Nesterov int ret = NOTIFY_OK; 9121da177e4SLinus Torvalds 9138bb78442SRafael J. Wysocki action &= ~CPU_TASKS_FROZEN; 9148bb78442SRafael J. Wysocki 9151da177e4SLinus Torvalds switch (action) { 9163af24433SOleg Nesterov case CPU_UP_PREPARE: 917e7577c50SRusty Russell cpumask_set_cpu(cpu, cpu_populated_map); 9183af24433SOleg Nesterov } 9198448502cSOleg Nesterov undo: 9201da177e4SLinus Torvalds list_for_each_entry(wq, &workqueues, list) { 9213af24433SOleg Nesterov cwq = per_cpu_ptr(wq->cpu_wq, cpu); 9223af24433SOleg Nesterov 9233af24433SOleg Nesterov switch (action) { 9243af24433SOleg Nesterov case CPU_UP_PREPARE: 9253af24433SOleg Nesterov if (!create_workqueue_thread(cwq, cpu)) 9261da177e4SLinus Torvalds break; 92795402b38SGautham R Shenoy printk(KERN_ERR "workqueue [%s] for %i failed\n", 92895402b38SGautham R Shenoy wq->name, cpu); 9298448502cSOleg Nesterov action = CPU_UP_CANCELED; 9308448502cSOleg Nesterov ret = NOTIFY_BAD; 9318448502cSOleg Nesterov goto undo; 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds case CPU_ONLINE: 93406ba38a9SOleg Nesterov start_workqueue_thread(cwq, cpu); 9351da177e4SLinus Torvalds break; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds case CPU_UP_CANCELED: 93806ba38a9SOleg Nesterov start_workqueue_thread(cwq, -1); 9393da1c84cSOleg Nesterov case CPU_POST_DEAD: 9401e35eaa2SOleg Nesterov cleanup_workqueue_thread(cwq); 9411da177e4SLinus Torvalds break; 9421da177e4SLinus Torvalds } 9433af24433SOleg Nesterov } 9441da177e4SLinus Torvalds 94500dfcaf7SOleg Nesterov switch (action) { 94600dfcaf7SOleg Nesterov case CPU_UP_CANCELED: 9473da1c84cSOleg Nesterov case CPU_POST_DEAD: 948e7577c50SRusty Russell cpumask_clear_cpu(cpu, cpu_populated_map); 94900dfcaf7SOleg Nesterov } 95000dfcaf7SOleg Nesterov 9518448502cSOleg Nesterov return ret; 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 9542d3854a3SRusty Russell #ifdef CONFIG_SMP 9558ccad40dSRusty Russell static struct workqueue_struct *work_on_cpu_wq __read_mostly; 9568ccad40dSRusty Russell 9572d3854a3SRusty Russell struct work_for_cpu { 9582d3854a3SRusty Russell struct work_struct work; 9592d3854a3SRusty Russell long (*fn)(void *); 9602d3854a3SRusty Russell void *arg; 9612d3854a3SRusty Russell long ret; 9622d3854a3SRusty Russell }; 9632d3854a3SRusty Russell 9642d3854a3SRusty Russell static void do_work_for_cpu(struct work_struct *w) 9652d3854a3SRusty Russell { 9662d3854a3SRusty Russell struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work); 9672d3854a3SRusty Russell 9682d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 9692d3854a3SRusty Russell } 9702d3854a3SRusty Russell 9712d3854a3SRusty Russell /** 9722d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 9732d3854a3SRusty Russell * @cpu: the cpu to run on 9742d3854a3SRusty Russell * @fn: the function to run 9752d3854a3SRusty Russell * @arg: the function arg 9762d3854a3SRusty Russell * 97731ad9081SRusty Russell * This will return the value @fn returns. 97831ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 9792d3854a3SRusty Russell */ 9802d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 9812d3854a3SRusty Russell { 9822d3854a3SRusty Russell struct work_for_cpu wfc; 9832d3854a3SRusty Russell 9842d3854a3SRusty Russell INIT_WORK(&wfc.work, do_work_for_cpu); 9852d3854a3SRusty Russell wfc.fn = fn; 9862d3854a3SRusty Russell wfc.arg = arg; 9878ccad40dSRusty Russell queue_work_on(cpu, work_on_cpu_wq, &wfc.work); 9882d3854a3SRusty Russell flush_work(&wfc.work); 9892d3854a3SRusty Russell 9902d3854a3SRusty Russell return wfc.ret; 9912d3854a3SRusty Russell } 9922d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 9932d3854a3SRusty Russell #endif /* CONFIG_SMP */ 9942d3854a3SRusty Russell 995c12920d1SOleg Nesterov void __init init_workqueues(void) 9961da177e4SLinus Torvalds { 997e7577c50SRusty Russell alloc_cpumask_var(&cpu_populated_map, GFP_KERNEL); 998e7577c50SRusty Russell 999e7577c50SRusty Russell cpumask_copy(cpu_populated_map, cpu_online_mask); 1000e7577c50SRusty Russell singlethread_cpu = cpumask_first(cpu_possible_mask); 1001e7577c50SRusty Russell cpu_singlethread_map = cpumask_of(singlethread_cpu); 10021da177e4SLinus Torvalds hotcpu_notifier(workqueue_cpu_callback, 0); 10031da177e4SLinus Torvalds keventd_wq = create_workqueue("events"); 10041da177e4SLinus Torvalds BUG_ON(!keventd_wq); 10058ccad40dSRusty Russell #ifdef CONFIG_SMP 10068ccad40dSRusty Russell work_on_cpu_wq = create_workqueue("work_on_cpu"); 10078ccad40dSRusty Russell BUG_ON(!work_on_cpu_wq); 10088ccad40dSRusty Russell #endif 10091da177e4SLinus Torvalds } 1010