3796 lines · 104.2 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/hashtable.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_MANAGING_WORKERS   = 1 << 1,       /* managing 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 */ 	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. */ /* struct worker is defined in workqueue_internal.h */ struct worker_pool {	spinlock_t		lock;		/* the pool lock */	unsigned 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		assoc_mutex;	/* protect POOL_DISASSOCIATED */	struct ida		worker_ida;	/* L: for worker IDs */ 	/*	 * 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;} ____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 */}; /* * 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 */}; /* * All cpumasks are assumed to be always set on UP and thus can't be * used to determine whether there's something to be done. */#ifdef CONFIG_SMPtypedef cpumask_var_t mayday_mask_t;#define mayday_test_and_set_cpu(cpu, mask)	\	cpumask_test_and_set_cpu((cpu), (mask))#define mayday_clear_cpu(cpu, mask)		cpumask_clear_cpu((cpu), (mask))#define for_each_mayday_cpu(cpu, mask)		for_each_cpu((cpu), (mask))#define alloc_mayday_mask(maskp, gfp)		zalloc_cpumask_var((maskp), (gfp))#define free_mayday_mask(mask)			free_cpumask_var((mask))#elsetypedef unsigned long mayday_mask_t;#define mayday_test_and_set_cpu(cpu, mask)	test_and_set_bit(0, &(mask))#define mayday_clear_cpu(cpu, mask)		clear_bit(0, &(mask))#define for_each_mayday_cpu(cpu, mask)		if ((cpu) = 0, (mask))#define alloc_mayday_mask(maskp, gfp)		true#define free_mayday_mask(mask)			do { } while (0)#endif /* * The externally visible workqueue abstraction is an array of * per-CPU workqueues: */struct workqueue_struct {	unsigned int		flags;		/* W: WQ_* flags */	union {		struct pool_workqueue __percpu		*pcpu;		struct pool_workqueue			*single;		unsigned long				v;	} pool_wq;				/* I: pwq's */	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 */ 	mayday_mask_t		mayday_mask;	/* cpus 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 */}; 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 for_each_std_worker_pool(pool, cpu)				\	for ((pool) = &std_worker_pools(cpu)[0];			\	     (pool) < &std_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) static inline int __next_wq_cpu(int cpu, const struct cpumask *mask,				unsigned int sw){	if (cpu < nr_cpu_ids) {		if (sw & 1) {			cpu = cpumask_next(cpu, mask);			if (cpu < nr_cpu_ids)				return cpu;		}		if (sw & 2)			return WORK_CPU_UNBOUND;	}	return WORK_CPU_END;} static inline int __next_pwq_cpu(int cpu, const struct cpumask *mask,				 struct workqueue_struct *wq){	return __next_wq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2);} /* * CPU iterators * * An extra cpu number is defined using an invalid cpu number * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any * specific CPU.  The following iterators are similar to for_each_*_cpu() * iterators but also considers the unbound CPU. * * for_each_wq_cpu()		: possible CPUs + WORK_CPU_UNBOUND * for_each_online_wq_cpu()	: online CPUs + WORK_CPU_UNBOUND * for_each_pwq_cpu()		: possible CPUs for bound workqueues, *				  WORK_CPU_UNBOUND for unbound workqueues */#define for_each_wq_cpu(cpu)						\	for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, 3);		\	     (cpu) < WORK_CPU_END;					\	     (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, 3)) #define for_each_online_wq_cpu(cpu)					\	for ((cpu) = __next_wq_cpu(-1, cpu_online_mask, 3);		\	     (cpu) < WORK_CPU_END;					\	     (cpu) = __next_wq_cpu((cpu), cpu_online_mask, 3)) #define for_each_pwq_cpu(cpu, wq)					\	for ((cpu) = __next_pwq_cpu(-1, cpu_possible_mask, (wq));	\	     (cpu) < WORK_CPU_END;					\	     (cpu) = __next_pwq_cpu((cpu), cpu_possible_mask, (wq))) #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_std_worker_pools);static struct worker_pool unbound_std_worker_pools[NR_STD_WORKER_POOLS]; /* idr of all pools */static DEFINE_MUTEX(worker_pool_idr_mutex);static DEFINE_IDR(worker_pool_idr); static int worker_thread(void *__worker); static struct worker_pool *std_worker_pools(int cpu){	if (cpu != WORK_CPU_UNBOUND)		return per_cpu(cpu_std_worker_pools, cpu);	else		return unbound_std_worker_pools;} static int std_worker_pool_pri(struct worker_pool *pool){	return pool - std_worker_pools(pool->cpu);} /* allocate ID and assign it to @pool */static int worker_pool_assign_id(struct worker_pool *pool){	int ret; 	mutex_lock(&worker_pool_idr_mutex);	ret = idr_alloc(&worker_pool_idr, pool, 0, 0, GFP_KERNEL);	if (ret >= 0)		pool->id = ret;	mutex_unlock(&worker_pool_idr_mutex); 	return ret < 0 ? ret : 0;} /* * Lookup worker_pool by id.  The idr currently is built during boot and * never modified.  Don't worry about locking for now. */static struct worker_pool *worker_pool_by_id(int pool_id){	return idr_find(&worker_pool_idr, pool_id);} static struct worker_pool *get_std_worker_pool(int cpu, bool highpri){	struct worker_pool *pools = std_worker_pools(cpu); 	return &pools[highpri];} static struct pool_workqueue *get_pwq(unsigned int cpu,				      struct workqueue_struct *wq){	if (!(wq->flags & WQ_UNBOUND)) {		if (likely(cpu < nr_cpu_ids))			return per_cpu_ptr(wq->pool_wq.pcpu, cpu);	} else if (likely(cpu == WORK_CPU_UNBOUND))		return wq->pool_wq.single;	return NULL;} static unsigned int work_color_to_flags(int color){	return color << WORK_STRUCT_COLOR_SHIFT;} static int get_work_color(struct work_struct *work)

Lines 1–500 of 3796.