gntalloc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /******************************************************************************
  2. * gntalloc.c
  3. *
  4. * Device for creating grant references (in user-space) that may be shared
  5. * with other domains.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. /*
  17. * This driver exists to allow userspace programs in Linux to allocate kernel
  18. * memory that will later be shared with another domain. Without this device,
  19. * Linux userspace programs cannot create grant references.
  20. *
  21. * How this stuff works:
  22. * X -> granting a page to Y
  23. * Y -> mapping the grant from X
  24. *
  25. * 1. X uses the gntalloc device to allocate a page of kernel memory, P.
  26. * 2. X creates an entry in the grant table that says domid(Y) can access P.
  27. * This is done without a hypercall unless the grant table needs expansion.
  28. * 3. X gives the grant reference identifier, GREF, to Y.
  29. * 4. Y maps the page, either directly into kernel memory for use in a backend
  30. * driver, or via a the gntdev device to map into the address space of an
  31. * application running in Y. This is the first point at which Xen does any
  32. * tracking of the page.
  33. * 5. A program in X mmap()s a segment of the gntalloc device that corresponds
  34. * to the shared page, and can now communicate with Y over the shared page.
  35. *
  36. *
  37. * NOTE TO USERSPACE LIBRARIES:
  38. * The grant allocation and mmap()ing are, naturally, two separate operations.
  39. * You set up the sharing by calling the create ioctl() and then the mmap().
  40. * Teardown requires munmap() and either close() or ioctl().
  41. *
  42. * WARNING: Since Xen does not allow a guest to forcibly end the use of a grant
  43. * reference, this device can be used to consume kernel memory by leaving grant
  44. * references mapped by another domain when an application exits. Therefore,
  45. * there is a global limit on the number of pages that can be allocated. When
  46. * all references to the page are unmapped, it will be freed during the next
  47. * grant operation.
  48. */
  49. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  50. #include <linux/atomic.h>
  51. #include <linux/module.h>
  52. #include <linux/miscdevice.h>
  53. #include <linux/kernel.h>
  54. #include <linux/init.h>
  55. #include <linux/slab.h>
  56. #include <linux/fs.h>
  57. #include <linux/device.h>
  58. #include <linux/mm.h>
  59. #include <linux/uaccess.h>
  60. #include <linux/types.h>
  61. #include <linux/list.h>
  62. #include <linux/highmem.h>
  63. #include <xen/xen.h>
  64. #include <xen/page.h>
  65. #include <xen/grant_table.h>
  66. #include <xen/gntalloc.h>
  67. #include <xen/events.h>
  68. static int limit = 1024;
  69. module_param(limit, int, 0644);
  70. MODULE_PARM_DESC(limit, "Maximum number of grants that may be allocated by "
  71. "the gntalloc device");
  72. static LIST_HEAD(gref_list);
  73. static DEFINE_MUTEX(gref_mutex);
  74. static int gref_size;
  75. struct notify_info {
  76. uint16_t pgoff:12; /* Bits 0-11: Offset of the byte to clear */
  77. uint16_t flags:2; /* Bits 12-13: Unmap notification flags */
  78. int event; /* Port (event channel) to notify */
  79. };
  80. /* Metadata on a grant reference. */
  81. struct gntalloc_gref {
  82. struct list_head next_gref; /* list entry gref_list */
  83. struct list_head next_file; /* list entry file->list, if open */
  84. struct page *page; /* The shared page */
  85. uint64_t file_index; /* File offset for mmap() */
  86. unsigned int users; /* Use count - when zero, waiting on Xen */
  87. grant_ref_t gref_id; /* The grant reference number */
  88. struct notify_info notify; /* Unmap notification */
  89. };
  90. struct gntalloc_file_private_data {
  91. struct list_head list;
  92. uint64_t index;
  93. };
  94. struct gntalloc_vma_private_data {
  95. struct gntalloc_gref *gref;
  96. int users;
  97. int count;
  98. };
  99. static void __del_gref(struct gntalloc_gref *gref);
  100. static void do_cleanup(void)
  101. {
  102. struct gntalloc_gref *gref, *n;
  103. list_for_each_entry_safe(gref, n, &gref_list, next_gref) {
  104. if (!gref->users)
  105. __del_gref(gref);
  106. }
  107. }
  108. static int add_grefs(struct ioctl_gntalloc_alloc_gref *op,
  109. uint32_t *gref_ids, struct gntalloc_file_private_data *priv)
  110. {
  111. int i, rc, readonly;
  112. LIST_HEAD(queue_gref);
  113. LIST_HEAD(queue_file);
  114. struct gntalloc_gref *gref, *next;
  115. readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE);
  116. rc = -ENOMEM;
  117. for (i = 0; i < op->count; i++) {
  118. gref = kzalloc(sizeof(*gref), GFP_KERNEL);
  119. if (!gref)
  120. goto undo;
  121. list_add_tail(&gref->next_gref, &queue_gref);
  122. list_add_tail(&gref->next_file, &queue_file);
  123. gref->users = 1;
  124. gref->file_index = op->index + i * PAGE_SIZE;
  125. gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  126. if (!gref->page)
  127. goto undo;
  128. /* Grant foreign access to the page. */
  129. rc = gnttab_grant_foreign_access(op->domid,
  130. xen_page_to_gfn(gref->page),
  131. readonly);
  132. if (rc < 0)
  133. goto undo;
  134. gref_ids[i] = gref->gref_id = rc;
  135. }
  136. /* Add to gref lists. */
  137. mutex_lock(&gref_mutex);
  138. list_splice_tail(&queue_gref, &gref_list);
  139. list_splice_tail(&queue_file, &priv->list);
  140. mutex_unlock(&gref_mutex);
  141. return 0;
  142. undo:
  143. mutex_lock(&gref_mutex);
  144. gref_size -= (op->count - i);
  145. list_for_each_entry_safe(gref, next, &queue_file, next_file) {
  146. list_del(&gref->next_file);
  147. __del_gref(gref);
  148. }
  149. /* It's possible for the target domain to map the just-allocated grant
  150. * references by blindly guessing their IDs; if this is done, then
  151. * __del_gref will leave them in the queue_gref list. They need to be
  152. * added to the global list so that we can free them when they are no
  153. * longer referenced.
  154. */
  155. if (unlikely(!list_empty(&queue_gref)))
  156. list_splice_tail(&queue_gref, &gref_list);
  157. mutex_unlock(&gref_mutex);
  158. return rc;
  159. }
  160. static void __del_gref(struct gntalloc_gref *gref)
  161. {
  162. if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  163. uint8_t *tmp = kmap(gref->page);
  164. tmp[gref->notify.pgoff] = 0;
  165. kunmap(gref->page);
  166. }
  167. if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  168. notify_remote_via_evtchn(gref->notify.event);
  169. evtchn_put(gref->notify.event);
  170. }
  171. gref->notify.flags = 0;
  172. if (gref->gref_id) {
  173. if (gnttab_query_foreign_access(gref->gref_id))
  174. return;
  175. if (!gnttab_end_foreign_access_ref(gref->gref_id, 0))
  176. return;
  177. gnttab_free_grant_reference(gref->gref_id);
  178. }
  179. gref_size--;
  180. list_del(&gref->next_gref);
  181. if (gref->page)
  182. __free_page(gref->page);
  183. kfree(gref);
  184. }
  185. /* finds contiguous grant references in a file, returns the first */
  186. static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv,
  187. uint64_t index, uint32_t count)
  188. {
  189. struct gntalloc_gref *rv = NULL, *gref;
  190. list_for_each_entry(gref, &priv->list, next_file) {
  191. if (gref->file_index == index && !rv)
  192. rv = gref;
  193. if (rv) {
  194. if (gref->file_index != index)
  195. return NULL;
  196. index += PAGE_SIZE;
  197. count--;
  198. if (count == 0)
  199. return rv;
  200. }
  201. }
  202. return NULL;
  203. }
  204. /*
  205. * -------------------------------------
  206. * File operations.
  207. * -------------------------------------
  208. */
  209. static int gntalloc_open(struct inode *inode, struct file *filp)
  210. {
  211. struct gntalloc_file_private_data *priv;
  212. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  213. if (!priv)
  214. goto out_nomem;
  215. INIT_LIST_HEAD(&priv->list);
  216. filp->private_data = priv;
  217. pr_debug("%s: priv %p\n", __func__, priv);
  218. return 0;
  219. out_nomem:
  220. return -ENOMEM;
  221. }
  222. static int gntalloc_release(struct inode *inode, struct file *filp)
  223. {
  224. struct gntalloc_file_private_data *priv = filp->private_data;
  225. struct gntalloc_gref *gref;
  226. pr_debug("%s: priv %p\n", __func__, priv);
  227. mutex_lock(&gref_mutex);
  228. while (!list_empty(&priv->list)) {
  229. gref = list_entry(priv->list.next,
  230. struct gntalloc_gref, next_file);
  231. list_del(&gref->next_file);
  232. gref->users--;
  233. if (gref->users == 0)
  234. __del_gref(gref);
  235. }
  236. kfree(priv);
  237. mutex_unlock(&gref_mutex);
  238. return 0;
  239. }
  240. static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
  241. struct ioctl_gntalloc_alloc_gref __user *arg)
  242. {
  243. int rc = 0;
  244. struct ioctl_gntalloc_alloc_gref op;
  245. uint32_t *gref_ids;
  246. pr_debug("%s: priv %p\n", __func__, priv);
  247. if (copy_from_user(&op, arg, sizeof(op))) {
  248. rc = -EFAULT;
  249. goto out;
  250. }
  251. gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_TEMPORARY);
  252. if (!gref_ids) {
  253. rc = -ENOMEM;
  254. goto out;
  255. }
  256. mutex_lock(&gref_mutex);
  257. /* Clean up pages that were at zero (local) users but were still mapped
  258. * by remote domains. Since those pages count towards the limit that we
  259. * are about to enforce, removing them here is a good idea.
  260. */
  261. do_cleanup();
  262. if (gref_size + op.count > limit) {
  263. mutex_unlock(&gref_mutex);
  264. rc = -ENOSPC;
  265. goto out_free;
  266. }
  267. gref_size += op.count;
  268. op.index = priv->index;
  269. priv->index += op.count * PAGE_SIZE;
  270. mutex_unlock(&gref_mutex);
  271. rc = add_grefs(&op, gref_ids, priv);
  272. if (rc < 0)
  273. goto out_free;
  274. /* Once we finish add_grefs, it is unsafe to touch the new reference,
  275. * since it is possible for a concurrent ioctl to remove it (by guessing
  276. * its index). If the userspace application doesn't provide valid memory
  277. * to write the IDs to, then it will need to close the file in order to
  278. * release - which it will do by segfaulting when it tries to access the
  279. * IDs to close them.
  280. */
  281. if (copy_to_user(arg, &op, sizeof(op))) {
  282. rc = -EFAULT;
  283. goto out_free;
  284. }
  285. if (copy_to_user(arg->gref_ids, gref_ids,
  286. sizeof(gref_ids[0]) * op.count)) {
  287. rc = -EFAULT;
  288. goto out_free;
  289. }
  290. out_free:
  291. kfree(gref_ids);
  292. out:
  293. return rc;
  294. }
  295. static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
  296. void __user *arg)
  297. {
  298. int i, rc = 0;
  299. struct ioctl_gntalloc_dealloc_gref op;
  300. struct gntalloc_gref *gref, *n;
  301. pr_debug("%s: priv %p\n", __func__, priv);
  302. if (copy_from_user(&op, arg, sizeof(op))) {
  303. rc = -EFAULT;
  304. goto dealloc_grant_out;
  305. }
  306. mutex_lock(&gref_mutex);
  307. gref = find_grefs(priv, op.index, op.count);
  308. if (gref) {
  309. /* Remove from the file list only, and decrease reference count.
  310. * The later call to do_cleanup() will remove from gref_list and
  311. * free the memory if the pages aren't mapped anywhere.
  312. */
  313. for (i = 0; i < op.count; i++) {
  314. n = list_entry(gref->next_file.next,
  315. struct gntalloc_gref, next_file);
  316. list_del(&gref->next_file);
  317. gref->users--;
  318. gref = n;
  319. }
  320. } else {
  321. rc = -EINVAL;
  322. }
  323. do_cleanup();
  324. mutex_unlock(&gref_mutex);
  325. dealloc_grant_out:
  326. return rc;
  327. }
  328. static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
  329. void __user *arg)
  330. {
  331. struct ioctl_gntalloc_unmap_notify op;
  332. struct gntalloc_gref *gref;
  333. uint64_t index;
  334. int pgoff;
  335. int rc;
  336. if (copy_from_user(&op, arg, sizeof(op)))
  337. return -EFAULT;
  338. index = op.index & ~(PAGE_SIZE - 1);
  339. pgoff = op.index & (PAGE_SIZE - 1);
  340. mutex_lock(&gref_mutex);
  341. gref = find_grefs(priv, index, 1);
  342. if (!gref) {
  343. rc = -ENOENT;
  344. goto unlock_out;
  345. }
  346. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
  347. rc = -EINVAL;
  348. goto unlock_out;
  349. }
  350. /* We need to grab a reference to the event channel we are going to use
  351. * to send the notify before releasing the reference we may already have
  352. * (if someone has called this ioctl twice). This is required so that
  353. * it is possible to change the clear_byte part of the notification
  354. * without disturbing the event channel part, which may now be the last
  355. * reference to that event channel.
  356. */
  357. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  358. if (evtchn_get(op.event_channel_port)) {
  359. rc = -EINVAL;
  360. goto unlock_out;
  361. }
  362. }
  363. if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
  364. evtchn_put(gref->notify.event);
  365. gref->notify.flags = op.action;
  366. gref->notify.pgoff = pgoff;
  367. gref->notify.event = op.event_channel_port;
  368. rc = 0;
  369. unlock_out:
  370. mutex_unlock(&gref_mutex);
  371. return rc;
  372. }
  373. static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
  374. unsigned long arg)
  375. {
  376. struct gntalloc_file_private_data *priv = filp->private_data;
  377. switch (cmd) {
  378. case IOCTL_GNTALLOC_ALLOC_GREF:
  379. return gntalloc_ioctl_alloc(priv, (void __user *)arg);
  380. case IOCTL_GNTALLOC_DEALLOC_GREF:
  381. return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
  382. case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
  383. return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
  384. default:
  385. return -ENOIOCTLCMD;
  386. }
  387. return 0;
  388. }
  389. static void gntalloc_vma_open(struct vm_area_struct *vma)
  390. {
  391. struct gntalloc_vma_private_data *priv = vma->vm_private_data;
  392. if (!priv)
  393. return;
  394. mutex_lock(&gref_mutex);
  395. priv->users++;
  396. mutex_unlock(&gref_mutex);
  397. }
  398. static void gntalloc_vma_close(struct vm_area_struct *vma)
  399. {
  400. struct gntalloc_vma_private_data *priv = vma->vm_private_data;
  401. struct gntalloc_gref *gref, *next;
  402. int i;
  403. if (!priv)
  404. return;
  405. mutex_lock(&gref_mutex);
  406. priv->users--;
  407. if (priv->users == 0) {
  408. gref = priv->gref;
  409. for (i = 0; i < priv->count; i++) {
  410. gref->users--;
  411. next = list_entry(gref->next_gref.next,
  412. struct gntalloc_gref, next_gref);
  413. if (gref->users == 0)
  414. __del_gref(gref);
  415. gref = next;
  416. }
  417. kfree(priv);
  418. }
  419. mutex_unlock(&gref_mutex);
  420. }
  421. static const struct vm_operations_struct gntalloc_vmops = {
  422. .open = gntalloc_vma_open,
  423. .close = gntalloc_vma_close,
  424. };
  425. static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma)
  426. {
  427. struct gntalloc_file_private_data *priv = filp->private_data;
  428. struct gntalloc_vma_private_data *vm_priv;
  429. struct gntalloc_gref *gref;
  430. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  431. int rv, i;
  432. if (!(vma->vm_flags & VM_SHARED)) {
  433. pr_err("%s: Mapping must be shared\n", __func__);
  434. return -EINVAL;
  435. }
  436. vm_priv = kmalloc(sizeof(*vm_priv), GFP_KERNEL);
  437. if (!vm_priv)
  438. return -ENOMEM;
  439. mutex_lock(&gref_mutex);
  440. pr_debug("%s: priv %p,%p, page %lu+%d\n", __func__,
  441. priv, vm_priv, vma->vm_pgoff, count);
  442. gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count);
  443. if (gref == NULL) {
  444. rv = -ENOENT;
  445. pr_debug("%s: Could not find grant reference",
  446. __func__);
  447. kfree(vm_priv);
  448. goto out_unlock;
  449. }
  450. vm_priv->gref = gref;
  451. vm_priv->users = 1;
  452. vm_priv->count = count;
  453. vma->vm_private_data = vm_priv;
  454. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  455. vma->vm_ops = &gntalloc_vmops;
  456. for (i = 0; i < count; i++) {
  457. gref->users++;
  458. rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
  459. gref->page);
  460. if (rv)
  461. goto out_unlock;
  462. gref = list_entry(gref->next_file.next,
  463. struct gntalloc_gref, next_file);
  464. }
  465. rv = 0;
  466. out_unlock:
  467. mutex_unlock(&gref_mutex);
  468. return rv;
  469. }
  470. static const struct file_operations gntalloc_fops = {
  471. .owner = THIS_MODULE,
  472. .open = gntalloc_open,
  473. .release = gntalloc_release,
  474. .unlocked_ioctl = gntalloc_ioctl,
  475. .mmap = gntalloc_mmap
  476. };
  477. /*
  478. * -------------------------------------
  479. * Module creation/destruction.
  480. * -------------------------------------
  481. */
  482. static struct miscdevice gntalloc_miscdev = {
  483. .minor = MISC_DYNAMIC_MINOR,
  484. .name = "xen/gntalloc",
  485. .fops = &gntalloc_fops,
  486. };
  487. static int __init gntalloc_init(void)
  488. {
  489. int err;
  490. if (!xen_domain())
  491. return -ENODEV;
  492. err = misc_register(&gntalloc_miscdev);
  493. if (err != 0) {
  494. pr_err("Could not register misc gntalloc device\n");
  495. return err;
  496. }
  497. pr_debug("Created grant allocation device at %d,%d\n",
  498. MISC_MAJOR, gntalloc_miscdev.minor);
  499. return 0;
  500. }
  501. static void __exit gntalloc_exit(void)
  502. {
  503. misc_deregister(&gntalloc_miscdev);
  504. }
  505. module_init(gntalloc_init);
  506. module_exit(gntalloc_exit);
  507. MODULE_LICENSE("GPL");
  508. MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, "
  509. "Daniel De Graaf <dgdegra@tycho.nsa.gov>");
  510. MODULE_DESCRIPTION("User-space grant reference allocator driver");