Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [High] iio: adc: stm32-mdf: NULL pointer dereference in
  stm32_mdf_sitf_get_clk()
- [Medium] iio: adc: stm32-mdf: off-by-one and underflow in
  stm32_mdf_sitf_parse()
- [Medium] iio: adc: stm32-mdf: invalid SCKSRC and bit update in
  stm32_mdf_sitf_parse()
- [Medium] iio: adc: stm32-mdf: missing max_register_is_0 and name in
  sitf regmap_config
- [Medium] iio: adc: stm32-mdf: unbalanced kclk prepare count on
  suspend/resume errors
- [Low] iio: adc: stm32-mdf: module self-pinning in
  stm32_mdf_sitf_get_clk()
- [Low] iio: adc: stm32-mdf: unused base member in struct stm32_mdf_sitf
--

commit a885a42b631244af337425ef9681fd1c4db1960e
Author: Olivier Moysan <[email protected]>

iio: adc: stm32-mdf: add serial interfaces support

This commit adds support for the STM32 MDF serial interfaces (SPI and
Manchester modes), including Device Tree parsing, clock selection,
suspend/resume handling, and reference-counted start and stop helpers.

> diff --git a/drivers/iio/adc/stm32-mdf-core.c 
> b/drivers/iio/adc/stm32-mdf-core.c
> index 4e462c6c42bc8..5b5d2d4cbb5c5 100644
> --- a/drivers/iio/adc/stm32-mdf-core.c
> +++ b/drivers/iio/adc/stm32-mdf-core.c
[ ... ]
> @@ -794,7 +806,9 @@ static int stm32_mdf_core_suspend(struct device *dev)
>       regcache_cache_only(priv->regmap, true);
>       regcache_mark_dirty(priv->regmap);
>  
> -     /* Balance regmap_mmio_attach_clk() clk_prepare() */
> +     /* Balance regmap_mmio_attach_clk() for the SITFs and the core. */
> +     list_for_each_entry(sitf, &mdf->sitf_list, entry)
> +             clk_unprepare(priv->kclk);
>       clk_unprepare(priv->kclk);
>  
>       return pinctrl_pm_select_sleep_state(dev);
> @@ -804,6 +818,8 @@ static int stm32_mdf_core_resume(struct device *dev)
>  {
>       struct stm32_mdf *mdf = dev_get_drvdata(dev);
>       struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> +     struct stm32_mdf_sitf *sitf;
> +     unsigned int prepared = 0;
>       int ret;
>  
>       ret = pinctrl_pm_select_default_state(dev);
> @@ -825,8 +841,24 @@ static int stm32_mdf_core_resume(struct device *dev)
>               goto err_cache;
>       }
>  
> +     list_for_each_entry(sitf, &mdf->sitf_list, entry) {
> +             ret = clk_prepare(priv->kclk);
> +             if (ret)
> +                     goto err_sitf;
> +             prepared++;
> +
> +             ret = regmap_write(sitf->regmap, MDF_SITFCR_REG, sitf->sitfcr);
> +             if (ret)
> +                     goto err_sitf;
> +     }
> +
>       return pm_runtime_force_resume(dev);
>  
> +err_sitf:
> +     while (prepared) {
> +             clk_unprepare(priv->kclk);
> +             prepared--;
> +     }
>  err_cache:
>       clk_unprepare(priv->kclk);
>  err_clk:

[Severity: Medium]
Does an error in pinctrl_pm_select_sleep_state() or in
stm32_mdf_core_resume() leave priv->kclk unprepared while the regmap_mmio
instances remain active?

Each regmap_mmio_attach_clk() call at probe holds a clk_prepare() reference
that regmap_mmio_free_context() releases on teardown.  If
pinctrl_pm_select_sleep_state() fails in stm32_mdf_core_suspend(), or if
stm32_mdf_core_resume() fails and unwinds clk_prepare(priv->kclk) (while
also leaving priv->regmap out of cache-only mode), later register accesses
call clk_enable() on an unprepared clock and driver removal triggers
clk_unprepare() underflow warnings.

> diff --git a/drivers/iio/adc/stm32-mdf-serial.c 
> b/drivers/iio/adc/stm32-mdf-serial.c
> new file mode 100644
> index 0000000000000..6ba3be0198478
> --- /dev/null
> +++ b/drivers/iio/adc/stm32-mdf-serial.c
[ ... ]
> +static const struct regmap_config stm32_sitf_regmap_cfg = {
> +     .reg_bits = 32,
> +     .val_bits = 32,
> +     .reg_stride = sizeof(u32),
> +     .max_register = MDF_SITFCR_REG,
> +     .fast_io = true,
> +     /* Do not use regcache as it does not support single register map */
> +};

[Severity: Medium]
Should stm32_sitf_regmap_cfg set .max_register_is_0 = true and give each
serial interface regmap a distinct .name?

Because MDF_SITFCR_REG is 0, __regmap_init() treats .max_register as
unspecified unless .max_register_is_0 = true is set, which disables upper
bounds checking on the 4-byte iomapped region (and is also why regcache
rejected the single-register map).  In addition, stm32_mdf_sitf_init()
registers every sitf->regmap on the parent device with .name == NULL, so
their debugfs entries collide with priv->regmap and with each other.

[ ... ]

> +static int stm32_mdf_sitf_get_clk(struct device *dev, struct device_node *np,
> +                               struct stm32_mdf_sitf *sitf)
> +{
> +     struct clk *sck;
> +     int ret;
> +
> +     /* SITF nodes have no device to apply their assigned clock 
> configuration. */
> +     ret = of_clk_set_defaults(np, false);
> +     if (ret)
> +             return dev_err_probe(dev, ret, "Can't configure serial clock 
> for %pOF\n", np);
> +
> +     sitf->scksrc = STM32_MDF_SCKSRC_NONE;
> +     /* Optional clock. Clock not needed in Manchester mode. */
> +     if (!of_property_present(np, "clocks"))
> +             return 0;
> +
> +     sck = devm_get_clk_from_child(dev, np, NULL);
> +     if (IS_ERR(sck))
> +             return dev_err_probe(dev, PTR_ERR(sck), "Can't get serial clock 
> for %pOF\n", np);

[Severity: Low]
When the SITF clock is one of the internal CCK0 or CCK1 clocks registered
by the same driver in stm32_mdf_core_register_clock_provider(), does
devm_get_clk_from_child() pin stm32-mdf.ko to itself?

devm_get_clk_from_child() resolves the clock through OF with a NULL
consumer owner, so clk_hw_create_clk() sees owner != core->owner and calls
try_module_get() on stm32-mdf.ko, preventing rmmod until the device is
manually unbound.

> +
> +     sitf->sck = sck;
> +     if (!strncmp(__clk_get_name(sck), STM32_MDF_CCK0, 
> sizeof(STM32_MDF_CCK0)))
> +             sitf->scksrc = STM32_MDF_SCKSRC_CCK0;
> +     else if (!strncmp(__clk_get_name(sck), STM32_MDF_CCK1, 
> sizeof(STM32_MDF_CCK1)))
> +             sitf->scksrc = STM32_MDF_SCKSRC_CCK1;
> +     else
> +             sitf->scksrc = STM32_MDF_SCKSRC_CLK;
> +
> +     return 0;
> +}

[Severity: High]
Can sck be NULL here, causing a NULL pointer dereference in strncmp()?

In stm32_mdf_core_register_clock_provider(), clk_data->num is set to
STM32_MDF_MAX_CCK while only clocks listed in clock-output-names are
populated in clk_data->hws[]:

drivers/iio/adc/stm32-mdf-core.c:stm32_mdf_core_register_clock_provider() {
    ...
                clk_data->hws[index] = mdf_hw[clk_id];
    ...
                index++;
        }

        clk_data->num = STM32_MDF_MAX_CCK;
}

If a sitf node references an unpopulated CCK index, of_clk_hw_onecell_get()
returns NULL and devm_get_clk_from_child() returns sck = NULL.  IS_ERR(sck)
does not catch NULL, so __clk_get_name(sck) returns NULL and strncmp()
dereferences it.

> +
> +static int stm32_mdf_sitf_parse(struct device *dev, struct device_node *np,
> +                             struct stm32_mdf_sitf *sitf)
> +{
> +     const char *str;
> +     int ret, mode;
> +     u32 idx, sitfcr;
> +
> +     ret = of_property_read_u32(np, "reg", &idx);
> +     if (ret)
> +             return dev_err_probe(dev, ret, "Could not get interface index 
> for %pOF\n", np);
> +
> +     if (idx % 0x80) {
> +             dev_err(dev, "Unexpected reg property value [%x]\n", idx);
> +             return -EINVAL;
> +     }
> +
> +     idx = (idx >> 7) - 1;
> +     if (idx > sitf->mdf->nbf) {
> +             dev_err(dev, "Interface index [%d] exceeds maximum [%d]\n", 
> idx, sitf->mdf->nbf);
> +             return -EINVAL;
> +     }

[Severity: Medium]
Should the bounds check on the 0-based idx be idx >= sitf->mdf->nbf?

With nbf serial interfaces at offsets 0x80 .. 0x80 * nbf, valid 0-based
indices are 0 .. sitf->mdf->nbf - 1, so idx > sitf->mdf->nbf is off by one
and accepts idx == sitf->mdf->nbf.  Also, if reg is 0, idx % 0x80 is 0 and
(idx >> 7) - 1 underflows u32 idx to U32_MAX, which %d prints as -1.

[ ... ]

> +     sitf->id = idx;
> +     sitf->node = of_fwnode_handle(np);
> +     spin_lock_init(&sitf->lock);
> +
> +     sitfcr |= FIELD_PREP(MDF_SITFCR_SCKSRC_MASK, sitf->scksrc);
> +
> +     return regmap_set_bits(sitf->regmap, MDF_SITFCR_REG, sitfcr);
> +}

[Severity: Medium]
Does this write the software sentinel STM32_MDF_SCKSRC_NONE into
MDF_SITFCR_SCKSRC_MASK when no clock is provided in Manchester mode, and
leave stale bits set in MDF_SITFCR_REG?

When the optional clocks property is absent, sitf->scksrc is
STM32_MDF_SCKSRC_NONE (3), which FIELD_PREP() encodes as reserved value
0b11 in SCKSRC[1:0].  Furthermore, regmap_set_bits() only sets 1-bits in
sitfcr and never clears 0-bits in MDF_SITFCR_SITFMOD_MASK |
MDF_SITFCR_SCKSRC_MASK (and performs no write at all when sitfcr is 0);
should this use regmap_update_bits() with that mask instead?

> diff --git a/drivers/iio/adc/stm32-mdf.h b/drivers/iio/adc/stm32-mdf.h
> index e3617b181e28a..fa689954d7c15 100644
> --- a/drivers/iio/adc/stm32-mdf.h
> +++ b/drivers/iio/adc/stm32-mdf.h
[ ... ]
> +struct stm32_mdf_sitf {
> +     struct list_head entry;
> +     struct device *dev;
> +     struct regmap *regmap;
> +     struct stm32_mdf *mdf;
> +     struct fwnode_handle *node;
> +     struct clk *sck;
> +     void __iomem *base;
> +     spinlock_t lock; /* Manage race conditions on sitf state */

[Severity: Low]
This isn't a bug, but base in struct stm32_mdf_sitf is never read or
written and can be dropped.

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

Reply via email to