Skip to content

Commit 793e95c

Browse files
committed
hw/bus: Update sanity check for locking
Code assume that node can be locked only once and second lock is probably for another node. Lock are internally use on each transaction. Write, read, write then read transactions can be used with just one lock. spiflash driver locks node when it wants to write, however writing consist of two transactions fist command is sent (4 bytes) then another write is done with separate buffer. CS must be low during those two writes. While write could be performed successfully current implementation would require SPI controller to be unlocked between writes. This could potentially lead to SPI controller being locked for another device while CS is still down for spi flash. In fact due to sanity check spi flash driver did not work at all with any bus driver since code tried to lock device before writes and then both writes would fail due to nested lock sanity check. Now sanit check verifies that nested lock is for same device. Signed-off-by: Jerzy Kasenberg <jerzy@apache.org>
1 parent 3321a93 commit 793e95c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

hw/bus/src/bus.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,13 @@ bus_node_lock(struct os_dev *node, os_time_t timeout)
531531
#endif
532532

533533
/*
534-
* Configuration is done on 1st lock so in case we need to configure device
535-
* on nested lock it means that most likely bus device was locked for one
536-
* node and then access is done on another node which is not correct.
534+
* Configuration is done on 1st lock. For nested lock make sure that
535+
* bnode did not change.
537536
*/
538537
if (MYNEWT_VAL(OS_SCHEDULING) && os_mutex_get_level(&bdev->lock) != 1) {
538+
if (bdev->configured_for == bnode) {
539+
return 0;
540+
}
539541
(void)bus_node_unlock(node);
540542
return SYS_EACCES;
541543
}

0 commit comments

Comments
 (0)