restrack.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
  4. */
  5. #ifndef _RDMA_RESTRACK_H_
  6. #define _RDMA_RESTRACK_H_
  7. #include <linux/typecheck.h>
  8. #include <linux/rwsem.h>
  9. #include <linux/sched.h>
  10. #include <linux/kref.h>
  11. #include <linux/completion.h>
  12. #include <linux/sched/task.h>
  13. #include <uapi/rdma/rdma_netlink.h>
  14. /**
  15. * enum rdma_restrack_type - HW objects to track
  16. */
  17. enum rdma_restrack_type {
  18. /**
  19. * @RDMA_RESTRACK_PD: Protection domain (PD)
  20. */
  21. RDMA_RESTRACK_PD,
  22. /**
  23. * @RDMA_RESTRACK_CQ: Completion queue (CQ)
  24. */
  25. RDMA_RESTRACK_CQ,
  26. /**
  27. * @RDMA_RESTRACK_QP: Queue pair (QP)
  28. */
  29. RDMA_RESTRACK_QP,
  30. /**
  31. * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
  32. */
  33. RDMA_RESTRACK_CM_ID,
  34. /**
  35. * @RDMA_RESTRACK_MR: Memory Region (MR)
  36. */
  37. RDMA_RESTRACK_MR,
  38. /**
  39. * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
  40. */
  41. RDMA_RESTRACK_MAX
  42. };
  43. #define RDMA_RESTRACK_HASH_BITS 8
  44. struct rdma_restrack_entry;
  45. /**
  46. * struct rdma_restrack_root - main resource tracking management
  47. * entity, per-device
  48. */
  49. struct rdma_restrack_root {
  50. /*
  51. * @rwsem: Read/write lock to protect lists
  52. */
  53. struct rw_semaphore rwsem;
  54. /**
  55. * @hash: global database for all resources per-device
  56. */
  57. DECLARE_HASHTABLE(hash, RDMA_RESTRACK_HASH_BITS);
  58. /**
  59. * @fill_res_entry: driver-specific fill function
  60. *
  61. * Allows rdma drivers to add their own restrack attributes.
  62. */
  63. int (*fill_res_entry)(struct sk_buff *msg,
  64. struct rdma_restrack_entry *entry);
  65. };
  66. /**
  67. * struct rdma_restrack_entry - metadata per-entry
  68. */
  69. struct rdma_restrack_entry {
  70. /**
  71. * @valid: validity indicator
  72. *
  73. * The entries are filled during rdma_restrack_add,
  74. * can be attempted to be free during rdma_restrack_del.
  75. *
  76. * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
  77. */
  78. bool valid;
  79. /*
  80. * @kref: Protect destroy of the resource
  81. */
  82. struct kref kref;
  83. /*
  84. * @comp: Signal that all consumers of resource are completed their work
  85. */
  86. struct completion comp;
  87. /**
  88. * @task: owner of resource tracking entity
  89. *
  90. * There are two types of entities: created by user and created
  91. * by kernel.
  92. *
  93. * This is relevant for the entities created by users.
  94. * For the entities created by kernel, this pointer will be NULL.
  95. */
  96. struct task_struct *task;
  97. /**
  98. * @kern_name: name of owner for the kernel created entities.
  99. */
  100. const char *kern_name;
  101. /**
  102. * @node: hash table entry
  103. */
  104. struct hlist_node node;
  105. /**
  106. * @type: various objects in restrack database
  107. */
  108. enum rdma_restrack_type type;
  109. };
  110. /**
  111. * rdma_restrack_init() - initialize resource tracking
  112. * @res: resource tracking root
  113. */
  114. void rdma_restrack_init(struct rdma_restrack_root *res);
  115. /**
  116. * rdma_restrack_clean() - clean resource tracking
  117. * @res: resource tracking root
  118. */
  119. void rdma_restrack_clean(struct rdma_restrack_root *res);
  120. /**
  121. * rdma_restrack_count() - the current usage of specific object
  122. * @res: resource entry
  123. * @type: actual type of object to operate
  124. * @ns: PID namespace
  125. */
  126. int rdma_restrack_count(struct rdma_restrack_root *res,
  127. enum rdma_restrack_type type,
  128. struct pid_namespace *ns);
  129. /**
  130. * rdma_restrack_add() - add object to the reource tracking database
  131. * @res: resource entry
  132. */
  133. void rdma_restrack_add(struct rdma_restrack_entry *res);
  134. /**
  135. * rdma_restrack_del() - delete object from the reource tracking database
  136. * @res: resource entry
  137. * @type: actual type of object to operate
  138. */
  139. void rdma_restrack_del(struct rdma_restrack_entry *res);
  140. /**
  141. * rdma_is_kernel_res() - check the owner of resource
  142. * @res: resource entry
  143. */
  144. static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
  145. {
  146. return !res->task;
  147. }
  148. /**
  149. * rdma_restrack_get() - grab to protect resource from release
  150. * @res: resource entry
  151. */
  152. int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
  153. /**
  154. * rdma_restrack_put() - release resource
  155. * @res: resource entry
  156. */
  157. int rdma_restrack_put(struct rdma_restrack_entry *res);
  158. /**
  159. * rdma_restrack_set_task() - set the task for this resource
  160. * @res: resource entry
  161. * @task: task struct
  162. */
  163. static inline void rdma_restrack_set_task(struct rdma_restrack_entry *res,
  164. struct task_struct *task)
  165. {
  166. if (res->task)
  167. put_task_struct(res->task);
  168. get_task_struct(task);
  169. res->task = task;
  170. }
  171. /*
  172. * Helper functions for rdma drivers when filling out
  173. * nldev driver attributes.
  174. */
  175. int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
  176. int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
  177. u32 value);
  178. int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
  179. int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
  180. u64 value);
  181. #endif /* _RDMA_RESTRACK_H_ */