mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <ukleinek@kernel.org>
To: shyckless@gmail.com
Cc: m.wilczynski@samsung.com, linux-kernel@vger.kernel.org,
	 linux-pwm@vger.kernel.org
Subject: Re: [PATCH] pwm: Don't dereference the chip after put_device() in __pwm_put()
Date: Thu, 19 Nov 2026 15:27:26 +0100	[thread overview]
Message-ID: <av8GQXBbkf8_sHhX@monoceros> (raw)
In-Reply-To: <20261119-pwm-v1-1-adeff0cc366a@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2169 bytes --]

On Thu, Nov 19, 2026 at 10:56:17AM +0000, Suyuan Wang via B4 Relay wrote:
> From: Suyuan Wang <stevewong@hust.edu.cn>
> 
> __pwm_put() drops the chip reference the consumer took and then reads the
> chip to drop the module reference:
> 
> 	put_device(&chip->dev);
> 	module_put(chip->owner);
> 
> A consumer takes both references in pwm_device_request()
> (try_module_get(chip->owner) and get_device(&chip->dev)). The chip's base
> reference is released by pwmchip_put(), not by pwmchip_remove(), so a
> consumer can still hold a pwm after the provider has removed and put the
> chip. When that consumer finally calls pwm_put(), the put_device() here
> drops the last reference, pwmchip_release() frees the chip, and
> module_put(chip->owner) then reads the freed chip.
> 
> Read chip->owner into a local before put_device() and use that.
> 
> Found with a static analysis tool and checked by hand.
> 
> Fixes: 4c56b1434b81 ("pwm: Add a struct device to struct pwm_chip")
> Cc: stable@vger.kernel.org
> Co-developed-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
> Signed-off-by: Kaixuan Li <kaixuan.li@ntu.edu.sg>
> Signed-off-by: Suyuan Wang <stevewong@hust.edu.cn>
> ---
> Compile tested on x86_64 (W=1 and sparse, no new warnings). I don't have
> the hardware.
> ---
>  drivers/pwm/core.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
> index 4c6d716e5fde..8988a7dbe5b1 100644
> --- a/drivers/pwm/core.c
> +++ b/drivers/pwm/core.c
> @@ -2020,6 +2020,7 @@ EXPORT_SYMBOL_GPL(pwm_get);
>  static void __pwm_put(struct pwm_device *pwm)
>  {
>  	struct pwm_chip *chip = pwm->chip;
> +	struct module *owner = chip->owner;
>  
>  	/*
>  	 * Trigger a warning if a consumer called pwm_put() twice.
> @@ -2038,7 +2039,7 @@ static void __pwm_put(struct pwm_device *pwm)
>  
>  	put_device(&chip->dev);
>  
> -	module_put(chip->owner);
> +	module_put(owner);
>  }

I wonder what ensures that the compiler doesn't reorder put_device() and
the assignment to owner. Maybe it would be more sensible to call
module_put() before put_device()?

Best regards
Uwe

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

      reply	other threads:[~2026-11-19 14:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 10:56 Suyuan Wang via B4 Relay
2026-11-19 14:27 ` Uwe Kleine-König [this message]

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=av8GQXBbkf8_sHhX@monoceros \
    --to=ukleinek@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=m.wilczynski@samsung.com \
    --cc=shyckless@gmail.com \
    /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®