3842 lines · 104.4 KB · C
RawDownload
 * Wait until @work has finished execution.  This function considers * only the last queueing instance of @work.  If @work has been * enqueued across different CPUs on a non-reentrant workqueue or on * multiple workqueues, @work might still be executing on return on * some of the CPUs from earlier queueing. * * If @work was queued only on a non-reentrant, ordered or unbound * workqueue, @work is guaranteed to be idle on return if it hasn't * been requeued since flush started. * * RETURNS: * %true if flush_work() waited for the work to finish execution, * %false if it was already idle. */bool flush_work(struct work_struct *work){	struct wq_barrier barr; 	lock_map_acquire(&work->lockdep_map);	lock_map_release(&work->lockdep_map); 	if (start_flush_work(work, &barr, true)) {		wait_for_completion(&barr.done);		destroy_work_on_stack(&barr.work);		return true;	} else		return false;}EXPORT_SYMBOL_GPL(flush_work); static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work){	struct wq_barrier barr;	struct worker *worker; 	spin_lock_irq(&gcwq->lock); 	worker = find_worker_executing_work(gcwq, work);	if (unlikely(worker))		insert_wq_barrier(worker->current_cwq, &barr, work, worker); 	spin_unlock_irq(&gcwq->lock); 	if (unlikely(worker)) {		wait_for_completion(&barr.done);		destroy_work_on_stack(&barr.work);		return true;	} else		return false;} static bool wait_on_work(struct work_struct *work){	bool ret = false;	int cpu; 	might_sleep(); 	lock_map_acquire(&work->lockdep_map);	lock_map_release(&work->lockdep_map); 	for_each_gcwq_cpu(cpu)		ret |= wait_on_cpu_work(get_gcwq(cpu), work);	return ret;} /** * flush_work_sync - wait until a work has finished execution * @work: the work to flush * * Wait until @work has finished execution.  On return, it's * guaranteed that all queueing instances of @work which happened * before this function is called are finished.  In other words, if * @work hasn't been requeued since this function was called, @work is * guaranteed to be idle on return. * * RETURNS: * %true if flush_work_sync() waited for the work to finish execution, * %false if it was already idle. */bool flush_work_sync(struct work_struct *work){	struct wq_barrier barr;	bool pending, waited; 	/* we'll wait for executions separately, queue barr only if pending */	pending = start_flush_work(work, &barr, false); 	/* wait for executions to finish */	waited = wait_on_work(work); 	/* wait for the pending one */	if (pending) {		wait_for_completion(&barr.done);		destroy_work_on_stack(&barr.work);	} 	return pending || waited;}EXPORT_SYMBOL_GPL(flush_work_sync); /* * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, * so this work can't be re-armed in any way. */static int try_to_grab_pending(struct work_struct *work){	struct global_cwq *gcwq;	int ret = -1; 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))		return 0; 	/*	 * The queueing is in progress, or it is already queued. Try to	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.	 */	gcwq = get_work_gcwq(work);	if (!gcwq)		return ret; 	spin_lock_irq(&gcwq->lock);	if (!list_empty(&work->entry)) {		/*		 * This work is queued, but perhaps we locked the wrong gcwq.		 * In that case we must see the new value after rmb(), see		 * insert_work()->wmb().		 */		smp_rmb();		if (gcwq == get_work_gcwq(work)) {			debug_work_deactivate(work);			list_del_init(&work->entry);			cwq_dec_nr_in_flight(get_work_cwq(work),				get_work_color(work),				*work_data_bits(work) & WORK_STRUCT_DELAYED);			ret = 1;		}	}	spin_unlock_irq(&gcwq->lock); 	return ret;} static bool __cancel_work_timer(struct work_struct *work,				struct timer_list* timer){	int ret; 	do {		ret = (timer && likely(del_timer(timer)));		if (!ret)			ret = try_to_grab_pending(work);		wait_on_work(work);	} while (unlikely(ret < 0)); 	clear_work_data(work);	return ret;} /** * cancel_work_sync - cancel a work and wait for it to finish * @work: the work to cancel * * Cancel @work and wait for its execution to finish.  This function * can be used even if the work re-queues itself or migrates to * another workqueue.  On return from this function, @work is * guaranteed to be not pending or executing on any CPU. * * cancel_work_sync(&delayed_work->work) must not be used for * delayed_work's.  Use cancel_delayed_work_sync() instead. * * The caller must ensure that the workqueue on which @work was last * queued can't be destroyed before this function returns. * * RETURNS: * %true if @work was pending, %false otherwise. */bool cancel_work_sync(struct work_struct *work){	return __cancel_work_timer(work, NULL);}EXPORT_SYMBOL_GPL(cancel_work_sync); /** * flush_delayed_work - wait for a dwork to finish executing the last queueing * @dwork: the delayed work to flush * * Delayed timer is cancelled and the pending work is queued for * immediate execution.  Like flush_work(), this function only * considers the last queueing instance of @dwork. * * RETURNS: * %true if flush_work() waited for the work to finish execution, * %false if it was already idle. */bool flush_delayed_work(struct delayed_work *dwork){	if (del_timer_sync(&dwork->timer))		__queue_work(raw_smp_processor_id(),			     get_work_cwq(&dwork->work)->wq, &dwork->work);	return flush_work(&dwork->work);}EXPORT_SYMBOL(flush_delayed_work); /** * flush_delayed_work_sync - wait for a dwork to finish * @dwork: the delayed work to flush * * Delayed timer is cancelled and the pending work is queued for * execution immediately.  Other than timer handling, its behavior * is identical to flush_work_sync(). * * RETURNS: * %true if flush_work_sync() waited for the work to finish execution, * %false if it was already idle. */bool flush_delayed_work_sync(struct delayed_work *dwork){	if (del_timer_sync(&dwork->timer))		__queue_work(raw_smp_processor_id(),			     get_work_cwq(&dwork->work)->wq, &dwork->work);	return flush_work_sync(&dwork->work);}EXPORT_SYMBOL(flush_delayed_work_sync); /** * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish * @dwork: the delayed work cancel * * This is cancel_work_sync() for delayed works. * * RETURNS: * %true if @dwork was pending, %false otherwise. */bool cancel_delayed_work_sync(struct delayed_work *dwork){	return __cancel_work_timer(&dwork->work, &dwork->timer);}EXPORT_SYMBOL(cancel_delayed_work_sync); /** * schedule_work - put work task in global workqueue * @work: job to be done * * Returns zero if @work was already on the kernel-global workqueue and * non-zero otherwise. * * This puts a job in the kernel-global workqueue if it was not already * queued and leaves it in the same position on the kernel-global * workqueue otherwise. */int schedule_work(struct work_struct *work){	return queue_work(system_wq, work);}EXPORT_SYMBOL(schedule_work); /* * schedule_work_on - put work task on a specific cpu * @cpu: cpu to put the work task on * @work: job to be done * * This puts a job on a specific cpu */int schedule_work_on(int cpu, struct work_struct *work){	return queue_work_on(cpu, system_wq, work);}EXPORT_SYMBOL(schedule_work_on); /** * schedule_delayed_work - put work task in global workqueue after delay * @dwork: job to be done * @delay: number of jiffies to wait or 0 for immediate execution * * After waiting for a given time this puts a job in the kernel-global * workqueue. */int schedule_delayed_work(struct delayed_work *dwork,					unsigned long delay){	return queue_delayed_work(system_wq, dwork, delay);}EXPORT_SYMBOL(schedule_delayed_work); /** * schedule_delayed_work_on - queue work in global workqueue on CPU after delay * @cpu: cpu to use * @dwork: job to be done * @delay: number of jiffies to wait * * After waiting for a given time this puts a job in the kernel-global * workqueue on the specified CPU. */int schedule_delayed_work_on(int cpu,			struct delayed_work *dwork, unsigned long delay){	return queue_delayed_work_on(cpu, system_wq, dwork, delay);}EXPORT_SYMBOL(schedule_delayed_work_on); /** * schedule_on_each_cpu - execute a function synchronously on each online CPU * @func: the function to call * * schedule_on_each_cpu() executes @func on each online CPU using the * system workqueue and blocks until all CPUs have completed. * schedule_on_each_cpu() is very slow. * * RETURNS: * 0 on success, -errno on failure. */int schedule_on_each_cpu(work_func_t func){	int cpu;	struct work_struct __percpu *works; 	works = alloc_percpu(struct work_struct);	if (!works)		return -ENOMEM; 	get_online_cpus(); 	for_each_online_cpu(cpu) {		struct work_struct *work = per_cpu_ptr(works, cpu); 		INIT_WORK(work, func);		schedule_work_on(cpu, work);	} 	for_each_online_cpu(cpu)		flush_work(per_cpu_ptr(works, cpu)); 	put_online_cpus();	free_percpu(works);	return 0;} /** * flush_scheduled_work - ensure that any scheduled work has run to completion. * * Forces execution of the kernel-global workqueue and blocks until its * completion. * * Think twice before calling this function!  It's very easy to get into * trouble if you don't take great care.  Either of the following situations * will lead to deadlock: * *	One of the work items currently on the workqueue needs to acquire *	a lock held by your code or its caller. * *	Your code is running in the context of a work routine. * * They will be detected by lockdep when they occur, but the first might not * occur very often.  It depends on what work items are on the workqueue and * what locks they need, which you have no control over. * * In most situations flushing the entire workqueue is overkill; you merely * need to know that a particular work item isn't queued and isn't running. * In such cases you should use cancel_delayed_work_sync() or * cancel_work_sync() instead. */void flush_scheduled_work(void){	flush_workqueue(system_wq);}EXPORT_SYMBOL(flush_scheduled_work); /** * execute_in_process_context - reliably execute the routine with user context * @fn:		the function to execute * @ew:		guaranteed storage for the execute work structure (must *		be available when the work executes) * * Executes the function immediately if process context is available, * otherwise schedules the function for delayed execution. * * Returns:	0 - function was executed *		1 - function was scheduled for execution */int execute_in_process_context(work_func_t fn, struct execute_work *ew){	if (!in_interrupt()) {		fn(&ew->work);		return 0;	} 	INIT_WORK(&ew->work, fn);	schedule_work(&ew->work); 	return 1;}EXPORT_SYMBOL_GPL(execute_in_process_context); int keventd_up(void){	return system_wq != NULL;} static int alloc_cwqs(struct workqueue_struct *wq){	/*	 * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS.	 * Make sure that the alignment isn't lower than that of	 * unsigned long long.	 */	const size_t size = sizeof(struct cpu_workqueue_struct);	const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS,				   __alignof__(unsigned long long)); 	if (!(wq->flags & WQ_UNBOUND))		wq->cpu_wq.pcpu = __alloc_percpu(size, align);	else {		void *ptr; 		/*		 * Allocate enough room to align cwq and put an extra		 * pointer at the end pointing back to the originally		 * allocated pointer which will be used for free.		 */		ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL);		if (ptr) {			wq->cpu_wq.single = PTR_ALIGN(ptr, align);			*(void **)(wq->cpu_wq.single + 1) = ptr;		}	} 	/* just in case, make sure it's actually aligned */	BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align));	return wq->cpu_wq.v ? 0 : -ENOMEM;} static void free_cwqs(struct workqueue_struct *wq){	if (!(wq->flags & WQ_UNBOUND))		free_percpu(wq->cpu_wq.pcpu);	else if (wq->cpu_wq.single) {		/* the pointer to free is stored right after the cwq */		kfree(*(void **)(wq->cpu_wq.single + 1));	}} static int wq_clamp_max_active(int max_active, unsigned int flags,			       const char *name){	int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 	if (max_active < 1 || max_active > lim)		printk(KERN_WARNING "workqueue: max_active %d requested for %s "		       "is out of range, clamping between %d and %d\n",		       max_active, name, 1, lim); 	return clamp_val(max_active, 1, lim);} struct workqueue_struct *__alloc_workqueue_key(const char *fmt,					       unsigned int flags,					       int max_active,					       struct lock_class_key *key,					       const char *lock_name, ...){	va_list args, args1;	struct workqueue_struct *wq;	unsigned int cpu;	size_t namelen; 	/* determine namelen, allocate wq and format name */	va_start(args, lock_name);	va_copy(args1, args);	namelen = vsnprintf(NULL, 0, fmt, args) + 1; 	wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL);	if (!wq)		goto err; 	vsnprintf(wq->name, namelen, fmt, args1);	va_end(args);	va_end(args1); 	/*	 * Workqueues which may be used during memory reclaim should	 * have a rescuer to guarantee forward progress.	 */	if (flags & WQ_MEM_RECLAIM)		flags |= WQ_RESCUER; 	/*	 * Unbound workqueues aren't concurrency managed and should be	 * dispatched to workers immediately.	 */	if (flags & WQ_UNBOUND)		flags |= WQ_HIGHPRI; 	max_active = max_active ?: WQ_DFL_ACTIVE;	max_active = wq_clamp_max_active(max_active, flags, wq->name); 	/* init wq */	wq->flags = flags;	wq->saved_max_active = max_active;	mutex_init(&wq->flush_mutex);

Lines 2501–3000 of 3842. Previous lines