Skip to contentSkip to conversation

Rework locking when rendering mountinfo cgroup paths

RFC PATCH In review Michal Koutný 4 participants 15 messages May 2, 2023 – May 23, 2023

Subsystems (2)
lore
Share & tools
lore

Michal Koutný

author maintainer
#0 lore raw

cover Rework locking when rendering mountinfo cgroup paths

Idea for these modification came up when css_set_lock seemed unneeded in cgroup_show_path.
It's a delicate change, so the deciding factor was when cgroup_show_path popped up also in some profiles of frequent mountinfo readers.
The idea is to trade the exclusive css_set_lock for the shared namespace_sem when rendering cgroup paths. Details are described more in individual commits.

Michal Koutný (3):
cgroup: Drop unused function for cgroup_path
cgroup: Rely on namespace_sem in current_cgns_cgroup_from_root
explicitly
cgroup: Do not take css_set_lock in cgroup_show_path

 fs/namespace.c         |  5 +++-
 include/linux/mount.h  |  4 +++
 kernel/cgroup/cgroup.c | 58 ++++++++----------------------------------
 3 files changed, 18 insertions(+), 49 deletions(-)
Signature · 1 line
2.40.1
author maintainer
#1 lore raw

RFC PATCH 1/3 cgroup: Drop unused function for cgroup_path

There is no current user and there are alternative methods to obtain task's cgroup path.

---
 kernel/cgroup/cgroup.c | 39 ---------------------------------------
 1 file changed, 39 deletions(-)
kernel/cgroup/cgroup.c +0 -39
index 625d7483951c..55e5f0110e3b 100644
@@ -2378,45 +2378,6 @@ int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
 }
 EXPORT_SYMBOL_GPL(cgroup_path_ns);
 
-/**
- * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
- * @task: target task
- * @buf: the buffer to write the path into
- * @buflen: the length of the buffer
- *
- * Determine @task's cgroup on the first (the one with the lowest non-zero
- * hierarchy_id) cgroup hierarchy and copy its path into @buf.  This
- * function grabs cgroup_mutex and shouldn't be used inside locks used by
- * cgroup controller callbacks.
- *
- * Return value is the same as kernfs_path().
- */
-int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
-{
-	struct cgroup_root *root;
-	struct cgroup *cgrp;
-	int hierarchy_id = 1;
-	int ret;
-
-	cgroup_lock();
-	spin_lock_irq(&css_set_lock);
-
-	root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
-
-	if (root) {
-		cgrp = task_cgroup_from_root(task, root);
-		ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
-	} else {
-		/* if no hierarchy exists, everyone is in "/" */
-		ret = strscpy(buf, "/", buflen);
-	}
-
-	spin_unlock_irq(&css_set_lock);
-	cgroup_unlock();
-	return ret;
-}
-EXPORT_SYMBOL_GPL(task_cgroup_path);
-
 /**
  * cgroup_attach_lock - Lock for ->attach()
  * @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem
Signature · 1 line
2.40.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [RFC PATCH 1/3] cgroup: Drop unused function for cgroup_path

Quoted 48 lines · Michal Koutný

There is no current user and there are alternative methods to obtain task's cgroup path.

---
  kernel/cgroup/cgroup.c | 39 ---------------------------------------
  1 file changed, 39 deletions(-)
kernel/cgroup/cgroup.c +0 -32
index 625d7483951c..55e5f0110e3b 100644
@@ -2378,45 +2378,6 @@ int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
  }
  EXPORT_SYMBOL_GPL(cgroup_path_ns);
 
-/**
- * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
- * @task: target task
- * @buf: the buffer to write the path into
- * @buflen: the length of the buffer
- *
- * Determine @task's cgroup on the first (the one with the lowest non-zero
- * hierarchy_id) cgroup hierarchy and copy its path into @buf.  This
- * function grabs cgroup_mutex and shouldn't be used inside locks used by
- * cgroup controller callbacks.
- *
- * Return value is the same as kernfs_path().
- */
-int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
-{
-	struct cgroup_root *root;
-	struct cgroup *cgrp;
-	int hierarchy_id = 1;
-	int ret;
-
-	cgroup_lock();
-	spin_lock_irq(&css_set_lock);
-
-	root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
-
-	if (root) {
-		cgrp = task_cgroup_from_root(task, root);
-		ret = cgroup_path_ns_locked(cgrp, buf, buflen, &init_cgroup_ns);
-	} else {
-		/* if no hierarchy exists, everyone is in "/" */
-		ret = strscpy(buf, "/", buflen);
-	}

kernel/cgroup/cgroup.c int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen, · line 2383

Quoted 10 lines
kernel/cgroup/cgroup.c +0 -7
-
-	spin_unlock_irq(&css_set_lock);
-	cgroup_unlock();
-	return ret;
-}
-EXPORT_SYMBOL_GPL(task_cgroup_path);
-
  /**
   * cgroup_attach_lock - Lock for ->attach()
   * @lock_threadgroup: whether to down_write cgroup_threadgroup_rwsem

I went to a few of earlier Linux version down to v3.11. task_cgroup_path() doesn't seems to have any users in my few attempts. Anyway,

author maintainer
#3 lore raw

RFC PATCH 2/3 cgroup: Rely on namespace_sem in current_cgns_cgroup_from_root explicitly

In reply to Michal Koutný

The function current_cgns_cgroup_from_root() expects a stable cgroup_root, which is currently ensured with RCU read side paired with cgroup_destroy_root() called after RCU period.

The particular current_cgns_cgroup_from_root() is called from VFS code and cgroup_root stability can be also ensured by namespace_sem. Mark it explicitly as a preparation for further rework.

---
 fs/namespace.c         | 5 ++++-
 include/linux/mount.h  | 4 ++++
 kernel/cgroup/cgroup.c | 7 +++----
 3 files changed, 11 insertions(+), 5 deletions(-)
fs/namespace.c +4 -1
index 54847db5b819..0d2333832064 100644
@@ -71,7 +71,10 @@ static DEFINE_IDA(mnt_group_ida);
 static struct hlist_head *mount_hashtable __read_mostly;
 static struct hlist_head *mountpoint_hashtable __read_mostly;
 static struct kmem_cache *mnt_cache __read_mostly;
-static DECLARE_RWSEM(namespace_sem);
+DECLARE_RWSEM(namespace_sem);
+#ifdef CONFIG_LOCKDEP
+EXPORT_SYMBOL_GPL(namespace_sem);
+#endif
 static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
 static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
 
include/linux/mount.h +4 -0
index 1ea326c368f7..6277435f6748 100644
@@ -80,6 +80,10 @@ static inline struct mnt_idmap *mnt_idmap(const struct vfsmount *mnt)
 	return smp_load_acquire(&mnt->mnt_idmap);
 }
 
+#ifdef CONFIG_LOCKDEP
+extern struct rw_semaphore namespace_sem;
+#endif
+
 extern int mnt_want_write(struct vfsmount *mnt);
 extern int mnt_want_write_file(struct file *file);
 extern void mnt_drop_write(struct vfsmount *mnt);
kernel/cgroup/cgroup.c +3 -4
index 55e5f0110e3b..32d693a797b9 100644
@@ -1440,13 +1440,12 @@ current_cgns_cgroup_from_root(struct cgroup_root *root)
 
 	lockdep_assert_held(&css_set_lock);
 
-	rcu_read_lock();
+	/* namespace_sem ensures `root` stability on unmount */
+	lockdep_assert(lockdep_is_held_type(&namespace_sem, -1));
 
 	cset = current->nsproxy->cgroup_ns->root_cset;
 	res = __cset_cgroup_from_root(cset, root);
 
-	rcu_read_unlock();
-
 	return res;
 }
 
@@ -1454,7 +1453,7 @@ current_cgns_cgroup_from_root(struct cgroup_root *root)
  * Look up cgroup associated with current task's cgroup namespace on the default
  * hierarchy.
  *
- * Unlike current_cgns_cgroup_from_root(), this doesn't need locks:
+ * Relaxed locking requirements:
  * - Internal rcu_read_lock is unnecessary because we don't dereference any rcu
  *   pointers.
  * - css_set_lock is not needed because we just read cset->dfl_cgrp.
Signature · 1 line
2.40.1
#4 lore raw

Re: [RFC PATCH 2/3] cgroup: Rely on namespace_sem in current_cgns_cgroup_from_root explicitly

Quoted 20 lines · Michal Koutný

The function current_cgns_cgroup_from_root() expects a stable cgroup_root, which is currently ensured with RCU read side paired with cgroup_destroy_root() called after RCU period.

The particular current_cgns_cgroup_from_root() is called from VFS code and cgroup_root stability can be also ensured by namespace_sem. Mark it explicitly as a preparation for further rework.

---
  fs/namespace.c         | 5 ++++-
  include/linux/mount.h  | 4 ++++
  kernel/cgroup/cgroup.c | 7 +++----
  3 files changed, 11 insertions(+), 5 deletions(-)
fs/namespace.c +0 -0
index 54847db5b819..0d2333832064 100644

fs/namespace.c static DEFINE_IDA(mnt_group_ida); · line 76

Quoted 7 lines
fs/namespace.c +3 -1
@@ -71,7 +71,10 @@ static DEFINE_IDA(mnt_group_ida);
  static struct hlist_head *mount_hashtable __read_mostly;
  static struct hlist_head *mountpoint_hashtable __read_mostly;
  static struct kmem_cache *mnt_cache __read_mostly;
-static DECLARE_RWSEM(namespace_sem);
+DECLARE_RWSEM(namespace_sem);
+#ifdef CONFIG_LOCKDEP
+EXPORT_SYMBOL_GPL(namespace_sem);

I don't think fs/namespace.o and kernel/cgroup/cgroup.o can't be built into a kernel module. I doubt we need to export it.

Quoted 24 lines
fs/namespace.c +1 -0
+#endif
  static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
  static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
 
include/linux/mount.h +4 -0
index 1ea326c368f7..6277435f6748 100644
@@ -80,6 +80,10 @@ static inline struct mnt_idmap *mnt_idmap(const struct vfsmount *mnt)
  	return smp_load_acquire(&mnt->mnt_idmap);
  }
 
+#ifdef CONFIG_LOCKDEP
+extern struct rw_semaphore namespace_sem;
+#endif
+
  extern int mnt_want_write(struct vfsmount *mnt);
  extern int mnt_want_write_file(struct file *file);
  extern void mnt_drop_write(struct vfsmount *mnt);
kernel/cgroup/cgroup.c +0 -0
index 55e5f0110e3b..32d693a797b9 100644

kernel/cgroup/cgroup.c current_cgns_cgroup_from_root(struct cgroup_root *root) · line 1444

Quoted 6 lines
kernel/cgroup/cgroup.c +2 -1
@@ -1440,13 +1440,12 @@ current_cgns_cgroup_from_root(struct cgroup_root *root)
 
  	lockdep_assert_held(&css_set_lock);
 
-	rcu_read_lock();
+	/* namespace_sem ensures `root` stability on unmount */
+	lockdep_assert(lockdep_is_held_type(&namespace_sem, -1));

It will be easier if you just use lockdep_is_held() without the 2nd argment.

Quoted 10 lines
kernel/cgroup/cgroup.c +0 -2
  	cset = current->nsproxy->cgroup_ns->root_cset;
  	res = __cset_cgroup_from_root(cset, root);
 
-	rcu_read_unlock();
-
  	return res;
  }
 

kernel/cgroup/cgroup.c current_cgns_cgroup_from_root(struct cgroup_root *root) · line 1459

Quoted 8 lines
kernel/cgroup/cgroup.c +1 -1
@@ -1454,7 +1453,7 @@ current_cgns_cgroup_from_root(struct cgroup_root *root)
   * Look up cgroup associated with current task's cgroup namespace on the default
   * hierarchy.
   *
- * Unlike current_cgns_cgroup_from_root(), this doesn't need locks:
+ * Relaxed locking requirements:
   * - Internal rcu_read_lock is unnecessary because we don't dereference any rcu
   *   pointers.
   * - css_set_lock is not needed because we just read cset->dfl_cgrp.

Cheers,
Longman

#5 lore raw

Re: [RFC PATCH 2/3] cgroup: Rely on namespace_sem in current_cgns_cgroup_from_root explicitly

In reply to Michal Koutný
Quoted 36 lines · Michal Koutný

The function current_cgns_cgroup_from_root() expects a stable cgroup_root, which is currently ensured with RCU read side paired with cgroup_destroy_root() called after RCU period.

The particular current_cgns_cgroup_from_root() is called from VFS code and cgroup_root stability can be also ensured by namespace_sem. Mark it explicitly as a preparation for further rework.

---
 fs/namespace.c         | 5 ++++-
 include/linux/mount.h  | 4 ++++
 kernel/cgroup/cgroup.c | 7 +++----
 3 files changed, 11 insertions(+), 5 deletions(-)
fs/namespace.c +4 -1
index 54847db5b819..0d2333832064 100644
@@ -71,7 +71,10 @@ static DEFINE_IDA(mnt_group_ida);
 static struct hlist_head *mount_hashtable __read_mostly;
 static struct hlist_head *mountpoint_hashtable __read_mostly;
 static struct kmem_cache *mnt_cache __read_mostly;
-static DECLARE_RWSEM(namespace_sem);
+DECLARE_RWSEM(namespace_sem);
+#ifdef CONFIG_LOCKDEP
+EXPORT_SYMBOL_GPL(namespace_sem);
+#endif
 static HLIST_HEAD(unmounted);	/* protected by namespace_sem */
 static LIST_HEAD(ex_mountpoints); /* protected by namespace_sem */
 
include/linux/mount.h +0 -0
index 1ea326c368f7..6277435f6748 100644

include/linux/mount.h static inline struct mnt_idmap *mnt_idmap(const struct vfsmount *mnt) · line 85

Quoted 6 lines
include/linux/mount.h +3 -0
@@ -80,6 +80,10 @@ static inline struct mnt_idmap *mnt_idmap(const struct vfsmount *mnt)
 	return smp_load_acquire(&mnt->mnt_idmap);
 }
 
+#ifdef CONFIG_LOCKDEP
+extern struct rw_semaphore namespace_sem;
+#endif

Nope, we're not putting namespace_sem in a header. The code it protects is massively sensitive and it interacts with mount_lock and other locks. This stays private to fs/namespace.c as far as I'm concerned.

maintainer
#6 lore raw

Re: [RFC PATCH 2/3] cgroup: Rely on namespace_sem in current_cgns_cgroup_from_root explicitly

Hello,

On Tue, May 23, 2023 at 12:42:46PM +0200, Christian Brauner wrote:
...

Quoted 3 lines

Nope, we're not putting namespace_sem in a header. The code it protects is massively sensitive and it interacts with mount_lock and other locks. This stays private to fs/namespace.c as far as I'm concerned.

Michal, would it make sense to add a separate locking in cgroup.c? It'll add a bit more overhead but not massively so and we should be able to get similar gain without entangling with namespace locking.

Thanks.

Signature · 1 line
tejun
author maintainer
#7 lore raw

RFC PATCH 3/3 cgroup: Do not take css_set_lock in cgroup_show_path

In reply to Michal Koutný

/proc/$pid/mountinfo may accumulate lots of entries (causing frequent re-reads of whole file) or lots cgroupfs entries alone.
The cgroupfs entries rendered with cgroup_show_path() may increase/be subject of css_set_lock contention causing further slowdown -- not only mountinfo rendering but any other css_set_lock user.

We leverage the fact that mountinfo reading happens with namespace_sem taken and hierarchy roots thus cannot be freed concurrently.

There are three relevant nodes for each cgroupfs entry:

R ... cgroup hierarchy root
M ... mount root
C ... reader's cgroup NS root

mountinfo is supposed to show path from C to M.

Current's css_set (and linked root cgroups) are stable under namespace_sem, therefore current_cgns_cgroup_from_root() doesn't need css_set_lock.

When the path is assembled in kernfs_path_from_node(), we know that:
- C kernfs_node is (transitively) pinned via current->nsproxy,
- M kernfs_node is pinned thanks to namespace_sem,
- path C to M is pinned via child->parent references (this holds also when C and M are in distinct subtrees).

Streamline mountinfo rendering a bit by relieving css_set_lock and add careful notes about that.

---
 kernel/cgroup/cgroup.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/cgroup/cgroup.c +7 -5
index 32d693a797b9..e2ec6f0028be 100644
@@ -1407,12 +1407,18 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
 	struct cgroup *res_cgroup = NULL;
 
 	if (cset == &init_css_set) {
+		/* callers must ensure root stability */
 		res_cgroup = &root->cgrp;
 	} else if (root == &cgrp_dfl_root) {
 		res_cgroup = cset->dfl_cgrp;
 	} else {
 		struct cgrp_cset_link *link;
-		lockdep_assert_held(&css_set_lock);
+		/* cset's cgroups are pinned unless they are root cgroups that
+		 * were unmounted.  We look at links to !cgrp_dfl_root
+		 * cgroup_root, either lock ensures the list is not mutated
+		 */
+		lockdep_assert(lockdep_is_held(&css_set_lock) ||
+			       lockdep_is_held_type(&namespace_sem, -1));
 
 		list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
 			struct cgroup *c = link->cgrp;
@@ -1438,8 +1444,6 @@ current_cgns_cgroup_from_root(struct cgroup_root *root)
 	struct cgroup *res = NULL;
 	struct css_set *cset;
 
-	lockdep_assert_held(&css_set_lock);
-
 	/* namespace_sem ensures `root` stability on unmount */
 	lockdep_assert(lockdep_is_held_type(&namespace_sem, -1));
 
@@ -1905,10 +1909,8 @@ int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
 	if (!buf)
 		return -ENOMEM;
 
-	spin_lock_irq(&css_set_lock);
 	ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
 	len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
-	spin_unlock_irq(&css_set_lock);
 
 	if (len >= PATH_MAX)
 		len = -ERANGE;
Signature · 1 line
2.40.1
#8 lore raw

Re: [RFC PATCH 3/3] cgroup: Do not take css_set_lock in cgroup_show_path

Quoted 46 lines · Michal Koutný

/proc/$pid/mountinfo may accumulate lots of entries (causing frequent re-reads of whole file) or lots cgroupfs entries alone.
The cgroupfs entries rendered with cgroup_show_path() may increase/be subject of css_set_lock contention causing further slowdown -- not only mountinfo rendering but any other css_set_lock user.

We leverage the fact that mountinfo reading happens with namespace_sem taken and hierarchy roots thus cannot be freed concurrently.

There are three relevant nodes for each cgroupfs entry:

R ... cgroup hierarchy root
M ... mount root
C ... reader's cgroup NS root

mountinfo is supposed to show path from C to M.

Current's css_set (and linked root cgroups) are stable under namespace_sem, therefore current_cgns_cgroup_from_root() doesn't need css_set_lock.

When the path is assembled in kernfs_path_from_node(), we know that:
- C kernfs_node is (transitively) pinned via current->nsproxy,
- M kernfs_node is pinned thanks to namespace_sem,
- path C to M is pinned via child->parent references (this holds also when C and M are in distinct subtrees).

Streamline mountinfo rendering a bit by relieving css_set_lock and add careful notes about that.

---
  kernel/cgroup/cgroup.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
kernel/cgroup/cgroup.c +1 -0
index 32d693a797b9..e2ec6f0028be 100644
@@ -1407,12 +1407,18 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset,
  	struct cgroup *res_cgroup = NULL;
 
  	if (cset == &init_css_set) {
+		/* callers must ensure root stability */
  		res_cgroup = &root->cgrp;
  	} else if (root == &cgrp_dfl_root) {

kernel/cgroup/cgroup.c static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset, · line 1421

Quoted 10 lines
kernel/cgroup/cgroup.c +6 -1
  		res_cgroup = cset->dfl_cgrp;
  	} else {
  		struct cgrp_cset_link *link;
-		lockdep_assert_held(&css_set_lock);
+		/* cset's cgroups are pinned unless they are root cgroups that
+		 * were unmounted.  We look at links to !cgrp_dfl_root
+		 * cgroup_root, either lock ensures the list is not mutated
+		 */
+		lockdep_assert(lockdep_is_held(&css_set_lock) ||
+			       lockdep_is_held_type(&namespace_sem, -1));

Again lockdep_is_held(&namespace_sem) is good enough.

Quoted 13 lines
kernel/cgroup/cgroup.c +0 -2
  		list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
  			struct cgroup *c = link->cgrp;
@@ -1438,8 +1444,6 @@ current_cgns_cgroup_from_root(struct cgroup_root *root)
  	struct cgroup *res = NULL;
  	struct css_set *cset;
 
-	lockdep_assert_held(&css_set_lock);
-
  	/* namespace_sem ensures `root` stability on unmount */
  	lockdep_assert(lockdep_is_held_type(&namespace_sem, -1));
 

kernel/cgroup/cgroup.c int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node, · line 1916

Quoted 10 lines
kernel/cgroup/cgroup.c +0 -2
@@ -1905,10 +1909,8 @@ int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
  	if (!buf)
  		return -ENOMEM;
 
-	spin_lock_irq(&css_set_lock);
  	ns_cgroup = current_cgns_cgroup_from_root(kf_cgroot);
  	len = kernfs_path_from_node(kf_node, ns_cgroup->kn, buf, PATH_MAX);
-	spin_unlock_irq(&css_set_lock);
 
  	if (len >= PATH_MAX)
  		len = -ERANGE;

Cheers,
Longman

maintainer
#9 lore raw

Re: [RFC PATCH 3/3] cgroup: Do not take css_set_lock in cgroup_show_path

In reply to Michal Koutný

Hello,

Quoted 9 lines · Michal Koutný

/proc/$pid/mountinfo may accumulate lots of entries (causing frequent re-reads of whole file) or lots cgroupfs entries alone.
The cgroupfs entries rendered with cgroup_show_path() may increase/be subject of css_set_lock contention causing further slowdown -- not only mountinfo rendering but any other css_set_lock user.

We leverage the fact that mountinfo reading happens with namespace_sem taken and hierarchy roots thus cannot be freed concurrently.

Quoted 7 lines

There are three relevant nodes for each cgroupfs entry:

R ... cgroup hierarchy root
M ... mount root
C ... reader's cgroup NS root

mountinfo is supposed to show path from C to M.

At least for cgroup2, the path from C to M isn't gonna change once NS is established, right? Nothing can be moved or renamed while the NS root is there. If so, can't we just cache the formatted path and return the same thing without any locking? The proposed changes seem a bit too brittle to me.

Thanks.

Signature · 1 line
tejun
author maintainer
#10 lore raw

Re: [RFC PATCH 3/3] cgroup: Do not take css_set_lock in cgroup_show_path

Quoted 10 lines · Tejun Heo
Quoted 7 lines

There are three relevant nodes for each cgroupfs entry:

R ... cgroup hierarchy root
M ... mount root
C ... reader's cgroup NS root

mountinfo is supposed to show path from C to M.

At least for cgroup2, the path from C to M isn't gonna change once NS is established, right?

Right. Although, the argument about M (when C above M or when C and M in different subtrees) implicitly relies on the namespace_sem.

Quoted 3 lines

Nothing can be moved or renamed while the NS root is there. If so, can't we just cache the formatted path and return the same thing without any locking?

Here I find the caching complexity not worth it for v2 only (also C is in the eye of the beholder) -- because then css_set_lock can be dropped from cgroup_show_path with simpler reasoning.

(Sadly, the bigger benefit would be on hybrid setups multiplied by the number of v1 hierarchies listed.)

(OTOH, the caching argument and weights for it may be different for /proc/$pid/cgroup.)

Quoted 1 line

The proposed changes seem a bit too brittle to me.

OK, I'll look into separate cgroup_show_path and cgroup1_show_path approach.

Thanks,
Michal

maintainer
#11 lore raw

Re: [RFC PATCH 3/3] cgroup: Do not take css_set_lock in cgroup_show_path

Hello,

Quoted 7 lines · Michal Koutný
Quoted 6 lines · Tejun Heo
Quoted 6 lines

There are three relevant nodes for each cgroupfs entry:

R ... cgroup hierarchy root
M ... mount root
C ... reader's cgroup NS root

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

mountinfo is supposed to show path from C to M.

At least for cgroup2, the path from C to M isn't gonna change once NS is established, right?

Right. Although, the argument about M (when C above M or when C and M in different subtrees) implicitly relies on the namespace_sem.

I don't follow. Can you please elaborate a bit more?

Thanks.

Signature · 1 line
tejun
author maintainer
#12 lore raw

Re: [RFC PATCH 3/3] cgroup: Do not take css_set_lock in cgroup_show_path

Quoted 10 lines · Tejun Heo
Quoted 9 lines · Michal Koutný
Quoted 8 lines · Tejun Heo
Quoted 7 lines

There are three relevant nodes for each cgroupfs entry:

R ... cgroup hierarchy root
M ... mount root
C ... reader's cgroup NS root

mountinfo is supposed to show path from C to M.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

At least for cgroup2, the path from C to M isn't gonna change once NS is established, right?

Right. Although, the argument about M (when C above M or when C and M in different subtrees) implicitly relies on the namespace_sem.

I don't follow. Can you please elaborate a bit more?

I wanted to say that even with restriction to cgroup2, the css_set_lock removal would also rely on namespace_sem.

For a given mountinfo entry the path C--M won't change (no renames). The question is whether cgroup M will stay around (with the relaxed locking):

- C >= M (C is below M)

    -> C (transitively) pins M

- C < M (C is above M) or C and M are in two disjoint subtrees (path goes through a common ancestor)
-> M could be released without relation to C (even on cgroup2, with
the css_set_lock removed) but such a destructive operation on M is excluded as long as namespace_sem is held during entry rendering.

Does that clarify the trade-off of removing css_set_lock at this spot?

Thanks,
Michal

maintainer
#13 lore raw

Re: [RFC PATCH 3/3] cgroup: Do not take css_set_lock in cgroup_show_path

Hello, Michal.

Sorry about the delay.

Quoted 22 lines · Michal Koutný
Quoted 10 lines · Tejun Heo
Quoted 9 lines · Michal Koutný
Quoted 8 lines · Tejun Heo
Quoted 7 lines

There are three relevant nodes for each cgroupfs entry:

R ... cgroup hierarchy root
M ... mount root
C ... reader's cgroup NS root

mountinfo is supposed to show path from C to M.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

At least for cgroup2, the path from C to M isn't gonna change once NS is established, right?

Right. Although, the argument about M (when C above M or when C and M in different subtrees) implicitly relies on the namespace_sem.

I don't follow. Can you please elaborate a bit more?

I wanted to say that even with restriction to cgroup2, the css_set_lock removal would also rely on namespace_sem.

Quoted 6 lines

For a given mountinfo entry the path C--M won't change (no renames). The question is whether cgroup M will stay around (with the relaxed locking):

- C >= M (C is below M)

    -> C (transitively) pins M

Yeah, this was what I was thinking.

Quoted 8 lines

- C < M (C is above M) or C and M are in two disjoint subtrees (path goes through a common ancestor)
-> M could be released without relation to C (even on cgroup2, with
the css_set_lock removed) but such a destructive operation on M is excluded as long as namespace_sem is held during entry rendering.

Does that clarify the trade-off of removing css_set_lock at this spot?

Right, you can have cgroup outside NS root still mounted and that mount root can be viewed from multiple cgroup NS's, so the the path isn't fixed either.

Having enough lockdep annotations should do but if reasonable the preference is being a bit more self-contained.

Thanks for the explanation.

Signature · 1 line
tejun
#14 lore raw
In reply to Michal Koutný
Quoted 6 lines · Michal Koutný

Idea for these modification came up when css_set_lock seemed unneeded in cgroup_show_path.
It's a delicate change, so the deciding factor was when cgroup_show_path popped up also in some profiles of frequent mountinfo readers.
The idea is to trade the exclusive css_set_lock for the shared namespace_sem when rendering cgroup paths. Details are described more in

I have no issue with the cgroup specific part of relying on namespace_sem but kernel/cgroup/ has no business of being aware of namespace semaphore in any way. Leave a comment to clarify what you're doing but we're not going to sprinkle namespace_sem references - even if only for the sake of lockdep - into other subsystems.