From b40f568571cfaa2950162e27c503fffb1605026a Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:31:03 +0000 Subject: [PATCH 1/4] piolib: Really open the device with O_CLOEXEC O_CLOEXEC was being passed to open() as the mode, where it is ignored, rather than as one of the flags. As a result the PIO device was inherited by any program that the application went on to execute. Signed-off-by: Phil Elwell Co-Authored-By: Claude Mythos 5.2 --- piolib/examples/rp1sm.c | 2 +- piolib/pio_rp1.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/piolib/examples/rp1sm.c b/piolib/examples/rp1sm.c index d30e9ba..df28654 100644 --- a/piolib/examples/rp1sm.c +++ b/piolib/examples/rp1sm.c @@ -47,7 +47,7 @@ int main(int argc, const char **argv) if (argc > 2) usage(); - fd = open(devname, O_RDWR, O_CLOEXEC); + fd = open(devname, O_RDWR | O_CLOEXEC); if (fd < 0) return -errno; diff --git a/piolib/pio_rp1.c b/piolib/pio_rp1.c index 75cbfca..e1bc423 100644 --- a/piolib/pio_rp1.c +++ b/piolib/pio_rp1.c @@ -1040,7 +1040,7 @@ static int rp1_open_instance(PIO pio) RP1_PIO rp = (RP1_PIO)pio; int fd; - fd = open(rp->devname, O_RDWR, O_CLOEXEC); + fd = open(rp->devname, O_RDWR | O_CLOEXEC); if (fd < 0) return -errno; rp->fd = fd; From e16ffbaf72157687cde590a1aa7bc1970438dc8c Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:31:03 +0000 Subject: [PATCH 2/4] piolib: Check that a program fits before copying it The instructions of a program are copied into the fixed-size buffer in the ioctl arguments, but the only checks on the length of the program were parameter assertions, which are normally compiled out. A program of more than 32 instructions would therefore overflow the buffer on the stack by up to 446 bytes. Add a real check, and fail the operation if the program is too big or doesn't fit at the requested offset. Signed-off-by: Phil Elwell Co-Authored-By: Claude Mythos 5.2 --- piolib/pio_rp1.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/piolib/pio_rp1.c b/piolib/pio_rp1.c index e1bc423..c9f7f38 100644 --- a/piolib/pio_rp1.c +++ b/piolib/pio_rp1.c @@ -104,6 +104,20 @@ static inline void check_sm_mask(__unused uint mask) valid_params_if(PIO, mask < (1u << RP1_PIO_SM_COUNT)); } +/* + * The instructions are copied into a fixed-size buffer, so this has to be a + * real check, not a parameter assertion that is usually compiled out. + */ +static bool rp1_pio_program_fits(const pio_program_t *program, uint offset) +{ + if (program->length > RP1_PIO_INSTRUCTION_COUNT) + return false; + if (offset == PIO_ORIGIN_ANY) + return true; + return offset < RP1_PIO_INSTRUCTION_COUNT && + offset + program->length <= RP1_PIO_INSTRUCTION_COUNT; +} + static pio_sm_config rp1_pio_get_default_sm_config(PIO) { pio_sm_config c = { { 0 } }; @@ -303,6 +317,8 @@ static bool rp1_pio_can_add_program_at_offset(PIO pio, const pio_program_t *prog valid_params_if(PIO, offset < RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); valid_params_if(PIO, program->length <= RP1_PIO_INSTRUCTION_COUNT); valid_params_if(PIO, offset + program->length <= RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); + if (!rp1_pio_program_fits(program, offset)) + return false; if (program->origin >= 0 && (uint)program->origin != offset) return false; if (offset != PIO_ORIGIN_ANY) @@ -320,6 +336,8 @@ static uint rp1_pio_add_program_at_offset(PIO pio, const pio_program_t *program, valid_params_if(PIO, offset < RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); valid_params_if(PIO, program->length <= RP1_PIO_INSTRUCTION_COUNT); valid_params_if(PIO, offset + program->length <= RP1_PIO_INSTRUCTION_COUNT || offset == PIO_ORIGIN_ANY); + if (!rp1_pio_program_fits(program, offset)) + return PIO_ORIGIN_INVALID; if (offset != PIO_ORIGIN_ANY) args.origin = offset; memcpy(args.instrs, program->instructions, program->length * sizeof(uint16_t)); From 40ceff37967dc41bfaf3c66e825446a3c7f6a418 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:31:03 +0000 Subject: [PATCH 3/4] piolib: Rework the lifetime of the IRQ thread An IRQ thread was created for every PIO instance as soon as it was opened, and it then blocked indefinitely in the kernel, waiting for an interrupt. pio_close did nothing to stop it, and because the thread was still using the device, closing the file descriptor didn't release the device either - any state machines, programs and DMA channels remained claimed until the process exited. Should the thread ever wake up, it would then use a file descriptor number that could by then refer to a completely different file. Instead, only start the thread when the first IRQ is mapped, make it wait with a timeout so that it can notice a request to stop, and have pio_close stop it and wait for it before closing the device. Forget the file descriptor once it has been closed. While here: - don't call a NULL handler if an interrupt arrives when no handler is registered, - remove the leftover debug thread in the RP1 backend, which competed with the real IRQ thread for interrupts and discarded any it got, - initialise the handle lock statically, so that it can also protect pio_init against concurrent callers. Signed-off-by: Phil Elwell Co-Authored-By: Claude Mythos 5.2 --- piolib/pio_rp1.c | 38 +++---------------- piolib/piolib.c | 98 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 87 insertions(+), 49 deletions(-) diff --git a/piolib/pio_rp1.c b/piolib/pio_rp1.c index c9f7f38..fd49589 100644 --- a/piolib/pio_rp1.c +++ b/piolib/pio_rp1.c @@ -83,9 +83,6 @@ typedef struct rp1_pio_handle { struct pio_instance base; const char *devname; int fd; - bool irq_thread_running; - pthread_mutex_t lock; - pthread_t irq_thread; } *RP1_PIO; #define smc_to_rp1(_config, _c) rp1_pio_sm_config *_c = (rp1_pio_sm_config*)_config @@ -987,38 +984,10 @@ static void rp1_pio_interrupt_clear(PIO pio, uint pio_interrupt_num) (void)rp1_ioctl(pio, PIO_IOC_INTERRUPT_CLEAR, &args); } -static void *irq_wait_thread(void *arg) { - RP1_PIO rp = (RP1_PIO)arg; - struct rp1_pio_irq_wait_args args; - - args.timeout_ms = 0; - while (1) { - rp1_ioctl(&rp->base, PIO_IOC_IRQ_WAIT, &args); - if (!args.active_mask) - break; - printf("woke %x\n", args.active_mask); - } - - return NULL; -} - -static void rp1_pio_irq_start_thread(PIO pio) { - RP1_PIO rp = (RP1_PIO)pio; - pthread_mutex_lock(&rp->lock); - if (!rp->irq_thread_running) { - if (pthread_create(&rp->irq_thread, NULL, irq_wait_thread, rp)) - pio_panic("Failed to create irq wait thread!"); - rp->irq_thread_running = true; - } - pthread_mutex_unlock(&rp->lock); -} - static int rp1_pio_irq_claim(PIO pio) { struct rp1_pio_irq_claim_args args = { .irq_index = -1 }; (void)rp1_ioctl(pio, PIO_IOC_IRQ_CLAIM, &args); - if (args.irq_index >= 0) - rp1_pio_irq_start_thread(pio); return args.irq_index; } @@ -1048,7 +1017,10 @@ static PIO rp1_create_instance(PIO_CHIP_T *chip, uint index) pio->base.chip = chip; pio->fd = -1; pio->devname = strdup(pathbuf); - pthread_mutex_init(&pio->lock, NULL); + if (!pio->devname) { + free(pio); + return PIO_ERR(-ENOMEM); + } return &pio->base; } @@ -1069,6 +1041,8 @@ static void rp1_close_instance(PIO pio) { RP1_PIO rp = (RP1_PIO)pio; close(rp->fd); + /* Don't leave a stale descriptor that might end up referring to another file */ + rp->fd = -1; } DECLARE_PIO_CHIP(rp1) { diff --git a/piolib/piolib.c b/piolib/piolib.c index c0a9b33..f6afa38 100644 --- a/piolib/piolib.c +++ b/piolib/piolib.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,12 @@ #define PIO_MAX_INSTANCES 4 #define PIO_MAX_IRQS 16 +/* + * How often the IRQ thread checks whether it should exit, which is also how + * long pio_close may have to wait for it. + */ +#define PIO_IRQ_POLL_MS 100 + static __thread PIO __pio; struct pio_irq_handler { @@ -30,8 +37,10 @@ struct pio_irq_handler { static PIO pio_instances[PIO_MAX_INSTANCES]; static uint num_instances; -static pthread_mutex_t pio_handle_lock; +static pthread_mutex_t pio_handle_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_t pio_irq_threads[PIO_MAX_INSTANCES]; +static bool pio_irq_thread_running[PIO_MAX_INSTANCES]; +static atomic_bool pio_irq_thread_stop[PIO_MAX_INSTANCES]; static struct pio_irq_handler irq_handlers[PIO_MAX_IRQS]; static uint num_irqs; @@ -60,9 +69,15 @@ int pio_get_index(PIO pio) static void *irq_wait_thread(void *arg) { PIO pio = (PIO)arg; - - while (1) { - uint32_t active = pio->chip->pio_irq_wait(pio, 0); + int idx = pio_get_index(pio); + + /* + * Wait with a timeout, so that pio_close can stop the thread. A thread + * blocked indefinitely in the kernel would keep the device open, along + * with everything that had been claimed through it. + */ + while (!atomic_load(&pio_irq_thread_stop[idx])) { + uint32_t active = pio->chip->pio_irq_wait(pio, PIO_IRQ_POLL_MS); uint i; if (active == ~0u) break; @@ -70,8 +85,10 @@ static void *irq_wait_thread(void *arg) { uint32_t mask = (1 << i); if (active & mask) { struct pio_irq_handler *h = &irq_handlers[pio->irq_base + i]; + irq_handler_t handler = h->handler; active &= ~mask; - (h->handler)(h->context); + if (handler) + handler(h->context); } } } @@ -79,6 +96,50 @@ static void *irq_wait_thread(void *arg) { return NULL; } +/* The IRQ thread is only needed (and only started) once an IRQ is in use */ +static void pio_irq_thread_start(PIO pio) +{ + int idx = pio_get_index(pio); + + if (idx < 0) + return; + + pthread_mutex_lock(&pio_handle_lock); + if (!pio_irq_thread_running[idx]) { + atomic_store(&pio_irq_thread_stop[idx], false); + if (pthread_create(&pio_irq_threads[idx], NULL, irq_wait_thread, pio)) + pio_panic("Failed to create irq wait thread!"); + pio_irq_thread_running[idx] = true; + } + pthread_mutex_unlock(&pio_handle_lock); +} + +static void pio_irq_thread_stop_and_wait(PIO pio) +{ + int idx = pio_get_index(pio); + pthread_t thread; + bool running; + + if (idx < 0) + return; + + pthread_mutex_lock(&pio_handle_lock); + running = pio_irq_thread_running[idx]; + thread = pio_irq_threads[idx]; + pio_irq_thread_running[idx] = false; + pthread_mutex_unlock(&pio_handle_lock); + + if (!running) + return; + + atomic_store(&pio_irq_thread_stop[idx], true); + /* Closing from an IRQ handler means the thread can't wait for itself */ + if (pthread_equal(thread, pthread_self())) + pthread_detach(thread); + else + pthread_join(thread, NULL); +} + int pio_init(void) { #if LIBRARY_BUILD @@ -91,10 +152,13 @@ int pio_init(void) static bool initialised; const PIO_CHIP_T * const *p; uint i = 0; - int err; - if (initialised) + pthread_mutex_lock(&pio_handle_lock); + + if (initialised) { + pthread_mutex_unlock(&pio_handle_lock); return 0; + } num_instances = 0; num_irqs = 0; @@ -123,11 +187,8 @@ int pio_init(void) } } - err = pthread_mutex_init(&pio_handle_lock, NULL); - if (err) - return err; - initialised = true; + pthread_mutex_unlock(&pio_handle_lock); return 0; } @@ -161,16 +222,16 @@ PIO pio_open(uint idx) err = pio->chip->open_instance(pio); if (err) { + pthread_mutex_lock(&pio_handle_lock); pio->in_use = 0; + pthread_mutex_unlock(&pio_handle_lock); return PIO_ERR(err); - } else if (pio->irq_count) { - for (i = 0; i < pio->irq_count; i++) - pio->irqs[i] = -1; - - if (pthread_create(&pio_irq_threads[idx], NULL, irq_wait_thread, pio)) - pio_panic("Failed to create irq wait thread!"); } + /* The IRQ thread is started when the first IRQ is mapped */ + for (i = 0; i < pio->irq_count; i++) + pio->irqs[i] = -1; + pio_select(pio); return pio; @@ -213,6 +274,8 @@ PIO pio_open_helper(uint idx) void pio_close(PIO pio) { + /* Stop the IRQ thread first, otherwise it keeps the device open */ + pio_irq_thread_stop_and_wait(pio); pio->chip->close_instance(pio); pthread_mutex_lock(&pio_handle_lock); pio->in_use = 0; @@ -247,6 +310,7 @@ uint pio_irq_map(PIO pio, uint irq_index) { } if (pirq < 0) pio_panic("Unable to map irq"); + pio_irq_thread_start(pio); return (uint)pirq; } From f03ae7ed2e4e14c3c5f3ea885e5feff51008d226 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:31:03 +0000 Subject: [PATCH 4/4] piolib: Add some missing bounds checks pio_irq_map indexes the per-instance IRQ table with a value that was only checked by a parameter assertion, which is normally compiled out (and was off by one). Make it a real check, and also check the IRQ number returned by the kernel. Make sure that pio_init stays within the instance and IRQ handler tables, however many devices and IRQs there are, and that pio_open_helper doesn't read beyond the end of the instance table. Signed-off-by: Phil Elwell Co-Authored-By: Claude Mythos 5.2 --- piolib/piolib.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/piolib/piolib.c b/piolib/piolib.c index f6afa38..c84bc94 100644 --- a/piolib/piolib.c +++ b/piolib/piolib.c @@ -163,12 +163,18 @@ int pio_init(void) num_irqs = 0; p = start; - while (p < end) + while (p < end && num_instances < PIO_MAX_INSTANCES) { PIO_CHIP_T *chip = *p; PIO pio = chip->create_instance(chip, i); if (pio && !PIO_IS_ERR(pio)) { - int irq_count = chip->irq_count; + uint irq_count = chip->irq_count; + /* Stay within the per-instance and global IRQ tables */ + if (irq_count > NUM_PIO_IRQS) + irq_count = NUM_PIO_IRQS; + if (num_irqs + irq_count > PIO_MAX_IRQS) + irq_count = 0; + pio->irq_count = 0; if (irq_count) { pio->irq_base = num_irqs; pio->irq_count = irq_count; @@ -260,7 +266,7 @@ PIO pio_open_by_name(const char *name) PIO pio_open_helper(uint idx) { - PIO pio = pio_instances[idx]; + PIO pio = (idx < PIO_MAX_INSTANCES) ? pio_instances[idx] : NULL; if (!pio || !pio->in_use) { pio = pio_open(idx); if (PIO_IS_ERR(pio)) { @@ -300,10 +306,14 @@ void sleep_us(uint64_t us) uint pio_irq_map(PIO pio, uint irq_index) { int pirq; check_pio_param(pio); - invalid_params_if(PIO, irq_index > pio->irq_count); + /* This indexes an array, so it has to be a real check */ + if (irq_index >= pio->irq_count) + pio_panic("irq index out of range"); pirq = pio->irqs[irq_index]; if (pirq < 0) { pirq = pio->chip->pio_irq_claim(pio); + if (pirq >= (int)pio->irq_count) + pirq = -1; if (pirq >= 0) { pio->irqs[irq_index] = pirq; }