Skip to contentSkip to conversation

cls_cgroup: Fix rcu lockdep warning

PATCH Superseded Li Zefan 5 participants 6 messages Sep 2, 2010 – Sep 3, 2010

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

PATCH 1/1 cls_cgroup: Fix rcu lockdep warning

Calling task_subsys_state() without holding rcu_read_lock or cgroup_mutex can cause lockdep warning.

---
 include/net/cls_cgroup.h |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
include/net/cls_cgroup.h +8 -2
index dd1fdb8..a4dc5b0 100644
@@ -27,11 +27,17 @@ struct cgroup_cls_state
 #ifdef CONFIG_NET_CLS_CGROUP
 static inline u32 task_cls_classid(struct task_struct *p)
 {
+	int classid;
+
 	if (in_interrupt())
 		return 0;
 
-	return container_of(task_subsys_state(p, net_cls_subsys_id),
-			    struct cgroup_cls_state, css)->classid;
+	rcu_read_lock();
+	classid = container_of(task_subsys_state(p, net_cls_subsys_id),
+			       struct cgroup_cls_state, css)->classid;
+	rcu_read_unlock();
+
+	return classid;
 }
 #else
 extern int net_cls_subsys_id;
Signature · 1 line
1.6.3
#1 lore raw
Quoted 3 lines · Li Zefan

Calling task_subsys_state() without holding rcu_read_lock or cgroup_mutex can cause lockdep warning.

That is not a suitable changelog.

Was the warning correct? Is your patch correct? What does RCU protect here and why can we use classid after dropping it.

Simply frobbing code to make the warning go away is not good.

maintainer
#2 lore raw

From: Peter Zijlstra <[email protected]>
Date: Thu, 02 Sep 2010 12:38:36 +0200

Quoted 11 lines · Li Zefan
Quoted 3 lines

Calling task_subsys_state() without holding rcu_read_lock or cgroup_mutex can cause lockdep warning.

That is not a suitable changelog.

Was the warning correct? Is your patch correct? What does RCU protect here and why can we use classid after dropping it.

Simply frobbing code to make the warning go away is not good.

In fact shouldn't this be a rcu_dereference() or similar?

#3 lore raw
Quoted 8 lines · David Miller

From: Peter Zijlstra <[email protected]>
Date: Thu, 02 Sep 2010 12:38:36 +0200

Quoted 5 lines · Li Zefan
Quoted 3 lines

Calling task_subsys_state() without holding rcu_read_lock or cgroup_mutex can cause lockdep warning.

Quoted 8 lines
Quoted 6 lines

That is not a suitable changelog.

Was the warning correct? Is your patch correct? What does RCU protect here and why can we use classid after dropping it.

Simply frobbing code to make the warning go away is not good.

In fact shouldn't this be a rcu_dereference() or similar?

The rcu_dereference() is there, just buried in task_subsys_state_check() which is called from task_subsys_state(). The code extracts the classid, which is an integer, so the RCU-protected pointer does not leak out of the RCU read-side critical section.

So this is OK, but the changelog should indeed say why it is OK.

Thanx, Paul

author
#4 lore raw
In reply to Peter Zijlstra
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Li Zefan

Calling task_subsys_state() without holding rcu_read_lock or cgroup_mutex can cause lockdep warning.

Quoted 8 lines

That is not a suitable changelog.

Was the warning correct? Is your patch correct? What does RCU protect here and why can we use classid after dropping it.

Simply frobbing code to make the warning go away is not good.

task->cgroups and task->cgroups->subsys[i] are protected by RCU. So we avoid accessing invalid pointers here. This can happen, for example, when you are deref those pointers while someone move @task from one cgroup to another.

otoh, there is no lock rule for ->classid.

Acked-by on v1 1/1
#5 lore raw
In reply to Li Zefan
Quoted 5 lines · Li Zefan

Calling task_subsys_state() without holding rcu_read_lock or cgroup_mutex can cause lockdep warning.

Thanks for catching this.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt