Skip to content

Commit 194684e

Browse files
Mika KuoppalaJean Delvare
authored andcommitted
i2c: Prevent priority inversion on top of bus lock
Low priority thread holding the i2c bus mutex could block higher priority threads to access the bus resulting in unacceptable latencies. Change the mutex type to rt_mutex preventing priority inversion. Tested-by: Peter Ujfalusi <peter.ujfalusi@nokia.com> Signed-off-by: Mika Kuoppala <mika.kuoppala@nokia.com> Signed-off-by: Jean Delvare <khali@linux-fr.org>
1 parent a0c11cd commit 194684e

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

‎drivers/i2c/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
menuconfig I2C
66
tristate "I2C support"
77
depends on HAS_IOMEM
8+
select RT_MUTEXES
89
---help---
910
I2C (pronounce: I-square-C) is a slow serial bus protocol used in
1011
many micro controller applications and developed by Philips. SMBus,

‎drivers/i2c/i2c-core.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
584584
goto out_list;
585585
}
586586

587-
mutex_init(&adap->bus_lock);
587+
rt_mutex_init(&adap->bus_lock);
588588

589589
/* Set default timeout to 1 second if not already set */
590590
if (adap->timeout == 0)
@@ -1092,12 +1092,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
10921092
#endif
10931093

10941094
if (in_atomic() || irqs_disabled()) {
1095-
ret = mutex_trylock(&adap->bus_lock);
1095+
ret = rt_mutex_trylock(&adap->bus_lock);
10961096
if (!ret)
10971097
/* I2C activity is ongoing. */
10981098
return -EAGAIN;
10991099
} else {
1100-
mutex_lock_nested(&adap->bus_lock, adap->level);
1100+
rt_mutex_lock(&adap->bus_lock);
11011101
}
11021102

11031103
/* Retry automatically on arbitration loss */
@@ -1109,7 +1109,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
11091109
if (time_after(jiffies, orig_jiffies + adap->timeout))
11101110
break;
11111111
}
1112-
mutex_unlock(&adap->bus_lock);
1112+
rt_mutex_unlock(&adap->bus_lock);
11131113

11141114
return ret;
11151115
} else {
@@ -1913,7 +1913,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
19131913
flags &= I2C_M_TEN | I2C_CLIENT_PEC;
19141914

19151915
if (adapter->algo->smbus_xfer) {
1916-
mutex_lock(&adapter->bus_lock);
1916+
rt_mutex_lock(&adapter->bus_lock);
19171917

19181918
/* Retry automatically on arbitration loss */
19191919
orig_jiffies = jiffies;
@@ -1927,7 +1927,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
19271927
orig_jiffies + adapter->timeout))
19281928
break;
19291929
}
1930-
mutex_unlock(&adapter->bus_lock);
1930+
rt_mutex_unlock(&adapter->bus_lock);
19311931
} else
19321932
res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
19331933
command, protocol, data);

‎include/linux/i2c.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ struct i2c_adapter {
338338
void *algo_data;
339339

340340
/* data fields that are valid for all devices */
341-
u8 level; /* nesting level for lockdep */
342-
struct mutex bus_lock;
341+
struct rt_mutex bus_lock;
343342

344343
int timeout; /* in jiffies */
345344
int retries;
@@ -367,7 +366,7 @@ static inline void i2c_set_adapdata(struct i2c_adapter *dev, void *data)
367366
*/
368367
static inline void i2c_lock_adapter(struct i2c_adapter *adapter)
369368
{
370-
mutex_lock(&adapter->bus_lock);
369+
rt_mutex_lock(&adapter->bus_lock);
371370
}
372371

373372
/**
@@ -376,7 +375,7 @@ static inline void i2c_lock_adapter(struct i2c_adapter *adapter)
376375
*/
377376
static inline void i2c_unlock_adapter(struct i2c_adapter *adapter)
378377
{
379-
mutex_unlock(&adapter->bus_lock);
378+
rt_mutex_unlock(&adapter->bus_lock);
380379
}
381380

382381
/*flags for the client struct: */

0 commit comments

Comments
 (0)