Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1628279

[RFC PATCH 08/14] cgroup: Keep accurate count of tasks in each css_set

From Waiman Long <longman@redhat.com>
Newsgroups linux.kernel
Subject [RFC PATCH 08/14] cgroup: Keep accurate count of tasks in each css_set
Date 2017-04-21 16:10 +0200
Message-ID <tyHq1-6LU-3@gated-at.bofh.it> (permalink)
References <tyHq1-6LU-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


The reference count in the css_set data structure was used as a
proxy of the number of tasks attached to that css_set. However, that
count is actually not an accurate measure especially with thread mode
support. So a new variable task_count is added to the css_set to keep
track of the actual task count. This new variable is protected by
the css_set_lock. Functions that require the actual task count are
updated to use the new variable.

Signed-off-by: Waiman Long <longman@redhat.com>
---
 include/linux/cgroup-defs.h | 3 +++
 kernel/cgroup/cgroup-v1.c   | 6 +-----
 kernel/cgroup/cgroup.c      | 5 +++++
 kernel/cgroup/debug.c       | 6 +-----
 4 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h
index bb4752a..7be1a90 100644
--- a/include/linux/cgroup-defs.h
+++ b/include/linux/cgroup-defs.h
@@ -158,6 +158,9 @@ struct css_set {
 	/* reference count */
 	atomic_t refcount;
 
+	/* internal task count, protected by css_set_lock */
+	int task_count;
+
 	/*
 	 * If not threaded, the following points to self.  If threaded, to
 	 * a cset which belongs to the top cgroup of the threaded subtree.
diff --git a/kernel/cgroup/cgroup-v1.c b/kernel/cgroup/cgroup-v1.c
index 6757a50..6d69796 100644
--- a/kernel/cgroup/cgroup-v1.c
+++ b/kernel/cgroup/cgroup-v1.c
@@ -334,10 +334,6 @@ static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
 /**
  * cgroup_task_count - count the number of tasks in a cgroup.
  * @cgrp: the cgroup in question
- *
- * Return the number of tasks in the cgroup.  The returned number can be
- * higher than the actual number of tasks due to css_set references from
- * namespace roots and temporary usages.
  */
 static int cgroup_task_count(const struct cgroup *cgrp)
 {
@@ -346,7 +342,7 @@ static int cgroup_task_count(const struct cgroup *cgrp)
 
 	spin_lock_irq(&css_set_lock);
 	list_for_each_entry(link, &cgrp->cset_links, cset_link)
-		count += atomic_read(&link->cset->refcount);
+		count += link->cset->task_count;
 	spin_unlock_irq(&css_set_lock);
 	return count;
 }
diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index d48eedd..3186b1f 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -1671,6 +1671,7 @@ static void cgroup_enable_task_cg_lists(void)
 				css_set_update_populated(cset, true);
 			list_add_tail(&p->cg_list, &cset->tasks);
 			get_css_set(cset);
+			cset->task_count++;
 		}
 		spin_unlock(&p->sighand->siglock);
 	} while_each_thread(g, p);
@@ -2154,8 +2155,10 @@ static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx)
 			struct css_set *to_cset = cset->mg_dst_cset;
 
 			get_css_set(to_cset);
+			to_cset->task_count++;
 			css_set_move_task(task, from_cset, to_cset, true);
 			put_css_set_locked(from_cset);
+			from_cset->task_count--;
 		}
 	}
 	spin_unlock_irq(&css_set_lock);
@@ -5150,6 +5153,7 @@ void cgroup_post_fork(struct task_struct *child)
 		cset = task_css_set(current);
 		if (list_empty(&child->cg_list)) {
 			get_css_set(cset);
+			cset->task_count++;
 			css_set_move_task(child, NULL, cset, false);
 		}
 		spin_unlock_irq(&css_set_lock);
@@ -5199,6 +5203,7 @@ void cgroup_exit(struct task_struct *tsk)
 	if (!list_empty(&tsk->cg_list)) {
 		spin_lock_irq(&css_set_lock);
 		css_set_move_task(tsk, cset, NULL, false);
+		cset->task_count--;
 		spin_unlock_irq(&css_set_lock);
 	} else {
 		get_css_set(cset);
diff --git a/kernel/cgroup/debug.c b/kernel/cgroup/debug.c
index 9146461..c8f7590 100644
--- a/kernel/cgroup/debug.c
+++ b/kernel/cgroup/debug.c
@@ -23,10 +23,6 @@ static void debug_css_free(struct cgroup_subsys_state *css)
 /*
  * debug_taskcount_read - return the number of tasks in a cgroup.
  * @cgrp: the cgroup in question
- *
- * Return the number of tasks in the cgroup.  The returned number can be
- * higher than the actual number of tasks due to css_set references from
- * namespace roots and temporary usages.
  */
 static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
 				struct cftype *cft)
@@ -37,7 +33,7 @@ static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
 
 	spin_lock_irq(&css_set_lock);
 	list_for_each_entry(link, &cgrp->cset_links, cset_link)
-		count += atomic_read(&link->cset->refcount);
+		count += link->cset->task_count;
 	spin_unlock_irq(&css_set_lock);
 	return count;
 }
-- 
1.8.3.1

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[RFC PATCH 00/14] cgroup: Implement cgroup v2 thread mode & CPU controller Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 08/14] cgroup: Keep accurate count of tasks in each css_set Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 12/14] sched: Implement interface for cgroup unified hierarchy Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 13/14] sched: Make cpu/cpuacct threaded controllers Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 03/14] cgroup: introduce cgroup->proc_cgrp and threaded css_set handling Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 09/14] cgroup: Make debug cgroup support v2 and thread mode Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 06/14] cgroup: Fix reference counting bug in cgroup_procs_write() Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 07/14] cgroup: Move debug cgroup to its own file Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 05/14] cgroup: implement cgroup v2 thread support Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 10/14] cgroup: Implement new thread mode semantics Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 04/14] cgroup: implement CSS_TASK_ITER_THREADED Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 14/14] cgroup: Enable separate control knobs for thread root internal processes Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 02/14] cgroup: add @flags to css_task_iter_start() and implement CSS_TASK_ITER_PROCS Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 11/14] sched: Misc preps for cgroup unified hierarchy interface Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200
  [RFC PATCH 01/14] cgroup: reorganize cgroup.procs / task write path Waiman Long <longman@redhat.com> - 2017-04-21 16:10 +0200

csiph-web