8475 lines · 239.9 KB · C
RawDownload
 * This goes through a pool's idle workers and gets rid of those that have been * idle for at least IDLE_WORKER_TIMEOUT seconds. * * We don't want to disturb isolated CPUs because of a pcpu kworker being * culled, so this also resets worker affinity. This requires a sleepable * context, hence the split between timer callback and work item. */static void idle_cull_fn(struct work_struct *work){	struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work);	LIST_HEAD(cull_list); 	/*	 * Grabbing wq_pool_attach_mutex here ensures an already-running worker	 * cannot proceed beyong set_pf_worker() in its self-destruct path.	 * This is required as a previously-preempted worker could run after	 * set_worker_dying() has happened but before detach_dying_workers() did.	 */	mutex_lock(&wq_pool_attach_mutex);	raw_spin_lock_irq(&pool->lock); 	while (too_many_workers(pool)) {		struct worker *worker;		unsigned long expires; 		worker = list_last_entry(&pool->idle_list, struct worker, entry);		expires = worker->last_active + IDLE_WORKER_TIMEOUT; 		if (time_before(jiffies, expires)) {			mod_timer(&pool->idle_timer, expires);			break;		} 		set_worker_dying(worker, &cull_list);	} 	raw_spin_unlock_irq(&pool->lock);	detach_dying_workers(&cull_list);	mutex_unlock(&wq_pool_attach_mutex); 	reap_dying_workers(&cull_list);} static void send_mayday(struct pool_workqueue *pwq){	struct workqueue_struct *wq = pwq->wq; 	lockdep_assert_held(&wq_mayday_lock); 	if (!wq->rescuer)		return; 	/* mayday mayday mayday */	if (list_empty(&pwq->mayday_node)) {		/*		 * If @pwq is for an unbound wq, its base ref may be put at		 * any time due to an attribute change.  Pin @pwq until the		 * rescuer is done with it.		 */		get_pwq(pwq);		list_add_tail(&pwq->mayday_node, &wq->maydays);		wake_up_process(wq->rescuer->task);		pwq->stats[PWQ_STAT_MAYDAY]++;	}} static void pool_mayday_timeout(struct timer_list *t){	struct worker_pool *pool = timer_container_of(pool, t, mayday_timer);	struct work_struct *work; 	raw_spin_lock_irq(&pool->lock);	raw_spin_lock(&wq_mayday_lock);		/* for wq->maydays */ 	if (need_to_create_worker(pool)) {		/*		 * We've been trying to create a new worker but		 * haven't been successful.  We might be hitting an		 * allocation deadlock.  Send distress signals to		 * rescuers.		 */		list_for_each_entry(work, &pool->worklist, entry)			send_mayday(get_work_pwq(work));	} 	raw_spin_unlock(&wq_mayday_lock);	raw_spin_unlock_irq(&pool->lock); 	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL);} /** * maybe_create_worker - create a new worker if necessary * @pool: pool to create a new worker for * * Create a new worker for @pool if necessary.  @pool is guaranteed to * have at least one idle worker on return from this function.  If * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is * sent to all rescuers with works scheduled on @pool to resolve * possible allocation deadlock. * * On return, need_to_create_worker() is guaranteed to be %false and * may_start_working() %true. * * LOCKING: * raw_spin_lock_irq(pool->lock) which may be released and regrabbed * multiple times.  Does GFP_KERNEL allocations.  Called only from * manager. */static void maybe_create_worker(struct worker_pool *pool)__releases(&pool->lock)__acquires(&pool->lock){restart:	raw_spin_unlock_irq(&pool->lock); 	/* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */	mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 	while (true) {		if (create_worker(pool) || !need_to_create_worker(pool))			break; 		schedule_timeout_interruptible(CREATE_COOLDOWN); 		if (!need_to_create_worker(pool))			break;	} 	timer_delete_sync(&pool->mayday_timer);	raw_spin_lock_irq(&pool->lock);	/*	 * This is necessary even after a new worker was just successfully	 * created as @pool->lock was dropped and the new worker might have	 * already become busy.	 */	if (need_to_create_worker(pool))		goto restart;} #ifdef CONFIG_PREEMPT_RTstatic void worker_lock_callback(struct worker_pool *pool){	spin_lock(&pool->cb_lock);} static void worker_unlock_callback(struct worker_pool *pool){	spin_unlock(&pool->cb_lock);} static void workqueue_callback_cancel_wait_running(struct worker_pool *pool){	spin_lock(&pool->cb_lock);	spin_unlock(&pool->cb_lock);} #else static void worker_lock_callback(struct worker_pool *pool) { }static void worker_unlock_callback(struct worker_pool *pool) { }static void workqueue_callback_cancel_wait_running(struct worker_pool *pool) { } #endif /** * manage_workers - manage worker pool * @worker: self * * Assume the manager role and manage the worker pool @worker belongs * to.  At any given time, there can be only zero or one manager per * pool.  The exclusion is handled automatically by this function. * * The caller can safely start processing works on false return.  On * true return, it's guaranteed that need_to_create_worker() is false * and may_start_working() is true. * * CONTEXT: * raw_spin_lock_irq(pool->lock) which may be released and regrabbed * multiple times.  Does GFP_KERNEL allocations. * * Return: * %false if the pool doesn't need management and the caller can safely * start processing works, %true if management function was performed and * the conditions that the caller verified before calling the function may * no longer be true. */static bool manage_workers(struct worker *worker){	struct worker_pool *pool = worker->pool; 	if (pool->flags & POOL_MANAGER_ACTIVE)		return false; 	pool->flags |= POOL_MANAGER_ACTIVE;	pool->manager = worker; 	maybe_create_worker(pool); 	pool->manager = NULL;	pool->flags &= ~POOL_MANAGER_ACTIVE;	rcuwait_wake_up(&manager_wait);	return true;} /** * process_one_work - process single work * @worker: self * @work: work to process * * Process @work.  This function contains all the logics necessary to * process a single work including synchronization against and * interaction with other workers on the same cpu, queueing and * flushing.  As long as context requirement is met, any worker can * call this function to process a work. * * CONTEXT: * raw_spin_lock_irq(pool->lock) which is released and regrabbed. */static void process_one_work(struct worker *worker, struct work_struct *work)__releases(&pool->lock)__acquires(&pool->lock){	struct pool_workqueue *pwq = get_work_pwq(work);	struct worker_pool *pool = worker->pool;	unsigned long work_data;	int lockdep_start_depth, rcu_start_depth;	bool bh_draining = pool->flags & POOL_BH_DRAINING;#ifdef CONFIG_LOCKDEP	/*	 * It is permissible to free the struct work_struct from	 * inside the function that is called from it, this we need to	 * take into account for lockdep too.  To avoid bogus "held	 * lock freed" warnings as well as problems when looking into	 * work->lockdep_map, make a copy and use that here.	 */	struct lockdep_map lockdep_map; 	lockdep_copy_map(&lockdep_map, &work->lockdep_map);#endif	/* ensure we're on the correct CPU */	WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&		     raw_smp_processor_id() != pool->cpu); 	/* claim and dequeue */	debug_work_deactivate(work);	hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work);	worker->current_work = work;	worker->current_func = work->func;	worker->current_pwq = pwq;	if (worker->task)		worker->current_at = worker->task->se.sum_exec_runtime;	worker->current_start = jiffies;	work_data = *work_data_bits(work);	worker->current_color = get_work_color(work_data); 	/*	 * Record wq name for cmdline and debug reporting, may get	 * overridden through set_worker_desc().	 */	strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 	list_del_init(&work->entry); 	/*	 * CPU intensive works don't participate in concurrency management.	 * They're the scheduler's responsibility.  This takes @worker out	 * of concurrency management and the next code block will chain	 * execution of the pending work items.	 */	if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE))		worker_set_flags(worker, WORKER_CPU_INTENSIVE); 	/*	 * Kick @pool if necessary. It's always noop for per-cpu worker pools	 * since nr_running would always be >= 1 at this point. This is used to	 * chain execution of the pending work items for WORKER_NOT_RUNNING	 * workers such as the UNBOUND and CPU_INTENSIVE ones.	 */	kick_pool(pool); 	/*	 * Record the last pool and clear PENDING which should be the last	 * update to @work.  Also, do this inside @pool->lock so that	 * PENDING and queued state changes happen together while IRQ is	 * disabled.	 */	set_work_pool_and_clear_pending(work, pool->id, pool_offq_flags(pool)); 	pwq->stats[PWQ_STAT_STARTED]++;	raw_spin_unlock_irq(&pool->lock); 	rcu_start_depth = rcu_preempt_depth();	lockdep_start_depth = lockdep_depth(current);	/* see drain_dead_softirq_workfn() */	if (!bh_draining)		lock_map_acquire(pwq->wq->lockdep_map);	lock_map_acquire(&lockdep_map);	/*	 * Strictly speaking we should mark the invariant state without holding	 * any locks, that is, before these two lock_map_acquire()'s.	 *	 * However, that would result in:	 *	 *   A(W1)	 *   WFC(C)	 *		A(W1)	 *		C(C)	 *	 * Which would create W1->C->W1 dependencies, even though there is no	 * actual deadlock possible. There are two solutions, using a	 * read-recursive acquire on the work(queue) 'locks', but this will then	 * hit the lockdep limitation on recursive locks, or simply discard	 * these locks.	 *	 * AFAICT there is no possible deadlock scenario between the	 * flush_work() and complete() primitives (except for single-threaded	 * workqueues), so hiding them isn't a problem.	 */	lockdep_invariant_state(true);	trace_workqueue_execute_start(work);	worker->current_func(work);	/*	 * While we must be careful to not use "work" after this, the trace	 * point will only record its address.	 */	trace_workqueue_execute_end(work, worker->current_func); 	lock_map_release(&lockdep_map);	if (!bh_draining)		lock_map_release(pwq->wq->lockdep_map); 	if (unlikely((worker->task && in_atomic()) ||		     lockdep_depth(current) != lockdep_start_depth ||		     rcu_preempt_depth() != rcu_start_depth)) {		pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n"		       "     preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n",		       current->comm, task_pid_nr(current), preempt_count(),		       lockdep_start_depth, lockdep_depth(current),		       rcu_start_depth, rcu_preempt_depth(),		       worker->current_func);		debug_show_held_locks(current);		dump_stack();	} 	/*	 * The following prevents a kworker from hogging CPU on !PREEMPTION	 * kernels, where a requeueing work item waiting for something to	 * happen could deadlock with stop_machine as such work item could	 * indefinitely requeue itself while all other CPUs are trapped in	 * stop_machine. At the same time, report a quiescent RCU state so	 * the same condition doesn't freeze RCU.	 */	if (worker->task)		cond_resched(); 	raw_spin_lock_irq(&pool->lock); 	pwq->stats[PWQ_STAT_COMPLETED]++; 	/*	 * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked	 * CPU intensive by wq_worker_tick() if @work hogged CPU longer than	 * wq_cpu_intensive_thresh_us. Clear it.	 */	worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 	/* tag the worker for identification in schedule() */	worker->last_func = worker->current_func; 	/* we're done with it, release */	hash_del(&worker->hentry);	worker->current_work = NULL;	worker->current_func = NULL;	worker->current_pwq = NULL;	worker->current_color = INT_MAX; 	/* must be the last step, see the function comment */	pwq_dec_nr_in_flight(pwq, work_data);} /** * process_scheduled_works - process scheduled works * @worker: self * * Process all scheduled works.  Please note that the scheduled list * may change while processing a work, so this function repeatedly * fetches a work from the top and executes it. * * CONTEXT: * raw_spin_lock_irq(pool->lock) which may be released and regrabbed * multiple times. */static void process_scheduled_works(struct worker *worker){	struct work_struct *work;	bool first = true; 	while ((work = list_first_entry_or_null(&worker->scheduled,						struct work_struct, entry))) {		if (first) {			worker->pool->last_progress_ts = jiffies;			first = false;		}		process_one_work(worker, work);	}} static void set_pf_worker(bool val){	mutex_lock(&wq_pool_attach_mutex);	if (val)		current->flags |= PF_WQ_WORKER;	else		current->flags &= ~PF_WQ_WORKER;	mutex_unlock(&wq_pool_attach_mutex);} /** * worker_thread - the worker thread function * @__worker: self * * The worker thread function.  All workers belong to a worker_pool - * either a per-cpu one or dynamic unbound one.  These workers process all * work items regardless of their specific target workqueue.  The only * exception is work items which belong to workqueues with a rescuer which * will be explained in rescuer_thread(). * * Return: 0 */static int worker_thread(void *__worker){	struct worker *worker = __worker;	struct worker_pool *pool = worker->pool; 	/* tell the scheduler that this is a workqueue worker */	set_pf_worker(true);woke_up:	raw_spin_lock_irq(&pool->lock); 	/* am I supposed to die? */	if (unlikely(worker->flags & WORKER_DIE)) {		raw_spin_unlock_irq(&pool->lock);		set_pf_worker(false);		/*		 * The worker is dead and PF_WQ_WORKER is cleared, worker->pool		 * shouldn't be accessed, reset it to NULL in case otherwise.		 */		worker->pool = NULL;		ida_free(&pool->worker_ida, worker->id);		return 0;	} 	worker_leave_idle(worker);recheck:	/* no more worker necessary? */	if (!need_more_worker(pool))		goto sleep; 	/* do we need to manage? */	if (unlikely(!may_start_working(pool)) && manage_workers(worker))		goto recheck; 	/*	 * ->scheduled list can only be filled while a worker is	 * preparing to process a work or actually processing it.	 * Make sure nobody diddled with it while I was sleeping.	 */	WARN_ON_ONCE(!list_empty(&worker->scheduled)); 	/*	 * Finish PREP stage.  We're guaranteed to have at least one idle	 * worker or that someone else has already assumed the manager	 * role.  This is where @worker starts participating in concurrency	 * management if applicable and concurrency management is restored	 * after being rebound.  See rebind_workers() for details.	 */	worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 	do {		struct work_struct *work =			list_first_entry(&pool->worklist,					 struct work_struct, entry); 		if (assign_work(work, worker, NULL))			process_scheduled_works(worker);	} while (keep_working(pool)); 	worker_set_flags(worker, WORKER_PREP);sleep:	/*	 * pool->lock is held and there's no work to process and no need to	 * manage, sleep.  Workers are woken up only while holding	 * pool->lock or from local cpu, so setting the current state	 * before releasing pool->lock is enough to prevent losing any	 * event.	 */	worker_enter_idle(worker);	__set_current_state(TASK_IDLE);	raw_spin_unlock_irq(&pool->lock);

Lines 3001–3500 of 8475. Previous lines