tcp_memcontrol.c 5.4 KB

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