restrack.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/sched.h>
  9. #include <linux/kref.h>
  10. #include <linux/completion.h>
  11. #include <linux/sched/task.h>
  12. #include <uapi/rdma/rdma_netlink.h>
  13. #include <linux/xarray.h>
  14. /* Mark entry as containing driver specific details, it is used to provide QP subtype for now */
  15. #define RESTRACK_DD XA_MARK_1
  16. struct ib_device;
  17. struct sk_buff;
  18. /**
  19. * enum rdma_restrack_type - HW objects to track
  20. */
  21. enum rdma_restrack_type {
  22. /**
  23. * @RDMA_RESTRACK_PD: Protection domain (PD)
  24. */
  25. RDMA_RESTRACK_PD,
  26. /**
  27. * @RDMA_RESTRACK_CQ: Completion queue (CQ)
  28. */
  29. RDMA_RESTRACK_CQ,
  30. /**
  31. * @RDMA_RESTRACK_QP: Queue pair (QP)
  32. */
  33. RDMA_RESTRACK_QP,
  34. /**
  35. * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
  36. */
  37. RDMA_RESTRACK_CM_ID,
  38. /**
  39. * @RDMA_RESTRACK_MR: Memory Region (MR)
  40. */
  41. RDMA_RESTRACK_MR,
  42. /**
  43. * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
  44. */
  45. RDMA_RESTRACK_CTX,
  46. /**
  47. * @RDMA_RESTRACK_COUNTER: Statistic Counter
  48. */
  49. RDMA_RESTRACK_COUNTER,
  50. /**
  51. * @RDMA_RESTRACK_SRQ: Shared receive queue (SRQ)
  52. */
  53. RDMA_RESTRACK_SRQ,
  54. /**
  55. * @RDMA_RESTRACK_DMAH: DMA handle
  56. */
  57. RDMA_RESTRACK_DMAH,
  58. /**
  59. * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
  60. */
  61. RDMA_RESTRACK_MAX
  62. };
  63. /**
  64. * struct rdma_restrack_entry - metadata per-entry
  65. */
  66. struct rdma_restrack_entry {
  67. /**
  68. * @valid: validity indicator
  69. *
  70. * The entries are filled during rdma_restrack_add,
  71. * can be attempted to be free during rdma_restrack_del.
  72. *
  73. * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
  74. */
  75. bool valid;
  76. /**
  77. * @no_track: don't add this entry to restrack DB
  78. *
  79. * This field is used to mark an entry that doesn't need to be added to
  80. * internal restrack DB and presented later to the users at the nldev
  81. * query stage.
  82. */
  83. u8 no_track : 1;
  84. /*
  85. * @kref: Protect destroy of the resource
  86. */
  87. struct kref kref;
  88. /*
  89. * @comp: Signal that all consumers of resource are completed their work
  90. */
  91. struct completion comp;
  92. /**
  93. * @task: owner of resource tracking entity
  94. *
  95. * There are two types of entities: created by user and created
  96. * by kernel.
  97. *
  98. * This is relevant for the entities created by users.
  99. * For the entities created by kernel, this pointer will be NULL.
  100. */
  101. struct task_struct *task;
  102. /**
  103. * @kern_name: name of owner for the kernel created entities.
  104. */
  105. const char *kern_name;
  106. /**
  107. * @type: various objects in restrack database
  108. */
  109. enum rdma_restrack_type type;
  110. /**
  111. * @user: user resource
  112. */
  113. bool user;
  114. /**
  115. * @id: ID to expose to users
  116. */
  117. u32 id;
  118. };
  119. int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type,
  120. bool show_details);
  121. /**
  122. * rdma_is_kernel_res() - check the owner of resource
  123. * @res: resource entry
  124. */
  125. static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)
  126. {
  127. return !res->user;
  128. }
  129. /**
  130. * rdma_restrack_get() - grab to protect resource from release
  131. * @res: resource entry
  132. */
  133. int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
  134. /**
  135. * rdma_restrack_put() - release resource
  136. * @res: resource entry
  137. */
  138. int rdma_restrack_put(struct rdma_restrack_entry *res);
  139. /*
  140. * Helper functions for rdma drivers when filling out
  141. * nldev driver attributes.
  142. */
  143. int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
  144. int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
  145. u32 value);
  146. int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
  147. int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
  148. u64 value);
  149. int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
  150. const char *str);
  151. int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
  152. u64 value);
  153. struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
  154. enum rdma_restrack_type type,
  155. u32 id);
  156. /**
  157. * rdma_restrack_no_track() - don't add resource to the DB
  158. * @res: resource entry
  159. *
  160. * Every user of this API should be cross examined.
  161. * Probably you don't need to use this function.
  162. */
  163. static inline void rdma_restrack_no_track(struct rdma_restrack_entry *res)
  164. {
  165. res->no_track = true;
  166. }
  167. static inline bool rdma_restrack_is_tracked(struct rdma_restrack_entry *res)
  168. {
  169. return !res->no_track;
  170. }
  171. #endif /* _RDMA_RESTRACK_H_ */