rxe_queue.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 retailuce 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/vmalloc.h>
  34. #include "rxe.h"
  35. #include "rxe_loc.h"
  36. #include "rxe_queue.h"
  37. int do_mmap_info(struct rxe_dev *rxe,
  38. struct ib_udata *udata,
  39. bool is_req,
  40. struct ib_ucontext *context,
  41. struct rxe_queue_buf *buf,
  42. size_t buf_size,
  43. struct rxe_mmap_info **ip_p)
  44. {
  45. int err;
  46. u32 len, offset;
  47. struct rxe_mmap_info *ip = NULL;
  48. if (udata) {
  49. if (is_req) {
  50. len = udata->outlen - sizeof(struct mminfo);
  51. offset = sizeof(struct mminfo);
  52. } else {
  53. len = udata->outlen;
  54. offset = 0;
  55. }
  56. if (len < sizeof(ip->info))
  57. goto err1;
  58. ip = rxe_create_mmap_info(rxe, buf_size, context, buf);
  59. if (!ip)
  60. goto err1;
  61. err = copy_to_user(udata->outbuf + offset, &ip->info,
  62. sizeof(ip->info));
  63. if (err)
  64. goto err2;
  65. spin_lock_bh(&rxe->pending_lock);
  66. list_add(&ip->pending_mmaps, &rxe->pending_mmaps);
  67. spin_unlock_bh(&rxe->pending_lock);
  68. }
  69. *ip_p = ip;
  70. return 0;
  71. err2:
  72. kfree(ip);
  73. err1:
  74. return -EINVAL;
  75. }
  76. struct rxe_queue *rxe_queue_init(struct rxe_dev *rxe,
  77. int *num_elem,
  78. unsigned int elem_size)
  79. {
  80. struct rxe_queue *q;
  81. size_t buf_size;
  82. unsigned int num_slots;
  83. /* num_elem == 0 is allowed, but uninteresting */
  84. if (*num_elem < 0)
  85. goto err1;
  86. q = kmalloc(sizeof(*q), GFP_KERNEL);
  87. if (!q)
  88. goto err1;
  89. q->rxe = rxe;
  90. /* used in resize, only need to copy used part of queue */
  91. q->elem_size = elem_size;
  92. /* pad element up to at least a cacheline and always a power of 2 */
  93. if (elem_size < cache_line_size())
  94. elem_size = cache_line_size();
  95. elem_size = roundup_pow_of_two(elem_size);
  96. q->log2_elem_size = order_base_2(elem_size);
  97. num_slots = *num_elem + 1;
  98. num_slots = roundup_pow_of_two(num_slots);
  99. q->index_mask = num_slots - 1;
  100. buf_size = sizeof(struct rxe_queue_buf) + num_slots * elem_size;
  101. q->buf = vmalloc_user(buf_size);
  102. if (!q->buf)
  103. goto err2;
  104. q->buf->log2_elem_size = q->log2_elem_size;
  105. q->buf->index_mask = q->index_mask;
  106. q->buf_size = buf_size;
  107. *num_elem = num_slots - 1;
  108. return q;
  109. err2:
  110. kfree(q);
  111. err1:
  112. return NULL;
  113. }
  114. /* copies elements from original q to new q and then swaps the contents of the
  115. * two q headers. This is so that if anyone is holding a pointer to q it will
  116. * still work
  117. */
  118. static int resize_finish(struct rxe_queue *q, struct rxe_queue *new_q,
  119. unsigned int num_elem)
  120. {
  121. if (!queue_empty(q) && (num_elem < queue_count(q)))
  122. return -EINVAL;
  123. while (!queue_empty(q)) {
  124. memcpy(producer_addr(new_q), consumer_addr(q),
  125. new_q->elem_size);
  126. advance_producer(new_q);
  127. advance_consumer(q);
  128. }
  129. swap(*q, *new_q);
  130. return 0;
  131. }
  132. int rxe_queue_resize(struct rxe_queue *q,
  133. unsigned int *num_elem_p,
  134. unsigned int elem_size,
  135. struct ib_ucontext *context,
  136. struct ib_udata *udata,
  137. spinlock_t *producer_lock,
  138. spinlock_t *consumer_lock)
  139. {
  140. struct rxe_queue *new_q;
  141. unsigned int num_elem = *num_elem_p;
  142. int err;
  143. unsigned long flags = 0, flags1;
  144. new_q = rxe_queue_init(q->rxe, &num_elem, elem_size);
  145. if (!new_q)
  146. return -ENOMEM;
  147. err = do_mmap_info(new_q->rxe, udata, false, context, new_q->buf,
  148. new_q->buf_size, &new_q->ip);
  149. if (err) {
  150. vfree(new_q->buf);
  151. kfree(new_q);
  152. goto err1;
  153. }
  154. spin_lock_irqsave(consumer_lock, flags1);
  155. if (producer_lock) {
  156. spin_lock_irqsave(producer_lock, flags);
  157. err = resize_finish(q, new_q, num_elem);
  158. spin_unlock_irqrestore(producer_lock, flags);
  159. } else {
  160. err = resize_finish(q, new_q, num_elem);
  161. }
  162. spin_unlock_irqrestore(consumer_lock, flags1);
  163. rxe_queue_cleanup(new_q); /* new/old dep on err */
  164. if (err)
  165. goto err1;
  166. *num_elem_p = num_elem;
  167. return 0;
  168. err1:
  169. return err;
  170. }
  171. void rxe_queue_cleanup(struct rxe_queue *q)
  172. {
  173. if (q->ip)
  174. kref_put(&q->ip->ref, rxe_mmap_release);
  175. else
  176. vfree(q->buf);
  177. kfree(q);
  178. }