drm/msm/dp: Avoid division by zero in msm_dp_ctrl_config_msa()
PATCH 1/1 drm/msm/dp: Avoid division by zero in msm_dp_ctrl_config_msa()
An (admittedly problematic) optimization change in LLVM 20 [1] turns known division by zero into the equivalent of __builtin_unreachable(), which invokes undefined behavior if it is encountered in a control flow graph, destroying code generation. When compile testing for x86_64, objtool flags an instance of this optimization triggering in msm_dp_ctrl_config_msa(), inlined into msm_dp_ctrl_on_stream():
drivers/gpu/drm/msm/msm.o: warning: objtool: msm_dp_ctrl_on_stream(): unexpected end of section .text.msm_dp_ctrl_on_stream
The zero division happens if the else branch in the first if statement in msm_dp_ctrl_config_msa() is taken because pixel_div is initialized to zero and it is not possible for LLVM to eliminate the else branch since rate is still not known after inlining into msm_dp_ctrl_on_stream(). Change pixel_div to one to make the division well defined in the presence of an unsupported rate, relying on the DRM_ERROR print to indicate the error to the user.
--- drivers/gpu/drm/msm/dp/dp_ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/msm/dp/dp_ctrl.c +1 -1
@@ -2395,7 +2395,7 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, bool is_ycbcr_420) {u32 pixel_m, pixel_n; -u32 mvid, nvid, pixel_div =0 , dispcc_input_rate; +u32 mvid, nvid, pixel_div =1 , dispcc_input_rate;u32 const nvid_fixed =DP_LINK_CONSTANT_N_VALUE ;u32 const link_rate_hbr2 =540000 ;u32 const link_rate_hbr3 =810000 ;
---
base-commit: 66691e272e40c91305f1704695e0cb340cd162ff
change-id: 20260108-drm-msm-dp_ctrl-avoid-zero-div-be5dc40cbc18
Best regards,
Signature · 1 line
Nathan Chancellor <[email protected]>
Quoted 41 lines · Nathan Chancellor
An (admittedly problematic) optimization change in LLVM 20 [1] turns known division by zero into the equivalent of
__builtin_unreachable(), which invokes undefined behavior if it is encountered in a control flow graph, destroying code generation. When compile testing forx86_64, objtool flags an instance of this optimization triggering inmsm_dp_ctrl_config_msa(), inlined intomsm_dp_ctrl_on_stream():
drivers/gpu/drm/msm/msm.o: warning: objtool:msm_dp_ctrl_on_stream(): unexpected end of section .text.msm_dp_ctrl_on_streamThe zero division happens if the else branch in the first if statement in
msm_dp_ctrl_config_msa()is taken becausepixel_divis initialized to zero and it is not possible for LLVM to eliminate the else branch since rate is still not known after inlining intomsm_dp_ctrl_on_stream(). Changepixel_divto one to make the division well defined in the presence of an unsupported rate, relying on theDRM_ERRORprint to indicate the error to the user.Fixes c943b4948b58 ("drm/msm/dp: add displayPort driver support")Reported-by kernel test robot <[email protected]>Signed-off-by Nathan Chancellor <[email protected]>--- drivers/gpu/drm/msm/dp/dp_ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/msm/dp/dp_ctrl.c +1
-2@@ -2395,7 +2395,7 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, bool is_ycbcr_420) {u32 pixel_m, pixel_n; -u32 mvid, nvid, pixel_div =0 , dispcc_input_rate; +u32 mvid, nvid, pixel_div =1 , dispcc_input_rate;u32 const nvid_fixed =DP_LINK_CONSTANT_N_VALUE ;u32 const link_rate_hbr2 =540000 ;u32 const link_rate_hbr3 =810000 ; ---
Quoted 2 lines
base-commit:
66691e272e40c91305f1704695e0cb340cd162ff
change-id: 20260108-drm-msm-dp_ctrl-avoid-zero-div-be5dc40cbc18
Dmitry, would it be beneficial to throw an actual error when the rate is is mangled? i.e.
drivers/gpu/drm/msm/dp/dp_ctrl.c +22 -12
@@ -2404,9 +2404,9 @@ static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl)return msm_dp_ctrl_setup_main_link (ctrl, &training_step); } -static void msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, -u32 rate,u32 stream_rate_khz, -bool is_ycbcr_420) +static int msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, +u32 rate,u32 stream_rate_khz, +bool is_ycbcr_420) {u32 pixel_m, pixel_n;u32 mvid, nvid, pixel_div =0 , dispcc_input_rate;
@@ -2415,14 +2415,21 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,u32 const link_rate_hbr3 =810000 ;unsigned long den, num; -if (rate == link_rate_hbr3) +switch (rate) { +case link_rate_hbr3: pixel_div =6 ; -else if (rate ==162000 || rate ==270000 ) - pixel_div =2 ; -else if (rate == link_rate_hbr2) +break ; +case link_rate_hbr2: pixel_div =4 ; -else +break ; +case 270000 : +case 162000 : + pixel_div =2 ; +break ; +default :DRM_ERROR ("Invalid pixel mux divider\n" ); +return -EINVAL ; + } dispcc_input_rate = (rate *10 ) / pixel_div;
@@ -2458,6 +2465,8 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,drm_dbg_dp (ctrl->drm_dev,"mvid=0x%x, nvid=0x%x\n" , mvid, nvid);msm_dp_write_link (ctrl,REG_DP_SOFTWARE_MVID , mvid);msm_dp_write_link (ctrl,REG_DP_SOFTWARE_NVID , nvid); + +return 0 ; }int msm_dp_ctrl_on_stream (struct msm_dp_ctrl *msm_dp_ctrl,bool force_link_train)
@@ -2525,10 +2534,11 @@ int msm_dp_ctrl_on_stream(struct msm_dp_ctrl *msm_dp_ctrl, bool force_link_trainmsm_dp_ctrl_configure_source_params (ctrl); -msm_dp_ctrl_config_msa (ctrl, - ctrl->link->link_params.rate, - pixel_rate_orig, - ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420); + ret =msm_dp_ctrl_config_msa (ctrl, ctrl->link->link_params.rate, + pixel_rate_orig, + ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420); +if (ret) +return ret;msm_dp_panel_clear_dsc_dto (ctrl->panel);
Konrad
Quoted 2 lines · Konrad Dybcio
Dmitry, would it be beneficial to throw an actual error when the rate is is mangled? i.e.
This certainly seems more robust if acceptable.
Quoted 7 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +0
-0@@ -2404,9 +2404,9 @@ static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl)return msm_dp_ctrl_setup_main_link (ctrl, &training_step); }
drivers/gpu/drm/msm/dp/dp_ctrl.c static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl) · line 2412
Quoted 10 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +3
-3-static void msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, -u32 rate,u32 stream_rate_khz, -bool is_ycbcr_420) +static int msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, +u32 rate,u32 stream_rate_khz, +bool is_ycbcr_420) {u32 pixel_m, pixel_n;u32 mvid, nvid, pixel_div =0 , dispcc_input_rate;
This initialization is now unnecessary with this change.
Quoted 55 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +19
-9@@ -2415,14 +2415,21 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,u32 const link_rate_hbr3 =810000 ;unsigned long den, num; -if (rate == link_rate_hbr3) +switch (rate) { +case link_rate_hbr3: pixel_div =6 ; -else if (rate ==162000 || rate ==270000 ) - pixel_div =2 ; -else if (rate == link_rate_hbr2) +break ; +case link_rate_hbr2: pixel_div =4 ; -else +break ; +case 270000 : +case 162000 : + pixel_div =2 ; +break ; +default :DRM_ERROR ("Invalid pixel mux divider\n" ); +return -EINVAL ; + } dispcc_input_rate = (rate *10 ) / pixel_div;@@ -2458,6 +2465,8 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,drm_dbg_dp (ctrl->drm_dev,"mvid=0x%x, nvid=0x%x\n" , mvid, nvid);msm_dp_write_link (ctrl,REG_DP_SOFTWARE_MVID , mvid);msm_dp_write_link (ctrl,REG_DP_SOFTWARE_NVID , nvid); + +return 0 ; }int msm_dp_ctrl_on_stream (struct msm_dp_ctrl *msm_dp_ctrl,bool force_link_train)@@ -2525,10 +2534,11 @@ int msm_dp_ctrl_on_stream(struct msm_dp_ctrl *msm_dp_ctrl, bool force_link_trainmsm_dp_ctrl_configure_source_params (ctrl); -msm_dp_ctrl_config_msa (ctrl, - ctrl->link->link_params.rate, - pixel_rate_orig, - ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420); + ret =msm_dp_ctrl_config_msa (ctrl, ctrl->link->link_params.rate, + pixel_rate_orig, + ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420); +if (ret) +return ret;msm_dp_panel_clear_dsc_dto (ctrl->panel);Konrad
Quoted 78 lines · Konrad Dybcio
Quoted 41 lines · Nathan Chancellor
An (admittedly problematic) optimization change in LLVM 20 [1] turns known division by zero into the equivalent of
__builtin_unreachable(), which invokes undefined behavior if it is encountered in a control flow graph, destroying code generation. When compile testing forx86_64, objtool flags an instance of this optimization triggering inmsm_dp_ctrl_config_msa(), inlined intomsm_dp_ctrl_on_stream():
drivers/gpu/drm/msm/msm.o: warning: objtool:msm_dp_ctrl_on_stream(): unexpected end of section .text.msm_dp_ctrl_on_streamThe zero division happens if the else branch in the first if statement in
msm_dp_ctrl_config_msa()is taken becausepixel_divis initialized to zero and it is not possible for LLVM to eliminate the else branch since rate is still not known after inlining intomsm_dp_ctrl_on_stream(). Changepixel_divto one to make the division well defined in the presence of an unsupported rate, relying on theDRM_ERRORprint to indicate the error to the user.Fixes c943b4948b58 ("drm/msm/dp: add displayPort driver support")Reported-by kernel test robot <[email protected]>Signed-off-by Nathan Chancellor <[email protected]>--- drivers/gpu/drm/msm/dp/dp_ctrl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/gpu/drm/msm/dp/dp_ctrl.c +1
-2@@ -2395,7 +2395,7 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, bool is_ycbcr_420) {u32 pixel_m, pixel_n; -u32 mvid, nvid, pixel_div =0 , dispcc_input_rate; +u32 mvid, nvid, pixel_div =1 , dispcc_input_rate;u32 const nvid_fixed =DP_LINK_CONSTANT_N_VALUE ;u32 const link_rate_hbr2 =540000 ;u32 const link_rate_hbr3 =810000 ; ---Quoted 2 lines
base-commit:
66691e272e40c91305f1704695e0cb340cd162ff
change-id: 20260108-drm-msm-dp_ctrl-avoid-zero-div-be5dc40cbc18Dmitry, would it be beneficial to throw an actual error when the rate is is mangled? i.e.
drivers/gpu/drm/msm/dp/dp_ctrl.c +7
-7@@ -2404,9 +2404,9 @@ static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl)return msm_dp_ctrl_setup_main_link (ctrl, &training_step); } -static void msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, -u32 rate,u32 stream_rate_khz, -bool is_ycbcr_420) +static int msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, +u32 rate,u32 stream_rate_khz, +bool is_ycbcr_420) {u32 pixel_m, pixel_n;u32 mvid, nvid, pixel_div =0 , dispcc_input_rate;@@ -2415,14 +2415,21 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,u32 const link_rate_hbr3 =810000 ;unsigned long den, num; -if (rate == link_rate_hbr3) +switch (rate) { +case link_rate_hbr3: pixel_div =6 ; -else if (rate ==162000 || rate ==270000 ) - pixel_div =2 ; -else if (rate == link_rate_hbr2) +break ; +case link_rate_hbr2:
drivers/gpu/drm/msm/dp/dp_ctrl.c static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, · line 2431
Quoted 10 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +7
-1pixel_div =4 ; - else +break ; +case 270000 : +case 162000 : + pixel_div =2 ; +break ; +default :DRM_ERROR ("Invalid pixel mux divider\n" ); +return -EINVAL ;
Well... In the ideal world, we can't end up here, PHY's configure_dp_clocks (or qcom_edp_set_vco_div()) will fail if the link rate is is invalid here. I think, we should return an error here, but there is no need to propagate it further.
See the discussion at https://lore.kernel.org/dri-devel/[email protected]/
Quoted 32 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +8
-4+ } dispcc_input_rate = (rate *10 ) / pixel_div;@@ -2458,6 +2465,8 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,drm_dbg_dp (ctrl->drm_dev,"mvid=0x%x, nvid=0x%x\n" , mvid, nvid);msm_dp_write_link (ctrl,REG_DP_SOFTWARE_MVID , mvid);msm_dp_write_link (ctrl,REG_DP_SOFTWARE_NVID , nvid); + +return 0 ; }int msm_dp_ctrl_on_stream (struct msm_dp_ctrl *msm_dp_ctrl,bool force_link_train)@@ -2525,10 +2534,11 @@ int msm_dp_ctrl_on_stream(struct msm_dp_ctrl *msm_dp_ctrl, bool force_link_trainmsm_dp_ctrl_configure_source_params (ctrl); -msm_dp_ctrl_config_msa (ctrl, - ctrl->link->link_params.rate, - pixel_rate_orig, - ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420); + ret =msm_dp_ctrl_config_msa (ctrl, ctrl->link->link_params.rate, + pixel_rate_orig, + ctrl->panel->msm_dp_mode.out_fmt_is_yuv_420); +if (ret) +return ret;msm_dp_panel_clear_dsc_dto (ctrl->panel);Konrad
Signature · 2 lines
With best wishes Dmitry
Quoted 50 lines · Dmitry Baryshkov
Quoted 33 lines · Konrad Dybcio
Dmitry, would it be beneficial to throw an actual error when the rate is is mangled? i.e.
drivers/gpu/drm/msm/dp/dp_ctrl.c +7
-7@@ -2404,9 +2404,9 @@ static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl)return msm_dp_ctrl_setup_main_link (ctrl, &training_step); } -static void msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, -u32 rate,u32 stream_rate_khz, -bool is_ycbcr_420) +static int msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, +u32 rate,u32 stream_rate_khz, +bool is_ycbcr_420) {u32 pixel_m, pixel_n;u32 mvid, nvid, pixel_div =0 , dispcc_input_rate;@@ -2415,14 +2415,21 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,u32 const link_rate_hbr3 =810000 ;unsigned long den, num; -if (rate == link_rate_hbr3) +switch (rate) { +case link_rate_hbr3: pixel_div =6 ; -else if (rate ==162000 || rate ==270000 ) - pixel_div =2 ; -else if (rate == link_rate_hbr2) +break ; +case link_rate_hbr2:drivers/gpu/drm/msm/dp/dp_ctrl.c static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, · line 2431
Quoted 10 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +7
-1pixel_div =4 ; - else +break ; +case 270000 : +case 162000 : + pixel_div =2 ; +break ; +default :DRM_ERROR ("Invalid pixel mux divider\n" ); +return -EINVAL ;Well... In the ideal world, we can't end up here, PHY's
configure_dp_clocks(orqcom_edp_set_vco_div()) will fail if the link rate is is invalid here. I think, we should return an error here, but there is no need to propagate it further.
Quoted 1 line
See the discussion at https://lore.kernel.org/dri-devel/[email protected]/
I interpret that as approving of the above hunk but omitting the hunk that modifies msm_dp_ctrl_on_stream(). In that case, what is the point of changing the return type of msm_dp_ctrl_config_msa()? Wouldn't the below diff have the same exact effect as a smaller change? I don't mind rolling this up as a v2.
Cheers,
Nathan
drivers/gpu/drm/msm/dp/dp_ctrl.c +13 -6
@@ -2409,20 +2409,27 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, bool is_ycbcr_420) {u32 pixel_m, pixel_n; -u32 mvid, nvid, pixel_div =0 , dispcc_input_rate; +u32 mvid, nvid, pixel_div, dispcc_input_rate;u32 const nvid_fixed =DP_LINK_CONSTANT_N_VALUE ;u32 const link_rate_hbr2 =540000 ;u32 const link_rate_hbr3 =810000 ;unsigned long den, num; -if (rate == link_rate_hbr3) +switch (rate) { +case link_rate_hbr3: pixel_div =6 ; -else if (rate ==162000 || rate ==270000 ) - pixel_div =2 ; -else if (rate == link_rate_hbr2) +break ; +case link_rate_hbr2: pixel_div =4 ; -else +break ; +case 162000 : +case 270000 : + pixel_div =2 ; +break ; +default :DRM_ERROR ("Invalid pixel mux divider\n" ); +return ; + } dispcc_input_rate = (rate *10 ) / pixel_div;
Quoted 86 lines · Nathan Chancellor
Quoted 50 lines · Dmitry Baryshkov
Quoted 33 lines · Konrad Dybcio
Dmitry, would it be beneficial to throw an actual error when the rate is is mangled? i.e.
drivers/gpu/drm/msm/dp/dp_ctrl.c +7
-7@@ -2404,9 +2404,9 @@ static int msm_dp_ctrl_link_retrain(struct msm_dp_ctrl_private *ctrl)return msm_dp_ctrl_setup_main_link (ctrl, &training_step); } -static void msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, -u32 rate,u32 stream_rate_khz, -bool is_ycbcr_420) +static int msm_dp_ctrl_config_msa (struct msm_dp_ctrl_private *ctrl, +u32 rate,u32 stream_rate_khz, +bool is_ycbcr_420) {u32 pixel_m, pixel_n;u32 mvid, nvid, pixel_div =0 , dispcc_input_rate;@@ -2415,14 +2415,21 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl,u32 const link_rate_hbr3 =810000 ;unsigned long den, num; -if (rate == link_rate_hbr3) +switch (rate) { +case link_rate_hbr3: pixel_div =6 ; -else if (rate ==162000 || rate ==270000 ) - pixel_div =2 ; -else if (rate == link_rate_hbr2) +break ; +case link_rate_hbr2:drivers/gpu/drm/msm/dp/dp_ctrl.c static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, · line 2431
Quoted 10 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +7
-1pixel_div =4 ; - else +break ; +case 270000 : +case 162000 : + pixel_div =2 ; +break ; +default :DRM_ERROR ("Invalid pixel mux divider\n" ); +return -EINVAL ;Well... In the ideal world, we can't end up here, PHY's
configure_dp_clocks(orqcom_edp_set_vco_div()) will fail if the link rate is is invalid here. I think, we should return an error here, but there is no need to propagate it further.Quoted 1 line
See the discussion at https://lore.kernel.org/dri-devel/[email protected]/
I interpret that as approving of the above hunk but omitting the hunk that modifies
msm_dp_ctrl_on_stream(). In that case, what is the point of changing the return type ofmsm_dp_ctrl_config_msa()? Wouldn't the below diff have the same exact effect as a smaller change? I don't mind rolling this up as a v2.Cheers,
Nathandrivers/gpu/drm/msm/dp/dp_ctrl.c +5
-5@@ -2409,20 +2409,27 @@ static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, bool is_ycbcr_420) {u32 pixel_m, pixel_n; -u32 mvid, nvid, pixel_div =0 , dispcc_input_rate; +u32 mvid, nvid, pixel_div, dispcc_input_rate;u32 const nvid_fixed =DP_LINK_CONSTANT_N_VALUE ;u32 const link_rate_hbr2 =540000 ;u32 const link_rate_hbr3 =810000 ;unsigned long den, num; -if (rate == link_rate_hbr3) +switch (rate) { +case link_rate_hbr3: pixel_div =6 ; -else if (rate ==162000 || rate ==270000 ) - pixel_div =2 ; -else if (rate == link_rate_hbr2) +break ; +case link_rate_hbr2:
drivers/gpu/drm/msm/dp/dp_ctrl.c static void msm_dp_ctrl_config_msa(struct msm_dp_ctrl_private *ctrl, · line 2431
Quoted 10 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +7
-1pixel_div =4 ; - else +break ; +case 162000 : +case 270000 : + pixel_div =2 ; +break ; +default :DRM_ERROR ("Invalid pixel mux divider\n" ); +return ;
Please add a comment, stating that we can't actually reach it. LGTM otherwise.
Quoted 4 lines
drivers/gpu/drm/msm/dp/dp_ctrl.c +1
-0+ } dispcc_input_rate = (rate *10 ) / pixel_div;
Signature · 2 lines
With best wishes Dmitry