Skip to contentSkip to conversation

Fix problem with static_key decrement

PATCH Superseded Glauber Costa 5 participants 15 messages Apr 19, 2012 – Apr 23, 2012 9 messages not in the archive

Subsystems (9)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix problem with static_key decrement

Hi,

This is my proposed fix for the sock memcg static_key problem raised by Kamezawa. It works for me, but I would Kame, please confirm.

For that to work, I am dependent on two cgroup patches that goes attached. The rationale behind it, is that we can't do static_key updates with the cgroup_mutex held, or we risk deadlocking.

Looking closely, there seem to be no particular reason to hold the cgroup_mutex during destruction. Subsystems that really need it, can hold it themselves.

Tejun, let me know if this is acceptable from your PoV.

Glauber Costa (3):
don't attach a task to a dead cgroup
don't take cgroup_mutex in destroy() decrement static keys on real destroy time

 block/blk-cgroup.c        |    2 +
 include/net/sock.h        |    9 +++++++
 kernel/cgroup.c           |   12 ++++++----
 kernel/cpuset.c           |    2 +
 mm/memcontrol.c           |   20 +++++++++++++++-
 net/ipv4/tcp_memcontrol.c |   52 ++++++++++++++++++++++++++++++++++++++------
 6 files changed, 83 insertions(+), 14 deletions(-)
Signature · 1 line
1.7.7.6

A message that is not in the archive (sent off-list or not yet received): 1334875758-20939-1-git-send-email-glommer-bzQdu9zFT3WakBO8gow8eQ@public.gmane.org

#2 lore raw

PATCH 1/3 don't attach a task to a dead cgroup

Not all external callers of cgroup_attach_task() test to see if the cgroup is still live - the internal callers at cgroup.c does.

With this test in cgroup_attach_task, we can assure that no tasks are ever moved to a cgroup that is past its destruction point and was already marked as dead.

CC Tejun Heo <[email protected]>
CC Li Zefan <[email protected]>
CC Kamezawa Hiroyuki <[email protected]>
---
 kernel/cgroup.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/cgroup.c +3 -0
index b61b938..932c318 100644
@@ -1927,6 +1927,9 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 	struct cgroup_taskset tset = { };
 	struct css_set *newcg;
 
+	if (cgroup_is_removed(cgrp))
+		return -ENODEV;
+
 	/* @tsk either already exited or can't exit until the end */
 	if (tsk->flags & PF_EXITING)
 		return -ESRCH;
Signature · 1 line
1.7.7.6

A message that is not in the archive (sent off-list or not yet received): 1334875758-20939-2-git-send-email-glommer-bzQdu9zFT3WakBO8gow8eQ@public.gmane.org

#4 lore raw

Re: [PATCH 1/3] don't attach a task to a dead cgroup

Quoted 21 lines · Glauber Costa

Not all external callers of cgroup_attach_task() test to see if the cgroup is still live - the internal callers at cgroup.c does.

With this test in cgroup_attach_task, we can assure that no tasks are ever moved to a cgroup that is past its destruction point and was already marked as dead.

CC Tejun Heo <[email protected]>
CC Li Zefan <[email protected]>
CC Kamezawa Hiroyuki <[email protected]>
---
 kernel/cgroup.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
kernel/cgroup.c +0 -0
index b61b938..932c318 100644

kernel/cgroup.c int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) · line 1932

Quoted 6 lines
kernel/cgroup.c +3 -0
@@ -1927,6 +1927,9 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 	struct cgroup_taskset tset = { };
 	struct css_set *newcg;
 
+	if (cgroup_is_removed(cgrp))
+		return -ENODEV;
+

Isn't the test in cgroup_lock_live_group() enough?

Signature · 1 line
tejun
#5 lore raw

Re: [PATCH 1/3] don't attach a task to a dead cgroup

Quoted 25 lines · Tejun Heo
Quoted 24 lines · Glauber Costa

Not all external callers of cgroup_attach_task() test to see if the cgroup is still live - the internal callers at cgroup.c does.

With this test in cgroup_attach_task, we can assure that no tasks are ever moved to a cgroup that is past its destruction point and was already marked as dead.

CC Tejun Heo<[email protected]>
CC Li Zefan<[email protected]>
CC Kamezawa Hiroyuki<[email protected]>
---
  kernel/cgroup.c |    3 +++
  1 files changed, 3 insertions(+), 0 deletions(-)
kernel/cgroup.c +0 -0
index b61b938..932c318 100644
@@ -1927,6 +1927,9 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
  	struct cgroup_taskset tset = { };
  	struct css_set *newcg;
 
Quoted 6 lines

kernel/cgroup.c int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk) · line 1932

Quoted 3 lines
kernel/cgroup.c +3 -0
+	if (cgroup_is_removed(cgrp))
+		return -ENODEV;
+

Isn't the test in cgroup_lock_live_group() enough?

Yes, when it is done.

Not all callers take it, specially the external ones.

#6 lore raw

PATCH 2/3 don't take cgroup_mutex in destroy()

In reply to a message not in the archive

Most of the destroy functions are only doing very simple things like freeing memory.

The ones who goes through lists and such, already use its own locking for those.

* The cgroup itself won't go away until we free it, (after destroy)
* The parent won't go away because we hold a reference count
* There are no more tasks in the cgroup, and the cgroup is declared dead (cgroup_is_removed() == true)

For the blk-cgroup and the cpusets, I got the impression that the mutex is still necessary.

For those, I grabbed it from within the destroy function itself.

If the maintainer for those subsystems consider it safe to remove it, we can discuss it separately.

CC Tejun Heo <[email protected]>
CC Li Zefan <[email protected]>
CC Kamezawa Hiroyuki <[email protected]>
CC Vivek Goyal <[email protected]>
---
 block/blk-cgroup.c |    2 ++
 kernel/cgroup.c    |    9 ++++-----
 kernel/cpuset.c    |    2 ++
 3 files changed, 8 insertions(+), 5 deletions(-)
block/blk-cgroup.c +2 -0
index 126c341..477463f 100644
@@ -1527,6 +1527,7 @@ static void blkiocg_destroy(struct cgroup *cgroup)
 	struct blkio_policy_type *blkiop;
 	struct blkio_policy_node *pn, *pntmp;
 
+	cgroup_lock();
 	rcu_read_lock();
 	do {
 		spin_lock_irqsave(&blkcg->lock, flags);
@@ -1566,6 +1567,7 @@ static void blkiocg_destroy(struct cgroup *cgroup)
 	rcu_read_unlock();
 	if (blkcg != &blkio_root_cgroup)
 		kfree(blkcg);
+	cgroup_unlock();
 }
 
 static struct cgroup_subsys_state *blkiocg_create(struct cgroup *cgroup)
kernel/cgroup.c +4 -5
index 932c318..976d332 100644
@@ -869,13 +869,13 @@ static void cgroup_diput(struct dentry *dentry, struct inode *inode)
 		 * agent */
 		synchronize_rcu();
 
-		mutex_lock(&cgroup_mutex);
 		/*
 		 * Release the subsystem state objects.
 		 */
 		for_each_subsys(cgrp->root, ss)
 			ss->destroy(cgrp);
 
+		mutex_lock(&cgroup_mutex);
 		cgrp->root->number_of_cgroups--;
 		mutex_unlock(&cgroup_mutex);
 
@@ -3994,13 +3994,12 @@ static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
 
  err_destroy:
 
+	mutex_unlock(&cgroup_mutex);
 	for_each_subsys(root, ss) {
 		if (cgrp->subsys[ss->subsys_id])
 			ss->destroy(cgrp);
 	}
 
-	mutex_unlock(&cgroup_mutex);
-
 	/* Release the reference count that we took on the superblock */
 	deactivate_super(sb);
 
@@ -4349,9 +4348,9 @@ int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
 		int ret = cgroup_init_idr(ss, css);
 		if (ret) {
 			dummytop->subsys[ss->subsys_id] = NULL;
+			mutex_unlock(&cgroup_mutex);
 			ss->destroy(dummytop);
 			subsys[i] = NULL;
-			mutex_unlock(&cgroup_mutex);
 			return ret;
 		}
 	}
@@ -4447,10 +4446,10 @@ void cgroup_unload_subsys(struct cgroup_subsys *ss)
 	 * pointer to find their state. note that this also takes care of
 	 * freeing the css_id.
 	 */
+	mutex_unlock(&cgroup_mutex);
 	ss->destroy(dummytop);
 	dummytop->subsys[ss->subsys_id] = NULL;
 
-	mutex_unlock(&cgroup_mutex);
 }
 EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
 
kernel/cpuset.c +2 -0
index 8c8bd65..3cd4916 100644
@@ -1862,10 +1862,12 @@ static void cpuset_destroy(struct cgroup *cont)
 {
 	struct cpuset *cs = cgroup_cs(cont);
 
+	cgroup_lock();
 	if (is_sched_load_balance(cs))
 		update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
 
 	number_of_cpusets--;
+	cgroup_unlock();
 	free_cpumask_var(cs->cpus_allowed);
 	kfree(cs);
 }
Signature · 1 line
1.7.7.6

A message that is not in the archive (sent off-list or not yet received): 1334875758-20939-3-git-send-email-glommer-bzQdu9zFT3WakBO8gow8eQ@public.gmane.org

#8 lore raw

Re: [PATCH 2/3] don't take cgroup_mutex in destroy()

Quoted 11 lines · Glauber Costa

Most of the destroy functions are only doing very simple things like freeing memory.

The ones who goes through lists and such, already use its own locking for those.

* The cgroup itself won't go away until we free it, (after destroy)
* The parent won't go away because we hold a reference count
* There are no more tasks in the cgroup, and the cgroup is declared dead (cgroup_is_removed() == true)

Quoted 7 lines

For the blk-cgroup and the cpusets, I got the impression that the mutex is still necessary.

For those, I grabbed it from within the destroy function itself.

If the maintainer for those subsystems consider it safe to remove it, we can discuss it separately.

I really don't like cgroup_lock() usage spreading more. It's something which should be contained in cgroup.c proper. I looked at the existing users a while ago and they seemed to be compensating deficencies in API, so, if at all possible, let's not spread the disease.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#10 lore raw

Re: [PATCH 2/3] don't take cgroup_mutex in destroy()

Quoted 17 lines · Tejun Heo
Quoted 16 lines · Glauber Costa

Most of the destroy functions are only doing very simple things like freeing memory.

The ones who goes through lists and such, already use its own locking for those.

* The cgroup itself won't go away until we free it, (after destroy)
* The parent won't go away because we hold a reference count
* There are no more tasks in the cgroup, and the cgroup is declared dead (cgroup_is_removed() == true)

For the blk-cgroup and the cpusets, I got the impression that the mutex is still necessary.

For those, I grabbed it from within the destroy function itself.

Quoted 9 lines
Quoted 2 lines

If the maintainer for those subsystems consider it safe to remove it, we can discuss it separately.

I really don't like cgroup_lock() usage spreading more. It's something which should be contained in cgroup.c proper. I looked at the existing users a while ago and they seemed to be compensating deficencies in API, so, if at all possible, let's not spread the disease.

Agreed. I used to do cleanups to remove cgroup_lock()s in subsystems which are really not necessary.

#11 lore raw

Re: [PATCH 2/3] don't take cgroup_mutex in destroy()

In reply to a message not in the archive
Quoted 17 lines · Tejun Heo
Quoted 16 lines · Glauber Costa

Most of the destroy functions are only doing very simple things like freeing memory.

The ones who goes through lists and such, already use its own locking for those.

* The cgroup itself won't go away until we free it, (after destroy)
* The parent won't go away because we hold a reference count
* There are no more tasks in the cgroup, and the cgroup is declared dead (cgroup_is_removed() == true)

For the blk-cgroup and the cpusets, I got the impression that the mutex is still necessary.

For those, I grabbed it from within the destroy function itself.

Quoted 8 lines
Quoted 2 lines

If the maintainer for those subsystems consider it safe to remove it, we can discuss it separately.

I really don't like cgroup_lock() usage spreading more. It's something which should be contained in cgroup.c proper. I looked at the existing users a while ago and they seemed to be compensating deficencies in API, so, if at all possible, let's not spread the disease.

Well, I can dig deeper and see if they are really needed. I don't know cpusets and blkcg *that* well, that's why I took them there, hoping that someone could enlighten me, maybe they aren't really needed even now.

I agree with the compensating: As I mentioned, most of them are already taking other kinds of lock to protect their structures, which is the right thing to do.

There were only two or three spots in cpusets and blkcg where I wasn't that sure that we could drop the lock... What do you say about that ?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#13 lore raw

Re: [PATCH 2/3] don't take cgroup_mutex in destroy()

Quoted 27 lines · Glauber Costa
Quoted 17 lines · Tejun Heo
Quoted 16 lines · Glauber Costa

Most of the destroy functions are only doing very simple things like freeing memory.

The ones who goes through lists and such, already use its own locking for those.

* The cgroup itself won't go away until we free it, (after destroy)
* The parent won't go away because we hold a reference count
* There are no more tasks in the cgroup, and the cgroup is declared dead (cgroup_is_removed() == true)

For the blk-cgroup and the cpusets, I got the impression that the mutex is still necessary.

For those, I grabbed it from within the destroy function itself.

Quoted 8 lines
Quoted 2 lines

If the maintainer for those subsystems consider it safe to remove it, we can discuss it separately.

I really don't like cgroup_lock() usage spreading more. It's something which should be contained in cgroup.c proper. I looked at the existing users a while ago and they seemed to be compensating deficencies in API, so, if at all possible, let's not spread the disease.

Quoted 6 lines

Well, I can dig deeper and see if they are really needed. I don't know cpusets and blkcg *that* well, that's why I took them there, hoping that someone could enlighten me, maybe they aren't really needed even now.

I agree with the compensating: As I mentioned, most of them are already taking other kinds of lock to protect their structures, which is the right thing to do.

There were only two or three spots in cpusets and blkcg where I wasn't that sure that we could drop the lock... What do you say about that ? .

We can drop cgroup_mutex for cpusets with changes like this:

(Note: as I'm not able to get the latest code at this momment, this patch is based on 3.0.)

There are several places reading number_of_cpusets, but no one holds cgroup_mutex, except the one in generate_sched_domains(). With this patch, both cpuset_create() and generate_sched_domains() are still holding cgroup_mutex, so it's safe.

kernel/cpuset.c renamed from kernel/cpuset.c.orig +11 -1
@@ -1876,7 +1876,9 @@ static struct cgroup_subsys_state *cpuse
 	cs->relax_domain_level = -1;
 
 	cs->parent = parent;
+	mutex_lock(&callback_mutex);
 	number_of_cpusets++;
+	mutex_unlock(&callback_mutex);
 	return &cs->css ;
 }
 
@@ -1890,10 +1892,18 @@ static void cpuset_destroy(struct cgroup
 {
 	struct cpuset *cs = cgroup_cs(cont);
 
-	if (is_sched_load_balance(cs))
+	if (is_sched_load_balance(cs)) {
+		/*
+		 * This cpuset is under destruction, so no one else can
+		 * modify it, so it's safe to call update_flag() without
+		 * cgroup_lock.
+		 */
 		update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
+	}
 
+	mutex_lock(&callback_mutex);
 	number_of_cpusets--;
+	mutex_lock(&callback_mutex);
 	free_cpumask_var(cs->cpus_allowed);
 	kfree(cs);
 }

A message that is not in the archive (sent off-list or not yet received): [email protected]

#15 lore raw

Re: [PATCH 2/3] don't take cgroup_mutex in destroy()

Quoted 66 lines · Li Zefan
Quoted 27 lines · Glauber Costa
Quoted 17 lines · Tejun Heo
Quoted 16 lines · Glauber Costa

Most of the destroy functions are only doing very simple things like freeing memory.

The ones who goes through lists and such, already use its own locking for those.

* The cgroup itself won't go away until we free it, (after destroy)
* The parent won't go away because we hold a reference count
* There are no more tasks in the cgroup, and the cgroup is declared

    dead (cgroup_is_removed() == true)

For the blk-cgroup and the cpusets, I got the impression that the mutex is still necessary.

For those, I grabbed it from within the destroy function itself.

Quoted 8 lines
Quoted 2 lines

If the maintainer for those subsystems consider it safe to remove it, we can discuss it separately.

I really don't like cgroup_lock() usage spreading more. It's something which should be contained in cgroup.c proper. I looked at the existing users a while ago and they seemed to be compensating deficencies in API, so, if at all possible, let's not spread the disease.

Quoted 6 lines

Well, I can dig deeper and see if they are really needed. I don't know cpusets and blkcg *that* well, that's why I took them there, hoping that someone could enlighten me, maybe they aren't really needed even now.

I agree with the compensating: As I mentioned, most of them are already taking other kinds of lock to protect their structures, which is the right thing to do.

There were only two or three spots in cpusets and blkcg where I wasn't that sure that we could drop the lock... What do you say about that ? .

We can drop cgroup_mutex for cpusets with changes like this:

(Note: as I'm not able to get the latest code at this momment, this patch is based on 3.0.)

There are several places reading number_of_cpusets, but no one holds cgroup_mutex, except the one in generate_sched_domains(). With this patch, both cpuset_create() and generate_sched_domains() are still holding cgroup_mutex, so it's safe.

kernel/cpuset.c renamed from kernel/cpuset.c.orig +7 -1
@@ -1876,7 +1876,9 @@ static struct cgroup_subsys_state *cpuse
  	cs->relax_domain_level = -1;
 
  	cs->parent = parent;
+	mutex_lock(&callback_mutex);
  	number_of_cpusets++;
+	mutex_unlock(&callback_mutex);
  	return&cs->css ;
  }
 
@@ -1890,10 +1892,18 @@ static void cpuset_destroy(struct cgroup
  {
  	struct cpuset *cs = cgroup_cs(cont);
 
-	if (is_sched_load_balance(cs))
+	if (is_sched_load_balance(cs)) {
+		/*
+		 * This cpuset is under destruction, so no one else can
+		 * modify it, so it's safe to call update_flag() without
+		 * cgroup_lock.

kernel/cpuset.c static void cpuset_destroy(struct cgroup · line 1909

Quoted 10 lines
kernel/cpuset.c renamed from kernel/cpuset.c.orig +4 -0
+		 */
  		update_flag(CS_SCHED_LOAD_BALANCE, cs, 0);
+	}
 
+	mutex_lock(&callback_mutex);
  	number_of_cpusets--;
+	mutex_lock(&callback_mutex);
  	free_cpumask_var(cs->cpus_allowed);
  	kfree(cs);
  }

I'll see if I can work something out.

#16 lore raw

PATCH 3/3 decrement static keys on real destroy time

In reply to a message not in the archive

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.

[v2: changed a tcp limited flag for a generic proto limited flag ]

---
 include/net/sock.h        |    9 +++++++
 mm/memcontrol.c           |   20 +++++++++++++++-
 net/ipv4/tcp_memcontrol.c |   52 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 72 insertions(+), 9 deletions(-)
include/net/sock.h +9 -0
index b3ebe6b..c5a2010 100644
@@ -914,6 +914,15 @@ struct cg_proto {
 	int			*memory_pressure;
 	long			*sysctl_mem;
 	/*
+	 * active means it is currently active, and new sockets should
+	 * be assigned to cgroups.
+	 *
+	 * activated means it was ever activated, and we need to
+	 * disarm the static keys on destruction
+	 */
+	bool			activated;
+	bool			active; 
+	/*
 	 * memcg field is used to find which memcg we belong directly
 	 * Each memcg struct can hold more than one cg_proto, so container_of
 	 * won't really cut.
mm/memcontrol.c +18 -2
index 7832b4d..01d25a0 100644
@@ -404,6 +404,7 @@ void sock_update_memcg(struct sock *sk)
 {
 	if (mem_cgroup_sockets_enabled) {
 		struct mem_cgroup *memcg;
+		struct cg_proto *cg_proto;
 
 		BUG_ON(!sk->sk_prot->proto_cgroup);
 
@@ -423,9 +424,10 @@ void sock_update_memcg(struct sock *sk)
 
 		rcu_read_lock();
 		memcg = mem_cgroup_from_task(current);
-		if (!mem_cgroup_is_root(memcg)) {
+		cg_proto = sk->sk_prot->proto_cgroup(memcg);
+		if (!mem_cgroup_is_root(memcg) && cg_proto->active) {
 			mem_cgroup_get(memcg);
-			sk->sk_cgrp = sk->sk_prot->proto_cgroup(memcg);
+			sk->sk_cgrp = cg_proto;
 		}
 		rcu_read_unlock();
 	}
@@ -442,6 +444,14 @@ void sock_release_memcg(struct sock *sk)
 	}
 }
 
+static void disarm_static_keys(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_INET
+	if (memcg->tcp_mem.cg_proto.activated)
+		static_key_slow_dec(&memcg_socket_limit_enabled);
+#endif
+}
+
 #ifdef CONFIG_INET
 struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
 {
@@ -452,6 +462,11 @@ struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
 }
 EXPORT_SYMBOL(tcp_proto_cgroup);
 #endif /* CONFIG_INET */
+#else
+static inline void disarm_static_keys(struct mem_cgroup *memcg)
+{
+}
+
 #endif /* CONFIG_CGROUP_MEM_RES_CTLR_KMEM */
 
 static void drain_all_stock_async(struct mem_cgroup *memcg);
@@ -4883,6 +4898,7 @@ static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
 {
 	if (atomic_sub_and_test(count, &memcg->refcnt)) {
 		struct mem_cgroup *parent = parent_mem_cgroup(memcg);
+		disarm_static_keys(memcg);
 		__mem_cgroup_free(memcg);
 		if (parent)
 			mem_cgroup_put(parent);
net/ipv4/tcp_memcontrol.c +45 -7

73 lines not on this page · raw message

Signature · 1 line
1.7.7.6

A message that is not in the archive (sent off-list or not yet received): 1334875758-20939-4-git-send-email-glommer-bzQdu9zFT3WakBO8gow8eQ@public.gmane.org

#18 lore raw

Re: [PATCH 3/3] decrement static keys on real destroy time

Quoted 82 lines · Glauber Costa

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.

[v2: changed a tcp limited flag for a generic proto limited flag ]

---
 include/net/sock.h        |    9 +++++++
 mm/memcontrol.c           |   20 +++++++++++++++-
 net/ipv4/tcp_memcontrol.c |   52 ++++++++++++++++++++++++++++++++++++++------
 3 files changed, 72 insertions(+), 9 deletions(-)
include/net/sock.h +9 -0
index b3ebe6b..c5a2010 100644
@@ -914,6 +914,15 @@ struct cg_proto {
 	int			*memory_pressure;
 	long			*sysctl_mem;
 	/*
+	 * active means it is currently active, and new sockets should
+	 * be assigned to cgroups.
+	 *
+	 * activated means it was ever activated, and we need to
+	 * disarm the static keys on destruction
+	 */
+	bool			activated;
+	bool			active; 
+	/*
 	 * memcg field is used to find which memcg we belong directly
 	 * Each memcg struct can hold more than one cg_proto, so container_of
 	 * won't really cut.
mm/memcontrol.c +4 -2
index 7832b4d..01d25a0 100644
@@ -404,6 +404,7 @@ void sock_update_memcg(struct sock *sk)
 {
 	if (mem_cgroup_sockets_enabled) {
 		struct mem_cgroup *memcg;
+		struct cg_proto *cg_proto;
 
 		BUG_ON(!sk->sk_prot->proto_cgroup);
 
@@ -423,9 +424,10 @@ void sock_update_memcg(struct sock *sk)
 
 		rcu_read_lock();
 		memcg = mem_cgroup_from_task(current);
-		if (!mem_cgroup_is_root(memcg)) {
+		cg_proto = sk->sk_prot->proto_cgroup(memcg);
+		if (!mem_cgroup_is_root(memcg) && cg_proto->active) {
 
 
 
 			mem_cgroup_get(memcg);
-			sk->sk_cgrp = sk->sk_prot->proto_cgroup(memcg);
+			sk->sk_cgrp = cg_proto;
Quoted 1 line

}

Is this correct ? cg_proto->active can be true before all jump_labels are patched, then we can loose accounting. That will cause underflow of res_countner.

cg_proto->active should be set after jump_label modification. Then, things will work, I guess.

Thanks,
-Kame

Quoted 117 lines
mm/memcontrol.c +14 -0
 		rcu_read_unlock();
 	}
@@ -442,6 +444,14 @@ void sock_release_memcg(struct sock *sk)
 	}
 }
 
+static void disarm_static_keys(struct mem_cgroup *memcg)
+{
+#ifdef CONFIG_INET
+	if (memcg->tcp_mem.cg_proto.activated)
+		static_key_slow_dec(&memcg_socket_limit_enabled);
+#endif
+}
+
 #ifdef CONFIG_INET
 struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
 {
@@ -452,6 +462,11 @@ struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
 }
 EXPORT_SYMBOL(tcp_proto_cgroup);
 #endif /* CONFIG_INET */
+#else
+static inline void disarm_static_keys(struct mem_cgroup *memcg)
+{
+}
+
 #endif /* CONFIG_CGROUP_MEM_RES_CTLR_KMEM */
 
 static void drain_all_stock_async(struct mem_cgroup *memcg);
@@ -4883,6 +4898,7 @@ static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
 {
 	if (atomic_sub_and_test(count, &memcg->refcnt)) {
 		struct mem_cgroup *parent = parent_mem_cgroup(memcg);
+		disarm_static_keys(memcg);
 		__mem_cgroup_free(memcg);
 		if (parent)
 			mem_cgroup_put(parent);
net/ipv4/tcp_memcontrol.c +45 -7
index 1517037..d02573a 100644
@@ -54,6 +54,8 @@ int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
 	cg_proto->sysctl_mem = tcp->tcp_prot_mem;
 	cg_proto->memory_allocated = &tcp->tcp_memory_allocated;
 	cg_proto->sockets_allocated = &tcp->tcp_sockets_allocated;
+	cg_proto->active = false;
+	cg_proto->activated = false;
 	cg_proto->memcg = memcg;
 
 	return 0;
@@ -74,12 +76,23 @@ void tcp_destroy_cgroup(struct mem_cgroup *memcg)
 	percpu_counter_destroy(&tcp->tcp_sockets_allocated);
 
 	val = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
-
-	if (val != RESOURCE_MAX)
-		static_key_slow_dec(&memcg_socket_limit_enabled);
 }
 EXPORT_SYMBOL(tcp_destroy_cgroup);
 
+/*
+ * This is to prevent two writes arriving at the same time
+ * at kmem.tcp.limit_in_bytes.
+ *
+ * There is a race at the first time we write to this file:
+ *
+ * - cg_proto->activated == false for all writers.
+ * - They all do a static_key_slow_inc().
+ * - When we are finally read to decrement the static_keys,
+ *   we'll do it only once per activated cgroup. So we won't
+ *   be able to disable it.
+ */
+static DEFINE_MUTEX(tcp_set_limit_mutex);
+
 static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
 {
 	struct net *net = current->nsproxy->net_ns;
@@ -107,10 +120,35 @@ static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
 		tcp->tcp_prot_mem[i] = min_t(long, val >> PAGE_SHIFT,
 					     net->ipv4.sysctl_tcp_mem[i]);
 
-	if (val == RESOURCE_MAX && old_lim != RESOURCE_MAX)
-		static_key_slow_dec(&memcg_socket_limit_enabled);
-	else if (old_lim == RESOURCE_MAX && val != RESOURCE_MAX)
-		static_key_slow_inc(&memcg_socket_limit_enabled);
+	if (val == RESOURCE_MAX)
+		cg_proto->active = false;
+	else if (val != RESOURCE_MAX) {
+		cg_proto->active = true;
+
+
+		/*
+		 * ->activated 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.
+		 */
+		mutex_lock(&tcp_set_limit_mutex);
+		if (!cg_proto->activated) {
+			static_key_slow_inc(&memcg_socket_limit_enabled);
+			cg_proto->activated = true;
+		}
+		mutex_unlock(&tcp_set_limit_mutex);
+	}
 
 	return 0;
 }

A message that is not in the archive (sent off-list or not yet received): [email protected]

#20 lore raw

Re: [PATCH 3/3] decrement static keys on real destroy time

Quoted 10 lines · KAMEZAWA Hiroyuki
Quoted 4 lines

mem_cgroup_get(memcg);

Quoted 3 lines

- sk->sk_cgrp = sk->sk_prot->proto_cgroup(memcg);

 +			sk->sk_cgrp = cg_proto;
   		}

Is this correct ? cg_proto->active can be true before all jump_labels are patched, then we can loose accounting. That will cause underflow of res_countner.

Quoted 6 lines

cg_proto->active should be set after jump_label modification. Then, things will work, I guess.

Thanks,
-Kame

Kame,

You are right.

The first update needs to be done after the jump label activation as well. I got myself confused with the two flags =(

I will repost with this fixed once I get into agreement with Tejun and Li about the lockless ->destroy()

#21 lore raw
In reply to a message not in the archive
Quoted 5 lines · Glauber Costa

Hi,

This is my proposed fix for the sock memcg static_key problem raised by Kamezawa. It works for me, but I would Kame, please confirm.

Please detail the problem. I don't follow what's the purpose here.

Thanks.

Signature · 1 line
tejun

A message that is not in the archive (sent off-list or not yet received): [email protected]

#23 lore raw
Quoted 9 lines · Tejun Heo
Quoted 5 lines · Glauber Costa

Hi,

This is my proposed fix for the sock memcg static_key problem raised by Kamezawa. It works for me, but I would Kame, please confirm.

Please detail the problem. I don't follow what's the purpose here.

Ok.

1) Kame found the following bug: we were decrementing the jump label when the socket limit was set back to unlimited. The problem is that the sockets outlive the memcg, so we can only do that when the last reference count is dropped. It is worth mentioning that kmem controller for memcg will have the exact same problem - I am actually updating my series with all the results of this discussion here.

2) If, however, there are no sockets in flight, mem_cgroup_put() during ->destroy() will be the last one, and the decrementing will happen there.

3) static_key updates cannot happen with the cgroup_mutex held. This is
because cpusets hold it from within the cpu_hotplug.lock - that static_keys take through get_online_cpus() in its cpu hotplug handler.

4) Looking at the cpusets code, it really seems necessary, at least by now.

5) Deferring all this to worker threads as you suggested in the cpu thread - that has a similar problem - can solve this problem, but in general, will create tons of others, like windows of inconsistent information.

That's basically it.