SUNRPC: rpc_timeout_upcall_queue should not sleep

The function rpc_timeout_upcall_queue runs from a workqueue, and hence
sleeping is not recommended. Convert the protection of the upcall queue
from being mutex-based to being spinlock-based.

Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>

+58 -38
+58 -38
net/sunrpc/rpc_pipe.c
··· 38 39 #define RPC_UPCALL_TIMEOUT (30*HZ) 40 41 - static void 42 - __rpc_purge_list(struct rpc_inode *rpci, struct list_head *head, int err) 43 { 44 struct rpc_pipe_msg *msg; 45 - void (*destroy_msg)(struct rpc_pipe_msg *); 46 47 - destroy_msg = rpci->ops->destroy_msg; 48 - while (!list_empty(head)) { 49 msg = list_entry(head->next, struct rpc_pipe_msg, list); 50 - list_del_init(&msg->list); 51 msg->errno = err; 52 destroy_msg(msg); 53 - } 54 - } 55 - 56 - static void 57 - __rpc_purge_upcall(struct inode *inode, int err) 58 - { 59 - struct rpc_inode *rpci = RPC_I(inode); 60 - 61 - __rpc_purge_list(rpci, &rpci->pipe, err); 62 - rpci->pipelen = 0; 63 wake_up(&rpci->waitq); 64 } 65 66 static void 67 rpc_timeout_upcall_queue(void *data) 68 { 69 struct rpc_inode *rpci = (struct rpc_inode *)data; 70 struct inode *inode = &rpci->vfs_inode; 71 72 - mutex_lock(&inode->i_mutex); 73 - if (rpci->ops == NULL) 74 - goto out; 75 - if (rpci->nreaders == 0 && !list_empty(&rpci->pipe)) 76 - __rpc_purge_upcall(inode, -ETIMEDOUT); 77 - out: 78 - mutex_unlock(&inode->i_mutex); 79 } 80 81 int ··· 82 struct rpc_inode *rpci = RPC_I(inode); 83 int res = -EPIPE; 84 85 - mutex_lock(&inode->i_mutex); 86 if (rpci->ops == NULL) 87 goto out; 88 if (rpci->nreaders) { ··· 98 res = 0; 99 } 100 out: 101 - mutex_unlock(&inode->i_mutex); 102 wake_up(&rpci->waitq); 103 return res; 104 } ··· 113 rpc_close_pipes(struct inode *inode) 114 { 115 struct rpc_inode *rpci = RPC_I(inode); 116 117 mutex_lock(&inode->i_mutex); 118 - if (rpci->ops != NULL) { 119 rpci->nreaders = 0; 120 - __rpc_purge_list(rpci, &rpci->in_upcall, -EPIPE); 121 - __rpc_purge_upcall(inode, -EPIPE); 122 - rpci->nwriters = 0; 123 - if (rpci->ops->release_pipe) 124 - rpci->ops->release_pipe(inode); 125 rpci->ops = NULL; 126 } 127 rpc_inode_setowner(inode, NULL); 128 mutex_unlock(&inode->i_mutex); 129 - cancel_delayed_work(&rpci->queue_timeout); 130 - flush_scheduled_work(); 131 } 132 133 static struct inode * ··· 183 goto out; 184 msg = (struct rpc_pipe_msg *)filp->private_data; 185 if (msg != NULL) { 186 msg->errno = -EAGAIN; 187 - list_del_init(&msg->list); 188 rpci->ops->destroy_msg(msg); 189 } 190 if (filp->f_mode & FMODE_WRITE) 191 rpci->nwriters --; 192 - if (filp->f_mode & FMODE_READ) 193 rpci->nreaders --; 194 - if (!rpci->nreaders) 195 - __rpc_purge_upcall(inode, -EAGAIN); 196 if (rpci->ops->release_pipe) 197 rpci->ops->release_pipe(inode); 198 out: ··· 225 } 226 msg = filp->private_data; 227 if (msg == NULL) { 228 if (!list_empty(&rpci->pipe)) { 229 msg = list_entry(rpci->pipe.next, 230 struct rpc_pipe_msg, ··· 235 filp->private_data = msg; 236 msg->copied = 0; 237 } 238 if (msg == NULL) 239 goto out_unlock; 240 } ··· 243 res = rpci->ops->upcall(filp, msg, buf, len); 244 if (res < 0 || msg->len == msg->copied) { 245 filp->private_data = NULL; 246 - list_del_init(&msg->list); 247 rpci->ops->destroy_msg(msg); 248 } 249 out_unlock:
··· 38 39 #define RPC_UPCALL_TIMEOUT (30*HZ) 40 41 + static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head, 42 + void (*destroy_msg)(struct rpc_pipe_msg *), int err) 43 { 44 struct rpc_pipe_msg *msg; 45 46 + if (list_empty(head)) 47 + return; 48 + do { 49 msg = list_entry(head->next, struct rpc_pipe_msg, list); 50 + list_del(&msg->list); 51 msg->errno = err; 52 destroy_msg(msg); 53 + } while (!list_empty(head)); 54 wake_up(&rpci->waitq); 55 } 56 57 static void 58 rpc_timeout_upcall_queue(void *data) 59 { 60 + LIST_HEAD(free_list); 61 struct rpc_inode *rpci = (struct rpc_inode *)data; 62 struct inode *inode = &rpci->vfs_inode; 63 + void (*destroy_msg)(struct rpc_pipe_msg *); 64 65 + spin_lock(&inode->i_lock); 66 + if (rpci->ops == NULL) { 67 + spin_unlock(&inode->i_lock); 68 + return; 69 + } 70 + destroy_msg = rpci->ops->destroy_msg; 71 + if (rpci->nreaders == 0) { 72 + list_splice_init(&rpci->pipe, &free_list); 73 + rpci->pipelen = 0; 74 + } 75 + spin_unlock(&inode->i_lock); 76 + rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT); 77 } 78 79 int ··· 84 struct rpc_inode *rpci = RPC_I(inode); 85 int res = -EPIPE; 86 87 + spin_lock(&inode->i_lock); 88 if (rpci->ops == NULL) 89 goto out; 90 if (rpci->nreaders) { ··· 100 res = 0; 101 } 102 out: 103 + spin_unlock(&inode->i_lock); 104 wake_up(&rpci->waitq); 105 return res; 106 } ··· 115 rpc_close_pipes(struct inode *inode) 116 { 117 struct rpc_inode *rpci = RPC_I(inode); 118 + struct rpc_pipe_ops *ops; 119 120 mutex_lock(&inode->i_mutex); 121 + ops = rpci->ops; 122 + if (ops != NULL) { 123 + LIST_HEAD(free_list); 124 + 125 + spin_lock(&inode->i_lock); 126 rpci->nreaders = 0; 127 + list_splice_init(&rpci->in_upcall, &free_list); 128 + list_splice_init(&rpci->pipe, &free_list); 129 + rpci->pipelen = 0; 130 rpci->ops = NULL; 131 + spin_unlock(&inode->i_lock); 132 + rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE); 133 + rpci->nwriters = 0; 134 + if (ops->release_pipe) 135 + ops->release_pipe(inode); 136 + cancel_delayed_work(&rpci->queue_timeout); 137 + flush_scheduled_work(); 138 } 139 rpc_inode_setowner(inode, NULL); 140 mutex_unlock(&inode->i_mutex); 141 } 142 143 static struct inode * ··· 177 goto out; 178 msg = (struct rpc_pipe_msg *)filp->private_data; 179 if (msg != NULL) { 180 + spin_lock(&inode->i_lock); 181 msg->errno = -EAGAIN; 182 + list_del(&msg->list); 183 + spin_unlock(&inode->i_lock); 184 rpci->ops->destroy_msg(msg); 185 } 186 if (filp->f_mode & FMODE_WRITE) 187 rpci->nwriters --; 188 + if (filp->f_mode & FMODE_READ) { 189 rpci->nreaders --; 190 + if (rpci->nreaders == 0) { 191 + LIST_HEAD(free_list); 192 + spin_lock(&inode->i_lock); 193 + list_splice_init(&rpci->pipe, &free_list); 194 + rpci->pipelen = 0; 195 + spin_unlock(&inode->i_lock); 196 + rpc_purge_list(rpci, &free_list, 197 + rpci->ops->destroy_msg, -EAGAIN); 198 + } 199 + } 200 if (rpci->ops->release_pipe) 201 rpci->ops->release_pipe(inode); 202 out: ··· 209 } 210 msg = filp->private_data; 211 if (msg == NULL) { 212 + spin_lock(&inode->i_lock); 213 if (!list_empty(&rpci->pipe)) { 214 msg = list_entry(rpci->pipe.next, 215 struct rpc_pipe_msg, ··· 218 filp->private_data = msg; 219 msg->copied = 0; 220 } 221 + spin_unlock(&inode->i_lock); 222 if (msg == NULL) 223 goto out_unlock; 224 } ··· 225 res = rpci->ops->upcall(filp, msg, buf, len); 226 if (res < 0 || msg->len == msg->copied) { 227 filp->private_data = NULL; 228 + spin_lock(&inode->i_lock); 229 + list_del(&msg->list); 230 + spin_unlock(&inode->i_lock); 231 rpci->ops->destroy_msg(msg); 232 } 233 out_unlock: