restrack.c 5.1 KB

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