Skip to content

Commit bcb35da

Browse files
GustavoARSilvaalexdeucher
authored andcommitted
drm/amd/powerplay/smu10_hwmgr: use struct_size() in kzalloc()
One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example: struct foo { int stuff; struct boo entry[]; }; size = sizeof(struct foo) + count * sizeof(struct boo); instance = kzalloc(size, GFP_KERNEL); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL); Notice that, in this case, variable table_size is not necessary, hence it is removed. This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
1 parent 7f5725f commit bcb35da

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

drivers/gpu/drm/amd/powerplay/hwmgr/smu10_hwmgr.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,10 @@ static int smu10_construct_max_power_limits_table(struct pp_hwmgr *hwmgr,
139139
static int smu10_init_dynamic_state_adjustment_rule_settings(
140140
struct pp_hwmgr *hwmgr)
141141
{
142-
uint32_t table_size =
143-
sizeof(struct phm_clock_voltage_dependency_table) +
144-
(7 * sizeof(struct phm_clock_voltage_dependency_record));
142+
struct phm_clock_voltage_dependency_table *table_clk_vlt;
145143

146-
struct phm_clock_voltage_dependency_table *table_clk_vlt =
147-
kzalloc(table_size, GFP_KERNEL);
144+
table_clk_vlt = kzalloc(struct_size(table_clk_vlt, entries, 7),
145+
GFP_KERNEL);
148146

149147
if (NULL == table_clk_vlt) {
150148
pr_err("Can not allocate memory!\n");

0 commit comments

Comments
 (0)