xref: /openbmc/linux/net/devlink/core.c (revision db66795f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5  */
6 
7 #include <net/genetlink.h>
8 
9 #include "devl_internal.h"
10 
11 DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
12 
13 void *devlink_priv(struct devlink *devlink)
14 {
15 	return &devlink->priv;
16 }
17 EXPORT_SYMBOL_GPL(devlink_priv);
18 
19 struct devlink *priv_to_devlink(void *priv)
20 {
21 	return container_of(priv, struct devlink, priv);
22 }
23 EXPORT_SYMBOL_GPL(priv_to_devlink);
24 
25 struct device *devlink_to_dev(const struct devlink *devlink)
26 {
27 	return devlink->dev;
28 }
29 EXPORT_SYMBOL_GPL(devlink_to_dev);
30 
31 struct net *devlink_net(const struct devlink *devlink)
32 {
33 	return read_pnet(&devlink->_net);
34 }
35 EXPORT_SYMBOL_GPL(devlink_net);
36 
37 void devl_assert_locked(struct devlink *devlink)
38 {
39 	lockdep_assert_held(&devlink->lock);
40 }
41 EXPORT_SYMBOL_GPL(devl_assert_locked);
42 
43 #ifdef CONFIG_LOCKDEP
44 /* For use in conjunction with LOCKDEP only e.g. rcu_dereference_protected() */
45 bool devl_lock_is_held(struct devlink *devlink)
46 {
47 	return lockdep_is_held(&devlink->lock);
48 }
49 EXPORT_SYMBOL_GPL(devl_lock_is_held);
50 #endif
51 
52 void devl_lock(struct devlink *devlink)
53 {
54 	mutex_lock(&devlink->lock);
55 }
56 EXPORT_SYMBOL_GPL(devl_lock);
57 
58 int devl_trylock(struct devlink *devlink)
59 {
60 	return mutex_trylock(&devlink->lock);
61 }
62 EXPORT_SYMBOL_GPL(devl_trylock);
63 
64 void devl_unlock(struct devlink *devlink)
65 {
66 	mutex_unlock(&devlink->lock);
67 }
68 EXPORT_SYMBOL_GPL(devl_unlock);
69 
70 /**
71  * devlink_try_get() - try to obtain a reference on a devlink instance
72  * @devlink: instance to reference
73  *
74  * Obtain a reference on a devlink instance. A reference on a devlink instance
75  * only implies that it's safe to take the instance lock. It does not imply
76  * that the instance is registered, use devl_is_registered() after taking
77  * the instance lock to check registration status.
78  */
79 struct devlink *__must_check devlink_try_get(struct devlink *devlink)
80 {
81 	if (refcount_inc_not_zero(&devlink->refcount))
82 		return devlink;
83 	return NULL;
84 }
85 
86 static void devlink_release(struct work_struct *work)
87 {
88 	struct devlink *devlink;
89 
90 	devlink = container_of(to_rcu_work(work), struct devlink, rwork);
91 
92 	mutex_destroy(&devlink->lock);
93 	lockdep_unregister_key(&devlink->lock_key);
94 	kfree(devlink);
95 }
96 
97 void devlink_put(struct devlink *devlink)
98 {
99 	if (refcount_dec_and_test(&devlink->refcount))
100 		queue_rcu_work(system_wq, &devlink->rwork);
101 }
102 
103 struct devlink *devlinks_xa_find_get(struct net *net, unsigned long *indexp)
104 {
105 	struct devlink *devlink = NULL;
106 
107 	rcu_read_lock();
108 retry:
109 	devlink = xa_find(&devlinks, indexp, ULONG_MAX, DEVLINK_REGISTERED);
110 	if (!devlink)
111 		goto unlock;
112 
113 	if (!devlink_try_get(devlink))
114 		goto next;
115 	if (!net_eq(devlink_net(devlink), net)) {
116 		devlink_put(devlink);
117 		goto next;
118 	}
119 unlock:
120 	rcu_read_unlock();
121 	return devlink;
122 
123 next:
124 	(*indexp)++;
125 	goto retry;
126 }
127 
128 /**
129  * devl_register - Register devlink instance
130  * @devlink: devlink
131  */
132 int devl_register(struct devlink *devlink)
133 {
134 	ASSERT_DEVLINK_NOT_REGISTERED(devlink);
135 	devl_assert_locked(devlink);
136 
137 	xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
138 	devlink_notify_register(devlink);
139 
140 	return 0;
141 }
142 EXPORT_SYMBOL_GPL(devl_register);
143 
144 void devlink_register(struct devlink *devlink)
145 {
146 	devl_lock(devlink);
147 	devl_register(devlink);
148 	devl_unlock(devlink);
149 }
150 EXPORT_SYMBOL_GPL(devlink_register);
151 
152 /**
153  * devl_unregister - Unregister devlink instance
154  * @devlink: devlink
155  */
156 void devl_unregister(struct devlink *devlink)
157 {
158 	ASSERT_DEVLINK_REGISTERED(devlink);
159 	devl_assert_locked(devlink);
160 
161 	devlink_notify_unregister(devlink);
162 	xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
163 }
164 EXPORT_SYMBOL_GPL(devl_unregister);
165 
166 void devlink_unregister(struct devlink *devlink)
167 {
168 	devl_lock(devlink);
169 	devl_unregister(devlink);
170 	devl_unlock(devlink);
171 }
172 EXPORT_SYMBOL_GPL(devlink_unregister);
173 
174 /**
175  *	devlink_alloc_ns - Allocate new devlink instance resources
176  *	in specific namespace
177  *
178  *	@ops: ops
179  *	@priv_size: size of user private data
180  *	@net: net namespace
181  *	@dev: parent device
182  *
183  *	Allocate new devlink instance resources, including devlink index
184  *	and name.
185  */
186 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
187 				 size_t priv_size, struct net *net,
188 				 struct device *dev)
189 {
190 	struct devlink *devlink;
191 	static u32 last_id;
192 	int ret;
193 
194 	WARN_ON(!ops || !dev);
195 	if (!devlink_reload_actions_valid(ops))
196 		return NULL;
197 
198 	devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
199 	if (!devlink)
200 		return NULL;
201 
202 	ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
203 			      &last_id, GFP_KERNEL);
204 	if (ret < 0)
205 		goto err_xa_alloc;
206 
207 	devlink->dev = dev;
208 	devlink->ops = ops;
209 	xa_init_flags(&devlink->ports, XA_FLAGS_ALLOC);
210 	xa_init_flags(&devlink->params, XA_FLAGS_ALLOC);
211 	xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
212 	write_pnet(&devlink->_net, net);
213 	INIT_LIST_HEAD(&devlink->rate_list);
214 	INIT_LIST_HEAD(&devlink->linecard_list);
215 	INIT_LIST_HEAD(&devlink->sb_list);
216 	INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
217 	INIT_LIST_HEAD(&devlink->resource_list);
218 	INIT_LIST_HEAD(&devlink->region_list);
219 	INIT_LIST_HEAD(&devlink->reporter_list);
220 	INIT_LIST_HEAD(&devlink->trap_list);
221 	INIT_LIST_HEAD(&devlink->trap_group_list);
222 	INIT_LIST_HEAD(&devlink->trap_policer_list);
223 	INIT_RCU_WORK(&devlink->rwork, devlink_release);
224 	lockdep_register_key(&devlink->lock_key);
225 	mutex_init(&devlink->lock);
226 	lockdep_set_class(&devlink->lock, &devlink->lock_key);
227 	refcount_set(&devlink->refcount, 1);
228 
229 	return devlink;
230 
231 err_xa_alloc:
232 	kfree(devlink);
233 	return NULL;
234 }
235 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
236 
237 /**
238  *	devlink_free - Free devlink instance resources
239  *
240  *	@devlink: devlink
241  */
242 void devlink_free(struct devlink *devlink)
243 {
244 	ASSERT_DEVLINK_NOT_REGISTERED(devlink);
245 
246 	WARN_ON(!list_empty(&devlink->trap_policer_list));
247 	WARN_ON(!list_empty(&devlink->trap_group_list));
248 	WARN_ON(!list_empty(&devlink->trap_list));
249 	WARN_ON(!list_empty(&devlink->reporter_list));
250 	WARN_ON(!list_empty(&devlink->region_list));
251 	WARN_ON(!list_empty(&devlink->resource_list));
252 	WARN_ON(!list_empty(&devlink->dpipe_table_list));
253 	WARN_ON(!list_empty(&devlink->sb_list));
254 	WARN_ON(!list_empty(&devlink->rate_list));
255 	WARN_ON(!list_empty(&devlink->linecard_list));
256 	WARN_ON(!xa_empty(&devlink->ports));
257 
258 	xa_destroy(&devlink->snapshot_ids);
259 	xa_destroy(&devlink->params);
260 	xa_destroy(&devlink->ports);
261 
262 	xa_erase(&devlinks, devlink->index);
263 
264 	devlink_put(devlink);
265 }
266 EXPORT_SYMBOL_GPL(devlink_free);
267 
268 static void __net_exit devlink_pernet_pre_exit(struct net *net)
269 {
270 	struct devlink *devlink;
271 	u32 actions_performed;
272 	unsigned long index;
273 	int err;
274 
275 	/* In case network namespace is getting destroyed, reload
276 	 * all devlink instances from this namespace into init_net.
277 	 */
278 	devlinks_xa_for_each_registered_get(net, index, devlink) {
279 		devl_lock(devlink);
280 		err = 0;
281 		if (devl_is_registered(devlink))
282 			err = devlink_reload(devlink, &init_net,
283 					     DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
284 					     DEVLINK_RELOAD_LIMIT_UNSPEC,
285 					     &actions_performed, NULL);
286 		devl_unlock(devlink);
287 		devlink_put(devlink);
288 		if (err && err != -EOPNOTSUPP)
289 			pr_warn("Failed to reload devlink instance into init_net\n");
290 	}
291 }
292 
293 static struct pernet_operations devlink_pernet_ops __net_initdata = {
294 	.pre_exit = devlink_pernet_pre_exit,
295 };
296 
297 static struct notifier_block devlink_port_netdevice_nb = {
298 	.notifier_call = devlink_port_netdevice_event,
299 };
300 
301 static int __init devlink_init(void)
302 {
303 	int err;
304 
305 	err = genl_register_family(&devlink_nl_family);
306 	if (err)
307 		goto out;
308 	err = register_pernet_subsys(&devlink_pernet_ops);
309 	if (err)
310 		goto out;
311 	err = register_netdevice_notifier(&devlink_port_netdevice_nb);
312 
313 out:
314 	WARN_ON(err);
315 	return err;
316 }
317 
318 subsys_initcall(devlink_init);
319