Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] iio: adc: stm32-mdf: premature rounding in
  stm32_mdf_core_cck_determine_rate()
- [High] iio: adc: stm32-mdf: ABBA deadlock between CCF enable_lock and
  regmap spinlock
- [High] iio: adc: stm32-mdf: broken onecell indexing and __counted_by
  in clock provider
- [Medium] iio: adc: stm32-mdf: unlocked RMW and unchecked read in
  clk_cckx_gate_ops
- [Medium] iio: adc: stm32-mdf: static global clk_hw structs and stack
  init data in probe
- [Medium] iio: adc: stm32-mdf: unbalanced kclk exclusivity in
  lock/unlock_kclk_rate()
--

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

iio: adc: stm32-mdf: add cck clocks provider

This commit registers the MDF CCK0 and CCK1 clock outputs with the common
clock framework and adds helpers to lock, unlock, query and restore the
clock rate.

> diff --git a/drivers/iio/adc/stm32-mdf-core.c 
> b/drivers/iio/adc/stm32-mdf-core.c
> index ca50ce566a435..4e462c6c42bc8 100644
> --- a/drivers/iio/adc/stm32-mdf-core.c
> +++ b/drivers/iio/adc/stm32-mdf-core.c
[ ... ]
> @@ -194,6 +210,96 @@ int stm32_mdf_core_trigger(struct stm32_mdf *mdf)
[ ... ]
> +int stm32_mdf_core_lock_kclk_rate(struct stm32_mdf *mdf)
> +{
> +     struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> +     struct device *dev = &priv->pdev->dev;
> +     int ret;
> +
> +     if (!atomic_read(&priv->n_active_ch)) {
> +             /* Request rate exclusivity on kernel clock right now */
> +             ret = clk_rate_exclusive_get(priv->kclk);
> +             if (ret < 0) {
> +                     dev_err(dev, "Failed to get exclusive rate on kernel 
> clock: [%d]\n", ret);
> +                     return ret;
> +             }
> +
> +             priv->exclusive_flg = true;
> +     }
> +
> +     return 0;
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_core_lock_kclk_rate);
> +
> +/**
> + * stm32_mdf_core_unlock_kclk_rate() - Release the MDF kernel clock rate lock
> + * @mdf: MDF instance
> + *
> + * Release an exclusive rate acquired by stm32_mdf_core_lock_kclk_rate() when
> + * no MDF channels are active. The lock is retained while channels are 
> active.
> + */
> +void stm32_mdf_core_unlock_kclk_rate(struct stm32_mdf *mdf)
> +{
> +     struct stm32_mdf_priv *priv = to_stm32_mdf_priv(mdf);
> +
> +     if (!atomic_read(&priv->n_active_ch)) {
> +             clk_rate_exclusive_put(priv->kclk);
> +             priv->exclusive_flg = false;
> +     }
> +}
> +EXPORT_SYMBOL_GPL(stm32_mdf_core_unlock_kclk_rate);

[Severity: Medium]
Can concurrent calls across sibling MDF filter devices unbalance
clk_rate_exclusive_get() and clk_rate_exclusive_put() on priv->kclk?

Neither stm32_mdf_core_lock_kclk_rate() nor
stm32_mdf_core_unlock_kclk_rate() takes a lock or checks
priv->exclusive_flg:

CPU0 (Filter A)                   CPU1 (Filter B)
stm32_mdf_core_start_mdf()
  n_active_ch becomes 1
  clk_rate_exclusive_get()
  priv->exclusive_flg = true
                                  stm32_mdf_core_lock_kclk_rate()
                                    /* n_active_ch != 0: does nothing */
stm32_mdf_core_stop_mdf()
  n_active_ch becomes 0
  clk_rate_exclusive_put()
  priv->exclusive_flg = false
                                  stm32_mdf_core_unlock_kclk_rate()
                                    /* n_active_ch == 0 */
                                    clk_rate_exclusive_put()  <-- underflow

That extra put triggers WARN_ON(clk->exclusive_count <= 0) in
clk_rate_exclusive_put().  Conversely, if two idle filter devices call
stm32_mdf_core_lock_kclk_rate() while n_active_ch is 0,
clk_rate_exclusive_get() runs twice while priv->exclusive_flg only records
a single boolean; if a channel starts before they unlock,
stm32_mdf_core_stop_mdf() only puts once and leaks an exclusive reference,
causing later clk_set_rate() calls in stm32_mdf_core_restore_cck() to fail.

[ ... ]

> @@ -223,11 +329,301 @@ int stm32_mdf_core_stop_mdf(struct stm32_mdf *mdf)
>  }
>  EXPORT_SYMBOL_GPL(stm32_mdf_core_stop_mdf);
>  
> +static int stm32_mdf_core_cck_divider_set_rate(struct platform_device *pdev,
> +                                            struct stm32_mdf_priv *priv,
> +                                            unsigned long rate,
> +                                            unsigned long parent_rate)
> +{
> +     struct device *dev = &pdev->dev;
> +     unsigned long ratio, delta, delta_ppm, procdiv;
> +     unsigned int cckdiv;
> +     int ret;
> +
> +     if (!rate || rate > STM32_MDF_MAX_CCK_RATE || !parent_rate)
> +             return -EINVAL;
> +
> +     ratio = DIV_ROUND_CLOSEST(parent_rate, rate);
> +     if (!ratio) {
> +             dev_err(dev, "CCK frequency above kernel frequency\n");
> +             return -EINVAL;
> +     }
> +
> +     delta = abs(parent_rate - (ratio * rate));
> +
> +     delta_ppm = (1000000 * delta) / parent_rate;
> +     if (delta_ppm > 1000) {
> +             /* If frequency deviation is higher than 1000 ppm return error 
> */
> +             dev_dbg(dev, "CCK clock deviation [%lu] ppm too large: [%lu] vs 
> [%lu] Hz\n",
> +                     delta_ppm, DIV_ROUND_CLOSEST(parent_rate, ratio), rate);
> +             return -EINVAL;
> +     } else if (delta_ppm) {
> +             dev_dbg(dev, "CCK clock frequency not accurate: [%lu] ppm 
> deviation\n", delta_ppm);
> +     }
> +
> +     /*
> +      * The total divider ratio must be split between proc divider and
> +      * cck divider. Try to maximize cck divider first, to help fulfilling
> +      * frequency ratio requirements between fproc and fcck.
> +      */
> +     cckdiv = min_t(unsigned long, ratio, MDF_CKG_CCKDIV_MAX);
> +     while (ratio % cckdiv)
> +             cckdiv--;
> +     procdiv = ratio / cckdiv;
> +
> +     if (procdiv > MDF_PROCDIV_MAX) {
> +             dev_err(dev, "Proc divider out of range: %lu > %lu\n", procdiv,
> +                     MDF_PROCDIV_MAX);
> +             return -EINVAL;
> +     }
> +
> +     priv->mdf.fproc = DIV_ROUND_CLOSEST(parent_rate, procdiv);
> +
> +     /* Configure CKGCR register */
> +     dev_dbg(dev, "Set MDF dividers: CCKDIV [%u], PROCDIV [%lu]\n", cckdiv, 
> procdiv);
> +     ret = regmap_update_bits(priv->regmap, MDF_CKGCR_REG,
> +                              MDF_CKG_CCKDIV_MASK | MDF_CKG_PROCDIV_MASK,
> +                              FIELD_PREP(MDF_CKG_CCKDIV_MASK, cckdiv - 1) |
> +                              FIELD_PREP(MDF_CKG_PROCDIV_MASK, procdiv - 1));
> +     if (!ret)
> +             priv->cck_freq_actual = DIV_ROUND_CLOSEST(parent_rate, ratio);
> +
> +     return ret;
> +}
> +
> +static int stm32_mdf_core_cck_set_rate(struct clk_hw *hw, unsigned long rate,
> +                                    unsigned long parent_rate)
> +{
> +     struct clk_mdf_cck *clk = to_clk_mdf_cck(hw);
> +     struct stm32_mdf_priv *priv = clk->priv;
> +
> +     return stm32_mdf_core_cck_divider_set_rate(priv->pdev, priv, rate, 
> parent_rate);
> +}
> +
> +static int stm32_mdf_core_cck_determine_rate(struct clk_hw *hw,
> +                                          struct clk_rate_request *req)
> +{
> +     struct clk_mdf_cck *clk = to_clk_mdf_cck(hw);
> +     struct stm32_mdf_priv *priv = clk->priv;
> +     struct platform_device *pdev = priv->pdev;
> +     unsigned long ratio;
> +
> +     if (!req->rate || req->rate > STM32_MDF_MAX_CCK_RATE || 
> !req->best_parent_rate)
> +             return -EINVAL;
> +
> +     ratio = DIV_ROUND_CLOSEST(req->best_parent_rate, req->rate);
> +     if (!ratio) {
> +             dev_err(&pdev->dev, "CCK frequency above kernel frequency\n");
> +             return -EINVAL;
> +     }
> +
> +     req->rate = DIV_ROUND_CLOSEST(req->best_parent_rate, ratio);
> +
> +     return 0;
> +}

[Severity: High]
Does rounding req->rate in stm32_mdf_core_cck_determine_rate() bypass the
1000 ppm accuracy check and divider validation in
stm32_mdf_core_cck_divider_set_rate()?

During clk_set_rate(), clk_calc_new_rates() calls
stm32_mdf_core_cck_determine_rate() first and stores the rounded req->rate
in core->new_rate.  clk_change_rate() then passes that already-rounded
core->new_rate (not the caller's requested rate) to
stm32_mdf_core_cck_set_rate(), and ignores the return value of
core->ops->set_rate().  As a result:

- Inside stm32_mdf_core_cck_divider_set_rate(), rate is already
  DIV_ROUND_CLOSEST(parent_rate, ratio), so delta only measures the integer
  division remainder rather than the deviation from the requested frequency,
  defeating the 1000 ppm check.
- If procdiv > MDF_PROCDIV_MAX, stm32_mdf_core_cck_divider_set_rate()
  returns -EINVAL, which clk_change_rate() ignores; clk_set_rate() returns
  0 while MDF_CKGCR_REG and priv->cck_freq_actual remain unconfigured.

Also, in stm32_mdf_core_cck_divider_set_rate(), 1000000 * delta is computed
in unsigned long and overflows on 32-bit builds when delta >= 4295 Hz, and
priv->mdf.fproc is updated before checking whether regmap_update_bits()
succeeded.  Should the 1000 ppm and procdiv <= MDF_PROCDIV_MAX checks be
performed in stm32_mdf_core_cck_determine_rate() using 64-bit arithmetic?

[ ... ]

> +static inline u32 clk_cckx_gate_readl(struct clk_cckx_gate *gate)
> +{
> +     u32 val;
> +
> +     regmap_read(gate->map, gate->map_offset, &val);
> +
> +     return val;
> +}
> +
> +static inline void clk_cckx_gate_writel(struct clk_cckx_gate *gate, u32 val)
> +{
> +     regmap_write(gate->map, gate->map_offset, val);
> +}
> +
> +static void clk_cckx_gate_endisable(struct clk_hw *hw, int enable)
> +{
> +     struct clk_cckx_gate *gate = to_clk_cckx_gate(hw);
> +     unsigned long flags = 0;
> +     u32 reg;
> +
> +     if (gate->lock)
> +             spin_lock_irqsave(gate->lock, flags);
> +     else
> +             __acquire(gate->lock);
> +
> +     reg = clk_cckx_gate_readl(gate);
> +
> +     if (enable)
> +             reg |= BIT(gate->bit_idx);
> +     else
> +             reg &= ~BIT(gate->bit_idx);
> +
> +     clk_cckx_gate_writel(gate, reg);
> +
> +     if (gate->lock)
> +             spin_unlock_irqrestore(gate->lock, flags);
> +     else
> +             __release(gate->lock);
> +}
> +
> +static int clk_cckx_gate_enable(struct clk_hw *hw)
> +{
> +     clk_cckx_gate_endisable(hw, 1);
> +
> +     return 0;
> +}
> +
> +static void clk_cckx_gate_disable(struct clk_hw *hw)
> +{
> +     clk_cckx_gate_endisable(hw, 0);
> +}
> +
> +static int clk_cckx_gate_is_enabled(struct clk_hw *hw)
> +{
> +     struct clk_cckx_gate *gate = to_clk_cckx_gate(hw);
> +     u32 reg;
> +
> +     reg = clk_cckx_gate_readl(gate);
> +
> +     reg &= BIT(gate->bit_idx);
> +
> +     return reg ? 1 : 0;
> +}

[Severity: High]
Can calling regmap_read() and regmap_write() on priv->regmap inside
clk_cckx_gate_ops cause an ABBA deadlock with the clock framework's
enable_lock?

priv->regmap uses .fast_io = true (map->spinlock) and attaches priv->kclk
via regmap_mmio_attach_clk() in stm32_mdf_regmap_init().  Every hardware
access on priv->regmap acquires map->spinlock first and then calls
clk_enable(ctx->clk) in regmap_mmio_read()/regmap_mmio_write(), which takes
the CCF global enable_lock.  Conversely, CCF calls .enable, .disable and
.is_enabled in clk_cckx_gate_ops while already holding enable_lock:

CPU0                              CPU1
----                              ----
lock(&map->spinlock)
                                  lock(&enable_lock)
                                  lock(&map->spinlock)
lock(&enable_lock)

 *** DEADLOCK ***

CPU0: stm32_mdf_core_trigger() -> regmap_read() -> regmap_mmio_read() ->
      clk_enable()
CPU1: clk_enable() -> clk_cckx_gate_enable() -> clk_cckx_gate_endisable() ->
      regmap_read()

> +static const struct clk_ops clk_cckx_gate_ops = {
> +     .enable = clk_cckx_gate_enable,
> +     .disable = clk_cckx_gate_disable,
> +     .is_enabled = clk_cckx_gate_is_enabled,
> +};
> +
> +static struct clk_cckx_gate clk_cck0 = {
> +     .map_offset = MDF_CKGCR_REG,
> +     .bit_idx = 1,
> +};
> +
> +static struct clk_cckx_gate clk_cck1 = {
> +     .map_offset = MDF_CKGCR_REG,
> +     .bit_idx = 2,
> +};

[Severity: Medium]
Does clk_cckx_gate_endisable() perform an unlocked read-modify-write on
MDF_CKGCR_REG using an unchecked regmap_read()?

clk_cck0.lock and clk_cck1.lock are never initialized, so gate->lock is
always NULL and no lock is held between clk_cckx_gate_readl() and
clk_cckx_gate_writel().  A concurrent update to MDF_CKGCR_REG in
stm32_mdf_core_start_mdf(), stm32_mdf_core_stop_mdf() or
stm32_mdf_core_cck_divider_set_rate() can be overwritten with stale bits.
In addition, clk_cckx_gate_readl() leaves val uninitialized on the stack and
ignores the return value of regmap_read(), which would write uninitialized
stack data to MDF_CKGCR_REG if the read fails.

> +static int stm32_mdf_core_register_clock_provider(struct platform_device 
> *pdev,
> +                                               struct stm32_mdf_priv *priv)
> +{
> +     struct device_node *node = pdev->dev.of_node;
> +     struct device *dev = &pdev->dev;
> +     struct clk_hw_onecell_data *clk_data;
> +     struct property *prop;
> +     const char *clk_name;
> +     u32 ckgcr = 0;
> +     int index = 0, ret, clk_id;
> +     struct clk_hw *mdf_hw[] = { &clk_cck0.hw, &clk_cck1.hw };
> +     struct clk_cckx_gate *gate;
> +
> +     clk_cck.hw.init = CLK_HW_INIT_PARENTS_DATA("clk_cck", 
> clk_cck_parent_data,
> +                                                &clk_cck_ops, 0);
> +     clk_cck0.hw.init = CLK_HW_INIT_HW(STM32_MDF_CCK0, &clk_cck.hw, 
> &clk_cckx_gate_ops,
> +                                       CLK_SET_RATE_PARENT | 
> CLK_IGNORE_UNUSED);
> +     clk_cck1.hw.init = CLK_HW_INIT_HW(STM32_MDF_CCK1, &clk_cck.hw, 
> &clk_cckx_gate_ops,
> +                                       CLK_SET_RATE_PARENT | 
> CLK_IGNORE_UNUSED);
> +
> +     clk_cck.priv = priv;
> +
> +     ret = devm_clk_hw_register(dev, &clk_cck.hw);
> +     if (ret)
> +             return dev_err_probe(dev, ret, "Failed to register clk_cck");
> +
> +     clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, 
> STM32_MDF_MAX_CCK), GFP_KERNEL);
> +     if (!clk_data)
> +             return -ENOMEM;
> +
> +     of_property_for_each_string(node, "clock-output-names", prop, clk_name) 
> {
> +             if (index >= STM32_MDF_MAX_CCK) {
> +                     dev_err(dev, "Too many cck providers defined\n");
> +                     return -EINVAL;
> +             }
> +
> +             if (!strncmp(clk_name, "cck0", 4)) {
> +                     clk_id = 0;
> +             } else if (!strncmp(clk_name, "cck1", 4)) {
> +                     clk_id = 1;
> +             } else {
> +                     dev_err(dev, "Unexpected cck clock provider name 
> [%s]\n", clk_name);
> +                     return -EINVAL;
> +             }
> +
> +             gate = to_clk_cckx_gate(mdf_hw[clk_id]);
> +             gate->map = priv->regmap;
> +
> +             ret = devm_clk_hw_register(dev, mdf_hw[clk_id]);

[Severity: Medium]
Can static file-scope clk_cck, clk_cck0 and clk_cck1 structures and stack
compound literals in CLK_HW_INIT_* corrupt state or dereference NULL?

- CLK_HW_INIT_PARENTS_DATA() and CLK_HW_INIT_HW() create compound literal
  struct clk_init_data objects on stm32_mdf_core_register_clock_provider()'s
  stack.  __clk_register() only clears hw->init = NULL for clocks that are
  registered, so when clock-output-names lists fewer than two clocks, the
  unregistered global clk_cck0.hw.init or clk_cck1.hw.init pointer is left
  dangling into expired stack memory after this function returns.
- Conversely, if clock-output-names contains duplicate names (such as
  "cck1", "cck1"), the first devm_clk_hw_register() clears
  mdf_hw[clk_id]->init = NULL and the second call dereferences NULL at
  hw->init->name in __clk_register().
- Because clk_cck, clk_cck0 and clk_cck1 are file-scope static globals with
  fixed clock names, probing a second MDF instance overwrites clk_cck.priv
  and gate->map of the first instance and fails registration with -EEXIST.

> +             if (ret)
> +                     return dev_err_probe(dev, ret, "Failed to register %s 
> clock provider",
> +                                          clk_name);
> +
> +             clk_data->hws[index] = mdf_hw[clk_id];
> +
> +             /* Configure the CCKx clock as output */
> +             ckgcr |= clk_id ? MDF_CKG_CCK1DIR : MDF_CKG_CCK0DIR;
> +
> +             index++;
> +     }
> +
> +     /*
> +      * Set num to max number of clock. This allows any clocks indice below 
> maximum
> +      * in of_clk_hw_onecell_get, whatever the actual number of providers.
> +      */
> +     clk_data->num = STM32_MDF_MAX_CCK;
> +     ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_data);
> +     if (ret) {
> +             dev_err(dev, "Failed to add %s clock provider: %d\n",
> +                     clk_name ? clk_name : "", ret);
> +             return ret;
> +     }

[Severity: High]
Does writing clk_data->hws[index] before setting clk_data->num violate the
__counted_by(num) annotation on struct clk_hw_onecell_data and mis-index
single-clock configurations?

struct clk_hw_onecell_data declares hws[] with __counted_by(num), so
writing to clk_data->hws[index] while clk_data->num is still 0 triggers a
UBSAN bounds violation when CONFIG_UBSAN_BOUNDS is enabled; clk_data->num
must be set before accessing clk_data->hws[].

In addition:

- Indexing clk_data->hws[] by sequential loop counter index instead of
  clk_id places clk_cck1 at hws[0] when clock-output-names only contains
  "cck1", and leaves unpopulated slots in hws[] as NULL (which
  of_clk_hw_onecell_get() returns as a dummy NULL clock rather than
  ERR_PTR(-ENOENT)).
- The DT binding allows #clock-cells = <0> when a single CCK output is
  used, in which case of_parse_clkspec() parses 0 arguments and
  of_clk_hw_onecell_get() reads clkspec->args[0] from uninitialized stack
  memory in of_clk_get_hw().
- After of_property_for_each_string() finishes, clk_name is always NULL, so
  the dev_err() message on devm_of_clk_add_hw_provider() failure always
  prints an empty name.

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

Reply via email to