Linux clock framework development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Echo Zhang" <echo.zhang@mediatek.com>,
	devicetree@vger.kernel.org, linux-clk@vger.kernel.org
Cc: robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 5/7] soc: mediatek: mmdvfs: add MT8196 support
Date: Wed, 25 Nov 2026 06:15:05 +0000	[thread overview]
Message-ID: <sashiko-outbox-232653@kernel.org> (raw)
In-Reply-To: <20261125060037.649659-6-echo.zhang@mediatek.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] soc: mediatek: mmdvfs: wrong bitmask in mt8196_hwccf_vote()
  prepare poll
- [Medium] soc: mediatek: mmdvfs: spurious update in mt8196_hwccf_vote()
  on zero delta
- [Medium] soc: mediatek: mmdvfs: premature devm_kfree() in
  mt8196_deinit_hw()
- [Medium] soc: mediatek: mmdvfs: 5-second udelay() spin in
  mt8196_hwccf_vote()
- [Low] soc: mediatek: mmdvfs: wrong comment in mt8196_hwccf_vote() and
  unused macros
--

commit ed153f3d0764b28cb3e0456d69d5fe0a30a25f4f
Author: Echo Zhang <echo.zhang@mediatek.com>

soc: mediatek: mmdvfs: add MT8196 support

This commit adds MT8196 MMDVFS platform data, OPP table parsing, and HWCCF
DVFS voter support for multimedia clock muxes.

> diff --git a/drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c b/drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c
> new file mode 100644
> index 0000000000000..1075884791e45
> --- /dev/null
> +++ b/drivers/soc/mediatek/mmdvfs/mmdvfs-mt8196.c
> @@ -0,0 +1,637 @@

[ ... ]

> +/* HWCCF timing constants */
> +#define MTK_WAIT_HWV_PREPARE_CNT	200
> +#define MTK_WAIT_HWV_PREPARE_US		1
> +#define MTK_WAIT_HWV_DONE_CNT		5000000
> +#define MTK_WAIT_HWV_DONE_US		1
> +#define MT8196_HWV_LEVELS		7
> +
> +/* HWCCF DVFS Voter control bits */
> +#define HWCCF_VOTER_ENABLE		BIT(0)
> +#define HWCCF_VOTER_REQ_VALID		BIT(1)
> +#define HWCCF_VOTER_ACK_VALID		BIT(2)

[ ... ]

> +/*
> + * mt8196_hwccf_vote - perform HWCCF DVFS voter operation
> + * @mmdvfs: MMDVFS device structure
> + * @user: user structure
> + * @level: target performance level (higher value = higher performance)
> + *
> + * Implement HWCCF DVFS voter logic using the standard voting sequence:
> + *
> + * 1. WRITE: issue vote to SET/CLEAR register based on OPP delta
> + * 2. PREPARE: poll hwv_set_ofs until HW accepts (200us timeout)
> + * 3. DONE: poll hwv_sta_ofs until operation completes (5s timeout)
> + *
> + * OPP encoding uses cumulative bitmasks where index = level_num - 1 - level.
> + *
> + * Return: 0 on success, negative error code on failure
> + */
> +static int mt8196_hwccf_vote(struct mmdvfs_device *mmdvfs,
> +			     struct mmdvfs_user *user, u8 level)
> +{
> +	struct mt8196_mmdvfs_data *mt8196_data = mmdvfs->data->platform_data;
> +	struct mmdvfs_data *data = mmdvfs->data;
> +	struct mmdvfs_mux *mux;
> +	struct mmdvfs_rc *rc;
> +	u32 mask, opp_mask;
> +	u32 val = 0, orig = 0, renew = 0;
> +	u8 index;
> +	int i = 0, ret = 0;

[ ... ]

> +	index = level;
> +	mask = GENMASK(6, 0) << mux->shift;
> +	opp_mask = GENMASK(index, 0) << mux->shift;

[Severity: Low]
This isn't a bug, but the function header comment above states that
"index = level_num - 1 - level", whereas the implementation uses
"index = level" directly.  Also, HWCCF_VOTER_ENABLE,
HWCCF_VOTER_REQ_VALID, and HWCCF_VOTER_ACK_VALID are unused.

> +
> +	/* Phase 1: Read current setting and calculate delta */
> +	ret = regmap_read(mt8196_data->hwv_regmap, mux->hwccf.hwv_set_ofs, &orig);
> +	if (ret) {
> +		dev_err(mmdvfs->dev, "Failed to read HWV set register: %d\n", ret);
> +		goto hwv_fail;
> +	}
> +
> +	val = orig & mask;
> +	val ^= opp_mask;
> +
> +	/* Write to SET or CLEAR register based on delta direction */
> +	if (val > opp_mask)
> +		ret = regmap_write(mt8196_data->hwv_regmap, mux->hwccf.hwv_clr_ofs, val);
> +	else
> +		ret = regmap_write(mt8196_data->hwv_regmap, mux->hwccf.hwv_set_ofs, val);
> +
> +	if (ret) {
> +		dev_err(mmdvfs->dev, "Failed to write HWV register (ofs=0x%x): %d\n",
> +			val > opp_mask ? mux->hwccf.hwv_clr_ofs : mux->hwccf.hwv_set_ofs, ret);
> +		goto hwv_fail;
> +	}
> +
> +	/* Write UPDATE register to trigger hardware processing. */
> +	if (mux->hwccf.hwv_upd_id != 0xFF) {
> +		ret = regmap_write(mt8196_data->hwv_regmap,
> +				   mux->hwccf.hwv_upd_ofs,
> +				   BIT(mux->hwccf.hwv_upd_id));
> +		if (ret) {
> +			dev_err(mmdvfs->dev, "Failed to write HWV update register (ofs=0x%x bit=%u): %d\n",
> +				mux->hwccf.hwv_upd_ofs, mux->hwccf.hwv_upd_id, ret);
> +			goto hwv_fail;
> +		}
> +	}

[Severity: Medium]
Should mt8196_hwccf_vote() return 0 early when val == 0?

Every mux in mt8196_mux_user_map is shared across multiple users, and
mmdvfs_set_rate() only skips calling hw_vote() when the calling user's own
user->level is unchanged.  When a user updates its vote while another user
on the same mux already holds an equal or higher vote, aggregated_level
matches what is already in hwv_set_ofs, so val is 0.  Without an early
return, this writes 0 to hwv_set_ofs, pulses BIT(mux->hwccf.hwv_upd_id) in
hwv_upd_ofs to trigger a hardware update transaction, and then exits Phase
3 without waiting because (renew & 0) == 0 is always true.

> +
> +	/* Phase 2: Poll until hardware accepts the vote (200us timeout) */
> +	i = 0;
> +	while (1) {
> +		ret = regmap_read(mt8196_data->hwv_regmap, mux->hwccf.hwv_set_ofs, &renew);
> +		if (ret) {
> +			dev_err(mmdvfs->dev, "Failed to read HWV set status: %d\n", ret);
> +			goto hwv_fail;
> +		}
> +
> +		if ((renew & opp_mask) == opp_mask)
> +			break;
> +
> +		if (i < MTK_WAIT_HWV_PREPARE_CNT) {
> +			udelay(MTK_WAIT_HWV_PREPARE_US);
> +			i++;
> +		} else {
> +			dev_err(mmdvfs->dev,
> +				"HWV prepare timeout(%d loops, %dus): renew=0x%x opp_mask=0x%x mask=0x%x\n",
> +				i, i * MTK_WAIT_HWV_PREPARE_US, renew, opp_mask, mask);
> +			dev_err(mmdvfs->dev,
> +				"  mux_id=%u shift=%u hwv_set_ofs=0x%x renew&mask=0x%x\n",
> +				user->mux, mux->shift, mux->hwccf.hwv_set_ofs, renew & mask);
> +			ret = -ETIMEDOUT;
> +			goto hwv_fail;
> +		}
> +	}

[Severity: High]
Should the Phase 2 condition check (renew & mask) == opp_mask instead of
(renew & opp_mask) == opp_mask?

When lowering a mux OPP level, the lower bits in opp_mask were already set
in hwv_set_ofs by the previous higher level, while the upper bits in val
are being cleared via hwv_clr_ofs.  Masking renew with opp_mask ignores
those upper bits, so (renew & opp_mask) == opp_mask succeeds on the first
read before the hardware voter clears the upper bits or latches the
downward vote, allowing Phase 3 to read pre-transition DONE bits in
hwv_sta_ofs and return before the transition completes.

> +
> +	/* Phase 3: Poll until operation completes (5s timeout) */
> +	i = 0;
> +	while (1) {
> +		ret = regmap_read(mt8196_data->hwv_regmap, mux->hwccf.hwv_sta_ofs, &renew);
> +		if (ret) {
> +			dev_err(mmdvfs->dev, "Failed to read HWV status: %d\n", ret);
> +			goto hwv_fail;
> +		}
> +
> +		if ((renew & val) == val)
> +			break;
> +
> +		if (i < MTK_WAIT_HWV_DONE_CNT) {
> +			udelay(MTK_WAIT_HWV_DONE_US);
> +			i++;
> +		} else {
> +			dev_err(mmdvfs->dev,
> +				"HWV done timeout(%d loops, %dms): renew=0x%x val=0x%x\n",
> +				i, i * MTK_WAIT_HWV_DONE_US / 1000, renew, val);
> +			ret = -ETIMEDOUT;
> +			goto hwv_fail;
> +		}
> +	}

[Severity: Medium]
Can this Phase 3 loop use a sleeping poll such as
regmap_read_poll_timeout() or a bounded microsecond timeout instead of
spinning with udelay(1) for up to 5 seconds?

mt8196_hwccf_vote() is called from mmdvfs_set_rate() in sleepable process
context while holding mux->lock and the global clock framework
prepare_lock.  If the hardware voter is slow or times out waiting for
hwv_sta_ofs, 5,000,000 iterations of udelay(1) monopolize the CPU for 5
seconds without yielding and block all clock prepare and rate operations
system-wide.

[ ... ]

> +/*
> + * mt8196_deinit_hw - Deinitialize hardware
> + */
> +static void mt8196_deinit_hw(struct mmdvfs_device *mmdvfs)
> +{
> +	struct mmdvfs_data *data = mmdvfs->data;
> +
> +	if (data->platform_data) {
> +		devm_kfree(mmdvfs->dev, data->platform_data);
> +		data->platform_data = NULL;
> +	}
> +
> +	dev_info(mmdvfs->dev, "MT8196 HWCCF DVFS Voter deinitialized\n");
> +}

[Severity: Medium]
Can calling devm_kfree() in mt8196_deinit_hw() cause a use-after-free in a
concurrent mt8196_hwccf_vote()?

mmdvfs_probe() registers the user clocks via devm_clk_hw_register() after
mt8196_init_hw() allocates data->platform_data with devm_kzalloc(), and
mmdvfs_remove() invokes deinit_hw() before devres unregisters those clock
providers:

CPU0                                  CPU1
mmdvfs_set_rate()
  mutex_lock(&mux->lock)
  mt8196_hw_vote()
    mt8196_hwccf_vote()
      mt8196_data =
        mmdvfs->data->platform_data
                                      mmdvfs_remove()
                                        mt8196_deinit_hw()
                                          devm_kfree(...,
                                            data->platform_data)
      regmap_read(
        mt8196_data->hwv_regmap, ...)
        <-- use-after-free

Dropping the manual devm_kfree() lets devres free data->platform_data on
device release after the devm-managed clocks have been unregistered.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125060037.649659-1-echo.zhang@mediatek.com?part=5

  reply	other threads:[~2026-11-25  6:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  6:00 [PATCH 0/7] soc: mediatek: add MMDVFS support for MT8196 Echo Zhang
2026-11-25  6:00 ` [PATCH 1/7] dt-bindings: clock: mediatek: add MT8196 MMDVFS clock IDs Echo Zhang
2026-11-25  6:13   ` sashiko-bot
2026-11-25  6:00 ` [PATCH 2/7] dt-bindings: soc: mediatek: add MMDVFS binding Echo Zhang
2026-11-25  6:12   ` sashiko-bot
2026-11-25  6:00 ` [PATCH 3/7] soc: mediatek: add MMDVFS core driver Echo Zhang
2026-11-25  6:18   ` sashiko-bot
2026-11-25  6:00 ` [PATCH 4/7] soc: mediatek: mmdvfs: add VCP backend Echo Zhang
2026-11-25  6:21   ` sashiko-bot
2026-11-25  6:00 ` [PATCH 5/7] soc: mediatek: mmdvfs: add MT8196 support Echo Zhang
2026-11-25  6:15   ` sashiko-bot [this message]
2026-11-25  6:00 ` [PATCH 6/7] soc: mediatek: mmdvfs: add read-only status debug Echo Zhang
2026-11-25  6:00 ` [PATCH 7/7] MAINTAINERS: add MediaTek MMDVFS entry Echo Zhang

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-232653@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=echo.zhang@mediatek.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