drm/radeon: Always flush the VM
[firefly-linux-kernel-4.4.55.git] / net / sunrpc / sched.c
1 /*
2  * linux/net/sunrpc/sched.c
3  *
4  * Scheduling for synchronous and asynchronous RPC requests.
5  *
6  * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de>
7  *
8  * TCP NFS related read + write fixes
9  * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10  */
11
12 #include <linux/module.h>
13
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/mempool.h>
18 #include <linux/smp.h>
19 #include <linux/spinlock.h>
20 #include <linux/mutex.h>
21 #include <linux/freezer.h>
22
23 #include <linux/sunrpc/clnt.h>
24
25 #include "sunrpc.h"
26
27 #ifdef RPC_DEBUG
28 #define RPCDBG_FACILITY         RPCDBG_SCHED
29 #endif
30
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/sunrpc.h>
33
34 /*
35  * RPC slabs and memory pools
36  */
37 #define RPC_BUFFER_MAXSIZE      (2048)
38 #define RPC_BUFFER_POOLSIZE     (8)
39 #define RPC_TASK_POOLSIZE       (8)
40 static struct kmem_cache        *rpc_task_slabp __read_mostly;
41 static struct kmem_cache        *rpc_buffer_slabp __read_mostly;
42 static mempool_t        *rpc_task_mempool __read_mostly;
43 static mempool_t        *rpc_buffer_mempool __read_mostly;
44
45 static void                     rpc_async_schedule(struct work_struct *);
46 static void                      rpc_release_task(struct rpc_task *task);
47 static void __rpc_queue_timer_fn(unsigned long ptr);
48
49 /*
50  * RPC tasks sit here while waiting for conditions to improve.
51  */
52 static struct rpc_wait_queue delay_queue;
53
54 /*
55  * rpciod-related stuff
56  */
57 struct workqueue_struct *rpciod_workqueue;
58
59 /*
60  * Disable the timer for a given RPC task. Should be called with
61  * queue->lock and bh_disabled in order to avoid races within
62  * rpc_run_timer().
63  */
64 static void
65 __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
66 {
67         if (task->tk_timeout == 0)
68                 return;
69         dprintk("RPC: %5u disabling timer\n", task->tk_pid);
70         task->tk_timeout = 0;
71         list_del(&task->u.tk_wait.timer_list);
72         if (list_empty(&queue->timer_list.list))
73                 del_timer(&queue->timer_list.timer);
74 }
75
76 static void
77 rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires)
78 {
79         queue->timer_list.expires = expires;
80         mod_timer(&queue->timer_list.timer, expires);
81 }
82
83 /*
84  * Set up a timer for the current task.
85  */
86 static void
87 __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task)
88 {
89         if (!task->tk_timeout)
90                 return;
91
92         dprintk("RPC: %5u setting alarm for %lu ms\n",
93                         task->tk_pid, task->tk_timeout * 1000 / HZ);
94
95         task->u.tk_wait.expires = jiffies + task->tk_timeout;
96         if (list_empty(&queue->timer_list.list) || time_before(task->u.tk_wait.expires, queue->timer_list.expires))
97                 rpc_set_queue_timer(queue, task->u.tk_wait.expires);
98         list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list);
99 }
100
101 static void rpc_rotate_queue_owner(struct rpc_wait_queue *queue)
102 {
103         struct list_head *q = &queue->tasks[queue->priority];
104         struct rpc_task *task;
105
106         if (!list_empty(q)) {
107                 task = list_first_entry(q, struct rpc_task, u.tk_wait.list);
108                 if (task->tk_owner == queue->owner)
109                         list_move_tail(&task->u.tk_wait.list, q);
110         }
111 }
112
113 static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
114 {
115         if (queue->priority != priority) {
116                 /* Fairness: rotate the list when changing priority */
117                 rpc_rotate_queue_owner(queue);
118                 queue->priority = priority;
119         }
120 }
121
122 static void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid)
123 {
124         queue->owner = pid;
125         queue->nr = RPC_BATCH_COUNT;
126 }
127
128 static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
129 {
130         rpc_set_waitqueue_priority(queue, queue->maxpriority);
131         rpc_set_waitqueue_owner(queue, 0);
132 }
133
134 /*
135  * Add new request to a priority queue.
136  */
137 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue,
138                 struct rpc_task *task,
139                 unsigned char queue_priority)
140 {
141         struct list_head *q;
142         struct rpc_task *t;
143
144         INIT_LIST_HEAD(&task->u.tk_wait.links);
145         if (unlikely(queue_priority > queue->maxpriority))
146                 queue_priority = queue->maxpriority;
147         if (queue_priority > queue->priority)
148                 rpc_set_waitqueue_priority(queue, queue_priority);
149         q = &queue->tasks[queue_priority];
150         list_for_each_entry(t, q, u.tk_wait.list) {
151                 if (t->tk_owner == task->tk_owner) {
152                         list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
153                         return;
154                 }
155         }
156         list_add_tail(&task->u.tk_wait.list, q);
157 }
158
159 /*
160  * Add new request to wait queue.
161  *
162  * Swapper tasks always get inserted at the head of the queue.
163  * This should avoid many nasty memory deadlocks and hopefully
164  * improve overall performance.
165  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
166  */
167 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue,
168                 struct rpc_task *task,
169                 unsigned char queue_priority)
170 {
171         WARN_ON_ONCE(RPC_IS_QUEUED(task));
172         if (RPC_IS_QUEUED(task))
173                 return;
174
175         if (RPC_IS_PRIORITY(queue))
176                 __rpc_add_wait_queue_priority(queue, task, queue_priority);
177         else if (RPC_IS_SWAPPER(task))
178                 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
179         else
180                 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
181         task->tk_waitqueue = queue;
182         queue->qlen++;
183         rpc_set_queued(task);
184
185         dprintk("RPC: %5u added to queue %p \"%s\"\n",
186                         task->tk_pid, queue, rpc_qname(queue));
187 }
188
189 /*
190  * Remove request from a priority queue.
191  */
192 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
193 {
194         struct rpc_task *t;
195
196         if (!list_empty(&task->u.tk_wait.links)) {
197                 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
198                 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
199                 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
200         }
201 }
202
203 /*
204  * Remove request from queue.
205  * Note: must be called with spin lock held.
206  */
207 static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
208 {
209         __rpc_disable_timer(queue, task);
210         if (RPC_IS_PRIORITY(queue))
211                 __rpc_remove_wait_queue_priority(task);
212         list_del(&task->u.tk_wait.list);
213         queue->qlen--;
214         dprintk("RPC: %5u removed from queue %p \"%s\"\n",
215                         task->tk_pid, queue, rpc_qname(queue));
216 }
217
218 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues)
219 {
220         int i;
221
222         spin_lock_init(&queue->lock);
223         for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
224                 INIT_LIST_HEAD(&queue->tasks[i]);
225         queue->maxpriority = nr_queues - 1;
226         rpc_reset_waitqueue_priority(queue);
227         queue->qlen = 0;
228         setup_timer(&queue->timer_list.timer, __rpc_queue_timer_fn, (unsigned long)queue);
229         INIT_LIST_HEAD(&queue->timer_list.list);
230         rpc_assign_waitqueue_name(queue, qname);
231 }
232
233 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
234 {
235         __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY);
236 }
237 EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue);
238
239 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
240 {
241         __rpc_init_priority_wait_queue(queue, qname, 1);
242 }
243 EXPORT_SYMBOL_GPL(rpc_init_wait_queue);
244
245 void rpc_destroy_wait_queue(struct rpc_wait_queue *queue)
246 {
247         del_timer_sync(&queue->timer_list.timer);
248 }
249 EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue);
250
251 static int rpc_wait_bit_killable(void *word)
252 {
253         if (fatal_signal_pending(current))
254                 return -ERESTARTSYS;
255         freezable_schedule();
256         return 0;
257 }
258
259 #ifdef RPC_DEBUG
260 static void rpc_task_set_debuginfo(struct rpc_task *task)
261 {
262         static atomic_t rpc_pid;
263
264         task->tk_pid = atomic_inc_return(&rpc_pid);
265 }
266 #else
267 static inline void rpc_task_set_debuginfo(struct rpc_task *task)
268 {
269 }
270 #endif
271
272 static void rpc_set_active(struct rpc_task *task)
273 {
274         trace_rpc_task_begin(task->tk_client, task, NULL);
275
276         rpc_task_set_debuginfo(task);
277         set_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
278 }
279
280 /*
281  * Mark an RPC call as having completed by clearing the 'active' bit
282  * and then waking up all tasks that were sleeping.
283  */
284 static int rpc_complete_task(struct rpc_task *task)
285 {
286         void *m = &task->tk_runstate;
287         wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE);
288         struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE);
289         unsigned long flags;
290         int ret;
291
292         trace_rpc_task_complete(task->tk_client, task, NULL);
293
294         spin_lock_irqsave(&wq->lock, flags);
295         clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
296         ret = atomic_dec_and_test(&task->tk_count);
297         if (waitqueue_active(wq))
298                 __wake_up_locked_key(wq, TASK_NORMAL, &k);
299         spin_unlock_irqrestore(&wq->lock, flags);
300         return ret;
301 }
302
303 /*
304  * Allow callers to wait for completion of an RPC call
305  *
306  * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit()
307  * to enforce taking of the wq->lock and hence avoid races with
308  * rpc_complete_task().
309  */
310 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
311 {
312         if (action == NULL)
313                 action = rpc_wait_bit_killable;
314         return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
315                         action, TASK_KILLABLE);
316 }
317 EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task);
318
319 /*
320  * Make an RPC task runnable.
321  *
322  * Note: If the task is ASYNC, and is being made runnable after sitting on an
323  * rpc_wait_queue, this must be called with the queue spinlock held to protect
324  * the wait queue operation.
325  */
326 static void rpc_make_runnable(struct rpc_task *task)
327 {
328         rpc_clear_queued(task);
329         if (rpc_test_and_set_running(task))
330                 return;
331         if (RPC_IS_ASYNC(task)) {
332                 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
333                 queue_work(rpciod_workqueue, &task->u.tk_work);
334         } else
335                 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
336 }
337
338 /*
339  * Prepare for sleeping on a wait queue.
340  * By always appending tasks to the list we ensure FIFO behavior.
341  * NB: An RPC task will only receive interrupt-driven events as long
342  * as it's on a wait queue.
343  */
344 static void __rpc_sleep_on_priority(struct rpc_wait_queue *q,
345                 struct rpc_task *task,
346                 rpc_action action,
347                 unsigned char queue_priority)
348 {
349         dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
350                         task->tk_pid, rpc_qname(q), jiffies);
351
352         trace_rpc_task_sleep(task->tk_client, task, q);
353
354         __rpc_add_wait_queue(q, task, queue_priority);
355
356         WARN_ON_ONCE(task->tk_callback != NULL);
357         task->tk_callback = action;
358         __rpc_add_timer(q, task);
359 }
360
361 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
362                                 rpc_action action)
363 {
364         /* We shouldn't ever put an inactive task to sleep */
365         WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
366         if (!RPC_IS_ACTIVATED(task)) {
367                 task->tk_status = -EIO;
368                 rpc_put_task_async(task);
369                 return;
370         }
371
372         /*
373          * Protect the queue operations.
374          */
375         spin_lock_bh(&q->lock);
376         __rpc_sleep_on_priority(q, task, action, task->tk_priority);
377         spin_unlock_bh(&q->lock);
378 }
379 EXPORT_SYMBOL_GPL(rpc_sleep_on);
380
381 void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task,
382                 rpc_action action, int priority)
383 {
384         /* We shouldn't ever put an inactive task to sleep */
385         WARN_ON_ONCE(!RPC_IS_ACTIVATED(task));
386         if (!RPC_IS_ACTIVATED(task)) {
387                 task->tk_status = -EIO;
388                 rpc_put_task_async(task);
389                 return;
390         }
391
392         /*
393          * Protect the queue operations.
394          */
395         spin_lock_bh(&q->lock);
396         __rpc_sleep_on_priority(q, task, action, priority - RPC_PRIORITY_LOW);
397         spin_unlock_bh(&q->lock);
398 }
399 EXPORT_SYMBOL_GPL(rpc_sleep_on_priority);
400
401 /**
402  * __rpc_do_wake_up_task - wake up a single rpc_task
403  * @queue: wait queue
404  * @task: task to be woken up
405  *
406  * Caller must hold queue->lock, and have cleared the task queued flag.
407  */
408 static void __rpc_do_wake_up_task(struct rpc_wait_queue *queue, struct rpc_task *task)
409 {
410         dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
411                         task->tk_pid, jiffies);
412
413         /* Has the task been executed yet? If not, we cannot wake it up! */
414         if (!RPC_IS_ACTIVATED(task)) {
415                 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
416                 return;
417         }
418
419         trace_rpc_task_wakeup(task->tk_client, task, queue);
420
421         __rpc_remove_wait_queue(queue, task);
422
423         rpc_make_runnable(task);
424
425         dprintk("RPC:       __rpc_wake_up_task done\n");
426 }
427
428 /*
429  * Wake up a queued task while the queue lock is being held
430  */
431 static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task)
432 {
433         if (RPC_IS_QUEUED(task) && task->tk_waitqueue == queue)
434                 __rpc_do_wake_up_task(queue, task);
435 }
436
437 /*
438  * Tests whether rpc queue is empty
439  */
440 int rpc_queue_empty(struct rpc_wait_queue *queue)
441 {
442         int res;
443
444         spin_lock_bh(&queue->lock);
445         res = queue->qlen;
446         spin_unlock_bh(&queue->lock);
447         return res == 0;
448 }
449 EXPORT_SYMBOL_GPL(rpc_queue_empty);
450
451 /*
452  * Wake up a task on a specific queue
453  */
454 void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task)
455 {
456         spin_lock_bh(&queue->lock);
457         rpc_wake_up_task_queue_locked(queue, task);
458         spin_unlock_bh(&queue->lock);
459 }
460 EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task);
461
462 /*
463  * Wake up the next task on a priority queue.
464  */
465 static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue)
466 {
467         struct list_head *q;
468         struct rpc_task *task;
469
470         /*
471          * Service a batch of tasks from a single owner.
472          */
473         q = &queue->tasks[queue->priority];
474         if (!list_empty(q)) {
475                 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
476                 if (queue->owner == task->tk_owner) {
477                         if (--queue->nr)
478                                 goto out;
479                         list_move_tail(&task->u.tk_wait.list, q);
480                 }
481                 /*
482                  * Check if we need to switch queues.
483                  */
484                 goto new_owner;
485         }
486
487         /*
488          * Service the next queue.
489          */
490         do {
491                 if (q == &queue->tasks[0])
492                         q = &queue->tasks[queue->maxpriority];
493                 else
494                         q = q - 1;
495                 if (!list_empty(q)) {
496                         task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
497                         goto new_queue;
498                 }
499         } while (q != &queue->tasks[queue->priority]);
500
501         rpc_reset_waitqueue_priority(queue);
502         return NULL;
503
504 new_queue:
505         rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
506 new_owner:
507         rpc_set_waitqueue_owner(queue, task->tk_owner);
508 out:
509         return task;
510 }
511
512 static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue)
513 {
514         if (RPC_IS_PRIORITY(queue))
515                 return __rpc_find_next_queued_priority(queue);
516         if (!list_empty(&queue->tasks[0]))
517                 return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list);
518         return NULL;
519 }
520
521 /*
522  * Wake up the first task on the wait queue.
523  */
524 struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue,
525                 bool (*func)(struct rpc_task *, void *), void *data)
526 {
527         struct rpc_task *task = NULL;
528
529         dprintk("RPC:       wake_up_first(%p \"%s\")\n",
530                         queue, rpc_qname(queue));
531         spin_lock_bh(&queue->lock);
532         task = __rpc_find_next_queued(queue);
533         if (task != NULL) {
534                 if (func(task, data))
535                         rpc_wake_up_task_queue_locked(queue, task);
536                 else
537                         task = NULL;
538         }
539         spin_unlock_bh(&queue->lock);
540
541         return task;
542 }
543 EXPORT_SYMBOL_GPL(rpc_wake_up_first);
544
545 static bool rpc_wake_up_next_func(struct rpc_task *task, void *data)
546 {
547         return true;
548 }
549
550 /*
551  * Wake up the next task on the wait queue.
552 */
553 struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue)
554 {
555         return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL);
556 }
557 EXPORT_SYMBOL_GPL(rpc_wake_up_next);
558
559 /**
560  * rpc_wake_up - wake up all rpc_tasks
561  * @queue: rpc_wait_queue on which the tasks are sleeping
562  *
563  * Grabs queue->lock
564  */
565 void rpc_wake_up(struct rpc_wait_queue *queue)
566 {
567         struct list_head *head;
568
569         spin_lock_bh(&queue->lock);
570         head = &queue->tasks[queue->maxpriority];
571         for (;;) {
572                 while (!list_empty(head)) {
573                         struct rpc_task *task;
574                         task = list_first_entry(head,
575                                         struct rpc_task,
576                                         u.tk_wait.list);
577                         rpc_wake_up_task_queue_locked(queue, task);
578                 }
579                 if (head == &queue->tasks[0])
580                         break;
581                 head--;
582         }
583         spin_unlock_bh(&queue->lock);
584 }
585 EXPORT_SYMBOL_GPL(rpc_wake_up);
586
587 /**
588  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
589  * @queue: rpc_wait_queue on which the tasks are sleeping
590  * @status: status value to set
591  *
592  * Grabs queue->lock
593  */
594 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
595 {
596         struct list_head *head;
597
598         spin_lock_bh(&queue->lock);
599         head = &queue->tasks[queue->maxpriority];
600         for (;;) {
601                 while (!list_empty(head)) {
602                         struct rpc_task *task;
603                         task = list_first_entry(head,
604                                         struct rpc_task,
605                                         u.tk_wait.list);
606                         task->tk_status = status;
607                         rpc_wake_up_task_queue_locked(queue, task);
608                 }
609                 if (head == &queue->tasks[0])
610                         break;
611                 head--;
612         }
613         spin_unlock_bh(&queue->lock);
614 }
615 EXPORT_SYMBOL_GPL(rpc_wake_up_status);
616
617 static void __rpc_queue_timer_fn(unsigned long ptr)
618 {
619         struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr;
620         struct rpc_task *task, *n;
621         unsigned long expires, now, timeo;
622
623         spin_lock(&queue->lock);
624         expires = now = jiffies;
625         list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) {
626                 timeo = task->u.tk_wait.expires;
627                 if (time_after_eq(now, timeo)) {
628                         dprintk("RPC: %5u timeout\n", task->tk_pid);
629                         task->tk_status = -ETIMEDOUT;
630                         rpc_wake_up_task_queue_locked(queue, task);
631                         continue;
632                 }
633                 if (expires == now || time_after(expires, timeo))
634                         expires = timeo;
635         }
636         if (!list_empty(&queue->timer_list.list))
637                 rpc_set_queue_timer(queue, expires);
638         spin_unlock(&queue->lock);
639 }
640
641 static void __rpc_atrun(struct rpc_task *task)
642 {
643         task->tk_status = 0;
644 }
645
646 /*
647  * Run a task at a later time
648  */
649 void rpc_delay(struct rpc_task *task, unsigned long delay)
650 {
651         task->tk_timeout = delay;
652         rpc_sleep_on(&delay_queue, task, __rpc_atrun);
653 }
654 EXPORT_SYMBOL_GPL(rpc_delay);
655
656 /*
657  * Helper to call task->tk_ops->rpc_call_prepare
658  */
659 void rpc_prepare_task(struct rpc_task *task)
660 {
661         task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
662 }
663
664 static void
665 rpc_init_task_statistics(struct rpc_task *task)
666 {
667         /* Initialize retry counters */
668         task->tk_garb_retry = 2;
669         task->tk_cred_retry = 2;
670         task->tk_rebind_retry = 2;
671
672         /* starting timestamp */
673         task->tk_start = ktime_get();
674 }
675
676 static void
677 rpc_reset_task_statistics(struct rpc_task *task)
678 {
679         task->tk_timeouts = 0;
680         task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT);
681
682         rpc_init_task_statistics(task);
683 }
684
685 /*
686  * Helper that calls task->tk_ops->rpc_call_done if it exists
687  */
688 void rpc_exit_task(struct rpc_task *task)
689 {
690         task->tk_action = NULL;
691         if (task->tk_ops->rpc_call_done != NULL) {
692                 task->tk_ops->rpc_call_done(task, task->tk_calldata);
693                 if (task->tk_action != NULL) {
694                         WARN_ON(RPC_ASSASSINATED(task));
695                         /* Always release the RPC slot and buffer memory */
696                         xprt_release(task);
697                         rpc_reset_task_statistics(task);
698                 }
699         }
700 }
701
702 void rpc_exit(struct rpc_task *task, int status)
703 {
704         task->tk_status = status;
705         task->tk_action = rpc_exit_task;
706         if (RPC_IS_QUEUED(task))
707                 rpc_wake_up_queued_task(task->tk_waitqueue, task);
708 }
709 EXPORT_SYMBOL_GPL(rpc_exit);
710
711 void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
712 {
713         if (ops->rpc_release != NULL)
714                 ops->rpc_release(calldata);
715 }
716
717 /*
718  * This is the RPC `scheduler' (or rather, the finite state machine).
719  */
720 static void __rpc_execute(struct rpc_task *task)
721 {
722         struct rpc_wait_queue *queue;
723         int task_is_async = RPC_IS_ASYNC(task);
724         int status = 0;
725
726         dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
727                         task->tk_pid, task->tk_flags);
728
729         WARN_ON_ONCE(RPC_IS_QUEUED(task));
730         if (RPC_IS_QUEUED(task))
731                 return;
732
733         for (;;) {
734                 void (*do_action)(struct rpc_task *);
735
736                 /*
737                  * Execute any pending callback first.
738                  */
739                 do_action = task->tk_callback;
740                 task->tk_callback = NULL;
741                 if (do_action == NULL) {
742                         /*
743                          * Perform the next FSM step.
744                          * tk_action may be NULL if the task has been killed.
745                          * In particular, note that rpc_killall_tasks may
746                          * do this at any time, so beware when dereferencing.
747                          */
748                         do_action = task->tk_action;
749                         if (do_action == NULL)
750                                 break;
751                 }
752                 trace_rpc_task_run_action(task->tk_client, task, task->tk_action);
753                 do_action(task);
754
755                 /*
756                  * Lockless check for whether task is sleeping or not.
757                  */
758                 if (!RPC_IS_QUEUED(task))
759                         continue;
760                 /*
761                  * The queue->lock protects against races with
762                  * rpc_make_runnable().
763                  *
764                  * Note that once we clear RPC_TASK_RUNNING on an asynchronous
765                  * rpc_task, rpc_make_runnable() can assign it to a
766                  * different workqueue. We therefore cannot assume that the
767                  * rpc_task pointer may still be dereferenced.
768                  */
769                 queue = task->tk_waitqueue;
770                 spin_lock_bh(&queue->lock);
771                 if (!RPC_IS_QUEUED(task)) {
772                         spin_unlock_bh(&queue->lock);
773                         continue;
774                 }
775                 rpc_clear_running(task);
776                 spin_unlock_bh(&queue->lock);
777                 if (task_is_async)
778                         return;
779
780                 /* sync task: sleep here */
781                 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
782                 status = out_of_line_wait_on_bit(&task->tk_runstate,
783                                 RPC_TASK_QUEUED, rpc_wait_bit_killable,
784                                 TASK_KILLABLE);
785                 if (status == -ERESTARTSYS) {
786                         /*
787                          * When a sync task receives a signal, it exits with
788                          * -ERESTARTSYS. In order to catch any callbacks that
789                          * clean up after sleeping on some queue, we don't
790                          * break the loop here, but go around once more.
791                          */
792                         dprintk("RPC: %5u got signal\n", task->tk_pid);
793                         task->tk_flags |= RPC_TASK_KILLED;
794                         rpc_exit(task, -ERESTARTSYS);
795                 }
796                 rpc_set_running(task);
797                 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
798         }
799
800         dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
801                         task->tk_status);
802         /* Release all resources associated with the task */
803         rpc_release_task(task);
804 }
805
806 /*
807  * User-visible entry point to the scheduler.
808  *
809  * This may be called recursively if e.g. an async NFS task updates
810  * the attributes and finds that dirty pages must be flushed.
811  * NOTE: Upon exit of this function the task is guaranteed to be
812  *       released. In particular note that tk_release() will have
813  *       been called, so your task memory may have been freed.
814  */
815 void rpc_execute(struct rpc_task *task)
816 {
817         rpc_set_active(task);
818         rpc_make_runnable(task);
819         if (!RPC_IS_ASYNC(task))
820                 __rpc_execute(task);
821 }
822
823 static void rpc_async_schedule(struct work_struct *work)
824 {
825         current->flags |= PF_FSTRANS;
826         __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
827         current->flags &= ~PF_FSTRANS;
828 }
829
830 /**
831  * rpc_malloc - allocate an RPC buffer
832  * @task: RPC task that will use this buffer
833  * @size: requested byte size
834  *
835  * To prevent rpciod from hanging, this allocator never sleeps,
836  * returning NULL if the request cannot be serviced immediately.
837  * The caller can arrange to sleep in a way that is safe for rpciod.
838  *
839  * Most requests are 'small' (under 2KiB) and can be serviced from a
840  * mempool, ensuring that NFS reads and writes can always proceed,
841  * and that there is good locality of reference for these buffers.
842  *
843  * In order to avoid memory starvation triggering more writebacks of
844  * NFS requests, we avoid using GFP_KERNEL.
845  */
846 void *rpc_malloc(struct rpc_task *task, size_t size)
847 {
848         struct rpc_buffer *buf;
849         gfp_t gfp = GFP_NOWAIT;
850
851         if (RPC_IS_SWAPPER(task))
852                 gfp |= __GFP_MEMALLOC;
853
854         size += sizeof(struct rpc_buffer);
855         if (size <= RPC_BUFFER_MAXSIZE)
856                 buf = mempool_alloc(rpc_buffer_mempool, gfp);
857         else
858                 buf = kmalloc(size, gfp);
859
860         if (!buf)
861                 return NULL;
862
863         buf->len = size;
864         dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
865                         task->tk_pid, size, buf);
866         return &buf->data;
867 }
868 EXPORT_SYMBOL_GPL(rpc_malloc);
869
870 /**
871  * rpc_free - free buffer allocated via rpc_malloc
872  * @buffer: buffer to free
873  *
874  */
875 void rpc_free(void *buffer)
876 {
877         size_t size;
878         struct rpc_buffer *buf;
879
880         if (!buffer)
881                 return;
882
883         buf = container_of(buffer, struct rpc_buffer, data);
884         size = buf->len;
885
886         dprintk("RPC:       freeing buffer of size %zu at %p\n",
887                         size, buf);
888
889         if (size <= RPC_BUFFER_MAXSIZE)
890                 mempool_free(buf, rpc_buffer_mempool);
891         else
892                 kfree(buf);
893 }
894 EXPORT_SYMBOL_GPL(rpc_free);
895
896 /*
897  * Creation and deletion of RPC task structures
898  */
899 static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data)
900 {
901         memset(task, 0, sizeof(*task));
902         atomic_set(&task->tk_count, 1);
903         task->tk_flags  = task_setup_data->flags;
904         task->tk_ops = task_setup_data->callback_ops;
905         task->tk_calldata = task_setup_data->callback_data;
906         INIT_LIST_HEAD(&task->tk_task);
907
908         task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW;
909         task->tk_owner = current->tgid;
910
911         /* Initialize workqueue for async tasks */
912         task->tk_workqueue = task_setup_data->workqueue;
913
914         if (task->tk_ops->rpc_call_prepare != NULL)
915                 task->tk_action = rpc_prepare_task;
916
917         rpc_init_task_statistics(task);
918
919         dprintk("RPC:       new task initialized, procpid %u\n",
920                                 task_pid_nr(current));
921 }
922
923 static struct rpc_task *
924 rpc_alloc_task(void)
925 {
926         return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOIO);
927 }
928
929 /*
930  * Create a new task for the specified client.
931  */
932 struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data)
933 {
934         struct rpc_task *task = setup_data->task;
935         unsigned short flags = 0;
936
937         if (task == NULL) {
938                 task = rpc_alloc_task();
939                 if (task == NULL) {
940                         rpc_release_calldata(setup_data->callback_ops,
941                                         setup_data->callback_data);
942                         return ERR_PTR(-ENOMEM);
943                 }
944                 flags = RPC_TASK_DYNAMIC;
945         }
946
947         rpc_init_task(task, setup_data);
948         task->tk_flags |= flags;
949         dprintk("RPC:       allocated task %p\n", task);
950         return task;
951 }
952
953 /*
954  * rpc_free_task - release rpc task and perform cleanups
955  *
956  * Note that we free up the rpc_task _after_ rpc_release_calldata()
957  * in order to work around a workqueue dependency issue.
958  *
959  * Tejun Heo states:
960  * "Workqueue currently considers two work items to be the same if they're
961  * on the same address and won't execute them concurrently - ie. it
962  * makes a work item which is queued again while being executed wait
963  * for the previous execution to complete.
964  *
965  * If a work function frees the work item, and then waits for an event
966  * which should be performed by another work item and *that* work item
967  * recycles the freed work item, it can create a false dependency loop.
968  * There really is no reliable way to detect this short of verifying
969  * every memory free."
970  *
971  */
972 static void rpc_free_task(struct rpc_task *task)
973 {
974         unsigned short tk_flags = task->tk_flags;
975
976         rpc_release_calldata(task->tk_ops, task->tk_calldata);
977
978         if (tk_flags & RPC_TASK_DYNAMIC) {
979                 dprintk("RPC: %5u freeing task\n", task->tk_pid);
980                 mempool_free(task, rpc_task_mempool);
981         }
982 }
983
984 static void rpc_async_release(struct work_struct *work)
985 {
986         rpc_free_task(container_of(work, struct rpc_task, u.tk_work));
987 }
988
989 static void rpc_release_resources_task(struct rpc_task *task)
990 {
991         xprt_release(task);
992         if (task->tk_msg.rpc_cred) {
993                 put_rpccred(task->tk_msg.rpc_cred);
994                 task->tk_msg.rpc_cred = NULL;
995         }
996         rpc_task_release_client(task);
997 }
998
999 static void rpc_final_put_task(struct rpc_task *task,
1000                 struct workqueue_struct *q)
1001 {
1002         if (q != NULL) {
1003                 INIT_WORK(&task->u.tk_work, rpc_async_release);
1004                 queue_work(q, &task->u.tk_work);
1005         } else
1006                 rpc_free_task(task);
1007 }
1008
1009 static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q)
1010 {
1011         if (atomic_dec_and_test(&task->tk_count)) {
1012                 rpc_release_resources_task(task);
1013                 rpc_final_put_task(task, q);
1014         }
1015 }
1016
1017 void rpc_put_task(struct rpc_task *task)
1018 {
1019         rpc_do_put_task(task, NULL);
1020 }
1021 EXPORT_SYMBOL_GPL(rpc_put_task);
1022
1023 void rpc_put_task_async(struct rpc_task *task)
1024 {
1025         rpc_do_put_task(task, task->tk_workqueue);
1026 }
1027 EXPORT_SYMBOL_GPL(rpc_put_task_async);
1028
1029 static void rpc_release_task(struct rpc_task *task)
1030 {
1031         dprintk("RPC: %5u release task\n", task->tk_pid);
1032
1033         WARN_ON_ONCE(RPC_IS_QUEUED(task));
1034
1035         rpc_release_resources_task(task);
1036
1037         /*
1038          * Note: at this point we have been removed from rpc_clnt->cl_tasks,
1039          * so it should be safe to use task->tk_count as a test for whether
1040          * or not any other processes still hold references to our rpc_task.
1041          */
1042         if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) {
1043                 /* Wake up anyone who may be waiting for task completion */
1044                 if (!rpc_complete_task(task))
1045                         return;
1046         } else {
1047                 if (!atomic_dec_and_test(&task->tk_count))
1048                         return;
1049         }
1050         rpc_final_put_task(task, task->tk_workqueue);
1051 }
1052
1053 int rpciod_up(void)
1054 {
1055         return try_module_get(THIS_MODULE) ? 0 : -EINVAL;
1056 }
1057
1058 void rpciod_down(void)
1059 {
1060         module_put(THIS_MODULE);
1061 }
1062
1063 /*
1064  * Start up the rpciod workqueue.
1065  */
1066 static int rpciod_start(void)
1067 {
1068         struct workqueue_struct *wq;
1069
1070         /*
1071          * Create the rpciod thread and wait for it to start.
1072          */
1073         dprintk("RPC:       creating workqueue rpciod\n");
1074         wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM, 1);
1075         rpciod_workqueue = wq;
1076         return rpciod_workqueue != NULL;
1077 }
1078
1079 static void rpciod_stop(void)
1080 {
1081         struct workqueue_struct *wq = NULL;
1082
1083         if (rpciod_workqueue == NULL)
1084                 return;
1085         dprintk("RPC:       destroying workqueue rpciod\n");
1086
1087         wq = rpciod_workqueue;
1088         rpciod_workqueue = NULL;
1089         destroy_workqueue(wq);
1090 }
1091
1092 void
1093 rpc_destroy_mempool(void)
1094 {
1095         rpciod_stop();
1096         if (rpc_buffer_mempool)
1097                 mempool_destroy(rpc_buffer_mempool);
1098         if (rpc_task_mempool)
1099                 mempool_destroy(rpc_task_mempool);
1100         if (rpc_task_slabp)
1101                 kmem_cache_destroy(rpc_task_slabp);
1102         if (rpc_buffer_slabp)
1103                 kmem_cache_destroy(rpc_buffer_slabp);
1104         rpc_destroy_wait_queue(&delay_queue);
1105 }
1106
1107 int
1108 rpc_init_mempool(void)
1109 {
1110         /*
1111          * The following is not strictly a mempool initialisation,
1112          * but there is no harm in doing it here
1113          */
1114         rpc_init_wait_queue(&delay_queue, "delayq");
1115         if (!rpciod_start())
1116                 goto err_nomem;
1117
1118         rpc_task_slabp = kmem_cache_create("rpc_tasks",
1119                                              sizeof(struct rpc_task),
1120                                              0, SLAB_HWCACHE_ALIGN,
1121                                              NULL);
1122         if (!rpc_task_slabp)
1123                 goto err_nomem;
1124         rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1125                                              RPC_BUFFER_MAXSIZE,
1126                                              0, SLAB_HWCACHE_ALIGN,
1127                                              NULL);
1128         if (!rpc_buffer_slabp)
1129                 goto err_nomem;
1130         rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1131                                                     rpc_task_slabp);
1132         if (!rpc_task_mempool)
1133                 goto err_nomem;
1134         rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1135                                                       rpc_buffer_slabp);
1136         if (!rpc_buffer_mempool)
1137                 goto err_nomem;
1138         return 0;
1139 err_nomem:
1140         rpc_destroy_mempool();
1141         return -ENOMEM;
1142 }