mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: github@szelinsky.de
Cc: o.rempel@pengutronix.de, kory.maincent@bootlin.com,
	andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	broonie@kernel.org, lgirdwood@gmail.com,
	maxime.chevallier@bootlin.com, kyle.swenson@est.tech,
	ross@r-sc.ca, netdev-bot+sashiko@kernel.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister
Date: Fri, 20 Nov 2026 06:41:53 +0000	[thread overview]
Message-ID: <179515691327.3136.4761180533573430766@kernel.org> (raw)
In-Reply-To: <20261118183957.2031311-9-github@szelinsky.de>

Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] pse_controller_unregister() sets pcdev->unregistering = true and
  nothing ever clears it.

Pre-existing issues:
- [High] At this commit, pse_controller_unregister() still calls
  pse_release_pis(), which does kfree(pcdev->pi), at line 1188.
- [Medium] At this commit, pse_controller_register() returns directly
  after kfifo_alloc() on several failures and leaks ntf_fifo: missing…

--

net: pse-pd: stop async event sources before freeing PI data in unregister

This patch reorders pse_controller_unregister() so that the IRQ is
disabled and the notification worker is drained before the power domains
are flushed and the PI array is freed. It also adds a
pcdev->unregistering flag, set under pcdev->lock. After it is set,
pse_pi_enable() and _pse_pi_delivery_power_sw_pw_ctrl() return -ENODEV,
so the worker can't be queued again after the drain.

> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 8041ad3e330b9..22f2d8041bd4b 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c

[ ... ]

> @@ -1158,11 +1170,22 @@ EXPORT_SYMBOL_GPL(pse_controller_register);
>   */
>  void pse_controller_unregister(struct pse_controller_dev *pcdev)
>  {
> -	pse_flush_pw_ds(pcdev);
> -	pse_release_pis(pcdev);
> +	/* From now on the PSE core must not power a PI, so nothing queues
> +	 * a new notification once the worker is drained below.
> +	 */
> +	mutex_lock(&pcdev->lock);
> +	pcdev->unregistering = true;
> +	mutex_unlock(&pcdev->lock);

[Severity: Low]
This is the only write to pcdev->unregistering, and nothing clears it
again. pse_controller_register() doesn't reset it either. It only
initializes these:

pse_controller_register() {
	mutex_init(&pcdev->lock);
	INIT_LIST_HEAD(&pcdev->pse_control_head);
	spin_lock_init(&pcdev->ntf_fifo_lock);
	...
}

If the same pse_controller_dev were registered again, wouldn't
pse_pi_enable() and _pse_pi_delivery_power_sw_pw_ctrl() always return
-ENODEV? pse_set_config_isr() would then never power a PI.

No in-tree caller does this. pd692x0, pse_regulator, realtek-pse-mcu,
si3474 and tps23881 all call devm_pse_controller_register() on a struct
allocated with devm_kzalloc. So each bind gets a zeroed pcdev. Other
fields are not reset on register either, for example no_of_pse_pi and
irq.

Could the flag be cleared in pse_controller_register()? Or could the
kernel-doc say that the core expects a zeroed pcdev for each
registration?

> +
> +	/* Stop the IRQ and the notification worker first: pse_isr() uses
> +	 * pcdev->pi and the power domains that pse_flush_pw_ds() drops,
> +	 * and the worker reads the kfifo that is freed below.
> +	 */
>  	if (pcdev->irq)
>  		disable_irq(pcdev->irq);
>  	cancel_work_sync(&pcdev->ntf_work);
> +	pse_flush_pw_ds(pcdev);
> +	pse_release_pis(pcdev);

[Severity: High]
This is a pre-existing issue and was not introduced by this patch.
pse_release_pis() still does kfree(pcdev->pi) while other code can
still reach the array.

The PI regulators were registered with devm_pse_pi_regulator_register().
Devres removes them only after this function returns. pcdev also stays
on pse_controller_list until the list_del() further down.

In that window, can these read or write the freed pcdev->pi[]?

  pse_pi_is_enabled()
  pse_pi_disable()->_pse_pi_disable()
  __pse_control_release()
  of_pse_control_get()->of_pse_match_pi()

For example:

of_pse_control_get() {
	...
	list_for_each_entry(r, &pse_controller_list, list) {
		if (!r->no_of_pse_pi) {
			ret = of_pse_match_pi(r, args.np);
	...
}

__pse_control_release() {
	...
	if (psec->pcdev->pi[psec->id].admin_state_enabled)
		regulator_disable(psec->ps);
	...
}

pse_release_pis() also doesn't take pcdev->lock, and it leaves
pcdev->pi pointing at freed memory.

This patch only moves the free after the IRQ and worker drain. It
doesn't make the window wider.

Later patches in this series fix it:

  "net: pse-pd: unregister from the controller list before freeing PI
  data" moves list_del() ahead of the drain and the frees.

  "net: pse-pd: keep the PI data until the PI regulators are gone"
  removes pse_release_pis() from unregister. The array is then freed
  from a devm_pse_release_pis devres action, which runs after the PI
  regulators are gone.

That later commit also says the series does not fix a consumer that
still holds a pse_control after the controller unbinds.

>  	kfifo_free(&pcdev->ntf_fifo);

[Severity: Medium]
This is also a pre-existing issue and was not introduced by this patch.
Is ntf_fifo leaked when pse_controller_register() fails after
kfifo_alloc()? Several error paths there return directly without
calling kfifo_free():

pse_controller_register() {
	...
	if (!pcdev->ops->pi_get_admin_state ||
	    !pcdev->ops->pi_get_pw_status) {
		dev_err(pcdev->dev,
			"Mandatory status report callbacks are missing");
		return -EINVAL;
	}
	...
	ret = pse_register_pw_ds(pcdev);
	if (ret)
		return ret;
	...
}

The failures of of_load_pse_pis(), setup_pi_matrix(), devm_kzalloc()
and devm_pse_pi_regulator_register() return directly too. The later
failures leak pcdev->pi as well. These paths can be hit repeatedly
during -EPROBE_DEFER retries.

Later patches in this series fix it:

  "net: pse-pd: free the notification kfifo when registration fails"
  sends every error path to a free_kfifo label, which calls
  cancel_work_sync() and kfifo_free().

  "net: pse-pd: keep the PI data until the PI regulators are gone"
  registers devm_pse_release_pis right after of_load_pse_pis()
  succeeds. of_load_pse_pis() already frees the PI array on its own
  errors.

>  	mutex_lock(&pse_list_mutex);
>  	list_del(&pcdev->list);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118183957.2031311-1-github%40szelinsky.de

  reply	other threads:[~2026-11-20  6:41 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 18:39 [PATCH net v5 00/14] net: pse-pd: assorted lifetime and error-path fixes Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 01/14] net: pse-pd: size the notification kfifo after defaulting nr_lines Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 02/14] net: pse-pd: remove dead pi_get_admin_state() checks Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 03/14] net: pse-pd: fix module refcount leak in pse_control_get_internal() Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 04/14] net: pse-pd: do not keep a failed IRQ in pcdev->irq Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 05/14] net: pse-pd: do not reach a freed PHY from the notification worker Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 06/14] net: pse-pd: check the PI state under the lock when setting a power limit Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 07/14] net: pse-pd: do not tie a shared power domain to its creator's devres Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko
2026-11-18 18:39 ` [PATCH net v5 08/14] net: pse-pd: stop async event sources before freeing PI data in unregister Carlo Szelinsky
2026-11-20  6:41   ` netdev-bot+sashiko [this message]
2026-11-18 18:39 ` [PATCH net v5 09/14] net: pse-pd: unregister from the controller list before freeing PI data Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 10/14] net: pse-pd: keep the PI data until the PI regulators are gone Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 11/14] net: pse-pd: free the notification kfifo when registration fails Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 12/14] net: pse-pd: use each PI's own supply handle for the power budget Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 13/14] net: pse-pd: pd692x0: release the manager power budget through devres Carlo Szelinsky
2026-11-18 18:39 ` [PATCH net v5 14/14] net: pse-pd: pd692x0: defer probe while a manager supply is unresolved Carlo Szelinsky

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=179515691327.3136.4761180533573430766@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=andrew+netdev@lunn.ch \
    --cc=broonie@kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=github@szelinsky.de \
    --cc=horms@kernel.org \
    --cc=kory.maincent@bootlin.com \
    --cc=kuba@kernel.org \
    --cc=kyle.swenson@est.tech \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maxime.chevallier@bootlin.com \
    --cc=netdev@vger.kernel.org \
    --cc=o.rempel@pengutronix.de \
    --cc=pabeni@redhat.com \
    --cc=ross@r-sc.ca \
    /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

all inboxes | Powered by JetHome®