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