2011-12-12 05:47:04 +08:00
|
|
|
#include <net/tcp.h>
|
|
|
|
#include <net/tcp_memcontrol.h>
|
|
|
|
#include <net/sock.h>
|
2011-12-12 05:47:05 +08:00
|
|
|
#include <net/ip.h>
|
|
|
|
#include <linux/nsproxy.h>
|
2011-12-12 05:47:04 +08:00
|
|
|
#include <linux/memcontrol.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
2012-04-10 06:36:33 +08:00
|
|
|
int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
|
2011-12-12 05:47:04 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The root cgroup does not use res_counters, but rather,
|
|
|
|
* rely on the data already collected by the network
|
|
|
|
* subsystem
|
|
|
|
*/
|
|
|
|
struct res_counter *res_parent = NULL;
|
|
|
|
struct cg_proto *cg_proto, *parent_cg;
|
|
|
|
struct mem_cgroup *parent = parent_mem_cgroup(memcg);
|
|
|
|
|
|
|
|
cg_proto = tcp_prot.proto_cgroup(memcg);
|
|
|
|
if (!cg_proto)
|
2012-04-02 03:09:55 +08:00
|
|
|
return 0;
|
2011-12-12 05:47:04 +08:00
|
|
|
|
2013-10-20 07:26:19 +08:00
|
|
|
cg_proto->sysctl_mem[0] = sysctl_tcp_mem[0];
|
|
|
|
cg_proto->sysctl_mem[1] = sysctl_tcp_mem[1];
|
|
|
|
cg_proto->sysctl_mem[2] = sysctl_tcp_mem[2];
|
|
|
|
cg_proto->memory_pressure = 0;
|
|
|
|
cg_proto->memcg = memcg;
|
2011-12-12 05:47:04 +08:00
|
|
|
|
|
|
|
parent_cg = tcp_prot.proto_cgroup(parent);
|
|
|
|
if (parent_cg)
|
2013-10-20 07:26:19 +08:00
|
|
|
res_parent = &parent_cg->memory_allocated;
|
2011-12-12 05:47:04 +08:00
|
|
|
|
2013-10-20 07:26:19 +08:00
|
|
|
res_counter_init(&cg_proto->memory_allocated, res_parent);
|
|
|
|
percpu_counter_init(&cg_proto->sockets_allocated, 0);
|
2011-12-12 05:47:04 +08:00
|
|
|
|
2012-04-02 03:09:55 +08:00
|
|
|
return 0;
|
2011-12-12 05:47:04 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tcp_init_cgroup);
|
|
|
|
|
2012-04-10 06:36:33 +08:00
|
|
|
void tcp_destroy_cgroup(struct mem_cgroup *memcg)
|
2011-12-12 05:47:04 +08:00
|
|
|
{
|
|
|
|
struct cg_proto *cg_proto;
|
|
|
|
|
|
|
|
cg_proto = tcp_prot.proto_cgroup(memcg);
|
|
|
|
if (!cg_proto)
|
|
|
|
return;
|
|
|
|
|
2013-10-20 07:26:19 +08:00
|
|
|
percpu_counter_destroy(&cg_proto->sockets_allocated);
|
2011-12-12 05:47:04 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tcp_destroy_cgroup);
|
2011-12-12 05:47:06 +08:00
|
|
|
|
|
|
|
static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
|
|
|
|
{
|
|
|
|
struct cg_proto *cg_proto;
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
cg_proto = tcp_prot.proto_cgroup(memcg);
|
|
|
|
if (!cg_proto)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2013-09-13 06:13:47 +08:00
|
|
|
if (val > RES_COUNTER_MAX)
|
|
|
|
val = RES_COUNTER_MAX;
|
2011-12-12 05:47:06 +08:00
|
|
|
|
2013-10-20 07:26:19 +08:00
|
|
|
ret = res_counter_set_limit(&cg_proto->memory_allocated, val);
|
2011-12-12 05:47:06 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
2013-10-20 07:26:19 +08:00
|
|
|
cg_proto->sysctl_mem[i] = min_t(long, val >> PAGE_SHIFT,
|
|
|
|
sysctl_tcp_mem[i]);
|
2011-12-12 05:47:06 +08:00
|
|
|
|
2013-09-13 06:13:47 +08:00
|
|
|
if (val == RES_COUNTER_MAX)
|
memcg: decrement static keys at real destroy time
We call the destroy function when a cgroup starts to be removed, such as
by a rmdir event.
However, because of our reference counters, some objects are still
inflight. Right now, we are decrementing the static_keys at destroy()
time, meaning that if we get rid of the last static_key reference, some
objects will still have charges, but the code to properly uncharge them
won't be run.
This becomes a problem specially if it is ever enabled again, because now
new charges will be added to the staled charges making keeping it pretty
much impossible.
We just need to be careful with the static branch activation: since there
is no particular preferred order of their activation, we need to make sure
that we only start using it after all call sites are active. This is
achieved by having a per-memcg flag that is only updated after
static_key_slow_inc() returns. At this time, we are sure all sites are
active.
This is made per-memcg, not global, for a reason: it also has the effect
of making socket accounting more consistent. The first memcg to be
limited will trigger static_key() activation, therefore, accounting. But
all the others will then be accounted no matter what. After this patch,
only limited memcgs will have its sockets accounted.
[akpm@linux-foundation.org: move enum sock_flag_bits into sock.h,
document enum sock_flag_bits,
convert memcg_proto_active() and memcg_proto_activated() to test_bit(),
redo tcp_update_limit() comment to 80 cols]
Signed-off-by: Glauber Costa <glommer@parallels.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Li Zefan <lizefan@huawei.com>
Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.cz>
Acked-by: David Miller <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-05-30 06:07:11 +08:00
|
|
|
clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
|
2013-09-13 06:13:47 +08:00
|
|
|
else if (val != RES_COUNTER_MAX) {
|
memcg: decrement static keys at real destroy time
We call the destroy function when a cgroup starts to be removed, such as
by a rmdir event.
However, because of our reference counters, some objects are still
inflight. Right now, we are decrementing the static_keys at destroy()
time, meaning that if we get rid of the last static_key reference, some
objects will still have charges, but the code to properly uncharge them
won't be run.
This becomes a problem specially if it is ever enabled again, because now
new charges will be added to the staled charges making keeping it pretty
much impossible.
We just need to be careful with the static branch activation: since there
is no particular preferred order of their activation, we need to make sure
that we only start using it after all call sites are active. This is
achieved by having a per-memcg flag that is only updated after
static_key_slow_inc() returns. At this time, we are sure all sites are
active.
This is made per-memcg, not global, for a reason: it also has the effect
of making socket accounting more consistent. The first memcg to be
limited will trigger static_key() activation, therefore, accounting. But
all the others will then be accounted no matter what. After this patch,
only limited memcgs will have its sockets accounted.
[akpm@linux-foundation.org: move enum sock_flag_bits into sock.h,
document enum sock_flag_bits,
convert memcg_proto_active() and memcg_proto_activated() to test_bit(),
redo tcp_update_limit() comment to 80 cols]
Signed-off-by: Glauber Costa <glommer@parallels.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Li Zefan <lizefan@huawei.com>
Acked-by: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@suse.cz>
Acked-by: David Miller <davem@davemloft.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-05-30 06:07:11 +08:00
|
|
|
/*
|
|
|
|
* The active bit needs to be written after the static_key
|
|
|
|
* update. This is what guarantees that the socket activation
|
|
|
|
* function is the last one to run. See sock_update_memcg() for
|
|
|
|
* details, and note that we don't mark any socket as belonging
|
|
|
|
* to this memcg until that flag is up.
|
|
|
|
*
|
|
|
|
* We need to do this, because static_keys will span multiple
|
|
|
|
* sites, but we can't control their order. If we mark a socket
|
|
|
|
* as accounted, but the accounting functions are not patched in
|
|
|
|
* yet, we'll lose accounting.
|
|
|
|
*
|
|
|
|
* We never race with the readers in sock_update_memcg(),
|
|
|
|
* because when this value change, the code to process it is not
|
|
|
|
* patched in yet.
|
|
|
|
*
|
|
|
|
* The activated bit is used to guarantee that no two writers
|
|
|
|
* will do the update in the same memcg. Without that, we can't
|
|
|
|
* properly shutdown the static key.
|
|
|
|
*/
|
|
|
|
if (!test_and_set_bit(MEMCG_SOCK_ACTIVATED, &cg_proto->flags))
|
|
|
|
static_key_slow_inc(&memcg_socket_limit_enabled);
|
|
|
|
set_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
|
|
|
|
}
|
2011-12-12 05:47:06 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-09 08:11:24 +08:00
|
|
|
static int tcp_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
|
2011-12-12 05:47:06 +08:00
|
|
|
const char *buffer)
|
|
|
|
{
|
2013-08-09 08:11:24 +08:00
|
|
|
struct mem_cgroup *memcg = mem_cgroup_from_css(css);
|
2011-12-12 05:47:06 +08:00
|
|
|
unsigned long long val;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
switch (cft->private) {
|
|
|
|
case RES_LIMIT:
|
|
|
|
/* see memcontrol.c */
|
|
|
|
ret = res_counter_memparse_write_strategy(buffer, &val);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
ret = tcp_update_limit(memcg, val);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
|
|
|
|
{
|
|
|
|
struct cg_proto *cg_proto;
|
|
|
|
|
|
|
|
cg_proto = tcp_prot.proto_cgroup(memcg);
|
|
|
|
if (!cg_proto)
|
|
|
|
return default_val;
|
|
|
|
|
2013-10-20 07:26:19 +08:00
|
|
|
return res_counter_read_u64(&cg_proto->memory_allocated, type);
|
2011-12-12 05:47:06 +08:00
|
|
|
}
|
|
|
|
|
2011-12-12 05:47:07 +08:00
|
|
|
static u64 tcp_read_usage(struct mem_cgroup *memcg)
|
|
|
|
{
|
|
|
|
struct cg_proto *cg_proto;
|
|
|
|
|
|
|
|
cg_proto = tcp_prot.proto_cgroup(memcg);
|
|
|
|
if (!cg_proto)
|
|
|
|
return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
|
|
|
|
|
2013-10-20 07:26:19 +08:00
|
|
|
return res_counter_read_u64(&cg_proto->memory_allocated, RES_USAGE);
|
2011-12-12 05:47:07 +08:00
|
|
|
}
|
|
|
|
|
2013-08-09 08:11:24 +08:00
|
|
|
static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
|
2011-12-12 05:47:06 +08:00
|
|
|
{
|
2013-08-09 08:11:24 +08:00
|
|
|
struct mem_cgroup *memcg = mem_cgroup_from_css(css);
|
2011-12-12 05:47:06 +08:00
|
|
|
u64 val;
|
|
|
|
|
|
|
|
switch (cft->private) {
|
|
|
|
case RES_LIMIT:
|
2013-09-13 06:13:47 +08:00
|
|
|
val = tcp_read_stat(memcg, RES_LIMIT, RES_COUNTER_MAX);
|
2011-12-12 05:47:06 +08:00
|
|
|
break;
|
2011-12-12 05:47:07 +08:00
|
|
|
case RES_USAGE:
|
|
|
|
val = tcp_read_usage(memcg);
|
|
|
|
break;
|
2011-12-12 05:47:08 +08:00
|
|
|
case RES_FAILCNT:
|
2011-12-12 05:47:09 +08:00
|
|
|
case RES_MAX_USAGE:
|
|
|
|
val = tcp_read_stat(memcg, cft->private, 0);
|
2011-12-12 05:47:08 +08:00
|
|
|
break;
|
2011-12-12 05:47:06 +08:00
|
|
|
default:
|
|
|
|
BUG();
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2013-08-09 08:11:24 +08:00
|
|
|
static int tcp_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
|
2011-12-12 05:47:08 +08:00
|
|
|
{
|
|
|
|
struct mem_cgroup *memcg;
|
|
|
|
struct cg_proto *cg_proto;
|
|
|
|
|
2013-08-09 08:11:24 +08:00
|
|
|
memcg = mem_cgroup_from_css(css);
|
2011-12-12 05:47:08 +08:00
|
|
|
cg_proto = tcp_prot.proto_cgroup(memcg);
|
|
|
|
if (!cg_proto)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (event) {
|
2011-12-12 05:47:09 +08:00
|
|
|
case RES_MAX_USAGE:
|
2013-10-20 07:26:19 +08:00
|
|
|
res_counter_reset_max(&cg_proto->memory_allocated);
|
2011-12-12 05:47:09 +08:00
|
|
|
break;
|
2011-12-12 05:47:08 +08:00
|
|
|
case RES_FAILCNT:
|
2013-10-20 07:26:19 +08:00
|
|
|
res_counter_reset_failcnt(&cg_proto->memory_allocated);
|
2011-12-12 05:47:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-02 03:09:55 +08:00
|
|
|
static struct cftype tcp_files[] = {
|
|
|
|
{
|
|
|
|
.name = "kmem.tcp.limit_in_bytes",
|
|
|
|
.write_string = tcp_cgroup_write,
|
|
|
|
.read_u64 = tcp_cgroup_read,
|
|
|
|
.private = RES_LIMIT,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "kmem.tcp.usage_in_bytes",
|
|
|
|
.read_u64 = tcp_cgroup_read,
|
|
|
|
.private = RES_USAGE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "kmem.tcp.failcnt",
|
|
|
|
.private = RES_FAILCNT,
|
|
|
|
.trigger = tcp_cgroup_reset,
|
|
|
|
.read_u64 = tcp_cgroup_read,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "kmem.tcp.max_usage_in_bytes",
|
|
|
|
.private = RES_MAX_USAGE,
|
|
|
|
.trigger = tcp_cgroup_reset,
|
|
|
|
.read_u64 = tcp_cgroup_read,
|
|
|
|
},
|
2012-04-02 03:09:55 +08:00
|
|
|
{ } /* terminate */
|
2012-04-02 03:09:55 +08:00
|
|
|
};
|
2012-04-02 03:09:55 +08:00
|
|
|
|
|
|
|
static int __init tcp_memcontrol_init(void)
|
|
|
|
{
|
|
|
|
WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, tcp_files));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
__initcall(tcp_memcontrol_init);
|