All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] pwm: add might_sleep annotations
@ 2021-09-09  9:48 Uwe Kleine-König
  2021-09-09  9:48 ` [PATCH 1/2] pwm: Add might_sleep() annotations for !CONFIG_PWM API functions Uwe Kleine-König
  2021-09-09  9:48 ` [PATCH 2/2] pwm: Make it explicit that pwm_apply_state() might sleep Uwe Kleine-König
  0 siblings, 2 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2021-09-09  9:48 UTC (permalink / raw)
  To: Thierry Reding, Lee Jones; +Cc: linux-pwm, kernel, Mårten Lindahl

Hello,

earlier today I noticed that the samsung PWM driver uses
spin_lock_irqsave() which made me wonder if the PWM functions are used
in atomic context at all. To find out I suggest adding a might_sleep()
and look if anyone complains.

The first patch should be indisputable, if you know a valid atomic use
case for pwm_apply_state, please speak up (which would make the second
patch wrong).

Best regards
Uwe

Uwe Kleine-König (2):
  pwm: Add might_sleep() annotations for !CONFIG_PWM API functions
  pwm: Make it explicit that pwm_apply_state() might sleep

 drivers/pwm/core.c  |  9 +++++++++
 include/linux/pwm.h | 13 +++++++++++++
 2 files changed, 22 insertions(+)


base-commit: 3f2b16734914fa7c53ef7f8a10a63828890dbd37
-- 
2.30.2


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] pwm: Add might_sleep() annotations for !CONFIG_PWM API functions
  2021-09-09  9:48 [PATCH 0/2] pwm: add might_sleep annotations Uwe Kleine-König
@ 2021-09-09  9:48 ` Uwe Kleine-König
  2021-09-09  9:48 ` [PATCH 2/2] pwm: Make it explicit that pwm_apply_state() might sleep Uwe Kleine-König
  1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2021-09-09  9:48 UTC (permalink / raw)
  To: Thierry Reding, Lee Jones; +Cc: linux-pwm, kernel, Mårten Lindahl

The normal implementations of these functions make use of mutexes. To
make it obvious that these functions might sleep also add annotations to
the dummy implementations in the !CONFIG_PWM case.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 include/linux/pwm.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index 725c9b784e60..515e33978e97 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -429,11 +429,13 @@ struct pwm_device *devm_fwnode_pwm_get(struct device *dev,
 #else
 static inline struct pwm_device *pwm_request(int pwm_id, const char *label)
 {
+	might_sleep();
 	return ERR_PTR(-ENODEV);
 }
 
 static inline void pwm_free(struct pwm_device *pwm)
 {
+	might_sleep();
 }
 
 static inline int pwm_apply_state(struct pwm_device *pwm,
@@ -493,12 +495,14 @@ static inline struct pwm_device *pwm_request_from_chip(struct pwm_chip *chip,
 						       unsigned int index,
 						       const char *label)
 {
+	might_sleep();
 	return ERR_PTR(-ENODEV);
 }
 
 static inline struct pwm_device *pwm_get(struct device *dev,
 					 const char *consumer)
 {
+	might_sleep();
 	return ERR_PTR(-ENODEV);
 }
 
@@ -506,16 +510,19 @@ static inline struct pwm_device *of_pwm_get(struct device *dev,
 					    struct device_node *np,
 					    const char *con_id)
 {
+	might_sleep();
 	return ERR_PTR(-ENODEV);
 }
 
 static inline void pwm_put(struct pwm_device *pwm)
 {
+	might_sleep();
 }
 
 static inline struct pwm_device *devm_pwm_get(struct device *dev,
 					      const char *consumer)
 {
+	might_sleep();
 	return ERR_PTR(-ENODEV);
 }
 
@@ -523,6 +530,7 @@ static inline struct pwm_device *devm_of_pwm_get(struct device *dev,
 						 struct device_node *np,
 						 const char *con_id)
 {
+	might_sleep();
 	return ERR_PTR(-ENODEV);
 }
 
@@ -530,6 +538,7 @@ static inline struct pwm_device *
 devm_fwnode_pwm_get(struct device *dev, struct fwnode_handle *fwnode,
 		    const char *con_id)
 {
+	might_sleep();
 	return ERR_PTR(-ENODEV);
 }
 #endif
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] pwm: Make it explicit that pwm_apply_state() might sleep
  2021-09-09  9:48 [PATCH 0/2] pwm: add might_sleep annotations Uwe Kleine-König
  2021-09-09  9:48 ` [PATCH 1/2] pwm: Add might_sleep() annotations for !CONFIG_PWM API functions Uwe Kleine-König
@ 2021-09-09  9:48 ` Uwe Kleine-König
  1 sibling, 0 replies; 3+ messages in thread
From: Uwe Kleine-König @ 2021-09-09  9:48 UTC (permalink / raw)
  To: Thierry Reding, Lee Jones; +Cc: linux-pwm, kernel, Mårten Lindahl

At least some implementations sleep. So mark pwm_apply_state() with a
might_sleep() to make callers aware. In the worst case this uncovers a
valid atomic user, then we revert this patch and at least gained some more
knowledge and then can work on a concept similar to
gpio_get_value/gpio_get_value_cansleep.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/pwm/core.c  | 9 +++++++++
 include/linux/pwm.h | 4 ++++
 2 files changed, 13 insertions(+)

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 4527f09a5c50..b7cda8a3eb85 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -532,6 +532,15 @@ int pwm_apply_state(struct pwm_device *pwm, const struct pwm_state *state)
 	struct pwm_chip *chip;
 	int err;
 
+	/*
+	 * Some lowlevel driver's implementations of .apply() make use of
+	 * mutexes, also with some drivers only returning when the new
+	 * configuration is active calling pwm_apply_state() from atomic context
+	 * is a bad idea. So make it explict that calling this function might
+	 * sleep.
+	 */
+	might_sleep();
+
 	if (!pwm || !state || !state->period ||
 	    state->duty_cycle > state->period)
 		return -EINVAL;
diff --git a/include/linux/pwm.h b/include/linux/pwm.h
index 515e33978e97..e6dac95e4960 100644
--- a/include/linux/pwm.h
+++ b/include/linux/pwm.h
@@ -441,6 +441,7 @@ static inline void pwm_free(struct pwm_device *pwm)
 static inline int pwm_apply_state(struct pwm_device *pwm,
 				  const struct pwm_state *state)
 {
+	might_sleep();
 	return -ENOTSUPP;
 }
 
@@ -452,6 +453,7 @@ static inline int pwm_adjust_config(struct pwm_device *pwm)
 static inline int pwm_config(struct pwm_device *pwm, int duty_ns,
 			     int period_ns)
 {
+	might_sleep();
 	return -EINVAL;
 }
 
@@ -464,11 +466,13 @@ static inline int pwm_capture(struct pwm_device *pwm,
 
 static inline int pwm_enable(struct pwm_device *pwm)
 {
+	might_sleep();
 	return -EINVAL;
 }
 
 static inline void pwm_disable(struct pwm_device *pwm)
 {
+	might_sleep();
 }
 
 static inline int pwm_set_chip_data(struct pwm_device *pwm, void *data)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-09  9:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-09  9:48 [PATCH 0/2] pwm: add might_sleep annotations Uwe Kleine-König
2021-09-09  9:48 ` [PATCH 1/2] pwm: Add might_sleep() annotations for !CONFIG_PWM API functions Uwe Kleine-König
2021-09-09  9:48 ` [PATCH 2/2] pwm: Make it explicit that pwm_apply_state() might sleep Uwe Kleine-König

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.