Stefano Stabellini | 72e59c3 | 2017-07-05 13:08:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (c) 2017 Stefano Stabellini <stefano@aporeto.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
Stefano Stabellini | fb02987 | 2017-07-06 11:01:00 -0700 | [diff] [blame] | 15 | #include <linux/inet.h> |
Stefano Stabellini | 72e59c3 | 2017-07-05 13:08:39 -0700 | [diff] [blame] | 16 | #include <linux/kthread.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/radix-tree.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/semaphore.h> |
| 21 | #include <linux/wait.h> |
Stefano Stabellini | fb02987 | 2017-07-06 11:01:00 -0700 | [diff] [blame] | 22 | #include <net/sock.h> |
| 23 | #include <net/inet_common.h> |
| 24 | #include <net/inet_connection_sock.h> |
| 25 | #include <net/request_sock.h> |
Stefano Stabellini | 72e59c3 | 2017-07-05 13:08:39 -0700 | [diff] [blame] | 26 | |
| 27 | #include <xen/events.h> |
| 28 | #include <xen/grant_table.h> |
| 29 | #include <xen/xen.h> |
| 30 | #include <xen/xenbus.h> |
| 31 | #include <xen/interface/io/pvcalls.h> |
| 32 | |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 33 | #define PVCALLS_VERSIONS "1" |
| 34 | #define MAX_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER |
| 35 | |
Stefano Stabellini | 9be0733 | 2017-07-05 13:08:48 -0700 | [diff] [blame] | 36 | struct pvcalls_back_global { |
| 37 | struct list_head frontends; |
| 38 | struct semaphore frontends_lock; |
| 39 | } pvcalls_back_global; |
| 40 | |
Stefano Stabellini | d0e4d56 | 2017-07-06 10:59:29 -0700 | [diff] [blame] | 41 | /* |
| 42 | * Per-frontend data structure. It contains pointers to the command |
| 43 | * ring, its event channel, a list of active sockets and a tree of |
| 44 | * passive sockets. |
| 45 | */ |
| 46 | struct pvcalls_fedata { |
| 47 | struct list_head list; |
| 48 | struct xenbus_device *dev; |
| 49 | struct xen_pvcalls_sring *sring; |
| 50 | struct xen_pvcalls_back_ring ring; |
| 51 | int irq; |
| 52 | struct list_head socket_mappings; |
| 53 | struct radix_tree_root socketpass_mappings; |
| 54 | struct semaphore socket_lock; |
| 55 | }; |
| 56 | |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 57 | struct pvcalls_ioworker { |
| 58 | struct work_struct register_work; |
| 59 | struct workqueue_struct *wq; |
| 60 | }; |
| 61 | |
| 62 | struct sock_mapping { |
| 63 | struct list_head list; |
| 64 | struct pvcalls_fedata *fedata; |
Stefano Stabellini | 6f474e7 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 65 | struct sockpass_mapping *sockpass; |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 66 | struct socket *sock; |
| 67 | uint64_t id; |
| 68 | grant_ref_t ref; |
| 69 | struct pvcalls_data_intf *ring; |
| 70 | void *bytes; |
| 71 | struct pvcalls_data data; |
| 72 | uint32_t ring_order; |
| 73 | int irq; |
| 74 | atomic_t read; |
| 75 | atomic_t write; |
| 76 | atomic_t io; |
| 77 | atomic_t release; |
| 78 | void (*saved_data_ready)(struct sock *sk); |
| 79 | struct pvcalls_ioworker ioworker; |
| 80 | }; |
| 81 | |
Stefano Stabellini | 331a63e | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 82 | struct sockpass_mapping { |
| 83 | struct list_head list; |
| 84 | struct pvcalls_fedata *fedata; |
| 85 | struct socket *sock; |
| 86 | uint64_t id; |
| 87 | struct xen_pvcalls_request reqcopy; |
| 88 | spinlock_t copy_lock; |
| 89 | struct workqueue_struct *wq; |
| 90 | struct work_struct register_work; |
| 91 | void (*saved_data_ready)(struct sock *sk); |
| 92 | }; |
| 93 | |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 94 | static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map); |
| 95 | static int pvcalls_back_release_active(struct xenbus_device *dev, |
| 96 | struct pvcalls_fedata *fedata, |
| 97 | struct sock_mapping *map); |
| 98 | |
Stefano Stabellini | 5d520d8 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 99 | static void pvcalls_conn_back_read(void *opaque) |
| 100 | { |
Stefano Stabellini | b3f9f77 | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 101 | struct sock_mapping *map = (struct sock_mapping *)opaque; |
| 102 | struct msghdr msg; |
| 103 | struct kvec vec[2]; |
| 104 | RING_IDX cons, prod, size, wanted, array_size, masked_prod, masked_cons; |
| 105 | int32_t error; |
| 106 | struct pvcalls_data_intf *intf = map->ring; |
| 107 | struct pvcalls_data *data = &map->data; |
| 108 | unsigned long flags; |
| 109 | int ret; |
| 110 | |
| 111 | array_size = XEN_FLEX_RING_SIZE(map->ring_order); |
| 112 | cons = intf->in_cons; |
| 113 | prod = intf->in_prod; |
| 114 | error = intf->in_error; |
| 115 | /* read the indexes first, then deal with the data */ |
| 116 | virt_mb(); |
| 117 | |
| 118 | if (error) |
| 119 | return; |
| 120 | |
| 121 | size = pvcalls_queued(prod, cons, array_size); |
| 122 | if (size >= array_size) |
| 123 | return; |
| 124 | spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags); |
| 125 | if (skb_queue_empty(&map->sock->sk->sk_receive_queue)) { |
| 126 | atomic_set(&map->read, 0); |
| 127 | spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, |
| 128 | flags); |
| 129 | return; |
| 130 | } |
| 131 | spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags); |
| 132 | wanted = array_size - size; |
| 133 | masked_prod = pvcalls_mask(prod, array_size); |
| 134 | masked_cons = pvcalls_mask(cons, array_size); |
| 135 | |
| 136 | memset(&msg, 0, sizeof(msg)); |
Stefano Stabellini | b3f9f77 | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 137 | if (masked_prod < masked_cons) { |
| 138 | vec[0].iov_base = data->in + masked_prod; |
| 139 | vec[0].iov_len = wanted; |
Al Viro | 83683dc | 2017-09-24 17:46:21 -0400 | [diff] [blame] | 140 | iov_iter_kvec(&msg.msg_iter, ITER_KVEC|WRITE, vec, 1, wanted); |
Stefano Stabellini | b3f9f77 | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 141 | } else { |
| 142 | vec[0].iov_base = data->in + masked_prod; |
| 143 | vec[0].iov_len = array_size - masked_prod; |
| 144 | vec[1].iov_base = data->in; |
| 145 | vec[1].iov_len = wanted - vec[0].iov_len; |
Al Viro | 83683dc | 2017-09-24 17:46:21 -0400 | [diff] [blame] | 146 | iov_iter_kvec(&msg.msg_iter, ITER_KVEC|WRITE, vec, 2, wanted); |
Stefano Stabellini | b3f9f77 | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | atomic_set(&map->read, 0); |
| 150 | ret = inet_recvmsg(map->sock, &msg, wanted, MSG_DONTWAIT); |
| 151 | WARN_ON(ret > wanted); |
| 152 | if (ret == -EAGAIN) /* shouldn't happen */ |
| 153 | return; |
| 154 | if (!ret) |
| 155 | ret = -ENOTCONN; |
| 156 | spin_lock_irqsave(&map->sock->sk->sk_receive_queue.lock, flags); |
| 157 | if (ret > 0 && !skb_queue_empty(&map->sock->sk->sk_receive_queue)) |
| 158 | atomic_inc(&map->read); |
| 159 | spin_unlock_irqrestore(&map->sock->sk->sk_receive_queue.lock, flags); |
| 160 | |
| 161 | /* write the data, then modify the indexes */ |
| 162 | virt_wmb(); |
| 163 | if (ret < 0) |
| 164 | intf->in_error = ret; |
| 165 | else |
| 166 | intf->in_prod = prod + ret; |
| 167 | /* update the indexes, then notify the other end */ |
| 168 | virt_wmb(); |
| 169 | notify_remote_via_irq(map->irq); |
| 170 | |
| 171 | return; |
Stefano Stabellini | 5d520d8 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void pvcalls_conn_back_write(struct sock_mapping *map) |
| 175 | { |
Stefano Stabellini | 5ad9918f | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 176 | struct pvcalls_data_intf *intf = map->ring; |
| 177 | struct pvcalls_data *data = &map->data; |
| 178 | struct msghdr msg; |
| 179 | struct kvec vec[2]; |
| 180 | RING_IDX cons, prod, size, array_size; |
| 181 | int ret; |
| 182 | |
| 183 | cons = intf->out_cons; |
| 184 | prod = intf->out_prod; |
| 185 | /* read the indexes before dealing with the data */ |
| 186 | virt_mb(); |
| 187 | |
| 188 | array_size = XEN_FLEX_RING_SIZE(map->ring_order); |
| 189 | size = pvcalls_queued(prod, cons, array_size); |
| 190 | if (size == 0) |
| 191 | return; |
| 192 | |
| 193 | memset(&msg, 0, sizeof(msg)); |
| 194 | msg.msg_flags |= MSG_DONTWAIT; |
Stefano Stabellini | 5ad9918f | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 195 | if (pvcalls_mask(prod, array_size) > pvcalls_mask(cons, array_size)) { |
| 196 | vec[0].iov_base = data->out + pvcalls_mask(cons, array_size); |
| 197 | vec[0].iov_len = size; |
Al Viro | 83683dc | 2017-09-24 17:46:21 -0400 | [diff] [blame] | 198 | iov_iter_kvec(&msg.msg_iter, ITER_KVEC|READ, vec, 1, size); |
Stefano Stabellini | 5ad9918f | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 199 | } else { |
| 200 | vec[0].iov_base = data->out + pvcalls_mask(cons, array_size); |
| 201 | vec[0].iov_len = array_size - pvcalls_mask(cons, array_size); |
| 202 | vec[1].iov_base = data->out; |
| 203 | vec[1].iov_len = size - vec[0].iov_len; |
Al Viro | 83683dc | 2017-09-24 17:46:21 -0400 | [diff] [blame] | 204 | iov_iter_kvec(&msg.msg_iter, ITER_KVEC|READ, vec, 2, size); |
Stefano Stabellini | 5ad9918f | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | atomic_set(&map->write, 0); |
| 208 | ret = inet_sendmsg(map->sock, &msg, size); |
| 209 | if (ret == -EAGAIN || (ret >= 0 && ret < size)) { |
| 210 | atomic_inc(&map->write); |
| 211 | atomic_inc(&map->io); |
| 212 | } |
| 213 | if (ret == -EAGAIN) |
| 214 | return; |
| 215 | |
| 216 | /* write the data, then update the indexes */ |
| 217 | virt_wmb(); |
| 218 | if (ret < 0) { |
| 219 | intf->out_error = ret; |
| 220 | } else { |
| 221 | intf->out_error = 0; |
| 222 | intf->out_cons = cons + ret; |
| 223 | prod = intf->out_prod; |
| 224 | } |
| 225 | /* update the indexes, then notify the other end */ |
| 226 | virt_wmb(); |
| 227 | if (prod != cons + ret) |
| 228 | atomic_inc(&map->write); |
| 229 | notify_remote_via_irq(map->irq); |
Stefano Stabellini | 5d520d8 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 232 | static void pvcalls_back_ioworker(struct work_struct *work) |
| 233 | { |
Stefano Stabellini | 5d520d8 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 234 | struct pvcalls_ioworker *ioworker = container_of(work, |
| 235 | struct pvcalls_ioworker, register_work); |
| 236 | struct sock_mapping *map = container_of(ioworker, struct sock_mapping, |
| 237 | ioworker); |
| 238 | |
| 239 | while (atomic_read(&map->io) > 0) { |
| 240 | if (atomic_read(&map->release) > 0) { |
| 241 | atomic_set(&map->release, 0); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if (atomic_read(&map->read) > 0) |
| 246 | pvcalls_conn_back_read(map); |
| 247 | if (atomic_read(&map->write) > 0) |
| 248 | pvcalls_conn_back_write(map); |
| 249 | |
| 250 | atomic_dec(&map->io); |
| 251 | } |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 254 | static int pvcalls_back_socket(struct xenbus_device *dev, |
| 255 | struct xen_pvcalls_request *req) |
| 256 | { |
Stefano Stabellini | fb02987 | 2017-07-06 11:01:00 -0700 | [diff] [blame] | 257 | struct pvcalls_fedata *fedata; |
| 258 | int ret; |
| 259 | struct xen_pvcalls_response *rsp; |
| 260 | |
| 261 | fedata = dev_get_drvdata(&dev->dev); |
| 262 | |
| 263 | if (req->u.socket.domain != AF_INET || |
| 264 | req->u.socket.type != SOCK_STREAM || |
| 265 | (req->u.socket.protocol != IPPROTO_IP && |
| 266 | req->u.socket.protocol != AF_INET)) |
| 267 | ret = -EAFNOSUPPORT; |
| 268 | else |
| 269 | ret = 0; |
| 270 | |
| 271 | /* leave the actual socket allocation for later */ |
| 272 | |
| 273 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 274 | rsp->req_id = req->req_id; |
| 275 | rsp->cmd = req->cmd; |
| 276 | rsp->u.socket.id = req->u.socket.id; |
| 277 | rsp->ret = ret; |
| 278 | |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 282 | static void pvcalls_sk_state_change(struct sock *sock) |
| 283 | { |
| 284 | struct sock_mapping *map = sock->sk_user_data; |
| 285 | struct pvcalls_data_intf *intf; |
| 286 | |
| 287 | if (map == NULL) |
| 288 | return; |
| 289 | |
| 290 | intf = map->ring; |
| 291 | intf->in_error = -ENOTCONN; |
| 292 | notify_remote_via_irq(map->irq); |
| 293 | } |
| 294 | |
| 295 | static void pvcalls_sk_data_ready(struct sock *sock) |
| 296 | { |
Stefano Stabellini | b3f9f77 | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 297 | struct sock_mapping *map = sock->sk_user_data; |
| 298 | struct pvcalls_ioworker *iow; |
| 299 | |
| 300 | if (map == NULL) |
| 301 | return; |
| 302 | |
| 303 | iow = &map->ioworker; |
| 304 | atomic_inc(&map->read); |
| 305 | atomic_inc(&map->io); |
| 306 | queue_work(iow->wq, &iow->register_work); |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static struct sock_mapping *pvcalls_new_active_socket( |
| 310 | struct pvcalls_fedata *fedata, |
| 311 | uint64_t id, |
| 312 | grant_ref_t ref, |
| 313 | uint32_t evtchn, |
| 314 | struct socket *sock) |
| 315 | { |
| 316 | int ret; |
| 317 | struct sock_mapping *map; |
| 318 | void *page; |
| 319 | |
| 320 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 321 | if (map == NULL) |
| 322 | return NULL; |
| 323 | |
| 324 | map->fedata = fedata; |
| 325 | map->sock = sock; |
| 326 | map->id = id; |
| 327 | map->ref = ref; |
| 328 | |
| 329 | ret = xenbus_map_ring_valloc(fedata->dev, &ref, 1, &page); |
| 330 | if (ret < 0) |
| 331 | goto out; |
| 332 | map->ring = page; |
| 333 | map->ring_order = map->ring->ring_order; |
| 334 | /* first read the order, then map the data ring */ |
| 335 | virt_rmb(); |
| 336 | if (map->ring_order > MAX_RING_ORDER) { |
| 337 | pr_warn("%s frontend requested ring_order %u, which is > MAX (%u)\n", |
| 338 | __func__, map->ring_order, MAX_RING_ORDER); |
| 339 | goto out; |
| 340 | } |
| 341 | ret = xenbus_map_ring_valloc(fedata->dev, map->ring->ref, |
| 342 | (1 << map->ring_order), &page); |
| 343 | if (ret < 0) |
| 344 | goto out; |
| 345 | map->bytes = page; |
| 346 | |
| 347 | ret = bind_interdomain_evtchn_to_irqhandler(fedata->dev->otherend_id, |
| 348 | evtchn, |
| 349 | pvcalls_back_conn_event, |
| 350 | 0, |
| 351 | "pvcalls-backend", |
| 352 | map); |
| 353 | if (ret < 0) |
| 354 | goto out; |
| 355 | map->irq = ret; |
| 356 | |
| 357 | map->data.in = map->bytes; |
| 358 | map->data.out = map->bytes + XEN_FLEX_RING_SIZE(map->ring_order); |
| 359 | |
| 360 | map->ioworker.wq = alloc_workqueue("pvcalls_io", WQ_UNBOUND, 1); |
| 361 | if (!map->ioworker.wq) |
| 362 | goto out; |
| 363 | atomic_set(&map->io, 1); |
| 364 | INIT_WORK(&map->ioworker.register_work, pvcalls_back_ioworker); |
| 365 | |
| 366 | down(&fedata->socket_lock); |
| 367 | list_add_tail(&map->list, &fedata->socket_mappings); |
| 368 | up(&fedata->socket_lock); |
| 369 | |
| 370 | write_lock_bh(&map->sock->sk->sk_callback_lock); |
| 371 | map->saved_data_ready = map->sock->sk->sk_data_ready; |
| 372 | map->sock->sk->sk_user_data = map; |
| 373 | map->sock->sk->sk_data_ready = pvcalls_sk_data_ready; |
| 374 | map->sock->sk->sk_state_change = pvcalls_sk_state_change; |
| 375 | write_unlock_bh(&map->sock->sk->sk_callback_lock); |
| 376 | |
| 377 | return map; |
| 378 | out: |
| 379 | down(&fedata->socket_lock); |
| 380 | list_del(&map->list); |
| 381 | pvcalls_back_release_active(fedata->dev, fedata, map); |
| 382 | up(&fedata->socket_lock); |
| 383 | return NULL; |
| 384 | } |
| 385 | |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 386 | static int pvcalls_back_connect(struct xenbus_device *dev, |
| 387 | struct xen_pvcalls_request *req) |
| 388 | { |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 389 | struct pvcalls_fedata *fedata; |
| 390 | int ret = -EINVAL; |
| 391 | struct socket *sock; |
| 392 | struct sock_mapping *map; |
| 393 | struct xen_pvcalls_response *rsp; |
| 394 | struct sockaddr *sa = (struct sockaddr *)&req->u.connect.addr; |
| 395 | |
| 396 | fedata = dev_get_drvdata(&dev->dev); |
| 397 | |
| 398 | if (req->u.connect.len < sizeof(sa->sa_family) || |
| 399 | req->u.connect.len > sizeof(req->u.connect.addr) || |
| 400 | sa->sa_family != AF_INET) |
| 401 | goto out; |
| 402 | |
| 403 | ret = sock_create(AF_INET, SOCK_STREAM, 0, &sock); |
| 404 | if (ret < 0) |
| 405 | goto out; |
| 406 | ret = inet_stream_connect(sock, sa, req->u.connect.len, 0); |
| 407 | if (ret < 0) { |
| 408 | sock_release(sock); |
| 409 | goto out; |
| 410 | } |
| 411 | |
| 412 | map = pvcalls_new_active_socket(fedata, |
| 413 | req->u.connect.id, |
| 414 | req->u.connect.ref, |
| 415 | req->u.connect.evtchn, |
| 416 | sock); |
| 417 | if (!map) { |
| 418 | ret = -EFAULT; |
Colin Ian King | 68d2059 | 2018-02-22 17:22:59 +0000 | [diff] [blame] | 419 | sock_release(sock); |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | out: |
| 423 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 424 | rsp->req_id = req->req_id; |
| 425 | rsp->cmd = req->cmd; |
| 426 | rsp->u.connect.id = req->u.connect.id; |
| 427 | rsp->ret = ret; |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static int pvcalls_back_release_active(struct xenbus_device *dev, |
| 433 | struct pvcalls_fedata *fedata, |
| 434 | struct sock_mapping *map) |
| 435 | { |
Stefano Stabellini | a51729c | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 436 | disable_irq(map->irq); |
| 437 | if (map->sock->sk != NULL) { |
| 438 | write_lock_bh(&map->sock->sk->sk_callback_lock); |
| 439 | map->sock->sk->sk_user_data = NULL; |
| 440 | map->sock->sk->sk_data_ready = map->saved_data_ready; |
| 441 | write_unlock_bh(&map->sock->sk->sk_callback_lock); |
| 442 | } |
| 443 | |
| 444 | atomic_set(&map->release, 1); |
| 445 | flush_work(&map->ioworker.register_work); |
| 446 | |
| 447 | xenbus_unmap_ring_vfree(dev, map->bytes); |
| 448 | xenbus_unmap_ring_vfree(dev, (void *)map->ring); |
| 449 | unbind_from_irqhandler(map->irq, map); |
| 450 | |
| 451 | sock_release(map->sock); |
| 452 | kfree(map); |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | static int pvcalls_back_release_passive(struct xenbus_device *dev, |
| 458 | struct pvcalls_fedata *fedata, |
| 459 | struct sockpass_mapping *mappass) |
| 460 | { |
| 461 | if (mappass->sock->sk != NULL) { |
| 462 | write_lock_bh(&mappass->sock->sk->sk_callback_lock); |
| 463 | mappass->sock->sk->sk_user_data = NULL; |
| 464 | mappass->sock->sk->sk_data_ready = mappass->saved_data_ready; |
| 465 | write_unlock_bh(&mappass->sock->sk->sk_callback_lock); |
| 466 | } |
| 467 | sock_release(mappass->sock); |
| 468 | flush_workqueue(mappass->wq); |
| 469 | destroy_workqueue(mappass->wq); |
| 470 | kfree(mappass); |
| 471 | |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int pvcalls_back_release(struct xenbus_device *dev, |
| 476 | struct xen_pvcalls_request *req) |
| 477 | { |
Stefano Stabellini | a51729c | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 478 | struct pvcalls_fedata *fedata; |
| 479 | struct sock_mapping *map, *n; |
| 480 | struct sockpass_mapping *mappass; |
| 481 | int ret = 0; |
| 482 | struct xen_pvcalls_response *rsp; |
| 483 | |
| 484 | fedata = dev_get_drvdata(&dev->dev); |
| 485 | |
| 486 | down(&fedata->socket_lock); |
| 487 | list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) { |
| 488 | if (map->id == req->u.release.id) { |
| 489 | list_del(&map->list); |
| 490 | up(&fedata->socket_lock); |
| 491 | ret = pvcalls_back_release_active(dev, fedata, map); |
| 492 | goto out; |
| 493 | } |
| 494 | } |
| 495 | mappass = radix_tree_lookup(&fedata->socketpass_mappings, |
| 496 | req->u.release.id); |
| 497 | if (mappass != NULL) { |
| 498 | radix_tree_delete(&fedata->socketpass_mappings, mappass->id); |
| 499 | up(&fedata->socket_lock); |
| 500 | ret = pvcalls_back_release_passive(dev, fedata, mappass); |
| 501 | } else |
| 502 | up(&fedata->socket_lock); |
| 503 | |
| 504 | out: |
| 505 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 506 | rsp->req_id = req->req_id; |
| 507 | rsp->u.release.id = req->u.release.id; |
| 508 | rsp->cmd = req->cmd; |
| 509 | rsp->ret = ret; |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Stefano Stabellini | 331a63e | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 513 | static void __pvcalls_back_accept(struct work_struct *work) |
| 514 | { |
Stefano Stabellini | 6f474e7 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 515 | struct sockpass_mapping *mappass = container_of( |
| 516 | work, struct sockpass_mapping, register_work); |
| 517 | struct sock_mapping *map; |
| 518 | struct pvcalls_ioworker *iow; |
| 519 | struct pvcalls_fedata *fedata; |
| 520 | struct socket *sock; |
| 521 | struct xen_pvcalls_response *rsp; |
| 522 | struct xen_pvcalls_request *req; |
| 523 | int notify; |
| 524 | int ret = -EINVAL; |
| 525 | unsigned long flags; |
| 526 | |
| 527 | fedata = mappass->fedata; |
| 528 | /* |
| 529 | * __pvcalls_back_accept can race against pvcalls_back_accept. |
| 530 | * We only need to check the value of "cmd" on read. It could be |
| 531 | * done atomically, but to simplify the code on the write side, we |
| 532 | * use a spinlock. |
| 533 | */ |
| 534 | spin_lock_irqsave(&mappass->copy_lock, flags); |
| 535 | req = &mappass->reqcopy; |
| 536 | if (req->cmd != PVCALLS_ACCEPT) { |
| 537 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 538 | return; |
| 539 | } |
| 540 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 541 | |
| 542 | sock = sock_alloc(); |
| 543 | if (sock == NULL) |
| 544 | goto out_error; |
| 545 | sock->type = mappass->sock->type; |
| 546 | sock->ops = mappass->sock->ops; |
| 547 | |
| 548 | ret = inet_accept(mappass->sock, sock, O_NONBLOCK, true); |
| 549 | if (ret == -EAGAIN) { |
| 550 | sock_release(sock); |
Stefano Stabellini | 7611912 | 2018-02-02 17:34:09 -0800 | [diff] [blame] | 551 | return; |
Stefano Stabellini | 6f474e7 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | map = pvcalls_new_active_socket(fedata, |
| 555 | req->u.accept.id_new, |
| 556 | req->u.accept.ref, |
| 557 | req->u.accept.evtchn, |
| 558 | sock); |
| 559 | if (!map) { |
| 560 | ret = -EFAULT; |
| 561 | sock_release(sock); |
| 562 | goto out_error; |
| 563 | } |
| 564 | |
| 565 | map->sockpass = mappass; |
| 566 | iow = &map->ioworker; |
| 567 | atomic_inc(&map->read); |
| 568 | atomic_inc(&map->io); |
| 569 | queue_work(iow->wq, &iow->register_work); |
| 570 | |
| 571 | out_error: |
| 572 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 573 | rsp->req_id = req->req_id; |
| 574 | rsp->cmd = req->cmd; |
| 575 | rsp->u.accept.id = req->u.accept.id; |
| 576 | rsp->ret = ret; |
| 577 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify); |
| 578 | if (notify) |
| 579 | notify_remote_via_irq(fedata->irq); |
| 580 | |
| 581 | mappass->reqcopy.cmd = 0; |
Stefano Stabellini | 331a63e | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static void pvcalls_pass_sk_data_ready(struct sock *sock) |
| 585 | { |
Stefano Stabellini | 6f474e7 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 586 | struct sockpass_mapping *mappass = sock->sk_user_data; |
Stefano Stabellini | 3cf33a587 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 587 | struct pvcalls_fedata *fedata; |
| 588 | struct xen_pvcalls_response *rsp; |
| 589 | unsigned long flags; |
| 590 | int notify; |
Stefano Stabellini | 6f474e7 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 591 | |
| 592 | if (mappass == NULL) |
| 593 | return; |
| 594 | |
Stefano Stabellini | 3cf33a587 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 595 | fedata = mappass->fedata; |
| 596 | spin_lock_irqsave(&mappass->copy_lock, flags); |
| 597 | if (mappass->reqcopy.cmd == PVCALLS_POLL) { |
| 598 | rsp = RING_GET_RESPONSE(&fedata->ring, |
| 599 | fedata->ring.rsp_prod_pvt++); |
| 600 | rsp->req_id = mappass->reqcopy.req_id; |
| 601 | rsp->u.poll.id = mappass->reqcopy.u.poll.id; |
| 602 | rsp->cmd = mappass->reqcopy.cmd; |
| 603 | rsp->ret = 0; |
| 604 | |
| 605 | mappass->reqcopy.cmd = 0; |
| 606 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 607 | |
| 608 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&fedata->ring, notify); |
| 609 | if (notify) |
| 610 | notify_remote_via_irq(mappass->fedata->irq); |
| 611 | } else { |
| 612 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 613 | queue_work(mappass->wq, &mappass->register_work); |
| 614 | } |
Stefano Stabellini | 331a63e | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 617 | static int pvcalls_back_bind(struct xenbus_device *dev, |
| 618 | struct xen_pvcalls_request *req) |
| 619 | { |
Stefano Stabellini | 331a63e | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 620 | struct pvcalls_fedata *fedata; |
| 621 | int ret; |
| 622 | struct sockpass_mapping *map; |
| 623 | struct xen_pvcalls_response *rsp; |
| 624 | |
| 625 | fedata = dev_get_drvdata(&dev->dev); |
| 626 | |
| 627 | map = kzalloc(sizeof(*map), GFP_KERNEL); |
| 628 | if (map == NULL) { |
| 629 | ret = -ENOMEM; |
| 630 | goto out; |
| 631 | } |
| 632 | |
| 633 | INIT_WORK(&map->register_work, __pvcalls_back_accept); |
| 634 | spin_lock_init(&map->copy_lock); |
| 635 | map->wq = alloc_workqueue("pvcalls_wq", WQ_UNBOUND, 1); |
| 636 | if (!map->wq) { |
| 637 | ret = -ENOMEM; |
| 638 | goto out; |
| 639 | } |
| 640 | |
| 641 | ret = sock_create(AF_INET, SOCK_STREAM, 0, &map->sock); |
| 642 | if (ret < 0) |
| 643 | goto out; |
| 644 | |
| 645 | ret = inet_bind(map->sock, (struct sockaddr *)&req->u.bind.addr, |
| 646 | req->u.bind.len); |
| 647 | if (ret < 0) |
| 648 | goto out; |
| 649 | |
| 650 | map->fedata = fedata; |
| 651 | map->id = req->u.bind.id; |
| 652 | |
| 653 | down(&fedata->socket_lock); |
| 654 | ret = radix_tree_insert(&fedata->socketpass_mappings, map->id, |
| 655 | map); |
| 656 | up(&fedata->socket_lock); |
| 657 | if (ret) |
| 658 | goto out; |
| 659 | |
| 660 | write_lock_bh(&map->sock->sk->sk_callback_lock); |
| 661 | map->saved_data_ready = map->sock->sk->sk_data_ready; |
| 662 | map->sock->sk->sk_user_data = map; |
| 663 | map->sock->sk->sk_data_ready = pvcalls_pass_sk_data_ready; |
| 664 | write_unlock_bh(&map->sock->sk->sk_callback_lock); |
| 665 | |
| 666 | out: |
| 667 | if (ret) { |
| 668 | if (map && map->sock) |
| 669 | sock_release(map->sock); |
| 670 | if (map && map->wq) |
| 671 | destroy_workqueue(map->wq); |
| 672 | kfree(map); |
| 673 | } |
| 674 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 675 | rsp->req_id = req->req_id; |
| 676 | rsp->cmd = req->cmd; |
| 677 | rsp->u.bind.id = req->u.bind.id; |
| 678 | rsp->ret = ret; |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static int pvcalls_back_listen(struct xenbus_device *dev, |
| 683 | struct xen_pvcalls_request *req) |
| 684 | { |
Stefano Stabellini | 8ce3f76 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 685 | struct pvcalls_fedata *fedata; |
| 686 | int ret = -EINVAL; |
| 687 | struct sockpass_mapping *map; |
| 688 | struct xen_pvcalls_response *rsp; |
| 689 | |
| 690 | fedata = dev_get_drvdata(&dev->dev); |
| 691 | |
| 692 | down(&fedata->socket_lock); |
| 693 | map = radix_tree_lookup(&fedata->socketpass_mappings, req->u.listen.id); |
| 694 | up(&fedata->socket_lock); |
| 695 | if (map == NULL) |
| 696 | goto out; |
| 697 | |
| 698 | ret = inet_listen(map->sock, req->u.listen.backlog); |
| 699 | |
| 700 | out: |
| 701 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 702 | rsp->req_id = req->req_id; |
| 703 | rsp->cmd = req->cmd; |
| 704 | rsp->u.listen.id = req->u.listen.id; |
| 705 | rsp->ret = ret; |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static int pvcalls_back_accept(struct xenbus_device *dev, |
| 710 | struct xen_pvcalls_request *req) |
| 711 | { |
Stefano Stabellini | 6f474e7 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 712 | struct pvcalls_fedata *fedata; |
| 713 | struct sockpass_mapping *mappass; |
| 714 | int ret = -EINVAL; |
| 715 | struct xen_pvcalls_response *rsp; |
| 716 | unsigned long flags; |
| 717 | |
| 718 | fedata = dev_get_drvdata(&dev->dev); |
| 719 | |
| 720 | down(&fedata->socket_lock); |
| 721 | mappass = radix_tree_lookup(&fedata->socketpass_mappings, |
| 722 | req->u.accept.id); |
| 723 | up(&fedata->socket_lock); |
| 724 | if (mappass == NULL) |
| 725 | goto out_error; |
| 726 | |
| 727 | /* |
| 728 | * Limitation of the current implementation: only support one |
| 729 | * concurrent accept or poll call on one socket. |
| 730 | */ |
| 731 | spin_lock_irqsave(&mappass->copy_lock, flags); |
| 732 | if (mappass->reqcopy.cmd != 0) { |
| 733 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 734 | ret = -EINTR; |
| 735 | goto out_error; |
| 736 | } |
| 737 | |
| 738 | mappass->reqcopy = *req; |
| 739 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 740 | queue_work(mappass->wq, &mappass->register_work); |
| 741 | |
| 742 | /* Tell the caller we don't need to send back a notification yet */ |
| 743 | return -1; |
| 744 | |
| 745 | out_error: |
| 746 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 747 | rsp->req_id = req->req_id; |
| 748 | rsp->cmd = req->cmd; |
| 749 | rsp->u.accept.id = req->u.accept.id; |
| 750 | rsp->ret = ret; |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static int pvcalls_back_poll(struct xenbus_device *dev, |
| 755 | struct xen_pvcalls_request *req) |
| 756 | { |
Stefano Stabellini | 3cf33a587 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 757 | struct pvcalls_fedata *fedata; |
| 758 | struct sockpass_mapping *mappass; |
| 759 | struct xen_pvcalls_response *rsp; |
| 760 | struct inet_connection_sock *icsk; |
| 761 | struct request_sock_queue *queue; |
| 762 | unsigned long flags; |
| 763 | int ret; |
| 764 | bool data; |
| 765 | |
| 766 | fedata = dev_get_drvdata(&dev->dev); |
| 767 | |
| 768 | down(&fedata->socket_lock); |
| 769 | mappass = radix_tree_lookup(&fedata->socketpass_mappings, |
| 770 | req->u.poll.id); |
| 771 | up(&fedata->socket_lock); |
| 772 | if (mappass == NULL) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | /* |
| 776 | * Limitation of the current implementation: only support one |
| 777 | * concurrent accept or poll call on one socket. |
| 778 | */ |
| 779 | spin_lock_irqsave(&mappass->copy_lock, flags); |
| 780 | if (mappass->reqcopy.cmd != 0) { |
| 781 | ret = -EINTR; |
| 782 | goto out; |
| 783 | } |
| 784 | |
| 785 | mappass->reqcopy = *req; |
| 786 | icsk = inet_csk(mappass->sock->sk); |
| 787 | queue = &icsk->icsk_accept_queue; |
| 788 | data = queue->rskq_accept_head != NULL; |
| 789 | if (data) { |
| 790 | mappass->reqcopy.cmd = 0; |
| 791 | ret = 0; |
| 792 | goto out; |
| 793 | } |
| 794 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 795 | |
| 796 | /* Tell the caller we don't need to send back a notification yet */ |
| 797 | return -1; |
| 798 | |
| 799 | out: |
| 800 | spin_unlock_irqrestore(&mappass->copy_lock, flags); |
| 801 | |
| 802 | rsp = RING_GET_RESPONSE(&fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 803 | rsp->req_id = req->req_id; |
| 804 | rsp->cmd = req->cmd; |
| 805 | rsp->u.poll.id = req->u.poll.id; |
| 806 | rsp->ret = ret; |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static int pvcalls_back_handle_cmd(struct xenbus_device *dev, |
| 811 | struct xen_pvcalls_request *req) |
| 812 | { |
| 813 | int ret = 0; |
| 814 | |
| 815 | switch (req->cmd) { |
| 816 | case PVCALLS_SOCKET: |
| 817 | ret = pvcalls_back_socket(dev, req); |
| 818 | break; |
| 819 | case PVCALLS_CONNECT: |
| 820 | ret = pvcalls_back_connect(dev, req); |
| 821 | break; |
| 822 | case PVCALLS_RELEASE: |
| 823 | ret = pvcalls_back_release(dev, req); |
| 824 | break; |
| 825 | case PVCALLS_BIND: |
| 826 | ret = pvcalls_back_bind(dev, req); |
| 827 | break; |
| 828 | case PVCALLS_LISTEN: |
| 829 | ret = pvcalls_back_listen(dev, req); |
| 830 | break; |
| 831 | case PVCALLS_ACCEPT: |
| 832 | ret = pvcalls_back_accept(dev, req); |
| 833 | break; |
| 834 | case PVCALLS_POLL: |
| 835 | ret = pvcalls_back_poll(dev, req); |
| 836 | break; |
| 837 | default: |
| 838 | { |
| 839 | struct pvcalls_fedata *fedata; |
| 840 | struct xen_pvcalls_response *rsp; |
| 841 | |
| 842 | fedata = dev_get_drvdata(&dev->dev); |
| 843 | rsp = RING_GET_RESPONSE( |
| 844 | &fedata->ring, fedata->ring.rsp_prod_pvt++); |
| 845 | rsp->req_id = req->req_id; |
| 846 | rsp->cmd = req->cmd; |
| 847 | rsp->ret = -ENOTSUPP; |
| 848 | break; |
| 849 | } |
| 850 | } |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | static void pvcalls_back_work(struct pvcalls_fedata *fedata) |
| 855 | { |
| 856 | int notify, notify_all = 0, more = 1; |
| 857 | struct xen_pvcalls_request req; |
| 858 | struct xenbus_device *dev = fedata->dev; |
| 859 | |
| 860 | while (more) { |
| 861 | while (RING_HAS_UNCONSUMED_REQUESTS(&fedata->ring)) { |
| 862 | RING_COPY_REQUEST(&fedata->ring, |
| 863 | fedata->ring.req_cons++, |
| 864 | &req); |
| 865 | |
| 866 | if (!pvcalls_back_handle_cmd(dev, &req)) { |
| 867 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY( |
| 868 | &fedata->ring, notify); |
| 869 | notify_all += notify; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | if (notify_all) { |
| 874 | notify_remote_via_irq(fedata->irq); |
| 875 | notify_all = 0; |
| 876 | } |
| 877 | |
| 878 | RING_FINAL_CHECK_FOR_REQUESTS(&fedata->ring, more); |
| 879 | } |
| 880 | } |
| 881 | |
Stefano Stabellini | d0e4d56 | 2017-07-06 10:59:29 -0700 | [diff] [blame] | 882 | static irqreturn_t pvcalls_back_event(int irq, void *dev_id) |
| 883 | { |
Stefano Stabellini | b1efa69 | 2017-07-06 11:00:00 -0700 | [diff] [blame] | 884 | struct xenbus_device *dev = dev_id; |
| 885 | struct pvcalls_fedata *fedata = NULL; |
| 886 | |
| 887 | if (dev == NULL) |
| 888 | return IRQ_HANDLED; |
| 889 | |
| 890 | fedata = dev_get_drvdata(&dev->dev); |
| 891 | if (fedata == NULL) |
| 892 | return IRQ_HANDLED; |
| 893 | |
| 894 | pvcalls_back_work(fedata); |
Stefano Stabellini | d0e4d56 | 2017-07-06 10:59:29 -0700 | [diff] [blame] | 895 | return IRQ_HANDLED; |
| 896 | } |
| 897 | |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 898 | static irqreturn_t pvcalls_back_conn_event(int irq, void *sock_map) |
| 899 | { |
Stefano Stabellini | 5ad9918f | 2017-07-06 11:01:08 -0700 | [diff] [blame] | 900 | struct sock_mapping *map = sock_map; |
| 901 | struct pvcalls_ioworker *iow; |
| 902 | |
| 903 | if (map == NULL || map->sock == NULL || map->sock->sk == NULL || |
| 904 | map->sock->sk->sk_user_data != map) |
| 905 | return IRQ_HANDLED; |
| 906 | |
| 907 | iow = &map->ioworker; |
| 908 | |
| 909 | atomic_inc(&map->write); |
| 910 | atomic_inc(&map->io); |
| 911 | queue_work(iow->wq, &iow->register_work); |
| 912 | |
Stefano Stabellini | 5db4d28 | 2017-07-06 11:01:06 -0700 | [diff] [blame] | 913 | return IRQ_HANDLED; |
| 914 | } |
| 915 | |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 916 | static int backend_connect(struct xenbus_device *dev) |
| 917 | { |
Stefano Stabellini | d0e4d56 | 2017-07-06 10:59:29 -0700 | [diff] [blame] | 918 | int err, evtchn; |
| 919 | grant_ref_t ring_ref; |
| 920 | struct pvcalls_fedata *fedata = NULL; |
| 921 | |
| 922 | fedata = kzalloc(sizeof(struct pvcalls_fedata), GFP_KERNEL); |
| 923 | if (!fedata) |
| 924 | return -ENOMEM; |
| 925 | |
| 926 | fedata->irq = -1; |
| 927 | err = xenbus_scanf(XBT_NIL, dev->otherend, "port", "%u", |
| 928 | &evtchn); |
| 929 | if (err != 1) { |
| 930 | err = -EINVAL; |
| 931 | xenbus_dev_fatal(dev, err, "reading %s/event-channel", |
| 932 | dev->otherend); |
| 933 | goto error; |
| 934 | } |
| 935 | |
| 936 | err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref", "%u", &ring_ref); |
| 937 | if (err != 1) { |
| 938 | err = -EINVAL; |
| 939 | xenbus_dev_fatal(dev, err, "reading %s/ring-ref", |
| 940 | dev->otherend); |
| 941 | goto error; |
| 942 | } |
| 943 | |
| 944 | err = bind_interdomain_evtchn_to_irq(dev->otherend_id, evtchn); |
| 945 | if (err < 0) |
| 946 | goto error; |
| 947 | fedata->irq = err; |
| 948 | |
| 949 | err = request_threaded_irq(fedata->irq, NULL, pvcalls_back_event, |
| 950 | IRQF_ONESHOT, "pvcalls-back", dev); |
| 951 | if (err < 0) |
| 952 | goto error; |
| 953 | |
| 954 | err = xenbus_map_ring_valloc(dev, &ring_ref, 1, |
| 955 | (void **)&fedata->sring); |
| 956 | if (err < 0) |
| 957 | goto error; |
| 958 | |
| 959 | BACK_RING_INIT(&fedata->ring, fedata->sring, XEN_PAGE_SIZE * 1); |
| 960 | fedata->dev = dev; |
| 961 | |
| 962 | INIT_LIST_HEAD(&fedata->socket_mappings); |
| 963 | INIT_RADIX_TREE(&fedata->socketpass_mappings, GFP_KERNEL); |
| 964 | sema_init(&fedata->socket_lock, 1); |
| 965 | dev_set_drvdata(&dev->dev, fedata); |
| 966 | |
| 967 | down(&pvcalls_back_global.frontends_lock); |
| 968 | list_add_tail(&fedata->list, &pvcalls_back_global.frontends); |
| 969 | up(&pvcalls_back_global.frontends_lock); |
| 970 | |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 971 | return 0; |
Stefano Stabellini | d0e4d56 | 2017-07-06 10:59:29 -0700 | [diff] [blame] | 972 | |
| 973 | error: |
| 974 | if (fedata->irq >= 0) |
| 975 | unbind_from_irqhandler(fedata->irq, dev); |
| 976 | if (fedata->sring != NULL) |
| 977 | xenbus_unmap_ring_vfree(dev, fedata->sring); |
| 978 | kfree(fedata); |
| 979 | return err; |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static int backend_disconnect(struct xenbus_device *dev) |
| 983 | { |
Stefano Stabellini | 0a85d23 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 984 | struct pvcalls_fedata *fedata; |
| 985 | struct sock_mapping *map, *n; |
| 986 | struct sockpass_mapping *mappass; |
| 987 | struct radix_tree_iter iter; |
| 988 | void **slot; |
| 989 | |
| 990 | |
| 991 | fedata = dev_get_drvdata(&dev->dev); |
| 992 | |
| 993 | down(&fedata->socket_lock); |
| 994 | list_for_each_entry_safe(map, n, &fedata->socket_mappings, list) { |
| 995 | list_del(&map->list); |
| 996 | pvcalls_back_release_active(dev, fedata, map); |
| 997 | } |
| 998 | |
| 999 | radix_tree_for_each_slot(slot, &fedata->socketpass_mappings, &iter, 0) { |
| 1000 | mappass = radix_tree_deref_slot(slot); |
| 1001 | if (!mappass) |
| 1002 | continue; |
| 1003 | if (radix_tree_exception(mappass)) { |
| 1004 | if (radix_tree_deref_retry(mappass)) |
| 1005 | slot = radix_tree_iter_retry(&iter); |
| 1006 | } else { |
| 1007 | radix_tree_delete(&fedata->socketpass_mappings, |
| 1008 | mappass->id); |
| 1009 | pvcalls_back_release_passive(dev, fedata, mappass); |
| 1010 | } |
| 1011 | } |
| 1012 | up(&fedata->socket_lock); |
| 1013 | |
| 1014 | unbind_from_irqhandler(fedata->irq, dev); |
| 1015 | xenbus_unmap_ring_vfree(dev, fedata->sring); |
| 1016 | |
| 1017 | list_del(&fedata->list); |
| 1018 | kfree(fedata); |
| 1019 | dev_set_drvdata(&dev->dev, NULL); |
| 1020 | |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | |
Stefano Stabellini | 72e59c3 | 2017-07-05 13:08:39 -0700 | [diff] [blame] | 1024 | static int pvcalls_back_probe(struct xenbus_device *dev, |
| 1025 | const struct xenbus_device_id *id) |
| 1026 | { |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1027 | int err, abort; |
| 1028 | struct xenbus_transaction xbt; |
| 1029 | |
| 1030 | again: |
| 1031 | abort = 1; |
| 1032 | |
| 1033 | err = xenbus_transaction_start(&xbt); |
| 1034 | if (err) { |
| 1035 | pr_warn("%s cannot create xenstore transaction\n", __func__); |
| 1036 | return err; |
| 1037 | } |
| 1038 | |
| 1039 | err = xenbus_printf(xbt, dev->nodename, "versions", "%s", |
| 1040 | PVCALLS_VERSIONS); |
| 1041 | if (err) { |
| 1042 | pr_warn("%s write out 'versions' failed\n", __func__); |
| 1043 | goto abort; |
| 1044 | } |
| 1045 | |
| 1046 | err = xenbus_printf(xbt, dev->nodename, "max-page-order", "%u", |
| 1047 | MAX_RING_ORDER); |
| 1048 | if (err) { |
| 1049 | pr_warn("%s write out 'max-page-order' failed\n", __func__); |
| 1050 | goto abort; |
| 1051 | } |
| 1052 | |
| 1053 | err = xenbus_printf(xbt, dev->nodename, "function-calls", |
| 1054 | XENBUS_FUNCTIONS_CALLS); |
| 1055 | if (err) { |
| 1056 | pr_warn("%s write out 'function-calls' failed\n", __func__); |
| 1057 | goto abort; |
| 1058 | } |
| 1059 | |
| 1060 | abort = 0; |
| 1061 | abort: |
| 1062 | err = xenbus_transaction_end(xbt, abort); |
| 1063 | if (err) { |
| 1064 | if (err == -EAGAIN && !abort) |
| 1065 | goto again; |
| 1066 | pr_warn("%s cannot complete xenstore transaction\n", __func__); |
| 1067 | return err; |
| 1068 | } |
| 1069 | |
| 1070 | if (abort) |
| 1071 | return -EFAULT; |
| 1072 | |
| 1073 | xenbus_switch_state(dev, XenbusStateInitWait); |
| 1074 | |
Stefano Stabellini | 72e59c3 | 2017-07-05 13:08:39 -0700 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1078 | static void set_backend_state(struct xenbus_device *dev, |
| 1079 | enum xenbus_state state) |
| 1080 | { |
| 1081 | while (dev->state != state) { |
| 1082 | switch (dev->state) { |
| 1083 | case XenbusStateClosed: |
| 1084 | switch (state) { |
| 1085 | case XenbusStateInitWait: |
| 1086 | case XenbusStateConnected: |
| 1087 | xenbus_switch_state(dev, XenbusStateInitWait); |
| 1088 | break; |
| 1089 | case XenbusStateClosing: |
| 1090 | xenbus_switch_state(dev, XenbusStateClosing); |
| 1091 | break; |
| 1092 | default: |
Arnd Bergmann | fefcfb9 | 2017-07-21 18:17:45 +0200 | [diff] [blame] | 1093 | WARN_ON(1); |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1094 | } |
| 1095 | break; |
| 1096 | case XenbusStateInitWait: |
| 1097 | case XenbusStateInitialised: |
| 1098 | switch (state) { |
| 1099 | case XenbusStateConnected: |
| 1100 | backend_connect(dev); |
| 1101 | xenbus_switch_state(dev, XenbusStateConnected); |
| 1102 | break; |
| 1103 | case XenbusStateClosing: |
| 1104 | case XenbusStateClosed: |
| 1105 | xenbus_switch_state(dev, XenbusStateClosing); |
| 1106 | break; |
| 1107 | default: |
Arnd Bergmann | fefcfb9 | 2017-07-21 18:17:45 +0200 | [diff] [blame] | 1108 | WARN_ON(1); |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1109 | } |
| 1110 | break; |
| 1111 | case XenbusStateConnected: |
| 1112 | switch (state) { |
| 1113 | case XenbusStateInitWait: |
| 1114 | case XenbusStateClosing: |
| 1115 | case XenbusStateClosed: |
| 1116 | down(&pvcalls_back_global.frontends_lock); |
| 1117 | backend_disconnect(dev); |
| 1118 | up(&pvcalls_back_global.frontends_lock); |
| 1119 | xenbus_switch_state(dev, XenbusStateClosing); |
| 1120 | break; |
| 1121 | default: |
Arnd Bergmann | fefcfb9 | 2017-07-21 18:17:45 +0200 | [diff] [blame] | 1122 | WARN_ON(1); |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1123 | } |
| 1124 | break; |
| 1125 | case XenbusStateClosing: |
| 1126 | switch (state) { |
| 1127 | case XenbusStateInitWait: |
| 1128 | case XenbusStateConnected: |
| 1129 | case XenbusStateClosed: |
| 1130 | xenbus_switch_state(dev, XenbusStateClosed); |
| 1131 | break; |
| 1132 | default: |
Arnd Bergmann | fefcfb9 | 2017-07-21 18:17:45 +0200 | [diff] [blame] | 1133 | WARN_ON(1); |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1134 | } |
| 1135 | break; |
| 1136 | default: |
Arnd Bergmann | fefcfb9 | 2017-07-21 18:17:45 +0200 | [diff] [blame] | 1137 | WARN_ON(1); |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | |
Stefano Stabellini | 72e59c3 | 2017-07-05 13:08:39 -0700 | [diff] [blame] | 1142 | static void pvcalls_back_changed(struct xenbus_device *dev, |
| 1143 | enum xenbus_state frontend_state) |
| 1144 | { |
Stefano Stabellini | 0a9c75c | 2017-07-06 10:59:17 -0700 | [diff] [blame] | 1145 | switch (frontend_state) { |
| 1146 | case XenbusStateInitialising: |
| 1147 | set_backend_state(dev, XenbusStateInitWait); |
| 1148 | break; |
| 1149 | |
| 1150 | case XenbusStateInitialised: |
| 1151 | case XenbusStateConnected: |
| 1152 | set_backend_state(dev, XenbusStateConnected); |
| 1153 | break; |
| 1154 | |
| 1155 | case XenbusStateClosing: |
| 1156 | set_backend_state(dev, XenbusStateClosing); |
| 1157 | break; |
| 1158 | |
| 1159 | case XenbusStateClosed: |
| 1160 | set_backend_state(dev, XenbusStateClosed); |
| 1161 | if (xenbus_dev_is_online(dev)) |
| 1162 | break; |
| 1163 | device_unregister(&dev->dev); |
| 1164 | break; |
| 1165 | case XenbusStateUnknown: |
| 1166 | set_backend_state(dev, XenbusStateClosed); |
| 1167 | device_unregister(&dev->dev); |
| 1168 | break; |
| 1169 | |
| 1170 | default: |
| 1171 | xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend", |
| 1172 | frontend_state); |
| 1173 | break; |
| 1174 | } |
Stefano Stabellini | 72e59c3 | 2017-07-05 13:08:39 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | static int pvcalls_back_remove(struct xenbus_device *dev) |
| 1178 | { |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | static int pvcalls_back_uevent(struct xenbus_device *xdev, |
| 1183 | struct kobj_uevent_env *env) |
| 1184 | { |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static const struct xenbus_device_id pvcalls_back_ids[] = { |
| 1189 | { "pvcalls" }, |
| 1190 | { "" } |
| 1191 | }; |
| 1192 | |
| 1193 | static struct xenbus_driver pvcalls_back_driver = { |
| 1194 | .ids = pvcalls_back_ids, |
| 1195 | .probe = pvcalls_back_probe, |
| 1196 | .remove = pvcalls_back_remove, |
| 1197 | .uevent = pvcalls_back_uevent, |
| 1198 | .otherend_changed = pvcalls_back_changed, |
| 1199 | }; |
Stefano Stabellini | 9be0733 | 2017-07-05 13:08:48 -0700 | [diff] [blame] | 1200 | |
| 1201 | static int __init pvcalls_back_init(void) |
| 1202 | { |
| 1203 | int ret; |
| 1204 | |
| 1205 | if (!xen_domain()) |
| 1206 | return -ENODEV; |
| 1207 | |
| 1208 | ret = xenbus_register_backend(&pvcalls_back_driver); |
| 1209 | if (ret < 0) |
| 1210 | return ret; |
| 1211 | |
| 1212 | sema_init(&pvcalls_back_global.frontends_lock, 1); |
| 1213 | INIT_LIST_HEAD(&pvcalls_back_global.frontends); |
| 1214 | return 0; |
| 1215 | } |
| 1216 | module_init(pvcalls_back_init); |
Stefano Stabellini | 0a85d23 | 2017-07-06 11:01:07 -0700 | [diff] [blame] | 1217 | |
| 1218 | static void __exit pvcalls_back_fin(void) |
| 1219 | { |
| 1220 | struct pvcalls_fedata *fedata, *nfedata; |
| 1221 | |
| 1222 | down(&pvcalls_back_global.frontends_lock); |
| 1223 | list_for_each_entry_safe(fedata, nfedata, |
| 1224 | &pvcalls_back_global.frontends, list) { |
| 1225 | backend_disconnect(fedata->dev); |
| 1226 | } |
| 1227 | up(&pvcalls_back_global.frontends_lock); |
| 1228 | |
| 1229 | xenbus_unregister_driver(&pvcalls_back_driver); |
| 1230 | } |
| 1231 | |
| 1232 | module_exit(pvcalls_back_fin); |
Boris Ostrovsky | 24e7f84 | 2017-11-15 11:24:02 -0500 | [diff] [blame] | 1233 | |
| 1234 | MODULE_DESCRIPTION("Xen PV Calls backend driver"); |
| 1235 | MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>"); |
| 1236 | MODULE_LICENSE("GPL"); |