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