ib_umem_odp.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2014 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #ifndef IB_UMEM_ODP_H
  33. #define IB_UMEM_ODP_H
  34. #include <rdma/ib_umem.h>
  35. #include <rdma/ib_verbs.h>
  36. #include <linux/interval_tree.h>
  37. struct umem_odp_node {
  38. u64 __subtree_last;
  39. struct rb_node rb;
  40. };
  41. struct ib_umem_odp {
  42. struct ib_umem umem;
  43. struct ib_ucontext_per_mm *per_mm;
  44. /*
  45. * An array of the pages included in the on-demand paging umem.
  46. * Indices of pages that are currently not mapped into the device will
  47. * contain NULL.
  48. */
  49. struct page **page_list;
  50. /*
  51. * An array of the same size as page_list, with DMA addresses mapped
  52. * for pages the pages in page_list. The lower two bits designate
  53. * access permissions. See ODP_READ_ALLOWED_BIT and
  54. * ODP_WRITE_ALLOWED_BIT.
  55. */
  56. dma_addr_t *dma_list;
  57. /*
  58. * The umem_mutex protects the page_list and dma_list fields of an ODP
  59. * umem, allowing only a single thread to map/unmap pages. The mutex
  60. * also protects access to the mmu notifier counters.
  61. */
  62. struct mutex umem_mutex;
  63. void *private; /* for the HW driver to use. */
  64. int notifiers_seq;
  65. int notifiers_count;
  66. /* Tree tracking */
  67. struct umem_odp_node interval_tree;
  68. struct completion notifier_completion;
  69. int dying;
  70. struct work_struct work;
  71. };
  72. static inline struct ib_umem_odp *to_ib_umem_odp(struct ib_umem *umem)
  73. {
  74. return container_of(umem, struct ib_umem_odp, umem);
  75. }
  76. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  77. struct ib_ucontext_per_mm {
  78. struct ib_ucontext *context;
  79. struct mm_struct *mm;
  80. struct pid *tgid;
  81. bool active;
  82. struct rb_root_cached umem_tree;
  83. /* Protects umem_tree */
  84. struct rw_semaphore umem_rwsem;
  85. struct mmu_notifier mn;
  86. unsigned int odp_mrs_count;
  87. struct list_head ucontext_list;
  88. struct rcu_head rcu;
  89. };
  90. int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access);
  91. struct ib_umem_odp *ib_alloc_odp_umem(struct ib_ucontext_per_mm *per_mm,
  92. unsigned long addr, size_t size);
  93. void ib_umem_odp_release(struct ib_umem_odp *umem_odp);
  94. /*
  95. * The lower 2 bits of the DMA address signal the R/W permissions for
  96. * the entry. To upgrade the permissions, provide the appropriate
  97. * bitmask to the map_dma_pages function.
  98. *
  99. * Be aware that upgrading a mapped address might result in change of
  100. * the DMA address for the page.
  101. */
  102. #define ODP_READ_ALLOWED_BIT (1<<0ULL)
  103. #define ODP_WRITE_ALLOWED_BIT (1<<1ULL)
  104. #define ODP_DMA_ADDR_MASK (~(ODP_READ_ALLOWED_BIT | ODP_WRITE_ALLOWED_BIT))
  105. int ib_umem_odp_map_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
  106. u64 bcnt, u64 access_mask,
  107. unsigned long current_seq);
  108. void ib_umem_odp_unmap_dma_pages(struct ib_umem_odp *umem_odp, u64 start_offset,
  109. u64 bound);
  110. typedef int (*umem_call_back)(struct ib_umem_odp *item, u64 start, u64 end,
  111. void *cookie);
  112. /*
  113. * Call the callback on each ib_umem in the range. Returns the logical or of
  114. * the return values of the functions called.
  115. */
  116. int rbt_ib_umem_for_each_in_range(struct rb_root_cached *root,
  117. u64 start, u64 end,
  118. umem_call_back cb,
  119. bool blockable, void *cookie);
  120. /*
  121. * Find first region intersecting with address range.
  122. * Return NULL if not found
  123. */
  124. struct ib_umem_odp *rbt_ib_umem_lookup(struct rb_root_cached *root,
  125. u64 addr, u64 length);
  126. static inline int ib_umem_mmu_notifier_retry(struct ib_umem_odp *umem_odp,
  127. unsigned long mmu_seq)
  128. {
  129. /*
  130. * This code is strongly based on the KVM code from
  131. * mmu_notifier_retry. Should be called with
  132. * the relevant locks taken (umem_odp->umem_mutex
  133. * and the ucontext umem_mutex semaphore locked for read).
  134. */
  135. if (unlikely(umem_odp->notifiers_count))
  136. return 1;
  137. if (umem_odp->notifiers_seq != mmu_seq)
  138. return 1;
  139. return 0;
  140. }
  141. #else /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  142. static inline int ib_umem_odp_get(struct ib_umem_odp *umem_odp, int access)
  143. {
  144. return -EINVAL;
  145. }
  146. static inline struct ib_umem_odp *
  147. ib_alloc_odp_umem(struct ib_ucontext *context, unsigned long addr, size_t size)
  148. {
  149. return ERR_PTR(-EINVAL);
  150. }
  151. static inline void ib_umem_odp_release(struct ib_umem_odp *umem_odp) {}
  152. #endif /* CONFIG_INFINIBAND_ON_DEMAND_PAGING */
  153. #endif /* IB_UMEM_ODP_H */