Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] accel/thames: out-of-bounds IPC write in
  thames_transfers_alloc_table()
- [Medium] accel/thames: missing wmb() after WC IPC writes in
  thames_transfers_write_table()
--

commit b506c4c5faf43e8de930635840dc32e5769eb0c2
Author: Tomeu Vizoso <[email protected]>

accel/thames: Add support for submitting DMA operations

This commit adds validation, address translation, and IPC transfer table
pool management for C7x DRU DMA transfer records.

> diff --git a/drivers/accel/thames/thames_transfers.c 
> b/drivers/accel/thames/thames_transfers.c
> new file mode 100644
> index 0000000000000..113f0b73875cc
> --- /dev/null
> +++ b/drivers/accel/thames/thames_transfers.c
[ ... ]
> +static void thames_transfers_write_table(struct thames_core *core,
> +                                      const struct thames_transfer_table 
> *table)
> +{
> +     static const unsigned int addr_word[2] = { TR_ADDR_LO, TR_DADDR_LO };
> +     const struct thames_transfer *records = table->records;
> +     const u16 *group_counts = table->group_counts;
> +     void *base = core->ipc->transfer_table_pool + table->offset;
> +     struct thames_dru_record *out = base;
> +     u8 *channels = base + 
> THAMES_TRANSFER_TABLE_CHANNELS_OFFSET(table->count);
> +     u32 i, s;
> +
> +     for (i = 0; i < table->count; i++) {
> +             struct thames_dru_record record = records[i].record;
> +
> +             for (s = 0; s < 2; s++) {
> +                     u32 *lo = &record.words[addr_word[s]];
> +                     u64 addr;
> +
> +                     if (records[i].side[s] == THAMES_TRANSFER_SIDE_DDR)
> +                             continue;
> +
> +                     addr = ((u64)lo[1] << 32 | lo[0]) +
> +                            thames_core_window(core, 
> records[i].side[s])->start;
> +                     lo[0] = lower_32_bits(addr);
> +                     lo[1] = upper_32_bits(addr);
> +             }
> +
> +             memcpy(&out[i], &record, sizeof(record));
> +             channels[i] = records[i].channel;
> +     }
> +
> +     memcpy(base + THAMES_TRANSFER_TABLE_GROUPS_OFFSET(table->count), 
> group_counts,
> +            table->group_count * sizeof(u16));
> +}
> +
> +/* Same offsets, so the Ring entries of the Jobs still queued stay valid. */
> +void thames_transfers_rewrite(struct thames_core *core)
> +{
> +     struct thames_device *tdev = core->tdev;
> +     struct thames_transfer_table *table;
> +
> +     guard(mutex)(&tdev->transfer_table_pool_lock);
> +
> +     list_for_each_entry(table, &tdev->transfer_tables, node)
> +             thames_transfers_write_table(core, table);
> +}
> +
> +static int thames_transfers_alloc_table(struct thames_device *tdev,
> +                                     struct thames_transfer_table *table)
> +{
> +     const unsigned long lines = tdev->caps.transfer_table_pool_size / 
> THAMES_ABI_LINE_SIZE;
> +     unsigned long start, nr = table->size / THAMES_ABI_LINE_SIZE;
> +     u32 i;
> +
> +     guard(mutex)(&tdev->transfer_table_pool_lock);
> +
> +     start = bitmap_find_next_zero_area(tdev->transfer_table_pool_map, 
> lines, 0, nr, 0);
> +     if (start + nr > lines)
> +             return -ENOSPC;
> +
> +     bitmap_set(tdev->transfer_table_pool_map, start, nr);
> +     table->offset = start * THAMES_ABI_LINE_SIZE;
> +
> +     for (i = 0; i < tdev->num_cores; i++)
> +             thames_transfers_write_table(&tdev->cores[i], table);
> +
> +     list_add_tail(&table->node, &tdev->transfer_tables);
> +     return 0;
> +}

[Severity: Medium]
Does thames_transfers_write_table() need a trailing wmb() after writing to
core->ipc->transfer_table_pool?

core->ipc is mapped with MEMREMAP_WC, and unlike
thames_context_bump_generation(), neither thames_transfers_alloc_table()
nor thames_transfers_rewrite() issues a wmb() before releasing
transfer_table_pool_lock.  If a job is created on one CPU and submitted to
the ring by a scheduler worker on another CPU (or if a core comes online
after thames_transfers_rewrite()), the DSP can observe stale or partially
written DRU transfer records in the write-combined IPC pool.

[Severity: Medium]
Can thames_transfers_alloc_table() write past the end of core->ipc when
cores have different ipc_size values or a core has not attached?

thames_platform_probe() sizes the bitmap from the largest core->ipc_size
using max() and keeps offline or failed-attach cores in
tdev->cores[0 .. tdev->num_cores - 1].  When the first core attaches,
thames_core_read_caps() checks caps->transfer_table_pool_size only against
that core's ipc_size:

drivers/accel/thames/thames_core.c:thames_core_read_caps() {
    ...
        if (!caps->transfer_table_pool_size ||
            !IS_ALIGNED(caps->transfer_table_pool_size, THAMES_ABI_LINE_SIZE) ||
            pool_offset + caps->transfer_table_pool_size > core->ipc_size) {
    ...
}

Because thames_transfers_alloc_table() writes at table->offset into every
core in tdev->cores[], any other core whose ipc_size is smaller than
pool_offset + table->offset + table->size will suffer an out-of-bounds
write past its memremap()ed core->ipc mapping.

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

Reply via email to