linux/drivers/xen/xenbus/xenbus_client.c
<<
>>
Prefs
   1/******************************************************************************
   2 * Client-facing interface for the Xenbus driver.  In other words, the
   3 * interface between the Xenbus and the device-specific code, be it the
   4 * frontend or the backend of that driver.
   5 *
   6 * Copyright (C) 2005 XenSource Ltd
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License version 2
  10 * as published by the Free Software Foundation; or, when distributed
  11 * separately from the Linux kernel or incorporated into other
  12 * software packages, subject to the following license:
  13 *
  14 * Permission is hereby granted, free of charge, to any person obtaining a copy
  15 * of this source file (the "Software"), to deal in the Software without
  16 * restriction, including without limitation the rights to use, copy, modify,
  17 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18 * and to permit persons to whom the Software is furnished to do so, subject to
  19 * the following conditions:
  20 *
  21 * The above copyright notice and this permission notice shall be included in
  22 * all copies or substantial portions of the Software.
  23 *
  24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30 * IN THE SOFTWARE.
  31 */
  32
  33#include <linux/mm.h>
  34#include <linux/slab.h>
  35#include <linux/types.h>
  36#include <linux/spinlock.h>
  37#include <linux/vmalloc.h>
  38#include <linux/export.h>
  39#include <asm/xen/hypervisor.h>
  40#include <xen/page.h>
  41#include <xen/interface/xen.h>
  42#include <xen/interface/event_channel.h>
  43#include <xen/balloon.h>
  44#include <xen/events.h>
  45#include <xen/grant_table.h>
  46#include <xen/xenbus.h>
  47#include <xen/xen.h>
  48#include <xen/features.h>
  49
  50#include "xenbus.h"
  51
  52#define XENBUS_PAGES(_grants)   (DIV_ROUND_UP(_grants, XEN_PFN_PER_PAGE))
  53
  54#define XENBUS_MAX_RING_PAGES   (XENBUS_PAGES(XENBUS_MAX_RING_GRANTS))
  55
  56struct xenbus_map_node {
  57        struct list_head next;
  58        union {
  59                struct {
  60                        struct vm_struct *area;
  61                } pv;
  62                struct {
  63                        struct page *pages[XENBUS_MAX_RING_PAGES];
  64                        unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  65                        void *addr;
  66                } hvm;
  67        };
  68        grant_handle_t handles[XENBUS_MAX_RING_GRANTS];
  69        unsigned int   nr_handles;
  70};
  71
  72struct map_ring_valloc {
  73        struct xenbus_map_node *node;
  74
  75        /* Why do we need two arrays? See comment of __xenbus_map_ring */
  76        unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  77        phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];
  78
  79        struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS];
  80        struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
  81
  82        unsigned int idx;
  83};
  84
  85static DEFINE_SPINLOCK(xenbus_valloc_lock);
  86static LIST_HEAD(xenbus_valloc_pages);
  87
  88struct xenbus_ring_ops {
  89        int (*map)(struct xenbus_device *dev, struct map_ring_valloc *info,
  90                   grant_ref_t *gnt_refs, unsigned int nr_grefs,
  91                   void **vaddr);
  92        int (*unmap)(struct xenbus_device *dev, void *vaddr);
  93};
  94
  95static const struct xenbus_ring_ops *ring_ops __read_mostly;
  96
  97const char *xenbus_strstate(enum xenbus_state state)
  98{
  99        static const char *const name[] = {
 100                [ XenbusStateUnknown      ] = "Unknown",
 101                [ XenbusStateInitialising ] = "Initialising",
 102                [ XenbusStateInitWait     ] = "InitWait",
 103                [ XenbusStateInitialised  ] = "Initialised",
 104                [ XenbusStateConnected    ] = "Connected",
 105                [ XenbusStateClosing      ] = "Closing",
 106                [ XenbusStateClosed       ] = "Closed",
 107                [XenbusStateReconfiguring] = "Reconfiguring",
 108                [XenbusStateReconfigured] = "Reconfigured",
 109        };
 110        return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
 111}
 112EXPORT_SYMBOL_GPL(xenbus_strstate);
 113
 114/**
 115 * xenbus_watch_path - register a watch
 116 * @dev: xenbus device
 117 * @path: path to watch
 118 * @watch: watch to register
 119 * @callback: callback to register
 120 *
 121 * Register a @watch on the given path, using the given xenbus_watch structure
 122 * for storage, and the given @callback function as the callback.  Return 0 on
 123 * success, or -errno on error.  On success, the given @path will be saved as
 124 * @watch->node, and remains the caller's to free.  On error, @watch->node will
 125 * be NULL, the device will switch to %XenbusStateClosing, and the error will
 126 * be saved in the store.
 127 */
 128int xenbus_watch_path(struct xenbus_device *dev, const char *path,
 129                      struct xenbus_watch *watch,
 130                      void (*callback)(struct xenbus_watch *,
 131                                       const char *, const char *))
 132{
 133        int err;
 134
 135        watch->node = path;
 136        watch->callback = callback;
 137
 138        err = register_xenbus_watch(watch);
 139
 140        if (err) {
 141                watch->node = NULL;
 142                watch->callback = NULL;
 143                xenbus_dev_fatal(dev, err, "adding watch on %s", path);
 144        }
 145
 146        return err;
 147}
 148EXPORT_SYMBOL_GPL(xenbus_watch_path);
 149
 150
 151/**
 152 * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
 153 * @dev: xenbus device
 154 * @watch: watch to register
 155 * @callback: callback to register
 156 * @pathfmt: format of path to watch
 157 *
 158 * Register a watch on the given @path, using the given xenbus_watch
 159 * structure for storage, and the given @callback function as the callback.
 160 * Return 0 on success, or -errno on error.  On success, the watched path
 161 * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
 162 * kfree().  On error, watch->node will be NULL, so the caller has nothing to
 163 * free, the device will switch to %XenbusStateClosing, and the error will be
 164 * saved in the store.
 165 */
 166int xenbus_watch_pathfmt(struct xenbus_device *dev,
 167                         struct xenbus_watch *watch,
 168                         void (*callback)(struct xenbus_watch *,
 169                                          const char *, const char *),
 170                         const char *pathfmt, ...)
 171{
 172        int err;
 173        va_list ap;
 174        char *path;
 175
 176        va_start(ap, pathfmt);
 177        path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
 178        va_end(ap);
 179
 180        if (!path) {
 181                xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
 182                return -ENOMEM;
 183        }
 184        err = xenbus_watch_path(dev, path, watch, callback);
 185
 186        if (err)
 187                kfree(path);
 188        return err;
 189}
 190EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
 191
 192static void xenbus_switch_fatal(struct xenbus_device *, int, int,
 193                                const char *, ...);
 194
 195static int
 196__xenbus_switch_state(struct xenbus_device *dev,
 197                      enum xenbus_state state, int depth)
 198{
 199        /* We check whether the state is currently set to the given value, and
 200           if not, then the state is set.  We don't want to unconditionally
 201           write the given state, because we don't want to fire watches
 202           unnecessarily.  Furthermore, if the node has gone, we don't write
 203           to it, as the device will be tearing down, and we don't want to
 204           resurrect that directory.
 205
 206           Note that, because of this cached value of our state, this
 207           function will not take a caller's Xenstore transaction
 208           (something it was trying to in the past) because dev->state
 209           would not get reset if the transaction was aborted.
 210         */
 211
 212        struct xenbus_transaction xbt;
 213        int current_state;
 214        int err, abort;
 215
 216        if (state == dev->state)
 217                return 0;
 218
 219again:
 220        abort = 1;
 221
 222        err = xenbus_transaction_start(&xbt);
 223        if (err) {
 224                xenbus_switch_fatal(dev, depth, err, "starting transaction");
 225                return 0;
 226        }
 227
 228        err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
 229        if (err != 1)
 230                goto abort;
 231
 232        err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
 233        if (err) {
 234                xenbus_switch_fatal(dev, depth, err, "writing new state");
 235                goto abort;
 236        }
 237
 238        abort = 0;
 239abort:
 240        err = xenbus_transaction_end(xbt, abort);
 241        if (err) {
 242                if (err == -EAGAIN && !abort)
 243                        goto again;
 244                xenbus_switch_fatal(dev, depth, err, "ending transaction");
 245        } else
 246                dev->state = state;
 247
 248        return 0;
 249}
 250
 251/**
 252 * xenbus_switch_state
 253 * @dev: xenbus device
 254 * @state: new state
 255 *
 256 * Advertise in the store a change of the given driver to the given new_state.
 257 * Return 0 on success, or -errno on error.  On error, the device will switch
 258 * to XenbusStateClosing, and the error will be saved in the store.
 259 */
 260int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
 261{
 262        return __xenbus_switch_state(dev, state, 0);
 263}
 264
 265EXPORT_SYMBOL_GPL(xenbus_switch_state);
 266
 267int xenbus_frontend_closed(struct xenbus_device *dev)
 268{
 269        xenbus_switch_state(dev, XenbusStateClosed);
 270        complete(&dev->down);
 271        return 0;
 272}
 273EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
 274
 275static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
 276                                const char *fmt, va_list ap)
 277{
 278        unsigned int len;
 279        char *printf_buffer;
 280        char *path_buffer;
 281
 282#define PRINTF_BUFFER_SIZE 4096
 283
 284        printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
 285        if (!printf_buffer)
 286                return;
 287
 288        len = sprintf(printf_buffer, "%i ", -err);
 289        vsnprintf(printf_buffer + len, PRINTF_BUFFER_SIZE - len, fmt, ap);
 290
 291        dev_err(&dev->dev, "%s\n", printf_buffer);
 292
 293        path_buffer = kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
 294        if (path_buffer)
 295                xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer);
 296
 297        kfree(printf_buffer);
 298        kfree(path_buffer);
 299}
 300
 301/**
 302 * xenbus_dev_error
 303 * @dev: xenbus device
 304 * @err: error to report
 305 * @fmt: error message format
 306 *
 307 * Report the given negative errno into the store, along with the given
 308 * formatted message.
 309 */
 310void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
 311{
 312        va_list ap;
 313
 314        va_start(ap, fmt);
 315        xenbus_va_dev_error(dev, err, fmt, ap);
 316        va_end(ap);
 317}
 318EXPORT_SYMBOL_GPL(xenbus_dev_error);
 319
 320/**
 321 * xenbus_dev_fatal
 322 * @dev: xenbus device
 323 * @err: error to report
 324 * @fmt: error message format
 325 *
 326 * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
 327 * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
 328 * closedown of this driver and its peer.
 329 */
 330
 331void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
 332{
 333        va_list ap;
 334
 335        va_start(ap, fmt);
 336        xenbus_va_dev_error(dev, err, fmt, ap);
 337        va_end(ap);
 338
 339        xenbus_switch_state(dev, XenbusStateClosing);
 340}
 341EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
 342
 343/**
 344 * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
 345 * avoiding recursion within xenbus_switch_state.
 346 */
 347static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
 348                                const char *fmt, ...)
 349{
 350        va_list ap;
 351
 352        va_start(ap, fmt);
 353        xenbus_va_dev_error(dev, err, fmt, ap);
 354        va_end(ap);
 355
 356        if (!depth)
 357                __xenbus_switch_state(dev, XenbusStateClosing, 1);
 358}
 359
 360/**
 361 * xenbus_grant_ring
 362 * @dev: xenbus device
 363 * @vaddr: starting virtual address of the ring
 364 * @nr_pages: number of pages to be granted
 365 * @grefs: grant reference array to be filled in
 366 *
 367 * Grant access to the given @vaddr to the peer of the given device.
 368 * Then fill in @grefs with grant references.  Return 0 on success, or
 369 * -errno on error.  On error, the device will switch to
 370 * XenbusStateClosing, and the error will be saved in the store.
 371 */
 372int xenbus_grant_ring(struct xenbus_device *dev, void *vaddr,
 373                      unsigned int nr_pages, grant_ref_t *grefs)
 374{
 375        int err;
 376        int i, j;
 377
 378        for (i = 0; i < nr_pages; i++) {
 379                unsigned long gfn;
 380
 381                if (is_vmalloc_addr(vaddr))
 382                        gfn = pfn_to_gfn(vmalloc_to_pfn(vaddr));
 383                else
 384                        gfn = virt_to_gfn(vaddr);
 385
 386                err = gnttab_grant_foreign_access(dev->otherend_id, gfn, 0);
 387                if (err < 0) {
 388                        xenbus_dev_fatal(dev, err,
 389                                         "granting access to ring page");
 390                        goto fail;
 391                }
 392                grefs[i] = err;
 393
 394                vaddr = vaddr + XEN_PAGE_SIZE;
 395        }
 396
 397        return 0;
 398
 399fail:
 400        for (j = 0; j < i; j++)
 401                gnttab_end_foreign_access_ref(grefs[j], 0);
 402        return err;
 403}
 404EXPORT_SYMBOL_GPL(xenbus_grant_ring);
 405
 406
 407/**
 408 * Allocate an event channel for the given xenbus_device, assigning the newly
 409 * created local port to *port.  Return 0 on success, or -errno on error.  On
 410 * error, the device will switch to XenbusStateClosing, and the error will be
 411 * saved in the store.
 412 */
 413int xenbus_alloc_evtchn(struct xenbus_device *dev, evtchn_port_t *port)
 414{
 415        struct evtchn_alloc_unbound alloc_unbound;
 416        int err;
 417
 418        alloc_unbound.dom = DOMID_SELF;
 419        alloc_unbound.remote_dom = dev->otherend_id;
 420
 421        err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
 422                                          &alloc_unbound);
 423        if (err)
 424                xenbus_dev_fatal(dev, err, "allocating event channel");
 425        else
 426                *port = alloc_unbound.port;
 427
 428        return err;
 429}
 430EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
 431
 432
 433/**
 434 * Free an existing event channel. Returns 0 on success or -errno on error.
 435 */
 436int xenbus_free_evtchn(struct xenbus_device *dev, evtchn_port_t port)
 437{
 438        struct evtchn_close close;
 439        int err;
 440
 441        close.port = port;
 442
 443        err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
 444        if (err)
 445                xenbus_dev_error(dev, err, "freeing event channel %u", port);
 446
 447        return err;
 448}
 449EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
 450
 451
 452/**
 453 * xenbus_map_ring_valloc
 454 * @dev: xenbus device
 455 * @gnt_refs: grant reference array
 456 * @nr_grefs: number of grant references
 457 * @vaddr: pointer to address to be filled out by mapping
 458 *
 459 * Map @nr_grefs pages of memory into this domain from another
 460 * domain's grant table.  xenbus_map_ring_valloc allocates @nr_grefs
 461 * pages of virtual address space, maps the pages to that address, and
 462 * sets *vaddr to that address.  Returns 0 on success, and -errno on
 463 * error. If an error is returned, device will switch to
 464 * XenbusStateClosing and the error message will be saved in XenStore.
 465 */
 466int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
 467                           unsigned int nr_grefs, void **vaddr)
 468{
 469        int err;
 470        struct map_ring_valloc *info;
 471
 472        *vaddr = NULL;
 473
 474        if (nr_grefs > XENBUS_MAX_RING_GRANTS)
 475                return -EINVAL;
 476
 477        info = kzalloc(sizeof(*info), GFP_KERNEL);
 478        if (!info)
 479                return -ENOMEM;
 480
 481        info->node = kzalloc(sizeof(*info->node), GFP_KERNEL);
 482        if (!info->node)
 483                err = -ENOMEM;
 484        else
 485                err = ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr);
 486
 487        kfree(info->node);
 488        kfree(info);
 489        return err;
 490}
 491EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
 492
 493/* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned
 494 * long), e.g. 32-on-64.  Caller is responsible for preparing the
 495 * right array to feed into this function */
 496static int __xenbus_map_ring(struct xenbus_device *dev,
 497                             grant_ref_t *gnt_refs,
 498                             unsigned int nr_grefs,
 499                             grant_handle_t *handles,
 500                             struct map_ring_valloc *info,
 501                             unsigned int flags,
 502                             bool *leaked)
 503{
 504        int i, j;
 505
 506        if (nr_grefs > XENBUS_MAX_RING_GRANTS)
 507                return -EINVAL;
 508
 509        for (i = 0; i < nr_grefs; i++) {
 510                gnttab_set_map_op(&info->map[i], info->phys_addrs[i], flags,
 511                                  gnt_refs[i], dev->otherend_id);
 512                handles[i] = INVALID_GRANT_HANDLE;
 513        }
 514
 515        gnttab_batch_map(info->map, i);
 516
 517        for (i = 0; i < nr_grefs; i++) {
 518                if (info->map[i].status != GNTST_okay) {
 519                        xenbus_dev_fatal(dev, info->map[i].status,
 520                                         "mapping in shared page %d from domain %d",
 521                                         gnt_refs[i], dev->otherend_id);
 522                        goto fail;
 523                } else
 524                        handles[i] = info->map[i].handle;
 525        }
 526
 527        return 0;
 528
 529 fail:
 530        for (i = j = 0; i < nr_grefs; i++) {
 531                if (handles[i] != INVALID_GRANT_HANDLE) {
 532                        gnttab_set_unmap_op(&info->unmap[j],
 533                                            info->phys_addrs[i],
 534                                            GNTMAP_host_map, handles[i]);
 535                        j++;
 536                }
 537        }
 538
 539        if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, info->unmap, j))
 540                BUG();
 541
 542        *leaked = false;
 543        for (i = 0; i < j; i++) {
 544                if (info->unmap[i].status != GNTST_okay) {
 545                        *leaked = true;
 546                        break;
 547                }
 548        }
 549
 550        return -ENOENT;
 551}
 552
 553/**
 554 * xenbus_unmap_ring
 555 * @dev: xenbus device
 556 * @handles: grant handle array
 557 * @nr_handles: number of handles in the array
 558 * @vaddrs: addresses to unmap
 559 *
 560 * Unmap memory in this domain that was imported from another domain.
 561 * Returns 0 on success and returns GNTST_* on error
 562 * (see xen/include/interface/grant_table.h).
 563 */
 564static int xenbus_unmap_ring(struct xenbus_device *dev, grant_handle_t *handles,
 565                             unsigned int nr_handles, unsigned long *vaddrs)
 566{
 567        struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
 568        int i;
 569        int err;
 570
 571        if (nr_handles > XENBUS_MAX_RING_GRANTS)
 572                return -EINVAL;
 573
 574        for (i = 0; i < nr_handles; i++)
 575                gnttab_set_unmap_op(&unmap[i], vaddrs[i],
 576                                    GNTMAP_host_map, handles[i]);
 577
 578        if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
 579                BUG();
 580
 581        err = GNTST_okay;
 582        for (i = 0; i < nr_handles; i++) {
 583                if (unmap[i].status != GNTST_okay) {
 584                        xenbus_dev_error(dev, unmap[i].status,
 585                                         "unmapping page at handle %d error %d",
 586                                         handles[i], unmap[i].status);
 587                        err = unmap[i].status;
 588                        break;
 589                }
 590        }
 591
 592        return err;
 593}
 594
 595static void xenbus_map_ring_setup_grant_hvm(unsigned long gfn,
 596                                            unsigned int goffset,
 597                                            unsigned int len,
 598                                            void *data)
 599{
 600        struct map_ring_valloc *info = data;
 601        unsigned long vaddr = (unsigned long)gfn_to_virt(gfn);
 602
 603        info->phys_addrs[info->idx] = vaddr;
 604        info->addrs[info->idx] = vaddr;
 605
 606        info->idx++;
 607}
 608
 609static int xenbus_map_ring_hvm(struct xenbus_device *dev,
 610                               struct map_ring_valloc *info,
 611                               grant_ref_t *gnt_ref,
 612                               unsigned int nr_grefs,
 613                               void **vaddr)
 614{
 615        struct xenbus_map_node *node = info->node;
 616        int err;
 617        void *addr;
 618        bool leaked = false;
 619        unsigned int nr_pages = XENBUS_PAGES(nr_grefs);
 620
 621        err = xen_alloc_unpopulated_pages(nr_pages, node->hvm.pages);
 622        if (err)
 623                goto out_err;
 624
 625        gnttab_foreach_grant(node->hvm.pages, nr_grefs,
 626                             xenbus_map_ring_setup_grant_hvm,
 627                             info);
 628
 629        err = __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles,
 630                                info, GNTMAP_host_map, &leaked);
 631        node->nr_handles = nr_grefs;
 632
 633        if (err)
 634                goto out_free_ballooned_pages;
 635
 636        addr = vmap(node->hvm.pages, nr_pages, VM_MAP | VM_IOREMAP,
 637                    PAGE_KERNEL);
 638        if (!addr) {
 639                err = -ENOMEM;
 640                goto out_xenbus_unmap_ring;
 641        }
 642
 643        node->hvm.addr = addr;
 644
 645        spin_lock(&xenbus_valloc_lock);
 646        list_add(&node->next, &xenbus_valloc_pages);
 647        spin_unlock(&xenbus_valloc_lock);
 648
 649        *vaddr = addr;
 650        info->node = NULL;
 651
 652        return 0;
 653
 654 out_xenbus_unmap_ring:
 655        if (!leaked)
 656                xenbus_unmap_ring(dev, node->handles, nr_grefs, info->addrs);
 657        else
 658                pr_alert("leaking %p size %u page(s)",
 659                         addr, nr_pages);
 660 out_free_ballooned_pages:
 661        if (!leaked)
 662                xen_free_unpopulated_pages(nr_pages, node->hvm.pages);
 663 out_err:
 664        return err;
 665}
 666
 667/**
 668 * xenbus_unmap_ring_vfree
 669 * @dev: xenbus device
 670 * @vaddr: addr to unmap
 671 *
 672 * Based on Rusty Russell's skeleton driver's unmap_page.
 673 * Unmap a page of memory in this domain that was imported from another domain.
 674 * Use xenbus_unmap_ring_vfree if you mapped in your memory with
 675 * xenbus_map_ring_valloc (it will free the virtual address space).
 676 * Returns 0 on success and returns GNTST_* on error
 677 * (see xen/include/interface/grant_table.h).
 678 */
 679int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
 680{
 681        return ring_ops->unmap(dev, vaddr);
 682}
 683EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
 684
 685#ifdef CONFIG_XEN_PV
 686static int map_ring_apply(pte_t *pte, unsigned long addr, void *data)
 687{
 688        struct map_ring_valloc *info = data;
 689
 690        info->phys_addrs[info->idx++] = arbitrary_virt_to_machine(pte).maddr;
 691        return 0;
 692}
 693
 694static int xenbus_map_ring_pv(struct xenbus_device *dev,
 695                              struct map_ring_valloc *info,
 696                              grant_ref_t *gnt_refs,
 697                              unsigned int nr_grefs,
 698                              void **vaddr)
 699{
 700        struct xenbus_map_node *node = info->node;
 701        struct vm_struct *area;
 702        bool leaked = false;
 703        int err = -ENOMEM;
 704
 705        area = get_vm_area(XEN_PAGE_SIZE * nr_grefs, VM_IOREMAP);
 706        if (!area)
 707                return -ENOMEM;
 708        if (apply_to_page_range(&init_mm, (unsigned long)area->addr,
 709                                XEN_PAGE_SIZE * nr_grefs, map_ring_apply, info))
 710                goto failed;
 711        err = __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles,
 712                                info, GNTMAP_host_map | GNTMAP_contains_pte,
 713                                &leaked);
 714        if (err)
 715                goto failed;
 716
 717        node->nr_handles = nr_grefs;
 718        node->pv.area = area;
 719
 720        spin_lock(&xenbus_valloc_lock);
 721        list_add(&node->next, &xenbus_valloc_pages);
 722        spin_unlock(&xenbus_valloc_lock);
 723
 724        *vaddr = area->addr;
 725        info->node = NULL;
 726
 727        return 0;
 728
 729failed:
 730        if (!leaked)
 731                free_vm_area(area);
 732        else
 733                pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs);
 734
 735        return err;
 736}
 737
 738static int xenbus_unmap_ring_pv(struct xenbus_device *dev, void *vaddr)
 739{
 740        struct xenbus_map_node *node;
 741        struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
 742        unsigned int level;
 743        int i;
 744        bool leaked = false;
 745        int err;
 746
 747        spin_lock(&xenbus_valloc_lock);
 748        list_for_each_entry(node, &xenbus_valloc_pages, next) {
 749                if (node->pv.area->addr == vaddr) {
 750                        list_del(&node->next);
 751                        goto found;
 752                }
 753        }
 754        node = NULL;
 755 found:
 756        spin_unlock(&xenbus_valloc_lock);
 757
 758        if (!node) {
 759                xenbus_dev_error(dev, -ENOENT,
 760                                 "can't find mapped virtual address %p", vaddr);
 761                return GNTST_bad_virt_addr;
 762        }
 763
 764        for (i = 0; i < node->nr_handles; i++) {
 765                unsigned long addr;
 766
 767                memset(&unmap[i], 0, sizeof(unmap[i]));
 768                addr = (unsigned long)vaddr + (XEN_PAGE_SIZE * i);
 769                unmap[i].host_addr = arbitrary_virt_to_machine(
 770                        lookup_address(addr, &level)).maddr;
 771                unmap[i].dev_bus_addr = 0;
 772                unmap[i].handle = node->handles[i];
 773        }
 774
 775        if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
 776                BUG();
 777
 778        err = GNTST_okay;
 779        leaked = false;
 780        for (i = 0; i < node->nr_handles; i++) {
 781                if (unmap[i].status != GNTST_okay) {
 782                        leaked = true;
 783                        xenbus_dev_error(dev, unmap[i].status,
 784                                         "unmapping page at handle %d error %d",
 785                                         node->handles[i], unmap[i].status);
 786                        err = unmap[i].status;
 787                        break;
 788                }
 789        }
 790
 791        if (!leaked)
 792                free_vm_area(node->pv.area);
 793        else
 794                pr_alert("leaking VM area %p size %u page(s)",
 795                         node->pv.area, node->nr_handles);
 796
 797        kfree(node);
 798        return err;
 799}
 800
 801static const struct xenbus_ring_ops ring_ops_pv = {
 802        .map = xenbus_map_ring_pv,
 803        .unmap = xenbus_unmap_ring_pv,
 804};
 805#endif
 806
 807struct unmap_ring_hvm
 808{
 809        unsigned int idx;
 810        unsigned long addrs[XENBUS_MAX_RING_GRANTS];
 811};
 812
 813static void xenbus_unmap_ring_setup_grant_hvm(unsigned long gfn,
 814                                              unsigned int goffset,
 815                                              unsigned int len,
 816                                              void *data)
 817{
 818        struct unmap_ring_hvm *info = data;
 819
 820        info->addrs[info->idx] = (unsigned long)gfn_to_virt(gfn);
 821
 822        info->idx++;
 823}
 824
 825static int xenbus_unmap_ring_hvm(struct xenbus_device *dev, void *vaddr)
 826{
 827        int rv;
 828        struct xenbus_map_node *node;
 829        void *addr;
 830        struct unmap_ring_hvm info = {
 831                .idx = 0,
 832        };
 833        unsigned int nr_pages;
 834
 835        spin_lock(&xenbus_valloc_lock);
 836        list_for_each_entry(node, &xenbus_valloc_pages, next) {
 837                addr = node->hvm.addr;
 838                if (addr == vaddr) {
 839                        list_del(&node->next);
 840                        goto found;
 841                }
 842        }
 843        node = addr = NULL;
 844 found:
 845        spin_unlock(&xenbus_valloc_lock);
 846
 847        if (!node) {
 848                xenbus_dev_error(dev, -ENOENT,
 849                                 "can't find mapped virtual address %p", vaddr);
 850                return GNTST_bad_virt_addr;
 851        }
 852
 853        nr_pages = XENBUS_PAGES(node->nr_handles);
 854
 855        gnttab_foreach_grant(node->hvm.pages, node->nr_handles,
 856                             xenbus_unmap_ring_setup_grant_hvm,
 857                             &info);
 858
 859        rv = xenbus_unmap_ring(dev, node->handles, node->nr_handles,
 860                               info.addrs);
 861        if (!rv) {
 862                vunmap(vaddr);
 863                xen_free_unpopulated_pages(nr_pages, node->hvm.pages);
 864        }
 865        else
 866                WARN(1, "Leaking %p, size %u page(s)\n", vaddr, nr_pages);
 867
 868        kfree(node);
 869        return rv;
 870}
 871
 872/**
 873 * xenbus_read_driver_state
 874 * @path: path for driver
 875 *
 876 * Return the state of the driver rooted at the given store path, or
 877 * XenbusStateUnknown if no state can be read.
 878 */
 879enum xenbus_state xenbus_read_driver_state(const char *path)
 880{
 881        enum xenbus_state result;
 882        int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
 883        if (err)
 884                result = XenbusStateUnknown;
 885
 886        return result;
 887}
 888EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
 889
 890static const struct xenbus_ring_ops ring_ops_hvm = {
 891        .map = xenbus_map_ring_hvm,
 892        .unmap = xenbus_unmap_ring_hvm,
 893};
 894
 895void __init xenbus_ring_ops_init(void)
 896{
 897#ifdef CONFIG_XEN_PV
 898        if (!xen_feature(XENFEAT_auto_translated_physmap))
 899                ring_ops = &ring_ops_pv;
 900        else
 901#endif
 902                ring_ops = &ring_ops_hvm;
 903}
 904