Compare
40bb03d3ea6f6a915218508819ecc4d7a7fa260d..205cccd06e545694754e1bf8d626ea96e742cd8a
gpio: mxc: fix race between gpiochip registration and IRQ domain setup
devm_gpiochip_add_data() makes the GPIO chip visible to consumers
immediately. Since the chip sets .to_irq = mxc_gpio_to_irq, which calls
irq_find_mapping(port->domain, offset), any to_irq() call between
gpiochip registration and IRQ domain creation will operate on a NULL
domain.
Fix by moving devm_gpiochip_add_data() after the IRQ domain and generic
irq chip are fully initialized, ensuring port->domain and the irq chip
handlers are ready before the chip becomes visible to consumers.
Fixes: d37a65bb4663 ("gpio/mxc: Move Freescale MXC gpio driver to drivers/gpio")
Signed-off-by: Peng Fan <peng.fan@nxp.com>
Reviewed-by: Linus Walleij <linusw@kernel.org>
Reviewed-by: Frank Li <Frank.Li@nxp.com>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Link: https://patch.msgid.link/20261013-gpio-mxc-cleanup-v7-4-c4d769587d3e@nxp.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
Peng Fan <peng.fan@nxp.com> · 2026-10-13 06:01 UTC · committed by Bartosz Golaszewski
12 files changed,
+62
−40
- drivers/gpio/gpio-mxc.c
- drivers/gpio/gpio-nct6694.c
- drivers/gpio/gpiolib-kunit.c
- drivers/gpio/gpiolib-shared.c
- drivers/gpio/gpiolib.c
- fs/ocfs2/dlm/dlmcommon.h
- fs/ocfs2/dlm/dlmdebug.c
- fs/ocfs2/dlm/dlmrecovery.c
- fs/pipe.c
- include/linux/sched/signal.h
- samples/pidfd/Makefile
- tools/gpio/Makefile
modified drivers/gpio/gpio-mxc.c
| @@ -7,6 +7,7 @@ | |||
| 7 | 7 | // Authors: Daniel Mack, Juergen Beisert. | |
| 8 | 8 | // Copyright (C) 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved. | |
| 9 | 9 | ||
| 10 | + | #include <linux/bitops.h> | |
| 10 | 11 | #include <linux/cleanup.h> | |
| 11 | 12 | #include <linux/clk.h> | |
| 12 | 13 | #include <linux/err.h> | |
| @@ -68,11 +69,11 @@ struct mxc_gpio_port { | |||
| 68 | 69 | struct irq_domain *domain; | |
| 69 | 70 | struct gpio_generic_chip gen_gc; | |
| 70 | 71 | struct device *dev; | |
| 71 | - | u32 both_edges; | |
| 72 | + | unsigned long both_edges; | |
| 72 | 73 | struct mxc_gpio_reg_saved gpio_saved_reg; | |
| 73 | 74 | bool power_off; | |
| 74 | - | u32 wakeup_pads; | |
| 75 | 75 | bool is_pad_wakeup; | |
| 76 | + | unsigned long wakeup_pads; | |
| 76 | 77 | u32 pad_type[32]; | |
| 77 | 78 | const struct mxc_gpio_hwdata *hwdata; | |
| 78 | 79 | }; | |
| @@ -168,7 +169,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type) | |||
| 168 | 169 | int edge; | |
| 169 | 170 | void __iomem *reg = port->base; | |
| 170 | 171 | ||
| 171 | - | port->both_edges &= ~(1 << gpio_idx); | |
| 172 | + | clear_bit(gpio_idx, &port->both_edges); | |
| 172 | 173 | switch (type) { | |
| 173 | 174 | case IRQ_TYPE_EDGE_RISING: | |
| 174 | 175 | edge = GPIO_INT_RISE_EDGE; | |
| @@ -188,7 +189,7 @@ static int gpio_set_irq_type(struct irq_data *d, u32 type) | |||
| 188 | 189 | edge = GPIO_INT_HIGH_LEV; | |
| 189 | 190 | pr_debug("mxc: set GPIO %d to high trigger\n", gpio_idx); | |
| 190 | 191 | } | |
| 191 | - | port->both_edges |= 1 << gpio_idx; | |
| 192 | + | set_bit(gpio_idx, &port->both_edges); | |
| 192 | 193 | } | |
| 193 | 194 | break; | |
| 194 | 195 | case IRQ_TYPE_LEVEL_LOW: | |
| @@ -259,7 +260,7 @@ static void mxc_gpio_irq_handler(struct mxc_gpio_port *port, u32 irq_stat) | |||
| 259 | 260 | while (irq_stat != 0) { | |
| 260 | 261 | int irqoffset = fls(irq_stat) - 1; | |
| 261 | 262 | ||
| 262 | - | if (port->both_edges & (1 << irqoffset)) | |
| 263 | + | if (test_bit(irqoffset, &port->both_edges)) | |
| 263 | 264 | mxc_flip_edge(port, irqoffset); | |
| 264 | 265 | ||
| 265 | 266 | generic_handle_domain_irq(port->domain, irqoffset); | |
| @@ -330,16 +331,18 @@ static int gpio_set_wake_irq(struct irq_data *d, u32 enable) | |||
| 330 | 331 | ret = enable_irq_wake(port->irq_high); | |
| 331 | 332 | else | |
| 332 | 333 | ret = enable_irq_wake(port->irq); | |
| 333 | - | port->wakeup_pads |= BIT(gpio_idx); | |
| 334 | 334 | } else { | |
| 335 | 335 | if (port->irq_high && (gpio_idx >= 16)) | |
| 336 | 336 | ret = disable_irq_wake(port->irq_high); | |
| 337 | 337 | else | |
| 338 | 338 | ret = disable_irq_wake(port->irq); | |
| 339 | - | port->wakeup_pads &= ~BIT(gpio_idx); | |
| 340 | 339 | } | |
| 340 | + | if (ret) | |
| 341 | + | return ret; | |
| 341 | 342 | ||
| 342 | - | return ret; | |
| 343 | + | assign_bit(gpio_idx, &port->wakeup_pads, enable); | |
| 344 | + | ||
| 345 | + | return 0; | |
| 343 | 346 | } | |
| 344 | 347 | ||
| 345 | 348 | static int mxc_gpio_init_gc(struct mxc_gpio_port *port, int irq_base) | |
| @@ -474,8 +477,6 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 474 | 477 | } else | |
| 475 | 478 | port->mx_irq_handler = mx3_gpio_irq_handler; | |
| 476 | 479 | ||
| 477 | - | mxc_update_irq_chained_handler(port, true); | |
| 478 | - | ||
| 479 | 480 | config.dev = &pdev->dev; | |
| 480 | 481 | config.sz = 4; | |
| 481 | 482 | config.dat = port->base + GPIO_PSR; | |
| @@ -499,10 +500,6 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 499 | 500 | else /* silence boot time warning */ | |
| 500 | 501 | port->gen_gc.gc.base = -1; | |
| 501 | 502 | ||
| 502 | - | err = devm_gpiochip_add_data(&pdev->dev, &port->gen_gc.gc, port); | |
| 503 | - | if (err) | |
| 504 | - | goto out_bgio; | |
| 505 | - | ||
| 506 | 503 | irq_base = devm_irq_alloc_descs(&pdev->dev, -1, 0, 32, numa_node_id()); | |
| 507 | 504 | if (irq_base < 0) { | |
| 508 | 505 | err = irq_base; | |
| @@ -523,8 +520,19 @@ static int mxc_gpio_probe(struct platform_device *pdev) | |||
| 523 | 520 | if (err < 0) | |
| 524 | 521 | goto out_irqdomain_remove; | |
| 525 | 522 | ||
| 523 | + | /* | |
| 524 | + | * Register the GPIO chip last: devm_gpiochip_add_data() makes the | |
| 525 | + | * chip visible to consumers immediately, so the IRQ domain and | |
| 526 | + | * generic irq chip must be ready before any to_irq() call. | |
| 527 | + | */ | |
| 528 | + | err = devm_gpiochip_add_data(&pdev->dev, &port->gen_gc.gc, port); | |
| 529 | + | if (err) | |
| 530 | + | goto out_irqdomain_remove; | |
| 531 | + | ||
| 526 | 532 | list_add_tail(&port->node, &mxc_gpio_ports); | |
| 527 | 533 | ||
| 534 | + | mxc_update_irq_chained_handler(port, true); | |
| 535 | + | ||
| 528 | 536 | platform_set_drvdata(pdev, port); | |
| 529 | 537 | pm_runtime_put_autosuspend(&pdev->dev); | |
| 530 | 538 | ||
modified drivers/gpio/gpio-nct6694.c
| @@ -444,7 +444,7 @@ static int nct6694_gpio_probe(struct platform_device *pdev) | |||
| 444 | 444 | data->gpio.set_config = nct6694_set_config; | |
| 445 | 445 | data->gpio.init_valid_mask = nct6694_init_valid_mask; | |
| 446 | 446 | data->gpio.base = -1; | |
| 447 | - | data->gpio.can_sleep = false; | |
| 447 | + | data->gpio.can_sleep = true; | |
| 448 | 448 | data->gpio.owner = THIS_MODULE; | |
| 449 | 449 | data->gpio.ngpio = NCT6694_NR_GPIO; | |
| 450 | 450 | ||
modified drivers/gpio/gpiolib-kunit.c
| @@ -28,6 +28,8 @@ | |||
| 28 | 28 | ||
| 29 | 29 | #define GPIO_TEST_PROVIDER_NGPIO 4 | |
| 30 | 30 | ||
| 31 | + | KUNIT_DEFINE_ACTION_WRAPPER(gpio_device_put_wrapper, gpio_device_put, struct gpio_device *); | |
| 32 | + | ||
| 31 | 33 | /* | |
| 32 | 34 | * The test provider tracks per-line direction and value so that lines can be | |
| 33 | 35 | * driven as both inputs and outputs - this is needed to exercise input as well | |
| @@ -770,6 +772,7 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset, | |||
| 770 | 772 | struct property_entry properties[2]; | |
| 771 | 773 | struct platform_device *pdev; | |
| 772 | 774 | struct fwnode_handle *fwnode; | |
| 775 | + | struct gpio_device *gdev; | |
| 773 | 776 | struct gpio_desc *desc; | |
| 774 | 777 | bool bound = true; | |
| 775 | 778 | int ret; | |
| @@ -789,10 +792,12 @@ static void gpio_hog_assert(struct kunit *test, unsigned int offset, | |||
| 789 | 792 | wait_for_device_probe(); | |
| 790 | 793 | ||
| 791 | 794 | /* The hog must have configured the line with the expected direction. */ | |
| 792 | - | struct gpio_device *gdev __free(gpio_device_put) = | |
| 793 | - | gpio_device_find_by_label(GPIO_CONSUMER_NAME); | |
| 795 | + | gdev = gpio_device_find_by_label(GPIO_CONSUMER_NAME); | |
| 794 | 796 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, gdev); | |
| 795 | 797 | ||
| 798 | + | ret = kunit_add_action_or_reset(test, gpio_device_put_wrapper, gdev); | |
| 799 | + | KUNIT_ASSERT_EQ(test, ret, 0); | |
| 800 | + | ||
| 796 | 801 | desc = gpio_device_get_desc(gdev, offset); | |
| 797 | 802 | KUNIT_ASSERT_NOT_ERR_OR_NULL(test, desc); | |
| 798 | 803 | ||
modified drivers/gpio/gpiolib.c
| @@ -3954,16 +3954,17 @@ int gpiod_set_array_value_complex(bool raw, bool can_sleep, | |||
| 3954 | 3954 | } while ((i < array_size) && | |
| 3955 | 3955 | gpio_device_chip_cmp(desc_array[i]->gdev, guard.gc)); | |
| 3956 | 3956 | /* push collected bits to outputs */ | |
| 3957 | - | if (count != 0) { | |
| 3957 | + | ret = 0; | |
| 3958 | + | if (count != 0) | |
| 3958 | 3959 | ret = gpiochip_set_multiple(guard.gc, mask, bits); | |
| 3959 | - | if (ret) | |
| 3960 | - | return ret; | |
| 3961 | - | } | |
| 3962 | 3960 | ||
| 3963 | 3961 | if (mask != fastpath_mask) | |
| 3964 | 3962 | bitmap_free(mask); | |
| 3965 | 3963 | if (bits != fastpath_bits) | |
| 3966 | 3964 | bitmap_free(bits); | |
| 3965 | + | ||
| 3966 | + | if (ret) | |
| 3967 | + | return ret; | |
| 3967 | 3968 | } | |
| 3968 | 3969 | return 0; | |
| 3969 | 3970 | } | |
modified fs/ocfs2/dlm/dlmcommon.h
| @@ -435,8 +435,6 @@ enum { | |||
| 435 | 435 | DLM_DEREF_LOCKRES_DONE = 522, | |
| 436 | 436 | }; | |
| 437 | 437 | ||
| 438 | - | extern spinlock_t dlm_reco_state_lock; | |
| 439 | - | ||
| 440 | 438 | struct dlm_reco_node_data | |
| 441 | 439 | { | |
| 442 | 440 | int state; | |
modified fs/ocfs2/dlm/dlmdebug.c
| @@ -736,11 +736,8 @@ static int debug_state_print(struct dlm_ctxt *dlm, char *buf, int len) | |||
| 736 | 736 | buf + out, len - out); | |
| 737 | 737 | out += scnprintf(buf + out, len - out, "\n"); | |
| 738 | 738 | ||
| 739 | - | spin_unlock(&dlm->spinlock); | |
| 740 | - | ||
| 741 | 739 | /* Recovery Node State: */ | |
| 742 | 740 | out += scnprintf(buf + out, len - out, "Recovery Node State:\n"); | |
| 743 | - | spin_lock(&dlm_reco_state_lock); | |
| 744 | 741 | list_for_each_entry(node, &dlm->reco.node_data, list) { | |
| 745 | 742 | switch (node->state) { | |
| 746 | 743 | case DLM_RECO_NODE_DATA_INIT: | |
| @@ -772,7 +769,7 @@ static int debug_state_print(struct dlm_ctxt *dlm, char *buf, int len) | |||
| 772 | 769 | node->node_num, state); | |
| 773 | 770 | } | |
| 774 | 771 | ||
| 775 | - | spin_unlock(&dlm_reco_state_lock); | |
| 772 | + | spin_unlock(&dlm->spinlock); | |
| 776 | 773 | ||
| 777 | 774 | return out; | |
| 778 | 775 | } | |
modified fs/ocfs2/dlm/dlmrecovery.c
| @@ -80,7 +80,7 @@ static int dlm_lockres_master_requery(struct dlm_ctxt *dlm, | |||
| 80 | 80 | ||
| 81 | 81 | static u64 dlm_get_next_mig_cookie(void); | |
| 82 | 82 | ||
| 83 | - | DEFINE_SPINLOCK(dlm_reco_state_lock); | |
| 83 | + | static DEFINE_SPINLOCK(dlm_reco_state_lock); | |
| 84 | 84 | static DEFINE_SPINLOCK(dlm_mig_cookie_lock); | |
| 85 | 85 | static u64 dlm_mig_cookie = 1; | |
| 86 | 86 | ||
modified fs/pipe.c
| @@ -838,11 +838,9 @@ pipe_fasync(int fd, struct file *filp, int on) | |||
| 838 | 838 | if (filp->f_mode & FMODE_READ) | |
| 839 | 839 | retval = fasync_helper(fd, filp, on, &pipe->fasync_readers); | |
| 840 | 840 | if ((filp->f_mode & FMODE_WRITE) && retval >= 0) { | |
| 841 | - | int read_ret = retval; | |
| 842 | - | ||
| 843 | 841 | retval = fasync_helper(fd, filp, on, &pipe->fasync_writers); | |
| 844 | - | /* Undo only a reader entry added by this call. */ | |
| 845 | - | if (retval < 0 && read_ret > 0) | |
| 842 | + | if (retval < 0 && (filp->f_mode & FMODE_READ)) | |
| 843 | + | /* this can happen only if on == T */ | |
| 846 | 844 | fasync_helper(-1, filp, 0, &pipe->fasync_readers); | |
| 847 | 845 | } | |
| 848 | 846 | mutex_unlock(&pipe->mutex); | |
modified include/linux/sched/signal.h
| @@ -130,8 +130,8 @@ struct signal_struct { | |||
| 130 | 130 | * process will inherit a flag if they should look for a | |
| 131 | 131 | * child_subreaper process at exit. | |
| 132 | 132 | */ | |
| 133 | - | bool is_child_subreaper; | |
| 134 | - | bool has_child_subreaper; | |
| 133 | + | unsigned int is_child_subreaper:1; | |
| 134 | + | unsigned int has_child_subreaper:1; | |
| 135 | 135 | unsigned int autoreap:1; | |
| 136 | 136 | ||
| 137 | 137 | #ifdef CONFIG_POSIX_TIMERS | |
modified samples/pidfd/Makefile
| @@ -1,4 +1,4 @@ | |||
| 1 | 1 | # SPDX-License-Identifier: GPL-2.0 | |
| 2 | - | userprogs-always-y += pidfd-metadata | |
| 2 | + | usertprogs-always-y += pidfd-metadata | |
| 3 | 3 | ||
| 4 | 4 | userccflags += -I usr/include | |
modified tools/gpio/Makefile
| @@ -7,7 +7,13 @@ bindir ?= /usr/bin | |||
| 7 | 7 | # isn't set and when invoked from selftests build, where srctree | |
| 8 | 8 | # is set to ".". building_out_of_srctree is undefined for in srctree | |
| 9 | 9 | # builds | |
| 10 | + | ifeq ($(srctree),) | |
| 11 | + | update_srctree := 1 | |
| 12 | + | endif | |
| 10 | 13 | ifndef building_out_of_srctree | |
| 14 | + | update_srctree := 1 | |
| 15 | + | endif | |
| 16 | + | ifeq ($(update_srctree),1) | |
| 11 | 17 | srctree := $(patsubst %/,%,$(dir $(CURDIR))) | |
| 12 | 18 | srctree := $(patsubst %/,%,$(dir $(srctree))) | |
| 13 | 19 | endif | |