All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/5] Add devm helper for work-queue initialization
@ 2021-05-18 12:31 Matti Vaittinen
  2021-05-18 12:31 ` [PATCH v2 1/5] devm-helpers: Add resource managed version of work init Matti Vaittinen
                   ` (6 more replies)
  0 siblings, 7 replies; 11+ messages in thread
From: Matti Vaittinen @ 2021-05-18 12:31 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Hans de Goede, Matti Vaittinen, Marek Szyprowski,
	linux-kernel

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

This series adds new devm_work_autocancel() helper.

Many drivers which use work-queues must ensure the work is not queued when
driver is detached. Often this is done by ensuring new work is not added and
then calling cancel_work_sync() at remove(). In many cases this also requires
cleanup at probe error path - which is easy to forget (or get wrong).

Also the "by ensuring new work is not added" has a gotcha.

It is not strange to see devm managed IRQs scheduling work.
Mixing this with manual wq clean-up is hard to do correctly because the
devm is likely to free the IRQ only after the remove() is ran. So manual
wq cancellation and devm-based IRQ management do not mix well - there is
a short(?) time-window after the wq clean-up when IRQs are still not
freed and may schedule new work.

When both WQs and IRQs are managed by devm things are likely to just
work. WQs should be initialized before IRQs (when IRQs need to schedule
work) and devm unwinds things in "FILO" order.

This series implements wq cancellation on top of devm and replaces
the obvious cases where only thing remove call-back in a driver does is
cancelling the work. There might be other cases where we could switch
more than just work cancellation to use managed version and thus get rid
of remove or mixed (manual and devm) resource management.

Changelog v2:
  - rebased on v5.13-rc2
  - split the extcon-max8997 change into two. First a simple,
    back-portable fix for omitting IRQ freeing at error path, second
    being the devm-simpification which does not need backporting.

---

Matti Vaittinen (5):
  devm-helpers: Add resource managed version of work init
  extcon: extcon-max14577: Fix potential work-queue cancellation race
  extcon: extcon-max77693.c: Fix potential work-queue cancellation race
  extcon: extcon-max8997: Fix IRQ freeing at error path
  extcon: extcon-max8997: Simplify driver using devm

 drivers/extcon/extcon-max14577.c | 16 ++++--------
 drivers/extcon/extcon-max77693.c | 17 ++++--------
 drivers/extcon/extcon-max8997.c  | 45 +++++++++++---------------------
 include/linux/devm-helpers.h     | 25 ++++++++++++++++++
 4 files changed, 50 insertions(+), 53 deletions(-)


base-commit: d07f6ca923ea0927a1024dfccafc5b53b61cfecc
-- 
2.25.4


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

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

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

* [PATCH v2 1/5] devm-helpers: Add resource managed version of work init
  2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
@ 2021-05-18 12:31 ` Matti Vaittinen
  2021-05-18 12:32 ` [PATCH v2 2/5] extcon: extcon-max14577: Fix potential work-queue cancellation race Matti Vaittinen
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Matti Vaittinen @ 2021-05-18 12:31 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Hans de Goede, Matti Vaittinen, Marek Szyprowski,
	linux-kernel

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

A few drivers which need a work-queue must cancel work at driver detach.
Some of those implement remove() solely for this purpose. Help drivers to
avoid unnecessary remove and error-branch implementation by adding managed
verision of work initialization. This will also help drivers to avoid
mixing manual and devm based unwinding when other resources are handled by
devm.

Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
---
 include/linux/devm-helpers.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/include/linux/devm-helpers.h b/include/linux/devm-helpers.h
index f40f77717a24..74891802200d 100644
--- a/include/linux/devm-helpers.h
+++ b/include/linux/devm-helpers.h
@@ -51,4 +51,29 @@ static inline int devm_delayed_work_autocancel(struct device *dev,
 	return devm_add_action(dev, devm_delayed_work_drop, w);
 }
 
+static inline void devm_work_drop(void *res)
+{
+	cancel_work_sync(res);
+}
+
+/**
+ * devm_work_autocancel - Resource-managed work allocation
+ * @dev:	Device which lifetime work is bound to
+ * @w:		Work to be added (and automatically cancelled)
+ * @worker:	Worker function
+ *
+ * Initialize work which is automatically cancelled when driver is detached.
+ * A few drivers need to queue work which must be cancelled before driver
+ * is detached to avoid accessing removed resources.
+ * devm_work_autocancel() can be used to omit the explicit
+ * cancelleation when driver is detached.
+ */
+static inline int devm_work_autocancel(struct device *dev,
+				       struct work_struct *w,
+				       work_func_t worker)
+{
+	INIT_WORK(w, worker);
+	return devm_add_action(dev, devm_work_drop, w);
+}
+
 #endif
-- 
2.25.4


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

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

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

* [PATCH v2 2/5] extcon: extcon-max14577: Fix potential work-queue cancellation race
  2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
  2021-05-18 12:31 ` [PATCH v2 1/5] devm-helpers: Add resource managed version of work init Matti Vaittinen
@ 2021-05-18 12:32 ` Matti Vaittinen
  2021-05-18 12:32 ` [PATCH v2 3/5] extcon: extcon-max77693.c: " Matti Vaittinen
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Matti Vaittinen @ 2021-05-18 12:32 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Hans de Goede, Matti Vaittinen, Marek Szyprowski,
	linux-kernel

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

The extcon IRQ schedules a work item. IRQ is requested using devm while
WQ is cancelld at remove(). This mixing of devm and manual unwinding has
potential case where the WQ has been emptied (.remove() was ran) but
devm unwinding of IRQ was not yet done. It is possible the IRQ is triggered
at this point scheduling new work item to the already flushed queue.

Use new devm_work_autocancel() to remove the remove() and to kill the bug.

Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
---

Please note that the change is compile-tested only. All proper testing is
highly appreciated.
---
 drivers/extcon/extcon-max14577.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/drivers/extcon/extcon-max14577.c b/drivers/extcon/extcon-max14577.c
index ace523924e58..5476f48ed74b 100644
--- a/drivers/extcon/extcon-max14577.c
+++ b/drivers/extcon/extcon-max14577.c
@@ -6,6 +6,7 @@
 // Chanwoo Choi <cw00.choi@samsung.com>
 // Krzysztof Kozlowski <krzk@kernel.org>
 
+#include <linux/devm-helpers.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/i2c.h>
@@ -673,7 +674,10 @@ static int max14577_muic_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, info);
 	mutex_init(&info->mutex);
 
-	INIT_WORK(&info->irq_work, max14577_muic_irq_work);
+	ret = devm_work_autocancel(&pdev->dev, &info->irq_work,
+				   max14577_muic_irq_work);
+	if (ret)
+		return ret;
 
 	switch (max14577->dev_type) {
 	case MAXIM_DEVICE_TYPE_MAX77836:
@@ -766,15 +770,6 @@ static int max14577_muic_probe(struct platform_device *pdev)
 	return ret;
 }
 
-static int max14577_muic_remove(struct platform_device *pdev)
-{
-	struct max14577_muic_info *info = platform_get_drvdata(pdev);
-
-	cancel_work_sync(&info->irq_work);
-
-	return 0;
-}
-
 static const struct platform_device_id max14577_muic_id[] = {
 	{ "max14577-muic", MAXIM_DEVICE_TYPE_MAX14577, },
 	{ "max77836-muic", MAXIM_DEVICE_TYPE_MAX77836, },
@@ -797,7 +792,6 @@ static struct platform_driver max14577_muic_driver = {
 		.of_match_table = of_max14577_muic_dt_match,
 	},
 	.probe		= max14577_muic_probe,
-	.remove		= max14577_muic_remove,
 	.id_table	= max14577_muic_id,
 };
 
-- 
2.25.4


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

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

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

* [PATCH v2 3/5] extcon: extcon-max77693.c: Fix potential work-queue cancellation race
  2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
  2021-05-18 12:31 ` [PATCH v2 1/5] devm-helpers: Add resource managed version of work init Matti Vaittinen
  2021-05-18 12:32 ` [PATCH v2 2/5] extcon: extcon-max14577: Fix potential work-queue cancellation race Matti Vaittinen
@ 2021-05-18 12:32 ` Matti Vaittinen
  2021-05-18 12:32 ` [PATCH v2 4/5] extcon: extcon-max8997: Fix IRQ freeing at error path Matti Vaittinen
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Matti Vaittinen @ 2021-05-18 12:32 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Hans de Goede, Matti Vaittinen, Marek Szyprowski,
	linux-kernel

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

The extcon IRQ schedules a work item. IRQ is requested using devm while
WQ is cancelld at remove(). This mixing of devm and manual unwinding has
potential case where the WQ has been emptied (.remove() was ran) but
devm unwinding of IRQ was not yet done. It may be possible the IRQ is
triggered at this point scheduling new work item to the already flushed
queue.

According to the input documentation the input device allocated by
devm_input_allocate_device() does not need to be explicitly unregistered.
Use the new devm_work_autocancel() and remove the remove() to simplify the
code.

Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
---

Please note that the change is compile-tested only. All proper testing is
highly appreciated.
---
 drivers/extcon/extcon-max77693.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)

diff --git a/drivers/extcon/extcon-max77693.c b/drivers/extcon/extcon-max77693.c
index 92af97e00828..1f1d9ab0c5c7 100644
--- a/drivers/extcon/extcon-max77693.c
+++ b/drivers/extcon/extcon-max77693.c
@@ -5,6 +5,7 @@
 // Copyright (C) 2012 Samsung Electrnoics
 // Chanwoo Choi <cw00.choi@samsung.com>
 
+#include <linux/devm-helpers.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/i2c.h>
@@ -1127,7 +1128,10 @@ static int max77693_muic_probe(struct platform_device *pdev)
 	platform_set_drvdata(pdev, info);
 	mutex_init(&info->mutex);
 
-	INIT_WORK(&info->irq_work, max77693_muic_irq_work);
+	ret = devm_work_autocancel(&pdev->dev, &info->irq_work,
+				   max77693_muic_irq_work);
+	if (ret)
+		return ret;
 
 	/* Support irq domain for MAX77693 MUIC device */
 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
@@ -1254,22 +1258,11 @@ static int max77693_muic_probe(struct platform_device *pdev)
 	return ret;
 }
 
-static int max77693_muic_remove(struct platform_device *pdev)
-{
-	struct max77693_muic_info *info = platform_get_drvdata(pdev);
-
-	cancel_work_sync(&info->irq_work);
-	input_unregister_device(info->dock);
-
-	return 0;
-}
-
 static struct platform_driver max77693_muic_driver = {
 	.driver		= {
 		.name	= DEV_NAME,
 	},
 	.probe		= max77693_muic_probe,
-	.remove		= max77693_muic_remove,
 };
 
 module_platform_driver(max77693_muic_driver);
-- 
2.25.4


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

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

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

* [PATCH v2 4/5] extcon: extcon-max8997: Fix IRQ freeing at error path
  2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
                   ` (2 preceding siblings ...)
  2021-05-18 12:32 ` [PATCH v2 3/5] extcon: extcon-max77693.c: " Matti Vaittinen
@ 2021-05-18 12:32 ` Matti Vaittinen
  2021-05-25 15:58   ` Krzysztof Kozlowski
  2021-05-18 12:33 ` [PATCH v2 5/5] extcon: extcon-max8997: Simplify driver using devm Matti Vaittinen
                   ` (2 subsequent siblings)
  6 siblings, 1 reply; 11+ messages in thread
From: Matti Vaittinen @ 2021-05-18 12:32 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Hans de Goede, Matti Vaittinen, Marek Szyprowski,
	linux-kernel

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

If reading MAX8997_MUIC_REG_STATUS1 fails at probe the driver exits
without freeing the requested IRQs.

Free the IRQs prior returning if reading the status fails.

Fixes: 3e34c8198960 ("extcon: max8997: Avoid forcing UART path on drive probe")
Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
---
Changelog:
 v2:
   - new patch (avoid backporting devm_wq just to fix IRQ freeing)
---
 drivers/extcon/extcon-max8997.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/extcon/extcon-max8997.c b/drivers/extcon/extcon-max8997.c
index e1408075ef7d..c15a612067af 100644
--- a/drivers/extcon/extcon-max8997.c
+++ b/drivers/extcon/extcon-max8997.c
@@ -733,7 +733,7 @@ static int max8997_muic_probe(struct platform_device *pdev)
 				2, info->status);
 	if (ret) {
 		dev_err(info->dev, "failed to read MUIC register\n");
-		return ret;
+		goto err_irq;
 	}
 	cable_type = max8997_muic_get_cable_type(info,
 					   MAX8997_CABLE_GROUP_ADC, &attached);
-- 
2.25.4


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

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

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

* [PATCH v2 5/5] extcon: extcon-max8997: Simplify driver using devm
  2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
                   ` (3 preceding siblings ...)
  2021-05-18 12:32 ` [PATCH v2 4/5] extcon: extcon-max8997: Fix IRQ freeing at error path Matti Vaittinen
@ 2021-05-18 12:33 ` Matti Vaittinen
  2021-05-25 15:59   ` Krzysztof Kozlowski
  2021-05-19  0:39 ` [PATCH v2 0/5] Add devm helper for work-queue initialization Rob Herring
  2021-05-27 11:07 ` Hans de Goede
  6 siblings, 1 reply; 11+ messages in thread
From: Matti Vaittinen @ 2021-05-18 12:33 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Hans de Goede, Matti Vaittinen, Marek Szyprowski,
	linux-kernel

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

Simplify driver by switching to use the resource managed IRQ
requesting and resource managed work-queue initialization.

Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
---
Changelog:
 v2:
  - IRQ freeing fix splitted in own patch

Please note that the change is compile-tested only. All proper testing is
highly appreciated.
---
 drivers/extcon/extcon-max8997.c | 47 +++++++++++----------------------
 1 file changed, 16 insertions(+), 31 deletions(-)

diff --git a/drivers/extcon/extcon-max8997.c b/drivers/extcon/extcon-max8997.c
index c15a612067af..bbc592823570 100644
--- a/drivers/extcon/extcon-max8997.c
+++ b/drivers/extcon/extcon-max8997.c
@@ -5,6 +5,7 @@
 //  Copyright (C) 2012 Samsung Electronics
 //  Donggeun Kim <dg77.kim@samsung.com>
 
+#include <linux/devm-helpers.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/i2c.h>
@@ -650,27 +651,30 @@ static int max8997_muic_probe(struct platform_device *pdev)
 	mutex_init(&info->mutex);
 
 	INIT_WORK(&info->irq_work, max8997_muic_irq_work);
+	ret = devm_work_autocancel(&pdev->dev, &info->irq_work,
+				   max8997_muic_irq_work);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
 		struct max8997_muic_irq *muic_irq = &muic_irqs[i];
 		unsigned int virq = 0;
 
 		virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
-		if (!virq) {
-			ret = -EINVAL;
-			goto err_irq;
-		}
+		if (!virq)
+			return -EINVAL;
+
 		muic_irq->virq = virq;
 
-		ret = request_threaded_irq(virq, NULL,
-				max8997_muic_irq_handler,
-				IRQF_NO_SUSPEND,
-				muic_irq->name, info);
+		ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
+						max8997_muic_irq_handler,
+						IRQF_NO_SUSPEND,
+						muic_irq->name, info);
 		if (ret) {
 			dev_err(&pdev->dev,
 				"failed: irq request (IRQ: %d, error :%d)\n",
 				muic_irq->irq, ret);
-			goto err_irq;
+			return ret;
 		}
 	}
 
@@ -678,14 +682,13 @@ static int max8997_muic_probe(struct platform_device *pdev)
 	info->edev = devm_extcon_dev_allocate(&pdev->dev, max8997_extcon_cable);
 	if (IS_ERR(info->edev)) {
 		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
-		ret = PTR_ERR(info->edev);
-		goto err_irq;
+		return PTR_ERR(info->edev);
 	}
 
 	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
 	if (ret) {
 		dev_err(&pdev->dev, "failed to register extcon device\n");
-		goto err_irq;
+		return ret;
 	}
 
 	if (pdata && pdata->muic_pdata) {
@@ -733,7 +736,7 @@ static int max8997_muic_probe(struct platform_device *pdev)
 				2, info->status);
 	if (ret) {
 		dev_err(info->dev, "failed to read MUIC register\n");
-		goto err_irq;
+		return ret;
 	}
 	cable_type = max8997_muic_get_cable_type(info,
 					   MAX8997_CABLE_GROUP_ADC, &attached);
@@ -756,23 +759,6 @@ static int max8997_muic_probe(struct platform_device *pdev)
 			delay_jiffies);
 
 	return 0;
-
-err_irq:
-	while (--i >= 0)
-		free_irq(muic_irqs[i].virq, info);
-	return ret;
-}
-
-static int max8997_muic_remove(struct platform_device *pdev)
-{
-	struct max8997_muic_info *info = platform_get_drvdata(pdev);
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
-		free_irq(muic_irqs[i].virq, info);
-	cancel_work_sync(&info->irq_work);
-
-	return 0;
 }
 
 static struct platform_driver max8997_muic_driver = {
@@ -780,7 +766,6 @@ static struct platform_driver max8997_muic_driver = {
 		.name	= DEV_NAME,
 	},
 	.probe		= max8997_muic_probe,
-	.remove		= max8997_muic_remove,
 };
 
 module_platform_driver(max8997_muic_driver);
-- 
2.25.4


-- 
Matti Vaittinen, Linux device drivers
ROHM Semiconductors, Finland SWDC
Kiviharjunlenkki 1E
90220 OULU
FINLAND

~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
Simon says - in Latin please.
~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
Thanks to Simon Glass for the translation =] 

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

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

* Re: [PATCH v2 0/5] Add devm helper for work-queue initialization
  2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
                   ` (4 preceding siblings ...)
  2021-05-18 12:33 ` [PATCH v2 5/5] extcon: extcon-max8997: Simplify driver using devm Matti Vaittinen
@ 2021-05-19  0:39 ` Rob Herring
  2021-05-19  5:33   ` Matti Vaittinen
  2021-05-27 11:07 ` Hans de Goede
  6 siblings, 1 reply; 11+ messages in thread
From: Rob Herring @ 2021-05-19  0:39 UTC (permalink / raw)
  To: Matti Vaittinen
  Cc: Matti Vaittinen, hanwoo Choi, Krzysztof Kozlowski,
	Bartlomiej Zolnierkiewicz, MyungJoo Ham, Hans de Goede,
	Marek Szyprowski, linux-kernel

On Tue, May 18, 2021 at 03:31:37PM +0300, Matti Vaittinen wrote:
> This series adds new devm_work_autocancel() helper.
> 
> Many drivers which use work-queues must ensure the work is not queued when
> driver is detached. Often this is done by ensuring new work is not added and
> then calling cancel_work_sync() at remove(). In many cases this also requires
> cleanup at probe error path - which is easy to forget (or get wrong).
> 
> Also the "by ensuring new work is not added" has a gotcha.
> 
> It is not strange to see devm managed IRQs scheduling work.
> Mixing this with manual wq clean-up is hard to do correctly because the
> devm is likely to free the IRQ only after the remove() is ran. So manual
> wq cancellation and devm-based IRQ management do not mix well - there is
> a short(?) time-window after the wq clean-up when IRQs are still not
> freed and may schedule new work.
> 
> When both WQs and IRQs are managed by devm things are likely to just
> work. WQs should be initialized before IRQs (when IRQs need to schedule
> work) and devm unwinds things in "FILO" order.

Wouldn't it be better to convert drivers to use threaded IRQ handlers?

> 
> This series implements wq cancellation on top of devm and replaces
> the obvious cases where only thing remove call-back in a driver does is
> cancelling the work. There might be other cases where we could switch
> more than just work cancellation to use managed version and thus get rid
> of remove or mixed (manual and devm) resource management.
> 
> Changelog v2:
>   - rebased on v5.13-rc2
>   - split the extcon-max8997 change into two. First a simple,
>     back-portable fix for omitting IRQ freeing at error path, second
>     being the devm-simpification which does not need backporting.
> 
> ---
> 
> Matti Vaittinen (5):
>   devm-helpers: Add resource managed version of work init
>   extcon: extcon-max14577: Fix potential work-queue cancellation race
>   extcon: extcon-max77693.c: Fix potential work-queue cancellation race
>   extcon: extcon-max8997: Fix IRQ freeing at error path
>   extcon: extcon-max8997: Simplify driver using devm
> 
>  drivers/extcon/extcon-max14577.c | 16 ++++--------
>  drivers/extcon/extcon-max77693.c | 17 ++++--------
>  drivers/extcon/extcon-max8997.c  | 45 +++++++++++---------------------
>  include/linux/devm-helpers.h     | 25 ++++++++++++++++++
>  4 files changed, 50 insertions(+), 53 deletions(-)
> 
> 
> base-commit: d07f6ca923ea0927a1024dfccafc5b53b61cfecc
> -- 
> 2.25.4
> 
> 
> -- 
> Matti Vaittinen, Linux device drivers
> ROHM Semiconductors, Finland SWDC
> Kiviharjunlenkki 1E
> 90220 OULU
> FINLAND
> 
> ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~
> Simon says - in Latin please.
> ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~
> Thanks to Simon Glass for the translation =] 



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

* Re: [PATCH v2 0/5] Add devm helper for work-queue initialization
  2021-05-19  0:39 ` [PATCH v2 0/5] Add devm helper for work-queue initialization Rob Herring
@ 2021-05-19  5:33   ` Matti Vaittinen
  0 siblings, 0 replies; 11+ messages in thread
From: Matti Vaittinen @ 2021-05-19  5:33 UTC (permalink / raw)
  To: Rob Herring
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Hans de Goede, Marek Szyprowski, linux-kernel


On Tue, 2021-05-18 at 19:39 -0500, Rob Herring wrote:
> On Tue, May 18, 2021 at 03:31:37PM +0300, Matti Vaittinen wrote:
> > This series adds new devm_work_autocancel() helper.
> > 
> > Many drivers which use work-queues must ensure the work is not
> > queued when
> > driver is detached. Often this is done by ensuring new work is not
> > added and
> > then calling cancel_work_sync() at remove(). In many cases this
> > also requires
> > cleanup at probe error path - which is easy to forget (or get
> > wrong).
> > 
> > Also the "by ensuring new work is not added" has a gotcha.
> > 
> > It is not strange to see devm managed IRQs scheduling work.
> > Mixing this with manual wq clean-up is hard to do correctly because
> > the
> > devm is likely to free the IRQ only after the remove() is ran. So
> > manual
> > wq cancellation and devm-based IRQ management do not mix well -
> > there is
> > a short(?) time-window after the wq clean-up when IRQs are still
> > not
> > freed and may schedule new work.
> > 
> > When both WQs and IRQs are managed by devm things are likely to
> > just
> > work. WQs should be initialized before IRQs (when IRQs need to
> > schedule
> > work) and devm unwinds things in "FILO" order.
> 
> Wouldn't it be better to convert drivers to use threaded IRQ
> handlers?

Morning Rob,

I think for example the extcon drivers were using threaded IRQs. In
general, there may be legitimate use-cases for having threaded IRQs but
still offloading some work to WQ. I guess that for example the
IRQF_ONESHOT usage could be such, right?

Best Regards
	Matti Vaittinen



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

* Re: [PATCH v2 4/5] extcon: extcon-max8997: Fix IRQ freeing at error path
  2021-05-18 12:32 ` [PATCH v2 4/5] extcon: extcon-max8997: Fix IRQ freeing at error path Matti Vaittinen
@ 2021-05-25 15:58   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 11+ messages in thread
From: Krzysztof Kozlowski @ 2021-05-25 15:58 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Bartlomiej Zolnierkiewicz, MyungJoo Ham,
	Hans de Goede, Marek Szyprowski, linux-kernel

On 18/05/2021 08:32, Matti Vaittinen wrote:
> If reading MAX8997_MUIC_REG_STATUS1 fails at probe the driver exits
> without freeing the requested IRQs.
> 
> Free the IRQs prior returning if reading the status fails.
> 
> Fixes: 3e34c8198960 ("extcon: max8997: Avoid forcing UART path on drive probe")
> Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
> ---
> Changelog:
>  v2:
>    - new patch (avoid backporting devm_wq just to fix IRQ freeing)
> ---
>  drivers/extcon/extcon-max8997.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 

Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>


Best regards,
Krzysztof

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

* Re: [PATCH v2 5/5] extcon: extcon-max8997: Simplify driver using devm
  2021-05-18 12:33 ` [PATCH v2 5/5] extcon: extcon-max8997: Simplify driver using devm Matti Vaittinen
@ 2021-05-25 15:59   ` Krzysztof Kozlowski
  0 siblings, 0 replies; 11+ messages in thread
From: Krzysztof Kozlowski @ 2021-05-25 15:59 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Bartlomiej Zolnierkiewicz, MyungJoo Ham,
	Hans de Goede, Marek Szyprowski, linux-kernel

On 18/05/2021 08:33, Matti Vaittinen wrote:
> Simplify driver by switching to use the resource managed IRQ
> requesting and resource managed work-queue initialization.
> 
> Signed-off-by: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
> ---
> Changelog:
>  v2:
>   - IRQ freeing fix splitted in own patch
> 
> Please note that the change is compile-tested only. All proper testing is
> highly appreciated.
> ---
>  drivers/extcon/extcon-max8997.c | 47 +++++++++++----------------------
>  1 file changed, 16 insertions(+), 31 deletions(-)
> 


Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>


Best regards,
Krzysztof

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

* Re: [PATCH v2 0/5] Add devm helper for work-queue initialization
  2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
                   ` (5 preceding siblings ...)
  2021-05-19  0:39 ` [PATCH v2 0/5] Add devm helper for work-queue initialization Rob Herring
@ 2021-05-27 11:07 ` Hans de Goede
  6 siblings, 0 replies; 11+ messages in thread
From: Hans de Goede @ 2021-05-27 11:07 UTC (permalink / raw)
  To: Matti Vaittinen, Matti Vaittinen
  Cc: hanwoo Choi, Krzysztof Kozlowski, Bartlomiej Zolnierkiewicz,
	MyungJoo Ham, Marek Szyprowski, linux-kernel

Hi,

On 5/18/21 2:31 PM, Matti Vaittinen wrote:
> This series adds new devm_work_autocancel() helper.
> 
> Many drivers which use work-queues must ensure the work is not queued when
> driver is detached. Often this is done by ensuring new work is not added and
> then calling cancel_work_sync() at remove(). In many cases this also requires
> cleanup at probe error path - which is easy to forget (or get wrong).
> 
> Also the "by ensuring new work is not added" has a gotcha.
> 
> It is not strange to see devm managed IRQs scheduling work.
> Mixing this with manual wq clean-up is hard to do correctly because the
> devm is likely to free the IRQ only after the remove() is ran. So manual
> wq cancellation and devm-based IRQ management do not mix well - there is
> a short(?) time-window after the wq clean-up when IRQs are still not
> freed and may schedule new work.
> 
> When both WQs and IRQs are managed by devm things are likely to just
> work. WQs should be initialized before IRQs (when IRQs need to schedule
> work) and devm unwinds things in "FILO" order.
> 
> This series implements wq cancellation on top of devm and replaces
> the obvious cases where only thing remove call-back in a driver does is
> cancelling the work. There might be other cases where we could switch
> more than just work cancellation to use managed version and thus get rid
> of remove or mixed (manual and devm) resource management.
> 
> Changelog v2:
>   - rebased on v5.13-rc2
>   - split the extcon-max8997 change into two. First a simple,
>     back-portable fix for omitting IRQ freeing at error path, second
>     being the devm-simpification which does not need backporting.

Thanks, the entire series looks good to me:

Reviewed-by: Hans de Goede <hdegoede@redhat.com>

for the series.

As for merging this. Normally as the include/linux/devm-helpers.h
maintainer I would pick-up at least patch 1/5 and then provide an
immutable branch + pull-req; or just merge the entire series.

But since there are no other include/linux/devm-helpers.h changes
planned for 5.14 and since all the other patches are extcon changes,
I believe it would be best if the entire series would just be merged
through the extcon tree. Chanwoo / MyungJoo does that work for you ?

Regards,

Hans

> 
> ---
> 
> Matti Vaittinen (5):
>   devm-helpers: Add resource managed version of work init
>   extcon: extcon-max14577: Fix potential work-queue cancellation race
>   extcon: extcon-max77693.c: Fix potential work-queue cancellation race
>   extcon: extcon-max8997: Fix IRQ freeing at error path
>   extcon: extcon-max8997: Simplify driver using devm
> 
>  drivers/extcon/extcon-max14577.c | 16 ++++--------
>  drivers/extcon/extcon-max77693.c | 17 ++++--------
>  drivers/extcon/extcon-max8997.c  | 45 +++++++++++---------------------
>  include/linux/devm-helpers.h     | 25 ++++++++++++++++++
>  4 files changed, 50 insertions(+), 53 deletions(-)
> 
> 
> base-commit: d07f6ca923ea0927a1024dfccafc5b53b61cfecc
> 


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

end of thread, other threads:[~2021-05-27 11:07 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-18 12:31 [PATCH v2 0/5] Add devm helper for work-queue initialization Matti Vaittinen
2021-05-18 12:31 ` [PATCH v2 1/5] devm-helpers: Add resource managed version of work init Matti Vaittinen
2021-05-18 12:32 ` [PATCH v2 2/5] extcon: extcon-max14577: Fix potential work-queue cancellation race Matti Vaittinen
2021-05-18 12:32 ` [PATCH v2 3/5] extcon: extcon-max77693.c: " Matti Vaittinen
2021-05-18 12:32 ` [PATCH v2 4/5] extcon: extcon-max8997: Fix IRQ freeing at error path Matti Vaittinen
2021-05-25 15:58   ` Krzysztof Kozlowski
2021-05-18 12:33 ` [PATCH v2 5/5] extcon: extcon-max8997: Simplify driver using devm Matti Vaittinen
2021-05-25 15:59   ` Krzysztof Kozlowski
2021-05-19  0:39 ` [PATCH v2 0/5] Add devm helper for work-queue initialization Rob Herring
2021-05-19  5:33   ` Matti Vaittinen
2021-05-27 11:07 ` Hans de Goede

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.