Skip to content

Commit 1a793ca

Browse files
zhang-ruigroeck
authored andcommitted
hwmon: (coretemp) Use dynamic allocated memory for core temp_data
The total memory needed for saving per core temperature data depends on the number of cores in a package. Using static allocated memory wastes memories on systems with low per package core count. Improve the code to use dynamic allocated memory so that it can be improved further when per package core count information becomes available. No functional change intended. Signed-off-by: Zhang Rui <[email protected]> Link: https://lore.kernel.org/r/[email protected] [groeck: Fixed continuation line alignment] Signed-off-by: Guenter Roeck <[email protected]>
1 parent 18b24a5 commit 1a793ca

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

drivers/hwmon/coretemp.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ struct temp_data {
9191
struct platform_data {
9292
struct device *hwmon_dev;
9393
u16 pkg_id;
94+
int nr_cores;
9495
struct ida ida;
9596
struct cpumask cpumask;
9697
struct temp_data *pkg_data;
97-
struct temp_data *core_data[NUM_REAL_CORES];
98+
struct temp_data **core_data;
9899
struct device_attribute name_attr;
99100
};
100101

@@ -480,6 +481,20 @@ init_temp_data(struct platform_data *pdata, unsigned int cpu, int pkg_flag)
480481
{
481482
struct temp_data *tdata;
482483

484+
if (!pdata->core_data) {
485+
/*
486+
* TODO:
487+
* The information of actual possible cores in a package is broken for now.
488+
* Will replace hardcoded NUM_REAL_CORES with actual per package core count
489+
* when this information becomes available.
490+
*/
491+
pdata->nr_cores = NUM_REAL_CORES;
492+
pdata->core_data = kcalloc(pdata->nr_cores, sizeof(struct temp_data *),
493+
GFP_KERNEL);
494+
if (!pdata->core_data)
495+
return NULL;
496+
}
497+
483498
tdata = kzalloc(sizeof(struct temp_data), GFP_KERNEL);
484499
if (!tdata)
485500
return NULL;
@@ -489,7 +504,7 @@ init_temp_data(struct platform_data *pdata, unsigned int cpu, int pkg_flag)
489504
/* Use tdata->index as indicator of package temp data */
490505
tdata->index = -1;
491506
} else {
492-
tdata->index = ida_alloc_max(&pdata->ida, NUM_REAL_CORES - 1, GFP_KERNEL);
507+
tdata->index = ida_alloc_max(&pdata->ida, pdata->nr_cores - 1, GFP_KERNEL);
493508
if (tdata->index < 0) {
494509
kfree(tdata);
495510
return NULL;
@@ -510,6 +525,9 @@ static void destroy_temp_data(struct platform_data *pdata, struct temp_data *tda
510525
{
511526
if (is_pkg_temp_data(tdata)) {
512527
pdata->pkg_data = NULL;
528+
kfree(pdata->core_data);
529+
pdata->core_data = NULL;
530+
pdata->nr_cores = 0;
513531
} else {
514532
pdata->core_data[tdata->index] = NULL;
515533
ida_free(&pdata->ida, tdata->index);
@@ -525,7 +543,7 @@ static struct temp_data *get_temp_data(struct platform_data *pdata, int cpu)
525543
if (cpu < 0)
526544
return pdata->pkg_data;
527545

528-
for (i = 0; i < NUM_REAL_CORES; i++) {
546+
for (i = 0; i < pdata->nr_cores; i++) {
529547
if (pdata->core_data[i] &&
530548
pdata->core_data[i]->cpu_core_id == topology_core_id(cpu))
531549
return pdata->core_data[i];

0 commit comments

Comments
 (0)