tcp_memcontrol.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <net/tcp.h>
  2. #include <net/tcp_memcontrol.h>
  3. #include <net/sock.h>
  4. #include <net/ip.h>
  5. #include <linux/nsproxy.h>
  6. #include <linux/memcontrol.h>
  7. #include <linux/module.h>
  8. int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
  9. {
  10. /*
  11. * The root cgroup does not use page_counters, but rather,
  12. * rely on the data already collected by the network
  13. * subsystem
  14. */
  15. struct mem_cgroup *parent = parent_mem_cgroup(memcg);
  16. struct page_counter *counter_parent = NULL;
  17. struct cg_proto *cg_proto, *parent_cg;
  18. cg_proto = tcp_prot.proto_cgroup(memcg);
  19. if (!cg_proto)
  20. return 0;
  21. cg_proto->sysctl_mem[0] = sysctl_tcp_mem[0];
  22. cg_proto->sysctl_mem[1] = sysctl_tcp_mem[1];
  23. cg_proto->sysctl_mem[2] = sysctl_tcp_mem[2];
  24. cg_proto->memory_pressure = 0;
  25. cg_proto->memcg = memcg;
  26. parent_cg = tcp_prot.proto_cgroup(parent);
  27. if (parent_cg)
  28. counter_parent = &parent_cg->memory_allocated;
  29. page_counter_init(&cg_proto->memory_allocated, counter_parent);
  30. percpu_counter_init(&cg_proto->sockets_allocated, 0, GFP_KERNEL);
  31. return 0;
  32. }
  33. EXPORT_SYMBOL(tcp_init_cgroup);
  34. void tcp_destroy_cgroup(struct mem_cgroup *memcg)
  35. {
  36. struct cg_proto *cg_proto;
  37. cg_proto = tcp_prot.proto_cgroup(memcg);
  38. if (!cg_proto)
  39. return;
  40. percpu_counter_destroy(&cg_proto->sockets_allocated);
  41. if (test_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags))
  42. static_key_slow_dec(&memcg_socket_limit_enabled);
  43. }
  44. EXPORT_SYMBOL(tcp_destroy_cgroup);
  45. static int tcp_update_limit(struct mem_cgroup *memcg, unsigned long nr_pages)
  46. {
  47. struct cg_proto *cg_proto;
  48. int i;
  49. int ret;
  50. cg_proto = tcp_prot.proto_cgroup(memcg);
  51. if (!cg_proto)
  52. return -EINVAL;
  53. ret = page_counter_limit(&cg_proto->memory_allocated, nr_pages);
  54. if (ret)
  55. return ret;
  56. for (i = 0; i < 3; i++)
  57. cg_proto->sysctl_mem[i] = min_t(long, nr_pages,
  58. sysctl_tcp_mem[i]);
  59. if (nr_pages == PAGE_COUNTER_MAX)
  60. clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
  61. else {
  62. /*
  63. * The active bit needs to be written after the static_key
  64. * update. This is what guarantees that the socket activation
  65. * function is the last one to run. See sock_update_memcg() for
  66. * details, and note that we don't mark any socket as belonging
  67. * to this memcg until that flag is up.
  68. *
  69. * We need to do this, because static_keys will span multiple
  70. * sites, but we can't control their order. If we mark a socket
  71. * as accounted, but the accounting functions are not patched in
  72. * yet, we'll lose accounting.
  73. *
  74. * We never race with the readers in sock_update_memcg(),
  75. * because when this value change, the code to process it is not
  76. * patched in yet.
  77. *
  78. * The activated bit is used to guarantee that no two writers
  79. * will do the update in the same memcg. Without that, we can't
  80. * properly shutdown the static key.
  81. */
  82. if (!test_and_set_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags))
  83. static_key_slow_inc(&memcg_socket_limit_enabled);
  84. set_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
  85. }
  86. return 0;
  87. }
  88. enum {
  89. RES_USAGE,
  90. RES_LIMIT,
  91. RES_MAX_USAGE,
  92. RES_FAILCNT,
  93. };
  94. static DEFINE_MUTEX(tcp_limit_mutex);
  95. static ssize_t tcp_cgroup_write(struct kernfs_open_file *of,
  96. char *buf, size_t nbytes, loff_t off)
  97. {
  98. struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
  99. unsigned long nr_pages;
  100. int ret = 0;
  101. buf = strstrip(buf);
  102. switch (of_cft(of)->private) {
  103. case RES_LIMIT:
  104. /* see memcontrol.c */
  105. ret = page_counter_memparse(buf, "-1", &nr_pages);
  106. if (ret)
  107. break;
  108. mutex_lock(&tcp_limit_mutex);
  109. ret = tcp_update_limit(memcg, nr_pages);
  110. mutex_unlock(&tcp_limit_mutex);
  111. break;
  112. default:
  113. ret = -EINVAL;
  114. break;
  115. }
  116. return ret ?: nbytes;
  117. }
  118. static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
  119. {
  120. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  121. struct cg_proto *cg_proto = tcp_prot.proto_cgroup(memcg);
  122. u64 val;
  123. switch (cft->private) {
  124. case RES_LIMIT:
  125. if (!cg_proto)
  126. return PAGE_COUNTER_MAX;
  127. val = cg_proto->memory_allocated.limit;
  128. val *= PAGE_SIZE;
  129. break;
  130. case RES_USAGE:
  131. if (!cg_proto)
  132. val = atomic_long_read(&tcp_memory_allocated);
  133. else
  134. val = page_counter_read(&cg_proto->memory_allocated);
  135. val *= PAGE_SIZE;
  136. break;
  137. case RES_FAILCNT:
  138. if (!cg_proto)
  139. return 0;
  140. val = cg_proto->memory_allocated.failcnt;
  141. break;
  142. case RES_MAX_USAGE:
  143. if (!cg_proto)
  144. return 0;
  145. val = cg_proto->memory_allocated.watermark;
  146. val *= PAGE_SIZE;
  147. break;
  148. default:
  149. BUG();
  150. }
  151. return val;
  152. }
  153. static ssize_t tcp_cgroup_reset(struct kernfs_open_file *of,
  154. char *buf, size_t nbytes, loff_t off)
  155. {
  156. struct mem_cgroup *memcg;
  157. struct cg_proto *cg_proto;
  158. memcg = mem_cgroup_from_css(of_css(of));
  159. cg_proto = tcp_prot.proto_cgroup(memcg);
  160. if (!cg_proto)
  161. return nbytes;
  162. switch (of_cft(of)->private) {
  163. case RES_MAX_USAGE:
  164. page_counter_reset_watermark(&cg_proto->memory_allocated);
  165. break;
  166. case RES_FAILCNT:
  167. cg_proto->memory_allocated.failcnt = 0;
  168. break;
  169. }
  170. return nbytes;
  171. }
  172. static struct cftype tcp_files[] = {
  173. {
  174. .name = "kmem.tcp.limit_in_bytes",
  175. .write = tcp_cgroup_write,
  176. .read_u64 = tcp_cgroup_read,
  177. .private = RES_LIMIT,
  178. },
  179. {
  180. .name = "kmem.tcp.usage_in_bytes",
  181. .read_u64 = tcp_cgroup_read,
  182. .private = RES_USAGE,
  183. },
  184. {
  185. .name = "kmem.tcp.failcnt",
  186. .private = RES_FAILCNT,
  187. .write = tcp_cgroup_reset,
  188. .read_u64 = tcp_cgroup_read,
  189. },
  190. {
  191. .name = "kmem.tcp.max_usage_in_bytes",
  192. .private = RES_MAX_USAGE,
  193. .write = tcp_cgroup_reset,
  194. .read_u64 = tcp_cgroup_read,
  195. },
  196. { } /* terminate */
  197. };
  198. static int __init tcp_memcontrol_init(void)
  199. {
  200. WARN_ON(cgroup_add_legacy_cftypes(&memory_cgrp_subsys, tcp_files));
  201. return 0;
  202. }
  203. __initcall(tcp_memcontrol_init);