Skip to contentSkip to conversation

pwm: Don't dereference the chip after put_device() in __pwm_put()

PATCH In review Suyuan Wang 2 participants 3 messages Nov 19 PWM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: Don't dereference the chip after put_device() in __pwm_put()

From: Suyuan Wang <[email protected]>

__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")

---
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(-)
drivers/pwm/core.c +2 -1
index 4c6d716e5fde..8988a7dbe5b1 100644
@@ -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);
 }
 
 /**
#1 lore raw
Quoted 45 lines · On Thu, Nov 19, 2026 at 10:56:17AM +0000, Suyuan Wang via B4 Relay wrote:

From: Suyuan Wang <[email protected]>

__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")

---
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(-)
drivers/pwm/core.c +1 -0
index 4c6d716e5fde..8988a7dbe5b1 100644
@@ -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.

drivers/pwm/core.c static void __pwm_put(struct pwm_device *pwm) · line 2043

Quoted 6 lines
drivers/pwm/core.c +1 -1
@@ -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

author
#2 lore raw

Hi Uwe,

Quoted 3 lines · Uwe Kleine-König

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()?

put_device() is an out-of-line call that may free *chip, so the compiler has to do the load of chip->owner before the call. But your way is simpler and doesn't need that argument: pwmchip_release() lives in the PWM core, so dropping the module reference first is fine. I'll send a v2 that does that.

Thanks,
Suyuan

Quoted 60 lines · Uwe Kleine-König <[email protected]> 于2026年11月19日周四 22:27写道:
Quoted 45 lines · On Thu, Nov 19, 2026 at 10:56:17AM +0000, Suyuan Wang via B4 Relay wrote:

From: Suyuan Wang <[email protected]>

__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")

---
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(-)
drivers/pwm/core.c +1 -0
index 4c6d716e5fde..8988a7dbe5b1 100644
@@ -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.

drivers/pwm/core.c static void __pwm_put(struct pwm_device *pwm) · line 2043

Quoted 6 lines
drivers/pwm/core.c +1 -1
@@ -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