Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Bartosz Golaszewski" <bartosz.golaszewski@oss.qualcomm.com>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH 3/5] gpiolib: provide gpiod_update_flags()
Date: Wed, 04 Nov 2026 15:52:56 +0000	[thread overview]
Message-ID: <sashiko-outbox-197549@kernel.org> (raw)
In-Reply-To: <20261104-gpio-desc-flags-atomic-v1-3-e23cedf619ba@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] gpiolib: gpiod_update_flags() open-codes set_mask_bits()
--

commit ecaf66699b4205d02175949ed385d130f83c667e
Author: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>

gpiolib: provide gpiod_update_flags()

This commit adds gpiod_update_flags() in drivers/gpio/gpiolib.h to atomically
update a masked set of bits in desc->flags using a try_cmpxchg() loop so
concurrent updates to other bits are not lost.

> diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h
> index 650a702741dfb..10659ffcf2bae 100644
> --- a/drivers/gpio/gpiolib.h
> +++ b/drivers/gpio/gpiolib.h
> @@ -9,6 +9,7 @@
>  #ifndef GPIOLIB_H
>  #define GPIOLIB_H
>  
> +#include <linux/atomic.h>
>  #include <linux/cdev.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
> @@ -223,6 +224,26 @@ struct gpio_desc {
>  #endif
>  };
>  
> +/**
> + * gpiod_update_flags() - Atomically update several descriptor flags at once
> + * @desc: GPIO descriptor to update.
> + * @mask: Bits (not bit numbers) to modify.
> + * @val: New values for the bits selected by @mask.
> + *
> + * Replace the bits in @mask with the corresponding bits of @val in a single
> + * atomic step, so that no other context can observe a half-updated state of
> + * these bits. Bits outside of @mask are never rewritten with stale values, so
> + * concurrent atomic bit operations on them are preserved.
> + */
> +static inline void gpiod_update_flags(struct gpio_desc *desc,
> +				      unsigned long mask, unsigned long val)
> +{
> +	unsigned long old = READ_ONCE(desc->flags);
> +
> +	while (!try_cmpxchg(&desc->flags, &old, (old & ~mask) | (val & mask)))
> +		;
> +}

[Severity: Low]
This isn't a bug, but include/linux/bitops.h already provides set_mask_bits():

#define set_mask_bits(ptr, mask, bits)	\
({								\
	const typeof(*(ptr)) mask__ = (mask), bits__ = (bits);	\
	typeof(*(ptr)) old__, new__;				\
								\
	old__ = READ_ONCE(*(ptr));				\
	do {							\
		new__ = (old__ & ~mask__) | bits__;		\
	} while (!try_cmpxchg(ptr, &old__, new__));		\
								\
	old__;							\
})

Could gpiod_update_flags() call set_mask_bits(&desc->flags, mask, val & mask)
instead of open-coding the READ_ONCE() and try_cmpxchg() loop?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261104-gpio-desc-flags-atomic-v1-0-e23cedf619ba@oss.qualcomm.com?part=3

  reply	other threads:[~2026-11-04 15:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-04 15:36 [PATCH 0/5] gpiolib: improve concurrent access to the flags field of struct gpio_desc Bartosz Golaszewski
2026-11-04 15:36 ` [PATCH 1/5] gpiolib: don't overwrite the entire flags field in gpiod_get_direction() Bartosz Golaszewski
2026-11-04 15:36 ` [PATCH 2/5] gpio: shared: use atomic set_bit() for descriptor flags Bartosz Golaszewski
2026-11-04 15:36 ` [PATCH 3/5] gpiolib: provide gpiod_update_flags() Bartosz Golaszewski
2026-11-04 15:52   ` sashiko-bot [this message]
2026-11-05 12:59   ` Linus Walleij
2026-11-04 15:36 ` [PATCH 4/5] gpiolib: don't overwrite the entire flags field in gpiod_free_commit() Bartosz Golaszewski
2026-11-05  4:29   ` Kent Gibson
2026-11-04 15:37 ` [PATCH 5/5] gpio: cdev: don't overwrite the whole flags field when configuring lines Bartosz Golaszewski
2026-11-04 21:55   ` Andy Shevchenko
2026-11-05  4:23   ` Kent Gibson

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-197549@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bartosz.golaszewski@oss.qualcomm.com \
    --cc=linux-gpio@vger.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