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); 47ff861c4dSKees Cook static void __rpc_queue_timer_fn(struct timer_list *t); 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 102c05eecf6STrond Myklebust static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority) 103c05eecf6STrond Myklebust { 104edd2e36fSTrond Myklebust if (queue->priority != priority) { 105c05eecf6STrond Myklebust queue->priority = priority; 106f42f7c28STrond Myklebust queue->nr = 1U << priority; 107c05eecf6STrond Myklebust } 108edd2e36fSTrond Myklebust } 109c05eecf6STrond Myklebust 110c05eecf6STrond Myklebust static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue) 111c05eecf6STrond Myklebust { 112c05eecf6STrond Myklebust rpc_set_waitqueue_priority(queue, queue->maxpriority); 113f42f7c28STrond Myklebust } 114f42f7c28STrond Myklebust 115f42f7c28STrond Myklebust /* 116f42f7c28STrond Myklebust * Add a request to a queue list 117f42f7c28STrond Myklebust */ 118f42f7c28STrond Myklebust static void 119f42f7c28STrond Myklebust __rpc_list_enqueue_task(struct list_head *q, struct rpc_task *task) 120f42f7c28STrond Myklebust { 121f42f7c28STrond Myklebust struct rpc_task *t; 122f42f7c28STrond Myklebust 123f42f7c28STrond Myklebust list_for_each_entry(t, q, u.tk_wait.list) { 124f42f7c28STrond Myklebust if (t->tk_owner == task->tk_owner) { 125f42f7c28STrond Myklebust list_add_tail(&task->u.tk_wait.links, 126f42f7c28STrond Myklebust &t->u.tk_wait.links); 127f42f7c28STrond Myklebust /* Cache the queue head in task->u.tk_wait.list */ 128f42f7c28STrond Myklebust task->u.tk_wait.list.next = q; 129f42f7c28STrond Myklebust task->u.tk_wait.list.prev = NULL; 130f42f7c28STrond Myklebust return; 131f42f7c28STrond Myklebust } 132f42f7c28STrond Myklebust } 133f42f7c28STrond Myklebust INIT_LIST_HEAD(&task->u.tk_wait.links); 134f42f7c28STrond Myklebust list_add_tail(&task->u.tk_wait.list, q); 135f42f7c28STrond Myklebust } 136f42f7c28STrond Myklebust 137f42f7c28STrond Myklebust /* 138f42f7c28STrond Myklebust * Remove request from a queue list 139f42f7c28STrond Myklebust */ 140f42f7c28STrond Myklebust static void 141f42f7c28STrond Myklebust __rpc_list_dequeue_task(struct rpc_task *task) 142f42f7c28STrond Myklebust { 143f42f7c28STrond Myklebust struct list_head *q; 144f42f7c28STrond Myklebust struct rpc_task *t; 145f42f7c28STrond Myklebust 146f42f7c28STrond Myklebust if (task->u.tk_wait.list.prev == NULL) { 147f42f7c28STrond Myklebust list_del(&task->u.tk_wait.links); 148f42f7c28STrond Myklebust return; 149f42f7c28STrond Myklebust } 150f42f7c28STrond Myklebust if (!list_empty(&task->u.tk_wait.links)) { 151f42f7c28STrond Myklebust t = list_first_entry(&task->u.tk_wait.links, 152f42f7c28STrond Myklebust struct rpc_task, 153f42f7c28STrond Myklebust u.tk_wait.links); 154f42f7c28STrond Myklebust /* Assume __rpc_list_enqueue_task() cached the queue head */ 155f42f7c28STrond Myklebust q = t->u.tk_wait.list.next; 156f42f7c28STrond Myklebust list_add_tail(&t->u.tk_wait.list, q); 157f42f7c28STrond Myklebust list_del(&task->u.tk_wait.links); 158f42f7c28STrond Myklebust } 159f42f7c28STrond Myklebust list_del(&task->u.tk_wait.list); 160c05eecf6STrond Myklebust } 161c05eecf6STrond Myklebust 1621da177e4SLinus Torvalds /* 1631da177e4SLinus Torvalds * Add new request to a priority queue. 1641da177e4SLinus Torvalds */ 1653b27bad7STrond Myklebust static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, 1663b27bad7STrond Myklebust struct rpc_task *task, 1673b27bad7STrond Myklebust unsigned char queue_priority) 1681da177e4SLinus Torvalds { 1693b27bad7STrond Myklebust if (unlikely(queue_priority > queue->maxpriority)) 170c05eecf6STrond Myklebust queue_priority = queue->maxpriority; 171f42f7c28STrond Myklebust __rpc_list_enqueue_task(&queue->tasks[queue_priority], task); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* 1751da177e4SLinus Torvalds * Add new request to wait queue. 1761da177e4SLinus Torvalds * 1771da177e4SLinus Torvalds * Swapper tasks always get inserted at the head of the queue. 1781da177e4SLinus Torvalds * This should avoid many nasty memory deadlocks and hopefully 1791da177e4SLinus Torvalds * improve overall performance. 1801da177e4SLinus Torvalds * Everyone else gets appended to the queue to ensure proper FIFO behavior. 1811da177e4SLinus Torvalds */ 1823b27bad7STrond Myklebust static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, 1833b27bad7STrond Myklebust struct rpc_task *task, 1843b27bad7STrond Myklebust unsigned char queue_priority) 1851da177e4SLinus Torvalds { 1862bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 1872bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 1882bd4eef8SWeston Andros Adamson return; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 1913b27bad7STrond Myklebust __rpc_add_wait_queue_priority(queue, task, queue_priority); 1921da177e4SLinus Torvalds else if (RPC_IS_SWAPPER(task)) 1931da177e4SLinus Torvalds list_add(&task->u.tk_wait.list, &queue->tasks[0]); 1941da177e4SLinus Torvalds else 1951da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]); 19696ef13b2STrond Myklebust task->tk_waitqueue = queue; 197e19b63daSChuck Lever queue->qlen++; 1981166fde6STrond Myklebust /* barrier matches the read in rpc_wake_up_task_queue_locked() */ 1991166fde6STrond Myklebust smp_wmb(); 2001da177e4SLinus Torvalds rpc_set_queued(task); 2011da177e4SLinus Torvalds 20246121cf7SChuck Lever dprintk("RPC: %5u added to queue %p \"%s\"\n", 2031da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * Remove request from a priority queue. 2081da177e4SLinus Torvalds */ 2091da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task) 2101da177e4SLinus Torvalds { 211f42f7c28STrond Myklebust __rpc_list_dequeue_task(task); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds /* 2151da177e4SLinus Torvalds * Remove request from queue. 2161da177e4SLinus Torvalds * Note: must be called with spin lock held. 2171da177e4SLinus Torvalds */ 21896ef13b2STrond Myklebust static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task) 2191da177e4SLinus Torvalds { 220eb276c0eSTrond Myklebust __rpc_disable_timer(queue, task); 2211da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 2221da177e4SLinus Torvalds __rpc_remove_wait_queue_priority(task); 223f42f7c28STrond Myklebust else 2241da177e4SLinus Torvalds list_del(&task->u.tk_wait.list); 225e19b63daSChuck Lever queue->qlen--; 22646121cf7SChuck Lever dprintk("RPC: %5u removed from queue %p \"%s\"\n", 2271da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2303ff7576dSTrond Myklebust static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds int i; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds spin_lock_init(&queue->lock); 2351da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(queue->tasks); i++) 2361da177e4SLinus Torvalds INIT_LIST_HEAD(&queue->tasks[i]); 2373ff7576dSTrond Myklebust queue->maxpriority = nr_queues - 1; 2381da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 23936df9aaeSTrond Myklebust queue->qlen = 0; 240ff861c4dSKees Cook timer_setup(&queue->timer_list.timer, __rpc_queue_timer_fn, 0); 24136df9aaeSTrond Myklebust INIT_LIST_HEAD(&queue->timer_list.list); 2422f09c242STrond Myklebust rpc_assign_waitqueue_name(queue, qname); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2461da177e4SLinus Torvalds { 2473ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY); 2481da177e4SLinus Torvalds } 249689cf5c1SAlexandros Batsakis EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2521da177e4SLinus Torvalds { 2533ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, 1); 2541da177e4SLinus Torvalds } 255e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_wait_queue); 2561da177e4SLinus Torvalds 257f6a1cc89STrond Myklebust void rpc_destroy_wait_queue(struct rpc_wait_queue *queue) 258f6a1cc89STrond Myklebust { 25936df9aaeSTrond Myklebust del_timer_sync(&queue->timer_list.timer); 260f6a1cc89STrond Myklebust } 261f6a1cc89STrond Myklebust EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue); 262f6a1cc89STrond Myklebust 263dfd01f02SPeter Zijlstra static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode) 26444c28873STrond Myklebust { 265416ad3c9SColin Cross freezable_schedule_unsafe(); 266dfd01f02SPeter Zijlstra if (signal_pending_state(mode, current)) 267dfd01f02SPeter Zijlstra return -ERESTARTSYS; 26844c28873STrond Myklebust return 0; 26944c28873STrond Myklebust } 27044c28873STrond Myklebust 2711306729bSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS) 272c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task) 273c44fe705STrond Myklebust { 274c44fe705STrond Myklebust static atomic_t rpc_pid; 275c44fe705STrond Myklebust 276c44fe705STrond Myklebust task->tk_pid = atomic_inc_return(&rpc_pid); 277c44fe705STrond Myklebust } 278c44fe705STrond Myklebust #else 279c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task) 280c44fe705STrond Myklebust { 281c44fe705STrond Myklebust } 282c44fe705STrond Myklebust #endif 283c44fe705STrond Myklebust 284e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task) 285e6b3c4dbSTrond Myklebust { 286c44fe705STrond Myklebust rpc_task_set_debuginfo(task); 28758f9612cSTrond Myklebust set_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 288e671edb9SChuck Lever trace_rpc_task_begin(task, NULL); 289e6b3c4dbSTrond Myklebust } 290e6b3c4dbSTrond Myklebust 29144c28873STrond Myklebust /* 29244c28873STrond Myklebust * Mark an RPC call as having completed by clearing the 'active' bit 293bf294b41STrond Myklebust * and then waking up all tasks that were sleeping. 29444c28873STrond Myklebust */ 295bf294b41STrond Myklebust static int rpc_complete_task(struct rpc_task *task) 29644c28873STrond Myklebust { 297bf294b41STrond Myklebust void *m = &task->tk_runstate; 298bf294b41STrond Myklebust wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE); 299bf294b41STrond Myklebust struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE); 300bf294b41STrond Myklebust unsigned long flags; 301bf294b41STrond Myklebust int ret; 302bf294b41STrond Myklebust 303e671edb9SChuck Lever trace_rpc_task_complete(task, NULL); 30482b0a4c3STrond Myklebust 305bf294b41STrond Myklebust spin_lock_irqsave(&wq->lock, flags); 306e6b3c4dbSTrond Myklebust clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 307bf294b41STrond Myklebust ret = atomic_dec_and_test(&task->tk_count); 308bf294b41STrond Myklebust if (waitqueue_active(wq)) 309ac5be6b4SAndrea Arcangeli __wake_up_locked_key(wq, TASK_NORMAL, &k); 310bf294b41STrond Myklebust spin_unlock_irqrestore(&wq->lock, flags); 311bf294b41STrond Myklebust return ret; 31244c28873STrond Myklebust } 31344c28873STrond Myklebust 31444c28873STrond Myklebust /* 31544c28873STrond Myklebust * Allow callers to wait for completion of an RPC call 316bf294b41STrond Myklebust * 317bf294b41STrond Myklebust * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit() 318bf294b41STrond Myklebust * to enforce taking of the wq->lock and hence avoid races with 319bf294b41STrond Myklebust * rpc_complete_task(). 32044c28873STrond Myklebust */ 321c1221321SNeilBrown int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action) 32244c28873STrond Myklebust { 32344c28873STrond Myklebust if (action == NULL) 324150030b7SMatthew Wilcox action = rpc_wait_bit_killable; 325bf294b41STrond Myklebust return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE, 326150030b7SMatthew Wilcox action, TASK_KILLABLE); 32744c28873STrond Myklebust } 328e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task); 32944c28873STrond Myklebust 3301da177e4SLinus Torvalds /* 3311da177e4SLinus Torvalds * Make an RPC task runnable. 3321da177e4SLinus Torvalds * 333506026c3SJeff Layton * Note: If the task is ASYNC, and is being made runnable after sitting on an 334506026c3SJeff Layton * rpc_wait_queue, this must be called with the queue spinlock held to protect 335506026c3SJeff Layton * the wait queue operation. 336a3c3cac5STrond Myklebust * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(), 337a3c3cac5STrond Myklebust * which is needed to ensure that __rpc_execute() doesn't loop (due to the 338a3c3cac5STrond Myklebust * lockless RPC_IS_QUEUED() test) before we've had a chance to test 339a3c3cac5STrond Myklebust * the RPC_TASK_RUNNING flag. 3401da177e4SLinus Torvalds */ 341f1dc237cSTrond Myklebust static void rpc_make_runnable(struct workqueue_struct *wq, 342f1dc237cSTrond Myklebust struct rpc_task *task) 3431da177e4SLinus Torvalds { 344a3c3cac5STrond Myklebust bool need_wakeup = !rpc_test_and_set_running(task); 345a3c3cac5STrond Myklebust 3461da177e4SLinus Torvalds rpc_clear_queued(task); 347a3c3cac5STrond Myklebust if (!need_wakeup) 3481da177e4SLinus Torvalds return; 3491da177e4SLinus Torvalds if (RPC_IS_ASYNC(task)) { 35065f27f38SDavid Howells INIT_WORK(&task->u.tk_work, rpc_async_schedule); 351f1dc237cSTrond Myklebust queue_work(wq, &task->u.tk_work); 3521da177e4SLinus Torvalds } else 35396651ab3STrond Myklebust wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED); 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds /* 3571da177e4SLinus Torvalds * Prepare for sleeping on a wait queue. 3581da177e4SLinus Torvalds * By always appending tasks to the list we ensure FIFO behavior. 3591da177e4SLinus Torvalds * NB: An RPC task will only receive interrupt-driven events as long 3601da177e4SLinus Torvalds * as it's on a wait queue. 3611da177e4SLinus Torvalds */ 3623b27bad7STrond Myklebust static void __rpc_sleep_on_priority(struct rpc_wait_queue *q, 3633b27bad7STrond Myklebust struct rpc_task *task, 3643b27bad7STrond Myklebust rpc_action action, 3653b27bad7STrond Myklebust unsigned char queue_priority) 3661da177e4SLinus Torvalds { 36746121cf7SChuck Lever dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n", 36846121cf7SChuck Lever task->tk_pid, rpc_qname(q), jiffies); 3691da177e4SLinus Torvalds 370e671edb9SChuck Lever trace_rpc_task_sleep(task, q); 37182b0a4c3STrond Myklebust 3723b27bad7STrond Myklebust __rpc_add_wait_queue(q, task, queue_priority); 3731da177e4SLinus Torvalds 374f50ad428SWeston Andros Adamson WARN_ON_ONCE(task->tk_callback != NULL); 3751da177e4SLinus Torvalds task->tk_callback = action; 376eb276c0eSTrond Myklebust __rpc_add_timer(q, task); 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, 3805d00837bSTrond Myklebust rpc_action action) 3811da177e4SLinus Torvalds { 38258f9612cSTrond Myklebust /* We shouldn't ever put an inactive task to sleep */ 383e454a7a8SWeston Andros Adamson WARN_ON_ONCE(!RPC_IS_ACTIVATED(task)); 384e454a7a8SWeston Andros Adamson if (!RPC_IS_ACTIVATED(task)) { 385e454a7a8SWeston Andros Adamson task->tk_status = -EIO; 386e454a7a8SWeston Andros Adamson rpc_put_task_async(task); 387e454a7a8SWeston Andros Adamson return; 388e454a7a8SWeston Andros Adamson } 389e6b3c4dbSTrond Myklebust 3901da177e4SLinus Torvalds /* 3911da177e4SLinus Torvalds * Protect the queue operations. 3921da177e4SLinus Torvalds */ 3931da177e4SLinus Torvalds spin_lock_bh(&q->lock); 3943b27bad7STrond Myklebust __rpc_sleep_on_priority(q, task, action, task->tk_priority); 3951da177e4SLinus Torvalds spin_unlock_bh(&q->lock); 3961da177e4SLinus Torvalds } 397e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on); 3981da177e4SLinus Torvalds 3993b27bad7STrond Myklebust void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task, 4003b27bad7STrond Myklebust rpc_action action, int priority) 4013b27bad7STrond Myklebust { 4023b27bad7STrond Myklebust /* We shouldn't ever put an inactive task to sleep */ 403e454a7a8SWeston Andros Adamson WARN_ON_ONCE(!RPC_IS_ACTIVATED(task)); 404e454a7a8SWeston Andros Adamson if (!RPC_IS_ACTIVATED(task)) { 405e454a7a8SWeston Andros Adamson task->tk_status = -EIO; 406e454a7a8SWeston Andros Adamson rpc_put_task_async(task); 407e454a7a8SWeston Andros Adamson return; 408e454a7a8SWeston Andros Adamson } 4093b27bad7STrond Myklebust 4103b27bad7STrond Myklebust /* 4113b27bad7STrond Myklebust * Protect the queue operations. 4123b27bad7STrond Myklebust */ 4133b27bad7STrond Myklebust spin_lock_bh(&q->lock); 4143b27bad7STrond Myklebust __rpc_sleep_on_priority(q, task, action, priority - RPC_PRIORITY_LOW); 4153b27bad7STrond Myklebust spin_unlock_bh(&q->lock); 4163b27bad7STrond Myklebust } 4171e1093c7STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority); 4183b27bad7STrond Myklebust 4191da177e4SLinus Torvalds /** 420f1dc237cSTrond Myklebust * __rpc_do_wake_up_task_on_wq - wake up a single rpc_task 421f1dc237cSTrond Myklebust * @wq: workqueue on which to run task 42296ef13b2STrond Myklebust * @queue: wait queue 4231da177e4SLinus Torvalds * @task: task to be woken up 4241da177e4SLinus Torvalds * 4251da177e4SLinus Torvalds * Caller must hold queue->lock, and have cleared the task queued flag. 4261da177e4SLinus Torvalds */ 427f1dc237cSTrond Myklebust static void __rpc_do_wake_up_task_on_wq(struct workqueue_struct *wq, 428f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 429f1dc237cSTrond Myklebust struct rpc_task *task) 4301da177e4SLinus Torvalds { 43146121cf7SChuck Lever dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n", 43246121cf7SChuck Lever task->tk_pid, jiffies); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* Has the task been executed yet? If not, we cannot wake it up! */ 4351da177e4SLinus Torvalds if (!RPC_IS_ACTIVATED(task)) { 4361da177e4SLinus Torvalds printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task); 4371da177e4SLinus Torvalds return; 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 440e671edb9SChuck Lever trace_rpc_task_wakeup(task, queue); 44182b0a4c3STrond Myklebust 44296ef13b2STrond Myklebust __rpc_remove_wait_queue(queue, task); 4431da177e4SLinus Torvalds 444f1dc237cSTrond Myklebust rpc_make_runnable(wq, task); 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds dprintk("RPC: __rpc_wake_up_task done\n"); 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds /* 45096ef13b2STrond Myklebust * Wake up a queued task while the queue lock is being held 4511da177e4SLinus Torvalds */ 452359c48c0STrond Myklebust static struct rpc_task * 453359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(struct workqueue_struct *wq, 454359c48c0STrond Myklebust struct rpc_wait_queue *queue, struct rpc_task *task, 455359c48c0STrond Myklebust bool (*action)(struct rpc_task *, void *), void *data) 4561da177e4SLinus Torvalds { 4571166fde6STrond Myklebust if (RPC_IS_QUEUED(task)) { 4581166fde6STrond Myklebust smp_rmb(); 459359c48c0STrond Myklebust if (task->tk_waitqueue == queue) { 460359c48c0STrond Myklebust if (action == NULL || action(task, data)) { 461f1dc237cSTrond Myklebust __rpc_do_wake_up_task_on_wq(wq, queue, task); 462359c48c0STrond Myklebust return task; 4631da177e4SLinus Torvalds } 4641166fde6STrond Myklebust } 465359c48c0STrond Myklebust } 466359c48c0STrond Myklebust return NULL; 467359c48c0STrond Myklebust } 468359c48c0STrond Myklebust 469359c48c0STrond Myklebust static void 470359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_locked(struct workqueue_struct *wq, 471359c48c0STrond Myklebust struct rpc_wait_queue *queue, struct rpc_task *task) 472359c48c0STrond Myklebust { 473359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(wq, queue, task, NULL, NULL); 474359c48c0STrond Myklebust } 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* 477f1dc237cSTrond Myklebust * Wake up a queued task while the queue lock is being held 478f1dc237cSTrond Myklebust */ 479f1dc237cSTrond Myklebust static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task) 480f1dc237cSTrond Myklebust { 481f1dc237cSTrond Myklebust rpc_wake_up_task_on_wq_queue_locked(rpciod_workqueue, queue, task); 482f1dc237cSTrond Myklebust } 483f1dc237cSTrond Myklebust 484f1dc237cSTrond Myklebust /* 48596ef13b2STrond Myklebust * Wake up a task on a specific queue 48696ef13b2STrond Myklebust */ 4872275cde4STrond Myklebust void rpc_wake_up_queued_task_on_wq(struct workqueue_struct *wq, 4882275cde4STrond Myklebust struct rpc_wait_queue *queue, 4892275cde4STrond Myklebust struct rpc_task *task) 4902275cde4STrond Myklebust { 4915ce97039STrond Myklebust if (!RPC_IS_QUEUED(task)) 4925ce97039STrond Myklebust return; 4932275cde4STrond Myklebust spin_lock_bh(&queue->lock); 4942275cde4STrond Myklebust rpc_wake_up_task_on_wq_queue_locked(wq, queue, task); 4952275cde4STrond Myklebust spin_unlock_bh(&queue->lock); 4962275cde4STrond Myklebust } 4972275cde4STrond Myklebust 4982275cde4STrond Myklebust /* 4992275cde4STrond Myklebust * Wake up a task on a specific queue 5002275cde4STrond Myklebust */ 50196ef13b2STrond Myklebust void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task) 50296ef13b2STrond Myklebust { 5035ce97039STrond Myklebust if (!RPC_IS_QUEUED(task)) 5045ce97039STrond Myklebust return; 5055e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 50696ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 5075e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 50896ef13b2STrond Myklebust } 50996ef13b2STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task); 51096ef13b2STrond Myklebust 511359c48c0STrond Myklebust static bool rpc_task_action_set_status(struct rpc_task *task, void *status) 512359c48c0STrond Myklebust { 513359c48c0STrond Myklebust task->tk_status = *(int *)status; 514359c48c0STrond Myklebust return true; 515359c48c0STrond Myklebust } 516359c48c0STrond Myklebust 517359c48c0STrond Myklebust static void 518359c48c0STrond Myklebust rpc_wake_up_task_queue_set_status_locked(struct rpc_wait_queue *queue, 519359c48c0STrond Myklebust struct rpc_task *task, int status) 520359c48c0STrond Myklebust { 521359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue, 522359c48c0STrond Myklebust task, rpc_task_action_set_status, &status); 523359c48c0STrond Myklebust } 524359c48c0STrond Myklebust 525359c48c0STrond Myklebust /** 526359c48c0STrond Myklebust * rpc_wake_up_queued_task_set_status - wake up a task and set task->tk_status 527359c48c0STrond Myklebust * @queue: pointer to rpc_wait_queue 528359c48c0STrond Myklebust * @task: pointer to rpc_task 529359c48c0STrond Myklebust * @status: integer error value 530359c48c0STrond Myklebust * 531359c48c0STrond Myklebust * If @task is queued on @queue, then it is woken up, and @task->tk_status is 532359c48c0STrond Myklebust * set to the value of @status. 533359c48c0STrond Myklebust */ 534359c48c0STrond Myklebust void 535359c48c0STrond Myklebust rpc_wake_up_queued_task_set_status(struct rpc_wait_queue *queue, 536359c48c0STrond Myklebust struct rpc_task *task, int status) 537359c48c0STrond Myklebust { 538359c48c0STrond Myklebust if (!RPC_IS_QUEUED(task)) 539359c48c0STrond Myklebust return; 540359c48c0STrond Myklebust spin_lock_bh(&queue->lock); 541359c48c0STrond Myklebust rpc_wake_up_task_queue_set_status_locked(queue, task, status); 542359c48c0STrond Myklebust spin_unlock_bh(&queue->lock); 543359c48c0STrond Myklebust } 544359c48c0STrond Myklebust 54596ef13b2STrond Myklebust /* 5461da177e4SLinus Torvalds * Wake up the next task on a priority queue. 5471da177e4SLinus Torvalds */ 548961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue) 5491da177e4SLinus Torvalds { 5501da177e4SLinus Torvalds struct list_head *q; 5511da177e4SLinus Torvalds struct rpc_task *task; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds /* 5543ff7576dSTrond Myklebust * Service a batch of tasks from a single owner. 5551da177e4SLinus Torvalds */ 5561da177e4SLinus Torvalds q = &queue->tasks[queue->priority]; 557f42f7c28STrond Myklebust if (!list_empty(q) && --queue->nr) { 558f42f7c28STrond Myklebust task = list_first_entry(q, struct rpc_task, u.tk_wait.list); 5591da177e4SLinus Torvalds goto out; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * Service the next queue. 5641da177e4SLinus Torvalds */ 5651da177e4SLinus Torvalds do { 5661da177e4SLinus Torvalds if (q == &queue->tasks[0]) 5671da177e4SLinus Torvalds q = &queue->tasks[queue->maxpriority]; 5681da177e4SLinus Torvalds else 5691da177e4SLinus Torvalds q = q - 1; 5701da177e4SLinus Torvalds if (!list_empty(q)) { 571f42f7c28STrond Myklebust task = list_first_entry(q, struct rpc_task, u.tk_wait.list); 5721da177e4SLinus Torvalds goto new_queue; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds } while (q != &queue->tasks[queue->priority]); 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 5771da177e4SLinus Torvalds return NULL; 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds new_queue: 5801da177e4SLinus Torvalds rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0])); 5811da177e4SLinus Torvalds out: 5821da177e4SLinus Torvalds return task; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 585961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue) 586961a828dSTrond Myklebust { 587961a828dSTrond Myklebust if (RPC_IS_PRIORITY(queue)) 588961a828dSTrond Myklebust return __rpc_find_next_queued_priority(queue); 589961a828dSTrond Myklebust if (!list_empty(&queue->tasks[0])) 590961a828dSTrond Myklebust return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list); 591961a828dSTrond Myklebust return NULL; 592961a828dSTrond Myklebust } 593961a828dSTrond Myklebust 594961a828dSTrond Myklebust /* 595961a828dSTrond Myklebust * Wake up the first task on the wait queue. 596961a828dSTrond Myklebust */ 597f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq, 598f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 599961a828dSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 600961a828dSTrond Myklebust { 601961a828dSTrond Myklebust struct rpc_task *task = NULL; 602961a828dSTrond Myklebust 603961a828dSTrond Myklebust dprintk("RPC: wake_up_first(%p \"%s\")\n", 604961a828dSTrond Myklebust queue, rpc_qname(queue)); 605961a828dSTrond Myklebust spin_lock_bh(&queue->lock); 606961a828dSTrond Myklebust task = __rpc_find_next_queued(queue); 607359c48c0STrond Myklebust if (task != NULL) 608359c48c0STrond Myklebust task = rpc_wake_up_task_on_wq_queue_action_locked(wq, queue, 609359c48c0STrond Myklebust task, func, data); 610961a828dSTrond Myklebust spin_unlock_bh(&queue->lock); 611961a828dSTrond Myklebust 612961a828dSTrond Myklebust return task; 613961a828dSTrond Myklebust } 614f1dc237cSTrond Myklebust 615f1dc237cSTrond Myklebust /* 616f1dc237cSTrond Myklebust * Wake up the first task on the wait queue. 617f1dc237cSTrond Myklebust */ 618f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue, 619f1dc237cSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 620f1dc237cSTrond Myklebust { 621f1dc237cSTrond Myklebust return rpc_wake_up_first_on_wq(rpciod_workqueue, queue, func, data); 622f1dc237cSTrond Myklebust } 623961a828dSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_first); 624961a828dSTrond Myklebust 625961a828dSTrond Myklebust static bool rpc_wake_up_next_func(struct rpc_task *task, void *data) 626961a828dSTrond Myklebust { 627961a828dSTrond Myklebust return true; 628961a828dSTrond Myklebust } 629961a828dSTrond Myklebust 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * Wake up the next task on the wait queue. 6321da177e4SLinus Torvalds */ 6331da177e4SLinus Torvalds struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue) 6341da177e4SLinus Torvalds { 635961a828dSTrond Myklebust return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL); 6361da177e4SLinus Torvalds } 637e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds /** 6401da177e4SLinus Torvalds * rpc_wake_up - wake up all rpc_tasks 6411da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 6421da177e4SLinus Torvalds * 6431da177e4SLinus Torvalds * Grabs queue->lock 6441da177e4SLinus Torvalds */ 6451da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue) 6461da177e4SLinus Torvalds { 6471da177e4SLinus Torvalds struct list_head *head; 648e6d83d55STrond Myklebust 6495e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 6501da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 6511da177e4SLinus Torvalds for (;;) { 652540a0f75STrond Myklebust while (!list_empty(head)) { 653540a0f75STrond Myklebust struct rpc_task *task; 654540a0f75STrond Myklebust task = list_first_entry(head, 655540a0f75STrond Myklebust struct rpc_task, 656540a0f75STrond Myklebust u.tk_wait.list); 65796ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 658540a0f75STrond Myklebust } 6591da177e4SLinus Torvalds if (head == &queue->tasks[0]) 6601da177e4SLinus Torvalds break; 6611da177e4SLinus Torvalds head--; 6621da177e4SLinus Torvalds } 6635e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 6641da177e4SLinus Torvalds } 665e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /** 6681da177e4SLinus Torvalds * rpc_wake_up_status - wake up all rpc_tasks and set their status value. 6691da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 6701da177e4SLinus Torvalds * @status: status value to set 6711da177e4SLinus Torvalds * 6721da177e4SLinus Torvalds * Grabs queue->lock 6731da177e4SLinus Torvalds */ 6741da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status) 6751da177e4SLinus Torvalds { 6761da177e4SLinus Torvalds struct list_head *head; 6771da177e4SLinus Torvalds 6785e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 6791da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 6801da177e4SLinus Torvalds for (;;) { 681540a0f75STrond Myklebust while (!list_empty(head)) { 682540a0f75STrond Myklebust struct rpc_task *task; 683540a0f75STrond Myklebust task = list_first_entry(head, 684540a0f75STrond Myklebust struct rpc_task, 685540a0f75STrond Myklebust u.tk_wait.list); 6861da177e4SLinus Torvalds task->tk_status = status; 68796ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds if (head == &queue->tasks[0]) 6901da177e4SLinus Torvalds break; 6911da177e4SLinus Torvalds head--; 6921da177e4SLinus Torvalds } 6935e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 6941da177e4SLinus Torvalds } 695e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status); 6961da177e4SLinus Torvalds 697ff861c4dSKees Cook static void __rpc_queue_timer_fn(struct timer_list *t) 69836df9aaeSTrond Myklebust { 699ff861c4dSKees Cook struct rpc_wait_queue *queue = from_timer(queue, t, timer_list.timer); 70036df9aaeSTrond Myklebust struct rpc_task *task, *n; 70136df9aaeSTrond Myklebust unsigned long expires, now, timeo; 70236df9aaeSTrond Myklebust 70336df9aaeSTrond Myklebust spin_lock(&queue->lock); 70436df9aaeSTrond Myklebust expires = now = jiffies; 70536df9aaeSTrond Myklebust list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) { 70636df9aaeSTrond Myklebust timeo = task->u.tk_wait.expires; 70736df9aaeSTrond Myklebust if (time_after_eq(now, timeo)) { 70836df9aaeSTrond Myklebust dprintk("RPC: %5u timeout\n", task->tk_pid); 70936df9aaeSTrond Myklebust task->tk_status = -ETIMEDOUT; 71036df9aaeSTrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 71136df9aaeSTrond Myklebust continue; 71236df9aaeSTrond Myklebust } 71336df9aaeSTrond Myklebust if (expires == now || time_after(expires, timeo)) 71436df9aaeSTrond Myklebust expires = timeo; 71536df9aaeSTrond Myklebust } 71636df9aaeSTrond Myklebust if (!list_empty(&queue->timer_list.list)) 71736df9aaeSTrond Myklebust rpc_set_queue_timer(queue, expires); 71836df9aaeSTrond Myklebust spin_unlock(&queue->lock); 71936df9aaeSTrond Myklebust } 72036df9aaeSTrond Myklebust 7218014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task) 7228014793bSTrond Myklebust { 7236bd14416STrond Myklebust if (task->tk_status == -ETIMEDOUT) 7245d00837bSTrond Myklebust task->tk_status = 0; 7258014793bSTrond Myklebust } 7268014793bSTrond Myklebust 7271da177e4SLinus Torvalds /* 7281da177e4SLinus Torvalds * Run a task at a later time 7291da177e4SLinus Torvalds */ 7308014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds task->tk_timeout = delay; 7335d00837bSTrond Myklebust rpc_sleep_on(&delay_queue, task, __rpc_atrun); 7341da177e4SLinus Torvalds } 735e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay); 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds /* 7384ce70adaSTrond Myklebust * Helper to call task->tk_ops->rpc_call_prepare 7394ce70adaSTrond Myklebust */ 740aae2006eSAndy Adamson void rpc_prepare_task(struct rpc_task *task) 7414ce70adaSTrond Myklebust { 7424ce70adaSTrond Myklebust task->tk_ops->rpc_call_prepare(task, task->tk_calldata); 7434ce70adaSTrond Myklebust } 7444ce70adaSTrond Myklebust 7457fdcf13bSTrond Myklebust static void 7467fdcf13bSTrond Myklebust rpc_init_task_statistics(struct rpc_task *task) 7477fdcf13bSTrond Myklebust { 7487fdcf13bSTrond Myklebust /* Initialize retry counters */ 7497fdcf13bSTrond Myklebust task->tk_garb_retry = 2; 7507fdcf13bSTrond Myklebust task->tk_cred_retry = 2; 7517fdcf13bSTrond Myklebust task->tk_rebind_retry = 2; 7527fdcf13bSTrond Myklebust 7537fdcf13bSTrond Myklebust /* starting timestamp */ 7547fdcf13bSTrond Myklebust task->tk_start = ktime_get(); 7557fdcf13bSTrond Myklebust } 7567fdcf13bSTrond Myklebust 7577fdcf13bSTrond Myklebust static void 7587fdcf13bSTrond Myklebust rpc_reset_task_statistics(struct rpc_task *task) 7597fdcf13bSTrond Myklebust { 7607fdcf13bSTrond Myklebust task->tk_timeouts = 0; 7617fdcf13bSTrond Myklebust task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_KILLED|RPC_TASK_SENT); 7627fdcf13bSTrond Myklebust 7637fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 7647fdcf13bSTrond Myklebust } 7657fdcf13bSTrond Myklebust 7664ce70adaSTrond Myklebust /* 767963d8fe5STrond Myklebust * Helper that calls task->tk_ops->rpc_call_done if it exists 768d05fdb0cSTrond Myklebust */ 769abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task) 770d05fdb0cSTrond Myklebust { 771abbcf28fSTrond Myklebust task->tk_action = NULL; 772963d8fe5STrond Myklebust if (task->tk_ops->rpc_call_done != NULL) { 773963d8fe5STrond Myklebust task->tk_ops->rpc_call_done(task, task->tk_calldata); 774d05fdb0cSTrond Myklebust if (task->tk_action != NULL) { 775abbcf28fSTrond Myklebust WARN_ON(RPC_ASSASSINATED(task)); 776abbcf28fSTrond Myklebust /* Always release the RPC slot and buffer memory */ 777d05fdb0cSTrond Myklebust xprt_release(task); 7787fdcf13bSTrond Myklebust rpc_reset_task_statistics(task); 779d05fdb0cSTrond Myklebust } 780d05fdb0cSTrond Myklebust } 781d05fdb0cSTrond Myklebust } 782d9b6cd94STrond Myklebust 783d9b6cd94STrond Myklebust void rpc_exit(struct rpc_task *task, int status) 784d9b6cd94STrond Myklebust { 785d9b6cd94STrond Myklebust task->tk_status = status; 786d9b6cd94STrond Myklebust task->tk_action = rpc_exit_task; 787d9b6cd94STrond Myklebust if (RPC_IS_QUEUED(task)) 788d9b6cd94STrond Myklebust rpc_wake_up_queued_task(task->tk_waitqueue, task); 789d9b6cd94STrond Myklebust } 790d9b6cd94STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit); 791d05fdb0cSTrond Myklebust 792bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata) 793bbd5a1f9STrond Myklebust { 794a86dc496STrond Myklebust if (ops->rpc_release != NULL) 795bbd5a1f9STrond Myklebust ops->rpc_release(calldata); 796bbd5a1f9STrond Myklebust } 797bbd5a1f9STrond Myklebust 798d05fdb0cSTrond Myklebust /* 7991da177e4SLinus Torvalds * This is the RPC `scheduler' (or rather, the finite state machine). 8001da177e4SLinus Torvalds */ 8012efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task) 8021da177e4SLinus Torvalds { 803eb9b55abSTrond Myklebust struct rpc_wait_queue *queue; 804eb9b55abSTrond Myklebust int task_is_async = RPC_IS_ASYNC(task); 8051da177e4SLinus Torvalds int status = 0; 8061da177e4SLinus Torvalds 80746121cf7SChuck Lever dprintk("RPC: %5u __rpc_execute flags=0x%x\n", 8081da177e4SLinus Torvalds task->tk_pid, task->tk_flags); 8091da177e4SLinus Torvalds 8102bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 8112bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 8122bd4eef8SWeston Andros Adamson return; 8131da177e4SLinus Torvalds 814d05fdb0cSTrond Myklebust for (;;) { 815b55c5989STrond Myklebust void (*do_action)(struct rpc_task *); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds /* 81821ead9ffSChuck Lever * Perform the next FSM step or a pending callback. 81921ead9ffSChuck Lever * 820b55c5989STrond Myklebust * tk_action may be NULL if the task has been killed. 821b55c5989STrond Myklebust * In particular, note that rpc_killall_tasks may 822b55c5989STrond Myklebust * do this at any time, so beware when dereferencing. 8231da177e4SLinus Torvalds */ 824b55c5989STrond Myklebust do_action = task->tk_action; 82521ead9ffSChuck Lever if (task->tk_callback) { 82621ead9ffSChuck Lever do_action = task->tk_callback; 82721ead9ffSChuck Lever task->tk_callback = NULL; 8281da177e4SLinus Torvalds } 82921ead9ffSChuck Lever if (!do_action) 83021ead9ffSChuck Lever break; 831e671edb9SChuck Lever trace_rpc_task_run_action(task, do_action); 832b55c5989STrond Myklebust do_action(task); 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* 8351da177e4SLinus Torvalds * Lockless check for whether task is sleeping or not. 8361da177e4SLinus Torvalds */ 8371da177e4SLinus Torvalds if (!RPC_IS_QUEUED(task)) 8381da177e4SLinus Torvalds continue; 839eb9b55abSTrond Myklebust /* 840eb9b55abSTrond Myklebust * The queue->lock protects against races with 841eb9b55abSTrond Myklebust * rpc_make_runnable(). 842eb9b55abSTrond Myklebust * 843eb9b55abSTrond Myklebust * Note that once we clear RPC_TASK_RUNNING on an asynchronous 844eb9b55abSTrond Myklebust * rpc_task, rpc_make_runnable() can assign it to a 845eb9b55abSTrond Myklebust * different workqueue. We therefore cannot assume that the 846eb9b55abSTrond Myklebust * rpc_task pointer may still be dereferenced. 847eb9b55abSTrond Myklebust */ 848eb9b55abSTrond Myklebust queue = task->tk_waitqueue; 849eb9b55abSTrond Myklebust spin_lock_bh(&queue->lock); 850eb9b55abSTrond Myklebust if (!RPC_IS_QUEUED(task)) { 851eb9b55abSTrond Myklebust spin_unlock_bh(&queue->lock); 8521da177e4SLinus Torvalds continue; 8531da177e4SLinus Torvalds } 854eb9b55abSTrond Myklebust rpc_clear_running(task); 855eb9b55abSTrond Myklebust spin_unlock_bh(&queue->lock); 856eb9b55abSTrond Myklebust if (task_is_async) 857eb9b55abSTrond Myklebust return; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* sync task: sleep here */ 86046121cf7SChuck Lever dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid); 86196651ab3STrond Myklebust status = out_of_line_wait_on_bit(&task->tk_runstate, 862150030b7SMatthew Wilcox RPC_TASK_QUEUED, rpc_wait_bit_killable, 863150030b7SMatthew Wilcox TASK_KILLABLE); 86496651ab3STrond Myklebust if (status == -ERESTARTSYS) { 8651da177e4SLinus Torvalds /* 8661da177e4SLinus Torvalds * When a sync task receives a signal, it exits with 8671da177e4SLinus Torvalds * -ERESTARTSYS. In order to catch any callbacks that 8681da177e4SLinus Torvalds * clean up after sleeping on some queue, we don't 8691da177e4SLinus Torvalds * break the loop here, but go around once more. 8701da177e4SLinus Torvalds */ 87146121cf7SChuck Lever dprintk("RPC: %5u got signal\n", task->tk_pid); 8721da177e4SLinus Torvalds task->tk_flags |= RPC_TASK_KILLED; 8731da177e4SLinus Torvalds rpc_exit(task, -ERESTARTSYS); 8741da177e4SLinus Torvalds } 87546121cf7SChuck Lever dprintk("RPC: %5u sync task resuming\n", task->tk_pid); 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 87846121cf7SChuck Lever dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status, 87946121cf7SChuck Lever task->tk_status); 8801da177e4SLinus Torvalds /* Release all resources associated with the task */ 8811da177e4SLinus Torvalds rpc_release_task(task); 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds /* 8851da177e4SLinus Torvalds * User-visible entry point to the scheduler. 8861da177e4SLinus Torvalds * 8871da177e4SLinus Torvalds * This may be called recursively if e.g. an async NFS task updates 8881da177e4SLinus Torvalds * the attributes and finds that dirty pages must be flushed. 8891da177e4SLinus Torvalds * NOTE: Upon exit of this function the task is guaranteed to be 8901da177e4SLinus Torvalds * released. In particular note that tk_release() will have 8911da177e4SLinus Torvalds * been called, so your task memory may have been freed. 8921da177e4SLinus Torvalds */ 8932efef837STrond Myklebust void rpc_execute(struct rpc_task *task) 8941da177e4SLinus Torvalds { 895a76580fbSTrond Myklebust bool is_async = RPC_IS_ASYNC(task); 896a76580fbSTrond Myklebust 89744c28873STrond Myklebust rpc_set_active(task); 898f1dc237cSTrond Myklebust rpc_make_runnable(rpciod_workqueue, task); 899a76580fbSTrond Myklebust if (!is_async) 9002efef837STrond Myklebust __rpc_execute(task); 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 90365f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work) 9041da177e4SLinus Torvalds { 90565f27f38SDavid Howells __rpc_execute(container_of(work, struct rpc_task, u.tk_work)); 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 90802107148SChuck Lever /** 9095fe6eaa1SChuck Lever * rpc_malloc - allocate RPC buffer resources 9105fe6eaa1SChuck Lever * @task: RPC task 9115fe6eaa1SChuck Lever * 9125fe6eaa1SChuck Lever * A single memory region is allocated, which is split between the 9135fe6eaa1SChuck Lever * RPC call and RPC reply that this task is being used for. When 9145fe6eaa1SChuck Lever * this RPC is retired, the memory is released by calling rpc_free. 9151da177e4SLinus Torvalds * 916c5a4dd8bSChuck Lever * To prevent rpciod from hanging, this allocator never sleeps, 9175fe6eaa1SChuck Lever * returning -ENOMEM and suppressing warning if the request cannot 9185fe6eaa1SChuck Lever * be serviced immediately. The caller can arrange to sleep in a 9195fe6eaa1SChuck Lever * way that is safe for rpciod. 920c5a4dd8bSChuck Lever * 921c5a4dd8bSChuck Lever * Most requests are 'small' (under 2KiB) and can be serviced from a 922c5a4dd8bSChuck Lever * mempool, ensuring that NFS reads and writes can always proceed, 923c5a4dd8bSChuck Lever * and that there is good locality of reference for these buffers. 924c5a4dd8bSChuck Lever * 9251da177e4SLinus Torvalds * In order to avoid memory starvation triggering more writebacks of 926c5a4dd8bSChuck Lever * NFS requests, we avoid using GFP_KERNEL. 9271da177e4SLinus Torvalds */ 9285fe6eaa1SChuck Lever int rpc_malloc(struct rpc_task *task) 9291da177e4SLinus Torvalds { 9305fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 9315fe6eaa1SChuck Lever size_t size = rqst->rq_callsize + rqst->rq_rcvsize; 932aa3d1faeSChuck Lever struct rpc_buffer *buf; 933c4a7ca77STrond Myklebust gfp_t gfp = GFP_NOIO | __GFP_NOWARN; 934a564b8f0SMel Gorman 935a564b8f0SMel Gorman if (RPC_IS_SWAPPER(task)) 936c4a7ca77STrond Myklebust gfp = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN; 9371da177e4SLinus Torvalds 938aa3d1faeSChuck Lever size += sizeof(struct rpc_buffer); 939c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 940c5a4dd8bSChuck Lever buf = mempool_alloc(rpc_buffer_mempool, gfp); 9411da177e4SLinus Torvalds else 942c5a4dd8bSChuck Lever buf = kmalloc(size, gfp); 943ddce40dfSPeter Zijlstra 944ddce40dfSPeter Zijlstra if (!buf) 9455fe6eaa1SChuck Lever return -ENOMEM; 946ddce40dfSPeter Zijlstra 947aa3d1faeSChuck Lever buf->len = size; 948215d0678SGeert Uytterhoeven dprintk("RPC: %5u allocated buffer of size %zu at %p\n", 949c5a4dd8bSChuck Lever task->tk_pid, size, buf); 9505fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 95168778945SChuck Lever rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 9525fe6eaa1SChuck Lever return 0; 9531da177e4SLinus Torvalds } 95412444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc); 9551da177e4SLinus Torvalds 95602107148SChuck Lever /** 9573435c74aSChuck Lever * rpc_free - free RPC buffer resources allocated via rpc_malloc 9583435c74aSChuck Lever * @task: RPC task 95902107148SChuck Lever * 96002107148SChuck Lever */ 9613435c74aSChuck Lever void rpc_free(struct rpc_task *task) 9621da177e4SLinus Torvalds { 9633435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 964aa3d1faeSChuck Lever size_t size; 965aa3d1faeSChuck Lever struct rpc_buffer *buf; 96602107148SChuck Lever 967aa3d1faeSChuck Lever buf = container_of(buffer, struct rpc_buffer, data); 968aa3d1faeSChuck Lever size = buf->len; 969c5a4dd8bSChuck Lever 970215d0678SGeert Uytterhoeven dprintk("RPC: freeing buffer of size %zu at %p\n", 971c5a4dd8bSChuck Lever size, buf); 972aa3d1faeSChuck Lever 973c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 974c5a4dd8bSChuck Lever mempool_free(buf, rpc_buffer_mempool); 9751da177e4SLinus Torvalds else 976c5a4dd8bSChuck Lever kfree(buf); 9771da177e4SLinus Torvalds } 97812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* 9811da177e4SLinus Torvalds * Creation and deletion of RPC task structures 9821da177e4SLinus Torvalds */ 98347fe0648STrond Myklebust static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data) 9841da177e4SLinus Torvalds { 9851da177e4SLinus Torvalds memset(task, 0, sizeof(*task)); 98644c28873STrond Myklebust atomic_set(&task->tk_count, 1); 98784115e1cSTrond Myklebust task->tk_flags = task_setup_data->flags; 98884115e1cSTrond Myklebust task->tk_ops = task_setup_data->callback_ops; 98984115e1cSTrond Myklebust task->tk_calldata = task_setup_data->callback_data; 9906529eba0STrond Myklebust INIT_LIST_HEAD(&task->tk_task); 9911da177e4SLinus Torvalds 9923ff7576dSTrond Myklebust task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW; 9933ff7576dSTrond Myklebust task->tk_owner = current->tgid; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* Initialize workqueue for async tasks */ 99632bfb5c0STrond Myklebust task->tk_workqueue = task_setup_data->workqueue; 9971da177e4SLinus Torvalds 9989d61498dSTrond Myklebust task->tk_xprt = xprt_get(task_setup_data->rpc_xprt); 9999d61498dSTrond Myklebust 10001de7eea9SNeilBrown task->tk_op_cred = get_rpccred(task_setup_data->rpc_op_cred); 10011de7eea9SNeilBrown 100284115e1cSTrond Myklebust if (task->tk_ops->rpc_call_prepare != NULL) 100384115e1cSTrond Myklebust task->tk_action = rpc_prepare_task; 1004963d8fe5STrond Myklebust 10057fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 1006ef759a2eSChuck Lever 100746121cf7SChuck Lever dprintk("RPC: new task initialized, procpid %u\n", 1008ba25f9dcSPavel Emelyanov task_pid_nr(current)); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds static struct rpc_task * 10121da177e4SLinus Torvalds rpc_alloc_task(void) 10131da177e4SLinus Torvalds { 1014a564b8f0SMel Gorman return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOIO); 10151da177e4SLinus Torvalds } 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds /* 101890c5755fSTrond Myklebust * Create a new task for the specified client. 10191da177e4SLinus Torvalds */ 102084115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data) 10211da177e4SLinus Torvalds { 1022e8f5d77cSTrond Myklebust struct rpc_task *task = setup_data->task; 1023e8f5d77cSTrond Myklebust unsigned short flags = 0; 10241da177e4SLinus Torvalds 1025e8f5d77cSTrond Myklebust if (task == NULL) { 10261da177e4SLinus Torvalds task = rpc_alloc_task(); 1027e8f5d77cSTrond Myklebust flags = RPC_TASK_DYNAMIC; 1028e8f5d77cSTrond Myklebust } 10291da177e4SLinus Torvalds 103084115e1cSTrond Myklebust rpc_init_task(task, setup_data); 1031e8f5d77cSTrond Myklebust task->tk_flags |= flags; 103246121cf7SChuck Lever dprintk("RPC: allocated task %p\n", task); 10331da177e4SLinus Torvalds return task; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 1036c6567ed1STrond Myklebust /* 1037c6567ed1STrond Myklebust * rpc_free_task - release rpc task and perform cleanups 1038c6567ed1STrond Myklebust * 1039c6567ed1STrond Myklebust * Note that we free up the rpc_task _after_ rpc_release_calldata() 1040c6567ed1STrond Myklebust * in order to work around a workqueue dependency issue. 1041c6567ed1STrond Myklebust * 1042c6567ed1STrond Myklebust * Tejun Heo states: 1043c6567ed1STrond Myklebust * "Workqueue currently considers two work items to be the same if they're 1044c6567ed1STrond Myklebust * on the same address and won't execute them concurrently - ie. it 1045c6567ed1STrond Myklebust * makes a work item which is queued again while being executed wait 1046c6567ed1STrond Myklebust * for the previous execution to complete. 1047c6567ed1STrond Myklebust * 1048c6567ed1STrond Myklebust * If a work function frees the work item, and then waits for an event 1049c6567ed1STrond Myklebust * which should be performed by another work item and *that* work item 1050c6567ed1STrond Myklebust * recycles the freed work item, it can create a false dependency loop. 1051c6567ed1STrond Myklebust * There really is no reliable way to detect this short of verifying 1052c6567ed1STrond Myklebust * every memory free." 1053c6567ed1STrond Myklebust * 1054c6567ed1STrond Myklebust */ 105532bfb5c0STrond Myklebust static void rpc_free_task(struct rpc_task *task) 10561da177e4SLinus Torvalds { 1057c6567ed1STrond Myklebust unsigned short tk_flags = task->tk_flags; 10581da177e4SLinus Torvalds 10591de7eea9SNeilBrown put_rpccred(task->tk_op_cred); 1060c6567ed1STrond Myklebust rpc_release_calldata(task->tk_ops, task->tk_calldata); 1061c6567ed1STrond Myklebust 1062c6567ed1STrond Myklebust if (tk_flags & RPC_TASK_DYNAMIC) { 10635e4424afSTrond Myklebust dprintk("RPC: %5u freeing task\n", task->tk_pid); 10645e4424afSTrond Myklebust mempool_free(task, rpc_task_mempool); 10655e4424afSTrond Myklebust } 106632bfb5c0STrond Myklebust } 106732bfb5c0STrond Myklebust 106832bfb5c0STrond Myklebust static void rpc_async_release(struct work_struct *work) 106932bfb5c0STrond Myklebust { 107032bfb5c0STrond Myklebust rpc_free_task(container_of(work, struct rpc_task, u.tk_work)); 107132bfb5c0STrond Myklebust } 107232bfb5c0STrond Myklebust 1073bf294b41STrond Myklebust static void rpc_release_resources_task(struct rpc_task *task) 107432bfb5c0STrond Myklebust { 1075e6b3c4dbSTrond Myklebust xprt_release(task); 1076a271c5a0SOGAWA Hirofumi if (task->tk_msg.rpc_cred) { 1077*a52458b4SNeilBrown put_cred(task->tk_msg.rpc_cred); 1078a271c5a0SOGAWA Hirofumi task->tk_msg.rpc_cred = NULL; 1079a271c5a0SOGAWA Hirofumi } 108058f9612cSTrond Myklebust rpc_task_release_client(task); 1081bf294b41STrond Myklebust } 1082bf294b41STrond Myklebust 1083bf294b41STrond Myklebust static void rpc_final_put_task(struct rpc_task *task, 1084bf294b41STrond Myklebust struct workqueue_struct *q) 1085bf294b41STrond Myklebust { 1086bf294b41STrond Myklebust if (q != NULL) { 108732bfb5c0STrond Myklebust INIT_WORK(&task->u.tk_work, rpc_async_release); 1088bf294b41STrond Myklebust queue_work(q, &task->u.tk_work); 108932bfb5c0STrond Myklebust } else 109032bfb5c0STrond Myklebust rpc_free_task(task); 1091e6b3c4dbSTrond Myklebust } 1092bf294b41STrond Myklebust 1093bf294b41STrond Myklebust static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q) 1094bf294b41STrond Myklebust { 1095bf294b41STrond Myklebust if (atomic_dec_and_test(&task->tk_count)) { 1096bf294b41STrond Myklebust rpc_release_resources_task(task); 1097bf294b41STrond Myklebust rpc_final_put_task(task, q); 1098bf294b41STrond Myklebust } 1099bf294b41STrond Myklebust } 1100bf294b41STrond Myklebust 1101bf294b41STrond Myklebust void rpc_put_task(struct rpc_task *task) 1102bf294b41STrond Myklebust { 1103bf294b41STrond Myklebust rpc_do_put_task(task, NULL); 1104bf294b41STrond Myklebust } 1105e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task); 1106e6b3c4dbSTrond Myklebust 1107bf294b41STrond Myklebust void rpc_put_task_async(struct rpc_task *task) 1108bf294b41STrond Myklebust { 1109bf294b41STrond Myklebust rpc_do_put_task(task, task->tk_workqueue); 1110bf294b41STrond Myklebust } 1111bf294b41STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task_async); 1112bf294b41STrond Myklebust 1113bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task) 1114e6b3c4dbSTrond Myklebust { 111546121cf7SChuck Lever dprintk("RPC: %5u release task\n", task->tk_pid); 11161da177e4SLinus Torvalds 11170a0c2a57SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 11181da177e4SLinus Torvalds 1119bf294b41STrond Myklebust rpc_release_resources_task(task); 1120e6b3c4dbSTrond Myklebust 1121bf294b41STrond Myklebust /* 1122bf294b41STrond Myklebust * Note: at this point we have been removed from rpc_clnt->cl_tasks, 1123bf294b41STrond Myklebust * so it should be safe to use task->tk_count as a test for whether 1124bf294b41STrond Myklebust * or not any other processes still hold references to our rpc_task. 1125bf294b41STrond Myklebust */ 1126bf294b41STrond Myklebust if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) { 1127bf294b41STrond Myklebust /* Wake up anyone who may be waiting for task completion */ 1128bf294b41STrond Myklebust if (!rpc_complete_task(task)) 1129bf294b41STrond Myklebust return; 1130bf294b41STrond Myklebust } else { 1131bf294b41STrond Myklebust if (!atomic_dec_and_test(&task->tk_count)) 1132bf294b41STrond Myklebust return; 1133bf294b41STrond Myklebust } 1134bf294b41STrond Myklebust rpc_final_put_task(task, task->tk_workqueue); 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 1137b247bbf1STrond Myklebust int rpciod_up(void) 1138b247bbf1STrond Myklebust { 1139b247bbf1STrond Myklebust return try_module_get(THIS_MODULE) ? 0 : -EINVAL; 1140b247bbf1STrond Myklebust } 1141b247bbf1STrond Myklebust 1142b247bbf1STrond Myklebust void rpciod_down(void) 1143b247bbf1STrond Myklebust { 1144b247bbf1STrond Myklebust module_put(THIS_MODULE); 1145b247bbf1STrond Myklebust } 1146b247bbf1STrond Myklebust 11471da177e4SLinus Torvalds /* 1148b247bbf1STrond Myklebust * Start up the rpciod workqueue. 11491da177e4SLinus Torvalds */ 1150b247bbf1STrond Myklebust static int rpciod_start(void) 11511da177e4SLinus Torvalds { 11521da177e4SLinus Torvalds struct workqueue_struct *wq; 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds /* 11551da177e4SLinus Torvalds * Create the rpciod thread and wait for it to start. 11561da177e4SLinus Torvalds */ 1157ab418d70STrond Myklebust dprintk("RPC: creating workqueue rpciod\n"); 1158f515f86bSOlga Kornievskaia wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); 115940a5f1b1STrond Myklebust if (!wq) 116040a5f1b1STrond Myklebust goto out_failed; 11611da177e4SLinus Torvalds rpciod_workqueue = wq; 116240a5f1b1STrond Myklebust /* Note: highpri because network receive is latency sensitive */ 116390ea9f1bSTrond Myklebust wq = alloc_workqueue("xprtiod", WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_HIGHPRI, 0); 116440a5f1b1STrond Myklebust if (!wq) 116540a5f1b1STrond Myklebust goto free_rpciod; 116640a5f1b1STrond Myklebust xprtiod_workqueue = wq; 116740a5f1b1STrond Myklebust return 1; 116840a5f1b1STrond Myklebust free_rpciod: 116940a5f1b1STrond Myklebust wq = rpciod_workqueue; 117040a5f1b1STrond Myklebust rpciod_workqueue = NULL; 117140a5f1b1STrond Myklebust destroy_workqueue(wq); 117240a5f1b1STrond Myklebust out_failed: 117340a5f1b1STrond Myklebust return 0; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds 1176b247bbf1STrond Myklebust static void rpciod_stop(void) 11771da177e4SLinus Torvalds { 1178b247bbf1STrond Myklebust struct workqueue_struct *wq = NULL; 1179ab418d70STrond Myklebust 1180b247bbf1STrond Myklebust if (rpciod_workqueue == NULL) 1181b247bbf1STrond Myklebust return; 1182ab418d70STrond Myklebust dprintk("RPC: destroying workqueue rpciod\n"); 11831da177e4SLinus Torvalds 1184b247bbf1STrond Myklebust wq = rpciod_workqueue; 11851da177e4SLinus Torvalds rpciod_workqueue = NULL; 1186b247bbf1STrond Myklebust destroy_workqueue(wq); 118740a5f1b1STrond Myklebust wq = xprtiod_workqueue; 118840a5f1b1STrond Myklebust xprtiod_workqueue = NULL; 118940a5f1b1STrond Myklebust destroy_workqueue(wq); 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 11921da177e4SLinus Torvalds void 11931da177e4SLinus Torvalds rpc_destroy_mempool(void) 11941da177e4SLinus Torvalds { 1195b247bbf1STrond Myklebust rpciod_stop(); 11961da177e4SLinus Torvalds mempool_destroy(rpc_buffer_mempool); 11971da177e4SLinus Torvalds mempool_destroy(rpc_task_mempool); 11981a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_task_slabp); 11991a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_buffer_slabp); 1200f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&delay_queue); 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds int 12041da177e4SLinus Torvalds rpc_init_mempool(void) 12051da177e4SLinus Torvalds { 1206f6a1cc89STrond Myklebust /* 1207f6a1cc89STrond Myklebust * The following is not strictly a mempool initialisation, 1208f6a1cc89STrond Myklebust * but there is no harm in doing it here 1209f6a1cc89STrond Myklebust */ 1210f6a1cc89STrond Myklebust rpc_init_wait_queue(&delay_queue, "delayq"); 1211f6a1cc89STrond Myklebust if (!rpciod_start()) 1212f6a1cc89STrond Myklebust goto err_nomem; 1213f6a1cc89STrond Myklebust 12141da177e4SLinus Torvalds rpc_task_slabp = kmem_cache_create("rpc_tasks", 12151da177e4SLinus Torvalds sizeof(struct rpc_task), 12161da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 121720c2df83SPaul Mundt NULL); 12181da177e4SLinus Torvalds if (!rpc_task_slabp) 12191da177e4SLinus Torvalds goto err_nomem; 12201da177e4SLinus Torvalds rpc_buffer_slabp = kmem_cache_create("rpc_buffers", 12211da177e4SLinus Torvalds RPC_BUFFER_MAXSIZE, 12221da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 122320c2df83SPaul Mundt NULL); 12241da177e4SLinus Torvalds if (!rpc_buffer_slabp) 12251da177e4SLinus Torvalds goto err_nomem; 122693d2341cSMatthew Dobson rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE, 12271da177e4SLinus Torvalds rpc_task_slabp); 12281da177e4SLinus Torvalds if (!rpc_task_mempool) 12291da177e4SLinus Torvalds goto err_nomem; 123093d2341cSMatthew Dobson rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE, 12311da177e4SLinus Torvalds rpc_buffer_slabp); 12321da177e4SLinus Torvalds if (!rpc_buffer_mempool) 12331da177e4SLinus Torvalds goto err_nomem; 12341da177e4SLinus Torvalds return 0; 12351da177e4SLinus Torvalds err_nomem: 12361da177e4SLinus Torvalds rpc_destroy_mempool(); 12371da177e4SLinus Torvalds return -ENOMEM; 12381da177e4SLinus Torvalds } 1239