msgpool.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/err.h>
  4. #include <linux/sched.h>
  5. #include <linux/types.h>
  6. #include <linux/vmalloc.h>
  7. #include <linux/ceph/messenger.h>
  8. #include <linux/ceph/msgpool.h>
  9. static void *msgpool_alloc(gfp_t gfp_mask, void *arg)
  10. {
  11. struct ceph_msgpool *pool = arg;
  12. struct ceph_msg *msg;
  13. msg = ceph_msg_new(pool->type, pool->front_len, gfp_mask, true);
  14. if (!msg) {
  15. dout("msgpool_alloc %s failed\n", pool->name);
  16. } else {
  17. dout("msgpool_alloc %s %p\n", pool->name, msg);
  18. msg->pool = pool;
  19. }
  20. return msg;
  21. }
  22. static void msgpool_free(void *element, void *arg)
  23. {
  24. struct ceph_msgpool *pool = arg;
  25. struct ceph_msg *msg = element;
  26. dout("msgpool_release %s %p\n", pool->name, msg);
  27. msg->pool = NULL;
  28. ceph_msg_put(msg);
  29. }
  30. int ceph_msgpool_init(struct ceph_msgpool *pool, int type,
  31. int front_len, int size, bool blocking, const char *name)
  32. {
  33. dout("msgpool %s init\n", name);
  34. pool->type = type;
  35. pool->front_len = front_len;
  36. pool->pool = mempool_create(size, msgpool_alloc, msgpool_free, pool);
  37. if (!pool->pool)
  38. return -ENOMEM;
  39. pool->name = name;
  40. return 0;
  41. }
  42. void ceph_msgpool_destroy(struct ceph_msgpool *pool)
  43. {
  44. dout("msgpool %s destroy\n", pool->name);
  45. mempool_destroy(pool->pool);
  46. }
  47. struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool,
  48. int front_len)
  49. {
  50. struct ceph_msg *msg;
  51. if (front_len > pool->front_len) {
  52. dout("msgpool_get %s need front %d, pool size is %d\n",
  53. pool->name, front_len, pool->front_len);
  54. WARN_ON(1);
  55. /* try to alloc a fresh message */
  56. return ceph_msg_new(pool->type, front_len, GFP_NOFS, false);
  57. }
  58. msg = mempool_alloc(pool->pool, GFP_NOFS);
  59. dout("msgpool_get %s %p\n", pool->name, msg);
  60. return msg;
  61. }
  62. void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg)
  63. {
  64. dout("msgpool_put %s %p\n", pool->name, msg);
  65. /* reset msg front_len; user may have changed it */
  66. msg->front.iov_len = pool->front_len;
  67. msg->hdr.front_len = cpu_to_le32(pool->front_len);
  68. kref_init(&msg->kref); /* retake single ref */
  69. mempool_free(msg, pool->pool);
  70. }