restrack.h 4.2 KB

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