blob: f756fc48eee57b701512a4aec20b8813e51db4f4 [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 Romanovsky02d88832018-01-28 11:17:20 +020016
17/**
18 * enum rdma_restrack_type - HW objects to track
19 */
20enum 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 Wise00313982018-03-01 13:57:44 -080034 * @RDMA_RESTRACK_CM_ID: Connection Manager ID (CM_ID)
35 */
36 RDMA_RESTRACK_CM_ID,
37 /**
Steve Wisefccec5b2018-03-01 13:58:13 -080038 * @RDMA_RESTRACK_MR: Memory Region (MR)
39 */
40 RDMA_RESTRACK_MR,
41 /**
Leon Romanovsky60615212018-11-28 13:16:43 +020042 * @RDMA_RESTRACK_CTX: Verbs contexts (CTX)
43 */
44 RDMA_RESTRACK_CTX,
45 /**
Leon Romanovsky02d88832018-01-28 11:17:20 +020046 * @RDMA_RESTRACK_MAX: Last entry, used for array dclarations
47 */
48 RDMA_RESTRACK_MAX
49};
50
51#define RDMA_RESTRACK_HASH_BITS 8
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 /**
65 * @hash: global database for all resources per-device
66 */
67 DECLARE_HASHTABLE(hash, RDMA_RESTRACK_HASH_BITS);
Steve Wiseda5c8502018-05-03 08:41:30 -070068 /**
69 * @fill_res_entry: driver-specific fill function
70 *
71 * Allows rdma drivers to add their own restrack attributes.
72 */
73 int (*fill_res_entry)(struct sk_buff *msg,
74 struct rdma_restrack_entry *entry);
Leon Romanovsky02d88832018-01-28 11:17:20 +020075};
76
77/**
78 * struct rdma_restrack_entry - metadata per-entry
79 */
80struct rdma_restrack_entry {
81 /**
82 * @valid: validity indicator
83 *
84 * The entries are filled during rdma_restrack_add,
85 * can be attempted to be free during rdma_restrack_del.
86 *
87 * As an example for that, see mlx5 QPs with type MLX5_IB_QPT_HW_GSI
88 */
89 bool valid;
90 /*
91 * @kref: Protect destroy of the resource
92 */
93 struct kref kref;
94 /*
95 * @comp: Signal that all consumers of resource are completed their work
96 */
97 struct completion comp;
98 /**
99 * @task: owner of resource tracking entity
100 *
101 * There are two types of entities: created by user and created
102 * by kernel.
103 *
104 * This is relevant for the entities created by users.
105 * For the entities created by kernel, this pointer will be NULL.
106 */
107 struct task_struct *task;
108 /**
109 * @kern_name: name of owner for the kernel created entities.
110 */
111 const char *kern_name;
112 /**
113 * @node: hash table entry
114 */
115 struct hlist_node node;
116 /**
117 * @type: various objects in restrack database
118 */
119 enum rdma_restrack_type type;
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200120 /**
121 * @user: user resource
122 */
123 bool user;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200124};
125
Leon Romanovsky0ad699c2019-01-30 12:48:58 +0200126void rdma_restrack_init(struct ib_device *dev);
127void rdma_restrack_clean(struct ib_device *dev);
128int rdma_restrack_count(struct ib_device *dev,
Leon Romanovsky02d88832018-01-28 11:17:20 +0200129 enum rdma_restrack_type type,
130 struct pid_namespace *ns);
131
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200132void rdma_restrack_kadd(struct rdma_restrack_entry *res);
133void rdma_restrack_uadd(struct rdma_restrack_entry *res);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200134
135/**
136 * rdma_restrack_del() - delete object from the reource tracking database
137 * @res: resource entry
138 * @type: actual type of object to operate
139 */
140void rdma_restrack_del(struct rdma_restrack_entry *res);
141
142/**
143 * rdma_is_kernel_res() - check the owner of resource
144 * @res: resource entry
145 */
146static inline bool rdma_is_kernel_res(struct rdma_restrack_entry *res)
147{
Shamir Rabinovitchaf8d7032018-12-17 17:15:16 +0200148 return !res->user;
Leon Romanovsky02d88832018-01-28 11:17:20 +0200149}
150
151/**
152 * rdma_restrack_get() - grab to protect resource from release
153 * @res: resource entry
154 */
155int __must_check rdma_restrack_get(struct rdma_restrack_entry *res);
156
157/**
Leon Romanovsky03286032018-03-21 17:12:42 +0200158 * rdma_restrack_put() - release resource
Leon Romanovsky02d88832018-01-28 11:17:20 +0200159 * @res: resource entry
160 */
161int rdma_restrack_put(struct rdma_restrack_entry *res);
Steve Wise00313982018-03-01 13:57:44 -0800162
163/**
164 * rdma_restrack_set_task() - set the task for this resource
165 * @res: resource entry
Leon Romanovsky2165fc22018-10-02 11:48:02 +0300166 * @caller: kernel name, the current task will be used if the caller is NULL.
Steve Wise00313982018-03-01 13:57:44 -0800167 */
Leon Romanovsky363ad352018-10-02 11:48:01 +0300168void rdma_restrack_set_task(struct rdma_restrack_entry *res,
Leon Romanovsky2165fc22018-10-02 11:48:02 +0300169 const char *caller);
Steve Wise00313982018-03-01 13:57:44 -0800170
Steve Wise73937e82018-05-03 08:41:42 -0700171/*
172 * Helper functions for rdma drivers when filling out
173 * nldev driver attributes.
174 */
175int rdma_nl_put_driver_u32(struct sk_buff *msg, const char *name, u32 value);
176int rdma_nl_put_driver_u32_hex(struct sk_buff *msg, const char *name,
177 u32 value);
178int rdma_nl_put_driver_u64(struct sk_buff *msg, const char *name, u64 value);
179int rdma_nl_put_driver_u64_hex(struct sk_buff *msg, const char *name,
180 u64 value);
Leon Romanovsky02d88832018-01-28 11:17:20 +0200181#endif /* _RDMA_RESTRACK_H_ */