Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] accel/amd-ai-engine: memory leak and lifetime bug in
  aie_part_create()
- [Low] accel/amd-ai-engine: dead code, unused exports, and missing
  accel integration
--

commit 9d0271621aaf2e78655cf01aa521aa41dffc506a
Author: Gregory Williams <[email protected]>

accel/amd-ai-engine: Add AMD AI Engine device driver

This commit adds the initial AMD Versal AI Engine platform driver under
drivers/accel/amd-ai-engine, including device probing, column resource
tracking, and partition request and release APIs.

> diff --git a/drivers/accel/amd-ai-engine/Kconfig 
> b/drivers/accel/amd-ai-engine/Kconfig
> new file mode 100644
> index 0000000000000..7f32928cb20f2
> --- /dev/null
> +++ b/drivers/accel/amd-ai-engine/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +
> +config DRM_ACCEL_AMDAIE
> +     tristate "AMD Versal AI Engine"
> +     depends on ARM64 || COMPILE_TEST
> +     depends on ZYNQMP_FIRMWARE
> +     depends on DRM_ACCEL
> +     help
> +       This option enables support for the AMD AI engine driver.
> +       One AMD AI engine device can have multiple partitions (groups of
> +       AI engine tiles). AMD AI engine device driver instance manages
> +       AI engine partitions. Applications access its partitions through
> +       AI engine partition device instance.
> +
> +       If unsure, say N.

[Severity: Low]
Why is this driver placed under drivers/accel/ with a dependency on
DRM_ACCEL when it does not register a drm_device with DRIVER_COMPUTE_ACCEL
or expose any userspace interface across the series?

Also, aie_partition_request() and aie_partition_release() in
ai-engine-dev.c are exported with EXPORT_SYMBOL_GPL() without any in-tree
callers in the series, and aie_resource_check_region() in ai-engine-res.c
is never called.

> diff --git a/drivers/accel/amd-ai-engine/ai-engine-part.c 
> b/drivers/accel/amd-ai-engine/ai-engine-part.c
> new file mode 100644
> index 0000000000000..f80a6fa433aa1
> --- /dev/null
> +++ b/drivers/accel/amd-ai-engine/ai-engine-part.c

[ ... ]

> @@ -0,0 +1,77 @@

[ ... ]

> +void aie_part_release(struct aie_partition *apart)
> +{
> +     struct aie_device *adev = apart->adev;
> +
> +     list_del(&apart->node);
> +     aie_resource_put_region(&adev->cols_res,
> +                             apart->range.start.col -
> +                             adev->range.start.col,
> +                             apart->range.size.col);
> +     device_unregister(&apart->dev);
> +}
> +
> +static void aie_part_release_device(struct device *dev)
> +{
> +     (void)dev;
> +}
> +
> +/**
> + * aie_part_create() - create AI engine partition instance
> + * @adev: AI engine device
> + * @start_col: start column of AI engine partition
> + * @num_col: number of columns of AI engine partition
> + *
> + * Return: created AI engine partition pointer for success, and PTR_ERR
> + *      for failure.
> + */
> +struct aie_partition *aie_part_create(struct aie_device *adev,
> +                                   u8 start_col, u8 num_col)
> +{
> +     struct aie_partition *apart;
> +     struct device *dev;
> +
> +     apart = devm_kzalloc(adev->dev, sizeof(*apart), GFP_KERNEL);
> +     if (!apart)
> +             return ERR_PTR(-ENOMEM);
> +
> +     apart->adev = adev;
> +     mutex_init(&apart->mlock);
> +     apart->range.start.col = start_col;
> +     apart->range.size.col = num_col;
> +     apart->range.start.row = adev->range.start.row;
> +     apart->range.size.row = adev->range.size.row;
> +
> +     /* create partition device */
> +     dev = &apart->dev;
> +     dev->parent = apart->adev->dev;
> +     dev_set_name(dev, "aiepart_%u_%u", apart->range.start.col,
> +                  apart->range.size.col);
> +     dev_set_drvdata(dev, apart);
> +     dev->release = aie_part_release_device;
> +
> +     if (device_register(&apart->dev)) {
> +             dev_err(adev->dev, "failed to register partition device\n");
> +             put_device(dev);
> +             return ERR_PTR(-ENODEV);
> +     }
> +
> +     return apart;
> +}

[Severity: High]
Does allocating apart with devm_kzalloc(adev->dev, ...) and using a no-op
dev->release callback leak struct aie_partition across runtime partition
request and release cycles?

Every call to aie_partition_release() (as well as the device_register()
error path in aie_part_create()) drops the reference on apart->dev without
freeing apart, leaving it allocated on adev->dev's devres list until the
parent driver unbinds.  In addition, tying a structure that embeds a
reference-counted struct device to parent devres frees apart on parent
unbind even if external references to apart->dev are still held, causing a
use-after-free when the last reference is dropped.

Allocating apart with kzalloc() and freeing it with kfree() in
aie_part_release_device() would tie its lifetime to apart->dev;
aie_partition_release() in ai-engine-dev.c would then also need to save
apart->adev in a local variable before calling aie_part_release(apart) so
that mutex_unlock(&adev->mlock) does not dereference apart after
device_unregister(&apart->dev) frees it.

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

Reply via email to