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/sched.h>
11 #include <linux/kref.h>
12 #include <linux/completion.h>
13 #include <linux/sched/task.h>
14 #include <uapi/rdma/rdma_netlink.h>
15 #include <linux/xarray.h>
17 /* Mark entry as containing driver specific details, it is used to provide QP subtype for now */
18 #define RESTRACK_DD XA_MARK_1
24 * enum rdma_restrack_type - HW objects to track
26 enum rdma_restrack_type
{
28 * @RDMA_RESTRACK_PD: Protection domain (PD)
32 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
36 * @RDMA_RESTRACK_QP: Queue pair (QP)
40 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
44 * @RDMA_RESTRACK_MR: Memory Region (MR)
48 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
52 * @RDMA_RESTRACK_COUNTER: Statistic Counter
54 RDMA_RESTRACK_COUNTER
,
56 * @RDMA_RESTRACK_SRQ: Shared receive queue (SRQ)
60 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
66 * struct rdma_restrack_entry - metadata per-entry
68 struct rdma_restrack_entry
{
70 * @valid: validity indicator
72 * The entries are filled during rdma_restrack_add,
73 * can be attempted to be free during rdma_restrack_del.
75 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
79 * @no_track: don't add this entry to restrack DB
81 * This field is used to mark an entry that doesn't need to be added to
82 * internal restrack DB and presented later to the users at the nldev
87 * @kref: Protect destroy of the resource
91 * @comp: Signal that all consumers of resource are completed their work
93 struct completion comp
;
95 * @task: owner of resource tracking entity
97 * There are two types of entities: created by user and created
100 * This is relevant for the entities created by users.
101 * For the entities created by kernel, this pointer will be NULL.
103 struct task_struct
*task
;
105 * @kern_name: name of owner for the kernel created entities.
107 const char *kern_name
;
109 * @type: various objects in restrack database
111 enum rdma_restrack_type type
;
113 * @user: user resource
117 * @id: ID to expose to users
122 int rdma_restrack_count(struct ib_device
*dev
, enum rdma_restrack_type type
,
125 * rdma_is_kernel_res() - check the owner of resource
126 * @res: resource entry
128 static inline bool rdma_is_kernel_res(const struct rdma_restrack_entry
*res
)
134 * rdma_restrack_get() - grab to protect resource from release
135 * @res: resource entry
137 int __must_check
rdma_restrack_get(struct rdma_restrack_entry
*res
);
140 * rdma_restrack_put() - release resource
141 * @res: resource entry
143 int rdma_restrack_put(struct rdma_restrack_entry
*res
);
146 * Helper functions for rdma drivers when filling out
147 * nldev driver attributes.
149 int rdma_nl_put_driver_u32(struct sk_buff
*msg
, const char *name
, u32 value
);
150 int rdma_nl_put_driver_u32_hex(struct sk_buff
*msg
, const char *name
,
152 int rdma_nl_put_driver_u64(struct sk_buff
*msg
, const char *name
, u64 value
);
153 int rdma_nl_put_driver_u64_hex(struct sk_buff
*msg
, const char *name
,
155 int rdma_nl_put_driver_string(struct sk_buff
*msg
, const char *name
,
157 int rdma_nl_stat_hwcounter_entry(struct sk_buff
*msg
, const char *name
,
160 struct rdma_restrack_entry
*rdma_restrack_get_byid(struct ib_device
*dev
,
161 enum rdma_restrack_type type
,
165 * rdma_restrack_no_track() - don't add resource to the DB
166 * @res: resource entry
168 * Every user of this API should be cross examined.
169 * Probably you don't need to use this function.
171 static inline void rdma_restrack_no_track(struct rdma_restrack_entry
*res
)
173 res
->no_track
= true;
175 static inline bool rdma_restrack_is_tracked(struct rdma_restrack_entry
*res
)
177 return !res
->no_track
;
179 #endif /* _RDMA_RESTRACK_H_ */