6039 lines · 165.7 KB · C
RawDownload
 * unrelated work executions through a work item being recycled while still * being executed. * * This is a bit tricky.  A work item may be freed once its execution * starts and nothing prevents the freed area from being recycled for * another work item.  If the same work item address ends up being reused * before the original execution finishes, workqueue will identify the * recycled work item as currently executing and make it wait until the * current execution finishes, introducing an unwanted dependency. * * This function checks the work item address and work function to avoid * false positives.  Note that this isn't complete as one may construct a * work function which can introduce dependency onto itself through a * recycled work item.  Well, if somebody wants to shoot oneself in the * foot that badly, there's only so much we can do, and if such deadlock * actually occurs, it should be easy to locate the culprit work function. * * CONTEXT: * raw_spin_lock_irq(pool->lock). * * Return: * Pointer to worker which is executing @work if found, %NULL * otherwise. */static struct worker *find_worker_executing_work(struct worker_pool *pool,						 struct work_struct *work){	struct worker *worker; 	hash_for_each_possible(pool->busy_hash, worker, hentry,			       (unsigned long)work)		if (worker->current_work == work &&		    worker->current_func == work->func)			return worker; 	return NULL;} /** * move_linked_works - move linked works to a list * @work: start of series of works to be scheduled * @head: target list to append @work to * @nextp: out parameter for nested worklist walking * * Schedule linked works starting from @work to @head.  Work series to * be scheduled starts at @work and includes any consecutive work with * WORK_STRUCT_LINKED set in its predecessor. * * If @nextp is not NULL, it's updated to point to the next work of * the last scheduled work.  This allows move_linked_works() to be * nested inside outer list_for_each_entry_safe(). * * CONTEXT: * raw_spin_lock_irq(pool->lock). */static void move_linked_works(struct work_struct *work, struct list_head *head,			      struct work_struct **nextp){	struct work_struct *n; 	/*	 * Linked worklist will always end before the end of the list,	 * use NULL for list head.	 */	list_for_each_entry_safe_from(work, n, NULL, entry) {		list_move_tail(&work->entry, head);		if (!(*work_data_bits(work) & WORK_STRUCT_LINKED))			break;	} 	/*	 * If we're already inside safe list traversal and have moved	 * multiple works to the scheduled queue, the next position	 * needs to be updated.	 */	if (nextp)		*nextp = n;} /** * get_pwq - get an extra reference on the specified pool_workqueue * @pwq: pool_workqueue to get * * Obtain an extra reference on @pwq.  The caller should guarantee that * @pwq has positive refcnt and be holding the matching pool->lock. */static void get_pwq(struct pool_workqueue *pwq){	lockdep_assert_held(&pwq->pool->lock);	WARN_ON_ONCE(pwq->refcnt <= 0);	pwq->refcnt++;} /** * put_pwq - put a pool_workqueue reference * @pwq: pool_workqueue to put * * Drop a reference of @pwq.  If its refcnt reaches zero, schedule its * destruction.  The caller should be holding the matching pool->lock. */static void put_pwq(struct pool_workqueue *pwq){	lockdep_assert_held(&pwq->pool->lock);	if (likely(--pwq->refcnt))		return;	if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND)))		return;	/*	 * @pwq can't be released under pool->lock, bounce to	 * pwq_unbound_release_workfn().  This never recurses on the same	 * pool->lock as this path is taken only for unbound workqueues and	 * the release work item is scheduled on a per-cpu workqueue.  To	 * avoid lockdep warning, unbound pool->locks are given lockdep	 * subclass of 1 in get_unbound_pool().	 */	schedule_work(&pwq->unbound_release_work);} /** * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock * @pwq: pool_workqueue to put (can be %NULL) * * put_pwq() with locking.  This function also allows %NULL @pwq. */static void put_pwq_unlocked(struct pool_workqueue *pwq){	if (pwq) {		/*		 * As both pwqs and pools are RCU protected, the		 * following lock operations are safe.		 */		raw_spin_lock_irq(&pwq->pool->lock);		put_pwq(pwq);		raw_spin_unlock_irq(&pwq->pool->lock);	}} static void pwq_activate_delayed_work(struct work_struct *work){	struct pool_workqueue *pwq = get_work_pwq(work); 	trace_workqueue_activate_work(work);	if (list_empty(&pwq->pool->worklist))		pwq->pool->watchdog_ts = jiffies;	move_linked_works(work, &pwq->pool->worklist, NULL);	__clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work));	pwq->nr_active++;} static void pwq_activate_first_delayed(struct pool_workqueue *pwq){	struct work_struct *work = list_first_entry(&pwq->delayed_works,						    struct work_struct, entry); 	pwq_activate_delayed_work(work);} /** * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight * @pwq: pwq of interest * @color: color of work which left the queue * * A work either has completed or is removed from pending queue, * decrement nr_in_flight of its pwq and handle workqueue flushing. * * CONTEXT: * raw_spin_lock_irq(pool->lock). */static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color){	/* uncolored work items don't participate in flushing or nr_active */	if (color == WORK_NO_COLOR)		goto out_put; 	pwq->nr_in_flight[color]--; 	pwq->nr_active--;	if (!list_empty(&pwq->delayed_works)) {		/* one down, submit a delayed one */		if (pwq->nr_active < pwq->max_active)			pwq_activate_first_delayed(pwq);	} 	/* is flush in progress and are we at the flushing tip? */	if (likely(pwq->flush_color != color))		goto out_put; 	/* are there still in-flight works? */	if (pwq->nr_in_flight[color])		goto out_put; 	/* this pwq is done, clear flush_color */	pwq->flush_color = -1; 	/*	 * If this was the last pwq, wake up the first flusher.  It	 * will handle the rest.	 */	if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush))		complete(&pwq->wq->first_flusher->done);out_put:	put_pwq(pwq);} /** * try_to_grab_pending - steal work item from worklist and disable irq * @work: work item to steal * @is_dwork: @work is a delayed_work * @flags: place to store irq state * * Try to grab PENDING bit of @work.  This function can handle @work in any * stable state - idle, on timer or on worklist. * * Return: *  1		if @work was pending and we successfully stole PENDING *  0		if @work was idle and we claimed PENDING *  -EAGAIN	if PENDING couldn't be grabbed at the moment, safe to busy-retry *  -ENOENT	if someone else is canceling @work, this state may persist *		for arbitrarily long * * Note: * On >= 0 return, the caller owns @work's PENDING bit.  To avoid getting * interrupted while holding PENDING and @work off queue, irq must be * disabled on entry.  This, combined with delayed_work->timer being * irqsafe, ensures that we return -EAGAIN for finite short period of time. * * On successful return, >= 0, irq is disabled and the caller is * responsible for releasing it using local_irq_restore(*@flags). * * This function is safe to call from any context including IRQ handler. */static int try_to_grab_pending(struct work_struct *work, bool is_dwork,			       unsigned long *flags){	struct worker_pool *pool;	struct pool_workqueue *pwq; 	local_irq_save(*flags); 	/* try to steal the timer if it exists */	if (is_dwork) {		struct delayed_work *dwork = to_delayed_work(work); 		/*		 * dwork->timer is irqsafe.  If del_timer() fails, it's		 * guaranteed that the timer is not queued anywhere and not		 * running on the local CPU.		 */		if (likely(del_timer(&dwork->timer)))			return 1;	} 	/* try to claim PENDING the normal way */	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)))		return 0; 	rcu_read_lock();	/*	 * The queueing is in progress, or it is already queued. Try to	 * steal it from ->worklist without clearing WORK_STRUCT_PENDING.	 */	pool = get_work_pool(work);	if (!pool)		goto fail; 	raw_spin_lock(&pool->lock);	/*	 * work->data is guaranteed to point to pwq only while the work	 * item is queued on pwq->wq, and both updating work->data to point	 * to pwq on queueing and to pool on dequeueing are done under	 * pwq->pool->lock.  This in turn guarantees that, if work->data	 * points to pwq which is associated with a locked pool, the work	 * item is currently queued on that pool.	 */	pwq = get_work_pwq(work);	if (pwq && pwq->pool == pool) {		debug_work_deactivate(work); 		/*		 * A delayed work item cannot be grabbed directly because		 * it might have linked NO_COLOR work items which, if left		 * on the delayed_list, will confuse pwq->nr_active		 * management later on and cause stall.  Make sure the work		 * item is activated before grabbing.		 */		if (*work_data_bits(work) & WORK_STRUCT_DELAYED)			pwq_activate_delayed_work(work); 		list_del_init(&work->entry);		pwq_dec_nr_in_flight(pwq, get_work_color(work)); 		/* work->data points to pwq iff queued, point to pool */		set_work_pool_and_keep_pending(work, pool->id); 		raw_spin_unlock(&pool->lock);		rcu_read_unlock();		return 1;	}	raw_spin_unlock(&pool->lock);fail:	rcu_read_unlock();	local_irq_restore(*flags);	if (work_is_canceling(work))		return -ENOENT;	cpu_relax();	return -EAGAIN;} /** * insert_work - insert a work into a pool * @pwq: pwq @work belongs to * @work: work to insert * @head: insertion point * @extra_flags: extra WORK_STRUCT_* flags to set * * Insert @work which belongs to @pwq after @head.  @extra_flags is or'd to * work_struct flags. * * CONTEXT: * raw_spin_lock_irq(pool->lock). */static void insert_work(struct pool_workqueue *pwq, struct work_struct *work,			struct list_head *head, unsigned int extra_flags){	struct worker_pool *pool = pwq->pool; 	/* we own @work, set data and link */	set_work_pwq(work, pwq, extra_flags);	list_add_tail(&work->entry, head);	get_pwq(pwq); 	/*	 * Ensure either wq_worker_sleeping() sees the above	 * list_add_tail() or we see zero nr_running to avoid workers lying	 * around lazily while there are works to be processed.	 */	smp_mb(); 	if (__need_more_worker(pool))		wake_up_worker(pool);} /* * Test whether @work is being queued from another work executing on the * same workqueue. */static bool is_chained_work(struct workqueue_struct *wq){	struct worker *worker; 	worker = current_wq_worker();	/*	 * Return %true iff I'm a worker executing a work item on @wq.  If	 * I'm @worker, it's safe to dereference it without locking.	 */	return worker && worker->current_pwq->wq == wq;} /* * When queueing an unbound work item to a wq, prefer local CPU if allowed * by wq_unbound_cpumask.  Otherwise, round robin among the allowed ones to * avoid perturbing sensitive tasks. */static int wq_select_unbound_cpu(int cpu){	static bool printed_dbg_warning;	int new_cpu; 	if (likely(!wq_debug_force_rr_cpu)) {		if (cpumask_test_cpu(cpu, wq_unbound_cpumask))			return cpu;	} else if (!printed_dbg_warning) {		pr_warn("workqueue: round-robin CPU selection forced, expect performance impact\n");		printed_dbg_warning = true;	} 	if (cpumask_empty(wq_unbound_cpumask))		return cpu; 	new_cpu = __this_cpu_read(wq_rr_cpu_last);	new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask);	if (unlikely(new_cpu >= nr_cpu_ids)) {		new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask);		if (unlikely(new_cpu >= nr_cpu_ids))			return cpu;	}	__this_cpu_write(wq_rr_cpu_last, new_cpu); 	return new_cpu;} static void __queue_work(int cpu, struct workqueue_struct *wq,			 struct work_struct *work){	struct pool_workqueue *pwq;	struct worker_pool *last_pool;	struct list_head *worklist;	unsigned int work_flags;	unsigned int req_cpu = cpu; 	/*	 * While a work item is PENDING && off queue, a task trying to	 * steal the PENDING will busy-loop waiting for it to either get	 * queued or lose PENDING.  Grabbing PENDING and queueing should	 * happen with IRQ disabled.	 */	lockdep_assert_irqs_disabled(); 	debug_work_activate(work); 	/* if draining, only works from the same workqueue are allowed */	if (unlikely(wq->flags & __WQ_DRAINING) &&	    WARN_ON_ONCE(!is_chained_work(wq)))		return;	rcu_read_lock();retry:	/* pwq which will be used unless @work is executing elsewhere */	if (wq->flags & WQ_UNBOUND) {		if (req_cpu == WORK_CPU_UNBOUND)			cpu = wq_select_unbound_cpu(raw_smp_processor_id());		pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu));	} else {		if (req_cpu == WORK_CPU_UNBOUND)			cpu = raw_smp_processor_id();		pwq = per_cpu_ptr(wq->cpu_pwqs, cpu);	} 	/*	 * If @work was previously on a different pool, it might still be	 * running there, in which case the work needs to be queued on that	 * pool to guarantee non-reentrancy.	 */	last_pool = get_work_pool(work);	if (last_pool && last_pool != pwq->pool) {		struct worker *worker; 		raw_spin_lock(&last_pool->lock); 		worker = find_worker_executing_work(last_pool, work); 		if (worker && worker->current_pwq->wq == wq) {			pwq = worker->current_pwq;		} else {			/* meh... not running there, queue here */			raw_spin_unlock(&last_pool->lock);			raw_spin_lock(&pwq->pool->lock);		}	} else {		raw_spin_lock(&pwq->pool->lock);	} 	/*	 * pwq is determined and locked.  For unbound pools, we could have	 * raced with pwq release and it could already be dead.  If its	 * refcnt is zero, repeat pwq selection.  Note that pwqs never die	 * without another pwq replacing it in the numa_pwq_tbl or while	 * work items are executing on it, so the retrying is guaranteed to	 * make forward-progress.	 */	if (unlikely(!pwq->refcnt)) {		if (wq->flags & WQ_UNBOUND) {			raw_spin_unlock(&pwq->pool->lock);			cpu_relax();			goto retry;		}		/* oops */		WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt",			  wq->name, cpu);	} 	/* pwq determined, queue */	trace_workqueue_queue_work(req_cpu, pwq, work); 	if (WARN_ON(!list_empty(&work->entry)))		goto out; 	pwq->nr_in_flight[pwq->work_color]++;	work_flags = work_color_to_flags(pwq->work_color); 	if (likely(pwq->nr_active < pwq->max_active)) {		trace_workqueue_activate_work(work);		pwq->nr_active++;		worklist = &pwq->pool->worklist;		if (list_empty(worklist))			pwq->pool->watchdog_ts = jiffies;	} else {		work_flags |= WORK_STRUCT_DELAYED;		worklist = &pwq->delayed_works;	} 	insert_work(pwq, work, worklist, work_flags); out:	raw_spin_unlock(&pwq->pool->lock);	rcu_read_unlock();} /** * queue_work_on - queue work on specific cpu * @cpu: CPU number to execute work on

Lines 1001–1500 of 6039. Previous lines