Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alex Elder" <elder@riscstar.com>
Cc: linux-clk@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v5 3/4] clk: toshiba: introduce a TC9564 clock driver
Date: Thu, 03 Dec 2026 20:22:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-248624@kernel.org> (raw)
In-Reply-To: <20261203200948.1595470-4-elder@riscstar.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] clk: tc9564: reversed devres order frees aux_data before aux
  device destroy
- [Medium] clk: tc9564: memory leak of aux_data on ida_alloc() failure
- [Medium] clk: tc9564: rate-based parent lookup breaks orphan clocks
  and probe order
- [Low] clk: tc9564: duplicate of_clk_hw_onecell_get() and unused
  clock_data slots
--

commit f8b60dd77f8cd8c579ca740b036d26f6d05abd3d
Author: Alex Elder <elder@kernel.org>

clk: toshiba: introduce a TC9564 clock driver

This commit adds a platform clock driver for the Toshiba TC9564 SoC that
registers a PLL, fixed-rate divider clocks, and gate clocks controlled by
two MMIO registers.  It also creates an auxiliary device for the embedded
reset controller.

> diff --git a/drivers/clk/clk-tc9564.c b/drivers/clk/clk-tc9564.c
> new file mode 100644
> index 0000000000000..fb429d2ffe039
> --- /dev/null
> +++ b/drivers/clk/clk-tc9564.c
> @@ -0,0 +1,326 @@

[ ... ]

> +/* Return the clk_hw pointer for a fixed clock having the given rate */
> +static struct clk_hw *fixed_hw_find_by_rate(struct clk_hw **fixed_hws,
> +					    unsigned long rate)
> +{
> +	for (u32 i = 0; i < FIXED_COUNT; i++)
> +		if (clk_hw_get_rate(fixed_hws[i]) == rate)
> +			return fixed_hws[i];
> +
> +	return NULL;
> +}
> +
> +static struct clk_hw *gate_clock_register(struct device *dev,
> +					  struct clk_hw **fixed_hws,
> +					  const struct gate_init *ginit,
> +					  void __iomem *regs)
> +{
> +	const char *name __free(kfree) = clock_name_alloc(dev, ginit->name);
> +	unsigned long flags = ginit->critical ? CLK_IS_CRITICAL : 0;
> +	void __iomem *reg = regs + ginit->offset;
> +	struct clk_hw *parent_hw;
> +
> +	if (!name)
> +		return ERR_PTR(-ENOMEM);
> +
> +	/* Each gate clock's parent is one of the fixed rate clocks */
> +	parent_hw = fixed_hw_find_by_rate(fixed_hws, ginit->parent_rate);
> +	if (!parent_hw) {
> +		dev_err(dev, "failed to find %lu parent\n", ginit->parent_rate);
> +
> +		return ERR_PTR(-EINVAL);
> +	}

[Severity: Medium]
Can fixed_hw_find_by_rate() permanently fail probe when the input clock
provider has not registered yet or runs at a rate other than 25 MHz?

If the input clock provider at index 0 is not yet registered, pll_register()
succeeds with pll_hw orphaned at rate 0 instead of deferring probe.  Each
fixed clock inherits rate 0, so fixed_hw_find_by_rate() returns NULL and
tc9564_clk_probe() fails permanently with -EINVAL.

clk_hw_get_rate() also reads core->parent and core->rate without
prepare_lock, racing with clk_core_reparent_orphans() if the input clock
registers concurrently.  Selecting each gate's parent statically by index
into fixed_init[] avoids both problems and supports any input frequency.

[ ... ]

> +static struct clk_hw *
> +gate_of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
> +{
> +	struct clk_hw **gate_hws = data;
> +	u32 id = clkspec->args[0];
> +
> +	if (id < GATE_COUNT)
> +		return gate_hws[id];
> +
> +	pr_err("%s: invalid clock ID %u\n", __func__, id);
> +
> +	return ERR_PTR(-EINVAL);
> +}

[Severity: Low]
As reported by Jerome Brunet on v4 [1], gate_of_clk_hw_onecell_get()
duplicates of_clk_hw_onecell_get() only because clock_data prepends pll_hw
and fixed_hws slots that are never read after probe.

Keeping fixed_hws in a local array during probe and sizing clock_data to
GATE_COUNT allows passing of_clk_hw_onecell_get and clock_data directly to
devm_of_clk_add_hw_provider().

> +static void reset_adev_free(void *data)
> +{
> +	struct auxiliary_device *adev = data;
> +
> +	ida_free(&reset_ids, adev->id);
> +	kfree(dev_get_platdata(&adev->dev));
> +}
> +
> +/* Create the TC9564 reset auxiliary device; called by probe */
> +static int devm_reset_register(struct device *dev, void __iomem *regs)
> +{
> +	struct tc9564_aux_data *aux_data;
> +	struct auxiliary_device *adev;
> +	int id;
> +
> +	aux_data = kzalloc_obj(*aux_data);
> +	if (!aux_data)
> +		return -ENOMEM;
> +	aux_data->regs = regs;
> +
> +	id = ida_alloc(&reset_ids, GFP_KERNEL);
> +	if (id < 0)
> +		return dev_err_probe(dev, id, "failed to get reset id\n");

[Severity: Medium]
Does this return leak aux_data when ida_alloc() fails?

> +	adev = __devm_auxiliary_device_create(dev, TC9564_CLK_MODNAME,
> +					      TC9564_RESET_DEV_NAME,
> +					      aux_data, id);
> +	if (!adev) {
> +		ida_free(&reset_ids, id);
> +		kfree(aux_data);
> +		return -ENOMEM;
> +	}
> +
> +	return devm_add_action_or_reset(dev, reset_adev_free, adev);
> +}

[Severity: Medium]
Can reset_adev_free() free aux_data while the reset auxiliary device is
still registered and cause a use-after-free in tc9564_reset_probe()?

__devm_auxiliary_device_create() registers auxiliary_device_destroy() as a
devres action before devm_add_action_or_reset() registers reset_adev_free().
Because devres runs actions in LIFO order, unbinding the clock device frees
aux_data and releases adev->id before adev is removed from the bus:

CPU0                                  CPU1
devres_release_all()
  reset_adev_free(adev)
    ida_free(&reset_ids, adev->id)
    kfree(aux_data)
                                      tc9564_reset_probe()
                                        aux_data = dev_get_platdata(dev)
                                        reset->regs = aux_data->regs
                                                      <-- use-after-free
  auxiliary_device_destroy(adev)

Freeing adev->id before adev is deleted also allows a concurrent probe of
another TC9564 instance to reuse the ID and fail with -EEXIST.  Allocating
aux_data with devm_kzalloc_obj() and registering an IDA-release devres
action before __devm_auxiliary_device_create() keeps the teardown order safe.

[1] https://lore.kernel.org/r/179631464066.3767986.13287406761565723851.b4-reply@b4

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203200948.1595470-1-elder@riscstar.com?part=3

  reply	other threads:[~2026-12-03 20:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 20:09 [PATCH v5 0/4] clk: tc9564: introduce clock and reset drivers Alex Elder
2026-12-03 20:09 ` [PATCH v5 1/4] dt-bindings: toshiba: introduce toshiba,tc9564-clock.yaml Alex Elder
2026-12-03 20:18   ` sashiko-bot
2026-12-03 20:09 ` [PATCH v5 2/4] reset: toshiba: introduce a TC9564 reset driver Alex Elder
2026-12-03 20:09 ` [PATCH v5 3/4] clk: toshiba: introduce a TC9564 clock driver Alex Elder
2026-12-03 20:22   ` sashiko-bot [this message]
2026-12-03 20:09 ` [PATCH v5 4/4] arm64: dts: qcom: qcs6490-rb3gen2: add TC9564 clock node Alex Elder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-248624@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=elder@riscstar.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox