8475 lines · 239.9 KB · C
RawDownload
			comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);		}		pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);		pr_cont("\n");	}} /** * show_one_workqueue - dump state of specified workqueue * @wq: workqueue whose state will be printed */void show_one_workqueue(struct workqueue_struct *wq){	struct pool_workqueue *pwq;	bool idle = true;	unsigned long irq_flags; 	for_each_pwq(pwq, wq) {		if (!pwq_is_empty(pwq)) {			idle = false;			break;		}	}	if (idle) /* Nothing to print for idle workqueue */		return; 	pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 	for_each_pwq(pwq, wq) {		raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags);		if (!pwq_is_empty(pwq)) {			/*			 * Defer printing to avoid deadlocks in console			 * drivers that queue work while holding locks			 * also taken in their write paths.			 */			printk_deferred_enter();			show_pwq(pwq);			printk_deferred_exit();		}		raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags);		/*		 * We could be printing a lot from atomic context, e.g.		 * sysrq-t -> show_all_workqueues(). Avoid triggering		 * hard lockup.		 */		touch_nmi_watchdog();	} } /** * show_one_worker_pool - dump state of specified worker pool * @pool: worker pool whose state will be printed */static void show_one_worker_pool(struct worker_pool *pool){	struct worker *worker;	bool first = true;	unsigned long irq_flags;	unsigned long hung = 0; 	raw_spin_lock_irqsave(&pool->lock, irq_flags);	if (pool->nr_workers == pool->nr_idle)		goto next_pool; 	/* How long the first pending work is waiting for a worker. */	if (!list_empty(&pool->worklist))		hung = jiffies_to_msecs(jiffies - pool->last_progress_ts) / 1000; 	/*	 * Defer printing to avoid deadlocks in console drivers that	 * queue work while holding locks also taken in their write	 * paths.	 */	printk_deferred_enter();	pr_info("pool %d:", pool->id);	pr_cont_pool_info(pool);	pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers);	if (pool->manager)		pr_cont(" manager: %d",			task_pid_nr(pool->manager->task));	list_for_each_entry(worker, &pool->idle_list, entry) {		pr_cont(" %s", first ? "idle: " : "");		pr_cont_worker_id(worker);		first = false;	}	pr_cont("\n");	printk_deferred_exit();next_pool:	raw_spin_unlock_irqrestore(&pool->lock, irq_flags);	/*	 * We could be printing a lot from atomic context, e.g.	 * sysrq-t -> show_all_workqueues(). Avoid triggering	 * hard lockup.	 */	touch_nmi_watchdog(); } /** * show_all_workqueues - dump workqueue state * * Called from a sysrq handler and prints out all busy workqueues and pools. */void show_all_workqueues(void){	struct workqueue_struct *wq;	struct worker_pool *pool;	int pi; 	rcu_read_lock(); 	pr_info("Showing busy workqueues and worker pools:\n"); 	list_for_each_entry_rcu(wq, &workqueues, list)		show_one_workqueue(wq); 	for_each_pool(pool, pi)		show_one_worker_pool(pool); 	rcu_read_unlock();} /** * show_freezable_workqueues - dump freezable workqueue state * * Called from try_to_freeze_tasks() and prints out all freezable workqueues * still busy. */void show_freezable_workqueues(void){	struct workqueue_struct *wq; 	rcu_read_lock(); 	pr_info("Showing freezable workqueues that are still busy:\n"); 	list_for_each_entry_rcu(wq, &workqueues, list) {		if (!(wq->flags & WQ_FREEZABLE))			continue;		show_one_workqueue(wq);	} 	rcu_read_unlock();} /* used to show worker information through /proc/PID/{comm,stat,status} */void wq_worker_comm(char *buf, size_t size, struct task_struct *task){	/* stabilize PF_WQ_WORKER and worker pool association */	mutex_lock(&wq_pool_attach_mutex); 	if (task->flags & PF_WQ_WORKER) {		struct worker *worker = kthread_data(task);		struct worker_pool *pool = worker->pool;		int off; 		off = format_worker_id(buf, size, worker, pool); 		if (pool) {			raw_spin_lock_irq(&pool->lock);			/*			 * ->desc tracks information (wq name or			 * set_worker_desc()) for the latest execution.  If			 * current, prepend '+', otherwise '-'.			 */			if (worker->desc[0] != '\0') {				if (worker->current_work)					scnprintf(buf + off, size - off, "+%s",						  worker->desc);				else					scnprintf(buf + off, size - off, "-%s",						  worker->desc);			}			raw_spin_unlock_irq(&pool->lock);		}	} else {		strscpy(buf, task->comm, size);	} 	mutex_unlock(&wq_pool_attach_mutex);} #ifdef CONFIG_SMP /* * CPU hotplug. * * There are two challenges in supporting CPU hotplug.  Firstly, there * are a lot of assumptions on strong associations among work, pwq and * pool which make migrating pending and scheduled works very * difficult to implement without impacting hot paths.  Secondly, * worker pools serve mix of short, long and very long running works making * blocked draining impractical. * * This is solved by allowing the pools to be disassociated from the CPU * running as an unbound one and allowing it to be reattached later if the * cpu comes back online. */ static void unbind_workers(int cpu){	struct worker_pool *pool;	struct worker *worker; 	for_each_cpu_worker_pool(pool, cpu) {		mutex_lock(&wq_pool_attach_mutex);		raw_spin_lock_irq(&pool->lock); 		/*		 * We've blocked all attach/detach operations. Make all workers		 * unbound and set DISASSOCIATED.  Before this, all workers		 * must be on the cpu.  After this, they may become diasporas.		 * And the preemption disabled section in their sched callbacks		 * are guaranteed to see WORKER_UNBOUND since the code here		 * is on the same cpu.		 */		for_each_pool_worker(worker, pool)			worker->flags |= WORKER_UNBOUND; 		pool->flags |= POOL_DISASSOCIATED; 		/*		 * The handling of nr_running in sched callbacks are disabled		 * now.  Zap nr_running.  After this, nr_running stays zero and		 * need_more_worker() and keep_working() are always true as		 * long as the worklist is not empty.  This pool now behaves as		 * an unbound (in terms of concurrency management) pool which		 * are served by workers tied to the pool.		 */		pool->nr_running = 0; 		/*		 * With concurrency management just turned off, a busy		 * worker blocking could lead to lengthy stalls.  Kick off		 * unbound chain execution of currently pending work items.		 */		kick_pool(pool); 		raw_spin_unlock_irq(&pool->lock); 		for_each_pool_worker(worker, pool)			unbind_worker(worker); 		mutex_unlock(&wq_pool_attach_mutex);	}} /** * rebind_workers - rebind all workers of a pool to the associated CPU * @pool: pool of interest * * @pool->cpu is coming online.  Rebind all workers to the CPU. */static void rebind_workers(struct worker_pool *pool){	struct worker *worker; 	lockdep_assert_held(&wq_pool_attach_mutex); 	/*	 * Restore CPU affinity of all workers.  As all idle workers should	 * be on the run-queue of the associated CPU before any local	 * wake-ups for concurrency management happen, restore CPU affinity	 * of all workers first and then clear UNBOUND.  As we're called	 * from CPU_ONLINE, the following shouldn't fail.	 */	for_each_pool_worker(worker, pool) {		kthread_set_per_cpu(worker->task, pool->cpu);		WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task,						  pool_allowed_cpus(pool)) < 0);	} 	raw_spin_lock_irq(&pool->lock); 	pool->flags &= ~POOL_DISASSOCIATED; 	for_each_pool_worker(worker, pool) {		unsigned int worker_flags = worker->flags; 		/*		 * We want to clear UNBOUND but can't directly call		 * worker_clr_flags() or adjust nr_running.  Atomically		 * replace UNBOUND with another NOT_RUNNING flag REBOUND.		 * @worker will clear REBOUND using worker_clr_flags() when		 * it initiates the next execution cycle thus restoring		 * concurrency management.  Note that when or whether		 * @worker clears REBOUND doesn't affect correctness.		 *		 * WRITE_ONCE() is necessary because @worker->flags may be		 * tested without holding any lock in		 * wq_worker_running().  Without it, NOT_RUNNING test may		 * fail incorrectly leading to premature concurrency		 * management operations.		 */		WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND));		worker_flags |= WORKER_REBOUND;		worker_flags &= ~WORKER_UNBOUND;		WRITE_ONCE(worker->flags, worker_flags);	} 	raw_spin_unlock_irq(&pool->lock);} /** * restore_unbound_workers_cpumask - restore cpumask of unbound workers * @pool: unbound pool of interest * @cpu: the CPU which is coming up * * An unbound pool may end up with a cpumask which doesn't have any online * CPUs.  When a worker of such pool get scheduled, the scheduler resets * its cpus_allowed.  If @cpu is in @pool's cpumask which didn't have any * online CPU before, cpus_allowed of all its workers should be restored. */static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu){	static cpumask_t cpumask;	struct worker *worker; 	lockdep_assert_held(&wq_pool_attach_mutex); 	/* is @cpu allowed for @pool? */	if (!cpumask_test_cpu(cpu, pool->attrs->cpumask))		return; 	cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 	/* as we're called from CPU_ONLINE, the following shouldn't fail */	for_each_pool_worker(worker, pool)		WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0);} int workqueue_prepare_cpu(unsigned int cpu){	struct worker_pool *pool; 	for_each_cpu_worker_pool(pool, cpu) {		if (pool->nr_workers)			continue;		if (!create_worker(pool))			return -ENOMEM;	}	return 0;} int workqueue_online_cpu(unsigned int cpu){	struct worker_pool *pool;	struct workqueue_struct *wq;	int pi; 	mutex_lock(&wq_pool_mutex); 	cpumask_set_cpu(cpu, wq_online_cpumask); 	for_each_pool(pool, pi) {		/* BH pools aren't affected by hotplug */		if (pool->flags & POOL_BH)			continue; 		mutex_lock(&wq_pool_attach_mutex);		if (pool->cpu == cpu)			rebind_workers(pool);		else if (pool->cpu < 0)			restore_unbound_workers_cpumask(pool, cpu);		mutex_unlock(&wq_pool_attach_mutex);	} 	/* update pod affinity of unbound workqueues */	list_for_each_entry(wq, &workqueues, list) {		struct workqueue_attrs *attrs = wq->unbound_attrs; 		if (attrs) {			const struct wq_pod_type *pt = wqattrs_pod_type(attrs);			int tcpu; 			for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]])				unbound_wq_update_pwq(wq, tcpu); 			mutex_lock(&wq->mutex);			wq_update_node_max_active(wq, -1);			mutex_unlock(&wq->mutex);		}	} 	mutex_unlock(&wq_pool_mutex);	return 0;} int workqueue_offline_cpu(unsigned int cpu){	struct workqueue_struct *wq; 	/* unbinding per-cpu workers should happen on the local CPU */	if (WARN_ON(cpu != smp_processor_id()))		return -1; 	unbind_workers(cpu); 	/* update pod affinity of unbound workqueues */	mutex_lock(&wq_pool_mutex); 	cpumask_clear_cpu(cpu, wq_online_cpumask); 	list_for_each_entry(wq, &workqueues, list) {		struct workqueue_attrs *attrs = wq->unbound_attrs; 		if (attrs) {			const struct wq_pod_type *pt = wqattrs_pod_type(attrs);			int tcpu; 			for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]])				unbound_wq_update_pwq(wq, tcpu); 			mutex_lock(&wq->mutex);			wq_update_node_max_active(wq, cpu);			mutex_unlock(&wq->mutex);		}	}	mutex_unlock(&wq_pool_mutex); 	return 0;} struct work_for_cpu {	struct work_struct work;	long (*fn)(void *);	void *arg;	long ret;}; static void work_for_cpu_fn(struct work_struct *work){	struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 	wfc->ret = wfc->fn(wfc->arg);} /** * work_on_cpu_key - run a function in thread context on a particular cpu * @cpu: the cpu to run on * @fn: the function to run * @arg: the function arg * @key: The lock class key for lock debugging purposes * * It is up to the caller to ensure that the cpu doesn't go offline. * The caller must not hold any locks which would prevent @fn from completing. * * Return: The value @fn returns. */long work_on_cpu_key(int cpu, long (*fn)(void *),		     void *arg, struct lock_class_key *key){	struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 	INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key);	schedule_work_on(cpu, &wfc.work);	flush_work(&wfc.work);	destroy_work_on_stack(&wfc.work);	return wfc.ret;}EXPORT_SYMBOL_GPL(work_on_cpu_key);#endif /* CONFIG_SMP */ #ifdef CONFIG_FREEZER /** * freeze_workqueues_begin - begin freezing workqueues * * Start freezing workqueues.  After this function returns, all freezable * workqueues will queue new works to their inactive_works list instead of * pool->worklist. * * CONTEXT: * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. */void freeze_workqueues_begin(void){	struct workqueue_struct *wq; 	mutex_lock(&wq_pool_mutex); 	WARN_ON_ONCE(workqueue_freezing);	workqueue_freezing = true; 	list_for_each_entry(wq, &workqueues, list) {		mutex_lock(&wq->mutex);		wq_adjust_max_active(wq);		mutex_unlock(&wq->mutex);	} 	mutex_unlock(&wq_pool_mutex);} /** * freeze_workqueues_busy - are freezable workqueues still busy? * * Check whether freezing is complete.  This function must be called * between freeze_workqueues_begin() and thaw_workqueues().

Lines 6501–7000 of 8475. Previous lines