netprio_cgroup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/core/netprio_cgroup.c Priority Control Group
  4. *
  5. * Authors: Neil Horman <nhorman@tuxdriver.com>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/cgroup.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/atomic.h>
  17. #include <linux/sched/task.h>
  18. #include <net/rtnetlink.h>
  19. #include <net/pkt_cls.h>
  20. #include <net/sock.h>
  21. #include <net/netprio_cgroup.h>
  22. #include <linux/fdtable.h>
  23. /*
  24. * netprio allocates per-net_device priomap array which is indexed by
  25. * css->id. Limiting css ID to 16bits doesn't lose anything.
  26. */
  27. #define NETPRIO_ID_MAX USHRT_MAX
  28. #define PRIOMAP_MIN_SZ 128
  29. /*
  30. * Extend @dev->priomap so that it's large enough to accommodate
  31. * @target_idx. @dev->priomap.priomap_len > @target_idx after successful
  32. * return. Must be called under rtnl lock.
  33. */
  34. static int extend_netdev_table(struct net_device *dev, u32 target_idx)
  35. {
  36. struct netprio_map *old, *new;
  37. size_t new_sz, new_len;
  38. /* is the existing priomap large enough? */
  39. old = rtnl_dereference(dev->priomap);
  40. if (old && old->priomap_len > target_idx)
  41. return 0;
  42. /*
  43. * Determine the new size. Let's keep it power-of-two. We start
  44. * from PRIOMAP_MIN_SZ and double it until it's large enough to
  45. * accommodate @target_idx.
  46. */
  47. new_sz = PRIOMAP_MIN_SZ;
  48. while (true) {
  49. new_len = (new_sz - offsetof(struct netprio_map, priomap)) /
  50. sizeof(new->priomap[0]);
  51. if (new_len > target_idx)
  52. break;
  53. new_sz *= 2;
  54. /* overflowed? */
  55. if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
  56. return -ENOSPC;
  57. }
  58. /* allocate & copy */
  59. new = kzalloc(new_sz, GFP_KERNEL);
  60. if (!new)
  61. return -ENOMEM;
  62. if (old)
  63. memcpy(new->priomap, old->priomap,
  64. old->priomap_len * sizeof(old->priomap[0]));
  65. new->priomap_len = new_len;
  66. /* install the new priomap */
  67. rcu_assign_pointer(dev->priomap, new);
  68. if (old)
  69. kfree_rcu(old, rcu);
  70. return 0;
  71. }
  72. /**
  73. * netprio_prio - return the effective netprio of a cgroup-net_device pair
  74. * @css: css part of the target pair
  75. * @dev: net_device part of the target pair
  76. *
  77. * Should be called under RCU read or rtnl lock.
  78. */
  79. static u32 netprio_prio(struct cgroup_subsys_state *css, struct net_device *dev)
  80. {
  81. struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
  82. int id = css->id;
  83. if (map && id < map->priomap_len)
  84. return map->priomap[id];
  85. return 0;
  86. }
  87. /**
  88. * netprio_set_prio - set netprio on a cgroup-net_device pair
  89. * @css: css part of the target pair
  90. * @dev: net_device part of the target pair
  91. * @prio: prio to set
  92. *
  93. * Set netprio to @prio on @css-@dev pair. Should be called under rtnl
  94. * lock and may fail under memory pressure for non-zero @prio.
  95. */
  96. static int netprio_set_prio(struct cgroup_subsys_state *css,
  97. struct net_device *dev, u32 prio)
  98. {
  99. struct netprio_map *map;
  100. int id = css->id;
  101. int ret;
  102. /* avoid extending priomap for zero writes */
  103. map = rtnl_dereference(dev->priomap);
  104. if (!prio && (!map || map->priomap_len <= id))
  105. return 0;
  106. ret = extend_netdev_table(dev, id);
  107. if (ret)
  108. return ret;
  109. map = rtnl_dereference(dev->priomap);
  110. map->priomap[id] = prio;
  111. return 0;
  112. }
  113. static struct cgroup_subsys_state *
  114. cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  115. {
  116. struct cgroup_subsys_state *css;
  117. css = kzalloc(sizeof(*css), GFP_KERNEL);
  118. if (!css)
  119. return ERR_PTR(-ENOMEM);
  120. return css;
  121. }
  122. static int cgrp_css_online(struct cgroup_subsys_state *css)
  123. {
  124. struct cgroup_subsys_state *parent_css = css->parent;
  125. struct net_device *dev;
  126. int ret = 0;
  127. if (css->id > NETPRIO_ID_MAX)
  128. return -ENOSPC;
  129. if (!parent_css)
  130. return 0;
  131. rtnl_lock();
  132. /*
  133. * Inherit prios from the parent. As all prios are set during
  134. * onlining, there is no need to clear them on offline.
  135. */
  136. for_each_netdev(&init_net, dev) {
  137. u32 prio = netprio_prio(parent_css, dev);
  138. ret = netprio_set_prio(css, dev, prio);
  139. if (ret)
  140. break;
  141. }
  142. rtnl_unlock();
  143. return ret;
  144. }
  145. static void cgrp_css_free(struct cgroup_subsys_state *css)
  146. {
  147. kfree(css);
  148. }
  149. static u64 read_prioidx(struct cgroup_subsys_state *css, struct cftype *cft)
  150. {
  151. return css->id;
  152. }
  153. static int read_priomap(struct seq_file *sf, void *v)
  154. {
  155. struct net_device *dev;
  156. rcu_read_lock();
  157. for_each_netdev_rcu(&init_net, dev)
  158. seq_printf(sf, "%s %u\n", dev->name,
  159. netprio_prio(seq_css(sf), dev));
  160. rcu_read_unlock();
  161. return 0;
  162. }
  163. static ssize_t write_priomap(struct kernfs_open_file *of,
  164. char *buf, size_t nbytes, loff_t off)
  165. {
  166. char devname[IFNAMSIZ + 1];
  167. struct net_device *dev;
  168. u32 prio;
  169. int ret;
  170. if (sscanf(buf, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  171. return -EINVAL;
  172. dev = dev_get_by_name(&init_net, devname);
  173. if (!dev)
  174. return -ENODEV;
  175. rtnl_lock();
  176. ret = netprio_set_prio(of_css(of), dev, prio);
  177. rtnl_unlock();
  178. dev_put(dev);
  179. return ret ?: nbytes;
  180. }
  181. static int update_netprio(const void *v, struct file *file, unsigned n)
  182. {
  183. struct socket *sock = sock_from_file(file);
  184. if (sock)
  185. sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
  186. (unsigned long)v);
  187. return 0;
  188. }
  189. static void net_prio_attach(struct cgroup_taskset *tset)
  190. {
  191. struct task_struct *p;
  192. struct cgroup_subsys_state *css;
  193. cgroup_taskset_for_each(p, css, tset) {
  194. void *v = (void *)(unsigned long)css->id;
  195. task_lock(p);
  196. iterate_fd(p->files, 0, update_netprio, v);
  197. task_unlock(p);
  198. }
  199. }
  200. static struct cftype ss_files[] = {
  201. {
  202. .name = "prioidx",
  203. .read_u64 = read_prioidx,
  204. },
  205. {
  206. .name = "ifpriomap",
  207. .seq_show = read_priomap,
  208. .write = write_priomap,
  209. },
  210. { } /* terminate */
  211. };
  212. struct cgroup_subsys net_prio_cgrp_subsys = {
  213. .css_alloc = cgrp_css_alloc,
  214. .css_online = cgrp_css_online,
  215. .css_free = cgrp_css_free,
  216. .attach = net_prio_attach,
  217. .legacy_cftypes = ss_files,
  218. };
  219. static int netprio_device_event(struct notifier_block *unused,
  220. unsigned long event, void *ptr)
  221. {
  222. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  223. struct netprio_map *old;
  224. /*
  225. * Note this is called with rtnl_lock held so we have update side
  226. * protection on our rcu assignments
  227. */
  228. switch (event) {
  229. case NETDEV_UNREGISTER:
  230. old = rtnl_dereference(dev->priomap);
  231. RCU_INIT_POINTER(dev->priomap, NULL);
  232. if (old)
  233. kfree_rcu(old, rcu);
  234. break;
  235. }
  236. return NOTIFY_DONE;
  237. }
  238. static struct notifier_block netprio_device_notifier = {
  239. .notifier_call = netprio_device_event
  240. };
  241. static int __init init_cgroup_netprio(void)
  242. {
  243. register_netdevice_notifier(&netprio_device_notifier);
  244. return 0;
  245. }
  246. subsys_initcall(init_cgroup_netprio);