Merge tag 'devfreq-fixes-for-5.19-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/chanwoo/linux

Pull devfreq fixes for 5.19-rc5 from Chanwoo Choi:

"1. Fix devfreq passive governor issue when cpufreq policies are not
ready during kernel boot because some CPUs turn on after kernel
booting or others.

- Re-initialize the vairables of struct devfreq_passive_data when
PROBE_DEFER happens when cpufreq_get() returns NULL.

- Use dev_err_probe to mute warning when PROBE_DEFER.

- Fix cpufreq passive unregister erroring on PROBE_DEFER
by using the allocated parent_cpu_data list to free resouce
instead of for_each_possible_cpu().

- Remove duplicate cpufreq passive unregister and warning when
PROBE_DEFER.

- Use HZ_PER_KZH macro in units.h.

- Fix wrong indentation in SPDX-License line.

2. Fix reference count leak in exynos-ppmu.c by using of_node_put().

3. Rework freq_table to be local to devfreq struct

- struct devfreq_dev_profile includes freq_table array to store
the supported frequencies. If devfreq driver doesn't initialize
the freq_table, devfreq core allocates the memory and initializes
the freq_table.

On a devfreq PROBE_DEFER, the freq_table in the driver profile
struct is never reset and may be left in an undefined state. To fix
this and correctly handle PROBE_DEFER, use a local freq_table and
max_state in the devfreq struct."

* tag 'devfreq-fixes-for-5.19-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/chanwoo/linux:
PM / devfreq: passive: revert an editing accident in SPDX-License line
PM / devfreq: Fix kernel warning with cpufreq passive register fail
PM / devfreq: Rework freq_table to be local to devfreq struct
PM / devfreq: exynos-ppmu: Fix refcount leak in of_get_devfreq_events
PM / devfreq: passive: Use HZ_PER_KHZ macro in units.h
PM / devfreq: Fix cpufreq passive unregister erroring on PROBE_DEFER
PM / devfreq: Mute warning on governor PROBE_DEFER
PM / devfreq: Fix kernel panic with cpu based scaling to passive gov

+75 -76
+37 -39
drivers/devfreq/devfreq.c
··· 123 unsigned long *min_freq, 124 unsigned long *max_freq) 125 { 126 - unsigned long *freq_table = devfreq->profile->freq_table; 127 s32 qos_min_freq, qos_max_freq; 128 129 lockdep_assert_held(&devfreq->lock); ··· 133 * The devfreq drivers can initialize this in either ascending or 134 * descending order and devfreq core supports both. 135 */ 136 - if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) { 137 *min_freq = freq_table[0]; 138 - *max_freq = freq_table[devfreq->profile->max_state - 1]; 139 } else { 140 - *min_freq = freq_table[devfreq->profile->max_state - 1]; 141 *max_freq = freq_table[0]; 142 } 143 ··· 169 { 170 int lev; 171 172 - for (lev = 0; lev < devfreq->profile->max_state; lev++) 173 - if (freq == devfreq->profile->freq_table[lev]) 174 return lev; 175 176 return -EINVAL; ··· 178 179 static int set_freq_table(struct devfreq *devfreq) 180 { 181 - struct devfreq_dev_profile *profile = devfreq->profile; 182 struct dev_pm_opp *opp; 183 unsigned long freq; 184 int i, count; ··· 187 if (count <= 0) 188 return -EINVAL; 189 190 - profile->max_state = count; 191 - profile->freq_table = devm_kcalloc(devfreq->dev.parent, 192 - profile->max_state, 193 - sizeof(*profile->freq_table), 194 - GFP_KERNEL); 195 - if (!profile->freq_table) { 196 - profile->max_state = 0; 197 return -ENOMEM; 198 - } 199 200 - for (i = 0, freq = 0; i < profile->max_state; i++, freq++) { 201 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq); 202 if (IS_ERR(opp)) { 203 - devm_kfree(devfreq->dev.parent, profile->freq_table); 204 - profile->max_state = 0; 205 return PTR_ERR(opp); 206 } 207 dev_pm_opp_put(opp); 208 - profile->freq_table[i] = freq; 209 } 210 211 return 0; ··· 242 243 if (lev != prev_lev) { 244 devfreq->stats.trans_table[ 245 - (prev_lev * devfreq->profile->max_state) + lev]++; 246 devfreq->stats.total_trans++; 247 } 248 ··· 831 if (err < 0) 832 goto err_dev; 833 mutex_lock(&devfreq->lock); 834 } 835 836 devfreq->scaling_min_freq = find_available_min_freq(devfreq); ··· 869 870 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev, 871 array3_size(sizeof(unsigned int), 872 - devfreq->profile->max_state, 873 - devfreq->profile->max_state), 874 GFP_KERNEL); 875 if (!devfreq->stats.trans_table) { 876 mutex_unlock(&devfreq->lock); ··· 879 } 880 881 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev, 882 - devfreq->profile->max_state, 883 sizeof(*devfreq->stats.time_in_state), 884 GFP_KERNEL); 885 if (!devfreq->stats.time_in_state) { ··· 931 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START, 932 NULL); 933 if (err) { 934 - dev_err(dev, "%s: Unable to start governor for the device\n", 935 - __func__); 936 goto err_init; 937 } 938 create_sysfs_files(devfreq, devfreq->governor); ··· 1665 1666 mutex_lock(&df->lock); 1667 1668 - for (i = 0; i < df->profile->max_state; i++) 1669 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), 1670 - "%lu ", df->profile->freq_table[i]); 1671 1672 mutex_unlock(&df->lock); 1673 /* Truncate the trailing space */ ··· 1690 1691 if (!df->profile) 1692 return -EINVAL; 1693 - max_state = df->profile->max_state; 1694 1695 if (max_state == 0) 1696 return sprintf(buf, "Not Supported.\n"); ··· 1707 len += sprintf(buf + len, " :"); 1708 for (i = 0; i < max_state; i++) 1709 len += sprintf(buf + len, "%10lu", 1710 - df->profile->freq_table[i]); 1711 1712 len += sprintf(buf + len, " time(ms)\n"); 1713 1714 for (i = 0; i < max_state; i++) { 1715 - if (df->profile->freq_table[i] 1716 - == df->previous_freq) { 1717 len += sprintf(buf + len, "*"); 1718 - } else { 1719 len += sprintf(buf + len, " "); 1720 - } 1721 - len += sprintf(buf + len, "%10lu:", 1722 - df->profile->freq_table[i]); 1723 for (j = 0; j < max_state; j++) 1724 len += sprintf(buf + len, "%10u", 1725 df->stats.trans_table[(i * max_state) + j]); ··· 1741 if (!df->profile) 1742 return -EINVAL; 1743 1744 - if (df->profile->max_state == 0) 1745 return count; 1746 1747 err = kstrtoint(buf, 10, &value); ··· 1749 return -EINVAL; 1750 1751 mutex_lock(&df->lock); 1752 - memset(df->stats.time_in_state, 0, (df->profile->max_state * 1753 sizeof(*df->stats.time_in_state))); 1754 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int), 1755 - df->profile->max_state, 1756 - df->profile->max_state)); 1757 df->stats.total_trans = 0; 1758 df->stats.last_update = get_jiffies_64(); 1759 mutex_unlock(&df->lock);
··· 123 unsigned long *min_freq, 124 unsigned long *max_freq) 125 { 126 + unsigned long *freq_table = devfreq->freq_table; 127 s32 qos_min_freq, qos_max_freq; 128 129 lockdep_assert_held(&devfreq->lock); ··· 133 * The devfreq drivers can initialize this in either ascending or 134 * descending order and devfreq core supports both. 135 */ 136 + if (freq_table[0] < freq_table[devfreq->max_state - 1]) { 137 *min_freq = freq_table[0]; 138 + *max_freq = freq_table[devfreq->max_state - 1]; 139 } else { 140 + *min_freq = freq_table[devfreq->max_state - 1]; 141 *max_freq = freq_table[0]; 142 } 143 ··· 169 { 170 int lev; 171 172 + for (lev = 0; lev < devfreq->max_state; lev++) 173 + if (freq == devfreq->freq_table[lev]) 174 return lev; 175 176 return -EINVAL; ··· 178 179 static int set_freq_table(struct devfreq *devfreq) 180 { 181 struct dev_pm_opp *opp; 182 unsigned long freq; 183 int i, count; ··· 188 if (count <= 0) 189 return -EINVAL; 190 191 + devfreq->max_state = count; 192 + devfreq->freq_table = devm_kcalloc(devfreq->dev.parent, 193 + devfreq->max_state, 194 + sizeof(*devfreq->freq_table), 195 + GFP_KERNEL); 196 + if (!devfreq->freq_table) 197 return -ENOMEM; 198 199 + for (i = 0, freq = 0; i < devfreq->max_state; i++, freq++) { 200 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq); 201 if (IS_ERR(opp)) { 202 + devm_kfree(devfreq->dev.parent, devfreq->freq_table); 203 return PTR_ERR(opp); 204 } 205 dev_pm_opp_put(opp); 206 + devfreq->freq_table[i] = freq; 207 } 208 209 return 0; ··· 246 247 if (lev != prev_lev) { 248 devfreq->stats.trans_table[ 249 + (prev_lev * devfreq->max_state) + lev]++; 250 devfreq->stats.total_trans++; 251 } 252 ··· 835 if (err < 0) 836 goto err_dev; 837 mutex_lock(&devfreq->lock); 838 + } else { 839 + devfreq->freq_table = devfreq->profile->freq_table; 840 + devfreq->max_state = devfreq->profile->max_state; 841 } 842 843 devfreq->scaling_min_freq = find_available_min_freq(devfreq); ··· 870 871 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev, 872 array3_size(sizeof(unsigned int), 873 + devfreq->max_state, 874 + devfreq->max_state), 875 GFP_KERNEL); 876 if (!devfreq->stats.trans_table) { 877 mutex_unlock(&devfreq->lock); ··· 880 } 881 882 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev, 883 + devfreq->max_state, 884 sizeof(*devfreq->stats.time_in_state), 885 GFP_KERNEL); 886 if (!devfreq->stats.time_in_state) { ··· 932 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START, 933 NULL); 934 if (err) { 935 + dev_err_probe(dev, err, 936 + "%s: Unable to start governor for the device\n", 937 + __func__); 938 goto err_init; 939 } 940 create_sysfs_files(devfreq, devfreq->governor); ··· 1665 1666 mutex_lock(&df->lock); 1667 1668 + for (i = 0; i < df->max_state; i++) 1669 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), 1670 + "%lu ", df->freq_table[i]); 1671 1672 mutex_unlock(&df->lock); 1673 /* Truncate the trailing space */ ··· 1690 1691 if (!df->profile) 1692 return -EINVAL; 1693 + max_state = df->max_state; 1694 1695 if (max_state == 0) 1696 return sprintf(buf, "Not Supported.\n"); ··· 1707 len += sprintf(buf + len, " :"); 1708 for (i = 0; i < max_state; i++) 1709 len += sprintf(buf + len, "%10lu", 1710 + df->freq_table[i]); 1711 1712 len += sprintf(buf + len, " time(ms)\n"); 1713 1714 for (i = 0; i < max_state; i++) { 1715 + if (df->freq_table[i] == df->previous_freq) 1716 len += sprintf(buf + len, "*"); 1717 + else 1718 len += sprintf(buf + len, " "); 1719 + 1720 + len += sprintf(buf + len, "%10lu:", df->freq_table[i]); 1721 for (j = 0; j < max_state; j++) 1722 len += sprintf(buf + len, "%10u", 1723 df->stats.trans_table[(i * max_state) + j]); ··· 1743 if (!df->profile) 1744 return -EINVAL; 1745 1746 + if (df->max_state == 0) 1747 return count; 1748 1749 err = kstrtoint(buf, 10, &value); ··· 1751 return -EINVAL; 1752 1753 mutex_lock(&df->lock); 1754 + memset(df->stats.time_in_state, 0, (df->max_state * 1755 sizeof(*df->stats.time_in_state))); 1756 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int), 1757 + df->max_state, 1758 + df->max_state)); 1759 df->stats.total_trans = 0; 1760 df->stats.last_update = get_jiffies_64(); 1761 mutex_unlock(&df->lock);
+6 -2
drivers/devfreq/event/exynos-ppmu.c
··· 519 520 count = of_get_child_count(events_np); 521 desc = devm_kcalloc(dev, count, sizeof(*desc), GFP_KERNEL); 522 - if (!desc) 523 return -ENOMEM; 524 info->num_events = count; 525 526 of_id = of_match_device(exynos_ppmu_id_match, dev); 527 if (of_id) 528 info->ppmu_type = (enum exynos_ppmu_type)of_id->data; 529 - else 530 return -EINVAL; 531 532 j = 0; 533 for_each_child_of_node(events_np, node) {
··· 519 520 count = of_get_child_count(events_np); 521 desc = devm_kcalloc(dev, count, sizeof(*desc), GFP_KERNEL); 522 + if (!desc) { 523 + of_node_put(events_np); 524 return -ENOMEM; 525 + } 526 info->num_events = count; 527 528 of_id = of_match_device(exynos_ppmu_id_match, dev); 529 if (of_id) 530 info->ppmu_type = (enum exynos_ppmu_type)of_id->data; 531 + else { 532 + of_node_put(events_np); 533 return -EINVAL; 534 + } 535 536 j = 0; 537 for_each_child_of_node(events_np, node) {
+27 -35
drivers/devfreq/governor_passive.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/drivers/devfreq/governor_passive.c 4 * ··· 14 #include <linux/slab.h> 15 #include <linux/device.h> 16 #include <linux/devfreq.h> 17 #include "governor.h" 18 - 19 - #define HZ_PER_KHZ 1000 20 21 static struct devfreq_cpu_data * 22 get_parent_cpu_data(struct devfreq_passive_data *p_data, ··· 31 return parent_cpu_data; 32 33 return NULL; 34 } 35 36 static unsigned long get_target_freq_by_required_opp(struct device *p_dev, ··· 144 goto out; 145 146 /* Use interpolation if required opps is not available */ 147 - for (i = 0; i < parent_devfreq->profile->max_state; i++) 148 - if (parent_devfreq->profile->freq_table[i] == *freq) 149 break; 150 151 - if (i == parent_devfreq->profile->max_state) 152 return -EINVAL; 153 154 - if (i < devfreq->profile->max_state) { 155 - child_freq = devfreq->profile->freq_table[i]; 156 } else { 157 - count = devfreq->profile->max_state; 158 - child_freq = devfreq->profile->freq_table[count - 1]; 159 } 160 161 out: ··· 235 { 236 struct devfreq_passive_data *p_data 237 = (struct devfreq_passive_data *)devfreq->data; 238 - struct devfreq_cpu_data *parent_cpu_data; 239 - int cpu, ret = 0; 240 241 if (p_data->nb.notifier_call) { 242 ret = cpufreq_unregister_notifier(&p_data->nb, ··· 244 return ret; 245 } 246 247 - for_each_possible_cpu(cpu) { 248 - struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 249 - if (!policy) { 250 - ret = -EINVAL; 251 - continue; 252 - } 253 254 - parent_cpu_data = get_parent_cpu_data(p_data, policy); 255 - if (!parent_cpu_data) { 256 - cpufreq_cpu_put(policy); 257 - continue; 258 - } 259 - 260 - list_del(&parent_cpu_data->node); 261 - if (parent_cpu_data->opp_table) 262 - dev_pm_opp_put_opp_table(parent_cpu_data->opp_table); 263 - kfree(parent_cpu_data); 264 - cpufreq_cpu_put(policy); 265 - } 266 - 267 - return ret; 268 } 269 270 static int cpufreq_passive_register_notifier(struct devfreq *devfreq) ··· 330 err_put_policy: 331 cpufreq_cpu_put(policy); 332 err: 333 - WARN_ON(cpufreq_passive_unregister_notifier(devfreq)); 334 335 return ret; 336 } ··· 400 if (!p_data) 401 return -EINVAL; 402 403 - if (!p_data->this) 404 - p_data->this = devfreq; 405 406 switch (event) { 407 case DEVFREQ_GOV_START:
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/drivers/devfreq/governor_passive.c 4 * ··· 14 #include <linux/slab.h> 15 #include <linux/device.h> 16 #include <linux/devfreq.h> 17 + #include <linux/units.h> 18 #include "governor.h" 19 20 static struct devfreq_cpu_data * 21 get_parent_cpu_data(struct devfreq_passive_data *p_data, ··· 32 return parent_cpu_data; 33 34 return NULL; 35 + } 36 + 37 + static void delete_parent_cpu_data(struct devfreq_passive_data *p_data) 38 + { 39 + struct devfreq_cpu_data *parent_cpu_data, *tmp; 40 + 41 + list_for_each_entry_safe(parent_cpu_data, tmp, &p_data->cpu_data_list, node) { 42 + list_del(&parent_cpu_data->node); 43 + 44 + if (parent_cpu_data->opp_table) 45 + dev_pm_opp_put_opp_table(parent_cpu_data->opp_table); 46 + 47 + kfree(parent_cpu_data); 48 + } 49 } 50 51 static unsigned long get_target_freq_by_required_opp(struct device *p_dev, ··· 131 goto out; 132 133 /* Use interpolation if required opps is not available */ 134 + for (i = 0; i < parent_devfreq->max_state; i++) 135 + if (parent_devfreq->freq_table[i] == *freq) 136 break; 137 138 + if (i == parent_devfreq->max_state) 139 return -EINVAL; 140 141 + if (i < devfreq->max_state) { 142 + child_freq = devfreq->freq_table[i]; 143 } else { 144 + count = devfreq->max_state; 145 + child_freq = devfreq->freq_table[count - 1]; 146 } 147 148 out: ··· 222 { 223 struct devfreq_passive_data *p_data 224 = (struct devfreq_passive_data *)devfreq->data; 225 + int ret; 226 227 if (p_data->nb.notifier_call) { 228 ret = cpufreq_unregister_notifier(&p_data->nb, ··· 232 return ret; 233 } 234 235 + delete_parent_cpu_data(p_data); 236 237 + return 0; 238 } 239 240 static int cpufreq_passive_register_notifier(struct devfreq *devfreq) ··· 336 err_put_policy: 337 cpufreq_cpu_put(policy); 338 err: 339 340 return ret; 341 } ··· 407 if (!p_data) 408 return -EINVAL; 409 410 + p_data->this = devfreq; 411 412 switch (event) { 413 case DEVFREQ_GOV_START:
+5
include/linux/devfreq.h
··· 148 * reevaluate operable frequencies. Devfreq users may use 149 * devfreq.nb to the corresponding register notifier call chain. 150 * @work: delayed work for load monitoring. 151 * @previous_freq: previously configured frequency value. 152 * @last_status: devfreq user device info, performance statistics 153 * @data: Private data of the governor. The devfreq framework does not ··· 186 struct opp_table *opp_table; 187 struct notifier_block nb; 188 struct delayed_work work; 189 190 unsigned long previous_freq; 191 struct devfreq_dev_status last_status;
··· 148 * reevaluate operable frequencies. Devfreq users may use 149 * devfreq.nb to the corresponding register notifier call chain. 150 * @work: delayed work for load monitoring. 151 + * @freq_table: current frequency table used by the devfreq driver. 152 + * @max_state: count of entry present in the frequency table. 153 * @previous_freq: previously configured frequency value. 154 * @last_status: devfreq user device info, performance statistics 155 * @data: Private data of the governor. The devfreq framework does not ··· 184 struct opp_table *opp_table; 185 struct notifier_block nb; 186 struct delayed_work work; 187 + 188 + unsigned long *freq_table; 189 + unsigned int max_state; 190 191 unsigned long previous_freq; 192 struct devfreq_dev_status last_status;