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

Configure Feed

Select the types of activity you want to include in your feed.

drm/amd/display: Fix implicit narrowing conversions in modules

[Why]: Implicit narrowing of wider integer types (unsigned int, uint64_t)
into narrower fields (uint8_t, uint16_t, unsigned short) has potential
truncation issues.

[How]: For each warning site, added ASSERT(<value> <= 0xFFFF/0xFF) for
debug-mode bounds verification followed by an explicit cast. Typed
intermediate variables introduced where needed for clarity.

No functional change intended.

Reviewed-by: Dillon Varone <dillon.varone@amd.com>
Signed-off-by: Gaghik Khachatrian <gaghik.khachatrian@amd.com>
Signed-off-by: Roman Li <roman.li@amd.com>
Tested-by: Dan Wheeler <daniel.wheeler@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>

authored by

Gaghik Khachatrian and committed by
Alex Deucher
35540804 463a84da

+61 -28
+21 -11
drivers/gpu/drm/amd/display/modules/freesync/freesync.c
··· 153 153 * round down the vtotal value to avoid stretching vblank over 154 154 * panel's vtotal boundary. 155 155 */ 156 - v_total = div64_u64(div64_u64(((unsigned long long)( 156 + v_total = (unsigned int)div64_u64(div64_u64(((unsigned long long)( 157 157 frame_duration_in_ns) * (stream->timing.pix_clk_100hz / 10)), 158 158 stream->timing.h_total), 1000000); 159 159 } else if (refresh_in_uhz >= stream->timing.max_refresh_in_uhz) { ··· 161 161 * round up the vtotal value to prevent off-by-one error causing 162 162 * v_total_min to be below the panel's lower bound 163 163 */ 164 - v_total = div64_u64(div64_u64(((unsigned long long)( 164 + v_total = (unsigned int)div64_u64(div64_u64(((unsigned long long)( 165 165 frame_duration_in_ns) * (stream->timing.pix_clk_100hz / 10)), 166 166 stream->timing.h_total) + (1000000 - 1), 1000000); 167 167 } else { 168 - v_total = div64_u64(div64_u64(((unsigned long long)( 168 + v_total = (unsigned int)div64_u64(div64_u64(((unsigned long long)( 169 169 frame_duration_in_ns) * (stream->timing.pix_clk_100hz / 10)), 170 170 stream->timing.h_total) + 500000, 1000000); 171 171 } ··· 196 196 uint32_t h_total_up_scaled; 197 197 198 198 h_total_up_scaled = stream->timing.h_total * 10000; 199 - v_total = div_u64((unsigned long long)duration_in_us 199 + v_total = (unsigned int)div_u64((unsigned long long)duration_in_us 200 200 * stream->timing.pix_clk_100hz + (h_total_up_scaled - 1), 201 201 h_total_up_scaled); //ceiling for MMax and MMin for MVRR 202 202 } else { 203 - v_total = div64_u64(div64_u64(((unsigned long long)( 203 + v_total = (unsigned int)div64_u64(div64_u64(((unsigned long long)( 204 204 duration_in_us) * (stream->timing.pix_clk_100hz / 10)), 205 205 stream->timing.h_total), 1000); 206 206 } ··· 232 232 target_duration_in_us; 233 233 234 234 /* Calculate ratio between new and current frame duration with 3 digit */ 235 - unsigned int frame_duration_ratio = div64_u64(1000000, 235 + uint64_t frame_duration_ratio_u64 = div64_u64(1000000, 236 236 (1000 + div64_u64(((unsigned long long)( 237 237 STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) * 238 238 current_duration_in_us), 239 239 1000000))); 240 + ASSERT(frame_duration_ratio_u64 <= 0xFFFFFFFF); 241 + unsigned int frame_duration_ratio = (unsigned int)frame_duration_ratio_u64; 240 242 241 243 /* Calculate delta between new and current frame duration in us */ 242 - unsigned int frame_duration_delta = div64_u64(((unsigned long long)( 244 + uint64_t frame_duration_delta_u64 = div64_u64(((unsigned long long)( 243 245 current_duration_in_us) * 244 246 (1000 - frame_duration_ratio)), 1000); 247 + ASSERT(frame_duration_delta_u64 <= 0xFFFFFFFF); 248 + unsigned int frame_duration_delta = (unsigned int)frame_duration_delta_u64; 245 249 246 250 /* Adjust frame duration delta based on ratio between current and 247 251 * standard frame duration (frame duration at 60 Hz refresh rate). 248 252 */ 249 - unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)( 253 + uint64_t ramp_rate_interpolated_u64 = div64_u64(((unsigned long long)( 250 254 frame_duration_delta) * current_duration_in_us), 16666); 255 + ASSERT(ramp_rate_interpolated_u64 <= 0xFFFFFFFF); 256 + unsigned int ramp_rate_interpolated = (unsigned int)ramp_rate_interpolated_u64; 251 257 252 258 /* Going to a higher refresh rate (lower frame duration) */ 253 259 if (ramp_direction_is_up) { ··· 283 277 } 284 278 } 285 279 286 - v_total = div64_u64(div64_u64(((unsigned long long)( 280 + v_total = (unsigned int)div64_u64(div64_u64(((unsigned long long)( 287 281 current_duration_in_us) * (stream->timing.pix_clk_100hz / 10)), 288 282 stream->timing.h_total), 1000); 289 283 ··· 1064 1058 else 1065 1059 in_out_vrr->fixed_refresh_in_uhz = 0; 1066 1060 1067 - refresh_range = div_u64(in_out_vrr->max_refresh_in_uhz + 500000, 1000000) - 1068 - div_u64(in_out_vrr->min_refresh_in_uhz + 500000, 1000000); 1061 + { 1062 + uint64_t rr_tmp = div_u64(in_out_vrr->max_refresh_in_uhz + 500000, 1000000) - 1063 + div_u64(in_out_vrr->min_refresh_in_uhz + 500000, 1000000); 1064 + ASSERT(rr_tmp <= 0xFFFFFFFF); 1065 + refresh_range = (unsigned int)rr_tmp; 1066 + } 1069 1067 1070 1068 in_out_vrr->supported = true; 1071 1069 }
+34 -15
drivers/gpu/drm/amd/display/modules/power/power_helpers.c
··· 250 250 unsigned int lut_index; 251 251 252 252 table->backlight_thresholds[0] = 0; 253 - table->backlight_offsets[0] = params.backlight_lut_array[0]; 253 + ASSERT(params.backlight_lut_array[0] <= 0xFFFF); 254 + table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0]; 254 255 table->backlight_thresholds[num_entries-1] = 0xFFFF; 256 + ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF); 255 257 table->backlight_offsets[num_entries-1] = 256 - params.backlight_lut_array[params.backlight_lut_array_size - 1]; 258 + (uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1]; 257 259 258 260 /* Setup all brightness levels between 0% and 100% exclusive 259 261 * Fills brightness-to-backlight transform table. Backlight custom curve ··· 267 265 */ 268 266 for (i = 1; i+1 < num_entries; i++) { 269 267 lut_index = (params.backlight_lut_array_size - 1) * i / (num_entries - 1); 268 + 270 269 ASSERT(lut_index < params.backlight_lut_array_size); 271 270 272 - table->backlight_thresholds[i] = 273 - cpu_to_be16(DIV_ROUNDUP((i * 65536), num_entries)); 274 - table->backlight_offsets[i] = 275 - cpu_to_be16(params.backlight_lut_array[lut_index]); 271 + unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries); 272 + unsigned int offset_val = params.backlight_lut_array[lut_index]; 273 + 274 + ASSERT(threshold_val <= 0xFFFF); 275 + ASSERT(offset_val <= 0xFFFF); 276 + 277 + table->backlight_thresholds[i] = cpu_to_be16((uint16_t)threshold_val); 278 + table->backlight_offsets[i] = cpu_to_be16((uint16_t)offset_val); 276 279 } 277 280 } 278 281 ··· 289 282 unsigned int lut_index; 290 283 291 284 table->backlight_thresholds[0] = 0; 292 - table->backlight_offsets[0] = params.backlight_lut_array[0]; 285 + ASSERT(params.backlight_lut_array[0] <= 0xFFFF); 286 + table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0]; 293 287 table->backlight_thresholds[num_entries-1] = 0xFFFF; 288 + ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF); 294 289 table->backlight_offsets[num_entries-1] = 295 - params.backlight_lut_array[params.backlight_lut_array_size - 1]; 290 + (uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1]; 296 291 297 292 /* Setup all brightness levels between 0% and 100% exclusive 298 293 * Fills brightness-to-backlight transform table. Backlight custom curve ··· 308 299 lut_index = DIV_ROUNDUP((i * params.backlight_lut_array_size), num_entries); 309 300 ASSERT(lut_index < params.backlight_lut_array_size); 310 301 302 + unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries); 303 + unsigned int offset_val = params.backlight_lut_array[lut_index]; 304 + 305 + ASSERT(threshold_val <= 0xFFFF); 306 + ASSERT(offset_val <= 0xFFFF); 307 + 311 308 table->backlight_thresholds[i] = (big_endian) ? 312 - cpu_to_be16(DIV_ROUNDUP((i * 65536), num_entries)) : 313 - cpu_to_le16(DIV_ROUNDUP((i * 65536), num_entries)); 309 + cpu_to_be16((uint16_t)threshold_val) : cpu_to_le16((uint16_t)threshold_val); 314 310 table->backlight_offsets[i] = (big_endian) ? 315 - cpu_to_be16(params.backlight_lut_array[lut_index]) : 316 - cpu_to_le16(params.backlight_lut_array[lut_index]); 311 + cpu_to_be16((uint16_t)offset_val) : cpu_to_le16((uint16_t)offset_val); 317 312 } 318 313 } 319 314 ··· 753 740 } 754 741 755 742 if (params.backlight_ramping_override) { 743 + 744 + ASSERT(params.backlight_ramping_reduction <= 0xFFFF); 745 + ASSERT(params.backlight_ramping_start <= 0xFFFF); 756 746 for (i = 0; i < NUM_AGGR_LEVEL; i++) { 757 - config.blRampReduction[i] = params.backlight_ramping_reduction; 758 - config.blRampStart[i] = params.backlight_ramping_start; 747 + config.blRampReduction[i] = (uint16_t)params.backlight_ramping_reduction; 748 + config.blRampStart[i] = (uint16_t)params.backlight_ramping_start; 759 749 } 760 750 } else { 761 751 for (i = 0; i < NUM_AGGR_LEVEL; i++) { ··· 1076 1060 bool fill_custom_backlight_caps(unsigned int config_no, struct dm_acpi_atif_backlight_caps *caps) 1077 1061 { 1078 1062 unsigned int data_points_size; 1063 + uint64_t caps_size; 1079 1064 1080 1065 if (config_no >= ARRAY_SIZE(custom_backlight_profiles)) 1081 1066 return false; ··· 1084 1067 data_points_size = custom_backlight_profiles[config_no].num_data_points 1085 1068 * sizeof(custom_backlight_profiles[config_no].data_points[0]); 1086 1069 1087 - caps->size = sizeof(struct dm_acpi_atif_backlight_caps) - sizeof(caps->data_points) + data_points_size; 1070 + caps_size = sizeof(struct dm_acpi_atif_backlight_caps) - sizeof(caps->data_points) + data_points_size; 1071 + ASSERT(caps_size <= 0xFFFF); 1072 + caps->size = (uint16_t)caps_size; 1088 1073 caps->flags = 0; 1089 1074 caps->error_code = 0; 1090 1075 caps->ac_level_percentage = custom_backlight_profiles[config_no].ac_level_percentage;
+6 -2
drivers/gpu/drm/amd/display/modules/vmid/vmid.c
··· 57 57 static void evict_vmids(struct core_vmid *core_vmid) 58 58 { 59 59 int i; 60 - uint16_t ord = dc_get_vmid_use_vector(core_vmid->dc); 60 + int ord_int = dc_get_vmid_use_vector(core_vmid->dc); 61 + 62 + ASSERT(ord_int >= 0 && ord_int <= 0xFFFF); 63 + uint16_t ord = (uint16_t)ord_int; 61 64 62 65 // At this point any positions with value 0 are unused vmids, evict them 63 66 for (i = 1; i < core_vmid->num_vmid; i++) { ··· 123 120 ASSERT(0); 124 121 } 125 122 126 - return vmid; 123 + ASSERT(vmid >= 0 && vmid <= 0xFF); 124 + return (uint8_t)vmid; 127 125 } 128 126 129 127 void mod_vmid_reset(struct mod_vmid *mod_vmid)