rxe_mmap.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/mm.h>
  36. #include <linux/errno.h>
  37. #include <asm/pgtable.h>
  38. #include "rxe.h"
  39. #include "rxe_loc.h"
  40. #include "rxe_queue.h"
  41. void rxe_mmap_release(struct kref *ref)
  42. {
  43. struct rxe_mmap_info *ip = container_of(ref,
  44. struct rxe_mmap_info, ref);
  45. struct rxe_dev *rxe = to_rdev(ip->context->device);
  46. spin_lock_bh(&rxe->pending_lock);
  47. if (!list_empty(&ip->pending_mmaps))
  48. list_del(&ip->pending_mmaps);
  49. spin_unlock_bh(&rxe->pending_lock);
  50. vfree(ip->obj); /* buf */
  51. kfree(ip);
  52. }
  53. /*
  54. * open and close keep track of how many times the memory region is mapped,
  55. * to avoid releasing it.
  56. */
  57. static void rxe_vma_open(struct vm_area_struct *vma)
  58. {
  59. struct rxe_mmap_info *ip = vma->vm_private_data;
  60. kref_get(&ip->ref);
  61. }
  62. static void rxe_vma_close(struct vm_area_struct *vma)
  63. {
  64. struct rxe_mmap_info *ip = vma->vm_private_data;
  65. kref_put(&ip->ref, rxe_mmap_release);
  66. }
  67. static struct vm_operations_struct rxe_vm_ops = {
  68. .open = rxe_vma_open,
  69. .close = rxe_vma_close,
  70. };
  71. /**
  72. * rxe_mmap - create a new mmap region
  73. * @context: the IB user context of the process making the mmap() call
  74. * @vma: the VMA to be initialized
  75. * Return zero if the mmap is OK. Otherwise, return an errno.
  76. */
  77. int rxe_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  78. {
  79. struct rxe_dev *rxe = to_rdev(context->device);
  80. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  81. unsigned long size = vma->vm_end - vma->vm_start;
  82. struct rxe_mmap_info *ip, *pp;
  83. int ret;
  84. /*
  85. * Search the device's list of objects waiting for a mmap call.
  86. * Normally, this list is very short since a call to create a
  87. * CQ, QP, or SRQ is soon followed by a call to mmap().
  88. */
  89. spin_lock_bh(&rxe->pending_lock);
  90. list_for_each_entry_safe(ip, pp, &rxe->pending_mmaps, pending_mmaps) {
  91. if (context != ip->context || (__u64)offset != ip->info.offset)
  92. continue;
  93. /* Don't allow a mmap larger than the object. */
  94. if (size > ip->info.size) {
  95. pr_err("mmap region is larger than the object!\n");
  96. spin_unlock_bh(&rxe->pending_lock);
  97. ret = -EINVAL;
  98. goto done;
  99. }
  100. goto found_it;
  101. }
  102. pr_warn("unable to find pending mmap info\n");
  103. spin_unlock_bh(&rxe->pending_lock);
  104. ret = -EINVAL;
  105. goto done;
  106. found_it:
  107. list_del_init(&ip->pending_mmaps);
  108. spin_unlock_bh(&rxe->pending_lock);
  109. ret = remap_vmalloc_range(vma, ip->obj, 0);
  110. if (ret) {
  111. pr_err("err %d from remap_vmalloc_range\n", ret);
  112. goto done;
  113. }
  114. vma->vm_ops = &rxe_vm_ops;
  115. vma->vm_private_data = ip;
  116. rxe_vma_open(vma);
  117. done:
  118. return ret;
  119. }
  120. /*
  121. * Allocate information for rxe_mmap
  122. */
  123. struct rxe_mmap_info *rxe_create_mmap_info(struct rxe_dev *rxe,
  124. u32 size,
  125. struct ib_ucontext *context,
  126. void *obj)
  127. {
  128. struct rxe_mmap_info *ip;
  129. ip = kmalloc(sizeof(*ip), GFP_KERNEL);
  130. if (!ip)
  131. return NULL;
  132. size = PAGE_ALIGN(size);
  133. spin_lock_bh(&rxe->mmap_offset_lock);
  134. if (rxe->mmap_offset == 0)
  135. rxe->mmap_offset = ALIGN(PAGE_SIZE, SHMLBA);
  136. ip->info.offset = rxe->mmap_offset;
  137. rxe->mmap_offset += ALIGN(size, SHMLBA);
  138. spin_unlock_bh(&rxe->mmap_offset_lock);
  139. INIT_LIST_HEAD(&ip->pending_mmaps);
  140. ip->info.size = size;
  141. ip->context = context;
  142. ip->obj = obj;
  143. kref_init(&ip->ref);
  144. return ip;
  145. }