1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/sched.c
5 * Scheduling for synchronous and asynchronous RPC requests.
9 * TCP NFS related read + write fixes
13 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
17 #include <linux/slab.h>
18 #include <linux/mempool.h>
19 #include <linux/smp.h>
20 #include <linux/spinlock.h>
21 #include <linux/mutex.h>
22 #include <linux/freezer.h>
23 #include <linux/sched/mm.h>
25 #include <linux/sunrpc/clnt.h>
26 #include <linux/sunrpc/metrics.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/sunrpc.h>
34 * RPC slabs and memory pools
36 #define RPC_BUFFER_MAXSIZE (2048)
37 #define RPC_BUFFER_POOLSIZE (8)
38 #define RPC_TASK_POOLSIZE (8)
39 static struct kmem_cache *rpc_task_slabp __read_mostly;
40 static struct kmem_cache *rpc_buffer_slabp __read_mostly;
41 static mempool_t *rpc_task_mempool __read_mostly;
42 static mempool_t *rpc_buffer_mempool __read_mostly;
44 static void rpc_async_schedule(struct work_struct *);
45 static void rpc_release_task(struct rpc_task *task);
46 static void __rpc_queue_timer_fn(struct work_struct *);
49 * RPC tasks sit here while waiting for conditions to improve.
51 static struct rpc_wait_queue delay_queue;
54 * rpciod-related stuff
56 struct workqueue_struct *rpciod_workqueue __read_mostly;
57 struct workqueue_struct *xprtiod_workqueue __read_mostly;
58 EXPORT_SYMBOL_GPL(xprtiod_workqueue);
60 gfp_t rpc_task_gfp_mask(void)
62 if (current->flags & PF_WQ_WORKER)
63 return GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN;
66 EXPORT_SYMBOL_GPL(rpc_task_gfp_mask);
69 rpc_task_timeout(const struct rpc_task *task)
71 unsigned long timeout = READ_ONCE(task->tk_timeout);
74 unsigned long now = jiffies;
75 if (time_before(now, timeout))
80 EXPORT_SYMBOL_GPL(rpc_task_timeout);
83 * Disable the timer for a given RPC task. Should be called with
84 * queue->lock and bh_disabled in order to avoid races within
88 __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
90 if (list_empty(&task->u.tk_wait.timer_list))
93 list_del(&task->u.tk_wait.timer_list);
94 if (list_empty(&queue->timer_list.list))
95 cancel_delayed_work(&queue->timer_list.dwork);
99 rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
101 unsigned long now = jiffies;
102 queue->timer_list.expires = expires;
103 if (time_before_eq(expires, now))
107 mod_delayed_work(rpciod_workqueue, &queue->timer_list.dwork, expires);
111 * Set up a timer for the current task.
114 __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task,
115 unsigned long timeout)
117 task->tk_timeout = timeout;
118 if (list_empty(&queue->timer_list.list) || time_before(timeout, queue->timer_list.expires))
119 rpc_set_queue_timer(queue, timeout);
120 list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list);
123 static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
125 if (queue->priority != priority) {
126 queue->priority = priority;
127 queue->nr = 1U << priority;
131 static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
133 rpc_set_waitqueue_priority(queue, queue->maxpriority);
137 * Add a request to a queue list
140 __rpc_list_enqueue_task(struct list_head *q, struct rpc_task *task)
144 list_for_each_entry(t, q, u.tk_wait.list) {
145 if (t->tk_owner == task->tk_owner) {
146 list_add_tail(&task->u.tk_wait.links,
147 &t->u.tk_wait.links);
148 /* Cache the queue head in task->u.tk_wait.list */
149 task->u.tk_wait.list.next = q;
150 task->u.tk_wait.list.prev = NULL;
154 INIT_LIST_HEAD(&task->u.tk_wait.links);
155 list_add_tail(&task->u.tk_wait.list, q);
159 * Remove request from a queue list
162 __rpc_list_dequeue_task(struct rpc_task *task)
167 if (task->u.tk_wait.list.prev == NULL) {
168 list_del(&task->u.tk_wait.links);
171 if (!list_empty(&task->u.tk_wait.links)) {
172 t = list_first_entry(&task->u.tk_wait.links,
175 /* Assume __rpc_list_enqueue_task() cached the queue head */
176 q = t->u.tk_wait.list.next;
177 list_add_tail(&t->u.tk_wait.list, q);
178 list_del(&task->u.tk_wait.links);
180 list_del(&task->u.tk_wait.list);
184 * Add new request to a priority queue.
186 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue,
187 struct rpc_task *task,
188 unsigned char queue_priority)
190 if (unlikely(queue_priority > queue->maxpriority))
191 queue_priority = queue->maxpriority;
192 __rpc_list_enqueue_task(&queue->tasks[queue_priority], task);
196 * Add new request to wait queue.
198 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue,
199 struct rpc_task *task,
200 unsigned char queue_priority)
202 INIT_LIST_HEAD(&task->u.tk_wait.timer_list);
203 if (RPC_IS_PRIORITY(queue))
204 __rpc_add_wait_queue_priority(queue, task, queue_priority);
206 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
207 task->tk_waitqueue = queue;
209 /* barrier matches the read in rpc_wake_up_task_queue_locked() */
211 rpc_set_queued(task);
215 * Remove request from a priority queue.
217 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
219 __rpc_list_dequeue_task(task);
223 * Remove request from queue.
224 * Note: must be called with spin lock held.
226 static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
228 __rpc_disable_timer(queue, task);
229 if (RPC_IS_PRIORITY(queue))
230 __rpc_remove_wait_queue_priority(task);
232 list_del(&task->u.tk_wait.list);
236 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
240 spin_lock_init(&queue->lock);
241 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
242 INIT_LIST_HEAD(&queue->tasks[i]);
243 queue->maxpriority = nr_queues - 1;
244 rpc_reset_waitqueue_priority(queue);
246 queue->timer_list.expires = 0;
247 INIT_DELAYED_WORK(&queue->timer_list.dwork, __rpc_queue_timer_fn);
248 INIT_LIST_HEAD(&queue->timer_list.list);
249 rpc_assign_waitqueue_name(queue, qname);
252 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
254 __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
256 EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue);
258 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
260 __rpc_init_priority_wait_queue(queue, qname, 1);
262 EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
264 void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
266 cancel_delayed_work_sync(&queue->timer_list.dwork);
268 EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
270 static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode)
272 freezable_schedule_unsafe();
273 if (signal_pending_state(mode, current))
278 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS)
279 static void rpc_task_set_debuginfo(struct rpc_task *task)
281 struct rpc_clnt *clnt = task->tk_client;
283 /* Might be a task carrying a reverse-direction operation */
285 static atomic_t rpc_pid;
287 task->tk_pid = atomic_inc_return(&rpc_pid);
291 task->tk_pid = atomic_inc_return(&clnt->cl_pid);
294 static inline void rpc_task_set_debuginfo(struct rpc_task *task)
299 static void rpc_set_active(struct rpc_task *task)
301 rpc_task_set_debuginfo(task);
302 set_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
303 trace_rpc_task_begin(task, NULL);
307 * Mark an RPC call as having completed by clearing the 'active' bit
308 * and then waking up all tasks that were sleeping.
310 static int rpc_complete_task(struct rpc_task *task)
312 void *m = &task->tk_runstate;
313 wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE);
314 struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE);
318 trace_rpc_task_complete(task, NULL);
320 spin_lock_irqsave(&wq->lock, flags);
321 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
322 ret = atomic_dec_and_test(&task->tk_count);
323 if (waitqueue_active(wq))
324 __wake_up_locked_key(wq, TASK_NORMAL, &k);
325 spin_unlock_irqrestore(&wq->lock, flags);
330 * Allow callers to wait for completion of an RPC call
332 * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit()
333 * to enforce taking of the wq->lock and hence avoid races with
334 * rpc_complete_task().
336 int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action)
339 action = rpc_wait_bit_killable;
340 return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
341 action, TASK_KILLABLE);
343 EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
346 * Make an RPC task runnable.
348 * Note: If the task is ASYNC, and is being made runnable after sitting on an
349 * rpc_wait_queue, this must be called with the queue spinlock held to protect
350 * the wait queue operation.
351 * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(),
352 * which is needed to ensure that __rpc_execute() doesn't loop (due to the
353 * lockless RPC_IS_QUEUED() test) before we've had a chance to test
354 * the RPC_TASK_RUNNING flag.
356 static void rpc_make_runnable(struct workqueue_struct *wq,
357 struct rpc_task *task)
359 bool need_wakeup = !rpc_test_and_set_running(task);
361 rpc_clear_queued(task);
364 if (RPC_IS_ASYNC(task)) {
365 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
366 queue_work(wq, &task->u.tk_work);
368 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
372 * Prepare for sleeping on a wait queue.
373 * By always appending tasks to the list we ensure FIFO behavior.
374 * NB: An RPC task will only receive interrupt-driven events as long
375 * as it's on a wait queue.
377 static void __rpc_do_sleep_on_priority(struct rpc_wait_queue *q,
378 struct rpc_task *task,
379 unsigned char queue_priority)
381 trace_rpc_task_sleep(task, q);
383 __rpc_add_wait_queue(q, task, queue_priority);
386 static void __rpc_sleep_on_priority(struct rpc_wait_queue *q,
387 struct rpc_task *task,
388 unsigned char queue_priority)
390 if (WARN_ON_ONCE(RPC_IS_QUEUED(task)))
392 __rpc_do_sleep_on_priority(q, task, queue_priority);
395 static void __rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q,
396 struct rpc_task *task, unsigned long timeout,
397 unsigned char queue_priority)
399 if (WARN_ON_ONCE(RPC_IS_QUEUED(task)))
401 if (time_is_after_jiffies(timeout)) {
402 __rpc_do_sleep_on_priority(q, task, queue_priority);
403 __rpc_add_timer(q, task, timeout);
405 task->tk_status = -ETIMEDOUT;
408 static void rpc_set_tk_callback(struct rpc_task *task, rpc_action action)
410 if (action && !WARN_ON_ONCE(task->tk_callback != NULL))
411 task->tk_callback = action;
414 static bool rpc_sleep_check_activated(struct rpc_task *task)
416 /* We shouldn't ever put an inactive task to sleep */
417 if (WARN_ON_ONCE(!RPC_IS_ACTIVATED(task))) {
418 task->tk_status = -EIO;
419 rpc_put_task_async(task);
425 void rpc_sleep_on_timeout(struct rpc_wait_queue *q, struct rpc_task *task,
426 rpc_action action, unsigned long timeout)
428 if (!rpc_sleep_check_activated(task))
431 rpc_set_tk_callback(task, action);
434 * Protect the queue operations.
437 __rpc_sleep_on_priority_timeout(q, task, timeout, task->tk_priority);
438 spin_unlock(&q->lock);
440 EXPORT_SYMBOL_GPL(rpc_sleep_on_timeout);
442 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
445 if (!rpc_sleep_check_activated(task))
448 rpc_set_tk_callback(task, action);
450 WARN_ON_ONCE(task->tk_timeout != 0);
452 * Protect the queue operations.
455 __rpc_sleep_on_priority(q, task, task->tk_priority);
456 spin_unlock(&q->lock);
458 EXPORT_SYMBOL_GPL(rpc_sleep_on);
460 void rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q,
461 struct rpc_task *task, unsigned long timeout, int priority)
463 if (!rpc_sleep_check_activated(task))
466 priority -= RPC_PRIORITY_LOW;
468 * Protect the queue operations.
471 __rpc_sleep_on_priority_timeout(q, task, timeout, priority);
472 spin_unlock(&q->lock);
474 EXPORT_SYMBOL_GPL(rpc_sleep_on_priority_timeout);
476 void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task,
479 if (!rpc_sleep_check_activated(task))
482 WARN_ON_ONCE(task->tk_timeout != 0);
483 priority -= RPC_PRIORITY_LOW;
485 * Protect the queue operations.
488 __rpc_sleep_on_priority(q, task, priority);
489 spin_unlock(&q->lock);
491 EXPORT_SYMBOL_GPL(rpc_sleep_on_priority);
494 * __rpc_do_wake_up_task_on_wq - wake up a single rpc_task
495 * @wq: workqueue on which to run task
497 * @task: task to be woken up
499 * Caller must hold queue->lock, and have cleared the task queued flag.
501 static void __rpc_do_wake_up_task_on_wq(struct workqueue_struct *wq,
502 struct rpc_wait_queue *queue,
503 struct rpc_task *task)
505 /* Has the task been executed yet? If not, we cannot wake it up! */
506 if (!RPC_IS_ACTIVATED(task)) {
507 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
511 trace_rpc_task_wakeup(task, queue);
513 __rpc_remove_wait_queue(queue, task);
515 rpc_make_runnable(wq, task);
519 * Wake up a queued task while the queue lock is being held
521 static struct rpc_task *
522 rpc_wake_up_task_on_wq_queue_action_locked(struct workqueue_struct *wq,
523 struct rpc_wait_queue *queue, struct rpc_task *task,
524 bool (*action)(struct rpc_task *, void *), void *data)
526 if (RPC_IS_QUEUED(task)) {
528 if (task->tk_waitqueue == queue) {
529 if (action == NULL || action(task, data)) {
530 __rpc_do_wake_up_task_on_wq(wq, queue, task);
539 * Wake up a queued task while the queue lock is being held
541 static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue,
542 struct rpc_task *task)
544 rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue,
549 * Wake up a task on a specific queue
551 void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
553 if (!RPC_IS_QUEUED(task))
555 spin_lock(&queue->lock);
556 rpc_wake_up_task_queue_locked(queue, task);
557 spin_unlock(&queue->lock);
559 EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
561 static bool rpc_task_action_set_status(struct rpc_task *task, void *status)
563 task->tk_status = *(int *)status;
568 rpc_wake_up_task_queue_set_status_locked(struct rpc_wait_queue *queue,
569 struct rpc_task *task, int status)
571 rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue,
572 task, rpc_task_action_set_status, &status);
576 * rpc_wake_up_queued_task_set_status - wake up a task and set task->tk_status
577 * @queue: pointer to rpc_wait_queue
578 * @task: pointer to rpc_task
579 * @status: integer error value
581 * If @task is queued on @queue, then it is woken up, and @task->tk_status is
582 * set to the value of @status.
585 rpc_wake_up_queued_task_set_status(struct rpc_wait_queue *queue,
586 struct rpc_task *task, int status)
588 if (!RPC_IS_QUEUED(task))
590 spin_lock(&queue->lock);
591 rpc_wake_up_task_queue_set_status_locked(queue, task, status);
592 spin_unlock(&queue->lock);
596 * Wake up the next task on a priority queue.
598 static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue)
601 struct rpc_task *task;
604 * Service the privileged queue.
606 q = &queue->tasks[RPC_NR_PRIORITY - 1];
607 if (queue->maxpriority > RPC_PRIORITY_PRIVILEGED && !list_empty(q)) {
608 task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
613 * Service a batch of tasks from a single owner.
615 q = &queue->tasks[queue->priority];
616 if (!list_empty(q) && queue->nr) {
618 task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
623 * Service the next queue.
626 if (q == &queue->tasks[0])
627 q = &queue->tasks[queue->maxpriority];
630 if (!list_empty(q)) {
631 task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
634 } while (q != &queue->tasks[queue->priority]);
636 rpc_reset_waitqueue_priority(queue);
640 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
645 static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue)
647 if (RPC_IS_PRIORITY(queue))
648 return __rpc_find_next_queued_priority(queue);
649 if (!list_empty(&queue->tasks[0]))
650 return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list);
655 * Wake up the first task on the wait queue.
657 struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq,
658 struct rpc_wait_queue *queue,
659 bool (*func)(struct rpc_task *, void *), void *data)
661 struct rpc_task *task = NULL;
663 spin_lock(&queue->lock);
664 task = __rpc_find_next_queued(queue);
666 task = rpc_wake_up_task_on_wq_queue_action_locked(wq, queue,
668 spin_unlock(&queue->lock);
674 * Wake up the first task on the wait queue.
676 struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue,
677 bool (*func)(struct rpc_task *, void *), void *data)
679 return rpc_wake_up_first_on_wq(rpciod_workqueue, queue, func, data);
681 EXPORT_SYMBOL_GPL(rpc_wake_up_first);
683 static bool rpc_wake_up_next_func(struct rpc_task *task, void *data)
689 * Wake up the next task on the wait queue.
691 struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue)
693 return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL);
695 EXPORT_SYMBOL_GPL(rpc_wake_up_next);
698 * rpc_wake_up_locked - wake up all rpc_tasks
699 * @queue: rpc_wait_queue on which the tasks are sleeping
702 static void rpc_wake_up_locked(struct rpc_wait_queue *queue)
704 struct rpc_task *task;
707 task = __rpc_find_next_queued(queue);
710 rpc_wake_up_task_queue_locked(queue, task);
715 * rpc_wake_up - wake up all rpc_tasks
716 * @queue: rpc_wait_queue on which the tasks are sleeping
720 void rpc_wake_up(struct rpc_wait_queue *queue)
722 spin_lock(&queue->lock);
723 rpc_wake_up_locked(queue);
724 spin_unlock(&queue->lock);
726 EXPORT_SYMBOL_GPL(rpc_wake_up);
729 * rpc_wake_up_status_locked - wake up all rpc_tasks and set their status value.
730 * @queue: rpc_wait_queue on which the tasks are sleeping
731 * @status: status value to set
733 static void rpc_wake_up_status_locked(struct rpc_wait_queue *queue, int status)
735 struct rpc_task *task;
738 task = __rpc_find_next_queued(queue);
741 rpc_wake_up_task_queue_set_status_locked(queue, task, status);
746 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
747 * @queue: rpc_wait_queue on which the tasks are sleeping
748 * @status: status value to set
752 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
754 spin_lock(&queue->lock);
755 rpc_wake_up_status_locked(queue, status);
756 spin_unlock(&queue->lock);
758 EXPORT_SYMBOL_GPL(rpc_wake_up_status);
760 static void __rpc_queue_timer_fn(struct work_struct *work)
762 struct rpc_wait_queue *queue = container_of(work,
763 struct rpc_wait_queue,
764 timer_list.dwork.work);
765 struct rpc_task *task, *n;
766 unsigned long expires, now, timeo;
768 spin_lock(&queue->lock);
769 expires = now = jiffies;
770 list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
771 timeo = task->tk_timeout;
772 if (time_after_eq(now, timeo)) {
773 trace_rpc_task_timeout(task, task->tk_action);
774 task->tk_status = -ETIMEDOUT;
775 rpc_wake_up_task_queue_locked(queue, task);
778 if (expires == now || time_after(expires, timeo))
781 if (!list_empty(&queue->timer_list.list))
782 rpc_set_queue_timer(queue, expires);
783 spin_unlock(&queue->lock);
786 static void __rpc_atrun(struct rpc_task *task)
788 if (task->tk_status == -ETIMEDOUT)
793 * Run a task at a later time
795 void rpc_delay(struct rpc_task *task, unsigned long delay)
797 rpc_sleep_on_timeout(&delay_queue, task, __rpc_atrun, jiffies + delay);
799 EXPORT_SYMBOL_GPL(rpc_delay);
802 * Helper to call task->tk_ops->rpc_call_prepare
804 void rpc_prepare_task(struct rpc_task *task)
806 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
810 rpc_init_task_statistics(struct rpc_task *task)
812 /* Initialize retry counters */
813 task->tk_garb_retry = 2;
814 task->tk_cred_retry = 2;
815 task->tk_rebind_retry = 2;
817 /* starting timestamp */
818 task->tk_start = ktime_get();
822 rpc_reset_task_statistics(struct rpc_task *task)
824 task->tk_timeouts = 0;
825 task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_SENT);
826 rpc_init_task_statistics(task);
830 * Helper that calls task->tk_ops->rpc_call_done if it exists
832 void rpc_exit_task(struct rpc_task *task)
834 trace_rpc_task_end(task, task->tk_action);
835 task->tk_action = NULL;
836 if (task->tk_ops->rpc_count_stats)
837 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
838 else if (task->tk_client)
839 rpc_count_iostats(task, task->tk_client->cl_metrics);
840 if (task->tk_ops->rpc_call_done != NULL) {
841 trace_rpc_task_call_done(task, task->tk_ops->rpc_call_done);
842 task->tk_ops->rpc_call_done(task, task->tk_calldata);
843 if (task->tk_action != NULL) {
844 /* Always release the RPC slot and buffer memory */
846 rpc_reset_task_statistics(task);
851 void rpc_signal_task(struct rpc_task *task)
853 struct rpc_wait_queue *queue;
855 if (!RPC_IS_ACTIVATED(task))
858 trace_rpc_task_signalled(task, task->tk_action);
859 set_bit(RPC_TASK_SIGNALLED, &task->tk_runstate);
860 smp_mb__after_atomic();
861 queue = READ_ONCE(task->tk_waitqueue);
863 rpc_wake_up_queued_task_set_status(queue, task, -ERESTARTSYS);
866 void rpc_exit(struct rpc_task *task, int status)
868 task->tk_status = status;
869 task->tk_action = rpc_exit_task;
870 rpc_wake_up_queued_task(task->tk_waitqueue, task);
872 EXPORT_SYMBOL_GPL(rpc_exit);
874 void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
876 if (ops->rpc_release != NULL)
877 ops->rpc_release(calldata);
880 static bool xprt_needs_memalloc(struct rpc_xprt *xprt, struct rpc_task *tk)
884 if (!atomic_read(&xprt->swapper))
886 return test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == tk;
890 * This is the RPC `scheduler' (or rather, the finite state machine).
892 static void __rpc_execute(struct rpc_task *task)
894 struct rpc_wait_queue *queue;
895 int task_is_async = RPC_IS_ASYNC(task);
897 unsigned long pflags = current->flags;
899 WARN_ON_ONCE(RPC_IS_QUEUED(task));
900 if (RPC_IS_QUEUED(task))
904 void (*do_action)(struct rpc_task *);
907 * Perform the next FSM step or a pending callback.
909 * tk_action may be NULL if the task has been killed.
910 * In particular, note that rpc_killall_tasks may
911 * do this at any time, so beware when dereferencing.
913 do_action = task->tk_action;
914 if (task->tk_callback) {
915 do_action = task->tk_callback;
916 task->tk_callback = NULL;
920 if (RPC_IS_SWAPPER(task) ||
921 xprt_needs_memalloc(task->tk_xprt, task))
922 current->flags |= PF_MEMALLOC;
924 trace_rpc_task_run_action(task, do_action);
928 * Lockless check for whether task is sleeping or not.
930 if (!RPC_IS_QUEUED(task)) {
936 * Signalled tasks should exit rather than sleep.
938 if (RPC_SIGNALLED(task)) {
939 task->tk_rpc_status = -ERESTARTSYS;
940 rpc_exit(task, -ERESTARTSYS);
944 * The queue->lock protects against races with
945 * rpc_make_runnable().
947 * Note that once we clear RPC_TASK_RUNNING on an asynchronous
948 * rpc_task, rpc_make_runnable() can assign it to a
949 * different workqueue. We therefore cannot assume that the
950 * rpc_task pointer may still be dereferenced.
952 queue = task->tk_waitqueue;
953 spin_lock(&queue->lock);
954 if (!RPC_IS_QUEUED(task)) {
955 spin_unlock(&queue->lock);
958 rpc_clear_running(task);
959 spin_unlock(&queue->lock);
963 /* sync task: sleep here */
964 trace_rpc_task_sync_sleep(task, task->tk_action);
965 status = out_of_line_wait_on_bit(&task->tk_runstate,
966 RPC_TASK_QUEUED, rpc_wait_bit_killable,
970 * When a sync task receives a signal, it exits with
971 * -ERESTARTSYS. In order to catch any callbacks that
972 * clean up after sleeping on some queue, we don't
973 * break the loop here, but go around once more.
975 trace_rpc_task_signalled(task, task->tk_action);
976 set_bit(RPC_TASK_SIGNALLED, &task->tk_runstate);
977 task->tk_rpc_status = -ERESTARTSYS;
978 rpc_exit(task, -ERESTARTSYS);
980 trace_rpc_task_sync_wake(task, task->tk_action);
983 /* Release all resources associated with the task */
984 rpc_release_task(task);
986 current_restore_flags(pflags, PF_MEMALLOC);
990 * User-visible entry point to the scheduler.
992 * This may be called recursively if e.g. an async NFS task updates
993 * the attributes and finds that dirty pages must be flushed.
994 * NOTE: Upon exit of this function the task is guaranteed to be
995 * released. In particular note that tk_release() will have
996 * been called, so your task memory may have been freed.
998 void rpc_execute(struct rpc_task *task)
1000 bool is_async = RPC_IS_ASYNC(task);
1002 rpc_set_active(task);
1003 rpc_make_runnable(rpciod_workqueue, task);
1005 unsigned int pflags = memalloc_nofs_save();
1006 __rpc_execute(task);
1007 memalloc_nofs_restore(pflags);
1011 static void rpc_async_schedule(struct work_struct *work)
1013 unsigned int pflags = memalloc_nofs_save();
1015 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
1016 memalloc_nofs_restore(pflags);
1020 * rpc_malloc - allocate RPC buffer resources
1023 * A single memory region is allocated, which is split between the
1024 * RPC call and RPC reply that this task is being used for. When
1025 * this RPC is retired, the memory is released by calling rpc_free.
1027 * To prevent rpciod from hanging, this allocator never sleeps,
1028 * returning -ENOMEM and suppressing warning if the request cannot
1029 * be serviced immediately. The caller can arrange to sleep in a
1030 * way that is safe for rpciod.
1032 * Most requests are 'small' (under 2KiB) and can be serviced from a
1033 * mempool, ensuring that NFS reads and writes can always proceed,
1034 * and that there is good locality of reference for these buffers.
1036 int rpc_malloc(struct rpc_task *task)
1038 struct rpc_rqst *rqst = task->tk_rqstp;
1039 size_t size = rqst->rq_callsize + rqst->rq_rcvsize;
1040 struct rpc_buffer *buf;
1041 gfp_t gfp = rpc_task_gfp_mask();
1043 size += sizeof(struct rpc_buffer);
1044 if (size <= RPC_BUFFER_MAXSIZE) {
1045 buf = kmem_cache_alloc(rpc_buffer_slabp, gfp);
1046 /* Reach for the mempool if dynamic allocation fails */
1047 if (!buf && RPC_IS_ASYNC(task))
1048 buf = mempool_alloc(rpc_buffer_mempool, GFP_NOWAIT);
1050 buf = kmalloc(size, gfp);
1056 rqst->rq_buffer = buf->data;
1057 rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
1060 EXPORT_SYMBOL_GPL(rpc_malloc);
1063 * rpc_free - free RPC buffer resources allocated via rpc_malloc
1067 void rpc_free(struct rpc_task *task)
1069 void *buffer = task->tk_rqstp->rq_buffer;
1071 struct rpc_buffer *buf;
1073 buf = container_of(buffer, struct rpc_buffer, data);
1076 if (size <= RPC_BUFFER_MAXSIZE)
1077 mempool_free(buf, rpc_buffer_mempool);
1081 EXPORT_SYMBOL_GPL(rpc_free);
1084 * Creation and deletion of RPC task structures
1086 static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
1088 memset(task, 0, sizeof(*task));
1089 atomic_set(&task->tk_count, 1);
1090 task->tk_flags = task_setup_data->flags;
1091 task->tk_ops = task_setup_data->callback_ops;
1092 task->tk_calldata = task_setup_data->callback_data;
1093 INIT_LIST_HEAD(&task->tk_task);
1095 task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
1096 task->tk_owner = current->tgid;
1098 /* Initialize workqueue for async tasks */
1099 task->tk_workqueue = task_setup_data->workqueue;
1101 task->tk_xprt = rpc_task_get_xprt(task_setup_data->rpc_client,
1102 xprt_get(task_setup_data->rpc_xprt));
1104 task->tk_op_cred = get_rpccred(task_setup_data->rpc_op_cred);
1106 if (task->tk_ops->rpc_call_prepare != NULL)
1107 task->tk_action = rpc_prepare_task;
1109 rpc_init_task_statistics(task);
1112 static struct rpc_task *rpc_alloc_task(void)
1114 struct rpc_task *task;
1116 task = kmem_cache_alloc(rpc_task_slabp, rpc_task_gfp_mask());
1119 return mempool_alloc(rpc_task_mempool, GFP_NOWAIT);
1123 * Create a new task for the specified client.
1125 struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
1127 struct rpc_task *task = setup_data->task;
1128 unsigned short flags = 0;
1131 task = rpc_alloc_task();
1133 rpc_release_calldata(setup_data->callback_ops,
1134 setup_data->callback_data);
1135 return ERR_PTR(-ENOMEM);
1137 flags = RPC_TASK_DYNAMIC;
1140 rpc_init_task(task, setup_data);
1141 task->tk_flags |= flags;
1146 * rpc_free_task - release rpc task and perform cleanups
1148 * Note that we free up the rpc_task _after_ rpc_release_calldata()
1149 * in order to work around a workqueue dependency issue.
1152 * "Workqueue currently considers two work items to be the same if they're
1153 * on the same address and won't execute them concurrently - ie. it
1154 * makes a work item which is queued again while being executed wait
1155 * for the previous execution to complete.
1157 * If a work function frees the work item, and then waits for an event
1158 * which should be performed by another work item and *that* work item
1159 * recycles the freed work item, it can create a false dependency loop.
1160 * There really is no reliable way to detect this short of verifying
1161 * every memory free."
1164 static void rpc_free_task(struct rpc_task *task)
1166 unsigned short tk_flags = task->tk_flags;
1168 put_rpccred(task->tk_op_cred);
1169 rpc_release_calldata(task->tk_ops, task->tk_calldata);
1171 if (tk_flags & RPC_TASK_DYNAMIC)
1172 mempool_free(task, rpc_task_mempool);
1175 static void rpc_async_release(struct work_struct *work)
1177 unsigned int pflags = memalloc_nofs_save();
1179 rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
1180 memalloc_nofs_restore(pflags);
1183 static void rpc_release_resources_task(struct rpc_task *task)
1186 if (task->tk_msg.rpc_cred) {
1187 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1188 put_cred(task->tk_msg.rpc_cred);
1189 task->tk_msg.rpc_cred = NULL;
1191 rpc_task_release_client(task);
1194 static void rpc_final_put_task(struct rpc_task *task,
1195 struct workqueue_struct *q)
1198 INIT_WORK(&task->u.tk_work, rpc_async_release);
1199 queue_work(q, &task->u.tk_work);
1201 rpc_free_task(task);
1204 static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
1206 if (atomic_dec_and_test(&task->tk_count)) {
1207 rpc_release_resources_task(task);
1208 rpc_final_put_task(task, q);
1212 void rpc_put_task(struct rpc_task *task)
1214 rpc_do_put_task(task, NULL);
1216 EXPORT_SYMBOL_GPL(rpc_put_task);
1218 void rpc_put_task_async(struct rpc_task *task)
1220 rpc_do_put_task(task, task->tk_workqueue);
1222 EXPORT_SYMBOL_GPL(rpc_put_task_async);
1224 static void rpc_release_task(struct rpc_task *task)
1226 WARN_ON_ONCE(RPC_IS_QUEUED(task));
1228 rpc_release_resources_task(task);
1231 * Note: at this point we have been removed from rpc_clnt->cl_tasks,
1232 * so it should be safe to use task->tk_count as a test for whether
1233 * or not any other processes still hold references to our rpc_task.
1235 if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
1236 /* Wake up anyone who may be waiting for task completion */
1237 if (!rpc_complete_task(task))
1240 if (!atomic_dec_and_test(&task->tk_count))
1243 rpc_final_put_task(task, task->tk_workqueue);
1248 return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
1251 void rpciod_down(void)
1253 module_put(THIS_MODULE);
1257 * Start up the rpciod workqueue.
1259 static int rpciod_start(void)
1261 struct workqueue_struct *wq;
1264 * Create the rpciod thread and wait for it to start.
1266 wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
1269 rpciod_workqueue = wq;
1270 wq = alloc_workqueue("xprtiod", WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
1273 xprtiod_workqueue = wq;
1276 wq = rpciod_workqueue;
1277 rpciod_workqueue = NULL;
1278 destroy_workqueue(wq);
1283 static void rpciod_stop(void)
1285 struct workqueue_struct *wq = NULL;
1287 if (rpciod_workqueue == NULL)
1290 wq = rpciod_workqueue;
1291 rpciod_workqueue = NULL;
1292 destroy_workqueue(wq);
1293 wq = xprtiod_workqueue;
1294 xprtiod_workqueue = NULL;
1295 destroy_workqueue(wq);
1299 rpc_destroy_mempool(void)
1302 mempool_destroy(rpc_buffer_mempool);
1303 mempool_destroy(rpc_task_mempool);
1304 kmem_cache_destroy(rpc_task_slabp);
1305 kmem_cache_destroy(rpc_buffer_slabp);
1306 rpc_destroy_wait_queue(&delay_queue);
1310 rpc_init_mempool(void)
1313 * The following is not strictly a mempool initialisation,
1314 * but there is no harm in doing it here
1316 rpc_init_wait_queue(&delay_queue, "delayq");
1317 if (!rpciod_start())
1320 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1321 sizeof(struct rpc_task),
1322 0, SLAB_HWCACHE_ALIGN,
1324 if (!rpc_task_slabp)
1326 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1328 0, SLAB_HWCACHE_ALIGN,
1330 if (!rpc_buffer_slabp)
1332 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1334 if (!rpc_task_mempool)
1336 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1338 if (!rpc_buffer_mempool)
1342 rpc_destroy_mempool();