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>
18 * enum rdma_restrack_type - HW objects to track
20 enum rdma_restrack_type {
22 * @RDMA_RESTRACK_PD: Protection domain (PD)
26 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
30 * @RDMA_RESTRACK_QP: Queue pair (QP)
34 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
38 * @RDMA_RESTRACK_MR: Memory Region (MR)
42 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
46 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
54 * struct rdma_restrack_entry - metadata per-entry
56 struct rdma_restrack_entry {
58 * @valid: validity indicator
60 * The entries are filled during rdma_restrack_add,
61 * can be attempted to be free during rdma_restrack_del.
63 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
67 * @kref: Protect destroy of the resource
71 * @comp: Signal that all consumers of resource are completed their work
73 struct completion comp;
75 * @task: owner of resource tracking entity
77 * There are two types of entities: created by user and created
80 * This is relevant for the entities created by users.
81 * For the entities created by kernel, this pointer will be NULL.
83 struct task_struct *task;
85 * @kern_name: name of owner for the kernel created entities.
87 const char *kern_name;
89 * @type: various objects in restrack database
91 enum rdma_restrack_type type;
93 * @user: user resource
97 * @id: ID to expose to users
102 int rdma_restrack_count(struct ib_device *dev,
103 enum rdma_restrack_type type,
104 struct pid_namespace *ns);
106 void rdma_restrack_kadd(struct rdma_restrack_entry *res);
107 void rdma_restrack_uadd(struct rdma_restrack_entry *res);
110 * rdma_restrack_del() - delete object from the reource tracking database
111 * @res: resource entry
112 * @type: actual type of object to operate
114 void rdma_restrack_del(struct rdma_restrack_entry *res);
117 * rdma_is_kernel_res() - check the owner of resource
118 * @res: resource entry
120 static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
126 * rdma_restrack_get() - grab to protect resource from release
127 * @res: resource entry
129 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
132 * rdma_restrack_put() - release resource
133 * @res: resource entry
135 int rdma_restrack_put(struct rdma_restrack_entry *res);
138 * rdma_restrack_set_task() - set the task for this resource
139 * @res: resource entry
140 * @caller: kernel name, the current task will be used if the caller is NULL.
142 void rdma_restrack_set_task(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 struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
156 enum rdma_restrack_type type,
158 #endif /* _RDMA_RESTRACK_H_ */