cgroup: Simplify code in css_set_move_task

Message ID 20221020074701.84326-1-haifeng.xu@shopee.com
State New
Headers
Series cgroup: Simplify code in css_set_move_task |

Commit Message

Haifeng Xu Oct. 20, 2022, 7:47 a.m. UTC
  Check the populated state of css_set in css_set_update_populated
and update the populated state of to_cset after from_cset is updated.

Signed-off-by: haifeng.xu <haifeng.xu@shopee.com>
---
 kernel/cgroup/cgroup.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
  

Comments

Michal Koutný Oct. 27, 2022, 8:05 a.m. UTC | #1
Hello.

On Thu, Oct 20, 2022 at 07:47:01AM +0000, "haifeng.xu" <haifeng.xu@shopee.com> wrote:
> -	lockdep_assert_held(&css_set_lock);
> +	if (!cset || css_set_populated(cset))
> +		return;

1) the guard should be css_set_populated() ^ populated (when unsetting
the populated trait)

2) I'd keep the lockdep_assert_held() after it anyway.

Also the commit message should explain what's the reason to move
css_set_populated() after css_set_move_task().


Thanks,
Michal
  
Haifeng Xu Oct. 28, 2022, 11:29 a.m. UTC | #2
On 2022/10/27 16:05, Michal Koutný wrote:
> Hello.
> 
> On Thu, Oct 20, 2022 at 07:47:01AM +0000, "haifeng.xu" <haifeng.xu@shopee.com> wrote:
>> -	lockdep_assert_held(&css_set_lock);
>> +	if (!cset || cset is either getting the first task or losing the last(cset))
>> +		return;
> 
> 1) the guard should be css_set_populated() ^ populated (when unsetting
> the populated trait)
> 
> 2) I'd keep the lockdep_assert_held() after it anyway.
> 
> Also the commit message should explain what's the reason to move
> css_set_populated() after css_set_move_task().
> 
> 
> Thanks,
> Michal
Hi, Michal.

1) If calls 'css_set_update_populated' , the cset is either getting the
first task or losing the last. There is a need to update the populated
state of the cset only when 'css_set_populated' returns false.
In other words, the last has been deleted from from_cset and the first
hasn't been added to to_cset yet.

2) Thanks for your suggestion. I'll keep 'lockdep_assert_held'.

3) In order to update the populated state of to_cset the same way
from_cset does, 'css_set_update_populated' is also invoked during the
process of moving a task to to_cset.

Thanks,
Haifeng
  
Michal Koutný Oct. 31, 2022, 1:11 p.m. UTC | #3
Hello.

> 1) If calls 'css_set_update_populated' , the cset is either getting the
> first task or losing the last. There is a need to update the populated
> state of the cset only when 'css_set_populated' returns false.
> In other words, the last has been deleted from from_cset and the first
> hasn't been added to to_cset yet.

I've likely misread the condition previously. I see how this works now
(update happens after "from_cset" but before "to_cset" migration).

> 3) In order to update the populated state of to_cset the same way
> from_cset does, 'css_set_update_populated' is also invoked during the
> process of moving a task to to_cset.

As I think more about this in the context of vertical migrations
(ancestor<->descendant, such as during controller dis- or enablement),
I'm afraid the inverted order would lead to "spurious" emptiness
notifications in ancestors (in the case a there is just a single task
that migrates parent->child, parent/cgroup.populated would generate and
event that'd be nullified by the subsequent population of the child).

So I'm not sure the change is worth it.

Michal
  
Haifeng Xu Nov. 3, 2022, 2:13 a.m. UTC | #4
On 2022/10/31 21:11, Michal Koutný wrote:
> Hello.
> 
>> 1) If calls 'css_set_update_populated' , the cset is either getting the
>> first task or losing the last. There is a need to update the populated
>> state of the cset only when 'css_set_populated' returns false.
>> In other words, the last has been deleted from from_cset and the first
>> hasn't been added to to_cset yet.
> 
> I've likely misread the condition previously. I see how this works now
> (update happens after "from_cset" but before "to_cset" migration).
> 
>> 3) In order to update the populated state of to_cset the same way
>> from_cset does, 'css_set_update_populated' is also invoked during the
>> process of moving a task to to_cset.
> 
> As I think more about this in the context of vertical migrations
> (ancestor<->descendant, such as during controller dis- or enablement),
> I'm afraid the inverted order would lead to "spurious" emptiness
> notifications in ancestors (in the case a there is just a single task
> that migrates parent->child, parent/cgroup.populated would generate and
> event that'd be nullified by the subsequent population of the child).
Hi, Michal.

I understand your worries. Can I just check the populated state of
css_set in 'css_set_update_populated' and don't change the order any
more? I think it can also streamline 'css_set_move_task' a bit.

Thanks,
Hiafeng.
> 
> So I'm not sure the change is worth it.
> 
> Michal
  
Tejun Heo Nov. 3, 2022, 2:31 a.m. UTC | #5
On Thu, Nov 03, 2022 at 10:13:22AM +0800, Haifeng Xu wrote:
> I understand your worries. Can I just check the populated state of
> css_set in 'css_set_update_populated' and don't change the order any
> more? I think it can also streamline 'css_set_move_task' a bit.

FWIW, I don't see much value in the proposed change. The resulting code
isn't better in any noticeable way. Even if the change were straightforward,
the value of the patch would seem questionable. There's no point in creating
code churns like this. Nothing is improved in any material way while
creating completely unnecessary risk for subtle breakages.

Thanks.
  
Haifeng Xu Nov. 3, 2022, 2:55 p.m. UTC | #6
On 2022/11/3 10:31, Tejun Heo wrote:
> On Thu, Nov 03, 2022 at 10:13:22AM +0800, Haifeng Xu wrote:
>> I understand your worries. Can I just check the populated state of
>> css_set in 'css_set_update_populated' and don't change the order any
>> more? I think it can also streamline 'css_set_move_task' a bit.
> 
> FWIW, I don't see much value in the proposed change. The resulting code
> isn't better in any noticeable way. Even if the change were straightforward,
> the value of the patch would seem questionable. There's no point in creating
> code churns like this. Nothing is improved in any material way while
> creating completely unnecessary risk for subtle breakages.
> 
> Thanks.
> 
Got it, thanks.
  

Patch

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index d922773fa90b..6c474be57f91 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -861,7 +861,8 @@  static void css_set_update_populated(struct css_set *cset, bool populated)
 {
 	struct cgrp_cset_link *link;
 
-	lockdep_assert_held(&css_set_lock);
+	if (!cset || css_set_populated(cset))
+		return;
 
 	list_for_each_entry(link, &cset->cgrp_links, cgrp_link)
 		cgroup_update_populated(link->cgrp, populated);
@@ -903,16 +904,12 @@  static void css_set_move_task(struct task_struct *task,
 {
 	lockdep_assert_held(&css_set_lock);
 
-	if (to_cset && !css_set_populated(to_cset))
-		css_set_update_populated(to_cset, true);
-
 	if (from_cset) {
 		WARN_ON_ONCE(list_empty(&task->cg_list));
 
 		css_set_skip_task_iters(from_cset, task);
 		list_del_init(&task->cg_list);
-		if (!css_set_populated(from_cset))
-			css_set_update_populated(from_cset, false);
+		css_set_update_populated(from_cset, false);
 	} else {
 		WARN_ON_ONCE(!list_empty(&task->cg_list));
 	}
@@ -926,6 +923,7 @@  static void css_set_move_task(struct task_struct *task,
 		WARN_ON_ONCE(task->flags & PF_EXITING);
 
 		cgroup_move_task(task, to_cset);
+		css_set_update_populated(to_cset, true);
 		list_add_tail(&task->cg_list, use_mg_tasks ? &to_cset->mg_tasks :
 							     &to_cset->tasks);
 	}