Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * (c) 2017 Stefano Stabellini <stefano@aporeto.com>
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/net.h>
0008 #include <linux/socket.h>
0009 
0010 #include <net/sock.h>
0011 
0012 #include <xen/events.h>
0013 #include <xen/grant_table.h>
0014 #include <xen/xen.h>
0015 #include <xen/xenbus.h>
0016 #include <xen/interface/io/pvcalls.h>
0017 
0018 #include "pvcalls-front.h"
0019 
0020 #define PVCALLS_INVALID_ID UINT_MAX
0021 #define PVCALLS_RING_ORDER XENBUS_MAX_RING_GRANT_ORDER
0022 #define PVCALLS_NR_RSP_PER_RING __CONST_RING_SIZE(xen_pvcalls, XEN_PAGE_SIZE)
0023 #define PVCALLS_FRONT_MAX_SPIN 5000
0024 
0025 static struct proto pvcalls_proto = {
0026     .name   = "PVCalls",
0027     .owner  = THIS_MODULE,
0028     .obj_size = sizeof(struct sock),
0029 };
0030 
0031 struct pvcalls_bedata {
0032     struct xen_pvcalls_front_ring ring;
0033     grant_ref_t ref;
0034     int irq;
0035 
0036     struct list_head socket_mappings;
0037     spinlock_t socket_lock;
0038 
0039     wait_queue_head_t inflight_req;
0040     struct xen_pvcalls_response rsp[PVCALLS_NR_RSP_PER_RING];
0041 };
0042 /* Only one front/back connection supported. */
0043 static struct xenbus_device *pvcalls_front_dev;
0044 static atomic_t pvcalls_refcount;
0045 
0046 /* first increment refcount, then proceed */
0047 #define pvcalls_enter() {               \
0048     atomic_inc(&pvcalls_refcount);      \
0049 }
0050 
0051 /* first complete other operations, then decrement refcount */
0052 #define pvcalls_exit() {                \
0053     atomic_dec(&pvcalls_refcount);      \
0054 }
0055 
0056 struct sock_mapping {
0057     bool active_socket;
0058     struct list_head list;
0059     struct socket *sock;
0060     atomic_t refcount;
0061     union {
0062         struct {
0063             int irq;
0064             grant_ref_t ref;
0065             struct pvcalls_data_intf *ring;
0066             struct pvcalls_data data;
0067             struct mutex in_mutex;
0068             struct mutex out_mutex;
0069 
0070             wait_queue_head_t inflight_conn_req;
0071         } active;
0072         struct {
0073         /*
0074          * Socket status, needs to be 64-bit aligned due to the
0075          * test_and_* functions which have this requirement on arm64.
0076          */
0077 #define PVCALLS_STATUS_UNINITALIZED  0
0078 #define PVCALLS_STATUS_BIND          1
0079 #define PVCALLS_STATUS_LISTEN        2
0080             uint8_t status __attribute__((aligned(8)));
0081         /*
0082          * Internal state-machine flags.
0083          * Only one accept operation can be inflight for a socket.
0084          * Only one poll operation can be inflight for a given socket.
0085          * flags needs to be 64-bit aligned due to the test_and_*
0086          * functions which have this requirement on arm64.
0087          */
0088 #define PVCALLS_FLAG_ACCEPT_INFLIGHT 0
0089 #define PVCALLS_FLAG_POLL_INFLIGHT   1
0090 #define PVCALLS_FLAG_POLL_RET        2
0091             uint8_t flags __attribute__((aligned(8)));
0092             uint32_t inflight_req_id;
0093             struct sock_mapping *accept_map;
0094             wait_queue_head_t inflight_accept_req;
0095         } passive;
0096     };
0097 };
0098 
0099 static inline struct sock_mapping *pvcalls_enter_sock(struct socket *sock)
0100 {
0101     struct sock_mapping *map;
0102 
0103     if (!pvcalls_front_dev ||
0104         dev_get_drvdata(&pvcalls_front_dev->dev) == NULL)
0105         return ERR_PTR(-ENOTCONN);
0106 
0107     map = (struct sock_mapping *)sock->sk->sk_send_head;
0108     if (map == NULL)
0109         return ERR_PTR(-ENOTSOCK);
0110 
0111     pvcalls_enter();
0112     atomic_inc(&map->refcount);
0113     return map;
0114 }
0115 
0116 static inline void pvcalls_exit_sock(struct socket *sock)
0117 {
0118     struct sock_mapping *map;
0119 
0120     map = (struct sock_mapping *)sock->sk->sk_send_head;
0121     atomic_dec(&map->refcount);
0122     pvcalls_exit();
0123 }
0124 
0125 static inline int get_request(struct pvcalls_bedata *bedata, int *req_id)
0126 {
0127     *req_id = bedata->ring.req_prod_pvt & (RING_SIZE(&bedata->ring) - 1);
0128     if (RING_FULL(&bedata->ring) ||
0129         bedata->rsp[*req_id].req_id != PVCALLS_INVALID_ID)
0130         return -EAGAIN;
0131     return 0;
0132 }
0133 
0134 static bool pvcalls_front_write_todo(struct sock_mapping *map)
0135 {
0136     struct pvcalls_data_intf *intf = map->active.ring;
0137     RING_IDX cons, prod, size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
0138     int32_t error;
0139 
0140     error = intf->out_error;
0141     if (error == -ENOTCONN)
0142         return false;
0143     if (error != 0)
0144         return true;
0145 
0146     cons = intf->out_cons;
0147     prod = intf->out_prod;
0148     return !!(size - pvcalls_queued(prod, cons, size));
0149 }
0150 
0151 static bool pvcalls_front_read_todo(struct sock_mapping *map)
0152 {
0153     struct pvcalls_data_intf *intf = map->active.ring;
0154     RING_IDX cons, prod;
0155     int32_t error;
0156 
0157     cons = intf->in_cons;
0158     prod = intf->in_prod;
0159     error = intf->in_error;
0160     return (error != 0 ||
0161         pvcalls_queued(prod, cons,
0162                    XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER)) != 0);
0163 }
0164 
0165 static irqreturn_t pvcalls_front_event_handler(int irq, void *dev_id)
0166 {
0167     struct xenbus_device *dev = dev_id;
0168     struct pvcalls_bedata *bedata;
0169     struct xen_pvcalls_response *rsp;
0170     uint8_t *src, *dst;
0171     int req_id = 0, more = 0, done = 0;
0172 
0173     if (dev == NULL)
0174         return IRQ_HANDLED;
0175 
0176     pvcalls_enter();
0177     bedata = dev_get_drvdata(&dev->dev);
0178     if (bedata == NULL) {
0179         pvcalls_exit();
0180         return IRQ_HANDLED;
0181     }
0182 
0183 again:
0184     while (RING_HAS_UNCONSUMED_RESPONSES(&bedata->ring)) {
0185         rsp = RING_GET_RESPONSE(&bedata->ring, bedata->ring.rsp_cons);
0186 
0187         req_id = rsp->req_id;
0188         if (rsp->cmd == PVCALLS_POLL) {
0189             struct sock_mapping *map = (struct sock_mapping *)(uintptr_t)
0190                            rsp->u.poll.id;
0191 
0192             clear_bit(PVCALLS_FLAG_POLL_INFLIGHT,
0193                   (void *)&map->passive.flags);
0194             /*
0195              * clear INFLIGHT, then set RET. It pairs with
0196              * the checks at the beginning of
0197              * pvcalls_front_poll_passive.
0198              */
0199             smp_wmb();
0200             set_bit(PVCALLS_FLAG_POLL_RET,
0201                 (void *)&map->passive.flags);
0202         } else {
0203             dst = (uint8_t *)&bedata->rsp[req_id] +
0204                   sizeof(rsp->req_id);
0205             src = (uint8_t *)rsp + sizeof(rsp->req_id);
0206             memcpy(dst, src, sizeof(*rsp) - sizeof(rsp->req_id));
0207             /*
0208              * First copy the rest of the data, then req_id. It is
0209              * paired with the barrier when accessing bedata->rsp.
0210              */
0211             smp_wmb();
0212             bedata->rsp[req_id].req_id = req_id;
0213         }
0214 
0215         done = 1;
0216         bedata->ring.rsp_cons++;
0217     }
0218 
0219     RING_FINAL_CHECK_FOR_RESPONSES(&bedata->ring, more);
0220     if (more)
0221         goto again;
0222     if (done)
0223         wake_up(&bedata->inflight_req);
0224     pvcalls_exit();
0225     return IRQ_HANDLED;
0226 }
0227 
0228 static void pvcalls_front_free_map(struct pvcalls_bedata *bedata,
0229                    struct sock_mapping *map)
0230 {
0231     int i;
0232 
0233     unbind_from_irqhandler(map->active.irq, map);
0234 
0235     spin_lock(&bedata->socket_lock);
0236     if (!list_empty(&map->list))
0237         list_del_init(&map->list);
0238     spin_unlock(&bedata->socket_lock);
0239 
0240     for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
0241         gnttab_end_foreign_access(map->active.ring->ref[i], NULL);
0242     gnttab_end_foreign_access(map->active.ref, NULL);
0243     free_page((unsigned long)map->active.ring);
0244 
0245     kfree(map);
0246 }
0247 
0248 static irqreturn_t pvcalls_front_conn_handler(int irq, void *sock_map)
0249 {
0250     struct sock_mapping *map = sock_map;
0251 
0252     if (map == NULL)
0253         return IRQ_HANDLED;
0254 
0255     wake_up_interruptible(&map->active.inflight_conn_req);
0256 
0257     return IRQ_HANDLED;
0258 }
0259 
0260 int pvcalls_front_socket(struct socket *sock)
0261 {
0262     struct pvcalls_bedata *bedata;
0263     struct sock_mapping *map = NULL;
0264     struct xen_pvcalls_request *req;
0265     int notify, req_id, ret;
0266 
0267     /*
0268      * PVCalls only supports domain AF_INET,
0269      * type SOCK_STREAM and protocol 0 sockets for now.
0270      *
0271      * Check socket type here, AF_INET and protocol checks are done
0272      * by the caller.
0273      */
0274     if (sock->type != SOCK_STREAM)
0275         return -EOPNOTSUPP;
0276 
0277     pvcalls_enter();
0278     if (!pvcalls_front_dev) {
0279         pvcalls_exit();
0280         return -EACCES;
0281     }
0282     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
0283 
0284     map = kzalloc(sizeof(*map), GFP_KERNEL);
0285     if (map == NULL) {
0286         pvcalls_exit();
0287         return -ENOMEM;
0288     }
0289 
0290     spin_lock(&bedata->socket_lock);
0291 
0292     ret = get_request(bedata, &req_id);
0293     if (ret < 0) {
0294         kfree(map);
0295         spin_unlock(&bedata->socket_lock);
0296         pvcalls_exit();
0297         return ret;
0298     }
0299 
0300     /*
0301      * sock->sk->sk_send_head is not used for ip sockets: reuse the
0302      * field to store a pointer to the struct sock_mapping
0303      * corresponding to the socket. This way, we can easily get the
0304      * struct sock_mapping from the struct socket.
0305      */
0306     sock->sk->sk_send_head = (void *)map;
0307     list_add_tail(&map->list, &bedata->socket_mappings);
0308 
0309     req = RING_GET_REQUEST(&bedata->ring, req_id);
0310     req->req_id = req_id;
0311     req->cmd = PVCALLS_SOCKET;
0312     req->u.socket.id = (uintptr_t) map;
0313     req->u.socket.domain = AF_INET;
0314     req->u.socket.type = SOCK_STREAM;
0315     req->u.socket.protocol = IPPROTO_IP;
0316 
0317     bedata->ring.req_prod_pvt++;
0318     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
0319     spin_unlock(&bedata->socket_lock);
0320     if (notify)
0321         notify_remote_via_irq(bedata->irq);
0322 
0323     wait_event(bedata->inflight_req,
0324            READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
0325 
0326     /* read req_id, then the content */
0327     smp_rmb();
0328     ret = bedata->rsp[req_id].ret;
0329     bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
0330 
0331     pvcalls_exit();
0332     return ret;
0333 }
0334 
0335 static void free_active_ring(struct sock_mapping *map)
0336 {
0337     if (!map->active.ring)
0338         return;
0339 
0340     free_pages_exact(map->active.data.in,
0341              PAGE_SIZE << map->active.ring->ring_order);
0342     free_page((unsigned long)map->active.ring);
0343 }
0344 
0345 static int alloc_active_ring(struct sock_mapping *map)
0346 {
0347     void *bytes;
0348 
0349     map->active.ring = (struct pvcalls_data_intf *)
0350         get_zeroed_page(GFP_KERNEL);
0351     if (!map->active.ring)
0352         goto out;
0353 
0354     map->active.ring->ring_order = PVCALLS_RING_ORDER;
0355     bytes = alloc_pages_exact(PAGE_SIZE << PVCALLS_RING_ORDER,
0356                   GFP_KERNEL | __GFP_ZERO);
0357     if (!bytes)
0358         goto out;
0359 
0360     map->active.data.in = bytes;
0361     map->active.data.out = bytes +
0362         XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
0363 
0364     return 0;
0365 
0366 out:
0367     free_active_ring(map);
0368     return -ENOMEM;
0369 }
0370 
0371 static int create_active(struct sock_mapping *map, evtchn_port_t *evtchn)
0372 {
0373     void *bytes;
0374     int ret, irq = -1, i;
0375 
0376     *evtchn = 0;
0377     init_waitqueue_head(&map->active.inflight_conn_req);
0378 
0379     bytes = map->active.data.in;
0380     for (i = 0; i < (1 << PVCALLS_RING_ORDER); i++)
0381         map->active.ring->ref[i] = gnttab_grant_foreign_access(
0382             pvcalls_front_dev->otherend_id,
0383             pfn_to_gfn(virt_to_pfn(bytes) + i), 0);
0384 
0385     map->active.ref = gnttab_grant_foreign_access(
0386         pvcalls_front_dev->otherend_id,
0387         pfn_to_gfn(virt_to_pfn((void *)map->active.ring)), 0);
0388 
0389     ret = xenbus_alloc_evtchn(pvcalls_front_dev, evtchn);
0390     if (ret)
0391         goto out_error;
0392     irq = bind_evtchn_to_irqhandler(*evtchn, pvcalls_front_conn_handler,
0393                     0, "pvcalls-frontend", map);
0394     if (irq < 0) {
0395         ret = irq;
0396         goto out_error;
0397     }
0398 
0399     map->active.irq = irq;
0400     map->active_socket = true;
0401     mutex_init(&map->active.in_mutex);
0402     mutex_init(&map->active.out_mutex);
0403 
0404     return 0;
0405 
0406 out_error:
0407     if (*evtchn > 0)
0408         xenbus_free_evtchn(pvcalls_front_dev, *evtchn);
0409     return ret;
0410 }
0411 
0412 int pvcalls_front_connect(struct socket *sock, struct sockaddr *addr,
0413                 int addr_len, int flags)
0414 {
0415     struct pvcalls_bedata *bedata;
0416     struct sock_mapping *map = NULL;
0417     struct xen_pvcalls_request *req;
0418     int notify, req_id, ret;
0419     evtchn_port_t evtchn;
0420 
0421     if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
0422         return -EOPNOTSUPP;
0423 
0424     map = pvcalls_enter_sock(sock);
0425     if (IS_ERR(map))
0426         return PTR_ERR(map);
0427 
0428     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
0429     ret = alloc_active_ring(map);
0430     if (ret < 0) {
0431         pvcalls_exit_sock(sock);
0432         return ret;
0433     }
0434 
0435     spin_lock(&bedata->socket_lock);
0436     ret = get_request(bedata, &req_id);
0437     if (ret < 0) {
0438         spin_unlock(&bedata->socket_lock);
0439         free_active_ring(map);
0440         pvcalls_exit_sock(sock);
0441         return ret;
0442     }
0443     ret = create_active(map, &evtchn);
0444     if (ret < 0) {
0445         spin_unlock(&bedata->socket_lock);
0446         free_active_ring(map);
0447         pvcalls_exit_sock(sock);
0448         return ret;
0449     }
0450 
0451     req = RING_GET_REQUEST(&bedata->ring, req_id);
0452     req->req_id = req_id;
0453     req->cmd = PVCALLS_CONNECT;
0454     req->u.connect.id = (uintptr_t)map;
0455     req->u.connect.len = addr_len;
0456     req->u.connect.flags = flags;
0457     req->u.connect.ref = map->active.ref;
0458     req->u.connect.evtchn = evtchn;
0459     memcpy(req->u.connect.addr, addr, sizeof(*addr));
0460 
0461     map->sock = sock;
0462 
0463     bedata->ring.req_prod_pvt++;
0464     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
0465     spin_unlock(&bedata->socket_lock);
0466 
0467     if (notify)
0468         notify_remote_via_irq(bedata->irq);
0469 
0470     wait_event(bedata->inflight_req,
0471            READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
0472 
0473     /* read req_id, then the content */
0474     smp_rmb();
0475     ret = bedata->rsp[req_id].ret;
0476     bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
0477     pvcalls_exit_sock(sock);
0478     return ret;
0479 }
0480 
0481 static int __write_ring(struct pvcalls_data_intf *intf,
0482             struct pvcalls_data *data,
0483             struct iov_iter *msg_iter,
0484             int len)
0485 {
0486     RING_IDX cons, prod, size, masked_prod, masked_cons;
0487     RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
0488     int32_t error;
0489 
0490     error = intf->out_error;
0491     if (error < 0)
0492         return error;
0493     cons = intf->out_cons;
0494     prod = intf->out_prod;
0495     /* read indexes before continuing */
0496     virt_mb();
0497 
0498     size = pvcalls_queued(prod, cons, array_size);
0499     if (size > array_size)
0500         return -EINVAL;
0501     if (size == array_size)
0502         return 0;
0503     if (len > array_size - size)
0504         len = array_size - size;
0505 
0506     masked_prod = pvcalls_mask(prod, array_size);
0507     masked_cons = pvcalls_mask(cons, array_size);
0508 
0509     if (masked_prod < masked_cons) {
0510         len = copy_from_iter(data->out + masked_prod, len, msg_iter);
0511     } else {
0512         if (len > array_size - masked_prod) {
0513             int ret = copy_from_iter(data->out + masked_prod,
0514                        array_size - masked_prod, msg_iter);
0515             if (ret != array_size - masked_prod) {
0516                 len = ret;
0517                 goto out;
0518             }
0519             len = ret + copy_from_iter(data->out, len - ret, msg_iter);
0520         } else {
0521             len = copy_from_iter(data->out + masked_prod, len, msg_iter);
0522         }
0523     }
0524 out:
0525     /* write to ring before updating pointer */
0526     virt_wmb();
0527     intf->out_prod += len;
0528 
0529     return len;
0530 }
0531 
0532 int pvcalls_front_sendmsg(struct socket *sock, struct msghdr *msg,
0533               size_t len)
0534 {
0535     struct sock_mapping *map;
0536     int sent, tot_sent = 0;
0537     int count = 0, flags;
0538 
0539     flags = msg->msg_flags;
0540     if (flags & (MSG_CONFIRM|MSG_DONTROUTE|MSG_EOR|MSG_OOB))
0541         return -EOPNOTSUPP;
0542 
0543     map = pvcalls_enter_sock(sock);
0544     if (IS_ERR(map))
0545         return PTR_ERR(map);
0546 
0547     mutex_lock(&map->active.out_mutex);
0548     if ((flags & MSG_DONTWAIT) && !pvcalls_front_write_todo(map)) {
0549         mutex_unlock(&map->active.out_mutex);
0550         pvcalls_exit_sock(sock);
0551         return -EAGAIN;
0552     }
0553     if (len > INT_MAX)
0554         len = INT_MAX;
0555 
0556 again:
0557     count++;
0558     sent = __write_ring(map->active.ring,
0559                 &map->active.data, &msg->msg_iter,
0560                 len);
0561     if (sent > 0) {
0562         len -= sent;
0563         tot_sent += sent;
0564         notify_remote_via_irq(map->active.irq);
0565     }
0566     if (sent >= 0 && len > 0 && count < PVCALLS_FRONT_MAX_SPIN)
0567         goto again;
0568     if (sent < 0)
0569         tot_sent = sent;
0570 
0571     mutex_unlock(&map->active.out_mutex);
0572     pvcalls_exit_sock(sock);
0573     return tot_sent;
0574 }
0575 
0576 static int __read_ring(struct pvcalls_data_intf *intf,
0577                struct pvcalls_data *data,
0578                struct iov_iter *msg_iter,
0579                size_t len, int flags)
0580 {
0581     RING_IDX cons, prod, size, masked_prod, masked_cons;
0582     RING_IDX array_size = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
0583     int32_t error;
0584 
0585     cons = intf->in_cons;
0586     prod = intf->in_prod;
0587     error = intf->in_error;
0588     /* get pointers before reading from the ring */
0589     virt_rmb();
0590 
0591     size = pvcalls_queued(prod, cons, array_size);
0592     masked_prod = pvcalls_mask(prod, array_size);
0593     masked_cons = pvcalls_mask(cons, array_size);
0594 
0595     if (size == 0)
0596         return error ?: size;
0597 
0598     if (len > size)
0599         len = size;
0600 
0601     if (masked_prod > masked_cons) {
0602         len = copy_to_iter(data->in + masked_cons, len, msg_iter);
0603     } else {
0604         if (len > (array_size - masked_cons)) {
0605             int ret = copy_to_iter(data->in + masked_cons,
0606                      array_size - masked_cons, msg_iter);
0607             if (ret != array_size - masked_cons) {
0608                 len = ret;
0609                 goto out;
0610             }
0611             len = ret + copy_to_iter(data->in, len - ret, msg_iter);
0612         } else {
0613             len = copy_to_iter(data->in + masked_cons, len, msg_iter);
0614         }
0615     }
0616 out:
0617     /* read data from the ring before increasing the index */
0618     virt_mb();
0619     if (!(flags & MSG_PEEK))
0620         intf->in_cons += len;
0621 
0622     return len;
0623 }
0624 
0625 int pvcalls_front_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
0626              int flags)
0627 {
0628     int ret;
0629     struct sock_mapping *map;
0630 
0631     if (flags & (MSG_CMSG_CLOEXEC|MSG_ERRQUEUE|MSG_OOB|MSG_TRUNC))
0632         return -EOPNOTSUPP;
0633 
0634     map = pvcalls_enter_sock(sock);
0635     if (IS_ERR(map))
0636         return PTR_ERR(map);
0637 
0638     mutex_lock(&map->active.in_mutex);
0639     if (len > XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER))
0640         len = XEN_FLEX_RING_SIZE(PVCALLS_RING_ORDER);
0641 
0642     while (!(flags & MSG_DONTWAIT) && !pvcalls_front_read_todo(map)) {
0643         wait_event_interruptible(map->active.inflight_conn_req,
0644                      pvcalls_front_read_todo(map));
0645     }
0646     ret = __read_ring(map->active.ring, &map->active.data,
0647               &msg->msg_iter, len, flags);
0648 
0649     if (ret > 0)
0650         notify_remote_via_irq(map->active.irq);
0651     if (ret == 0)
0652         ret = (flags & MSG_DONTWAIT) ? -EAGAIN : 0;
0653     if (ret == -ENOTCONN)
0654         ret = 0;
0655 
0656     mutex_unlock(&map->active.in_mutex);
0657     pvcalls_exit_sock(sock);
0658     return ret;
0659 }
0660 
0661 int pvcalls_front_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
0662 {
0663     struct pvcalls_bedata *bedata;
0664     struct sock_mapping *map = NULL;
0665     struct xen_pvcalls_request *req;
0666     int notify, req_id, ret;
0667 
0668     if (addr->sa_family != AF_INET || sock->type != SOCK_STREAM)
0669         return -EOPNOTSUPP;
0670 
0671     map = pvcalls_enter_sock(sock);
0672     if (IS_ERR(map))
0673         return PTR_ERR(map);
0674     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
0675 
0676     spin_lock(&bedata->socket_lock);
0677     ret = get_request(bedata, &req_id);
0678     if (ret < 0) {
0679         spin_unlock(&bedata->socket_lock);
0680         pvcalls_exit_sock(sock);
0681         return ret;
0682     }
0683     req = RING_GET_REQUEST(&bedata->ring, req_id);
0684     req->req_id = req_id;
0685     map->sock = sock;
0686     req->cmd = PVCALLS_BIND;
0687     req->u.bind.id = (uintptr_t)map;
0688     memcpy(req->u.bind.addr, addr, sizeof(*addr));
0689     req->u.bind.len = addr_len;
0690 
0691     init_waitqueue_head(&map->passive.inflight_accept_req);
0692 
0693     map->active_socket = false;
0694 
0695     bedata->ring.req_prod_pvt++;
0696     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
0697     spin_unlock(&bedata->socket_lock);
0698     if (notify)
0699         notify_remote_via_irq(bedata->irq);
0700 
0701     wait_event(bedata->inflight_req,
0702            READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
0703 
0704     /* read req_id, then the content */
0705     smp_rmb();
0706     ret = bedata->rsp[req_id].ret;
0707     bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
0708 
0709     map->passive.status = PVCALLS_STATUS_BIND;
0710     pvcalls_exit_sock(sock);
0711     return 0;
0712 }
0713 
0714 int pvcalls_front_listen(struct socket *sock, int backlog)
0715 {
0716     struct pvcalls_bedata *bedata;
0717     struct sock_mapping *map;
0718     struct xen_pvcalls_request *req;
0719     int notify, req_id, ret;
0720 
0721     map = pvcalls_enter_sock(sock);
0722     if (IS_ERR(map))
0723         return PTR_ERR(map);
0724     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
0725 
0726     if (map->passive.status != PVCALLS_STATUS_BIND) {
0727         pvcalls_exit_sock(sock);
0728         return -EOPNOTSUPP;
0729     }
0730 
0731     spin_lock(&bedata->socket_lock);
0732     ret = get_request(bedata, &req_id);
0733     if (ret < 0) {
0734         spin_unlock(&bedata->socket_lock);
0735         pvcalls_exit_sock(sock);
0736         return ret;
0737     }
0738     req = RING_GET_REQUEST(&bedata->ring, req_id);
0739     req->req_id = req_id;
0740     req->cmd = PVCALLS_LISTEN;
0741     req->u.listen.id = (uintptr_t) map;
0742     req->u.listen.backlog = backlog;
0743 
0744     bedata->ring.req_prod_pvt++;
0745     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
0746     spin_unlock(&bedata->socket_lock);
0747     if (notify)
0748         notify_remote_via_irq(bedata->irq);
0749 
0750     wait_event(bedata->inflight_req,
0751            READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
0752 
0753     /* read req_id, then the content */
0754     smp_rmb();
0755     ret = bedata->rsp[req_id].ret;
0756     bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
0757 
0758     map->passive.status = PVCALLS_STATUS_LISTEN;
0759     pvcalls_exit_sock(sock);
0760     return ret;
0761 }
0762 
0763 int pvcalls_front_accept(struct socket *sock, struct socket *newsock, int flags)
0764 {
0765     struct pvcalls_bedata *bedata;
0766     struct sock_mapping *map;
0767     struct sock_mapping *map2 = NULL;
0768     struct xen_pvcalls_request *req;
0769     int notify, req_id, ret, nonblock;
0770     evtchn_port_t evtchn;
0771 
0772     map = pvcalls_enter_sock(sock);
0773     if (IS_ERR(map))
0774         return PTR_ERR(map);
0775     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
0776 
0777     if (map->passive.status != PVCALLS_STATUS_LISTEN) {
0778         pvcalls_exit_sock(sock);
0779         return -EINVAL;
0780     }
0781 
0782     nonblock = flags & SOCK_NONBLOCK;
0783     /*
0784      * Backend only supports 1 inflight accept request, will return
0785      * errors for the others
0786      */
0787     if (test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0788                  (void *)&map->passive.flags)) {
0789         req_id = READ_ONCE(map->passive.inflight_req_id);
0790         if (req_id != PVCALLS_INVALID_ID &&
0791             READ_ONCE(bedata->rsp[req_id].req_id) == req_id) {
0792             map2 = map->passive.accept_map;
0793             goto received;
0794         }
0795         if (nonblock) {
0796             pvcalls_exit_sock(sock);
0797             return -EAGAIN;
0798         }
0799         if (wait_event_interruptible(map->passive.inflight_accept_req,
0800             !test_and_set_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0801                       (void *)&map->passive.flags))) {
0802             pvcalls_exit_sock(sock);
0803             return -EINTR;
0804         }
0805     }
0806 
0807     map2 = kzalloc(sizeof(*map2), GFP_KERNEL);
0808     if (map2 == NULL) {
0809         clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0810               (void *)&map->passive.flags);
0811         pvcalls_exit_sock(sock);
0812         return -ENOMEM;
0813     }
0814     ret = alloc_active_ring(map2);
0815     if (ret < 0) {
0816         clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0817                 (void *)&map->passive.flags);
0818         kfree(map2);
0819         pvcalls_exit_sock(sock);
0820         return ret;
0821     }
0822     spin_lock(&bedata->socket_lock);
0823     ret = get_request(bedata, &req_id);
0824     if (ret < 0) {
0825         clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0826               (void *)&map->passive.flags);
0827         spin_unlock(&bedata->socket_lock);
0828         free_active_ring(map2);
0829         kfree(map2);
0830         pvcalls_exit_sock(sock);
0831         return ret;
0832     }
0833 
0834     ret = create_active(map2, &evtchn);
0835     if (ret < 0) {
0836         free_active_ring(map2);
0837         kfree(map2);
0838         clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0839               (void *)&map->passive.flags);
0840         spin_unlock(&bedata->socket_lock);
0841         pvcalls_exit_sock(sock);
0842         return ret;
0843     }
0844     list_add_tail(&map2->list, &bedata->socket_mappings);
0845 
0846     req = RING_GET_REQUEST(&bedata->ring, req_id);
0847     req->req_id = req_id;
0848     req->cmd = PVCALLS_ACCEPT;
0849     req->u.accept.id = (uintptr_t) map;
0850     req->u.accept.ref = map2->active.ref;
0851     req->u.accept.id_new = (uintptr_t) map2;
0852     req->u.accept.evtchn = evtchn;
0853     map->passive.accept_map = map2;
0854 
0855     bedata->ring.req_prod_pvt++;
0856     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
0857     spin_unlock(&bedata->socket_lock);
0858     if (notify)
0859         notify_remote_via_irq(bedata->irq);
0860     /* We could check if we have received a response before returning. */
0861     if (nonblock) {
0862         WRITE_ONCE(map->passive.inflight_req_id, req_id);
0863         pvcalls_exit_sock(sock);
0864         return -EAGAIN;
0865     }
0866 
0867     if (wait_event_interruptible(bedata->inflight_req,
0868         READ_ONCE(bedata->rsp[req_id].req_id) == req_id)) {
0869         pvcalls_exit_sock(sock);
0870         return -EINTR;
0871     }
0872     /* read req_id, then the content */
0873     smp_rmb();
0874 
0875 received:
0876     map2->sock = newsock;
0877     newsock->sk = sk_alloc(sock_net(sock->sk), PF_INET, GFP_KERNEL, &pvcalls_proto, false);
0878     if (!newsock->sk) {
0879         bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
0880         map->passive.inflight_req_id = PVCALLS_INVALID_ID;
0881         clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0882               (void *)&map->passive.flags);
0883         pvcalls_front_free_map(bedata, map2);
0884         pvcalls_exit_sock(sock);
0885         return -ENOMEM;
0886     }
0887     newsock->sk->sk_send_head = (void *)map2;
0888 
0889     ret = bedata->rsp[req_id].ret;
0890     bedata->rsp[req_id].req_id = PVCALLS_INVALID_ID;
0891     map->passive.inflight_req_id = PVCALLS_INVALID_ID;
0892 
0893     clear_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT, (void *)&map->passive.flags);
0894     wake_up(&map->passive.inflight_accept_req);
0895 
0896     pvcalls_exit_sock(sock);
0897     return ret;
0898 }
0899 
0900 static __poll_t pvcalls_front_poll_passive(struct file *file,
0901                            struct pvcalls_bedata *bedata,
0902                            struct sock_mapping *map,
0903                            poll_table *wait)
0904 {
0905     int notify, req_id, ret;
0906     struct xen_pvcalls_request *req;
0907 
0908     if (test_bit(PVCALLS_FLAG_ACCEPT_INFLIGHT,
0909              (void *)&map->passive.flags)) {
0910         uint32_t req_id = READ_ONCE(map->passive.inflight_req_id);
0911 
0912         if (req_id != PVCALLS_INVALID_ID &&
0913             READ_ONCE(bedata->rsp[req_id].req_id) == req_id)
0914             return EPOLLIN | EPOLLRDNORM;
0915 
0916         poll_wait(file, &map->passive.inflight_accept_req, wait);
0917         return 0;
0918     }
0919 
0920     if (test_and_clear_bit(PVCALLS_FLAG_POLL_RET,
0921                    (void *)&map->passive.flags))
0922         return EPOLLIN | EPOLLRDNORM;
0923 
0924     /*
0925      * First check RET, then INFLIGHT. No barriers necessary to
0926      * ensure execution ordering because of the conditional
0927      * instructions creating control dependencies.
0928      */
0929 
0930     if (test_and_set_bit(PVCALLS_FLAG_POLL_INFLIGHT,
0931                  (void *)&map->passive.flags)) {
0932         poll_wait(file, &bedata->inflight_req, wait);
0933         return 0;
0934     }
0935 
0936     spin_lock(&bedata->socket_lock);
0937     ret = get_request(bedata, &req_id);
0938     if (ret < 0) {
0939         spin_unlock(&bedata->socket_lock);
0940         return ret;
0941     }
0942     req = RING_GET_REQUEST(&bedata->ring, req_id);
0943     req->req_id = req_id;
0944     req->cmd = PVCALLS_POLL;
0945     req->u.poll.id = (uintptr_t) map;
0946 
0947     bedata->ring.req_prod_pvt++;
0948     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
0949     spin_unlock(&bedata->socket_lock);
0950     if (notify)
0951         notify_remote_via_irq(bedata->irq);
0952 
0953     poll_wait(file, &bedata->inflight_req, wait);
0954     return 0;
0955 }
0956 
0957 static __poll_t pvcalls_front_poll_active(struct file *file,
0958                           struct pvcalls_bedata *bedata,
0959                           struct sock_mapping *map,
0960                           poll_table *wait)
0961 {
0962     __poll_t mask = 0;
0963     int32_t in_error, out_error;
0964     struct pvcalls_data_intf *intf = map->active.ring;
0965 
0966     out_error = intf->out_error;
0967     in_error = intf->in_error;
0968 
0969     poll_wait(file, &map->active.inflight_conn_req, wait);
0970     if (pvcalls_front_write_todo(map))
0971         mask |= EPOLLOUT | EPOLLWRNORM;
0972     if (pvcalls_front_read_todo(map))
0973         mask |= EPOLLIN | EPOLLRDNORM;
0974     if (in_error != 0 || out_error != 0)
0975         mask |= EPOLLERR;
0976 
0977     return mask;
0978 }
0979 
0980 __poll_t pvcalls_front_poll(struct file *file, struct socket *sock,
0981                    poll_table *wait)
0982 {
0983     struct pvcalls_bedata *bedata;
0984     struct sock_mapping *map;
0985     __poll_t ret;
0986 
0987     map = pvcalls_enter_sock(sock);
0988     if (IS_ERR(map))
0989         return EPOLLNVAL;
0990     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
0991 
0992     if (map->active_socket)
0993         ret = pvcalls_front_poll_active(file, bedata, map, wait);
0994     else
0995         ret = pvcalls_front_poll_passive(file, bedata, map, wait);
0996     pvcalls_exit_sock(sock);
0997     return ret;
0998 }
0999 
1000 int pvcalls_front_release(struct socket *sock)
1001 {
1002     struct pvcalls_bedata *bedata;
1003     struct sock_mapping *map;
1004     int req_id, notify, ret;
1005     struct xen_pvcalls_request *req;
1006 
1007     if (sock->sk == NULL)
1008         return 0;
1009 
1010     map = pvcalls_enter_sock(sock);
1011     if (IS_ERR(map)) {
1012         if (PTR_ERR(map) == -ENOTCONN)
1013             return -EIO;
1014         else
1015             return 0;
1016     }
1017     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
1018 
1019     spin_lock(&bedata->socket_lock);
1020     ret = get_request(bedata, &req_id);
1021     if (ret < 0) {
1022         spin_unlock(&bedata->socket_lock);
1023         pvcalls_exit_sock(sock);
1024         return ret;
1025     }
1026     sock->sk->sk_send_head = NULL;
1027 
1028     req = RING_GET_REQUEST(&bedata->ring, req_id);
1029     req->req_id = req_id;
1030     req->cmd = PVCALLS_RELEASE;
1031     req->u.release.id = (uintptr_t)map;
1032 
1033     bedata->ring.req_prod_pvt++;
1034     RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&bedata->ring, notify);
1035     spin_unlock(&bedata->socket_lock);
1036     if (notify)
1037         notify_remote_via_irq(bedata->irq);
1038 
1039     wait_event(bedata->inflight_req,
1040            READ_ONCE(bedata->rsp[req_id].req_id) == req_id);
1041 
1042     if (map->active_socket) {
1043         /*
1044          * Set in_error and wake up inflight_conn_req to force
1045          * recvmsg waiters to exit.
1046          */
1047         map->active.ring->in_error = -EBADF;
1048         wake_up_interruptible(&map->active.inflight_conn_req);
1049 
1050         /*
1051          * We need to make sure that sendmsg/recvmsg on this socket have
1052          * not started before we've cleared sk_send_head here. The
1053          * easiest way to guarantee this is to see that no pvcalls
1054          * (other than us) is in progress on this socket.
1055          */
1056         while (atomic_read(&map->refcount) > 1)
1057             cpu_relax();
1058 
1059         pvcalls_front_free_map(bedata, map);
1060     } else {
1061         wake_up(&bedata->inflight_req);
1062         wake_up(&map->passive.inflight_accept_req);
1063 
1064         while (atomic_read(&map->refcount) > 1)
1065             cpu_relax();
1066 
1067         spin_lock(&bedata->socket_lock);
1068         list_del(&map->list);
1069         spin_unlock(&bedata->socket_lock);
1070         if (READ_ONCE(map->passive.inflight_req_id) != PVCALLS_INVALID_ID &&
1071             READ_ONCE(map->passive.inflight_req_id) != 0) {
1072             pvcalls_front_free_map(bedata,
1073                            map->passive.accept_map);
1074         }
1075         kfree(map);
1076     }
1077     WRITE_ONCE(bedata->rsp[req_id].req_id, PVCALLS_INVALID_ID);
1078 
1079     pvcalls_exit();
1080     return 0;
1081 }
1082 
1083 static const struct xenbus_device_id pvcalls_front_ids[] = {
1084     { "pvcalls" },
1085     { "" }
1086 };
1087 
1088 static int pvcalls_front_remove(struct xenbus_device *dev)
1089 {
1090     struct pvcalls_bedata *bedata;
1091     struct sock_mapping *map = NULL, *n;
1092 
1093     bedata = dev_get_drvdata(&pvcalls_front_dev->dev);
1094     dev_set_drvdata(&dev->dev, NULL);
1095     pvcalls_front_dev = NULL;
1096     if (bedata->irq >= 0)
1097         unbind_from_irqhandler(bedata->irq, dev);
1098 
1099     list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1100         map->sock->sk->sk_send_head = NULL;
1101         if (map->active_socket) {
1102             map->active.ring->in_error = -EBADF;
1103             wake_up_interruptible(&map->active.inflight_conn_req);
1104         }
1105     }
1106 
1107     smp_mb();
1108     while (atomic_read(&pvcalls_refcount) > 0)
1109         cpu_relax();
1110     list_for_each_entry_safe(map, n, &bedata->socket_mappings, list) {
1111         if (map->active_socket) {
1112             /* No need to lock, refcount is 0 */
1113             pvcalls_front_free_map(bedata, map);
1114         } else {
1115             list_del(&map->list);
1116             kfree(map);
1117         }
1118     }
1119     if (bedata->ref != -1)
1120         gnttab_end_foreign_access(bedata->ref, NULL);
1121     kfree(bedata->ring.sring);
1122     kfree(bedata);
1123     xenbus_switch_state(dev, XenbusStateClosed);
1124     return 0;
1125 }
1126 
1127 static int pvcalls_front_probe(struct xenbus_device *dev,
1128               const struct xenbus_device_id *id)
1129 {
1130     int ret = -ENOMEM, i;
1131     evtchn_port_t evtchn;
1132     unsigned int max_page_order, function_calls, len;
1133     char *versions;
1134     grant_ref_t gref_head = 0;
1135     struct xenbus_transaction xbt;
1136     struct pvcalls_bedata *bedata = NULL;
1137     struct xen_pvcalls_sring *sring;
1138 
1139     if (pvcalls_front_dev != NULL) {
1140         dev_err(&dev->dev, "only one PV Calls connection supported\n");
1141         return -EINVAL;
1142     }
1143 
1144     versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len);
1145     if (IS_ERR(versions))
1146         return PTR_ERR(versions);
1147     if (!len)
1148         return -EINVAL;
1149     if (strcmp(versions, "1")) {
1150         kfree(versions);
1151         return -EINVAL;
1152     }
1153     kfree(versions);
1154     max_page_order = xenbus_read_unsigned(dev->otherend,
1155                           "max-page-order", 0);
1156     if (max_page_order < PVCALLS_RING_ORDER)
1157         return -ENODEV;
1158     function_calls = xenbus_read_unsigned(dev->otherend,
1159                           "function-calls", 0);
1160     /* See XENBUS_FUNCTIONS_CALLS in pvcalls.h */
1161     if (function_calls != 1)
1162         return -ENODEV;
1163     pr_info("%s max-page-order is %u\n", __func__, max_page_order);
1164 
1165     bedata = kzalloc(sizeof(struct pvcalls_bedata), GFP_KERNEL);
1166     if (!bedata)
1167         return -ENOMEM;
1168 
1169     dev_set_drvdata(&dev->dev, bedata);
1170     pvcalls_front_dev = dev;
1171     init_waitqueue_head(&bedata->inflight_req);
1172     INIT_LIST_HEAD(&bedata->socket_mappings);
1173     spin_lock_init(&bedata->socket_lock);
1174     bedata->irq = -1;
1175     bedata->ref = -1;
1176 
1177     for (i = 0; i < PVCALLS_NR_RSP_PER_RING; i++)
1178         bedata->rsp[i].req_id = PVCALLS_INVALID_ID;
1179 
1180     sring = (struct xen_pvcalls_sring *) __get_free_page(GFP_KERNEL |
1181                                  __GFP_ZERO);
1182     if (!sring)
1183         goto error;
1184     SHARED_RING_INIT(sring);
1185     FRONT_RING_INIT(&bedata->ring, sring, XEN_PAGE_SIZE);
1186 
1187     ret = xenbus_alloc_evtchn(dev, &evtchn);
1188     if (ret)
1189         goto error;
1190 
1191     bedata->irq = bind_evtchn_to_irqhandler(evtchn,
1192                         pvcalls_front_event_handler,
1193                         0, "pvcalls-frontend", dev);
1194     if (bedata->irq < 0) {
1195         ret = bedata->irq;
1196         goto error;
1197     }
1198 
1199     ret = gnttab_alloc_grant_references(1, &gref_head);
1200     if (ret < 0)
1201         goto error;
1202     ret = gnttab_claim_grant_reference(&gref_head);
1203     if (ret < 0)
1204         goto error;
1205     bedata->ref = ret;
1206     gnttab_grant_foreign_access_ref(bedata->ref, dev->otherend_id,
1207                     virt_to_gfn((void *)sring), 0);
1208 
1209  again:
1210     ret = xenbus_transaction_start(&xbt);
1211     if (ret) {
1212         xenbus_dev_fatal(dev, ret, "starting transaction");
1213         goto error;
1214     }
1215     ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1);
1216     if (ret)
1217         goto error_xenbus;
1218     ret = xenbus_printf(xbt, dev->nodename, "ring-ref", "%d", bedata->ref);
1219     if (ret)
1220         goto error_xenbus;
1221     ret = xenbus_printf(xbt, dev->nodename, "port", "%u",
1222                 evtchn);
1223     if (ret)
1224         goto error_xenbus;
1225     ret = xenbus_transaction_end(xbt, 0);
1226     if (ret) {
1227         if (ret == -EAGAIN)
1228             goto again;
1229         xenbus_dev_fatal(dev, ret, "completing transaction");
1230         goto error;
1231     }
1232     xenbus_switch_state(dev, XenbusStateInitialised);
1233 
1234     return 0;
1235 
1236  error_xenbus:
1237     xenbus_transaction_end(xbt, 1);
1238     xenbus_dev_fatal(dev, ret, "writing xenstore");
1239  error:
1240     pvcalls_front_remove(dev);
1241     return ret;
1242 }
1243 
1244 static void pvcalls_front_changed(struct xenbus_device *dev,
1245                 enum xenbus_state backend_state)
1246 {
1247     switch (backend_state) {
1248     case XenbusStateReconfiguring:
1249     case XenbusStateReconfigured:
1250     case XenbusStateInitialising:
1251     case XenbusStateInitialised:
1252     case XenbusStateUnknown:
1253         break;
1254 
1255     case XenbusStateInitWait:
1256         break;
1257 
1258     case XenbusStateConnected:
1259         xenbus_switch_state(dev, XenbusStateConnected);
1260         break;
1261 
1262     case XenbusStateClosed:
1263         if (dev->state == XenbusStateClosed)
1264             break;
1265         /* Missed the backend's CLOSING state */
1266         fallthrough;
1267     case XenbusStateClosing:
1268         xenbus_frontend_closed(dev);
1269         break;
1270     }
1271 }
1272 
1273 static struct xenbus_driver pvcalls_front_driver = {
1274     .ids = pvcalls_front_ids,
1275     .probe = pvcalls_front_probe,
1276     .remove = pvcalls_front_remove,
1277     .otherend_changed = pvcalls_front_changed,
1278     .not_essential = true,
1279 };
1280 
1281 static int __init pvcalls_frontend_init(void)
1282 {
1283     if (!xen_domain())
1284         return -ENODEV;
1285 
1286     pr_info("Initialising Xen pvcalls frontend driver\n");
1287 
1288     return xenbus_register_frontend(&pvcalls_front_driver);
1289 }
1290 
1291 module_init(pvcalls_frontend_init);
1292 
1293 MODULE_DESCRIPTION("Xen PV Calls frontend driver");
1294 MODULE_AUTHOR("Stefano Stabellini <sstabellini@kernel.org>");
1295 MODULE_LICENSE("GPL");