]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/sunrpc/sched.c
Subject: Re: [PATCH] Fix SUNRPC wakeup/execute race condition
[mirror_ubuntu-zesty-kernel.git] / net / sunrpc / sched.c
CommitLineData
1da177e4
LT
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/smp_lock.h>
20#include <linux/spinlock.h>
4a3e2f71 21#include <linux/mutex.h>
1da177e4
LT
22
23#include <linux/sunrpc/clnt.h>
1da177e4
LT
24
25#ifdef RPC_DEBUG
26#define RPCDBG_FACILITY RPCDBG_SCHED
27#define RPC_TASK_MAGIC_ID 0xf00baa
28static int rpc_task_id;
29#endif
30
31/*
32 * RPC slabs and memory pools
33 */
34#define RPC_BUFFER_MAXSIZE (2048)
35#define RPC_BUFFER_POOLSIZE (8)
36#define RPC_TASK_POOLSIZE (8)
ba89966c
ED
37static kmem_cache_t *rpc_task_slabp __read_mostly;
38static kmem_cache_t *rpc_buffer_slabp __read_mostly;
39static mempool_t *rpc_task_mempool __read_mostly;
40static mempool_t *rpc_buffer_mempool __read_mostly;
1da177e4
LT
41
42static void __rpc_default_timer(struct rpc_task *task);
43static void rpciod_killall(void);
1da177e4
LT
44static void rpc_async_schedule(void *);
45
1da177e4
LT
46/*
47 * RPC tasks sit here while waiting for conditions to improve.
48 */
49static RPC_WAITQ(delay_queue, "delayq");
50
51/*
52 * All RPC tasks are linked into this list
53 */
54static LIST_HEAD(all_tasks);
55
56/*
57 * rpciod-related stuff
58 */
4a3e2f71 59static DEFINE_MUTEX(rpciod_mutex);
1da177e4 60static unsigned int rpciod_users;
24c5d9d7 61struct workqueue_struct *rpciod_workqueue;
1da177e4
LT
62
63/*
64 * Spinlock for other critical sections of code.
65 */
66static DEFINE_SPINLOCK(rpc_sched_lock);
67
68/*
69 * Disable the timer for a given RPC task. Should be called with
70 * queue->lock and bh_disabled in order to avoid races within
71 * rpc_run_timer().
72 */
73static inline void
74__rpc_disable_timer(struct rpc_task *task)
75{
76 dprintk("RPC: %4d disabling timer\n", task->tk_pid);
77 task->tk_timeout_fn = NULL;
78 task->tk_timeout = 0;
79}
80
81/*
82 * Run a timeout function.
83 * We use the callback in order to allow __rpc_wake_up_task()
84 * and friends to disable the timer synchronously on SMP systems
85 * without calling del_timer_sync(). The latter could cause a
86 * deadlock if called while we're holding spinlocks...
87 */
88static void rpc_run_timer(struct rpc_task *task)
89{
90 void (*callback)(struct rpc_task *);
91
92 callback = task->tk_timeout_fn;
93 task->tk_timeout_fn = NULL;
94 if (callback && RPC_IS_QUEUED(task)) {
95 dprintk("RPC: %4d running timer\n", task->tk_pid);
96 callback(task);
97 }
98 smp_mb__before_clear_bit();
99 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
100 smp_mb__after_clear_bit();
101}
102
103/*
104 * Set up a timer for the current task.
105 */
106static inline void
107__rpc_add_timer(struct rpc_task *task, rpc_action timer)
108{
109 if (!task->tk_timeout)
110 return;
111
112 dprintk("RPC: %4d setting alarm for %lu ms\n",
113 task->tk_pid, task->tk_timeout * 1000 / HZ);
114
115 if (timer)
116 task->tk_timeout_fn = timer;
117 else
118 task->tk_timeout_fn = __rpc_default_timer;
119 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
120 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
121}
122
123/*
124 * Delete any timer for the current task. Because we use del_timer_sync(),
125 * this function should never be called while holding queue->lock.
126 */
127static void
128rpc_delete_timer(struct rpc_task *task)
129{
130 if (RPC_IS_QUEUED(task))
131 return;
132 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
133 del_singleshot_timer_sync(&task->tk_timer);
134 dprintk("RPC: %4d deleting timer\n", task->tk_pid);
135 }
136}
137
138/*
139 * Add new request to a priority queue.
140 */
141static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
142{
143 struct list_head *q;
144 struct rpc_task *t;
145
146 INIT_LIST_HEAD(&task->u.tk_wait.links);
147 q = &queue->tasks[task->tk_priority];
148 if (unlikely(task->tk_priority > queue->maxpriority))
149 q = &queue->tasks[queue->maxpriority];
150 list_for_each_entry(t, q, u.tk_wait.list) {
151 if (t->tk_cookie == task->tk_cookie) {
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 */
167static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
168{
169 BUG_ON (RPC_IS_QUEUED(task));
170
171 if (RPC_IS_PRIORITY(queue))
172 __rpc_add_wait_queue_priority(queue, task);
173 else if (RPC_IS_SWAPPER(task))
174 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
175 else
176 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
177 task->u.tk_wait.rpc_waitq = queue;
e19b63da 178 queue->qlen++;
1da177e4
LT
179 rpc_set_queued(task);
180
181 dprintk("RPC: %4d added to queue %p \"%s\"\n",
182 task->tk_pid, queue, rpc_qname(queue));
183}
184
185/*
186 * Remove request from a priority queue.
187 */
188static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
189{
190 struct rpc_task *t;
191
192 if (!list_empty(&task->u.tk_wait.links)) {
193 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
194 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
195 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
196 }
197 list_del(&task->u.tk_wait.list);
198}
199
200/*
201 * Remove request from queue.
202 * Note: must be called with spin lock held.
203 */
204static void __rpc_remove_wait_queue(struct rpc_task *task)
205{
206 struct rpc_wait_queue *queue;
207 queue = task->u.tk_wait.rpc_waitq;
208
209 if (RPC_IS_PRIORITY(queue))
210 __rpc_remove_wait_queue_priority(task);
211 else
212 list_del(&task->u.tk_wait.list);
e19b63da 213 queue->qlen--;
1da177e4
LT
214 dprintk("RPC: %4d removed from queue %p \"%s\"\n",
215 task->tk_pid, queue, rpc_qname(queue));
216}
217
218static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
219{
220 queue->priority = priority;
221 queue->count = 1 << (priority * 2);
222}
223
224static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
225{
226 queue->cookie = cookie;
227 queue->nr = RPC_BATCH_COUNT;
228}
229
230static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
231{
232 rpc_set_waitqueue_priority(queue, queue->maxpriority);
233 rpc_set_waitqueue_cookie(queue, 0);
234}
235
236static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
237{
238 int i;
239
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 = maxprio;
244 rpc_reset_waitqueue_priority(queue);
245#ifdef RPC_DEBUG
246 queue->name = qname;
247#endif
248}
249
250void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
251{
252 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
253}
254
255void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
256{
257 __rpc_init_priority_wait_queue(queue, qname, 0);
258}
259EXPORT_SYMBOL(rpc_init_wait_queue);
260
44c28873
TM
261static int rpc_wait_bit_interruptible(void *word)
262{
263 if (signal_pending(current))
264 return -ERESTARTSYS;
265 schedule();
266 return 0;
267}
268
269/*
270 * Mark an RPC call as having completed by clearing the 'active' bit
271 */
272static inline void rpc_mark_complete_task(struct rpc_task *task)
273{
274 rpc_clear_active(task);
275 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
276}
277
278/*
279 * Allow callers to wait for completion of an RPC call
280 */
281int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
282{
283 if (action == NULL)
284 action = rpc_wait_bit_interruptible;
285 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
286 action, TASK_INTERRUPTIBLE);
287}
288EXPORT_SYMBOL(__rpc_wait_for_completion_task);
289
1da177e4
LT
290/*
291 * Make an RPC task runnable.
292 *
293 * Note: If the task is ASYNC, this must be called with
294 * the spinlock held to protect the wait queue operation.
295 */
296static void rpc_make_runnable(struct rpc_task *task)
297{
1da177e4 298 BUG_ON(task->tk_timeout_fn);
1da177e4 299 rpc_clear_queued(task);
cc4dc59e
CS
300 if (rpc_test_and_set_running(task))
301 return;
302 /* We might have raced */
303 if (RPC_IS_QUEUED(task)) {
304 rpc_clear_running(task);
1da177e4 305 return;
cc4dc59e 306 }
1da177e4
LT
307 if (RPC_IS_ASYNC(task)) {
308 int status;
309
310 INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
311 status = queue_work(task->tk_workqueue, &task->u.tk_work);
312 if (status < 0) {
313 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
314 task->tk_status = status;
315 return;
316 }
317 } else
96651ab3 318 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
1da177e4
LT
319}
320
1da177e4
LT
321/*
322 * Prepare for sleeping on a wait queue.
323 * By always appending tasks to the list we ensure FIFO behavior.
324 * NB: An RPC task will only receive interrupt-driven events as long
325 * as it's on a wait queue.
326 */
327static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
328 rpc_action action, rpc_action timer)
329{
330 dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
331 rpc_qname(q), jiffies);
332
333 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
334 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
335 return;
336 }
337
338 /* Mark the task as being activated if so needed */
44c28873 339 rpc_set_active(task);
1da177e4
LT
340
341 __rpc_add_wait_queue(q, task);
342
343 BUG_ON(task->tk_callback != NULL);
344 task->tk_callback = action;
345 __rpc_add_timer(task, timer);
346}
347
348void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
349 rpc_action action, rpc_action timer)
350{
351 /*
352 * Protect the queue operations.
353 */
354 spin_lock_bh(&q->lock);
355 __rpc_sleep_on(q, task, action, timer);
356 spin_unlock_bh(&q->lock);
357}
358
359/**
360 * __rpc_do_wake_up_task - wake up a single rpc_task
361 * @task: task to be woken up
362 *
363 * Caller must hold queue->lock, and have cleared the task queued flag.
364 */
365static void __rpc_do_wake_up_task(struct rpc_task *task)
366{
367 dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
368
369#ifdef RPC_DEBUG
370 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
371#endif
372 /* Has the task been executed yet? If not, we cannot wake it up! */
373 if (!RPC_IS_ACTIVATED(task)) {
374 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
375 return;
376 }
377
378 __rpc_disable_timer(task);
379 __rpc_remove_wait_queue(task);
380
381 rpc_make_runnable(task);
382
383 dprintk("RPC: __rpc_wake_up_task done\n");
384}
385
386/*
387 * Wake up the specified task
388 */
389static void __rpc_wake_up_task(struct rpc_task *task)
390{
391 if (rpc_start_wakeup(task)) {
392 if (RPC_IS_QUEUED(task))
393 __rpc_do_wake_up_task(task);
394 rpc_finish_wakeup(task);
395 }
396}
397
398/*
399 * Default timeout handler if none specified by user
400 */
401static void
402__rpc_default_timer(struct rpc_task *task)
403{
404 dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
405 task->tk_status = -ETIMEDOUT;
406 rpc_wake_up_task(task);
407}
408
409/*
410 * Wake up the specified task
411 */
412void rpc_wake_up_task(struct rpc_task *task)
413{
414 if (rpc_start_wakeup(task)) {
415 if (RPC_IS_QUEUED(task)) {
416 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
417
418 spin_lock_bh(&queue->lock);
419 __rpc_do_wake_up_task(task);
420 spin_unlock_bh(&queue->lock);
421 }
422 rpc_finish_wakeup(task);
423 }
424}
425
426/*
427 * Wake up the next task on a priority queue.
428 */
429static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
430{
431 struct list_head *q;
432 struct rpc_task *task;
433
434 /*
435 * Service a batch of tasks from a single cookie.
436 */
437 q = &queue->tasks[queue->priority];
438 if (!list_empty(q)) {
439 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
440 if (queue->cookie == task->tk_cookie) {
441 if (--queue->nr)
442 goto out;
443 list_move_tail(&task->u.tk_wait.list, q);
444 }
445 /*
446 * Check if we need to switch queues.
447 */
448 if (--queue->count)
449 goto new_cookie;
450 }
451
452 /*
453 * Service the next queue.
454 */
455 do {
456 if (q == &queue->tasks[0])
457 q = &queue->tasks[queue->maxpriority];
458 else
459 q = q - 1;
460 if (!list_empty(q)) {
461 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
462 goto new_queue;
463 }
464 } while (q != &queue->tasks[queue->priority]);
465
466 rpc_reset_waitqueue_priority(queue);
467 return NULL;
468
469new_queue:
470 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
471new_cookie:
472 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
473out:
474 __rpc_wake_up_task(task);
475 return task;
476}
477
478/*
479 * Wake up the next task on the wait queue.
480 */
481struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
482{
483 struct rpc_task *task = NULL;
484
485 dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
486 spin_lock_bh(&queue->lock);
487 if (RPC_IS_PRIORITY(queue))
488 task = __rpc_wake_up_next_priority(queue);
489 else {
490 task_for_first(task, &queue->tasks[0])
491 __rpc_wake_up_task(task);
492 }
493 spin_unlock_bh(&queue->lock);
494
495 return task;
496}
497
498/**
499 * rpc_wake_up - wake up all rpc_tasks
500 * @queue: rpc_wait_queue on which the tasks are sleeping
501 *
502 * Grabs queue->lock
503 */
504void rpc_wake_up(struct rpc_wait_queue *queue)
505{
e6d83d55 506 struct rpc_task *task, *next;
1da177e4 507 struct list_head *head;
e6d83d55 508
1da177e4
LT
509 spin_lock_bh(&queue->lock);
510 head = &queue->tasks[queue->maxpriority];
511 for (;;) {
e6d83d55 512 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
1da177e4 513 __rpc_wake_up_task(task);
1da177e4
LT
514 if (head == &queue->tasks[0])
515 break;
516 head--;
517 }
518 spin_unlock_bh(&queue->lock);
519}
520
521/**
522 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
523 * @queue: rpc_wait_queue on which the tasks are sleeping
524 * @status: status value to set
525 *
526 * Grabs queue->lock
527 */
528void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
529{
e6d83d55 530 struct rpc_task *task, *next;
1da177e4 531 struct list_head *head;
1da177e4
LT
532
533 spin_lock_bh(&queue->lock);
534 head = &queue->tasks[queue->maxpriority];
535 for (;;) {
e6d83d55 536 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
1da177e4
LT
537 task->tk_status = status;
538 __rpc_wake_up_task(task);
539 }
540 if (head == &queue->tasks[0])
541 break;
542 head--;
543 }
544 spin_unlock_bh(&queue->lock);
545}
546
8014793b
TM
547static void __rpc_atrun(struct rpc_task *task)
548{
549 rpc_wake_up_task(task);
550}
551
1da177e4
LT
552/*
553 * Run a task at a later time
554 */
8014793b 555void rpc_delay(struct rpc_task *task, unsigned long delay)
1da177e4
LT
556{
557 task->tk_timeout = delay;
558 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
559}
560
4ce70ada
TM
561/*
562 * Helper to call task->tk_ops->rpc_call_prepare
563 */
564static void rpc_prepare_task(struct rpc_task *task)
565{
566 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
567}
568
d05fdb0c 569/*
963d8fe5 570 * Helper that calls task->tk_ops->rpc_call_done if it exists
d05fdb0c 571 */
abbcf28f 572void rpc_exit_task(struct rpc_task *task)
d05fdb0c 573{
abbcf28f 574 task->tk_action = NULL;
963d8fe5
TM
575 if (task->tk_ops->rpc_call_done != NULL) {
576 task->tk_ops->rpc_call_done(task, task->tk_calldata);
d05fdb0c 577 if (task->tk_action != NULL) {
abbcf28f
TM
578 WARN_ON(RPC_ASSASSINATED(task));
579 /* Always release the RPC slot and buffer memory */
580 xprt_release(task);
d05fdb0c
TM
581 }
582 }
d05fdb0c 583}
abbcf28f 584EXPORT_SYMBOL(rpc_exit_task);
d05fdb0c 585
1da177e4
LT
586/*
587 * This is the RPC `scheduler' (or rather, the finite state machine).
588 */
589static int __rpc_execute(struct rpc_task *task)
590{
591 int status = 0;
592
593 dprintk("RPC: %4d rpc_execute flgs %x\n",
594 task->tk_pid, task->tk_flags);
595
596 BUG_ON(RPC_IS_QUEUED(task));
597
d05fdb0c 598 for (;;) {
1da177e4
LT
599 /*
600 * Garbage collection of pending timers...
601 */
602 rpc_delete_timer(task);
603
604 /*
605 * Execute any pending callback.
606 */
607 if (RPC_DO_CALLBACK(task)) {
608 /* Define a callback save pointer */
609 void (*save_callback)(struct rpc_task *);
610
611 /*
612 * If a callback exists, save it, reset it,
613 * call it.
614 * The save is needed to stop from resetting
615 * another callback set within the callback handler
616 * - Dave
617 */
618 save_callback=task->tk_callback;
619 task->tk_callback=NULL;
620 lock_kernel();
621 save_callback(task);
622 unlock_kernel();
623 }
624
625 /*
626 * Perform the next FSM step.
627 * tk_action may be NULL when the task has been killed
628 * by someone else.
629 */
630 if (!RPC_IS_QUEUED(task)) {
abbcf28f 631 if (task->tk_action == NULL)
1da177e4 632 break;
abbcf28f
TM
633 lock_kernel();
634 task->tk_action(task);
635 unlock_kernel();
1da177e4
LT
636 }
637
638 /*
639 * Lockless check for whether task is sleeping or not.
640 */
641 if (!RPC_IS_QUEUED(task))
642 continue;
643 rpc_clear_running(task);
644 if (RPC_IS_ASYNC(task)) {
645 /* Careful! we may have raced... */
646 if (RPC_IS_QUEUED(task))
647 return 0;
648 if (rpc_test_and_set_running(task))
649 return 0;
650 continue;
651 }
652
653 /* sync task: sleep here */
654 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
96651ab3
TM
655 /* Note: Caller should be using rpc_clnt_sigmask() */
656 status = out_of_line_wait_on_bit(&task->tk_runstate,
657 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
658 TASK_INTERRUPTIBLE);
659 if (status == -ERESTARTSYS) {
1da177e4
LT
660 /*
661 * When a sync task receives a signal, it exits with
662 * -ERESTARTSYS. In order to catch any callbacks that
663 * clean up after sleeping on some queue, we don't
664 * break the loop here, but go around once more.
665 */
96651ab3
TM
666 dprintk("RPC: %4d got signal\n", task->tk_pid);
667 task->tk_flags |= RPC_TASK_KILLED;
668 rpc_exit(task, -ERESTARTSYS);
669 rpc_wake_up_task(task);
1da177e4
LT
670 }
671 rpc_set_running(task);
672 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
673 }
674
e60859ac 675 dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
44c28873
TM
676 /* Wake up anyone who is waiting for task completion */
677 rpc_mark_complete_task(task);
1da177e4
LT
678 /* Release all resources associated with the task */
679 rpc_release_task(task);
680 return status;
681}
682
683/*
684 * User-visible entry point to the scheduler.
685 *
686 * This may be called recursively if e.g. an async NFS task updates
687 * the attributes and finds that dirty pages must be flushed.
688 * NOTE: Upon exit of this function the task is guaranteed to be
689 * released. In particular note that tk_release() will have
690 * been called, so your task memory may have been freed.
691 */
692int
693rpc_execute(struct rpc_task *task)
694{
44c28873 695 rpc_set_active(task);
1da177e4
LT
696 rpc_set_running(task);
697 return __rpc_execute(task);
698}
699
700static void rpc_async_schedule(void *arg)
701{
702 __rpc_execute((struct rpc_task *)arg);
703}
704
02107148
CL
705/**
706 * rpc_malloc - allocate an RPC buffer
707 * @task: RPC task that will use this buffer
708 * @size: requested byte size
1da177e4
LT
709 *
710 * We try to ensure that some NFS reads and writes can always proceed
711 * by using a mempool when allocating 'small' buffers.
712 * In order to avoid memory starvation triggering more writebacks of
713 * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
714 */
02107148 715void * rpc_malloc(struct rpc_task *task, size_t size)
1da177e4 716{
02107148 717 struct rpc_rqst *req = task->tk_rqstp;
dd0fc66f 718 gfp_t gfp;
1da177e4
LT
719
720 if (task->tk_flags & RPC_TASK_SWAPPER)
721 gfp = GFP_ATOMIC;
722 else
723 gfp = GFP_NOFS;
724
725 if (size > RPC_BUFFER_MAXSIZE) {
02107148
CL
726 req->rq_buffer = kmalloc(size, gfp);
727 if (req->rq_buffer)
728 req->rq_bufsize = size;
1da177e4 729 } else {
02107148
CL
730 req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
731 if (req->rq_buffer)
732 req->rq_bufsize = RPC_BUFFER_MAXSIZE;
1da177e4 733 }
02107148 734 return req->rq_buffer;
1da177e4
LT
735}
736
02107148
CL
737/**
738 * rpc_free - free buffer allocated via rpc_malloc
739 * @task: RPC task with a buffer to be freed
740 *
741 */
742void rpc_free(struct rpc_task *task)
1da177e4 743{
02107148
CL
744 struct rpc_rqst *req = task->tk_rqstp;
745
746 if (req->rq_buffer) {
747 if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
748 mempool_free(req->rq_buffer, rpc_buffer_mempool);
1da177e4 749 else
02107148
CL
750 kfree(req->rq_buffer);
751 req->rq_buffer = NULL;
752 req->rq_bufsize = 0;
1da177e4
LT
753 }
754}
755
756/*
757 * Creation and deletion of RPC task structures
758 */
963d8fe5 759void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
1da177e4
LT
760{
761 memset(task, 0, sizeof(*task));
762 init_timer(&task->tk_timer);
763 task->tk_timer.data = (unsigned long) task;
764 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
44c28873 765 atomic_set(&task->tk_count, 1);
1da177e4
LT
766 task->tk_client = clnt;
767 task->tk_flags = flags;
963d8fe5 768 task->tk_ops = tk_ops;
4ce70ada
TM
769 if (tk_ops->rpc_call_prepare != NULL)
770 task->tk_action = rpc_prepare_task;
963d8fe5 771 task->tk_calldata = calldata;
1da177e4
LT
772
773 /* Initialize retry counters */
774 task->tk_garb_retry = 2;
775 task->tk_cred_retry = 2;
776
777 task->tk_priority = RPC_PRIORITY_NORMAL;
778 task->tk_cookie = (unsigned long)current;
779
780 /* Initialize workqueue for async tasks */
781 task->tk_workqueue = rpciod_workqueue;
1da177e4
LT
782
783 if (clnt) {
784 atomic_inc(&clnt->cl_users);
785 if (clnt->cl_softrtry)
786 task->tk_flags |= RPC_TASK_SOFT;
787 if (!clnt->cl_intr)
788 task->tk_flags |= RPC_TASK_NOINTR;
789 }
790
791#ifdef RPC_DEBUG
792 task->tk_magic = RPC_TASK_MAGIC_ID;
793 task->tk_pid = rpc_task_id++;
794#endif
795 /* Add to global list of all tasks */
796 spin_lock(&rpc_sched_lock);
797 list_add_tail(&task->tk_task, &all_tasks);
798 spin_unlock(&rpc_sched_lock);
799
963d8fe5
TM
800 BUG_ON(task->tk_ops == NULL);
801
ef759a2e
CL
802 /* starting timestamp */
803 task->tk_start = jiffies;
804
1da177e4
LT
805 dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
806 current->pid);
807}
808
809static struct rpc_task *
810rpc_alloc_task(void)
811{
812 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
813}
814
963d8fe5 815static void rpc_free_task(struct rpc_task *task)
1da177e4
LT
816{
817 dprintk("RPC: %4d freeing task\n", task->tk_pid);
818 mempool_free(task, rpc_task_mempool);
819}
820
821/*
822 * Create a new task for the specified client. We have to
823 * clean up after an allocation failure, as the client may
824 * have specified "oneshot".
825 */
963d8fe5 826struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
1da177e4
LT
827{
828 struct rpc_task *task;
829
830 task = rpc_alloc_task();
831 if (!task)
832 goto cleanup;
833
963d8fe5 834 rpc_init_task(task, clnt, flags, tk_ops, calldata);
1da177e4
LT
835
836 dprintk("RPC: %4d allocated task\n", task->tk_pid);
837 task->tk_flags |= RPC_TASK_DYNAMIC;
838out:
839 return task;
840
841cleanup:
842 /* Check whether to release the client */
843 if (clnt) {
844 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
845 atomic_read(&clnt->cl_users), clnt->cl_oneshot);
846 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
847 rpc_release_client(clnt);
848 }
849 goto out;
850}
851
852void rpc_release_task(struct rpc_task *task)
853{
963d8fe5
TM
854 const struct rpc_call_ops *tk_ops = task->tk_ops;
855 void *calldata = task->tk_calldata;
1da177e4
LT
856
857#ifdef RPC_DEBUG
858 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
859#endif
44c28873
TM
860 if (!atomic_dec_and_test(&task->tk_count))
861 return;
862 dprintk("RPC: %4d release task\n", task->tk_pid);
1da177e4
LT
863
864 /* Remove from global task list */
865 spin_lock(&rpc_sched_lock);
866 list_del(&task->tk_task);
867 spin_unlock(&rpc_sched_lock);
868
869 BUG_ON (RPC_IS_QUEUED(task));
1da177e4
LT
870
871 /* Synchronously delete any running timer */
872 rpc_delete_timer(task);
873
874 /* Release resources */
875 if (task->tk_rqstp)
876 xprt_release(task);
877 if (task->tk_msg.rpc_cred)
878 rpcauth_unbindcred(task);
1da177e4
LT
879 if (task->tk_client) {
880 rpc_release_client(task->tk_client);
881 task->tk_client = NULL;
882 }
883
884#ifdef RPC_DEBUG
885 task->tk_magic = 0;
886#endif
963d8fe5
TM
887 if (task->tk_flags & RPC_TASK_DYNAMIC)
888 rpc_free_task(task);
889 if (tk_ops->rpc_release)
890 tk_ops->rpc_release(calldata);
1da177e4
LT
891}
892
44c28873
TM
893/**
894 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
99acf044
MW
895 * @clnt: pointer to RPC client
896 * @flags: RPC flags
897 * @ops: RPC call ops
898 * @data: user call data
44c28873
TM
899 */
900struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
901 const struct rpc_call_ops *ops,
902 void *data)
903{
904 struct rpc_task *task;
905 task = rpc_new_task(clnt, flags, ops, data);
7a1218a2
TM
906 if (task == NULL) {
907 if (ops->rpc_release != NULL)
908 ops->rpc_release(data);
44c28873 909 return ERR_PTR(-ENOMEM);
7a1218a2 910 }
44c28873
TM
911 atomic_inc(&task->tk_count);
912 rpc_execute(task);
913 return task;
914}
915EXPORT_SYMBOL(rpc_run_task);
916
1da177e4
LT
917/*
918 * Kill all tasks for the given client.
919 * XXX: kill their descendants as well?
920 */
921void rpc_killall_tasks(struct rpc_clnt *clnt)
922{
923 struct rpc_task *rovr;
924 struct list_head *le;
925
926 dprintk("RPC: killing all tasks for client %p\n", clnt);
927
928 /*
929 * Spin lock all_tasks to prevent changes...
930 */
931 spin_lock(&rpc_sched_lock);
932 alltask_for_each(rovr, le, &all_tasks) {
933 if (! RPC_IS_ACTIVATED(rovr))
934 continue;
935 if (!clnt || rovr->tk_client == clnt) {
936 rovr->tk_flags |= RPC_TASK_KILLED;
937 rpc_exit(rovr, -EIO);
938 rpc_wake_up_task(rovr);
939 }
940 }
941 spin_unlock(&rpc_sched_lock);
942}
943
944static DECLARE_MUTEX_LOCKED(rpciod_running);
945
946static void rpciod_killall(void)
947{
948 unsigned long flags;
949
950 while (!list_empty(&all_tasks)) {
951 clear_thread_flag(TIF_SIGPENDING);
952 rpc_killall_tasks(NULL);
953 flush_workqueue(rpciod_workqueue);
954 if (!list_empty(&all_tasks)) {
955 dprintk("rpciod_killall: waiting for tasks to exit\n");
956 yield();
957 }
958 }
959
960 spin_lock_irqsave(&current->sighand->siglock, flags);
961 recalc_sigpending();
962 spin_unlock_irqrestore(&current->sighand->siglock, flags);
963}
964
965/*
966 * Start up the rpciod process if it's not already running.
967 */
968int
969rpciod_up(void)
970{
971 struct workqueue_struct *wq;
972 int error = 0;
973
4a3e2f71 974 mutex_lock(&rpciod_mutex);
1da177e4
LT
975 dprintk("rpciod_up: users %d\n", rpciod_users);
976 rpciod_users++;
977 if (rpciod_workqueue)
978 goto out;
979 /*
980 * If there's no pid, we should be the first user.
981 */
982 if (rpciod_users > 1)
983 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
984 /*
985 * Create the rpciod thread and wait for it to start.
986 */
987 error = -ENOMEM;
988 wq = create_workqueue("rpciod");
989 if (wq == NULL) {
990 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
991 rpciod_users--;
992 goto out;
993 }
994 rpciod_workqueue = wq;
995 error = 0;
996out:
4a3e2f71 997 mutex_unlock(&rpciod_mutex);
1da177e4
LT
998 return error;
999}
1000
1001void
1002rpciod_down(void)
1003{
4a3e2f71 1004 mutex_lock(&rpciod_mutex);
1da177e4
LT
1005 dprintk("rpciod_down sema %d\n", rpciod_users);
1006 if (rpciod_users) {
1007 if (--rpciod_users)
1008 goto out;
1009 } else
1010 printk(KERN_WARNING "rpciod_down: no users??\n");
1011
1012 if (!rpciod_workqueue) {
1013 dprintk("rpciod_down: Nothing to do!\n");
1014 goto out;
1015 }
1016 rpciod_killall();
1017
1018 destroy_workqueue(rpciod_workqueue);
1019 rpciod_workqueue = NULL;
1020 out:
4a3e2f71 1021 mutex_unlock(&rpciod_mutex);
1da177e4
LT
1022}
1023
1024#ifdef RPC_DEBUG
1025void rpc_show_tasks(void)
1026{
1027 struct list_head *le;
1028 struct rpc_task *t;
1029
1030 spin_lock(&rpc_sched_lock);
1031 if (list_empty(&all_tasks)) {
1032 spin_unlock(&rpc_sched_lock);
1033 return;
1034 }
1035 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
963d8fe5 1036 "-rpcwait -action- ---ops--\n");
1da177e4
LT
1037 alltask_for_each(t, le, &all_tasks) {
1038 const char *rpc_waitq = "none";
1039
1040 if (RPC_IS_QUEUED(t))
1041 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1042
1043 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1044 t->tk_pid,
1045 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1046 t->tk_flags, t->tk_status,
1047 t->tk_client,
1048 (t->tk_client ? t->tk_client->cl_prog : 0),
1049 t->tk_rqstp, t->tk_timeout,
1050 rpc_waitq,
963d8fe5 1051 t->tk_action, t->tk_ops);
1da177e4
LT
1052 }
1053 spin_unlock(&rpc_sched_lock);
1054}
1055#endif
1056
1057void
1058rpc_destroy_mempool(void)
1059{
1060 if (rpc_buffer_mempool)
1061 mempool_destroy(rpc_buffer_mempool);
1062 if (rpc_task_mempool)
1063 mempool_destroy(rpc_task_mempool);
1a1d92c1
AD
1064 if (rpc_task_slabp)
1065 kmem_cache_destroy(rpc_task_slabp);
1066 if (rpc_buffer_slabp)
1067 kmem_cache_destroy(rpc_buffer_slabp);
1da177e4
LT
1068}
1069
1070int
1071rpc_init_mempool(void)
1072{
1073 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1074 sizeof(struct rpc_task),
1075 0, SLAB_HWCACHE_ALIGN,
1076 NULL, NULL);
1077 if (!rpc_task_slabp)
1078 goto err_nomem;
1079 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1080 RPC_BUFFER_MAXSIZE,
1081 0, SLAB_HWCACHE_ALIGN,
1082 NULL, NULL);
1083 if (!rpc_buffer_slabp)
1084 goto err_nomem;
93d2341c
MD
1085 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1086 rpc_task_slabp);
1da177e4
LT
1087 if (!rpc_task_mempool)
1088 goto err_nomem;
93d2341c
MD
1089 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1090 rpc_buffer_slabp);
1da177e4
LT
1091 if (!rpc_buffer_mempool)
1092 goto err_nomem;
1093 return 0;
1094err_nomem:
1095 rpc_destroy_mempool();
1096 return -ENOMEM;
1097}