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> |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 10 | #include <linux/sched.h> |
| 11 | #include <linux/kref.h> |
| 12 | #include <linux/completion.h> |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 13 | #include <linux/sched/task.h> |
Steve Wise | 73937e8 | 2018-05-03 08:41:42 -0700 | [diff] [blame] | 14 | #include <uapi/rdma/rdma_netlink.h> |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 15 | #include <linux/xarray.h> |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 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 | /** |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 34 | * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID) |
| 35 | */ |
| 36 | RDMA_RESTRACK_CM_ID, |
| 37 | /** |
Steve Wise | fccec5b | 2018-03-01 13:58:13 -0800 | [diff] [blame] | 38 | * @RDMA_RESTRACK_MR: Memory Region (MR) |
| 39 | */ |
| 40 | RDMA_RESTRACK_MR, |
| 41 | /** |
Leon Romanovsky | 6061521 | 2018-11-28 13:16:43 +0200 | [diff] [blame] | 42 | * @RDMA_RESTRACK_CTX: Verbs contexts (CTX) |
| 43 | */ |
| 44 | RDMA_RESTRACK_CTX, |
| 45 | /** |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 46 | * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations |
| 47 | */ |
| 48 | RDMA_RESTRACK_MAX |
| 49 | }; |
| 50 | |
Leon Romanovsky | 0ad699c | 2019-01-30 12:48:58 +0200 | [diff] [blame] | 51 | struct ib_device; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * struct rdma_restrack_entry - metadata per-entry |
| 55 | */ |
| 56 | struct rdma_restrack_entry { |
| 57 | /** |
| 58 | * @valid: validity indicator |
| 59 | * |
| 60 | * The entries are filled during rdma_restrack_add, |
| 61 | * can be attempted to be free during rdma_restrack_del. |
| 62 | * |
| 63 | * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI |
| 64 | */ |
| 65 | bool valid; |
| 66 | /* |
| 67 | * @kref: Protect destroy of the resource |
| 68 | */ |
| 69 | struct kref kref; |
| 70 | /* |
| 71 | * @comp: Signal that all consumers of resource are completed their work |
| 72 | */ |
| 73 | struct completion comp; |
| 74 | /** |
| 75 | * @task: owner of resource tracking entity |
| 76 | * |
| 77 | * There are two types of entities: created by user and created |
| 78 | * by kernel. |
| 79 | * |
| 80 | * This is relevant for the entities created by users. |
| 81 | * For the entities created by kernel, this pointer will be NULL. |
| 82 | */ |
| 83 | struct task_struct *task; |
| 84 | /** |
| 85 | * @kern_name: name of owner for the kernel created entities. |
| 86 | */ |
| 87 | const char *kern_name; |
| 88 | /** |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 89 | * @type: various objects in restrack database |
| 90 | */ |
| 91 | enum rdma_restrack_type type; |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 92 | /** |
| 93 | * @user: user resource |
| 94 | */ |
| 95 | bool user; |
Leon Romanovsky | fd47c2f | 2019-02-18 22:25:43 +0200 | [diff] [blame] | 96 | /** |
| 97 | * @id: ID to expose to users |
| 98 | */ |
| 99 | u32 id; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Leon Romanovsky | 0ad699c | 2019-01-30 12:48:58 +0200 | [diff] [blame] | 102 | int rdma_restrack_count(struct ib_device *dev, |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 103 | enum rdma_restrack_type type, |
| 104 | struct pid_namespace *ns); |
| 105 | |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 106 | void rdma_restrack_kadd(struct rdma_restrack_entry *res); |
| 107 | void rdma_restrack_uadd(struct rdma_restrack_entry *res); |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * rdma_restrack_del() - delete object from the reource tracking database |
| 111 | * @res: resource entry |
| 112 | * @type: actual type of object to operate |
| 113 | */ |
| 114 | void rdma_restrack_del(struct rdma_restrack_entry *res); |
| 115 | |
| 116 | /** |
| 117 | * rdma_is_kernel_res() - check the owner of resource |
| 118 | * @res: resource entry |
| 119 | */ |
| 120 | static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res) |
| 121 | { |
Shamir Rabinovitch | af8d703 | 2018-12-17 17:15:16 +0200 | [diff] [blame] | 122 | return !res->user; |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * rdma_restrack_get() - grab to protect resource from release |
| 127 | * @res: resource entry |
| 128 | */ |
| 129 | int __must_check rdma_restrack_get(struct rdma_restrack_entry *res); |
| 130 | |
| 131 | /** |
Leon Romanovsky | 0328603 | 2018-03-21 17:12:42 +0200 | [diff] [blame] | 132 | * rdma_restrack_put() - release resource |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 133 | * @res: resource entry |
| 134 | */ |
| 135 | int rdma_restrack_put(struct rdma_restrack_entry *res); |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * rdma_restrack_set_task() - set the task for this resource |
| 139 | * @res: resource entry |
Leon Romanovsky | 2165fc2 | 2018-10-02 11:48:02 +0300 | [diff] [blame] | 140 | * @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] | 141 | */ |
Leon Romanovsky | 363ad35 | 2018-10-02 11:48:01 +0300 | [diff] [blame] | 142 | void rdma_restrack_set_task(struct rdma_restrack_entry *res, |
Leon Romanovsky | 2165fc2 | 2018-10-02 11:48:02 +0300 | [diff] [blame] | 143 | const char *caller); |
Steve Wise | 0031398 | 2018-03-01 13:57:44 -0800 | [diff] [blame] | 144 | |
Steve Wise | 73937e8 | 2018-05-03 08:41:42 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Helper functions for rdma drivers when filling out |
| 147 | * nldev driver attributes. |
| 148 | */ |
| 149 | int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value); |
| 150 | int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name, |
| 151 | u32 value); |
| 152 | int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value); |
| 153 | int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name, |
| 154 | u64 value); |
Leon Romanovsky | 18c4c66 | 2019-02-18 22:25:44 +0200 | [diff] [blame] | 155 | struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev, |
| 156 | enum rdma_restrack_type type, |
| 157 | u32 id); |
Leon Romanovsky | 02d8883 | 2018-01-28 11:17:20 +0200 | [diff] [blame] | 158 | #endif /* _RDMA_RESTRACK_H_ */ |