Skip to content

Commit

Permalink
hrt_ioctl: Fix issue with SMP
Browse files Browse the repository at this point in the history
The interrupt handler needs to take the spinlock as well, as another
CPU can be futzing around with the lists when another CPU takes the
HRT trap.
  • Loading branch information
pussuw authored and jlaitine committed Jan 10, 2025
1 parent a0d7d92 commit d2aece5
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions platforms/nuttx/src/px4/common/hrt_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,13 @@ static struct usr_hrt_call *dup_entry(const px4_hrt_handle_t handle, struct hrt_
e = (void *)sq_remfirst(&callout_freelist);
}

spin_unlock_irqrestore_wo_note(&g_hrt_ioctl_lock, flags);

if (!e) {
/* Allocate a new kernel side item for the user call */

e = kmm_malloc(sizeof(struct usr_hrt_call));
}

if (e) {

/* Store the user side callout function and argument to the user's handle */
entry->callout = callout;
entry->arg = arg;
Expand All @@ -165,29 +162,33 @@ static struct usr_hrt_call *dup_entry(const px4_hrt_handle_t handle, struct hrt_
e->usr_entry = entry;

/* Add this to the callout_queue list */
flags = spin_lock_irqsave_wo_note(&g_hrt_ioctl_lock);

sq_addfirst(&e->list_item, &callout_queue);
spin_unlock_irqrestore_wo_note(&g_hrt_ioctl_lock, flags);

} else {
PX4_ERR("out of memory");

}

spin_unlock_irqrestore_wo_note(&g_hrt_ioctl_lock, flags);

return e;
}

void hrt_usr_call(void *arg)
{
// This is called from hrt interrupt
struct usr_hrt_call *e = (struct usr_hrt_call *)arg;
irqstate_t flags = spin_lock_irqsave_wo_note(&g_hrt_ioctl_lock);

// Make sure the event is not already in flight
if (!entry_inlist(&callout_inflight, (sq_entry_t *)e)) {
sq_rem(&e->list_item, &callout_queue);
sq_addfirst(&e->list_item, &callout_inflight);
px4_sem_post(e->entry.callout_sem);
}

spin_unlock_irqrestore_wo_note(&g_hrt_ioctl_lock, flags);
}

int hrt_ioctl(unsigned int cmd, unsigned long arg);
Expand Down

0 comments on commit d2aece5

Please sign in to comment.