Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] drm/msm/dcp: uninitialized caps_cache completions in
  dcp_hfi_init() and deinit
- [High] drm/msm/dcp: race scheduling resource_vote_work after
  dcp_hfi_stop()
- [Medium] drm/msm/dcp: malformed RESOURCE_VOTE clobbers pending vote in
  dcp_hfi_dispatch_event()
- [Medium] drm/msm/dcp: unconsumed vote state in
  dcp_hfi_resource_vote_work()
- [Medium] drm/msm/dcp: missing power-control handling in
  dcp_hfi_dispatch_event()
--

commit e810b83b6a7c8b39206eb3014fc0813a600fec0a
Author: Jayant Shekhar <[email protected]>

drm/msm/dcp: Handle asynchronous HFI responses

This commit dispatches asynchronous HFI display events, caches capability
payloads, and defers resource-vote processing to a work item before
falling back to synchronous command completion handling.

> diff --git a/drivers/gpu/drm/msm/dcp/dcp_hfi_core.c 
> b/drivers/gpu/drm/msm/dcp/dcp_hfi_core.c
> index 9c46a4560a0de..1a1addd33877c 100644
> --- a/drivers/gpu/drm/msm/dcp/dcp_hfi_core.c
> +++ b/drivers/gpu/drm/msm/dcp/dcp_hfi_core.c
[ ... ]
> @@ -228,6 +233,191 @@ void dcp_hfi_cmd_callback(struct virtqueue *vq)
>       spin_unlock_irqrestore(&hfi->cmd_lock, flags);
>  }
>  
> +static void dcp_hfi_store_caps_payload(struct dcp_hfi *hfi,
> +                                    struct completion *done,
> +                                    u8 *dst, u32 *dst_len,
> +                                    const struct hfi_packet_info *pkt_info,
> +                                    const char *name)
> +{
> +     u32 n;
> +
> +     if (!pkt_info->payload_ptr || !pkt_info->payload_size) {
> +             dev_dbg(hfi->dev, "%s: empty payload\n", name);
> +             return;
> +     }
> +
> +     if (pkt_info->payload_size > DCP_HFI_CAPS_BUF_SIZE) {
> +             dev_warn(hfi->dev, "%s: payload too large: %u\n",
> +                      name, pkt_info->payload_size);
> +             return;
> +     }
> +
> +     n = pkt_info->payload_size;
> +     memcpy(dst, pkt_info->payload_ptr, n);
> +     *dst_len = n;
> +     if (done)
> +             complete(done);
> +     dev_dbg(hfi->dev, "%s: stored %u bytes\n", name, n);
> +}
> +
> +/* Route asynchronous events and capability packets before flag handling. */
> +static bool dcp_hfi_dispatch_event(struct dcp_hfi *hfi,
> +                                const struct hfi_packet_info *pkt_info)
> +{
> +     switch (pkt_info->cmd) {
> +     case HFI_COMMAND_DISPLAY_EVENT_VSYNC: {
> +             return true;
> +     }

[Severity: Medium]
Does dcp_hfi_dispatch_event() omit handling for display power-control
messages?

The commit message states that display power control produces asynchronous
responses and that this patch delivers power-control notifications, but
neither HFI_COMMAND_DISPLAY_POWER_CONTROL nor
HFI_COMMAND_DISPLAY_EVENT_POWER is handled here.  Any asynchronous
power-control response with HFI_RX_FLAGS_SUCCESS or
HFI_RX_FLAGS_INFORMATION will fall through in dcp_hfi_resp_callback() to
dcp_hfi_complete_pending().

[ ... ]

> +     case HFI_COMMAND_DEVICE_CALLBACK_RESOURCE_VOTE: {
> +             unsigned long vote_flags;
> +             u32 payload_size = pkt_info->payload_size;
> +             bool valid = pkt_info->payload_ptr && payload_size &&
> +                     payload_size <= DCP_HFI_RESOURCE_VOTE_BUF_SIZE &&
> +                     payload_size % sizeof(struct dcp_resource_vote_wire) == 
> 0;
> +
> +             if (!valid) {
> +                     dev_warn_ratelimited(hfi->dev,
> +                                          "dropping malformed RESOURCE_VOTE 
> payload size=%u max=%u\n",
> +                                          payload_size, 
> DCP_HFI_RESOURCE_VOTE_BUF_SIZE);
> +                     payload_size = 0;
> +             }
> +
> +             spin_lock_irqsave(&hfi->resource_vote_lock, vote_flags);
> +             if (payload_size)
> +                     memcpy(hfi->resource_vote_buf, pkt_info->payload_ptr,
> +                            payload_size);
> +             hfi->resource_vote_len = payload_size;
> +             hfi->resource_vote_generation++;
> +             spin_unlock_irqrestore(&hfi->resource_vote_lock, vote_flags);
> +
> +             if (payload_size)
> +                     schedule_work(&hfi->resource_vote_work);
> +             return true;
> +     }

[Severity: Medium]
When a malformed HFI_COMMAND_DEVICE_CALLBACK_RESOURCE_VOTE packet arrives,
does setting payload_size = 0 and continuing discard a previously buffered
valid resource vote?

Instead of returning early on !valid, this overwrites
hfi->resource_vote_len = 0 and increments hfi->resource_vote_generation,
so dcp_hfi_resource_vote_work() sees len == 0 and returns without
delivering the earlier unconsumed vote.

[ ... ]

> @@ -642,6 +834,47 @@ int dcp_hfi_send_no_wait(struct dcp_hfi *hfi,
>                       payload_type, payload, payload_size, HFI_TX_FLAGS_NONE);
>  }
>  
> +static void dcp_hfi_resource_vote_work(struct work_struct *work)
> +{
> +     struct dcp_hfi *hfi = container_of(work, struct dcp_hfi,
> +                                          resource_vote_work);
> +     u8 buf[DCP_HFI_RESOURCE_VOTE_BUF_SIZE];
> +     struct dcp_resource_vote_pair pairs[DCP_HFI_RESOURCE_VOTE_BUF_SIZE /
> +                                                      sizeof(struct 
> dcp_resource_vote_wire)];
> +     const struct dcp_resource_vote_wire *wire;
> +     u32 len, generation, count, i;
> +     bool dirty;
> +     unsigned long flags;
> +
> +     do {
> +             spin_lock_irqsave(&hfi->resource_vote_lock, flags);
> +             len = hfi->resource_vote_len;
> +             generation = hfi->resource_vote_generation;
> +             if (len)
> +                     memcpy(buf, hfi->resource_vote_buf, len);
> +             spin_unlock_irqrestore(&hfi->resource_vote_lock, flags);
> +
> +             if (!len || !hfi->resource_vote_cb)
> +                     return;
> +             if (len % sizeof(struct dcp_resource_vote_wire)) {
> +                     dev_warn_ratelimited(hfi->dev,
> +                                          "dropping malformed RESOURCE_VOTE 
> size=%u\n", len);
> +                     return;
> +             }
> +             count = len / sizeof(struct dcp_resource_vote_wire);
> +             wire = (const struct dcp_resource_vote_wire *)buf;
> +             for (i = 0; i < count; i++) {
> +                     pairs[i].key = le32_to_cpu(wire[i].key);
> +                     pairs[i].value = get_unaligned_le64(&wire[i].value);
> +             }
> +
> +             hfi->resource_vote_cb(hfi->drv, pairs, count);
> +             spin_lock_irqsave(&hfi->resource_vote_lock, flags);
> +             dirty = generation != hfi->resource_vote_generation;
> +             spin_unlock_irqrestore(&hfi->resource_vote_lock, flags);
> +     } while (dirty);
> +}

[Severity: Medium]
Does dcp_hfi_resource_vote_work() invoke hfi->resource_vote_cb() twice
with the same vote when a new vote arrives while the worker is already
running?

Because the workqueue core clears WORK_STRUCT_PENDING_BIT before running
dcp_hfi_resource_vote_work(), an HFI_COMMAND_DEVICE_CALLBACK_RESOURCE_VOTE
event arriving while the worker is in hfi->resource_vote_cb() both causes
the do-while(dirty) loop to consume the new generation and causes
schedule_work(&hfi->resource_vote_work) to queue a second run.  Since
hfi->resource_vote_len is never cleared when snapshotting
hfi->resource_vote_buf, the queued second run sees the same non-zero
hfi->resource_vote_len and delivers the stale vote again.  Clearing
hfi->resource_vote_len = 0 under hfi->resource_vote_lock when copying
hfi->resource_vote_buf (and looping while hfi->resource_vote_len != 0)
would avoid duplicate callbacks.

[ ... ]

> @@ -807,6 +1047,7 @@ void dcp_hfi_stop(struct dcp_hfi *hfi)
>       atomic_set(&hfi->shutdown_in_progress, 1);
>       spin_unlock_irqrestore(&hfi->cmd_lock, flags);
>  
> +     cancel_work_sync(&hfi->resource_vote_work);
>       cancel_delayed_work_sync(&hfi->resp_repost_work);
>       dcp_hfi_cancel_pending(hfi, -ENODEV);
>  }

[Severity: High]
Can dcp_hfi_resp_callback() re-schedule hfi->resource_vote_work after
cancel_work_sync() in dcp_hfi_stop() returns?

dcp_hfi_resp_callback() checks hfi->shutdown_in_progress only once on
entry and drops hfi->resp_drain_lock while unpacking packets and calling
dcp_hfi_dispatch_event():

CPU0                                  CPU1
dcp_hfi_resp_callback()
  !atomic_read(&shutdown_in_progress)
  spin_unlock(&hfi->resp_drain_lock)
                                      dcp_hfi_stop()
                                        atomic_set(&shutdown_in_progress, 1)
                                        cancel_work_sync(&resource_vote_work)
  dcp_hfi_dispatch_event()
    schedule_work(&resource_vote_work)
                                      dcp_hfi_deinit()
                                      /* hfi / drv torn down */
                                      dcp_hfi_resource_vote_work()
                                        <-- runs after stop / use-after-free

Neither HFI_COMMAND_DEVICE_CALLBACK_RESOURCE_VOTE in
dcp_hfi_dispatch_event() nor dcp_hfi_resource_vote_work() checks
hfi->shutdown_in_progress, and resource_vote_work is not cancelled again
after virtqueue callbacks are synchronized.

> @@ -864,6 +1105,10 @@ int dcp_hfi_restore_queues(struct dcp_hfi *hfi, struct 
> virtqueue *cmd_vq,
>  
>  void dcp_hfi_deinit(struct dcp_hfi *hfi)
>  {
> +     mutex_lock(&hfi->caps_cache_lock);
> +     memset(&hfi->caps_cache, 0, sizeof(hfi->caps_cache));
> +     mutex_unlock(&hfi->caps_cache_lock);
> +
>       xa_destroy(&hfi->pending_xa);

[Severity: High]
Are hfi->caps_cache.device_done and hfi->caps_cache.display_done left
uninitialized in dcp_hfi_init() and wiped by memset() in dcp_hfi_deinit()?

dcp_hfi_init() allocates struct dcp_hfi with devm_kzalloc() and pre-posts
response buffers to resp_vq without calling init_completion() on
device_done or display_done, and dcp_hfi_deinit() zeroes the entire
caps_cache structure with memset() without holding hfi->caps_lock.  A
zeroed struct completion has wait.task_list.next == NULL, so when
HFI_COMMAND_DEVICE_INIT_DEVICE_CAPS or
HFI_COMMAND_DEVICE_INIT_DISPLAY_CAPS arrives and
dcp_hfi_store_caps_payload() calls complete(done), swake_up_locked() sees
!list_empty(&q->task_list) and dereferences a NULL pointer.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=7

Reply via email to