Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cpufreq: Init completion before kobject_init_and_add()

In cpufreq_policy_alloc(), it will call uninitialed completion in
cpufreq_sysfs_release() when kobject_init_and_add() fails. And
that will cause a crash such as the following page fault in complete:

BUG: unable to handle page fault for address: fffffffffffffff8
[..]
RIP: 0010:complete+0x98/0x1f0
[..]
Call Trace:
kobject_put+0x1be/0x4c0
cpufreq_online.cold+0xee/0x1fd
cpufreq_add_dev+0x183/0x1e0
subsys_interface_register+0x3f5/0x4e0
cpufreq_register_driver+0x3b7/0x670
acpi_cpufreq_init+0x56c/0x1000 [acpi_cpufreq]
do_one_initcall+0x13d/0x780
do_init_module+0x1c3/0x630
load_module+0x6e67/0x73b0
__do_sys_finit_module+0x181/0x240
do_syscall_64+0x35/0x80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

Fixes: 4ebe36c94aed ("cpufreq: Fix kobject memleak")
Signed-off-by: Yongqiang Liu <liuyongqiang13@huawei.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Cc: 5.2+ <stable@vger.kernel.org> # 5.2+
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

authored by

Yongqiang Liu and committed by
Rafael J. Wysocki
5c510548 c7a5518a

+1 -1
+1 -1
drivers/cpufreq/cpufreq.c
··· 1207 1207 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 1208 1208 goto err_free_rcpumask; 1209 1209 1210 + init_completion(&policy->kobj_unregister); 1210 1211 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 1211 1212 cpufreq_global_kobject, "policy%u", cpu); 1212 1213 if (ret) { ··· 1246 1245 init_rwsem(&policy->rwsem); 1247 1246 spin_lock_init(&policy->transition_lock); 1248 1247 init_waitqueue_head(&policy->transition_wait); 1249 - init_completion(&policy->kobj_unregister); 1250 1248 INIT_WORK(&policy->update, handle_update); 1251 1249 1252 1250 policy->cpu = cpu;