xref: /linux/include/rdma/restrack.h (revision b68fc09be48edbc47de1a0f3d42ef8adf6c0ac55)
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /*
3  * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
4  */
5 
6 #ifndef _RDMA_RESTRACK_H_
7 #define _RDMA_RESTRACK_H_
8 
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 
17 /**
18  * enum rdma_restrack_type - HW objects to track
19  */
20 enum rdma_restrack_type {
21 	/**
22 	 * @RDMA_RESTRACK_PD: Protection domain (PD)
23 	 */
24 	RDMA_RESTRACK_PD,
25 	/**
26 	 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
27 	 */
28 	RDMA_RESTRACK_CQ,
29 	/**
30 	 * @RDMA_RESTRACK_QP: Queue pair (QP)
31 	 */
32 	RDMA_RESTRACK_QP,
33 	/**
34 	 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
35 	 */
36 	RDMA_RESTRACK_CM_ID,
37 	/**
38 	 * @RDMA_RESTRACK_MR: Memory Region (MR)
39 	 */
40 	RDMA_RESTRACK_MR,
41 	/**
42 	 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
43 	 */
44 	RDMA_RESTRACK_MAX
45 };
46 
47 #define RDMA_RESTRACK_HASH_BITS	8
48 struct rdma_restrack_entry;
49 
50 /**
51  * struct rdma_restrack_root - main resource tracking management
52  * entity, per-device
53  */
54 struct rdma_restrack_root {
55 	/*
56 	 * @rwsem: Read/write lock to protect lists
57 	 */
58 	struct rw_semaphore	rwsem;
59 	/**
60 	 * @hash: global database for all resources per-device
61 	 */
62 	DECLARE_HASHTABLE(hash, RDMA_RESTRACK_HASH_BITS);
63 	/**
64 	 * @fill_res_entry: driver-specific fill function
65 	 *
66 	 * Allows rdma drivers to add their own restrack attributes.
67 	 */
68 	int (*fill_res_entry)(struct sk_buff *msg,
69 			      struct rdma_restrack_entry *entry);
70 };
71 
72 /**
73  * struct rdma_restrack_entry - metadata per-entry
74  */
75 struct rdma_restrack_entry {
76 	/**
77 	 * @valid: validity indicator
78 	 *
79 	 * The entries are filled during rdma_restrack_add,
80 	 * can be attempted to be free during rdma_restrack_del.
81 	 *
82 	 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
83 	 */
84 	bool			valid;
85 	/*
86 	 * @kref: Protect destroy of the resource
87 	 */
88 	struct kref		kref;
89 	/*
90 	 * @comp: Signal that all consumers of resource are completed their work
91 	 */
92 	struct completion	comp;
93 	/**
94 	 * @task: owner of resource tracking entity
95 	 *
96 	 * There are two types of entities: created by user and created
97 	 * by kernel.
98 	 *
99 	 * This is relevant for the entities created by users.
100 	 * For the entities created by kernel, this pointer will be NULL.
101 	 */
102 	struct task_struct	*task;
103 	/**
104 	 * @kern_name: name of owner for the kernel created entities.
105 	 */
106 	const char		*kern_name;
107 	/**
108 	 * @node: hash table entry
109 	 */
110 	struct hlist_node	node;
111 	/**
112 	 * @type: various objects in restrack database
113 	 */
114 	enum rdma_restrack_type	type;
115 };
116 
117 /**
118  * rdma_restrack_init() - initialize resource tracking
119  * @res:  resource tracking root
120  */
121 void rdma_restrack_init(struct rdma_restrack_root *res);
122 
123 /**
124  * rdma_restrack_clean() - clean resource tracking
125  * @res:  resource tracking root
126  */
127 void rdma_restrack_clean(struct rdma_restrack_root *res);
128 
129 /**
130  * rdma_restrack_count() - the current usage of specific object
131  * @res:  resource entry
132  * @type: actual type of object to operate
133  * @ns:   PID namespace
134  */
135 int rdma_restrack_count(struct rdma_restrack_root *res,
136 			enum rdma_restrack_type type,
137 			struct pid_namespace *ns);
138 
139 /**
140  * rdma_restrack_add() - add object to the reource tracking database
141  * @res:  resource entry
142  */
143 void rdma_restrack_add(struct rdma_restrack_entry *res);
144 
145 /**
146  * rdma_restrack_del() - delete object from the reource tracking database
147  * @res:  resource entry
148  * @type: actual type of object to operate
149  */
150 void rdma_restrack_del(struct rdma_restrack_entry *res);
151 
152 /**
153  * rdma_is_kernel_res() - check the owner of resource
154  * @res:  resource entry
155  */
156 static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
157 {
158 	return !res->task;
159 }
160 
161 /**
162  * rdma_restrack_get() - grab to protect resource from release
163  * @res:  resource entry
164  */
165 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
166 
167 /**
168  * rdma_restrack_put() - release resource
169  * @res:  resource entry
170  */
171 int rdma_restrack_put(struct rdma_restrack_entry *res);
172 
173 /**
174  * rdma_restrack_set_task() - set the task for this resource
175  * @res:  resource entry
176  * @task: task struct
177  */
178 static inline void rdma_restrack_set_task(struct rdma_restrack_entry *res,
179 					  struct task_struct *task)
180 {
181 	if (res->task)
182 		put_task_struct(res->task);
183 	get_task_struct(task);
184 	res->task = task;
185 }
186 
187 /*
188  * Helper functions for rdma drivers when filling out
189  * nldev driver attributes.
190  */
191 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
192 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
193 			       u32 value);
194 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
195 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
196 			       u64 value);
197 #endif /* _RDMA_RESTRACK_H_ */
198