linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] i2c: core: Serialize 10-bit client instantiation with mutex
@ 2025-06-14  8:37 Abd-Alrhman Masalkhi
  2025-06-21  9:17 ` Abd-Alrhman Masalkhi
  0 siblings, 1 reply; 2+ messages in thread
From: Abd-Alrhman Masalkhi @ 2025-06-14  8:37 UTC (permalink / raw)
  To: wsa+renesas; +Cc: linux-i2c, linux-kernel, Abd-Alrhman Masalkhi

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] i2c: core: Serialize 10-bit client instantiation with mutex
  2025-06-14  8:37 [PATCH] i2c: core: Serialize 10-bit client instantiation with mutex Abd-Alrhman Masalkhi
@ 2025-06-21  9:17 ` Abd-Alrhman Masalkhi
  0 siblings, 0 replies; 2+ messages in thread
From: Abd-Alrhman Masalkhi @ 2025-06-21  9:17 UTC (permalink / raw)
  To: abd.masalkhi; +Cc: linux-i2c, linux-kernel, wsa+renesas

Hi all,

Gentle ping.

Best regards,
Abd-Alrhman Masalkhi

On 2025/6/14 08:37, Abd-Alrhman Masalkhi wrote:
> 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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-06-21  9:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-14  8:37 [PATCH] i2c: core: Serialize 10-bit client instantiation with mutex Abd-Alrhman Masalkhi
2025-06-21  9:17 ` Abd-Alrhman Masalkhi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).