rxe_queue.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  5. */
  6. #include <linux/vmalloc.h>
  7. #include "rxe.h"
  8. #include "rxe_loc.h"
  9. #include "rxe_queue.h"
  10. int do_mmap_info(struct rxe_dev *rxe, struct mminfo __user *outbuf,
  11. struct ib_udata *udata, struct rxe_queue_buf *buf,
  12. size_t buf_size, struct rxe_mmap_info **ip_p)
  13. {
  14. int err;
  15. struct rxe_mmap_info *ip = NULL;
  16. if (outbuf) {
  17. ip = rxe_create_mmap_info(rxe, buf_size, udata, buf);
  18. if (IS_ERR(ip)) {
  19. err = PTR_ERR(ip);
  20. goto err1;
  21. }
  22. if (copy_to_user(outbuf, &ip->info, sizeof(ip->info))) {
  23. err = -EFAULT;
  24. goto err2;
  25. }
  26. spin_lock_bh(&rxe->pending_lock);
  27. list_add(&ip->pending_mmaps, &rxe->pending_mmaps);
  28. spin_unlock_bh(&rxe->pending_lock);
  29. }
  30. *ip_p = ip;
  31. return 0;
  32. err2:
  33. kfree(ip);
  34. err1:
  35. return err;
  36. }
  37. inline void rxe_queue_reset(struct rxe_queue *q)
  38. {
  39. /* queue is comprised from header and the memory
  40. * of the actual queue. See "struct rxe_queue_buf" in rxe_queue.h
  41. * reset only the queue itself and not the management header
  42. */
  43. memset(q->buf->data, 0, q->buf_size - sizeof(struct rxe_queue_buf));
  44. }
  45. struct rxe_queue *rxe_queue_init(struct rxe_dev *rxe, int *num_elem,
  46. unsigned int elem_size, enum queue_type type)
  47. {
  48. struct rxe_queue *q;
  49. size_t buf_size;
  50. unsigned int num_slots;
  51. /* num_elem == 0 is allowed, but uninteresting */
  52. if (*num_elem < 0)
  53. goto err1;
  54. q = kzalloc(sizeof(*q), GFP_KERNEL);
  55. if (!q)
  56. goto err1;
  57. q->rxe = rxe;
  58. q->type = type;
  59. /* used in resize, only need to copy used part of queue */
  60. q->elem_size = elem_size;
  61. /* pad element up to at least a cacheline and always a power of 2 */
  62. if (elem_size < cache_line_size())
  63. elem_size = cache_line_size();
  64. elem_size = roundup_pow_of_two(elem_size);
  65. q->log2_elem_size = order_base_2(elem_size);
  66. num_slots = *num_elem + 1;
  67. num_slots = roundup_pow_of_two(num_slots);
  68. q->index_mask = num_slots - 1;
  69. buf_size = sizeof(struct rxe_queue_buf) + num_slots * elem_size;
  70. q->buf = vmalloc_user(buf_size);
  71. if (!q->buf)
  72. goto err2;
  73. q->buf->log2_elem_size = q->log2_elem_size;
  74. q->buf->index_mask = q->index_mask;
  75. q->buf_size = buf_size;
  76. *num_elem = num_slots - 1;
  77. return q;
  78. err2:
  79. kfree(q);
  80. err1:
  81. return NULL;
  82. }
  83. /* copies elements from original q to new q and then swaps the contents of the
  84. * two q headers. This is so that if anyone is holding a pointer to q it will
  85. * still work
  86. */
  87. static int resize_finish(struct rxe_queue *q, struct rxe_queue *new_q,
  88. unsigned int num_elem)
  89. {
  90. enum queue_type type = q->type;
  91. u32 new_prod;
  92. u32 prod;
  93. u32 cons;
  94. if (!queue_empty(q, q->type) && (num_elem < queue_count(q, type)))
  95. return -EINVAL;
  96. new_prod = queue_get_producer(new_q, type);
  97. prod = queue_get_producer(q, type);
  98. cons = queue_get_consumer(q, type);
  99. while ((prod - cons) & q->index_mask) {
  100. memcpy(queue_addr_from_index(new_q, new_prod),
  101. queue_addr_from_index(q, cons), new_q->elem_size);
  102. new_prod = queue_next_index(new_q, new_prod);
  103. cons = queue_next_index(q, cons);
  104. }
  105. new_q->buf->producer_index = new_prod;
  106. q->buf->consumer_index = cons;
  107. /* update private index copies */
  108. if (type == QUEUE_TYPE_TO_CLIENT)
  109. new_q->index = new_q->buf->producer_index;
  110. else
  111. q->index = q->buf->consumer_index;
  112. /* exchange rxe_queue headers */
  113. swap(*q, *new_q);
  114. return 0;
  115. }
  116. int rxe_queue_resize(struct rxe_queue *q, unsigned int *num_elem_p,
  117. unsigned int elem_size, struct ib_udata *udata,
  118. struct mminfo __user *outbuf, spinlock_t *producer_lock,
  119. spinlock_t *consumer_lock)
  120. {
  121. struct rxe_queue *new_q;
  122. unsigned int num_elem = *num_elem_p;
  123. int err;
  124. unsigned long producer_flags;
  125. unsigned long consumer_flags;
  126. new_q = rxe_queue_init(q->rxe, &num_elem, elem_size, q->type);
  127. if (!new_q)
  128. return -ENOMEM;
  129. err = do_mmap_info(new_q->rxe, outbuf, udata, new_q->buf,
  130. new_q->buf_size, &new_q->ip);
  131. if (err) {
  132. vfree(new_q->buf);
  133. kfree(new_q);
  134. goto err1;
  135. }
  136. spin_lock_irqsave(consumer_lock, consumer_flags);
  137. if (producer_lock) {
  138. spin_lock_irqsave(producer_lock, producer_flags);
  139. err = resize_finish(q, new_q, num_elem);
  140. spin_unlock_irqrestore(producer_lock, producer_flags);
  141. } else {
  142. err = resize_finish(q, new_q, num_elem);
  143. }
  144. spin_unlock_irqrestore(consumer_lock, consumer_flags);
  145. rxe_queue_cleanup(new_q); /* new/old dep on err */
  146. if (err)
  147. goto err1;
  148. *num_elem_p = num_elem;
  149. return 0;
  150. err1:
  151. return err;
  152. }
  153. void rxe_queue_cleanup(struct rxe_queue *q)
  154. {
  155. if (q->ip)
  156. kref_put(&q->ip->ref, rxe_mmap_release);
  157. else
  158. vfree(q->buf);
  159. kfree(q);
  160. }