* A question about the mutex (or not) in the kernel's I2C handling
@ 2023-11-05 12:57 Chris Green
2023-11-08 22:50 ` Stefan Lengfeld
0 siblings, 1 reply; 3+ messages in thread
From: Chris Green @ 2023-11-05 12:57 UTC (permalink / raw)
To: linux-i2c
I have spent quite a while searching in places such as stackoverflow
and in the docmentation at linux.kernel.org but I haven't found a very
conclusive answer (no to me anyway).
I have a single I2C bus connected to a single board computer
(Beaglebone Black) running Debian 11, Kernel: 5.10.168.
Is it safe to allow multiple processes to read and write various
devices on the I2C bus? This may be either different processes
accessing the same device or different processes accessing different
devices on the I2C bus.
I.e. is there a mutex (or equivalent code) in the Linux Kernel I2C
drivers that guarantees completion of one process's I2C transaction
before another one starts?
The information I have managed to find suggests that there is such a
mutex and that I don't need to make sure my processes don't try to
read/write the I2C bus simultaneously but I can't find a definitive
statement to this effect.
So, I'm sorry to ask such a 'user' question here but I would really
like a definite answer please.
--
Chris Green
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: A question about the mutex (or not) in the kernel's I2C handling
2023-11-05 12:57 A question about the mutex (or not) in the kernel's I2C handling Chris Green
@ 2023-11-08 22:50 ` Stefan Lengfeld
2023-11-09 10:50 ` Chris Green
0 siblings, 1 reply; 3+ messages in thread
From: Stefan Lengfeld @ 2023-11-08 22:50 UTC (permalink / raw)
To: Chris Green; +Cc: linux-i2c
Hi Chris,
On Sun, Nov 05, 2023 at 12:57:31PM +0000, Chris Green wrote:
> Is it safe to allow multiple processes to read and write various
> devices on the I2C bus? This may be either different processes
> accessing the same device or different processes accessing different
> devices on the I2C bus.
Yes, it's save. Different processes and different in kernel users like
drivers can access the I2C bus at the same time. The I2C transfers will
not interfere with each other. They are serialized by the kernel and
execute one after another.
> I.e. is there a mutex (or equivalent code) in the Linux Kernel I2C
> drivers that guarantees completion of one process's I2C transaction
> before another one starts?
Yes, exactly. The mutex is called 'bus_lock' and can be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/i2c.h?h=v6.6&id=ffc253263a1375a65fa6c9f62a893e9767fbebfa#n727
And here the adapter/bus is locked before a transfer starts:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/i2c/i2c-core-base.c?h=v6.6&id=ffc253263a1375a65fa6c9f62a893e9767fbebfa#n2279
> The information I have managed to find suggests that there is such a
> mutex and that I don't need to make sure my processes don't try to
> read/write the I2C bus simultaneously but I can't find a definitive
> statement to this effect.
So as the I2C bus is concerned, everything is safe. The I2C read/writes
are serialized. But logically there maybe issues. E.g. if two processes
or driver writes to the same I2C register of the same device at the same
time. You will not know which transfer was the last one. So you don't
know which write has won.
So you can have race conditions on a logical level.
Kind regards,
Stefan
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: A question about the mutex (or not) in the kernel's I2C handling
2023-11-08 22:50 ` Stefan Lengfeld
@ 2023-11-09 10:50 ` Chris Green
0 siblings, 0 replies; 3+ messages in thread
From: Chris Green @ 2023-11-09 10:50 UTC (permalink / raw)
To: linux-i2c
On Wed, Nov 08, 2023 at 11:50:28PM +0100, Stefan Lengfeld wrote:
[snip long, comprehensive, reply]
Stefan, thank you so much, that's just what I wanted to be sure about.
--
Chris Green
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-11-09 11:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-05 12:57 A question about the mutex (or not) in the kernel's I2C handling Chris Green
2023-11-08 22:50 ` Stefan Lengfeld
2023-11-09 10:50 ` Chris Green
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).