netclassid_cgroup.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * net/core/netclassid_cgroup.c Classid Cgroupfs Handling
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/cgroup.h>
  13. #include <linux/fdtable.h>
  14. #include <linux/sched/task.h>
  15. #include <net/cls_cgroup.h>
  16. #include <net/sock.h>
  17. static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css)
  18. {
  19. return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
  20. }
  21. struct cgroup_cls_state *task_cls_state(struct task_struct *p)
  22. {
  23. return css_cls_state(task_css_check(p, net_cls_cgrp_id,
  24. rcu_read_lock_bh_held()));
  25. }
  26. EXPORT_SYMBOL_GPL(task_cls_state);
  27. static struct cgroup_subsys_state *
  28. cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  29. {
  30. struct cgroup_cls_state *cs;
  31. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  32. if (!cs)
  33. return ERR_PTR(-ENOMEM);
  34. return &cs->css;
  35. }
  36. static int cgrp_css_online(struct cgroup_subsys_state *css)
  37. {
  38. struct cgroup_cls_state *cs = css_cls_state(css);
  39. struct cgroup_cls_state *parent = css_cls_state(css->parent);
  40. if (parent)
  41. cs->classid = parent->classid;
  42. return 0;
  43. }
  44. static void cgrp_css_free(struct cgroup_subsys_state *css)
  45. {
  46. kfree(css_cls_state(css));
  47. }
  48. /*
  49. * To avoid freezing of sockets creation for tasks with big number of threads
  50. * and opened sockets lets release file_lock every 1000 iterated descriptors.
  51. * New sockets will already have been created with new classid.
  52. */
  53. struct update_classid_context {
  54. u32 classid;
  55. unsigned int batch;
  56. };
  57. #define UPDATE_CLASSID_BATCH 1000
  58. static int update_classid_sock(const void *v, struct file *file, unsigned n)
  59. {
  60. int err;
  61. struct update_classid_context *ctx = (void *)v;
  62. struct socket *sock = sock_from_file(file, &err);
  63. if (sock) {
  64. spin_lock(&cgroup_sk_update_lock);
  65. sock_cgroup_set_classid(&sock->sk->sk_cgrp_data, ctx->classid);
  66. spin_unlock(&cgroup_sk_update_lock);
  67. }
  68. if (--ctx->batch == 0) {
  69. ctx->batch = UPDATE_CLASSID_BATCH;
  70. return n + 1;
  71. }
  72. return 0;
  73. }
  74. static void update_classid_task(struct task_struct *p, u32 classid)
  75. {
  76. struct update_classid_context ctx = {
  77. .classid = classid,
  78. .batch = UPDATE_CLASSID_BATCH
  79. };
  80. unsigned int fd = 0;
  81. do {
  82. task_lock(p);
  83. fd = iterate_fd(p->files, fd, update_classid_sock, &ctx);
  84. task_unlock(p);
  85. cond_resched();
  86. } while (fd);
  87. }
  88. static void cgrp_attach(struct cgroup_taskset *tset)
  89. {
  90. struct cgroup_subsys_state *css;
  91. struct task_struct *p;
  92. cgroup_taskset_for_each(p, css, tset) {
  93. update_classid_task(p, css_cls_state(css)->classid);
  94. }
  95. }
  96. static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft)
  97. {
  98. return css_cls_state(css)->classid;
  99. }
  100. static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
  101. u64 value)
  102. {
  103. struct cgroup_cls_state *cs = css_cls_state(css);
  104. struct css_task_iter it;
  105. struct task_struct *p;
  106. cgroup_sk_alloc_disable();
  107. cs->classid = (u32)value;
  108. css_task_iter_start(css, 0, &it);
  109. while ((p = css_task_iter_next(&it))) {
  110. update_classid_task(p, cs->classid);
  111. cond_resched();
  112. }
  113. css_task_iter_end(&it);
  114. return 0;
  115. }
  116. static struct cftype ss_files[] = {
  117. {
  118. .name = "classid",
  119. .read_u64 = read_classid,
  120. .write_u64 = write_classid,
  121. },
  122. { } /* terminate */
  123. };
  124. struct cgroup_subsys net_cls_cgrp_subsys = {
  125. .css_alloc = cgrp_css_alloc,
  126. .css_online = cgrp_css_online,
  127. .css_free = cgrp_css_free,
  128. .attach = cgrp_attach,
  129. .legacy_cftypes = ss_files,
  130. };