Leon Romanovsky | 33edc3b | 2018-06-05 07:48:08 +0300 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 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 Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 14 | #include <linux/sched/task.h> |
Steve Wise | 73937e8 | 2018-05-03 08:41:42 -0700 | [diff] [blame] | 15 | #include <uapi/rdma/rdma_netlink.h> |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 16 | #include <linux/xarray.h> |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * enum rdma_restrack_type - HW objects to track |
| 20 | */ |
| 21 | enum rdma_restrack_type { |
| 22 | /** |
| 23 | * @RDMA_RESTRACK_PD: Protection domain (PD) |
| 24 | */ |
| 25 | RDMA_RESTRACK_PD, |
| 26 | /** |
| 27 | * @RDMA_RESTRACK_CQ: Completion queue (CQ) |
| 28 | */ |
| 29 | RDMA_RESTRACK_CQ, |
| 30 | /** |
| 31 | * @RDMA_RESTRACK_QP: Queue pair (QP) |
| 32 | */ |
| 33 | RDMA_RESTRACK_QP, |
| 34 | /** |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 35 | * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID) |
| 36 | */ |
| 37 | RDMA_RESTRACK_CM_ID, |
| 38 | /** |
Steve Wise | fccec5b | 2018-03-01 13:58:13 -0800 | [diff] [blame] | 39 | * @RDMA_RESTRACK_MR: Memory Region (MR) |
| 40 | */ |
| 41 | RDMA_RESTRACK_MR, |
| 42 | /** |
Leon Romanovsky | 6061521 | 2018-11-28 13:16:43 +0200 | [diff] [blame] | 43 | * @RDMA_RESTRACK_CTX: Verbs contexts (CTX) |
| 44 | */ |
| 45 | RDMA_RESTRACK_CTX, |
| 46 | /** |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 47 | * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations |
| 48 | */ |
| 49 | RDMA_RESTRACK_MAX |
| 50 | }; |
| 51 | |
Leon Romanovsky | 0ad699c | 2019-01-30 12:48:58 +0200 | [diff] [blame] | 52 | struct ib_device; |
Steve Wise | da5c850 | 2018-05-03 08:41:30 -0700 | [diff] [blame] | 53 | struct rdma_restrack_entry; |
| 54 | |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 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 | /** |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 65 | * @xa: Array of XArray structures to hold restrack entries. |
| 66 | * We want to use array of XArrays because insertion is type |
| 67 | * dependent. For types with xisiting unique ID (like QPN), |
| 68 | * we will insert to that unique index. For other types, |
| 69 | * we insert based on pointers and auto-allocate unique index. |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 70 | */ |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 71 | struct xarray xa[RDMA_RESTRACK_MAX]; |
| 72 | /** |
| 73 | * @next_id: Next ID to support cyclic allocation |
| 74 | */ |
| 75 | u32 next_id[RDMA_RESTRACK_MAX]; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * struct rdma_restrack_entry - metadata per-entry |
| 80 | */ |
| 81 | struct rdma_restrack_entry { |
| 82 | /** |
| 83 | * @valid: validity indicator |
| 84 | * |
| 85 | * The entries are filled during rdma_restrack_add, |
| 86 | * can be attempted to be free during rdma_restrack_del. |
| 87 | * |
| 88 | * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI |
| 89 | */ |
| 90 | bool valid; |
| 91 | /* |
| 92 | * @kref: Protect destroy of the resource |
| 93 | */ |
| 94 | struct kref kref; |
| 95 | /* |
| 96 | * @comp: Signal that all consumers of resource are completed their work |
| 97 | */ |
| 98 | struct completion comp; |
| 99 | /** |
| 100 | * @task: owner of resource tracking entity |
| 101 | * |
| 102 | * There are two types of entities: created by user and created |
| 103 | * by kernel. |
| 104 | * |
| 105 | * This is relevant for the entities created by users. |
| 106 | * For the entities created by kernel, this pointer will be NULL. |
| 107 | */ |
| 108 | struct task_struct *task; |
| 109 | /** |
| 110 | * @kern_name: name of owner for the kernel created entities. |
| 111 | */ |
| 112 | const char *kern_name; |
| 113 | /** |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 114 | * @type: various objects in restrack database |
| 115 | */ |
| 116 | enum rdma_restrack_type type; |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 117 | /** |
| 118 | * @user: user resource |
| 119 | */ |
| 120 | bool user; |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 121 | /** |
| 122 | * @id: ID to expose to users |
| 123 | */ |
| 124 | u32 id; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
Leon Romanovsky | 0ad699c | 2019-01-30 12:48:58 +0200 | [diff] [blame] | 127 | void rdma_restrack_init(struct ib_device *dev); |
| 128 | void rdma_restrack_clean(struct ib_device *dev); |
| 129 | int rdma_restrack_count(struct ib_device *dev, |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 130 | enum rdma_restrack_type type, |
| 131 | struct pid_namespace *ns); |
| 132 | |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 133 | void rdma_restrack_kadd(struct rdma_restrack_entry *res); |
| 134 | void rdma_restrack_uadd(struct rdma_restrack_entry *res); |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * rdma_restrack_del() - delete object from the reource tracking database |
| 138 | * @res: resource entry |
| 139 | * @type: actual type of object to operate |
| 140 | */ |
| 141 | void rdma_restrack_del(struct rdma_restrack_entry *res); |
| 142 | |
| 143 | /** |
| 144 | * rdma_is_kernel_res() - check the owner of resource |
| 145 | * @res: resource entry |
| 146 | */ |
| 147 | static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res) |
| 148 | { |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 149 | return !res->user; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
| 153 | * rdma_restrack_get() - grab to protect resource from release |
| 154 | * @res: resource entry |
| 155 | */ |
| 156 | int __must_check rdma_restrack_get(struct rdma_restrack_entry *res); |
| 157 | |
| 158 | /** |
Leon Romanovsky | 0328603 | 2018-03-21 17:12:42 +0200 | [diff] [blame] | 159 | * rdma_restrack_put() - release resource |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 160 | * @res: resource entry |
| 161 | */ |
| 162 | int rdma_restrack_put(struct rdma_restrack_entry *res); |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * rdma_restrack_set_task() - set the task for this resource |
| 166 | * @res: resource entry |
Leon Romanovsky | 2165fc2 | 2018-10-02 11:48:02 +0300 | [diff] [blame] | 167 | * @caller: kernel name, the current task will be used if the caller is NULL. |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 168 | */ |
Leon Romanovsky | 363ad35 | 2018-10-02 11:48:01 +0300 | [diff] [blame] | 169 | void rdma_restrack_set_task(struct rdma_restrack_entry *res, |
Leon Romanovsky | 2165fc2 | 2018-10-02 11:48:02 +0300 | [diff] [blame] | 170 | const char *caller); |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 171 | |
Steve Wise | 73937e8 | 2018-05-03 08:41:42 -0700 | [diff] [blame] | 172 | /* |
| 173 | * Helper functions for rdma drivers when filling out |
| 174 | * nldev driver attributes. |
| 175 | */ |
| 176 | int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value); |
| 177 | int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name, |
| 178 | u32 value); |
| 179 | int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value); |
| 180 | int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, |
| 181 | u64 value); |
Leon Romanovsky | 18c4c66 | 2019-02-18 22:25:44 +0200 | [diff] [blame^] | 182 | struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev, |
| 183 | enum rdma_restrack_type type, |
| 184 | u32 id); |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 185 | #endif /* _RDMA_RESTRACK_H_ */ |