mmap.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/slab.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/mm.h>
  50. #include <asm/pgtable.h>
  51. #include "mmap.h"
  52. /**
  53. * rvt_mmap_init - init link list and lock for mem map
  54. * @rdi: rvt dev struct
  55. */
  56. void rvt_mmap_init(struct rvt_dev_info *rdi)
  57. {
  58. INIT_LIST_HEAD(&rdi->pending_mmaps);
  59. spin_lock_init(&rdi->pending_lock);
  60. rdi->mmap_offset = PAGE_SIZE;
  61. spin_lock_init(&rdi->mmap_offset_lock);
  62. }
  63. /**
  64. * rvt_release_mmap_info - free mmap info structure
  65. * @ref: a pointer to the kref within struct rvt_mmap_info
  66. */
  67. void rvt_release_mmap_info(struct kref *ref)
  68. {
  69. struct rvt_mmap_info *ip =
  70. container_of(ref, struct rvt_mmap_info, ref);
  71. struct rvt_dev_info *rdi = ib_to_rvt(ip->context->device);
  72. spin_lock_irq(&rdi->pending_lock);
  73. list_del(&ip->pending_mmaps);
  74. spin_unlock_irq(&rdi->pending_lock);
  75. vfree(ip->obj);
  76. kfree(ip);
  77. }
  78. static void rvt_vma_open(struct vm_area_struct *vma)
  79. {
  80. struct rvt_mmap_info *ip = vma->vm_private_data;
  81. kref_get(&ip->ref);
  82. }
  83. static void rvt_vma_close(struct vm_area_struct *vma)
  84. {
  85. struct rvt_mmap_info *ip = vma->vm_private_data;
  86. kref_put(&ip->ref, rvt_release_mmap_info);
  87. }
  88. static const struct vm_operations_struct rvt_vm_ops = {
  89. .open = rvt_vma_open,
  90. .close = rvt_vma_close,
  91. };
  92. /**
  93. * rvt_mmap - create a new mmap region
  94. * @context: the IB user context of the process making the mmap() call
  95. * @vma: the VMA to be initialized
  96. *
  97. * Return: zero if the mmap is OK. Otherwise, return an errno.
  98. */
  99. int rvt_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  100. {
  101. struct rvt_dev_info *rdi = ib_to_rvt(context->device);
  102. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  103. unsigned long size = vma->vm_end - vma->vm_start;
  104. struct rvt_mmap_info *ip, *pp;
  105. int ret = -EINVAL;
  106. /*
  107. * Search the device's list of objects waiting for a mmap call.
  108. * Normally, this list is very short since a call to create a
  109. * CQ, QP, or SRQ is soon followed by a call to mmap().
  110. */
  111. spin_lock_irq(&rdi->pending_lock);
  112. list_for_each_entry_safe(ip, pp, &rdi->pending_mmaps,
  113. pending_mmaps) {
  114. /* Only the creator is allowed to mmap the object */
  115. if (context != ip->context || (__u64)offset != ip->offset)
  116. continue;
  117. /* Don't allow a mmap larger than the object. */
  118. if (size > ip->size)
  119. break;
  120. list_del_init(&ip->pending_mmaps);
  121. spin_unlock_irq(&rdi->pending_lock);
  122. ret = remap_vmalloc_range(vma, ip->obj, 0);
  123. if (ret)
  124. goto done;
  125. vma->vm_ops = &rvt_vm_ops;
  126. vma->vm_private_data = ip;
  127. rvt_vma_open(vma);
  128. goto done;
  129. }
  130. spin_unlock_irq(&rdi->pending_lock);
  131. done:
  132. return ret;
  133. }
  134. /**
  135. * rvt_create_mmap_info - allocate information for hfi1_mmap
  136. * @rdi: rvt dev struct
  137. * @size: size in bytes to map
  138. * @context: user context
  139. * @obj: opaque pointer to a cq, wq etc
  140. *
  141. * Return: rvt_mmap struct on success
  142. */
  143. struct rvt_mmap_info *rvt_create_mmap_info(struct rvt_dev_info *rdi,
  144. u32 size,
  145. struct ib_ucontext *context,
  146. void *obj)
  147. {
  148. struct rvt_mmap_info *ip;
  149. ip = kmalloc_node(sizeof(*ip), GFP_KERNEL, rdi->dparms.node);
  150. if (!ip)
  151. return ip;
  152. size = PAGE_ALIGN(size);
  153. spin_lock_irq(&rdi->mmap_offset_lock);
  154. if (rdi->mmap_offset == 0)
  155. rdi->mmap_offset = ALIGN(PAGE_SIZE, SHMLBA);
  156. ip->offset = rdi->mmap_offset;
  157. rdi->mmap_offset += ALIGN(size, SHMLBA);
  158. spin_unlock_irq(&rdi->mmap_offset_lock);
  159. INIT_LIST_HEAD(&ip->pending_mmaps);
  160. ip->size = size;
  161. ip->context = context;
  162. ip->obj = obj;
  163. kref_init(&ip->ref);
  164. return ip;
  165. }
  166. /**
  167. * rvt_update_mmap_info - update a mem map
  168. * @rdi: rvt dev struct
  169. * @ip: mmap info pointer
  170. * @size: size to grow by
  171. * @obj: opaque pointer to cq, wq, etc.
  172. */
  173. void rvt_update_mmap_info(struct rvt_dev_info *rdi, struct rvt_mmap_info *ip,
  174. u32 size, void *obj)
  175. {
  176. size = PAGE_ALIGN(size);
  177. spin_lock_irq(&rdi->mmap_offset_lock);
  178. if (rdi->mmap_offset == 0)
  179. rdi->mmap_offset = PAGE_SIZE;
  180. ip->offset = rdi->mmap_offset;
  181. rdi->mmap_offset += size;
  182. spin_unlock_irq(&rdi->mmap_offset_lock);
  183. ip->size = size;
  184. ip->obj = obj;
  185. }