1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
6 #ifndef _RDMA_RESTRACK_H_
7 #define _RDMA_RESTRACK_H_
9 #include <linux/typecheck.h>
10 #include <linux/rwsem.h>
11 #include <linux/sched.h>
12 #include <linux/kref.h>
13 #include <linux/completion.h>
14 #include <linux/sched/task.h>
15 #include <uapi/rdma/rdma_netlink.h>
16 #include <linux/xarray.h>
19 * enum rdma_restrack_type - HW objects to track
21 enum rdma_restrack_type {
23 * @RDMA_RESTRACK_PD: Protection domain (PD)
27 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
31 * @RDMA_RESTRACK_QP: Queue pair (QP)
35 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
39 * @RDMA_RESTRACK_MR: Memory Region (MR)
43 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
47 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
53 struct rdma_restrack_entry;
56 * struct rdma_restrack_root - main resource tracking management
59 struct rdma_restrack_root {
61 * @rwsem: Read/write lock to protect lists
63 struct rw_semaphore rwsem;
65 * @xa: Array of XArray structures to hold restrack entries.
66 * We want to use array of XArrays because insertion is type
67 * dependent. For types with xisiting unique ID (like QPN),
68 * we will insert to that unique index. For other types,
69 * we insert based on pointers and auto-allocate unique index.
71 struct xarray xa[RDMA_RESTRACK_MAX];
73 * @next_id: Next ID to support cyclic allocation
75 u32 next_id[RDMA_RESTRACK_MAX];
79 * struct rdma_restrack_entry - metadata per-entry
81 struct rdma_restrack_entry {
83 * @valid: validity indicator
85 * The entries are filled during rdma_restrack_add,
86 * can be attempted to be free during rdma_restrack_del.
88 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
92 * @kref: Protect destroy of the resource
96 * @comp: Signal that all consumers of resource are completed their work
98 struct completion comp;
100 * @task: owner of resource tracking entity
102 * There are two types of entities: created by user and created
105 * This is relevant for the entities created by users.
106 * For the entities created by kernel, this pointer will be NULL.
108 struct task_struct *task;
110 * @kern_name: name of owner for the kernel created entities.
112 const char *kern_name;
114 * @type: various objects in restrack database
116 enum rdma_restrack_type type;
118 * @user: user resource
122 * @id: ID to expose to users
127 void rdma_restrack_init(struct ib_device *dev);
128 void rdma_restrack_clean(struct ib_device *dev);
129 int rdma_restrack_count(struct ib_device *dev,
130 enum rdma_restrack_type type,
131 struct pid_namespace *ns);
133 void rdma_restrack_kadd(struct rdma_restrack_entry *res);
134 void rdma_restrack_uadd(struct rdma_restrack_entry *res);
137 * rdma_restrack_del() - delete object from the reource tracking database
138 * @res: resource entry
139 * @type: actual type of object to operate
141 void rdma_restrack_del(struct rdma_restrack_entry *res);
144 * rdma_is_kernel_res() - check the owner of resource
145 * @res: resource entry
147 static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
153 * rdma_restrack_get() - grab to protect resource from release
154 * @res: resource entry
156 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
159 * rdma_restrack_put() - release resource
160 * @res: resource entry
162 int rdma_restrack_put(struct rdma_restrack_entry *res);
165 * rdma_restrack_set_task() - set the task for this resource
166 * @res: resource entry
167 * @caller: kernel name, the current task will be used if the caller is NULL.
169 void rdma_restrack_set_task(struct rdma_restrack_entry *res,
173 * Helper functions for rdma drivers when filling out
174 * nldev driver attributes.
176 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
177 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
179 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
180 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
182 struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
183 enum rdma_restrack_type type,
185 #endif /* _RDMA_RESTRACK_H_ */