mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Abd-Alrhman Masalkhi <abd.masalkhi@gmail.com>
To: wsa+renesas@sang-engineering.com
Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org,
	Abd-Alrhman Masalkhi <abd.masalkhi@gmail.com>
Subject: [PATCH] i2c: core: Serialize 10-bit client instantiation with mutex
Date: Sat, 14 Jun 2025 08:37:34 +0000	[thread overview]
Message-ID: <20250614083734.3385182-1-abd.masalkhi@gmail.com> (raw)

Add a mutex to protect against race conditions when instantiating
10-bit address I2C clients. It serves the same purpose as the 7-bit
address bitmap (addrs_in_instantiation), but uses a mutex instead,
since 10-bit clients are rare and a full bitmap would unnecessarily
increase the size of struct i2c_adapter.

Signed-off-by: Abd-Alrhman Masalkhi <abd.masalkhi@gmail.com>
---
 drivers/i2c/i2c-core-base.c | 8 +++++++-
 include/linux/i2c.h         | 3 +++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 2ad2b1838f0f..f5f53d378fff 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -929,6 +929,9 @@ int i2c_dev_irq_from_resources(const struct resource *resources,
 static int i2c_lock_addr(struct i2c_adapter *adap, unsigned short addr,
 			 unsigned short flags)
 {
+	if (flags & I2C_CLIENT_TEN)
+		mutex_lock(&adap->addrs_10bit_lock);
+
 	if (!(flags & I2C_CLIENT_TEN) &&
 	    test_and_set_bit(addr, adap->addrs_in_instantiation))
 		return -EBUSY;
@@ -939,7 +942,9 @@ static int i2c_lock_addr(struct i2c_adapter *adap, unsigned short addr,
 static void i2c_unlock_addr(struct i2c_adapter *adap, unsigned short addr,
 			    unsigned short flags)
 {
-	if (!(flags & I2C_CLIENT_TEN))
+	if (flags & I2C_CLIENT_TEN)
+		mutex_unlock(&adap->addrs_10bit_lock);
+	else
 		clear_bit(addr, adap->addrs_in_instantiation);
 }
 
@@ -1538,6 +1543,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
 	adap->locked_flags = 0;
 	rt_mutex_init(&adap->bus_lock);
 	rt_mutex_init(&adap->mux_lock);
+	mutex_init(&adap->addrs_10bit_lock);
 	mutex_init(&adap->userspace_clients_lock);
 	INIT_LIST_HEAD(&adap->userspace_clients);
 
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 20fd41b51d5c..1d4d0577b5b1 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -765,6 +765,9 @@ struct i2c_adapter {
 
 	/* 7bit address space */
 	DECLARE_BITMAP(addrs_in_instantiation, 1 << 7);
+
+	/* Lock for 10bit address instantiation */
+	struct mutex addrs_10bit_lock;
 };
 #define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
 
-- 
2.43.0


             reply	other threads:[~2025-06-14  8:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-14  8:37 Abd-Alrhman Masalkhi [this message]
2025-06-21  9:17 ` Abd-Alrhman Masalkhi

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=20250614083734.3385182-1-abd.masalkhi@gmail.com \
    --to=abd.masalkhi@gmail.com \
    --cc=linux-i2c@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wsa+renesas@sang-engineering.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

all inboxes | Powered by JetHome®