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> 269dfe52a9SDave Wysochanski #include <linux/sunrpc/metrics.h> 271da177e4SLinus Torvalds 286951867bSBenny Halevy #include "sunrpc.h" 296951867bSBenny Halevy 30f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) 311da177e4SLinus Torvalds #define RPCDBG_FACILITY RPCDBG_SCHED 321da177e4SLinus Torvalds #endif 331da177e4SLinus Torvalds 3482b0a4c3STrond Myklebust #define CREATE_TRACE_POINTS 3582b0a4c3STrond Myklebust #include <trace/events/sunrpc.h> 3682b0a4c3STrond Myklebust 371da177e4SLinus Torvalds /* 381da177e4SLinus Torvalds * RPC slabs and memory pools 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds #define RPC_BUFFER_MAXSIZE (2048) 411da177e4SLinus Torvalds #define RPC_BUFFER_POOLSIZE (8) 421da177e4SLinus Torvalds #define RPC_TASK_POOLSIZE (8) 43e18b890bSChristoph Lameter static struct kmem_cache *rpc_task_slabp __read_mostly; 44e18b890bSChristoph Lameter static struct kmem_cache *rpc_buffer_slabp __read_mostly; 45ba89966cSEric Dumazet static mempool_t *rpc_task_mempool __read_mostly; 46ba89966cSEric Dumazet static mempool_t *rpc_buffer_mempool __read_mostly; 471da177e4SLinus Torvalds 4865f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *); 49bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task); 507e0a0e38STrond Myklebust static void __rpc_queue_timer_fn(struct work_struct *); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* 531da177e4SLinus Torvalds * RPC tasks sit here while waiting for conditions to improve. 541da177e4SLinus Torvalds */ 55a4a87499STrond Myklebust static struct rpc_wait_queue delay_queue; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* 581da177e4SLinus Torvalds * rpciod-related stuff 591da177e4SLinus Torvalds */ 6040a5f1b1STrond Myklebust struct workqueue_struct *rpciod_workqueue __read_mostly; 6140a5f1b1STrond Myklebust struct workqueue_struct *xprtiod_workqueue __read_mostly; 62675dd90aSChuck Lever EXPORT_SYMBOL_GPL(xprtiod_workqueue); 631da177e4SLinus Torvalds 645efd1876STrond Myklebust unsigned long 655efd1876STrond Myklebust rpc_task_timeout(const struct rpc_task *task) 665efd1876STrond Myklebust { 675efd1876STrond Myklebust unsigned long timeout = READ_ONCE(task->tk_timeout); 685efd1876STrond Myklebust 695efd1876STrond Myklebust if (timeout != 0) { 705efd1876STrond Myklebust unsigned long now = jiffies; 715efd1876STrond Myklebust if (time_before(now, timeout)) 725efd1876STrond Myklebust return timeout - now; 735efd1876STrond Myklebust } 745efd1876STrond Myklebust return 0; 755efd1876STrond Myklebust } 765efd1876STrond Myklebust EXPORT_SYMBOL_GPL(rpc_task_timeout); 775efd1876STrond Myklebust 781da177e4SLinus Torvalds /* 791da177e4SLinus Torvalds * Disable the timer for a given RPC task. Should be called with 801da177e4SLinus Torvalds * queue->lock and bh_disabled in order to avoid races within 811da177e4SLinus Torvalds * rpc_run_timer(). 821da177e4SLinus Torvalds */ 835d00837bSTrond Myklebust static void 84eb276c0eSTrond Myklebust __rpc_disable_timer(struct rpc_wait_queue *queue, struct rpc_task *task) 851da177e4SLinus Torvalds { 866b2e6856STrond Myklebust if (list_empty(&task->u.tk_wait.timer_list)) 8736df9aaeSTrond Myklebust return; 8846121cf7SChuck Lever dprintk("RPC: %5u disabling timer\n", task->tk_pid); 891da177e4SLinus Torvalds task->tk_timeout = 0; 9036df9aaeSTrond Myklebust list_del(&task->u.tk_wait.timer_list); 91eb276c0eSTrond Myklebust if (list_empty(&queue->timer_list.list)) 927e0a0e38STrond Myklebust cancel_delayed_work(&queue->timer_list.dwork); 9336df9aaeSTrond Myklebust } 9436df9aaeSTrond Myklebust 9536df9aaeSTrond Myklebust static void 9636df9aaeSTrond Myklebust rpc_set_queue_timer(struct rpc_wait_queue *queue, unsigned long expires) 9736df9aaeSTrond Myklebust { 987e0a0e38STrond Myklebust unsigned long now = jiffies; 997e0a0e38STrond Myklebust queue->timer_list.expires = expires; 1007e0a0e38STrond Myklebust if (time_before_eq(expires, now)) 1017e0a0e38STrond Myklebust expires = 0; 1027e0a0e38STrond Myklebust else 1037e0a0e38STrond Myklebust expires -= now; 1047e0a0e38STrond Myklebust mod_delayed_work(rpciod_workqueue, &queue->timer_list.dwork, expires); 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Set up a timer for the current task. 1091da177e4SLinus Torvalds */ 1105d00837bSTrond Myklebust static void 1116b2e6856STrond Myklebust __rpc_add_timer(struct rpc_wait_queue *queue, struct rpc_task *task, 1126b2e6856STrond Myklebust unsigned long timeout) 1131da177e4SLinus Torvalds { 11455cc1d78SNicholas Mc Guire dprintk("RPC: %5u setting alarm for %u ms\n", 1156b2e6856STrond Myklebust task->tk_pid, jiffies_to_msecs(timeout - jiffies)); 1161da177e4SLinus Torvalds 1176b2e6856STrond Myklebust task->tk_timeout = timeout; 1187e0a0e38STrond Myklebust if (list_empty(&queue->timer_list.list) || time_before(timeout, queue->timer_list.expires)) 1196b2e6856STrond Myklebust rpc_set_queue_timer(queue, timeout); 120eb276c0eSTrond Myklebust list_add(&task->u.tk_wait.timer_list, &queue->timer_list.list); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 123c05eecf6STrond Myklebust static void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority) 124c05eecf6STrond Myklebust { 125edd2e36fSTrond Myklebust if (queue->priority != priority) { 126c05eecf6STrond Myklebust queue->priority = priority; 127f42f7c28STrond Myklebust queue->nr = 1U << priority; 128c05eecf6STrond Myklebust } 129edd2e36fSTrond Myklebust } 130c05eecf6STrond Myklebust 131c05eecf6STrond Myklebust static void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue) 132c05eecf6STrond Myklebust { 133c05eecf6STrond Myklebust rpc_set_waitqueue_priority(queue, queue->maxpriority); 134f42f7c28STrond Myklebust } 135f42f7c28STrond Myklebust 136f42f7c28STrond Myklebust /* 137f42f7c28STrond Myklebust * Add a request to a queue list 138f42f7c28STrond Myklebust */ 139f42f7c28STrond Myklebust static void 140f42f7c28STrond Myklebust __rpc_list_enqueue_task(struct list_head *q, struct rpc_task *task) 141f42f7c28STrond Myklebust { 142f42f7c28STrond Myklebust struct rpc_task *t; 143f42f7c28STrond Myklebust 144f42f7c28STrond Myklebust list_for_each_entry(t, q, u.tk_wait.list) { 145f42f7c28STrond Myklebust if (t->tk_owner == task->tk_owner) { 146f42f7c28STrond Myklebust list_add_tail(&task->u.tk_wait.links, 147f42f7c28STrond Myklebust &t->u.tk_wait.links); 148f42f7c28STrond Myklebust /* Cache the queue head in task->u.tk_wait.list */ 149f42f7c28STrond Myklebust task->u.tk_wait.list.next = q; 150f42f7c28STrond Myklebust task->u.tk_wait.list.prev = NULL; 151f42f7c28STrond Myklebust return; 152f42f7c28STrond Myklebust } 153f42f7c28STrond Myklebust } 154f42f7c28STrond Myklebust INIT_LIST_HEAD(&task->u.tk_wait.links); 155f42f7c28STrond Myklebust list_add_tail(&task->u.tk_wait.list, q); 156f42f7c28STrond Myklebust } 157f42f7c28STrond Myklebust 158f42f7c28STrond Myklebust /* 159f42f7c28STrond Myklebust * Remove request from a queue list 160f42f7c28STrond Myklebust */ 161f42f7c28STrond Myklebust static void 162f42f7c28STrond Myklebust __rpc_list_dequeue_task(struct rpc_task *task) 163f42f7c28STrond Myklebust { 164f42f7c28STrond Myklebust struct list_head *q; 165f42f7c28STrond Myklebust struct rpc_task *t; 166f42f7c28STrond Myklebust 167f42f7c28STrond Myklebust if (task->u.tk_wait.list.prev == NULL) { 168f42f7c28STrond Myklebust list_del(&task->u.tk_wait.links); 169f42f7c28STrond Myklebust return; 170f42f7c28STrond Myklebust } 171f42f7c28STrond Myklebust if (!list_empty(&task->u.tk_wait.links)) { 172f42f7c28STrond Myklebust t = list_first_entry(&task->u.tk_wait.links, 173f42f7c28STrond Myklebust struct rpc_task, 174f42f7c28STrond Myklebust u.tk_wait.links); 175f42f7c28STrond Myklebust /* Assume __rpc_list_enqueue_task() cached the queue head */ 176f42f7c28STrond Myklebust q = t->u.tk_wait.list.next; 177f42f7c28STrond Myklebust list_add_tail(&t->u.tk_wait.list, q); 178f42f7c28STrond Myklebust list_del(&task->u.tk_wait.links); 179f42f7c28STrond Myklebust } 180f42f7c28STrond Myklebust list_del(&task->u.tk_wait.list); 181c05eecf6STrond Myklebust } 182c05eecf6STrond Myklebust 1831da177e4SLinus Torvalds /* 1841da177e4SLinus Torvalds * Add new request to a priority queue. 1851da177e4SLinus Torvalds */ 1863b27bad7STrond Myklebust static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, 1873b27bad7STrond Myklebust struct rpc_task *task, 1883b27bad7STrond Myklebust unsigned char queue_priority) 1891da177e4SLinus Torvalds { 1903b27bad7STrond Myklebust if (unlikely(queue_priority > queue->maxpriority)) 191c05eecf6STrond Myklebust queue_priority = queue->maxpriority; 192f42f7c28STrond Myklebust __rpc_list_enqueue_task(&queue->tasks[queue_priority], task); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* 1961da177e4SLinus Torvalds * Add new request to wait queue. 1971da177e4SLinus Torvalds * 1981da177e4SLinus Torvalds * Swapper tasks always get inserted at the head of the queue. 1991da177e4SLinus Torvalds * This should avoid many nasty memory deadlocks and hopefully 2001da177e4SLinus Torvalds * improve overall performance. 2011da177e4SLinus Torvalds * Everyone else gets appended to the queue to ensure proper FIFO behavior. 2021da177e4SLinus Torvalds */ 2033b27bad7STrond Myklebust static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, 2043b27bad7STrond Myklebust struct rpc_task *task, 2053b27bad7STrond Myklebust unsigned char queue_priority) 2061da177e4SLinus Torvalds { 2072bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 2082bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 2092bd4eef8SWeston Andros Adamson return; 2101da177e4SLinus Torvalds 2116b2e6856STrond Myklebust INIT_LIST_HEAD(&task->u.tk_wait.timer_list); 2121da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 2133b27bad7STrond Myklebust __rpc_add_wait_queue_priority(queue, task, queue_priority); 2141da177e4SLinus Torvalds else if (RPC_IS_SWAPPER(task)) 2151da177e4SLinus Torvalds list_add(&task->u.tk_wait.list, &queue->tasks[0]); 2161da177e4SLinus Torvalds else 2171da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]); 21896ef13b2STrond Myklebust task->tk_waitqueue = queue; 219e19b63daSChuck Lever queue->qlen++; 2201166fde6STrond Myklebust /* barrier matches the read in rpc_wake_up_task_queue_locked() */ 2211166fde6STrond Myklebust smp_wmb(); 2221da177e4SLinus Torvalds rpc_set_queued(task); 2231da177e4SLinus Torvalds 22446121cf7SChuck Lever dprintk("RPC: %5u added to queue %p \"%s\"\n", 2251da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * Remove request from a priority queue. 2301da177e4SLinus Torvalds */ 2311da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task) 2321da177e4SLinus Torvalds { 233f42f7c28STrond Myklebust __rpc_list_dequeue_task(task); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds /* 2371da177e4SLinus Torvalds * Remove request from queue. 2381da177e4SLinus Torvalds * Note: must be called with spin lock held. 2391da177e4SLinus Torvalds */ 24096ef13b2STrond Myklebust static void __rpc_remove_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task) 2411da177e4SLinus Torvalds { 242eb276c0eSTrond Myklebust __rpc_disable_timer(queue, task); 2431da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 2441da177e4SLinus Torvalds __rpc_remove_wait_queue_priority(task); 245f42f7c28STrond Myklebust else 2461da177e4SLinus Torvalds list_del(&task->u.tk_wait.list); 247e19b63daSChuck Lever queue->qlen--; 24846121cf7SChuck Lever dprintk("RPC: %5u removed from queue %p \"%s\"\n", 2491da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2523ff7576dSTrond Myklebust static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, unsigned char nr_queues) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds int i; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds spin_lock_init(&queue->lock); 2571da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(queue->tasks); i++) 2581da177e4SLinus Torvalds INIT_LIST_HEAD(&queue->tasks[i]); 2593ff7576dSTrond Myklebust queue->maxpriority = nr_queues - 1; 2601da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 26136df9aaeSTrond Myklebust queue->qlen = 0; 2627e0a0e38STrond Myklebust queue->timer_list.expires = 0; 2637e0a0e38STrond Myklebust INIT_DEFERRABLE_WORK(&queue->timer_list.dwork, __rpc_queue_timer_fn); 26436df9aaeSTrond Myklebust INIT_LIST_HEAD(&queue->timer_list.list); 2652f09c242STrond Myklebust rpc_assign_waitqueue_name(queue, qname); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2691da177e4SLinus Torvalds { 2703ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, RPC_NR_PRIORITY); 2711da177e4SLinus Torvalds } 272689cf5c1SAlexandros Batsakis EXPORT_SYMBOL_GPL(rpc_init_priority_wait_queue); 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2751da177e4SLinus Torvalds { 2763ff7576dSTrond Myklebust __rpc_init_priority_wait_queue(queue, qname, 1); 2771da177e4SLinus Torvalds } 278e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_init_wait_queue); 2791da177e4SLinus Torvalds 280f6a1cc89STrond Myklebust void rpc_destroy_wait_queue(struct rpc_wait_queue *queue) 281f6a1cc89STrond Myklebust { 2827e0a0e38STrond Myklebust cancel_delayed_work_sync(&queue->timer_list.dwork); 283f6a1cc89STrond Myklebust } 284f6a1cc89STrond Myklebust EXPORT_SYMBOL_GPL(rpc_destroy_wait_queue); 285f6a1cc89STrond Myklebust 286dfd01f02SPeter Zijlstra static int rpc_wait_bit_killable(struct wait_bit_key *key, int mode) 28744c28873STrond Myklebust { 288416ad3c9SColin Cross freezable_schedule_unsafe(); 289dfd01f02SPeter Zijlstra if (signal_pending_state(mode, current)) 290dfd01f02SPeter Zijlstra return -ERESTARTSYS; 29144c28873STrond Myklebust return 0; 29244c28873STrond Myklebust } 29344c28873STrond Myklebust 2941306729bSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) || IS_ENABLED(CONFIG_TRACEPOINTS) 295c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task) 296c44fe705STrond Myklebust { 297c44fe705STrond Myklebust static atomic_t rpc_pid; 298c44fe705STrond Myklebust 299c44fe705STrond Myklebust task->tk_pid = atomic_inc_return(&rpc_pid); 300c44fe705STrond Myklebust } 301c44fe705STrond Myklebust #else 302c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task) 303c44fe705STrond Myklebust { 304c44fe705STrond Myklebust } 305c44fe705STrond Myklebust #endif 306c44fe705STrond Myklebust 307e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task) 308e6b3c4dbSTrond Myklebust { 309c44fe705STrond Myklebust rpc_task_set_debuginfo(task); 31058f9612cSTrond Myklebust set_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 311e671edb9SChuck Lever trace_rpc_task_begin(task, NULL); 312e6b3c4dbSTrond Myklebust } 313e6b3c4dbSTrond Myklebust 31444c28873STrond Myklebust /* 31544c28873STrond Myklebust * Mark an RPC call as having completed by clearing the 'active' bit 316bf294b41STrond Myklebust * and then waking up all tasks that were sleeping. 31744c28873STrond Myklebust */ 318bf294b41STrond Myklebust static int rpc_complete_task(struct rpc_task *task) 31944c28873STrond Myklebust { 320bf294b41STrond Myklebust void *m = &task->tk_runstate; 321bf294b41STrond Myklebust wait_queue_head_t *wq = bit_waitqueue(m, RPC_TASK_ACTIVE); 322bf294b41STrond Myklebust struct wait_bit_key k = __WAIT_BIT_KEY_INITIALIZER(m, RPC_TASK_ACTIVE); 323bf294b41STrond Myklebust unsigned long flags; 324bf294b41STrond Myklebust int ret; 325bf294b41STrond Myklebust 326e671edb9SChuck Lever trace_rpc_task_complete(task, NULL); 32782b0a4c3STrond Myklebust 328bf294b41STrond Myklebust spin_lock_irqsave(&wq->lock, flags); 329e6b3c4dbSTrond Myklebust clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 330bf294b41STrond Myklebust ret = atomic_dec_and_test(&task->tk_count); 331bf294b41STrond Myklebust if (waitqueue_active(wq)) 332ac5be6b4SAndrea Arcangeli __wake_up_locked_key(wq, TASK_NORMAL, &k); 333bf294b41STrond Myklebust spin_unlock_irqrestore(&wq->lock, flags); 334bf294b41STrond Myklebust return ret; 33544c28873STrond Myklebust } 33644c28873STrond Myklebust 33744c28873STrond Myklebust /* 33844c28873STrond Myklebust * Allow callers to wait for completion of an RPC call 339bf294b41STrond Myklebust * 340bf294b41STrond Myklebust * Note the use of out_of_line_wait_on_bit() rather than wait_on_bit() 341bf294b41STrond Myklebust * to enforce taking of the wq->lock and hence avoid races with 342bf294b41STrond Myklebust * rpc_complete_task(). 34344c28873STrond Myklebust */ 344c1221321SNeilBrown int __rpc_wait_for_completion_task(struct rpc_task *task, wait_bit_action_f *action) 34544c28873STrond Myklebust { 34644c28873STrond Myklebust if (action == NULL) 347150030b7SMatthew Wilcox action = rpc_wait_bit_killable; 348bf294b41STrond Myklebust return out_of_line_wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE, 349150030b7SMatthew Wilcox action, TASK_KILLABLE); 35044c28873STrond Myklebust } 351e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(__rpc_wait_for_completion_task); 35244c28873STrond Myklebust 3531da177e4SLinus Torvalds /* 3541da177e4SLinus Torvalds * Make an RPC task runnable. 3551da177e4SLinus Torvalds * 356506026c3SJeff Layton * Note: If the task is ASYNC, and is being made runnable after sitting on an 357506026c3SJeff Layton * rpc_wait_queue, this must be called with the queue spinlock held to protect 358506026c3SJeff Layton * the wait queue operation. 359a3c3cac5STrond Myklebust * Note the ordering of rpc_test_and_set_running() and rpc_clear_queued(), 360a3c3cac5STrond Myklebust * which is needed to ensure that __rpc_execute() doesn't loop (due to the 361a3c3cac5STrond Myklebust * lockless RPC_IS_QUEUED() test) before we've had a chance to test 362a3c3cac5STrond Myklebust * the RPC_TASK_RUNNING flag. 3631da177e4SLinus Torvalds */ 364f1dc237cSTrond Myklebust static void rpc_make_runnable(struct workqueue_struct *wq, 365f1dc237cSTrond Myklebust struct rpc_task *task) 3661da177e4SLinus Torvalds { 367a3c3cac5STrond Myklebust bool need_wakeup = !rpc_test_and_set_running(task); 368a3c3cac5STrond Myklebust 3691da177e4SLinus Torvalds rpc_clear_queued(task); 370a3c3cac5STrond Myklebust if (!need_wakeup) 3711da177e4SLinus Torvalds return; 3721da177e4SLinus Torvalds if (RPC_IS_ASYNC(task)) { 37365f27f38SDavid Howells INIT_WORK(&task->u.tk_work, rpc_async_schedule); 374f1dc237cSTrond Myklebust queue_work(wq, &task->u.tk_work); 3751da177e4SLinus Torvalds } else 37696651ab3STrond Myklebust wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED); 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds /* 3801da177e4SLinus Torvalds * Prepare for sleeping on a wait queue. 3811da177e4SLinus Torvalds * By always appending tasks to the list we ensure FIFO behavior. 3821da177e4SLinus Torvalds * NB: An RPC task will only receive interrupt-driven events as long 3831da177e4SLinus Torvalds * as it's on a wait queue. 3841da177e4SLinus Torvalds */ 3853b27bad7STrond Myklebust static void __rpc_sleep_on_priority(struct rpc_wait_queue *q, 3863b27bad7STrond Myklebust struct rpc_task *task, 3873b27bad7STrond Myklebust unsigned char queue_priority) 3881da177e4SLinus Torvalds { 38946121cf7SChuck Lever dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n", 39046121cf7SChuck Lever task->tk_pid, rpc_qname(q), jiffies); 3911da177e4SLinus Torvalds 392e671edb9SChuck Lever trace_rpc_task_sleep(task, q); 39382b0a4c3STrond Myklebust 3943b27bad7STrond Myklebust __rpc_add_wait_queue(q, task, queue_priority); 3951da177e4SLinus Torvalds 3966b2e6856STrond Myklebust } 3976b2e6856STrond Myklebust 3986b2e6856STrond Myklebust static void __rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q, 3996b2e6856STrond Myklebust struct rpc_task *task, unsigned long timeout, 4006b2e6856STrond Myklebust unsigned char queue_priority) 4016b2e6856STrond Myklebust { 4026b2e6856STrond Myklebust if (time_is_after_jiffies(timeout)) { 4036b2e6856STrond Myklebust __rpc_sleep_on_priority(q, task, queue_priority); 4046b2e6856STrond Myklebust __rpc_add_timer(q, task, timeout); 4056b2e6856STrond Myklebust } else 4066b2e6856STrond Myklebust task->tk_status = -ETIMEDOUT; 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 40987150aaeSTrond Myklebust static void rpc_set_tk_callback(struct rpc_task *task, rpc_action action) 41087150aaeSTrond Myklebust { 41187150aaeSTrond Myklebust if (action && !WARN_ON_ONCE(task->tk_callback != NULL)) 41287150aaeSTrond Myklebust task->tk_callback = action; 41387150aaeSTrond Myklebust } 41487150aaeSTrond Myklebust 41587150aaeSTrond Myklebust static bool rpc_sleep_check_activated(struct rpc_task *task) 41687150aaeSTrond Myklebust { 41787150aaeSTrond Myklebust /* We shouldn't ever put an inactive task to sleep */ 41887150aaeSTrond Myklebust if (WARN_ON_ONCE(!RPC_IS_ACTIVATED(task))) { 41987150aaeSTrond Myklebust task->tk_status = -EIO; 42087150aaeSTrond Myklebust rpc_put_task_async(task); 42187150aaeSTrond Myklebust return false; 42287150aaeSTrond Myklebust } 42387150aaeSTrond Myklebust return true; 42487150aaeSTrond Myklebust } 42587150aaeSTrond Myklebust 4266b2e6856STrond Myklebust void rpc_sleep_on_timeout(struct rpc_wait_queue *q, struct rpc_task *task, 4276b2e6856STrond Myklebust rpc_action action, unsigned long timeout) 4281da177e4SLinus Torvalds { 42987150aaeSTrond Myklebust if (!rpc_sleep_check_activated(task)) 430e454a7a8SWeston Andros Adamson return; 43187150aaeSTrond Myklebust 43287150aaeSTrond Myklebust rpc_set_tk_callback(task, action); 433e6b3c4dbSTrond Myklebust 4341da177e4SLinus Torvalds /* 4351da177e4SLinus Torvalds * Protect the queue operations. 4361da177e4SLinus Torvalds */ 437c049f8eaSTrond Myklebust spin_lock(&q->lock); 4386b2e6856STrond Myklebust __rpc_sleep_on_priority_timeout(q, task, timeout, task->tk_priority); 439c049f8eaSTrond Myklebust spin_unlock(&q->lock); 4406b2e6856STrond Myklebust } 4416b2e6856STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_timeout); 4426b2e6856STrond Myklebust 4436b2e6856STrond Myklebust void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, 4446b2e6856STrond Myklebust rpc_action action) 4456b2e6856STrond Myklebust { 4466b2e6856STrond Myklebust if (!rpc_sleep_check_activated(task)) 4476b2e6856STrond Myklebust return; 4486b2e6856STrond Myklebust 4496b2e6856STrond Myklebust rpc_set_tk_callback(task, action); 4506b2e6856STrond Myklebust 4516b2e6856STrond Myklebust WARN_ON_ONCE(task->tk_timeout != 0); 4526b2e6856STrond Myklebust /* 4536b2e6856STrond Myklebust * Protect the queue operations. 4546b2e6856STrond Myklebust */ 455c049f8eaSTrond Myklebust spin_lock(&q->lock); 45687150aaeSTrond Myklebust __rpc_sleep_on_priority(q, task, task->tk_priority); 457c049f8eaSTrond Myklebust spin_unlock(&q->lock); 4581da177e4SLinus Torvalds } 459e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on); 4601da177e4SLinus Torvalds 4616b2e6856STrond Myklebust void rpc_sleep_on_priority_timeout(struct rpc_wait_queue *q, 4626b2e6856STrond Myklebust struct rpc_task *task, unsigned long timeout, int priority) 4636b2e6856STrond Myklebust { 4646b2e6856STrond Myklebust if (!rpc_sleep_check_activated(task)) 4656b2e6856STrond Myklebust return; 4666b2e6856STrond Myklebust 4676b2e6856STrond Myklebust priority -= RPC_PRIORITY_LOW; 4686b2e6856STrond Myklebust /* 4696b2e6856STrond Myklebust * Protect the queue operations. 4706b2e6856STrond Myklebust */ 471c049f8eaSTrond Myklebust spin_lock(&q->lock); 4726b2e6856STrond Myklebust __rpc_sleep_on_priority_timeout(q, task, timeout, priority); 473c049f8eaSTrond Myklebust spin_unlock(&q->lock); 4746b2e6856STrond Myklebust } 4756b2e6856STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority_timeout); 4766b2e6856STrond Myklebust 4773b27bad7STrond Myklebust void rpc_sleep_on_priority(struct rpc_wait_queue *q, struct rpc_task *task, 4788357a9b6STrond Myklebust int priority) 4793b27bad7STrond Myklebust { 48087150aaeSTrond Myklebust if (!rpc_sleep_check_activated(task)) 481e454a7a8SWeston Andros Adamson return; 48287150aaeSTrond Myklebust 4836b2e6856STrond Myklebust WARN_ON_ONCE(task->tk_timeout != 0); 4848357a9b6STrond Myklebust priority -= RPC_PRIORITY_LOW; 4853b27bad7STrond Myklebust /* 4863b27bad7STrond Myklebust * Protect the queue operations. 4873b27bad7STrond Myklebust */ 488c049f8eaSTrond Myklebust spin_lock(&q->lock); 4898357a9b6STrond Myklebust __rpc_sleep_on_priority(q, task, priority); 490c049f8eaSTrond Myklebust spin_unlock(&q->lock); 4913b27bad7STrond Myklebust } 4921e1093c7STrond Myklebust EXPORT_SYMBOL_GPL(rpc_sleep_on_priority); 4933b27bad7STrond Myklebust 4941da177e4SLinus Torvalds /** 495f1dc237cSTrond Myklebust * __rpc_do_wake_up_task_on_wq - wake up a single rpc_task 496f1dc237cSTrond Myklebust * @wq: workqueue on which to run task 49796ef13b2STrond Myklebust * @queue: wait queue 4981da177e4SLinus Torvalds * @task: task to be woken up 4991da177e4SLinus Torvalds * 5001da177e4SLinus Torvalds * Caller must hold queue->lock, and have cleared the task queued flag. 5011da177e4SLinus Torvalds */ 502f1dc237cSTrond Myklebust static void __rpc_do_wake_up_task_on_wq(struct workqueue_struct *wq, 503f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 504f1dc237cSTrond Myklebust struct rpc_task *task) 5051da177e4SLinus Torvalds { 50646121cf7SChuck Lever dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n", 50746121cf7SChuck Lever task->tk_pid, jiffies); 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* Has the task been executed yet? If not, we cannot wake it up! */ 5101da177e4SLinus Torvalds if (!RPC_IS_ACTIVATED(task)) { 5111da177e4SLinus Torvalds printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task); 5121da177e4SLinus Torvalds return; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 515e671edb9SChuck Lever trace_rpc_task_wakeup(task, queue); 51682b0a4c3STrond Myklebust 51796ef13b2STrond Myklebust __rpc_remove_wait_queue(queue, task); 5181da177e4SLinus Torvalds 519f1dc237cSTrond Myklebust rpc_make_runnable(wq, task); 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds dprintk("RPC: __rpc_wake_up_task done\n"); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds /* 52596ef13b2STrond Myklebust * Wake up a queued task while the queue lock is being held 5261da177e4SLinus Torvalds */ 527359c48c0STrond Myklebust static struct rpc_task * 528359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(struct workqueue_struct *wq, 529359c48c0STrond Myklebust struct rpc_wait_queue *queue, struct rpc_task *task, 530359c48c0STrond Myklebust bool (*action)(struct rpc_task *, void *), void *data) 5311da177e4SLinus Torvalds { 5321166fde6STrond Myklebust if (RPC_IS_QUEUED(task)) { 5331166fde6STrond Myklebust smp_rmb(); 534359c48c0STrond Myklebust if (task->tk_waitqueue == queue) { 535359c48c0STrond Myklebust if (action == NULL || action(task, data)) { 536f1dc237cSTrond Myklebust __rpc_do_wake_up_task_on_wq(wq, queue, task); 537359c48c0STrond Myklebust return task; 5381da177e4SLinus Torvalds } 5391166fde6STrond Myklebust } 540359c48c0STrond Myklebust } 541359c48c0STrond Myklebust return NULL; 542359c48c0STrond Myklebust } 543359c48c0STrond Myklebust 5441da177e4SLinus Torvalds /* 545f1dc237cSTrond Myklebust * Wake up a queued task while the queue lock is being held 546f1dc237cSTrond Myklebust */ 547691b45ddSChuck Lever static void rpc_wake_up_task_queue_locked(struct rpc_wait_queue *queue, 5482275cde4STrond Myklebust struct rpc_task *task) 5492275cde4STrond Myklebust { 550691b45ddSChuck Lever rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue, 551691b45ddSChuck Lever task, NULL, NULL); 5522275cde4STrond Myklebust } 5532275cde4STrond Myklebust 5542275cde4STrond Myklebust /* 5552275cde4STrond Myklebust * Wake up a task on a specific queue 5562275cde4STrond Myklebust */ 55796ef13b2STrond Myklebust void rpc_wake_up_queued_task(struct rpc_wait_queue *queue, struct rpc_task *task) 55896ef13b2STrond Myklebust { 5595ce97039STrond Myklebust if (!RPC_IS_QUEUED(task)) 5605ce97039STrond Myklebust return; 561c049f8eaSTrond Myklebust spin_lock(&queue->lock); 56296ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 563c049f8eaSTrond Myklebust spin_unlock(&queue->lock); 56496ef13b2STrond Myklebust } 56596ef13b2STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_queued_task); 56696ef13b2STrond Myklebust 567359c48c0STrond Myklebust static bool rpc_task_action_set_status(struct rpc_task *task, void *status) 568359c48c0STrond Myklebust { 569359c48c0STrond Myklebust task->tk_status = *(int *)status; 570359c48c0STrond Myklebust return true; 571359c48c0STrond Myklebust } 572359c48c0STrond Myklebust 573359c48c0STrond Myklebust static void 574359c48c0STrond Myklebust rpc_wake_up_task_queue_set_status_locked(struct rpc_wait_queue *queue, 575359c48c0STrond Myklebust struct rpc_task *task, int status) 576359c48c0STrond Myklebust { 577359c48c0STrond Myklebust rpc_wake_up_task_on_wq_queue_action_locked(rpciod_workqueue, queue, 578359c48c0STrond Myklebust task, rpc_task_action_set_status, &status); 579359c48c0STrond Myklebust } 580359c48c0STrond Myklebust 581359c48c0STrond Myklebust /** 582359c48c0STrond Myklebust * rpc_wake_up_queued_task_set_status - wake up a task and set task->tk_status 583359c48c0STrond Myklebust * @queue: pointer to rpc_wait_queue 584359c48c0STrond Myklebust * @task: pointer to rpc_task 585359c48c0STrond Myklebust * @status: integer error value 586359c48c0STrond Myklebust * 587359c48c0STrond Myklebust * If @task is queued on @queue, then it is woken up, and @task->tk_status is 588359c48c0STrond Myklebust * set to the value of @status. 589359c48c0STrond Myklebust */ 590359c48c0STrond Myklebust void 591359c48c0STrond Myklebust rpc_wake_up_queued_task_set_status(struct rpc_wait_queue *queue, 592359c48c0STrond Myklebust struct rpc_task *task, int status) 593359c48c0STrond Myklebust { 594359c48c0STrond Myklebust if (!RPC_IS_QUEUED(task)) 595359c48c0STrond Myklebust return; 596c049f8eaSTrond Myklebust spin_lock(&queue->lock); 597359c48c0STrond Myklebust rpc_wake_up_task_queue_set_status_locked(queue, task, status); 598c049f8eaSTrond Myklebust spin_unlock(&queue->lock); 599359c48c0STrond Myklebust } 600359c48c0STrond Myklebust 60196ef13b2STrond Myklebust /* 6021da177e4SLinus Torvalds * Wake up the next task on a priority queue. 6031da177e4SLinus Torvalds */ 604961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued_priority(struct rpc_wait_queue *queue) 6051da177e4SLinus Torvalds { 6061da177e4SLinus Torvalds struct list_head *q; 6071da177e4SLinus Torvalds struct rpc_task *task; 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds /* 6103ff7576dSTrond Myklebust * Service a batch of tasks from a single owner. 6111da177e4SLinus Torvalds */ 6121da177e4SLinus Torvalds q = &queue->tasks[queue->priority]; 613f42f7c28STrond Myklebust if (!list_empty(q) && --queue->nr) { 614f42f7c28STrond Myklebust task = list_first_entry(q, struct rpc_task, u.tk_wait.list); 6151da177e4SLinus Torvalds goto out; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* 6191da177e4SLinus Torvalds * Service the next queue. 6201da177e4SLinus Torvalds */ 6211da177e4SLinus Torvalds do { 6221da177e4SLinus Torvalds if (q == &queue->tasks[0]) 6231da177e4SLinus Torvalds q = &queue->tasks[queue->maxpriority]; 6241da177e4SLinus Torvalds else 6251da177e4SLinus Torvalds q = q - 1; 6261da177e4SLinus Torvalds if (!list_empty(q)) { 627f42f7c28STrond Myklebust task = list_first_entry(q, struct rpc_task, u.tk_wait.list); 6281da177e4SLinus Torvalds goto new_queue; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds } while (q != &queue->tasks[queue->priority]); 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 6331da177e4SLinus Torvalds return NULL; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds new_queue: 6361da177e4SLinus Torvalds rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0])); 6371da177e4SLinus Torvalds out: 6381da177e4SLinus Torvalds return task; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 641961a828dSTrond Myklebust static struct rpc_task *__rpc_find_next_queued(struct rpc_wait_queue *queue) 642961a828dSTrond Myklebust { 643961a828dSTrond Myklebust if (RPC_IS_PRIORITY(queue)) 644961a828dSTrond Myklebust return __rpc_find_next_queued_priority(queue); 645961a828dSTrond Myklebust if (!list_empty(&queue->tasks[0])) 646961a828dSTrond Myklebust return list_first_entry(&queue->tasks[0], struct rpc_task, u.tk_wait.list); 647961a828dSTrond Myklebust return NULL; 648961a828dSTrond Myklebust } 649961a828dSTrond Myklebust 650961a828dSTrond Myklebust /* 651961a828dSTrond Myklebust * Wake up the first task on the wait queue. 652961a828dSTrond Myklebust */ 653f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first_on_wq(struct workqueue_struct *wq, 654f1dc237cSTrond Myklebust struct rpc_wait_queue *queue, 655961a828dSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 656961a828dSTrond Myklebust { 657961a828dSTrond Myklebust struct rpc_task *task = NULL; 658961a828dSTrond Myklebust 659961a828dSTrond Myklebust dprintk("RPC: wake_up_first(%p \"%s\")\n", 660961a828dSTrond Myklebust queue, rpc_qname(queue)); 661c049f8eaSTrond Myklebust spin_lock(&queue->lock); 662961a828dSTrond Myklebust task = __rpc_find_next_queued(queue); 663359c48c0STrond Myklebust if (task != NULL) 664359c48c0STrond Myklebust task = rpc_wake_up_task_on_wq_queue_action_locked(wq, queue, 665359c48c0STrond Myklebust task, func, data); 666c049f8eaSTrond Myklebust spin_unlock(&queue->lock); 667961a828dSTrond Myklebust 668961a828dSTrond Myklebust return task; 669961a828dSTrond Myklebust } 670f1dc237cSTrond Myklebust 671f1dc237cSTrond Myklebust /* 672f1dc237cSTrond Myklebust * Wake up the first task on the wait queue. 673f1dc237cSTrond Myklebust */ 674f1dc237cSTrond Myklebust struct rpc_task *rpc_wake_up_first(struct rpc_wait_queue *queue, 675f1dc237cSTrond Myklebust bool (*func)(struct rpc_task *, void *), void *data) 676f1dc237cSTrond Myklebust { 677f1dc237cSTrond Myklebust return rpc_wake_up_first_on_wq(rpciod_workqueue, queue, func, data); 678f1dc237cSTrond Myklebust } 679961a828dSTrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_first); 680961a828dSTrond Myklebust 681961a828dSTrond Myklebust static bool rpc_wake_up_next_func(struct rpc_task *task, void *data) 682961a828dSTrond Myklebust { 683961a828dSTrond Myklebust return true; 684961a828dSTrond Myklebust } 685961a828dSTrond Myklebust 6861da177e4SLinus Torvalds /* 6871da177e4SLinus Torvalds * Wake up the next task on the wait queue. 6881da177e4SLinus Torvalds */ 6891da177e4SLinus Torvalds struct rpc_task *rpc_wake_up_next(struct rpc_wait_queue *queue) 6901da177e4SLinus Torvalds { 691961a828dSTrond Myklebust return rpc_wake_up_first(queue, rpc_wake_up_next_func, NULL); 6921da177e4SLinus Torvalds } 693e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_next); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds /** 6961da177e4SLinus Torvalds * rpc_wake_up - wake up all rpc_tasks 6971da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 6981da177e4SLinus Torvalds * 6991da177e4SLinus Torvalds * Grabs queue->lock 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue) 7021da177e4SLinus Torvalds { 7031da177e4SLinus Torvalds struct list_head *head; 704e6d83d55STrond Myklebust 705c049f8eaSTrond Myklebust spin_lock(&queue->lock); 7061da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 7071da177e4SLinus Torvalds for (;;) { 708540a0f75STrond Myklebust while (!list_empty(head)) { 709540a0f75STrond Myklebust struct rpc_task *task; 710540a0f75STrond Myklebust task = list_first_entry(head, 711540a0f75STrond Myklebust struct rpc_task, 712540a0f75STrond Myklebust u.tk_wait.list); 71396ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 714540a0f75STrond Myklebust } 7151da177e4SLinus Torvalds if (head == &queue->tasks[0]) 7161da177e4SLinus Torvalds break; 7171da177e4SLinus Torvalds head--; 7181da177e4SLinus Torvalds } 719c049f8eaSTrond Myklebust spin_unlock(&queue->lock); 7201da177e4SLinus Torvalds } 721e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /** 7241da177e4SLinus Torvalds * rpc_wake_up_status - wake up all rpc_tasks and set their status value. 7251da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 7261da177e4SLinus Torvalds * @status: status value to set 7271da177e4SLinus Torvalds * 7281da177e4SLinus Torvalds * Grabs queue->lock 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds struct list_head *head; 7331da177e4SLinus Torvalds 734c049f8eaSTrond Myklebust spin_lock(&queue->lock); 7351da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 7361da177e4SLinus Torvalds for (;;) { 737540a0f75STrond Myklebust while (!list_empty(head)) { 738540a0f75STrond Myklebust struct rpc_task *task; 739540a0f75STrond Myklebust task = list_first_entry(head, 740540a0f75STrond Myklebust struct rpc_task, 741540a0f75STrond Myklebust u.tk_wait.list); 7421da177e4SLinus Torvalds task->tk_status = status; 74396ef13b2STrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds if (head == &queue->tasks[0]) 7461da177e4SLinus Torvalds break; 7471da177e4SLinus Torvalds head--; 7481da177e4SLinus Torvalds } 749c049f8eaSTrond Myklebust spin_unlock(&queue->lock); 7501da177e4SLinus Torvalds } 751e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_wake_up_status); 7521da177e4SLinus Torvalds 7537e0a0e38STrond Myklebust static void __rpc_queue_timer_fn(struct work_struct *work) 75436df9aaeSTrond Myklebust { 7557e0a0e38STrond Myklebust struct rpc_wait_queue *queue = container_of(work, 7567e0a0e38STrond Myklebust struct rpc_wait_queue, 7577e0a0e38STrond Myklebust timer_list.dwork.work); 75836df9aaeSTrond Myklebust struct rpc_task *task, *n; 75936df9aaeSTrond Myklebust unsigned long expires, now, timeo; 76036df9aaeSTrond Myklebust 76136df9aaeSTrond Myklebust spin_lock(&queue->lock); 76236df9aaeSTrond Myklebust expires = now = jiffies; 76336df9aaeSTrond Myklebust list_for_each_entry_safe(task, n, &queue->timer_list.list, u.tk_wait.timer_list) { 7646b2e6856STrond Myklebust timeo = task->tk_timeout; 76536df9aaeSTrond Myklebust if (time_after_eq(now, timeo)) { 76636df9aaeSTrond Myklebust dprintk("RPC: %5u timeout\n", task->tk_pid); 76736df9aaeSTrond Myklebust task->tk_status = -ETIMEDOUT; 76836df9aaeSTrond Myklebust rpc_wake_up_task_queue_locked(queue, task); 76936df9aaeSTrond Myklebust continue; 77036df9aaeSTrond Myklebust } 77136df9aaeSTrond Myklebust if (expires == now || time_after(expires, timeo)) 77236df9aaeSTrond Myklebust expires = timeo; 77336df9aaeSTrond Myklebust } 77436df9aaeSTrond Myklebust if (!list_empty(&queue->timer_list.list)) 77536df9aaeSTrond Myklebust rpc_set_queue_timer(queue, expires); 77636df9aaeSTrond Myklebust spin_unlock(&queue->lock); 77736df9aaeSTrond Myklebust } 77836df9aaeSTrond Myklebust 7798014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task) 7808014793bSTrond Myklebust { 7816bd14416STrond Myklebust if (task->tk_status == -ETIMEDOUT) 7825d00837bSTrond Myklebust task->tk_status = 0; 7838014793bSTrond Myklebust } 7848014793bSTrond Myklebust 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * Run a task at a later time 7871da177e4SLinus Torvalds */ 7888014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay) 7891da177e4SLinus Torvalds { 7906b2e6856STrond Myklebust rpc_sleep_on_timeout(&delay_queue, task, __rpc_atrun, jiffies + delay); 7911da177e4SLinus Torvalds } 792e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_delay); 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7954ce70adaSTrond Myklebust * Helper to call task->tk_ops->rpc_call_prepare 7964ce70adaSTrond Myklebust */ 797aae2006eSAndy Adamson void rpc_prepare_task(struct rpc_task *task) 7984ce70adaSTrond Myklebust { 7994ce70adaSTrond Myklebust task->tk_ops->rpc_call_prepare(task, task->tk_calldata); 8004ce70adaSTrond Myklebust } 8014ce70adaSTrond Myklebust 8027fdcf13bSTrond Myklebust static void 8037fdcf13bSTrond Myklebust rpc_init_task_statistics(struct rpc_task *task) 8047fdcf13bSTrond Myklebust { 8057fdcf13bSTrond Myklebust /* Initialize retry counters */ 8067fdcf13bSTrond Myklebust task->tk_garb_retry = 2; 8077fdcf13bSTrond Myklebust task->tk_cred_retry = 2; 8087fdcf13bSTrond Myklebust task->tk_rebind_retry = 2; 8097fdcf13bSTrond Myklebust 8107fdcf13bSTrond Myklebust /* starting timestamp */ 8117fdcf13bSTrond Myklebust task->tk_start = ktime_get(); 8127fdcf13bSTrond Myklebust } 8137fdcf13bSTrond Myklebust 8147fdcf13bSTrond Myklebust static void 8157fdcf13bSTrond Myklebust rpc_reset_task_statistics(struct rpc_task *task) 8167fdcf13bSTrond Myklebust { 8177fdcf13bSTrond Myklebust task->tk_timeouts = 0; 818ae67bd38STrond Myklebust task->tk_flags &= ~(RPC_CALL_MAJORSEEN|RPC_TASK_SENT); 8197fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 8207fdcf13bSTrond Myklebust } 8217fdcf13bSTrond Myklebust 8224ce70adaSTrond Myklebust /* 823963d8fe5STrond Myklebust * Helper that calls task->tk_ops->rpc_call_done if it exists 824d05fdb0cSTrond Myklebust */ 825abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task) 826d05fdb0cSTrond Myklebust { 827abbcf28fSTrond Myklebust task->tk_action = NULL; 8289dfe52a9SDave Wysochanski if (task->tk_ops->rpc_count_stats) 8299dfe52a9SDave Wysochanski task->tk_ops->rpc_count_stats(task, task->tk_calldata); 8309dfe52a9SDave Wysochanski else if (task->tk_client) 8319dfe52a9SDave Wysochanski rpc_count_iostats(task, task->tk_client->cl_metrics); 832963d8fe5STrond Myklebust if (task->tk_ops->rpc_call_done != NULL) { 833963d8fe5STrond Myklebust task->tk_ops->rpc_call_done(task, task->tk_calldata); 834d05fdb0cSTrond Myklebust if (task->tk_action != NULL) { 835abbcf28fSTrond Myklebust /* Always release the RPC slot and buffer memory */ 836d05fdb0cSTrond Myklebust xprt_release(task); 8377fdcf13bSTrond Myklebust rpc_reset_task_statistics(task); 838d05fdb0cSTrond Myklebust } 839d05fdb0cSTrond Myklebust } 840d05fdb0cSTrond Myklebust } 841d9b6cd94STrond Myklebust 842ae67bd38STrond Myklebust void rpc_signal_task(struct rpc_task *task) 843ae67bd38STrond Myklebust { 844ae67bd38STrond Myklebust struct rpc_wait_queue *queue; 845ae67bd38STrond Myklebust 846ae67bd38STrond Myklebust if (!RPC_IS_ACTIVATED(task)) 847ae67bd38STrond Myklebust return; 848ae67bd38STrond Myklebust set_bit(RPC_TASK_SIGNALLED, &task->tk_runstate); 849ae67bd38STrond Myklebust smp_mb__after_atomic(); 850ae67bd38STrond Myklebust queue = READ_ONCE(task->tk_waitqueue); 851ae67bd38STrond Myklebust if (queue) 852ae67bd38STrond Myklebust rpc_wake_up_queued_task_set_status(queue, task, -ERESTARTSYS); 853ae67bd38STrond Myklebust } 854ae67bd38STrond Myklebust 855d9b6cd94STrond Myklebust void rpc_exit(struct rpc_task *task, int status) 856d9b6cd94STrond Myklebust { 857d9b6cd94STrond Myklebust task->tk_status = status; 858d9b6cd94STrond Myklebust task->tk_action = rpc_exit_task; 859d9b6cd94STrond Myklebust rpc_wake_up_queued_task(task->tk_waitqueue, task); 860d9b6cd94STrond Myklebust } 861d9b6cd94STrond Myklebust EXPORT_SYMBOL_GPL(rpc_exit); 862d05fdb0cSTrond Myklebust 863bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata) 864bbd5a1f9STrond Myklebust { 865a86dc496STrond Myklebust if (ops->rpc_release != NULL) 866bbd5a1f9STrond Myklebust ops->rpc_release(calldata); 867bbd5a1f9STrond Myklebust } 868bbd5a1f9STrond Myklebust 869d05fdb0cSTrond Myklebust /* 8701da177e4SLinus Torvalds * This is the RPC `scheduler' (or rather, the finite state machine). 8711da177e4SLinus Torvalds */ 8722efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task) 8731da177e4SLinus Torvalds { 874eb9b55abSTrond Myklebust struct rpc_wait_queue *queue; 875eb9b55abSTrond Myklebust int task_is_async = RPC_IS_ASYNC(task); 8761da177e4SLinus Torvalds int status = 0; 8771da177e4SLinus Torvalds 87846121cf7SChuck Lever dprintk("RPC: %5u __rpc_execute flags=0x%x\n", 8791da177e4SLinus Torvalds task->tk_pid, task->tk_flags); 8801da177e4SLinus Torvalds 8812bd4eef8SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 8822bd4eef8SWeston Andros Adamson if (RPC_IS_QUEUED(task)) 8832bd4eef8SWeston Andros Adamson return; 8841da177e4SLinus Torvalds 885d05fdb0cSTrond Myklebust for (;;) { 886b55c5989STrond Myklebust void (*do_action)(struct rpc_task *); 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds /* 88921ead9ffSChuck Lever * Perform the next FSM step or a pending callback. 89021ead9ffSChuck Lever * 891b55c5989STrond Myklebust * tk_action may be NULL if the task has been killed. 892b55c5989STrond Myklebust * In particular, note that rpc_killall_tasks may 893b55c5989STrond Myklebust * do this at any time, so beware when dereferencing. 8941da177e4SLinus Torvalds */ 895b55c5989STrond Myklebust do_action = task->tk_action; 89621ead9ffSChuck Lever if (task->tk_callback) { 89721ead9ffSChuck Lever do_action = task->tk_callback; 89821ead9ffSChuck Lever task->tk_callback = NULL; 8991da177e4SLinus Torvalds } 90021ead9ffSChuck Lever if (!do_action) 90121ead9ffSChuck Lever break; 902e671edb9SChuck Lever trace_rpc_task_run_action(task, do_action); 903b55c5989STrond Myklebust do_action(task); 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds /* 9061da177e4SLinus Torvalds * Lockless check for whether task is sleeping or not. 9071da177e4SLinus Torvalds */ 9081da177e4SLinus Torvalds if (!RPC_IS_QUEUED(task)) 9091da177e4SLinus Torvalds continue; 910ae67bd38STrond Myklebust 911ae67bd38STrond Myklebust /* 912ae67bd38STrond Myklebust * Signalled tasks should exit rather than sleep. 913ae67bd38STrond Myklebust */ 914*714fbc73STrond Myklebust if (RPC_SIGNALLED(task)) { 915*714fbc73STrond Myklebust task->tk_rpc_status = -ERESTARTSYS; 916ae67bd38STrond Myklebust rpc_exit(task, -ERESTARTSYS); 917*714fbc73STrond Myklebust } 918ae67bd38STrond Myklebust 919eb9b55abSTrond Myklebust /* 920eb9b55abSTrond Myklebust * The queue->lock protects against races with 921eb9b55abSTrond Myklebust * rpc_make_runnable(). 922eb9b55abSTrond Myklebust * 923eb9b55abSTrond Myklebust * Note that once we clear RPC_TASK_RUNNING on an asynchronous 924eb9b55abSTrond Myklebust * rpc_task, rpc_make_runnable() can assign it to a 925eb9b55abSTrond Myklebust * different workqueue. We therefore cannot assume that the 926eb9b55abSTrond Myklebust * rpc_task pointer may still be dereferenced. 927eb9b55abSTrond Myklebust */ 928eb9b55abSTrond Myklebust queue = task->tk_waitqueue; 929c049f8eaSTrond Myklebust spin_lock(&queue->lock); 930eb9b55abSTrond Myklebust if (!RPC_IS_QUEUED(task)) { 931c049f8eaSTrond Myklebust spin_unlock(&queue->lock); 9321da177e4SLinus Torvalds continue; 9331da177e4SLinus Torvalds } 934eb9b55abSTrond Myklebust rpc_clear_running(task); 935c049f8eaSTrond Myklebust spin_unlock(&queue->lock); 936eb9b55abSTrond Myklebust if (task_is_async) 937eb9b55abSTrond Myklebust return; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /* sync task: sleep here */ 94046121cf7SChuck Lever dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid); 94196651ab3STrond Myklebust status = out_of_line_wait_on_bit(&task->tk_runstate, 942150030b7SMatthew Wilcox RPC_TASK_QUEUED, rpc_wait_bit_killable, 943150030b7SMatthew Wilcox TASK_KILLABLE); 944ae67bd38STrond Myklebust if (status < 0) { 9451da177e4SLinus Torvalds /* 9461da177e4SLinus Torvalds * When a sync task receives a signal, it exits with 9471da177e4SLinus Torvalds * -ERESTARTSYS. In order to catch any callbacks that 9481da177e4SLinus Torvalds * clean up after sleeping on some queue, we don't 9491da177e4SLinus Torvalds * break the loop here, but go around once more. 9501da177e4SLinus Torvalds */ 95146121cf7SChuck Lever dprintk("RPC: %5u got signal\n", task->tk_pid); 952ae67bd38STrond Myklebust set_bit(RPC_TASK_SIGNALLED, &task->tk_runstate); 953*714fbc73STrond Myklebust task->tk_rpc_status = -ERESTARTSYS; 9541da177e4SLinus Torvalds rpc_exit(task, -ERESTARTSYS); 9551da177e4SLinus Torvalds } 95646121cf7SChuck Lever dprintk("RPC: %5u sync task resuming\n", task->tk_pid); 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 95946121cf7SChuck Lever dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status, 96046121cf7SChuck Lever task->tk_status); 9611da177e4SLinus Torvalds /* Release all resources associated with the task */ 9621da177e4SLinus Torvalds rpc_release_task(task); 9631da177e4SLinus Torvalds } 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds /* 9661da177e4SLinus Torvalds * User-visible entry point to the scheduler. 9671da177e4SLinus Torvalds * 9681da177e4SLinus Torvalds * This may be called recursively if e.g. an async NFS task updates 9691da177e4SLinus Torvalds * the attributes and finds that dirty pages must be flushed. 9701da177e4SLinus Torvalds * NOTE: Upon exit of this function the task is guaranteed to be 9711da177e4SLinus Torvalds * released. In particular note that tk_release() will have 9721da177e4SLinus Torvalds * been called, so your task memory may have been freed. 9731da177e4SLinus Torvalds */ 9742efef837STrond Myklebust void rpc_execute(struct rpc_task *task) 9751da177e4SLinus Torvalds { 976a76580fbSTrond Myklebust bool is_async = RPC_IS_ASYNC(task); 977a76580fbSTrond Myklebust 97844c28873STrond Myklebust rpc_set_active(task); 979f1dc237cSTrond Myklebust rpc_make_runnable(rpciod_workqueue, task); 980a76580fbSTrond Myklebust if (!is_async) 9812efef837STrond Myklebust __rpc_execute(task); 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 98465f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work) 9851da177e4SLinus Torvalds { 986a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 987a1231fdaSTrond Myklebust 98865f27f38SDavid Howells __rpc_execute(container_of(work, struct rpc_task, u.tk_work)); 989a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds 99202107148SChuck Lever /** 9935fe6eaa1SChuck Lever * rpc_malloc - allocate RPC buffer resources 9945fe6eaa1SChuck Lever * @task: RPC task 9955fe6eaa1SChuck Lever * 9965fe6eaa1SChuck Lever * A single memory region is allocated, which is split between the 9975fe6eaa1SChuck Lever * RPC call and RPC reply that this task is being used for. When 9985fe6eaa1SChuck Lever * this RPC is retired, the memory is released by calling rpc_free. 9991da177e4SLinus Torvalds * 1000c5a4dd8bSChuck Lever * To prevent rpciod from hanging, this allocator never sleeps, 10015fe6eaa1SChuck Lever * returning -ENOMEM and suppressing warning if the request cannot 10025fe6eaa1SChuck Lever * be serviced immediately. The caller can arrange to sleep in a 10035fe6eaa1SChuck Lever * way that is safe for rpciod. 1004c5a4dd8bSChuck Lever * 1005c5a4dd8bSChuck Lever * Most requests are 'small' (under 2KiB) and can be serviced from a 1006c5a4dd8bSChuck Lever * mempool, ensuring that NFS reads and writes can always proceed, 1007c5a4dd8bSChuck Lever * and that there is good locality of reference for these buffers. 10081da177e4SLinus Torvalds */ 10095fe6eaa1SChuck Lever int rpc_malloc(struct rpc_task *task) 10101da177e4SLinus Torvalds { 10115fe6eaa1SChuck Lever struct rpc_rqst *rqst = task->tk_rqstp; 10125fe6eaa1SChuck Lever size_t size = rqst->rq_callsize + rqst->rq_rcvsize; 1013aa3d1faeSChuck Lever struct rpc_buffer *buf; 101412a3ad61STrond Myklebust gfp_t gfp = GFP_NOFS; 1015a564b8f0SMel Gorman 1016a564b8f0SMel Gorman if (RPC_IS_SWAPPER(task)) 1017c4a7ca77STrond Myklebust gfp = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN; 10181da177e4SLinus Torvalds 1019aa3d1faeSChuck Lever size += sizeof(struct rpc_buffer); 1020c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 1021c5a4dd8bSChuck Lever buf = mempool_alloc(rpc_buffer_mempool, gfp); 10221da177e4SLinus Torvalds else 1023c5a4dd8bSChuck Lever buf = kmalloc(size, gfp); 1024ddce40dfSPeter Zijlstra 1025ddce40dfSPeter Zijlstra if (!buf) 10265fe6eaa1SChuck Lever return -ENOMEM; 1027ddce40dfSPeter Zijlstra 1028aa3d1faeSChuck Lever buf->len = size; 1029215d0678SGeert Uytterhoeven dprintk("RPC: %5u allocated buffer of size %zu at %p\n", 1030c5a4dd8bSChuck Lever task->tk_pid, size, buf); 10315fe6eaa1SChuck Lever rqst->rq_buffer = buf->data; 103268778945SChuck Lever rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; 10335fe6eaa1SChuck Lever return 0; 10341da177e4SLinus Torvalds } 103512444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_malloc); 10361da177e4SLinus Torvalds 103702107148SChuck Lever /** 10383435c74aSChuck Lever * rpc_free - free RPC buffer resources allocated via rpc_malloc 10393435c74aSChuck Lever * @task: RPC task 104002107148SChuck Lever * 104102107148SChuck Lever */ 10423435c74aSChuck Lever void rpc_free(struct rpc_task *task) 10431da177e4SLinus Torvalds { 10443435c74aSChuck Lever void *buffer = task->tk_rqstp->rq_buffer; 1045aa3d1faeSChuck Lever size_t size; 1046aa3d1faeSChuck Lever struct rpc_buffer *buf; 104702107148SChuck Lever 1048aa3d1faeSChuck Lever buf = container_of(buffer, struct rpc_buffer, data); 1049aa3d1faeSChuck Lever size = buf->len; 1050c5a4dd8bSChuck Lever 1051215d0678SGeert Uytterhoeven dprintk("RPC: freeing buffer of size %zu at %p\n", 1052c5a4dd8bSChuck Lever size, buf); 1053aa3d1faeSChuck Lever 1054c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 1055c5a4dd8bSChuck Lever mempool_free(buf, rpc_buffer_mempool); 10561da177e4SLinus Torvalds else 1057c5a4dd8bSChuck Lever kfree(buf); 10581da177e4SLinus Torvalds } 105912444809S\"Talpey, Thomas\ EXPORT_SYMBOL_GPL(rpc_free); 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* 10621da177e4SLinus Torvalds * Creation and deletion of RPC task structures 10631da177e4SLinus Torvalds */ 106447fe0648STrond Myklebust static void rpc_init_task(struct rpc_task *task, const struct rpc_task_setup *task_setup_data) 10651da177e4SLinus Torvalds { 10661da177e4SLinus Torvalds memset(task, 0, sizeof(*task)); 106744c28873STrond Myklebust atomic_set(&task->tk_count, 1); 106884115e1cSTrond Myklebust task->tk_flags = task_setup_data->flags; 106984115e1cSTrond Myklebust task->tk_ops = task_setup_data->callback_ops; 107084115e1cSTrond Myklebust task->tk_calldata = task_setup_data->callback_data; 10716529eba0STrond Myklebust INIT_LIST_HEAD(&task->tk_task); 10721da177e4SLinus Torvalds 10733ff7576dSTrond Myklebust task->tk_priority = task_setup_data->priority - RPC_PRIORITY_LOW; 10743ff7576dSTrond Myklebust task->tk_owner = current->tgid; 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* Initialize workqueue for async tasks */ 107732bfb5c0STrond Myklebust task->tk_workqueue = task_setup_data->workqueue; 10781da177e4SLinus Torvalds 1079a101b043STrond Myklebust task->tk_xprt = rpc_task_get_xprt(task_setup_data->rpc_client, 1080a101b043STrond Myklebust xprt_get(task_setup_data->rpc_xprt)); 10819d61498dSTrond Myklebust 10821de7eea9SNeilBrown task->tk_op_cred = get_rpccred(task_setup_data->rpc_op_cred); 10831de7eea9SNeilBrown 108484115e1cSTrond Myklebust if (task->tk_ops->rpc_call_prepare != NULL) 108584115e1cSTrond Myklebust task->tk_action = rpc_prepare_task; 1086963d8fe5STrond Myklebust 10877fdcf13bSTrond Myklebust rpc_init_task_statistics(task); 1088ef759a2eSChuck Lever 108946121cf7SChuck Lever dprintk("RPC: new task initialized, procpid %u\n", 1090ba25f9dcSPavel Emelyanov task_pid_nr(current)); 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds static struct rpc_task * 10941da177e4SLinus Torvalds rpc_alloc_task(void) 10951da177e4SLinus Torvalds { 109612a3ad61STrond Myklebust return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS); 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* 110090c5755fSTrond Myklebust * Create a new task for the specified client. 11011da177e4SLinus Torvalds */ 110284115e1cSTrond Myklebust struct rpc_task *rpc_new_task(const struct rpc_task_setup *setup_data) 11031da177e4SLinus Torvalds { 1104e8f5d77cSTrond Myklebust struct rpc_task *task = setup_data->task; 1105e8f5d77cSTrond Myklebust unsigned short flags = 0; 11061da177e4SLinus Torvalds 1107e8f5d77cSTrond Myklebust if (task == NULL) { 11081da177e4SLinus Torvalds task = rpc_alloc_task(); 1109e8f5d77cSTrond Myklebust flags = RPC_TASK_DYNAMIC; 1110e8f5d77cSTrond Myklebust } 11111da177e4SLinus Torvalds 111284115e1cSTrond Myklebust rpc_init_task(task, setup_data); 1113e8f5d77cSTrond Myklebust task->tk_flags |= flags; 111446121cf7SChuck Lever dprintk("RPC: allocated task %p\n", task); 11151da177e4SLinus Torvalds return task; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 1118c6567ed1STrond Myklebust /* 1119c6567ed1STrond Myklebust * rpc_free_task - release rpc task and perform cleanups 1120c6567ed1STrond Myklebust * 1121c6567ed1STrond Myklebust * Note that we free up the rpc_task _after_ rpc_release_calldata() 1122c6567ed1STrond Myklebust * in order to work around a workqueue dependency issue. 1123c6567ed1STrond Myklebust * 1124c6567ed1STrond Myklebust * Tejun Heo states: 1125c6567ed1STrond Myklebust * "Workqueue currently considers two work items to be the same if they're 1126c6567ed1STrond Myklebust * on the same address and won't execute them concurrently - ie. it 1127c6567ed1STrond Myklebust * makes a work item which is queued again while being executed wait 1128c6567ed1STrond Myklebust * for the previous execution to complete. 1129c6567ed1STrond Myklebust * 1130c6567ed1STrond Myklebust * If a work function frees the work item, and then waits for an event 1131c6567ed1STrond Myklebust * which should be performed by another work item and *that* work item 1132c6567ed1STrond Myklebust * recycles the freed work item, it can create a false dependency loop. 1133c6567ed1STrond Myklebust * There really is no reliable way to detect this short of verifying 1134c6567ed1STrond Myklebust * every memory free." 1135c6567ed1STrond Myklebust * 1136c6567ed1STrond Myklebust */ 113732bfb5c0STrond Myklebust static void rpc_free_task(struct rpc_task *task) 11381da177e4SLinus Torvalds { 1139c6567ed1STrond Myklebust unsigned short tk_flags = task->tk_flags; 11401da177e4SLinus Torvalds 11411de7eea9SNeilBrown put_rpccred(task->tk_op_cred); 1142c6567ed1STrond Myklebust rpc_release_calldata(task->tk_ops, task->tk_calldata); 1143c6567ed1STrond Myklebust 1144c6567ed1STrond Myklebust if (tk_flags & RPC_TASK_DYNAMIC) { 11455e4424afSTrond Myklebust dprintk("RPC: %5u freeing task\n", task->tk_pid); 11465e4424afSTrond Myklebust mempool_free(task, rpc_task_mempool); 11475e4424afSTrond Myklebust } 114832bfb5c0STrond Myklebust } 114932bfb5c0STrond Myklebust 115032bfb5c0STrond Myklebust static void rpc_async_release(struct work_struct *work) 115132bfb5c0STrond Myklebust { 1152a1231fdaSTrond Myklebust unsigned int pflags = memalloc_nofs_save(); 1153a1231fdaSTrond Myklebust 115432bfb5c0STrond Myklebust rpc_free_task(container_of(work, struct rpc_task, u.tk_work)); 1155a1231fdaSTrond Myklebust memalloc_nofs_restore(pflags); 115632bfb5c0STrond Myklebust } 115732bfb5c0STrond Myklebust 1158bf294b41STrond Myklebust static void rpc_release_resources_task(struct rpc_task *task) 115932bfb5c0STrond Myklebust { 1160e6b3c4dbSTrond Myklebust xprt_release(task); 1161a271c5a0SOGAWA Hirofumi if (task->tk_msg.rpc_cred) { 1162a52458b4SNeilBrown put_cred(task->tk_msg.rpc_cred); 1163a271c5a0SOGAWA Hirofumi task->tk_msg.rpc_cred = NULL; 1164a271c5a0SOGAWA Hirofumi } 116558f9612cSTrond Myklebust rpc_task_release_client(task); 1166bf294b41STrond Myklebust } 1167bf294b41STrond Myklebust 1168bf294b41STrond Myklebust static void rpc_final_put_task(struct rpc_task *task, 1169bf294b41STrond Myklebust struct workqueue_struct *q) 1170bf294b41STrond Myklebust { 1171bf294b41STrond Myklebust if (q != NULL) { 117232bfb5c0STrond Myklebust INIT_WORK(&task->u.tk_work, rpc_async_release); 1173bf294b41STrond Myklebust queue_work(q, &task->u.tk_work); 117432bfb5c0STrond Myklebust } else 117532bfb5c0STrond Myklebust rpc_free_task(task); 1176e6b3c4dbSTrond Myklebust } 1177bf294b41STrond Myklebust 1178bf294b41STrond Myklebust static void rpc_do_put_task(struct rpc_task *task, struct workqueue_struct *q) 1179bf294b41STrond Myklebust { 1180bf294b41STrond Myklebust if (atomic_dec_and_test(&task->tk_count)) { 1181bf294b41STrond Myklebust rpc_release_resources_task(task); 1182bf294b41STrond Myklebust rpc_final_put_task(task, q); 1183bf294b41STrond Myklebust } 1184bf294b41STrond Myklebust } 1185bf294b41STrond Myklebust 1186bf294b41STrond Myklebust void rpc_put_task(struct rpc_task *task) 1187bf294b41STrond Myklebust { 1188bf294b41STrond Myklebust rpc_do_put_task(task, NULL); 1189bf294b41STrond Myklebust } 1190e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task); 1191e6b3c4dbSTrond Myklebust 1192bf294b41STrond Myklebust void rpc_put_task_async(struct rpc_task *task) 1193bf294b41STrond Myklebust { 1194bf294b41STrond Myklebust rpc_do_put_task(task, task->tk_workqueue); 1195bf294b41STrond Myklebust } 1196bf294b41STrond Myklebust EXPORT_SYMBOL_GPL(rpc_put_task_async); 1197bf294b41STrond Myklebust 1198bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task) 1199e6b3c4dbSTrond Myklebust { 120046121cf7SChuck Lever dprintk("RPC: %5u release task\n", task->tk_pid); 12011da177e4SLinus Torvalds 12020a0c2a57SWeston Andros Adamson WARN_ON_ONCE(RPC_IS_QUEUED(task)); 12031da177e4SLinus Torvalds 1204bf294b41STrond Myklebust rpc_release_resources_task(task); 1205e6b3c4dbSTrond Myklebust 1206bf294b41STrond Myklebust /* 1207bf294b41STrond Myklebust * Note: at this point we have been removed from rpc_clnt->cl_tasks, 1208bf294b41STrond Myklebust * so it should be safe to use task->tk_count as a test for whether 1209bf294b41STrond Myklebust * or not any other processes still hold references to our rpc_task. 1210bf294b41STrond Myklebust */ 1211bf294b41STrond Myklebust if (atomic_read(&task->tk_count) != 1 + !RPC_IS_ASYNC(task)) { 1212bf294b41STrond Myklebust /* Wake up anyone who may be waiting for task completion */ 1213bf294b41STrond Myklebust if (!rpc_complete_task(task)) 1214bf294b41STrond Myklebust return; 1215bf294b41STrond Myklebust } else { 1216bf294b41STrond Myklebust if (!atomic_dec_and_test(&task->tk_count)) 1217bf294b41STrond Myklebust return; 1218bf294b41STrond Myklebust } 1219bf294b41STrond Myklebust rpc_final_put_task(task, task->tk_workqueue); 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 1222b247bbf1STrond Myklebust int rpciod_up(void) 1223b247bbf1STrond Myklebust { 1224b247bbf1STrond Myklebust return try_module_get(THIS_MODULE) ? 0 : -EINVAL; 1225b247bbf1STrond Myklebust } 1226b247bbf1STrond Myklebust 1227b247bbf1STrond Myklebust void rpciod_down(void) 1228b247bbf1STrond Myklebust { 1229b247bbf1STrond Myklebust module_put(THIS_MODULE); 1230b247bbf1STrond Myklebust } 1231b247bbf1STrond Myklebust 12321da177e4SLinus Torvalds /* 1233b247bbf1STrond Myklebust * Start up the rpciod workqueue. 12341da177e4SLinus Torvalds */ 1235b247bbf1STrond Myklebust static int rpciod_start(void) 12361da177e4SLinus Torvalds { 12371da177e4SLinus Torvalds struct workqueue_struct *wq; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds /* 12401da177e4SLinus Torvalds * Create the rpciod thread and wait for it to start. 12411da177e4SLinus Torvalds */ 1242ab418d70STrond Myklebust dprintk("RPC: creating workqueue rpciod\n"); 1243f515f86bSOlga Kornievskaia wq = alloc_workqueue("rpciod", WQ_MEM_RECLAIM | WQ_UNBOUND, 0); 124440a5f1b1STrond Myklebust if (!wq) 124540a5f1b1STrond Myklebust goto out_failed; 12461da177e4SLinus Torvalds rpciod_workqueue = wq; 124740a5f1b1STrond Myklebust /* Note: highpri because network receive is latency sensitive */ 124890ea9f1bSTrond Myklebust wq = alloc_workqueue("xprtiod", WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_HIGHPRI, 0); 124940a5f1b1STrond Myklebust if (!wq) 125040a5f1b1STrond Myklebust goto free_rpciod; 125140a5f1b1STrond Myklebust xprtiod_workqueue = wq; 125240a5f1b1STrond Myklebust return 1; 125340a5f1b1STrond Myklebust free_rpciod: 125440a5f1b1STrond Myklebust wq = rpciod_workqueue; 125540a5f1b1STrond Myklebust rpciod_workqueue = NULL; 125640a5f1b1STrond Myklebust destroy_workqueue(wq); 125740a5f1b1STrond Myklebust out_failed: 125840a5f1b1STrond Myklebust return 0; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 1261b247bbf1STrond Myklebust static void rpciod_stop(void) 12621da177e4SLinus Torvalds { 1263b247bbf1STrond Myklebust struct workqueue_struct *wq = NULL; 1264ab418d70STrond Myklebust 1265b247bbf1STrond Myklebust if (rpciod_workqueue == NULL) 1266b247bbf1STrond Myklebust return; 1267ab418d70STrond Myklebust dprintk("RPC: destroying workqueue rpciod\n"); 12681da177e4SLinus Torvalds 1269b247bbf1STrond Myklebust wq = rpciod_workqueue; 12701da177e4SLinus Torvalds rpciod_workqueue = NULL; 1271b247bbf1STrond Myklebust destroy_workqueue(wq); 127240a5f1b1STrond Myklebust wq = xprtiod_workqueue; 127340a5f1b1STrond Myklebust xprtiod_workqueue = NULL; 127440a5f1b1STrond Myklebust destroy_workqueue(wq); 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds void 12781da177e4SLinus Torvalds rpc_destroy_mempool(void) 12791da177e4SLinus Torvalds { 1280b247bbf1STrond Myklebust rpciod_stop(); 12811da177e4SLinus Torvalds mempool_destroy(rpc_buffer_mempool); 12821da177e4SLinus Torvalds mempool_destroy(rpc_task_mempool); 12831a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_task_slabp); 12841a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_buffer_slabp); 1285f6a1cc89STrond Myklebust rpc_destroy_wait_queue(&delay_queue); 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds int 12891da177e4SLinus Torvalds rpc_init_mempool(void) 12901da177e4SLinus Torvalds { 1291f6a1cc89STrond Myklebust /* 1292f6a1cc89STrond Myklebust * The following is not strictly a mempool initialisation, 1293f6a1cc89STrond Myklebust * but there is no harm in doing it here 1294f6a1cc89STrond Myklebust */ 1295f6a1cc89STrond Myklebust rpc_init_wait_queue(&delay_queue, "delayq"); 1296f6a1cc89STrond Myklebust if (!rpciod_start()) 1297f6a1cc89STrond Myklebust goto err_nomem; 1298f6a1cc89STrond Myklebust 12991da177e4SLinus Torvalds rpc_task_slabp = kmem_cache_create("rpc_tasks", 13001da177e4SLinus Torvalds sizeof(struct rpc_task), 13011da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 130220c2df83SPaul Mundt NULL); 13031da177e4SLinus Torvalds if (!rpc_task_slabp) 13041da177e4SLinus Torvalds goto err_nomem; 13051da177e4SLinus Torvalds rpc_buffer_slabp = kmem_cache_create("rpc_buffers", 13061da177e4SLinus Torvalds RPC_BUFFER_MAXSIZE, 13071da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 130820c2df83SPaul Mundt NULL); 13091da177e4SLinus Torvalds if (!rpc_buffer_slabp) 13101da177e4SLinus Torvalds goto err_nomem; 131193d2341cSMatthew Dobson rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE, 13121da177e4SLinus Torvalds rpc_task_slabp); 13131da177e4SLinus Torvalds if (!rpc_task_mempool) 13141da177e4SLinus Torvalds goto err_nomem; 131593d2341cSMatthew Dobson rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE, 13161da177e4SLinus Torvalds rpc_buffer_slabp); 13171da177e4SLinus Torvalds if (!rpc_buffer_mempool) 13181da177e4SLinus Torvalds goto err_nomem; 13191da177e4SLinus Torvalds return 0; 13201da177e4SLinus Torvalds err_nomem: 13211da177e4SLinus Torvalds rpc_destroy_mempool(); 13221da177e4SLinus Torvalds return -ENOMEM; 13231da177e4SLinus Torvalds } 1324