restrack.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_MAX: Last entry, used for array dclarations
  56. */
  57. RDMA_RESTRACK_MAX
  58. };
  59. /**
  60. * struct rdma_restrack_entry - metadata per-entry
  61. */
  62. struct rdma_restrack_entry {
  63. /**
  64. * @valid: validity indicator
  65. *
  66. * The entries are filled during rdma_restrack_add,
  67. * can be attempted to be free during rdma_restrack_del.
  68. *
  69. * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
  70. */
  71. bool valid;
  72. /**
  73. * @no_track: don't add this entry to restrack DB
  74. *
  75. * This field is used to mark an entry that doesn't need to be added to
  76. * internal restrack DB and presented later to the users at the nldev
  77. * query stage.
  78. */
  79. u8 no_track : 1;
  80. /*
  81. * @kref: Protect destroy of the resource
  82. */
  83. struct kref kref;
  84. /*
  85. * @comp: Signal that all consumers of resource are completed their work
  86. */
  87. struct completion comp;
  88. /**
  89. * @task: owner of resource tracking entity
  90. *
  91. * There are two types of entities: created by user and created
  92. * by kernel.
  93. *
  94. * This is relevant for the entities created by users.
  95. * For the entities created by kernel, this pointer will be NULL.
  96. */
  97. struct task_struct *task;
  98. /**
  99. * @kern_name: name of owner for the kernel created entities.
  100. */
  101. const char *kern_name;
  102. /**
  103. * @type: various objects in restrack database
  104. */
  105. enum rdma_restrack_type type;
  106. /**
  107. * @user: user resource
  108. */
  109. bool user;
  110. /**
  111. * @id: ID to expose to users
  112. */
  113. u32 id;
  114. };
  115. int rdma_restrack_count(struct ib_device *dev, enum rdma_restrack_type type,
  116. bool show_details);
  117. /**
  118. * rdma_is_kernel_res() - check the owner of resource
  119. * @res: resource entry
  120. */
  121. static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)
  122. {
  123. return !res->user;
  124. }
  125. /**
  126. * rdma_restrack_get() - grab to protect resource from release
  127. * @res: resource entry
  128. */
  129. int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
  130. /**
  131. * rdma_restrack_put() - release resource
  132. * @res: resource entry
  133. */
  134. int rdma_restrack_put(struct rdma_restrack_entry *res);
  135. /*
  136. * Helper functions for rdma drivers when filling out
  137. * nldev driver attributes.
  138. */
  139. int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
  140. int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
  141. u32 value);
  142. int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
  143. int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
  144. u64 value);
  145. int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
  146. const char *str);
  147. int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
  148. u64 value);
  149. struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
  150. enum rdma_restrack_type type,
  151. u32 id);
  152. /**
  153. * rdma_restrack_no_track() - don't add resource to the DB
  154. * @res: resource entry
  155. *
  156. * Every user of this API should be cross examined.
  157. * Probably you don't need to use this function.
  158. */
  159. static inline void rdma_restrack_no_track(struct rdma_restrack_entry *res)
  160. {
  161. res->no_track = true;
  162. }
  163. static inline bool rdma_restrack_is_tracked(struct rdma_restrack_entry *res)
  164. {
  165. return !res->no_track;
  166. }
  167. #endif /* _RDMA_RESTRACK_H_ */