xenbus_client.c 25 KB

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