init_powernv_pstates() allocates hash entries to build the P-state
reverse map. These entries are leaked if allocation fails partway
through initialization, if subsequent chip initialization or cpufreq
driver registration fails, or when the module is unloaded.

Add a helper to free the reverse map and call it on all relevant
initialization failure paths and during module exit.

Fixes: 332f0a01f0dd ("powernv-cpufreq: Fix pstate_to_idx() to handle 
non-continguous pstates")
Signed-off-by: Haoxiang Li <[email protected]>
---
 drivers/cpufreq/powernv-cpufreq.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/cpufreq/powernv-cpufreq.c 
b/drivers/cpufreq/powernv-cpufreq.c
index 42ff23e14095..7015beec3f9d 100644
--- a/drivers/cpufreq/powernv-cpufreq.c
+++ b/drivers/cpufreq/powernv-cpufreq.c
@@ -1108,6 +1108,18 @@ static inline void unregister_all_notifiers(void)
        unregister_reboot_notifier(&powernv_cpufreq_reboot_nb);
 }
 
+static void free_pstate_revmap(void)
+{
+       struct pstate_idx_revmap_data *entry;
+       struct hlist_node *tmp;
+       unsigned int bucket;
+
+       hash_for_each_safe(pstate_revmap, bucket, tmp, entry, hentry) {
+               hash_del(&entry->hentry);
+               kfree(entry);
+       }
+}
+
 static int __init powernv_cpufreq_init(void)
 {
        int rc = 0;
@@ -1142,6 +1154,7 @@ static int __init powernv_cpufreq_init(void)
 cleanup:
        clean_chip_info();
 out:
+       free_pstate_revmap();
        pr_info("Platform driver disabled. System does not support PState 
control\n");
        return rc;
 }
@@ -1152,6 +1165,7 @@ static void __exit powernv_cpufreq_exit(void)
        cpufreq_unregister_driver(&powernv_cpufreq_driver);
        unregister_all_notifiers();
        clean_chip_info();
+       free_pstate_revmap();
 }
 module_exit(powernv_cpufreq_exit);
 
-- 
2.25.1


Reply via email to