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> 36c34056a3STejun Heo #include <linux/idr.h> 377e11629dSTejun Heo #include <linux/delay.h> 381da177e4SLinus Torvalds 39c8e55f36STejun Heo enum { 40db7bccf4STejun Heo /* global_cwq flags */ 41db7bccf4STejun Heo GCWQ_FREEZING = 1 << 3, /* freeze in progress */ 42db7bccf4STejun Heo 43c8e55f36STejun Heo /* worker flags */ 44c8e55f36STejun Heo WORKER_STARTED = 1 << 0, /* started */ 45c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 46c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 47db7bccf4STejun Heo WORKER_ROGUE = 1 << 4, /* not bound to any cpu */ 48db7bccf4STejun Heo 49db7bccf4STejun Heo /* gcwq->trustee_state */ 50db7bccf4STejun Heo TRUSTEE_START = 0, /* start */ 51db7bccf4STejun Heo TRUSTEE_IN_CHARGE = 1, /* trustee in charge of gcwq */ 52db7bccf4STejun Heo TRUSTEE_BUTCHER = 2, /* butcher workers */ 53db7bccf4STejun Heo TRUSTEE_RELEASE = 3, /* release workers */ 54db7bccf4STejun Heo TRUSTEE_DONE = 4, /* trustee is done */ 55c8e55f36STejun Heo 56c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 57c8e55f36STejun Heo BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER, 58c8e55f36STejun Heo BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1, 59db7bccf4STejun Heo 60db7bccf4STejun Heo TRUSTEE_COOLDOWN = HZ / 10, /* for trustee draining */ 61c8e55f36STejun Heo }; 62c8e55f36STejun Heo 631da177e4SLinus Torvalds /* 644690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 654690c4abSTejun Heo * 664690c4abSTejun Heo * I: Set during initialization and read-only afterwards. 674690c4abSTejun Heo * 688b03ae3cSTejun Heo * L: gcwq->lock protected. Access with gcwq->lock held. 694690c4abSTejun Heo * 7073f53c4aSTejun Heo * F: wq->flush_mutex protected. 7173f53c4aSTejun Heo * 724690c4abSTejun Heo * W: workqueue_lock protected. 734690c4abSTejun Heo */ 744690c4abSTejun Heo 758b03ae3cSTejun Heo struct global_cwq; 76c34056a3STejun Heo 77c34056a3STejun Heo struct worker { 78c8e55f36STejun Heo /* on idle list while idle, on busy hash table while busy */ 79c8e55f36STejun Heo union { 80c8e55f36STejun Heo struct list_head entry; /* L: while idle */ 81c8e55f36STejun Heo struct hlist_node hentry; /* L: while busy */ 82c8e55f36STejun Heo }; 83c8e55f36STejun Heo 84c34056a3STejun Heo struct work_struct *current_work; /* L: work being processed */ 858cca0eeaSTejun Heo struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */ 86affee4b2STejun Heo struct list_head scheduled; /* L: scheduled works */ 87c34056a3STejun Heo struct task_struct *task; /* I: worker task */ 888b03ae3cSTejun Heo struct global_cwq *gcwq; /* I: the associated gcwq */ 89c8e55f36STejun Heo unsigned int flags; /* L: flags */ 90c34056a3STejun Heo int id; /* I: worker id */ 91c34056a3STejun Heo }; 92c34056a3STejun Heo 934690c4abSTejun Heo /* 948b03ae3cSTejun Heo * Global per-cpu workqueue. 958b03ae3cSTejun Heo */ 968b03ae3cSTejun Heo struct global_cwq { 978b03ae3cSTejun Heo spinlock_t lock; /* the gcwq lock */ 987e11629dSTejun Heo struct list_head worklist; /* L: list of pending works */ 998b03ae3cSTejun Heo unsigned int cpu; /* I: the associated cpu */ 100db7bccf4STejun Heo unsigned int flags; /* L: GCWQ_* flags */ 101c8e55f36STejun Heo 102c8e55f36STejun Heo int nr_workers; /* L: total number of workers */ 103c8e55f36STejun Heo int nr_idle; /* L: currently idle ones */ 104c8e55f36STejun Heo 105c8e55f36STejun Heo /* workers are chained either in the idle_list or busy_hash */ 106c8e55f36STejun Heo struct list_head idle_list; /* L: list of idle workers */ 107c8e55f36STejun Heo struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE]; 108c8e55f36STejun Heo /* L: hash of busy workers */ 109c8e55f36STejun Heo 1108b03ae3cSTejun Heo struct ida worker_ida; /* L: for worker IDs */ 111db7bccf4STejun Heo 112db7bccf4STejun Heo struct task_struct *trustee; /* L: for gcwq shutdown */ 113db7bccf4STejun Heo unsigned int trustee_state; /* L: trustee state */ 114db7bccf4STejun Heo wait_queue_head_t trustee_wait; /* trustee wait */ 1158b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1168b03ae3cSTejun Heo 1178b03ae3cSTejun Heo /* 118502ca9d8STejun Heo * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of 1190f900049STejun Heo * work_struct->data are used for flags and thus cwqs need to be 1200f900049STejun Heo * aligned at two's power of the number of flag bits. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds struct cpu_workqueue_struct { 1238b03ae3cSTejun Heo struct global_cwq *gcwq; /* I: the associated gcwq */ 124c34056a3STejun Heo struct worker *worker; 1254690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 12673f53c4aSTejun Heo int work_color; /* L: current color */ 12773f53c4aSTejun Heo int flush_color; /* L: flushing color */ 12873f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 12973f53c4aSTejun Heo /* L: nr of in_flight works */ 1301e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 131a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 1321e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 1330f900049STejun Heo }; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* 13673f53c4aSTejun Heo * Structure used to wait for workqueue flush. 13773f53c4aSTejun Heo */ 13873f53c4aSTejun Heo struct wq_flusher { 13973f53c4aSTejun Heo struct list_head list; /* F: list of flushers */ 14073f53c4aSTejun Heo int flush_color; /* F: flush color waiting for */ 14173f53c4aSTejun Heo struct completion done; /* flush completion */ 14273f53c4aSTejun Heo }; 14373f53c4aSTejun Heo 14473f53c4aSTejun Heo /* 1451da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 1461da177e4SLinus Torvalds * per-CPU workqueues: 1471da177e4SLinus Torvalds */ 1481da177e4SLinus Torvalds struct workqueue_struct { 14997e37d7bSTejun Heo unsigned int flags; /* I: WQ_* flags */ 1504690c4abSTejun Heo struct cpu_workqueue_struct *cpu_wq; /* I: cwq's */ 1514690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 15273f53c4aSTejun Heo 15373f53c4aSTejun Heo struct mutex flush_mutex; /* protects wq flushing */ 15473f53c4aSTejun Heo int work_color; /* F: current work color */ 15573f53c4aSTejun Heo int flush_color; /* F: current flush color */ 15673f53c4aSTejun Heo atomic_t nr_cwqs_to_flush; /* flush in progress */ 15773f53c4aSTejun Heo struct wq_flusher *first_flusher; /* F: first flusher */ 15873f53c4aSTejun Heo struct list_head flusher_queue; /* F: flush waiters */ 15973f53c4aSTejun Heo struct list_head flusher_overflow; /* F: flush overflow list */ 16073f53c4aSTejun Heo 161502ca9d8STejun Heo unsigned long single_cpu; /* cpu for single cpu wq */ 162502ca9d8STejun Heo 163a0a1a5fdSTejun Heo int saved_max_active; /* I: saved cwq max_active */ 1644690c4abSTejun Heo const char *name; /* I: workqueue name */ 1654e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 1664e6045f1SJohannes Berg struct lockdep_map lockdep_map; 1674e6045f1SJohannes Berg #endif 1681da177e4SLinus Torvalds }; 1691da177e4SLinus Torvalds 170db7bccf4STejun Heo #define for_each_busy_worker(worker, i, pos, gcwq) \ 171db7bccf4STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ 172db7bccf4STejun Heo hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) 173db7bccf4STejun Heo 174dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 175dc186ad7SThomas Gleixner 176dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 177dc186ad7SThomas Gleixner 178dc186ad7SThomas Gleixner /* 179dc186ad7SThomas Gleixner * fixup_init is called when: 180dc186ad7SThomas Gleixner * - an active object is initialized 181dc186ad7SThomas Gleixner */ 182dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 183dc186ad7SThomas Gleixner { 184dc186ad7SThomas Gleixner struct work_struct *work = addr; 185dc186ad7SThomas Gleixner 186dc186ad7SThomas Gleixner switch (state) { 187dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 188dc186ad7SThomas Gleixner cancel_work_sync(work); 189dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 190dc186ad7SThomas Gleixner return 1; 191dc186ad7SThomas Gleixner default: 192dc186ad7SThomas Gleixner return 0; 193dc186ad7SThomas Gleixner } 194dc186ad7SThomas Gleixner } 195dc186ad7SThomas Gleixner 196dc186ad7SThomas Gleixner /* 197dc186ad7SThomas Gleixner * fixup_activate is called when: 198dc186ad7SThomas Gleixner * - an active object is activated 199dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 200dc186ad7SThomas Gleixner */ 201dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 202dc186ad7SThomas Gleixner { 203dc186ad7SThomas Gleixner struct work_struct *work = addr; 204dc186ad7SThomas Gleixner 205dc186ad7SThomas Gleixner switch (state) { 206dc186ad7SThomas Gleixner 207dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 208dc186ad7SThomas Gleixner /* 209dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 210dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 211dc186ad7SThomas Gleixner * is tracked in the object tracker. 212dc186ad7SThomas Gleixner */ 21322df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 214dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 215dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 216dc186ad7SThomas Gleixner return 0; 217dc186ad7SThomas Gleixner } 218dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 219dc186ad7SThomas Gleixner return 0; 220dc186ad7SThomas Gleixner 221dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 222dc186ad7SThomas Gleixner WARN_ON(1); 223dc186ad7SThomas Gleixner 224dc186ad7SThomas Gleixner default: 225dc186ad7SThomas Gleixner return 0; 226dc186ad7SThomas Gleixner } 227dc186ad7SThomas Gleixner } 228dc186ad7SThomas Gleixner 229dc186ad7SThomas Gleixner /* 230dc186ad7SThomas Gleixner * fixup_free is called when: 231dc186ad7SThomas Gleixner * - an active object is freed 232dc186ad7SThomas Gleixner */ 233dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 234dc186ad7SThomas Gleixner { 235dc186ad7SThomas Gleixner struct work_struct *work = addr; 236dc186ad7SThomas Gleixner 237dc186ad7SThomas Gleixner switch (state) { 238dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 239dc186ad7SThomas Gleixner cancel_work_sync(work); 240dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 241dc186ad7SThomas Gleixner return 1; 242dc186ad7SThomas Gleixner default: 243dc186ad7SThomas Gleixner return 0; 244dc186ad7SThomas Gleixner } 245dc186ad7SThomas Gleixner } 246dc186ad7SThomas Gleixner 247dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 248dc186ad7SThomas Gleixner .name = "work_struct", 249dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 250dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 251dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 252dc186ad7SThomas Gleixner }; 253dc186ad7SThomas Gleixner 254dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 255dc186ad7SThomas Gleixner { 256dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 257dc186ad7SThomas Gleixner } 258dc186ad7SThomas Gleixner 259dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 260dc186ad7SThomas Gleixner { 261dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 262dc186ad7SThomas Gleixner } 263dc186ad7SThomas Gleixner 264dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 265dc186ad7SThomas Gleixner { 266dc186ad7SThomas Gleixner if (onstack) 267dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 268dc186ad7SThomas Gleixner else 269dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 270dc186ad7SThomas Gleixner } 271dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 272dc186ad7SThomas Gleixner 273dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 274dc186ad7SThomas Gleixner { 275dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 276dc186ad7SThomas Gleixner } 277dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 278dc186ad7SThomas Gleixner 279dc186ad7SThomas Gleixner #else 280dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 281dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 282dc186ad7SThomas Gleixner #endif 283dc186ad7SThomas Gleixner 28495402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 28595402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 2861da177e4SLinus Torvalds static LIST_HEAD(workqueues); 287a0a1a5fdSTejun Heo static bool workqueue_freezing; /* W: have wqs started freezing? */ 288c34056a3STejun Heo 2898b03ae3cSTejun Heo static DEFINE_PER_CPU(struct global_cwq, global_cwq); 2908b03ae3cSTejun Heo 291c34056a3STejun Heo static int worker_thread(void *__worker); 2921da177e4SLinus Torvalds 2938b03ae3cSTejun Heo static struct global_cwq *get_gcwq(unsigned int cpu) 2948b03ae3cSTejun Heo { 2958b03ae3cSTejun Heo return &per_cpu(global_cwq, cpu); 2968b03ae3cSTejun Heo } 2978b03ae3cSTejun Heo 2984690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, 2994690c4abSTejun Heo struct workqueue_struct *wq) 300a848e3b6SOleg Nesterov { 301a848e3b6SOleg Nesterov return per_cpu_ptr(wq->cpu_wq, cpu); 302a848e3b6SOleg Nesterov } 303a848e3b6SOleg Nesterov 30473f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 30573f53c4aSTejun Heo { 30673f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 30773f53c4aSTejun Heo } 30873f53c4aSTejun Heo 30973f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 31073f53c4aSTejun Heo { 31173f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 31273f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 31373f53c4aSTejun Heo } 31473f53c4aSTejun Heo 31573f53c4aSTejun Heo static int work_next_color(int color) 31673f53c4aSTejun Heo { 31773f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 31873f53c4aSTejun Heo } 31973f53c4aSTejun Heo 3204594bf15SDavid Howells /* 3217a22ad75STejun Heo * Work data points to the cwq while a work is on queue. Once 3227a22ad75STejun Heo * execution starts, it points to the cpu the work was last on. This 3237a22ad75STejun Heo * can be distinguished by comparing the data value against 3247a22ad75STejun Heo * PAGE_OFFSET. 3257a22ad75STejun Heo * 3267a22ad75STejun Heo * set_work_{cwq|cpu}() and clear_work_data() can be used to set the 3277a22ad75STejun Heo * cwq, cpu or clear work->data. These functions should only be 3287a22ad75STejun Heo * called while the work is owned - ie. while the PENDING bit is set. 3297a22ad75STejun Heo * 3307a22ad75STejun Heo * get_work_[g]cwq() can be used to obtain the gcwq or cwq 3317a22ad75STejun Heo * corresponding to a work. gcwq is available once the work has been 3327a22ad75STejun Heo * queued anywhere after initialization. cwq is available only from 3337a22ad75STejun Heo * queueing until execution starts. 3344594bf15SDavid Howells */ 3357a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 3367a22ad75STejun Heo unsigned long flags) 3377a22ad75STejun Heo { 3387a22ad75STejun Heo BUG_ON(!work_pending(work)); 3397a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 3407a22ad75STejun Heo } 3417a22ad75STejun Heo 3427a22ad75STejun Heo static void set_work_cwq(struct work_struct *work, 3434690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 3444690c4abSTejun Heo unsigned long extra_flags) 345365970a1SDavid Howells { 3467a22ad75STejun Heo set_work_data(work, (unsigned long)cwq, 34722df02bbSTejun Heo WORK_STRUCT_PENDING | extra_flags); 348365970a1SDavid Howells } 349365970a1SDavid Howells 3507a22ad75STejun Heo static void set_work_cpu(struct work_struct *work, unsigned int cpu) 3514d707b9fSOleg Nesterov { 3527a22ad75STejun Heo set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING); 3534d707b9fSOleg Nesterov } 3544d707b9fSOleg Nesterov 3557a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 356365970a1SDavid Howells { 3577a22ad75STejun Heo set_work_data(work, WORK_STRUCT_NO_CPU, 0); 3587a22ad75STejun Heo } 3597a22ad75STejun Heo 3607a22ad75STejun Heo static inline unsigned long get_work_data(struct work_struct *work) 3617a22ad75STejun Heo { 3627a22ad75STejun Heo return atomic_long_read(&work->data) & WORK_STRUCT_WQ_DATA_MASK; 3637a22ad75STejun Heo } 3647a22ad75STejun Heo 3657a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) 3667a22ad75STejun Heo { 3677a22ad75STejun Heo unsigned long data = get_work_data(work); 3687a22ad75STejun Heo 3697a22ad75STejun Heo return data >= PAGE_OFFSET ? (void *)data : NULL; 3707a22ad75STejun Heo } 3717a22ad75STejun Heo 3727a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work) 3737a22ad75STejun Heo { 3747a22ad75STejun Heo unsigned long data = get_work_data(work); 3757a22ad75STejun Heo unsigned int cpu; 3767a22ad75STejun Heo 3777a22ad75STejun Heo if (data >= PAGE_OFFSET) 3787a22ad75STejun Heo return ((struct cpu_workqueue_struct *)data)->gcwq; 3797a22ad75STejun Heo 3807a22ad75STejun Heo cpu = data >> WORK_STRUCT_FLAG_BITS; 3817a22ad75STejun Heo if (cpu == NR_CPUS) 3827a22ad75STejun Heo return NULL; 3837a22ad75STejun Heo 3847a22ad75STejun Heo BUG_ON(cpu >= num_possible_cpus()); 3857a22ad75STejun Heo return get_gcwq(cpu); 386365970a1SDavid Howells } 387365970a1SDavid Howells 3887e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 3897e11629dSTejun Heo static struct worker *first_worker(struct global_cwq *gcwq) 3907e11629dSTejun Heo { 3917e11629dSTejun Heo if (unlikely(list_empty(&gcwq->idle_list))) 3927e11629dSTejun Heo return NULL; 3937e11629dSTejun Heo 3947e11629dSTejun Heo return list_first_entry(&gcwq->idle_list, struct worker, entry); 3957e11629dSTejun Heo } 3967e11629dSTejun Heo 3977e11629dSTejun Heo /** 3987e11629dSTejun Heo * wake_up_worker - wake up an idle worker 3997e11629dSTejun Heo * @gcwq: gcwq to wake worker for 4007e11629dSTejun Heo * 4017e11629dSTejun Heo * Wake up the first idle worker of @gcwq. 4027e11629dSTejun Heo * 4037e11629dSTejun Heo * CONTEXT: 4047e11629dSTejun Heo * spin_lock_irq(gcwq->lock). 4057e11629dSTejun Heo */ 4067e11629dSTejun Heo static void wake_up_worker(struct global_cwq *gcwq) 4077e11629dSTejun Heo { 4087e11629dSTejun Heo struct worker *worker = first_worker(gcwq); 4097e11629dSTejun Heo 4107e11629dSTejun Heo if (likely(worker)) 4117e11629dSTejun Heo wake_up_process(worker->task); 4127e11629dSTejun Heo } 4137e11629dSTejun Heo 4144690c4abSTejun Heo /** 415*d302f017STejun Heo * worker_set_flags - set worker flags 416*d302f017STejun Heo * @worker: worker to set flags for 417*d302f017STejun Heo * @flags: flags to set 418*d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 419*d302f017STejun Heo * 420*d302f017STejun Heo * Set @flags in @worker->flags. 421*d302f017STejun Heo * 422*d302f017STejun Heo * LOCKING: 423*d302f017STejun Heo * spin_lock_irq(gcwq->lock). 424*d302f017STejun Heo */ 425*d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 426*d302f017STejun Heo bool wakeup) 427*d302f017STejun Heo { 428*d302f017STejun Heo worker->flags |= flags; 429*d302f017STejun Heo } 430*d302f017STejun Heo 431*d302f017STejun Heo /** 432*d302f017STejun Heo * worker_clr_flags - clear worker flags 433*d302f017STejun Heo * @worker: worker to set flags for 434*d302f017STejun Heo * @flags: flags to clear 435*d302f017STejun Heo * 436*d302f017STejun Heo * Clear @flags in @worker->flags. 437*d302f017STejun Heo * 438*d302f017STejun Heo * LOCKING: 439*d302f017STejun Heo * spin_lock_irq(gcwq->lock). 440*d302f017STejun Heo */ 441*d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 442*d302f017STejun Heo { 443*d302f017STejun Heo worker->flags &= ~flags; 444*d302f017STejun Heo } 445*d302f017STejun Heo 446*d302f017STejun Heo /** 447c8e55f36STejun Heo * busy_worker_head - return the busy hash head for a work 448c8e55f36STejun Heo * @gcwq: gcwq of interest 449c8e55f36STejun Heo * @work: work to be hashed 450c8e55f36STejun Heo * 451c8e55f36STejun Heo * Return hash head of @gcwq for @work. 452c8e55f36STejun Heo * 453c8e55f36STejun Heo * CONTEXT: 454c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 455c8e55f36STejun Heo * 456c8e55f36STejun Heo * RETURNS: 457c8e55f36STejun Heo * Pointer to the hash head. 458c8e55f36STejun Heo */ 459c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, 460c8e55f36STejun Heo struct work_struct *work) 461c8e55f36STejun Heo { 462c8e55f36STejun Heo const int base_shift = ilog2(sizeof(struct work_struct)); 463c8e55f36STejun Heo unsigned long v = (unsigned long)work; 464c8e55f36STejun Heo 465c8e55f36STejun Heo /* simple shift and fold hash, do we need something better? */ 466c8e55f36STejun Heo v >>= base_shift; 467c8e55f36STejun Heo v += v >> BUSY_WORKER_HASH_ORDER; 468c8e55f36STejun Heo v &= BUSY_WORKER_HASH_MASK; 469c8e55f36STejun Heo 470c8e55f36STejun Heo return &gcwq->busy_hash[v]; 471c8e55f36STejun Heo } 472c8e55f36STejun Heo 473c8e55f36STejun Heo /** 4748cca0eeaSTejun Heo * __find_worker_executing_work - find worker which is executing a work 4758cca0eeaSTejun Heo * @gcwq: gcwq of interest 4768cca0eeaSTejun Heo * @bwh: hash head as returned by busy_worker_head() 4778cca0eeaSTejun Heo * @work: work to find worker for 4788cca0eeaSTejun Heo * 4798cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. @bwh should be 4808cca0eeaSTejun Heo * the hash head obtained by calling busy_worker_head() with the same 4818cca0eeaSTejun Heo * work. 4828cca0eeaSTejun Heo * 4838cca0eeaSTejun Heo * CONTEXT: 4848cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 4858cca0eeaSTejun Heo * 4868cca0eeaSTejun Heo * RETURNS: 4878cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 4888cca0eeaSTejun Heo * otherwise. 4898cca0eeaSTejun Heo */ 4908cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, 4918cca0eeaSTejun Heo struct hlist_head *bwh, 4928cca0eeaSTejun Heo struct work_struct *work) 4938cca0eeaSTejun Heo { 4948cca0eeaSTejun Heo struct worker *worker; 4958cca0eeaSTejun Heo struct hlist_node *tmp; 4968cca0eeaSTejun Heo 4978cca0eeaSTejun Heo hlist_for_each_entry(worker, tmp, bwh, hentry) 4988cca0eeaSTejun Heo if (worker->current_work == work) 4998cca0eeaSTejun Heo return worker; 5008cca0eeaSTejun Heo return NULL; 5018cca0eeaSTejun Heo } 5028cca0eeaSTejun Heo 5038cca0eeaSTejun Heo /** 5048cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 5058cca0eeaSTejun Heo * @gcwq: gcwq of interest 5068cca0eeaSTejun Heo * @work: work to find worker for 5078cca0eeaSTejun Heo * 5088cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. This function is 5098cca0eeaSTejun Heo * identical to __find_worker_executing_work() except that this 5108cca0eeaSTejun Heo * function calculates @bwh itself. 5118cca0eeaSTejun Heo * 5128cca0eeaSTejun Heo * CONTEXT: 5138cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 5148cca0eeaSTejun Heo * 5158cca0eeaSTejun Heo * RETURNS: 5168cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 5178cca0eeaSTejun Heo * otherwise. 5188cca0eeaSTejun Heo */ 5198cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq, 5208cca0eeaSTejun Heo struct work_struct *work) 5218cca0eeaSTejun Heo { 5228cca0eeaSTejun Heo return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), 5238cca0eeaSTejun Heo work); 5248cca0eeaSTejun Heo } 5258cca0eeaSTejun Heo 5268cca0eeaSTejun Heo /** 5277e11629dSTejun Heo * insert_work - insert a work into gcwq 5284690c4abSTejun Heo * @cwq: cwq @work belongs to 5294690c4abSTejun Heo * @work: work to insert 5304690c4abSTejun Heo * @head: insertion point 5314690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 5324690c4abSTejun Heo * 5337e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 5347e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 5354690c4abSTejun Heo * 5364690c4abSTejun Heo * CONTEXT: 5378b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 5384690c4abSTejun Heo */ 539b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 5404690c4abSTejun Heo struct work_struct *work, struct list_head *head, 5414690c4abSTejun Heo unsigned int extra_flags) 542b89deed3SOleg Nesterov { 5434690c4abSTejun Heo /* we own @work, set data and link */ 5447a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 5454690c4abSTejun Heo 5466e84d644SOleg Nesterov /* 5476e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 5486e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 5496e84d644SOleg Nesterov */ 5506e84d644SOleg Nesterov smp_wmb(); 5514690c4abSTejun Heo 5521a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 5537e11629dSTejun Heo wake_up_worker(cwq->gcwq); 554b89deed3SOleg Nesterov } 555b89deed3SOleg Nesterov 556502ca9d8STejun Heo /** 557502ca9d8STejun Heo * cwq_unbind_single_cpu - unbind cwq from single cpu workqueue processing 558502ca9d8STejun Heo * @cwq: cwq to unbind 559502ca9d8STejun Heo * 560502ca9d8STejun Heo * Try to unbind @cwq from single cpu workqueue processing. If 561502ca9d8STejun Heo * @cwq->wq is frozen, unbind is delayed till the workqueue is thawed. 562502ca9d8STejun Heo * 563502ca9d8STejun Heo * CONTEXT: 564502ca9d8STejun Heo * spin_lock_irq(gcwq->lock). 565502ca9d8STejun Heo */ 566502ca9d8STejun Heo static void cwq_unbind_single_cpu(struct cpu_workqueue_struct *cwq) 567502ca9d8STejun Heo { 568502ca9d8STejun Heo struct workqueue_struct *wq = cwq->wq; 569502ca9d8STejun Heo struct global_cwq *gcwq = cwq->gcwq; 570502ca9d8STejun Heo 571502ca9d8STejun Heo BUG_ON(wq->single_cpu != gcwq->cpu); 572502ca9d8STejun Heo /* 573502ca9d8STejun Heo * Unbind from workqueue if @cwq is not frozen. If frozen, 574502ca9d8STejun Heo * thaw_workqueues() will either restart processing on this 575502ca9d8STejun Heo * cpu or unbind if empty. This keeps works queued while 576502ca9d8STejun Heo * frozen fully ordered and flushable. 577502ca9d8STejun Heo */ 578502ca9d8STejun Heo if (likely(!(gcwq->flags & GCWQ_FREEZING))) { 579502ca9d8STejun Heo smp_wmb(); /* paired with cmpxchg() in __queue_work() */ 580502ca9d8STejun Heo wq->single_cpu = NR_CPUS; 581502ca9d8STejun Heo } 582502ca9d8STejun Heo } 583502ca9d8STejun Heo 5844690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 5851da177e4SLinus Torvalds struct work_struct *work) 5861da177e4SLinus Torvalds { 587502ca9d8STejun Heo struct global_cwq *gcwq; 588502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 5891e19ffc6STejun Heo struct list_head *worklist; 5901da177e4SLinus Torvalds unsigned long flags; 591502ca9d8STejun Heo bool arbitrate; 5921da177e4SLinus Torvalds 593dc186ad7SThomas Gleixner debug_work_activate(work); 5941e19ffc6STejun Heo 59518aa9effSTejun Heo /* 59618aa9effSTejun Heo * Determine gcwq to use. SINGLE_CPU is inherently 59718aa9effSTejun Heo * NON_REENTRANT, so test it first. 59818aa9effSTejun Heo */ 599502ca9d8STejun Heo if (!(wq->flags & WQ_SINGLE_CPU)) { 60018aa9effSTejun Heo struct global_cwq *last_gcwq; 60118aa9effSTejun Heo 60218aa9effSTejun Heo /* 60318aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 60418aa9effSTejun Heo * was previously on a different cpu, it might still 60518aa9effSTejun Heo * be running there, in which case the work needs to 60618aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 60718aa9effSTejun Heo */ 608502ca9d8STejun Heo gcwq = get_gcwq(cpu); 60918aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 61018aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 61118aa9effSTejun Heo struct worker *worker; 61218aa9effSTejun Heo 61318aa9effSTejun Heo spin_lock_irqsave(&last_gcwq->lock, flags); 61418aa9effSTejun Heo 61518aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 61618aa9effSTejun Heo 61718aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 61818aa9effSTejun Heo gcwq = last_gcwq; 61918aa9effSTejun Heo else { 62018aa9effSTejun Heo /* meh... not running there, queue here */ 62118aa9effSTejun Heo spin_unlock_irqrestore(&last_gcwq->lock, flags); 62218aa9effSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 62318aa9effSTejun Heo } 62418aa9effSTejun Heo } else 6258b03ae3cSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 626502ca9d8STejun Heo } else { 627502ca9d8STejun Heo unsigned int req_cpu = cpu; 628502ca9d8STejun Heo 629502ca9d8STejun Heo /* 630502ca9d8STejun Heo * It's a bit more complex for single cpu workqueues. 631502ca9d8STejun Heo * We first need to determine which cpu is going to be 632502ca9d8STejun Heo * used. If no cpu is currently serving this 633502ca9d8STejun Heo * workqueue, arbitrate using atomic accesses to 634502ca9d8STejun Heo * wq->single_cpu; otherwise, use the current one. 635502ca9d8STejun Heo */ 636502ca9d8STejun Heo retry: 637502ca9d8STejun Heo cpu = wq->single_cpu; 638502ca9d8STejun Heo arbitrate = cpu == NR_CPUS; 639502ca9d8STejun Heo if (arbitrate) 640502ca9d8STejun Heo cpu = req_cpu; 641502ca9d8STejun Heo 642502ca9d8STejun Heo gcwq = get_gcwq(cpu); 643502ca9d8STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 644502ca9d8STejun Heo 645502ca9d8STejun Heo /* 646502ca9d8STejun Heo * The following cmpxchg() is a full barrier paired 647502ca9d8STejun Heo * with smp_wmb() in cwq_unbind_single_cpu() and 648502ca9d8STejun Heo * guarantees that all changes to wq->st_* fields are 649502ca9d8STejun Heo * visible on the new cpu after this point. 650502ca9d8STejun Heo */ 651502ca9d8STejun Heo if (arbitrate) 652502ca9d8STejun Heo cmpxchg(&wq->single_cpu, NR_CPUS, cpu); 653502ca9d8STejun Heo 654502ca9d8STejun Heo if (unlikely(wq->single_cpu != cpu)) { 655502ca9d8STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 656502ca9d8STejun Heo goto retry; 657502ca9d8STejun Heo } 658502ca9d8STejun Heo } 659502ca9d8STejun Heo 660502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 661502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 662502ca9d8STejun Heo 6634690c4abSTejun Heo BUG_ON(!list_empty(&work->entry)); 6641e19ffc6STejun Heo 66573f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 6661e19ffc6STejun Heo 6671e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 6681e19ffc6STejun Heo cwq->nr_active++; 6697e11629dSTejun Heo worklist = &gcwq->worklist; 6701e19ffc6STejun Heo } else 6711e19ffc6STejun Heo worklist = &cwq->delayed_works; 6721e19ffc6STejun Heo 6731e19ffc6STejun Heo insert_work(cwq, work, worklist, work_color_to_flags(cwq->work_color)); 6741e19ffc6STejun Heo 6758b03ae3cSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6780fcb78c2SRolf Eike Beer /** 6790fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 6800fcb78c2SRolf Eike Beer * @wq: workqueue to use 6810fcb78c2SRolf Eike Beer * @work: work to queue 6820fcb78c2SRolf Eike Beer * 683057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 6841da177e4SLinus Torvalds * 68500dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 68600dfcaf7SOleg Nesterov * it can be processed by another CPU. 6871da177e4SLinus Torvalds */ 6887ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 6891da177e4SLinus Torvalds { 690ef1ca236SOleg Nesterov int ret; 6911da177e4SLinus Torvalds 692ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 693a848e3b6SOleg Nesterov put_cpu(); 694ef1ca236SOleg Nesterov 6951da177e4SLinus Torvalds return ret; 6961da177e4SLinus Torvalds } 697ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 6981da177e4SLinus Torvalds 699c1a220e7SZhang Rui /** 700c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 701c1a220e7SZhang Rui * @cpu: CPU number to execute work on 702c1a220e7SZhang Rui * @wq: workqueue to use 703c1a220e7SZhang Rui * @work: work to queue 704c1a220e7SZhang Rui * 705c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 706c1a220e7SZhang Rui * 707c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 708c1a220e7SZhang Rui * can't go away. 709c1a220e7SZhang Rui */ 710c1a220e7SZhang Rui int 711c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 712c1a220e7SZhang Rui { 713c1a220e7SZhang Rui int ret = 0; 714c1a220e7SZhang Rui 71522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 7164690c4abSTejun Heo __queue_work(cpu, wq, work); 717c1a220e7SZhang Rui ret = 1; 718c1a220e7SZhang Rui } 719c1a220e7SZhang Rui return ret; 720c1a220e7SZhang Rui } 721c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 722c1a220e7SZhang Rui 7236d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 7241da177e4SLinus Torvalds { 72552bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 7267a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 7271da177e4SLinus Torvalds 7284690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7310fcb78c2SRolf Eike Beer /** 7320fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 7330fcb78c2SRolf Eike Beer * @wq: workqueue to use 734af9997e4SRandy Dunlap * @dwork: delayable work to queue 7350fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 7360fcb78c2SRolf Eike Beer * 737057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 7380fcb78c2SRolf Eike Beer */ 7397ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 74052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 7411da177e4SLinus Torvalds { 74252bad64dSDavid Howells if (delay == 0) 74363bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 7441da177e4SLinus Torvalds 74563bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 7461da177e4SLinus Torvalds } 747ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 7481da177e4SLinus Torvalds 7490fcb78c2SRolf Eike Beer /** 7500fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 7510fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 7520fcb78c2SRolf Eike Beer * @wq: workqueue to use 753af9997e4SRandy Dunlap * @dwork: work to queue 7540fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 7550fcb78c2SRolf Eike Beer * 756057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 7570fcb78c2SRolf Eike Beer */ 7587a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 75952bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 7607a6bc1cdSVenkatesh Pallipadi { 7617a6bc1cdSVenkatesh Pallipadi int ret = 0; 76252bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 76352bad64dSDavid Howells struct work_struct *work = &dwork->work; 7647a6bc1cdSVenkatesh Pallipadi 76522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 7667a22ad75STejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 7677a22ad75STejun Heo unsigned int lcpu = gcwq ? gcwq->cpu : raw_smp_processor_id(); 7687a22ad75STejun Heo 7697a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 7707a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 7717a6bc1cdSVenkatesh Pallipadi 7728a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 7737a22ad75STejun Heo /* 7747a22ad75STejun Heo * This stores cwq for the moment, for the timer_fn. 7757a22ad75STejun Heo * Note that the work's gcwq is preserved to allow 7767a22ad75STejun Heo * reentrance detection for delayed works. 7777a22ad75STejun Heo */ 7787a22ad75STejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 7797a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 78052bad64dSDavid Howells timer->data = (unsigned long)dwork; 7817a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 78263bc0362SOleg Nesterov 78363bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 7847a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 78563bc0362SOleg Nesterov else 78663bc0362SOleg Nesterov add_timer(timer); 7877a6bc1cdSVenkatesh Pallipadi ret = 1; 7887a6bc1cdSVenkatesh Pallipadi } 7897a6bc1cdSVenkatesh Pallipadi return ret; 7907a6bc1cdSVenkatesh Pallipadi } 791ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 7921da177e4SLinus Torvalds 793c8e55f36STejun Heo /** 794c8e55f36STejun Heo * worker_enter_idle - enter idle state 795c8e55f36STejun Heo * @worker: worker which is entering idle state 796c8e55f36STejun Heo * 797c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 798c8e55f36STejun Heo * necessary. 799c8e55f36STejun Heo * 800c8e55f36STejun Heo * LOCKING: 801c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 802c8e55f36STejun Heo */ 803c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 804c8e55f36STejun Heo { 805c8e55f36STejun Heo struct global_cwq *gcwq = worker->gcwq; 806c8e55f36STejun Heo 807c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 808c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 809c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 810c8e55f36STejun Heo 811*d302f017STejun Heo worker_set_flags(worker, WORKER_IDLE, false); 812c8e55f36STejun Heo gcwq->nr_idle++; 813c8e55f36STejun Heo 814c8e55f36STejun Heo /* idle_list is LIFO */ 815c8e55f36STejun Heo list_add(&worker->entry, &gcwq->idle_list); 816db7bccf4STejun Heo 817db7bccf4STejun Heo if (unlikely(worker->flags & WORKER_ROGUE)) 818db7bccf4STejun Heo wake_up_all(&gcwq->trustee_wait); 819c8e55f36STejun Heo } 820c8e55f36STejun Heo 821c8e55f36STejun Heo /** 822c8e55f36STejun Heo * worker_leave_idle - leave idle state 823c8e55f36STejun Heo * @worker: worker which is leaving idle state 824c8e55f36STejun Heo * 825c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 826c8e55f36STejun Heo * 827c8e55f36STejun Heo * LOCKING: 828c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 829c8e55f36STejun Heo */ 830c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 831c8e55f36STejun Heo { 832c8e55f36STejun Heo struct global_cwq *gcwq = worker->gcwq; 833c8e55f36STejun Heo 834c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 835*d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 836c8e55f36STejun Heo gcwq->nr_idle--; 837c8e55f36STejun Heo list_del_init(&worker->entry); 838c8e55f36STejun Heo } 839c8e55f36STejun Heo 840c34056a3STejun Heo static struct worker *alloc_worker(void) 841c34056a3STejun Heo { 842c34056a3STejun Heo struct worker *worker; 843c34056a3STejun Heo 844c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 845c8e55f36STejun Heo if (worker) { 846c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 847affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 848c8e55f36STejun Heo } 849c34056a3STejun Heo return worker; 850c34056a3STejun Heo } 851c34056a3STejun Heo 852c34056a3STejun Heo /** 853c34056a3STejun Heo * create_worker - create a new workqueue worker 8547e11629dSTejun Heo * @gcwq: gcwq the new worker will belong to 855c34056a3STejun Heo * @bind: whether to set affinity to @cpu or not 856c34056a3STejun Heo * 8577e11629dSTejun Heo * Create a new worker which is bound to @gcwq. The returned worker 858c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 859c34056a3STejun Heo * destroy_worker(). 860c34056a3STejun Heo * 861c34056a3STejun Heo * CONTEXT: 862c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 863c34056a3STejun Heo * 864c34056a3STejun Heo * RETURNS: 865c34056a3STejun Heo * Pointer to the newly created worker. 866c34056a3STejun Heo */ 8677e11629dSTejun Heo static struct worker *create_worker(struct global_cwq *gcwq, bool bind) 868c34056a3STejun Heo { 869c34056a3STejun Heo int id = -1; 870c34056a3STejun Heo struct worker *worker = NULL; 871c34056a3STejun Heo 8728b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 8738b03ae3cSTejun Heo while (ida_get_new(&gcwq->worker_ida, &id)) { 8748b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 8758b03ae3cSTejun Heo if (!ida_pre_get(&gcwq->worker_ida, GFP_KERNEL)) 876c34056a3STejun Heo goto fail; 8778b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 878c34056a3STejun Heo } 8798b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 880c34056a3STejun Heo 881c34056a3STejun Heo worker = alloc_worker(); 882c34056a3STejun Heo if (!worker) 883c34056a3STejun Heo goto fail; 884c34056a3STejun Heo 8858b03ae3cSTejun Heo worker->gcwq = gcwq; 886c34056a3STejun Heo worker->id = id; 887c34056a3STejun Heo 888c34056a3STejun Heo worker->task = kthread_create(worker_thread, worker, "kworker/%u:%d", 8898b03ae3cSTejun Heo gcwq->cpu, id); 890c34056a3STejun Heo if (IS_ERR(worker->task)) 891c34056a3STejun Heo goto fail; 892c34056a3STejun Heo 893db7bccf4STejun Heo /* 894db7bccf4STejun Heo * A rogue worker will become a regular one if CPU comes 895db7bccf4STejun Heo * online later on. Make sure every worker has 896db7bccf4STejun Heo * PF_THREAD_BOUND set. 897db7bccf4STejun Heo */ 898c34056a3STejun Heo if (bind) 8998b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 900db7bccf4STejun Heo else 901db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 902c34056a3STejun Heo 903c34056a3STejun Heo return worker; 904c34056a3STejun Heo fail: 905c34056a3STejun Heo if (id >= 0) { 9068b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 9078b03ae3cSTejun Heo ida_remove(&gcwq->worker_ida, id); 9088b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 909c34056a3STejun Heo } 910c34056a3STejun Heo kfree(worker); 911c34056a3STejun Heo return NULL; 912c34056a3STejun Heo } 913c34056a3STejun Heo 914c34056a3STejun Heo /** 915c34056a3STejun Heo * start_worker - start a newly created worker 916c34056a3STejun Heo * @worker: worker to start 917c34056a3STejun Heo * 918c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 919c34056a3STejun Heo * 920c34056a3STejun Heo * CONTEXT: 9218b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 922c34056a3STejun Heo */ 923c34056a3STejun Heo static void start_worker(struct worker *worker) 924c34056a3STejun Heo { 925*d302f017STejun Heo worker_set_flags(worker, WORKER_STARTED, false); 926c8e55f36STejun Heo worker->gcwq->nr_workers++; 927c8e55f36STejun Heo worker_enter_idle(worker); 928c34056a3STejun Heo wake_up_process(worker->task); 929c34056a3STejun Heo } 930c34056a3STejun Heo 931c34056a3STejun Heo /** 932c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 933c34056a3STejun Heo * @worker: worker to be destroyed 934c34056a3STejun Heo * 935c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 936c8e55f36STejun Heo * 937c8e55f36STejun Heo * CONTEXT: 938c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 939c34056a3STejun Heo */ 940c34056a3STejun Heo static void destroy_worker(struct worker *worker) 941c34056a3STejun Heo { 9428b03ae3cSTejun Heo struct global_cwq *gcwq = worker->gcwq; 943c34056a3STejun Heo int id = worker->id; 944c34056a3STejun Heo 945c34056a3STejun Heo /* sanity check frenzy */ 946c34056a3STejun Heo BUG_ON(worker->current_work); 947affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 948c34056a3STejun Heo 949c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 950c8e55f36STejun Heo gcwq->nr_workers--; 951c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 952c8e55f36STejun Heo gcwq->nr_idle--; 953c8e55f36STejun Heo 954c8e55f36STejun Heo list_del_init(&worker->entry); 955*d302f017STejun Heo worker_set_flags(worker, WORKER_DIE, false); 956c8e55f36STejun Heo 957c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 958c8e55f36STejun Heo 959c34056a3STejun Heo kthread_stop(worker->task); 960c34056a3STejun Heo kfree(worker); 961c34056a3STejun Heo 9628b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 9638b03ae3cSTejun Heo ida_remove(&gcwq->worker_ida, id); 964c34056a3STejun Heo } 965c34056a3STejun Heo 966a62428c0STejun Heo /** 967affee4b2STejun Heo * move_linked_works - move linked works to a list 968affee4b2STejun Heo * @work: start of series of works to be scheduled 969affee4b2STejun Heo * @head: target list to append @work to 970affee4b2STejun Heo * @nextp: out paramter for nested worklist walking 971affee4b2STejun Heo * 972affee4b2STejun Heo * Schedule linked works starting from @work to @head. Work series to 973affee4b2STejun Heo * be scheduled starts at @work and includes any consecutive work with 974affee4b2STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 975affee4b2STejun Heo * 976affee4b2STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 977affee4b2STejun Heo * the last scheduled work. This allows move_linked_works() to be 978affee4b2STejun Heo * nested inside outer list_for_each_entry_safe(). 979affee4b2STejun Heo * 980affee4b2STejun Heo * CONTEXT: 9818b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 982affee4b2STejun Heo */ 983affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 984affee4b2STejun Heo struct work_struct **nextp) 985affee4b2STejun Heo { 986affee4b2STejun Heo struct work_struct *n; 987affee4b2STejun Heo 988affee4b2STejun Heo /* 989affee4b2STejun Heo * Linked worklist will always end before the end of the list, 990affee4b2STejun Heo * use NULL for list head. 991affee4b2STejun Heo */ 992affee4b2STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 993affee4b2STejun Heo list_move_tail(&work->entry, head); 994affee4b2STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 995affee4b2STejun Heo break; 996affee4b2STejun Heo } 997affee4b2STejun Heo 998affee4b2STejun Heo /* 999affee4b2STejun Heo * If we're already inside safe list traversal and have moved 1000affee4b2STejun Heo * multiple works to the scheduled queue, the next position 1001affee4b2STejun Heo * needs to be updated. 1002affee4b2STejun Heo */ 1003affee4b2STejun Heo if (nextp) 1004affee4b2STejun Heo *nextp = n; 1005affee4b2STejun Heo } 1006affee4b2STejun Heo 10071e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 10081e19ffc6STejun Heo { 10091e19ffc6STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 10101e19ffc6STejun Heo struct work_struct, entry); 10111e19ffc6STejun Heo 10127e11629dSTejun Heo move_linked_works(work, &cwq->gcwq->worklist, NULL); 10131e19ffc6STejun Heo cwq->nr_active++; 10141e19ffc6STejun Heo } 10151e19ffc6STejun Heo 1016affee4b2STejun Heo /** 101773f53c4aSTejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 101873f53c4aSTejun Heo * @cwq: cwq of interest 101973f53c4aSTejun Heo * @color: color of work which left the queue 102073f53c4aSTejun Heo * 102173f53c4aSTejun Heo * A work either has completed or is removed from pending queue, 102273f53c4aSTejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 102373f53c4aSTejun Heo * 102473f53c4aSTejun Heo * CONTEXT: 10258b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 102673f53c4aSTejun Heo */ 102773f53c4aSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color) 102873f53c4aSTejun Heo { 102973f53c4aSTejun Heo /* ignore uncolored works */ 103073f53c4aSTejun Heo if (color == WORK_NO_COLOR) 103173f53c4aSTejun Heo return; 103273f53c4aSTejun Heo 103373f53c4aSTejun Heo cwq->nr_in_flight[color]--; 10341e19ffc6STejun Heo cwq->nr_active--; 10351e19ffc6STejun Heo 1036502ca9d8STejun Heo if (!list_empty(&cwq->delayed_works)) { 10371e19ffc6STejun Heo /* one down, submit a delayed one */ 1038502ca9d8STejun Heo if (cwq->nr_active < cwq->max_active) 10391e19ffc6STejun Heo cwq_activate_first_delayed(cwq); 1040502ca9d8STejun Heo } else if (!cwq->nr_active && cwq->wq->flags & WQ_SINGLE_CPU) { 1041502ca9d8STejun Heo /* this was the last work, unbind from single cpu */ 1042502ca9d8STejun Heo cwq_unbind_single_cpu(cwq); 1043502ca9d8STejun Heo } 104473f53c4aSTejun Heo 104573f53c4aSTejun Heo /* is flush in progress and are we at the flushing tip? */ 104673f53c4aSTejun Heo if (likely(cwq->flush_color != color)) 104773f53c4aSTejun Heo return; 104873f53c4aSTejun Heo 104973f53c4aSTejun Heo /* are there still in-flight works? */ 105073f53c4aSTejun Heo if (cwq->nr_in_flight[color]) 105173f53c4aSTejun Heo return; 105273f53c4aSTejun Heo 105373f53c4aSTejun Heo /* this cwq is done, clear flush_color */ 105473f53c4aSTejun Heo cwq->flush_color = -1; 105573f53c4aSTejun Heo 105673f53c4aSTejun Heo /* 105773f53c4aSTejun Heo * If this was the last cwq, wake up the first flusher. It 105873f53c4aSTejun Heo * will handle the rest. 105973f53c4aSTejun Heo */ 106073f53c4aSTejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 106173f53c4aSTejun Heo complete(&cwq->wq->first_flusher->done); 106273f53c4aSTejun Heo } 106373f53c4aSTejun Heo 106473f53c4aSTejun Heo /** 1065a62428c0STejun Heo * process_one_work - process single work 1066c34056a3STejun Heo * @worker: self 1067a62428c0STejun Heo * @work: work to process 1068a62428c0STejun Heo * 1069a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1070a62428c0STejun Heo * process a single work including synchronization against and 1071a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 1072a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 1073a62428c0STejun Heo * call this function to process a work. 1074a62428c0STejun Heo * 1075a62428c0STejun Heo * CONTEXT: 10768b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1077a62428c0STejun Heo */ 1078c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 10791da177e4SLinus Torvalds { 10807e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 10818b03ae3cSTejun Heo struct global_cwq *gcwq = cwq->gcwq; 1082c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 10836bb49e59SDavid Howells work_func_t f = work->func; 108473f53c4aSTejun Heo int work_color; 10857e11629dSTejun Heo struct worker *collision; 10864e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 10874e6045f1SJohannes Berg /* 1088a62428c0STejun Heo * It is permissible to free the struct work_struct from 1089a62428c0STejun Heo * inside the function that is called from it, this we need to 1090a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 1091a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 1092a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 10934e6045f1SJohannes Berg */ 10944e6045f1SJohannes Berg struct lockdep_map lockdep_map = work->lockdep_map; 10954e6045f1SJohannes Berg #endif 10967e11629dSTejun Heo /* 10977e11629dSTejun Heo * A single work shouldn't be executed concurrently by 10987e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 10997e11629dSTejun Heo * already processing the work. If so, defer the work to the 11007e11629dSTejun Heo * currently executing one. 11017e11629dSTejun Heo */ 11027e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 11037e11629dSTejun Heo if (unlikely(collision)) { 11047e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 11057e11629dSTejun Heo return; 11067e11629dSTejun Heo } 11077e11629dSTejun Heo 1108a62428c0STejun Heo /* claim and process */ 1109dc186ad7SThomas Gleixner debug_work_deactivate(work); 1110c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 1111c34056a3STejun Heo worker->current_work = work; 11128cca0eeaSTejun Heo worker->current_cwq = cwq; 111373f53c4aSTejun Heo work_color = get_work_color(work); 11147a22ad75STejun Heo 11157a22ad75STejun Heo /* record the current cpu number in the work data and dequeue */ 11167a22ad75STejun Heo set_work_cpu(work, gcwq->cpu); 1117a62428c0STejun Heo list_del_init(&work->entry); 1118a62428c0STejun Heo 11198b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 11201da177e4SLinus Torvalds 112123b2e599SOleg Nesterov work_clear_pending(work); 11223295f0efSIngo Molnar lock_map_acquire(&cwq->wq->lockdep_map); 11233295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 112465f27f38SDavid Howells f(work); 11253295f0efSIngo Molnar lock_map_release(&lockdep_map); 11263295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 11271da177e4SLinus Torvalds 1128d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 1129d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 1130d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 1131a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 1132d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 1133d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 1134d5abe669SPeter Zijlstra debug_show_held_locks(current); 1135d5abe669SPeter Zijlstra dump_stack(); 1136d5abe669SPeter Zijlstra } 1137d5abe669SPeter Zijlstra 11388b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1139a62428c0STejun Heo 1140a62428c0STejun Heo /* we're done with it, release */ 1141c8e55f36STejun Heo hlist_del_init(&worker->hentry); 1142c34056a3STejun Heo worker->current_work = NULL; 11438cca0eeaSTejun Heo worker->current_cwq = NULL; 114473f53c4aSTejun Heo cwq_dec_nr_in_flight(cwq, work_color); 11451da177e4SLinus Torvalds } 1146a62428c0STejun Heo 1147affee4b2STejun Heo /** 1148affee4b2STejun Heo * process_scheduled_works - process scheduled works 1149affee4b2STejun Heo * @worker: self 1150affee4b2STejun Heo * 1151affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 1152affee4b2STejun Heo * may change while processing a work, so this function repeatedly 1153affee4b2STejun Heo * fetches a work from the top and executes it. 1154affee4b2STejun Heo * 1155affee4b2STejun Heo * CONTEXT: 11568b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1157affee4b2STejun Heo * multiple times. 1158affee4b2STejun Heo */ 1159affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 1160a62428c0STejun Heo { 1161affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 1162affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 1163a62428c0STejun Heo struct work_struct, entry); 1164c34056a3STejun Heo process_one_work(worker, work); 1165a62428c0STejun Heo } 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 11684690c4abSTejun Heo /** 11694690c4abSTejun Heo * worker_thread - the worker thread function 1170c34056a3STejun Heo * @__worker: self 11714690c4abSTejun Heo * 11724690c4abSTejun Heo * The cwq worker thread function. 11734690c4abSTejun Heo */ 1174c34056a3STejun Heo static int worker_thread(void *__worker) 11751da177e4SLinus Torvalds { 1176c34056a3STejun Heo struct worker *worker = __worker; 11778b03ae3cSTejun Heo struct global_cwq *gcwq = worker->gcwq; 11781da177e4SLinus Torvalds 1179c8e55f36STejun Heo woke_up: 11808b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1181affee4b2STejun Heo 1182c8e55f36STejun Heo /* DIE can be set only while we're idle, checking here is enough */ 1183c8e55f36STejun Heo if (worker->flags & WORKER_DIE) { 1184c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1185c8e55f36STejun Heo return 0; 1186c8e55f36STejun Heo } 1187c8e55f36STejun Heo 1188c8e55f36STejun Heo worker_leave_idle(worker); 1189db7bccf4STejun Heo recheck: 1190c8e55f36STejun Heo /* 1191c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 1192c8e55f36STejun Heo * preparing to process a work or actually processing it. 1193c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 1194c8e55f36STejun Heo */ 1195c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1196c8e55f36STejun Heo 11977e11629dSTejun Heo while (!list_empty(&gcwq->worklist)) { 1198affee4b2STejun Heo struct work_struct *work = 11997e11629dSTejun Heo list_first_entry(&gcwq->worklist, 1200affee4b2STejun Heo struct work_struct, entry); 1201affee4b2STejun Heo 1202db7bccf4STejun Heo /* 1203db7bccf4STejun Heo * The following is a rather inefficient way to close 1204db7bccf4STejun Heo * race window against cpu hotplug operations. Will 1205db7bccf4STejun Heo * be replaced soon. 1206db7bccf4STejun Heo */ 1207db7bccf4STejun Heo if (unlikely(!(worker->flags & WORKER_ROGUE) && 1208db7bccf4STejun Heo !cpumask_equal(&worker->task->cpus_allowed, 1209db7bccf4STejun Heo get_cpu_mask(gcwq->cpu)))) { 1210db7bccf4STejun Heo spin_unlock_irq(&gcwq->lock); 1211db7bccf4STejun Heo set_cpus_allowed_ptr(worker->task, 1212db7bccf4STejun Heo get_cpu_mask(gcwq->cpu)); 1213db7bccf4STejun Heo cpu_relax(); 1214db7bccf4STejun Heo spin_lock_irq(&gcwq->lock); 1215db7bccf4STejun Heo goto recheck; 1216db7bccf4STejun Heo } 1217db7bccf4STejun Heo 1218c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 1219affee4b2STejun Heo /* optimization path, not strictly necessary */ 1220affee4b2STejun Heo process_one_work(worker, work); 1221affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 1222affee4b2STejun Heo process_scheduled_works(worker); 1223affee4b2STejun Heo } else { 1224c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 1225affee4b2STejun Heo process_scheduled_works(worker); 1226affee4b2STejun Heo } 1227affee4b2STejun Heo } 1228affee4b2STejun Heo 1229c8e55f36STejun Heo /* 1230c8e55f36STejun Heo * gcwq->lock is held and there's no work to process, sleep. 1231c8e55f36STejun Heo * Workers are woken up only while holding gcwq->lock, so 1232c8e55f36STejun Heo * setting the current state before releasing gcwq->lock is 1233c8e55f36STejun Heo * enough to prevent losing any event. 1234c8e55f36STejun Heo */ 1235c8e55f36STejun Heo worker_enter_idle(worker); 1236c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 12378b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1238c8e55f36STejun Heo schedule(); 1239c8e55f36STejun Heo goto woke_up; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 1242fc2e4d70SOleg Nesterov struct wq_barrier { 1243fc2e4d70SOleg Nesterov struct work_struct work; 1244fc2e4d70SOleg Nesterov struct completion done; 1245fc2e4d70SOleg Nesterov }; 1246fc2e4d70SOleg Nesterov 1247fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 1248fc2e4d70SOleg Nesterov { 1249fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 1250fc2e4d70SOleg Nesterov complete(&barr->done); 1251fc2e4d70SOleg Nesterov } 1252fc2e4d70SOleg Nesterov 12534690c4abSTejun Heo /** 12544690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 12554690c4abSTejun Heo * @cwq: cwq to insert barrier into 12564690c4abSTejun Heo * @barr: wq_barrier to insert 1257affee4b2STejun Heo * @target: target work to attach @barr to 1258affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 12594690c4abSTejun Heo * 1260affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 1261affee4b2STejun Heo * @target finishes execution. Please note that the ordering 1262affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 1263affee4b2STejun Heo * cpu. 1264affee4b2STejun Heo * 1265affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 1266affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 1267affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 1268affee4b2STejun Heo * flag of the previous work while there must be a valid next work 1269affee4b2STejun Heo * after a work with LINKED flag set. 1270affee4b2STejun Heo * 1271affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 1272affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 12734690c4abSTejun Heo * 12744690c4abSTejun Heo * CONTEXT: 12758b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 12764690c4abSTejun Heo */ 127783c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 1278affee4b2STejun Heo struct wq_barrier *barr, 1279affee4b2STejun Heo struct work_struct *target, struct worker *worker) 1280fc2e4d70SOleg Nesterov { 1281affee4b2STejun Heo struct list_head *head; 1282affee4b2STejun Heo unsigned int linked = 0; 1283affee4b2STejun Heo 1284dc186ad7SThomas Gleixner /* 12858b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 1286dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 1287dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 1288dc186ad7SThomas Gleixner * might deadlock. 1289dc186ad7SThomas Gleixner */ 1290dc186ad7SThomas Gleixner INIT_WORK_ON_STACK(&barr->work, wq_barrier_func); 129122df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 1292fc2e4d70SOleg Nesterov init_completion(&barr->done); 129383c22520SOleg Nesterov 1294affee4b2STejun Heo /* 1295affee4b2STejun Heo * If @target is currently being executed, schedule the 1296affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 1297affee4b2STejun Heo */ 1298affee4b2STejun Heo if (worker) 1299affee4b2STejun Heo head = worker->scheduled.next; 1300affee4b2STejun Heo else { 1301affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 1302affee4b2STejun Heo 1303affee4b2STejun Heo head = target->entry.next; 1304affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 1305affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 1306affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 1307affee4b2STejun Heo } 1308affee4b2STejun Heo 1309dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 1310affee4b2STejun Heo insert_work(cwq, &barr->work, head, 1311affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 1312fc2e4d70SOleg Nesterov } 1313fc2e4d70SOleg Nesterov 131473f53c4aSTejun Heo /** 131573f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 131673f53c4aSTejun Heo * @wq: workqueue being flushed 131773f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 131873f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 131973f53c4aSTejun Heo * 132073f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 132173f53c4aSTejun Heo * 132273f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 132373f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 132473f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 132573f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 132673f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 132773f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 132873f53c4aSTejun Heo * 132973f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 133073f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 133173f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 133273f53c4aSTejun Heo * is returned. 133373f53c4aSTejun Heo * 133473f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 133573f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 133673f53c4aSTejun Heo * advanced to @work_color. 133773f53c4aSTejun Heo * 133873f53c4aSTejun Heo * CONTEXT: 133973f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 134073f53c4aSTejun Heo * 134173f53c4aSTejun Heo * RETURNS: 134273f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 134373f53c4aSTejun Heo * otherwise. 134473f53c4aSTejun Heo */ 134573f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 134673f53c4aSTejun Heo int flush_color, int work_color) 13471da177e4SLinus Torvalds { 134873f53c4aSTejun Heo bool wait = false; 134973f53c4aSTejun Heo unsigned int cpu; 13501da177e4SLinus Torvalds 135173f53c4aSTejun Heo if (flush_color >= 0) { 135273f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 135373f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 135473f53c4aSTejun Heo } 135573f53c4aSTejun Heo 135673f53c4aSTejun Heo for_each_possible_cpu(cpu) { 135773f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 13588b03ae3cSTejun Heo struct global_cwq *gcwq = cwq->gcwq; 13592355b70fSLai Jiangshan 13608b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 136173f53c4aSTejun Heo 136273f53c4aSTejun Heo if (flush_color >= 0) { 136373f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 136473f53c4aSTejun Heo 136573f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 136673f53c4aSTejun Heo cwq->flush_color = flush_color; 136773f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 136873f53c4aSTejun Heo wait = true; 136983c22520SOleg Nesterov } 137073f53c4aSTejun Heo } 137173f53c4aSTejun Heo 137273f53c4aSTejun Heo if (work_color >= 0) { 137373f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 137473f53c4aSTejun Heo cwq->work_color = work_color; 137573f53c4aSTejun Heo } 137673f53c4aSTejun Heo 13778b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1378dc186ad7SThomas Gleixner } 137914441960SOleg Nesterov 138073f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 138173f53c4aSTejun Heo complete(&wq->first_flusher->done); 138273f53c4aSTejun Heo 138373f53c4aSTejun Heo return wait; 138483c22520SOleg Nesterov } 13851da177e4SLinus Torvalds 13860fcb78c2SRolf Eike Beer /** 13871da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 13880fcb78c2SRolf Eike Beer * @wq: workqueue to flush 13891da177e4SLinus Torvalds * 13901da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 13911da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 13921da177e4SLinus Torvalds * 1393fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 1394fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 13951da177e4SLinus Torvalds */ 13967ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 13971da177e4SLinus Torvalds { 139873f53c4aSTejun Heo struct wq_flusher this_flusher = { 139973f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 140073f53c4aSTejun Heo .flush_color = -1, 140173f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 140273f53c4aSTejun Heo }; 140373f53c4aSTejun Heo int next_color; 1404b1f4ec17SOleg Nesterov 14053295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 14063295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 140773f53c4aSTejun Heo 140873f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 140973f53c4aSTejun Heo 141073f53c4aSTejun Heo /* 141173f53c4aSTejun Heo * Start-to-wait phase 141273f53c4aSTejun Heo */ 141373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 141473f53c4aSTejun Heo 141573f53c4aSTejun Heo if (next_color != wq->flush_color) { 141673f53c4aSTejun Heo /* 141773f53c4aSTejun Heo * Color space is not full. The current work_color 141873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 141973f53c4aSTejun Heo * by one. 142073f53c4aSTejun Heo */ 142173f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 142273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 142373f53c4aSTejun Heo wq->work_color = next_color; 142473f53c4aSTejun Heo 142573f53c4aSTejun Heo if (!wq->first_flusher) { 142673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 142773f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 142873f53c4aSTejun Heo 142973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 143073f53c4aSTejun Heo 143173f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 143273f53c4aSTejun Heo wq->work_color)) { 143373f53c4aSTejun Heo /* nothing to flush, done */ 143473f53c4aSTejun Heo wq->flush_color = next_color; 143573f53c4aSTejun Heo wq->first_flusher = NULL; 143673f53c4aSTejun Heo goto out_unlock; 143773f53c4aSTejun Heo } 143873f53c4aSTejun Heo } else { 143973f53c4aSTejun Heo /* wait in queue */ 144073f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 144173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 144273f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 144373f53c4aSTejun Heo } 144473f53c4aSTejun Heo } else { 144573f53c4aSTejun Heo /* 144673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 144773f53c4aSTejun Heo * The next flush completion will assign us 144873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 144973f53c4aSTejun Heo */ 145073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 145173f53c4aSTejun Heo } 145273f53c4aSTejun Heo 145373f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 145473f53c4aSTejun Heo 145573f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 145673f53c4aSTejun Heo 145773f53c4aSTejun Heo /* 145873f53c4aSTejun Heo * Wake-up-and-cascade phase 145973f53c4aSTejun Heo * 146073f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 146173f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 146273f53c4aSTejun Heo */ 146373f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 146473f53c4aSTejun Heo return; 146573f53c4aSTejun Heo 146673f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 146773f53c4aSTejun Heo 146873f53c4aSTejun Heo wq->first_flusher = NULL; 146973f53c4aSTejun Heo 147073f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 147173f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 147273f53c4aSTejun Heo 147373f53c4aSTejun Heo while (true) { 147473f53c4aSTejun Heo struct wq_flusher *next, *tmp; 147573f53c4aSTejun Heo 147673f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 147773f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 147873f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 147973f53c4aSTejun Heo break; 148073f53c4aSTejun Heo list_del_init(&next->list); 148173f53c4aSTejun Heo complete(&next->done); 148273f53c4aSTejun Heo } 148373f53c4aSTejun Heo 148473f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 148573f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 148673f53c4aSTejun Heo 148773f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 148873f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 148973f53c4aSTejun Heo 149073f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 149173f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 149273f53c4aSTejun Heo /* 149373f53c4aSTejun Heo * Assign the same color to all overflowed 149473f53c4aSTejun Heo * flushers, advance work_color and append to 149573f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 149673f53c4aSTejun Heo * phase for these overflowed flushers. 149773f53c4aSTejun Heo */ 149873f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 149973f53c4aSTejun Heo tmp->flush_color = wq->work_color; 150073f53c4aSTejun Heo 150173f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 150273f53c4aSTejun Heo 150373f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 150473f53c4aSTejun Heo &wq->flusher_queue); 150573f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 150673f53c4aSTejun Heo } 150773f53c4aSTejun Heo 150873f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 150973f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 151073f53c4aSTejun Heo break; 151173f53c4aSTejun Heo } 151273f53c4aSTejun Heo 151373f53c4aSTejun Heo /* 151473f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 151573f53c4aSTejun Heo * the new first flusher and arm cwqs. 151673f53c4aSTejun Heo */ 151773f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 151873f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 151973f53c4aSTejun Heo 152073f53c4aSTejun Heo list_del_init(&next->list); 152173f53c4aSTejun Heo wq->first_flusher = next; 152273f53c4aSTejun Heo 152373f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 152473f53c4aSTejun Heo break; 152573f53c4aSTejun Heo 152673f53c4aSTejun Heo /* 152773f53c4aSTejun Heo * Meh... this color is already done, clear first 152873f53c4aSTejun Heo * flusher and repeat cascading. 152973f53c4aSTejun Heo */ 153073f53c4aSTejun Heo wq->first_flusher = NULL; 153173f53c4aSTejun Heo } 153273f53c4aSTejun Heo 153373f53c4aSTejun Heo out_unlock: 153473f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 15351da177e4SLinus Torvalds } 1536ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 15371da177e4SLinus Torvalds 1538db700897SOleg Nesterov /** 1539db700897SOleg Nesterov * flush_work - block until a work_struct's callback has terminated 1540db700897SOleg Nesterov * @work: the work which is to be flushed 1541db700897SOleg Nesterov * 1542a67da70dSOleg Nesterov * Returns false if @work has already terminated. 1543a67da70dSOleg Nesterov * 1544db700897SOleg Nesterov * It is expected that, prior to calling flush_work(), the caller has 1545db700897SOleg Nesterov * arranged for the work to not be requeued, otherwise it doesn't make 1546db700897SOleg Nesterov * sense to use this function. 1547db700897SOleg Nesterov */ 1548db700897SOleg Nesterov int flush_work(struct work_struct *work) 1549db700897SOleg Nesterov { 1550affee4b2STejun Heo struct worker *worker = NULL; 15518b03ae3cSTejun Heo struct global_cwq *gcwq; 15527a22ad75STejun Heo struct cpu_workqueue_struct *cwq; 1553db700897SOleg Nesterov struct wq_barrier barr; 1554db700897SOleg Nesterov 1555db700897SOleg Nesterov might_sleep(); 15567a22ad75STejun Heo gcwq = get_work_gcwq(work); 15577a22ad75STejun Heo if (!gcwq) 1558db700897SOleg Nesterov return 0; 1559a67da70dSOleg Nesterov 15608b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1561db700897SOleg Nesterov if (!list_empty(&work->entry)) { 1562db700897SOleg Nesterov /* 1563db700897SOleg Nesterov * See the comment near try_to_grab_pending()->smp_rmb(). 15647a22ad75STejun Heo * If it was re-queued to a different gcwq under us, we 15657a22ad75STejun Heo * are not going to wait. 1566db700897SOleg Nesterov */ 1567db700897SOleg Nesterov smp_rmb(); 15687a22ad75STejun Heo cwq = get_work_cwq(work); 15697a22ad75STejun Heo if (unlikely(!cwq || gcwq != cwq->gcwq)) 15704690c4abSTejun Heo goto already_gone; 1571db700897SOleg Nesterov } else { 15727a22ad75STejun Heo worker = find_worker_executing_work(gcwq, work); 1573affee4b2STejun Heo if (!worker) 15744690c4abSTejun Heo goto already_gone; 15757a22ad75STejun Heo cwq = worker->current_cwq; 1576db700897SOleg Nesterov } 1577db700897SOleg Nesterov 1578affee4b2STejun Heo insert_wq_barrier(cwq, &barr, work, worker); 15798b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 15807a22ad75STejun Heo 15817a22ad75STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 15827a22ad75STejun Heo lock_map_release(&cwq->wq->lockdep_map); 15837a22ad75STejun Heo 1584db700897SOleg Nesterov wait_for_completion(&barr.done); 1585dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 1586db700897SOleg Nesterov return 1; 15874690c4abSTejun Heo already_gone: 15888b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 15894690c4abSTejun Heo return 0; 1590db700897SOleg Nesterov } 1591db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 1592db700897SOleg Nesterov 15936e84d644SOleg Nesterov /* 15941f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 15956e84d644SOleg Nesterov * so this work can't be re-armed in any way. 15966e84d644SOleg Nesterov */ 15976e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 15986e84d644SOleg Nesterov { 15998b03ae3cSTejun Heo struct global_cwq *gcwq; 16001f1f642eSOleg Nesterov int ret = -1; 16016e84d644SOleg Nesterov 160222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 16031f1f642eSOleg Nesterov return 0; 16046e84d644SOleg Nesterov 16056e84d644SOleg Nesterov /* 16066e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 16076e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 16086e84d644SOleg Nesterov */ 16097a22ad75STejun Heo gcwq = get_work_gcwq(work); 16107a22ad75STejun Heo if (!gcwq) 16116e84d644SOleg Nesterov return ret; 16126e84d644SOleg Nesterov 16138b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 16146e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 16156e84d644SOleg Nesterov /* 16167a22ad75STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 16176e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 16186e84d644SOleg Nesterov * insert_work()->wmb(). 16196e84d644SOleg Nesterov */ 16206e84d644SOleg Nesterov smp_rmb(); 16217a22ad75STejun Heo if (gcwq == get_work_gcwq(work)) { 1622dc186ad7SThomas Gleixner debug_work_deactivate(work); 16236e84d644SOleg Nesterov list_del_init(&work->entry); 16247a22ad75STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 16257a22ad75STejun Heo get_work_color(work)); 16266e84d644SOleg Nesterov ret = 1; 16276e84d644SOleg Nesterov } 16286e84d644SOleg Nesterov } 16298b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 16306e84d644SOleg Nesterov 16316e84d644SOleg Nesterov return ret; 16326e84d644SOleg Nesterov } 16336e84d644SOleg Nesterov 16347a22ad75STejun Heo static void wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 1635b89deed3SOleg Nesterov { 1636b89deed3SOleg Nesterov struct wq_barrier barr; 1637affee4b2STejun Heo struct worker *worker; 1638b89deed3SOleg Nesterov 16398b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1640affee4b2STejun Heo 16417a22ad75STejun Heo worker = find_worker_executing_work(gcwq, work); 16427a22ad75STejun Heo if (unlikely(worker)) 16437a22ad75STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 1644affee4b2STejun Heo 16458b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1646b89deed3SOleg Nesterov 1647affee4b2STejun Heo if (unlikely(worker)) { 1648b89deed3SOleg Nesterov wait_for_completion(&barr.done); 1649dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 1650dc186ad7SThomas Gleixner } 1651b89deed3SOleg Nesterov } 1652b89deed3SOleg Nesterov 16536e84d644SOleg Nesterov static void wait_on_work(struct work_struct *work) 1654b89deed3SOleg Nesterov { 1655b1f4ec17SOleg Nesterov int cpu; 1656b89deed3SOleg Nesterov 1657f293ea92SOleg Nesterov might_sleep(); 1658f293ea92SOleg Nesterov 16593295f0efSIngo Molnar lock_map_acquire(&work->lockdep_map); 16603295f0efSIngo Molnar lock_map_release(&work->lockdep_map); 16614e6045f1SJohannes Berg 16621537663fSTejun Heo for_each_possible_cpu(cpu) 16637a22ad75STejun Heo wait_on_cpu_work(get_gcwq(cpu), work); 16646e84d644SOleg Nesterov } 16656e84d644SOleg Nesterov 16661f1f642eSOleg Nesterov static int __cancel_work_timer(struct work_struct *work, 16671f1f642eSOleg Nesterov struct timer_list* timer) 16681f1f642eSOleg Nesterov { 16691f1f642eSOleg Nesterov int ret; 16701f1f642eSOleg Nesterov 16711f1f642eSOleg Nesterov do { 16721f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 16731f1f642eSOleg Nesterov if (!ret) 16741f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 16751f1f642eSOleg Nesterov wait_on_work(work); 16761f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 16771f1f642eSOleg Nesterov 16787a22ad75STejun Heo clear_work_data(work); 16791f1f642eSOleg Nesterov return ret; 16801f1f642eSOleg Nesterov } 16811f1f642eSOleg Nesterov 16826e84d644SOleg Nesterov /** 16836e84d644SOleg Nesterov * cancel_work_sync - block until a work_struct's callback has terminated 16846e84d644SOleg Nesterov * @work: the work which is to be flushed 16856e84d644SOleg Nesterov * 16861f1f642eSOleg Nesterov * Returns true if @work was pending. 16871f1f642eSOleg Nesterov * 16886e84d644SOleg Nesterov * cancel_work_sync() will cancel the work if it is queued. If the work's 16896e84d644SOleg Nesterov * callback appears to be running, cancel_work_sync() will block until it 16906e84d644SOleg Nesterov * has completed. 16916e84d644SOleg Nesterov * 16926e84d644SOleg Nesterov * It is possible to use this function if the work re-queues itself. It can 16936e84d644SOleg Nesterov * cancel the work even if it migrates to another workqueue, however in that 16946e84d644SOleg Nesterov * case it only guarantees that work->func() has completed on the last queued 16956e84d644SOleg Nesterov * workqueue. 16966e84d644SOleg Nesterov * 16976e84d644SOleg Nesterov * cancel_work_sync(&delayed_work->work) should be used only if ->timer is not 16986e84d644SOleg Nesterov * pending, otherwise it goes into a busy-wait loop until the timer expires. 16996e84d644SOleg Nesterov * 17006e84d644SOleg Nesterov * The caller must ensure that workqueue_struct on which this work was last 17016e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 17026e84d644SOleg Nesterov */ 17031f1f642eSOleg Nesterov int cancel_work_sync(struct work_struct *work) 17046e84d644SOleg Nesterov { 17051f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 1706b89deed3SOleg Nesterov } 170728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 1708b89deed3SOleg Nesterov 17096e84d644SOleg Nesterov /** 1710f5a421a4SOleg Nesterov * cancel_delayed_work_sync - reliably kill off a delayed work. 17116e84d644SOleg Nesterov * @dwork: the delayed work struct 17126e84d644SOleg Nesterov * 17131f1f642eSOleg Nesterov * Returns true if @dwork was pending. 17141f1f642eSOleg Nesterov * 17156e84d644SOleg Nesterov * It is possible to use this function if @dwork rearms itself via queue_work() 17166e84d644SOleg Nesterov * or queue_delayed_work(). See also the comment for cancel_work_sync(). 17176e84d644SOleg Nesterov */ 17181f1f642eSOleg Nesterov int cancel_delayed_work_sync(struct delayed_work *dwork) 17196e84d644SOleg Nesterov { 17201f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 17216e84d644SOleg Nesterov } 1722f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 17231da177e4SLinus Torvalds 17246e84d644SOleg Nesterov static struct workqueue_struct *keventd_wq __read_mostly; 17251da177e4SLinus Torvalds 17260fcb78c2SRolf Eike Beer /** 17270fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 17280fcb78c2SRolf Eike Beer * @work: job to be done 17290fcb78c2SRolf Eike Beer * 17305b0f437dSBart Van Assche * Returns zero if @work was already on the kernel-global workqueue and 17315b0f437dSBart Van Assche * non-zero otherwise. 17325b0f437dSBart Van Assche * 17335b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 17345b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 17355b0f437dSBart Van Assche * workqueue otherwise. 17360fcb78c2SRolf Eike Beer */ 17377ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 17381da177e4SLinus Torvalds { 17391da177e4SLinus Torvalds return queue_work(keventd_wq, work); 17401da177e4SLinus Torvalds } 1741ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 17421da177e4SLinus Torvalds 1743c1a220e7SZhang Rui /* 1744c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 1745c1a220e7SZhang Rui * @cpu: cpu to put the work task on 1746c1a220e7SZhang Rui * @work: job to be done 1747c1a220e7SZhang Rui * 1748c1a220e7SZhang Rui * This puts a job on a specific cpu 1749c1a220e7SZhang Rui */ 1750c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 1751c1a220e7SZhang Rui { 1752c1a220e7SZhang Rui return queue_work_on(cpu, keventd_wq, work); 1753c1a220e7SZhang Rui } 1754c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 1755c1a220e7SZhang Rui 17560fcb78c2SRolf Eike Beer /** 17570fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 175852bad64dSDavid Howells * @dwork: job to be done 175952bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 17600fcb78c2SRolf Eike Beer * 17610fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 17620fcb78c2SRolf Eike Beer * workqueue. 17630fcb78c2SRolf Eike Beer */ 17647ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 176582f67cd9SIngo Molnar unsigned long delay) 17661da177e4SLinus Torvalds { 176752bad64dSDavid Howells return queue_delayed_work(keventd_wq, dwork, delay); 17681da177e4SLinus Torvalds } 1769ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 17701da177e4SLinus Torvalds 17710fcb78c2SRolf Eike Beer /** 17728c53e463SLinus Torvalds * flush_delayed_work - block until a dwork_struct's callback has terminated 17738c53e463SLinus Torvalds * @dwork: the delayed work which is to be flushed 17748c53e463SLinus Torvalds * 17758c53e463SLinus Torvalds * Any timeout is cancelled, and any pending work is run immediately. 17768c53e463SLinus Torvalds */ 17778c53e463SLinus Torvalds void flush_delayed_work(struct delayed_work *dwork) 17788c53e463SLinus Torvalds { 17798c53e463SLinus Torvalds if (del_timer_sync(&dwork->timer)) { 17807a22ad75STejun Heo __queue_work(get_cpu(), get_work_cwq(&dwork->work)->wq, 17814690c4abSTejun Heo &dwork->work); 17828c53e463SLinus Torvalds put_cpu(); 17838c53e463SLinus Torvalds } 17848c53e463SLinus Torvalds flush_work(&dwork->work); 17858c53e463SLinus Torvalds } 17868c53e463SLinus Torvalds EXPORT_SYMBOL(flush_delayed_work); 17878c53e463SLinus Torvalds 17888c53e463SLinus Torvalds /** 17890fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 17900fcb78c2SRolf Eike Beer * @cpu: cpu to use 179152bad64dSDavid Howells * @dwork: job to be done 17920fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 17930fcb78c2SRolf Eike Beer * 17940fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 17950fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 17960fcb78c2SRolf Eike Beer */ 17971da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 179852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 17991da177e4SLinus Torvalds { 180052bad64dSDavid Howells return queue_delayed_work_on(cpu, keventd_wq, dwork, delay); 18011da177e4SLinus Torvalds } 1802ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 18031da177e4SLinus Torvalds 1804b6136773SAndrew Morton /** 1805b6136773SAndrew Morton * schedule_on_each_cpu - call a function on each online CPU from keventd 1806b6136773SAndrew Morton * @func: the function to call 1807b6136773SAndrew Morton * 1808b6136773SAndrew Morton * Returns zero on success. 1809b6136773SAndrew Morton * Returns -ve errno on failure. 1810b6136773SAndrew Morton * 1811b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 1812b6136773SAndrew Morton */ 181365f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 181415316ba8SChristoph Lameter { 181515316ba8SChristoph Lameter int cpu; 181665a64464SAndi Kleen int orig = -1; 1817b6136773SAndrew Morton struct work_struct *works; 181815316ba8SChristoph Lameter 1819b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 1820b6136773SAndrew Morton if (!works) 182115316ba8SChristoph Lameter return -ENOMEM; 1822b6136773SAndrew Morton 182395402b38SGautham R Shenoy get_online_cpus(); 182493981800STejun Heo 182593981800STejun Heo /* 182693981800STejun Heo * When running in keventd don't schedule a work item on 182793981800STejun Heo * itself. Can just call directly because the work queue is 182893981800STejun Heo * already bound. This also is faster. 182993981800STejun Heo */ 183093981800STejun Heo if (current_is_keventd()) 183193981800STejun Heo orig = raw_smp_processor_id(); 183293981800STejun Heo 183315316ba8SChristoph Lameter for_each_online_cpu(cpu) { 18349bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 18359bfb1839SIngo Molnar 18369bfb1839SIngo Molnar INIT_WORK(work, func); 183793981800STejun Heo if (cpu != orig) 18388de6d308SOleg Nesterov schedule_work_on(cpu, work); 183915316ba8SChristoph Lameter } 184093981800STejun Heo if (orig >= 0) 184193981800STejun Heo func(per_cpu_ptr(works, orig)); 184293981800STejun Heo 184393981800STejun Heo for_each_online_cpu(cpu) 18448616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 184593981800STejun Heo 184695402b38SGautham R Shenoy put_online_cpus(); 1847b6136773SAndrew Morton free_percpu(works); 184815316ba8SChristoph Lameter return 0; 184915316ba8SChristoph Lameter } 185015316ba8SChristoph Lameter 1851eef6a7d5SAlan Stern /** 1852eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 1853eef6a7d5SAlan Stern * 1854eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 1855eef6a7d5SAlan Stern * completion. 1856eef6a7d5SAlan Stern * 1857eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 1858eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 1859eef6a7d5SAlan Stern * will lead to deadlock: 1860eef6a7d5SAlan Stern * 1861eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 1862eef6a7d5SAlan Stern * a lock held by your code or its caller. 1863eef6a7d5SAlan Stern * 1864eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 1865eef6a7d5SAlan Stern * 1866eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 1867eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 1868eef6a7d5SAlan Stern * what locks they need, which you have no control over. 1869eef6a7d5SAlan Stern * 1870eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 1871eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 1872eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 1873eef6a7d5SAlan Stern * cancel_work_sync() instead. 1874eef6a7d5SAlan Stern */ 18751da177e4SLinus Torvalds void flush_scheduled_work(void) 18761da177e4SLinus Torvalds { 18771da177e4SLinus Torvalds flush_workqueue(keventd_wq); 18781da177e4SLinus Torvalds } 1879ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /** 18821fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 18831fa44ecaSJames Bottomley * @fn: the function to execute 18841fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 18851fa44ecaSJames Bottomley * be available when the work executes) 18861fa44ecaSJames Bottomley * 18871fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 18881fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 18891fa44ecaSJames Bottomley * 18901fa44ecaSJames Bottomley * Returns: 0 - function was executed 18911fa44ecaSJames Bottomley * 1 - function was scheduled for execution 18921fa44ecaSJames Bottomley */ 189365f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 18941fa44ecaSJames Bottomley { 18951fa44ecaSJames Bottomley if (!in_interrupt()) { 189665f27f38SDavid Howells fn(&ew->work); 18971fa44ecaSJames Bottomley return 0; 18981fa44ecaSJames Bottomley } 18991fa44ecaSJames Bottomley 190065f27f38SDavid Howells INIT_WORK(&ew->work, fn); 19011fa44ecaSJames Bottomley schedule_work(&ew->work); 19021fa44ecaSJames Bottomley 19031fa44ecaSJames Bottomley return 1; 19041fa44ecaSJames Bottomley } 19051fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 19061fa44ecaSJames Bottomley 19071da177e4SLinus Torvalds int keventd_up(void) 19081da177e4SLinus Torvalds { 19091da177e4SLinus Torvalds return keventd_wq != NULL; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds int current_is_keventd(void) 19131da177e4SLinus Torvalds { 19147e11629dSTejun Heo bool found = false; 19157e11629dSTejun Heo unsigned int cpu; 19161da177e4SLinus Torvalds 19177e11629dSTejun Heo /* 19187e11629dSTejun Heo * There no longer is one-to-one relation between worker and 19197e11629dSTejun Heo * work queue and a worker task might be unbound from its cpu 19207e11629dSTejun Heo * if the cpu was offlined. Match all busy workers. This 19217e11629dSTejun Heo * function will go away once dynamic pool is implemented. 19227e11629dSTejun Heo */ 19237e11629dSTejun Heo for_each_possible_cpu(cpu) { 19247e11629dSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 19257e11629dSTejun Heo struct worker *worker; 19267e11629dSTejun Heo struct hlist_node *pos; 19277e11629dSTejun Heo unsigned long flags; 19287e11629dSTejun Heo int i; 19291da177e4SLinus Torvalds 19307e11629dSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 19311da177e4SLinus Torvalds 19327e11629dSTejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 19337e11629dSTejun Heo if (worker->task == current) { 19347e11629dSTejun Heo found = true; 19357e11629dSTejun Heo break; 19367e11629dSTejun Heo } 19377e11629dSTejun Heo } 19381da177e4SLinus Torvalds 19397e11629dSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 19407e11629dSTejun Heo if (found) 19417e11629dSTejun Heo break; 19427e11629dSTejun Heo } 19437e11629dSTejun Heo 19447e11629dSTejun Heo return found; 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19470f900049STejun Heo static struct cpu_workqueue_struct *alloc_cwqs(void) 19480f900049STejun Heo { 19490f900049STejun Heo /* 19500f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 19510f900049STejun Heo * Make sure that the alignment isn't lower than that of 19520f900049STejun Heo * unsigned long long. 19530f900049STejun Heo */ 19540f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 19550f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 19560f900049STejun Heo __alignof__(unsigned long long)); 19570f900049STejun Heo struct cpu_workqueue_struct *cwqs; 19580f900049STejun Heo #ifndef CONFIG_SMP 19590f900049STejun Heo void *ptr; 19600f900049STejun Heo 19610f900049STejun Heo /* 19620f900049STejun Heo * On UP, percpu allocator doesn't honor alignment parameter 19630f900049STejun Heo * and simply uses arch-dependent default. Allocate enough 19640f900049STejun Heo * room to align cwq and put an extra pointer at the end 19650f900049STejun Heo * pointing back to the originally allocated pointer which 19660f900049STejun Heo * will be used for free. 19670f900049STejun Heo * 19680f900049STejun Heo * FIXME: This really belongs to UP percpu code. Update UP 19690f900049STejun Heo * percpu code to honor alignment and remove this ugliness. 19700f900049STejun Heo */ 19710f900049STejun Heo ptr = __alloc_percpu(size + align + sizeof(void *), 1); 19720f900049STejun Heo cwqs = PTR_ALIGN(ptr, align); 19730f900049STejun Heo *(void **)per_cpu_ptr(cwqs + 1, 0) = ptr; 19740f900049STejun Heo #else 19750f900049STejun Heo /* On SMP, percpu allocator can do it itself */ 19760f900049STejun Heo cwqs = __alloc_percpu(size, align); 19770f900049STejun Heo #endif 19780f900049STejun Heo /* just in case, make sure it's actually aligned */ 19790f900049STejun Heo BUG_ON(!IS_ALIGNED((unsigned long)cwqs, align)); 19800f900049STejun Heo return cwqs; 19810f900049STejun Heo } 19820f900049STejun Heo 19830f900049STejun Heo static void free_cwqs(struct cpu_workqueue_struct *cwqs) 19840f900049STejun Heo { 19850f900049STejun Heo #ifndef CONFIG_SMP 19860f900049STejun Heo /* on UP, the pointer to free is stored right after the cwq */ 19870f900049STejun Heo if (cwqs) 19880f900049STejun Heo free_percpu(*(void **)per_cpu_ptr(cwqs + 1, 0)); 19890f900049STejun Heo #else 19900f900049STejun Heo free_percpu(cwqs); 19910f900049STejun Heo #endif 19920f900049STejun Heo } 19930f900049STejun Heo 19944e6045f1SJohannes Berg struct workqueue_struct *__create_workqueue_key(const char *name, 199597e37d7bSTejun Heo unsigned int flags, 19961e19ffc6STejun Heo int max_active, 1997eb13ba87SJohannes Berg struct lock_class_key *key, 1998eb13ba87SJohannes Berg const char *lock_name) 19993af24433SOleg Nesterov { 20003af24433SOleg Nesterov struct workqueue_struct *wq; 2001c34056a3STejun Heo bool failed = false; 2002c34056a3STejun Heo unsigned int cpu; 20033af24433SOleg Nesterov 20041e19ffc6STejun Heo max_active = clamp_val(max_active, 1, INT_MAX); 20051e19ffc6STejun Heo 20063af24433SOleg Nesterov wq = kzalloc(sizeof(*wq), GFP_KERNEL); 20073af24433SOleg Nesterov if (!wq) 20084690c4abSTejun Heo goto err; 20093af24433SOleg Nesterov 20100f900049STejun Heo wq->cpu_wq = alloc_cwqs(); 20114690c4abSTejun Heo if (!wq->cpu_wq) 20124690c4abSTejun Heo goto err; 20133af24433SOleg Nesterov 201497e37d7bSTejun Heo wq->flags = flags; 2015a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 201673f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 201773f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 201873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 201973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 2020502ca9d8STejun Heo wq->single_cpu = NR_CPUS; 2021502ca9d8STejun Heo 20223af24433SOleg Nesterov wq->name = name; 2023eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 2024cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 20253af24433SOleg Nesterov 20263da1c84cSOleg Nesterov cpu_maps_update_begin(); 20276af8bf3dSOleg Nesterov /* 20286af8bf3dSOleg Nesterov * We must initialize cwqs for each possible cpu even if we 20296af8bf3dSOleg Nesterov * are going to call destroy_workqueue() finally. Otherwise 20306af8bf3dSOleg Nesterov * cpu_up() can hit the uninitialized cwq once we drop the 20316af8bf3dSOleg Nesterov * lock. 20326af8bf3dSOleg Nesterov */ 20333af24433SOleg Nesterov for_each_possible_cpu(cpu) { 20341537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 20358b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 20361537663fSTejun Heo 20370f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 20388b03ae3cSTejun Heo cwq->gcwq = gcwq; 2039c34056a3STejun Heo cwq->wq = wq; 204073f53c4aSTejun Heo cwq->flush_color = -1; 20411e19ffc6STejun Heo cwq->max_active = max_active; 20421e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 20431537663fSTejun Heo 2044c34056a3STejun Heo if (failed) 20453af24433SOleg Nesterov continue; 20467e11629dSTejun Heo cwq->worker = create_worker(gcwq, cpu_online(cpu)); 2047c34056a3STejun Heo if (cwq->worker) 2048c34056a3STejun Heo start_worker(cwq->worker); 20491537663fSTejun Heo else 2050c34056a3STejun Heo failed = true; 20513af24433SOleg Nesterov } 20521537663fSTejun Heo 2053a0a1a5fdSTejun Heo /* 2054a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 2055a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 2056a0a1a5fdSTejun Heo * workqueue to workqueues list. 2057a0a1a5fdSTejun Heo */ 20581537663fSTejun Heo spin_lock(&workqueue_lock); 2059a0a1a5fdSTejun Heo 2060a0a1a5fdSTejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZEABLE) 2061a0a1a5fdSTejun Heo for_each_possible_cpu(cpu) 2062a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 2063a0a1a5fdSTejun Heo 20641537663fSTejun Heo list_add(&wq->list, &workqueues); 2065a0a1a5fdSTejun Heo 20661537663fSTejun Heo spin_unlock(&workqueue_lock); 20671537663fSTejun Heo 20683da1c84cSOleg Nesterov cpu_maps_update_done(); 20693af24433SOleg Nesterov 2070c34056a3STejun Heo if (failed) { 20713af24433SOleg Nesterov destroy_workqueue(wq); 20723af24433SOleg Nesterov wq = NULL; 20733af24433SOleg Nesterov } 20743af24433SOleg Nesterov return wq; 20754690c4abSTejun Heo err: 20764690c4abSTejun Heo if (wq) { 20770f900049STejun Heo free_cwqs(wq->cpu_wq); 20784690c4abSTejun Heo kfree(wq); 20794690c4abSTejun Heo } 20804690c4abSTejun Heo return NULL; 20813af24433SOleg Nesterov } 20824e6045f1SJohannes Berg EXPORT_SYMBOL_GPL(__create_workqueue_key); 20833af24433SOleg Nesterov 20843af24433SOleg Nesterov /** 20853af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 20863af24433SOleg Nesterov * @wq: target workqueue 20873af24433SOleg Nesterov * 20883af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 20893af24433SOleg Nesterov */ 20903af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 20913af24433SOleg Nesterov { 2092c8e55f36STejun Heo unsigned int cpu; 20933af24433SOleg Nesterov 2094a0a1a5fdSTejun Heo flush_workqueue(wq); 2095a0a1a5fdSTejun Heo 2096a0a1a5fdSTejun Heo /* 2097a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 2098a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 2099a0a1a5fdSTejun Heo */ 21003da1c84cSOleg Nesterov cpu_maps_update_begin(); 210195402b38SGautham R Shenoy spin_lock(&workqueue_lock); 21023af24433SOleg Nesterov list_del(&wq->list); 210395402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 21043da1c84cSOleg Nesterov cpu_maps_update_done(); 21053af24433SOleg Nesterov 210673f53c4aSTejun Heo for_each_possible_cpu(cpu) { 210773f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 21087e11629dSTejun Heo struct global_cwq *gcwq = cwq->gcwq; 210973f53c4aSTejun Heo int i; 211073f53c4aSTejun Heo 2111c34056a3STejun Heo if (cwq->worker) { 21127e11629dSTejun Heo retry: 21137e11629dSTejun Heo spin_lock_irq(&gcwq->lock); 21147e11629dSTejun Heo /* 21157e11629dSTejun Heo * Worker can only be destroyed while idle. 21167e11629dSTejun Heo * Wait till it becomes idle. This is ugly 21177e11629dSTejun Heo * and prone to starvation. It will go away 21187e11629dSTejun Heo * once dynamic worker pool is implemented. 21197e11629dSTejun Heo */ 21207e11629dSTejun Heo if (!(cwq->worker->flags & WORKER_IDLE)) { 21217e11629dSTejun Heo spin_unlock_irq(&gcwq->lock); 21227e11629dSTejun Heo msleep(100); 21237e11629dSTejun Heo goto retry; 21247e11629dSTejun Heo } 2125c34056a3STejun Heo destroy_worker(cwq->worker); 2126c34056a3STejun Heo cwq->worker = NULL; 21277e11629dSTejun Heo spin_unlock_irq(&gcwq->lock); 212873f53c4aSTejun Heo } 212973f53c4aSTejun Heo 213073f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 213173f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 21321e19ffc6STejun Heo BUG_ON(cwq->nr_active); 21331e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 213473f53c4aSTejun Heo } 21351537663fSTejun Heo 21360f900049STejun Heo free_cwqs(wq->cpu_wq); 21373af24433SOleg Nesterov kfree(wq); 21383af24433SOleg Nesterov } 21393af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 21403af24433SOleg Nesterov 2141db7bccf4STejun Heo /* 2142db7bccf4STejun Heo * CPU hotplug. 2143db7bccf4STejun Heo * 2144db7bccf4STejun Heo * CPU hotplug is implemented by allowing cwqs to be detached from 2145db7bccf4STejun Heo * CPU, running with unbound workers and allowing them to be 2146db7bccf4STejun Heo * reattached later if the cpu comes back online. A separate thread 2147db7bccf4STejun Heo * is created to govern cwqs in such state and is called the trustee. 2148db7bccf4STejun Heo * 2149db7bccf4STejun Heo * Trustee states and their descriptions. 2150db7bccf4STejun Heo * 2151db7bccf4STejun Heo * START Command state used on startup. On CPU_DOWN_PREPARE, a 2152db7bccf4STejun Heo * new trustee is started with this state. 2153db7bccf4STejun Heo * 2154db7bccf4STejun Heo * IN_CHARGE Once started, trustee will enter this state after 2155db7bccf4STejun Heo * making all existing workers rogue. DOWN_PREPARE waits 2156db7bccf4STejun Heo * for trustee to enter this state. After reaching 2157db7bccf4STejun Heo * IN_CHARGE, trustee tries to execute the pending 2158db7bccf4STejun Heo * worklist until it's empty and the state is set to 2159db7bccf4STejun Heo * BUTCHER, or the state is set to RELEASE. 2160db7bccf4STejun Heo * 2161db7bccf4STejun Heo * BUTCHER Command state which is set by the cpu callback after 2162db7bccf4STejun Heo * the cpu has went down. Once this state is set trustee 2163db7bccf4STejun Heo * knows that there will be no new works on the worklist 2164db7bccf4STejun Heo * and once the worklist is empty it can proceed to 2165db7bccf4STejun Heo * killing idle workers. 2166db7bccf4STejun Heo * 2167db7bccf4STejun Heo * RELEASE Command state which is set by the cpu callback if the 2168db7bccf4STejun Heo * cpu down has been canceled or it has come online 2169db7bccf4STejun Heo * again. After recognizing this state, trustee stops 2170db7bccf4STejun Heo * trying to drain or butcher and transits to DONE. 2171db7bccf4STejun Heo * 2172db7bccf4STejun Heo * DONE Trustee will enter this state after BUTCHER or RELEASE 2173db7bccf4STejun Heo * is complete. 2174db7bccf4STejun Heo * 2175db7bccf4STejun Heo * trustee CPU draining 2176db7bccf4STejun Heo * took over down complete 2177db7bccf4STejun Heo * START -----------> IN_CHARGE -----------> BUTCHER -----------> DONE 2178db7bccf4STejun Heo * | | ^ 2179db7bccf4STejun Heo * | CPU is back online v return workers | 2180db7bccf4STejun Heo * ----------------> RELEASE -------------- 2181db7bccf4STejun Heo */ 2182db7bccf4STejun Heo 2183db7bccf4STejun Heo /** 2184db7bccf4STejun Heo * trustee_wait_event_timeout - timed event wait for trustee 2185db7bccf4STejun Heo * @cond: condition to wait for 2186db7bccf4STejun Heo * @timeout: timeout in jiffies 2187db7bccf4STejun Heo * 2188db7bccf4STejun Heo * wait_event_timeout() for trustee to use. Handles locking and 2189db7bccf4STejun Heo * checks for RELEASE request. 2190db7bccf4STejun Heo * 2191db7bccf4STejun Heo * CONTEXT: 2192db7bccf4STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2193db7bccf4STejun Heo * multiple times. To be used by trustee. 2194db7bccf4STejun Heo * 2195db7bccf4STejun Heo * RETURNS: 2196db7bccf4STejun Heo * Positive indicating left time if @cond is satisfied, 0 if timed 2197db7bccf4STejun Heo * out, -1 if canceled. 2198db7bccf4STejun Heo */ 2199db7bccf4STejun Heo #define trustee_wait_event_timeout(cond, timeout) ({ \ 2200db7bccf4STejun Heo long __ret = (timeout); \ 2201db7bccf4STejun Heo while (!((cond) || (gcwq->trustee_state == TRUSTEE_RELEASE)) && \ 2202db7bccf4STejun Heo __ret) { \ 2203db7bccf4STejun Heo spin_unlock_irq(&gcwq->lock); \ 2204db7bccf4STejun Heo __wait_event_timeout(gcwq->trustee_wait, (cond) || \ 2205db7bccf4STejun Heo (gcwq->trustee_state == TRUSTEE_RELEASE), \ 2206db7bccf4STejun Heo __ret); \ 2207db7bccf4STejun Heo spin_lock_irq(&gcwq->lock); \ 2208db7bccf4STejun Heo } \ 2209db7bccf4STejun Heo gcwq->trustee_state == TRUSTEE_RELEASE ? -1 : (__ret); \ 2210db7bccf4STejun Heo }) 2211db7bccf4STejun Heo 2212db7bccf4STejun Heo /** 2213db7bccf4STejun Heo * trustee_wait_event - event wait for trustee 2214db7bccf4STejun Heo * @cond: condition to wait for 2215db7bccf4STejun Heo * 2216db7bccf4STejun Heo * wait_event() for trustee to use. Automatically handles locking and 2217db7bccf4STejun Heo * checks for CANCEL request. 2218db7bccf4STejun Heo * 2219db7bccf4STejun Heo * CONTEXT: 2220db7bccf4STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2221db7bccf4STejun Heo * multiple times. To be used by trustee. 2222db7bccf4STejun Heo * 2223db7bccf4STejun Heo * RETURNS: 2224db7bccf4STejun Heo * 0 if @cond is satisfied, -1 if canceled. 2225db7bccf4STejun Heo */ 2226db7bccf4STejun Heo #define trustee_wait_event(cond) ({ \ 2227db7bccf4STejun Heo long __ret1; \ 2228db7bccf4STejun Heo __ret1 = trustee_wait_event_timeout(cond, MAX_SCHEDULE_TIMEOUT);\ 2229db7bccf4STejun Heo __ret1 < 0 ? -1 : 0; \ 2230db7bccf4STejun Heo }) 2231db7bccf4STejun Heo 2232db7bccf4STejun Heo static int __cpuinit trustee_thread(void *__gcwq) 2233db7bccf4STejun Heo { 2234db7bccf4STejun Heo struct global_cwq *gcwq = __gcwq; 2235db7bccf4STejun Heo struct worker *worker; 2236db7bccf4STejun Heo struct hlist_node *pos; 2237db7bccf4STejun Heo int i; 2238db7bccf4STejun Heo 2239db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 2240db7bccf4STejun Heo 2241db7bccf4STejun Heo spin_lock_irq(&gcwq->lock); 2242db7bccf4STejun Heo /* 2243502ca9d8STejun Heo * Make all workers rogue. Trustee must be bound to the 2244502ca9d8STejun Heo * target cpu and can't be cancelled. 2245db7bccf4STejun Heo */ 2246db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 2247db7bccf4STejun Heo 2248db7bccf4STejun Heo list_for_each_entry(worker, &gcwq->idle_list, entry) 2249*d302f017STejun Heo worker_set_flags(worker, WORKER_ROGUE, false); 2250db7bccf4STejun Heo 2251db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 2252*d302f017STejun Heo worker_set_flags(worker, WORKER_ROGUE, false); 2253db7bccf4STejun Heo 2254db7bccf4STejun Heo /* 2255db7bccf4STejun Heo * We're now in charge. Notify and proceed to drain. We need 2256db7bccf4STejun Heo * to keep the gcwq running during the whole CPU down 2257db7bccf4STejun Heo * procedure as other cpu hotunplug callbacks may need to 2258db7bccf4STejun Heo * flush currently running tasks. 2259db7bccf4STejun Heo */ 2260db7bccf4STejun Heo gcwq->trustee_state = TRUSTEE_IN_CHARGE; 2261db7bccf4STejun Heo wake_up_all(&gcwq->trustee_wait); 2262db7bccf4STejun Heo 2263db7bccf4STejun Heo /* 2264db7bccf4STejun Heo * The original cpu is in the process of dying and may go away 2265db7bccf4STejun Heo * anytime now. When that happens, we and all workers would 2266db7bccf4STejun Heo * be migrated to other cpus. Try draining any left work. 2267db7bccf4STejun Heo * Note that if the gcwq is frozen, there may be frozen works 2268db7bccf4STejun Heo * in freezeable cwqs. Don't declare completion while frozen. 2269db7bccf4STejun Heo */ 2270db7bccf4STejun Heo while (gcwq->nr_workers != gcwq->nr_idle || 2271db7bccf4STejun Heo gcwq->flags & GCWQ_FREEZING || 2272db7bccf4STejun Heo gcwq->trustee_state == TRUSTEE_IN_CHARGE) { 2273db7bccf4STejun Heo /* give a breather */ 2274db7bccf4STejun Heo if (trustee_wait_event_timeout(false, TRUSTEE_COOLDOWN) < 0) 2275db7bccf4STejun Heo break; 2276db7bccf4STejun Heo } 2277db7bccf4STejun Heo 2278db7bccf4STejun Heo /* notify completion */ 2279db7bccf4STejun Heo gcwq->trustee = NULL; 2280db7bccf4STejun Heo gcwq->trustee_state = TRUSTEE_DONE; 2281db7bccf4STejun Heo wake_up_all(&gcwq->trustee_wait); 2282db7bccf4STejun Heo spin_unlock_irq(&gcwq->lock); 2283db7bccf4STejun Heo return 0; 2284db7bccf4STejun Heo } 2285db7bccf4STejun Heo 2286db7bccf4STejun Heo /** 2287db7bccf4STejun Heo * wait_trustee_state - wait for trustee to enter the specified state 2288db7bccf4STejun Heo * @gcwq: gcwq the trustee of interest belongs to 2289db7bccf4STejun Heo * @state: target state to wait for 2290db7bccf4STejun Heo * 2291db7bccf4STejun Heo * Wait for the trustee to reach @state. DONE is already matched. 2292db7bccf4STejun Heo * 2293db7bccf4STejun Heo * CONTEXT: 2294db7bccf4STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2295db7bccf4STejun Heo * multiple times. To be used by cpu_callback. 2296db7bccf4STejun Heo */ 2297db7bccf4STejun Heo static void __cpuinit wait_trustee_state(struct global_cwq *gcwq, int state) 2298db7bccf4STejun Heo { 2299db7bccf4STejun Heo if (!(gcwq->trustee_state == state || 2300db7bccf4STejun Heo gcwq->trustee_state == TRUSTEE_DONE)) { 2301db7bccf4STejun Heo spin_unlock_irq(&gcwq->lock); 2302db7bccf4STejun Heo __wait_event(gcwq->trustee_wait, 2303db7bccf4STejun Heo gcwq->trustee_state == state || 2304db7bccf4STejun Heo gcwq->trustee_state == TRUSTEE_DONE); 2305db7bccf4STejun Heo spin_lock_irq(&gcwq->lock); 2306db7bccf4STejun Heo } 2307db7bccf4STejun Heo } 2308db7bccf4STejun Heo 23099c7b216dSChandra Seetharaman static int __devinit workqueue_cpu_callback(struct notifier_block *nfb, 23101da177e4SLinus Torvalds unsigned long action, 23111da177e4SLinus Torvalds void *hcpu) 23121da177e4SLinus Torvalds { 23133af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 2314db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 2315db7bccf4STejun Heo struct task_struct *new_trustee = NULL; 2316db7bccf4STejun Heo struct worker *worker; 2317db7bccf4STejun Heo struct hlist_node *pos; 2318db7bccf4STejun Heo unsigned long flags; 2319db7bccf4STejun Heo int i; 23201da177e4SLinus Torvalds 23218bb78442SRafael J. Wysocki action &= ~CPU_TASKS_FROZEN; 23228bb78442SRafael J. Wysocki 2323db7bccf4STejun Heo switch (action) { 2324db7bccf4STejun Heo case CPU_DOWN_PREPARE: 2325db7bccf4STejun Heo new_trustee = kthread_create(trustee_thread, gcwq, 2326db7bccf4STejun Heo "workqueue_trustee/%d\n", cpu); 2327db7bccf4STejun Heo if (IS_ERR(new_trustee)) 2328db7bccf4STejun Heo return notifier_from_errno(PTR_ERR(new_trustee)); 2329db7bccf4STejun Heo kthread_bind(new_trustee, cpu); 2330db7bccf4STejun Heo } 23311537663fSTejun Heo 2332db7bccf4STejun Heo /* some are called w/ irq disabled, don't disturb irq status */ 2333db7bccf4STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 23343af24433SOleg Nesterov 23353af24433SOleg Nesterov switch (action) { 2336db7bccf4STejun Heo case CPU_DOWN_PREPARE: 2337db7bccf4STejun Heo /* initialize trustee and tell it to acquire the gcwq */ 2338db7bccf4STejun Heo BUG_ON(gcwq->trustee || gcwq->trustee_state != TRUSTEE_DONE); 2339db7bccf4STejun Heo gcwq->trustee = new_trustee; 2340db7bccf4STejun Heo gcwq->trustee_state = TRUSTEE_START; 2341db7bccf4STejun Heo wake_up_process(gcwq->trustee); 2342db7bccf4STejun Heo wait_trustee_state(gcwq, TRUSTEE_IN_CHARGE); 2343db7bccf4STejun Heo break; 2344db7bccf4STejun Heo 23453da1c84cSOleg Nesterov case CPU_POST_DEAD: 2346db7bccf4STejun Heo gcwq->trustee_state = TRUSTEE_BUTCHER; 2347db7bccf4STejun Heo break; 2348db7bccf4STejun Heo 2349db7bccf4STejun Heo case CPU_DOWN_FAILED: 2350db7bccf4STejun Heo case CPU_ONLINE: 2351db7bccf4STejun Heo if (gcwq->trustee_state != TRUSTEE_DONE) { 2352db7bccf4STejun Heo gcwq->trustee_state = TRUSTEE_RELEASE; 2353db7bccf4STejun Heo wake_up_process(gcwq->trustee); 2354db7bccf4STejun Heo wait_trustee_state(gcwq, TRUSTEE_DONE); 2355db7bccf4STejun Heo } 2356db7bccf4STejun Heo 2357502ca9d8STejun Heo /* clear ROGUE from all workers */ 2358db7bccf4STejun Heo list_for_each_entry(worker, &gcwq->idle_list, entry) 2359*d302f017STejun Heo worker_clr_flags(worker, WORKER_ROGUE); 2360db7bccf4STejun Heo 2361db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 2362*d302f017STejun Heo worker_clr_flags(worker, WORKER_ROGUE); 23631da177e4SLinus Torvalds break; 23641da177e4SLinus Torvalds } 2365db7bccf4STejun Heo 2366db7bccf4STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 23671da177e4SLinus Torvalds 23681537663fSTejun Heo return notifier_from_errno(0); 23691da177e4SLinus Torvalds } 23701da177e4SLinus Torvalds 23712d3854a3SRusty Russell #ifdef CONFIG_SMP 23728ccad40dSRusty Russell 23732d3854a3SRusty Russell struct work_for_cpu { 23746b44003eSAndrew Morton struct completion completion; 23752d3854a3SRusty Russell long (*fn)(void *); 23762d3854a3SRusty Russell void *arg; 23772d3854a3SRusty Russell long ret; 23782d3854a3SRusty Russell }; 23792d3854a3SRusty Russell 23806b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 23812d3854a3SRusty Russell { 23826b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 23832d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 23846b44003eSAndrew Morton complete(&wfc->completion); 23856b44003eSAndrew Morton return 0; 23862d3854a3SRusty Russell } 23872d3854a3SRusty Russell 23882d3854a3SRusty Russell /** 23892d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 23902d3854a3SRusty Russell * @cpu: the cpu to run on 23912d3854a3SRusty Russell * @fn: the function to run 23922d3854a3SRusty Russell * @arg: the function arg 23932d3854a3SRusty Russell * 239431ad9081SRusty Russell * This will return the value @fn returns. 239531ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 23966b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 23972d3854a3SRusty Russell */ 23982d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 23992d3854a3SRusty Russell { 24006b44003eSAndrew Morton struct task_struct *sub_thread; 24016b44003eSAndrew Morton struct work_for_cpu wfc = { 24026b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 24036b44003eSAndrew Morton .fn = fn, 24046b44003eSAndrew Morton .arg = arg, 24056b44003eSAndrew Morton }; 24062d3854a3SRusty Russell 24076b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 24086b44003eSAndrew Morton if (IS_ERR(sub_thread)) 24096b44003eSAndrew Morton return PTR_ERR(sub_thread); 24106b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 24116b44003eSAndrew Morton wake_up_process(sub_thread); 24126b44003eSAndrew Morton wait_for_completion(&wfc.completion); 24132d3854a3SRusty Russell return wfc.ret; 24142d3854a3SRusty Russell } 24152d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 24162d3854a3SRusty Russell #endif /* CONFIG_SMP */ 24172d3854a3SRusty Russell 2418a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 2419a0a1a5fdSTejun Heo 2420a0a1a5fdSTejun Heo /** 2421a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 2422a0a1a5fdSTejun Heo * 2423a0a1a5fdSTejun Heo * Start freezing workqueues. After this function returns, all 2424a0a1a5fdSTejun Heo * freezeable workqueues will queue new works to their frozen_works 24257e11629dSTejun Heo * list instead of gcwq->worklist. 2426a0a1a5fdSTejun Heo * 2427a0a1a5fdSTejun Heo * CONTEXT: 24288b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 2429a0a1a5fdSTejun Heo */ 2430a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 2431a0a1a5fdSTejun Heo { 2432a0a1a5fdSTejun Heo struct workqueue_struct *wq; 2433a0a1a5fdSTejun Heo unsigned int cpu; 2434a0a1a5fdSTejun Heo 2435a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 2436a0a1a5fdSTejun Heo 2437a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 2438a0a1a5fdSTejun Heo workqueue_freezing = true; 2439a0a1a5fdSTejun Heo 2440a0a1a5fdSTejun Heo for_each_possible_cpu(cpu) { 24418b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 24428b03ae3cSTejun Heo 24438b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 24448b03ae3cSTejun Heo 2445db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 2446db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 2447db7bccf4STejun Heo 2448a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 2449a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2450a0a1a5fdSTejun Heo 2451a0a1a5fdSTejun Heo if (wq->flags & WQ_FREEZEABLE) 2452a0a1a5fdSTejun Heo cwq->max_active = 0; 2453a0a1a5fdSTejun Heo } 24548b03ae3cSTejun Heo 24558b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 2456a0a1a5fdSTejun Heo } 2457a0a1a5fdSTejun Heo 2458a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 2459a0a1a5fdSTejun Heo } 2460a0a1a5fdSTejun Heo 2461a0a1a5fdSTejun Heo /** 2462a0a1a5fdSTejun Heo * freeze_workqueues_busy - are freezeable workqueues still busy? 2463a0a1a5fdSTejun Heo * 2464a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 2465a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 2466a0a1a5fdSTejun Heo * 2467a0a1a5fdSTejun Heo * CONTEXT: 2468a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 2469a0a1a5fdSTejun Heo * 2470a0a1a5fdSTejun Heo * RETURNS: 2471a0a1a5fdSTejun Heo * %true if some freezeable workqueues are still busy. %false if 2472a0a1a5fdSTejun Heo * freezing is complete. 2473a0a1a5fdSTejun Heo */ 2474a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 2475a0a1a5fdSTejun Heo { 2476a0a1a5fdSTejun Heo struct workqueue_struct *wq; 2477a0a1a5fdSTejun Heo unsigned int cpu; 2478a0a1a5fdSTejun Heo bool busy = false; 2479a0a1a5fdSTejun Heo 2480a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 2481a0a1a5fdSTejun Heo 2482a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 2483a0a1a5fdSTejun Heo 2484a0a1a5fdSTejun Heo for_each_possible_cpu(cpu) { 2485a0a1a5fdSTejun Heo /* 2486a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 2487a0a1a5fdSTejun Heo * to peek without lock. 2488a0a1a5fdSTejun Heo */ 2489a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 2490a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2491a0a1a5fdSTejun Heo 2492a0a1a5fdSTejun Heo if (!(wq->flags & WQ_FREEZEABLE)) 2493a0a1a5fdSTejun Heo continue; 2494a0a1a5fdSTejun Heo 2495a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 2496a0a1a5fdSTejun Heo if (cwq->nr_active) { 2497a0a1a5fdSTejun Heo busy = true; 2498a0a1a5fdSTejun Heo goto out_unlock; 2499a0a1a5fdSTejun Heo } 2500a0a1a5fdSTejun Heo } 2501a0a1a5fdSTejun Heo } 2502a0a1a5fdSTejun Heo out_unlock: 2503a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 2504a0a1a5fdSTejun Heo return busy; 2505a0a1a5fdSTejun Heo } 2506a0a1a5fdSTejun Heo 2507a0a1a5fdSTejun Heo /** 2508a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 2509a0a1a5fdSTejun Heo * 2510a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 25117e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 2512a0a1a5fdSTejun Heo * 2513a0a1a5fdSTejun Heo * CONTEXT: 25148b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 2515a0a1a5fdSTejun Heo */ 2516a0a1a5fdSTejun Heo void thaw_workqueues(void) 2517a0a1a5fdSTejun Heo { 2518a0a1a5fdSTejun Heo struct workqueue_struct *wq; 2519a0a1a5fdSTejun Heo unsigned int cpu; 2520a0a1a5fdSTejun Heo 2521a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 2522a0a1a5fdSTejun Heo 2523a0a1a5fdSTejun Heo if (!workqueue_freezing) 2524a0a1a5fdSTejun Heo goto out_unlock; 2525a0a1a5fdSTejun Heo 2526a0a1a5fdSTejun Heo for_each_possible_cpu(cpu) { 25278b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 25288b03ae3cSTejun Heo 25298b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 25308b03ae3cSTejun Heo 2531db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 2532db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 2533db7bccf4STejun Heo 2534a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 2535a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2536a0a1a5fdSTejun Heo 2537a0a1a5fdSTejun Heo if (!(wq->flags & WQ_FREEZEABLE)) 2538a0a1a5fdSTejun Heo continue; 2539a0a1a5fdSTejun Heo 2540a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 2541a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 2542a0a1a5fdSTejun Heo 2543a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 2544a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 2545a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 2546a0a1a5fdSTejun Heo 2547502ca9d8STejun Heo /* perform delayed unbind from single cpu if empty */ 2548502ca9d8STejun Heo if (wq->single_cpu == gcwq->cpu && 2549502ca9d8STejun Heo !cwq->nr_active && list_empty(&cwq->delayed_works)) 2550502ca9d8STejun Heo cwq_unbind_single_cpu(cwq); 2551502ca9d8STejun Heo 2552c8e55f36STejun Heo wake_up_process(cwq->worker->task); 2553a0a1a5fdSTejun Heo } 25548b03ae3cSTejun Heo 25558b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 2556a0a1a5fdSTejun Heo } 2557a0a1a5fdSTejun Heo 2558a0a1a5fdSTejun Heo workqueue_freezing = false; 2559a0a1a5fdSTejun Heo out_unlock: 2560a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 2561a0a1a5fdSTejun Heo } 2562a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 2563a0a1a5fdSTejun Heo 2564c12920d1SOleg Nesterov void __init init_workqueues(void) 25651da177e4SLinus Torvalds { 2566c34056a3STejun Heo unsigned int cpu; 2567c8e55f36STejun Heo int i; 2568c34056a3STejun Heo 25697a22ad75STejun Heo /* 25707a22ad75STejun Heo * The pointer part of work->data is either pointing to the 25717a22ad75STejun Heo * cwq or contains the cpu number the work ran last on. Make 25727a22ad75STejun Heo * sure cpu number won't overflow into kernel pointer area so 25737a22ad75STejun Heo * that they can be distinguished. 25747a22ad75STejun Heo */ 25757a22ad75STejun Heo BUILD_BUG_ON(NR_CPUS << WORK_STRUCT_FLAG_BITS >= PAGE_OFFSET); 25767a22ad75STejun Heo 2577db7bccf4STejun Heo hotcpu_notifier(workqueue_cpu_callback, CPU_PRI_WORKQUEUE); 25788b03ae3cSTejun Heo 25798b03ae3cSTejun Heo /* initialize gcwqs */ 25808b03ae3cSTejun Heo for_each_possible_cpu(cpu) { 25818b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 25828b03ae3cSTejun Heo 25838b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 25847e11629dSTejun Heo INIT_LIST_HEAD(&gcwq->worklist); 25858b03ae3cSTejun Heo gcwq->cpu = cpu; 25868b03ae3cSTejun Heo 2587c8e55f36STejun Heo INIT_LIST_HEAD(&gcwq->idle_list); 2588c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 2589c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 2590c8e55f36STejun Heo 25918b03ae3cSTejun Heo ida_init(&gcwq->worker_ida); 2592db7bccf4STejun Heo 2593db7bccf4STejun Heo gcwq->trustee_state = TRUSTEE_DONE; 2594db7bccf4STejun Heo init_waitqueue_head(&gcwq->trustee_wait); 25958b03ae3cSTejun Heo } 25968b03ae3cSTejun Heo 25971da177e4SLinus Torvalds keventd_wq = create_workqueue("events"); 25981da177e4SLinus Torvalds BUG_ON(!keventd_wq); 25991da177e4SLinus Torvalds } 2600