x86/headers/UAPI: Use __u64 instead of u64 in <uapi/asm/hyperv.h>
[linux/fpc-iii.git] / include / rdma / restrack.h
blobc2d81167c8585ff68c6caf5127013c22b1bb8c11
1 /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
2 /*
3 * Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved.
4 */
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>
15 /**
16 * enum rdma_restrack_type - HW objects to track
18 enum rdma_restrack_type {
19 /**
20 * @RDMA_RESTRACK_PD: Protection domain (PD)
22 RDMA_RESTRACK_PD,
23 /**
24 * @RDMA_RESTRACK_CQ: Completion queue (CQ)
26 RDMA_RESTRACK_CQ,
27 /**
28 * @RDMA_RESTRACK_QP: Queue pair (QP)
30 RDMA_RESTRACK_QP,
31 /**
32 * @RDMA_RESTRACK_XRCD: XRC domain (XRCD)
34 RDMA_RESTRACK_XRCD,
35 /**
36 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
38 RDMA_RESTRACK_MAX
41 #define RDMA_RESTRACK_HASH_BITS 8
42 /**
43 * struct rdma_restrack_root - main resource tracking management
44 * entity, per-device
46 struct rdma_restrack_root {
48 * @rwsem: Read/write lock to protect lists
50 struct rw_semaphore rwsem;
51 /**
52 * @hash: global database for all resources per-device
54 DECLARE_HASHTABLE(hash, RDMA_RESTRACK_HASH_BITS);
57 /**
58 * struct rdma_restrack_entry - metadata per-entry
60 struct rdma_restrack_entry {
61 /**
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
69 bool valid;
71 * @kref: Protect destroy of the resource
73 struct kref kref;
75 * @comp: Signal that all consumers of resource are completed their work
77 struct completion comp;
78 /**
79 * @task: owner of resource tracking entity
81 * There are two types of entities: created by user and created
82 * by kernel.
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;
88 /**
89 * @kern_name: name of owner for the kernel created entities.
91 const char *kern_name;
92 /**
93 * @node: hash table entry
95 struct hlist_node node;
96 /**
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
118 * @ns: PID namespace
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)
143 return !res->task;
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_ */