tcp_memcontrol.c 7.1 KB

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