1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
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>
16 * enum rdma_restrack_type - HW objects to track
18 enum rdma_restrack_type
{
20 * @RDMA_RESTRACK_PD: Protection domain (PD)
24 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
28 * @RDMA_RESTRACK_QP: Queue pair (QP)
32 * @RDMA_RESTRACK_XRCD: XRC domain (XRCD)
36 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
41 #define RDMA_RESTRACK_HASH_BITS 8
43 * struct rdma_restrack_root - main resource tracking management
46 struct rdma_restrack_root
{
48 * @rwsem: Read/write lock to protect lists
50 struct rw_semaphore rwsem
;
52 * @hash: global database for all resources per-device
54 DECLARE_HASHTABLE(hash
, RDMA_RESTRACK_HASH_BITS
);
58 * struct rdma_restrack_entry - metadata per-entry
60 struct rdma_restrack_entry
{
62 * @valid: validity indicator
64 * The entries are filled during rdma_restrack_add,
65 * can be attempted to be free during rdma_restrack_del.
67 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
71 * @kref: Protect destroy of the resource
75 * @comp: Signal that all consumers of resource are completed their work
77 struct completion comp
;
79 * @task: owner of resource tracking entity
81 * There are two types of entities: created by user and created
84 * This is relevant for the entities created by users.
85 * For the entities created by kernel, this pointer will be NULL.
87 struct task_struct
*task
;
89 * @kern_name: name of owner for the kernel created entities.
91 const char *kern_name
;
93 * @node: hash table entry
95 struct hlist_node node
;
97 * @type: various objects in restrack database
99 enum rdma_restrack_type type
;
103 * rdma_restrack_init() - initialize resource tracking
104 * @res: resource tracking root
106 void rdma_restrack_init(struct rdma_restrack_root
*res
);
109 * rdma_restrack_clean() - clean resource tracking
110 * @res: resource tracking root
112 void rdma_restrack_clean(struct rdma_restrack_root
*res
);
115 * rdma_restrack_count() - the current usage of specific object
116 * @res: resource entry
117 * @type: actual type of object to operate
120 int rdma_restrack_count(struct rdma_restrack_root
*res
,
121 enum rdma_restrack_type type
,
122 struct pid_namespace
*ns
);
125 * rdma_restrack_add() - add object to the reource tracking database
126 * @res: resource entry
128 void rdma_restrack_add(struct rdma_restrack_entry
*res
);
131 * rdma_restrack_del() - delete object from the reource tracking database
132 * @res: resource entry
133 * @type: actual type of object to operate
135 void rdma_restrack_del(struct rdma_restrack_entry
*res
);
138 * rdma_is_kernel_res() - check the owner of resource
139 * @res: resource entry
141 static inline bool rdma_is_kernel_res(struct rdma_restrack_entry
*res
)
147 * rdma_restrack_get() - grab to protect resource from release
148 * @res: resource entry
150 int __must_check
rdma_restrack_get(struct rdma_restrack_entry
*res
);
153 * rdma_restrack_put() - relase resource
154 * @res: resource entry
156 int rdma_restrack_put(struct rdma_restrack_entry
*res
);
157 #endif /* _RDMA_RESTRACK_H_ */