* [PATCH RESEND] i2c: i2c-gpio: Enhance driver for buses with shared SDA
@ 2026-12-02 12:37 Anatoliy Oleinikov via B4 Relay
2026-12-02 12:51 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Anatoliy Oleinikov via B4 Relay @ 2026-12-02 12:37 UTC (permalink / raw)
To: Andi Shyti, linux-i2c
Cc: Wolfram Sang, Markus Stockhausen, Linus Walleij,
Bartosz Golaszewski, linux-gpio, linux-kernel, Anatoliy Oleinikov
From: Anatoliy Oleinikov <admin@wizle.tech>
Some Realtek based switches share the data line instead of the clock
line among the I2C buses of their SFP cages. E.g. the Acorid
GLS7700-24F8G-4X drives 24 SFP cages through three RTL8231 GPIO
expanders. Each cage has its own SCL, while every four cages share
one SDA.
Generalize the shared SCL handling so that it covers both lines.
- Turn the shared SCL structure and its helpers into line based ones.
The connection id, the index into the legacy "gpios" property and
the lockdep class are passed in. SDA is detected, requested and
reference counted exactly like SCL: by the phandle and all
additional cells of its devicetree reference.
- A pin can be shared as SCL or as SDA, but not as both. Such a
devicetree is rejected with -EINVAL.
- The custom lock_ops take the adapter bus lock, the SCL mutex and
then the SDA mutex, and release them in reverse order. This
serializes all buses that share either line. The SCL and SDA
mutexes have separate lockdep classes and are always taken in the
same order, so the nesting cannot deadlock.
- Skip the i2c-algo-bit bus test when SDA can be shared, as it
already is for SCL. The test toggles the lines without the bus lock.
Buses without a shared SDA behave as before. Their SDA mutex is
private to the adapter and never contended.
Tested on the Acorid GLS7700-24F8G-4X with an OpenWrt 6.18 kernel that
carries the shared SCL backport and this change, with kzalloc() for
kzalloc_obj(): the EEPROM of a module read the same 50 times in a row
while the other three cages on its SDA were probed in parallel.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Anatoliy Oleinikov <admin@wizle.tech>
---
The first submission did not reach the lists: the mail relay it went
through is on a blocklist that kernel.org uses.
---
drivers/i2c/busses/i2c-gpio.c | 189 +++++++++++++++++++++++++-----------------
1 file changed, 112 insertions(+), 77 deletions(-)
diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
index 56d6847cbf6d..40d7d0178af8 100644
--- a/drivers/i2c/busses/i2c-gpio.c
+++ b/drivers/i2c/busses/i2c-gpio.c
@@ -18,12 +18,18 @@
#include <linux/property.h>
#include <linux/slab.h>
-static LIST_HEAD(i2c_gpio_scl_list);
-static DEFINE_MUTEX(i2c_gpio_scl_list_lock);
+static LIST_HEAD(i2c_gpio_line_list);
+static DEFINE_MUTEX(i2c_gpio_line_list_lock);
static struct lock_class_key i2c_gpio_scl_lock_key;
+static struct lock_class_key i2c_gpio_sda_lock_key;
-struct i2c_gpio_scl_data {
+/*
+ * An SCL or SDA line, possibly shared by several buses. A transfer holds the lock of its SCL
+ * line and then that of its SDA line, so buses sharing either line are serialized.
+ */
+struct i2c_gpio_line_data {
struct fwnode_reference_args args;
+ const char *con_id;
struct gpio_desc *gpio;
struct rt_mutex lock;
struct list_head list;
@@ -31,8 +37,8 @@ struct i2c_gpio_scl_data {
};
struct i2c_gpio_private_data {
- struct gpio_desc *sda;
- struct i2c_gpio_scl_data *scl;
+ struct i2c_gpio_line_data *sda;
+ struct i2c_gpio_line_data *scl;
struct i2c_adapter adap;
struct i2c_algo_bit_data bit_data;
struct i2c_gpio_platform_data pdata;
@@ -57,7 +63,7 @@ static void i2c_gpio_setsda_val(void *data, int state)
{
struct i2c_gpio_private_data *priv = data;
- gpiod_set_value_cansleep(priv->sda, state);
+ gpiod_set_value_cansleep(priv->sda->gpio, state);
}
/*
@@ -77,7 +83,7 @@ static int i2c_gpio_getsda(void *data)
{
struct i2c_gpio_private_data *priv = data;
- return gpiod_get_value_cansleep(priv->sda);
+ return gpiod_get_value_cansleep(priv->sda->gpio);
}
static int i2c_gpio_getscl(void *data)
@@ -89,17 +95,28 @@ static int i2c_gpio_getscl(void *data)
static void i2c_gpio_lock_bus(struct i2c_adapter *adap, unsigned int flags)
{
+ struct i2c_gpio_private_data *priv = adap_to_priv(adap);
+
/* Take care about adapter lock. See i2c_adapter_lock_bus() and others. */
rt_mutex_lock_nested(&adap->bus_lock, i2c_adapter_depth(adap));
- rt_mutex_lock(&adap_to_priv(adap)->scl->lock);
+ rt_mutex_lock(&priv->scl->lock);
+ rt_mutex_lock(&priv->sda->lock);
}
static int i2c_gpio_trylock_bus(struct i2c_adapter *adap, unsigned int flags)
{
+ struct i2c_gpio_private_data *priv = adap_to_priv(adap);
+
if (!rt_mutex_trylock(&adap->bus_lock))
return 0;
- if (!rt_mutex_trylock(&adap_to_priv(adap)->scl->lock)) {
+ if (!rt_mutex_trylock(&priv->scl->lock)) {
+ rt_mutex_unlock(&adap->bus_lock);
+ return 0;
+ }
+
+ if (!rt_mutex_trylock(&priv->sda->lock)) {
+ rt_mutex_unlock(&priv->scl->lock);
rt_mutex_unlock(&adap->bus_lock);
return 0;
}
@@ -109,7 +126,10 @@ static int i2c_gpio_trylock_bus(struct i2c_adapter *adap, unsigned int flags)
static void i2c_gpio_unlock_bus(struct i2c_adapter *adap, unsigned int flags)
{
- rt_mutex_unlock(&adap_to_priv(adap)->scl->lock);
+ struct i2c_gpio_private_data *priv = adap_to_priv(adap);
+
+ rt_mutex_unlock(&priv->sda->lock);
+ rt_mutex_unlock(&priv->scl->lock);
rt_mutex_unlock(&adap->bus_lock);
}
@@ -358,8 +378,8 @@ static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
int ret;
/*
- * Don't use resource-managed functions. SCL may be shared across adapters and has
- * its own lifetime management. SDA uses the same path for consistency.
+ * Don't use resource-managed functions. SCL and SDA may be shared across adapters and
+ * have their own lifetime management.
*/
retdesc = gpiod_get(dev, con_id, gflags);
if (!IS_ERR(retdesc)) {
@@ -389,26 +409,30 @@ static struct gpio_desc *i2c_gpio_get_desc(struct device *dev,
return retdesc;
}
-static struct i2c_gpio_scl_data *i2c_gpio_create_scl(struct device *dev)
+static struct i2c_gpio_line_data *i2c_gpio_create_line(struct device *dev, const char *con_id,
+ unsigned int index,
+ struct lock_class_key *key)
{
struct fwnode_handle *fwnode = dev_fwnode(dev);
+ char propname[sizeof("scl-gpios")];
struct fwnode_reference_args args;
- struct i2c_gpio_scl_data *scl;
+ struct i2c_gpio_line_data *line;
bool sharable = false;
int ret;
/*
- * SCL gpios can be shared if they are defined in the devicetree and have at least one
- * cell in addition to the phandle. This is usually the pin. To support complex designs
+ * SCL and SDA gpios can be shared if they are defined in the devicetree and have at least
+ * one cell in addition to the phandle. This is usually the pin. To support complex designs
* with arbitrary of_xlate() mappings handle the node cells opaque.
*/
if (fwnode) {
- ret = fwnode_property_get_reference_args(fwnode, "scl-gpios",
+ snprintf(propname, sizeof(propname), "%s-gpios", con_id);
+ ret = fwnode_property_get_reference_args(fwnode, propname,
"#gpio-cells", 0, 0, &args);
if (ret)
/* try the ancient way */
ret = fwnode_property_get_reference_args(fwnode, "gpios",
- "#gpio-cells", 0, 1, &args);
+ "#gpio-cells", 0, index, &args);
if (!ret) {
if (args.nargs >= 1)
@@ -418,34 +442,35 @@ static struct i2c_gpio_scl_data *i2c_gpio_create_scl(struct device *dev)
}
}
- scl = kzalloc_obj(*scl);
- if (!scl) {
+ line = kzalloc_obj(*line);
+ if (!line) {
if (sharable)
fwnode_handle_put(args.fwnode);
return ERR_PTR(-ENOMEM);
}
if (sharable) {
- scl->args.fwnode = args.fwnode;
- scl->args.nargs = args.nargs;
- memcpy(scl->args.args, args.args, sizeof(args.args[0]) * args.nargs);
+ line->args.fwnode = args.fwnode;
+ line->args.nargs = args.nargs;
+ memcpy(line->args.args, args.args, sizeof(args.args[0]) * args.nargs);
}
- rt_mutex_init(&scl->lock);
- lockdep_set_class(&scl->lock, &i2c_gpio_scl_lock_key);
- refcount_set(&scl->ref, 1);
+ line->con_id = con_id;
+ rt_mutex_init(&line->lock);
+ lockdep_set_class(&line->lock, key);
+ refcount_set(&line->ref, 1);
- return scl;
+ return line;
}
-static void i2c_gpio_free_scl(struct i2c_gpio_scl_data *scl)
+static void i2c_gpio_free_line(struct i2c_gpio_line_data *line)
{
- if (scl->args.fwnode)
- fwnode_handle_put(scl->args.fwnode);
- kfree(scl);
+ if (line->args.fwnode)
+ fwnode_handle_put(line->args.fwnode);
+ kfree(line);
}
-static bool i2c_gpio_scl_matches(struct i2c_gpio_scl_data *a, struct i2c_gpio_scl_data *b)
+static bool i2c_gpio_line_matches(struct i2c_gpio_line_data *a, struct i2c_gpio_line_data *b)
{
if (!a->args.fwnode || !b->args.fwnode ||
a->args.fwnode != b->args.fwnode || a->args.nargs != b->args.nargs)
@@ -455,66 +480,76 @@ static bool i2c_gpio_scl_matches(struct i2c_gpio_scl_data *a, struct i2c_gpio_sc
}
/*
- * Look up an existing or create a new shared SCL structure. Optimistic setup sequence always
- * creates and tries to add a new entry to the list. This uses minimum locking and afterwards
- * requests the GPIO without a lock held. Concurrent probes for the same SCL pin see the entry
- * and do not race into a second gpiod_get(). Until everything is setup they terminate with
- * -EPROBE_DEFER.
+ * Look up an existing or create a new shared SCL or SDA structure. Optimistic setup sequence
+ * always creates and tries to add a new entry to the list. This uses minimum locking and
+ * afterwards requests the GPIO without a lock held. Concurrent probes for the same pin see the
+ * entry and do not race into a second gpiod_get(). Until everything is setup they terminate with
+ * -EPROBE_DEFER. A pin can be shared as SCL or as SDA, but not as both.
*/
-static struct i2c_gpio_scl_data *i2c_gpio_lookup_scl(struct device *dev, enum gpiod_flags gflags)
+static struct i2c_gpio_line_data *i2c_gpio_lookup_line(struct device *dev, const char *con_id,
+ unsigned int index,
+ struct lock_class_key *key,
+ enum gpiod_flags gflags)
{
- struct i2c_gpio_scl_data *scl, *new_scl;
+ struct i2c_gpio_line_data *line, *new_line;
struct gpio_desc *gpio;
- new_scl = i2c_gpio_create_scl(dev);
- if (IS_ERR(new_scl))
- return new_scl;
+ new_line = i2c_gpio_create_line(dev, con_id, index, key);
+ if (IS_ERR(new_line))
+ return new_line;
- scoped_guard(mutex, &i2c_gpio_scl_list_lock) {
- list_for_each_entry(scl, &i2c_gpio_scl_list, list) {
- if (!i2c_gpio_scl_matches(scl, new_scl))
+ scoped_guard(mutex, &i2c_gpio_line_list_lock) {
+ list_for_each_entry(line, &i2c_gpio_line_list, list) {
+ if (!i2c_gpio_line_matches(line, new_line))
continue;
- i2c_gpio_free_scl(new_scl);
- if (!scl->gpio)
+ i2c_gpio_free_line(new_line);
+ if (strcmp(line->con_id, con_id)) {
+ dev_err(dev, "%s pin (%pfwP %llu) already used as %s\n", con_id,
+ line->args.fwnode, line->args.args[0], line->con_id);
+ return ERR_PTR(-EINVAL);
+ }
+
+ if (!line->gpio)
return ERR_PTR(-EPROBE_DEFER);
- refcount_inc(&scl->ref);
- if (scl->args.fwnode)
- dev_dbg(dev, "reusing shared SCL (%pfwP)\n", scl->args.fwnode);
+ refcount_inc(&line->ref);
+ if (line->args.fwnode)
+ dev_dbg(dev, "reusing shared %s (%pfwP)\n", con_id,
+ line->args.fwnode);
- return scl;
+ return line;
}
- list_add(&new_scl->list, &i2c_gpio_scl_list);
+ list_add(&new_line->list, &i2c_gpio_line_list);
}
- gpio = i2c_gpio_get_desc(dev, "scl", 1, gflags);
+ gpio = i2c_gpio_get_desc(dev, con_id, index, gflags);
if (IS_ERR(gpio)) {
- scoped_guard(mutex, &i2c_gpio_scl_list_lock)
- list_del(&new_scl->list);
- i2c_gpio_free_scl(new_scl);
+ scoped_guard(mutex, &i2c_gpio_line_list_lock)
+ list_del(&new_line->list);
+ i2c_gpio_free_line(new_line);
return ERR_CAST(gpio);
}
- scoped_guard(mutex, &i2c_gpio_scl_list_lock)
- new_scl->gpio = gpio;
+ scoped_guard(mutex, &i2c_gpio_line_list_lock)
+ new_line->gpio = gpio;
- if (new_scl->args.fwnode)
- dev_dbg(dev, "registered shared SCL (%pfwP)\n", new_scl->args.fwnode);
+ if (new_line->args.fwnode)
+ dev_dbg(dev, "registered shared %s (%pfwP)\n", con_id, new_line->args.fwnode);
- return new_scl;
+ return new_line;
}
-static void i2c_gpio_cleanup_scl(struct i2c_gpio_scl_data *scl)
+static void i2c_gpio_cleanup_line(struct i2c_gpio_line_data *line)
{
- if (!refcount_dec_and_mutex_lock(&scl->ref, &i2c_gpio_scl_list_lock))
+ if (!refcount_dec_and_mutex_lock(&line->ref, &i2c_gpio_line_list_lock))
return;
- list_del(&scl->list);
- mutex_unlock(&i2c_gpio_scl_list_lock);
- gpiod_put(scl->gpio);
- i2c_gpio_free_scl(scl);
+ list_del(&line->list);
+ mutex_unlock(&i2c_gpio_line_list_lock);
+ gpiod_put(line->gpio);
+ i2c_gpio_free_line(line);
}
static int i2c_gpio_probe(struct platform_device *pdev)
@@ -559,7 +594,7 @@ static int i2c_gpio_probe(struct platform_device *pdev)
gflags = GPIOD_OUT_HIGH;
else
gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
- priv->sda = i2c_gpio_get_desc(dev, "sda", 0, gflags);
+ priv->sda = i2c_gpio_lookup_line(dev, "sda", 0, &i2c_gpio_sda_lock_key, gflags);
if (IS_ERR(priv->sda))
return PTR_ERR(priv->sda);
@@ -567,18 +602,18 @@ static int i2c_gpio_probe(struct platform_device *pdev)
gflags = GPIOD_OUT_HIGH;
else
gflags = GPIOD_OUT_HIGH_OPEN_DRAIN;
- priv->scl = i2c_gpio_lookup_scl(dev, gflags);
+ priv->scl = i2c_gpio_lookup_line(dev, "scl", 1, &i2c_gpio_scl_lock_key, gflags);
if (IS_ERR(priv->scl)) {
ret = PTR_ERR(priv->scl);
goto err_cleanup_sda;
}
- if (gpiod_cansleep(priv->sda) || gpiod_cansleep(priv->scl->gpio))
+ if (gpiod_cansleep(priv->sda->gpio) || gpiod_cansleep(priv->scl->gpio))
dev_warn(dev, "Slow GPIO pins might wreak havoc into I2C/SMBus bus timing");
else
bit_data->can_do_atomic = true;
- bit_data->skip_bit_test = !!priv->scl->args.fwnode;
+ bit_data->skip_bit_test = priv->scl->args.fwnode || priv->sda->args.fwnode;
bit_data->setsda = i2c_gpio_setsda_val;
bit_data->setscl = i2c_gpio_setscl_val;
@@ -607,7 +642,7 @@ static int i2c_gpio_probe(struct platform_device *pdev)
else
snprintf(adap->name, sizeof(adap->name), "i2c-gpio%d", pdev->id);
- /* Always use shared SCL aware locking */
+ /* Always use shared SCL and SDA aware locking */
adap->lock_ops = &i2c_gpio_lock_ops;
adap->algo_data = bit_data;
adap->class = I2C_CLASS_HWMON;
@@ -627,7 +662,7 @@ static int i2c_gpio_probe(struct platform_device *pdev)
* from the descriptor, then provide that instead.
*/
dev_info(dev, "using lines %u (SDA) and %u (SCL%s)\n",
- desc_to_gpio(priv->sda), desc_to_gpio(priv->scl->gpio),
+ desc_to_gpio(priv->sda->gpio), desc_to_gpio(priv->scl->gpio),
pdata->scl_is_output_only
? ", no clock stretching" : "");
@@ -636,9 +671,9 @@ static int i2c_gpio_probe(struct platform_device *pdev)
return 0;
err_cleanup_scl:
- i2c_gpio_cleanup_scl(priv->scl);
+ i2c_gpio_cleanup_line(priv->scl);
err_cleanup_sda:
- gpiod_put(priv->sda);
+ i2c_gpio_cleanup_line(priv->sda);
return ret;
}
@@ -652,8 +687,8 @@ static void i2c_gpio_remove(struct platform_device *pdev)
adap = &priv->adap;
i2c_del_adapter(adap);
- i2c_gpio_cleanup_scl(priv->scl);
- gpiod_put(priv->sda);
+ i2c_gpio_cleanup_line(priv->scl);
+ i2c_gpio_cleanup_line(priv->sda);
}
static const struct of_device_id i2c_gpio_dt_ids[] = {
---
base-commit: 177501be260dae21d8b095a4ec4c98714a4d9c68
change-id: 20261202-i2c-gpio-shared-sda-afbd172a9c74
Best regards,
--
Anatoliy Oleinikov <admin@wizle.tech>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH RESEND] i2c: i2c-gpio: Enhance driver for buses with shared SDA
2026-12-02 12:37 [PATCH RESEND] i2c: i2c-gpio: Enhance driver for buses with shared SDA Anatoliy Oleinikov via B4 Relay
@ 2026-12-02 12:51 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 12:51 UTC (permalink / raw)
To: Anatoliy Oleinikov; +Cc: linux-gpio
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] i2c: gpio: race in i2c_gpio_cleanup_line() unlocking before
gpiod_put()
--
commit dfec81ba8dfe460cb6648555affaacf28b81ef65
Author: Anatoliy Oleinikov <admin@wizle.tech>
i2c: i2c-gpio: Enhance driver for buses with shared SDA
This commit generalizes the shared SCL line tracking and locking in
i2c-gpio to also support sharing the SDA line across multiple I2C
adapters.
> diff --git a/drivers/i2c/busses/i2c-gpio.c b/drivers/i2c/busses/i2c-gpio.c
> index 56d6847cbf6d7..40d7d0178af86 100644
> --- a/drivers/i2c/busses/i2c-gpio.c
> +++ b/drivers/i2c/busses/i2c-gpio.c
[ ... ]
> @@ -455,66 +480,76 @@ static bool i2c_gpio_scl_matches(struct i2c_gpio_scl_data *a, struct i2c_gpio_sc
[ ... ]
> -static void i2c_gpio_cleanup_scl(struct i2c_gpio_scl_data *scl)
> +static void i2c_gpio_cleanup_line(struct i2c_gpio_line_data *line)
> {
> - if (!refcount_dec_and_mutex_lock(&scl->ref, &i2c_gpio_scl_list_lock))
> + if (!refcount_dec_and_mutex_lock(&line->ref, &i2c_gpio_line_list_lock))
> return;
>
> - list_del(&scl->list);
> - mutex_unlock(&i2c_gpio_scl_list_lock);
> - gpiod_put(scl->gpio);
> - i2c_gpio_free_scl(scl);
> + list_del(&line->list);
> + mutex_unlock(&i2c_gpio_line_list_lock);
> + gpiod_put(line->gpio);
> + i2c_gpio_free_line(line);
> }
[Severity: Medium]
Can a concurrent i2c_gpio_lookup_line() fail with -EBUSY if it runs
between mutex_unlock(&i2c_gpio_line_list_lock) and gpiod_put(line->gpio)?
With both SDA and SCL now going through i2c_gpio_lookup_line(), a probe
that succeeds on SDA and hits -EPROBE_DEFER on SCL unwinds via
err_cleanup_sda -> i2c_gpio_cleanup_line(priv->sda). If another adapter
sharing that SDA pin probes at the same time, the following interleaving
can occur:
CPU0 CPU1
i2c_gpio_probe() [adapter A] i2c_gpio_probe() [adapter B]
priv->sda = i2c_gpio_lookup_line()
priv->scl = i2c_gpio_lookup_line()
-> -EPROBE_DEFER
i2c_gpio_cleanup_line(priv->sda)
refcount_dec_and_mutex_lock()
list_del(&line->list)
mutex_unlock(&i2c_gpio_line_list_lock)
i2c_gpio_lookup_line("sda", ...)
lock(&i2c_gpio_line_list_lock)
// misses entry, adds new_line
unlock(&i2c_gpio_line_list_lock)
i2c_gpio_get_desc()
gpiod_get() -> -EBUSY
gpiod_put(line->gpio)
Because the GPIO descriptor still has FLAG_REQUESTED set until
gpiod_put() finishes, adapter B fails permanently with -EBUSY instead of
deferring probe. Calling gpiod_put(line->gpio) before removing the entry
and unlocking i2c_gpio_line_list_lock (or clearing line->gpio under the
lock before dropping it for gpiod_put()) would avoid this race.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-i2c-gpio-shared-sda-v1-1-8b411d498d14@wizle.tech?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-02 12:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 12:37 [PATCH RESEND] i2c: i2c-gpio: Enhance driver for buses with shared SDA Anatoliy Oleinikov via B4 Relay
2026-12-02 12:51 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox