sched/topology: Do not override sd->shared allocation
K Prateek Nayak
reviewerPATCH 1/1 sched/topology: Do not override sd->shared allocation
Breno reported a kmemleak for sd->shared assignment on an asymmetric system with a single LLC.
Post the unconditional assignment of sd->shared for sd_llc in commit 9e005ed21152d ("sched/topology: Allow multiple domains to claim sched_domain_shared"), init_sched_domain_shared() can overrwrite the sd->shared assignment for as_asym_cpucapacity when sd_llc and as_asym_cpucapacity point to the same domain.
Return early from init_sched_domain_shared() if a valid sd->shared assignment is detected. Since the degeneration path only decrements the refcount by 1, it is correct to only account the sd->shared assignment once per sched-domain level, even if the flags overlap.
While at it, correct a typo in init_sched_domain_shared().
--- kernel/sched/topology.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
kernel/sched/topology.c +9 -1
@@ -2942,6 +2942,14 @@ init_sched_domain_shared(struct s_data *d, struct sched_domain *sd, int flags)struct sched_domain_shared *sds =NULL ;int cpu; +/* +* sd->shared is already assigned for this domain level for a +* different flag. Nothing to do since this allocation is +* already accounted for. +*/ +if (sd->shared) +return ; +/* * Multiple domains can try to claim a shared object like * SD_ASYM_CPUCAPACITY and SD_SHARE_LLC which can alias to
@@ -2970,7 +2978,7 @@ init_sched_domain_shared(struct s_data *d, struct sched_domain *sd, int flags)/* * Use the sd_shared corresponding to the last -* CPU in the span if none are avaialable. +* CPU in the span if none are available. */ if (WARN_ON_ONCE (!sd->shared)) sd->shared = sds;
base-commit: 04998aa54848f15332202d0bea008d2ca1ed1713
Signature · 1 line
2.34.1
K Prateek Nayak
reviewerHello Peter,
Quoted 14 lines · K Prateek Nayak
Breno reported a kmemleak for
sd->sharedassignment on an asymmetric system with a single LLC.Post the unconditional assignment of
sd->sharedforsd_llcin commit9e005ed21152d("sched/topology: Allow multiple domains to claimsched_domain_shared"),init_sched_domain_shared()can overrwrite thesd->sharedassignment foras_asym_cpucapacitywhensd_llcandas_asym_cpucapacitypoint to the same domain.Return early from
init_sched_domain_shared()if a validsd->sharedassignment is detected. Since the degeneration path only decrements the refcount by 1, it is correct to only account thesd->sharedassignment once per sched-domain level, even if the flags overlap.
Quoted 8 lines
While at it, correct a typo in
init_sched_domain_shared().Reported-by Breno Leitao <[email protected]>Fixes 9e005ed21152d ("sched/topology: Allow multiple domains to claim sched_domain_shared")Tested-by Breno Leitao <[email protected]>Tested-by Dietmar Eggemann <[email protected]>Signed-off-by K Prateek Nayak <[email protected]>
Sorry for the late notice! Was stuck with some internal fire but in case sched/urgent is still open, could you stick this fix for a memory leak on top.
sched/core is good too otherwise - AUTOSEL should pick this up for stable, and if it doesn't, I'll send out a patch to Greg for v7.2. Sorry again for the late ping.
Quoted 34 lines
--- kernel/sched/topology.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)kernel/sched/topology.c +9
-1@@ -2942,6 +2942,14 @@ init_sched_domain_shared(struct s_data *d, struct sched_domain *sd, int flags)struct sched_domain_shared *sds =NULL ;int cpu; +/* +* sd->shared is already assigned for this domain level for a +* different flag. Nothing to do since this allocation is +* already accounted for. +*/ +if (sd->shared) +return ; +/* * Multiple domains can try to claim a shared object like * SD_ASYM_CPUCAPACITY and SD_SHARE_LLC which can alias to @@ -2970,7 +2978,7 @@ init_sched_domain_shared(struct s_data *d, struct sched_domain *sd, int flags)/* * Use the sd_shared corresponding to the last -* CPU in the span if none are avaialable. +* CPU in the span if none are available. */ if (WARN_ON_ONCE (!sd->shared)) sd->shared = sds;base-commit:
04998aa54848f15332202d0bea008d2ca1ed1713
Signature · 2 lines
Thanks and Regards, Prateek