Skip to content

Conversation

@alexanderwachter
Copy link
Member

@alexanderwachter alexanderwachter commented Jan 4, 2023

Replace all mutex with spinlocks to make the driver usable from ISRs.

Fixes #53964

@alexanderwachter
Copy link
Member Author

@cfriedt second try :-)

@alexanderwachter alexanderwachter force-pushed the gpio/emul/replaceMutex branch 2 times, most recently from 90bf3af to d7dc252 Compare January 4, 2023 12:03
@henrikbrixandersen
Copy link
Member

How will switching from a mutex to a spinlock "make the driver usable from ISRs"? If application level code holds the spinlock and an ISR attempts to acquire it, the ISR will spin forever?

@alexanderwachter
Copy link
Member Author

How will switching from a mutex to a spinlock "make the driver usable from ISRs"? If application level code holds the spinlock and an ISR attempts to acquire it, the ISR will spin forever?

How can an ISR execute if the spinlock is locked?
Do you think there is an issue with multicore?
I'm only using it in native posix, where the current solution is unusable, because our app reads the state of an input within the ISR.

Copy link
Member

@cfriedt cfriedt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK to me. I was actually not really sure if this would be possible, but I'm impressed. Just some minor changes requested to remove unnecessary changes / update docs.

I would like to take another look at the second draft as well and see if I can break it.

@alexanderwachter alexanderwachter force-pushed the gpio/emul/replaceMutex branch 3 times, most recently from 7ba1b5a to bd101e2 Compare January 9, 2023 15:03
cfriedt
cfriedt previously approved these changes Jan 17, 2023
@cfriedt cfriedt self-requested a review January 17, 2023 20:26
@cfriedt
Copy link
Member

cfriedt commented Jan 17, 2023

@alexanderwachter - I would go so far as to say that this fixes a bug. Do you know if an issue exists for that already?

Replace all mutex with spinlocks to make the driver usable
from ISRs.

Signed-off-by: Alexander Wachter <[email protected]>
@alexanderwachter
Copy link
Member Author

@cfriedt not that I'm aware of. At least not from me.
Agree, it is a bug. The API must return -EWOULDBLOCK.

@cfriedt
Copy link
Member

cfriedt commented Jan 20, 2023

@cfriedt not that I'm aware of. At least not from me.

@alexanderwachter - Oh, I meant not being able to use gpio_emul in IRQ contexts is a bug actually, so this PR is itself a Bugfix.

Can you also please create an issue and link it here?

Agree, it is a bug. The API must return -EWOULDBLOCK.

Right, I think I see what you mean.

Actually, that is part of a larger issue that we have in Zephyr. Very few Zephyr system calls have an associated timeout (including GPIO).

So there is no clean way at the moment to fail with an error if a such an operation would block.

Blocking I/O (with a timeout) would also require a mutex or something along those lines which is almost a step in the reverse direction. It would force users to incur not only the syscall overhead but also the overhead of locking a mutex. Maybe that would be negligible in the future with with zync, but we'll see.

One could also argue, much like accessing any kind of data structure, synchronizing control of a GPIO is entirely application dependent and is not needed by the large majority of GPIO users.

What you've identified is out of the scope of this PR, so I wouldn't worry about it here.

For the time being, the implicit assumption is that GPIO operations are quick and they never fail.

@alexanderwachter alexanderwachter added this to the v3.3.0 milestone Feb 3, 2023
@alexanderwachter
Copy link
Member Author

@cfriedt ready for merging?
I think this should go in 3.3.0

@cfriedt
Copy link
Member

cfriedt commented Feb 3, 2023

@alexanderwachter - yes, but I'll let @stephanosio or @laurenmurphyx64 do it once there is another review

@alexanderwachter
Copy link
Member Author

@mnkp any input?

@cfriedt
Copy link
Member

cfriedt commented Feb 7, 2023

@henrikbrixandersen - will you ack?

@stephanosio stephanosio modified the milestones: v3.3.0, v3.4.0 Feb 10, 2023
@teburd
Copy link
Contributor

teburd commented Feb 22, 2023

Is the GPIO interface labeled as ISR safe? This is kind of a confusing change to be honest.

Copy link
Contributor

@andyross andyross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks reasonable to me. Note that the "release the lock around the callback" pattern opens races if other contexts want to modify the data used by the callback while it's running. That's arguably user error, but might be worth documenting.

@carlescufi carlescufi merged commit 244f623 into zephyrproject-rtos:main Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gpio_emul: gpio_* functions not callable within an ISR

9 participants