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