blob: 44ce32cc0b518943af47451100efab2b253f454a [file] [log] [blame]
Leon Romanovsky33edc3b2018-06-05 07:48:08 +03001/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
Leon Romanovsky02d88832018-01-28 11:17:20 +02002/*
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 Wise00313982018-03-01 13:57:44 -080014#include <linux/sched/task.h>
Steve Wise73937e82018-05-03 08:41:42 -070015#include <uapi/rdma/rdma_netlink.h>
Leon Romanovskyfd47c2f2019-02-18 22:25:43 +020016#include <linux/xarray.h>
Leon Romanovsky02d88832018-01-28 11:17:20 +020017
18/**
19 * enum rdma_restrack_type - HW objects to track
20 */
21enum 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 Wise00313982018-03-01 13:57:44 -080035 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
36 */
37 RDMA_RESTRACK_CM_ID,
38 /**
Steve Wisefccec5b2018-03-01 13:58:13 -080039 * @RDMA_RESTRACK_MR: Memory Region (MR)
40 */
41 RDMA_RESTRACK_MR,
42 /**
Leon Romanovsky60615212018-11-28 13:16:43 +020043 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
44 */
45 RDMA_RESTRACK_CTX,
46 /**
Leon Romanovsky02d88832018-01-28 11:17:20 +020047 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
48 */
49 RDMA_RESTRACK_MAX
50};
51
Leon Romanovsky0ad699c2019-01-30 12:48:58 +020052struct ib_device;
Steve Wiseda5c8502018-05-03 08:41:30 -070053struct rdma_restrack_entry;
54
Leon Romanovsky02d88832018-01-28 11:17:20 +020055/**
56 * struct rdma_restrack_root - main resource tracking management
57 * entity, per-device
58 */
59struct rdma_restrack_root {
60 /*
61 * @rwsem: Read/write lock to protect lists
62 */
63 struct rw_semaphore rwsem;
64 /**
Leon Romanovskyfd47c2f2019-02-18 22:25:43 +020065 * @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 Romanovsky02d88832018-01-28 11:17:20 +020070 */
Leon Romanovskyfd47c2f2019-02-18 22:25:43 +020071 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 Romanovsky02d88832018-01-28 11:17:20 +020076};
77
78/**
79 * struct rdma_restrack_entry - metadata per-entry
80 */
81struct 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 Romanovsky02d88832018-01-28 11:17:20 +0200114 * @type: various objects in restrack database
115 */
116 enum rdma_restrack_type type;
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200117 /**
118 * @user: user resource
119 */
120 bool user;
Leon Romanovskyfd47c2f2019-02-18 22:25:43 +0200121 /**
122 * @id: ID to expose to users
123 */
124 u32 id;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200125};
126
Leon Romanovsky0ad699c2019-01-30 12:48:58 +0200127void rdma_restrack_init(struct ib_device *dev);
128void rdma_restrack_clean(struct ib_device *dev);
129int rdma_restrack_count(struct ib_device *dev,
Leon Romanovsky02d88832018-01-28 11:17:20 +0200130 enum rdma_restrack_type type,
131 struct pid_namespace *ns);
132
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200133void rdma_restrack_kadd(struct rdma_restrack_entry *res);
134void rdma_restrack_uadd(struct rdma_restrack_entry *res);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200135
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 */
141void 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 */
147static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
148{
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200149 return !res->user;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200150}
151
152/**
153 * rdma_restrack_get() - grab to protect resource from release
154 * @res: resource entry
155 */
156int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
157
158/**
Leon Romanovsky03286032018-03-21 17:12:42 +0200159 * rdma_restrack_put() - release resource
Leon Romanovsky02d88832018-01-28 11:17:20 +0200160 * @res: resource entry
161 */
162int rdma_restrack_put(struct rdma_restrack_entry *res);
Steve Wise00313982018-03-01 13:57:44 -0800163
164/**
165 * rdma_restrack_set_task() - set the task for this resource
166 * @res: resource entry
Leon Romanovsky2165fc22018-10-02 11:48:02 +0300167 * @caller: kernel name, the current task will be used if the caller is NULL.
Steve Wise00313982018-03-01 13:57:44 -0800168 */
Leon Romanovsky363ad352018-10-02 11:48:01 +0300169void rdma_restrack_set_task(struct rdma_restrack_entry *res,
Leon Romanovsky2165fc22018-10-02 11:48:02 +0300170 const char *caller);
Steve Wise00313982018-03-01 13:57:44 -0800171
Steve Wise73937e82018-05-03 08:41:42 -0700172/*
173 * Helper functions for rdma drivers when filling out
174 * nldev driver attributes.
175 */
176int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
177int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
178 u32 value);
179int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
180int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
181 u64 value);
Leon Romanovsky18c4c662019-02-18 22:25:44 +0200182struct rdma_restrack_entry *rdma_restrack_get_byid(struct ib_device *dev,
183 enum rdma_restrack_type type,
184 u32 id);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200185#endif /* _RDMA_RESTRACK_H_ */