gntalloc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. #include <linux/atomic.h>
  50. #include <linux/module.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/kernel.h>
  53. #include <linux/init.h>
  54. #include <linux/slab.h>
  55. #include <linux/fs.h>
  56. #include <linux/device.h>
  57. #include <linux/mm.h>
  58. #include <linux/uaccess.h>
  59. #include <linux/types.h>
  60. #include <linux/list.h>
  61. #include <linux/highmem.h>
  62. #include <xen/xen.h>
  63. #include <xen/page.h>
  64. #include <xen/grant_table.h>
  65. #include <xen/gntalloc.h>
  66. #include <xen/events.h>
  67. static int limit = 1024;
  68. module_param(limit, int, 0644);
  69. MODULE_PARM_DESC(limit, "Maximum number of grants that may be allocated by "
  70. "the gntalloc device");
  71. static LIST_HEAD(gref_list);
  72. static DEFINE_SPINLOCK(gref_lock);
  73. static int gref_size;
  74. struct notify_info {
  75. uint16_t pgoff:12; /* Bits 0-11: Offset of the byte to clear */
  76. uint16_t flags:2; /* Bits 12-13: Unmap notification flags */
  77. int event; /* Port (event channel) to notify */
  78. };
  79. /* Metadata on a grant reference. */
  80. struct gntalloc_gref {
  81. struct list_head next_gref; /* list entry gref_list */
  82. struct list_head next_file; /* list entry file->list, if open */
  83. struct page *page; /* The shared page */
  84. uint64_t file_index; /* File offset for mmap() */
  85. unsigned int users; /* Use count - when zero, waiting on Xen */
  86. grant_ref_t gref_id; /* The grant reference number */
  87. struct notify_info notify; /* Unmap notification */
  88. };
  89. struct gntalloc_file_private_data {
  90. struct list_head list;
  91. uint64_t index;
  92. };
  93. static void __del_gref(struct gntalloc_gref *gref);
  94. static void do_cleanup(void)
  95. {
  96. struct gntalloc_gref *gref, *n;
  97. list_for_each_entry_safe(gref, n, &gref_list, next_gref) {
  98. if (!gref->users)
  99. __del_gref(gref);
  100. }
  101. }
  102. static int add_grefs(struct ioctl_gntalloc_alloc_gref *op,
  103. uint32_t *gref_ids, struct gntalloc_file_private_data *priv)
  104. {
  105. int i, rc, readonly;
  106. LIST_HEAD(queue_gref);
  107. LIST_HEAD(queue_file);
  108. struct gntalloc_gref *gref;
  109. readonly = !(op->flags & GNTALLOC_FLAG_WRITABLE);
  110. rc = -ENOMEM;
  111. for (i = 0; i < op->count; i++) {
  112. gref = kzalloc(sizeof(*gref), GFP_KERNEL);
  113. if (!gref)
  114. goto undo;
  115. list_add_tail(&gref->next_gref, &queue_gref);
  116. list_add_tail(&gref->next_file, &queue_file);
  117. gref->users = 1;
  118. gref->file_index = op->index + i * PAGE_SIZE;
  119. gref->page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  120. if (!gref->page)
  121. goto undo;
  122. /* Grant foreign access to the page. */
  123. gref->gref_id = gnttab_grant_foreign_access(op->domid,
  124. pfn_to_mfn(page_to_pfn(gref->page)), readonly);
  125. if ((int)gref->gref_id < 0) {
  126. rc = gref->gref_id;
  127. goto undo;
  128. }
  129. gref_ids[i] = gref->gref_id;
  130. }
  131. /* Add to gref lists. */
  132. spin_lock(&gref_lock);
  133. list_splice_tail(&queue_gref, &gref_list);
  134. list_splice_tail(&queue_file, &priv->list);
  135. spin_unlock(&gref_lock);
  136. return 0;
  137. undo:
  138. spin_lock(&gref_lock);
  139. gref_size -= (op->count - i);
  140. list_for_each_entry(gref, &queue_file, next_file) {
  141. /* __del_gref does not remove from queue_file */
  142. __del_gref(gref);
  143. }
  144. /* It's possible for the target domain to map the just-allocated grant
  145. * references by blindly guessing their IDs; if this is done, then
  146. * __del_gref will leave them in the queue_gref list. They need to be
  147. * added to the global list so that we can free them when they are no
  148. * longer referenced.
  149. */
  150. if (unlikely(!list_empty(&queue_gref)))
  151. list_splice_tail(&queue_gref, &gref_list);
  152. spin_unlock(&gref_lock);
  153. return rc;
  154. }
  155. static void __del_gref(struct gntalloc_gref *gref)
  156. {
  157. if (gref->notify.flags & UNMAP_NOTIFY_CLEAR_BYTE) {
  158. uint8_t *tmp = kmap(gref->page);
  159. tmp[gref->notify.pgoff] = 0;
  160. kunmap(gref->page);
  161. }
  162. if (gref->notify.flags & UNMAP_NOTIFY_SEND_EVENT)
  163. notify_remote_via_evtchn(gref->notify.event);
  164. gref->notify.flags = 0;
  165. if (gref->gref_id > 0) {
  166. if (gnttab_query_foreign_access(gref->gref_id))
  167. return;
  168. if (!gnttab_end_foreign_access_ref(gref->gref_id, 0))
  169. return;
  170. }
  171. gref_size--;
  172. list_del(&gref->next_gref);
  173. if (gref->page)
  174. __free_page(gref->page);
  175. kfree(gref);
  176. }
  177. /* finds contiguous grant references in a file, returns the first */
  178. static struct gntalloc_gref *find_grefs(struct gntalloc_file_private_data *priv,
  179. uint64_t index, uint32_t count)
  180. {
  181. struct gntalloc_gref *rv = NULL, *gref;
  182. list_for_each_entry(gref, &priv->list, next_file) {
  183. if (gref->file_index == index && !rv)
  184. rv = gref;
  185. if (rv) {
  186. if (gref->file_index != index)
  187. return NULL;
  188. index += PAGE_SIZE;
  189. count--;
  190. if (count == 0)
  191. return rv;
  192. }
  193. }
  194. return NULL;
  195. }
  196. /*
  197. * -------------------------------------
  198. * File operations.
  199. * -------------------------------------
  200. */
  201. static int gntalloc_open(struct inode *inode, struct file *filp)
  202. {
  203. struct gntalloc_file_private_data *priv;
  204. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  205. if (!priv)
  206. goto out_nomem;
  207. INIT_LIST_HEAD(&priv->list);
  208. filp->private_data = priv;
  209. pr_debug("%s: priv %p\n", __func__, priv);
  210. return 0;
  211. out_nomem:
  212. return -ENOMEM;
  213. }
  214. static int gntalloc_release(struct inode *inode, struct file *filp)
  215. {
  216. struct gntalloc_file_private_data *priv = filp->private_data;
  217. struct gntalloc_gref *gref;
  218. pr_debug("%s: priv %p\n", __func__, priv);
  219. spin_lock(&gref_lock);
  220. while (!list_empty(&priv->list)) {
  221. gref = list_entry(priv->list.next,
  222. struct gntalloc_gref, next_file);
  223. list_del(&gref->next_file);
  224. gref->users--;
  225. if (gref->users == 0)
  226. __del_gref(gref);
  227. }
  228. kfree(priv);
  229. spin_unlock(&gref_lock);
  230. return 0;
  231. }
  232. static long gntalloc_ioctl_alloc(struct gntalloc_file_private_data *priv,
  233. struct ioctl_gntalloc_alloc_gref __user *arg)
  234. {
  235. int rc = 0;
  236. struct ioctl_gntalloc_alloc_gref op;
  237. uint32_t *gref_ids;
  238. pr_debug("%s: priv %p\n", __func__, priv);
  239. if (copy_from_user(&op, arg, sizeof(op))) {
  240. rc = -EFAULT;
  241. goto out;
  242. }
  243. gref_ids = kcalloc(op.count, sizeof(gref_ids[0]), GFP_TEMPORARY);
  244. if (!gref_ids) {
  245. rc = -ENOMEM;
  246. goto out;
  247. }
  248. spin_lock(&gref_lock);
  249. /* Clean up pages that were at zero (local) users but were still mapped
  250. * by remote domains. Since those pages count towards the limit that we
  251. * are about to enforce, removing them here is a good idea.
  252. */
  253. do_cleanup();
  254. if (gref_size + op.count > limit) {
  255. spin_unlock(&gref_lock);
  256. rc = -ENOSPC;
  257. goto out_free;
  258. }
  259. gref_size += op.count;
  260. op.index = priv->index;
  261. priv->index += op.count * PAGE_SIZE;
  262. spin_unlock(&gref_lock);
  263. rc = add_grefs(&op, gref_ids, priv);
  264. if (rc < 0)
  265. goto out_free;
  266. /* Once we finish add_grefs, it is unsafe to touch the new reference,
  267. * since it is possible for a concurrent ioctl to remove it (by guessing
  268. * its index). If the userspace application doesn't provide valid memory
  269. * to write the IDs to, then it will need to close the file in order to
  270. * release - which it will do by segfaulting when it tries to access the
  271. * IDs to close them.
  272. */
  273. if (copy_to_user(arg, &op, sizeof(op))) {
  274. rc = -EFAULT;
  275. goto out_free;
  276. }
  277. if (copy_to_user(arg->gref_ids, gref_ids,
  278. sizeof(gref_ids[0]) * op.count)) {
  279. rc = -EFAULT;
  280. goto out_free;
  281. }
  282. out_free:
  283. kfree(gref_ids);
  284. out:
  285. return rc;
  286. }
  287. static long gntalloc_ioctl_dealloc(struct gntalloc_file_private_data *priv,
  288. void __user *arg)
  289. {
  290. int i, rc = 0;
  291. struct ioctl_gntalloc_dealloc_gref op;
  292. struct gntalloc_gref *gref, *n;
  293. pr_debug("%s: priv %p\n", __func__, priv);
  294. if (copy_from_user(&op, arg, sizeof(op))) {
  295. rc = -EFAULT;
  296. goto dealloc_grant_out;
  297. }
  298. spin_lock(&gref_lock);
  299. gref = find_grefs(priv, op.index, op.count);
  300. if (gref) {
  301. /* Remove from the file list only, and decrease reference count.
  302. * The later call to do_cleanup() will remove from gref_list and
  303. * free the memory if the pages aren't mapped anywhere.
  304. */
  305. for (i = 0; i < op.count; i++) {
  306. n = list_entry(gref->next_file.next,
  307. struct gntalloc_gref, next_file);
  308. list_del(&gref->next_file);
  309. gref->users--;
  310. gref = n;
  311. }
  312. } else {
  313. rc = -EINVAL;
  314. }
  315. do_cleanup();
  316. spin_unlock(&gref_lock);
  317. dealloc_grant_out:
  318. return rc;
  319. }
  320. static long gntalloc_ioctl_unmap_notify(struct gntalloc_file_private_data *priv,
  321. void __user *arg)
  322. {
  323. struct ioctl_gntalloc_unmap_notify op;
  324. struct gntalloc_gref *gref;
  325. uint64_t index;
  326. int pgoff;
  327. int rc;
  328. if (copy_from_user(&op, arg, sizeof(op)))
  329. return -EFAULT;
  330. index = op.index & ~(PAGE_SIZE - 1);
  331. pgoff = op.index & (PAGE_SIZE - 1);
  332. spin_lock(&gref_lock);
  333. gref = find_grefs(priv, index, 1);
  334. if (!gref) {
  335. rc = -ENOENT;
  336. goto unlock_out;
  337. }
  338. if (op.action & ~(UNMAP_NOTIFY_CLEAR_BYTE|UNMAP_NOTIFY_SEND_EVENT)) {
  339. rc = -EINVAL;
  340. goto unlock_out;
  341. }
  342. gref->notify.flags = op.action;
  343. gref->notify.pgoff = pgoff;
  344. gref->notify.event = op.event_channel_port;
  345. rc = 0;
  346. unlock_out:
  347. spin_unlock(&gref_lock);
  348. return rc;
  349. }
  350. static long gntalloc_ioctl(struct file *filp, unsigned int cmd,
  351. unsigned long arg)
  352. {
  353. struct gntalloc_file_private_data *priv = filp->private_data;
  354. switch (cmd) {
  355. case IOCTL_GNTALLOC_ALLOC_GREF:
  356. return gntalloc_ioctl_alloc(priv, (void __user *)arg);
  357. case IOCTL_GNTALLOC_DEALLOC_GREF:
  358. return gntalloc_ioctl_dealloc(priv, (void __user *)arg);
  359. case IOCTL_GNTALLOC_SET_UNMAP_NOTIFY:
  360. return gntalloc_ioctl_unmap_notify(priv, (void __user *)arg);
  361. default:
  362. return -ENOIOCTLCMD;
  363. }
  364. return 0;
  365. }
  366. static void gntalloc_vma_open(struct vm_area_struct *vma)
  367. {
  368. struct gntalloc_gref *gref = vma->vm_private_data;
  369. if (!gref)
  370. return;
  371. spin_lock(&gref_lock);
  372. gref->users++;
  373. spin_unlock(&gref_lock);
  374. }
  375. static void gntalloc_vma_close(struct vm_area_struct *vma)
  376. {
  377. struct gntalloc_gref *gref = vma->vm_private_data;
  378. if (!gref)
  379. return;
  380. spin_lock(&gref_lock);
  381. gref->users--;
  382. if (gref->users == 0)
  383. __del_gref(gref);
  384. spin_unlock(&gref_lock);
  385. }
  386. static struct vm_operations_struct gntalloc_vmops = {
  387. .open = gntalloc_vma_open,
  388. .close = gntalloc_vma_close,
  389. };
  390. static int gntalloc_mmap(struct file *filp, struct vm_area_struct *vma)
  391. {
  392. struct gntalloc_file_private_data *priv = filp->private_data;
  393. struct gntalloc_gref *gref;
  394. int count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
  395. int rv, i;
  396. pr_debug("%s: priv %p, page %lu+%d\n", __func__,
  397. priv, vma->vm_pgoff, count);
  398. if (!(vma->vm_flags & VM_SHARED)) {
  399. printk(KERN_ERR "%s: Mapping must be shared.\n", __func__);
  400. return -EINVAL;
  401. }
  402. spin_lock(&gref_lock);
  403. gref = find_grefs(priv, vma->vm_pgoff << PAGE_SHIFT, count);
  404. if (gref == NULL) {
  405. rv = -ENOENT;
  406. pr_debug("%s: Could not find grant reference",
  407. __func__);
  408. goto out_unlock;
  409. }
  410. vma->vm_private_data = gref;
  411. vma->vm_flags |= VM_RESERVED;
  412. vma->vm_ops = &gntalloc_vmops;
  413. for (i = 0; i < count; i++) {
  414. gref->users++;
  415. rv = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
  416. gref->page);
  417. if (rv)
  418. goto out_unlock;
  419. gref = list_entry(gref->next_file.next,
  420. struct gntalloc_gref, next_file);
  421. }
  422. rv = 0;
  423. out_unlock:
  424. spin_unlock(&gref_lock);
  425. return rv;
  426. }
  427. static const struct file_operations gntalloc_fops = {
  428. .owner = THIS_MODULE,
  429. .open = gntalloc_open,
  430. .release = gntalloc_release,
  431. .unlocked_ioctl = gntalloc_ioctl,
  432. .mmap = gntalloc_mmap
  433. };
  434. /*
  435. * -------------------------------------
  436. * Module creation/destruction.
  437. * -------------------------------------
  438. */
  439. static struct miscdevice gntalloc_miscdev = {
  440. .minor = MISC_DYNAMIC_MINOR,
  441. .name = "xen/gntalloc",
  442. .fops = &gntalloc_fops,
  443. };
  444. static int __init gntalloc_init(void)
  445. {
  446. int err;
  447. if (!xen_domain())
  448. return -ENODEV;
  449. err = misc_register(&gntalloc_miscdev);
  450. if (err != 0) {
  451. printk(KERN_ERR "Could not register misc gntalloc device\n");
  452. return err;
  453. }
  454. pr_debug("Created grant allocation device at %d,%d\n",
  455. MISC_MAJOR, gntalloc_miscdev.minor);
  456. return 0;
  457. }
  458. static void __exit gntalloc_exit(void)
  459. {
  460. misc_deregister(&gntalloc_miscdev);
  461. }
  462. module_init(gntalloc_init);
  463. module_exit(gntalloc_exit);
  464. MODULE_LICENSE("GPL");
  465. MODULE_AUTHOR("Carter Weatherly <carter.weatherly@jhuapl.edu>, "
  466. "Daniel De Graaf <dgdegra@tycho.nsa.gov>");
  467. MODULE_DESCRIPTION("User-space grant reference allocator driver");