4074 lines · 110.7 KB · C
RawDownload
/* * kernel/workqueue.c - generic async execution with shared worker pool * * Copyright (C) 2002		Ingo Molnar * *   Derived from the taskqueue/keventd code by: *     David Woodhouse <dwmw2@infradead.org> *     Andrew Morton *     Kai Petzke <wpp@marie.physik.tu-berlin.de> *     Theodore Ts'o <tytso@mit.edu> * * Made to use alloc_percpu by Christoph Lameter. * * Copyright (C) 2010		SUSE Linux Products GmbH * Copyright (C) 2010		Tejun Heo <tj@kernel.org> * * This is the generic async execution mechanism.  Work items as are * executed in process context.  The worker pool is shared and * automatically managed.  There is one worker pool for each CPU and * one extra for works which are better served by workers which are * not bound to any specific CPU. * * Please read Documentation/workqueue.txt for details. */ #include <linux/export.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/init.h>#include <linux/signal.h>#include <linux/completion.h>#include <linux/workqueue.h>#include <linux/slab.h>#include <linux/cpu.h>#include <linux/notifier.h>#include <linux/kthread.h>#include <linux/hardirq.h>#include <linux/mempolicy.h>#include <linux/freezer.h>#include <linux/kallsyms.h>#include <linux/debug_locks.h>#include <linux/lockdep.h>#include <linux/idr.h>#include <linux/jhash.h>#include <linux/hashtable.h>#include <linux/rculist.h> #include "workqueue_internal.h" enum {	/*	 * worker_pool flags	 *	 * A bound pool is either associated or disassociated with its CPU.	 * While associated (!DISASSOCIATED), all workers are bound to the	 * CPU and none has %WORKER_UNBOUND set and concurrency management	 * is in effect.	 *	 * While DISASSOCIATED, the cpu may be offline and all workers have	 * %WORKER_UNBOUND set and concurrency management disabled, and may	 * be executing on any CPU.  The pool behaves as an unbound one.	 *	 * Note that DISASSOCIATED can be flipped only while holding	 * assoc_mutex to avoid changing binding state while	 * create_worker() is in progress.	 */	POOL_MANAGE_WORKERS	= 1 << 0,	/* need to manage workers */	POOL_DISASSOCIATED	= 1 << 2,	/* cpu can't serve workers */	POOL_FREEZING		= 1 << 3,	/* freeze in progress */ 	/* worker flags */	WORKER_STARTED		= 1 << 0,	/* started */	WORKER_DIE		= 1 << 1,	/* die die die */	WORKER_IDLE		= 1 << 2,	/* is idle */	WORKER_PREP		= 1 << 3,	/* preparing to run works */	WORKER_CPU_INTENSIVE	= 1 << 6,	/* cpu intensive */	WORKER_UNBOUND		= 1 << 7,	/* worker is unbound */ 	WORKER_NOT_RUNNING	= WORKER_PREP | WORKER_UNBOUND |				  WORKER_CPU_INTENSIVE, 	NR_STD_WORKER_POOLS	= 2,		/* # standard pools per cpu */ 	UNBOUND_POOL_HASH_ORDER	= 6,		/* hashed by pool->attrs */	BUSY_WORKER_HASH_ORDER	= 6,		/* 64 pointers */ 	MAX_IDLE_WORKERS_RATIO	= 4,		/* 1/4 of busy can be idle */	IDLE_WORKER_TIMEOUT	= 300 * HZ,	/* keep idle ones for 5 mins */ 	MAYDAY_INITIAL_TIMEOUT  = HZ / 100 >= 2 ? HZ / 100 : 2,						/* call for help after 10ms						   (min two ticks) */	MAYDAY_INTERVAL		= HZ / 10,	/* and then every 100ms */	CREATE_COOLDOWN		= HZ,		/* time to breath after fail */ 	/*	 * Rescue workers are used only on emergencies and shared by	 * all cpus.  Give -20.	 */	RESCUER_NICE_LEVEL	= -20,	HIGHPRI_NICE_LEVEL	= -20,}; /* * Structure fields follow one of the following exclusion rules. * * I: Modifiable by initialization/destruction paths and read-only for *    everyone else. * * P: Preemption protected.  Disabling preemption is enough and should *    only be modified and accessed from the local cpu. * * L: pool->lock protected.  Access with pool->lock held. * * X: During normal operation, modification requires pool->lock and should *    be done only from local cpu.  Either disabling preemption on local *    cpu or grabbing pool->lock is enough for read access.  If *    POOL_DISASSOCIATED is set, it's identical to L. * * F: wq->flush_mutex protected. * * W: workqueue_lock protected. * * R: workqueue_lock protected for writes.  Sched-RCU protected for reads. */ /* struct worker is defined in workqueue_internal.h */ struct worker_pool {	spinlock_t		lock;		/* the pool lock */	int			cpu;		/* I: the associated cpu */	int			id;		/* I: pool ID */	unsigned int		flags;		/* X: flags */ 	struct list_head	worklist;	/* L: list of pending works */	int			nr_workers;	/* L: total number of workers */ 	/* nr_idle includes the ones off idle_list for rebinding */	int			nr_idle;	/* L: currently idle ones */ 	struct list_head	idle_list;	/* X: list of idle workers */	struct timer_list	idle_timer;	/* L: worker idle timeout */	struct timer_list	mayday_timer;	/* L: SOS timer for workers */ 	/* workers are chained either in busy_hash or idle_list */	DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);						/* L: hash of busy workers */ 	struct mutex		manager_arb;	/* manager arbitration */	struct mutex		assoc_mutex;	/* protect POOL_DISASSOCIATED */	struct ida		worker_ida;	/* L: for worker IDs */ 	struct workqueue_attrs	*attrs;		/* I: worker attributes */	struct hlist_node	hash_node;	/* R: unbound_pool_hash node */	int			refcnt;		/* refcnt for unbound pools */ 	/*	 * The current concurrency level.  As it's likely to be accessed	 * from other CPUs during try_to_wake_up(), put it in a separate	 * cacheline.	 */	atomic_t		nr_running ____cacheline_aligned_in_smp; 	/*	 * Destruction of pool is sched-RCU protected to allow dereferences	 * from get_work_pool().	 */	struct rcu_head		rcu;} ____cacheline_aligned_in_smp; /* * The per-pool workqueue.  While queued, the lower WORK_STRUCT_FLAG_BITS * of work_struct->data are used for flags and the remaining high bits * point to the pwq; thus, pwqs need to be aligned at two's power of the * number of flag bits. */struct pool_workqueue {	struct worker_pool	*pool;		/* I: the associated pool */	struct workqueue_struct *wq;		/* I: the owning workqueue */	int			work_color;	/* L: current color */	int			flush_color;	/* L: flushing color */	int			nr_in_flight[WORK_NR_COLORS];						/* L: nr of in_flight works */	int			nr_active;	/* L: nr of active works */	int			max_active;	/* L: max active works */	struct list_head	delayed_works;	/* L: delayed works */	struct list_head	pwqs_node;	/* R: node on wq->pwqs */	struct list_head	mayday_node;	/* W: node on wq->maydays */} __aligned(1 << WORK_STRUCT_FLAG_BITS); /* * Structure used to wait for workqueue flush. */struct wq_flusher {	struct list_head	list;		/* F: list of flushers */	int			flush_color;	/* F: flush color waiting for */	struct completion	done;		/* flush completion */}; /* * The externally visible workqueue abstraction is an array of * per-CPU workqueues: */struct workqueue_struct {	unsigned int		flags;		/* W: WQ_* flags */	struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */	struct list_head	pwqs;		/* R: all pwqs of this wq */	struct list_head	list;		/* W: list of all workqueues */ 	struct mutex		flush_mutex;	/* protects wq flushing */	int			work_color;	/* F: current work color */	int			flush_color;	/* F: current flush color */	atomic_t		nr_pwqs_to_flush; /* flush in progress */	struct wq_flusher	*first_flusher;	/* F: first flusher */	struct list_head	flusher_queue;	/* F: flush waiters */	struct list_head	flusher_overflow; /* F: flush overflow list */ 	struct list_head	maydays;	/* W: pwqs requesting rescue */	struct worker		*rescuer;	/* I: rescue worker */ 	int			nr_drainers;	/* W: drain in progress */	int			saved_max_active; /* W: saved pwq max_active */#ifdef CONFIG_LOCKDEP	struct lockdep_map	lockdep_map;#endif	char			name[];		/* I: workqueue name */}; static struct kmem_cache *pwq_cache; /* hash of all unbound pools keyed by pool->attrs */static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; struct workqueue_struct *system_wq __read_mostly;EXPORT_SYMBOL_GPL(system_wq);struct workqueue_struct *system_highpri_wq __read_mostly;EXPORT_SYMBOL_GPL(system_highpri_wq);struct workqueue_struct *system_long_wq __read_mostly;EXPORT_SYMBOL_GPL(system_long_wq);struct workqueue_struct *system_unbound_wq __read_mostly;EXPORT_SYMBOL_GPL(system_unbound_wq);struct workqueue_struct *system_freezable_wq __read_mostly;EXPORT_SYMBOL_GPL(system_freezable_wq); #define CREATE_TRACE_POINTS#include <trace/events/workqueue.h> #define assert_rcu_or_wq_lock()						\	rcu_lockdep_assert(rcu_read_lock_sched_held() ||		\			   lockdep_is_held(&workqueue_lock),		\			   "sched RCU or workqueue lock should be held") #define for_each_cpu_worker_pool(pool, cpu)				\	for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0];		\	     (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \	     (pool)++) #define for_each_busy_worker(worker, i, pool)				\	hash_for_each(pool->busy_hash, i, worker, hentry) /** * for_each_pool - iterate through all worker_pools in the system * @pool: iteration cursor * @id: integer used for iteration * * This must be called either with workqueue_lock held or sched RCU read * locked.  If the pool needs to be used beyond the locking in effect, the * caller is responsible for guaranteeing that the pool stays online. * * The if/else clause exists only for the lockdep assertion and can be * ignored. */#define for_each_pool(pool, id)						\	idr_for_each_entry(&worker_pool_idr, pool, id)			\		if (({ assert_rcu_or_wq_lock(); false; })) { }		\		else /** * for_each_pwq - iterate through all pool_workqueues of the specified workqueue * @pwq: iteration cursor * @wq: the target workqueue * * This must be called either with workqueue_lock held or sched RCU read * locked.  If the pwq needs to be used beyond the locking in effect, the * caller is responsible for guaranteeing that the pwq stays online. * * The if/else clause exists only for the lockdep assertion and can be * ignored. */#define for_each_pwq(pwq, wq)						\	list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node)		\		if (({ assert_rcu_or_wq_lock(); false; })) { }		\		else #ifdef CONFIG_DEBUG_OBJECTS_WORK static struct debug_obj_descr work_debug_descr; static void *work_debug_hint(void *addr){	return ((struct work_struct *) addr)->func;} /* * fixup_init is called when: * - an active object is initialized */static int work_fixup_init(void *addr, enum debug_obj_state state){	struct work_struct *work = addr; 	switch (state) {	case ODEBUG_STATE_ACTIVE:		cancel_work_sync(work);		debug_object_init(work, &work_debug_descr);		return 1;	default:		return 0;	}} /* * fixup_activate is called when: * - an active object is activated * - an unknown object is activated (might be a statically initialized object) */static int work_fixup_activate(void *addr, enum debug_obj_state state){	struct work_struct *work = addr; 	switch (state) { 	case ODEBUG_STATE_NOTAVAILABLE:		/*		 * This is not really a fixup. The work struct was		 * statically initialized. We just make sure that it		 * is tracked in the object tracker.		 */		if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) {			debug_object_init(work, &work_debug_descr);			debug_object_activate(work, &work_debug_descr);			return 0;		}		WARN_ON_ONCE(1);		return 0; 	case ODEBUG_STATE_ACTIVE:		WARN_ON(1); 	default:		return 0;	}} /* * fixup_free is called when: * - an active object is freed */static int work_fixup_free(void *addr, enum debug_obj_state state){	struct work_struct *work = addr; 	switch (state) {	case ODEBUG_STATE_ACTIVE:		cancel_work_sync(work);		debug_object_free(work, &work_debug_descr);		return 1;	default:		return 0;	}} static struct debug_obj_descr work_debug_descr = {	.name		= "work_struct",	.debug_hint	= work_debug_hint,	.fixup_init	= work_fixup_init,	.fixup_activate	= work_fixup_activate,	.fixup_free	= work_fixup_free,}; static inline void debug_work_activate(struct work_struct *work){	debug_object_activate(work, &work_debug_descr);} static inline void debug_work_deactivate(struct work_struct *work){	debug_object_deactivate(work, &work_debug_descr);} void __init_work(struct work_struct *work, int onstack){	if (onstack)		debug_object_init_on_stack(work, &work_debug_descr);	else		debug_object_init(work, &work_debug_descr);}EXPORT_SYMBOL_GPL(__init_work); void destroy_work_on_stack(struct work_struct *work){	debug_object_free(work, &work_debug_descr);}EXPORT_SYMBOL_GPL(destroy_work_on_stack); #elsestatic inline void debug_work_activate(struct work_struct *work) { }static inline void debug_work_deactivate(struct work_struct *work) { }#endif /* Serializes the accesses to the list of workqueues. */static DEFINE_SPINLOCK(workqueue_lock);static LIST_HEAD(workqueues);static bool workqueue_freezing;		/* W: have wqs started freezing? */ /* * The CPU and unbound standard worker pools.  The unbound ones have * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set. */static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS],				     cpu_worker_pools); /* * idr of all pools.  Modifications are protected by workqueue_lock.  Read * accesses are protected by sched-RCU protected. */static DEFINE_IDR(worker_pool_idr); static int worker_thread(void *__worker); /* allocate ID and assign it to @pool */static int worker_pool_assign_id(struct worker_pool *pool){	int ret; 	do {		if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL))			return -ENOMEM; 		spin_lock_irq(&workqueue_lock);		ret = idr_get_new(&worker_pool_idr, pool, &pool->id);		spin_unlock_irq(&workqueue_lock);	} while (ret == -EAGAIN); 	return ret;} /** * first_pwq - return the first pool_workqueue of the specified workqueue * @wq: the target workqueue * * This must be called either with workqueue_lock held or sched RCU read * locked.  If the pwq needs to be used beyond the locking in effect, the * caller is responsible for guaranteeing that the pwq stays online. */static struct pool_workqueue *first_pwq(struct workqueue_struct *wq){	assert_rcu_or_wq_lock();	return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue,				      pwqs_node);} static unsigned int work_color_to_flags(int color){	return color << WORK_STRUCT_COLOR_SHIFT;} static int get_work_color(struct work_struct *work){	return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) &		((1 << WORK_STRUCT_COLOR_BITS) - 1);} static int work_next_color(int color){	return (color + 1) % WORK_NR_COLORS;} /* * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data * contain the pointer to the queued pwq.  Once execution starts, the flag * is cleared and the high bits contain OFFQ flags and pool ID. * * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() * and clear_work_data() can be used to set the pwq, pool or clear * work->data.  These functions should only be called while the work is * owned - ie. while the PENDING bit is set. * * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq * corresponding to a work.  Pool is available once the work has been * queued anywhere after initialization until it is sync canceled.  pwq is * available only while the work item is queued. * * %WORK_OFFQ_CANCELING is used to mark a work item which is being * canceled.  While being canceled, a work item may have its PENDING set * but stay off timer and worklist for arbitrarily long and nobody should * try to steal the PENDING bit. */

Lines 1–500 of 4074.