cpufreq: stats: Walk online CPUs with CPU offline/online locked
PATCH 1/1 cpufreq: stats: Walk online CPUs with CPU offline/online locked
From: Rafael J. Wysocki <[email protected]>
Loops over online CPUs in cpufreq_stats_init() and cpufreq_stats_exit() should be carried out with CPU offline/online locked or races are possible otherwise, so make that happen.
--- drivers/cpufreq/cpufreq_stats.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/cpufreq/cpufreq_stats.c +10 -2
@@ -322,6 +322,8 @@ static int __init cpufreq_stats_init(voiif (ret)return ret; +get_online_cpus (); +for_each_online_cpu (cpu)cpufreq_stats_create_table (cpu);
@@ -332,21 +334,27 @@ static int __init cpufreq_stats_init(voi CPUFREQ_POLICY_NOTIFIER);for_each_online_cpu (cpu)cpufreq_stats_free_table (cpu); -return ret; } -return 0 ; +put_online_cpus (); + +return ret; } static void __exit cpufreq_stats_exit(void) {unsigned int cpu; +get_online_cpus (); +cpufreq_unregister_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER );cpufreq_unregister_notifier (¬ifier_trans_block,CPUFREQ_TRANSITION_NOTIFIER ); +for_each_online_cpu (cpu)cpufreq_stats_free_table (cpu); + +put_online_cpus (); }MODULE_AUTHOR ("Zou Nan hai <[email protected]>" );
PATCH v2 1/1 cpufreq: stats: Walk online CPUs with CPU offline/online locked
From: Rafael J. Wysocki <[email protected]>
Loops over online CPUs in cpufreq_stats_init() and cpufreq_stats_exit() should be carried out with CPU offline/online locked or races are possible otherwise, so make that happen.
---
v1 -> v2: On a second thought, add the policy notifier in cpufreq_stats_init()
with CPU offline/online locked too.
--- drivers/cpufreq/cpufreq_stats.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/cpufreq/cpufreq_stats.c +13 -3
@@ -317,10 +317,13 @@ static int __init cpufreq_stats_init(voiunsigned int cpu;spin_lock_init (&cpufreq_stats_lock); + +get_online_cpus (); + ret =cpufreq_register_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER );if (ret) -return ret; + goto out;for_each_online_cpu (cpu)cpufreq_stats_create_table (cpu);
@@ -332,21 +335,28 @@ static int __init cpufreq_stats_init(voi CPUFREQ_POLICY_NOTIFIER);for_each_online_cpu (cpu)cpufreq_stats_free_table (cpu); -return ret; } -return 0 ; + out: +put_online_cpus (); + +return ret; } static void __exit cpufreq_stats_exit(void) {unsigned int cpu; +get_online_cpus (); +cpufreq_unregister_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER );cpufreq_unregister_notifier (¬ifier_trans_block,CPUFREQ_TRANSITION_NOTIFIER ); +for_each_online_cpu (cpu)cpufreq_stats_free_table (cpu); + +put_online_cpus (); }MODULE_AUTHOR ("Zou Nan hai <[email protected]>" );
Viresh Kumar
maintainerQuoted 21 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Loops over online CPUs in
cpufreq_stats_init()andcpufreq_stats_exit()should be carried out with CPU offline/online locked or races are possible otherwise, so make that happen.Signed-off-by Rafael J. Wysocki <[email protected]>---
v1 -> v2: On a second thought, add the policy notifier in
cpufreq_stats_init()
with CPU offline/online locked too.--- drivers/cpufreq/cpufreq_stats.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)Index:
linux-pm/drivers/cpufreq/cpufreq_stats.c
===================================================================
drivers/cpufreq/cpufreq_stats.c static int __init cpufreq_stats_init(voi · line 324
Quoted 8 lines
drivers/cpufreq/cpufreq_stats.c +3
-0@@ -317,10 +317,13 @@ static int __init cpufreq_stats_init(voiunsigned int cpu;spin_lock_init (&cpufreq_stats_lock); + +get_online_cpus (); + ret =cpufreq_register_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER );
Why is this required to be protected ?
drivers/cpufreq/cpufreq_stats.c static int __init cpufreq_stats_init(voi · line 337
Quoted 10 lines
drivers/cpufreq/cpufreq_stats.c +1
-1if (ret) -return ret; + goto out;for_each_online_cpu (cpu)cpufreq_stats_create_table (cpu);@@ -332,21 +335,28 @@ static int __init cpufreq_stats_init(voi CPUFREQ_POLICY_NOTIFIER);for_each_online_cpu (cpu)cpufreq_stats_free_table (cpu);
Maybe we can make this for_each_possible_cpu() then, and so getting a policy will fail for CPUs which aren't online.
And we wouldn't need to use get_online_cpus() then ?
Signature · 1 line
viresh
Quoted 24 lines · Viresh Kumar
Quoted 23 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Loops over online CPUs in
cpufreq_stats_init()andcpufreq_stats_exit()should be carried out with CPU offline/online locked or races are possible otherwise, so make that happen.Signed-off-by Rafael J. Wysocki <[email protected]>---
v1 -> v2: On a second thought, add the policy notifier in
cpufreq_stats_init()
with CPU offline/online locked too.--- drivers/cpufreq/cpufreq_stats.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)Index:
linux-pm/drivers/cpufreq/cpufreq_stats.c
===================================================================drivers/cpufreq/cpufreq_stats.c +0
-0@@ -317,10 +317,13 @@ static int __init cpufreq_stats_init(voiunsigned int cpu;
Quoted 8 lines
drivers/cpufreq/cpufreq_stats.c static int __init cpufreq_stats_init(voi · line 324
Quoted 6 lines
drivers/cpufreq/cpufreq_stats.c +3
-0spin_lock_init (&cpufreq_stats_lock); + +get_online_cpus (); + ret =cpufreq_register_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER );Why is this required to be protected ?
Last night I thought I saw a scenario in which that notifier could run in parallel with the loop below even with get_online_cpus() between them, but I don't see it right now.
Maybe I should not look at stuff late in the night ...
Quoted 14 lines
drivers/cpufreq/cpufreq_stats.c static int __init cpufreq_stats_init(voi · line 337
Quoted 10 lines
drivers/cpufreq/cpufreq_stats.c +1
-1if (ret) -return ret; + goto out;for_each_online_cpu (cpu)cpufreq_stats_create_table (cpu);@@ -332,21 +335,28 @@ static int __init cpufreq_stats_init(voi CPUFREQ_POLICY_NOTIFIER);for_each_online_cpu (cpu)cpufreq_stats_free_table (cpu);Maybe we can make this
for_each_possible_cpu()then, and so getting a policy will fail for CPUs which aren't online.
Quoted 1 line
And we wouldn't need to use
get_online_cpus()then ?
That could be done, but then there would be nothing to prevent the policy notifier from running in parallel with the loop.
Something like the patch below should do the trick, though.
---
From: Rafael J. Wysocki <[email protected]>
Subject: [PATCH] cpufreq: stats: Fix race conditions on init and cleanup
Loops over online CPUs in cpufreq_stats_init() and cpufreq_stats_exit() are not carried out with CPU offline/online locked, so races are possible with respect to policy initialization and cleanup.
To prevent that from happening, change the loops to walk all possible CPUs, as cpufreq_stats_create_table() and cpufreq_stats_free_table() handle the case when there's no policy for the given CPU cleanly, but also use policy->rwsem in there to prevent those routines from racing with the policy notifier.
--- drivers/cpufreq/cpufreq_stats.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/cpufreq/cpufreq_stats.c +9 -5
@@ -154,7 +154,9 @@ static void cpufreq_stats_free_table(unsif (!policy)return ; +down_write (&policy->rwsem);__cpufreq_stats_free_table (policy); +up_write (&policy->rwsem);cpufreq_cpu_put (policy); }
@@ -238,7 +240,9 @@ static void cpufreq_stats_create_table(uif (likely (!policy))return ; +down_write (&policy->rwsem);__cpufreq_stats_create_table (policy); +up_write (&policy->rwsem);cpufreq_cpu_put (policy); }
@@ -322,7 +326,7 @@ static int __init cpufreq_stats_init(voiif (ret)return ret; -for_each_online_cpu (cpu) +for_each_possible_cpu (cpu)cpufreq_stats_create_table (cpu); ret = cpufreq_register_notifier(¬ifier_trans_block,
@@ -330,12 +334,11 @@ static int __init cpufreq_stats_init(voiif (ret) {cpufreq_unregister_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER ); -for_each_online_cpu (cpu) +for_each_possible_cpu (cpu)cpufreq_stats_free_table (cpu); -return ret; } -return 0 ; +return ret; } static void __exit cpufreq_stats_exit(void) {
@@ -345,7 +348,8 @@ static void __exit cpufreq_stats_exit(vo CPUFREQ_POLICY_NOTIFIER);cpufreq_unregister_notifier (¬ifier_trans_block,CPUFREQ_TRANSITION_NOTIFIER ); -for_each_online_cpu (cpu) + +for_each_possible_cpu (cpu)cpufreq_stats_free_table (cpu); }
Quoted 54 lines · Rafael J. Wysocki
Quoted 24 lines · Viresh Kumar
Quoted 23 lines · Rafael J. Wysocki
From: Rafael J. Wysocki <[email protected]>
Loops over online CPUs in
cpufreq_stats_init()andcpufreq_stats_exit()should be carried out with CPU offline/online locked or races are possible otherwise, so make that happen.Signed-off-by Rafael J. Wysocki <[email protected]>---
v1 -> v2: On a second thought, add the policy notifier in
cpufreq_stats_init()
with CPU offline/online locked too.--- drivers/cpufreq/cpufreq_stats.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)Index:
linux-pm/drivers/cpufreq/cpufreq_stats.c
===================================================================drivers/cpufreq/cpufreq_stats.c +0
-0@@ -317,10 +317,13 @@ static int __init cpufreq_stats_init(voiunsigned int cpu;Quoted 8 lines
drivers/cpufreq/cpufreq_stats.c static int __init cpufreq_stats_init(voi · line 324
Quoted 6 lines
drivers/cpufreq/cpufreq_stats.c +3
-0spin_lock_init (&cpufreq_stats_lock); + +get_online_cpus (); + ret =cpufreq_register_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER );Why is this required to be protected ?
Last night I thought I saw a scenario in which that notifier could run in parallel with the loop below even with
get_online_cpus()between them, but I don't see it right now.Maybe I should not look at stuff late in the night ...
Quoted 14 lines
drivers/cpufreq/cpufreq_stats.c static int __init cpufreq_stats_init(voi · line 337
Quoted 10 lines
drivers/cpufreq/cpufreq_stats.c +1
-1if (ret) -return ret; + goto out;for_each_online_cpu (cpu)cpufreq_stats_create_table (cpu);@@ -332,21 +335,28 @@ static int __init cpufreq_stats_init(voi CPUFREQ_POLICY_NOTIFIER);for_each_online_cpu (cpu)cpufreq_stats_free_table (cpu);Maybe we can make this
for_each_possible_cpu()then, and so getting a policy will fail for CPUs which aren't online.
Quoted 6 lines
Quoted 1 line
And we wouldn't need to use
get_online_cpus()then ?That could be done, but then there would be nothing to prevent the policy notifier from running in parallel with the loop.
Something like the patch below should do the trick, though.
The policy rwsem is really only needed in cpufreq_stats_create_table(), because the policy notifier is gone when _free_table() runs, so another version of the patch goes below.
---
From: Rafael J. Wysocki <[email protected]>
Subject: [PATCH] cpufreq: stats: Fix race conditions on init and cleanup
Loops over online CPUs in cpufreq_stats_init() and cpufreq_stats_exit() are not carried out with CPU offline/online locked, so races are possible with respect to policy initialization and cleanup.
To prevent that from happening, change the loops to walk all possible CPUs, as cpufreq_stats_create_table() and cpufreq_stats_free_table() handle the case when there's no policy for the given CPU cleanly, but also use policy->rwsem in cpufreq_stats_create_table() to prevent it from racing with the policy notifier.
--- drivers/cpufreq/cpufreq_stats.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/cpufreq/cpufreq_stats.c +11 -5
@@ -238,7 +238,13 @@ static void cpufreq_stats_create_table(uif (likely (!policy))return ; +/* +* The policy notifier may run in parallel with this code, so use the +* policy rwsem to avoid racing with it. +*/ +down_write (&policy->rwsem);__cpufreq_stats_create_table (policy); +up_write (&policy->rwsem);cpufreq_cpu_put (policy); }
@@ -322,7 +328,7 @@ static int __init cpufreq_stats_init(voiif (ret)return ret; -for_each_online_cpu (cpu) +for_each_possible_cpu (cpu)cpufreq_stats_create_table (cpu); ret = cpufreq_register_notifier(¬ifier_trans_block,
@@ -330,12 +336,11 @@ static int __init cpufreq_stats_init(voiif (ret) {cpufreq_unregister_notifier (¬ifier_policy_block,CPUFREQ_POLICY_NOTIFIER ); -for_each_online_cpu (cpu) +for_each_possible_cpu (cpu)cpufreq_stats_free_table (cpu); -return ret; } -return 0 ; +return ret; } static void __exit cpufreq_stats_exit(void) {
@@ -345,7 +350,8 @@ static void __exit cpufreq_stats_exit(vo CPUFREQ_POLICY_NOTIFIER);cpufreq_unregister_notifier (¬ifier_trans_block,CPUFREQ_TRANSITION_NOTIFIER ); -for_each_online_cpu (cpu) + +for_each_possible_cpu (cpu)cpufreq_stats_free_table (cpu); }
Viresh Kumar
maintainerQuoted 3 lines · Rafael J. Wysocki
The policy rwsem is really only needed in
cpufreq_stats_create_table(), because the policy notifier is gone when_free_table()runs, so another version of the patch goes below.
Right. I saw that while reading your previous version but didn't reply because I wanted to do a more careful review.
The first issue I have here is that the _init and _exit routines in cpufreq-stats aren't opposite of each other. Which shouldn't be the case.
Quoted 24 lines
---
From: Rafael J. Wysocki <[email protected]>
Subject: [PATCH] cpufreq: stats: Fix race conditions on init and cleanupLoops over online CPUs in
cpufreq_stats_init()andcpufreq_stats_exit()are not carried out with CPU offline/online locked, so races are possible with respect to policy initialization and cleanup.To prevent that from happening, change the loops to walk all possible CPUs, as
cpufreq_stats_create_table()andcpufreq_stats_free_table()handle the case when there's no policy for the given CPU cleanly, but also usepolicy->rwsemincpufreq_stats_create_table()to prevent it from racing with the policy notifier.Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/cpufreq/cpufreq_stats.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)Index:
linux-pm/drivers/cpufreq/cpufreq_stats.c
===================================================================
drivers/cpufreq/cpufreq_stats.c static void cpufreq_stats_create_table(u · line 247
Quoted 10 lines
drivers/cpufreq/cpufreq_stats.c +6
-0@@ -238,7 +238,13 @@ static void cpufreq_stats_create_table(uif (likely (!policy))return ; +/* +* The policy notifier may run in parallel with this code, so use the +* policy rwsem to avoid racing with it. +*/ +down_write (&policy->rwsem);__cpufreq_stats_create_table (policy); +up_write (&policy->rwsem);
I am still trying to understand why we will ever have a race here. We might have it, but I just want to know how.
This is what we do in on addition of a policy:
- send the CREATE notifier
- Add policy to the list
So, the notifiers are guaranteed to complete before the policy is present in the list.
CPU 0 CPU 1 notifier cpufreq_stats_init() CREATE-POLICY X cpufreq_stats_create_table() __cpufreq_stats_create_table() cpufreq_cpu_get()
AFAICT, whatever may happen, __cpufreq_stats_create_table() will *not* get called in parallel for the same policy.
If __cpufreq_stats_create_table() is in progress on CPU0, CPU 1 will not find the policy with cpufreq_cpu_get(). And if cpufreq_cpu_get() finds a policy, the notifier would already have completed.
What do you say ?
Signature · 1 line
viresh
Quoted 11 lines · Viresh Kumar
Quoted 3 lines · Rafael J. Wysocki
The policy rwsem is really only needed in
cpufreq_stats_create_table(), because the policy notifier is gone when_free_table()runs, so another version of the patch goes below.Right. I saw that while reading your previous version but didn't reply because I wanted to do a more careful review.
The first issue I have here is that the
_initand_exitroutines in cpufreq-stats aren't opposite of each other. Which shouldn't be the case.
I'm not sure what you mean here.
Quoted 50 lines
Quoted 24 lines
---
From: Rafael J. Wysocki <[email protected]>
Subject: [PATCH] cpufreq: stats: Fix race conditions on init and cleanupLoops over online CPUs in
cpufreq_stats_init()andcpufreq_stats_exit()are not carried out with CPU offline/online locked, so races are possible with respect to policy initialization and cleanup.To prevent that from happening, change the loops to walk all possible CPUs, as
cpufreq_stats_create_table()andcpufreq_stats_free_table()handle the case when there's no policy for the given CPU cleanly, but also usepolicy->rwsemincpufreq_stats_create_table()to prevent it from racing with the policy notifier.Signed-off-by Rafael J. Wysocki <[email protected]>--- drivers/cpufreq/cpufreq_stats.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)Index:
linux-pm/drivers/cpufreq/cpufreq_stats.c
===================================================================drivers/cpufreq/cpufreq_stats.c static void cpufreq_stats_create_table(u · line 247
Quoted 10 lines
drivers/cpufreq/cpufreq_stats.c +6
-0@@ -238,7 +238,13 @@ static void cpufreq_stats_create_table(uif (likely (!policy))return ; +/* +* The policy notifier may run in parallel with this code, so use the +* policy rwsem to avoid racing with it. +*/ +down_write (&policy->rwsem);__cpufreq_stats_create_table (policy); +up_write (&policy->rwsem);I am still trying to understand why we will ever have a race here. We might have it, but I just want to know how.
This is what we do in on addition of a policy:
- send the CREATE notifier
- Add policy to the listSo, the notifiers are guaranteed to complete before the policy is present in the list.
CPU 0 CPU 1 notifier cpufreq_stats_init() CREATE-POLICY X cpufreq_stats_create_table() __cpufreq_stats_create_table() cpufreq_cpu_get()
Quoted 8 lines
AFAICT, whatever may happen,
__cpufreq_stats_create_table()will *not* get called in parallel for the same policy.If
__cpufreq_stats_create_table()is in progress on CPU0, CPU 1 will not find the policy withcpufreq_cpu_get(). And ifcpufreq_cpu_get()finds a policy, the notifier would already have completed.What do you say ?
Say cpufreq_stats_init() runs in parallel with a CPU online (say someone loads the cpufreq_stats module and a CPU goes online at the same time, not likely to happen, but still possible).
Then, the notifier may get invoked when the loop is in progress and because the CPU is added to policy->cpus (and the CPU's per-CPU pointer is set to it) before invoking the notifier, cpufreq_stats_init() may get the policy pointer for a policy that hasn't been initialized completely yet and then run in parallel with the notifier for that policy.
At least I don't see anything to prevent that from happening, maybe I'm overlooking something.
Thanks,
Rafael
Viresh Kumar
maintainerQuoted 6 lines · Rafael J. Wysocki
Quoted 5 lines · Viresh Kumar
Quoted 3 lines · Rafael J. Wysocki
The policy rwsem is really only needed in
cpufreq_stats_create_table(), because the policy notifier is gone when_free_table()runs, so another version of the patch goes below.
Quoted 8 lines
Quoted 6 lines
Right. I saw that while reading your previous version but didn't reply because I wanted to do a more careful review.
The first issue I have here is that the
_initand_exitroutines in cpufreq-stats aren't opposite of each other. Which shouldn't be the case.I'm not sure what you mean here.
Sorry about that. I meant that exit() should look opposite of init() ideally, whereas if you look at current code, both are (un)registering the POLICY_NOTIFIER at the top.
Quoted 15 lines
Quoted 15 lines
I am still trying to understand why we will ever have a race here. We might have it, but I just want to know how.
This is what we do in on addition of a policy:
- send the CREATE notifier
- Add policy to the listSo, the notifiers are guaranteed to complete before the policy is present in the list.
CPU 0 CPU 1 notifier cpufreq_stats_init() CREATE-POLICY X cpufreq_stats_create_table() __cpufreq_stats_create_table() cpufreq_cpu_get()
Quoted 8 lines
Quoted 8 lines
AFAICT, whatever may happen,
__cpufreq_stats_create_table()will *not* get called in parallel for the same policy.If
__cpufreq_stats_create_table()is in progress on CPU0, CPU 1 will not find the policy withcpufreq_cpu_get(). And ifcpufreq_cpu_get()finds a policy, the notifier would already have completed.What do you say ?
Until now I thought you are trying to prevent the race where __cpufreq_stats_create_table() gets called in parallel for the same policy. So, above explains that it can't happen for sure.
Quoted 3 lines
Say
cpufreq_stats_init()runs in parallel with a CPU online (say someone loads thecpufreq_statsmodule and a CPU goes online at the same time, not likely to happen, but still possible).
Of course, that will be a design problem if it ever happens. I agree.
Quoted 5 lines
Then, the notifier may get invoked when the loop is in progress and because the CPU is added to
policy->cpus(and the CPU's per-CPU pointer is set to it) before invoking the notifier,cpufreq_stats_init()may get the policy pointer for a policy that hasn't been initialized completely yet and then run in parallel with the notifier for that policy.
If the policy isn't initialized fully before its added to the list, then that's a problem in cpufreq.c I would say.
But, I don't see a problem here. The policy's kobject, etc gets initialized fully before its added to the list or the notifier is sent for CREATE_POLICY. Just that the governor isn't set properly, nothing else.
And if you think about it the other way round, we are sending the CREATE_POLICY notifier right at that point where we add it to the list, and the cpufreq-stats layer is expected to work on the policy right from that call. So, it is fully initialized from the perspective of the stats layer. Nothing should go wrong.
Signature · 1 line
viresh
Rafael J. Wysocki
maintainerQuoted 14 lines · Viresh Kumar
Quoted 13 lines · Rafael J. Wysocki
Quoted 11 lines · Viresh Kumar
Quoted 3 lines · Rafael J. Wysocki
The policy rwsem is really only needed in
cpufreq_stats_create_table(), because the policy notifier is gone when_free_table()runs, so another version of the patch goes below.Right. I saw that while reading your previous version but didn't reply because I wanted to do a more careful review.
The first issue I have here is that the
_initand_exitroutines in cpufreq-stats aren't opposite of each other. Which shouldn't be the case.
Quoted 5 lines
Quoted 1 line
I'm not sure what you mean here.
Sorry about that. I meant that
exit()should look opposite ofinit()ideally, whereas if you look at current code, both are (un)registering thePOLICY_NOTIFIERat the top.
That actually sort of makes sense, though, except that the code is racy. :-)
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
I am still trying to understand why we will ever have a race here. We might have it, but I just want to know how.
This is what we do in on addition of a policy:
- send the CREATE notifier
- Add policy to the listSo, the notifiers are guaranteed to complete before the policy is present in the list.
CPU 0 CPU 1 notifier cpufreq_stats_init() CREATE-POLICY X cpufreq_stats_create_table() __cpufreq_stats_create_table() cpufreq_cpu_get()AFAICT, whatever may happen,
__cpufreq_stats_create_table()will *not* get called in parallel for the same policy.If
__cpufreq_stats_create_table()is in progress on CPU0, CPU 1 will not find the policy withcpufreq_cpu_get(). And ifcpufreq_cpu_get()finds a policy, the notifier would already have completed.
Quoted 5 lines
Quoted 1 line
Quoted 1 line
What do you say ?
Until now I thought you are trying to prevent the race where
__cpufreq_stats_create_table()gets called in parallel for the same policy. So, above explains that it can't happen for sure.
Assuming that the loops are over online CPUs and not over possible CPUs I suppose?
Anyway, if you are talking about the code without the patch (which I guess is the case), the reason why it is racy is because, if cpufreq_stats_init() runs in parallel with CPU online, the CPU going online may be missed by it. To my eyes that happens if cpufreq_online() has already advanced beyond the point where the notifier would have been invoked, but hasn't returned yet when the for_each_online_cpu() loop in cpufreq_stats_init() is executed.
Worse yet, if a CPU goes offline when cpufreq_stats_exit() is running and that happens exactly between the notifier unregistration and the for_each_online_cpu() loop, the stats table will never be freed for that CPU (say the policy isn't shared).
Switching over to loops over possible CPUs doesn't address those races (at least not the second one), and I'm not really sure why I thought it would address them, but adding CPU online/offline locking to cpufreq_stats_init/exit() can address them, so it looks like the very first version of my patch (ie.
https://patchwork.kernel.org/patch/9128509/) was actually correct, because it didn't put too much code under the CPU offline/online locking. :-)
Viresh Kumar
maintainerQuoted 2 lines · Rafael J. Wysocki
Assuming that the loops are over online CPUs and not over possible CPUs I suppose?
I wasn't focussing on that loop lately but the policy->rwsem :)
Quoted 7 lines
Anyway, if you are talking about the code without the patch (which I guess is the case), the reason why it is racy is because, if
cpufreq_stats_init()runs in parallel with CPU online, the CPU going online may be missed by it. To my eyes that happens ifcpufreq_online()has already advanced beyond the point where the notifier would have been invoked, but hasn't returned yet when thefor_each_online_cpu()loop incpufreq_stats_init()is executed.
Yes. That's a race we need to fix. I agree.
Quoted 4 lines
Worse yet, if a CPU goes offline when
cpufreq_stats_exit()is running and that happens exactly between the notifier unregistration and thefor_each_online_cpu()loop, the stats table will never be freed for that CPU (say the policy isn't shared).
Same here.
Quoted 8 lines
Switching over to loops over possible CPUs doesn't address those races (at least not the second one), and I'm not really sure why I thought it would address them, but adding CPU online/offline locking to cpufreq_stats_init/exit() can address them, so it looks like the very first version of my patch (ie.
https://patchwork.kernel.org/patch/9128509/) was actually correct, because it didn't put too much code under the CPU offline/online locking. :-)
Well, I think there is one more way of getting all this fixed, which may eventually look much more cleaner.
What if we update cpufreq core instead of stats with something like this:
-------------------------8<-------------------------
From: Viresh Kumar <[email protected]>
Date: Tue, 24 May 2016 10:16:25 +0530
Subject: [PATCH] cpufreq: Initiate notifiers for existing policy
Races are possible in the init/exit paths of the cpufreq-stats layer, which may lead to 'stats' sysfs directory not getting created or removed for some of the policies. This can happen while the policy is getting created while cpufreq_stats_init/exit() are getting called.
To avoid adding unnecessary locks in the init/exit paths of the cpufreq-stats layer, update the policy notifier register/unregister routines to send notifications for any existing cpufreq policies.
Also make sure (with help of cpufreq_driver_lock) that CPUFREQ_CREATE/REMOVE notifiers aren't getting issued in parallel from the policy creation/removal paths.
--- drivers/cpufreq/cpufreq.c | 35 +++++++++++++++++++++++++- drivers/cpufreq/cpufreq_stats.c | 55 ++++++----------------------------------- 2 files changed, 41 insertions(+), 49 deletions(-)
drivers/cpufreq/cpufreq.c +34 -1
@@ -1269,10 +1269,10 @@ static int cpufreq_online(unsigned int cpu) ret =cpufreq_add_dev_interface (policy);if (ret) goto out_exit_policy; +write_lock_irqsave (&cpufreq_driver_lock, flags);blocking_notifier_call_chain (&cpufreq_policy_notifier_list,CPUFREQ_CREATE_POLICY , policy); -write_lock_irqsave (&cpufreq_driver_lock, flags);list_add (&policy->policy_list, &cpufreq_policy_list);write_unlock_irqrestore (&cpufreq_driver_lock, flags); }
@@ -1728,6 +1728,8 @@ EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);*/ int cpufreq_register_notifier (struct notifier_block *nb,unsigned int list) { +struct cpufreq_policy *policy; +unsigned long flags;int ret;if (cpufreq_disabled ())
@@ -1751,8 +1753,28 @@ int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)mutex_unlock (&cpufreq_fast_switch_lock);break ;case CPUFREQ_POLICY_NOTIFIER : +write_lock_irqsave (&cpufreq_driver_lock, flags); + +/* Notify about all existing policies */ +for_each_policy (policy) { + nb->notifier_call (nb,CPUFREQ_CREATE_POLICY , + policy); +if (policy_is_inactive (policy)) +continue ; + + nb->notifier_call (nb,CPUFREQ_START , policy); + } + ret =blocking_notifier_chain_register ( &cpufreq_policy_notifier_list, nb); +if (ret) { +/* Notify about all existing policies */ +for_each_policy (policy) { + nb->notifier_call (nb,CPUFREQ_REMOVE_POLICY , + policy); + } + } +write_unlock_irqrestore (&cpufreq_driver_lock, flags);break ;default : ret = -EINVAL ;
@@ -1774,6 +1796,8 @@ EXPORT_SYMBOL(cpufreq_register_notifier);*/ int cpufreq_unregister_notifier (struct notifier_block *nb,unsigned int list) { +struct cpufreq_policy *policy; +unsigned long flags;int ret;if (cpufreq_disabled ())
@@ -1793,6 +1817,15 @@ int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)case CPUFREQ_POLICY_NOTIFIER : ret =blocking_notifier_chain_unregister ( &cpufreq_policy_notifier_list, nb); +if (!ret) { +write_lock_irqsave (&cpufreq_driver_lock, flags); +/* Notify about all existing policies */ +for_each_policy (policy) { + nb->notifier_call (nb,CPUFREQ_REMOVE_POLICY , + policy); + } +write_unlock_irqrestore (&cpufreq_driver_lock, flags); + }break ;default : ret = -EINVAL ;
drivers/cpufreq/cpufreq_stats.c +7 -48
104 lines not on this page · raw message
Quoted 32 lines · Viresh Kumar
Quoted 2 lines · Rafael J. Wysocki
Assuming that the loops are over online CPUs and not over possible CPUs I suppose?
I wasn't focussing on that loop lately but the
policy->rwsem:)Quoted 7 lines
Anyway, if you are talking about the code without the patch (which I guess is the case), the reason why it is racy is because, if
cpufreq_stats_init()runs in parallel with CPU online, the CPU going online may be missed by it. To my eyes that happens ifcpufreq_online()has already advanced beyond the point where the notifier would have been invoked, but hasn't returned yet when thefor_each_online_cpu()loop incpufreq_stats_init()is executed.Yes. That's a race we need to fix. I agree.
Quoted 4 lines
Worse yet, if a CPU goes offline when
cpufreq_stats_exit()is running and that happens exactly between the notifier unregistration and thefor_each_online_cpu()loop, the stats table will never be freed for that CPU (say the policy isn't shared).Same here.
Quoted 8 lines
Switching over to loops over possible CPUs doesn't address those races (at least not the second one), and I'm not really sure why I thought it would address them, but adding CPU online/offline locking to cpufreq_stats_init/exit() can address them, so it looks like the very first version of my patch (ie.
https://patchwork.kernel.org/patch/9128509/) was actually correct, because it didn't put too much code under the CPU offline/online locking. :-)
Quoted 4 lines
Well, I think there is one more way of getting all this fixed, which may eventually look much more cleaner.
What if we update cpufreq core instead of stats with something like this:
The problem is in the stats module, so it needs to be addressed in there.
We can modify the core too if there are other problems with notifiers that need to be addressed.
For now, I'm going to apply https://patchwork.kernel.org/patch/9128509/
unless you point out a technical problem with it.
Quoted 6 lines
-------------------------8<-------------------------
From: Viresh Kumar <[email protected]>
Date: Tue, 24 May 2016 10:16:25 +0530
Subject: [PATCH] cpufreq: Initiate notifiers for existing policy
Quoted 6 lines
Races are possible in the init/exit paths of the cpufreq-stats layer, which may lead to 'stats' sysfs directory not getting created or removed for some of the policies. This can happen while the policy is getting created while cpufreq_stats_init/exit() are getting called.
To avoid adding unnecessary locks in the init/exit paths of the
I don't really get it why you don't like get/put_online_cpus() so much.
Quite arguably, they aren't unnecessary in the stats code, because looping over online CPUs without ensuring that the mask won't change when the loop is in progress is sort of buggy anyway.
Quoted 6 lines
cpufreq-stats layer, update the policy notifier register/unregister routines to send notifications for any existing cpufreq policies.
Also make sure (with help of
cpufreq_driver_lock) that CPUFREQ_CREATE/REMOVE notifiers aren't getting issued in parallel from the policy creation/removal paths.
So you add a lot of complexity to the core just in order to avoid the extra get/put_online_cpus() invocations? And what about people trying to understand why the hell the code is not racy in the future?
No, sorry.
Thanks,
Rafael
Viresh Kumar
maintainerQuoted 1 line · Rafael J. Wysocki
I don't really get it why you don't like get/put_online_cpus() so much.
Not that I don't like them, I just wanted to see if its possible to work without any additional locking.
Anyway, so the first version of your patch did the get_online_cpus() around a bigger section of the code instead of just the for_each_online_cpu() loop. Should we change that?
Signature · 1 line
viresh
Quoted 9 lines · Viresh Kumar
Quoted 1 line · Rafael J. Wysocki
I don't really get it why you don't like get/put_online_cpus() so much.
Not that I don't like them, I just wanted to see if its possible to work without any additional locking.
Anyway, so the first version of your patch did the
get_online_cpus()around a bigger section of the code instead of just thefor_each_online_cpu()loop. Should we change that?
It did that, because locking around the loop alone wouldn't close the races I'm concerned about.
That said, those same races are possible when the cpufreq driver is loaded along with the cpufreq_stats module or is unloaded along with that module. Again, if ether a CPU goes online or the cpufreq driver is loaded during cpufreq_stats_init(), the new CPU *or* a new policy may be missed by it. Similarly, if either a CPU goes offline or the cpufreq driver is unloaded during cpufreq_stats_exit(), that CPU or a policy that has just gone away may be missed by it.
The CPU "hotplug" locking is not sufficient to close the races related to the cpufreq driver load/unload, so an alternative approach is needed. IMO, however, changing the way the notifiers work is not the way to go here, because the problem is specific to the stats module. Unless there are other users of the notifiers with the same problem, it should be addressed in the stats code.
The stats code looks all pants to me now, TBH, and the way it uses notifiers (both policy notifiers and transition notifiers) is questionable at the very least. It looks like it would just be better to redesign it from scratch.
Thanks,
Rafael
Rafael J. Wysocki
maintainerQuoted 29 lines · Rafael J. Wysocki
Quoted 9 lines · Viresh Kumar
Quoted 1 line · Rafael J. Wysocki
I don't really get it why you don't like get/put_online_cpus() so much.
Not that I don't like them, I just wanted to see if its possible to work without any additional locking.
Anyway, so the first version of your patch did the
get_online_cpus()around a bigger section of the code instead of just thefor_each_online_cpu()loop. Should we change that?It did that, because locking around the loop alone wouldn't close the races I'm concerned about.
That said, those same races are possible when the cpufreq driver is loaded along with the
cpufreq_statsmodule or is unloaded along with that module. Again, if ether a CPU goes online or the cpufreq driver is loaded duringcpufreq_stats_init(), the new CPU *or* a new policy may be missed by it. Similarly, if either a CPU goes offline or the cpufreq driver is unloaded duringcpufreq_stats_exit(), that CPU or a policy that has just gone away may be missed by it.The CPU "hotplug" locking is not sufficient to close the races related to the cpufreq driver load/unload, so an alternative approach is needed. IMO, however, changing the way the notifiers work is not the way to go here, because the problem is specific to the stats module. Unless there are other users of the notifiers with the same problem, it should be addressed in the stats code.
Quoted 3 lines
The stats code looks all pants to me now, TBH, and the way it uses notifiers (both policy notifiers and transition notifiers) is questionable at the very least. It looks like it would just be better to redesign it from scratch.
I'm actually considering making the stats code non-modular.
It won't have a reason to use notifiers then and may be simplified quite a bit this way. As an added benefit, if it doesn't use the transition notifier, it won't interfere with fast frequency switching in the schedutil governor any more.
Are there any drawbacks?
Viresh Kumar
maintainerQuoted 8 lines · Rafael J. Wysocki
I'm actually considering making the stats code non-modular.
It won't have a reason to use notifiers then and may be simplified quite a bit this way. As an added benefit, if it doesn't use the transition notifier, it won't interfere with fast frequency switching in the schedutil governor any more.
Are there any drawbacks?
I don't think so. Please do that :)
Signature · 1 line
viresh