Linux-PWM Archive mirror
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: linux-pwm@vger.kernel.org
Cc: kernel@pengutronix.de
Subject: [PATCH 1/8] pwm: Ensure that pwm_chips are allocated using pwmchip_alloc()
Date: Sun, 17 Mar 2024 11:40:32 +0100	[thread overview]
Message-ID: <b2bf15914e7e064ed48832dbd9ba909584613a9b.1710670958.git.u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <cover.1710670958.git.u.kleine-koenig@pengutronix.de>

Memory holding a struct device must not be freed before the reference
count drops to zero. So a struct pwm_chip must not live in memory
freed by a driver on unbind. All in-tree drivers were fixed accordingly,
but as out-of-tree drivers, that were not adapted, still compile fine,
catch these in pwmchip_add().

Link: https://lore.kernel.org/r/35f5b229c98f78b2f6ce2397259a4a936be477c0.1707900770.git.u.kleine-koenig@pengutronix.de
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/pwm/core.c  | 10 ++++++++++
 include/linux/pwm.h |  2 ++
 2 files changed, 12 insertions(+)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index d70f793ce4b3..fe83333c466a 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -481,6 +481,7 @@ struct pwm_chip *pwmchip_alloc(struct device *parent, unsigned int npwm, size_t
 
 	chip->dev = parent;
 	chip->npwm = npwm;
+	chip->uses_pwmchip_alloc = true;
 
 	pwmchip_set_drvdata(chip, pwmchip_priv(chip));
 
@@ -561,6 +562,15 @@ int __pwmchip_add(struct pwm_chip *chip, struct module *owner)
 	if (!chip || !pwmchip_parent(chip) || !chip->ops || !chip->npwm)
 		return -EINVAL;
 
+	/*
+	 * a struct pwm_chip must be allocated using (devm_)pwmchip_alloc,
+	 * otherwise the embedded struct device might disappear too early
+	 * resulting in memory corruption.
+	 * Catch drivers that were not converted appropriately.
+	 */
+	if (!chip->uses_pwmchip_alloc)
+		return -EINVAL;
+
 	if (!pwm_ops_check(chip))
 		return -EINVAL;
 
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index 4a6568dfdf3f..94a642a88817 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -272,6 +272,7 @@ struct pwm_ops {
  * @npwm: number of PWMs controlled by this chip
  * @of_xlate: request a PWM device given a device tree PWM specifier
  * @atomic: can the driver's ->apply() be called in atomic context
+ * @uses_pwmchip_alloc: signals if pwmchip_allow was used to allocate this chip
  * @driver_data: Private pointer for driver specific info
  * @pwms: array of PWM devices allocated by the framework
  */
@@ -287,6 +288,7 @@ struct pwm_chip {
 	bool atomic;
 
 	/* only used internally by the PWM framework */
+	bool uses_pwmchip_alloc;
 	void *driver_data;
 	struct pwm_device *pwms;
 };
-- 
2.43.0


  reply	other threads:[~2024-03-17 10:40 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-17 10:40 [PATCH 0/8] pwm: Add support for character devices Uwe Kleine-König
2024-03-17 10:40 ` Uwe Kleine-König [this message]
2024-03-17 10:40 ` [PATCH 2/8] pwm: Give some sysfs related variables and functions better names Uwe Kleine-König
2024-03-17 10:40 ` [PATCH 3/8] pwm: Move contents of sysfs.c into core.c Uwe Kleine-König
2024-03-17 10:40 ` [PATCH 4/8] pwm: Ensure a struct pwm has the same lifetime as its pwm_chip Uwe Kleine-König
2024-03-17 10:40 ` [PATCH 5/8] pwm: Add a struct device to struct pwm_chip Uwe Kleine-König
2024-03-28 18:44   ` David Lechner
2024-03-29 10:19     ` Uwe Kleine-König
2024-03-17 10:40 ` [PATCH 6/8] pwm: Make pwmchip_[sg]et_drvdata() a wrapper around dev_set_drvdata() Uwe Kleine-König
2024-03-17 10:40 ` [PATCH 7/8] pwm: Add more locking Uwe Kleine-König
2024-03-18  7:28   ` Thorsten Scherer
     [not found]   ` <CGME20240404120932eucas1p1b3c1e07bf6f41f6330725148b0268b13@eucas1p1.samsung.com>
2024-04-04 12:09     ` Marek Szyprowski
2024-04-04 15:33       ` Uwe Kleine-König
2024-03-17 10:40 ` [PATCH 8/8] pwm: Add support for pwmchip devices for faster and easier userspace access Uwe Kleine-König
2024-04-08 15:42   ` John Ernberg
2024-04-09  7:49     ` Uwe Kleine-König
2024-04-15 11:27       ` John Ernberg
2024-05-07  1:11   ` Kent Gibson
2024-05-07  6:12     ` Uwe Kleine-König
2024-04-13  9:22 ` [PATCH 0/8] pwm: Add support for character devices Uwe Kleine-König
2024-04-22 18:59   ` David Lechner

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=b2bf15914e7e064ed48832dbd9ba909584613a9b.1710670958.git.u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=kernel@pengutronix.de \
    --cc=linux-pwm@vger.kernel.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).