msgpool.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "ceph_debug.h"
  2. #include <linux/err.h>
  3. #include <linux/sched.h>
  4. #include <linux/types.h>
  5. #include <linux/vmalloc.h>
  6. #include "msgpool.h"
  7. /*
  8. * We use msg pools to preallocate memory for messages we expect to
  9. * receive over the wire, to avoid getting ourselves into OOM
  10. * conditions at unexpected times. We take use a few different
  11. * strategies:
  12. *
  13. * - for request/response type interactions, we preallocate the
  14. * memory needed for the response when we generate the request.
  15. *
  16. * - for messages we can receive at any time from the MDS, we preallocate
  17. * a pool of messages we can re-use.
  18. *
  19. * - for writeback, we preallocate some number of messages to use for
  20. * requests and their replies, so that we always make forward
  21. * progress.
  22. *
  23. * The msgpool behaves like a mempool_t, but keeps preallocated
  24. * ceph_msgs strung together on a list_head instead of using a pointer
  25. * vector. This avoids vector reallocation when we adjust the number
  26. * of preallocated items (which happens frequently).
  27. */
  28. /*
  29. * Allocate or release as necessary to meet our target pool size.
  30. */
  31. static int __fill_msgpool(struct ceph_msgpool *pool)
  32. {
  33. struct ceph_msg *msg;
  34. while (pool->num < pool->min) {
  35. dout("fill_msgpool %p %d/%d allocating\n", pool, pool->num,
  36. pool->min);
  37. spin_unlock(&pool->lock);
  38. msg = ceph_msg_new(0, pool->front_len, 0, 0, NULL);
  39. spin_lock(&pool->lock);
  40. if (IS_ERR(msg))
  41. return PTR_ERR(msg);
  42. msg->pool = pool;
  43. list_add(&msg->list_head, &pool->msgs);
  44. pool->num++;
  45. }
  46. while (pool->num > pool->min) {
  47. msg = list_first_entry(&pool->msgs, struct ceph_msg, list_head);
  48. dout("fill_msgpool %p %d/%d releasing %p\n", pool, pool->num,
  49. pool->min, msg);
  50. list_del_init(&msg->list_head);
  51. pool->num--;
  52. ceph_msg_kfree(msg);
  53. }
  54. return 0;
  55. }
  56. int ceph_msgpool_init(struct ceph_msgpool *pool,
  57. int front_len, int min, bool blocking)
  58. {
  59. int ret;
  60. dout("msgpool_init %p front_len %d min %d\n", pool, front_len, min);
  61. spin_lock_init(&pool->lock);
  62. pool->front_len = front_len;
  63. INIT_LIST_HEAD(&pool->msgs);
  64. pool->num = 0;
  65. pool->min = min;
  66. pool->blocking = blocking;
  67. init_waitqueue_head(&pool->wait);
  68. spin_lock(&pool->lock);
  69. ret = __fill_msgpool(pool);
  70. spin_unlock(&pool->lock);
  71. return ret;
  72. }
  73. void ceph_msgpool_destroy(struct ceph_msgpool *pool)
  74. {
  75. dout("msgpool_destroy %p\n", pool);
  76. spin_lock(&pool->lock);
  77. pool->min = 0;
  78. __fill_msgpool(pool);
  79. spin_unlock(&pool->lock);
  80. }
  81. int ceph_msgpool_resv(struct ceph_msgpool *pool, int delta)
  82. {
  83. int ret;
  84. spin_lock(&pool->lock);
  85. dout("msgpool_resv %p delta %d\n", pool, delta);
  86. pool->min += delta;
  87. ret = __fill_msgpool(pool);
  88. spin_unlock(&pool->lock);
  89. return ret;
  90. }
  91. struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool, int front_len)
  92. {
  93. wait_queue_t wait;
  94. struct ceph_msg *msg;
  95. if (front_len && front_len > pool->front_len) {
  96. pr_err("msgpool_get pool %p need front %d, pool size is %d\n",
  97. pool, front_len, pool->front_len);
  98. WARN_ON(1);
  99. /* try to alloc a fresh message */
  100. msg = ceph_msg_new(0, front_len, 0, 0, NULL);
  101. if (!IS_ERR(msg))
  102. return msg;
  103. }
  104. if (!front_len)
  105. front_len = pool->front_len;
  106. if (pool->blocking) {
  107. /* mempool_t behavior; first try to alloc */
  108. msg = ceph_msg_new(0, front_len, 0, 0, NULL);
  109. if (!IS_ERR(msg))
  110. return msg;
  111. }
  112. while (1) {
  113. spin_lock(&pool->lock);
  114. if (likely(pool->num)) {
  115. msg = list_entry(pool->msgs.next, struct ceph_msg,
  116. list_head);
  117. list_del_init(&msg->list_head);
  118. pool->num--;
  119. dout("msgpool_get %p got %p, now %d/%d\n", pool, msg,
  120. pool->num, pool->min);
  121. spin_unlock(&pool->lock);
  122. return msg;
  123. }
  124. pr_err("msgpool_get %p now %d/%d, %s\n", pool, pool->num,
  125. pool->min, pool->blocking ? "waiting" : "may fail");
  126. spin_unlock(&pool->lock);
  127. if (!pool->blocking) {
  128. WARN_ON(1);
  129. /* maybe we can allocate it now? */
  130. msg = ceph_msg_new(0, front_len, 0, 0, NULL);
  131. if (!IS_ERR(msg))
  132. return msg;
  133. pr_err("msgpool_get %p empty + alloc failed\n", pool);
  134. return ERR_PTR(-ENOMEM);
  135. }
  136. init_wait(&wait);
  137. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  138. schedule();
  139. finish_wait(&pool->wait, &wait);
  140. }
  141. }
  142. void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  143. {
  144. spin_lock(&pool->lock);
  145. if (pool->num < pool->min) {
  146. /* reset msg front_len; user may have changed it */
  147. msg->front.iov_len = pool->front_len;
  148. msg->hdr.front_len = cpu_to_le32(pool->front_len);
  149. kref_set(&msg->kref, 1); /* retake a single ref */
  150. list_add(&msg->list_head, &pool->msgs);
  151. pool->num++;
  152. dout("msgpool_put %p reclaim %p, now %d/%d\n", pool, msg,
  153. pool->num, pool->min);
  154. spin_unlock(&pool->lock);
  155. wake_up(&pool->wait);
  156. } else {
  157. dout("msgpool_put %p drop %p, at %d/%d\n", pool, msg,
  158. pool->num, pool->min);
  159. spin_unlock(&pool->lock);
  160. ceph_msg_kfree(msg);
  161. }
  162. }