privcmd-buf.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /******************************************************************************
  3. * privcmd-buf.c
  4. *
  5. * Mmap of hypercall buffers.
  6. *
  7. * Copyright (c) 2018 Juergen Gross
  8. */
  9. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/list.h>
  13. #include <linux/miscdevice.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include "privcmd.h"
  17. MODULE_LICENSE("GPL");
  18. struct privcmd_buf_private {
  19. struct mutex lock;
  20. struct list_head list;
  21. };
  22. struct privcmd_buf_vma_private {
  23. struct privcmd_buf_private *file_priv;
  24. struct list_head list;
  25. unsigned int users;
  26. unsigned int n_pages;
  27. struct page *pages[];
  28. };
  29. static int privcmd_buf_open(struct inode *ino, struct file *file)
  30. {
  31. struct privcmd_buf_private *file_priv;
  32. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  33. if (!file_priv)
  34. return -ENOMEM;
  35. mutex_init(&file_priv->lock);
  36. INIT_LIST_HEAD(&file_priv->list);
  37. file->private_data = file_priv;
  38. return 0;
  39. }
  40. static void privcmd_buf_vmapriv_free(struct privcmd_buf_vma_private *vma_priv)
  41. {
  42. unsigned int i;
  43. list_del(&vma_priv->list);
  44. for (i = 0; i < vma_priv->n_pages; i++)
  45. __free_page(vma_priv->pages[i]);
  46. kfree(vma_priv);
  47. }
  48. static int privcmd_buf_release(struct inode *ino, struct file *file)
  49. {
  50. struct privcmd_buf_private *file_priv = file->private_data;
  51. struct privcmd_buf_vma_private *vma_priv;
  52. mutex_lock(&file_priv->lock);
  53. while (!list_empty(&file_priv->list)) {
  54. vma_priv = list_first_entry(&file_priv->list,
  55. struct privcmd_buf_vma_private,
  56. list);
  57. privcmd_buf_vmapriv_free(vma_priv);
  58. }
  59. mutex_unlock(&file_priv->lock);
  60. kfree(file_priv);
  61. return 0;
  62. }
  63. static void privcmd_buf_vma_open(struct vm_area_struct *vma)
  64. {
  65. struct privcmd_buf_vma_private *vma_priv = vma->vm_private_data;
  66. if (!vma_priv)
  67. return;
  68. mutex_lock(&vma_priv->file_priv->lock);
  69. vma_priv->users++;
  70. mutex_unlock(&vma_priv->file_priv->lock);
  71. }
  72. static void privcmd_buf_vma_close(struct vm_area_struct *vma)
  73. {
  74. struct privcmd_buf_vma_private *vma_priv = vma->vm_private_data;
  75. struct privcmd_buf_private *file_priv;
  76. if (!vma_priv)
  77. return;
  78. file_priv = vma_priv->file_priv;
  79. mutex_lock(&file_priv->lock);
  80. vma_priv->users--;
  81. if (!vma_priv->users)
  82. privcmd_buf_vmapriv_free(vma_priv);
  83. mutex_unlock(&file_priv->lock);
  84. }
  85. static vm_fault_t privcmd_buf_vma_fault(struct vm_fault *vmf)
  86. {
  87. pr_debug("fault: vma=%p %lx-%lx, pgoff=%lx, uv=%p\n",
  88. vmf->vma, vmf->vma->vm_start, vmf->vma->vm_end,
  89. vmf->pgoff, (void *)vmf->address);
  90. return VM_FAULT_SIGBUS;
  91. }
  92. static const struct vm_operations_struct privcmd_buf_vm_ops = {
  93. .open = privcmd_buf_vma_open,
  94. .close = privcmd_buf_vma_close,
  95. .fault = privcmd_buf_vma_fault,
  96. };
  97. static int privcmd_buf_mmap(struct file *file, struct vm_area_struct *vma)
  98. {
  99. struct privcmd_buf_private *file_priv = file->private_data;
  100. struct privcmd_buf_vma_private *vma_priv;
  101. unsigned long count = vma_pages(vma);
  102. unsigned int i;
  103. int ret = 0;
  104. if (!(vma->vm_flags & VM_SHARED))
  105. return -EINVAL;
  106. vma_priv = kzalloc(sizeof(*vma_priv) + count * sizeof(void *),
  107. GFP_KERNEL);
  108. if (!vma_priv)
  109. return -ENOMEM;
  110. for (i = 0; i < count; i++) {
  111. vma_priv->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
  112. if (!vma_priv->pages[i])
  113. break;
  114. vma_priv->n_pages++;
  115. }
  116. mutex_lock(&file_priv->lock);
  117. vma_priv->file_priv = file_priv;
  118. vma_priv->users = 1;
  119. vma->vm_flags |= VM_IO | VM_DONTEXPAND;
  120. vma->vm_ops = &privcmd_buf_vm_ops;
  121. vma->vm_private_data = vma_priv;
  122. list_add(&vma_priv->list, &file_priv->list);
  123. if (vma_priv->n_pages != count)
  124. ret = -ENOMEM;
  125. else
  126. for (i = 0; i < vma_priv->n_pages; i++) {
  127. ret = vm_insert_page(vma, vma->vm_start + i * PAGE_SIZE,
  128. vma_priv->pages[i]);
  129. if (ret)
  130. break;
  131. }
  132. if (ret)
  133. privcmd_buf_vmapriv_free(vma_priv);
  134. mutex_unlock(&file_priv->lock);
  135. return ret;
  136. }
  137. const struct file_operations xen_privcmdbuf_fops = {
  138. .owner = THIS_MODULE,
  139. .open = privcmd_buf_open,
  140. .release = privcmd_buf_release,
  141. .mmap = privcmd_buf_mmap,
  142. };
  143. EXPORT_SYMBOL_GPL(xen_privcmdbuf_fops);
  144. struct miscdevice xen_privcmdbuf_dev = {
  145. .minor = MISC_DYNAMIC_MINOR,
  146. .name = "xen/hypercall",
  147. .fops = &xen_privcmdbuf_fops,
  148. };