1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/net/sunrpc/sched.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Scheduling for synchronous and asynchronous RPC requests. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * TCP NFS related read + write fixes 101da177e4SLinus Torvalds * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 111da177e4SLinus Torvalds */ 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/sched.h> 161da177e4SLinus Torvalds #include <linux/interrupt.h> 171da177e4SLinus Torvalds #include <linux/slab.h> 181da177e4SLinus Torvalds #include <linux/mempool.h> 191da177e4SLinus Torvalds #include <linux/smp.h> 201da177e4SLinus Torvalds #include <linux/spinlock.h> 214a3e2f71SArjan van de Ven #include <linux/mutex.h> 22d310310cSJeff Layton #include <linux/freezer.h> 23a1231fdaSTrond Myklebust #include <linux/sched/mm.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 261da177e4SLinus Torvalds 276951867bSBenny Halevy #include "sunrpc.h" 286951867bSBenny Halevy 29f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 301da177e4SLinus Torvalds #define RPCDBG_FACILITY RPCDBG_SCHED 311da177e4SLinus Torvalds #endif 321da177e4SLinus Torvalds 3382b0a4c3STrond Myklebust #define CREATE_TRACE_POINTS 3482b0a4c3STrond Myklebust #include <trace/events/sunrpc.h> 3582b0a4c3STrond Myklebust 361da177e4SLinus Torvalds /* 371da177e4SLinus Torvalds * RPC slabs and memory pools 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds #define RPC_BUFFER_MAXSIZE (2048) 401da177e4SLinus Torvalds #define RPC_BUFFER_POOLSIZE (8) 411da177e4SLinus Torvalds #define RPC_TASK_POOLSIZE (8) 42e18b890bSChristoph Lameter static struct kmem_cache *rpc_task_slabp __read_mostly; 43e18b890bSChristoph Lameter static struct kmem_cache *rpc_buffer_slabp __read_mostly; 44ba89966cSEric Dumazet static mempool_t *rpc_task_mempool __read_mostly; 45ba89966cSEric Dumazet static mempool_t *rpc_buffer_mempool __read_mostly; 461da177e4SLinus Torvalds 4765f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *); 48bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task); 49ff861c4dSKees Cook static void __rpc_queue_timer_fn(struct timer_list *t); 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds /* 521da177e4SLinus Torvalds * RPC tasks sit here while waiting for conditions to improve. 531da177e4SLinus Torvalds */ 54a4a87499STrond Myklebust static struct rpc_wait_queue delay_queue; 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* 571da177e4SLinus Torvalds * rpciod-related stuff 581da177e4SLinus Torvalds */ 5940a5f1b1STrond Myklebust struct workqueue_struct *rpciod_workqueue __read_mostly; 6040a5f1b1STrond Myklebust struct workqueue_struct *xprtiod_workqueue __read_mostly; 61*675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprtiod_workqueue); 621da177e4SLinus Torvalds 635efd1876STrond Myklebust unsigned long 645efd1876STrond Myklebust rpc_task_timeout(const struct rpc_task *task) 655efd1876STrond Myklebust { 665efd1876STrond Myklebust unsigned long timeout = READ_ONCE(task->tk_timeout); 675efd1876STrond Myklebust 685efd1876STrond Myklebust if (timeout != 0) { 695efd1876STrond Myklebust unsigned long now = jiffies; 705efd1876STrond Myklebust if (time_before(now, timeout)) 715efd1876STrond Myklebust return timeout - now; 725efd1876STrond Myklebust } 735efd1876STrond Myklebust return 0; 745efd1876STrond Myklebust } 755efd1876STrond Myklebust EXPORT_SYMBOL_GPL(rpc_task_timeout); 765efd1876STrond Myklebust 771da177e4SLinus Torvalds /* 781da177e4SLinus Torvalds * Disable the timer for a given RPC task. Should be called with 791da177e4SLinus Torvalds * queue->lock and bh_disabled in order to avoid races within 801da177e4SLinus Torvalds * rpc_run_timer(). 811da177e4SLinus Torvalds */ 825d00837bSTrond Myklebust static void 83eb276c0eSTrond Myklebust __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task) 841da177e4SLinus Torvalds { 856b2e6856STrond Myklebust if (list_empty(&task->u.tk_wait.timer_list)) 8636df9aaeSTrond Myklebust return; 8746121cf7SChuck Lever dprintk("RPC: %5u disabling timer\n", task->tk_pid); 881da177e4SLinus Torvalds task->tk_timeout = 0; 8936df9aaeSTrond Myklebust list_del(&task->u.tk_wait.timer_list); 90eb276c0eSTrond Myklebust if (list_empty(&queue->timer_list.list)) 91eb276c0eSTrond Myklebust del_timer(&queue->timer_list.timer); 9236df9aaeSTrond Myklebust } 9336df9aaeSTrond Myklebust 9436df9aaeSTrond Myklebust static void 9536df9aaeSTrond Myklebust rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires) 9636df9aaeSTrond Myklebust { 9724a9d9a2STrond Myklebust timer_reduce(&queue->timer_list.timer, expires); 981da177e4SLinus Torvalds } 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* 1011da177e4SLinus Torvalds * Set up a timer for the current task. 1021da177e4SLinus Torvalds */ 1035d00837bSTrond Myklebust static void 1046b2e6856STrond Myklebust __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task, 1056b2e6856STrond Myklebust unsigned long timeout) 1061da177e4SLinus Torvalds { 10755cc1d78SNicholas Mc Guire dprintk("RPC: %5u setting alarm for %u ms\n", 1086b2e6856STrond Myklebust task->tk_pid, jiffies_to_msecs(timeout - jiffies)); 1091da177e4SLinus Torvalds 1106b2e6856STrond Myklebust task->tk_timeout = timeout; 1116b2e6856STrond Myklebust rpc_set_queue_timer(queue, timeout); 112eb276c0eSTrond Myklebust list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 115c05eecf6STrond Myklebust static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority) 116c05eecf6STrond Myklebust { 117edd2e36fSTrond Myklebust if (queue->priority != priority) { 118c05eecf6STrond Myklebust queue->priority = priority; 119f42f7c28STrond Myklebust queue->nr = 1U << priority; 120c05eecf6STrond Myklebust } 121edd2e36fSTrond Myklebust } 122c05eecf6STrond Myklebust 123c05eecf6STrond Myklebust static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue) 124c05eecf6STrond Myklebust { 125c05eecf6STrond Myklebust rpc_set_waitqueue_priority(queue, queue->maxpriority); 126f42f7c28STrond Myklebust } 127f42f7c28STrond Myklebust 128f42f7c28STrond Myklebust /* 129f42f7c28STrond Myklebust * Add a request to a queue list 130f42f7c28STrond Myklebust */ 131f42f7c28STrond Myklebust static void 132f42f7c28STrond Myklebust __rpc_list_enqueue_task(struct list_head *q, struct rpc_task *task) 133f42f7c28STrond Myklebust { 134f42f7c28STrond Myklebust struct rpc_task *t; 135f42f7c28STrond Myklebust 136f42f7c28STrond Myklebust list_for_each_entry(t, q, u.tk_wait.list) { 137f42f7c28STrond Myklebust if (t->tk_owner == task->tk_owner) { 138f42f7c28STrond Myklebust list_add_tail(&task->u.tk_wait.links, 139f42f7c28STrond Myklebust &t->u.tk_wait.links); 140f42f7c28STrond Myklebust /* Cache the queue head in task->u.tk_wait.list */ 141f42f7c28STrond Myklebust task->u.tk_wait.list.next = q; 142f42f7c28STrond Myklebust task->u.tk_wait.list.prev = NULL; 143f42f7c28STrond Myklebust return; 144f42f7c28STrond Myklebust } 145f42f7c28STrond Myklebust } 146f42f7c28STrond Myklebust INIT_LIST_HEAD(&task->u.tk_wait.links); 147f42f7c28STrond Myklebust list_add_tail(&task->u.tk_wait.list, q); 148f42f7c28STrond Myklebust } 149f42f7c28STrond Myklebust 150f42f7c28STrond Myklebust /* 151f42f7c28STrond Myklebust * Remove request from a queue list 152f42f7c28STrond Myklebust */ 153f42f7c28STrond Myklebust static void 154f42f7c28STrond Myklebust __rpc_list_dequeue_task(struct rpc_task *task) 155f42f7c28STrond Myklebust { 156f42f7c28STrond Myklebust struct list_head *q; 157f42f7c28STrond Myklebust struct rpc_task *t; 158f42f7c28STrond Myklebust 159f42f7c28STrond Myklebust if (task->u.tk_wait.list.prev == NULL) { 160f42f7c28STrond Myklebust list_del(&task->u.tk_wait.links); 161f42f7c28STrond Myklebust return; 162f42f7c28STrond Myklebust } 163f42f7c28STrond Myklebust if (!list_empty(&task->u.tk_wait.links)) { 164f42f7c28STrond Myklebust t = list_first_entry(&task->u.tk_wait.links, 165f42f7c28STrond Myklebust struct rpc_task, 166f42f7c28STrond Myklebust u.tk_wait.links); 167f42f7c28STrond Myklebust /* Assume __rpc_list_enqueue_task() cached the queue head */ 168f42f7c28STrond Myklebust q = t->u.tk_wait.list.next; 169f42f7c28STrond Myklebust list_add_tail(&t->u.tk_wait.list, q); 170f42f7c28STrond Myklebust list_del(&task->u.tk_wait.links); 171f42f7c28STrond Myklebust } 172f42f7c28STrond Myklebust list_del(&task->u.tk_wait.list); 173c05eecf6STrond Myklebust } 174c05eecf6STrond Myklebust 1751da177e4SLinus Torvalds /* 1761da177e4SLinus Torvalds * Add new request to a priority queue. 1771da177e4SLinus Torvalds */ 1783b27bad7STrond Myklebust static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, 1793b27bad7STrond Myklebust struct rpc_task *task, 1803b27bad7STrond Myklebust unsigned char queue_priority) 1811da177e4SLinus Torvalds { 1823b27bad7STrond Myklebust if (unlikely(queue_priority > queue->maxpriority)) 183c05eecf6STrond Myklebust queue_priority = queue->maxpriority; 184f42f7c28STrond Myklebust __rpc_list_enqueue_task(&queue->tasks[queue_priority], task); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 1881da177e4SLinus Torvalds * Add new request to wait queue. 1891da177e4SLinus Torvalds * 1901da177e4SLinus Torvalds * Swapper tasks always get inserted at the head of the queue. 1911da177e4SLinus Torvalds * This should avoid many nasty memory deadlocks and hopefully 1921da177e4SLinus Torvalds * improve overall performance. 1931da177e4SLinus Torvalds * Everyone else gets appended to the queue to ensure proper FIFO behavior. 1941da177e4SLinus Torvalds */ 1953b27bad7STrond Myklebust static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, 1963b27bad7STrond Myklebust struct rpc_task *task, 1973b27bad7STrond Myklebust unsigned char queue_priority) 1981da177e4SLinus Torvalds { 1992bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 2002bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 2012bd4eef8SWeston Andros Adamson return; 2021da177e4SLinus Torvalds 2036b2e6856STrond Myklebust INIT_LIST_HEAD(&task->u.tk_wait.timer_list); 2041da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 2053b27bad7STrond Myklebust __rpc_add_wait_queue_priority(queue, task, queue_priority); 2061da177e4SLinus Torvalds else if (RPC_IS_SWAPPER(task)) 2071da177e4SLinus Torvalds list_add(&task->u.tk_wait.list, &queue->tasks[0]); 2081da177e4SLinus Torvalds else 2091da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]); 21096ef13b2STrond Myklebust task->tk_waitqueue = queue; 211e19b63daSChuck Lever queue->qlen++; 2121166fde6STrond Myklebust /* barrier matches the read in rpc_wake_up_task_queue_locked() */ 2131166fde6STrond Myklebust smp_wmb(); 2141da177e4SLinus Torvalds rpc_set_queued(task); 2151da177e4SLinus Torvalds 21646121cf7SChuck Lever dprintk("RPC: %5u added to queue %p \"%s\"\n", 2171da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* 2211da177e4SLinus Torvalds * Remove request from a priority queue. 2221da177e4SLinus Torvalds */ 2231da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task) 2241da177e4SLinus Torvalds { 225f42f7c28STrond Myklebust __rpc_list_dequeue_task(task); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * Remove request from queue. 2301da177e4SLinus Torvalds * Note: must be called with spin lock held. 2311da177e4SLinus Torvalds */ 23296ef13b2STrond Myklebust static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task) 2331da177e4SLinus Torvalds { 234eb276c0eSTrond Myklebust __rpc_disable_timer(queue, task); 2351da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 2361da177e4SLinus Torvalds __rpc_remove_wait_queue_priority(task); 237f42f7c28STrond Myklebust else 2381da177e4SLinus Torvalds list_del(&task->u.tk_wait.list); 239e19b63daSChuck Lever queue->qlen--; 24046121cf7SChuck Lever dprintk("RPC: %5u removed from queue %p \"%s\"\n", 2411da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2443ff7576dSTrond Myklebust static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds int i; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds spin_lock_init(&queue->lock); 2491da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(queue->tasks); i++) 2501da177e4SLinus Torvalds INIT_LIST_HEAD(&queue->tasks[i]); 2513ff7576dSTrond Myklebust queue->maxpriority = nr_queues - 1; 2521da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 25336df9aaeSTrond Myklebust queue->qlen = 0; 25443123581STrond Myklebust timer_setup(&queue->timer_list.timer, 25543123581STrond Myklebust __rpc_queue_timer_fn, 25643123581STrond Myklebust TIMER_DEFERRABLE); 25736df9aaeSTrond Myklebust INIT_LIST_HEAD(&queue->timer_list.list); 2582f09c242STrond Myklebust rpc_assign_waitqueue_name(queue, qname); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2621da177e4SLinus Torvalds { 2633ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY); 2641da177e4SLinus Torvalds } 265689cf5c1SAlexandros Batsakis EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue); 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2681da177e4SLinus Torvalds { 2693ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, 1); 2701da177e4SLinus Torvalds } 271e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_wait_queue); 2721da177e4SLinus Torvalds 273f6a1cc89STrond Myklebust void rpc_destroy_wait_queue(struct rpc_wait_queue *queue) 274f6a1cc89STrond Myklebust { 27536df9aaeSTrond Myklebust del_timer_sync(&queue->timer_list.timer); 276f6a1cc89STrond Myklebust } 277f6a1cc89STrond Myklebust EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue); 278f6a1cc89STrond Myklebust 279dfd01f02SPeter Zijlstra static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode) 28044c28873STrond Myklebust { 281416ad3c9SColin Cross freezable_schedule_unsafe(); 282dfd01f02SPeter Zijlstra if (signal_pending_state(mode, current)) 283dfd01f02SPeter Zijlstra return -ERESTARTSYS; 28444c28873STrond Myklebust return 0; 28544c28873STrond Myklebust } 28644c28873STrond Myklebust 2871306729bSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS) 288c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task) 289c44fe705STrond Myklebust { 290c44fe705STrond Myklebust static atomic_t rpc_pid; 291c44fe705STrond Myklebust 292c44fe705STrond Myklebust task->tk_pid = atomic_inc_return(&rpc_pid); 293c44fe705STrond Myklebust } 294c44fe705STrond Myklebust #else 295c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task) 296c44fe705STrond Myklebust { 297c44fe705STrond Myklebust } 298c44fe705STrond Myklebust #endif 299c44fe705STrond Myklebust 300e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task) 301e6b3c4dbSTrond Myklebust { 302c44fe705STrond Myklebust rpc_task_set_debuginfo(task); 30358f9612cSTrond Myklebust set_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 304e671edb9SChuck Lever trace_rpc_task_begin(task, NULL); 305e6b3c4dbSTrond Myklebust } 306e6b3c4dbSTrond Myklebust 30744c28873STrond Myklebust /* 30844c28873STrond Myklebust * Mark an RPC call as having completed by clearing the 'active' bit 309bf294b41STrond Myklebust * and then waking up all tasks that were sleeping. 31044c28873STrond Myklebust */ 311bf294b41STrond Myklebust static int rpc_complete_task(struct rpc_task *task) 31244c28873STrond Myklebust { 313bf294b41STrond Myklebust void *m = &task->tk_runstate; 314bf294b41STrond Myklebust wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE); 315bf294b41STrond Myklebust struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE); 316bf294b41STrond Myklebust unsigned long flags; 317bf294b41STrond Myklebust int ret; 318bf294b41STrond Myklebust 319e671edb9SChuck Lever trace_rpc_task_complete(task, NULL); 32082b0a4c3STrond Myklebust 321bf294b41STrond Myklebust spin_lock_irqsave(&wq->lock, flags); 322e6b3c4dbSTrond Myklebust clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 323bf294b41STrond Myklebust ret = atomic_dec_and_test(&task->tk_count); 324bf294b41STrond Myklebust if (waitqueue_active(wq)) 325ac5be6b4SAndrea Arcangeli __wake_up_locked_key(wq, TASK_NORMAL, &k); 326bf294b41STrond Myklebust spin_unlock_irqrestore(&wq->lock, flags); 327bf294b41STrond Myklebust return ret; 32844c28873STrond Myklebust } 32944c28873STrond Myklebust 33044c28873STrond Myklebust /* 33144c28873STrond Myklebust * Allow callers to wait for completion of an RPC call 332bf294b41STrond Myklebust * 333bf294b41STrond Myklebust * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit() 334bf294b41STrond Myklebust * to enforce taking of the wq->lock and hence avoid races with 335bf294b41STrond Myklebust * rpc_complete_task(). 33644c28873STrond Myklebust */ 337c1221321SNeilBrown int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action) 33844c28873STrond Myklebust { 33944c28873STrond Myklebust if (action == NULL) 340150030b7SMatthew Wilcox action = rpc_wait_bit_killable; 341bf294b41STrond Myklebust return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE, 342150030b7SMatthew Wilcox action, TASK_KILLABLE); 34344c28873STrond Myklebust } 344e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task); 34544c28873STrond Myklebust 3461da177e4SLinus Torvalds /* 3471da177e4SLinus Torvalds * Make an RPC task runnable. 3481da177e4SLinus Torvalds * 349506026c3SJeff Layton * Note: If the task is ASYNC, and is being made runnable after sitting on an 350506026c3SJeff Layton * rpc_wait_queue, this must be called with the queue spinlock held to protect 351506026c3SJeff Layton * the wait queue operation. 352a3c3cac5STrond Myklebust * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(), 353a3c3cac5STrond Myklebust * which is needed to ensure that __rpc_execute() doesn't loop (due to the 354a3c3cac5STrond Myklebust * lockless RPC_IS_QUEUED() test) before we've had a chance to test 355a3c3cac5STrond Myklebust * the RPC_TASK_RUNNING flag. 3561da177e4SLinus Torvalds */ 357f1dc237cSTrond Myklebust static void rpc_make_runnable(struct workqueue_struct *wq, 358f1dc237cSTrond Myklebust struct rpc_task *task) 3591da177e4SLinus Torvalds { 360a3c3cac5STrond Myklebust bool need_wakeup = !rpc_test_and_set_running(task); 361a3c3cac5STrond Myklebust 3621da177e4SLinus Torvalds rpc_clear_queued(task); 363a3c3cac5STrond Myklebust if (!need_wakeup) 3641da177e4SLinus Torvalds return; 3651da177e4SLinus Torvalds if (RPC_IS_ASYNC(task)) { 36665f27f38SDavid Howells INIT_WORK(&task->u.tk_work, rpc_async_schedule); 367f1dc237cSTrond Myklebust queue_work(wq, &task->u.tk_work); 3681da177e4SLinus Torvalds } else 36996651ab3STrond Myklebust wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED); 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds /* 3731da177e4SLinus Torvalds * Prepare for sleeping on a wait queue. 3741da177e4SLinus Torvalds * By always appending tasks to the list we ensure FIFO behavior. 3751da177e4SLinus Torvalds * NB: An RPC task will only receive interrupt-driven events as long 3761da177e4SLinus Torvalds * as it's on a wait queue. 3771da177e4SLinus Torvalds */ 3783b27bad7STrond Myklebust static void __rpc_sleep_on_priority(struct rpc_wait_queue *q, 3793b27bad7STrond Myklebust struct rpc_task *task, 3803b27bad7STrond Myklebust unsigned char queue_priority) 3811da177e4SLinus Torvalds { 38246121cf7SChuck Lever dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n", 38346121cf7SChuck Lever task->tk_pid, rpc_qname(q), jiffies); 3841da177e4SLinus Torvalds 385e671edb9SChuck Lever trace_rpc_task_sleep(task, q); 38682b0a4c3STrond Myklebust 3873b27bad7STrond Myklebust __rpc_add_wait_queue(q, task, queue_priority); 3881da177e4SLinus Torvalds 3896b2e6856STrond Myklebust } 3906b2e6856STrond Myklebust 3916b2e6856STrond Myklebust static void __rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q, 3926b2e6856STrond Myklebust struct rpc_task *task, unsigned long timeout, 3936b2e6856STrond Myklebust unsigned char queue_priority) 3946b2e6856STrond Myklebust { 3956b2e6856STrond Myklebust if (time_is_after_jiffies(timeout)) { 3966b2e6856STrond Myklebust __rpc_sleep_on_priority(q, task, queue_priority); 3976b2e6856STrond Myklebust __rpc_add_timer(q, task, timeout); 3986b2e6856STrond Myklebust } else 3996b2e6856STrond Myklebust task->tk_status = -ETIMEDOUT; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 40287150aaeSTrond Myklebust static void rpc_set_tk_callback(struct rpc_task *task, rpc_action action) 40387150aaeSTrond Myklebust { 40487150aaeSTrond Myklebust if (action && !WARN_ON_ONCE(task->tk_callback != NULL)) 40587150aaeSTrond Myklebust task->tk_callback = action; 40687150aaeSTrond Myklebust } 40787150aaeSTrond Myklebust 40887150aaeSTrond Myklebust static bool rpc_sleep_check_activated(struct rpc_task *task) 40987150aaeSTrond Myklebust { 41087150aaeSTrond Myklebust /* We shouldn't ever put an inactive task to sleep */ 41187150aaeSTrond Myklebust if (WARN_ON_ONCE(!RPC_IS_ACTIVATED(task))) { 41287150aaeSTrond Myklebust task->tk_status = -EIO; 41387150aaeSTrond Myklebust rpc_put_task_async(task); 41487150aaeSTrond Myklebust return false; 41587150aaeSTrond Myklebust } 41687150aaeSTrond Myklebust return true; 41787150aaeSTrond Myklebust } 41887150aaeSTrond Myklebust 4196b2e6856STrond Myklebust void rpc_sleep_on_timeout(struct rpc_wait_queue *q, struct rpc_task *task, 4206b2e6856STrond Myklebust rpc_action action, unsigned long timeout) 4211da177e4SLinus Torvalds { 42287150aaeSTrond Myklebust if (!rpc_sleep_check_activated(task)) 423e454a7a8SWeston Andros Adamson return; 42487150aaeSTrond Myklebust 42587150aaeSTrond Myklebust rpc_set_tk_callback(task, action); 426e6b3c4dbSTrond Myklebust 4271da177e4SLinus Torvalds /* 4281da177e4SLinus Torvalds * Protect the queue operations. 4291da177e4SLinus Torvalds */ 4301da177e4SLinus Torvalds spin_lock_bh(&q->lock); 4316b2e6856STrond Myklebust __rpc_sleep_on_priority_timeout(q, task, timeout, task->tk_priority); 4326b2e6856STrond Myklebust spin_unlock_bh(&q->lock); 4336b2e6856STrond Myklebust } 4346b2e6856STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_timeout); 4356b2e6856STrond Myklebust 4366b2e6856STrond Myklebust void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, 4376b2e6856STrond Myklebust rpc_action action) 4386b2e6856STrond Myklebust { 4396b2e6856STrond Myklebust if (!rpc_sleep_check_activated(task)) 4406b2e6856STrond Myklebust return; 4416b2e6856STrond Myklebust 4426b2e6856STrond Myklebust rpc_set_tk_callback(task, action); 4436b2e6856STrond Myklebust 4446b2e6856STrond Myklebust WARN_ON_ONCE(task->tk_timeout != 0); 4456b2e6856STrond Myklebust /* 4466b2e6856STrond Myklebust * Protect the queue operations. 4476b2e6856STrond Myklebust */ 4486b2e6856STrond Myklebust spin_lock_bh(&q->lock); 44987150aaeSTrond Myklebust __rpc_sleep_on_priority(q, task, task->tk_priority); 4501da177e4SLinus Torvalds spin_unlock_bh(&q->lock); 4511da177e4SLinus Torvalds } 452e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on); 4531da177e4SLinus Torvalds 4546b2e6856STrond Myklebust void rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q, 4556b2e6856STrond Myklebust struct rpc_task *task, unsigned long timeout, int priority) 4566b2e6856STrond Myklebust { 4576b2e6856STrond Myklebust if (!rpc_sleep_check_activated(task)) 4586b2e6856STrond Myklebust return; 4596b2e6856STrond Myklebust 4606b2e6856STrond Myklebust priority -= RPC_PRIORITY_LOW; 4616b2e6856STrond Myklebust /* 4626b2e6856STrond Myklebust * Protect the queue operations. 4636b2e6856STrond Myklebust */ 4646b2e6856STrond Myklebust spin_lock_bh(&q->lock); 4656b2e6856STrond Myklebust __rpc_sleep_on_priority_timeout(q, task, timeout, priority); 4666b2e6856STrond Myklebust spin_unlock_bh(&q->lock); 4676b2e6856STrond Myklebust } 4686b2e6856STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority_timeout); 4696b2e6856STrond Myklebust 4703b27bad7STrond Myklebust void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task, 4718357a9b6STrond Myklebust int priority) 4723b27bad7STrond Myklebust { 47387150aaeSTrond Myklebust if (!rpc_sleep_check_activated(task)) 474e454a7a8SWeston Andros Adamson return; 47587150aaeSTrond Myklebust 4766b2e6856STrond Myklebust WARN_ON_ONCE(task->tk_timeout != 0); 4778357a9b6STrond Myklebust priority -= RPC_PRIORITY_LOW; 4783b27bad7STrond Myklebust /* 4793b27bad7STrond Myklebust * Protect the queue operations. 4803b27bad7STrond Myklebust */ 4813b27bad7STrond Myklebust spin_lock_bh(&q->lock); 4828357a9b6STrond Myklebust __rpc_sleep_on_priority(q, task, priority); 4833b27bad7STrond Myklebust spin_unlock_bh(&q->lock); 4843b27bad7STrond Myklebust } 4851e1093c7STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority); 4863b27bad7STrond Myklebust 4871da177e4SLinus Torvalds /** 488f1dc237cSTrond Myklebust * __rpc_do_wake_up_task_on_wq - wake up a single rpc_task 489f1dc237cSTrond Myklebust * @wq: workqueue on which to run task 49096ef13b2STrond Myklebust * @queue: wait queue 4911da177e4SLinus Torvalds * @task: task to be woken up 4921da177e4SLinus Torvalds * 4931da177e4SLinus Torvalds * Caller must hold queue->lock, and have cleared the task queued flag. 4941da177e4SLinus Torvalds */ 495f1dc237cSTrond Myklebust static void __rpc_do_wake_up_task_on_wq(struct workqueue_struct *wq, 496f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 497f1dc237cSTrond Myklebust struct rpc_task *task) 4981da177e4SLinus Torvalds { 49946121cf7SChuck Lever dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n", 50046121cf7SChuck Lever task->tk_pid, jiffies); 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* Has the task been executed yet? If not, we cannot wake it up! */ 5031da177e4SLinus Torvalds if (!RPC_IS_ACTIVATED(task)) { 5041da177e4SLinus Torvalds printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task); 5051da177e4SLinus Torvalds return; 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds 508e671edb9SChuck Lever trace_rpc_task_wakeup(task, queue); 50982b0a4c3STrond Myklebust 51096ef13b2STrond Myklebust __rpc_remove_wait_queue(queue, task); 5111da177e4SLinus Torvalds 512f1dc237cSTrond Myklebust rpc_make_runnable(wq, task); 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds dprintk("RPC: __rpc_wake_up_task done\n"); 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* 51896ef13b2STrond Myklebust * Wake up a queued task while the queue lock is being held 5191da177e4SLinus Torvalds */ 520359c48c0STrond Myklebust static struct rpc_task * 521359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(struct workqueue_struct *wq, 522359c48c0STrond Myklebust struct rpc_wait_queue *queue, struct rpc_task *task, 523359c48c0STrond Myklebust bool (*action)(struct rpc_task *, void *), void *data) 5241da177e4SLinus Torvalds { 5251166fde6STrond Myklebust if (RPC_IS_QUEUED(task)) { 5261166fde6STrond Myklebust smp_rmb(); 527359c48c0STrond Myklebust if (task->tk_waitqueue == queue) { 528359c48c0STrond Myklebust if (action == NULL || action(task, data)) { 529f1dc237cSTrond Myklebust __rpc_do_wake_up_task_on_wq(wq, queue, task); 530359c48c0STrond Myklebust return task; 5311da177e4SLinus Torvalds } 5321166fde6STrond Myklebust } 533359c48c0STrond Myklebust } 534359c48c0STrond Myklebust return NULL; 535359c48c0STrond Myklebust } 536359c48c0STrond Myklebust 537359c48c0STrond Myklebust static void 538359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_locked(struct workqueue_struct *wq, 539359c48c0STrond Myklebust struct rpc_wait_queue *queue, struct rpc_task *task) 540359c48c0STrond Myklebust { 541359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(wq, queue, task, NULL, NULL); 542359c48c0STrond Myklebust } 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /* 545f1dc237cSTrond Myklebust * Wake up a queued task while the queue lock is being held 546f1dc237cSTrond Myklebust */ 547f1dc237cSTrond Myklebust static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, struct rpc_task *task) 548f1dc237cSTrond Myklebust { 549f1dc237cSTrond Myklebust rpc_wake_up_task_on_wq_queue_locked(rpciod_workqueue, queue, task); 550f1dc237cSTrond Myklebust } 551f1dc237cSTrond Myklebust 552f1dc237cSTrond Myklebust /* 55396ef13b2STrond Myklebust * Wake up a task on a specific queue 55496ef13b2STrond Myklebust */ 5552275cde4STrond Myklebust void rpc_wake_up_queued_task_on_wq(struct workqueue_struct *wq, 5562275cde4STrond Myklebust struct rpc_wait_queue *queue, 5572275cde4STrond Myklebust struct rpc_task *task) 5582275cde4STrond Myklebust { 5595ce97039STrond Myklebust if (!RPC_IS_QUEUED(task)) 5605ce97039STrond Myklebust return; 5612275cde4STrond Myklebust spin_lock_bh(&queue->lock); 5622275cde4STrond Myklebust rpc_wake_up_task_on_wq_queue_locked(wq, queue, task); 5632275cde4STrond Myklebust spin_unlock_bh(&queue->lock); 5642275cde4STrond Myklebust } 5652275cde4STrond Myklebust 5662275cde4STrond Myklebust /* 5672275cde4STrond Myklebust * Wake up a task on a specific queue 5682275cde4STrond Myklebust */ 56996ef13b2STrond Myklebust void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task) 57096ef13b2STrond Myklebust { 5715ce97039STrond Myklebust if (!RPC_IS_QUEUED(task)) 5725ce97039STrond Myklebust return; 5735e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 57496ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 5755e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 57696ef13b2STrond Myklebust } 57796ef13b2STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task); 57896ef13b2STrond Myklebust 579359c48c0STrond Myklebust static bool rpc_task_action_set_status(struct rpc_task *task, void *status) 580359c48c0STrond Myklebust { 581359c48c0STrond Myklebust task->tk_status = *(int *)status; 582359c48c0STrond Myklebust return true; 583359c48c0STrond Myklebust } 584359c48c0STrond Myklebust 585359c48c0STrond Myklebust static void 586359c48c0STrond Myklebust rpc_wake_up_task_queue_set_status_locked(struct rpc_wait_queue *queue, 587359c48c0STrond Myklebust struct rpc_task *task, int status) 588359c48c0STrond Myklebust { 589359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue, 590359c48c0STrond Myklebust task, rpc_task_action_set_status, &status); 591359c48c0STrond Myklebust } 592359c48c0STrond Myklebust 593359c48c0STrond Myklebust /** 594359c48c0STrond Myklebust * rpc_wake_up_queued_task_set_status - wake up a task and set task->tk_status 595359c48c0STrond Myklebust * @queue: pointer to rpc_wait_queue 596359c48c0STrond Myklebust * @task: pointer to rpc_task 597359c48c0STrond Myklebust * @status: integer error value 598359c48c0STrond Myklebust * 599359c48c0STrond Myklebust * If @task is queued on @queue, then it is woken up, and @task->tk_status is 600359c48c0STrond Myklebust * set to the value of @status. 601359c48c0STrond Myklebust */ 602359c48c0STrond Myklebust void 603359c48c0STrond Myklebust rpc_wake_up_queued_task_set_status(struct rpc_wait_queue *queue, 604359c48c0STrond Myklebust struct rpc_task *task, int status) 605359c48c0STrond Myklebust { 606359c48c0STrond Myklebust if (!RPC_IS_QUEUED(task)) 607359c48c0STrond Myklebust return; 608359c48c0STrond Myklebust spin_lock_bh(&queue->lock); 609359c48c0STrond Myklebust rpc_wake_up_task_queue_set_status_locked(queue, task, status); 610359c48c0STrond Myklebust spin_unlock_bh(&queue->lock); 611359c48c0STrond Myklebust } 612359c48c0STrond Myklebust 61396ef13b2STrond Myklebust /* 6141da177e4SLinus Torvalds * Wake up the next task on a priority queue. 6151da177e4SLinus Torvalds */ 616961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue) 6171da177e4SLinus Torvalds { 6181da177e4SLinus Torvalds struct list_head *q; 6191da177e4SLinus Torvalds struct rpc_task *task; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /* 6223ff7576dSTrond Myklebust * Service a batch of tasks from a single owner. 6231da177e4SLinus Torvalds */ 6241da177e4SLinus Torvalds q = &queue->tasks[queue->priority]; 625f42f7c28STrond Myklebust if (!list_empty(q) && --queue->nr) { 626f42f7c28STrond Myklebust task = list_first_entry(q, struct rpc_task, u.tk_wait.list); 6271da177e4SLinus Torvalds goto out; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* 6311da177e4SLinus Torvalds * Service the next queue. 6321da177e4SLinus Torvalds */ 6331da177e4SLinus Torvalds do { 6341da177e4SLinus Torvalds if (q == &queue->tasks[0]) 6351da177e4SLinus Torvalds q = &queue->tasks[queue->maxpriority]; 6361da177e4SLinus Torvalds else 6371da177e4SLinus Torvalds q = q - 1; 6381da177e4SLinus Torvalds if (!list_empty(q)) { 639f42f7c28STrond Myklebust task = list_first_entry(q, struct rpc_task, u.tk_wait.list); 6401da177e4SLinus Torvalds goto new_queue; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds } while (q != &queue->tasks[queue->priority]); 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 6451da177e4SLinus Torvalds return NULL; 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds new_queue: 6481da177e4SLinus Torvalds rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0])); 6491da177e4SLinus Torvalds out: 6501da177e4SLinus Torvalds return task; 6511da177e4SLinus Torvalds } 6521da177e4SLinus Torvalds 653961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue) 654961a828dSTrond Myklebust { 655961a828dSTrond Myklebust if (RPC_IS_PRIORITY(queue)) 656961a828dSTrond Myklebust return __rpc_find_next_queued_priority(queue); 657961a828dSTrond Myklebust if (!list_empty(&queue->tasks[0])) 658961a828dSTrond Myklebust return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list); 659961a828dSTrond Myklebust return NULL; 660961a828dSTrond Myklebust } 661961a828dSTrond Myklebust 662961a828dSTrond Myklebust /* 663961a828dSTrond Myklebust * Wake up the first task on the wait queue. 664961a828dSTrond Myklebust */ 665f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq, 666f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 667961a828dSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 668961a828dSTrond Myklebust { 669961a828dSTrond Myklebust struct rpc_task *task = NULL; 670961a828dSTrond Myklebust 671961a828dSTrond Myklebust dprintk("RPC: wake_up_first(%p \"%s\")\n", 672961a828dSTrond Myklebust queue, rpc_qname(queue)); 673961a828dSTrond Myklebust spin_lock_bh(&queue->lock); 674961a828dSTrond Myklebust task = __rpc_find_next_queued(queue); 675359c48c0STrond Myklebust if (task != NULL) 676359c48c0STrond Myklebust task = rpc_wake_up_task_on_wq_queue_action_locked(wq, queue, 677359c48c0STrond Myklebust task, func, data); 678961a828dSTrond Myklebust spin_unlock_bh(&queue->lock); 679961a828dSTrond Myklebust 680961a828dSTrond Myklebust return task; 681961a828dSTrond Myklebust } 682f1dc237cSTrond Myklebust 683f1dc237cSTrond Myklebust /* 684f1dc237cSTrond Myklebust * Wake up the first task on the wait queue. 685f1dc237cSTrond Myklebust */ 686f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue, 687f1dc237cSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 688f1dc237cSTrond Myklebust { 689f1dc237cSTrond Myklebust return rpc_wake_up_first_on_wq(rpciod_workqueue, queue, func, data); 690f1dc237cSTrond Myklebust } 691961a828dSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_first); 692961a828dSTrond Myklebust 693961a828dSTrond Myklebust static bool rpc_wake_up_next_func(struct rpc_task *task, void *data) 694961a828dSTrond Myklebust { 695961a828dSTrond Myklebust return true; 696961a828dSTrond Myklebust } 697961a828dSTrond Myklebust 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * Wake up the next task on the wait queue. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue) 7021da177e4SLinus Torvalds { 703961a828dSTrond Myklebust return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL); 7041da177e4SLinus Torvalds } 705e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds /** 7081da177e4SLinus Torvalds * rpc_wake_up - wake up all rpc_tasks 7091da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 7101da177e4SLinus Torvalds * 7111da177e4SLinus Torvalds * Grabs queue->lock 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue) 7141da177e4SLinus Torvalds { 7151da177e4SLinus Torvalds struct list_head *head; 716e6d83d55STrond Myklebust 7175e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 7181da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 7191da177e4SLinus Torvalds for (;;) { 720540a0f75STrond Myklebust while (!list_empty(head)) { 721540a0f75STrond Myklebust struct rpc_task *task; 722540a0f75STrond Myklebust task = list_first_entry(head, 723540a0f75STrond Myklebust struct rpc_task, 724540a0f75STrond Myklebust u.tk_wait.list); 72596ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 726540a0f75STrond Myklebust } 7271da177e4SLinus Torvalds if (head == &queue->tasks[0]) 7281da177e4SLinus Torvalds break; 7291da177e4SLinus Torvalds head--; 7301da177e4SLinus Torvalds } 7315e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 7321da177e4SLinus Torvalds } 733e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up); 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds /** 7361da177e4SLinus Torvalds * rpc_wake_up_status - wake up all rpc_tasks and set their status value. 7371da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 7381da177e4SLinus Torvalds * @status: status value to set 7391da177e4SLinus Torvalds * 7401da177e4SLinus Torvalds * Grabs queue->lock 7411da177e4SLinus Torvalds */ 7421da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds struct list_head *head; 7451da177e4SLinus Torvalds 7465e4424afSTrond Myklebust spin_lock_bh(&queue->lock); 7471da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 7481da177e4SLinus Torvalds for (;;) { 749540a0f75STrond Myklebust while (!list_empty(head)) { 750540a0f75STrond Myklebust struct rpc_task *task; 751540a0f75STrond Myklebust task = list_first_entry(head, 752540a0f75STrond Myklebust struct rpc_task, 753540a0f75STrond Myklebust u.tk_wait.list); 7541da177e4SLinus Torvalds task->tk_status = status; 75596ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds if (head == &queue->tasks[0]) 7581da177e4SLinus Torvalds break; 7591da177e4SLinus Torvalds head--; 7601da177e4SLinus Torvalds } 7615e4424afSTrond Myklebust spin_unlock_bh(&queue->lock); 7621da177e4SLinus Torvalds } 763e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status); 7641da177e4SLinus Torvalds 765ff861c4dSKees Cook static void __rpc_queue_timer_fn(struct timer_list *t) 76636df9aaeSTrond Myklebust { 767ff861c4dSKees Cook struct rpc_wait_queue *queue = from_timer(queue, t, timer_list.timer); 76836df9aaeSTrond Myklebust struct rpc_task *task, *n; 76936df9aaeSTrond Myklebust unsigned long expires, now, timeo; 77036df9aaeSTrond Myklebust 77136df9aaeSTrond Myklebust spin_lock(&queue->lock); 77236df9aaeSTrond Myklebust expires = now = jiffies; 77336df9aaeSTrond Myklebust list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) { 7746b2e6856STrond Myklebust timeo = task->tk_timeout; 77536df9aaeSTrond Myklebust if (time_after_eq(now, timeo)) { 77636df9aaeSTrond Myklebust dprintk("RPC: %5u timeout\n", task->tk_pid); 77736df9aaeSTrond Myklebust task->tk_status = -ETIMEDOUT; 77836df9aaeSTrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 77936df9aaeSTrond Myklebust continue; 78036df9aaeSTrond Myklebust } 78136df9aaeSTrond Myklebust if (expires == now || time_after(expires, timeo)) 78236df9aaeSTrond Myklebust expires = timeo; 78336df9aaeSTrond Myklebust } 78436df9aaeSTrond Myklebust if (!list_empty(&queue->timer_list.list)) 78536df9aaeSTrond Myklebust rpc_set_queue_timer(queue, expires); 78636df9aaeSTrond Myklebust spin_unlock(&queue->lock); 78736df9aaeSTrond Myklebust } 78836df9aaeSTrond Myklebust 7898014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task) 7908014793bSTrond Myklebust { 7916bd14416STrond Myklebust if (task->tk_status == -ETIMEDOUT) 7925d00837bSTrond Myklebust task->tk_status = 0; 7938014793bSTrond Myklebust } 7948014793bSTrond Myklebust 7951da177e4SLinus Torvalds /* 7961da177e4SLinus Torvalds * Run a task at a later time 7971da177e4SLinus Torvalds */ 7988014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay) 7991da177e4SLinus Torvalds { 8006b2e6856STrond Myklebust rpc_sleep_on_timeout(&delay_queue, task, __rpc_atrun, jiffies + delay); 8011da177e4SLinus Torvalds } 802e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay); 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds /* 8054ce70adaSTrond Myklebust * Helper to call task->tk_ops->rpc_call_prepare 8064ce70adaSTrond Myklebust */ 807aae2006eSAndy Adamson void rpc_prepare_task(struct rpc_task *task) 8084ce70adaSTrond Myklebust { 8094ce70adaSTrond Myklebust task->tk_ops->rpc_call_prepare(task, task->tk_calldata); 8104ce70adaSTrond Myklebust } 8114ce70adaSTrond Myklebust 8127fdcf13bSTrond Myklebust static void 8137fdcf13bSTrond Myklebust rpc_init_task_statistics(struct rpc_task *task) 8147fdcf13bSTrond Myklebust { 8157fdcf13bSTrond Myklebust /* Initialize retry counters */ 8167fdcf13bSTrond Myklebust task->tk_garb_retry = 2; 8177fdcf13bSTrond Myklebust task->tk_cred_retry = 2; 8187fdcf13bSTrond Myklebust task->tk_rebind_retry = 2; 8197fdcf13bSTrond Myklebust 8207fdcf13bSTrond Myklebust /* starting timestamp */ 8217fdcf13bSTrond Myklebust task->tk_start = ktime_get(); 8227fdcf13bSTrond Myklebust } 8237fdcf13bSTrond Myklebust 8247fdcf13bSTrond Myklebust static void 8257fdcf13bSTrond Myklebust rpc_reset_task_statistics(struct rpc_task *task) 8267fdcf13bSTrond Myklebust { 8277fdcf13bSTrond Myklebust task->tk_timeouts = 0; 828ae67bd38STrond Myklebust task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_SENT); 8297fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 8307fdcf13bSTrond Myklebust } 8317fdcf13bSTrond Myklebust 8324ce70adaSTrond Myklebust /* 833963d8fe5STrond Myklebust * Helper that calls task->tk_ops->rpc_call_done if it exists 834d05fdb0cSTrond Myklebust */ 835abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task) 836d05fdb0cSTrond Myklebust { 837abbcf28fSTrond Myklebust task->tk_action = NULL; 838963d8fe5STrond Myklebust if (task->tk_ops->rpc_call_done != NULL) { 839963d8fe5STrond Myklebust task->tk_ops->rpc_call_done(task, task->tk_calldata); 840d05fdb0cSTrond Myklebust if (task->tk_action != NULL) { 841abbcf28fSTrond Myklebust /* Always release the RPC slot and buffer memory */ 842d05fdb0cSTrond Myklebust xprt_release(task); 8437fdcf13bSTrond Myklebust rpc_reset_task_statistics(task); 844d05fdb0cSTrond Myklebust } 845d05fdb0cSTrond Myklebust } 846d05fdb0cSTrond Myklebust } 847d9b6cd94STrond Myklebust 848ae67bd38STrond Myklebust void rpc_signal_task(struct rpc_task *task) 849ae67bd38STrond Myklebust { 850ae67bd38STrond Myklebust struct rpc_wait_queue *queue; 851ae67bd38STrond Myklebust 852ae67bd38STrond Myklebust if (!RPC_IS_ACTIVATED(task)) 853ae67bd38STrond Myklebust return; 854ae67bd38STrond Myklebust set_bit(RPC_TASK_SIGNALLED, &task->tk_runstate); 855ae67bd38STrond Myklebust smp_mb__after_atomic(); 856ae67bd38STrond Myklebust queue = READ_ONCE(task->tk_waitqueue); 857ae67bd38STrond Myklebust if (queue) 858ae67bd38STrond Myklebust rpc_wake_up_queued_task_set_status(queue, task, -ERESTARTSYS); 859ae67bd38STrond Myklebust } 860ae67bd38STrond Myklebust 861d9b6cd94STrond Myklebust void rpc_exit(struct rpc_task *task, int status) 862d9b6cd94STrond Myklebust { 863d9b6cd94STrond Myklebust task->tk_status = status; 864d9b6cd94STrond Myklebust task->tk_action = rpc_exit_task; 865d9b6cd94STrond Myklebust rpc_wake_up_queued_task(task->tk_waitqueue, task); 866d9b6cd94STrond Myklebust } 867d9b6cd94STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit); 868d05fdb0cSTrond Myklebust 869bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata) 870bbd5a1f9STrond Myklebust { 871a86dc496STrond Myklebust if (ops->rpc_release != NULL) 872bbd5a1f9STrond Myklebust ops->rpc_release(calldata); 873bbd5a1f9STrond Myklebust } 874bbd5a1f9STrond Myklebust 875d05fdb0cSTrond Myklebust /* 8761da177e4SLinus Torvalds * This is the RPC `scheduler' (or rather, the finite state machine). 8771da177e4SLinus Torvalds */ 8782efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task) 8791da177e4SLinus Torvalds { 880eb9b55abSTrond Myklebust struct rpc_wait_queue *queue; 881eb9b55abSTrond Myklebust int task_is_async = RPC_IS_ASYNC(task); 8821da177e4SLinus Torvalds int status = 0; 8831da177e4SLinus Torvalds 88446121cf7SChuck Lever dprintk("RPC: %5u __rpc_execute flags=0x%x\n", 8851da177e4SLinus Torvalds task->tk_pid, task->tk_flags); 8861da177e4SLinus Torvalds 8872bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 8882bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 8892bd4eef8SWeston Andros Adamson return; 8901da177e4SLinus Torvalds 891d05fdb0cSTrond Myklebust for (;;) { 892b55c5989STrond Myklebust void (*do_action)(struct rpc_task *); 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds /* 89521ead9ffSChuck Lever * Perform the next FSM step or a pending callback. 89621ead9ffSChuck Lever * 897b55c5989STrond Myklebust * tk_action may be NULL if the task has been killed. 898b55c5989STrond Myklebust * In particular, note that rpc_killall_tasks may 899b55c5989STrond Myklebust * do this at any time, so beware when dereferencing. 9001da177e4SLinus Torvalds */ 901b55c5989STrond Myklebust do_action = task->tk_action; 90221ead9ffSChuck Lever if (task->tk_callback) { 90321ead9ffSChuck Lever do_action = task->tk_callback; 90421ead9ffSChuck Lever task->tk_callback = NULL; 9051da177e4SLinus Torvalds } 90621ead9ffSChuck Lever if (!do_action) 90721ead9ffSChuck Lever break; 908e671edb9SChuck Lever trace_rpc_task_run_action(task, do_action); 909b55c5989STrond Myklebust do_action(task); 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds /* 9121da177e4SLinus Torvalds * Lockless check for whether task is sleeping or not. 9131da177e4SLinus Torvalds */ 9141da177e4SLinus Torvalds if (!RPC_IS_QUEUED(task)) 9151da177e4SLinus Torvalds continue; 916ae67bd38STrond Myklebust 917ae67bd38STrond Myklebust /* 918ae67bd38STrond Myklebust * Signalled tasks should exit rather than sleep. 919ae67bd38STrond Myklebust */ 920ae67bd38STrond Myklebust if (RPC_SIGNALLED(task)) 921ae67bd38STrond Myklebust rpc_exit(task, -ERESTARTSYS); 922ae67bd38STrond Myklebust 923eb9b55abSTrond Myklebust /* 924eb9b55abSTrond Myklebust * The queue->lock protects against races with 925eb9b55abSTrond Myklebust * rpc_make_runnable(). 926eb9b55abSTrond Myklebust * 927eb9b55abSTrond Myklebust * Note that once we clear RPC_TASK_RUNNING on an asynchronous 928eb9b55abSTrond Myklebust * rpc_task, rpc_make_runnable() can assign it to a 929eb9b55abSTrond Myklebust * different workqueue. We therefore cannot assume that the 930eb9b55abSTrond Myklebust * rpc_task pointer may still be dereferenced. 931eb9b55abSTrond Myklebust */ 932eb9b55abSTrond Myklebust queue = task->tk_waitqueue; 933eb9b55abSTrond Myklebust spin_lock_bh(&queue->lock); 934eb9b55abSTrond Myklebust if (!RPC_IS_QUEUED(task)) { 935eb9b55abSTrond Myklebust spin_unlock_bh(&queue->lock); 9361da177e4SLinus Torvalds continue; 9371da177e4SLinus Torvalds } 938eb9b55abSTrond Myklebust rpc_clear_running(task); 939eb9b55abSTrond Myklebust spin_unlock_bh(&queue->lock); 940eb9b55abSTrond Myklebust if (task_is_async) 941eb9b55abSTrond Myklebust return; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds /* sync task: sleep here */ 94446121cf7SChuck Lever dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid); 94596651ab3STrond Myklebust status = out_of_line_wait_on_bit(&task->tk_runstate, 946150030b7SMatthew Wilcox RPC_TASK_QUEUED, rpc_wait_bit_killable, 947150030b7SMatthew Wilcox TASK_KILLABLE); 948ae67bd38STrond Myklebust if (status < 0) { 9491da177e4SLinus Torvalds /* 9501da177e4SLinus Torvalds * When a sync task receives a signal, it exits with 9511da177e4SLinus Torvalds * -ERESTARTSYS. In order to catch any callbacks that 9521da177e4SLinus Torvalds * clean up after sleeping on some queue, we don't 9531da177e4SLinus Torvalds * break the loop here, but go around once more. 9541da177e4SLinus Torvalds */ 95546121cf7SChuck Lever dprintk("RPC: %5u got signal\n", task->tk_pid); 956ae67bd38STrond Myklebust set_bit(RPC_TASK_SIGNALLED, &task->tk_runstate); 9571da177e4SLinus Torvalds rpc_exit(task, -ERESTARTSYS); 9581da177e4SLinus Torvalds } 95946121cf7SChuck Lever dprintk("RPC: %5u sync task resuming\n", task->tk_pid); 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 96246121cf7SChuck Lever dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status, 96346121cf7SChuck Lever task->tk_status); 9641da177e4SLinus Torvalds /* Release all resources associated with the task */ 9651da177e4SLinus Torvalds rpc_release_task(task); 9661da177e4SLinus Torvalds } 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds /* 9691da177e4SLinus Torvalds * User-visible entry point to the scheduler. 9701da177e4SLinus Torvalds * 9711da177e4SLinus Torvalds * This may be called recursively if e.g. an async NFS task updates 9721da177e4SLinus Torvalds * the attributes and finds that dirty pages must be flushed. 9731da177e4SLinus Torvalds * NOTE: Upon exit of this function the task is guaranteed to be 9741da177e4SLinus Torvalds * released. In particular note that tk_release() will have 9751da177e4SLinus Torvalds * been called, so your task memory may have been freed. 9761da177e4SLinus Torvalds */ 9772efef837STrond Myklebust void rpc_execute(struct rpc_task *task) 9781da177e4SLinus Torvalds { 979a76580fbSTrond Myklebust bool is_async = RPC_IS_ASYNC(task); 980a76580fbSTrond Myklebust 98144c28873STrond Myklebust rpc_set_active(task); 982f1dc237cSTrond Myklebust rpc_make_runnable(rpciod_workqueue, task); 983a76580fbSTrond Myklebust if (!is_async) 9842efef837STrond Myklebust __rpc_execute(task); 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 98765f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work) 9881da177e4SLinus Torvalds { 989a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 990a1231fdaSTrond Myklebust 99165f27f38SDavid Howells __rpc_execute(container_of(work, struct rpc_task, u.tk_work)); 992a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 99502107148SChuck Lever /** 9965fe6eaa1SChuck Lever * rpc_malloc - allocate RPC buffer resources 9975fe6eaa1SChuck Lever * @task: RPC task 9985fe6eaa1SChuck Lever * 9995fe6eaa1SChuck Lever * A single memory region is allocated, which is split between the 10005fe6eaa1SChuck Lever * RPC call and RPC reply that this task is being used for. When 10015fe6eaa1SChuck Lever * this RPC is retired, the memory is released by calling rpc_free. 10021da177e4SLinus Torvalds * 1003c5a4dd8bSChuck Lever * To prevent rpciod from hanging, this allocator never sleeps, 10045fe6eaa1SChuck Lever * returning -ENOMEM and suppressing warning if the request cannot 10055fe6eaa1SChuck Lever * be serviced immediately. The caller can arrange to sleep in a 10065fe6eaa1SChuck Lever * way that is safe for rpciod. 1007c5a4dd8bSChuck Lever * 1008c5a4dd8bSChuck Lever * Most requests are 'small' (under 2KiB) and can be serviced from a 1009c5a4dd8bSChuck Lever * mempool, ensuring that NFS reads and writes can always proceed, 1010c5a4dd8bSChuck Lever * and that there is good locality of reference for these buffers. 10111da177e4SLinus Torvalds */ 10125fe6eaa1SChuck Lever int rpc_malloc(struct rpc_task *task) 10131da177e4SLinus Torvalds { 10145fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 10155fe6eaa1SChuck Lever size_t size = rqst->rq_callsize + rqst->rq_rcvsize; 1016aa3d1faeSChuck Lever struct rpc_buffer *buf; 101712a3ad61STrond Myklebust gfp_t gfp = GFP_NOFS; 1018a564b8f0SMel Gorman 1019a564b8f0SMel Gorman if (RPC_IS_SWAPPER(task)) 1020c4a7ca77STrond Myklebust gfp = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN; 10211da177e4SLinus Torvalds 1022aa3d1faeSChuck Lever size += sizeof(struct rpc_buffer); 1023c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 1024c5a4dd8bSChuck Lever buf = mempool_alloc(rpc_buffer_mempool, gfp); 10251da177e4SLinus Torvalds else 1026c5a4dd8bSChuck Lever buf = kmalloc(size, gfp); 1027ddce40dfSPeter Zijlstra 1028ddce40dfSPeter Zijlstra if (!buf) 10295fe6eaa1SChuck Lever return -ENOMEM; 1030ddce40dfSPeter Zijlstra 1031aa3d1faeSChuck Lever buf->len = size; 1032215d0678SGeert Uytterhoeven dprintk("RPC: %5u allocated buffer of size %zu at %p\n", 1033c5a4dd8bSChuck Lever task->tk_pid, size, buf); 10345fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 103568778945SChuck Lever rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 10365fe6eaa1SChuck Lever return 0; 10371da177e4SLinus Torvalds } 103812444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc); 10391da177e4SLinus Torvalds 104002107148SChuck Lever /** 10413435c74aSChuck Lever * rpc_free - free RPC buffer resources allocated via rpc_malloc 10423435c74aSChuck Lever * @task: RPC task 104302107148SChuck Lever * 104402107148SChuck Lever */ 10453435c74aSChuck Lever void rpc_free(struct rpc_task *task) 10461da177e4SLinus Torvalds { 10473435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 1048aa3d1faeSChuck Lever size_t size; 1049aa3d1faeSChuck Lever struct rpc_buffer *buf; 105002107148SChuck Lever 1051aa3d1faeSChuck Lever buf = container_of(buffer, struct rpc_buffer, data); 1052aa3d1faeSChuck Lever size = buf->len; 1053c5a4dd8bSChuck Lever 1054215d0678SGeert Uytterhoeven dprintk("RPC: freeing buffer of size %zu at %p\n", 1055c5a4dd8bSChuck Lever size, buf); 1056aa3d1faeSChuck Lever 1057c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 1058c5a4dd8bSChuck Lever mempool_free(buf, rpc_buffer_mempool); 10591da177e4SLinus Torvalds else 1060c5a4dd8bSChuck Lever kfree(buf); 10611da177e4SLinus Torvalds } 106212444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free); 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /* 10651da177e4SLinus Torvalds * Creation and deletion of RPC task structures 10661da177e4SLinus Torvalds */ 106747fe0648STrond Myklebust static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data) 10681da177e4SLinus Torvalds { 10691da177e4SLinus Torvalds memset(task, 0, sizeof(*task)); 107044c28873STrond Myklebust atomic_set(&task->tk_count, 1); 107184115e1cSTrond Myklebust task->tk_flags = task_setup_data->flags; 107284115e1cSTrond Myklebust task->tk_ops = task_setup_data->callback_ops; 107384115e1cSTrond Myklebust task->tk_calldata = task_setup_data->callback_data; 10746529eba0STrond Myklebust INIT_LIST_HEAD(&task->tk_task); 10751da177e4SLinus Torvalds 10763ff7576dSTrond Myklebust task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW; 10773ff7576dSTrond Myklebust task->tk_owner = current->tgid; 10781da177e4SLinus Torvalds 10791da177e4SLinus Torvalds /* Initialize workqueue for async tasks */ 108032bfb5c0STrond Myklebust task->tk_workqueue = task_setup_data->workqueue; 10811da177e4SLinus Torvalds 10829d61498dSTrond Myklebust task->tk_xprt = xprt_get(task_setup_data->rpc_xprt); 10839d61498dSTrond Myklebust 10841de7eea9SNeilBrown task->tk_op_cred = get_rpccred(task_setup_data->rpc_op_cred); 10851de7eea9SNeilBrown 108684115e1cSTrond Myklebust if (task->tk_ops->rpc_call_prepare != NULL) 108784115e1cSTrond Myklebust task->tk_action = rpc_prepare_task; 1088963d8fe5STrond Myklebust 10897fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 1090ef759a2eSChuck Lever 109146121cf7SChuck Lever dprintk("RPC: new task initialized, procpid %u\n", 1092ba25f9dcSPavel Emelyanov task_pid_nr(current)); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds static struct rpc_task * 10961da177e4SLinus Torvalds rpc_alloc_task(void) 10971da177e4SLinus Torvalds { 109812a3ad61STrond Myklebust return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS); 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds /* 110290c5755fSTrond Myklebust * Create a new task for the specified client. 11031da177e4SLinus Torvalds */ 110484115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data) 11051da177e4SLinus Torvalds { 1106e8f5d77cSTrond Myklebust struct rpc_task *task = setup_data->task; 1107e8f5d77cSTrond Myklebust unsigned short flags = 0; 11081da177e4SLinus Torvalds 1109e8f5d77cSTrond Myklebust if (task == NULL) { 11101da177e4SLinus Torvalds task = rpc_alloc_task(); 1111e8f5d77cSTrond Myklebust flags = RPC_TASK_DYNAMIC; 1112e8f5d77cSTrond Myklebust } 11131da177e4SLinus Torvalds 111484115e1cSTrond Myklebust rpc_init_task(task, setup_data); 1115e8f5d77cSTrond Myklebust task->tk_flags |= flags; 111646121cf7SChuck Lever dprintk("RPC: allocated task %p\n", task); 11171da177e4SLinus Torvalds return task; 11181da177e4SLinus Torvalds } 11191da177e4SLinus Torvalds 1120c6567ed1STrond Myklebust /* 1121c6567ed1STrond Myklebust * rpc_free_task - release rpc task and perform cleanups 1122c6567ed1STrond Myklebust * 1123c6567ed1STrond Myklebust * Note that we free up the rpc_task _after_ rpc_release_calldata() 1124c6567ed1STrond Myklebust * in order to work around a workqueue dependency issue. 1125c6567ed1STrond Myklebust * 1126c6567ed1STrond Myklebust * Tejun Heo states: 1127c6567ed1STrond Myklebust * "Workqueue currently considers two work items to be the same if they're 1128c6567ed1STrond Myklebust * on the same address and won't execute them concurrently - ie. it 1129c6567ed1STrond Myklebust * makes a work item which is queued again while being executed wait 1130c6567ed1STrond Myklebust * for the previous execution to complete. 1131c6567ed1STrond Myklebust * 1132c6567ed1STrond Myklebust * If a work function frees the work item, and then waits for an event 1133c6567ed1STrond Myklebust * which should be performed by another work item and *that* work item 1134c6567ed1STrond Myklebust * recycles the freed work item, it can create a false dependency loop. 1135c6567ed1STrond Myklebust * There really is no reliable way to detect this short of verifying 1136c6567ed1STrond Myklebust * every memory free." 1137c6567ed1STrond Myklebust * 1138c6567ed1STrond Myklebust */ 113932bfb5c0STrond Myklebust static void rpc_free_task(struct rpc_task *task) 11401da177e4SLinus Torvalds { 1141c6567ed1STrond Myklebust unsigned short tk_flags = task->tk_flags; 11421da177e4SLinus Torvalds 11431de7eea9SNeilBrown put_rpccred(task->tk_op_cred); 1144c6567ed1STrond Myklebust rpc_release_calldata(task->tk_ops, task->tk_calldata); 1145c6567ed1STrond Myklebust 1146c6567ed1STrond Myklebust if (tk_flags & RPC_TASK_DYNAMIC) { 11475e4424afSTrond Myklebust dprintk("RPC: %5u freeing task\n", task->tk_pid); 11485e4424afSTrond Myklebust mempool_free(task, rpc_task_mempool); 11495e4424afSTrond Myklebust } 115032bfb5c0STrond Myklebust } 115132bfb5c0STrond Myklebust 115232bfb5c0STrond Myklebust static void rpc_async_release(struct work_struct *work) 115332bfb5c0STrond Myklebust { 1154a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1155a1231fdaSTrond Myklebust 115632bfb5c0STrond Myklebust rpc_free_task(container_of(work, struct rpc_task, u.tk_work)); 1157a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 115832bfb5c0STrond Myklebust } 115932bfb5c0STrond Myklebust 1160bf294b41STrond Myklebust static void rpc_release_resources_task(struct rpc_task *task) 116132bfb5c0STrond Myklebust { 1162e6b3c4dbSTrond Myklebust xprt_release(task); 1163a271c5a0SOGAWA Hirofumi if (task->tk_msg.rpc_cred) { 1164a52458b4SNeilBrown put_cred(task->tk_msg.rpc_cred); 1165a271c5a0SOGAWA Hirofumi task->tk_msg.rpc_cred = NULL; 1166a271c5a0SOGAWA Hirofumi } 116758f9612cSTrond Myklebust rpc_task_release_client(task); 1168bf294b41STrond Myklebust } 1169bf294b41STrond Myklebust 1170bf294b41STrond Myklebust static void rpc_final_put_task(struct rpc_task *task, 1171bf294b41STrond Myklebust struct workqueue_struct *q) 1172bf294b41STrond Myklebust { 1173bf294b41STrond Myklebust if (q != NULL) { 117432bfb5c0STrond Myklebust INIT_WORK(&task->u.tk_work, rpc_async_release); 1175bf294b41STrond Myklebust queue_work(q, &task->u.tk_work); 117632bfb5c0STrond Myklebust } else 117732bfb5c0STrond Myklebust rpc_free_task(task); 1178e6b3c4dbSTrond Myklebust } 1179bf294b41STrond Myklebust 1180bf294b41STrond Myklebust static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q) 1181bf294b41STrond Myklebust { 1182bf294b41STrond Myklebust if (atomic_dec_and_test(&task->tk_count)) { 1183bf294b41STrond Myklebust rpc_release_resources_task(task); 1184bf294b41STrond Myklebust rpc_final_put_task(task, q); 1185bf294b41STrond Myklebust } 1186bf294b41STrond Myklebust } 1187bf294b41STrond Myklebust 1188bf294b41STrond Myklebust void rpc_put_task(struct rpc_task *task) 1189bf294b41STrond Myklebust { 1190bf294b41STrond Myklebust rpc_do_put_task(task, NULL); 1191bf294b41STrond Myklebust } 1192e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task); 1193e6b3c4dbSTrond Myklebust 1194bf294b41STrond Myklebust void rpc_put_task_async(struct rpc_task *task) 1195bf294b41STrond Myklebust { 1196bf294b41STrond Myklebust rpc_do_put_task(task, task->tk_workqueue); 1197bf294b41STrond Myklebust } 1198bf294b41STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task_async); 1199bf294b41STrond Myklebust 1200bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task) 1201e6b3c4dbSTrond Myklebust { 120246121cf7SChuck Lever dprintk("RPC: %5u release task\n", task->tk_pid); 12031da177e4SLinus Torvalds 12040a0c2a57SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 12051da177e4SLinus Torvalds 1206bf294b41STrond Myklebust rpc_release_resources_task(task); 1207e6b3c4dbSTrond Myklebust 1208bf294b41STrond Myklebust /* 1209bf294b41STrond Myklebust * Note: at this point we have been removed from rpc_clnt->cl_tasks, 1210bf294b41STrond Myklebust * so it should be safe to use task->tk_count as a test for whether 1211bf294b41STrond Myklebust * or not any other processes still hold references to our rpc_task. 1212bf294b41STrond Myklebust */ 1213bf294b41STrond Myklebust if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) { 1214bf294b41STrond Myklebust /* Wake up anyone who may be waiting for task completion */ 1215bf294b41STrond Myklebust if (!rpc_complete_task(task)) 1216bf294b41STrond Myklebust return; 1217bf294b41STrond Myklebust } else { 1218bf294b41STrond Myklebust if (!atomic_dec_and_test(&task->tk_count)) 1219bf294b41STrond Myklebust return; 1220bf294b41STrond Myklebust } 1221bf294b41STrond Myklebust rpc_final_put_task(task, task->tk_workqueue); 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 1224b247bbf1STrond Myklebust int rpciod_up(void) 1225b247bbf1STrond Myklebust { 1226b247bbf1STrond Myklebust return try_module_get(THIS_MODULE) ? 0 : -EINVAL; 1227b247bbf1STrond Myklebust } 1228b247bbf1STrond Myklebust 1229b247bbf1STrond Myklebust void rpciod_down(void) 1230b247bbf1STrond Myklebust { 1231b247bbf1STrond Myklebust module_put(THIS_MODULE); 1232b247bbf1STrond Myklebust } 1233b247bbf1STrond Myklebust 12341da177e4SLinus Torvalds /* 1235b247bbf1STrond Myklebust * Start up the rpciod workqueue. 12361da177e4SLinus Torvalds */ 1237b247bbf1STrond Myklebust static int rpciod_start(void) 12381da177e4SLinus Torvalds { 12391da177e4SLinus Torvalds struct workqueue_struct *wq; 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds /* 12421da177e4SLinus Torvalds * Create the rpciod thread and wait for it to start. 12431da177e4SLinus Torvalds */ 1244ab418d70STrond Myklebust dprintk("RPC: creating workqueue rpciod\n"); 1245f515f86bSOlga Kornievskaia wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); 124640a5f1b1STrond Myklebust if (!wq) 124740a5f1b1STrond Myklebust goto out_failed; 12481da177e4SLinus Torvalds rpciod_workqueue = wq; 124940a5f1b1STrond Myklebust /* Note: highpri because network receive is latency sensitive */ 125090ea9f1bSTrond Myklebust wq = alloc_workqueue("xprtiod", WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_HIGHPRI, 0); 125140a5f1b1STrond Myklebust if (!wq) 125240a5f1b1STrond Myklebust goto free_rpciod; 125340a5f1b1STrond Myklebust xprtiod_workqueue = wq; 125440a5f1b1STrond Myklebust return 1; 125540a5f1b1STrond Myklebust free_rpciod: 125640a5f1b1STrond Myklebust wq = rpciod_workqueue; 125740a5f1b1STrond Myklebust rpciod_workqueue = NULL; 125840a5f1b1STrond Myklebust destroy_workqueue(wq); 125940a5f1b1STrond Myklebust out_failed: 126040a5f1b1STrond Myklebust return 0; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 1263b247bbf1STrond Myklebust static void rpciod_stop(void) 12641da177e4SLinus Torvalds { 1265b247bbf1STrond Myklebust struct workqueue_struct *wq = NULL; 1266ab418d70STrond Myklebust 1267b247bbf1STrond Myklebust if (rpciod_workqueue == NULL) 1268b247bbf1STrond Myklebust return; 1269ab418d70STrond Myklebust dprintk("RPC: destroying workqueue rpciod\n"); 12701da177e4SLinus Torvalds 1271b247bbf1STrond Myklebust wq = rpciod_workqueue; 12721da177e4SLinus Torvalds rpciod_workqueue = NULL; 1273b247bbf1STrond Myklebust destroy_workqueue(wq); 127440a5f1b1STrond Myklebust wq = xprtiod_workqueue; 127540a5f1b1STrond Myklebust xprtiod_workqueue = NULL; 127640a5f1b1STrond Myklebust destroy_workqueue(wq); 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds void 12801da177e4SLinus Torvalds rpc_destroy_mempool(void) 12811da177e4SLinus Torvalds { 1282b247bbf1STrond Myklebust rpciod_stop(); 12831da177e4SLinus Torvalds mempool_destroy(rpc_buffer_mempool); 12841da177e4SLinus Torvalds mempool_destroy(rpc_task_mempool); 12851a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_task_slabp); 12861a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_buffer_slabp); 1287f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&delay_queue); 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds int 12911da177e4SLinus Torvalds rpc_init_mempool(void) 12921da177e4SLinus Torvalds { 1293f6a1cc89STrond Myklebust /* 1294f6a1cc89STrond Myklebust * The following is not strictly a mempool initialisation, 1295f6a1cc89STrond Myklebust * but there is no harm in doing it here 1296f6a1cc89STrond Myklebust */ 1297f6a1cc89STrond Myklebust rpc_init_wait_queue(&delay_queue, "delayq"); 1298f6a1cc89STrond Myklebust if (!rpciod_start()) 1299f6a1cc89STrond Myklebust goto err_nomem; 1300f6a1cc89STrond Myklebust 13011da177e4SLinus Torvalds rpc_task_slabp = kmem_cache_create("rpc_tasks", 13021da177e4SLinus Torvalds sizeof(struct rpc_task), 13031da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 130420c2df83SPaul Mundt NULL); 13051da177e4SLinus Torvalds if (!rpc_task_slabp) 13061da177e4SLinus Torvalds goto err_nomem; 13071da177e4SLinus Torvalds rpc_buffer_slabp = kmem_cache_create("rpc_buffers", 13081da177e4SLinus Torvalds RPC_BUFFER_MAXSIZE, 13091da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 131020c2df83SPaul Mundt NULL); 13111da177e4SLinus Torvalds if (!rpc_buffer_slabp) 13121da177e4SLinus Torvalds goto err_nomem; 131393d2341cSMatthew Dobson rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE, 13141da177e4SLinus Torvalds rpc_task_slabp); 13151da177e4SLinus Torvalds if (!rpc_task_mempool) 13161da177e4SLinus Torvalds goto err_nomem; 131793d2341cSMatthew Dobson rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE, 13181da177e4SLinus Torvalds rpc_buffer_slabp); 13191da177e4SLinus Torvalds if (!rpc_buffer_mempool) 13201da177e4SLinus Torvalds goto err_nomem; 13211da177e4SLinus Torvalds return 0; 13221da177e4SLinus Torvalds err_nomem: 13231da177e4SLinus Torvalds rpc_destroy_mempool(); 13241da177e4SLinus Torvalds return -ENOMEM; 13251da177e4SLinus Torvalds } 1326