xref: /openbmc/linux/net/core/netprio_cgroup.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25bc1421eSNeil Horman /*
35bc1421eSNeil Horman  * net/core/netprio_cgroup.c	Priority Control Group
45bc1421eSNeil Horman  *
55bc1421eSNeil Horman  * Authors:	Neil Horman <nhorman@tuxdriver.com>
65bc1421eSNeil Horman  */
75bc1421eSNeil Horman 
8e005d193SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9e005d193SJoe Perches 
10c6e970a0SAndrew Lunn #include <linux/module.h>
115bc1421eSNeil Horman #include <linux/slab.h>
125bc1421eSNeil Horman #include <linux/types.h>
135bc1421eSNeil Horman #include <linux/string.h>
145bc1421eSNeil Horman #include <linux/errno.h>
155bc1421eSNeil Horman #include <linux/skbuff.h>
165bc1421eSNeil Horman #include <linux/cgroup.h>
175bc1421eSNeil Horman #include <linux/rcupdate.h>
185bc1421eSNeil Horman #include <linux/atomic.h>
19f719ff9bSIngo Molnar #include <linux/sched/task.h>
20f719ff9bSIngo Molnar 
215bc1421eSNeil Horman #include <net/rtnetlink.h>
225bc1421eSNeil Horman #include <net/pkt_cls.h>
235bc1421eSNeil Horman #include <net/sock.h>
245bc1421eSNeil Horman #include <net/netprio_cgroup.h>
255bc1421eSNeil Horman 
26406a3c63SJohn Fastabend #include <linux/fdtable.h>
27406a3c63SJohn Fastabend 
28297dbde1STejun Heo /*
29297dbde1STejun Heo  * netprio allocates per-net_device priomap array which is indexed by
30297dbde1STejun Heo  * css->id.  Limiting css ID to 16bits doesn't lose anything.
31297dbde1STejun Heo  */
32297dbde1STejun Heo #define NETPRIO_ID_MAX		USHRT_MAX
33297dbde1STejun Heo 
344a6ee25cSTejun Heo #define PRIOMAP_MIN_SZ		128
355bc1421eSNeil Horman 
364a6ee25cSTejun Heo /*
378e3bff96Sstephen hemminger  * Extend @dev->priomap so that it's large enough to accommodate
384a6ee25cSTejun Heo  * @target_idx.  @dev->priomap.priomap_len > @target_idx after successful
394a6ee25cSTejun Heo  * return.  Must be called under rtnl lock.
404a6ee25cSTejun Heo  */
extend_netdev_table(struct net_device * dev,u32 target_idx)414a6ee25cSTejun Heo static int extend_netdev_table(struct net_device *dev, u32 target_idx)
425bc1421eSNeil Horman {
434a6ee25cSTejun Heo 	struct netprio_map *old, *new;
444a6ee25cSTejun Heo 	size_t new_sz, new_len;
455bc1421eSNeil Horman 
464a6ee25cSTejun Heo 	/* is the existing priomap large enough? */
4752bca930STejun Heo 	old = rtnl_dereference(dev->priomap);
484a6ee25cSTejun Heo 	if (old && old->priomap_len > target_idx)
494a6ee25cSTejun Heo 		return 0;
505bc1421eSNeil Horman 
514a6ee25cSTejun Heo 	/*
524a6ee25cSTejun Heo 	 * Determine the new size.  Let's keep it power-of-two.  We start
534a6ee25cSTejun Heo 	 * from PRIOMAP_MIN_SZ and double it until it's large enough to
544a6ee25cSTejun Heo 	 * accommodate @target_idx.
554a6ee25cSTejun Heo 	 */
564a6ee25cSTejun Heo 	new_sz = PRIOMAP_MIN_SZ;
574a6ee25cSTejun Heo 	while (true) {
584a6ee25cSTejun Heo 		new_len = (new_sz - offsetof(struct netprio_map, priomap)) /
594a6ee25cSTejun Heo 			sizeof(new->priomap[0]);
604a6ee25cSTejun Heo 		if (new_len > target_idx)
614a6ee25cSTejun Heo 			break;
624a6ee25cSTejun Heo 		new_sz *= 2;
634a6ee25cSTejun Heo 		/* overflowed? */
644a6ee25cSTejun Heo 		if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
654a6ee25cSTejun Heo 			return -ENOSPC;
664a6ee25cSTejun Heo 	}
674a6ee25cSTejun Heo 
684a6ee25cSTejun Heo 	/* allocate & copy */
694a6ee25cSTejun Heo 	new = kzalloc(new_sz, GFP_KERNEL);
7062b5942aSJoe Perches 	if (!new)
71ef209f15SGao feng 		return -ENOMEM;
725bc1421eSNeil Horman 
7352bca930STejun Heo 	if (old)
7452bca930STejun Heo 		memcpy(new->priomap, old->priomap,
7552bca930STejun Heo 		       old->priomap_len * sizeof(old->priomap[0]));
765bc1421eSNeil Horman 
7752bca930STejun Heo 	new->priomap_len = new_len;
785bc1421eSNeil Horman 
794a6ee25cSTejun Heo 	/* install the new priomap */
8052bca930STejun Heo 	rcu_assign_pointer(dev->priomap, new);
8152bca930STejun Heo 	if (old)
8252bca930STejun Heo 		kfree_rcu(old, rcu);
83ef209f15SGao feng 	return 0;
845bc1421eSNeil Horman }
855bc1421eSNeil Horman 
86666b0ebeSTejun Heo /**
87666b0ebeSTejun Heo  * netprio_prio - return the effective netprio of a cgroup-net_device pair
886d37b974STejun Heo  * @css: css part of the target pair
89666b0ebeSTejun Heo  * @dev: net_device part of the target pair
90666b0ebeSTejun Heo  *
91666b0ebeSTejun Heo  * Should be called under RCU read or rtnl lock.
92666b0ebeSTejun Heo  */
netprio_prio(struct cgroup_subsys_state * css,struct net_device * dev)936d37b974STejun Heo static u32 netprio_prio(struct cgroup_subsys_state *css, struct net_device *dev)
94666b0ebeSTejun Heo {
95666b0ebeSTejun Heo 	struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
96db53c73aSTejun Heo 	int id = css->id;
97666b0ebeSTejun Heo 
986d37b974STejun Heo 	if (map && id < map->priomap_len)
996d37b974STejun Heo 		return map->priomap[id];
100666b0ebeSTejun Heo 	return 0;
101666b0ebeSTejun Heo }
102666b0ebeSTejun Heo 
103666b0ebeSTejun Heo /**
104666b0ebeSTejun Heo  * netprio_set_prio - set netprio on a cgroup-net_device pair
1056d37b974STejun Heo  * @css: css part of the target pair
106666b0ebeSTejun Heo  * @dev: net_device part of the target pair
107666b0ebeSTejun Heo  * @prio: prio to set
108666b0ebeSTejun Heo  *
1096d37b974STejun Heo  * Set netprio to @prio on @css-@dev pair.  Should be called under rtnl
110666b0ebeSTejun Heo  * lock and may fail under memory pressure for non-zero @prio.
111666b0ebeSTejun Heo  */
netprio_set_prio(struct cgroup_subsys_state * css,struct net_device * dev,u32 prio)1126d37b974STejun Heo static int netprio_set_prio(struct cgroup_subsys_state *css,
1136d37b974STejun Heo 			    struct net_device *dev, u32 prio)
114666b0ebeSTejun Heo {
115666b0ebeSTejun Heo 	struct netprio_map *map;
116db53c73aSTejun Heo 	int id = css->id;
117666b0ebeSTejun Heo 	int ret;
118666b0ebeSTejun Heo 
119666b0ebeSTejun Heo 	/* avoid extending priomap for zero writes */
120666b0ebeSTejun Heo 	map = rtnl_dereference(dev->priomap);
1216d37b974STejun Heo 	if (!prio && (!map || map->priomap_len <= id))
122666b0ebeSTejun Heo 		return 0;
123666b0ebeSTejun Heo 
1246d37b974STejun Heo 	ret = extend_netdev_table(dev, id);
125666b0ebeSTejun Heo 	if (ret)
126666b0ebeSTejun Heo 		return ret;
127666b0ebeSTejun Heo 
128666b0ebeSTejun Heo 	map = rtnl_dereference(dev->priomap);
1296d37b974STejun Heo 	map->priomap[id] = prio;
130666b0ebeSTejun Heo 	return 0;
131666b0ebeSTejun Heo }
132666b0ebeSTejun Heo 
133eb95419bSTejun Heo static struct cgroup_subsys_state *
cgrp_css_alloc(struct cgroup_subsys_state * parent_css)134eb95419bSTejun Heo cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
1355bc1421eSNeil Horman {
1366d37b974STejun Heo 	struct cgroup_subsys_state *css;
13788d642faSTejun Heo 
1386d37b974STejun Heo 	css = kzalloc(sizeof(*css), GFP_KERNEL);
1396d37b974STejun Heo 	if (!css)
1405bc1421eSNeil Horman 		return ERR_PTR(-ENOMEM);
1415bc1421eSNeil Horman 
1426d37b974STejun Heo 	return css;
1435bc1421eSNeil Horman }
1445bc1421eSNeil Horman 
cgrp_css_online(struct cgroup_subsys_state * css)145eb95419bSTejun Heo static int cgrp_css_online(struct cgroup_subsys_state *css)
1465bc1421eSNeil Horman {
1475c9d535bSTejun Heo 	struct cgroup_subsys_state *parent_css = css->parent;
1485bc1421eSNeil Horman 	struct net_device *dev;
149811d8d6fSTejun Heo 	int ret = 0;
150811d8d6fSTejun Heo 
151297dbde1STejun Heo 	if (css->id > NETPRIO_ID_MAX)
152297dbde1STejun Heo 		return -ENOSPC;
153297dbde1STejun Heo 
154eb95419bSTejun Heo 	if (!parent_css)
155811d8d6fSTejun Heo 		return 0;
1565bc1421eSNeil Horman 
1575bc1421eSNeil Horman 	rtnl_lock();
158811d8d6fSTejun Heo 	/*
159811d8d6fSTejun Heo 	 * Inherit prios from the parent.  As all prios are set during
160811d8d6fSTejun Heo 	 * onlining, there is no need to clear them on offline.
161811d8d6fSTejun Heo 	 */
162811d8d6fSTejun Heo 	for_each_netdev(&init_net, dev) {
1636d37b974STejun Heo 		u32 prio = netprio_prio(parent_css, dev);
164811d8d6fSTejun Heo 
1656d37b974STejun Heo 		ret = netprio_set_prio(css, dev, prio);
166811d8d6fSTejun Heo 		if (ret)
167811d8d6fSTejun Heo 			break;
168811d8d6fSTejun Heo 	}
1695bc1421eSNeil Horman 	rtnl_unlock();
170811d8d6fSTejun Heo 	return ret;
171811d8d6fSTejun Heo }
172811d8d6fSTejun Heo 
cgrp_css_free(struct cgroup_subsys_state * css)173eb95419bSTejun Heo static void cgrp_css_free(struct cgroup_subsys_state *css)
174811d8d6fSTejun Heo {
175eb95419bSTejun Heo 	kfree(css);
1765bc1421eSNeil Horman }
1775bc1421eSNeil Horman 
read_prioidx(struct cgroup_subsys_state * css,struct cftype * cft)178182446d0STejun Heo static u64 read_prioidx(struct cgroup_subsys_state *css, struct cftype *cft)
1795bc1421eSNeil Horman {
180db53c73aSTejun Heo 	return css->id;
1815bc1421eSNeil Horman }
1825bc1421eSNeil Horman 
read_priomap(struct seq_file * sf,void * v)1832da8ca82STejun Heo static int read_priomap(struct seq_file *sf, void *v)
1845bc1421eSNeil Horman {
1855bc1421eSNeil Horman 	struct net_device *dev;
1865bc1421eSNeil Horman 
1875bc1421eSNeil Horman 	rcu_read_lock();
188666b0ebeSTejun Heo 	for_each_netdev_rcu(&init_net, dev)
1892da8ca82STejun Heo 		seq_printf(sf, "%s %u\n", dev->name,
1902da8ca82STejun Heo 			   netprio_prio(seq_css(sf), dev));
1915bc1421eSNeil Horman 	rcu_read_unlock();
1925bc1421eSNeil Horman 	return 0;
1935bc1421eSNeil Horman }
1945bc1421eSNeil Horman 
write_priomap(struct kernfs_open_file * of,char * buf,size_t nbytes,loff_t off)195451af504STejun Heo static ssize_t write_priomap(struct kernfs_open_file *of,
196451af504STejun Heo 			     char *buf, size_t nbytes, loff_t off)
1975bc1421eSNeil Horman {
1986d5759ddSTejun Heo 	char devname[IFNAMSIZ + 1];
1995bc1421eSNeil Horman 	struct net_device *dev;
2006d5759ddSTejun Heo 	u32 prio;
2016d5759ddSTejun Heo 	int ret;
2025bc1421eSNeil Horman 
203451af504STejun Heo 	if (sscanf(buf, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
2046d5759ddSTejun Heo 		return -EINVAL;
2055bc1421eSNeil Horman 
2065bc1421eSNeil Horman 	dev = dev_get_by_name(&init_net, devname);
2075bc1421eSNeil Horman 	if (!dev)
2086d5759ddSTejun Heo 		return -ENODEV;
2095bc1421eSNeil Horman 
210476ad154SJohn Fastabend 	rtnl_lock();
2116d5759ddSTejun Heo 
212451af504STejun Heo 	ret = netprio_set_prio(of_css(of), dev, prio);
213ef209f15SGao feng 
214476ad154SJohn Fastabend 	rtnl_unlock();
2155bc1421eSNeil Horman 	dev_put(dev);
216451af504STejun Heo 	return ret ?: nbytes;
2175bc1421eSNeil Horman }
2185bc1421eSNeil Horman 
update_netprio(const void * v,struct file * file,unsigned n)219c3c073f8SAl Viro static int update_netprio(const void *v, struct file *file, unsigned n)
220c3c073f8SAl Viro {
221dba4a925SFlorent Revest 	struct socket *sock = sock_from_file(file);
222*8520e224SDaniel Borkmann 
223*8520e224SDaniel Borkmann 	if (sock)
2242a56a1feSTejun Heo 		sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
2252a56a1feSTejun Heo 					(unsigned long)v);
226c3c073f8SAl Viro 	return 0;
227c3c073f8SAl Viro }
228c3c073f8SAl Viro 
net_prio_attach(struct cgroup_taskset * tset)2291f7dd3e5STejun Heo static void net_prio_attach(struct cgroup_taskset *tset)
230406a3c63SJohn Fastabend {
231406a3c63SJohn Fastabend 	struct task_struct *p;
2321f7dd3e5STejun Heo 	struct cgroup_subsys_state *css;
2331f7dd3e5STejun Heo 
2341f7dd3e5STejun Heo 	cgroup_taskset_for_each(p, css, tset) {
235db53c73aSTejun Heo 		void *v = (void *)(unsigned long)css->id;
236406a3c63SJohn Fastabend 
237406a3c63SJohn Fastabend 		task_lock(p);
238c3c073f8SAl Viro 		iterate_fd(p->files, 0, update_netprio, v);
239406a3c63SJohn Fastabend 		task_unlock(p);
240406a3c63SJohn Fastabend 	}
241406a3c63SJohn Fastabend }
242406a3c63SJohn Fastabend 
2435bc1421eSNeil Horman static struct cftype ss_files[] = {
2445bc1421eSNeil Horman 	{
2455bc1421eSNeil Horman 		.name = "prioidx",
2465bc1421eSNeil Horman 		.read_u64 = read_prioidx,
2475bc1421eSNeil Horman 	},
2485bc1421eSNeil Horman 	{
2495bc1421eSNeil Horman 		.name = "ifpriomap",
2502da8ca82STejun Heo 		.seq_show = read_priomap,
251451af504STejun Heo 		.write = write_priomap,
2525bc1421eSNeil Horman 	},
2534baf6e33STejun Heo 	{ }	/* terminate */
2545bc1421eSNeil Horman };
2555bc1421eSNeil Horman 
256073219e9STejun Heo struct cgroup_subsys net_prio_cgrp_subsys = {
25792fb9748STejun Heo 	.css_alloc	= cgrp_css_alloc,
258811d8d6fSTejun Heo 	.css_online	= cgrp_css_online,
25992fb9748STejun Heo 	.css_free	= cgrp_css_free,
260406a3c63SJohn Fastabend 	.attach		= net_prio_attach,
2615577964eSTejun Heo 	.legacy_cftypes	= ss_files,
262676f7c8fSTejun Heo };
2635bc1421eSNeil Horman 
netprio_device_event(struct notifier_block * unused,unsigned long event,void * ptr)2645bc1421eSNeil Horman static int netprio_device_event(struct notifier_block *unused,
2655bc1421eSNeil Horman 				unsigned long event, void *ptr)
2665bc1421eSNeil Horman {
267351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2685bc1421eSNeil Horman 	struct netprio_map *old;
2695bc1421eSNeil Horman 
2705bc1421eSNeil Horman 	/*
2715bc1421eSNeil Horman 	 * Note this is called with rtnl_lock held so we have update side
2725bc1421eSNeil Horman 	 * protection on our rcu assignments
2735bc1421eSNeil Horman 	 */
2745bc1421eSNeil Horman 
2755bc1421eSNeil Horman 	switch (event) {
2765bc1421eSNeil Horman 	case NETDEV_UNREGISTER:
2775bc1421eSNeil Horman 		old = rtnl_dereference(dev->priomap);
2782cfa5a04SEric Dumazet 		RCU_INIT_POINTER(dev->priomap, NULL);
2795bc1421eSNeil Horman 		if (old)
2805bc1421eSNeil Horman 			kfree_rcu(old, rcu);
2815bc1421eSNeil Horman 		break;
2825bc1421eSNeil Horman 	}
2835bc1421eSNeil Horman 	return NOTIFY_DONE;
2845bc1421eSNeil Horman }
2855bc1421eSNeil Horman 
2865bc1421eSNeil Horman static struct notifier_block netprio_device_notifier = {
2875bc1421eSNeil Horman 	.notifier_call = netprio_device_event
2885bc1421eSNeil Horman };
2895bc1421eSNeil Horman 
init_cgroup_netprio(void)2905bc1421eSNeil Horman static int __init init_cgroup_netprio(void)
2915bc1421eSNeil Horman {
2925bc1421eSNeil Horman 	register_netdevice_notifier(&netprio_device_notifier);
293af636337STejun Heo 	return 0;
2945bc1421eSNeil Horman }
295af636337STejun Heo subsys_initcall(init_cgroup_netprio);
296