6039 lines · 165.7 KB · C
RawDownload
 * @wq: workqueue to use * @work: work to queue * * We queue the work to a specific CPU, the caller must ensure it * can't go away. * * Return: %false if @work was already on a queue, %true otherwise. */bool queue_work_on(int cpu, struct workqueue_struct *wq,		   struct work_struct *work){	bool ret = false;	unsigned long flags; 	local_irq_save(flags); 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {		__queue_work(cpu, wq, work);		ret = true;	} 	local_irq_restore(flags);	return ret;}EXPORT_SYMBOL(queue_work_on); /** * workqueue_select_cpu_near - Select a CPU based on NUMA node * @node: NUMA node ID that we want to select a CPU from * * This function will attempt to find a "random" cpu available on a given * node. If there are no CPUs available on the given node it will return * WORK_CPU_UNBOUND indicating that we should just schedule to any * available CPU if we need to schedule this work. */static int workqueue_select_cpu_near(int node){	int cpu; 	/* No point in doing this if NUMA isn't enabled for workqueues */	if (!wq_numa_enabled)		return WORK_CPU_UNBOUND; 	/* Delay binding to CPU if node is not valid or online */	if (node < 0 || node >= MAX_NUMNODES || !node_online(node))		return WORK_CPU_UNBOUND; 	/* Use local node/cpu if we are already there */	cpu = raw_smp_processor_id();	if (node == cpu_to_node(cpu))		return cpu; 	/* Use "random" otherwise know as "first" online CPU of node */	cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 	/* If CPU is valid return that, otherwise just defer */	return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND;} /** * queue_work_node - queue work on a "random" cpu for a given NUMA node * @node: NUMA node that we are targeting the work for * @wq: workqueue to use * @work: work to queue * * We queue the work to a "random" CPU within a given NUMA node. The basic * idea here is to provide a way to somehow associate work with a given * NUMA node. * * This function will only make a best effort attempt at getting this onto * the right NUMA node. If no node is requested or the requested node is * offline then we just fall back to standard queue_work behavior. * * Currently the "random" CPU ends up being the first available CPU in the * intersection of cpu_online_mask and the cpumask of the node, unless we * are running on the node. In that case we just use the current CPU. * * Return: %false if @work was already on a queue, %true otherwise. */bool queue_work_node(int node, struct workqueue_struct *wq,		     struct work_struct *work){	unsigned long flags;	bool ret = false; 	/*	 * This current implementation is specific to unbound workqueues.	 * Specifically we only return the first available CPU for a given	 * node instead of cycling through individual CPUs within the node.	 *	 * If this is used with a per-cpu workqueue then the logic in	 * workqueue_select_cpu_near would need to be updated to allow for	 * some round robin type logic.	 */	WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 	local_irq_save(flags); 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {		int cpu = workqueue_select_cpu_near(node); 		__queue_work(cpu, wq, work);		ret = true;	} 	local_irq_restore(flags);	return ret;}EXPORT_SYMBOL_GPL(queue_work_node); void delayed_work_timer_fn(struct timer_list *t){	struct delayed_work *dwork = from_timer(dwork, t, timer); 	/* should have been called from irqsafe timer with irq already off */	__queue_work(dwork->cpu, dwork->wq, &dwork->work);}EXPORT_SYMBOL(delayed_work_timer_fn); static void __queue_delayed_work(int cpu, struct workqueue_struct *wq,				struct delayed_work *dwork, unsigned long delay){	struct timer_list *timer = &dwork->timer;	struct work_struct *work = &dwork->work; 	WARN_ON_ONCE(!wq);	WARN_ON_ONCE(timer->function != delayed_work_timer_fn);	WARN_ON_ONCE(timer_pending(timer));	WARN_ON_ONCE(!list_empty(&work->entry)); 	/*	 * If @delay is 0, queue @dwork->work immediately.  This is for	 * both optimization and correctness.  The earliest @timer can	 * expire is on the closest next tick and delayed_work users depend	 * on that there's no such delay when @delay is 0.	 */	if (!delay) {		__queue_work(cpu, wq, &dwork->work);		return;	} 	dwork->wq = wq;	dwork->cpu = cpu;	timer->expires = jiffies + delay; 	if (unlikely(cpu != WORK_CPU_UNBOUND))		add_timer_on(timer, cpu);	else		add_timer(timer);} /** * queue_delayed_work_on - queue work on specific CPU after delay * @cpu: CPU number to execute work on * @wq: workqueue to use * @dwork: work to queue * @delay: number of jiffies to wait before queueing * * Return: %false if @work was already on a queue, %true otherwise.  If * @delay is zero and @dwork is idle, it will be scheduled for immediate * execution. */bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq,			   struct delayed_work *dwork, unsigned long delay){	struct work_struct *work = &dwork->work;	bool ret = false;	unsigned long flags; 	/* read the comment in __queue_work() */	local_irq_save(flags); 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {		__queue_delayed_work(cpu, wq, dwork, delay);		ret = true;	} 	local_irq_restore(flags);	return ret;}EXPORT_SYMBOL(queue_delayed_work_on); /** * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU * @cpu: CPU number to execute work on * @wq: workqueue to use * @dwork: work to queue * @delay: number of jiffies to wait before queueing * * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, * modify @dwork's timer so that it expires after @delay.  If @delay is * zero, @work is guaranteed to be scheduled immediately regardless of its * current state. * * Return: %false if @dwork was idle and queued, %true if @dwork was * pending and its timer was modified. * * This function is safe to call from any context including IRQ handler. * See try_to_grab_pending() for details. */bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq,			 struct delayed_work *dwork, unsigned long delay){	unsigned long flags;	int ret; 	do {		ret = try_to_grab_pending(&dwork->work, true, &flags);	} while (unlikely(ret == -EAGAIN)); 	if (likely(ret >= 0)) {		__queue_delayed_work(cpu, wq, dwork, delay);		local_irq_restore(flags);	} 	/* -ENOENT from try_to_grab_pending() becomes %true */	return ret;}EXPORT_SYMBOL_GPL(mod_delayed_work_on); static void rcu_work_rcufn(struct rcu_head *rcu){	struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 	/* read the comment in __queue_work() */	local_irq_disable();	__queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work);	local_irq_enable();} /** * queue_rcu_work - queue work after a RCU grace period * @wq: workqueue to use * @rwork: work to queue * * Return: %false if @rwork was already pending, %true otherwise.  Note * that a full RCU grace period is guaranteed only after a %true return. * While @rwork is guaranteed to be executed after a %false return, the * execution may happen before a full RCU grace period has passed. */bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork){	struct work_struct *work = &rwork->work; 	if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) {		rwork->wq = wq;		call_rcu(&rwork->rcu, rcu_work_rcufn);		return true;	} 	return false;}EXPORT_SYMBOL(queue_rcu_work); /** * worker_enter_idle - enter idle state * @worker: worker which is entering idle state * * @worker is entering idle state.  Update stats and idle timer if * necessary. * * LOCKING: * raw_spin_lock_irq(pool->lock). */static void worker_enter_idle(struct worker *worker){	struct worker_pool *pool = worker->pool; 	if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) ||	    WARN_ON_ONCE(!list_empty(&worker->entry) &&			 (worker->hentry.next || worker->hentry.pprev)))		return; 	/* can't use worker_set_flags(), also called from create_worker() */	worker->flags |= WORKER_IDLE;	pool->nr_idle++;	worker->last_active = jiffies; 	/* idle_list is LIFO */	list_add(&worker->entry, &pool->idle_list); 	if (too_many_workers(pool) && !timer_pending(&pool->idle_timer))		mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 	/*	 * Sanity check nr_running.  Because unbind_workers() releases	 * pool->lock between setting %WORKER_UNBOUND and zapping	 * nr_running, the warning may trigger spuriously.  Check iff	 * unbind is not in progress.	 */	WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) &&		     pool->nr_workers == pool->nr_idle &&		     atomic_read(&pool->nr_running));} /** * worker_leave_idle - leave idle state * @worker: worker which is leaving idle state * * @worker is leaving idle state.  Update stats. * * LOCKING: * raw_spin_lock_irq(pool->lock). */static void worker_leave_idle(struct worker *worker){	struct worker_pool *pool = worker->pool; 	if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE)))		return;	worker_clr_flags(worker, WORKER_IDLE);	pool->nr_idle--;	list_del_init(&worker->entry);} static struct worker *alloc_worker(int node){	struct worker *worker; 	worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node);	if (worker) {		INIT_LIST_HEAD(&worker->entry);		INIT_LIST_HEAD(&worker->scheduled);		INIT_LIST_HEAD(&worker->node);		/* on creation a worker is in !idle && prep state */		worker->flags = WORKER_PREP;	}	return worker;} /** * worker_attach_to_pool() - attach a worker to a pool * @worker: worker to be attached * @pool: the target pool * * Attach @worker to @pool.  Once attached, the %WORKER_UNBOUND flag and * cpu-binding of @worker are kept coordinated with the pool across * cpu-[un]hotplugs. */static void worker_attach_to_pool(struct worker *worker,				   struct worker_pool *pool){	mutex_lock(&wq_pool_attach_mutex); 	/*	 * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any	 * online CPUs.  It'll be re-applied when any of the CPUs come up.	 */	set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 	/*	 * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains	 * stable across this function.  See the comments above the flag	 * definition for details.	 */	if (pool->flags & POOL_DISASSOCIATED)		worker->flags |= WORKER_UNBOUND; 	list_add_tail(&worker->node, &pool->workers);	worker->pool = pool; 	mutex_unlock(&wq_pool_attach_mutex);} /** * worker_detach_from_pool() - detach a worker from its pool * @worker: worker which is attached to its pool * * Undo the attaching which had been done in worker_attach_to_pool().  The * caller worker shouldn't access to the pool after detached except it has * other reference to the pool. */static void worker_detach_from_pool(struct worker *worker){	struct worker_pool *pool = worker->pool;	struct completion *detach_completion = NULL; 	mutex_lock(&wq_pool_attach_mutex); 	list_del(&worker->node);	worker->pool = NULL; 	if (list_empty(&pool->workers))		detach_completion = pool->detach_completion;	mutex_unlock(&wq_pool_attach_mutex); 	/* clear leftover flags without pool->lock after it is detached */	worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 	if (detach_completion)		complete(detach_completion);} /** * create_worker - create a new workqueue worker * @pool: pool the new worker will belong to * * Create and start a new worker which is attached to @pool. * * CONTEXT: * Might sleep.  Does GFP_KERNEL allocations. * * Return: * Pointer to the newly created worker. */static struct worker *create_worker(struct worker_pool *pool){	struct worker *worker = NULL;	int id = -1;	char id_buf[16]; 	/* ID is needed to determine kthread name */	id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL);	if (id < 0)		goto fail; 	worker = alloc_worker(pool->node);	if (!worker)		goto fail; 	worker->id = id; 	if (pool->cpu >= 0)		snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id,			 pool->attrs->nice < 0  ? "H" : "");	else		snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 	worker->task = kthread_create_on_node(worker_thread, worker, pool->node,					      "kworker/%s", id_buf);	if (IS_ERR(worker->task))		goto fail; 	set_user_nice(worker->task, pool->attrs->nice);	kthread_bind_mask(worker->task, pool->attrs->cpumask); 	/* successful, attach the worker to the pool */	worker_attach_to_pool(worker, pool); 	/* start the newly created worker */	raw_spin_lock_irq(&pool->lock);	worker->pool->nr_workers++;	worker_enter_idle(worker);	wake_up_process(worker->task);	raw_spin_unlock_irq(&pool->lock); 	return worker; fail:	if (id >= 0)		ida_simple_remove(&pool->worker_ida, id);	kfree(worker);	return NULL;} /** * destroy_worker - destroy a workqueue worker * @worker: worker to be destroyed * * Destroy @worker and adjust @pool stats accordingly.  The worker should * be idle. * * CONTEXT: * raw_spin_lock_irq(pool->lock). */static void destroy_worker(struct worker *worker){	struct worker_pool *pool = worker->pool; 	lockdep_assert_held(&pool->lock); 	/* sanity check frenzy */	if (WARN_ON(worker->current_work) ||	    WARN_ON(!list_empty(&worker->scheduled)) ||	    WARN_ON(!(worker->flags & WORKER_IDLE)))		return; 	pool->nr_workers--;	pool->nr_idle--; 	list_del_init(&worker->entry);	worker->flags |= WORKER_DIE;	wake_up_process(worker->task);} static void idle_worker_timeout(struct timer_list *t){	struct worker_pool *pool = from_timer(pool, t, idle_timer); 	raw_spin_lock_irq(&pool->lock); 	while (too_many_workers(pool)) {		struct worker *worker;		unsigned long expires; 		/* idle_list is kept in LIFO order, check the last one */		worker = list_entry(pool->idle_list.prev, struct worker, entry);		expires = worker->last_active + IDLE_WORKER_TIMEOUT; 		if (time_before(jiffies, expires)) {

Lines 1501–2000 of 6039. Previous lines