restrack.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_MAX: Last entry, used for array dclarations
  50. */
  51. RDMA_RESTRACK_MAX
  52. };
  53. /**
  54. * struct rdma_restrack_entry - metadata per-entry
  55. */
  56. struct rdma_restrack_entry {
  57. /**
  58. * @valid: validity indicator
  59. *
  60. * The entries are filled during rdma_restrack_add,
  61. * can be attempted to be free during rdma_restrack_del.
  62. *
  63. * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
  64. */
  65. bool valid;
  66. /*
  67. * @kref: Protect destroy of the resource
  68. */
  69. struct kref kref;
  70. /*
  71. * @comp: Signal that all consumers of resource are completed their work
  72. */
  73. struct completion comp;
  74. /**
  75. * @task: owner of resource tracking entity
  76. *
  77. * There are two types of entities: created by user and created
  78. * by kernel.
  79. *
  80. * This is relevant for the entities created by users.
  81. * For the entities created by kernel, this pointer will be NULL.
  82. */
  83. struct task_struct *task;
  84. /**
  85. * @kern_name: name of owner for the kernel created entities.
  86. */
  87. const char *kern_name;
  88. /**
  89. * @type: various objects in restrack database
  90. */
  91. enum rdma_restrack_type type;
  92. /**
  93. * @user: user resource
  94. */
  95. bool user;
  96. /**
  97. * @id: ID to expose to users
  98. */
  99. u32 id;
  100. };
  101. int rdma_restrack_count(struct ib_device *dev,
  102. enum rdma_restrack_type type);
  103. /**
  104. * rdma_is_kernel_res() - check the owner of resource
  105. * @res: resource entry
  106. */
  107. static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry *res)
  108. {
  109. return !res->user;
  110. }
  111. /**
  112. * rdma_restrack_get() - grab to protect resource from release
  113. * @res: resource entry
  114. */
  115. int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
  116. /**
  117. * rdma_restrack_put() - release resource
  118. * @res: resource entry
  119. */
  120. int rdma_restrack_put(struct rdma_restrack_entry *res);
  121. /*
  122. * Helper functions for rdma drivers when filling out
  123. * nldev driver attributes.
  124. */
  125. int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
  126. int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
  127. u32 value);
  128. int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
  129. int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
  130. u64 value);
  131. int rdma_nl_put_driver_string(struct sk_buff *msg, const char *name,
  132. const char *str);
  133. int rdma_nl_stat_hwcounter_entry(struct sk_buff *msg, const char *name,
  134. u64 value);
  135. struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
  136. enum rdma_restrack_type type,
  137. u32 id);
  138. #endif /* _RDMA_RESTRACK_H_ */