memory.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *******************************************************************************
  4. **
  5. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  6. ** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  7. **
  8. **
  9. *******************************************************************************
  10. ******************************************************************************/
  11. #include "dlm_internal.h"
  12. #include "midcomms.h"
  13. #include "lowcomms.h"
  14. #include "config.h"
  15. #include "memory.h"
  16. #include "ast.h"
  17. static struct kmem_cache *writequeue_cache;
  18. static struct kmem_cache *mhandle_cache;
  19. static struct kmem_cache *msg_cache;
  20. static struct kmem_cache *lkb_cache;
  21. static struct kmem_cache *rsb_cache;
  22. static struct kmem_cache *cb_cache;
  23. int __init dlm_memory_init(void)
  24. {
  25. writequeue_cache = dlm_lowcomms_writequeue_cache_create();
  26. if (!writequeue_cache)
  27. goto out;
  28. mhandle_cache = dlm_midcomms_cache_create();
  29. if (!mhandle_cache)
  30. goto mhandle;
  31. lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb),
  32. __alignof__(struct dlm_lkb), 0, NULL);
  33. if (!lkb_cache)
  34. goto lkb;
  35. msg_cache = dlm_lowcomms_msg_cache_create();
  36. if (!msg_cache)
  37. goto msg;
  38. rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb),
  39. __alignof__(struct dlm_rsb), 0, NULL);
  40. if (!rsb_cache)
  41. goto rsb;
  42. cb_cache = kmem_cache_create("dlm_cb", sizeof(struct dlm_callback),
  43. __alignof__(struct dlm_callback), 0,
  44. NULL);
  45. if (!cb_cache)
  46. goto cb;
  47. return 0;
  48. cb:
  49. kmem_cache_destroy(rsb_cache);
  50. rsb:
  51. kmem_cache_destroy(msg_cache);
  52. msg:
  53. kmem_cache_destroy(lkb_cache);
  54. lkb:
  55. kmem_cache_destroy(mhandle_cache);
  56. mhandle:
  57. kmem_cache_destroy(writequeue_cache);
  58. out:
  59. return -ENOMEM;
  60. }
  61. void dlm_memory_exit(void)
  62. {
  63. rcu_barrier();
  64. kmem_cache_destroy(writequeue_cache);
  65. kmem_cache_destroy(mhandle_cache);
  66. kmem_cache_destroy(msg_cache);
  67. kmem_cache_destroy(lkb_cache);
  68. kmem_cache_destroy(rsb_cache);
  69. kmem_cache_destroy(cb_cache);
  70. }
  71. char *dlm_allocate_lvb(struct dlm_ls *ls)
  72. {
  73. return kzalloc(ls->ls_lvblen, GFP_ATOMIC);
  74. }
  75. void dlm_free_lvb(char *p)
  76. {
  77. kfree(p);
  78. }
  79. struct dlm_rsb *dlm_allocate_rsb(void)
  80. {
  81. return kmem_cache_zalloc(rsb_cache, GFP_ATOMIC);
  82. }
  83. static void __free_rsb_rcu(struct rcu_head *rcu)
  84. {
  85. struct dlm_rsb *r = container_of(rcu, struct dlm_rsb, rcu);
  86. if (r->res_lvbptr)
  87. dlm_free_lvb(r->res_lvbptr);
  88. kmem_cache_free(rsb_cache, r);
  89. }
  90. void dlm_free_rsb(struct dlm_rsb *r)
  91. {
  92. call_rcu(&r->rcu, __free_rsb_rcu);
  93. }
  94. struct dlm_lkb *dlm_allocate_lkb(void)
  95. {
  96. return kmem_cache_zalloc(lkb_cache, GFP_ATOMIC);
  97. }
  98. static void __free_lkb_rcu(struct rcu_head *rcu)
  99. {
  100. struct dlm_lkb *lkb = container_of(rcu, struct dlm_lkb, rcu);
  101. if (test_bit(DLM_DFL_USER_BIT, &lkb->lkb_dflags)) {
  102. struct dlm_user_args *ua;
  103. ua = lkb->lkb_ua;
  104. if (ua) {
  105. kfree(ua->lksb.sb_lvbptr);
  106. kfree(ua);
  107. }
  108. }
  109. kmem_cache_free(lkb_cache, lkb);
  110. }
  111. void dlm_free_lkb(struct dlm_lkb *lkb)
  112. {
  113. call_rcu(&lkb->rcu, __free_lkb_rcu);
  114. }
  115. struct dlm_mhandle *dlm_allocate_mhandle(void)
  116. {
  117. return kmem_cache_alloc(mhandle_cache, GFP_ATOMIC);
  118. }
  119. void dlm_free_mhandle(struct dlm_mhandle *mhandle)
  120. {
  121. kmem_cache_free(mhandle_cache, mhandle);
  122. }
  123. struct writequeue_entry *dlm_allocate_writequeue(void)
  124. {
  125. return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC);
  126. }
  127. void dlm_free_writequeue(struct writequeue_entry *writequeue)
  128. {
  129. kmem_cache_free(writequeue_cache, writequeue);
  130. }
  131. struct dlm_msg *dlm_allocate_msg(void)
  132. {
  133. return kmem_cache_alloc(msg_cache, GFP_ATOMIC);
  134. }
  135. void dlm_free_msg(struct dlm_msg *msg)
  136. {
  137. kmem_cache_free(msg_cache, msg);
  138. }
  139. struct dlm_callback *dlm_allocate_cb(void)
  140. {
  141. return kmem_cache_alloc(cb_cache, GFP_ATOMIC);
  142. }
  143. void dlm_free_cb(struct dlm_callback *cb)
  144. {
  145. kmem_cache_free(cb_cache, cb);
  146. }