Skip to content

Commit

Permalink
kernek: don't allow mutex ops in ISRs
Browse files Browse the repository at this point in the history
Mutex operations check ownership against _current. But in an
ISR, _current is just whatever thread was interrupted when the
ISR fired. Explicitly do not allow this.

Signed-off-by: Andrew Boie <[email protected]>
  • Loading branch information
Andrew Boie authored and carlescufi committed May 27, 2020
1 parent 4cdaa71 commit 6af9793
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3610,6 +3610,8 @@ __syscall int k_mutex_init(struct k_mutex *mutex);
* A thread is permitted to lock a mutex it has already locked. The operation
* completes immediately and the lock count is increased by 1.
*
* Mutexes may not be locked in ISRs.
*
* @param mutex Address of the mutex.
* @param timeout Waiting period to lock the mutex,
* or one of the special values K_NO_WAIT and
Expand All @@ -3631,6 +3633,9 @@ __syscall int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout);
* the calling thread as many times as it was previously locked by that
* thread.
*
* Mutexes may not be unlocked in ISRs, as mutexes must only be manipulated
* in thread context due to ownership and priority inheritance semantics.
*
* @param mutex Address of the mutex.
*
* @retval 0 Mutex unlocked.
Expand Down
4 changes: 4 additions & 0 deletions kernel/mutex.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ int z_impl_k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout)
k_spinlock_key_t key;
bool resched = false;

__ASSERT(!arch_is_in_isr(), "mutexes cannot be used inside ISRs");

sys_trace_void(SYS_TRACE_ID_MUTEX_LOCK);
key = k_spin_lock(&lock);

Expand Down Expand Up @@ -211,6 +213,8 @@ int z_impl_k_mutex_unlock(struct k_mutex *mutex)
{
struct k_thread *new_owner;

__ASSERT(!arch_is_in_isr(), "mutexes cannot be used inside ISRs");

CHECKIF(mutex->owner == NULL) {
return -EINVAL;
}
Expand Down

0 comments on commit 6af9793

Please sign in to comment.