8475 lines · 239.9 KB · C
RawDownload
{	struct workqueue_struct *wq;	va_list args; 	va_start(args, lockdep_map);	wq = __alloc_workqueue(fmt, flags, max_active, args);	va_end(args);	if (!wq)		return NULL; 	wq->lockdep_map = lockdep_map; 	return wq;}EXPORT_SYMBOL_GPL(alloc_workqueue_lockdep_map);#endif static bool pwq_busy(struct pool_workqueue *pwq){	int i; 	for (i = 0; i < WORK_NR_COLORS; i++)		if (pwq->nr_in_flight[i])			return true; 	if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1))		return true;	if (!pwq_is_empty(pwq))		return true; 	return false;} /** * destroy_workqueue - safely terminate a workqueue * @wq: target workqueue * * Safely destroy a workqueue. All work currently pending will be done first. * * This function does NOT guarantee that non-pending work that has been * submitted with queue_delayed_work() and similar functions will be done * before destroying the workqueue. The fundamental problem is that, currently, * the workqueue has no way of accessing non-pending delayed_work. delayed_work * is only linked on the timer-side. All delayed_work must, therefore, be * canceled before calling this function. * * TODO: It would be better if the problem described above wouldn't exist and * destroy_workqueue() would cleanly cancel all pending and non-pending * delayed_work. */void destroy_workqueue(struct workqueue_struct *wq){	struct pool_workqueue *pwq;	int cpu; 	/*	 * Remove it from sysfs first so that sanity check failure doesn't	 * lead to sysfs name conflicts.	 */	workqueue_sysfs_unregister(wq); 	/* mark the workqueue destruction is in progress */	mutex_lock(&wq->mutex);	wq->flags |= __WQ_DESTROYING;	mutex_unlock(&wq->mutex); 	/* drain it before proceeding with destruction */	drain_workqueue(wq); 	/* kill rescuer, if sanity checks fail, leave it w/o rescuer */	if (wq->rescuer) {		/* rescuer will empty maydays list before exiting */		kthread_stop(wq->rescuer->task);		kfree(wq->rescuer);		wq->rescuer = NULL;	} 	/*	 * Sanity checks - grab all the locks so that we wait for all	 * in-flight operations which may do put_pwq().	 */	mutex_lock(&wq_pool_mutex);	mutex_lock(&wq->mutex);	for_each_pwq(pwq, wq) {		raw_spin_lock_irq(&pwq->pool->lock);		if (WARN_ON(pwq_busy(pwq))) {			pr_warn("%s: %s has the following busy pwq\n",				__func__, wq->name);			show_pwq(pwq);			raw_spin_unlock_irq(&pwq->pool->lock);			mutex_unlock(&wq->mutex);			mutex_unlock(&wq_pool_mutex);			show_one_workqueue(wq);			return;		}		raw_spin_unlock_irq(&pwq->pool->lock);	}	mutex_unlock(&wq->mutex); 	/*	 * wq list is used to freeze wq, remove from list after	 * flushing is complete in case freeze races us.	 */	list_del_rcu(&wq->list);	mutex_unlock(&wq_pool_mutex); 	/*	 * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq	 * to put the base refs. @wq will be auto-destroyed from the last	 * pwq_put. RCU read lock prevents @wq from going away from under us.	 */	rcu_read_lock(); 	for_each_possible_cpu(cpu) {		put_pwq_unlocked(unbound_pwq(wq, cpu));		RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL);	} 	put_pwq_unlocked(unbound_pwq(wq, -1));	RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 	rcu_read_unlock();}EXPORT_SYMBOL_GPL(destroy_workqueue); /** * workqueue_set_max_active - adjust max_active of a workqueue * @wq: target workqueue * @max_active: new max_active value. * * Set max_active of @wq to @max_active. See the alloc_workqueue() function * comment. * * CONTEXT: * Don't call from IRQ context. */void workqueue_set_max_active(struct workqueue_struct *wq, int max_active){	/* max_active doesn't mean anything for BH workqueues */	if (WARN_ON(wq->flags & WQ_BH))		return;	/* disallow meddling with max_active for ordered workqueues */	if (WARN_ON(wq->flags & __WQ_ORDERED))		return; 	max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 	mutex_lock(&wq->mutex); 	wq->saved_max_active = max_active;	if (wq->flags & WQ_UNBOUND)		wq->saved_min_active = min(wq->saved_min_active, max_active); 	wq_adjust_max_active(wq); 	mutex_unlock(&wq->mutex);}EXPORT_SYMBOL_GPL(workqueue_set_max_active); /** * workqueue_set_min_active - adjust min_active of an unbound workqueue * @wq: target unbound workqueue * @min_active: new min_active value * * Set min_active of an unbound workqueue. Unlike other types of workqueues, an * unbound workqueue is not guaranteed to be able to process max_active * interdependent work items. Instead, an unbound workqueue is guaranteed to be * able to process min_active number of interdependent work items which is * %WQ_DFL_MIN_ACTIVE by default. * * Use this function to adjust the min_active value between 0 and the current * max_active. */void workqueue_set_min_active(struct workqueue_struct *wq, int min_active){	/* min_active is only meaningful for non-ordered unbound workqueues */	if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) !=		    WQ_UNBOUND))		return; 	mutex_lock(&wq->mutex);	wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active);	wq_adjust_max_active(wq);	mutex_unlock(&wq->mutex);} /** * current_work - retrieve %current task's work struct * * Determine if %current task is a workqueue worker and what it's working on. * Useful to find out the context that the %current task is running in. * * Return: work struct if %current task is a workqueue worker, %NULL otherwise. */struct work_struct *current_work(void){	struct worker *worker = current_wq_worker(); 	return worker ? worker->current_work : NULL;}EXPORT_SYMBOL(current_work); /** * current_is_workqueue_rescuer - is %current workqueue rescuer? * * Determine whether %current is a workqueue rescuer.  Can be used from * work functions to determine whether it's being run off the rescuer task. * * Return: %true if %current is a workqueue rescuer. %false otherwise. */bool current_is_workqueue_rescuer(void){	struct worker *worker = current_wq_worker(); 	return worker && worker->rescue_wq;} /** * workqueue_congested - test whether a workqueue is congested * @cpu: CPU in question * @wq: target workqueue * * Test whether @wq's cpu workqueue for @cpu is congested.  There is * no synchronization around this function and the test result is * unreliable and only useful as advisory hints or for debugging. * * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. * * With the exception of ordered workqueues, all workqueues have per-cpu * pool_workqueues, each with its own congested state. A workqueue being * congested on one CPU doesn't mean that the workqueue is contested on any * other CPUs. * * Return: * %true if congested, %false otherwise. */bool workqueue_congested(int cpu, struct workqueue_struct *wq){	struct pool_workqueue *pwq;	bool ret; 	preempt_disable(); 	if (cpu == WORK_CPU_UNBOUND)		cpu = smp_processor_id(); 	pwq = *per_cpu_ptr(wq->cpu_pwq, cpu);	ret = !list_empty(&pwq->inactive_works); 	preempt_enable(); 	return ret;}EXPORT_SYMBOL_GPL(workqueue_congested); /** * work_busy - test whether a work is currently pending or running * @work: the work to be tested * * Test whether @work is currently pending or running.  There is no * synchronization around this function and the test result is * unreliable and only useful as advisory hints or for debugging. * * Return: * OR'd bitmask of WORK_BUSY_* bits. */unsigned int work_busy(struct work_struct *work){	struct worker_pool *pool;	unsigned long irq_flags;	unsigned int ret = 0; 	if (work_pending(work))		ret |= WORK_BUSY_PENDING; 	rcu_read_lock();	pool = get_work_pool(work);	if (pool) {		raw_spin_lock_irqsave(&pool->lock, irq_flags);		if (find_worker_executing_work(pool, work))			ret |= WORK_BUSY_RUNNING;		raw_spin_unlock_irqrestore(&pool->lock, irq_flags);	}	rcu_read_unlock(); 	return ret;}EXPORT_SYMBOL_GPL(work_busy); /** * set_worker_desc - set description for the current work item * @fmt: printf-style format string * @...: arguments for the format string * * This function can be called by a running work function to describe what * the work item is about.  If the worker task gets dumped, this * information will be printed out together to help debugging.  The * description can be at most WORKER_DESC_LEN including the trailing '\0'. */void set_worker_desc(const char *fmt, ...){	struct worker *worker = current_wq_worker();	va_list args; 	if (worker) {		va_start(args, fmt);		vsnprintf(worker->desc, sizeof(worker->desc), fmt, args);		va_end(args);	}}EXPORT_SYMBOL_GPL(set_worker_desc); /** * print_worker_info - print out worker information and description * @log_lvl: the log level to use when printing * @task: target task * * If @task is a worker and currently executing a work item, print out the * name of the workqueue being serviced and worker description set with * set_worker_desc() by the currently executing work item. * * This function can be safely called on any task as long as the * task_struct itself is accessible.  While safe, this function isn't * synchronized and may print out mixups or garbages of limited length. */void print_worker_info(const char *log_lvl, struct task_struct *task){	work_func_t fn = NULL;	char name[WQ_NAME_LEN] = { };	char desc[WORKER_DESC_LEN] = { };	struct pool_workqueue *pwq = NULL;	struct workqueue_struct *wq = NULL;	struct worker *worker; 	if (!(task->flags & PF_WQ_WORKER))		return; 	/*	 * This function is called without any synchronization and @task	 * could be in any state.  Be careful with dereferences.	 */	worker = kthread_probe_data(task); 	/*	 * Carefully copy the associated workqueue's workfn, name and desc.	 * Keep the original last '\0' in case the original is garbage.	 */	copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn));	copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq));	copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq));	copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1);	copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 	if (fn || name[0] || desc[0]) {		printk("%sWorkqueue: %s %ps", log_lvl, name, fn);		if (strcmp(name, desc))			pr_cont(" (%s)", desc);		pr_cont("\n");	}} static void pr_cont_pool_info(struct worker_pool *pool){	pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask);	if (pool->node != NUMA_NO_NODE)		pr_cont(" node=%d", pool->node);	pr_cont(" flags=0x%x", pool->flags);	if (pool->flags & POOL_BH)		pr_cont(" bh%s",			pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : "");	else		pr_cont(" nice=%d", pool->attrs->nice);} static void pr_cont_worker_id(struct worker *worker){	struct worker_pool *pool = worker->pool; 	if (pool->flags & POOL_BH)		pr_cont("bh%s",			pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : "");	else		pr_cont("%d%s", task_pid_nr(worker->task),			worker->rescue_wq ? "(RESCUER)" : "");} struct pr_cont_work_struct {	bool comma;	work_func_t func;	long ctr;}; static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp){	if (!pcwsp->ctr)		goto out_record;	if (func == pcwsp->func) {		pcwsp->ctr++;		return;	}	if (pcwsp->ctr == 1)		pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func);	else		pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func);	pcwsp->ctr = 0;out_record:	if ((long)func == -1L)		return;	pcwsp->comma = comma;	pcwsp->func = func;	pcwsp->ctr = 1;} static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp){	if (work->func == wq_barrier_func) {		struct wq_barrier *barr; 		barr = container_of(work, struct wq_barrier, work); 		pr_cont_work_flush(comma, (work_func_t)-1, pcwsp);		pr_cont("%s BAR(%d)", comma ? "," : "",			task_pid_nr(barr->task));	} else {		if (!comma)			pr_cont_work_flush(comma, (work_func_t)-1, pcwsp);		pr_cont_work_flush(comma, work->func, pcwsp);	}} static void show_pwq(struct pool_workqueue *pwq){	struct pr_cont_work_struct pcws = { .ctr = 0, };	struct worker_pool *pool = pwq->pool;	struct work_struct *work;	struct worker *worker;	bool has_in_flight = false, has_pending = false;	int bkt; 	pr_info("  pwq %d:", pool->id);	pr_cont_pool_info(pool); 	pr_cont(" active=%d refcnt=%d%s\n",		pwq->nr_active, pwq->refcnt,		!list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 	hash_for_each(pool->busy_hash, bkt, worker, hentry) {		if (worker->current_pwq == pwq) {			has_in_flight = true;			break;		}	}	if (has_in_flight) {		bool comma = false; 		pr_info("    in-flight:");		hash_for_each(pool->busy_hash, bkt, worker, hentry) {			if (worker->current_pwq != pwq)				continue; 			pr_cont(" %s", comma ? "," : "");			pr_cont_worker_id(worker);			pr_cont(":%ps", worker->current_func);			pr_cont(" for %us",				jiffies_to_msecs(jiffies - worker->current_start) / 1000);			list_for_each_entry(work, &worker->scheduled, entry)				pr_cont_work(false, work, &pcws);			pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);			comma = true;		}		pr_cont("\n");	} 	list_for_each_entry(work, &pool->worklist, entry) {		if (get_work_pwq(work) == pwq) {			has_pending = true;			break;		}	}	if (has_pending) {		bool comma = false; 		pr_info("    pending:");		list_for_each_entry(work, &pool->worklist, entry) {			if (get_work_pwq(work) != pwq)				continue; 			pr_cont_work(comma, work, &pcws);			comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED);		}		pr_cont_work_flush(comma, (work_func_t)-1L, &pcws);		pr_cont("\n");	} 	if (!list_empty(&pwq->inactive_works)) {		bool comma = false; 		pr_info("    inactive:");		list_for_each_entry(work, &pwq->inactive_works, entry) {			pr_cont_work(comma, work, &pcws);

Lines 6001–6500 of 8475. Previous lines