gntalloc.c 12 KB

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