cgroups.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
To: Michal Hocko <mhocko-AlSwsSmVLrQ@public.gmane.org>
Cc: Tejun Heo <tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
	lizefan-hv44wF8Li93QT0dZR+AlfA@public.gmane.org,
	cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org,
	linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org
Subject: [PATCH 1/3] memcg: introduce assign_new_owner()
Date: Fri, 22 May 2015 20:21:15 +0200	[thread overview]
Message-ID: <20150522182115.GB26770@redhat.com> (raw)
In-Reply-To: <20150522182054.GA26770-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>

The code under "assign_new_owner" looks very ugly and suboptimal.

We do not really need get_task_struct/put_task_struct(), we can
simply recheck/change mm->owner under tasklist_lock. And we do not
want to restart from the very beginning if ->mm was changed by the
time we take task_lock(), we can simply continue (if we do not drop
tasklist_lock).

Just move this code into the new simple helper, assign_new_owner().

Signed-off-by: Oleg Nesterov <oleg-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
---
 kernel/exit.c |   56 ++++++++++++++++++++++++++------------------------------
 1 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/kernel/exit.c b/kernel/exit.c
index 22fcc05..4d446ab 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -293,6 +293,23 @@ kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
 }
 
 #ifdef CONFIG_MEMCG
+static bool assign_new_owner(struct mm_struct *mm, struct task_struct *c)
+{
+	bool ret = false;
+
+	if (c->mm != mm)
+		return ret;
+
+	task_lock(c); /* protects c->mm from changing */
+	if (c->mm == mm) {
+		mm->owner = c;
+		ret = true;
+	}
+	task_unlock(c);
+
+	return ret;
+}
+
 /*
  * A task is exiting.   If it owned this mm, find a new owner for the mm.
  */
@@ -300,7 +317,6 @@ void mm_update_next_owner(struct mm_struct *mm)
 {
 	struct task_struct *c, *g, *p = current;
 
-retry:
 	/*
 	 * If the exiting or execing task is not the owner, it's
 	 * someone else's problem.
@@ -322,16 +338,16 @@ retry:
 	 * Search in the children
 	 */
 	list_for_each_entry(c, &p->children, sibling) {
-		if (c->mm == mm)
-			goto assign_new_owner;
+		if (assign_new_owner(mm, c))
+			goto done;
 	}
 
 	/*
 	 * Search in the siblings
 	 */
 	list_for_each_entry(c, &p->real_parent->children, sibling) {
-		if (c->mm == mm)
-			goto assign_new_owner;
+		if (assign_new_owner(mm, c))
+			goto done;
 	}
 
 	/*
@@ -341,42 +357,22 @@ retry:
 		if (g->flags & PF_KTHREAD)
 			continue;
 		for_each_thread(g, c) {
-			if (c->mm == mm)
-				goto assign_new_owner;
+			if (assign_new_owner(mm, c))
+				goto done;
 			if (c->mm)
 				break;
 		}
 	}
-	read_unlock(&tasklist_lock);
+
 	/*
 	 * We found no owner yet mm_users > 1: this implies that we are
 	 * most likely racing with swapoff (try_to_unuse()) or /proc or
 	 * ptrace or page migration (get_task_mm()).  Mark owner as NULL.
 	 */
 	mm->owner = NULL;
-	return;
-
-assign_new_owner:
-	BUG_ON(c == p);
-	get_task_struct(c);
-	/*
-	 * The task_lock protects c->mm from changing.
-	 * We always want mm->owner->mm == mm
-	 */
-	task_lock(c);
-	/*
-	 * Delay read_unlock() till we have the task_lock()
-	 * to ensure that c does not slip away underneath us
-	 */
+done:
 	read_unlock(&tasklist_lock);
-	if (c->mm != mm) {
-		task_unlock(c);
-		put_task_struct(c);
-		goto retry;
-	}
-	mm->owner = c;
-	task_unlock(c);
-	put_task_struct(c);
+	return;
 }
 #endif /* CONFIG_MEMCG */
 
-- 
1.5.5.1


  parent reply	other threads:[~2015-05-22 18:21 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-18 19:49 [PATCHSET cgroup/for-4.2] cgroup: make multi-process migration atomic Tejun Heo
     [not found] ` <1431978595-12176-1-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2015-05-18 19:49   ` [PATCH 1/7] cpuset: migrate memory only for threadgroup leaders Tejun Heo
2015-05-18 19:49   ` [PATCH 2/7] memcg: restructure mem_cgroup_can_attach() Tejun Heo
     [not found]     ` <1431978595-12176-3-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2015-05-19  9:03       ` Michal Hocko
2015-05-18 19:49   ` [PATCH 3/7] memcg: immigrate charges only when a threadgroup leader is moved Tejun Heo
     [not found]     ` <1431978595-12176-4-git-send-email-tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
2015-05-19 12:13       ` Michal Hocko
     [not found]         ` <20150519121321.GB6203-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2015-05-19 13:10           ` Michal Hocko
2015-05-19 21:27           ` Tejun Heo
2015-05-20 13:10             ` Michal Hocko
2015-05-20 13:21               ` Michal Hocko
     [not found]                 ` <20150520132158.GB28678-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2015-05-20 17:53                   ` Oleg Nesterov
2015-05-20 20:22                     ` Michal Hocko
     [not found]                       ` <20150520202221.GD14256-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2015-05-21 17:22                         ` Johannes Weiner
2015-05-22  9:34                           ` Michal Hocko
2015-05-21 19:27                         ` Oleg Nesterov
     [not found]                           ` <20150521192716.GA21304-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2015-05-22  9:36                             ` Michal Hocko
     [not found]                               ` <20150522093639.GE5109-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2015-05-22 16:29                                 ` Oleg Nesterov
2015-05-22 16:57                                   ` Michal Hocko
2015-05-22 18:30                                     ` Oleg Nesterov
     [not found]                                       ` <20150522183042.GF26770-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2015-05-25 16:06                                         ` Michal Hocko
     [not found]                                           ` <20150525160626.GC19389-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2015-05-25 17:06                                             ` Oleg Nesterov
     [not found]                                               ` <20150525170601.GA438-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2015-05-26  7:16                                                 ` Michal Hocko
2015-05-22 18:20                                   ` [PATCH 0/3] memcg: mm_update_next_owner() cleanups Oleg Nesterov
     [not found]                                     ` <20150522182054.GA26770-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2015-05-22 18:21                                       ` Oleg Nesterov [this message]
2015-05-22 18:21                                       ` [PATCH 2/3] memcg: change assign_new_owner() to consider the sub-htreads Oleg Nesterov
2015-05-22 18:21                                     ` [PATCH 3/3] memcg: change mm_update_next_owner() to search in sub-threads first Oleg Nesterov
2015-05-22 18:22                                     ` [PATCH 0/3] memcg: mm_update_next_owner() cleanups Oleg Nesterov
2015-05-21 14:12       ` [PATCH 3/7] memcg: immigrate charges only when a threadgroup leader is moved Michal Hocko
     [not found]         ` <20150521141225.GB14475-2MMpYkNvuYDjFM9bn6wA6Q@public.gmane.org>
2015-05-21 22:09           ` Tejun Heo
2015-05-18 19:49   ` [PATCH 4/7] cgroup, memcg, cpuset: implement cgroup_taskset_for_each_leader() Tejun Heo
2015-05-18 19:49   ` [PATCH 5/7] reorder cgroup_migrate()'s parameters Tejun Heo
2015-05-18 19:49   ` [PATCH 6/7] cgroup: separate out taskset operations from cgroup_migrate() Tejun Heo
2015-05-18 19:49   ` [PATCH 7/7] cgroup: make cgroup_update_dfl_csses() migrate all target processes atomically Tejun Heo
2015-05-19  6:57 ` [PATCHSET cgroup/for-4.2] cgroup: make multi-process migration atomic Zefan Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150522182115.GB26770@redhat.com \
    --to=oleg-h+wxahxf7alqt0dzr+alfa@public.gmane.org \
    --cc=cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org \
    --cc=linux-mm-Bw31MaZKKs3YtjvyW6yDsg@public.gmane.org \
    --cc=lizefan-hv44wF8Li93QT0dZR+AlfA@public.gmane.org \
    --cc=mhocko-AlSwsSmVLrQ@public.gmane.org \
    --cc=tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).