11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/net/sunrpc/sched.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Scheduling for synchronous and asynchronous RPC requests. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * TCP NFS related read + write fixes 91da177e4SLinus Torvalds * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include <linux/sched.h> 151da177e4SLinus Torvalds #include <linux/interrupt.h> 161da177e4SLinus Torvalds #include <linux/slab.h> 171da177e4SLinus Torvalds #include <linux/mempool.h> 181da177e4SLinus Torvalds #include <linux/smp.h> 191da177e4SLinus Torvalds #include <linux/smp_lock.h> 201da177e4SLinus Torvalds #include <linux/spinlock.h> 214a3e2f71SArjan van de Ven #include <linux/mutex.h> 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds #ifdef RPC_DEBUG 261da177e4SLinus Torvalds #define RPCDBG_FACILITY RPCDBG_SCHED 271da177e4SLinus Torvalds #define RPC_TASK_MAGIC_ID 0xf00baa 281da177e4SLinus Torvalds #endif 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds /* 311da177e4SLinus Torvalds * RPC slabs and memory pools 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds #define RPC_BUFFER_MAXSIZE (2048) 341da177e4SLinus Torvalds #define RPC_BUFFER_POOLSIZE (8) 351da177e4SLinus Torvalds #define RPC_TASK_POOLSIZE (8) 36e18b890bSChristoph Lameter static struct kmem_cache *rpc_task_slabp __read_mostly; 37e18b890bSChristoph Lameter static struct kmem_cache *rpc_buffer_slabp __read_mostly; 38ba89966cSEric Dumazet static mempool_t *rpc_task_mempool __read_mostly; 39ba89966cSEric Dumazet static mempool_t *rpc_buffer_mempool __read_mostly; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static void __rpc_default_timer(struct rpc_task *task); 4265f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *); 43bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task); 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * RPC tasks sit here while waiting for conditions to improve. 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds static RPC_WAITQ(delay_queue, "delayq"); 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /* 511da177e4SLinus Torvalds * rpciod-related stuff 521da177e4SLinus Torvalds */ 5324c5d9d7STrond Myklebust struct workqueue_struct *rpciod_workqueue; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* 561da177e4SLinus Torvalds * Disable the timer for a given RPC task. Should be called with 571da177e4SLinus Torvalds * queue->lock and bh_disabled in order to avoid races within 581da177e4SLinus Torvalds * rpc_run_timer(). 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds static inline void 611da177e4SLinus Torvalds __rpc_disable_timer(struct rpc_task *task) 621da177e4SLinus Torvalds { 6346121cf7SChuck Lever dprintk("RPC: %5u disabling timer\n", task->tk_pid); 641da177e4SLinus Torvalds task->tk_timeout_fn = NULL; 651da177e4SLinus Torvalds task->tk_timeout = 0; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* 691da177e4SLinus Torvalds * Run a timeout function. 701da177e4SLinus Torvalds * We use the callback in order to allow __rpc_wake_up_task() 711da177e4SLinus Torvalds * and friends to disable the timer synchronously on SMP systems 721da177e4SLinus Torvalds * without calling del_timer_sync(). The latter could cause a 731da177e4SLinus Torvalds * deadlock if called while we're holding spinlocks... 741da177e4SLinus Torvalds */ 751da177e4SLinus Torvalds static void rpc_run_timer(struct rpc_task *task) 761da177e4SLinus Torvalds { 771da177e4SLinus Torvalds void (*callback)(struct rpc_task *); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds callback = task->tk_timeout_fn; 801da177e4SLinus Torvalds task->tk_timeout_fn = NULL; 811da177e4SLinus Torvalds if (callback && RPC_IS_QUEUED(task)) { 8246121cf7SChuck Lever dprintk("RPC: %5u running timer\n", task->tk_pid); 831da177e4SLinus Torvalds callback(task); 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds smp_mb__before_clear_bit(); 861da177e4SLinus Torvalds clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate); 871da177e4SLinus Torvalds smp_mb__after_clear_bit(); 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* 911da177e4SLinus Torvalds * Set up a timer for the current task. 921da177e4SLinus Torvalds */ 931da177e4SLinus Torvalds static inline void 941da177e4SLinus Torvalds __rpc_add_timer(struct rpc_task *task, rpc_action timer) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds if (!task->tk_timeout) 971da177e4SLinus Torvalds return; 981da177e4SLinus Torvalds 9946121cf7SChuck Lever dprintk("RPC: %5u setting alarm for %lu ms\n", 1001da177e4SLinus Torvalds task->tk_pid, task->tk_timeout * 1000 / HZ); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds if (timer) 1031da177e4SLinus Torvalds task->tk_timeout_fn = timer; 1041da177e4SLinus Torvalds else 1051da177e4SLinus Torvalds task->tk_timeout_fn = __rpc_default_timer; 1061da177e4SLinus Torvalds set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate); 1071da177e4SLinus Torvalds mod_timer(&task->tk_timer, jiffies + task->tk_timeout); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* 1111da177e4SLinus Torvalds * Delete any timer for the current task. Because we use del_timer_sync(), 1121da177e4SLinus Torvalds * this function should never be called while holding queue->lock. 1131da177e4SLinus Torvalds */ 1141da177e4SLinus Torvalds static void 1151da177e4SLinus Torvalds rpc_delete_timer(struct rpc_task *task) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds if (RPC_IS_QUEUED(task)) 1181da177e4SLinus Torvalds return; 1191da177e4SLinus Torvalds if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) { 1201da177e4SLinus Torvalds del_singleshot_timer_sync(&task->tk_timer); 12146121cf7SChuck Lever dprintk("RPC: %5u deleting timer\n", task->tk_pid); 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * Add new request to a priority queue. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds struct list_head *q; 1311da177e4SLinus Torvalds struct rpc_task *t; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds INIT_LIST_HEAD(&task->u.tk_wait.links); 1341da177e4SLinus Torvalds q = &queue->tasks[task->tk_priority]; 1351da177e4SLinus Torvalds if (unlikely(task->tk_priority > queue->maxpriority)) 1361da177e4SLinus Torvalds q = &queue->tasks[queue->maxpriority]; 1371da177e4SLinus Torvalds list_for_each_entry(t, q, u.tk_wait.list) { 1381da177e4SLinus Torvalds if (t->tk_cookie == task->tk_cookie) { 1391da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links); 1401da177e4SLinus Torvalds return; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds } 1431da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, q); 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* 1471da177e4SLinus Torvalds * Add new request to wait queue. 1481da177e4SLinus Torvalds * 1491da177e4SLinus Torvalds * Swapper tasks always get inserted at the head of the queue. 1501da177e4SLinus Torvalds * This should avoid many nasty memory deadlocks and hopefully 1511da177e4SLinus Torvalds * improve overall performance. 1521da177e4SLinus Torvalds * Everyone else gets appended to the queue to ensure proper FIFO behavior. 1531da177e4SLinus Torvalds */ 1541da177e4SLinus Torvalds static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds BUG_ON (RPC_IS_QUEUED(task)); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 1591da177e4SLinus Torvalds __rpc_add_wait_queue_priority(queue, task); 1601da177e4SLinus Torvalds else if (RPC_IS_SWAPPER(task)) 1611da177e4SLinus Torvalds list_add(&task->u.tk_wait.list, &queue->tasks[0]); 1621da177e4SLinus Torvalds else 1631da177e4SLinus Torvalds list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]); 1641da177e4SLinus Torvalds task->u.tk_wait.rpc_waitq = queue; 165e19b63daSChuck Lever queue->qlen++; 1661da177e4SLinus Torvalds rpc_set_queued(task); 1671da177e4SLinus Torvalds 16846121cf7SChuck Lever dprintk("RPC: %5u added to queue %p \"%s\"\n", 1691da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds /* 1731da177e4SLinus Torvalds * Remove request from a priority queue. 1741da177e4SLinus Torvalds */ 1751da177e4SLinus Torvalds static void __rpc_remove_wait_queue_priority(struct rpc_task *task) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds struct rpc_task *t; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds if (!list_empty(&task->u.tk_wait.links)) { 1801da177e4SLinus Torvalds t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list); 1811da177e4SLinus Torvalds list_move(&t->u.tk_wait.list, &task->u.tk_wait.list); 1821da177e4SLinus Torvalds list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds list_del(&task->u.tk_wait.list); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 1881da177e4SLinus Torvalds * Remove request from queue. 1891da177e4SLinus Torvalds * Note: must be called with spin lock held. 1901da177e4SLinus Torvalds */ 1911da177e4SLinus Torvalds static void __rpc_remove_wait_queue(struct rpc_task *task) 1921da177e4SLinus Torvalds { 1931da177e4SLinus Torvalds struct rpc_wait_queue *queue; 1941da177e4SLinus Torvalds queue = task->u.tk_wait.rpc_waitq; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 1971da177e4SLinus Torvalds __rpc_remove_wait_queue_priority(task); 1981da177e4SLinus Torvalds else 1991da177e4SLinus Torvalds list_del(&task->u.tk_wait.list); 200e19b63daSChuck Lever queue->qlen--; 20146121cf7SChuck Lever dprintk("RPC: %5u removed from queue %p \"%s\"\n", 2021da177e4SLinus Torvalds task->tk_pid, queue, rpc_qname(queue)); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds queue->priority = priority; 2081da177e4SLinus Torvalds queue->count = 1 << (priority * 2); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds queue->cookie = cookie; 2141da177e4SLinus Torvalds queue->nr = RPC_BATCH_COUNT; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue) 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds rpc_set_waitqueue_priority(queue, queue->maxpriority); 2201da177e4SLinus Torvalds rpc_set_waitqueue_cookie(queue, 0); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds int i; 2261da177e4SLinus Torvalds 2271da177e4SLinus Torvalds spin_lock_init(&queue->lock); 2281da177e4SLinus Torvalds for (i = 0; i < ARRAY_SIZE(queue->tasks); i++) 2291da177e4SLinus Torvalds INIT_LIST_HEAD(&queue->tasks[i]); 2301da177e4SLinus Torvalds queue->maxpriority = maxprio; 2311da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 2321da177e4SLinus Torvalds #ifdef RPC_DEBUG 2331da177e4SLinus Torvalds queue->name = qname; 2341da177e4SLinus Torvalds #endif 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH); 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname) 2431da177e4SLinus Torvalds { 2441da177e4SLinus Torvalds __rpc_init_priority_wait_queue(queue, qname, 0); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds EXPORT_SYMBOL(rpc_init_wait_queue); 2471da177e4SLinus Torvalds 24844c28873STrond Myklebust static int rpc_wait_bit_interruptible(void *word) 24944c28873STrond Myklebust { 25044c28873STrond Myklebust if (signal_pending(current)) 25144c28873STrond Myklebust return -ERESTARTSYS; 25244c28873STrond Myklebust schedule(); 25344c28873STrond Myklebust return 0; 25444c28873STrond Myklebust } 25544c28873STrond Myklebust 256c44fe705STrond Myklebust #ifdef RPC_DEBUG 257c44fe705STrond Myklebust static void rpc_task_set_debuginfo(struct rpc_task *task) 258c44fe705STrond Myklebust { 259c44fe705STrond Myklebust static atomic_t rpc_pid; 260c44fe705STrond Myklebust 261c44fe705STrond Myklebust task->tk_magic = RPC_TASK_MAGIC_ID; 262c44fe705STrond Myklebust task->tk_pid = atomic_inc_return(&rpc_pid); 263c44fe705STrond Myklebust } 264c44fe705STrond Myklebust #else 265c44fe705STrond Myklebust static inline void rpc_task_set_debuginfo(struct rpc_task *task) 266c44fe705STrond Myklebust { 267c44fe705STrond Myklebust } 268c44fe705STrond Myklebust #endif 269c44fe705STrond Myklebust 270e6b3c4dbSTrond Myklebust static void rpc_set_active(struct rpc_task *task) 271e6b3c4dbSTrond Myklebust { 2724bef61ffSTrond Myklebust struct rpc_clnt *clnt; 273e6b3c4dbSTrond Myklebust if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0) 274e6b3c4dbSTrond Myklebust return; 275c44fe705STrond Myklebust rpc_task_set_debuginfo(task); 276e6b3c4dbSTrond Myklebust /* Add to global list of all tasks */ 2774bef61ffSTrond Myklebust clnt = task->tk_client; 2784bef61ffSTrond Myklebust if (clnt != NULL) { 2794bef61ffSTrond Myklebust spin_lock(&clnt->cl_lock); 2804bef61ffSTrond Myklebust list_add_tail(&task->tk_task, &clnt->cl_tasks); 2814bef61ffSTrond Myklebust spin_unlock(&clnt->cl_lock); 2824bef61ffSTrond Myklebust } 283e6b3c4dbSTrond Myklebust } 284e6b3c4dbSTrond Myklebust 28544c28873STrond Myklebust /* 28644c28873STrond Myklebust * Mark an RPC call as having completed by clearing the 'active' bit 28744c28873STrond Myklebust */ 288e6b3c4dbSTrond Myklebust static void rpc_mark_complete_task(struct rpc_task *task) 28944c28873STrond Myklebust { 290e6b3c4dbSTrond Myklebust smp_mb__before_clear_bit(); 291e6b3c4dbSTrond Myklebust clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate); 292e6b3c4dbSTrond Myklebust smp_mb__after_clear_bit(); 29344c28873STrond Myklebust wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE); 29444c28873STrond Myklebust } 29544c28873STrond Myklebust 29644c28873STrond Myklebust /* 29744c28873STrond Myklebust * Allow callers to wait for completion of an RPC call 29844c28873STrond Myklebust */ 29944c28873STrond Myklebust int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *)) 30044c28873STrond Myklebust { 30144c28873STrond Myklebust if (action == NULL) 30244c28873STrond Myklebust action = rpc_wait_bit_interruptible; 30344c28873STrond Myklebust return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE, 30444c28873STrond Myklebust action, TASK_INTERRUPTIBLE); 30544c28873STrond Myklebust } 30644c28873STrond Myklebust EXPORT_SYMBOL(__rpc_wait_for_completion_task); 30744c28873STrond Myklebust 3081da177e4SLinus Torvalds /* 3091da177e4SLinus Torvalds * Make an RPC task runnable. 3101da177e4SLinus Torvalds * 3111da177e4SLinus Torvalds * Note: If the task is ASYNC, this must be called with 3121da177e4SLinus Torvalds * the spinlock held to protect the wait queue operation. 3131da177e4SLinus Torvalds */ 3141da177e4SLinus Torvalds static void rpc_make_runnable(struct rpc_task *task) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds BUG_ON(task->tk_timeout_fn); 3171da177e4SLinus Torvalds rpc_clear_queued(task); 318cc4dc59eSChristophe Saout if (rpc_test_and_set_running(task)) 3191da177e4SLinus Torvalds return; 320cc4dc59eSChristophe Saout /* We might have raced */ 321cc4dc59eSChristophe Saout if (RPC_IS_QUEUED(task)) { 322cc4dc59eSChristophe Saout rpc_clear_running(task); 323cc4dc59eSChristophe Saout return; 324cc4dc59eSChristophe Saout } 3251da177e4SLinus Torvalds if (RPC_IS_ASYNC(task)) { 3261da177e4SLinus Torvalds int status; 3271da177e4SLinus Torvalds 32865f27f38SDavid Howells INIT_WORK(&task->u.tk_work, rpc_async_schedule); 3291da177e4SLinus Torvalds status = queue_work(task->tk_workqueue, &task->u.tk_work); 3301da177e4SLinus Torvalds if (status < 0) { 3311da177e4SLinus Torvalds printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status); 3321da177e4SLinus Torvalds task->tk_status = status; 3331da177e4SLinus Torvalds return; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds } else 33696651ab3STrond Myklebust wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED); 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* 3401da177e4SLinus Torvalds * Prepare for sleeping on a wait queue. 3411da177e4SLinus Torvalds * By always appending tasks to the list we ensure FIFO behavior. 3421da177e4SLinus Torvalds * NB: An RPC task will only receive interrupt-driven events as long 3431da177e4SLinus Torvalds * as it's on a wait queue. 3441da177e4SLinus Torvalds */ 3451da177e4SLinus Torvalds static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, 3461da177e4SLinus Torvalds rpc_action action, rpc_action timer) 3471da177e4SLinus Torvalds { 34846121cf7SChuck Lever dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n", 34946121cf7SChuck Lever task->tk_pid, rpc_qname(q), jiffies); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) { 3521da177e4SLinus Torvalds printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n"); 3531da177e4SLinus Torvalds return; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds __rpc_add_wait_queue(q, task); 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds BUG_ON(task->tk_callback != NULL); 3591da177e4SLinus Torvalds task->tk_callback = action; 3601da177e4SLinus Torvalds __rpc_add_timer(task, timer); 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task, 3641da177e4SLinus Torvalds rpc_action action, rpc_action timer) 3651da177e4SLinus Torvalds { 366e6b3c4dbSTrond Myklebust /* Mark the task as being activated if so needed */ 367e6b3c4dbSTrond Myklebust rpc_set_active(task); 368e6b3c4dbSTrond Myklebust 3691da177e4SLinus Torvalds /* 3701da177e4SLinus Torvalds * Protect the queue operations. 3711da177e4SLinus Torvalds */ 3721da177e4SLinus Torvalds spin_lock_bh(&q->lock); 3731da177e4SLinus Torvalds __rpc_sleep_on(q, task, action, timer); 3741da177e4SLinus Torvalds spin_unlock_bh(&q->lock); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds /** 3781da177e4SLinus Torvalds * __rpc_do_wake_up_task - wake up a single rpc_task 3791da177e4SLinus Torvalds * @task: task to be woken up 3801da177e4SLinus Torvalds * 3811da177e4SLinus Torvalds * Caller must hold queue->lock, and have cleared the task queued flag. 3821da177e4SLinus Torvalds */ 3831da177e4SLinus Torvalds static void __rpc_do_wake_up_task(struct rpc_task *task) 3841da177e4SLinus Torvalds { 38546121cf7SChuck Lever dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n", 38646121cf7SChuck Lever task->tk_pid, jiffies); 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds #ifdef RPC_DEBUG 3891da177e4SLinus Torvalds BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID); 3901da177e4SLinus Torvalds #endif 3911da177e4SLinus Torvalds /* Has the task been executed yet? If not, we cannot wake it up! */ 3921da177e4SLinus Torvalds if (!RPC_IS_ACTIVATED(task)) { 3931da177e4SLinus Torvalds printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task); 3941da177e4SLinus Torvalds return; 3951da177e4SLinus Torvalds } 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds __rpc_disable_timer(task); 3981da177e4SLinus Torvalds __rpc_remove_wait_queue(task); 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds rpc_make_runnable(task); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds dprintk("RPC: __rpc_wake_up_task done\n"); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* 4061da177e4SLinus Torvalds * Wake up the specified task 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds static void __rpc_wake_up_task(struct rpc_task *task) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds if (rpc_start_wakeup(task)) { 4111da177e4SLinus Torvalds if (RPC_IS_QUEUED(task)) 4121da177e4SLinus Torvalds __rpc_do_wake_up_task(task); 4131da177e4SLinus Torvalds rpc_finish_wakeup(task); 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* 4181da177e4SLinus Torvalds * Default timeout handler if none specified by user 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds static void 4211da177e4SLinus Torvalds __rpc_default_timer(struct rpc_task *task) 4221da177e4SLinus Torvalds { 42346121cf7SChuck Lever dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid); 4241da177e4SLinus Torvalds task->tk_status = -ETIMEDOUT; 4251da177e4SLinus Torvalds rpc_wake_up_task(task); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds /* 4291da177e4SLinus Torvalds * Wake up the specified task 4301da177e4SLinus Torvalds */ 4311da177e4SLinus Torvalds void rpc_wake_up_task(struct rpc_task *task) 4321da177e4SLinus Torvalds { 4338aca67f0STrond Myklebust rcu_read_lock_bh(); 4341da177e4SLinus Torvalds if (rpc_start_wakeup(task)) { 4351da177e4SLinus Torvalds if (RPC_IS_QUEUED(task)) { 4361da177e4SLinus Torvalds struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq; 4371da177e4SLinus Torvalds 4388aca67f0STrond Myklebust /* Note: we're already in a bh-safe context */ 4398aca67f0STrond Myklebust spin_lock(&queue->lock); 4401da177e4SLinus Torvalds __rpc_do_wake_up_task(task); 4418aca67f0STrond Myklebust spin_unlock(&queue->lock); 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds rpc_finish_wakeup(task); 4441da177e4SLinus Torvalds } 4458aca67f0STrond Myklebust rcu_read_unlock_bh(); 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* 4491da177e4SLinus Torvalds * Wake up the next task on a priority queue. 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue) 4521da177e4SLinus Torvalds { 4531da177e4SLinus Torvalds struct list_head *q; 4541da177e4SLinus Torvalds struct rpc_task *task; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* 4571da177e4SLinus Torvalds * Service a batch of tasks from a single cookie. 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds q = &queue->tasks[queue->priority]; 4601da177e4SLinus Torvalds if (!list_empty(q)) { 4611da177e4SLinus Torvalds task = list_entry(q->next, struct rpc_task, u.tk_wait.list); 4621da177e4SLinus Torvalds if (queue->cookie == task->tk_cookie) { 4631da177e4SLinus Torvalds if (--queue->nr) 4641da177e4SLinus Torvalds goto out; 4651da177e4SLinus Torvalds list_move_tail(&task->u.tk_wait.list, q); 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds /* 4681da177e4SLinus Torvalds * Check if we need to switch queues. 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds if (--queue->count) 4711da177e4SLinus Torvalds goto new_cookie; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds /* 4751da177e4SLinus Torvalds * Service the next queue. 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds do { 4781da177e4SLinus Torvalds if (q == &queue->tasks[0]) 4791da177e4SLinus Torvalds q = &queue->tasks[queue->maxpriority]; 4801da177e4SLinus Torvalds else 4811da177e4SLinus Torvalds q = q - 1; 4821da177e4SLinus Torvalds if (!list_empty(q)) { 4831da177e4SLinus Torvalds task = list_entry(q->next, struct rpc_task, u.tk_wait.list); 4841da177e4SLinus Torvalds goto new_queue; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } while (q != &queue->tasks[queue->priority]); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds rpc_reset_waitqueue_priority(queue); 4891da177e4SLinus Torvalds return NULL; 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds new_queue: 4921da177e4SLinus Torvalds rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0])); 4931da177e4SLinus Torvalds new_cookie: 4941da177e4SLinus Torvalds rpc_set_waitqueue_cookie(queue, task->tk_cookie); 4951da177e4SLinus Torvalds out: 4961da177e4SLinus Torvalds __rpc_wake_up_task(task); 4971da177e4SLinus Torvalds return task; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* 5011da177e4SLinus Torvalds * Wake up the next task on the wait queue. 5021da177e4SLinus Torvalds */ 5031da177e4SLinus Torvalds struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue) 5041da177e4SLinus Torvalds { 5051da177e4SLinus Torvalds struct rpc_task *task = NULL; 5061da177e4SLinus Torvalds 50746121cf7SChuck Lever dprintk("RPC: wake_up_next(%p \"%s\")\n", 50846121cf7SChuck Lever queue, rpc_qname(queue)); 5098aca67f0STrond Myklebust rcu_read_lock_bh(); 5108aca67f0STrond Myklebust spin_lock(&queue->lock); 5111da177e4SLinus Torvalds if (RPC_IS_PRIORITY(queue)) 5121da177e4SLinus Torvalds task = __rpc_wake_up_next_priority(queue); 5131da177e4SLinus Torvalds else { 5141da177e4SLinus Torvalds task_for_first(task, &queue->tasks[0]) 5151da177e4SLinus Torvalds __rpc_wake_up_task(task); 5161da177e4SLinus Torvalds } 5178aca67f0STrond Myklebust spin_unlock(&queue->lock); 5188aca67f0STrond Myklebust rcu_read_unlock_bh(); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds return task; 5211da177e4SLinus Torvalds } 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds /** 5241da177e4SLinus Torvalds * rpc_wake_up - wake up all rpc_tasks 5251da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 5261da177e4SLinus Torvalds * 5271da177e4SLinus Torvalds * Grabs queue->lock 5281da177e4SLinus Torvalds */ 5291da177e4SLinus Torvalds void rpc_wake_up(struct rpc_wait_queue *queue) 5301da177e4SLinus Torvalds { 531e6d83d55STrond Myklebust struct rpc_task *task, *next; 5321da177e4SLinus Torvalds struct list_head *head; 533e6d83d55STrond Myklebust 5348aca67f0STrond Myklebust rcu_read_lock_bh(); 5358aca67f0STrond Myklebust spin_lock(&queue->lock); 5361da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 5371da177e4SLinus Torvalds for (;;) { 538e6d83d55STrond Myklebust list_for_each_entry_safe(task, next, head, u.tk_wait.list) 5391da177e4SLinus Torvalds __rpc_wake_up_task(task); 5401da177e4SLinus Torvalds if (head == &queue->tasks[0]) 5411da177e4SLinus Torvalds break; 5421da177e4SLinus Torvalds head--; 5431da177e4SLinus Torvalds } 5448aca67f0STrond Myklebust spin_unlock(&queue->lock); 5458aca67f0STrond Myklebust rcu_read_unlock_bh(); 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /** 5491da177e4SLinus Torvalds * rpc_wake_up_status - wake up all rpc_tasks and set their status value. 5501da177e4SLinus Torvalds * @queue: rpc_wait_queue on which the tasks are sleeping 5511da177e4SLinus Torvalds * @status: status value to set 5521da177e4SLinus Torvalds * 5531da177e4SLinus Torvalds * Grabs queue->lock 5541da177e4SLinus Torvalds */ 5551da177e4SLinus Torvalds void rpc_wake_up_status(struct rpc_wait_queue *queue, int status) 5561da177e4SLinus Torvalds { 557e6d83d55STrond Myklebust struct rpc_task *task, *next; 5581da177e4SLinus Torvalds struct list_head *head; 5591da177e4SLinus Torvalds 5608aca67f0STrond Myklebust rcu_read_lock_bh(); 5618aca67f0STrond Myklebust spin_lock(&queue->lock); 5621da177e4SLinus Torvalds head = &queue->tasks[queue->maxpriority]; 5631da177e4SLinus Torvalds for (;;) { 564e6d83d55STrond Myklebust list_for_each_entry_safe(task, next, head, u.tk_wait.list) { 5651da177e4SLinus Torvalds task->tk_status = status; 5661da177e4SLinus Torvalds __rpc_wake_up_task(task); 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds if (head == &queue->tasks[0]) 5691da177e4SLinus Torvalds break; 5701da177e4SLinus Torvalds head--; 5711da177e4SLinus Torvalds } 5728aca67f0STrond Myklebust spin_unlock(&queue->lock); 5738aca67f0STrond Myklebust rcu_read_unlock_bh(); 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5768014793bSTrond Myklebust static void __rpc_atrun(struct rpc_task *task) 5778014793bSTrond Myklebust { 5788014793bSTrond Myklebust rpc_wake_up_task(task); 5798014793bSTrond Myklebust } 5808014793bSTrond Myklebust 5811da177e4SLinus Torvalds /* 5821da177e4SLinus Torvalds * Run a task at a later time 5831da177e4SLinus Torvalds */ 5848014793bSTrond Myklebust void rpc_delay(struct rpc_task *task, unsigned long delay) 5851da177e4SLinus Torvalds { 5861da177e4SLinus Torvalds task->tk_timeout = delay; 5871da177e4SLinus Torvalds rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun); 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* 5914ce70adaSTrond Myklebust * Helper to call task->tk_ops->rpc_call_prepare 5924ce70adaSTrond Myklebust */ 5934ce70adaSTrond Myklebust static void rpc_prepare_task(struct rpc_task *task) 5944ce70adaSTrond Myklebust { 5956d5fcb5aSTrond Myklebust lock_kernel(); 5964ce70adaSTrond Myklebust task->tk_ops->rpc_call_prepare(task, task->tk_calldata); 5976d5fcb5aSTrond Myklebust unlock_kernel(); 5984ce70adaSTrond Myklebust } 5994ce70adaSTrond Myklebust 6004ce70adaSTrond Myklebust /* 601963d8fe5STrond Myklebust * Helper that calls task->tk_ops->rpc_call_done if it exists 602d05fdb0cSTrond Myklebust */ 603abbcf28fSTrond Myklebust void rpc_exit_task(struct rpc_task *task) 604d05fdb0cSTrond Myklebust { 605abbcf28fSTrond Myklebust task->tk_action = NULL; 606963d8fe5STrond Myklebust if (task->tk_ops->rpc_call_done != NULL) { 6076d5fcb5aSTrond Myklebust lock_kernel(); 608963d8fe5STrond Myklebust task->tk_ops->rpc_call_done(task, task->tk_calldata); 6096d5fcb5aSTrond Myklebust unlock_kernel(); 610d05fdb0cSTrond Myklebust if (task->tk_action != NULL) { 611abbcf28fSTrond Myklebust WARN_ON(RPC_ASSASSINATED(task)); 612abbcf28fSTrond Myklebust /* Always release the RPC slot and buffer memory */ 613d05fdb0cSTrond Myklebust xprt_release(task); 614d05fdb0cSTrond Myklebust } 615d05fdb0cSTrond Myklebust } 616d05fdb0cSTrond Myklebust } 617abbcf28fSTrond Myklebust EXPORT_SYMBOL(rpc_exit_task); 618d05fdb0cSTrond Myklebust 619bbd5a1f9STrond Myklebust void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata) 620bbd5a1f9STrond Myklebust { 621bbd5a1f9STrond Myklebust if (ops->rpc_release != NULL) { 622bbd5a1f9STrond Myklebust lock_kernel(); 623bbd5a1f9STrond Myklebust ops->rpc_release(calldata); 624bbd5a1f9STrond Myklebust unlock_kernel(); 625bbd5a1f9STrond Myklebust } 626bbd5a1f9STrond Myklebust } 627bbd5a1f9STrond Myklebust 628d05fdb0cSTrond Myklebust /* 6291da177e4SLinus Torvalds * This is the RPC `scheduler' (or rather, the finite state machine). 6301da177e4SLinus Torvalds */ 6312efef837STrond Myklebust static void __rpc_execute(struct rpc_task *task) 6321da177e4SLinus Torvalds { 6331da177e4SLinus Torvalds int status = 0; 6341da177e4SLinus Torvalds 63546121cf7SChuck Lever dprintk("RPC: %5u __rpc_execute flags=0x%x\n", 6361da177e4SLinus Torvalds task->tk_pid, task->tk_flags); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds BUG_ON(RPC_IS_QUEUED(task)); 6391da177e4SLinus Torvalds 640d05fdb0cSTrond Myklebust for (;;) { 6411da177e4SLinus Torvalds /* 6421da177e4SLinus Torvalds * Garbage collection of pending timers... 6431da177e4SLinus Torvalds */ 6441da177e4SLinus Torvalds rpc_delete_timer(task); 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* 6471da177e4SLinus Torvalds * Execute any pending callback. 6481da177e4SLinus Torvalds */ 6491da177e4SLinus Torvalds if (RPC_DO_CALLBACK(task)) { 6501da177e4SLinus Torvalds /* Define a callback save pointer */ 6511da177e4SLinus Torvalds void (*save_callback)(struct rpc_task *); 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds /* 6541da177e4SLinus Torvalds * If a callback exists, save it, reset it, 6551da177e4SLinus Torvalds * call it. 6561da177e4SLinus Torvalds * The save is needed to stop from resetting 6571da177e4SLinus Torvalds * another callback set within the callback handler 6581da177e4SLinus Torvalds * - Dave 6591da177e4SLinus Torvalds */ 6601da177e4SLinus Torvalds save_callback=task->tk_callback; 6611da177e4SLinus Torvalds task->tk_callback=NULL; 6621da177e4SLinus Torvalds save_callback(task); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* 6661da177e4SLinus Torvalds * Perform the next FSM step. 6671da177e4SLinus Torvalds * tk_action may be NULL when the task has been killed 6681da177e4SLinus Torvalds * by someone else. 6691da177e4SLinus Torvalds */ 6701da177e4SLinus Torvalds if (!RPC_IS_QUEUED(task)) { 671abbcf28fSTrond Myklebust if (task->tk_action == NULL) 672abbcf28fSTrond Myklebust break; 6731da177e4SLinus Torvalds task->tk_action(task); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 6771da177e4SLinus Torvalds * Lockless check for whether task is sleeping or not. 6781da177e4SLinus Torvalds */ 6791da177e4SLinus Torvalds if (!RPC_IS_QUEUED(task)) 6801da177e4SLinus Torvalds continue; 6811da177e4SLinus Torvalds rpc_clear_running(task); 6821da177e4SLinus Torvalds if (RPC_IS_ASYNC(task)) { 6831da177e4SLinus Torvalds /* Careful! we may have raced... */ 6841da177e4SLinus Torvalds if (RPC_IS_QUEUED(task)) 6852efef837STrond Myklebust return; 6861da177e4SLinus Torvalds if (rpc_test_and_set_running(task)) 6872efef837STrond Myklebust return; 6881da177e4SLinus Torvalds continue; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* sync task: sleep here */ 69246121cf7SChuck Lever dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid); 69396651ab3STrond Myklebust /* Note: Caller should be using rpc_clnt_sigmask() */ 69496651ab3STrond Myklebust status = out_of_line_wait_on_bit(&task->tk_runstate, 69596651ab3STrond Myklebust RPC_TASK_QUEUED, rpc_wait_bit_interruptible, 69696651ab3STrond Myklebust TASK_INTERRUPTIBLE); 69796651ab3STrond Myklebust if (status == -ERESTARTSYS) { 6981da177e4SLinus Torvalds /* 6991da177e4SLinus Torvalds * When a sync task receives a signal, it exits with 7001da177e4SLinus Torvalds * -ERESTARTSYS. In order to catch any callbacks that 7011da177e4SLinus Torvalds * clean up after sleeping on some queue, we don't 7021da177e4SLinus Torvalds * break the loop here, but go around once more. 7031da177e4SLinus Torvalds */ 70446121cf7SChuck Lever dprintk("RPC: %5u got signal\n", task->tk_pid); 7051da177e4SLinus Torvalds task->tk_flags |= RPC_TASK_KILLED; 7061da177e4SLinus Torvalds rpc_exit(task, -ERESTARTSYS); 7071da177e4SLinus Torvalds rpc_wake_up_task(task); 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds rpc_set_running(task); 71046121cf7SChuck Lever dprintk("RPC: %5u sync task resuming\n", task->tk_pid); 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 71346121cf7SChuck Lever dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status, 71446121cf7SChuck Lever task->tk_status); 7151da177e4SLinus Torvalds /* Release all resources associated with the task */ 7161da177e4SLinus Torvalds rpc_release_task(task); 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds /* 7201da177e4SLinus Torvalds * User-visible entry point to the scheduler. 7211da177e4SLinus Torvalds * 7221da177e4SLinus Torvalds * This may be called recursively if e.g. an async NFS task updates 7231da177e4SLinus Torvalds * the attributes and finds that dirty pages must be flushed. 7241da177e4SLinus Torvalds * NOTE: Upon exit of this function the task is guaranteed to be 7251da177e4SLinus Torvalds * released. In particular note that tk_release() will have 7261da177e4SLinus Torvalds * been called, so your task memory may have been freed. 7271da177e4SLinus Torvalds */ 7282efef837STrond Myklebust void rpc_execute(struct rpc_task *task) 7291da177e4SLinus Torvalds { 73044c28873STrond Myklebust rpc_set_active(task); 7311da177e4SLinus Torvalds rpc_set_running(task); 7322efef837STrond Myklebust __rpc_execute(task); 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 73565f27f38SDavid Howells static void rpc_async_schedule(struct work_struct *work) 7361da177e4SLinus Torvalds { 73765f27f38SDavid Howells __rpc_execute(container_of(work, struct rpc_task, u.tk_work)); 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds 740aa3d1faeSChuck Lever struct rpc_buffer { 741aa3d1faeSChuck Lever size_t len; 742aa3d1faeSChuck Lever char data[]; 743aa3d1faeSChuck Lever }; 744aa3d1faeSChuck Lever 74502107148SChuck Lever /** 74602107148SChuck Lever * rpc_malloc - allocate an RPC buffer 74702107148SChuck Lever * @task: RPC task that will use this buffer 74802107148SChuck Lever * @size: requested byte size 7491da177e4SLinus Torvalds * 750c5a4dd8bSChuck Lever * To prevent rpciod from hanging, this allocator never sleeps, 751c5a4dd8bSChuck Lever * returning NULL if the request cannot be serviced immediately. 752c5a4dd8bSChuck Lever * The caller can arrange to sleep in a way that is safe for rpciod. 753c5a4dd8bSChuck Lever * 754c5a4dd8bSChuck Lever * Most requests are 'small' (under 2KiB) and can be serviced from a 755c5a4dd8bSChuck Lever * mempool, ensuring that NFS reads and writes can always proceed, 756c5a4dd8bSChuck Lever * and that there is good locality of reference for these buffers. 757c5a4dd8bSChuck Lever * 7581da177e4SLinus Torvalds * In order to avoid memory starvation triggering more writebacks of 759c5a4dd8bSChuck Lever * NFS requests, we avoid using GFP_KERNEL. 7601da177e4SLinus Torvalds */ 76102107148SChuck Lever void *rpc_malloc(struct rpc_task *task, size_t size) 7621da177e4SLinus Torvalds { 763aa3d1faeSChuck Lever struct rpc_buffer *buf; 764c5a4dd8bSChuck Lever gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT; 7651da177e4SLinus Torvalds 766aa3d1faeSChuck Lever size += sizeof(struct rpc_buffer); 767c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 768c5a4dd8bSChuck Lever buf = mempool_alloc(rpc_buffer_mempool, gfp); 7691da177e4SLinus Torvalds else 770c5a4dd8bSChuck Lever buf = kmalloc(size, gfp); 771ddce40dfSPeter Zijlstra 772ddce40dfSPeter Zijlstra if (!buf) 773ddce40dfSPeter Zijlstra return NULL; 774ddce40dfSPeter Zijlstra 775aa3d1faeSChuck Lever buf->len = size; 776215d0678SGeert Uytterhoeven dprintk("RPC: %5u allocated buffer of size %zu at %p\n", 777c5a4dd8bSChuck Lever task->tk_pid, size, buf); 778aa3d1faeSChuck Lever return &buf->data; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 78102107148SChuck Lever /** 78202107148SChuck Lever * rpc_free - free buffer allocated via rpc_malloc 783c5a4dd8bSChuck Lever * @buffer: buffer to free 78402107148SChuck Lever * 78502107148SChuck Lever */ 786c5a4dd8bSChuck Lever void rpc_free(void *buffer) 7871da177e4SLinus Torvalds { 788aa3d1faeSChuck Lever size_t size; 789aa3d1faeSChuck Lever struct rpc_buffer *buf; 79002107148SChuck Lever 791c5a4dd8bSChuck Lever if (!buffer) 792c5a4dd8bSChuck Lever return; 793aa3d1faeSChuck Lever 794aa3d1faeSChuck Lever buf = container_of(buffer, struct rpc_buffer, data); 795aa3d1faeSChuck Lever size = buf->len; 796c5a4dd8bSChuck Lever 797215d0678SGeert Uytterhoeven dprintk("RPC: freeing buffer of size %zu at %p\n", 798c5a4dd8bSChuck Lever size, buf); 799aa3d1faeSChuck Lever 800c5a4dd8bSChuck Lever if (size <= RPC_BUFFER_MAXSIZE) 801c5a4dd8bSChuck Lever mempool_free(buf, rpc_buffer_mempool); 8021da177e4SLinus Torvalds else 803c5a4dd8bSChuck Lever kfree(buf); 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds /* 8071da177e4SLinus Torvalds * Creation and deletion of RPC task structures 8081da177e4SLinus Torvalds */ 809963d8fe5STrond Myklebust void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata) 8101da177e4SLinus Torvalds { 8111da177e4SLinus Torvalds memset(task, 0, sizeof(*task)); 8121da177e4SLinus Torvalds init_timer(&task->tk_timer); 8131da177e4SLinus Torvalds task->tk_timer.data = (unsigned long) task; 8141da177e4SLinus Torvalds task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer; 81544c28873STrond Myklebust atomic_set(&task->tk_count, 1); 8161da177e4SLinus Torvalds task->tk_client = clnt; 8171da177e4SLinus Torvalds task->tk_flags = flags; 818963d8fe5STrond Myklebust task->tk_ops = tk_ops; 8194ce70adaSTrond Myklebust if (tk_ops->rpc_call_prepare != NULL) 8204ce70adaSTrond Myklebust task->tk_action = rpc_prepare_task; 821963d8fe5STrond Myklebust task->tk_calldata = calldata; 8226529eba0STrond Myklebust INIT_LIST_HEAD(&task->tk_task); 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* Initialize retry counters */ 8251da177e4SLinus Torvalds task->tk_garb_retry = 2; 8261da177e4SLinus Torvalds task->tk_cred_retry = 2; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds task->tk_priority = RPC_PRIORITY_NORMAL; 8291da177e4SLinus Torvalds task->tk_cookie = (unsigned long)current; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds /* Initialize workqueue for async tasks */ 8321da177e4SLinus Torvalds task->tk_workqueue = rpciod_workqueue; 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds if (clnt) { 83534f52e35STrond Myklebust kref_get(&clnt->cl_kref); 8361da177e4SLinus Torvalds if (clnt->cl_softrtry) 8371da177e4SLinus Torvalds task->tk_flags |= RPC_TASK_SOFT; 8381da177e4SLinus Torvalds if (!clnt->cl_intr) 8391da177e4SLinus Torvalds task->tk_flags |= RPC_TASK_NOINTR; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 842963d8fe5STrond Myklebust BUG_ON(task->tk_ops == NULL); 843963d8fe5STrond Myklebust 844ef759a2eSChuck Lever /* starting timestamp */ 845ef759a2eSChuck Lever task->tk_start = jiffies; 846ef759a2eSChuck Lever 84746121cf7SChuck Lever dprintk("RPC: new task initialized, procpid %u\n", 8481da177e4SLinus Torvalds current->pid); 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds 8511da177e4SLinus Torvalds static struct rpc_task * 8521da177e4SLinus Torvalds rpc_alloc_task(void) 8531da177e4SLinus Torvalds { 8541da177e4SLinus Torvalds return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS); 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8578aca67f0STrond Myklebust static void rpc_free_task(struct rcu_head *rcu) 8581da177e4SLinus Torvalds { 8598aca67f0STrond Myklebust struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu); 86046121cf7SChuck Lever dprintk("RPC: %5u freeing task\n", task->tk_pid); 8611da177e4SLinus Torvalds mempool_free(task, rpc_task_mempool); 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds /* 86590c5755fSTrond Myklebust * Create a new task for the specified client. 8661da177e4SLinus Torvalds */ 867963d8fe5STrond Myklebust struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata) 8681da177e4SLinus Torvalds { 8691da177e4SLinus Torvalds struct rpc_task *task; 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds task = rpc_alloc_task(); 8721da177e4SLinus Torvalds if (!task) 87390c5755fSTrond Myklebust goto out; 8741da177e4SLinus Torvalds 875963d8fe5STrond Myklebust rpc_init_task(task, clnt, flags, tk_ops, calldata); 8761da177e4SLinus Torvalds 87746121cf7SChuck Lever dprintk("RPC: allocated task %p\n", task); 8781da177e4SLinus Torvalds task->tk_flags |= RPC_TASK_DYNAMIC; 8791da177e4SLinus Torvalds out: 8801da177e4SLinus Torvalds return task; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 883e6b3c4dbSTrond Myklebust 884e6b3c4dbSTrond Myklebust void rpc_put_task(struct rpc_task *task) 8851da177e4SLinus Torvalds { 886963d8fe5STrond Myklebust const struct rpc_call_ops *tk_ops = task->tk_ops; 887963d8fe5STrond Myklebust void *calldata = task->tk_calldata; 8881da177e4SLinus Torvalds 889e6b3c4dbSTrond Myklebust if (!atomic_dec_and_test(&task->tk_count)) 890e6b3c4dbSTrond Myklebust return; 891e6b3c4dbSTrond Myklebust /* Release resources */ 892e6b3c4dbSTrond Myklebust if (task->tk_rqstp) 893e6b3c4dbSTrond Myklebust xprt_release(task); 894e6b3c4dbSTrond Myklebust if (task->tk_msg.rpc_cred) 895e6b3c4dbSTrond Myklebust rpcauth_unbindcred(task); 896e6b3c4dbSTrond Myklebust if (task->tk_client) { 897e6b3c4dbSTrond Myklebust rpc_release_client(task->tk_client); 898e6b3c4dbSTrond Myklebust task->tk_client = NULL; 899e6b3c4dbSTrond Myklebust } 900e6b3c4dbSTrond Myklebust if (task->tk_flags & RPC_TASK_DYNAMIC) 9018aca67f0STrond Myklebust call_rcu_bh(&task->u.tk_rcu, rpc_free_task); 902bbd5a1f9STrond Myklebust rpc_release_calldata(tk_ops, calldata); 903e6b3c4dbSTrond Myklebust } 904e6b3c4dbSTrond Myklebust EXPORT_SYMBOL(rpc_put_task); 905e6b3c4dbSTrond Myklebust 906bde8f00cSTrond Myklebust static void rpc_release_task(struct rpc_task *task) 907e6b3c4dbSTrond Myklebust { 9081da177e4SLinus Torvalds #ifdef RPC_DEBUG 9091da177e4SLinus Torvalds BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID); 9101da177e4SLinus Torvalds #endif 91146121cf7SChuck Lever dprintk("RPC: %5u release task\n", task->tk_pid); 9121da177e4SLinus Torvalds 9136529eba0STrond Myklebust if (!list_empty(&task->tk_task)) { 9144bef61ffSTrond Myklebust struct rpc_clnt *clnt = task->tk_client; 9156529eba0STrond Myklebust /* Remove from client task list */ 9164bef61ffSTrond Myklebust spin_lock(&clnt->cl_lock); 9171da177e4SLinus Torvalds list_del(&task->tk_task); 9184bef61ffSTrond Myklebust spin_unlock(&clnt->cl_lock); 9196529eba0STrond Myklebust } 9201da177e4SLinus Torvalds BUG_ON (RPC_IS_QUEUED(task)); 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds /* Synchronously delete any running timer */ 9231da177e4SLinus Torvalds rpc_delete_timer(task); 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds #ifdef RPC_DEBUG 9261da177e4SLinus Torvalds task->tk_magic = 0; 9271da177e4SLinus Torvalds #endif 928e6b3c4dbSTrond Myklebust /* Wake up anyone who is waiting for task completion */ 929e6b3c4dbSTrond Myklebust rpc_mark_complete_task(task); 930e6b3c4dbSTrond Myklebust 931e6b3c4dbSTrond Myklebust rpc_put_task(task); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* 9351da177e4SLinus Torvalds * Kill all tasks for the given client. 9361da177e4SLinus Torvalds * XXX: kill their descendants as well? 9371da177e4SLinus Torvalds */ 9384bef61ffSTrond Myklebust void rpc_killall_tasks(struct rpc_clnt *clnt) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds struct rpc_task *rovr; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds 9434bef61ffSTrond Myklebust if (list_empty(&clnt->cl_tasks)) 9444bef61ffSTrond Myklebust return; 9454bef61ffSTrond Myklebust dprintk("RPC: killing all tasks for client %p\n", clnt); 9464bef61ffSTrond Myklebust /* 9474bef61ffSTrond Myklebust * Spin lock all_tasks to prevent changes... 9484bef61ffSTrond Myklebust */ 9494bef61ffSTrond Myklebust spin_lock(&clnt->cl_lock); 9504bef61ffSTrond Myklebust list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) { 9511da177e4SLinus Torvalds if (! RPC_IS_ACTIVATED(rovr)) 9521da177e4SLinus Torvalds continue; 9536529eba0STrond Myklebust if (!(rovr->tk_flags & RPC_TASK_KILLED)) { 9541da177e4SLinus Torvalds rovr->tk_flags |= RPC_TASK_KILLED; 9551da177e4SLinus Torvalds rpc_exit(rovr, -EIO); 9561da177e4SLinus Torvalds rpc_wake_up_task(rovr); 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds } 9594bef61ffSTrond Myklebust spin_unlock(&clnt->cl_lock); 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 962*b247bbf1STrond Myklebust int rpciod_up(void) 963*b247bbf1STrond Myklebust { 964*b247bbf1STrond Myklebust return try_module_get(THIS_MODULE) ? 0 : -EINVAL; 965*b247bbf1STrond Myklebust } 966*b247bbf1STrond Myklebust 967*b247bbf1STrond Myklebust void rpciod_down(void) 968*b247bbf1STrond Myklebust { 969*b247bbf1STrond Myklebust module_put(THIS_MODULE); 970*b247bbf1STrond Myklebust } 971*b247bbf1STrond Myklebust 9721da177e4SLinus Torvalds /* 973*b247bbf1STrond Myklebust * Start up the rpciod workqueue. 9741da177e4SLinus Torvalds */ 975*b247bbf1STrond Myklebust static int rpciod_start(void) 9761da177e4SLinus Torvalds { 9771da177e4SLinus Torvalds struct workqueue_struct *wq; 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds /* 9801da177e4SLinus Torvalds * Create the rpciod thread and wait for it to start. 9811da177e4SLinus Torvalds */ 982ab418d70STrond Myklebust dprintk("RPC: creating workqueue rpciod\n"); 9831da177e4SLinus Torvalds wq = create_workqueue("rpciod"); 9841da177e4SLinus Torvalds rpciod_workqueue = wq; 985*b247bbf1STrond Myklebust return rpciod_workqueue != NULL; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 988*b247bbf1STrond Myklebust static void rpciod_stop(void) 9891da177e4SLinus Torvalds { 990*b247bbf1STrond Myklebust struct workqueue_struct *wq = NULL; 991ab418d70STrond Myklebust 992*b247bbf1STrond Myklebust if (rpciod_workqueue == NULL) 993*b247bbf1STrond Myklebust return; 994ab418d70STrond Myklebust dprintk("RPC: destroying workqueue rpciod\n"); 9951da177e4SLinus Torvalds 996*b247bbf1STrond Myklebust wq = rpciod_workqueue; 9971da177e4SLinus Torvalds rpciod_workqueue = NULL; 998*b247bbf1STrond Myklebust destroy_workqueue(wq); 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds void 10021da177e4SLinus Torvalds rpc_destroy_mempool(void) 10031da177e4SLinus Torvalds { 1004*b247bbf1STrond Myklebust rpciod_stop(); 10051da177e4SLinus Torvalds if (rpc_buffer_mempool) 10061da177e4SLinus Torvalds mempool_destroy(rpc_buffer_mempool); 10071da177e4SLinus Torvalds if (rpc_task_mempool) 10081da177e4SLinus Torvalds mempool_destroy(rpc_task_mempool); 10091a1d92c1SAlexey Dobriyan if (rpc_task_slabp) 10101a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_task_slabp); 10111a1d92c1SAlexey Dobriyan if (rpc_buffer_slabp) 10121a1d92c1SAlexey Dobriyan kmem_cache_destroy(rpc_buffer_slabp); 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds int 10161da177e4SLinus Torvalds rpc_init_mempool(void) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds rpc_task_slabp = kmem_cache_create("rpc_tasks", 10191da177e4SLinus Torvalds sizeof(struct rpc_task), 10201da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 102120c2df83SPaul Mundt NULL); 10221da177e4SLinus Torvalds if (!rpc_task_slabp) 10231da177e4SLinus Torvalds goto err_nomem; 10241da177e4SLinus Torvalds rpc_buffer_slabp = kmem_cache_create("rpc_buffers", 10251da177e4SLinus Torvalds RPC_BUFFER_MAXSIZE, 10261da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 102720c2df83SPaul Mundt NULL); 10281da177e4SLinus Torvalds if (!rpc_buffer_slabp) 10291da177e4SLinus Torvalds goto err_nomem; 103093d2341cSMatthew Dobson rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE, 10311da177e4SLinus Torvalds rpc_task_slabp); 10321da177e4SLinus Torvalds if (!rpc_task_mempool) 10331da177e4SLinus Torvalds goto err_nomem; 103493d2341cSMatthew Dobson rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE, 10351da177e4SLinus Torvalds rpc_buffer_slabp); 10361da177e4SLinus Torvalds if (!rpc_buffer_mempool) 10371da177e4SLinus Torvalds goto err_nomem; 1038*b247bbf1STrond Myklebust if (!rpciod_start()) 1039*b247bbf1STrond Myklebust goto err_nomem; 10401da177e4SLinus Torvalds return 0; 10411da177e4SLinus Torvalds err_nomem: 10421da177e4SLinus Torvalds rpc_destroy_mempool(); 10431da177e4SLinus Torvalds return -ENOMEM; 10441da177e4SLinus Torvalds } 1045