mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] pwm: Drop the module reference before the chip in __pwm_put()
@ 2026-11-19 19:28 Suyuan Wang via B4 Relay
  0 siblings, 0 replies; only message in thread
From: Suyuan Wang via B4 Relay @ 2026-11-19 19:28 UTC (permalink / raw)
  To: ukleinek, m.wilczynski; +Cc: linux-pwm, linux-kernel

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.

Call module_put() before put_device(). This is safe because the chip is
released by pwmchip_release() in the PWM core, not by code in the
provider module.

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>
---
Changes in v2:
- Call module_put() before put_device() instead of reading chip->owner
  into a local first (Uwe).

Compile tested on x86_64 (W=1 and sparse, no new warnings). I don't have
the hardware.

v1: https://lore.kernel.org/linux-pwm/20261119-pwm-v1-1-adeff0cc366a@gmail.com/
---
 drivers/pwm/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 4c6d716e5fde..4e8dba089261 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -2036,9 +2036,9 @@ static void __pwm_put(struct pwm_device *pwm)
 
 	pwm->label = NULL;
 
-	put_device(&chip->dev);
-
 	module_put(chip->owner);
+
+	put_device(&chip->dev);
 }
 
 /**





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-19 19:28 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 19:28 [PATCH v2] pwm: Drop the module reference before the chip in __pwm_put() Suyuan Wang via B4 Relay

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®