<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>I think we can ignore this change, as it already exists in the
      below commit of asdn.</p>
    <p><span><span class="ui-provider a b c d e f g h i j k l m n o p q r s t u v w x y z ab ac ae af ag ah ai aj ak" dir="ltr">commit b90c2f233397f40c757995b9c00f8c6e380c6913<br>
          Author: Alex Hung <a class="moz-txt-link-rfc2396E" href="mailto:alex.hung@amd.com"><alex.hung@amd.com></a><br>
          Date:   Thu Jun 27 17:38:16 2024 -0600
          <p></p>
          <p>     drm/amd/display: Check null pointers before using them</p>
        </span></span></p>
    <div class="moz-cite-prefix">On 7/17/2024 8:10 AM, Srinivasan
      Shanmugam wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:20240717024000.512972-1-srinivasan.shanmugam@amd.com">
      <pre class="moz-quote-pre" wrap="">This commit adds a null check for the dm_state variable in the
create_validate_stream_for_sink function. Previously, dm_state was being
checked for nullity at line 7194, but then it was being dereferenced
without any nullity check at line 7200. This could potentially lead to a
null pointer dereference error if dm_state is indeed null.

we now ensure that dm_state is not null before  dereferencing it. We do
this by adding a nullity check for dm_state  before the call to
create_stream_for_sink at line 7200. If dm_state  is null, we log an
error message and return NULL immediately.

This fix prevents a null pointer dereference error.

drivers/gpu/drm/amd/amdgpu/../display/amdgpu_dm/amdgpu_dm.c:7201 create_validate_stream_for_sink()
error: we previously assumed 'dm_state' could be null (see line 7194)

drivers/gpu/drm/amd/amdgpu/../display/amdgpu_dm/amdgpu_dm.c
    7185 struct dc_stream_state *
    7186 create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector,
    7187                                 const struct drm_display_mode *drm_mode,
    7188                                 const struct dm_connector_state *dm_state,
    7189                                 const struct dc_stream_state *old_stream)
    7190 {
    7191         struct drm_connector *connector = &aconnector->base;
    7192         struct amdgpu_device *adev = drm_to_adev(connector->dev);
    7193         struct dc_stream_state *stream;
    7194         const struct drm_connector_state *drm_state = dm_state ? &dm_state->base : NULL;
                                                               ^^^^^^^^
                                     ^^^^^^^^^ This used check connector->state but then we changed it to dm_state instead

    7195         int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
    7196         enum dc_status dc_result = DC_OK;
    7197
    7198         do {
    7199                 stream = create_stream_for_sink(connector, drm_mode,
    7200                                                 dm_state, old_stream,
                                                         ^^^^^^^^

But dm_state is dereferenced on the next line without checking.  (Presumably the NULL check can be removed).

--> 7201                                                 requested_bpc);
    7202                 if (stream == NULL) {
    7203                         DRM_ERROR("Failed to create stream for sink!\n");
    7204                         break;
    7205                 }
    7206
    7207                 if (aconnector->base.connector_type == DRM_MODE_CONNECTOR_WRITEBACK)

Fixes: fa7041d9d2fc ("drm/amd/display: Fix ineffective setting of max bpc property")
Reported-by: Dan Carpenter <a class="moz-txt-link-rfc2396E" href="mailto:dan.carpenter@linaro.org"><dan.carpenter@linaro.org></a>
Cc: Tom Chung <a class="moz-txt-link-rfc2396E" href="mailto:chiahsuan.chung@amd.com"><chiahsuan.chung@amd.com></a>
Cc: Rodrigo Siqueira <a class="moz-txt-link-rfc2396E" href="mailto:Rodrigo.Siqueira@amd.com"><Rodrigo.Siqueira@amd.com></a>
Cc: Roman Li <a class="moz-txt-link-rfc2396E" href="mailto:roman.li@amd.com"><roman.li@amd.com></a>
Cc: Hersen Wu <a class="moz-txt-link-rfc2396E" href="mailto:hersenxs.wu@amd.com"><hersenxs.wu@amd.com></a>
Cc: Alex Hung <a class="moz-txt-link-rfc2396E" href="mailto:alex.hung@amd.com"><alex.hung@amd.com></a>
Cc: Aurabindo Pillai <a class="moz-txt-link-rfc2396E" href="mailto:aurabindo.pillai@amd.com"><aurabindo.pillai@amd.com></a>
Cc: Harry Wentland <a class="moz-txt-link-rfc2396E" href="mailto:harry.wentland@amd.com"><harry.wentland@amd.com></a>
Cc: Hamza Mahfooz <a class="moz-txt-link-rfc2396E" href="mailto:hamza.mahfooz@amd.com"><hamza.mahfooz@amd.com></a>
Signed-off-by: Srinivasan Shanmugam <a class="moz-txt-link-rfc2396E" href="mailto:srinivasan.shanmugam@amd.com"><srinivasan.shanmugam@amd.com></a>
---
v2: s/DRM_ERROR/drm_err() (Hamza)
   
 drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
index d1527c2e46a1..e7516a2dcb10 100644
--- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
+++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c
@@ -7195,6 +7195,11 @@ create_validate_stream_for_sink(struct amdgpu_dm_connector *aconnector,
        int requested_bpc = drm_state ? drm_state->max_requested_bpc : 8;
        enum dc_status dc_result = DC_OK;
 
+       if (!dm_state) {
+               drm_err(&adev->ddev, "dm_state is NULL!\n");
+               return NULL;
+       }
+
        do {
                stream = create_stream_for_sink(connector, drm_mode,
                                                dm_state, old_stream,
</pre>
    </blockquote>
  </body>
</html>