Skip to content

Commit 8d3cefa

Browse files
hkallweitWolfram Sang
authored andcommitted
i2c: core: Lock address during client device instantiation
Krzysztof reported an issue [0] which is caused by parallel attempts to instantiate the same I2C client device. This can happen if driver supports auto-detection, but certain devices are also instantiated explicitly. The original change isn't actually wrong, it just revealed that I2C core isn't prepared yet to handle this scenario. Calls to i2c_new_client_device() can be nested, therefore we can't use a simple mutex here. Parallel instantiation of devices at different addresses is ok, so we just have to prevent parallel instantiation at the same address. We can use a bitmap with one bit per 7-bit I2C client address, and atomic bit operations to set/check/clear bits. Now a parallel attempt to instantiate a device at the same address will result in -EBUSY being returned, avoiding the "sysfs: cannot create duplicate filename" splash. Note: This patch version includes small cosmetic changes to the Tested-by version, only functional change is that address locking is supported for slave addresses too. [0] https://lore.kernel.org/linux-i2c/[email protected]/T/#m12706546e8e2414d8f1a0dc61c53393f731685cc Fixes: caba40e ("eeprom: at24: Probe for DDR3 thermal sensor in the SPD case") Cc: [email protected] Tested-by: Krzysztof Piotr Oledzki <[email protected]> Signed-off-by: Heiner Kallweit <[email protected]> Signed-off-by: Wolfram Sang <[email protected]>
1 parent 3d16973 commit 8d3cefa

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

drivers/i2c/i2c-core-base.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,27 @@ int i2c_dev_irq_from_resources(const struct resource *resources,
915915
return 0;
916916
}
917917

918+
/*
919+
* Serialize device instantiation in case it can be instantiated explicitly
920+
* and by auto-detection
921+
*/
922+
static int i2c_lock_addr(struct i2c_adapter *adap, unsigned short addr,
923+
unsigned short flags)
924+
{
925+
if (!(flags & I2C_CLIENT_TEN) &&
926+
test_and_set_bit(addr, adap->addrs_in_instantiation))
927+
return -EBUSY;
928+
929+
return 0;
930+
}
931+
932+
static void i2c_unlock_addr(struct i2c_adapter *adap, unsigned short addr,
933+
unsigned short flags)
934+
{
935+
if (!(flags & I2C_CLIENT_TEN))
936+
clear_bit(addr, adap->addrs_in_instantiation);
937+
}
938+
918939
/**
919940
* i2c_new_client_device - instantiate an i2c device
920941
* @adap: the adapter managing the device
@@ -962,6 +983,10 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
962983
goto out_err_silent;
963984
}
964985

986+
status = i2c_lock_addr(adap, client->addr, client->flags);
987+
if (status)
988+
goto out_err_silent;
989+
965990
/* Check for address business */
966991
status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
967992
if (status)
@@ -993,6 +1018,8 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
9931018
dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
9941019
client->name, dev_name(&client->dev));
9951020

1021+
i2c_unlock_addr(adap, client->addr, client->flags);
1022+
9961023
return client;
9971024

9981025
out_remove_swnode:
@@ -1004,6 +1031,7 @@ i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *inf
10041031
dev_err(&adap->dev,
10051032
"Failed to register i2c client %s at 0x%02x (%d)\n",
10061033
client->name, client->addr, status);
1034+
i2c_unlock_addr(adap, client->addr, client->flags);
10071035
out_err_silent:
10081036
if (need_put)
10091037
put_device(&client->dev);

include/linux/i2c.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ struct i2c_adapter {
761761
struct regulator *bus_regulator;
762762

763763
struct dentry *debugfs;
764+
765+
/* 7bit address space */
766+
DECLARE_BITMAP(addrs_in_instantiation, 1 << 7);
764767
};
765768
#define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
766769

0 commit comments

Comments
 (0)