Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] accel/thames: broken multi-core DRU channel check in
  thames_core_read_caps()
- [Medium] accel/thames: race writing context entry in
  thames_context_create()
--

commit 7eb0354ce4ccbc601749105abe32aaddd5f51384
Author: Tomeu Vizoso <[email protected]>

accel/thames: Create a context per client on device open

This commit creates a per-client context on device open, managing an
LPAE I/O page table, ASID, VA allocator, user stack and save area GEM DMA
objects, and IPC context table entries across the DSP cores.

> diff --git a/drivers/accel/thames/thames_core.c 
> b/drivers/accel/thames/thames_core.c
> index 8053eeeb39022..d1140a374bd62 100644
> --- a/drivers/accel/thames/thames_core.c
> +++ b/drivers/accel/thames/thames_core.c
> @@ -140,8 +141,16 @@ static int thames_core_read_caps(struct thames_core 
> *core, struct thames_handsha
>               return -EINVAL;
>       }
>  
> -     if (tdev->has_caps && memcmp(caps, &tdev->caps, sizeof(*caps))) {
> -             dev_err(core->dev, "Capabilities or window sizes differ from 
> the first core's\n");
> +     /*
> +      * A Context maps every core's DRU channels when the Context is
> +      * created, from channel 0 on a core that has not attached yet, so a
> +      * core attaching later must report the first channel its Contexts map.
> +      */
> +     if (tdev->has_caps &&
> +         (memcmp(caps, &tdev->caps, sizeof(*caps)) ||
> +          dru_kernel_channel_first != core->dru_kernel_channel_first)) {
> +             dev_err(core->dev,
> +                     "Capabilities, window sizes or first DRU channel differ 
> from those the device was set up with\n");
>               return -EINVAL;
>       }
>  

[Severity: High]
Does comparing dru_kernel_channel_first against
core->dru_kernel_channel_first reject the second core on initial attach
whenever the firmware reports a non-zero dru_kernel_channel_first?

tdev->has_caps is set as soon as the first core attaches, whereas
core->dru_kernel_channel_first on the second core is still 0 until the
assignment below this check.  When the second core attaches for the first
time with a non-zero dru_kernel_channel_first, this check compares it
against 0 and fails with -EINVAL, leaving the second core offline.

Because core->dru_kernel_channel_first is 0 on any unattached core,
thames_context_map_core_regions() also maps DRU channel 0 (the firmware's
private channels) for a core that has not attached yet instead of the
first core's dru_kernel_channel_first, and a second core reporting 0 would
pass 0 != 0 even when the first core reported a non-zero channel.  Storing
dru_kernel_channel_first in struct thames_caps and using
tdev->caps.dru_kernel_channel_first in thames_context_map_core_regions()
would fix both the comparison and the mapping.

[ ... ]

> diff --git a/drivers/accel/thames/thames_gem.c 
> b/drivers/accel/thames/thames_gem.c
> new file mode 100644
> index 0000000000000..e1f36a0daf3b8
> --- /dev/null
> +++ b/drivers/accel/thames/thames_gem.c

[ ... ]

> +int thames_context_create(struct thames_file_priv *priv)
> +{
> +     struct thames_device *tdev = priv->tdev;
> +     int ret, asid;
> +
> +     ret = ida_alloc_range(&tdev->ctx_ida, 1, THAMES_MAX_CONTEXTS - 1, 
> GFP_KERNEL);
> +     if (ret < 0)
> +             return ret;
> +
> +     priv->context_id = ret;
> +     priv->context_serial = atomic64_inc_return(&tdev->last_context_serial);
> +
> +     ret = thames_context_va_init(priv);
> +     if (ret)
> +             goto err_free_id;
> +
> +     ret = thames_context_pgtable_init(priv);
> +     if (ret)
> +             goto err_free_pgtable;
> +
> +     ret = thames_context_map_core_regions(priv);
> +     if (ret)
> +             goto err_free_pgtable;
> +
> +     ret = thames_context_map_user_pages(priv);
> +     if (ret)
> +             goto err_free_pgtable;
> +
> +     asid = thames_asid_alloc(tdev);
> +     if (asid < 0) {
> +             ret = asid;
> +             goto err_free_pgtable;
> +     }
> +     priv->asid = asid;
> +     dev_dbg(tdev->ddev.dev, "Context %u: ASID %d\n", priv->context_id, 
> asid);
> +
> +     thames_context_write_entry(priv);
> +
> +     scoped_guard(mutex, &tdev->contexts_lock)
> +             list_add_tail(&priv->context_node, &tdev->contexts);

[Severity: Medium]
Can thames_context_write_entry() race with a core attaching or resuming
when it runs outside tdev->contexts_lock?

CPU0                                  CPU1
thames_open()
  thames_context_create()
    thames_context_write_entry(priv)
                                      thames_core_attach()
                                        /* firmware boots and clears IPC */
                                        thames_core_wait_handshake()
                                        thames_context_rewrite_entries()
                                          guard(mutex)(&tdev->contexts_lock)
                                          /* priv is not in tdev->contexts */
    scoped_guard(mutex, &tdev->contexts_lock)
      list_add_tail(&priv->context_node,
                    &tdev->contexts)
                                      /* priv's entry on core is missing */

Calling thames_context_write_entry(priv) under tdev->contexts_lock while
adding priv->context_node to tdev->contexts avoids this race.

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

Reply via email to