8475 lines · 239.9 KB · C
RawDownload
static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], bh_worker_pools); /* the per-cpu worker pools */static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); static DEFINE_IDR(worker_pool_idr);	/* PR: idr of all pools */ /* PL: hash of all unbound pools keyed by pool->attrs */static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); /* I: attributes used when instantiating standard unbound pools on demand */static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; /* I: attributes used when instantiating ordered pools on demand */static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; /* * I: kthread_worker to release pwq's. pwq release needs to be bounced to a * process context while holding a pool lock. Bounce to a dedicated kthread * worker to avoid A-A deadlocks. */static struct kthread_worker *pwq_release_worker __ro_after_init; struct workqueue_struct *system_wq __ro_after_init;EXPORT_SYMBOL(system_wq);struct workqueue_struct *system_percpu_wq __ro_after_init;EXPORT_SYMBOL(system_percpu_wq);struct workqueue_struct *system_highpri_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_highpri_wq);struct workqueue_struct *system_long_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_long_wq);struct workqueue_struct *system_unbound_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_unbound_wq);struct workqueue_struct *system_dfl_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_dfl_wq);struct workqueue_struct *system_freezable_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_freezable_wq);struct workqueue_struct *system_power_efficient_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_power_efficient_wq);struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq);struct workqueue_struct *system_bh_wq;EXPORT_SYMBOL_GPL(system_bh_wq);struct workqueue_struct *system_bh_highpri_wq;EXPORT_SYMBOL_GPL(system_bh_highpri_wq);struct workqueue_struct *system_dfl_long_wq __ro_after_init;EXPORT_SYMBOL_GPL(system_dfl_long_wq); static int worker_thread(void *__worker);static void workqueue_sysfs_unregister(struct workqueue_struct *wq);static void show_pwq(struct pool_workqueue *pwq);static void show_one_worker_pool(struct worker_pool *pool); #define CREATE_TRACE_POINTS#include <trace/events/workqueue.h> #define assert_rcu_or_pool_mutex()					\	RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() &&			\			 !lockdep_is_held(&wq_pool_mutex),		\			 "RCU or wq_pool_mutex should be held") #define for_each_bh_worker_pool(pool, cpu)				\	for ((pool) = &per_cpu(bh_worker_pools, cpu)[0];		\	     (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \	     (pool)++) #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)++) /** * for_each_pool - iterate through all worker_pools in the system * @pool: iteration cursor * @pi: integer used for iteration * * This must be called either with wq_pool_mutex held or 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, pi)						\	idr_for_each_entry(&worker_pool_idr, pool, pi)			\		if (({ assert_rcu_or_pool_mutex(); false; })) { }	\		else /** * for_each_pool_worker - iterate through all workers of a worker_pool * @worker: iteration cursor * @pool: worker_pool to iterate workers of * * This must be called with wq_pool_attach_mutex. * * The if/else clause exists only for the lockdep assertion and can be * ignored. */#define for_each_pool_worker(worker, pool)				\	list_for_each_entry((worker), &(pool)->workers, node)		\		if (({ lockdep_assert_held(&wq_pool_attach_mutex); 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 wq->mutex held or 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,		\				 lockdep_is_held(&(wq->mutex))) #ifdef CONFIG_DEBUG_OBJECTS_WORK static const struct debug_obj_descr work_debug_descr; static void *work_debug_hint(void *addr){	return ((struct work_struct *) addr)->func;} static bool work_is_static_object(void *addr){	struct work_struct *work = addr; 	return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work));} /* * fixup_init is called when: * - an active object is initialized */static bool 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 true;	default:		return false;	}} /* * fixup_free is called when: * - an active object is freed */static bool 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 true;	default:		return false;	}} static const struct debug_obj_descr work_debug_descr = {	.name		= "work_struct",	.debug_hint	= work_debug_hint,	.is_static_object = work_is_static_object,	.fixup_init	= work_fixup_init,	.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); void destroy_delayed_work_on_stack(struct delayed_work *work){	timer_destroy_on_stack(&work->timer);	debug_object_free(&work->work, &work_debug_descr);}EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); #elsestatic inline void debug_work_activate(struct work_struct *work) { }static inline void debug_work_deactivate(struct work_struct *work) { }#endif /** * worker_pool_assign_id - allocate ID and assign it to @pool * @pool: the pool pointer of interest * * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned * successfully, -errno on failure. */static int worker_pool_assign_id(struct worker_pool *pool){	int ret; 	lockdep_assert_held(&wq_pool_mutex); 	ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE,			GFP_KERNEL);	if (ret >= 0) {		pool->id = ret;		return 0;	}	return ret;} static struct pool_workqueue __rcu **unbound_pwq_slot(struct workqueue_struct *wq, int cpu){       if (cpu >= 0)               return per_cpu_ptr(wq->cpu_pwq, cpu);       else               return &wq->dfl_pwq;} /* @cpu < 0 for dfl_pwq */static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu){	return rcu_dereference_check(*unbound_pwq_slot(wq, cpu),				     lockdep_is_held(&wq_pool_mutex) ||				     lockdep_is_held(&wq->mutex));} /** * unbound_effective_cpumask - effective cpumask of an unbound workqueue * @wq: workqueue of interest * * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which * is masked with wq_unbound_cpumask to determine the effective cpumask. The * default pwq is always mapped to the pool with the current effective cpumask. */static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq){	return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask;} static unsigned int work_color_to_flags(int color){	return color << WORK_STRUCT_COLOR_SHIFT;} static int get_work_color(unsigned long work_data){	return (work_data >> WORK_STRUCT_COLOR_SHIFT) &		((1 << WORK_STRUCT_COLOR_BITS) - 1);} static int work_next_color(int color){	return (color + 1) % WORK_NR_COLORS;} static unsigned long pool_offq_flags(struct worker_pool *pool){	return (pool->flags & POOL_BH) ? WORK_OFFQ_BH : 0;} /* * 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() and mark_work_canceling() * 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. */static inline void set_work_data(struct work_struct *work, unsigned long data){	WARN_ON_ONCE(!work_pending(work));	atomic_long_set(&work->data, data | work_static(work));} static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq,			 unsigned long flags){	set_work_data(work, (unsigned long)pwq | WORK_STRUCT_PENDING |		      WORK_STRUCT_PWQ | flags);} static void set_work_pool_and_keep_pending(struct work_struct *work,					   int pool_id, unsigned long flags){	set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) |		      WORK_STRUCT_PENDING | flags);} static void set_work_pool_and_clear_pending(struct work_struct *work,					    int pool_id, unsigned long flags){	/*	 * The following wmb is paired with the implied mb in	 * test_and_set_bit(PENDING) and ensures all updates to @work made	 * here are visible to and precede any updates by the next PENDING	 * owner.	 */	smp_wmb();	set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) |		      flags);	/*	 * The following mb guarantees that previous clear of a PENDING bit	 * will not be reordered with any speculative LOADS or STORES from	 * work->current_func, which is executed afterwards.  This possible	 * reordering can lead to a missed execution on attempt to queue	 * the same @work.  E.g. consider this case:	 *	 *   CPU#0                         CPU#1	 *   ----------------------------  --------------------------------	 *	 * 1  STORE event_indicated	 * 2  queue_work_on() {	 * 3    test_and_set_bit(PENDING)	 * 4 }                             set_..._and_clear_pending() {	 * 5                                 set_work_data() # clear bit	 * 6                                 smp_mb()	 * 7                               work->current_func() {	 * 8				      LOAD event_indicated	 *				   }	 *	 * Without an explicit full barrier speculative LOAD on line 8 can	 * be executed before CPU#0 does STORE on line 1.  If that happens,	 * CPU#0 observes the PENDING bit is still set and new execution of	 * a @work is not queued in a hope, that CPU#1 will eventually	 * finish the queued @work.  Meanwhile CPU#1 does not see	 * event_indicated is set, because speculative LOAD was executed	 * before actual STORE.	 */	smp_mb();} static inline struct pool_workqueue *work_struct_pwq(unsigned long data){	return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK);} static struct pool_workqueue *get_work_pwq(struct work_struct *work){	unsigned long data = atomic_long_read(&work->data); 	if (data & WORK_STRUCT_PWQ)		return work_struct_pwq(data);	else		return NULL;} /** * get_work_pool - return the worker_pool a given work was associated with * @work: the work item of interest * * Pools are created and destroyed under wq_pool_mutex, and allows read * access under RCU read lock.  As such, this function should be * called under wq_pool_mutex or inside of a rcu_read_lock() region. * * All fields of the returned pool are accessible as long as the above * mentioned locking is in effect.  If the returned pool needs to be used * beyond the critical section, the caller is responsible for ensuring the * returned pool is and stays online. * * Return: The worker_pool @work was last associated with.  %NULL if none. */static struct worker_pool *get_work_pool(struct work_struct *work){	unsigned long data = atomic_long_read(&work->data);	int pool_id; 	assert_rcu_or_pool_mutex(); 	if (data & WORK_STRUCT_PWQ)		return work_struct_pwq(data)->pool; 	pool_id = data >> WORK_OFFQ_POOL_SHIFT;	if (pool_id == WORK_OFFQ_POOL_NONE)		return NULL; 	return idr_find(&worker_pool_idr, pool_id);} static unsigned long shift_and_mask(unsigned long v, u32 shift, u32 bits){	return (v >> shift) & ((1U << bits) - 1);} static void work_offqd_unpack(struct work_offq_data *offqd, unsigned long data){	WARN_ON_ONCE(data & WORK_STRUCT_PWQ); 	offqd->pool_id = shift_and_mask(data, WORK_OFFQ_POOL_SHIFT,					WORK_OFFQ_POOL_BITS);	offqd->disable = shift_and_mask(data, WORK_OFFQ_DISABLE_SHIFT,					WORK_OFFQ_DISABLE_BITS);	offqd->flags = data & WORK_OFFQ_FLAG_MASK;} static unsigned long work_offqd_pack_flags(struct work_offq_data *offqd){	return ((unsigned long)offqd->disable << WORK_OFFQ_DISABLE_SHIFT) |		((unsigned long)offqd->flags);} /* * Policy functions.  These define the policies on how the global worker * pools are managed.  Unless noted otherwise, these functions assume that * they're being called with pool->lock held. */ /* * Need to wake up a worker?  Called from anything but currently * running workers. * * Note that, because unbound workers never contribute to nr_running, this * function will always return %true for unbound pools as long as the * worklist isn't empty. */static bool need_more_worker(struct worker_pool *pool){	return !list_empty(&pool->worklist) && !pool->nr_running;} /* Can I start working?  Called from busy but !running workers. */static bool may_start_working(struct worker_pool *pool){	return pool->nr_idle;} /* Do I need to keep working?  Called from currently running workers. */static bool keep_working(struct worker_pool *pool){	return !list_empty(&pool->worklist) && (pool->nr_running <= 1);} /* Do we need a new worker?  Called from manager. */static bool need_to_create_worker(struct worker_pool *pool){	return need_more_worker(pool) && !may_start_working(pool);} /* Do we have too many workers and should some go away? */static bool too_many_workers(struct worker_pool *pool){	bool managing = pool->flags & POOL_MANAGER_ACTIVE;	int nr_idle = pool->nr_idle + managing; /* manager is considered idle */	int nr_busy = pool->nr_workers - nr_idle; 	return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy;} /** * worker_set_flags - set worker flags and adjust nr_running accordingly * @worker: self * @flags: flags to set * * Set @flags in @worker->flags and adjust nr_running accordingly. */static inline void worker_set_flags(struct worker *worker, unsigned int flags){	struct worker_pool *pool = worker->pool; 	lockdep_assert_held(&pool->lock); 	/* If transitioning into NOT_RUNNING, adjust nr_running. */	if ((flags & WORKER_NOT_RUNNING) &&	    !(worker->flags & WORKER_NOT_RUNNING)) {		pool->nr_running--;	}

Lines 501–1000 of 8475. Previous lines