Skip to contentSkip to conversation

sched/topology: remove unneeded do while loop in cpu_attach_domain()

PATCH In review Miaohe Lin 4 participants 8 messages Jun 17, 2023 – Jul 12, 2023 Scheduler

lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 sched/topology: remove unneeded do while loop in cpu_attach_domain()

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

---
 kernel/sched/topology.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/topology.c +1 -4
index ca4472281c28..9010c93c3fdb 100644
@@ -750,10 +750,7 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 			 * domain for convenience. Clear such flags since
 			 * the child is being destroyed.
 			 */
-			do {
-				sg->flags = 0;
-			} while (sg != sd->groups);
-
+			sg->flags = 0;
 			sd->child = NULL;
 		}
 	}
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Miaohe Lin

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

---
 kernel/sched/topology.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/topology.c +0 -0
index ca4472281c28..9010c93c3fdb 100644

kernel/sched/topology.c cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) · line 754

Quoted 5 lines
kernel/sched/topology.c +0 -2
@@ -750,10 +750,7 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 			 * domain for convenience. Clear such flags since
 			 * the child is being destroyed.
 			 */
-			do {
-				sg->flags = 0;

I guess we don't need to clear the flag of remote groups for now.

Quoted 9 lines
kernel/sched/topology.c +1 -2
-			} while (sg != sd->groups);
-
+			sg->flags = 0;
 			sd->child = NULL;
 		}
 	}
Signature · 1 line
2.27.0

thanks,
Chenyu

maintainer
#2 lore raw
In reply to Miaohe Lin
Quoted 3 lines · Miaohe Lin

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

This Changelog makes no sense to me.. Yes, as is the do {} while loop is dead code, but it *should* have read like:

	do {
		sg->flags = 0;
		sg = sg->next;
	} while (sg != sd->groups);

as I noted here:

https://lore.kernel.org/all/[email protected]/T/#u

So what this changelog should argue is how there cannot be multiple groups here -- or if there can be, add the missing iteration.

Quoted 24 lines
---
 kernel/sched/topology.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
kernel/sched/topology.c +1 -4
index ca4472281c28..9010c93c3fdb 100644
@@ -750,10 +750,7 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 			 * domain for convenience. Clear such flags since
 			 * the child is being destroyed.
 			 */
-			do {
-				sg->flags = 0;
-			} while (sg != sd->groups);
-
+			sg->flags = 0;
 			sd->child = NULL;
 		}
 	}
Signature · 1 line
2.27.0
author
#3 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Miaohe Lin

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

This Changelog makes no sense to me.. Yes, as is the do {} while loop is dead code, but it *should* have read like:

Quoted 8 lines
	do {
		sg->flags = 0;
		sg = sg->next;
	} while (sg != sd->groups);

as I noted here:

https://lore.kernel.org/all/[email protected]/T/#u

[1]

Quoted 3 lines

So what this changelog should argue is how there cannot be multiple groups here -- or if there can be, add the missing iteration.

[1] said:
"
Yes, I missed that.

That being said, the only reason for sd to be degenerate is that there is only 1 group. Otherwise we will keep it and degenerate parents instead
"

IOW, "sg = sg->next;" is missed intentionally in the do while{} loop to show that there's only 1 sched group. This looks weird to me but no persist on change the code.

Thanks.

#4 lore raw
Quoted 6 lines · Miaohe Lin
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Miaohe Lin

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

Quoted 7 lines
Quoted 7 lines

This Changelog makes no sense to me.. Yes, as is the do {} while loop is dead code, but it *should* have read like:

	do {
		sg->flags = 0;
		sg = sg->next;
	} while (sg != sd->groups);

Yes, I agree that this is the correct solution.

Quoted 4 lines
Quoted 4 lines

as I noted here:

https://lore.kernel.org/all/[email protected]/T/#u

I apologize. I missed this e-mail.

Quoted 7 lines

[1]

Quoted 3 lines

So what this changelog should argue is how there cannot be multiple groups here -- or if there can be, add the missing iteration.

Quoted 8 lines

[1] said:
"
Yes, I missed that.

That being said, the only reason for sd to be degenerate is that there is only 1 group. Otherwise we will keep it and degenerate parents instead
"

In the section of the code in question ,sd now points to the parent of the sched group being degenerated. Thus, it may have more than one group, and we should iterate over them to clear the flags.

Quoted 3 lines

IOW, "sg = sg->next;" is missed intentionally in the do while{} loop to show that there's only 1 sched group. This looks weird to me but no persist on change the code.

Not having sg = sg->next; is a bug, IMO.

Thanks and BR,
Ricardo

#5 lore raw
Quoted 10 lines · Ricardo Neri
Quoted 9 lines · Miaohe Lin
Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Miaohe Lin

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

This Changelog makes no sense to me.. Yes, as is the do {} while loop is dead code, but it *should* have read like:

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
	do {
		sg->flags = 0;
		sg = sg->next;
	} while (sg != sd->groups);

Yes, I agree that this is the correct solution.

I take this back. I think we should do this:

@@ -758,19 +758,14 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
sd = sd->parent;
destroy_sched_domain(tmp);
if (sd) {
- struct sched_group *sg = sd->groups;
-
/*
* sched groups hold the flags of the child sched
* domain for convenience. Clear such flags since
* the child is being destroyed.
*/
- do {
- sg->flags = 0;
- } while (sg != sd->groups);
+ sd->groups->flags = 0;

sd->child = NULL;
- }
}

sched_domain_debug(sd, cpu);

A comment from Chenyu made got me thinking that we should only clear the flags of the local group as viewed from the parent domain. This is because the domain being degenerated defines the flags of such group only.

The current code does the right thing, but in a fortuitous and ugly manner.

Thanks and BR,
Ricardo

author
#6 lore raw
Quoted 42 lines · Ricardo Neri
Quoted 10 lines · Ricardo Neri
Quoted 9 lines · Miaohe Lin
Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Miaohe Lin

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

This Changelog makes no sense to me.. Yes, as is the do {} while loop is dead code, but it *should* have read like:

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
	do {
		sg->flags = 0;
		sg = sg->next;
	} while (sg != sd->groups);

Yes, I agree that this is the correct solution.

I take this back. I think we should do this:

(quoted diff) +1 -6
@@ -758,19 +758,14 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 		sd = sd->parent;
 		destroy_sched_domain(tmp);
 		if (sd) {
-			struct sched_group *sg = sd->groups;
-
 			/*
 			 * sched groups hold the flags of the child sched
 			 * domain for convenience. Clear such flags since
 			 * the child is being destroyed.
 			 */
-			do {
-				sg->flags = 0;
-			} while (sg != sd->groups);
+			sd->groups->flags = 0;
 
 			sd->child = NULL;
-		}
 	}
 
 	sched_domain_debug(sd, cpu);
Quoted 3 lines

A comment from Chenyu made got me thinking that we should only clear the flags of the local group as viewed from the parent domain. This is because the domain being degenerated defines the flags of such group only.

This looks better to my patch. Thanks.

#7 lore raw
Quoted 41 lines · Miaohe Lin
Quoted 40 lines · Ricardo Neri
Quoted 10 lines · Ricardo Neri
Quoted 9 lines · Miaohe Lin
Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Miaohe Lin

When sg != sd->groups, the do while loop would cause deadloop here. But that won't occur because sg is always equal to sd->groups now. Remove this unneeded do while loop.

This Changelog makes no sense to me.. Yes, as is the do {} while loop is dead code, but it *should* have read like:

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
	do {
		sg->flags = 0;
		sg = sg->next;
	} while (sg != sd->groups);

Yes, I agree that this is the correct solution.

I take this back. I think we should do this:

(quoted diff) +1 -6
@@ -758,19 +758,14 @@ cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
 		sd = sd->parent;
 		destroy_sched_domain(tmp);
 		if (sd) {
-			struct sched_group *sg = sd->groups;
-
 			/*
 			 * sched groups hold the flags of the child sched
 			 * domain for convenience. Clear such flags since
 			 * the child is being destroyed.
 			 */
-			do {
-				sg->flags = 0;
-			} while (sg != sd->groups);
+			sd->groups->flags = 0;
 
 			sd->child = NULL;
-		}
 	}
 
Quoted 7 lines
Quoted 5 lines
(quoted diff) +0 -0
 	sched_domain_debug(sd, cpu);

A comment from Chenyu made got me thinking that we should only clear the flags of the local group as viewed from the parent domain. This is because the domain being degenerated defines the flags of such group only.

This looks better to my patch. Thanks.

Are you planning on posting a v2? Maybe I missed it.

Quoted 1 line