cpufreq: cancel any pending policy update work scheduled before freeing
PATCH v2 1/1 cpufreq: cancel any pending policy update work scheduled before freeing
Schedules policy update work may end up racing with the freeing of the policy and unregistering the driver.
One possible race is as below where the cpufreq_driver is unregistered but the scheduled work gets executed at later stage when cpufreq_driver is NULL(i.e. after freeing the policy and driver)
Unable to handle kernel NULL pointer dereference at virtual address 0000001c pgd = (ptrval) [0000001c] *pgd=80000080204003, *pmd=00000000 Internal error: Oops: 206 [#1] SMP THUMB2 Modules linked in: CPU: 0 PID: 34 Comm: kworker/0:1 Not tainted 5.4.0-rc3-00006-g67f5a8081a4b #86 Hardware name: ARM-Versatile Express Workqueue: events handle_update PC is at cpufreq_set_policy+0x58/0x228 LR is at dev_pm_qos_read_value+0x77/0xac Control: 70c5387d Table: 80203000 DAC: fffffffd Process kworker/0:1 (pid: 34, stack limit = 0x(ptrval)) (cpufreq_set_policy) from (refresh_frequency_limits.part.24+0x37/0x48) (refresh_frequency_limits.part.24) from (handle_update+0x2f/0x38) (handle_update) from (process_one_work+0x16d/0x3cc) (process_one_work) from (worker_thread+0xff/0x414) (worker_thread) from (kthread+0xff/0x100) (kthread) from (ret_from_fork+0x11/0x28)
--- drivers/cpufreq/cpufreq.c | 2 ++ 1 file changed, 2 insertions(+)
v1->v2:
- Dropped check for work_pending as it can still be racy
- Changed from flush_work to cancel_work_sync
drivers/cpufreq/cpufreq.c +2 -0
@@ -1279,6 +1279,8 @@ static void cpufreq_policy_free(struct cpufreq_policy *policy) }dev_pm_qos_remove_request (policy->min_freq_req); +/* cancel any pending policy->update work before freeing the policy */ +cancel_work_sync (&policy->update);kfree (policy->min_freq_req);cpufreq_policy_put_kobj (policy);
--
2.17.1
Rafael J. Wysocki
maintainerQuoted 43 lines · Sudeep Holla
Schedules policy update work may end up racing with the freeing of the policy and unregistering the driver.
One possible race is as below where the
cpufreq_driveris unregistered but the scheduled work gets executed at later stage whencpufreq_driveris NULL(i.e. after freeing the policy and driver)Unable to handle kernel NULL pointer dereference at virtual address 0000001c pgd = (ptrval) [0000001c] *pgd=80000080204003, *pmd=00000000 Internal error: Oops: 206 [#1] SMP THUMB2 Modules linked in: CPU: 0 PID: 34 Comm: kworker/0:1 Not tainted 5.4.0-rc3-00006-g67f5a8081a4b #86 Hardware name: ARM-Versatile Express Workqueue: events handle_update PC is at cpufreq_set_policy+0x58/0x228 LR is at dev_pm_qos_read_value+0x77/0xac Control: 70c5387d Table: 80203000 DAC: fffffffd Process kworker/0:1 (pid: 34, stack limit = 0x(ptrval)) (cpufreq_set_policy) from (refresh_frequency_limits.part.24+0x37/0x48) (refresh_frequency_limits.part.24) from (handle_update+0x2f/0x38) (handle_update) from (process_one_work+0x16d/0x3cc) (process_one_work) from (worker_thread+0xff/0x414) (worker_thread) from (kthread+0xff/0x100) (kthread) from (ret_from_fork+0x11/0x28)Cc "Rafael J. Wysocki" <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Sudeep Holla <[email protected]>--- drivers/cpufreq/cpufreq.c | 2 ++ 1 file changed, 2 insertions(+)
v1->v2:
- Dropped check forwork_pendingas it can still be racy
- Changed fromflush_worktocancel_work_syncdrivers/cpufreq/cpufreq.c +0
-0
drivers/cpufreq/cpufreq.c static void cpufreq_policy_free(struct cpufreq_policy *policy)
Quoted 9 lines
drivers/cpufreq/cpufreq.c +2
-1@@ -1279,6 +1279,8 @@ static void cpufreq_policy_free(struct cpufreq_policy *policy) }dev_pm_qos_remove_request (policy->min_freq_req); +/* cancel any pending policy->update work before freeing the policy */ +cancel_work_sync (&policy->update);kfree (policy->min_freq_req);cpufreq_policy_put_kobj (policy); --
Applying as a fix for 5.4, thanks!
Quoted 42 lines · Sudeep Holla
Schedules policy update work may end up racing with the freeing of the policy and unregistering the driver.
One possible race is as below where the
cpufreq_driveris unregistered but the scheduled work gets executed at later stage whencpufreq_driveris NULL(i.e. after freeing the policy and driver)Unable to handle kernel NULL pointer dereference at virtual address 0000001c pgd = (ptrval) [0000001c] *pgd=80000080204003, *pmd=00000000 Internal error: Oops: 206 [#1] SMP THUMB2 Modules linked in: CPU: 0 PID: 34 Comm: kworker/0:1 Not tainted 5.4.0-rc3-00006-g67f5a8081a4b #86 Hardware name: ARM-Versatile Express Workqueue: events handle_update PC is at cpufreq_set_policy+0x58/0x228 LR is at dev_pm_qos_read_value+0x77/0xac Control: 70c5387d Table: 80203000 DAC: fffffffd Process kworker/0:1 (pid: 34, stack limit = 0x(ptrval)) (cpufreq_set_policy) from (refresh_frequency_limits.part.24+0x37/0x48) (refresh_frequency_limits.part.24) from (handle_update+0x2f/0x38) (handle_update) from (process_one_work+0x16d/0x3cc) (process_one_work) from (worker_thread+0xff/0x414) (worker_thread) from (kthread+0xff/0x100) (kthread) from (ret_from_fork+0x11/0x28)Cc "Rafael J. Wysocki" <[email protected]>Cc Viresh Kumar <[email protected]>Signed-off-by Sudeep Holla <[email protected]>--- drivers/cpufreq/cpufreq.c | 2 ++ 1 file changed, 2 insertions(+)
v1->v2:
- Dropped check forwork_pendingas it can still be racy
- Changed fromflush_worktocancel_work_syncdrivers/cpufreq/cpufreq.c +0
-0
drivers/cpufreq/cpufreq.c static void cpufreq_policy_free(struct cpufreq_policy *policy) · line 1286
Quoted 8 lines
drivers/cpufreq/cpufreq.c +2
-0@@ -1279,6 +1279,8 @@ static void cpufreq_policy_free(struct cpufreq_policy *policy) }dev_pm_qos_remove_request (policy->min_freq_req); +/* cancel any pending policy->update work before freeing the policy */ +cancel_work_sync (&policy->update);kfree (policy->min_freq_req);cpufreq_policy_put_kobj (policy);
Signature · 1 line
viresh