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