All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup: Return if dst_cgrp equals to src_cgrp
@ 2022-05-20 17:02 ` shisiyuan
  0 siblings, 0 replies; 5+ messages in thread
From: shisiyuan @ 2022-05-20 17:02 UTC (permalink / raw)
  Cc: shisiyuan, Tejun Heo, Li Zefan, Johannes Weiner, cgroups, linux-kernel

In function cgroup_migrate_add_src(), if dst_cgrp
equals to src_cgroup which the tasks link, dont
go on migrating tasks to another css_set.
This can save the cost of unnecessary migration.

Signed-off-by: shisiyuan <shisiyuan@xiaomi.com>
---
 kernel/cgroup/cgroup.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 6139460..97d7f68 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2634,6 +2634,9 @@ void cgroup_migrate_add_src(struct css_set *src_cset,
 
 	src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
 
+	if (src_cgrp == dst_cgrp)
+		return;
+
 	if (!list_empty(&src_cset->mg_preload_node))
 		return;
 
@@ -2780,6 +2783,9 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
 	rcu_read_unlock();
 	spin_unlock_irq(&css_set_lock);
 
+	if (list_empty(&mgctx.preloaded_src_csets))
+		return ret;
+
 	/* prepare dst csets and commit */
 	ret = cgroup_migrate_prepare_dst(&mgctx);
 	if (!ret)
@@ -2927,7 +2933,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 	struct cgroup_subsys_state *d_css;
 	struct cgroup *dsct;
 	struct css_set *src_cset;
-	int ret;
+	int ret = 0;
 
 	lockdep_assert_held(&cgroup_mutex);
 
@@ -2943,6 +2949,9 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 	}
 	spin_unlock_irq(&css_set_lock);
 
+	if (list_empty(&mgctx.preloaded_src_csets))
+		goto out_finish;
+
 	/* NULL dst indicates self on default hierarchy */
 	ret = cgroup_migrate_prepare_dst(&mgctx);
 	if (ret)
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH] cgroup: Return if dst_cgrp equals to src_cgrp
@ 2022-05-20 17:02 ` shisiyuan
  0 siblings, 0 replies; 5+ messages in thread
From: shisiyuan @ 2022-05-20 17:02 UTC (permalink / raw)
  Cc: shisiyuan, Tejun Heo, Li Zefan, Johannes Weiner, cgroups, linux-kernel

In function cgroup_migrate_add_src(), if dst_cgrp
equals to src_cgroup which the tasks link, dont
go on migrating tasks to another css_set.
This can save the cost of unnecessary migration.

Signed-off-by: shisiyuan <shisiyuan@xiaomi.com>
---
 kernel/cgroup/cgroup.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index 6139460..97d7f68 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2634,6 +2634,9 @@ void cgroup_migrate_add_src(struct css_set *src_cset,
 
 	src_cgrp = cset_cgroup_from_root(src_cset, dst_cgrp->root);
 
+	if (src_cgrp == dst_cgrp)
+		return;
+
 	if (!list_empty(&src_cset->mg_preload_node))
 		return;
 
@@ -2780,6 +2783,9 @@ int cgroup_attach_task(struct cgroup *dst_cgrp, struct task_struct *leader,
 	rcu_read_unlock();
 	spin_unlock_irq(&css_set_lock);
 
+	if (list_empty(&mgctx.preloaded_src_csets))
+		return ret;
+
 	/* prepare dst csets and commit */
 	ret = cgroup_migrate_prepare_dst(&mgctx);
 	if (!ret)
@@ -2927,7 +2933,7 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 	struct cgroup_subsys_state *d_css;
 	struct cgroup *dsct;
 	struct css_set *src_cset;
-	int ret;
+	int ret = 0;
 
 	lockdep_assert_held(&cgroup_mutex);
 
@@ -2943,6 +2949,9 @@ static int cgroup_update_dfl_csses(struct cgroup *cgrp)
 	}
 	spin_unlock_irq(&css_set_lock);
 
+	if (list_empty(&mgctx.preloaded_src_csets))
+		goto out_finish;
+
 	/* NULL dst indicates self on default hierarchy */
 	ret = cgroup_migrate_prepare_dst(&mgctx);
 	if (ret)
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] cgroup: Return if dst_cgrp equals to src_cgrp
  2022-05-20 17:02 ` shisiyuan
  (?)
@ 2022-05-25 22:51 ` Michal Koutný
       [not found]   ` <CAC=y0ud7oPtFqj=dqBSicoWwoN0knAwA6svidiYjbFn9BJMR3w@mail.gmail.com>
  -1 siblings, 1 reply; 5+ messages in thread
From: Michal Koutný @ 2022-05-25 22:51 UTC (permalink / raw)
  To: shisiyuan19870131
  Cc: Tejun Heo, Li Zefan, Johannes Weiner, cgroups, linux-kernel

Hello.

On Sat, May 21, 2022 at 01:02:00AM +0800, shisiyuan <shisiyuan19870131@gmail.com> wrote:
> In function cgroup_migrate_add_src(), if dst_cgrp
> equals to src_cgroup which the tasks link, dont
> go on migrating tasks to another css_set.

Do you have an example which operations lead to this situation?

> This can save the cost of unnecessary migration.

How much are the savings visible?

(Note, I suspect the shortcut on `src_cgrp == dst_cgrp` might not be
always correct when called from cgroup_update_dfl_csses() but not 100%
sure now.)

Thanks,
Michal

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cgroup: Return if dst_cgrp equals to src_cgrp
@ 2022-05-26 11:22       ` Michal Koutný
  0 siblings, 0 replies; 5+ messages in thread
From: Michal Koutný @ 2022-05-26 11:22 UTC (permalink / raw)
  To: 史思远
  Cc: Tejun Heo, Li Zefan, Johannes Weiner, cgroups, linux-kernel

On Thu, May 26, 2022 at 12:40:17PM +0800, 史思远 <shisiyuan19870131@gmail.com> wrote:
> echo 1413 >  /dev/cpuset/foreground/cgroup.procs
> echo 1413 >  /dev/cpuset/foreground/cgroup.procs
> echo 1413 >  /dev/cpuset/foreground/cgroup.procs

Thank you.

> In this situation, since the second echo, dst_cgrp equals to src_cgrp(both
> cgroups are the 'foreground' cpuset).
> cgroup1_procs_write()-> __cgroup1_procs_write()
> -> cgrp = cgroup_kn_lock_live()
> -> task = cgroup_procs_write_start(buf...)
> -> cgroup_attach_task(cgrp, task, threadgroup);
>   -> 1) cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);  //
> detect the (src_cgrp == dst_cgrp), don't need to continue the following
> migration flow.
>   -> 2) cgroup_migrate_prepare_dst(&mgctx)

Here, I'd like to let you know about [1]

>   -> 3) cgroup_migrate(leader, threadgroup, &mgctx)
>   -> 4) cgroup_migrate_finish(&mgctx) // cleanup
> -> cgroup_procs_write_finish();  // cleanup


> >> The main purpose for this patch is to handle the situation above, and
> just noticed that cgroup_update_dfl_csses() also
> calls cgroup_migrate_add_src(). So I assume it's a similar situation. We
> can discuss more about it.

So I think the patch doesn't save that much and identical css_set is
better condition to check on (covers cgroup_update_dfl_csses too).

Michal

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/cgroup/cgroup.c?h=v5.18#n2710

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] cgroup: Return if dst_cgrp equals to src_cgrp
@ 2022-05-26 11:22       ` Michal Koutný
  0 siblings, 0 replies; 5+ messages in thread
From: Michal Koutný @ 2022-05-26 11:22 UTC (permalink / raw)
  To: 史思远
  Cc: Tejun Heo, Li Zefan, Johannes Weiner,
	cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA

On Thu, May 26, 2022 at 12:40:17PM +0800, 史思远 <shisiyuan19870131-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> echo 1413 >  /dev/cpuset/foreground/cgroup.procs
> echo 1413 >  /dev/cpuset/foreground/cgroup.procs
> echo 1413 >  /dev/cpuset/foreground/cgroup.procs

Thank you.

> In this situation, since the second echo, dst_cgrp equals to src_cgrp(both
> cgroups are the 'foreground' cpuset).
> cgroup1_procs_write()-> __cgroup1_procs_write()
> -> cgrp = cgroup_kn_lock_live()
> -> task = cgroup_procs_write_start(buf...)
> -> cgroup_attach_task(cgrp, task, threadgroup);
>   -> 1) cgroup_migrate_add_src(task_css_set(task), dst_cgrp, &mgctx);  //
> detect the (src_cgrp == dst_cgrp), don't need to continue the following
> migration flow.
>   -> 2) cgroup_migrate_prepare_dst(&mgctx)

Here, I'd like to let you know about [1]

>   -> 3) cgroup_migrate(leader, threadgroup, &mgctx)
>   -> 4) cgroup_migrate_finish(&mgctx) // cleanup
> -> cgroup_procs_write_finish();  // cleanup


> >> The main purpose for this patch is to handle the situation above, and
> just noticed that cgroup_update_dfl_csses() also
> calls cgroup_migrate_add_src(). So I assume it's a similar situation. We
> can discuss more about it.

So I think the patch doesn't save that much and identical css_set is
better condition to check on (covers cgroup_update_dfl_csses too).

Michal

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/kernel/cgroup/cgroup.c?h=v5.18#n2710

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-05-26 11:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20 17:02 [PATCH] cgroup: Return if dst_cgrp equals to src_cgrp shisiyuan
2022-05-20 17:02 ` shisiyuan
2022-05-25 22:51 ` Michal Koutný
     [not found]   ` <CAC=y0ud7oPtFqj=dqBSicoWwoN0knAwA6svidiYjbFn9BJMR3w@mail.gmail.com>
2022-05-26 11:22     ` Michal Koutný
2022-05-26 11:22       ` Michal Koutný

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.