Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>,
	Michael Ellerman <mpe@ellerman.id.au>,
	Nicholas Piggin <npiggin@gmail.com>,
	"Christophe Leroy (CS GROUP)" <chleroy@kernel.org>,
	Linus Walleij <linusw@kernel.org>,
	Bartosz Golaszewski <brgl@kernel.org>,
	linuxppc-dev@lists.ozlabs.org (open list:LINUX FOR POWERPC
	(32-BIT AND 64-BIT)), linux-kernel@vger.kernel.org (open list)
Subject: [PATCHv2 7/8] gpio: ppc44x: fix undefined behavior in GPIO_MASK2 macro
Date: Mon,  3 Aug 2026 15:35:38 -0700	[thread overview]
Message-ID: <20260803223539.86303-8-rosenp@gmail.com> (raw)
In-Reply-To: <20260803223539.86303-1-rosenp@gmail.com>

Shifting a 32-bit unsigned integer by 32 or more places is undefined
behavior in C. GPIO_MASK2 computes its shift amount as (gpio) * 2, and
for pins 16-31 in the OSRH/TSRH bank this yields shifts of 32-62.
While this happens to work on PowerPC because slw masks the shift count
to the low 5 bits, compilers performing value-range propagation may
assume the else branch is unreachable and optimize it away, or may
evaluate the shift as zero on other architectures via COMPILE_TEST.

Mask gpio to the 16-pin bank index so the shift stays within [0, 30].
The registers are banked (OSRL/TSRL for gpio 0-15, OSRH/TSRH for
gpio 16-31) with an identical 2-bit-per-pin layout from MSB to LSB,
so masking to the within-bank index preserves the intended behavior.

Assisted-by: opencode:big-pickle
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/gpio/gpio-ppc44x.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-ppc44x.c b/drivers/gpio/gpio-ppc44x.c
index fd543fbb959a..9fdc84e922f4 100644
--- a/drivers/gpio/gpio-ppc44x.c
+++ b/drivers/gpio/gpio-ppc44x.c
@@ -19,7 +19,7 @@
 #include <linux/platform_device.h>
 
 #define GPIO_MASK(gpio)		(0x80000000 >> (gpio))
-#define GPIO_MASK2(gpio)	(0xc0000000 >> ((gpio) * 2))
+#define GPIO_MASK2(gpio)	(0xc0000000 >> (((gpio) % 16) * 2))
 
 /* Physical GPIO register layout */
 struct ppc44x_gpio {
-- 
2.55.0


  parent reply	other threads:[~2026-08-03 22:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03 22:35 [PATCHv2 0/8] gpio: move ppc4xx driver to drivers/gpio and modernize Rosen Penev
2026-08-03 22:35 ` [PATCHv2 1/8] gpio: move ppc4xx gpio driver from arch/powerpc to drivers/gpio Rosen Penev
2026-08-03 22:35 ` [PATCHv2 2/8] gpio: ppc44x: update all 4xx to 44x Rosen Penev
2026-08-03 22:35 ` [PATCHv2 3/8] gpio: ppc44x: Use module platform driver helper for GPIO Rosen Penev
2026-08-03 22:35 ` [PATCHv2 4/8] gpio: ppc44x: Use platform resource helper for GPIO MMIO Rosen Penev
2026-08-03 22:35 ` [PATCHv2 5/8] gpio: ppc44x: Convert GPIO to generic MMIO Rosen Penev
2026-08-03 22:35 ` [PATCHv2 6/8] gpio: ppc44x: drop PPC-specific IO helpers Rosen Penev
2026-08-03 22:35 ` Rosen Penev [this message]
2026-08-03 22:35 ` [PATCHv2 8/8] gpio: ppc44x: use dev_name() for chip label Rosen Penev
2026-08-06 22:15 ` [PATCHv2 0/8] gpio: move ppc4xx driver to drivers/gpio and modernize Linus Walleij
2026-08-07  3:54   ` Madhavan Srinivasan

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=20260803223539.86303-8-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=brgl@kernel.org \
    --cc=chleroy@kernel.org \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=maddy@linux.ibm.com \
    --cc=mpe@ellerman.id.au \
    --cc=npiggin@gmail.com \
    /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