restrack.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
  4. */
  5. #include <rdma/rdma_cm.h>
  6. #include <rdma/ib_verbs.h>
  7. #include <rdma/restrack.h>
  8. #include <linux/mutex.h>
  9. #include <linux/sched/task.h>
  10. #include <linux/pid_namespace.h>
  11. #include "cma_priv.h"
  12. static int fill_res_noop(struct sk_buff *msg,
  13. struct rdma_restrack_entry *entry)
  14. {
  15. return 0;
  16. }
  17. void rdma_restrack_init(struct rdma_restrack_root *res)
  18. {
  19. init_rwsem(&res->rwsem);
  20. res->fill_res_entry = fill_res_noop;
  21. }
  22. static const char *type2str(enum rdma_restrack_type type)
  23. {
  24. static const char * const names[RDMA_RESTRACK_MAX] = {
  25. [RDMA_RESTRACK_PD] = "PD",
  26. [RDMA_RESTRACK_CQ] = "CQ",
  27. [RDMA_RESTRACK_QP] = "QP",
  28. [RDMA_RESTRACK_CM_ID] = "CM_ID",
  29. [RDMA_RESTRACK_MR] = "MR",
  30. };
  31. return names[type];
  32. };
  33. void rdma_restrack_clean(struct rdma_restrack_root *res)
  34. {
  35. struct rdma_restrack_entry *e;
  36. char buf[TASK_COMM_LEN];
  37. struct ib_device *dev;
  38. const char *owner;
  39. int bkt;
  40. if (hash_empty(res->hash))
  41. return;
  42. dev = container_of(res, struct ib_device, res);
  43. pr_err("restrack: %s", CUT_HERE);
  44. pr_err("restrack: BUG: RESTRACK detected leak of resources on %s\n",
  45. dev->name);
  46. hash_for_each(res->hash, bkt, e, node) {
  47. if (rdma_is_kernel_res(e)) {
  48. owner = e->kern_name;
  49. } else {
  50. /*
  51. * There is no need to call get_task_struct here,
  52. * because we can be here only if there are more
  53. * get_task_struct() call than put_task_struct().
  54. */
  55. get_task_comm(buf, e->task);
  56. owner = buf;
  57. }
  58. pr_err("restrack: %s %s object allocated by %s is not freed\n",
  59. rdma_is_kernel_res(e) ? "Kernel" : "User",
  60. type2str(e->type), owner);
  61. }
  62. pr_err("restrack: %s", CUT_HERE);
  63. }
  64. int rdma_restrack_count(struct rdma_restrack_root *res,
  65. enum rdma_restrack_type type,
  66. struct pid_namespace *ns)
  67. {
  68. struct rdma_restrack_entry *e;
  69. u32 cnt = 0;
  70. down_read(&res->rwsem);
  71. hash_for_each_possible(res->hash, e, node, type) {
  72. if (ns == &init_pid_ns ||
  73. (!rdma_is_kernel_res(e) &&
  74. ns == task_active_pid_ns(e->task)))
  75. cnt++;
  76. }
  77. up_read(&res->rwsem);
  78. return cnt;
  79. }
  80. EXPORT_SYMBOL(rdma_restrack_count);
  81. static void set_kern_name(struct rdma_restrack_entry *res)
  82. {
  83. struct ib_pd *pd;
  84. switch (res->type) {
  85. case RDMA_RESTRACK_QP:
  86. pd = container_of(res, struct ib_qp, res)->pd;
  87. if (!pd) {
  88. WARN_ONCE(true, "XRC QPs are not supported\n");
  89. /* Survive, despite the programmer's error */
  90. res->kern_name = " ";
  91. }
  92. break;
  93. case RDMA_RESTRACK_MR:
  94. pd = container_of(res, struct ib_mr, res)->pd;
  95. break;
  96. default:
  97. /* Other types set kern_name directly */
  98. pd = NULL;
  99. break;
  100. }
  101. if (pd)
  102. res->kern_name = pd->res.kern_name;
  103. }
  104. static struct ib_device *res_to_dev(struct rdma_restrack_entry *res)
  105. {
  106. switch (res->type) {
  107. case RDMA_RESTRACK_PD:
  108. return container_of(res, struct ib_pd, res)->device;
  109. case RDMA_RESTRACK_CQ:
  110. return container_of(res, struct ib_cq, res)->device;
  111. case RDMA_RESTRACK_QP:
  112. return container_of(res, struct ib_qp, res)->device;
  113. case RDMA_RESTRACK_CM_ID:
  114. return container_of(res, struct rdma_id_private,
  115. res)->id.device;
  116. case RDMA_RESTRACK_MR:
  117. return container_of(res, struct ib_mr, res)->device;
  118. default:
  119. WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
  120. return NULL;
  121. }
  122. }
  123. static bool res_is_user(struct rdma_restrack_entry *res)
  124. {
  125. switch (res->type) {
  126. case RDMA_RESTRACK_PD:
  127. return container_of(res, struct ib_pd, res)->uobject;
  128. case RDMA_RESTRACK_CQ:
  129. return container_of(res, struct ib_cq, res)->uobject;
  130. case RDMA_RESTRACK_QP:
  131. return container_of(res, struct ib_qp, res)->uobject;
  132. case RDMA_RESTRACK_CM_ID:
  133. return !res->kern_name;
  134. case RDMA_RESTRACK_MR:
  135. return container_of(res, struct ib_mr, res)->pd->uobject;
  136. default:
  137. WARN_ONCE(true, "Wrong resource tracking type %u\n", res->type);
  138. return false;
  139. }
  140. }
  141. void rdma_restrack_add(struct rdma_restrack_entry *res)
  142. {
  143. struct ib_device *dev = res_to_dev(res);
  144. if (!dev)
  145. return;
  146. if (res->type != RDMA_RESTRACK_CM_ID || !res_is_user(res))
  147. res->task = NULL;
  148. if (res_is_user(res)) {
  149. if (!res->task)
  150. rdma_restrack_set_task(res, current);
  151. res->kern_name = NULL;
  152. } else {
  153. set_kern_name(res);
  154. }
  155. kref_init(&res->kref);
  156. init_completion(&res->comp);
  157. res->valid = true;
  158. down_write(&dev->res.rwsem);
  159. hash_add(dev->res.hash, &res->node, res->type);
  160. up_write(&dev->res.rwsem);
  161. }
  162. EXPORT_SYMBOL(rdma_restrack_add);
  163. int __must_check rdma_restrack_get(struct rdma_restrack_entry *res)
  164. {
  165. return kref_get_unless_zero(&res->kref);
  166. }
  167. EXPORT_SYMBOL(rdma_restrack_get);
  168. static void restrack_release(struct kref *kref)
  169. {
  170. struct rdma_restrack_entry *res;
  171. res = container_of(kref, struct rdma_restrack_entry, kref);
  172. complete(&res->comp);
  173. }
  174. int rdma_restrack_put(struct rdma_restrack_entry *res)
  175. {
  176. return kref_put(&res->kref, restrack_release);
  177. }
  178. EXPORT_SYMBOL(rdma_restrack_put);
  179. void rdma_restrack_del(struct rdma_restrack_entry *res)
  180. {
  181. struct ib_device *dev;
  182. if (!res->valid)
  183. goto out;
  184. dev = res_to_dev(res);
  185. if (!dev)
  186. return;
  187. rdma_restrack_put(res);
  188. wait_for_completion(&res->comp);
  189. down_write(&dev->res.rwsem);
  190. hash_del(&res->node);
  191. res->valid = false;
  192. up_write(&dev->res.rwsem);
  193. out:
  194. if (res->task) {
  195. put_task_struct(res->task);
  196. res->task = NULL;
  197. }
  198. }
  199. EXPORT_SYMBOL(rdma_restrack_del);