Skip to contentSkip to conversation

gpio: Introduce helper to dispose IRQ mappings

PATCH In review Andy Shevchenko 5 participants 13 messages Oct 21 – Nov 4

Subsystems (2)
lore
Share & tools
lore
#4 lore raw

PATCH 2/3 gpio: mockup: Replace custom devm_gpiochip_dispose_irq_mappings()

In reply to Andy Shevchenko

Replace custom implementation of devm_gpiochip_dispose_irq_mappings().

---
 drivers/gpio/gpio-mockup.c | 15 +--------------
 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/gpio/gpio-mockup.c +1 -14
index 1c6a2f3414f1..f782b55240b3 100644
@@ -400,19 +400,6 @@ static void gpio_mockup_debugfs_cleanup(void *data)
 	debugfs_remove_recursive(chip->dbg_dir);
 }
 
-static void gpio_mockup_dispose_mappings(void *data)
-{
-	struct gpio_mockup_chip *chip = data;
-	struct gpio_chip *gc = &chip->gc;
-	int i, irq;
-
-	for (i = 0; i < gc->ngpio; i++) {
-		irq = irq_find_mapping(chip->irq_sim_domain, i);
-		if (irq)
-			irq_dispose_mapping(irq);
-	}
-}
-
 static int gpio_mockup_probe(struct platform_device *pdev)
 {
 	struct gpio_mockup_chip *chip;
@@ -473,7 +460,7 @@ static int gpio_mockup_probe(struct platform_device *pdev)
 	if (IS_ERR(chip->irq_sim_domain))
 		return PTR_ERR(chip->irq_sim_domain);
 
-	rv = devm_add_action_or_reset(dev, gpio_mockup_dispose_mappings, chip);
+	rv = devm_gpiochip_dispose_irq_mappings(dev, gc, chip->irq_sim_domain);
 	if (rv)
 		return rv;
 
Signature · 1 line
2.50.1
#0 lore raw

cover gpio: Introduce helper to dispose IRQ mappings

I have noticed that a few drivers use almost the same code to dispose IRQ mappings. This series to deduplicate the efforts. I only converted a couple of the users (there are potentially more) in order to stay in the same subsystem.

Andy Shevchenko (3):
gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()
gpio: mockup: Replace custom devm_gpiochip_dispose_irq_mappings()
gpio: sim: Replace custom devm_gpiochip_dispose_irq_mappings()

 drivers/gpio/gpio-mockup.c    | 15 +--------------
 drivers/gpio/gpio-sim.c       | 17 ++++-------------
 drivers/gpio/gpiolib-devres.c | 33 +++++++++++++++++++++++++++++++++
 include/linux/gpio/driver.h   |  4 ++++
 4 files changed, 42 insertions(+), 27 deletions(-)
Signature · 1 line
2.50.1
#6 lore raw

PATCH 3/3 gpio: sim: Replace custom devm_gpiochip_dispose_irq_mappings()

Replace custom implementation of devm_gpiochip_dispose_irq_mappings().

---
 drivers/gpio/gpio-sim.c | 17 ++++-------------
 1 file changed, 4 insertions(+), 13 deletions(-)
drivers/gpio/gpio-sim.c +4 -13
index 34b45d0d3575..5667871f21c2 100644
@@ -338,15 +338,6 @@ static void gpio_sim_put_device(void *data)
 	put_device(dev);
 }
 
-static void gpio_sim_dispose_mappings(void *data)
-{
-	struct gpio_sim_chip *chip = data;
-	unsigned int i;
-
-	for (i = 0; i < chip->gc.ngpio; i++)
-		irq_dispose_mapping(irq_find_mapping(chip->irq_sim, i));
-}
-
 static void gpio_sim_sysfs_remove(void *data)
 {
 	struct gpio_sim_chip *chip = data;
@@ -467,10 +458,6 @@ static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev)
 	if (IS_ERR(chip->irq_sim))
 		return PTR_ERR(chip->irq_sim);
 
-	ret = devm_add_action_or_reset(dev, gpio_sim_dispose_mappings, chip);
-	if (ret)
-		return ret;
-
 	ret = devm_mutex_init(dev, &chip->lock);
 	if (ret)
 		return ret;
@@ -496,6 +483,10 @@ static int gpio_sim_add_bank(struct fwnode_handle *swnode, struct device *dev)
 	gc->dbg_show = PTR_IF(IS_ENABLED(CONFIG_DEBUG_FS), gpio_sim_dbg_show);
 	gc->can_sleep = true;
 
+	ret = devm_gpiochip_dispose_irq_mappings(dev, gc, chip->irq_sim);
+	if (ret)
+		return ret;
+
 	ret = devm_gpiochip_add_data(dev, gc, chip);
 	if (ret)
 		return ret;
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/3 gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()

In reply to Andy Shevchenko

Several drivers already copy'n'paste almost the same code to dispose IRQ mappings on remove over and over. Deduplicate that by introducing a devm_gpiochip_dispose_irq_mappings().

---
 drivers/gpio/gpiolib-devres.c | 33 +++++++++++++++++++++++++++++++++
 include/linux/gpio/driver.h   |  4 ++++
 2 files changed, 37 insertions(+)
drivers/gpio/gpiolib-devres.c +33 -0
index cc0e434a37d3..94690f14a949 100644
@@ -10,6 +10,7 @@
 #include <linux/err.h>
 #include <linux/export.h>
 #include <linux/gfp.h>
+#include <linux/irqdomain.h>
 #include <linux/types.h>
 
 #include <linux/gpio/consumer.h>
@@ -323,6 +324,38 @@ void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
 }
 EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
 
+struct gpiochip_irqdomain {
+	struct gpio_chip *gc;
+	struct irq_domain *domain;
+};
+
+static void gpiochip_dispose_irq_mappings(struct device *dev, void *res)
+{
+	struct gpiochip_irqdomain *gcirq = res;
+
+	for (unsigned int i = 0; i < gcirq->gc->ngpio; i++)
+		irq_dispose_mapping(irq_find_mapping(gcirq->domain, i));
+}
+
+int devm_gpiochip_dispose_irq_mappings(struct device *dev,
+				       struct gpio_chip *gc,
+				       struct irq_domain *domain)
+{
+	struct gpiochip_irqdomain *gcirq;
+
+	gcirq = devres_alloc(gpiochip_dispose_irq_mappings, sizeof(*gcirq), GFP_KERNEL);
+	if (!gcirq)
+		return -ENOMEM;
+
+	gcirq->gc = gc;
+	gcirq->domain = domain;
+
+	devres_add(dev, gcirq);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_gpiochip_dispose_irq_mappings);
+
 static void devm_gpio_chip_release(void *data)
 {
 	struct gpio_chip *gc = data;
include/linux/gpio/driver.h +4 -0
index b3b938ec5332..c610b5a9f8fa 100644
@@ -707,6 +707,10 @@ static inline int gpiochip_irqchip_add_domain(struct gpio_chip *gc,
 }
 #endif
 
+int devm_gpiochip_dispose_irq_mappings(struct device *dev,
+				       struct gpio_chip *gc,
+				       struct irq_domain *domain);
+
 int gpiochip_generic_request(struct gpio_chip *gc, unsigned int offset);
 void gpiochip_generic_free(struct gpio_chip *gc, unsigned int offset);
 int gpiochip_generic_config(struct gpio_chip *gc, unsigned int offset,
Signature · 1 line
2.50.1
#2 lore raw

Re: [PATCH v1 1/3] gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] gpiolib: build failure in gpiochip_dispose_irq_mappings() without IRQ_DOMAIN --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] gpiolib: build failure in gpiochip_dispose_irq_mappings() without IRQ_DOMAIN --

commit c8fefed15e55a94335f2d9f38c7ab3dbbe263c9b
Author: Andy Shevchenko <[email protected]>

gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()

This commit introduces the devm_gpiochip_dispose_irq_mappings() helper in drivers/gpio/gpiolib-devres.c to deduplicate disposing of GPIO chip IRQ mappings on device removal.

Quoted 4 lines
drivers/gpio/gpiolib-devres.c +0 -0
index 2ec825ffab7da..e7e6690d699e7 100644

[ ... ]

Quoted 6 lines
drivers/gpio/gpiolib-devres.c +2 -0
@@ -323,6 +324,38 @@ void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs)
 }
 EXPORT_SYMBOL_GPL(devm_gpiod_put_array);
 
+struct gpiochip_irqdomain {
+	struct gpio_chip *gc;

drivers/gpio/gpiolib-devres.c void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs) · line 338

Quoted 10 lines
drivers/gpio/gpiolib-devres.c +10 -0
+	struct irq_domain *domain;
+};
+
+static void gpiochip_dispose_irq_mappings(struct device *dev, void *res)
+{
+	struct gpiochip_irqdomain *gcirq = res;
+
+	for (unsigned int i = 0; i < gcirq->gc->ngpio; i++)
+		irq_dispose_mapping(irq_find_mapping(gcirq->domain, i));
+}

[Severity: Low]
Will gpiochip_dispose_irq_mappings() in drivers/gpio/gpiolib-devres.c fail to build when CONFIG_GPIOLIB=y and CONFIG_IRQ_DOMAIN is disabled?

In drivers/gpio/Makefile, gpiolib-devres.o is built whenever CONFIG_GPIOLIB is enabled:

    obj-$(CONFIG_GPIOLIB)		+= gpiolib-devres.o

In drivers/gpio/Kconfig, GPIOLIB does not select IRQ_DOMAIN (only GPIOLIB_IRQCHIP selects it).

In include/linux/irqdomain.h, irq_find_mapping() is only defined inside the CONFIG_IRQ_DOMAIN block, while the fallback block for !CONFIG_IRQ_DOMAIN provides a stub for irq_dispose_mapping() but omits irq_find_mapping():

    #else /* CONFIG_IRQ_DOMAIN */
    static inline void irq_dispose_mapping(unsigned int virq) { }
    static inline struct irq_domain *irq_find_matching_fwnode(...)
    {
            return NULL;
    }
    #endif /* !CONFIG_IRQ_DOMAIN */

Without a stub for irq_find_mapping() or a CONFIG_IRQ_DOMAIN guard, does compiling drivers/gpio/gpiolib-devres.c with CONFIG_GPIOLIB=y and CONFIG_IRQ_DOMAIN=n result in an implicit declaration error for irq_find_mapping()?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#8 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

I have noticed that a few drivers use almost the same code to dispose IRQ mappings. This series to deduplicate the efforts. I only converted a couple of the users (there are potentially more) in order to stay in the same subsystem.

The "few" being 2 testing drivers of which one (mockup) should ideally go away. Do we really want to add more code to GPIO core that will only be used in testing module(s)? I don't think this is useful, sorry.

Bartosz

#9 lore raw
Quoted 9 lines · Bartosz Golaszewski
Quoted 4 lines · Andy Shevchenko

I have noticed that a few drivers use almost the same code to dispose IRQ mappings. This series to deduplicate the efforts. I only converted a couple of the users (there are potentially more) in order to stay in the same subsystem.

The "few" being 2 testing drivers of which one (mockup) should ideally go away. Do we really want to add more code to GPIO core that will only be used in testing module(s)? I don't think this is useful, sorry.

I meant a few. As I pointed these were chosen explicitly for the sake of not crossing the subsystem boundaries. I looked at Broadcom one, it is so big mess but I think I can get an advantage on it. Also there are other drivers that just need more work to check (and many of them are outside of drivers/gpio), but I'm pretty sure the new API has a good potential.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
Quoted 4 lines · Andy Shevchenko

I have noticed that a few drivers use almost the same code to dispose IRQ mappings. This series to deduplicate the efforts. I only converted a couple of the users (there are potentially more) in order to stay in the same subsystem.

The "few" being 2 testing drivers of which one (mockup) should ideally go away. Do we really want to add more code to GPIO core that will only be used in testing module(s)? I don't think this is useful, sorry.

Quoted 6 lines

I meant a few. As I pointed these were chosen explicitly for the sake of not crossing the subsystem boundaries. I looked at Broadcom one, it is so big mess but I think I can get an advantage on it. Also there are other drivers that just need more work to check (and many of them are outside of drivers/gpio), but I'm pretty sure the new API has a good potential.

Would it be possible to provide at least one more user other than the testing drivers?

Bart

#11 lore raw
Quoted 13 lines · Bartosz Golaszewski
Quoted 11 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
Quoted 4 lines · Andy Shevchenko

I have noticed that a few drivers use almost the same code to dispose IRQ mappings. This series to deduplicate the efforts. I only converted a couple of the users (there are potentially more) in order to stay in the same subsystem.

The "few" being 2 testing drivers of which one (mockup) should ideally go away. Do we really want to add more code to GPIO core that will only be used in testing module(s)? I don't think this is useful, sorry.

Quoted 8 lines
Quoted 5 lines

I meant a few. As I pointed these were chosen explicitly for the sake of not crossing the subsystem boundaries. I looked at Broadcom one, it is so big mess but I think I can get an advantage on it. Also there are other drivers that just need more work to check (and many of them are outside of drivers/gpio), but I'm pretty sure the new API has a good potential.

Would it be possible to provide at least one more user other than the testing drivers?

Sure.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [PATCH v1 1/3] gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()

In reply to Andy Shevchenko
kernel test robot noticed the following build errors:

Hi Andy,

kernel test robot noticed the following build errors:

[auto build test ERROR on brgl/gpio/for-next]
[also build test ERROR on linus/master v7.3 next-20261022] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Andy-Shevchenko/gpiolib-Introduce-devm_gpiochip_dispose_irq_mappings/20261021-224443
base:   https://git.kernel.org/pub/scm/linux/kernel/git/brgl/linux.git gpio/for-next
patch link:    https://lore.kernel.org/r/20261021204654.1001101-2-andriy.shevchenko%40linux.intel.com
patch subject: [PATCH v1 1/3] gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()
config: m68k-allnoconfig (https://download.01.org/0day-ci/archive/20261023/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 16.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261023/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/gpio/gpiolib-devres.c: In function 'gpiochip_dispose_irq_mappings':

>> drivers/gpio/gpiolib-devres.c:337:37: error: implicit declaration of function 'irq_find_mapping'; did you mean 'irq_dispose_mapping'? [-Wimplicit-function-declaration]
     337 |                 irq_dispose_mapping(irq_find_mapping(gcirq->domain, i));
         |                                     ^~~~~~~~~~~~~~~~
         |                                     irq_dispose_mapping

vim +337 drivers/gpio/gpiolib-devres.c

331
332 static void gpiochip_dispose_irq_mappings(struct device *dev, void *res)
333 {
334 struct gpiochip_irqdomain *gcirq = res;
335
336 for (unsigned int i = 0; i < gcirq->gc->ngpio; i++)

 > 337			irq_dispose_mapping(irq_find_mapping(gcirq->domain, i));

338 }
339

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#12 lore raw
In reply to Andy Shevchenko
Quoted 10 lines · Andy Shevchenko

I have noticed that a few drivers use almost the same code to dispose IRQ mappings. This series to deduplicate the efforts. I only converted a couple of the users (there are potentially more) in order to stay in the same subsystem.

Andy Shevchenko (3):
gpiolib: Introduce devm_gpiochip_dispose_irq_mappings()
gpio: mockup: Replace custom devm_gpiochip_dispose_irq_mappings()
gpio: sim: Replace custom devm_gpiochip_dispose_irq_mappings()

Quoted 9 lines
 drivers/gpio/gpio-mockup.c    | 15 +--------------
 drivers/gpio/gpio-sim.c       | 17 ++++-------------
 drivers/gpio/gpiolib-devres.c | 33 +++++++++++++++++++++++++++++++++
 include/linux/gpio/driver.h   |  4 ++++
 4 files changed, 42 insertions(+), 27 deletions(-)
Signature · 1 line
2.50.1

Linus' responses reminded me about this series and I realized I don't really like the fact you hide the call to devm_add_action_or_reset() behind the new interface. I'd prefer to keep devm_add_action_or_reset() and add gpiochip_dispose_irq_mappings() that can be independently scheduled by each user.

Seeing a devm_something() makes me think, it allocates something that will be released at unbind(), which isn't really the case here, we really do schedule an "action". I'd prefer it be explicit.

Bart