Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fil Dunsky" <filipp.dunsky@gmail.com>
Cc: linux-watchdog@vger.kernel.org, conor+dt@kernel.org,
	linux-gpio@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org
Subject: Re: [PATCH 04/11] gpio: Add Cortina Systems CS75xx GPIO driver
Date: Mon, 12 Oct 2026 17:47:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-168492@kernel.org> (raw)
In-Reply-To: <20261012-b4-cs75xx-s2-v1-4-8a7641b7b79c@gmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] gpio: cs75xx: TOCTOU race overwrites IRQ trigger hardware state
- [Medium] gpio: cs75xx: BOTH_EDGES state corruption on invalid IRQ type
- [Medium] gpio: cs75xx: missing ngpios bounds check causes OOB read and shift UB
--

commit a5885a6ba82fad811f622ae265473cbbb66fe3ae
Author: Fil Dunsky <filipp.dunsky@gmail.com>

gpio: Add Cortina Systems CS75xx GPIO driver

This commit adds a driver for the GPIO banks of the Cortina Systems CS75xx
SoCs, handling data and direction registers via gpio-mmio and providing an
interrupt controller with level and edge trigger support.

> diff --git a/drivers/gpio/gpio-cs75xx.c b/drivers/gpio/gpio-cs75xx.c
> index 0000000000000..168af0e37ac20
> --- /dev/null
> +++ b/drivers/gpio/gpio-cs75xx.c
[ ... ]
> +static int cs75xx_gpio_irq_set_type(struct irq_data *d, unsigned int type)
> +{
> +	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
> +	struct cs75xx_gpio *g = gpiochip_get_data(gc);
> +	u32 mask = BIT(irqd_to_hwirq(d));
> +	u32 lvl, edge;
> +
> +	guard(gpio_generic_lock_irqsave)(&g->chip);
> +
> +	lvl = readl(g->base + CS75XX_GPIO_LVL);
> +	edge = readl(g->base + CS75XX_GPIO_EDGE);
> +	g->both_edges &= ~mask;
> +
> +	switch (type & IRQ_TYPE_SENSE_MASK) {
[ ... ]
> +	default:
> +		return -EINVAL;
> +	}

[Severity: Medium]
Does this sequence cause a regression in the BOTH_EDGES state if an invalid
IRQ type is requested?

The both_edges bitmask is modified before validating the requested type. If
the request falls through to the default case (e.g., IRQ_TYPE_NONE) and returns
-EINVAL, the state mutation is already committed.

If the pin was previously operating in IRQ_TYPE_EDGE_BOTH mode, the interrupt
handler will no longer flip its polarity on subsequent interrupts, causing
a regression in the pin's functionality.

Could we defer modifying g->both_edges until after all validation succeeds?

[ ... ]
> +static int cs75xx_gpio_irq_reqres(struct irq_data *d)
> +{
[ ... ]
> +	guard(gpio_generic_lock_irqsave)(&g->chip);
> +	g->irq_used |= BIT(hwirq);

[Severity: Medium]
Could this result in undefined shift behavior if hwirq is 32 or greater?

If a Device Tree specifies an ngpios value greater than 32, this assignment
could invoke shift undefined behavior because BIT(hwirq) is assigned into
32-bit state variables.

While the current in-tree DT might not specify ngpios, should we clamp
gc->ngpio to 32 during probe to protect against malformed DT inputs?

[ ... ]
> +static void cs75xx_gpio_irq_handler(struct irq_desc *desc)
> +{
> +	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
> +	struct cs75xx_gpio *g = gpiochip_get_data(gc);
> +	struct irq_chip *chip = irq_desc_get_chip(desc);
> +	unsigned long pending;
> +	unsigned int offset;
> +
> +	chained_irq_enter(chip, desc);
> +
> +	pending = readl(g->base + CS75XX_GPIO_INT) &
> +		  readl(g->base + CS75XX_GPIO_IE);
> +
> +	for_each_set_bit(offset, &pending, gc->ngpio) {

[Severity: Medium]
Will this loop read out of bounds if gc->ngpio exceeds BITS_PER_LONG?

If the Device Tree specifies a larger ngpios value, iterating over a single
stack-allocated unsigned long pending with gc->ngpio could cause a stack
out-of-bounds read on 32-bit architectures (where unsigned long is 32 bits).

> +		if (g->both_edges & BIT(offset)) {
> +			guard(gpio_generic_lock)(&g->chip);
> +			cs75xx_gpio_set_polarity(g, BIT(offset));
> +		}

[Severity: High]
Can this lockless condition check expose a Time-Of-Check to Time-Of-Use
(TOCTOU) race that can overwrite the IRQ trigger hardware state?

The g->both_edges bitmask is checked locklessly before acquiring g->chip.lock.
A concurrent call to cs75xx_gpio_irq_set_type() on another CPU could:

1. Acquire the lock
2. Reconfigure the trigger type (clearing the bit in g->both_edges)
3. Update the hardware LVL and EDGE registers
4. Release the lock

The interrupt handler would then acquire the lock and execute
cs75xx_gpio_set_polarity(), which blindly reads and overwrites the LVL register
based on the current pin level. This would overwrite the new trigger
configuration just established by irq_set_type(), leaving the hardware in an
incorrect state.

Should we re-evaluate the both_edges condition after the lock is acquired, or
acquire the lock before performing the check?

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261012-b4-cs75xx-s2-v1-0-8a7641b7b79c@gmail.com?part=4

  reply	other threads:[~2026-10-12 17:47 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-12 17:32 [PATCH 00/11] Cortina Systems CS75xx: interrupt controller, GPIO, I2C and TWD watchdog Fil Dunsky via B4 Relay
2026-10-12 17:32 ` [PATCH 01/11] dt-bindings: interrupt-controller: Add Cortina Systems CS7542 peripheral interrupt controller Fil Dunsky via B4 Relay
2026-10-12 17:32 ` [PATCH 02/11] irqchip: Add Cortina Systems CS75xx " Fil Dunsky via B4 Relay
2026-10-12 17:32 ` [PATCH 03/11] dt-bindings: gpio: Add Cortina Systems CS7542 GPIO controller Fil Dunsky via B4 Relay
2026-10-12 17:32 ` [PATCH 04/11] gpio: Add Cortina Systems CS75xx GPIO driver Fil Dunsky via B4 Relay
2026-10-12 17:47   ` sashiko-bot [this message]
2026-10-12 20:32   ` Uwe Kleine-König
2026-10-12 20:57     ` Fil Dunsky
2026-10-12 17:33 ` [PATCH 05/11] dt-bindings: i2c: Add Cortina Systems CS7542 I2C controller Fil Dunsky via B4 Relay
2026-10-12 17:33 ` [PATCH 06/11] i2c: Add Cortina Systems CS75xx I2C controller driver Fil Dunsky via B4 Relay
2026-10-12 17:46   ` sashiko-bot
2026-10-12 17:33 ` [PATCH 07/11] watchdog: Add ARM TWD (MPCore private) watchdog driver Fil Dunsky via B4 Relay
2026-10-12 17:47   ` sashiko-bot
2026-10-12 17:33 ` [PATCH 08/11] ARM: dts: cortina: cs7542: Add interrupt controller, GPIO and I2C Fil Dunsky via B4 Relay
2026-10-12 17:33 ` [PATCH 09/11] ARM: dts: cortina: almondplus: Add reset button and enable I2C Fil Dunsky via B4 Relay
2026-10-12 17:33 ` [PATCH 10/11] ARM: multi_v7_defconfig: Enable Cortina Systems CS75xx GPIO, I2C and TWD watchdog Fil Dunsky via B4 Relay
2026-10-12 17:33 ` [PATCH 11/11] MAINTAINERS: Add Cortina Systems CS75xx interrupt controller, GPIO and I2C Fil Dunsky via B4 Relay
2026-10-12 18:48 ` [PATCH 00/11] Cortina Systems CS75xx: interrupt controller, GPIO, I2C and TWD watchdog Conor Dooley
2026-10-13  7:10   ` Fil Dunsky
2026-10-13 16:52     ` Conor Dooley
2026-10-13 17:36       ` Fil Dunsky

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=sashiko-outbox-168492@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=filipp.dunsky@gmail.com \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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