sched/core: Clear the root_domain cpumasks in init_rootdomain()
Subsystems (2)
PATCH 1/1 sched/core: Clear the root_domain cpumasks in init_rootdomain()
root_domain::rto_mask allocated through alloc_cpumask_var() contains garbage data, this may cause problems. For instance, When doing pull_rt_task(), it may do useless iterations if rto_mask retains some extra garbage bits. Worse still, this violates the isolated domain rule for clustered scheduling using cpuset, because the tasks(with all the cpus allowed) belongs to one root domain can be pulled away into another root domain.
The patch cleans the garbage by using zalloc_cpumask_var() instead of alloc_cpumask_var() for root_domain::rto_mask allocation, thereby addressing the issues.
Do the same thing for root_domain's other cpumask memembers:dlo_mask, span, and online.
--- kernel/sched/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/core.c +4 -4
@@ -5858,13 +5858,13 @@ static int init_rootdomain(struct root_domain *rd) {memset (rd,0 ,sizeof (*rd)); -if (!alloc_cpumask_var (&rd->span,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->span,GFP_KERNEL )) goto out; -if (!alloc_cpumask_var (&rd->online,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->online,GFP_KERNEL )) goto free_span; -if (!alloc_cpumask_var (&rd->dlo_mask,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->dlo_mask,GFP_KERNEL )) goto free_online; -if (!alloc_cpumask_var (&rd->rto_mask,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->rto_mask,GFP_KERNEL )) goto free_dlo_mask;init_dl_bw (&rd->dl_bw);
Signature · 1 line
2.5.0
Peter Zijlstra
maintainerQuoted 3 lines · Xunlei Pang
The patch cleans the garbage by using
zalloc_cpumask_var()instead ofalloc_cpumask_var()forroot_domain::rto_maskallocation, thereby addressing the issues.
How did you notice this? Also do we want to do the same for the kmalloc in alloc_rootdomain() ?
Hi Peter,
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Xunlei Pang
The patch cleans the garbage by using
zalloc_cpumask_var()instead ofalloc_cpumask_var()forroot_domain::rto_maskallocation, thereby addressing the issues.How did you notice this? Also do we want to do the same for the kmalloc
When doing review.
Quoted 1 line
in
alloc_rootdomain()?
There is a "memset(rd, 0, sizeof(*rd))" in init_rootdomain(), so I don't think we need to do this in alloc_rootdomain().
Regards,
Xunlei
Peter Zijlstra
maintainerQuoted 10 lines · Xunlei Pang
Hi Peter,
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Xunlei Pang
The patch cleans the garbage by using
zalloc_cpumask_var()instead ofalloc_cpumask_var()forroot_domain::rto_maskallocation, thereby addressing the issues.How did you notice this? Also do we want to do the same for the kmalloc
When doing review.
Nice, will you be looking for similar issues elsewhere in the scheduler too?
Quoted 4 lines
Quoted 1 line
in
alloc_rootdomain()?There is a "
memset(rd, 0, sizeof(*rd))" ininit_rootdomain(), so I don't think we need to do this inalloc_rootdomain().
Ah, right there is. Which also clears the mask for small systems.
Hi Peter,
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Xunlei Pang
Hi Peter,
On 12/02/2015 at 08:34 PM, Peter Zijlstra wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 5 lines · Xunlei Pang
Quoted 3 lines
The patch cleans the garbage by using
zalloc_cpumask_var()instead ofalloc_cpumask_var()forroot_domain::rto_maskallocation, thereby addressing the issues.How did you notice this? Also do we want to do the same for the kmalloc
When doing review.
Nice, will you be looking for similar issues elsewhere in the scheduler too?
Sure :-)
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
in
alloc_rootdomain()?There is a "
memset(rd, 0, sizeof(*rd))" ininit_rootdomain(), so I don't think we need to do this inalloc_rootdomain().Ah, right there is. Which also clears the mask for small systems.
Yeah, maybe we can improve it using alloc_cpumask_var() with __GFP_ZERO instead of zalloc_cpumask_var() to avoid duplicate clean for small systems.
Regards,
Xunlei
Quoted 11 lines · Xunlei Pang
Hi Peter,
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Xunlei Pang
Hi Peter,
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Xunlei Pang
The patch cleans the garbage by using
zalloc_cpumask_var()instead ofalloc_cpumask_var()forroot_domain::rto_maskallocation, thereby addressing the issues.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
How did you notice this? Also do we want to do the same for the kmalloc
When doing review.
Nice, will you be looking for similar issues elsewhere in the scheduler too?
Sure :-)
Hm, is the alloc_cpumask_var() done in alloc_sched_domains() safe?
At least the usage pattern in init_sched_domains() looks unsafe:
doms_cur = alloc_sched_domains(ndoms_cur);
if (!doms_cur)
doms_cur = &fallback_doms;
cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);I think alloc_cpumask_var() is a fundamentally unsafe or at least fragile operation, because the uninitialized variable bug will only happen on large CPU count kernels AFAICS - so it's inviting such bugs.
How about we rename alloc_cpumask_var() to alloc_cpumask_var_noinit() or at least __alloc_cpumask_var(), to make this property easier to see?
Thanks,
Ingo
Peter Zijlstra
maintainerQuoted 4 lines · Xunlei Pang
Quoted 2 lines
Nice, will you be looking for similar issues elsewhere in the scheduler too?
Sure :-)
Thanks!
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
in
alloc_rootdomain()?There is a "
memset(rd, 0, sizeof(*rd))" ininit_rootdomain(), so I don't think we need to do this inalloc_rootdomain().Ah, right there is. Which also clears the mask for small systems.
Yeah, maybe we can improve it using
alloc_cpumask_var()with__GFP_ZEROinstead ofzalloc_cpumask_var()to avoid duplicate clean for small systems.
This isn't a performance critical path, so clarity and correctness are more important than performance.
Hi Ingo,
Quoted 13 lines · Ingo Molnar
Quoted 11 lines · Xunlei Pang
Hi Peter,
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Xunlei Pang
Hi Peter,
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines · Xunlei Pang
The patch cleans the garbage by using
zalloc_cpumask_var()instead ofalloc_cpumask_var()forroot_domain::rto_maskallocation, thereby addressing the issues.
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Quoted 1 line
How did you notice this? Also do we want to do the same for the kmalloc
When doing review.
Nice, will you be looking for similar issues elsewhere in the scheduler too?
Sure :-)
Hm, is the
alloc_cpumask_var()done inalloc_sched_domains()safe?
Until now, I haven't found any other similar issues, but I will check further.
Quoted 8 lines
At least the usage pattern in
init_sched_domains()looks unsafe:doms_cur = alloc_sched_domains(ndoms_cur); if (!doms_cur) doms_cur = &fallback_doms; cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
Quoted 6 lines
I think
alloc_cpumask_var()is a fundamentally unsafe or at least fragile operation, because the uninitialized variable bug will only happen on large CPU count kernels AFAICS - so it's inviting such bugs.How about we rename
alloc_cpumask_var()toalloc_cpumask_var_noinit()or at least__alloc_cpumask_var(), to make this property easier to see?
There have already been many call sites of it in the kernel, at least we still have zalloc_cpumask_var(), maybe we could add some function comments, reminding people of thinking of zalloc_cpumask_var() for their cases.
Regards,
Xunlei
Quoted 4 lines
Thanks,
Ingo
Quoted 11 lines · Xunlei Pang
Quoted 1 line
Hm, is the
alloc_cpumask_var()done inalloc_sched_domains()safe?Until now, I haven't found any other similar issues, but I will check further.
Quoted 7 lines
At least the usage pattern in
init_sched_domains()looks unsafe:doms_cur = alloc_sched_domains(ndoms_cur); if (!doms_cur) doms_cur = &fallback_doms; cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
So is this pattern in init_sched_domains() correct, for OFFSTACK=y?
It looks wrong to me, as alloc_sched_domains() allocates an uninitialized cpumask via alloc_cpumask_var() and returns it:
cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
{
int i;
cpumask_var_t *doms;
doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
if (!doms)
return NULL;
for (i = 0; i < ndoms; i++) {
if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
free_sched_domains(doms, i);
return NULL;
}
}
return doms;
}and then this code:
Quoted 1 line
Quoted 1 line
cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
uses it without first clearing it.
So is this another such bug, or am I missing something?
Thanks,
Ingo
Peter Zijlstra
maintainerQuoted 7 lines · Ingo Molnar
and then this code:
Quoted 1 line
Quoted 1 line
cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);uses it without first clearing it.
So is this another such bug, or am I missing something?
It uses it as destination, it does a complete write of the mask.
Hi Ingo,
Quoted 33 lines · Ingo Molnar
Quoted 9 lines · Xunlei Pang
Quoted 1 line
Hm, is the
alloc_cpumask_var()done inalloc_sched_domains()safe?Until now, I haven't found any other similar issues, but I will check further.
Quoted 6 lines
At least the usage pattern in
init_sched_domains()looks unsafe:doms_cur = alloc_sched_domains(ndoms_cur); if (!doms_cur) doms_cur = &fallback_doms; cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);So is this pattern in
init_sched_domains()correct, for OFFSTACK=y?It looks wrong to me, as
alloc_sched_domains()allocates an uninitialized cpumask viaalloc_cpumask_var()and returns it:
cpumask_var_t*alloc_sched_domains(unsigned int ndoms){ int i; cpumask_var_t *doms; doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL); if (!doms) return NULL; for (i = 0; i < ndoms; i++) { if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { free_sched_domains(doms, i); return NULL; } } return doms; }
Quoted 6 lines
and then this code:
Quoted 1 line
Quoted 1 line
cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);uses it without first clearing it.
So is this another such bug, or am I missing something?
Yeah, I noticed that as well. But fortunately cpumask_andnot(), cpumask_and() and the like will clear doms_cur[] indirectly, also cpu_isolated_map, cpu_active_mask, etc doesn't contain any garbage bits. I also checked the use of it by cpuset, no extra such bug found by me so far.
Regards,
Xunlei
Quoted 9 lines
Thanks,
Ingo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Quoted 10 lines · Peter Zijlstra
Quoted 7 lines · Ingo Molnar
and then this code:
Quoted 1 line
Quoted 1 line
cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);uses it without first clearing it.
So is this another such bug, or am I missing something?
It uses it as destination, it does a complete write of the mask.
ah, indeed! The cpumask primitive confused me.
Thanks,
Ingo
Quoted 41 lines · Xunlei Pang
Hi Ingo,
Quoted 38 lines · Ingo Molnar
Quoted 9 lines · Xunlei Pang
Quoted 1 line
Hm, is the
alloc_cpumask_var()done inalloc_sched_domains()safe?Until now, I haven't found any other similar issues, but I will check further.
Quoted 6 lines
At least the usage pattern in
init_sched_domains()looks unsafe:doms_cur = alloc_sched_domains(ndoms_cur); if (!doms_cur) doms_cur = &fallback_doms; cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);So is this pattern in
init_sched_domains()correct, for OFFSTACK=y?It looks wrong to me, as
alloc_sched_domains()allocates an uninitialized cpumask viaalloc_cpumask_var()and returns it:
cpumask_var_t*alloc_sched_domains(unsigned int ndoms){ int i; cpumask_var_t *doms; doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL); if (!doms) return NULL; for (i = 0; i < ndoms; i++) { if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { free_sched_domains(doms, i); return NULL; } } return doms; }and then this code:
Quoted 1 line
Quoted 1 line
cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);uses it without first clearing it.
Quoted 7 lines
Quoted 1 line
So is this another such bug, or am I missing something?
Yeah, I noticed that as well. But fortunately
cpumask_andnot(),cpumask_and()and the like will clear doms_cur[] indirectly, alsocpu_isolated_map,cpu_active_mask, etc doesn't contain any garbage bits. I also checked the use of it by cpuset, no extra such bug found by me so far.
Great, thanks for double checking!
Ingo
tip-bot for Xunlei Pang
merge botsched/core: Clear the root_domain cpumasks in init_rootdomain()
Commit-ID: 8295c69925ad53ec32ca54ac9fc194ff21bc40e2 Gitweb: http://git.kernel.org/tip/8295c69925ad53ec32ca54ac9fc194ff21bc40e2 Author: Xunlei Pang <[email protected]> AuthorDate: Wed, 2 Dec 2015 19:52:59 +0800 Committer: Ingo Molnar <[email protected]> CommitDate: Fri, 4 Dec 2015 10:16:21 +0100
sched/core: Clear the root_domain cpumasks in init_rootdomain()
root_domain::rto_mask allocated through alloc_cpumask_var() contains garbage data, this may cause problems. For instance, When doing pull_rt_task(), it may do useless iterations if rto_mask retains some extra garbage bits. Worse still, this violates the isolated domain rule for clustered scheduling using cpuset, because the tasks(with all the cpus allowed) belongs to one root domain can be pulled away into another root domain.
The patch cleans the garbage by using zalloc_cpumask_var() instead of alloc_cpumask_var() for root_domain::rto_mask allocation, thereby addressing the issues.
Do the same thing for root_domain's other cpumask memembers:dlo_mask, span, and online.
--- kernel/sched/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/sched/core.c +4 -4
@@ -5846,13 +5846,13 @@ static int init_rootdomain(struct root_domain *rd) {memset (rd,0 ,sizeof (*rd)); -if (!alloc_cpumask_var (&rd->span,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->span,GFP_KERNEL )) goto out; -if (!alloc_cpumask_var (&rd->online,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->online,GFP_KERNEL )) goto free_span; -if (!alloc_cpumask_var (&rd->dlo_mask,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->dlo_mask,GFP_KERNEL )) goto free_online; -if (!alloc_cpumask_var (&rd->rto_mask,GFP_KERNEL )) +if (!zalloc_cpumask_var (&rd->rto_mask,GFP_KERNEL )) goto free_dlo_mask;init_dl_bw (&rd->dl_bw);