msgpool.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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)
  92. {
  93. wait_queue_t wait;
  94. struct ceph_msg *msg;
  95. if (pool->blocking) {
  96. /* mempool_t behavior; first try to alloc */
  97. msg = ceph_msg_new(0, pool->front_len, 0, 0, NULL);
  98. if (!IS_ERR(msg))
  99. return msg;
  100. }
  101. while (1) {
  102. spin_lock(&pool->lock);
  103. if (likely(pool->num)) {
  104. msg = list_entry(pool->msgs.next, struct ceph_msg,
  105. list_head);
  106. list_del_init(&msg->list_head);
  107. pool->num--;
  108. dout("msgpool_get %p got %p, now %d/%d\n", pool, msg,
  109. pool->num, pool->min);
  110. spin_unlock(&pool->lock);
  111. return msg;
  112. }
  113. pr_err("msgpool_get %p now %d/%d, %s\n", pool, pool->num,
  114. pool->min, pool->blocking ? "waiting" : "failing");
  115. spin_unlock(&pool->lock);
  116. if (!pool->blocking) {
  117. WARN_ON(1);
  118. /* maybe we can allocate it now? */
  119. msg = ceph_msg_new(0, pool->front_len, 0, 0, NULL);
  120. if (!IS_ERR(msg))
  121. return msg;
  122. return ERR_PTR(-ENOMEM);
  123. }
  124. init_wait(&wait);
  125. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  126. schedule();
  127. finish_wait(&pool->wait, &wait);
  128. }
  129. }
  130. void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  131. {
  132. spin_lock(&pool->lock);
  133. if (pool->num < pool->min) {
  134. ceph_msg_get(msg); /* retake a single ref */
  135. list_add(&msg->list_head, &pool->msgs);
  136. pool->num++;
  137. dout("msgpool_put %p reclaim %p, now %d/%d\n", pool, msg,
  138. pool->num, pool->min);
  139. spin_unlock(&pool->lock);
  140. wake_up(&pool->wait);
  141. } else {
  142. dout("msgpool_put %p drop %p, at %d/%d\n", pool, msg,
  143. pool->num, pool->min);
  144. spin_unlock(&pool->lock);
  145. ceph_msg_kfree(msg);
  146. }
  147. }