tcp_memcontrol.c 7.2 KB

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