xref: /linux/net/core/page_pool_user.c (revision db30e412b7f543d00396ab27f690608cad06aa97)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/mutex.h>
4 #include <linux/netdevice.h>
5 #include <linux/xarray.h>
6 #include <net/busy_poll.h>
7 #include <net/net_debug.h>
8 #include <net/netdev_rx_queue.h>
9 #include <net/page_pool/helpers.h>
10 #include <net/page_pool/types.h>
11 #include <net/page_pool/memory_provider.h>
12 #include <net/sock.h>
13 
14 #include "page_pool_priv.h"
15 #include "netdev-genl-gen.h"
16 
17 static DEFINE_XARRAY_FLAGS(page_pools, XA_FLAGS_ALLOC1);
18 /* Protects: page_pools, netdevice->page_pools, pool->p.napi, pool->slow.netdev,
19  *	pool->user.
20  * Ordering: inside rtnl_lock
21  */
22 DEFINE_MUTEX(page_pools_lock);
23 
24 /* Page pools are only reachable from user space (via netlink) if they are
25  * linked to a netdev at creation time. Following page pool "visibility"
26  * states are possible:
27  *  - normal
28  *    - user.list: linked to real netdev, netdev: real netdev
29  *  - orphaned - real netdev has disappeared
30  *    - user.list: linked to lo, netdev: lo
31  *  - invisible - either (a) created without netdev linking, (b) unlisted due
32  *      to error, or (c) the entire namespace which owned this pool disappeared
33  *    - user.list: unhashed, netdev: unknown
34  */
35 
36 typedef int (*pp_nl_fill_cb)(struct sk_buff *rsp, const struct page_pool *pool,
37 			     const struct genl_info *info);
38 
39 static int
40 netdev_nl_page_pool_get_do(struct genl_info *info, u32 id, pp_nl_fill_cb fill)
41 {
42 	struct page_pool *pool;
43 	struct sk_buff *rsp;
44 	int err;
45 
46 	mutex_lock(&page_pools_lock);
47 	pool = xa_load(&page_pools, id);
48 	if (!pool || hlist_unhashed(&pool->user.list) ||
49 	    !net_eq(dev_net(pool->slow.netdev), genl_info_net(info))) {
50 		err = -ENOENT;
51 		goto err_unlock;
52 	}
53 
54 	rsp = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
55 	if (!rsp) {
56 		err = -ENOMEM;
57 		goto err_unlock;
58 	}
59 
60 	err = fill(rsp, pool, info);
61 	if (err)
62 		goto err_free_msg;
63 
64 	mutex_unlock(&page_pools_lock);
65 
66 	return genlmsg_reply(rsp, info);
67 
68 err_free_msg:
69 	nlmsg_free(rsp);
70 err_unlock:
71 	mutex_unlock(&page_pools_lock);
72 	return err;
73 }
74 
75 struct page_pool_dump_cb {
76 	unsigned long ifindex;
77 	u32 pp_id;
78 };
79 
80 static int
81 netdev_nl_page_pool_get_dump(struct sk_buff *skb, struct netlink_callback *cb,
82 			     pp_nl_fill_cb fill, struct nlattr *ifindex_attr)
83 {
84 	struct page_pool_dump_cb *state = (void *)cb->ctx;
85 	const struct genl_info *info = genl_info_dump(cb);
86 	struct net *net = sock_net(skb->sk);
87 	struct net_device *netdev;
88 	struct page_pool *pool;
89 	int err = 0;
90 
91 	if (ifindex_attr)
92 		state->ifindex = nla_get_u32(ifindex_attr);
93 
94 	rtnl_lock();
95 	mutex_lock(&page_pools_lock);
96 	for_each_netdev_dump(net, netdev, state->ifindex) {
97 		/* Either the provided ifindex doesn't exist or done dumping */
98 		if (ifindex_attr &&
99 		    netdev->ifindex != nla_get_u32(ifindex_attr))
100 			break;
101 
102 		hlist_for_each_entry(pool, &netdev->page_pools, user.list) {
103 			if (state->pp_id && state->pp_id < pool->user.id)
104 				continue;
105 
106 			state->pp_id = pool->user.id;
107 			err = fill(skb, pool, info);
108 			if (err)
109 				goto out;
110 		}
111 
112 		state->pp_id = 0;
113 	}
114 out:
115 	mutex_unlock(&page_pools_lock);
116 	rtnl_unlock();
117 
118 	return err;
119 }
120 
121 static int
122 page_pool_nl_stats_fill(struct sk_buff *rsp, const struct page_pool *pool,
123 			const struct genl_info *info)
124 {
125 #ifdef CONFIG_PAGE_POOL_STATS
126 	struct page_pool_stats stats = {};
127 	struct nlattr *nest;
128 	void *hdr;
129 
130 	if (!page_pool_get_stats(pool, &stats))
131 		return 0;
132 
133 	hdr = genlmsg_iput(rsp, info);
134 	if (!hdr)
135 		return -EMSGSIZE;
136 
137 	nest = nla_nest_start(rsp, NETDEV_A_PAGE_POOL_STATS_INFO);
138 
139 	if (nla_put_uint(rsp, NETDEV_A_PAGE_POOL_ID, pool->user.id) ||
140 	    (pool->slow.netdev->ifindex != LOOPBACK_IFINDEX &&
141 	     nla_put_u32(rsp, NETDEV_A_PAGE_POOL_IFINDEX,
142 			 pool->slow.netdev->ifindex)))
143 		goto err_cancel_nest;
144 
145 	nla_nest_end(rsp, nest);
146 
147 	if (nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_ALLOC_FAST,
148 			 stats.alloc_stats.fast) ||
149 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_ALLOC_SLOW,
150 			 stats.alloc_stats.slow) ||
151 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_ALLOC_SLOW_HIGH_ORDER,
152 			 stats.alloc_stats.slow_high_order) ||
153 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_ALLOC_EMPTY,
154 			 stats.alloc_stats.empty) ||
155 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_ALLOC_REFILL,
156 			 stats.alloc_stats.refill) ||
157 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_ALLOC_WAIVE,
158 			 stats.alloc_stats.waive) ||
159 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_RECYCLE_CACHED,
160 			 stats.recycle_stats.cached) ||
161 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_RECYCLE_CACHE_FULL,
162 			 stats.recycle_stats.cache_full) ||
163 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_RECYCLE_RING,
164 			 stats.recycle_stats.ring) ||
165 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_RECYCLE_RING_FULL,
166 			 stats.recycle_stats.ring_full) ||
167 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_STATS_RECYCLE_RELEASED_REFCNT,
168 			 stats.recycle_stats.released_refcnt))
169 		goto err_cancel_msg;
170 
171 	genlmsg_end(rsp, hdr);
172 
173 	return 0;
174 err_cancel_nest:
175 	nla_nest_cancel(rsp, nest);
176 err_cancel_msg:
177 	genlmsg_cancel(rsp, hdr);
178 	return -EMSGSIZE;
179 #else
180 	GENL_SET_ERR_MSG(info, "kernel built without CONFIG_PAGE_POOL_STATS");
181 	return -EOPNOTSUPP;
182 #endif
183 }
184 
185 int netdev_nl_page_pool_stats_get_doit(struct sk_buff *skb,
186 				       struct genl_info *info)
187 {
188 	struct nlattr *tb[ARRAY_SIZE(netdev_page_pool_info_nl_policy)];
189 	struct nlattr *nest;
190 	int err;
191 	u32 id;
192 
193 	if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_PAGE_POOL_STATS_INFO))
194 		return -EINVAL;
195 
196 	nest = info->attrs[NETDEV_A_PAGE_POOL_STATS_INFO];
197 	err = nla_parse_nested(tb, ARRAY_SIZE(tb) - 1, nest,
198 			       netdev_page_pool_info_nl_policy,
199 			       info->extack);
200 	if (err)
201 		return err;
202 
203 	if (NL_REQ_ATTR_CHECK(info->extack, nest, tb, NETDEV_A_PAGE_POOL_ID))
204 		return -EINVAL;
205 	if (tb[NETDEV_A_PAGE_POOL_IFINDEX]) {
206 		NL_SET_ERR_MSG_ATTR(info->extack,
207 				    tb[NETDEV_A_PAGE_POOL_IFINDEX],
208 				    "selecting by ifindex not supported");
209 		return -EINVAL;
210 	}
211 
212 	id = nla_get_uint(tb[NETDEV_A_PAGE_POOL_ID]);
213 
214 	return netdev_nl_page_pool_get_do(info, id, page_pool_nl_stats_fill);
215 }
216 
217 static const struct netlink_range_validation page_pool_ifindex_range = {
218 	.min	= 1ULL,
219 	.max	= S32_MAX,
220 };
221 
222 static const struct nla_policy
223 page_pool_stat_info_policy[NETDEV_A_PAGE_POOL_IFINDEX + 1] = {
224 	[NETDEV_A_PAGE_POOL_IFINDEX] =
225 		NLA_POLICY_FULL_RANGE(NLA_U32, &page_pool_ifindex_range),
226 };
227 
228 int netdev_nl_page_pool_stats_get_dumpit(struct sk_buff *skb,
229 					 struct netlink_callback *cb)
230 {
231 	struct nlattr *tb[ARRAY_SIZE(page_pool_stat_info_policy)];
232 	const struct genl_info *info = genl_info_dump(cb);
233 	struct nlattr *ifindex_attr = NULL;
234 
235 	if (info->attrs[NETDEV_A_PAGE_POOL_STATS_INFO]) {
236 		struct nlattr *nest;
237 		int err;
238 
239 		nest = info->attrs[NETDEV_A_PAGE_POOL_STATS_INFO];
240 		err = nla_parse_nested(tb, ARRAY_SIZE(tb) - 1, nest,
241 				       page_pool_stat_info_policy,
242 				       info->extack);
243 		if (err)
244 			return err;
245 
246 		ifindex_attr = tb[NETDEV_A_PAGE_POOL_IFINDEX];
247 	}
248 
249 	return netdev_nl_page_pool_get_dump(skb, cb, page_pool_nl_stats_fill,
250 					    ifindex_attr);
251 }
252 
253 static int
254 page_pool_nl_fill(struct sk_buff *rsp, const struct page_pool *pool,
255 		  const struct genl_info *info)
256 {
257 	size_t inflight, refsz;
258 	unsigned int napi_id;
259 	void *hdr;
260 
261 	hdr = genlmsg_iput(rsp, info);
262 	if (!hdr)
263 		return -EMSGSIZE;
264 
265 	if (nla_put_uint(rsp, NETDEV_A_PAGE_POOL_ID, pool->user.id))
266 		goto err_cancel;
267 
268 	if (pool->slow.netdev->ifindex != LOOPBACK_IFINDEX &&
269 	    nla_put_u32(rsp, NETDEV_A_PAGE_POOL_IFINDEX,
270 			pool->slow.netdev->ifindex))
271 		goto err_cancel;
272 
273 	napi_id = pool->p.napi ? READ_ONCE(pool->p.napi->napi_id) : 0;
274 	if (napi_id_valid(napi_id) &&
275 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_NAPI_ID, napi_id))
276 		goto err_cancel;
277 
278 	inflight = page_pool_inflight(pool, false);
279 	refsz =	PAGE_SIZE << pool->p.order;
280 	if (nla_put_uint(rsp, NETDEV_A_PAGE_POOL_INFLIGHT, inflight) ||
281 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_INFLIGHT_MEM,
282 			 inflight * refsz))
283 		goto err_cancel;
284 	if (pool->user.detach_time &&
285 	    nla_put_uint(rsp, NETDEV_A_PAGE_POOL_DETACH_TIME,
286 			 ktime_divns(pool->user.detach_time, NSEC_PER_SEC)))
287 		goto err_cancel;
288 
289 	if (pool->mp_ops && pool->mp_ops->nl_fill(pool->mp_priv, rsp, NULL))
290 		goto err_cancel;
291 
292 	genlmsg_end(rsp, hdr);
293 
294 	return 0;
295 err_cancel:
296 	genlmsg_cancel(rsp, hdr);
297 	return -EMSGSIZE;
298 }
299 
300 static void netdev_nl_page_pool_event(const struct page_pool *pool, u32 cmd)
301 {
302 	struct genl_info info;
303 	struct sk_buff *ntf;
304 	struct net *net;
305 
306 	lockdep_assert_held(&page_pools_lock);
307 
308 	/* 'invisible' page pools don't matter */
309 	if (hlist_unhashed(&pool->user.list))
310 		return;
311 	net = dev_net(pool->slow.netdev);
312 
313 	if (!genl_has_listeners(&netdev_nl_family, net, NETDEV_NLGRP_PAGE_POOL))
314 		return;
315 
316 	genl_info_init_ntf(&info, &netdev_nl_family, cmd);
317 
318 	ntf = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
319 	if (!ntf)
320 		return;
321 
322 	if (page_pool_nl_fill(ntf, pool, &info)) {
323 		nlmsg_free(ntf);
324 		return;
325 	}
326 
327 	genlmsg_multicast_netns(&netdev_nl_family, net, ntf,
328 				0, NETDEV_NLGRP_PAGE_POOL, GFP_KERNEL);
329 }
330 
331 int netdev_nl_page_pool_get_doit(struct sk_buff *skb, struct genl_info *info)
332 {
333 	u32 id;
334 
335 	if (GENL_REQ_ATTR_CHECK(info, NETDEV_A_PAGE_POOL_ID))
336 		return -EINVAL;
337 
338 	id = nla_get_uint(info->attrs[NETDEV_A_PAGE_POOL_ID]);
339 
340 	return netdev_nl_page_pool_get_do(info, id, page_pool_nl_fill);
341 }
342 
343 int netdev_nl_page_pool_get_dumpit(struct sk_buff *skb,
344 				   struct netlink_callback *cb)
345 {
346 	const struct genl_info *info = genl_info_dump(cb);
347 
348 	return netdev_nl_page_pool_get_dump(skb, cb, page_pool_nl_fill,
349 					    info->attrs[NETDEV_A_PAGE_POOL_IFINDEX]);
350 }
351 
352 int page_pool_list(struct page_pool *pool)
353 {
354 	static u32 id_alloc_next;
355 	int err;
356 
357 	mutex_lock(&page_pools_lock);
358 	err = xa_alloc_cyclic(&page_pools, &pool->user.id, pool, xa_limit_32b,
359 			      &id_alloc_next, GFP_KERNEL);
360 	if (err < 0)
361 		goto err_unlock;
362 
363 	INIT_HLIST_NODE(&pool->user.list);
364 	if (pool->slow.netdev) {
365 		hlist_add_head(&pool->user.list,
366 			       &pool->slow.netdev->page_pools);
367 		netdev_nl_page_pool_event(pool, NETDEV_CMD_PAGE_POOL_ADD_NTF);
368 	}
369 
370 	mutex_unlock(&page_pools_lock);
371 	return 0;
372 
373 err_unlock:
374 	mutex_unlock(&page_pools_lock);
375 	return err;
376 }
377 
378 void page_pool_detached(struct page_pool *pool)
379 {
380 	mutex_lock(&page_pools_lock);
381 	pool->user.detach_time = ktime_get_boottime();
382 	netdev_nl_page_pool_event(pool, NETDEV_CMD_PAGE_POOL_CHANGE_NTF);
383 	mutex_unlock(&page_pools_lock);
384 }
385 
386 void page_pool_unlist(struct page_pool *pool)
387 {
388 	mutex_lock(&page_pools_lock);
389 	netdev_nl_page_pool_event(pool, NETDEV_CMD_PAGE_POOL_DEL_NTF);
390 	xa_erase(&page_pools, pool->user.id);
391 	if (!hlist_unhashed(&pool->user.list))
392 		hlist_del(&pool->user.list);
393 	mutex_unlock(&page_pools_lock);
394 }
395 
396 int page_pool_check_memory_provider(struct net_device *dev,
397 				    struct netdev_rx_queue *rxq)
398 {
399 	void *binding = rxq->mp_params.mp_priv;
400 	struct page_pool *pool;
401 	struct hlist_node *n;
402 
403 	if (!binding)
404 		return 0;
405 
406 	mutex_lock(&page_pools_lock);
407 	hlist_for_each_entry_safe(pool, n, &dev->page_pools, user.list) {
408 		if (pool->mp_priv != binding)
409 			continue;
410 
411 		if (pool->slow.queue_idx == get_netdev_rx_queue_index(rxq)) {
412 			mutex_unlock(&page_pools_lock);
413 			return 0;
414 		}
415 	}
416 	mutex_unlock(&page_pools_lock);
417 	return -ENODATA;
418 }
419 
420 static void page_pool_unreg_netdev_wipe(struct net_device *netdev)
421 {
422 	struct page_pool *pool;
423 	struct hlist_node *n;
424 
425 	mutex_lock(&page_pools_lock);
426 	hlist_for_each_entry_safe(pool, n, &netdev->page_pools, user.list) {
427 		hlist_del_init(&pool->user.list);
428 		pool->slow.netdev = NET_PTR_POISON;
429 	}
430 	mutex_unlock(&page_pools_lock);
431 }
432 
433 static void page_pool_unreg_netdev(struct net_device *netdev)
434 {
435 	struct page_pool *pool, *last;
436 	struct net_device *lo;
437 
438 	lo = dev_net(netdev)->loopback_dev;
439 
440 	mutex_lock(&page_pools_lock);
441 	last = NULL;
442 	hlist_for_each_entry(pool, &netdev->page_pools, user.list) {
443 		pool->slow.netdev = lo;
444 		netdev_nl_page_pool_event(pool,
445 					  NETDEV_CMD_PAGE_POOL_CHANGE_NTF);
446 		last = pool;
447 	}
448 	if (last)
449 		hlist_splice_init(&netdev->page_pools, &last->user.list,
450 				  &lo->page_pools);
451 	mutex_unlock(&page_pools_lock);
452 }
453 
454 static int
455 page_pool_netdevice_event(struct notifier_block *nb,
456 			  unsigned long event, void *ptr)
457 {
458 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
459 
460 	if (event != NETDEV_UNREGISTER)
461 		return NOTIFY_DONE;
462 
463 	if (hlist_empty(&netdev->page_pools))
464 		return NOTIFY_OK;
465 
466 	if (netdev->ifindex != LOOPBACK_IFINDEX)
467 		page_pool_unreg_netdev(netdev);
468 	else
469 		page_pool_unreg_netdev_wipe(netdev);
470 	return NOTIFY_OK;
471 }
472 
473 static struct notifier_block page_pool_netdevice_nb = {
474 	.notifier_call = page_pool_netdevice_event,
475 };
476 
477 static int __init page_pool_user_init(void)
478 {
479 	return register_netdevice_notifier(&page_pool_netdevice_nb);
480 }
481 
482 subsys_initcall(page_pool_user_init);
483