Skip to content

Commit bf41e53

Browse files
aledemersnashif
authored andcommitted
disk: disk_access: changed mutex for a spinlock
disk_access functions were called from an interupt context, thus crashing on mutexes (no sleep is allowed in interrupt context). The usage of a spinlock is permitted. The functions guarded by the spinlock are executed very quickly anyway, so the spinlock is applicable. Signed-off-by: Alexi Demers <alexi.demers@axceta.com>
1 parent 32481b6 commit bf41e53

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

subsys/disk/disk_access.c

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ LOG_MODULE_REGISTER(disk);
2222
static sys_dlist_t disk_access_list = SYS_DLIST_STATIC_INIT(&disk_access_list);
2323

2424
/* lock to protect storage layer registration */
25-
static K_MUTEX_DEFINE(mutex);
25+
static struct k_spinlock lock;
2626

2727
struct disk_info *disk_access_get_di(const char *name)
2828
{
2929
struct disk_info *disk = NULL, *itr;
3030
size_t name_len = strlen(name);
3131
sys_dnode_t *node;
32+
k_spinlock_key_t spinlock_key = k_spin_lock(&lock);
3233

33-
k_mutex_lock(&mutex, K_FOREVER);
3434
SYS_DLIST_FOR_EACH_NODE(&disk_access_list, node) {
3535
itr = CONTAINER_OF(node, struct disk_info, node);
3636

@@ -49,7 +49,7 @@ struct disk_info *disk_access_get_di(const char *name)
4949
break;
5050
}
5151
}
52-
k_mutex_unlock(&mutex);
52+
k_spin_unlock(&lock, spinlock_key);
5353

5454
return disk;
5555
}
@@ -167,52 +167,47 @@ int disk_access_ioctl(const char *pdrv, uint8_t cmd, void *buf)
167167

168168
int disk_access_register(struct disk_info *disk)
169169
{
170-
int rc = 0;
170+
k_spinlock_key_t spinlock_key;
171171

172-
k_mutex_lock(&mutex, K_FOREVER);
173172
if ((disk == NULL) || (disk->name == NULL)) {
174173
LOG_ERR("invalid disk interface!!");
175-
rc = -EINVAL;
176-
goto reg_err;
174+
return -EINVAL;
177175
}
178176

179177
if (disk_access_get_di(disk->name) != NULL) {
180178
LOG_ERR("disk interface already registered!!");
181-
rc = -EINVAL;
182-
goto reg_err;
179+
return -EINVAL;
183180
}
184181

185182
/* Initialize reference count to zero */
186183
disk->refcnt = 0U;
187184

185+
spinlock_key = k_spin_lock(&lock);
188186
/* append to the disk list */
189187
sys_dlist_append(&disk_access_list, &disk->node);
190188
LOG_DBG("disk interface(%s) registered", disk->name);
191-
reg_err:
192-
k_mutex_unlock(&mutex);
193-
return rc;
189+
k_spin_unlock(&lock, spinlock_key);
190+
return 0;
194191
}
195192

196193
int disk_access_unregister(struct disk_info *disk)
197194
{
198-
int rc = 0;
195+
k_spinlock_key_t spinlock_key;
199196

200-
k_mutex_lock(&mutex, K_FOREVER);
201197
if ((disk == NULL) || (disk->name == NULL)) {
202198
LOG_ERR("invalid disk interface!!");
203-
rc = -EINVAL;
204-
goto unreg_err;
199+
return -EINVAL;
205200
}
206201

207202
if (disk_access_get_di(disk->name) == NULL) {
208203
LOG_ERR("disk interface not registered!!");
209-
rc = -EINVAL;
210-
goto unreg_err;
204+
return -EINVAL;
211205
}
206+
207+
spinlock_key = k_spin_lock(&lock);
212208
/* remove disk node from the list */
213209
sys_dlist_remove(&disk->node);
210+
k_spin_unlock(&lock, spinlock_key);
214211
LOG_DBG("disk interface(%s) unregistered", disk->name);
215-
unreg_err:
216-
k_mutex_unlock(&mutex);
217-
return rc;
212+
return 0;
218213
}

0 commit comments

Comments
 (0)