blob: 14304e2496851102dcfb7c736b17dc854317940e [file] [log] [blame]
Leon Romanovsky02d88832018-01-28 11:17:20 +02001/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
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>
Steve Wise00313982018-03-01 13:57:44 -080014#include <linux/sched/task.h>
Leon Romanovsky02d88832018-01-28 11:17:20 +020015
16/**
17 * enum rdma_restrack_type - HW objects to track
18 */
19enum rdma_restrack_type {
20 /**
21 * @RDMA_RESTRACK_PD: Protection domain (PD)
22 */
23 RDMA_RESTRACK_PD,
24 /**
25 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
26 */
27 RDMA_RESTRACK_CQ,
28 /**
29 * @RDMA_RESTRACK_QP: Queue pair (QP)
30 */
31 RDMA_RESTRACK_QP,
32 /**
Steve Wise00313982018-03-01 13:57:44 -080033 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
34 */
35 RDMA_RESTRACK_CM_ID,
36 /**
Steve Wisefccec5b2018-03-01 13:58:13 -080037 * @RDMA_RESTRACK_MR: Memory Region (MR)
38 */
39 RDMA_RESTRACK_MR,
40 /**
Leon Romanovsky02d88832018-01-28 11:17:20 +020041 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
42 */
43 RDMA_RESTRACK_MAX
44};
45
46#define RDMA_RESTRACK_HASH_BITS 8
Steve Wiseda5c8502018-05-03 08:41:30 -070047struct rdma_restrack_entry;
48
Leon Romanovsky02d88832018-01-28 11:17:20 +020049/**
50 * struct rdma_restrack_root - main resource tracking management
51 * entity, per-device
52 */
53struct rdma_restrack_root {
54 /*
55 * @rwsem: Read/write lock to protect lists
56 */
57 struct rw_semaphore rwsem;
58 /**
59 * @hash: global database for all resources per-device
60 */
61 DECLARE_HASHTABLE(hash, RDMA_RESTRACK_HASH_BITS);
Steve Wiseda5c8502018-05-03 08:41:30 -070062 /**
63 * @fill_res_entry: driver-specific fill function
64 *
65 * Allows rdma drivers to add their own restrack attributes.
66 */
67 int (*fill_res_entry)(struct sk_buff *msg,
68 struct rdma_restrack_entry *entry);
Leon Romanovsky02d88832018-01-28 11:17:20 +020069};
70
71/**
72 * struct rdma_restrack_entry - metadata per-entry
73 */
74struct rdma_restrack_entry {
75 /**
76 * @valid: validity indicator
77 *
78 * The entries are filled during rdma_restrack_add,
79 * can be attempted to be free during rdma_restrack_del.
80 *
81 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
82 */
83 bool valid;
84 /*
85 * @kref: Protect destroy of the resource
86 */
87 struct kref kref;
88 /*
89 * @comp: Signal that all consumers of resource are completed their work
90 */
91 struct completion comp;
92 /**
93 * @task: owner of resource tracking entity
94 *
95 * There are two types of entities: created by user and created
96 * by kernel.
97 *
98 * This is relevant for the entities created by users.
99 * For the entities created by kernel, this pointer will be NULL.
100 */
101 struct task_struct *task;
102 /**
103 * @kern_name: name of owner for the kernel created entities.
104 */
105 const char *kern_name;
106 /**
107 * @node: hash table entry
108 */
109 struct hlist_node node;
110 /**
111 * @type: various objects in restrack database
112 */
113 enum rdma_restrack_type type;
114};
115
116/**
117 * rdma_restrack_init() - initialize resource tracking
118 * @res: resource tracking root
119 */
120void rdma_restrack_init(struct rdma_restrack_root *res);
121
122/**
123 * rdma_restrack_clean() - clean resource tracking
124 * @res: resource tracking root
125 */
126void rdma_restrack_clean(struct rdma_restrack_root *res);
127
128/**
129 * rdma_restrack_count() - the current usage of specific object
130 * @res: resource entry
131 * @type: actual type of object to operate
132 * @ns: PID namespace
133 */
134int rdma_restrack_count(struct rdma_restrack_root *res,
135 enum rdma_restrack_type type,
136 struct pid_namespace *ns);
137
138/**
139 * rdma_restrack_add() - add object to the reource tracking database
140 * @res: resource entry
141 */
142void rdma_restrack_add(struct rdma_restrack_entry *res);
143
144/**
145 * rdma_restrack_del() - delete object from the reource tracking database
146 * @res: resource entry
147 * @type: actual type of object to operate
148 */
149void rdma_restrack_del(struct rdma_restrack_entry *res);
150
151/**
152 * rdma_is_kernel_res() - check the owner of resource
153 * @res: resource entry
154 */
155static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
156{
157 return !res->task;
158}
159
160/**
161 * rdma_restrack_get() - grab to protect resource from release
162 * @res: resource entry
163 */
164int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
165
166/**
Leon Romanovsky03286032018-03-21 17:12:42 +0200167 * rdma_restrack_put() - release resource
Leon Romanovsky02d88832018-01-28 11:17:20 +0200168 * @res: resource entry
169 */
170int rdma_restrack_put(struct rdma_restrack_entry *res);
Steve Wise00313982018-03-01 13:57:44 -0800171
172/**
173 * rdma_restrack_set_task() - set the task for this resource
174 * @res: resource entry
175 * @task: task struct
176 */
177static inline void rdma_restrack_set_task(struct rdma_restrack_entry *res,
178 struct task_struct *task)
179{
180 if (res->task)
181 put_task_struct(res->task);
182 get_task_struct(task);
183 res->task = task;
184}
185
Leon Romanovsky02d88832018-01-28 11:17:20 +0200186#endif /* _RDMA_RESTRACK_H_ */