Skip to contentSkip to conversation

hugetlb/cgroup: Simplify pre_destroy callback

PATCH In review Aneesh Kumar K.V 6 participants 9 messages Jul 18, 2012 – Jul 19, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hugetlb/cgroup: Simplify pre_destroy callback

From: "Aneesh Kumar K.V" <[email protected]>

Since we cannot fail in hugetlb_cgroup_move_parent, we don't really need to check whether cgroup have any change left after that. Also skip those hstates for which we don't have any charge in this cgroup.

Based on an earlier patch from Wanpeng Li <[email protected]>

---
 mm/hugetlb_cgroup.c |   49 +++++++++++++++++++++----------------------------
 1 file changed, 21 insertions(+), 28 deletions(-)
mm/hugetlb_cgroup.c +21 -28
index b834e8d..b355fb4 100644
@@ -65,18 +65,6 @@ static inline struct hugetlb_cgroup *parent_hugetlb_cgroup(struct cgroup *cg)
 	return hugetlb_cgroup_from_cgroup(cg->parent);
 }
 
-static inline bool hugetlb_cgroup_have_usage(struct cgroup *cg)
-{
-	int idx;
-	struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cg);
-
-	for (idx = 0; idx < hugetlb_max_hstate; idx++) {
-		if ((res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE)) > 0)
-			return true;
-	}
-	return false;
-}
-
 static struct cgroup_subsys_state *hugetlb_cgroup_create(struct cgroup *cgroup)
 {
 	int idx;
@@ -159,24 +147,29 @@ static int hugetlb_cgroup_pre_destroy(struct cgroup *cgroup)
 {
 	struct hstate *h;
 	struct page *page;
-	int ret = 0, idx = 0;
+	int ret = 0, idx;
+	struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
 
-	do {
-		if (cgroup_task_count(cgroup) ||
-		    !list_empty(&cgroup->children)) {
-			ret = -EBUSY;
-			goto out;
-		}
-		for_each_hstate(h) {
-			spin_lock(&hugetlb_lock);
-			list_for_each_entry(page, &h->hugepage_activelist, lru)
-				hugetlb_cgroup_move_parent(idx, cgroup, page);
 
-			spin_unlock(&hugetlb_lock);
-			idx++;
-		}
-		cond_resched();
-	} while (hugetlb_cgroup_have_usage(cgroup));
+	if (cgroup_task_count(cgroup) ||
+	    !list_empty(&cgroup->children)) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	for_each_hstate(h) {
+		/*
+		 * if we don't have any charge, skip this hstate
+		 */
+		idx = hstate_index(h);
+		if (res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE) == 0)
+			continue;
+		spin_lock(&hugetlb_lock);
+		list_for_each_entry(page, &h->hugepage_activelist, lru)
+			hugetlb_cgroup_move_parent(idx, cgroup, page);
+		spin_unlock(&hugetlb_lock);
+		VM_BUG_ON(res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE));
+	}
 out:
 	return ret;
 }
Signature · 7 lines
1.7.10

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 8 lines · Aneesh Kumar K.V

From: "Aneesh Kumar K.V" <[email protected]>

Since we cannot fail in hugetlb_cgroup_move_parent, we don't really need to check whether cgroup have any change left after that. Also skip those hstates for which we don't have any charge in this cgroup.

Based on an earlier patch from Wanpeng Li <[email protected]>

Quoted 71 lines

---
mm/hugetlb_cgroup.c | 49 +++++++++++++++++++++----------------------------

1 file changed, 21 insertions(+), 28 deletions(-)
mm/hugetlb_cgroup.c +21 -28
index b834e8d..b355fb4 100644
@@ -65,18 +65,6 @@ static inline struct hugetlb_cgroup *parent_hugetlb_cgroup(struct cgroup *cg)
 	return hugetlb_cgroup_from_cgroup(cg->parent);
 }
 
-static inline bool hugetlb_cgroup_have_usage(struct cgroup *cg)
-{
-	int idx;
-	struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cg);
-
-	for (idx = 0; idx < hugetlb_max_hstate; idx++) {
-		if ((res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE)) > 0)
-			return true;
-	}
-	return false;
-}
-
 static struct cgroup_subsys_state *hugetlb_cgroup_create(struct cgroup *cgroup)
 {
 	int idx;
@@ -159,24 +147,29 @@ static int hugetlb_cgroup_pre_destroy(struct cgroup *cgroup)
 {
 	struct hstate *h;
 	struct page *page;
-	int ret = 0, idx = 0;
+	int ret = 0, idx;
+	struct hugetlb_cgroup *h_cg = hugetlb_cgroup_from_cgroup(cgroup);
 
-	do {
-		if (cgroup_task_count(cgroup) ||
-		    !list_empty(&cgroup->children)) {
-			ret = -EBUSY;
-			goto out;
-		}
-		for_each_hstate(h) {
-			spin_lock(&hugetlb_lock);
-			list_for_each_entry(page, &h->hugepage_activelist, lru)
-				hugetlb_cgroup_move_parent(idx, cgroup, page);
 
-			spin_unlock(&hugetlb_lock);
-			idx++;
-		}
-		cond_resched();
-	} while (hugetlb_cgroup_have_usage(cgroup));
+	if (cgroup_task_count(cgroup) ||
+	    !list_empty(&cgroup->children)) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	for_each_hstate(h) {
+		/*
+		 * if we don't have any charge, skip this hstate
+		 */
+		idx = hstate_index(h);
+		if (res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE) == 0)
+			continue;
+		spin_lock(&hugetlb_lock);
+		list_for_each_entry(page, &h->hugepage_activelist, lru)
+			hugetlb_cgroup_move_parent(idx, cgroup, page);
+		spin_unlock(&hugetlb_lock);
+		VM_BUG_ON(res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE));
+	}

out:

Quoted 10 lines
	return ret;
}
Signature · 7 lines
1.7.10

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#2 lore raw
In reply to Aneesh Kumar K.V
Quoted 14 lines · On Wed, 18 Jul 2012 11:04:09 +0530 "Aneesh Kumar K.V"…

From: "Aneesh Kumar K.V" <[email protected]>

Since we cannot fail in hugetlb_cgroup_move_parent, we don't really need to check whether cgroup have any change left after that. Also skip those hstates for which we don't have any charge in this cgroup.

...

mm/hugetlb_cgroup.c +6 -0
+	for_each_hstate(h) {
+		/*
+		 * if we don't have any charge, skip this hstate
+		 */
+		idx = hstate_index(h);
+		if (res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE) == 0)

mm/hugetlb_cgroup.c static int hugetlb_cgroup_pre_destroy(struct cgroup *cgroup) · line 175

Quoted 10 lines
mm/hugetlb_cgroup.c +7 -0
+			continue;
+		spin_lock(&hugetlb_lock);
+		list_for_each_entry(page, &h->hugepage_activelist, lru)
+			hugetlb_cgroup_move_parent(idx, cgroup, page);
+		spin_unlock(&hugetlb_lock);
+		VM_BUG_ON(res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE));
+	}
 out:
 	return ret;
 }

This looks fishy.

We test RES_USAGE before taking hugetlb_lock. What prevents some other thread from increasing RES_USAGE after that test?

After walking the list we test RES_USAGE after dropping hugetlb_lock. What prevents another thread from incrementing RES_USAGE before that test, triggering the BUG?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 30 lines · Andrew Morton
Quoted 14 lines · On Wed, 18 Jul 2012 11:04:09 +0530 "Aneesh Kumar K.V"…

From: "Aneesh Kumar K.V" <[email protected]>

Since we cannot fail in hugetlb_cgroup_move_parent, we don't really need to check whether cgroup have any change left after that. Also skip those hstates for which we don't have any charge in this cgroup.

...

mm/hugetlb_cgroup.c +6 -0
+	for_each_hstate(h) {
+		/*
+		 * if we don't have any charge, skip this hstate
+		 */
+		idx = hstate_index(h);
+		if (res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE) == 0)

mm/hugetlb_cgroup.c static int hugetlb_cgroup_pre_destroy(struct cgroup *cgroup) · line 175

Quoted 10 lines
mm/hugetlb_cgroup.c +7 -0
+			continue;
+		spin_lock(&hugetlb_lock);
+		list_for_each_entry(page, &h->hugepage_activelist, lru)
+			hugetlb_cgroup_move_parent(idx, cgroup, page);
+		spin_unlock(&hugetlb_lock);
+		VM_BUG_ON(res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE));
+	}
 out:
 	return ret;
 }

This looks fishy.

Quoted 6 lines

We test RES_USAGE before taking hugetlb_lock. What prevents some other thread from increasing RES_USAGE after that test?

After walking the list we test RES_USAGE after dropping hugetlb_lock. What prevents another thread from incrementing RES_USAGE before that test, triggering the BUG?

IIUC core cgroup will prevent a new task getting added to the cgroup when we are in pre_destroy. Since we already check that the cgroup doesn't have any task, the RES_USAGE cannot increase in pre_destroy.

-aneesh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 35 lines · Aneesh Kumar K.V
Quoted 33 lines · Andrew Morton
Quoted 14 lines · On Wed, 18 Jul 2012 11:04:09 +0530 "Aneesh Kumar K.V"…

From: "Aneesh Kumar K.V" <[email protected]>

Since we cannot fail in hugetlb_cgroup_move_parent, we don't really need to check whether cgroup have any change left after that. Also skip those hstates for which we don't have any charge in this cgroup.

...

mm/hugetlb_cgroup.c +6 -0
+	for_each_hstate(h) {
+		/*
+		 * if we don't have any charge, skip this hstate
+		 */
+		idx = hstate_index(h);
+		if (res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE) == 0)

mm/hugetlb_cgroup.c static int hugetlb_cgroup_pre_destroy(struct cgroup *cgroup) · line 175

Quoted 10 lines
mm/hugetlb_cgroup.c +7 -0
+			continue;
+		spin_lock(&hugetlb_lock);
+		list_for_each_entry(page, &h->hugepage_activelist, lru)
+			hugetlb_cgroup_move_parent(idx, cgroup, page);
+		spin_unlock(&hugetlb_lock);
+		VM_BUG_ON(res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE));
+	}
 out:
 	return ret;
 }

This looks fishy.

We test RES_USAGE before taking hugetlb_lock. What prevents some other thread from increasing RES_USAGE after that test?

Quoted 8 lines
Quoted 3 lines

After walking the list we test RES_USAGE after dropping hugetlb_lock. What prevents another thread from incrementing RES_USAGE before that test, triggering the BUG?

IIUC core cgroup will prevent a new task getting added to the cgroup when we are in pre_destroy. Since we already check that the cgroup doesn't have any task, the RES_USAGE cannot increase in pre_destroy.

You're wrong here. We release cgroup_lock before calling pre_destroy and retrieve the lock after that, so a task can be attached to the cgroup in this interval.

See 3fa59dfbc3b223f02c26593be69ce6fc9a940405 ("cgroup: fix potential deadlock in pre_destroy")

But I think the memcg->pre_destroy has been reworked and now we can safely hold cgroup_lock when calling the callback, and this can make the code a bit simpler.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#5 lore raw
Quoted 41 lines · Li Zefan
Quoted 39 lines · Aneesh Kumar K.V
Quoted 36 lines · Andrew Morton
Quoted 14 lines · On Wed, 18 Jul 2012 11:04:09 +0530 "Aneesh Kumar K.V"…

From: "Aneesh Kumar K.V" <[email protected]>

Since we cannot fail in hugetlb_cgroup_move_parent, we don't really need to check whether cgroup have any change left after that. Also skip those hstates for which we don't have any charge in this cgroup.

...

mm/hugetlb_cgroup.c +6 -0
+	for_each_hstate(h) {
+		/*
+		 * if we don't have any charge, skip this hstate
+		 */
+		idx = hstate_index(h);
+		if (res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE) == 0)

mm/hugetlb_cgroup.c static int hugetlb_cgroup_pre_destroy(struct cgroup *cgroup) · line 175

Quoted 10 lines
mm/hugetlb_cgroup.c +7 -0
+			continue;
+		spin_lock(&hugetlb_lock);
+		list_for_each_entry(page, &h->hugepage_activelist, lru)
+			hugetlb_cgroup_move_parent(idx, cgroup, page);
+		spin_unlock(&hugetlb_lock);
+		VM_BUG_ON(res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE));
+	}
 out:
 	return ret;
 }

This looks fishy.

We test RES_USAGE before taking hugetlb_lock. What prevents some other thread from increasing RES_USAGE after that test?

After walking the list we test RES_USAGE after dropping hugetlb_lock. What prevents another thread from incrementing RES_USAGE before that test, triggering the BUG?

Quoted 9 lines
Quoted 4 lines

IIUC core cgroup will prevent a new task getting added to the cgroup when we are in pre_destroy. Since we already check that the cgroup doesn't have any task, the RES_USAGE cannot increase in pre_destroy.

You're wrong here. We release cgroup_lock before calling pre_destroy and retrieve the lock after that, so a task can be attached to the cgroup in this interval.

But that means rmdir can be racy right ? What happens if the task got added, allocated few pages and then moved out ? We still would have task count 0 but few pages, which we missed to to move to parent cgroup.

-aneesh

#6 lore raw
Quoted 49 lines · Aneesh Kumar K.V
Quoted 47 lines · Li Zefan
Quoted 43 lines · Aneesh Kumar K.V
Quoted 30 lines · Andrew Morton
Quoted 14 lines · On Wed, 18 Jul 2012 11:04:09 +0530 "Aneesh Kumar K.V"…

From: "Aneesh Kumar K.V" <[email protected]>

Since we cannot fail in hugetlb_cgroup_move_parent, we don't really need to check whether cgroup have any change left after that. Also skip those hstates for which we don't have any charge in this cgroup.

...

mm/hugetlb_cgroup.c +6 -0
+	for_each_hstate(h) {
+		/*
+		 * if we don't have any charge, skip this hstate
+		 */
+		idx = hstate_index(h);
+		if (res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE) == 0)

mm/hugetlb_cgroup.c static int hugetlb_cgroup_pre_destroy(struct cgroup *cgroup) · line 175

Quoted 10 lines
mm/hugetlb_cgroup.c +7 -0
+			continue;
+		spin_lock(&hugetlb_lock);
+		list_for_each_entry(page, &h->hugepage_activelist, lru)
+			hugetlb_cgroup_move_parent(idx, cgroup, page);
+		spin_unlock(&hugetlb_lock);
+		VM_BUG_ON(res_counter_read_u64(&h_cg->hugepage[idx], RES_USAGE));
+	}
  out:
  	return ret;
  }

This looks fishy.

Quoted 6 lines

We test RES_USAGE before taking hugetlb_lock. What prevents some other thread from increasing RES_USAGE after that test?

After walking the list we test RES_USAGE after dropping hugetlb_lock. What prevents another thread from incrementing RES_USAGE before that test, triggering the BUG?

IIUC core cgroup will prevent a new task getting added to the cgroup when we are in pre_destroy. Since we already check that the cgroup doesn't have any task, the RES_USAGE cannot increase in pre_destroy.

Quoted 8 lines
Quoted 3 lines

You're wrong here. We release cgroup_lock before calling pre_destroy and retrieve the lock after that, so a task can be attached to the cgroup in this interval.

But that means rmdir can be racy right ? What happens if the task got added, allocated few pages and then moved out ? We still would have task count 0 but few pages, which we missed to to move to parent cgroup.

That's a problem even if it's verrrry unlikely. I'd like to look into it and fix the race in cgroup layer. But I'm sorry I'm a bit busy in these days...

Thanks,
-Kame

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
Quoted 18 lines · Kamezawa Hiroyuki
Quoted 18 lines
Quoted 17 lines
Quoted 4 lines
Quoted 4 lines

We test RES_USAGE before taking hugetlb_lock. What prevents some other thread from increasing RES_USAGE after that test?

Quoted 8 lines
Quoted 3 lines

After walking the list we test RES_USAGE after dropping hugetlb_lock. What prevents another thread from incrementing RES_USAGE before that test, triggering the BUG?

IIUC core cgroup will prevent a new task getting added to the cgroup when we are in pre_destroy. Since we already check that the cgroup doesn't have any task, the RES_USAGE cannot increase in pre_destroy.

You're wrong here. We release cgroup_lock before calling pre_destroy and retrieve the lock after that, so a task can be attached to the cgroup in this interval.

Quoted 9 lines
Quoted 4 lines

But that means rmdir can be racy right ? What happens if the task got added, allocated few pages and then moved out ? We still would have task count 0 but few pages, which we missed to to move to parent cgroup.

That's a problem even if it's verrrry unlikely. I'd like to look into it and fix the race in cgroup layer. But I'm sorry I'm a bit busy in these days...

How about moving that mutex_unlock(&cgroup_mutex) to memcg callback ? That can be a patch for 3.5 ?

-aneesh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
Quoted 25 lines · Aneesh Kumar K.V
Quoted 23 lines · Kamezawa Hiroyuki
Quoted 22 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

We test RES_USAGE before taking hugetlb_lock. What prevents some other thread from increasing RES_USAGE after that test?

After walking the list we test RES_USAGE after dropping hugetlb_lock. What prevents another thread from incrementing RES_USAGE before that test, triggering the BUG?

Quoted 9 lines
Quoted 4 lines

IIUC core cgroup will prevent a new task getting added to the cgroup when we are in pre_destroy. Since we already check that the cgroup doesn't have any task, the RES_USAGE cannot increase in pre_destroy.

You're wrong here. We release cgroup_lock before calling pre_destroy and retrieve the lock after that, so a task can be attached to the cgroup in this interval.

But that means rmdir can be racy right ? What happens if the task got added, allocated few pages and then moved out ? We still would have task count 0 but few pages, which we missed to to move to parent cgroup.

Quoted 7 lines
Quoted 4 lines

That's a problem even if it's verrrry unlikely. I'd like to look into it and fix the race in cgroup layer. But I'm sorry I'm a bit busy in these days...

How about moving that mutex_unlock(&cgroup_mutex) to memcg callback ? That can be a patch for 3.5 ?

Bahh, I have just posted a follow up on mm-commits email exactly about this. Sorry I have missed that the discussion is still ongoing. I have posted also something I guess should help. Can we follow up on that one or should I post the patch here as well?

Quoted 4 lines

-aneesh

Signature · 12 lines
Michal Hocko
SUSE Labs
SUSE LINUX s.r.o.
Lihovarska 1060/12
190 00 Praha 9    
Czech Republic

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>