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_CTX: Verbs contexts (CTX) 43 */ 44 RDMA_RESTRACK_CTX, 45 /** 46 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations 47 */ 48 RDMA_RESTRACK_MAX 49 }; 50 51 #define RDMA_RESTRACK_HASH_BITS 8 52 struct rdma_restrack_entry; 53 54 /** 55 * struct rdma_restrack_root - main resource tracking management 56 * entity, per-device 57 */ 58 struct rdma_restrack_root { 59 /* 60 * @rwsem: Read/write lock to protect lists 61 */ 62 struct rw_semaphore rwsem; 63 /** 64 * @hash: global database for all resources per-device 65 */ 66 DECLARE_HASHTABLE(hash, RDMA_RESTRACK_HASH_BITS); 67 /** 68 * @fill_res_entry: driver-specific fill function 69 * 70 * Allows rdma drivers to add their own restrack attributes. 71 */ 72 int (*fill_res_entry)(struct sk_buff *msg, 73 struct rdma_restrack_entry *entry); 74 }; 75 76 /** 77 * struct rdma_restrack_entry - metadata per-entry 78 */ 79 struct rdma_restrack_entry { 80 /** 81 * @valid: validity indicator 82 * 83 * The entries are filled during rdma_restrack_add, 84 * can be attempted to be free during rdma_restrack_del. 85 * 86 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI 87 */ 88 bool valid; 89 /* 90 * @kref: Protect destroy of the resource 91 */ 92 struct kref kref; 93 /* 94 * @comp: Signal that all consumers of resource are completed their work 95 */ 96 struct completion comp; 97 /** 98 * @task: owner of resource tracking entity 99 * 100 * There are two types of entities: created by user and created 101 * by kernel. 102 * 103 * This is relevant for the entities created by users. 104 * For the entities created by kernel, this pointer will be NULL. 105 */ 106 struct task_struct *task; 107 /** 108 * @kern_name: name of owner for the kernel created entities. 109 */ 110 const char *kern_name; 111 /** 112 * @node: hash table entry 113 */ 114 struct hlist_node node; 115 /** 116 * @type: various objects in restrack database 117 */ 118 enum rdma_restrack_type type; 119 /** 120 * @user: user resource 121 */ 122 bool user; 123 }; 124 125 /** 126 * rdma_restrack_init() - initialize resource tracking 127 * @res: resource tracking root 128 */ 129 void rdma_restrack_init(struct rdma_restrack_root *res); 130 131 /** 132 * rdma_restrack_clean() - clean resource tracking 133 * @res: resource tracking root 134 */ 135 void rdma_restrack_clean(struct rdma_restrack_root *res); 136 137 /** 138 * rdma_restrack_count() - the current usage of specific object 139 * @res: resource entry 140 * @type: actual type of object to operate 141 * @ns: PID namespace 142 */ 143 int rdma_restrack_count(struct rdma_restrack_root *res, 144 enum rdma_restrack_type type, 145 struct pid_namespace *ns); 146 147 void rdma_restrack_kadd(struct rdma_restrack_entry *res); 148 void rdma_restrack_uadd(struct rdma_restrack_entry *res); 149 150 /** 151 * rdma_restrack_del() - delete object from the reource tracking database 152 * @res: resource entry 153 * @type: actual type of object to operate 154 */ 155 void rdma_restrack_del(struct rdma_restrack_entry *res); 156 157 /** 158 * rdma_is_kernel_res() - check the owner of resource 159 * @res: resource entry 160 */ 161 static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res) 162 { 163 return !res->user; 164 } 165 166 /** 167 * rdma_restrack_get() - grab to protect resource from release 168 * @res: resource entry 169 */ 170 int __must_check rdma_restrack_get(struct rdma_restrack_entry *res); 171 172 /** 173 * rdma_restrack_put() - release resource 174 * @res: resource entry 175 */ 176 int rdma_restrack_put(struct rdma_restrack_entry *res); 177 178 /** 179 * rdma_restrack_set_task() - set the task for this resource 180 * @res: resource entry 181 * @caller: kernel name, the current task will be used if the caller is NULL. 182 */ 183 void rdma_restrack_set_task(struct rdma_restrack_entry *res, 184 const char *caller); 185 186 /* 187 * Helper functions for rdma drivers when filling out 188 * nldev driver attributes. 189 */ 190 int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value); 191 int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name, 192 u32 value); 193 int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value); 194 int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, 195 u64 value); 196 #endif /* _RDMA_RESTRACK_H_ */ 197