11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/net/sunrpc/sched.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Scheduling for synchronous and asynchronous RPC requests. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * TCP NFS related read + write fixes 91da177e4SLinus Torvalds * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/sched.h> 151da177e4SLinus Torvalds #include <linux/interrupt.h> 161da177e4SLinus Torvalds #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/mempool.h> 181da177e4SLinus Torvalds #include <linux/smp.h> 191da177e4SLinus Torvalds #include <linux/spinlock.h> 204a3e2f71SArjan van de Ven #include <linux/mutex.h> 21d310310cSJeff Layton #include <linux/freezer.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 241da177e4SLinus Torvalds 256951867bSBenny Halevy #include "sunrpc.h" 266951867bSBenny Halevy 27f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 281da177e4SLinus Torvalds #define RPCDBG_FACILITY RPCDBG_SCHED 291da177e4SLinus Torvalds #endif 301da177e4SLinus Torvalds 3182b0a4c3STrond Myklebust #define CREATE_TRACE_POINTS 3282b0a4c3STrond Myklebust #include <trace/events/sunrpc.h> 3382b0a4c3STrond Myklebust 341da177e4SLinus Torvalds /* 351da177e4SLinus Torvalds * RPC slabs and memory pools 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds #define RPC_BUFFER_MAXSIZE (2048) 381da177e4SLinus Torvalds #define RPC_BUFFER_POOLSIZE (8) 391da177e4SLinus Torvalds #define RPC_TASK_POOLSIZE (8) 40e18b890bSChristoph Lameter static struct kmem_cache *rpc_task_slabp __read_mostly; 41e18b890bSChristoph Lameter static struct kmem_cache *rpc_buffer_slabp __read_mostly; 42ba89966cSEric Dumazet static mempool_t *rpc_task_mempool __read_mostly; 43ba89966cSEric Dumazet static mempool_t *rpc_buffer_mempool __read_mostly; 441da177e4SLinus Torvalds 4565f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *); 46bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task); 4736df9aaeSTrond Myklebust static void __rpc_queue_timer_fn(unsigned long ptr); 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds /* 501da177e4SLinus Torvalds * RPC tasks sit here while waiting for conditions to improve. 511da177e4SLinus Torvalds */ 52a4a87499STrond Myklebust static struct rpc_wait_queue delay_queue; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* 551da177e4SLinus Torvalds * rpciod-related stuff 561da177e4SLinus Torvalds */ 5740a5f1b1STrond Myklebust struct workqueue_struct *rpciod_workqueue __read_mostly; 5840a5f1b1STrond Myklebust struct workqueue_struct *xprtiod_workqueue __read_mostly; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds /* 611da177e4SLinus Torvalds * Disable the timer for a given RPC task. Should be called with 621da177e4SLinus Torvalds * queue->lock and bh_disabled in order to avoid races within 631da177e4SLinus Torvalds * rpc_run_timer(). 641da177e4SLinus Torvalds */ 655d00837bSTrond Myklebust static void 66eb276c0eSTrond Myklebust __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task) 671da177e4SLinus Torvalds { 6836df9aaeSTrond Myklebust if (task->tk_timeout == 0) 6936df9aaeSTrond Myklebust return; 7046121cf7SChuck Lever dprintk("RPC: %5u disabling timer\n", task->tk_pid); 711da177e4SLinus Torvalds task->tk_timeout = 0; 7236df9aaeSTrond Myklebust list_del(&task->u.tk_wait.timer_list); 73eb276c0eSTrond Myklebust if (list_empty(&queue->timer_list.list)) 74eb276c0eSTrond Myklebust del_timer(&queue->timer_list.timer); 7536df9aaeSTrond Myklebust } 7636df9aaeSTrond Myklebust 7736df9aaeSTrond Myklebust static void 7836df9aaeSTrond Myklebust rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires) 7936df9aaeSTrond Myklebust { 8036df9aaeSTrond Myklebust queue->timer_list.expires = expires; 8136df9aaeSTrond Myklebust mod_timer(&queue->timer_list.timer, expires); 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* 851da177e4SLinus Torvalds * Set up a timer for the current task. 861da177e4SLinus Torvalds */ 875d00837bSTrond Myklebust static void 88eb276c0eSTrond Myklebust __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds if (!task->tk_timeout) 911da177e4SLinus Torvalds return; 921da177e4SLinus Torvalds 9355cc1d78SNicholas Mc Guire dprintk("RPC: %5u setting alarm for %u ms\n", 94f0eede10SNicholas Mc Guire task->tk_pid, jiffies_to_msecs(task->tk_timeout)); 951da177e4SLinus Torvalds 96eb276c0eSTrond Myklebust task->u.tk_wait.expires = jiffies + task->tk_timeout; 97eb276c0eSTrond Myklebust if (list_empty(&queue->timer_list.list) || time_before(task->u.tk_wait.expires, queue->timer_list.expires)) 98eb276c0eSTrond Myklebust rpc_set_queue_timer(queue, task->u.tk_wait.expires); 99eb276c0eSTrond Myklebust list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds 102edd2e36fSTrond Myklebust static void rpc_rotate_queue_owner(struct rpc_wait_queue *queue) 103edd2e36fSTrond Myklebust { 104edd2e36fSTrond Myklebust struct list_head *q = &queue->tasks[queue->priority]; 105edd2e36fSTrond Myklebust struct rpc_task *task; 106edd2e36fSTrond Myklebust 107edd2e36fSTrond Myklebust if (!list_empty(q)) { 108edd2e36fSTrond Myklebust task = list_first_entry(q, struct rpc_task, u.tk_wait.list); 109edd2e36fSTrond Myklebust if (task->tk_owner == queue->owner) 110edd2e36fSTrond Myklebust list_move_tail(&task->u.tk_wait.list, q); 111edd2e36fSTrond Myklebust } 112edd2e36fSTrond Myklebust } 113edd2e36fSTrond Myklebust 114c05eecf6STrond Myklebust static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority) 115c05eecf6STrond Myklebust { 116edd2e36fSTrond Myklebust if (queue->priority != priority) { 117edd2e36fSTrond Myklebust /* Fairness: rotate the list when changing priority */ 118edd2e36fSTrond Myklebust rpc_rotate_queue_owner(queue); 119c05eecf6STrond Myklebust queue->priority = priority; 120c05eecf6STrond Myklebust } 121edd2e36fSTrond Myklebust } 122c05eecf6STrond Myklebust 123c05eecf6STrond Myklebust static void rpc_set_waitqueue_owner(struct rpc_wait_queue *queue, pid_t pid) 124c05eecf6STrond Myklebust { 125c05eecf6STrond Myklebust queue->owner = pid; 126c05eecf6STrond Myklebust queue->nr = RPC_BATCH_COUNT; 127c05eecf6STrond Myklebust } 128c05eecf6STrond Myklebust 129c05eecf6STrond Myklebust static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue) 130c05eecf6STrond Myklebust { 131c05eecf6STrond Myklebust rpc_set_waitqueue_priority(queue, queue->maxpriority); 132c05eecf6STrond Myklebust rpc_set_waitqueue_owner(queue, 0); 133c05eecf6STrond Myklebust } 134c05eecf6STrond Myklebust 1351da177e4SLinus Torvalds /* 1361da177e4SLinus Torvalds * Add new request to a priority queue. 1371da177e4SLinus Torvalds */ 1383b27bad7STrond Myklebust static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, 1393b27bad7STrond Myklebust struct rpc_task *task, 1403b27bad7STrond Myklebust unsigned char queue_priority) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds struct list_head *q; 1431da177e4SLinus Torvalds struct rpc_task *t; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds INIT_LIST_HEAD(&task->u.tk_wait.links); 1463b27bad7STrond Myklebust if (unlikely(queue_priority > queue->maxpriority)) 147c05eecf6STrond Myklebust queue_priority = queue->maxpriority; 148c05eecf6STrond Myklebust if (queue_priority > queue->priority) 149c05eecf6STrond Myklebust rpc_set_waitqueue_priority(queue, queue_priority); 150c05eecf6STrond Myklebust q = &queue->tasks[queue_priority]; 1511da177e4SLinus Torvalds list_for_each_entry(t, q, u.tk_wait.list) { 1523ff7576dSTrond Myklebust if (t->tk_owner == task->tk_owner) { 1531da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links); 1541da177e4SLinus Torvalds return; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, q); 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds /* 1611da177e4SLinus Torvalds * Add new request to wait queue. 1621da177e4SLinus Torvalds * 1631da177e4SLinus Torvalds * Swapper tasks always get inserted at the head of the queue. 1641da177e4SLinus Torvalds * This should avoid many nasty memory deadlocks and hopefully 1651da177e4SLinus Torvalds * improve overall performance. 1661da177e4SLinus Torvalds * Everyone else gets appended to the queue to ensure proper FIFO behavior. 1671da177e4SLinus Torvalds */ 1683b27bad7STrond Myklebust static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, 1693b27bad7STrond Myklebust struct rpc_task *task, 1703b27bad7STrond Myklebust unsigned char queue_priority) 1711da177e4SLinus Torvalds { 1722bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 1732bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 1742bd4eef8SWeston Andros Adamson return; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 1773b27bad7STrond Myklebust __rpc_add_wait_queue_priority(queue, task, queue_priority); 1781da177e4SLinus Torvalds else if (RPC_IS_SWAPPER(task)) 1791da177e4SLinus Torvalds list_add(&task->u.tk_wait.list, &queue->tasks[0]); 1801da177e4SLinus Torvalds else 1811da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]); 18296ef13b2STrond Myklebust task->tk_waitqueue = queue; 183e19b63daSChuck Lever queue->qlen++; 1841166fde6STrond Myklebust /* barrier matches the read in rpc_wake_up_task_queue_locked() */ 1851166fde6STrond Myklebust smp_wmb(); 1861da177e4SLinus Torvalds rpc_set_queued(task); 1871da177e4SLinus Torvalds 18846121cf7SChuck Lever dprintk("RPC: %5u added to queue %p \"%s\"\n", 1891da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* 1931da177e4SLinus Torvalds * Remove request from a priority queue. 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds struct rpc_task *t; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds if (!list_empty(&task->u.tk_wait.links)) { 2001da177e4SLinus Torvalds t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list); 2011da177e4SLinus Torvalds list_move(&t->u.tk_wait.list, &task->u.tk_wait.list); 2021da177e4SLinus Torvalds list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * Remove request from queue. 2081da177e4SLinus Torvalds * Note: must be called with spin lock held. 2091da177e4SLinus Torvalds */ 21096ef13b2STrond Myklebust static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task) 2111da177e4SLinus Torvalds { 212eb276c0eSTrond Myklebust __rpc_disable_timer(queue, task); 2131da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 2141da177e4SLinus Torvalds __rpc_remove_wait_queue_priority(task); 2151da177e4SLinus Torvalds list_del(&task->u.tk_wait.list); 216e19b63daSChuck Lever queue->qlen--; 21746121cf7SChuck Lever dprintk("RPC: %5u removed from queue %p \"%s\"\n", 2181da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2213ff7576dSTrond Myklebust static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds int i; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds spin_lock_init(&queue->lock); 2261da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(queue->tasks); i++) 2271da177e4SLinus Torvalds INIT_LIST_HEAD(&queue->tasks[i]); 2283ff7576dSTrond Myklebust queue->maxpriority = nr_queues - 1; 2291da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 23036df9aaeSTrond Myklebust queue->qlen = 0; 23136df9aaeSTrond Myklebust setup_timer(&queue->timer_list.timer, __rpc_queue_timer_fn, (unsigned long)queue); 23236df9aaeSTrond Myklebust INIT_LIST_HEAD(&queue->timer_list.list); 2332f09c242STrond Myklebust rpc_assign_waitqueue_name(queue, qname); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2371da177e4SLinus Torvalds { 2383ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY); 2391da177e4SLinus Torvalds } 240689cf5c1SAlexandros Batsakis EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2431da177e4SLinus Torvalds { 2443ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, 1); 2451da177e4SLinus Torvalds } 246e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_wait_queue); 2471da177e4SLinus Torvalds 248f6a1cc89STrond Myklebust void rpc_destroy_wait_queue(struct rpc_wait_queue *queue) 249f6a1cc89STrond Myklebust { 25036df9aaeSTrond Myklebust del_timer_sync(&queue->timer_list.timer); 251f6a1cc89STrond Myklebust } 252f6a1cc89STrond Myklebust EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue); 253f6a1cc89STrond Myklebust 254dfd01f02SPeter Zijlstra static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode) 25544c28873STrond Myklebust { 256416ad3c9SColin Cross freezable_schedule_unsafe(); 257dfd01f02SPeter Zijlstra if (signal_pending_state(mode, current)) 258dfd01f02SPeter Zijlstra return -ERESTARTSYS; 25944c28873STrond Myklebust return 0; 26044c28873STrond Myklebust } 26144c28873STrond Myklebust 2621306729bSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS) 263c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task) 264c44fe705STrond Myklebust { 265c44fe705STrond Myklebust static atomic_t rpc_pid; 266c44fe705STrond Myklebust 267c44fe705STrond Myklebust task->tk_pid = atomic_inc_return(&rpc_pid); 268c44fe705STrond Myklebust } 269c44fe705STrond Myklebust #else 270c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task) 271c44fe705STrond Myklebust { 272c44fe705STrond Myklebust } 273c44fe705STrond Myklebust #endif 274c44fe705STrond Myklebust 275e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task) 276e6b3c4dbSTrond Myklebust { 27782b0a4c3STrond Myklebust trace_rpc_task_begin(task->tk_client, task, NULL); 27882b0a4c3STrond Myklebust 279c44fe705STrond Myklebust rpc_task_set_debuginfo(task); 28058f9612cSTrond Myklebust set_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 281e6b3c4dbSTrond Myklebust } 282e6b3c4dbSTrond Myklebust 28344c28873STrond Myklebust /* 28444c28873STrond Myklebust * Mark an RPC call as having completed by clearing the 'active' bit 285bf294b41STrond Myklebust * and then waking up all tasks that were sleeping. 28644c28873STrond Myklebust */ 287bf294b41STrond Myklebust static int rpc_complete_task(struct rpc_task *task) 28844c28873STrond Myklebust { 289bf294b41STrond Myklebust void *m = &task->tk_runstate; 290bf294b41STrond Myklebust wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE); 291bf294b41STrond Myklebust struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE); 292bf294b41STrond Myklebust unsigned long flags; 293bf294b41STrond Myklebust int ret; 294bf294b41STrond Myklebust 29582b0a4c3STrond Myklebust trace_rpc_task_complete(task->tk_client, task, NULL); 29682b0a4c3STrond Myklebust 297bf294b41STrond Myklebust spin_lock_irqsave(&wq->lock, flags); 298e6b3c4dbSTrond Myklebust clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 299bf294b41STrond Myklebust ret = atomic_dec_and_test(&task->tk_count); 300bf294b41STrond Myklebust if (waitqueue_active(wq)) 301ac5be6b4SAndrea Arcangeli __wake_up_locked_key(wq, TASK_NORMAL, &k); 302bf294b41STrond Myklebust spin_unlock_irqrestore(&wq->lock, flags); 303bf294b41STrond Myklebust return ret; 30444c28873STrond Myklebust } 30544c28873STrond Myklebust 30644c28873STrond Myklebust /* 30744c28873STrond Myklebust * Allow callers to wait for completion of an RPC call 308bf294b41STrond Myklebust * 309bf294b41STrond Myklebust * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit() 310bf294b41STrond Myklebust * to enforce taking of the wq->lock and hence avoid races with 311bf294b41STrond Myklebust * rpc_complete_task(). 31244c28873STrond Myklebust */ 313c1221321SNeilBrown int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action) 31444c28873STrond Myklebust { 31544c28873STrond Myklebust if (action == NULL) 316150030b7SMatthew Wilcox action = rpc_wait_bit_killable; 317bf294b41STrond Myklebust return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE, 318150030b7SMatthew Wilcox action, TASK_KILLABLE); 31944c28873STrond Myklebust } 320e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task); 32144c28873STrond Myklebust 3221da177e4SLinus Torvalds /* 3231da177e4SLinus Torvalds * Make an RPC task runnable. 3241da177e4SLinus Torvalds * 325506026c3SJeff Layton * Note: If the task is ASYNC, and is being made runnable after sitting on an 326506026c3SJeff Layton * rpc_wait_queue, this must be called with the queue spinlock held to protect 327506026c3SJeff Layton * the wait queue operation. 328a3c3cac5STrond Myklebust * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(), 329a3c3cac5STrond Myklebust * which is needed to ensure that __rpc_execute() doesn't loop (due to the 330a3c3cac5STrond Myklebust * lockless RPC_IS_QUEUED() test) before we've had a chance to test 331a3c3cac5STrond Myklebust * the RPC_TASK_RUNNING flag. 3321da177e4SLinus Torvalds */ 333f1dc237cSTrond Myklebust static void rpc_make_runnable(struct workqueue_struct *wq, 334f1dc237cSTrond Myklebust struct rpc_task *task) 3351da177e4SLinus Torvalds { 336a3c3cac5STrond Myklebust bool need_wakeup = !rpc_test_and_set_running(task); 337a3c3cac5STrond Myklebust 3381da177e4SLinus Torvalds rpc_clear_queued(task); 339a3c3cac5STrond Myklebust if (!need_wakeup) 3401da177e4SLinus Torvalds return; 3411da177e4SLinus Torvalds if (RPC_IS_ASYNC(task)) { 34265f27f38SDavid Howells INIT_WORK(&task->u.tk_work, rpc_async_schedule); 343f1dc237cSTrond Myklebust queue_work(wq, &task->u.tk_work); 3441da177e4SLinus Torvalds } else 34596651ab3STrond Myklebust wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* 3491da177e4SLinus Torvalds * Prepare for sleeping on a wait queue. 3501da177e4SLinus Torvalds * By always appending tasks to the list we ensure FIFO behavior. 3511da177e4SLinus Torvalds * NB: An RPC task will only receive interrupt-driven events as long 3521da177e4SLinus Torvalds * as it's on a wait queue. 3531da177e4SLinus Torvalds */ 3543b27bad7STrond Myklebust static void __rpc_sleep_on_priority(struct rpc_wait_queue *q, 3553b27bad7STrond Myklebust struct rpc_task *task, 3563b27bad7STrond Myklebust rpc_action action, 3573b27bad7STrond Myklebust unsigned char queue_priority) 3581da177e4SLinus Torvalds { 35946121cf7SChuck Lever dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n", 36046121cf7SChuck Lever task->tk_pid, rpc_qname(q), jiffies); 3611da177e4SLinus Torvalds 36282b0a4c3STrond Myklebust trace_rpc_task_sleep(task->tk_client, task, q); 36382b0a4c3STrond Myklebust 3643b27bad7STrond Myklebust __rpc_add_wait_queue(q, task, queue_priority); 3651da177e4SLinus Torvalds 366f50ad428SWeston Andros Adamson WARN_ON_ONCE(task->tk_callback != NULL); 3671da177e4SLinus Torvalds task->tk_callback = action; 368eb276c0eSTrond Myklebust __rpc_add_timer(q, task); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, 3725d00837bSTrond Myklebust rpc_action action) 3731da177e4SLinus Torvalds { 37458f9612cSTrond Myklebust /* We shouldn't ever put an inactive task to sleep */ 375e454a7a8SWeston Andros Adamson WARN_ON_ONCE(!RPC_IS_ACTIVATED(task)); 376e454a7a8SWeston Andros Adamson if (!RPC_IS_ACTIVATED(task)) { 377e454a7a8SWeston Andros Adamson task->tk_status = -EIO; 378e454a7a8SWeston Andros Adamson rpc_put_task_async(task); 379e454a7a8SWeston Andros Adamson return; 380e454a7a8SWeston Andros Adamson } 381e6b3c4dbSTrond Myklebust 3821da177e4SLinus Torvalds /* 3831da177e4SLinus Torvalds * Protect the queue operations. 3841da177e4SLinus Torvalds */ 3851da177e4SLinus Torvalds spin_lock_bh(&q->lock); 3863b27bad7STrond Myklebust __rpc_sleep_on_priority(q, task, action, task->tk_priority); 3871da177e4SLinus Torvalds spin_unlock_bh(&q->lock); 3881da177e4SLinus Torvalds } 389e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on); 3901da177e4SLinus Torvalds 3913b27bad7STrond Myklebust void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task, 3923b27bad7STrond Myklebust rpc_action action, int priority) 3933b27bad7STrond Myklebust { 3943b27bad7STrond Myklebust /* We shouldn't ever put an inactive task to sleep */ 395e454a7a8SWeston Andros Adamson WARN_ON_ONCE(!RPC_IS_ACTIVATED(task)); 396e454a7a8SWeston Andros Adamson if (!RPC_IS_ACTIVATED(task)) { 397e454a7a8SWeston Andros Adamson task->tk_status = -EIO; 398e454a7a8SWeston Andros Adamson rpc_put_task_async(task); 399e454a7a8SWeston Andros Adamson return; 400e454a7a8SWeston Andros Adamson } 4013b27bad7STrond Myklebust 4023b27bad7STrond Myklebust /* 4033b27bad7STrond Myklebust * Protect the queue operations. 4043b27bad7STrond Myklebust */ 4053b27bad7STrond Myklebust spin_lock_bh(&q->lock); 4063b27bad7STrond Myklebust __rpc_sleep_on_priority(q, task, action, priority - RPC_PRIORITY_LOW); 4073b27bad7STrond Myklebust spin_unlock_bh(&q->lock); 4083b27bad7STrond Myklebust } 4091e1093c7STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority); 4103b27bad7STrond Myklebust 4111da177e4SLinus Torvalds /** 412f1dc237cSTrond Myklebust * __rpc_do_wake_up_task_on_wq - wake up a single rpc_task 413f1dc237cSTrond Myklebust * @wq: workqueue on which to run task 41496ef13b2STrond Myklebust * @queue: wait queue 4151da177e4SLinus Torvalds * @task: task to be woken up 4161da177e4SLinus Torvalds * 4171da177e4SLinus Torvalds * Caller must hold queue->lock, and have cleared the task queued flag. 4181da177e4SLinus Torvalds */ 419f1dc237cSTrond Myklebust static void __rpc_do_wake_up_task_on_wq(struct workqueue_struct *wq, 420f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 421f1dc237cSTrond Myklebust struct rpc_task *task) 4221da177e4SLinus Torvalds { 42346121cf7SChuck Lever dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n", 42446121cf7SChuck Lever task->tk_pid, jiffies); 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* Has the task been executed yet? If not, we cannot wake it up! */ 4271da177e4SLinus Torvalds if (!RPC_IS_ACTIVATED(task)) { 4281da177e4SLinus Torvalds printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task); 4291da177e4SLinus Torvalds return; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 43282b0a4c3STrond Myklebust trace_rpc_task_wakeup(task->tk_client, task, queue); 43382b0a4c3STrond Myklebust 43496ef13b2STrond Myklebust __rpc_remove_wait_queue(queue, task); 4351da177e4SLinus Torvalds 436f1dc237cSTrond Myklebust rpc_make_runnable(wq, task); 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds dprintk("RPC: __rpc_wake_up_task done\n"); 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds /* 44296ef13b2STrond Myklebust * Wake up a queued task while the queue lock is being held 4431da177e4SLinus Torvalds */ 444f1dc237cSTrond Myklebust static void rpc_wake_up_task_on_wq_queue_locked(struct workqueue_struct *wq, 445f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, struct rpc_task *task) 4461da177e4SLinus Torvalds { 4471166fde6STrond Myklebust if (RPC_IS_QUEUED(task)) { 4481166fde6STrond Myklebust smp_rmb(); 4491166fde6STrond Myklebust if (task->tk_waitqueue == queue) 450f1dc237cSTrond Myklebust __rpc_do_wake_up_task_on_wq(wq, queue, task); 4511da177e4SLinus Torvalds } 4521166fde6STrond Myklebust } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* 455f1dc237cSTrond Myklebust * Wake up a queued task while the queue lock is being held 456f1dc237cSTrond Myklebust */ 457f1dc237cSTrond Myklebust static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task) 458f1dc237cSTrond Myklebust { 459f1dc237cSTrond Myklebust rpc_wake_up_task_on_wq_queue_locked(rpciod_workqueue, queue, task); 460f1dc237cSTrond Myklebust } 461f1dc237cSTrond Myklebust 462f1dc237cSTrond Myklebust /* 46396ef13b2STrond Myklebust * Wake up a task on a specific queue 46496ef13b2STrond Myklebust */ 46596ef13b2STrond Myklebust void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task) 46696ef13b2STrond Myklebust { 4675e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 46896ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 4695e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 47096ef13b2STrond Myklebust } 47196ef13b2STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task); 47296ef13b2STrond Myklebust 47396ef13b2STrond Myklebust /* 4741da177e4SLinus Torvalds * Wake up the next task on a priority queue. 4751da177e4SLinus Torvalds */ 476961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds struct list_head *q; 4791da177e4SLinus Torvalds struct rpc_task *task; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds /* 4823ff7576dSTrond Myklebust * Service a batch of tasks from a single owner. 4831da177e4SLinus Torvalds */ 4841da177e4SLinus Torvalds q = &queue->tasks[queue->priority]; 4851da177e4SLinus Torvalds if (!list_empty(q)) { 4861da177e4SLinus Torvalds task = list_entry(q->next, struct rpc_task, u.tk_wait.list); 4873ff7576dSTrond Myklebust if (queue->owner == task->tk_owner) { 4881da177e4SLinus Torvalds if (--queue->nr) 4891da177e4SLinus Torvalds goto out; 4901da177e4SLinus Torvalds list_move_tail(&task->u.tk_wait.list, q); 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * Check if we need to switch queues. 4941da177e4SLinus Torvalds */ 4953ff7576dSTrond Myklebust goto new_owner; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /* 4991da177e4SLinus Torvalds * Service the next queue. 5001da177e4SLinus Torvalds */ 5011da177e4SLinus Torvalds do { 5021da177e4SLinus Torvalds if (q == &queue->tasks[0]) 5031da177e4SLinus Torvalds q = &queue->tasks[queue->maxpriority]; 5041da177e4SLinus Torvalds else 5051da177e4SLinus Torvalds q = q - 1; 5061da177e4SLinus Torvalds if (!list_empty(q)) { 5071da177e4SLinus Torvalds task = list_entry(q->next, struct rpc_task, u.tk_wait.list); 5081da177e4SLinus Torvalds goto new_queue; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds } while (q != &queue->tasks[queue->priority]); 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 5131da177e4SLinus Torvalds return NULL; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds new_queue: 5161da177e4SLinus Torvalds rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0])); 5173ff7576dSTrond Myklebust new_owner: 5183ff7576dSTrond Myklebust rpc_set_waitqueue_owner(queue, task->tk_owner); 5191da177e4SLinus Torvalds out: 5201da177e4SLinus Torvalds return task; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 523961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue) 524961a828dSTrond Myklebust { 525961a828dSTrond Myklebust if (RPC_IS_PRIORITY(queue)) 526961a828dSTrond Myklebust return __rpc_find_next_queued_priority(queue); 527961a828dSTrond Myklebust if (!list_empty(&queue->tasks[0])) 528961a828dSTrond Myklebust return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list); 529961a828dSTrond Myklebust return NULL; 530961a828dSTrond Myklebust } 531961a828dSTrond Myklebust 532961a828dSTrond Myklebust /* 533961a828dSTrond Myklebust * Wake up the first task on the wait queue. 534961a828dSTrond Myklebust */ 535f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq, 536f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 537961a828dSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 538961a828dSTrond Myklebust { 539961a828dSTrond Myklebust struct rpc_task *task = NULL; 540961a828dSTrond Myklebust 541961a828dSTrond Myklebust dprintk("RPC: wake_up_first(%p \"%s\")\n", 542961a828dSTrond Myklebust queue, rpc_qname(queue)); 543961a828dSTrond Myklebust spin_lock_bh(&queue->lock); 544961a828dSTrond Myklebust task = __rpc_find_next_queued(queue); 545961a828dSTrond Myklebust if (task != NULL) { 546961a828dSTrond Myklebust if (func(task, data)) 547f1dc237cSTrond Myklebust rpc_wake_up_task_on_wq_queue_locked(wq, queue, task); 548961a828dSTrond Myklebust else 549961a828dSTrond Myklebust task = NULL; 550961a828dSTrond Myklebust } 551961a828dSTrond Myklebust spin_unlock_bh(&queue->lock); 552961a828dSTrond Myklebust 553961a828dSTrond Myklebust return task; 554961a828dSTrond Myklebust } 555f1dc237cSTrond Myklebust 556f1dc237cSTrond Myklebust /* 557f1dc237cSTrond Myklebust * Wake up the first task on the wait queue. 558f1dc237cSTrond Myklebust */ 559f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue, 560f1dc237cSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 561f1dc237cSTrond Myklebust { 562f1dc237cSTrond Myklebust return rpc_wake_up_first_on_wq(rpciod_workqueue, queue, func, data); 563f1dc237cSTrond Myklebust } 564961a828dSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_first); 565961a828dSTrond Myklebust 566961a828dSTrond Myklebust static bool rpc_wake_up_next_func(struct rpc_task *task, void *data) 567961a828dSTrond Myklebust { 568961a828dSTrond Myklebust return true; 569961a828dSTrond Myklebust } 570961a828dSTrond Myklebust 5711da177e4SLinus Torvalds /* 5721da177e4SLinus Torvalds * Wake up the next task on the wait queue. 5731da177e4SLinus Torvalds */ 5741da177e4SLinus Torvalds struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue) 5751da177e4SLinus Torvalds { 576961a828dSTrond Myklebust return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL); 5771da177e4SLinus Torvalds } 578e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next); 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds /** 5811da177e4SLinus Torvalds * rpc_wake_up - wake up all rpc_tasks 5821da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 5831da177e4SLinus Torvalds * 5841da177e4SLinus Torvalds * Grabs queue->lock 5851da177e4SLinus Torvalds */ 5861da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue) 5871da177e4SLinus Torvalds { 5881da177e4SLinus Torvalds struct list_head *head; 589e6d83d55STrond Myklebust 5905e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 5911da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 5921da177e4SLinus Torvalds for (;;) { 593540a0f75STrond Myklebust while (!list_empty(head)) { 594540a0f75STrond Myklebust struct rpc_task *task; 595540a0f75STrond Myklebust task = list_first_entry(head, 596540a0f75STrond Myklebust struct rpc_task, 597540a0f75STrond Myklebust u.tk_wait.list); 59896ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 599540a0f75STrond Myklebust } 6001da177e4SLinus Torvalds if (head == &queue->tasks[0]) 6011da177e4SLinus Torvalds break; 6021da177e4SLinus Torvalds head--; 6031da177e4SLinus Torvalds } 6045e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 6051da177e4SLinus Torvalds } 606e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up); 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /** 6091da177e4SLinus Torvalds * rpc_wake_up_status - wake up all rpc_tasks and set their status value. 6101da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 6111da177e4SLinus Torvalds * @status: status value to set 6121da177e4SLinus Torvalds * 6131da177e4SLinus Torvalds * Grabs queue->lock 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status) 6161da177e4SLinus Torvalds { 6171da177e4SLinus Torvalds struct list_head *head; 6181da177e4SLinus Torvalds 6195e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 6201da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 6211da177e4SLinus Torvalds for (;;) { 622540a0f75STrond Myklebust while (!list_empty(head)) { 623540a0f75STrond Myklebust struct rpc_task *task; 624540a0f75STrond Myklebust task = list_first_entry(head, 625540a0f75STrond Myklebust struct rpc_task, 626540a0f75STrond Myklebust u.tk_wait.list); 6271da177e4SLinus Torvalds task->tk_status = status; 62896ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds if (head == &queue->tasks[0]) 6311da177e4SLinus Torvalds break; 6321da177e4SLinus Torvalds head--; 6331da177e4SLinus Torvalds } 6345e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 6351da177e4SLinus Torvalds } 636e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status); 6371da177e4SLinus Torvalds 63836df9aaeSTrond Myklebust static void __rpc_queue_timer_fn(unsigned long ptr) 63936df9aaeSTrond Myklebust { 64036df9aaeSTrond Myklebust struct rpc_wait_queue *queue = (struct rpc_wait_queue *)ptr; 64136df9aaeSTrond Myklebust struct rpc_task *task, *n; 64236df9aaeSTrond Myklebust unsigned long expires, now, timeo; 64336df9aaeSTrond Myklebust 64436df9aaeSTrond Myklebust spin_lock(&queue->lock); 64536df9aaeSTrond Myklebust expires = now = jiffies; 64636df9aaeSTrond Myklebust list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) { 64736df9aaeSTrond Myklebust timeo = task->u.tk_wait.expires; 64836df9aaeSTrond Myklebust if (time_after_eq(now, timeo)) { 64936df9aaeSTrond Myklebust dprintk("RPC: %5u timeout\n", task->tk_pid); 65036df9aaeSTrond Myklebust task->tk_status = -ETIMEDOUT; 65136df9aaeSTrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 65236df9aaeSTrond Myklebust continue; 65336df9aaeSTrond Myklebust } 65436df9aaeSTrond Myklebust if (expires == now || time_after(expires, timeo)) 65536df9aaeSTrond Myklebust expires = timeo; 65636df9aaeSTrond Myklebust } 65736df9aaeSTrond Myklebust if (!list_empty(&queue->timer_list.list)) 65836df9aaeSTrond Myklebust rpc_set_queue_timer(queue, expires); 65936df9aaeSTrond Myklebust spin_unlock(&queue->lock); 66036df9aaeSTrond Myklebust } 66136df9aaeSTrond Myklebust 6628014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task) 6638014793bSTrond Myklebust { 6646bd14416STrond Myklebust if (task->tk_status == -ETIMEDOUT) 6655d00837bSTrond Myklebust task->tk_status = 0; 6668014793bSTrond Myklebust } 6678014793bSTrond Myklebust 6681da177e4SLinus Torvalds /* 6691da177e4SLinus Torvalds * Run a task at a later time 6701da177e4SLinus Torvalds */ 6718014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds task->tk_timeout = delay; 6745d00837bSTrond Myklebust rpc_sleep_on(&delay_queue, task, __rpc_atrun); 6751da177e4SLinus Torvalds } 676e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* 6794ce70adaSTrond Myklebust * Helper to call task->tk_ops->rpc_call_prepare 6804ce70adaSTrond Myklebust */ 681aae2006eSAndy Adamson void rpc_prepare_task(struct rpc_task *task) 6824ce70adaSTrond Myklebust { 6834ce70adaSTrond Myklebust task->tk_ops->rpc_call_prepare(task, task->tk_calldata); 6844ce70adaSTrond Myklebust } 6854ce70adaSTrond Myklebust 6867fdcf13bSTrond Myklebust static void 6877fdcf13bSTrond Myklebust rpc_init_task_statistics(struct rpc_task *task) 6887fdcf13bSTrond Myklebust { 6897fdcf13bSTrond Myklebust /* Initialize retry counters */ 6907fdcf13bSTrond Myklebust task->tk_garb_retry = 2; 6917fdcf13bSTrond Myklebust task->tk_cred_retry = 2; 6927fdcf13bSTrond Myklebust task->tk_rebind_retry = 2; 6937fdcf13bSTrond Myklebust 6947fdcf13bSTrond Myklebust /* starting timestamp */ 6957fdcf13bSTrond Myklebust task->tk_start = ktime_get(); 6967fdcf13bSTrond Myklebust } 6977fdcf13bSTrond Myklebust 6987fdcf13bSTrond Myklebust static void 6997fdcf13bSTrond Myklebust rpc_reset_task_statistics(struct rpc_task *task) 7007fdcf13bSTrond Myklebust { 7017fdcf13bSTrond Myklebust task->tk_timeouts = 0; 7027fdcf13bSTrond Myklebust task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT); 7037fdcf13bSTrond Myklebust 7047fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 7057fdcf13bSTrond Myklebust } 7067fdcf13bSTrond Myklebust 7074ce70adaSTrond Myklebust /* 708963d8fe5STrond Myklebust * Helper that calls task->tk_ops->rpc_call_done if it exists 709d05fdb0cSTrond Myklebust */ 710abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task) 711d05fdb0cSTrond Myklebust { 712abbcf28fSTrond Myklebust task->tk_action = NULL; 713963d8fe5STrond Myklebust if (task->tk_ops->rpc_call_done != NULL) { 714963d8fe5STrond Myklebust task->tk_ops->rpc_call_done(task, task->tk_calldata); 715d05fdb0cSTrond Myklebust if (task->tk_action != NULL) { 716abbcf28fSTrond Myklebust WARN_ON(RPC_ASSASSINATED(task)); 717abbcf28fSTrond Myklebust /* Always release the RPC slot and buffer memory */ 718d05fdb0cSTrond Myklebust xprt_release(task); 7197fdcf13bSTrond Myklebust rpc_reset_task_statistics(task); 720d05fdb0cSTrond Myklebust } 721d05fdb0cSTrond Myklebust } 722d05fdb0cSTrond Myklebust } 723d9b6cd94STrond Myklebust 724d9b6cd94STrond Myklebust void rpc_exit(struct rpc_task *task, int status) 725d9b6cd94STrond Myklebust { 726d9b6cd94STrond Myklebust task->tk_status = status; 727d9b6cd94STrond Myklebust task->tk_action = rpc_exit_task; 728d9b6cd94STrond Myklebust if (RPC_IS_QUEUED(task)) 729d9b6cd94STrond Myklebust rpc_wake_up_queued_task(task->tk_waitqueue, task); 730d9b6cd94STrond Myklebust } 731d9b6cd94STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit); 732d05fdb0cSTrond Myklebust 733bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata) 734bbd5a1f9STrond Myklebust { 735a86dc496STrond Myklebust if (ops->rpc_release != NULL) 736bbd5a1f9STrond Myklebust ops->rpc_release(calldata); 737bbd5a1f9STrond Myklebust } 738bbd5a1f9STrond Myklebust 739d05fdb0cSTrond Myklebust /* 7401da177e4SLinus Torvalds * This is the RPC `scheduler' (or rather, the finite state machine). 7411da177e4SLinus Torvalds */ 7422efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task) 7431da177e4SLinus Torvalds { 744eb9b55abSTrond Myklebust struct rpc_wait_queue *queue; 745eb9b55abSTrond Myklebust int task_is_async = RPC_IS_ASYNC(task); 7461da177e4SLinus Torvalds int status = 0; 7471da177e4SLinus Torvalds 74846121cf7SChuck Lever dprintk("RPC: %5u __rpc_execute flags=0x%x\n", 7491da177e4SLinus Torvalds task->tk_pid, task->tk_flags); 7501da177e4SLinus Torvalds 7512bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 7522bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 7532bd4eef8SWeston Andros Adamson return; 7541da177e4SLinus Torvalds 755d05fdb0cSTrond Myklebust for (;;) { 756b55c5989STrond Myklebust void (*do_action)(struct rpc_task *); 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /* 759b55c5989STrond Myklebust * Execute any pending callback first. 7601da177e4SLinus Torvalds */ 761b55c5989STrond Myklebust do_action = task->tk_callback; 7621da177e4SLinus Torvalds task->tk_callback = NULL; 763b55c5989STrond Myklebust if (do_action == NULL) { 7641da177e4SLinus Torvalds /* 7651da177e4SLinus Torvalds * Perform the next FSM step. 766b55c5989STrond Myklebust * tk_action may be NULL if the task has been killed. 767b55c5989STrond Myklebust * In particular, note that rpc_killall_tasks may 768b55c5989STrond Myklebust * do this at any time, so beware when dereferencing. 7691da177e4SLinus Torvalds */ 770b55c5989STrond Myklebust do_action = task->tk_action; 771b55c5989STrond Myklebust if (do_action == NULL) 772abbcf28fSTrond Myklebust break; 7731da177e4SLinus Torvalds } 77482b0a4c3STrond Myklebust trace_rpc_task_run_action(task->tk_client, task, task->tk_action); 775b55c5989STrond Myklebust do_action(task); 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* 7781da177e4SLinus Torvalds * Lockless check for whether task is sleeping or not. 7791da177e4SLinus Torvalds */ 7801da177e4SLinus Torvalds if (!RPC_IS_QUEUED(task)) 7811da177e4SLinus Torvalds continue; 782eb9b55abSTrond Myklebust /* 783eb9b55abSTrond Myklebust * The queue->lock protects against races with 784eb9b55abSTrond Myklebust * rpc_make_runnable(). 785eb9b55abSTrond Myklebust * 786eb9b55abSTrond Myklebust * Note that once we clear RPC_TASK_RUNNING on an asynchronous 787eb9b55abSTrond Myklebust * rpc_task, rpc_make_runnable() can assign it to a 788eb9b55abSTrond Myklebust * different workqueue. We therefore cannot assume that the 789eb9b55abSTrond Myklebust * rpc_task pointer may still be dereferenced. 790eb9b55abSTrond Myklebust */ 791eb9b55abSTrond Myklebust queue = task->tk_waitqueue; 792eb9b55abSTrond Myklebust spin_lock_bh(&queue->lock); 793eb9b55abSTrond Myklebust if (!RPC_IS_QUEUED(task)) { 794eb9b55abSTrond Myklebust spin_unlock_bh(&queue->lock); 7951da177e4SLinus Torvalds continue; 7961da177e4SLinus Torvalds } 797eb9b55abSTrond Myklebust rpc_clear_running(task); 798eb9b55abSTrond Myklebust spin_unlock_bh(&queue->lock); 799eb9b55abSTrond Myklebust if (task_is_async) 800eb9b55abSTrond Myklebust return; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds /* sync task: sleep here */ 80346121cf7SChuck Lever dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid); 80496651ab3STrond Myklebust status = out_of_line_wait_on_bit(&task->tk_runstate, 805150030b7SMatthew Wilcox RPC_TASK_QUEUED, rpc_wait_bit_killable, 806150030b7SMatthew Wilcox TASK_KILLABLE); 80796651ab3STrond Myklebust if (status == -ERESTARTSYS) { 8081da177e4SLinus Torvalds /* 8091da177e4SLinus Torvalds * When a sync task receives a signal, it exits with 8101da177e4SLinus Torvalds * -ERESTARTSYS. In order to catch any callbacks that 8111da177e4SLinus Torvalds * clean up after sleeping on some queue, we don't 8121da177e4SLinus Torvalds * break the loop here, but go around once more. 8131da177e4SLinus Torvalds */ 81446121cf7SChuck Lever dprintk("RPC: %5u got signal\n", task->tk_pid); 8151da177e4SLinus Torvalds task->tk_flags |= RPC_TASK_KILLED; 8161da177e4SLinus Torvalds rpc_exit(task, -ERESTARTSYS); 8171da177e4SLinus Torvalds } 81846121cf7SChuck Lever dprintk("RPC: %5u sync task resuming\n", task->tk_pid); 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 82146121cf7SChuck Lever dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status, 82246121cf7SChuck Lever task->tk_status); 8231da177e4SLinus Torvalds /* Release all resources associated with the task */ 8241da177e4SLinus Torvalds rpc_release_task(task); 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * User-visible entry point to the scheduler. 8291da177e4SLinus Torvalds * 8301da177e4SLinus Torvalds * This may be called recursively if e.g. an async NFS task updates 8311da177e4SLinus Torvalds * the attributes and finds that dirty pages must be flushed. 8321da177e4SLinus Torvalds * NOTE: Upon exit of this function the task is guaranteed to be 8331da177e4SLinus Torvalds * released. In particular note that tk_release() will have 8341da177e4SLinus Torvalds * been called, so your task memory may have been freed. 8351da177e4SLinus Torvalds */ 8362efef837STrond Myklebust void rpc_execute(struct rpc_task *task) 8371da177e4SLinus Torvalds { 838a76580fbSTrond Myklebust bool is_async = RPC_IS_ASYNC(task); 839a76580fbSTrond Myklebust 84044c28873STrond Myklebust rpc_set_active(task); 841f1dc237cSTrond Myklebust rpc_make_runnable(rpciod_workqueue, task); 842a76580fbSTrond Myklebust if (!is_async) 8432efef837STrond Myklebust __rpc_execute(task); 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 84665f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work) 8471da177e4SLinus Torvalds { 84865f27f38SDavid Howells __rpc_execute(container_of(work, struct rpc_task, u.tk_work)); 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 85102107148SChuck Lever /** 852*5fe6eaa1SChuck Lever * rpc_malloc - allocate RPC buffer resources 853*5fe6eaa1SChuck Lever * @task: RPC task 854*5fe6eaa1SChuck Lever * 855*5fe6eaa1SChuck Lever * A single memory region is allocated, which is split between the 856*5fe6eaa1SChuck Lever * RPC call and RPC reply that this task is being used for. When 857*5fe6eaa1SChuck Lever * this RPC is retired, the memory is released by calling rpc_free. 8581da177e4SLinus Torvalds * 859c5a4dd8bSChuck Lever * To prevent rpciod from hanging, this allocator never sleeps, 860*5fe6eaa1SChuck Lever * returning -ENOMEM and suppressing warning if the request cannot 861*5fe6eaa1SChuck Lever * be serviced immediately. The caller can arrange to sleep in a 862*5fe6eaa1SChuck Lever * way that is safe for rpciod. 863c5a4dd8bSChuck Lever * 864c5a4dd8bSChuck Lever * Most requests are 'small' (under 2KiB) and can be serviced from a 865c5a4dd8bSChuck Lever * mempool, ensuring that NFS reads and writes can always proceed, 866c5a4dd8bSChuck Lever * and that there is good locality of reference for these buffers. 867c5a4dd8bSChuck Lever * 8681da177e4SLinus Torvalds * In order to avoid memory starvation triggering more writebacks of 869c5a4dd8bSChuck Lever * NFS requests, we avoid using GFP_KERNEL. 8701da177e4SLinus Torvalds */ 871*5fe6eaa1SChuck Lever int rpc_malloc(struct rpc_task *task) 8721da177e4SLinus Torvalds { 873*5fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 874*5fe6eaa1SChuck Lever size_t size = rqst->rq_callsize + rqst->rq_rcvsize; 875aa3d1faeSChuck Lever struct rpc_buffer *buf; 876c4a7ca77STrond Myklebust gfp_t gfp = GFP_NOIO | __GFP_NOWARN; 877a564b8f0SMel Gorman 878a564b8f0SMel Gorman if (RPC_IS_SWAPPER(task)) 879c4a7ca77STrond Myklebust gfp = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN; 8801da177e4SLinus Torvalds 881aa3d1faeSChuck Lever size += sizeof(struct rpc_buffer); 882c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 883c5a4dd8bSChuck Lever buf = mempool_alloc(rpc_buffer_mempool, gfp); 8841da177e4SLinus Torvalds else 885c5a4dd8bSChuck Lever buf = kmalloc(size, gfp); 886ddce40dfSPeter Zijlstra 887ddce40dfSPeter Zijlstra if (!buf) 888*5fe6eaa1SChuck Lever return -ENOMEM; 889ddce40dfSPeter Zijlstra 890aa3d1faeSChuck Lever buf->len = size; 891215d0678SGeert Uytterhoeven dprintk("RPC: %5u allocated buffer of size %zu at %p\n", 892c5a4dd8bSChuck Lever task->tk_pid, size, buf); 893*5fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 894*5fe6eaa1SChuck Lever return 0; 8951da177e4SLinus Torvalds } 89612444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc); 8971da177e4SLinus Torvalds 89802107148SChuck Lever /** 89902107148SChuck Lever * rpc_free - free buffer allocated via rpc_malloc 900c5a4dd8bSChuck Lever * @buffer: buffer to free 90102107148SChuck Lever * 90202107148SChuck Lever */ 903c5a4dd8bSChuck Lever void rpc_free(void *buffer) 9041da177e4SLinus Torvalds { 905aa3d1faeSChuck Lever size_t size; 906aa3d1faeSChuck Lever struct rpc_buffer *buf; 90702107148SChuck Lever 908c5a4dd8bSChuck Lever if (!buffer) 909c5a4dd8bSChuck Lever return; 910aa3d1faeSChuck Lever 911aa3d1faeSChuck Lever buf = container_of(buffer, struct rpc_buffer, data); 912aa3d1faeSChuck Lever size = buf->len; 913c5a4dd8bSChuck Lever 914215d0678SGeert Uytterhoeven dprintk("RPC: freeing buffer of size %zu at %p\n", 915c5a4dd8bSChuck Lever size, buf); 916aa3d1faeSChuck Lever 917c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 918c5a4dd8bSChuck Lever mempool_free(buf, rpc_buffer_mempool); 9191da177e4SLinus Torvalds else 920c5a4dd8bSChuck Lever kfree(buf); 9211da177e4SLinus Torvalds } 92212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds /* 9251da177e4SLinus Torvalds * Creation and deletion of RPC task structures 9261da177e4SLinus Torvalds */ 92747fe0648STrond Myklebust static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data) 9281da177e4SLinus Torvalds { 9291da177e4SLinus Torvalds memset(task, 0, sizeof(*task)); 93044c28873STrond Myklebust atomic_set(&task->tk_count, 1); 93184115e1cSTrond Myklebust task->tk_flags = task_setup_data->flags; 93284115e1cSTrond Myklebust task->tk_ops = task_setup_data->callback_ops; 93384115e1cSTrond Myklebust task->tk_calldata = task_setup_data->callback_data; 9346529eba0STrond Myklebust INIT_LIST_HEAD(&task->tk_task); 9351da177e4SLinus Torvalds 9363ff7576dSTrond Myklebust task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW; 9373ff7576dSTrond Myklebust task->tk_owner = current->tgid; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /* Initialize workqueue for async tasks */ 94032bfb5c0STrond Myklebust task->tk_workqueue = task_setup_data->workqueue; 9411da177e4SLinus Torvalds 9429d61498dSTrond Myklebust task->tk_xprt = xprt_get(task_setup_data->rpc_xprt); 9439d61498dSTrond Myklebust 94484115e1cSTrond Myklebust if (task->tk_ops->rpc_call_prepare != NULL) 94584115e1cSTrond Myklebust task->tk_action = rpc_prepare_task; 946963d8fe5STrond Myklebust 9477fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 948ef759a2eSChuck Lever 94946121cf7SChuck Lever dprintk("RPC: new task initialized, procpid %u\n", 950ba25f9dcSPavel Emelyanov task_pid_nr(current)); 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds static struct rpc_task * 9541da177e4SLinus Torvalds rpc_alloc_task(void) 9551da177e4SLinus Torvalds { 956a564b8f0SMel Gorman return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOIO); 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds /* 96090c5755fSTrond Myklebust * Create a new task for the specified client. 9611da177e4SLinus Torvalds */ 96284115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data) 9631da177e4SLinus Torvalds { 964e8f5d77cSTrond Myklebust struct rpc_task *task = setup_data->task; 965e8f5d77cSTrond Myklebust unsigned short flags = 0; 9661da177e4SLinus Torvalds 967e8f5d77cSTrond Myklebust if (task == NULL) { 9681da177e4SLinus Torvalds task = rpc_alloc_task(); 96919445b99STrond Myklebust if (task == NULL) { 97019445b99STrond Myklebust rpc_release_calldata(setup_data->callback_ops, 97119445b99STrond Myklebust setup_data->callback_data); 97219445b99STrond Myklebust return ERR_PTR(-ENOMEM); 97319445b99STrond Myklebust } 974e8f5d77cSTrond Myklebust flags = RPC_TASK_DYNAMIC; 975e8f5d77cSTrond Myklebust } 9761da177e4SLinus Torvalds 97784115e1cSTrond Myklebust rpc_init_task(task, setup_data); 978e8f5d77cSTrond Myklebust task->tk_flags |= flags; 97946121cf7SChuck Lever dprintk("RPC: allocated task %p\n", task); 9801da177e4SLinus Torvalds return task; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 983c6567ed1STrond Myklebust /* 984c6567ed1STrond Myklebust * rpc_free_task - release rpc task and perform cleanups 985c6567ed1STrond Myklebust * 986c6567ed1STrond Myklebust * Note that we free up the rpc_task _after_ rpc_release_calldata() 987c6567ed1STrond Myklebust * in order to work around a workqueue dependency issue. 988c6567ed1STrond Myklebust * 989c6567ed1STrond Myklebust * Tejun Heo states: 990c6567ed1STrond Myklebust * "Workqueue currently considers two work items to be the same if they're 991c6567ed1STrond Myklebust * on the same address and won't execute them concurrently - ie. it 992c6567ed1STrond Myklebust * makes a work item which is queued again while being executed wait 993c6567ed1STrond Myklebust * for the previous execution to complete. 994c6567ed1STrond Myklebust * 995c6567ed1STrond Myklebust * If a work function frees the work item, and then waits for an event 996c6567ed1STrond Myklebust * which should be performed by another work item and *that* work item 997c6567ed1STrond Myklebust * recycles the freed work item, it can create a false dependency loop. 998c6567ed1STrond Myklebust * There really is no reliable way to detect this short of verifying 999c6567ed1STrond Myklebust * every memory free." 1000c6567ed1STrond Myklebust * 1001c6567ed1STrond Myklebust */ 100232bfb5c0STrond Myklebust static void rpc_free_task(struct rpc_task *task) 10031da177e4SLinus Torvalds { 1004c6567ed1STrond Myklebust unsigned short tk_flags = task->tk_flags; 10051da177e4SLinus Torvalds 1006c6567ed1STrond Myklebust rpc_release_calldata(task->tk_ops, task->tk_calldata); 1007c6567ed1STrond Myklebust 1008c6567ed1STrond Myklebust if (tk_flags & RPC_TASK_DYNAMIC) { 10095e4424afSTrond Myklebust dprintk("RPC: %5u freeing task\n", task->tk_pid); 10105e4424afSTrond Myklebust mempool_free(task, rpc_task_mempool); 10115e4424afSTrond Myklebust } 101232bfb5c0STrond Myklebust } 101332bfb5c0STrond Myklebust 101432bfb5c0STrond Myklebust static void rpc_async_release(struct work_struct *work) 101532bfb5c0STrond Myklebust { 101632bfb5c0STrond Myklebust rpc_free_task(container_of(work, struct rpc_task, u.tk_work)); 101732bfb5c0STrond Myklebust } 101832bfb5c0STrond Myklebust 1019bf294b41STrond Myklebust static void rpc_release_resources_task(struct rpc_task *task) 102032bfb5c0STrond Myklebust { 1021e6b3c4dbSTrond Myklebust xprt_release(task); 1022a271c5a0SOGAWA Hirofumi if (task->tk_msg.rpc_cred) { 1023a17c2153STrond Myklebust put_rpccred(task->tk_msg.rpc_cred); 1024a271c5a0SOGAWA Hirofumi task->tk_msg.rpc_cred = NULL; 1025a271c5a0SOGAWA Hirofumi } 102658f9612cSTrond Myklebust rpc_task_release_client(task); 1027bf294b41STrond Myklebust } 1028bf294b41STrond Myklebust 1029bf294b41STrond Myklebust static void rpc_final_put_task(struct rpc_task *task, 1030bf294b41STrond Myklebust struct workqueue_struct *q) 1031bf294b41STrond Myklebust { 1032bf294b41STrond Myklebust if (q != NULL) { 103332bfb5c0STrond Myklebust INIT_WORK(&task->u.tk_work, rpc_async_release); 1034bf294b41STrond Myklebust queue_work(q, &task->u.tk_work); 103532bfb5c0STrond Myklebust } else 103632bfb5c0STrond Myklebust rpc_free_task(task); 1037e6b3c4dbSTrond Myklebust } 1038bf294b41STrond Myklebust 1039bf294b41STrond Myklebust static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q) 1040bf294b41STrond Myklebust { 1041bf294b41STrond Myklebust if (atomic_dec_and_test(&task->tk_count)) { 1042bf294b41STrond Myklebust rpc_release_resources_task(task); 1043bf294b41STrond Myklebust rpc_final_put_task(task, q); 1044bf294b41STrond Myklebust } 1045bf294b41STrond Myklebust } 1046bf294b41STrond Myklebust 1047bf294b41STrond Myklebust void rpc_put_task(struct rpc_task *task) 1048bf294b41STrond Myklebust { 1049bf294b41STrond Myklebust rpc_do_put_task(task, NULL); 1050bf294b41STrond Myklebust } 1051e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task); 1052e6b3c4dbSTrond Myklebust 1053bf294b41STrond Myklebust void rpc_put_task_async(struct rpc_task *task) 1054bf294b41STrond Myklebust { 1055bf294b41STrond Myklebust rpc_do_put_task(task, task->tk_workqueue); 1056bf294b41STrond Myklebust } 1057bf294b41STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task_async); 1058bf294b41STrond Myklebust 1059bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task) 1060e6b3c4dbSTrond Myklebust { 106146121cf7SChuck Lever dprintk("RPC: %5u release task\n", task->tk_pid); 10621da177e4SLinus Torvalds 10630a0c2a57SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 10641da177e4SLinus Torvalds 1065bf294b41STrond Myklebust rpc_release_resources_task(task); 1066e6b3c4dbSTrond Myklebust 1067bf294b41STrond Myklebust /* 1068bf294b41STrond Myklebust * Note: at this point we have been removed from rpc_clnt->cl_tasks, 1069bf294b41STrond Myklebust * so it should be safe to use task->tk_count as a test for whether 1070bf294b41STrond Myklebust * or not any other processes still hold references to our rpc_task. 1071bf294b41STrond Myklebust */ 1072bf294b41STrond Myklebust if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) { 1073bf294b41STrond Myklebust /* Wake up anyone who may be waiting for task completion */ 1074bf294b41STrond Myklebust if (!rpc_complete_task(task)) 1075bf294b41STrond Myklebust return; 1076bf294b41STrond Myklebust } else { 1077bf294b41STrond Myklebust if (!atomic_dec_and_test(&task->tk_count)) 1078bf294b41STrond Myklebust return; 1079bf294b41STrond Myklebust } 1080bf294b41STrond Myklebust rpc_final_put_task(task, task->tk_workqueue); 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 1083b247bbf1STrond Myklebust int rpciod_up(void) 1084b247bbf1STrond Myklebust { 1085b247bbf1STrond Myklebust return try_module_get(THIS_MODULE) ? 0 : -EINVAL; 1086b247bbf1STrond Myklebust } 1087b247bbf1STrond Myklebust 1088b247bbf1STrond Myklebust void rpciod_down(void) 1089b247bbf1STrond Myklebust { 1090b247bbf1STrond Myklebust module_put(THIS_MODULE); 1091b247bbf1STrond Myklebust } 1092b247bbf1STrond Myklebust 10931da177e4SLinus Torvalds /* 1094b247bbf1STrond Myklebust * Start up the rpciod workqueue. 10951da177e4SLinus Torvalds */ 1096b247bbf1STrond Myklebust static int rpciod_start(void) 10971da177e4SLinus Torvalds { 10981da177e4SLinus Torvalds struct workqueue_struct *wq; 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* 11011da177e4SLinus Torvalds * Create the rpciod thread and wait for it to start. 11021da177e4SLinus Torvalds */ 1103ab418d70STrond Myklebust dprintk("RPC: creating workqueue rpciod\n"); 110440a5f1b1STrond Myklebust wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM, 0); 110540a5f1b1STrond Myklebust if (!wq) 110640a5f1b1STrond Myklebust goto out_failed; 11071da177e4SLinus Torvalds rpciod_workqueue = wq; 110840a5f1b1STrond Myklebust /* Note: highpri because network receive is latency sensitive */ 110940a5f1b1STrond Myklebust wq = alloc_workqueue("xprtiod", WQ_MEM_RECLAIM | WQ_HIGHPRI, 0); 111040a5f1b1STrond Myklebust if (!wq) 111140a5f1b1STrond Myklebust goto free_rpciod; 111240a5f1b1STrond Myklebust xprtiod_workqueue = wq; 111340a5f1b1STrond Myklebust return 1; 111440a5f1b1STrond Myklebust free_rpciod: 111540a5f1b1STrond Myklebust wq = rpciod_workqueue; 111640a5f1b1STrond Myklebust rpciod_workqueue = NULL; 111740a5f1b1STrond Myklebust destroy_workqueue(wq); 111840a5f1b1STrond Myklebust out_failed: 111940a5f1b1STrond Myklebust return 0; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 1122b247bbf1STrond Myklebust static void rpciod_stop(void) 11231da177e4SLinus Torvalds { 1124b247bbf1STrond Myklebust struct workqueue_struct *wq = NULL; 1125ab418d70STrond Myklebust 1126b247bbf1STrond Myklebust if (rpciod_workqueue == NULL) 1127b247bbf1STrond Myklebust return; 1128ab418d70STrond Myklebust dprintk("RPC: destroying workqueue rpciod\n"); 11291da177e4SLinus Torvalds 1130b247bbf1STrond Myklebust wq = rpciod_workqueue; 11311da177e4SLinus Torvalds rpciod_workqueue = NULL; 1132b247bbf1STrond Myklebust destroy_workqueue(wq); 113340a5f1b1STrond Myklebust wq = xprtiod_workqueue; 113440a5f1b1STrond Myklebust xprtiod_workqueue = NULL; 113540a5f1b1STrond Myklebust destroy_workqueue(wq); 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds void 11391da177e4SLinus Torvalds rpc_destroy_mempool(void) 11401da177e4SLinus Torvalds { 1141b247bbf1STrond Myklebust rpciod_stop(); 11421da177e4SLinus Torvalds mempool_destroy(rpc_buffer_mempool); 11431da177e4SLinus Torvalds mempool_destroy(rpc_task_mempool); 11441a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_task_slabp); 11451a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_buffer_slabp); 1146f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&delay_queue); 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds int 11501da177e4SLinus Torvalds rpc_init_mempool(void) 11511da177e4SLinus Torvalds { 1152f6a1cc89STrond Myklebust /* 1153f6a1cc89STrond Myklebust * The following is not strictly a mempool initialisation, 1154f6a1cc89STrond Myklebust * but there is no harm in doing it here 1155f6a1cc89STrond Myklebust */ 1156f6a1cc89STrond Myklebust rpc_init_wait_queue(&delay_queue, "delayq"); 1157f6a1cc89STrond Myklebust if (!rpciod_start()) 1158f6a1cc89STrond Myklebust goto err_nomem; 1159f6a1cc89STrond Myklebust 11601da177e4SLinus Torvalds rpc_task_slabp = kmem_cache_create("rpc_tasks", 11611da177e4SLinus Torvalds sizeof(struct rpc_task), 11621da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 116320c2df83SPaul Mundt NULL); 11641da177e4SLinus Torvalds if (!rpc_task_slabp) 11651da177e4SLinus Torvalds goto err_nomem; 11661da177e4SLinus Torvalds rpc_buffer_slabp = kmem_cache_create("rpc_buffers", 11671da177e4SLinus Torvalds RPC_BUFFER_MAXSIZE, 11681da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 116920c2df83SPaul Mundt NULL); 11701da177e4SLinus Torvalds if (!rpc_buffer_slabp) 11711da177e4SLinus Torvalds goto err_nomem; 117293d2341cSMatthew Dobson rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE, 11731da177e4SLinus Torvalds rpc_task_slabp); 11741da177e4SLinus Torvalds if (!rpc_task_mempool) 11751da177e4SLinus Torvalds goto err_nomem; 117693d2341cSMatthew Dobson rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE, 11771da177e4SLinus Torvalds rpc_buffer_slabp); 11781da177e4SLinus Torvalds if (!rpc_buffer_mempool) 11791da177e4SLinus Torvalds goto err_nomem; 11801da177e4SLinus Torvalds return 0; 11811da177e4SLinus Torvalds err_nomem: 11821da177e4SLinus Torvalds rpc_destroy_mempool(); 11831da177e4SLinus Torvalds return -ENOMEM; 11841da177e4SLinus Torvalds } 1185