tcp_memcontrol.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. static inline struct tcp_memcontrol *tcp_from_cgproto(struct cg_proto *cg_proto)
  9. {
  10. return container_of(cg_proto, struct tcp_memcontrol, cg_proto);
  11. }
  12. static void memcg_tcp_enter_memory_pressure(struct sock *sk)
  13. {
  14. if (sk->sk_cgrp->memory_pressure)
  15. *sk->sk_cgrp->memory_pressure = 1;
  16. }
  17. EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
  18. int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
  19. {
  20. /*
  21. * The root cgroup does not use res_counters, but rather,
  22. * rely on the data already collected by the network
  23. * subsystem
  24. */
  25. struct res_counter *res_parent = NULL;
  26. struct cg_proto *cg_proto, *parent_cg;
  27. struct tcp_memcontrol *tcp;
  28. struct mem_cgroup *parent = parent_mem_cgroup(memcg);
  29. cg_proto = tcp_prot.proto_cgroup(memcg);
  30. if (!cg_proto)
  31. return 0;
  32. tcp = tcp_from_cgproto(cg_proto);
  33. tcp->tcp_prot_mem[0] = sysctl_tcp_mem[0];
  34. tcp->tcp_prot_mem[1] = sysctl_tcp_mem[1];
  35. tcp->tcp_prot_mem[2] = sysctl_tcp_mem[2];
  36. tcp->tcp_memory_pressure = 0;
  37. parent_cg = tcp_prot.proto_cgroup(parent);
  38. if (parent_cg)
  39. res_parent = parent_cg->memory_allocated;
  40. res_counter_init(&tcp->tcp_memory_allocated, res_parent);
  41. percpu_counter_init(&tcp->tcp_sockets_allocated, 0);
  42. cg_proto->enter_memory_pressure = memcg_tcp_enter_memory_pressure;
  43. cg_proto->memory_pressure = &tcp->tcp_memory_pressure;
  44. cg_proto->sysctl_mem = tcp->tcp_prot_mem;
  45. cg_proto->memory_allocated = &tcp->tcp_memory_allocated;
  46. cg_proto->sockets_allocated = &tcp->tcp_sockets_allocated;
  47. cg_proto->memcg = memcg;
  48. return 0;
  49. }
  50. EXPORT_SYMBOL(tcp_init_cgroup);
  51. void tcp_destroy_cgroup(struct mem_cgroup *memcg)
  52. {
  53. struct cg_proto *cg_proto;
  54. struct tcp_memcontrol *tcp;
  55. cg_proto = tcp_prot.proto_cgroup(memcg);
  56. if (!cg_proto)
  57. return;
  58. tcp = tcp_from_cgproto(cg_proto);
  59. percpu_counter_destroy(&tcp->tcp_sockets_allocated);
  60. }
  61. EXPORT_SYMBOL(tcp_destroy_cgroup);
  62. static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
  63. {
  64. struct tcp_memcontrol *tcp;
  65. struct cg_proto *cg_proto;
  66. u64 old_lim;
  67. int i;
  68. int ret;
  69. cg_proto = tcp_prot.proto_cgroup(memcg);
  70. if (!cg_proto)
  71. return -EINVAL;
  72. if (val > RES_COUNTER_MAX)
  73. val = RES_COUNTER_MAX;
  74. tcp = tcp_from_cgproto(cg_proto);
  75. old_lim = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
  76. ret = res_counter_set_limit(&tcp->tcp_memory_allocated, val);
  77. if (ret)
  78. return ret;
  79. for (i = 0; i < 3; i++)
  80. tcp->tcp_prot_mem[i] = min_t(long, val >> PAGE_SHIFT,
  81. sysctl_tcp_mem[i]);
  82. if (val == RES_COUNTER_MAX)
  83. clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
  84. else if (val != RES_COUNTER_MAX) {
  85. /*
  86. * The active bit needs to be written after the static_key
  87. * update. This is what guarantees that the socket activation
  88. * function is the last one to run. See sock_update_memcg() for
  89. * details, and note that we don't mark any socket as belonging
  90. * to this memcg until that flag is up.
  91. *
  92. * We need to do this, because static_keys will span multiple
  93. * sites, but we can't control their order. If we mark a socket
  94. * as accounted, but the accounting functions are not patched in
  95. * yet, we'll lose accounting.
  96. *
  97. * We never race with the readers in sock_update_memcg(),
  98. * because when this value change, the code to process it is not
  99. * patched in yet.
  100. *
  101. * The activated bit is used to guarantee that no two writers
  102. * will do the update in the same memcg. Without that, we can't
  103. * properly shutdown the static key.
  104. */
  105. if (!test_and_set_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags))
  106. static_key_slow_inc(&memcg_socket_limit_enabled);
  107. set_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
  108. }
  109. return 0;
  110. }
  111. static int tcp_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
  112. const char *buffer)
  113. {
  114. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  115. unsigned long long val;
  116. int ret = 0;
  117. switch (cft->private) {
  118. case RES_LIMIT:
  119. /* see memcontrol.c */
  120. ret = res_counter_memparse_write_strategy(buffer, &val);
  121. if (ret)
  122. break;
  123. ret = tcp_update_limit(memcg, val);
  124. break;
  125. default:
  126. ret = -EINVAL;
  127. break;
  128. }
  129. return ret;
  130. }
  131. static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
  132. {
  133. struct tcp_memcontrol *tcp;
  134. struct cg_proto *cg_proto;
  135. cg_proto = tcp_prot.proto_cgroup(memcg);
  136. if (!cg_proto)
  137. return default_val;
  138. tcp = tcp_from_cgproto(cg_proto);
  139. return res_counter_read_u64(&tcp->tcp_memory_allocated, type);
  140. }
  141. static u64 tcp_read_usage(struct mem_cgroup *memcg)
  142. {
  143. struct tcp_memcontrol *tcp;
  144. struct cg_proto *cg_proto;
  145. cg_proto = tcp_prot.proto_cgroup(memcg);
  146. if (!cg_proto)
  147. return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
  148. tcp = tcp_from_cgproto(cg_proto);
  149. return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
  150. }
  151. static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
  152. {
  153. struct mem_cgroup *memcg = mem_cgroup_from_css(css);
  154. u64 val;
  155. switch (cft->private) {
  156. case RES_LIMIT:
  157. val = tcp_read_stat(memcg, RES_LIMIT, RES_COUNTER_MAX);
  158. break;
  159. case RES_USAGE:
  160. val = tcp_read_usage(memcg);
  161. break;
  162. case RES_FAILCNT:
  163. case RES_MAX_USAGE:
  164. val = tcp_read_stat(memcg, cft->private, 0);
  165. break;
  166. default:
  167. BUG();
  168. }
  169. return val;
  170. }
  171. static int tcp_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
  172. {
  173. struct mem_cgroup *memcg;
  174. struct tcp_memcontrol *tcp;
  175. struct cg_proto *cg_proto;
  176. memcg = mem_cgroup_from_css(css);
  177. cg_proto = tcp_prot.proto_cgroup(memcg);
  178. if (!cg_proto)
  179. return 0;
  180. tcp = tcp_from_cgproto(cg_proto);
  181. switch (event) {
  182. case RES_MAX_USAGE:
  183. res_counter_reset_max(&tcp->tcp_memory_allocated);
  184. break;
  185. case RES_FAILCNT:
  186. res_counter_reset_failcnt(&tcp->tcp_memory_allocated);
  187. break;
  188. }
  189. return 0;
  190. }
  191. static struct cftype tcp_files[] = {
  192. {
  193. .name = "kmem.tcp.limit_in_bytes",
  194. .write_string = tcp_cgroup_write,
  195. .read_u64 = tcp_cgroup_read,
  196. .private = RES_LIMIT,
  197. },
  198. {
  199. .name = "kmem.tcp.usage_in_bytes",
  200. .read_u64 = tcp_cgroup_read,
  201. .private = RES_USAGE,
  202. },
  203. {
  204. .name = "kmem.tcp.failcnt",
  205. .private = RES_FAILCNT,
  206. .trigger = tcp_cgroup_reset,
  207. .read_u64 = tcp_cgroup_read,
  208. },
  209. {
  210. .name = "kmem.tcp.max_usage_in_bytes",
  211. .private = RES_MAX_USAGE,
  212. .trigger = tcp_cgroup_reset,
  213. .read_u64 = tcp_cgroup_read,
  214. },
  215. { } /* terminate */
  216. };
  217. static int __init tcp_memcontrol_init(void)
  218. {
  219. WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, tcp_files));
  220. return 0;
  221. }
  222. __initcall(tcp_memcontrol_init);