gntalloc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. for (i = 0; i < op->count; i++) {
  117. gref = kzalloc(sizeof(*gref), GFP_KERNEL);
  118. if (!gref) {
  119. rc = -ENOMEM;
  120. goto undo;
  121. }
  122. list_add_tail(&gref->next_gref, &queue_gref);
  123. list_add_tail(&gref->next_file, &queue_file);
  124. gref->users = 1;
  125. gref->file_index = op->index + i * PAGE_SIZE;
  126. gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  127. if (!gref->page) {
  128. rc = -ENOMEM;
  129. goto undo;
  130. }
  131. /* Grant foreign access to the page. */
  132. rc = gnttab_grant_foreign_access(op->domid,
  133. xen_page_to_gfn(gref->page),
  134. readonly);
  135. if (rc < 0)
  136. goto undo;
  137. gref_ids[i] = gref->gref_id = rc;
  138. }
  139. /* Add to gref lists. */
  140. mutex_lock(&gref_mutex);
  141. list_splice_tail(&queue_gref, &gref_list);
  142. list_splice_tail(&queue_file, &priv->list);
  143. mutex_unlock(&gref_mutex);
  144. return 0;
  145. undo:
  146. mutex_lock(&gref_mutex);
  147. gref_size -= (op->count - i);
  148. list_for_each_entry_safe(gref, next, &queue_file, next_file) {
  149. list_del(&gref->next_file);
  150. __del_gref(gref);
  151. }
  152. mutex_unlock(&gref_mutex);
  153. return rc;
  154. }
  155. static void __del_gref(struct gntalloc_gref *gref)
  156. {
  157. unsigned long addr;
  158. if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  159. uint8_t *tmp = kmap(gref->page);
  160. tmp[gref->notify.pgoff] = 0;
  161. kunmap(gref->page);
  162. }
  163. if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT) {
  164. notify_remote_via_evtchn(gref->notify.event);
  165. evtchn_put(gref->notify.event);
  166. }
  167. gref->notify.flags = 0;
  168. if (gref->gref_id) {
  169. if (gref->page) {
  170. addr = (unsigned long)page_to_virt(gref->page);
  171. gnttab_end_foreign_access(gref->gref_id, 0, addr);
  172. } else
  173. gnttab_free_grant_reference(gref->gref_id);
  174. }
  175. gref_size--;
  176. list_del(&gref->next_gref);
  177. kfree(gref);
  178. }
  179. /* finds contiguous grant references in a file, returns the first */
  180. static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv,
  181. uint64_t index, uint32_t count)
  182. {
  183. struct gntalloc_gref *rv = NULL, *gref;
  184. list_for_each_entry(gref, &priv->list, next_file) {
  185. if (gref->file_index == index && !rv)
  186. rv = gref;
  187. if (rv) {
  188. if (gref->file_index != index)
  189. return NULL;
  190. index += PAGE_SIZE;
  191. count--;
  192. if (count == 0)
  193. return rv;
  194. }
  195. }
  196. return NULL;
  197. }
  198. /*
  199. * -------------------------------------
  200. * File operations.
  201. * -------------------------------------
  202. */
  203. static int gntalloc_open(struct inode *inode, struct file *filp)
  204. {
  205. struct gntalloc_file_private_data *priv;
  206. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  207. if (!priv)
  208. goto out_nomem;
  209. INIT_LIST_HEAD(&priv->list);
  210. filp->private_data = priv;
  211. pr_debug("%s: priv %p\n", __func__, priv);
  212. return 0;
  213. out_nomem:
  214. return -ENOMEM;
  215. }
  216. static int gntalloc_release(struct inode *inode, struct file *filp)
  217. {
  218. struct gntalloc_file_private_data *priv = filp->private_data;
  219. struct gntalloc_gref *gref;
  220. pr_debug("%s: priv %p\n", __func__, priv);
  221. mutex_lock(&gref_mutex);
  222. while (!list_empty(&priv->list)) {
  223. gref = list_entry(priv->list.next,
  224. struct gntalloc_gref, next_file);
  225. list_del(&gref->next_file);
  226. gref->users--;
  227. if (gref->users == 0)
  228. __del_gref(gref);
  229. }
  230. kfree(priv);
  231. mutex_unlock(&gref_mutex);
  232. return 0;
  233. }
  234. static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
  235. struct ioctl_gntalloc_alloc_gref __user *arg)
  236. {
  237. int rc = 0;
  238. struct ioctl_gntalloc_alloc_gref op;
  239. uint32_t *gref_ids;
  240. pr_debug("%s: priv %p\n", __func__, priv);
  241. if (copy_from_user(&op, arg, sizeof(op))) {
  242. rc = -EFAULT;
  243. goto out;
  244. }
  245. gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_KERNEL);
  246. if (!gref_ids) {
  247. rc = -ENOMEM;
  248. goto out;
  249. }
  250. mutex_lock(&gref_mutex);
  251. /* Clean up pages that were at zero (local) users but were still mapped
  252. * by remote domains. Since those pages count towards the limit that we
  253. * are about to enforce, removing them here is a good idea.
  254. */
  255. do_cleanup();
  256. if (gref_size + op.count > limit) {
  257. mutex_unlock(&gref_mutex);
  258. rc = -ENOSPC;
  259. goto out_free;
  260. }
  261. gref_size += op.count;
  262. op.index = priv->index;
  263. priv->index += op.count * PAGE_SIZE;
  264. mutex_unlock(&gref_mutex);
  265. rc = add_grefs(&op, gref_ids, priv);
  266. if (rc < 0)
  267. goto out_free;
  268. /* Once we finish add_grefs, it is unsafe to touch the new reference,
  269. * since it is possible for a concurrent ioctl to remove it (by guessing
  270. * its index). If the userspace application doesn't provide valid memory
  271. * to write the IDs to, then it will need to close the file in order to
  272. * release - which it will do by segfaulting when it tries to access the
  273. * IDs to close them.
  274. */
  275. if (copy_to_user(arg, &op, sizeof(op))) {
  276. rc = -EFAULT;
  277. goto out_free;
  278. }
  279. if (copy_to_user(arg->gref_ids, gref_ids,
  280. sizeof(gref_ids[0]) * op.count)) {
  281. rc = -EFAULT;
  282. goto out_free;
  283. }
  284. out_free:
  285. kfree(gref_ids);
  286. out:
  287. return rc;
  288. }
  289. static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
  290. void __user *arg)
  291. {
  292. int i, rc = 0;
  293. struct ioctl_gntalloc_dealloc_gref op;
  294. struct gntalloc_gref *gref, *n;
  295. pr_debug("%s: priv %p\n", __func__, priv);
  296. if (copy_from_user(&op, arg, sizeof(op))) {
  297. rc = -EFAULT;
  298. goto dealloc_grant_out;
  299. }
  300. mutex_lock(&gref_mutex);
  301. gref = find_grefs(priv, op.index, op.count);
  302. if (gref) {
  303. /* Remove from the file list only, and decrease reference count.
  304. * The later call to do_cleanup() will remove from gref_list and
  305. * free the memory if the pages aren't mapped anywhere.
  306. */
  307. for (i = 0; i < op.count; i++) {
  308. n = list_entry(gref->next_file.next,
  309. struct gntalloc_gref, next_file);
  310. list_del(&gref->next_file);
  311. gref->users--;
  312. gref = n;
  313. }
  314. } else {
  315. rc = -EINVAL;
  316. }
  317. do_cleanup();
  318. mutex_unlock(&gref_mutex);
  319. dealloc_grant_out:
  320. return rc;
  321. }
  322. static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
  323. void __user *arg)
  324. {
  325. struct ioctl_gntalloc_unmap_notify op;
  326. struct gntalloc_gref *gref;
  327. uint64_t index;
  328. int pgoff;
  329. int rc;
  330. if (copy_from_user(&op, arg, sizeof(op)))
  331. return -EFAULT;
  332. index = op.index & ~(PAGE_SIZE - 1);
  333. pgoff = op.index & (PAGE_SIZE - 1);
  334. mutex_lock(&gref_mutex);
  335. gref = find_grefs(priv, index, 1);
  336. if (!gref) {
  337. rc = -ENOENT;
  338. goto unlock_out;
  339. }
  340. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
  341. rc = -EINVAL;
  342. goto unlock_out;
  343. }
  344. /* We need to grab a reference to the event channel we are going to use
  345. * to send the notify before releasing the reference we may already have
  346. * (if someone has called this ioctl twice). This is required so that
  347. * it is possible to change the clear_byte part of the notification
  348. * without disturbing the event channel part, which may now be the last
  349. * reference to that event channel.
  350. */
  351. if (op.action & UNMAP_NOTIFY_SEND_EVENT) {
  352. if (evtchn_get(op.event_channel_port)) {
  353. rc = -EINVAL;
  354. goto unlock_out;
  355. }
  356. }
  357. if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
  358. evtchn_put(gref->notify.event);
  359. gref->notify.flags = op.action;
  360. gref->notify.pgoff = pgoff;
  361. gref->notify.event = op.event_channel_port;
  362. rc = 0;
  363. unlock_out:
  364. mutex_unlock(&gref_mutex);
  365. return rc;
  366. }
  367. static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
  368. unsigned long arg)
  369. {
  370. struct gntalloc_file_private_data *priv = filp->private_data;
  371. switch (cmd) {
  372. case IOCTL_GNTALLOC_ALLOC_GREF:
  373. return gntalloc_ioctl_alloc(priv, (void __user *)arg);
  374. case IOCTL_GNTALLOC_DEALLOC_GREF:
  375. return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
  376. case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
  377. return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
  378. default:
  379. return -ENOIOCTLCMD;
  380. }
  381. return 0;
  382. }
  383. static void gntalloc_vma_open(struct vm_area_struct *vma)
  384. {
  385. struct gntalloc_vma_private_data *priv = vma->vm_private_data;
  386. if (!priv)
  387. return;
  388. mutex_lock(&gref_mutex);
  389. priv->users++;
  390. mutex_unlock(&gref_mutex);
  391. }
  392. static void gntalloc_vma_close(struct vm_area_struct *vma)
  393. {
  394. struct gntalloc_vma_private_data *priv = vma->vm_private_data;
  395. struct gntalloc_gref *gref, *next;
  396. int i;
  397. if (!priv)
  398. return;
  399. mutex_lock(&gref_mutex);
  400. priv->users--;
  401. if (priv->users == 0) {
  402. gref = priv->gref;
  403. for (i = 0; i < priv->count; i++) {
  404. gref->users--;
  405. next = list_entry(gref->next_gref.next,
  406. struct gntalloc_gref, next_gref);
  407. if (gref->users == 0)
  408. __del_gref(gref);
  409. gref = next;
  410. }
  411. kfree(priv);
  412. }
  413. mutex_unlock(&gref_mutex);
  414. }
  415. static const struct vm_operations_struct gntalloc_vmops = {
  416. .open = gntalloc_vma_open,
  417. .close = gntalloc_vma_close,
  418. };
  419. static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma)
  420. {
  421. struct gntalloc_file_private_data *priv = filp->private_data;
  422. struct gntalloc_vma_private_data *vm_priv;
  423. struct gntalloc_gref *gref;
  424. int count = vma_pages(vma);
  425. int rv, i;
  426. if (!(vma->vm_flags & VM_SHARED)) {
  427. pr_err("%s: Mapping must be shared\n", __func__);
  428. return -EINVAL;
  429. }
  430. vm_priv = kmalloc(sizeof(*vm_priv), GFP_KERNEL);
  431. if (!vm_priv)
  432. return -ENOMEM;
  433. mutex_lock(&gref_mutex);
  434. pr_debug("%s: priv %p,%p, page %lu+%d\n", __func__,
  435. priv, vm_priv, vma->vm_pgoff, count);
  436. gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count);
  437. if (gref == NULL) {
  438. rv = -ENOENT;
  439. pr_debug("%s: Could not find grant reference",
  440. __func__);
  441. kfree(vm_priv);
  442. goto out_unlock;
  443. }
  444. vm_priv->gref = gref;
  445. vm_priv->users = 1;
  446. vm_priv->count = count;
  447. vma->vm_private_data = vm_priv;
  448. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  449. vma->vm_ops = &gntalloc_vmops;
  450. for (i = 0; i < count; i++) {
  451. gref->users++;
  452. rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
  453. gref->page);
  454. if (rv)
  455. goto out_unlock;
  456. gref = list_entry(gref->next_file.next,
  457. struct gntalloc_gref, next_file);
  458. }
  459. rv = 0;
  460. out_unlock:
  461. mutex_unlock(&gref_mutex);
  462. return rv;
  463. }
  464. static const struct file_operations gntalloc_fops = {
  465. .owner = THIS_MODULE,
  466. .open = gntalloc_open,
  467. .release = gntalloc_release,
  468. .unlocked_ioctl = gntalloc_ioctl,
  469. .mmap = gntalloc_mmap
  470. };
  471. /*
  472. * -------------------------------------
  473. * Module creation/destruction.
  474. * -------------------------------------
  475. */
  476. static struct miscdevice gntalloc_miscdev = {
  477. .minor = MISC_DYNAMIC_MINOR,
  478. .name = "xen/gntalloc",
  479. .fops = &gntalloc_fops,
  480. };
  481. static int __init gntalloc_init(void)
  482. {
  483. int err;
  484. if (!xen_domain())
  485. return -ENODEV;
  486. err = misc_register(&gntalloc_miscdev);
  487. if (err != 0) {
  488. pr_err("Could not register misc gntalloc device\n");
  489. return err;
  490. }
  491. pr_debug("Created grant allocation device at %d,%d\n",
  492. MISC_MAJOR, gntalloc_miscdev.minor);
  493. return 0;
  494. }
  495. static void __exit gntalloc_exit(void)
  496. {
  497. misc_deregister(&gntalloc_miscdev);
  498. }
  499. module_init(gntalloc_init);
  500. module_exit(gntalloc_exit);
  501. MODULE_LICENSE("GPL");
  502. MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, "
  503. "Daniel De Graaf <dgdegra@tycho.nsa.gov>");
  504. MODULE_DESCRIPTION("User-space grant reference allocator driver");