Skip to contentSkip to conversation

cgroup: Remove pidlist caching from v1 code

PATCH In review Michal Koutný 2 participants 4 messages Aug 23, 2023 – Aug 29, 2023 cgroup

lore
Share & tools
lore

Michal Koutný

author maintainer
#0 lore raw

cover cgroup: Remove pidlist caching from v1 code

More details in the first commit message.

Michal Koutný (2):
cgroup: Print v1 cgroup.procs and tasks without pidlists
cgroup: Remove v1 specific pidlist code

 include/linux/cgroup-defs.h     |   7 -
 kernel/cgroup/cgroup-internal.h |  12 +-
 kernel/cgroup/cgroup-v1.c       | 384 ++------------------------------
 kernel/cgroup/cgroup.c          |  11 +-
 4 files changed, 26 insertions(+), 388 deletions(-)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/2 cgroup: Print v1 cgroup.procs and tasks without pidlists

pidlists are structure that allows printing cgroup member tasks in sorted order and with proper pidns (in)visibility.

The promise of sorted output was removed in the commit 7823da36ce8e ("cgroups: update documentation of cgroups tasks and procs files") (more than 13 years ago at the time of writing this).

On systems that still use v1 hierarchies (e.g. systemd in non-unified mode), pidlists are problematic because:
a) their cache unnecessarily busies workqueues (cgroup_pidlist_destroy_wq)
b) PID recycling [1] may lead to logging noise:

Quoted 1 line

seq_file: buggy .next function kernfs_seq_next did not update position index

It is possible to reuse cgroup v2 code that relies directly on css_set iterator without caching (effectively extracting css_task_iter_* calls from pidlist_array_load()).
We only need to make a provision for pidns by skipping external tasks (instead of printing '0' like in v2).

[1] cgroup v1 code uses PID as the iterator position, PID recycling causes a repetition of the same PID at the end of (tasks) pidlist and seq_file interprets this as a non-incremented index. (seq_file code corrects it by PID++, which should be harmless unless tasks file is read byte by byte).

---
 kernel/cgroup/cgroup-internal.h |  5 +++++
 kernel/cgroup/cgroup-v1.c       | 32 ++++++++++++++++++++++++--------
 kernel/cgroup/cgroup.c          |  8 ++++----
 3 files changed, 33 insertions(+), 12 deletions(-)
kernel/cgroup/cgroup-internal.h +5 -0
index c56071f150f2..8edf7aeac159 100644
@@ -264,6 +264,11 @@ int cgroup_show_path(struct seq_file *sf, struct kernfs_node *kf_node,
 int __cgroup_task_count(const struct cgroup *cgrp);
 int cgroup_task_count(const struct cgroup *cgrp);
 
+void cgroup_procs_release(struct kernfs_open_file *of);
+void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos);
+void *cgroup_procs_start(struct seq_file *s, loff_t *pos);
+void *cgroup_threads_start(struct seq_file *s, loff_t *pos);
+
 /*
  * rstat.c
  */
kernel/cgroup/cgroup-v1.c +24 -8
index 83044312bc41..7c0945ccba0d 100644
@@ -486,6 +486,22 @@ static int cgroup_pidlist_show(struct seq_file *s, void *v)
 	return 0;
 }
 
+static int cgroup1_procs_show(struct seq_file *s, void *v)
+{
+	pid_t pid;
+
+	/* Print PID both for `tasks` file (threads) and `cgroup.procs`
+	 * (processes), the latter iterates with CSS_TASK_ITER_PROCS hence we
+	 * get PIDs of thread group leaders, i.e. tgids.
+	 */
+	pid = task_pid_vnr(v);
+	if (!pid)
+		return SEQ_SKIP;
+
+	seq_printf(s, "%d\n", pid);
+	return 0;
+}
+
 static ssize_t __cgroup1_procs_write(struct kernfs_open_file *of,
 				     char *buf, size_t nbytes, loff_t off,
 				     bool threadgroup)
@@ -623,11 +639,11 @@ static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
 struct cftype cgroup1_base_files[] = {
 	{
 		.name = "cgroup.procs",
-		.seq_start = cgroup_pidlist_start,
-		.seq_next = cgroup_pidlist_next,
-		.seq_stop = cgroup_pidlist_stop,
-		.seq_show = cgroup_pidlist_show,
 		.private = CGROUP_FILE_PROCS,
+		.release = cgroup_procs_release,
+		.seq_start = cgroup_procs_start,
+		.seq_next = cgroup_procs_next,
+		.seq_show = cgroup1_procs_show,
 		.write = cgroup1_procs_write,
 	},
 	{
@@ -642,11 +658,11 @@ struct cftype cgroup1_base_files[] = {
 	},
 	{
 		.name = "tasks",
-		.seq_start = cgroup_pidlist_start,
-		.seq_next = cgroup_pidlist_next,
-		.seq_stop = cgroup_pidlist_stop,
-		.seq_show = cgroup_pidlist_show,
 		.private = CGROUP_FILE_TASKS,
+		.release = cgroup_procs_release,
+		.seq_start = cgroup_threads_start,
+		.seq_next = cgroup_procs_next,
+		.seq_show = cgroup1_procs_show,
 		.write = cgroup1_tasks_write,
 	},
 	{
kernel/cgroup/cgroup.c +4 -4
index f55a40db065f..3c5ba2ca7852 100644
@@ -4963,7 +4963,7 @@ void css_task_iter_end(struct css_task_iter *it)
 		put_task_struct(it->cur_task);
 }
 
-static void cgroup_procs_release(struct kernfs_open_file *of)
+void cgroup_procs_release(struct kernfs_open_file *of)
 {
 	struct cgroup_file_ctx *ctx = of->priv;
 
@@ -4971,7 +4971,7 @@ static void cgroup_procs_release(struct kernfs_open_file *of)
 		css_task_iter_end(&ctx->procs.iter);
 }
 
-static void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
+void *cgroup_procs_next(struct seq_file *s, void *v, loff_t *pos)
 {
 	struct kernfs_open_file *of = s->private;
 	struct cgroup_file_ctx *ctx = of->priv;
@@ -5008,7 +5008,7 @@ static void *__cgroup_procs_start(struct seq_file *s, loff_t *pos,
 	return cgroup_procs_next(s, NULL, NULL);
 }
 
-static void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
+void *cgroup_procs_start(struct seq_file *s, loff_t *pos)
 {
 	struct cgroup *cgrp = seq_css(s)->cgroup;
 
@@ -5152,7 +5152,7 @@ static ssize_t cgroup_procs_write(struct kernfs_open_file *of,
 	return __cgroup_procs_write(of, buf, true) ?: nbytes;
 }
 
-static void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
+void *cgroup_threads_start(struct seq_file *s, loff_t *pos)
 {
 	return __cgroup_procs_start(s, pos, 0);
 }
Signature · 1 line
2.41.0
maintainer
#2 lore raw

Re: [PATCH 1/2] cgroup: Print v1 cgroup.procs and tasks without pidlists

Hello, Michal.

Quoted 6 lines · Michal Koutný

pidlists are structure that allows printing cgroup member tasks in sorted order and with proper pidns (in)visibility.

The promise of sorted output was removed in the commit 7823da36ce8e ("cgroups: update documentation of cgroups tasks and procs files") (more than 13 years ago at the time of writing this).

While this is true, I'm not sure it'd be a good idea to change the behavior after all these years. Whoever is still using cgroup1 likely doens't mean to update their code at all and it's difficult to rule out odd-ball users that might have been depending on the ordered output knowingly or unknowingly.

Quoted 5 lines

On systems that still use v1 hierarchies (e.g. systemd in non-unified mode), pidlists are problematic because:
a) their cache unnecessarily busies workqueues (cgroup_pidlist_destroy_wq)
b) PID recycling [1] may lead to logging noise:

Quoted 1 line

seq_file: buggy .next function kernfs_seq_next did not update position index

While I like the code reduction, I wonder whether the right course of action is just fixing the warning here.

Thanks.

Signature · 1 line
tejun
author maintainer
#3 lore raw

PATCH 2/2 cgroup: Remove v1 specific pidlist code

In reply to Michal Koutný

Functions and structures for generating, caching, disposing and presenting pidlists are unused since v1 conversion to unordered listing of member tasks. Remove the dead code now.

---
 include/linux/cgroup-defs.h     |   7 -
 kernel/cgroup/cgroup-internal.h |   7 -
 kernel/cgroup/cgroup-v1.c       | 366 --------------------------------
 kernel/cgroup/cgroup.c          |   3 -
 4 files changed, 383 deletions(-)
include/linux/cgroup-defs.h +0 -7
index 8a0d5466c7be..639fc8e6c8c2 100644
@@ -482,13 +482,6 @@ struct cgroup {
 	struct cgroup_base_stat bstat;
 	struct prev_cputime prev_cputime;	/* for printing out cputime */
 
-	/*
-	 * list of pidlists, up to two for each namespace (one for procs, one
-	 * for tasks); created on demand.
-	 */
-	struct list_head pidlists;
-	struct mutex pidlist_mutex;
-
 	/* used to wait for offlining of csses */
 	wait_queue_head_t offline_waitq;
 
kernel/cgroup/cgroup-internal.h +0 -7
index 8edf7aeac159..3786009ef0ad 100644
@@ -64,8 +64,6 @@ static inline struct cgroup_fs_context *cgroup_fc2context(struct fs_context *fc)
 	return container_of(kfc, struct cgroup_fs_context, kfc);
 }
 
-struct cgroup_pidlist;
-
 struct cgroup_file_ctx {
 	struct cgroup_namespace	*ns;
 
@@ -77,10 +75,6 @@ struct cgroup_file_ctx {
 		bool			started;
 		struct css_task_iter	iter;
 	} procs;
-
-	struct {
-		struct cgroup_pidlist	*pidlist;
-	} procs1;
 };
 
 /*
@@ -291,7 +285,6 @@ extern const struct fs_parameter_spec cgroup1_fs_parameters[];
 
 int proc_cgroupstats_show(struct seq_file *m, void *v);
 bool cgroup1_ssid_disabled(int ssid);
-void cgroup1_pidlist_destroy_all(struct cgroup *cgrp);
 void cgroup1_release_agent(struct work_struct *work);
 void cgroup1_check_for_release(struct cgroup *cgrp);
 int cgroup1_parse_param(struct fs_context *fc, struct fs_parameter *param);
kernel/cgroup/cgroup-v1.c +0 -366

402 lines not on this page · raw message

kernel/cgroup/cgroup.c +0 -3
index 3c5ba2ca7852..e65cbe2bfd7b 100644
@@ -2014,8 +2014,6 @@ static void init_cgroup_housekeeping(struct cgroup *cgrp)
 	INIT_LIST_HEAD(&cgrp->self.sibling);
 	INIT_LIST_HEAD(&cgrp->self.children);
 	INIT_LIST_HEAD(&cgrp->cset_links);
-	INIT_LIST_HEAD(&cgrp->pidlists);
-	mutex_init(&cgrp->pidlist_mutex);
 	cgrp->self.cgroup = cgrp;
 	cgrp->self.flags |= CSS_ONLINE;
 	cgrp->dom_cgrp = cgrp;
@@ -5332,7 +5330,6 @@ static void css_free_rwork_fn(struct work_struct *work)
 	} else {
 		/* cgroup free path */
 		atomic_dec(&cgrp->root->nr_cgrps);
-		cgroup1_pidlist_destroy_all(cgrp);
 		cancel_work_sync(&cgrp->release_agent_work);
 		bpf_cgrp_storage_free(cgrp);
 
Signature · 1 line
2.41.0