From c5191256e364524a4a1cb7298ccce8dfd460bf66 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 10:58:49 +0000 Subject: [PATCH 1/7] misc: rp1-pio: Don't copy back more than the ioctl argument size A positive return value from an ioctl handler is used as the number of bytes of the argument buffer to copy back to the caller. However, some handlers return a value rather than a length - READ_HW returns the number of bytes read (up to 127 for a 16-byte argument), and SM_CLAIM returns the index of the claimed state machine (up to 3 for a 2-byte argument). In those cases the copy runs beyond the part of the on-stack argument buffer that was initialised from the caller's arguments, leaking uninitialised kernel stack and overwriting user memory beyond the argument structure. Limit the copy to the size of the ioctl argument. Co-Authored-By: Claude Mythos 5.2 Signed-off-by: Phil Elwell --- drivers/misc/rp1-pio.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/misc/rp1-pio.c b/drivers/misc/rp1-pio.c index 2dada1f5407301..cde3c2f758748a 100644 --- a/drivers/misc/rp1-pio.c +++ b/drivers/misc/rp1-pio.c @@ -2018,7 +2018,12 @@ static long rp1_pio_ioctl(struct file *filp, unsigned int ioctl_num, ret = (hdlr->func)(client, argbuf); dev_dbg(dev, "%s: %s -> %d\n", __func__, hdlr->name, ret); if (ret > 0) { - if (copy_to_user(argp, argbuf, ret)) + /* + * A positive result is the number of bytes of the arguments + * to return, but some handlers return a value instead (an SM + * index, a byte count), so never copy more than was passed in. + */ + if (copy_to_user(argp, argbuf, min(ret, sz))) ret = -EFAULT; } else if (ret == -ERESTARTSYS && hdlr->restartable) { /* Pass back the progress, so that a restart resumes from there */ From c1f347b912d7bed4bc73d75f077d8aca7303e7b7 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:00:41 +0000 Subject: [PATCH 2/7] misc: rp1-pio: Validate the parameters passed to the firmware The RP1 firmware is built without parameter assertions, so it uses state machine indices, state machine masks, GPIO numbers and interrupt numbers without checking them, and the driver passes the values from user space through unchanged. As a result, an out-of-range value can: - hang the firmware (SM_DRAIN_TX on a non-existent state machine never sees its TX FIFO empty, and the loop runs in the mailbox interrupt handler), - read and write registers outside the PIO block, such as the RIO registers that follow it (SM_PUT, SM_GET, SM_SET_CONFIG, SM_EXEC and SM_SET_DMACTRL index register arrays by state machine), - reconfigure pins in banks 1 and 2, which PIO can't even drive, - modify the firmware memory after its state machine claim mask. Reject them in the driver instead, along with zero-length programs (for which the mask calculations are undefined, and which can end up giving a client ownership of other clients' instructions), GPIO function numbers that would spill into other fields of the GPIO control register, and hardware accesses where the address range wraps. Co-Authored-By: Claude Mythos 5.2 Signed-off-by: Phil Elwell --- drivers/misc/rp1-pio.c | 119 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 4 deletions(-) diff --git a/drivers/misc/rp1-pio.c b/drivers/misc/rp1-pio.c index cde3c2f758748a..96331c316084c4 100644 --- a/drivers/misc/rp1-pio.c +++ b/drivers/misc/rp1-pio.c @@ -41,7 +41,10 @@ #define DRIVER_NAME "rp1-pio" #define RP1_PIO_SMS_COUNT 4 +#define RP1_PIO_SM_MASK ((1 << RP1_PIO_SMS_COUNT) - 1) #define RP1_PIO_INSTR_COUNT 32 +#define RP1_PIO_INTERRUPT_COUNT 8 +#define RP1_GPIO_FUNC_MAX 0x1f #define MAX_ARG_SIZE 256 @@ -221,6 +224,10 @@ static int rp1_pio_read_hw(struct rp1_pio_client *client, void *param) struct rp1_pio_device *pio = client->pio; struct rp1_access_hw_args *args = param; + /* Don't let the address range wrap */ + if (args->addr + args->len < args->addr) + return -EINVAL; + return rp1_pio_message_resp(pio, READ_HW, args, 8, NULL, args->data, args->len); } @@ -232,7 +239,13 @@ static int rp1_pio_write_hw(struct rp1_pio_client *client, void *param) uint32_t write_buf[32 + 1]; int len; + if (pio->fw_pio_count <= WRITE_HW) + return -EOPNOTSUPP; + len = min(args->len, sizeof(write_buf) - 4); + /* Don't let the address range wrap */ + if (args->addr + len < args->addr) + return -EINVAL; write_buf[0] = args->addr; if (copy_from_user(&write_buf[1], args->data, len)) return -EFAULT; @@ -272,7 +285,7 @@ int rp1_pio_can_add_program(struct rp1_pio_client *client, void *param) struct rp1_pio_device *pio = client->pio; int offset; - if (args->num_instrs > RP1_PIO_INSTR_COUNT || + if (!args->num_instrs || args->num_instrs > RP1_PIO_INSTR_COUNT || ((args->origin != RP1_PIO_ORIGIN_ANY) && (args->origin >= RP1_PIO_INSTR_COUNT || ((args->origin + args->num_instrs) > RP1_PIO_INSTR_COUNT)))) @@ -297,7 +310,7 @@ int rp1_pio_add_program(struct rp1_pio_client *client, void *param) int offset; uint i; - if (args->num_instrs > RP1_PIO_INSTR_COUNT || + if (!args->num_instrs || args->num_instrs > RP1_PIO_INSTR_COUNT || ((args->origin != RP1_PIO_ORIGIN_ANY) && (args->origin >= RP1_PIO_INSTR_COUNT || ((args->origin + args->num_instrs) > RP1_PIO_INSTR_COUNT)))) @@ -356,7 +369,7 @@ int rp1_pio_remove_program(struct rp1_pio_client *client, void *param) uint32_t used_mask; int ret = -ENODEV; - if (args->num_instrs > RP1_PIO_INSTR_COUNT || + if (!args->num_instrs || args->num_instrs > RP1_PIO_INSTR_COUNT || args->origin >= RP1_PIO_INSTR_COUNT || (args->origin + args->num_instrs) > RP1_PIO_INSTR_COUNT) return -EINVAL; @@ -391,12 +404,15 @@ int rp1_pio_sm_claim(struct rp1_pio_client *client, void *param) struct rp1_pio_device *pio = client->pio; int ret; + if (args->mask & ~RP1_PIO_SM_MASK) + return -EINVAL; + mutex_lock(&pio->instr_mutex); ret = rp1_pio_message(client->pio, PIO_SM_CLAIM, args, sizeof(*args)); if (ret >= 0) { if (args->mask) client->claimed_sms |= args->mask; - else + else if (ret < RP1_PIO_SMS_COUNT) client->claimed_sms |= (1 << ret); pio->claimed_sms |= client->claimed_sms; } @@ -410,6 +426,9 @@ int rp1_pio_sm_unclaim(struct rp1_pio_client *client, void *param) struct rp1_pio_sm_claim_args *args = param; struct rp1_pio_device *pio = client->pio; + if (args->mask & ~RP1_PIO_SM_MASK) + return -EINVAL; + mutex_lock(&pio->instr_mutex); (void)rp1_pio_message(client->pio, PIO_SM_UNCLAIM, args, sizeof(*args)); client->claimed_sms &= ~args->mask; @@ -423,6 +442,9 @@ int rp1_pio_sm_is_claimed(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_claim_args *args = param; + if (args->mask & ~RP1_PIO_SM_MASK) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_IS_CLAIMED, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_is_claimed); @@ -431,6 +453,10 @@ int rp1_pio_sm_init(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_init_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT || + args->initial_pc >= RP1_PIO_INSTR_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_INIT, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_init); @@ -439,6 +465,9 @@ int rp1_pio_sm_set_config(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_set_config_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_SET_CONFIG, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_set_config); @@ -449,6 +478,9 @@ int rp1_pio_sm_exec(struct rp1_pio_client *client, void *param) unsigned long interval = POLL_INTERVAL_MIN; int ret; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + if (args->blocking) args->blocking = 2; @@ -470,6 +502,9 @@ int rp1_pio_sm_clear_fifos(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_clear_fifos_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_CLEAR_FIFOS, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_clear_fifos); @@ -478,6 +513,9 @@ int rp1_pio_sm_set_clkdiv(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_set_clkdiv_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_SET_CLKDIV, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_set_clkdiv); @@ -486,6 +524,9 @@ int rp1_pio_sm_set_pins(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_set_pins_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_SET_PINS, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_set_pins); @@ -494,6 +535,9 @@ int rp1_pio_sm_set_pindirs(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_set_pindirs_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_SET_PINDIRS, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_set_pindirs); @@ -502,6 +546,9 @@ int rp1_pio_sm_set_enabled(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_set_enabled_args *args = param; + if (args->mask & ~RP1_PIO_SM_MASK) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_SET_ENABLED, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_set_enabled); @@ -510,6 +557,9 @@ int rp1_pio_sm_restart(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_restart_args *args = param; + if (args->mask & ~RP1_PIO_SM_MASK) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_RESTART, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_restart); @@ -518,6 +568,9 @@ int rp1_pio_sm_clkdiv_restart(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_restart_args *args = param; + if (args->mask & ~RP1_PIO_SM_MASK) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_CLKDIV_RESTART, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_clkdiv_restart); @@ -526,6 +579,9 @@ int rp1_pio_sm_enable_sync(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_enable_sync_args *args = param; + if (args->mask & ~RP1_PIO_SM_MASK) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_ENABLE_SYNC, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_enable_sync); @@ -536,6 +592,9 @@ int rp1_pio_sm_put(struct rp1_pio_client *client, void *param) unsigned long interval = POLL_INTERVAL_MIN; int ret; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + if (args->blocking) args->blocking = 2; @@ -559,6 +618,9 @@ int rp1_pio_sm_get(struct rp1_pio_client *client, void *param) unsigned long interval = POLL_INTERVAL_MIN; int ret; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + if (args->blocking) args->blocking = 2; @@ -583,6 +645,9 @@ int rp1_pio_sm_set_dmactrl(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_set_dmactrl_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_SET_DMACTRL, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_set_dmactrl); @@ -593,6 +658,9 @@ int rp1_pio_sm_get_dmactrl(struct rp1_pio_client *client, void *param) struct rp1_pio_sm_get_dmactrl_args *args = param; int ret; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + if (pio->fw_pio_count >= PIO_SM_GET_DMACTRL) { ret = rp1_pio_message_resp(pio, PIO_SM_GET_DMACTRL, args, sizeof(*args), &args->ctrl, NULL, 4); @@ -617,6 +685,9 @@ int rp1_pio_sm_fifo_state(struct rp1_pio_client *client, void *param) const int level_offset = offsetof(struct rp1_pio_sm_fifo_state_args, level); int ret; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + ret = rp1_pio_message_resp(client->pio, PIO_SM_FIFO_STATE, args, sizeof(*args), &args->level, NULL, sizeof(*args) - level_offset); if (ret >= 0) @@ -629,6 +700,9 @@ int rp1_pio_sm_drain_tx(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_clear_fifos_args *args = param; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SM_DRAIN_TX, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_sm_drain_tx); @@ -638,6 +712,9 @@ int rp1_pio_sm_get_flags(struct rp1_pio_client *client, void *param) struct rp1_pio_sm_get_flags_args *args = param; int ret; + if (args->sm >= RP1_PIO_SMS_COUNT) + return -EINVAL; + ret = rp1_pio_message_resp(client->pio, PIO_SM_GET_FLAGS, args, sizeof(*args), &args->flags, NULL, sizeof(args->flags)); if (ret >= 0) @@ -651,6 +728,9 @@ int rp1_pio_gpio_init(struct rp1_pio_client *client, void *param) { struct rp1_gpio_init_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_INIT, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_init); @@ -659,6 +739,10 @@ int rp1_pio_gpio_set_function(struct rp1_pio_client *client, void *param) { struct rp1_gpio_set_function_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT || + args->fn > RP1_GPIO_FUNC_MAX) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_SET_FUNCTION, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_set_function); @@ -667,6 +751,9 @@ int rp1_pio_gpio_set_pulls(struct rp1_pio_client *client, void *param) { struct rp1_gpio_set_pulls_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_SET_PULLS, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_set_pulls); @@ -675,6 +762,9 @@ int rp1_pio_gpio_set_outover(struct rp1_pio_client *client, void *param) { struct rp1_gpio_set_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_SET_OUTOVER, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_set_outover); @@ -683,6 +773,9 @@ int rp1_pio_gpio_set_inover(struct rp1_pio_client *client, void *param) { struct rp1_gpio_set_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_SET_INOVER, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_set_inover); @@ -691,6 +784,9 @@ int rp1_pio_gpio_set_oeover(struct rp1_pio_client *client, void *param) { struct rp1_gpio_set_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_SET_OEOVER, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_set_oeover); @@ -699,6 +795,9 @@ int rp1_pio_gpio_set_input_enabled(struct rp1_pio_client *client, void *param) { struct rp1_gpio_set_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_SET_INPUT_ENABLED, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_set_input_enabled); @@ -707,6 +806,9 @@ int rp1_pio_gpio_set_drive_strength(struct rp1_pio_client *client, void *param) { struct rp1_gpio_set_args *args = param; + if (args->gpio >= RP1_PIO_GPIO_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, GPIO_SET_DRIVE_STRENGTH, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_gpio_set_drive_strength); @@ -784,6 +886,9 @@ int rp1_pio_set_irqn_source_mask_enabled(struct rp1_pio_client *client, void *pa { struct rp1_pio_set_irqn_source_mask_enabled_args *args = param; + if (args->irq_index >= RP1_PIO_IRQ_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_SET_IRQN_SOURCE_MASK_ENABLED, args, sizeof(*args)); } @@ -794,6 +899,9 @@ int rp1_pio_interrupt_get(struct rp1_pio_client *client, void *param) struct rp1_pio_interrupt_get_args *args = param; int ret; + if (args->pio_interrupt_num >= RP1_PIO_INTERRUPT_COUNT) + return -EINVAL; + ret = rp1_pio_message_resp(client->pio, PIO_INTERRUPT_GET, args, sizeof(*args), &args->active, NULL, sizeof(args->active)); if (ret >= 0) @@ -806,6 +914,9 @@ int rp1_pio_interrupt_clear(struct rp1_pio_client *client, void *param) { struct rp1_pio_interrupt_clear_args *args = param; + if (args->pio_interrupt_num >= RP1_PIO_INTERRUPT_COUNT) + return -EINVAL; + return rp1_pio_message(client->pio, PIO_INTERRUPT_CLEAR, args, sizeof(*args)); } EXPORT_SYMBOL_GPL(rp1_pio_interrupt_clear); From ee2e38a5417c8ab9cc9aeb9dac799d9a0ce94424 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:00:43 +0000 Subject: [PATCH 3/7] misc: rp1-pio: Harden the program reference counting Identical programs are shared between clients, with a per-instruction reference count that is only 8 bits wide. Adding the same program 256 times wraps the count, after which a single removal frees instructions that another client is still executing. Refuse to share a program whose count is already at its maximum. Also check that an offset returned by the firmware fits in the instruction memory before using it to index the driver's own state. Co-Authored-By: Claude Mythos 5.2 Signed-off-by: Phil Elwell --- drivers/misc/rp1-pio.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/misc/rp1-pio.c b/drivers/misc/rp1-pio.c index 96331c316084c4..f3ead88c18bd4f 100644 --- a/drivers/misc/rp1-pio.c +++ b/drivers/misc/rp1-pio.c @@ -318,8 +318,20 @@ int rp1_pio_add_program(struct rp1_pio_client *client, void *param) mutex_lock(&pio->instr_mutex); offset = rp1_pio_find_program(pio, args); - if (offset < 0) + if (offset >= 0) { + /* Sharing an existing copy - make sure it can take another user */ + for (i = 0; i < args->num_instrs; i++) { + if (pio->instr_refcounts[offset + i] == U8_MAX) { + offset = -EBUSY; + break; + } + } + } else { offset = rp1_pio_message(client->pio, PIO_ADD_PROGRAM, args, sizeof(*args)); + /* Don't trust the firmware to return a usable offset */ + if (offset >= 0 && offset + args->num_instrs > RP1_PIO_INSTR_COUNT) + offset = -EIO; + } if (offset >= 0) { uint32_t used_mask; From 0cabf75b600760719cbd65438326e7c89c7ebd6e Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:00:46 +0000 Subject: [PATCH 4/7] misc: rp1-pio: Limit how long the firmware waits for SM flags SM_GET_FLAGS takes a timeout in microseconds that is passed straight to the firmware, which waits by polling in its mailbox interrupt handler. The driver gives up on a firmware message after one second, so a longer timeout fails with -ETIMEDOUT, blocks all other firmware users for as long as it lasts (potentially over an hour), and results in a late reply that overwrites the header of whichever request comes next. Only ask the firmware to wait for up to a millisecond at a time, and do the rest of the waiting in the driver, where it can sleep and be interrupted. Short timeouts behave exactly as before. Co-Authored-By: Claude Mythos 5.2 Signed-off-by: Phil Elwell --- drivers/misc/rp1-pio.c | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/drivers/misc/rp1-pio.c b/drivers/misc/rp1-pio.c index f3ead88c18bd4f..4a3950b792f20c 100644 --- a/drivers/misc/rp1-pio.c +++ b/drivers/misc/rp1-pio.c @@ -46,6 +46,13 @@ #define RP1_PIO_INTERRUPT_COUNT 8 #define RP1_GPIO_FUNC_MAX 0x1f +/* + * The longest that the firmware is asked to wait for SM flags in a single + * message. The firmware waits by polling, with the mailbox blocked, so + * longer timeouts are broken up into multiple messages. + */ +#define RP1_PIO_FLAGS_WAIT_MAX_US 1000 + #define MAX_ARG_SIZE 256 #define RP1_PIO_FIFO_TX0 0x00 @@ -722,15 +729,40 @@ EXPORT_SYMBOL_GPL(rp1_pio_sm_drain_tx); int rp1_pio_sm_get_flags(struct rp1_pio_client *client, void *param) { struct rp1_pio_sm_get_flags_args *args = param; + unsigned long interval = POLL_INTERVAL_MIN; + uint32_t timeout = args->timeout; + uint32_t flags = args->flags; + ktime_t end; int ret; if (args->sm >= RP1_PIO_SMS_COUNT) return -EINVAL; - ret = rp1_pio_message_resp(client->pio, PIO_SM_GET_FLAGS, args, sizeof(*args), - &args->flags, NULL, sizeof(args->flags)); - if (ret >= 0) - return offsetof(struct rp1_pio_sm_get_flags_args, flags) + ret; + /* + * The firmware waits for the flags by polling with the mailbox + * blocked, so only let it wait for a short time. Do the rest of the + * waiting here, where it can sleep and be interrupted. + */ + end = ktime_add_us(ktime_get(), timeout); + + while (1) { + s64 remaining = ktime_us_delta(end, ktime_get()); + + args->flags = flags; + args->timeout = clamp_t(s64, remaining, 0, + min_t(uint32_t, timeout, RP1_PIO_FLAGS_WAIT_MAX_US)); + ret = rp1_pio_message_resp(client->pio, PIO_SM_GET_FLAGS, args, sizeof(*args), + &args->flags, NULL, sizeof(args->flags)); + if (ret < 0) + break; + if (args->flags || remaining <= RP1_PIO_FLAGS_WAIT_MAX_US) + return offsetof(struct rp1_pio_sm_get_flags_args, flags) + ret; + if (signal_pending(current)) + return -ERESTARTSYS; + fsleep(min_t(s64, interval, remaining)); + POLL_INTERVAL_NEXT(interval); + } + args->flags = ~0; return ret; } From 18348935de2117a293bfaa762716ad313f971369 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:01:49 +0000 Subject: [PATCH 5/7] misc: rp1-pio: Serialise DMA configuration and transfers Nothing stops a client from issuing ioctls on the same file from several threads at once, and the DMA state has no protection against that. In particular, reconfiguring a channel frees its buffers and releases the DMA channel while a transfer in another thread may be sleeping with pointers into them, or be part way through copying user data. The transfer then reads from or writes to freed memory, the cyclic paths can write beyond a new, smaller buffer using the old period size, and two concurrent configurations free everything twice. Add a mutex to each channel, and hold it for the duration of every configuration and user transfer. Decide whether there is an old configuration to free from the state of the channel, under the mutex, rather than from the claim bits, and make sure dma->chan never holds an error pointer. The transfer paths sleep interruptibly, so a reconfiguration waits for a blocked transfer to finish or be interrupted. A channel can be configured with no bounce buffers, for the benefit of kernel clients that supply their own DMA addresses. That is no use to user space, so fail user transfers on such a channel instead of taking the remainder of a division by zero. Co-Authored-By: Claude Mythos 5.2 Signed-off-by: Phil Elwell --- drivers/misc/rp1-pio.c | 114 ++++++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 31 deletions(-) diff --git a/drivers/misc/rp1-pio.c b/drivers/misc/rp1-pio.c index 4a3950b792f20c..b8a42e1f948e5f 100644 --- a/drivers/misc/rp1-pio.c +++ b/drivers/misc/rp1-pio.c @@ -112,6 +112,12 @@ struct dma_buf_info { }; struct dma_info { + /* + * Serialises the configuration and use of the channel by user space, + * so that the channel and its buffers can't be freed or replaced + * during a transfer. + */ + struct mutex lock; struct semaphore buf_sem; wait_queue_head_t cyclic_wait; struct dma_chan *chan; @@ -1258,7 +1264,7 @@ static int rp1_pio_sm_config_xfer_internal(struct rp1_pio_client *client, uint s bool tx = dir == RP1_PIO_DIR_TO_SM; bool prefer_light_dma = flags & BIT(0); bool force_dma_type = flags & BIT(1); - bool reconfigure = false; + struct dma_chan *chan; phys_addr_t fifo_addr; uint32_t dma_mask; char chan_name[5]; @@ -1296,31 +1302,39 @@ static int rp1_pio_sm_config_xfer_internal(struct rp1_pio_client *client, uint s dma_mask = 1 << (sm * 2 + dir); + /* Fail quickly, rather than wait for another client's transfer */ spin_lock(&pio->lock); - if (!(pio->claimed_dmas & dma_mask & ~client->claimed_dmas)) { + if (!(pio->claimed_dmas & dma_mask & ~client->claimed_dmas)) dma = &pio->dma_configs[sm][dir]; - if (client->claimed_dmas & dma_mask) - reconfigure = true; + spin_unlock(&pio->lock); + if (!dma) + return -EBUSY; + + /* + * Wait for any transfer or configuration by this client to finish, + * then claim the channel for real. + */ + if (mutex_lock_interruptible(&dma->lock)) + return -ERESTARTSYS; + + spin_lock(&pio->lock); + if (pio->claimed_dmas & dma_mask & ~client->claimed_dmas) { + ret = -EBUSY; + } else { pio->claimed_dmas |= dma_mask; client->claimed_dmas |= dma_mask; } spin_unlock(&pio->lock); - if (!dma) - return -EBUSY; + if (ret) + goto out_unlock; /* dma_release_channel() sleeps, so free the old channel outside the lock. */ - if (reconfigure) + if (dma->chan) rp1_pio_sm_dma_free(dma); /* Note: the semaphore is only used by non-cyclic DMA */ sema_init(&dma->buf_sem, 0); - if (!reconfigure) { - /* the waitqueue and spinlock are only used by cyclic DMA */ - init_waitqueue_head(&dma->cyclic_wait); - spin_lock_init(&dma->cyclic_lock); - } - /* Allocate and configure a DMA channel */ /* Careful - each SM FIFO has its own DREQ value */ chan_name[0] = tx ? 't' : 'r'; @@ -1338,11 +1352,15 @@ static int rp1_pio_sm_config_xfer_internal(struct rp1_pio_client *client, uint s dma->cyclic_underflow = false; dma->cyclic_tx_issued = false; dma->tx = tx; - dma->chan = dma_request_chan(dev, chan_name); - if (IS_ERR(dma->chan)) { - ret = PTR_ERR(dma->chan); + dma->buf_size = 0; + dma->buf_count = 0; + /* dma->chan is only ever NULL or a valid channel */ + chan = dma_request_chan(dev, chan_name); + if (IS_ERR(chan)) { + ret = PTR_ERR(chan); goto err_unclaim; } + dma->chan = chan; dma_get_slave_caps(dma->chan, &dma_caps); if (force_dma_type && dma_caps.max_burst != (prefer_light_dma ? 4 : 8)) { @@ -1455,7 +1473,7 @@ static int rp1_pio_sm_config_xfer_internal(struct rp1_pio_client *client, uint s if (!tx) dma_async_issue_pending(dma->chan); } - return 0; + goto out_unlock; err_dma_free: rp1_pio_sm_dma_free(dma); @@ -1466,6 +1484,9 @@ static int rp1_pio_sm_config_xfer_internal(struct rp1_pio_client *client, uint s pio->claimed_dmas &= ~dma_mask; spin_unlock(&pio->lock); +out_unlock: + mutex_unlock(&dma->lock); + return ret; } @@ -1805,8 +1826,10 @@ static int rp1_pio_sm_xfer_data32_user(struct rp1_pio_client *client, void *para { struct rp1_pio_sm_xfer_data32_args *args = param; struct rp1_pio_device *pio = client->pio; - struct dma_info *dma = NULL; + struct dma_info *dma; uint32_t dma_mask; + bool claimed; + int ret; if (args->sm >= RP1_PIO_SMS_COUNT || args->dir >= RP1_PIO_DIR_COUNT || !args->data_bytes || @@ -1814,19 +1837,32 @@ static int rp1_pio_sm_xfer_data32_user(struct rp1_pio_client *client, void *para return -EINVAL; dma_mask = 1 << (args->sm * 2 + args->dir); + dma = &pio->dma_configs[args->sm][args->dir]; + + /* + * Hold the lock for the whole transfer, even though it may block, + * so that the channel can't be reconfigured underneath it. + */ + if (mutex_lock_interruptible(&dma->lock)) + return -ERESTARTSYS; + spin_lock(&pio->lock); - if (client->claimed_dmas & dma_mask) - dma = &pio->dma_configs[args->sm][args->dir]; + claimed = client->claimed_dmas & dma_mask; spin_unlock(&pio->lock); - if (!dma) - return -EINVAL; - if (args->dir == RP1_PIO_DIR_TO_SM) - return dma->cyclic ? rp1_pio_sm_tx_user_cyclic(pio, dma, args) : - rp1_pio_sm_tx_user(pio, dma, args); + /* A channel configured with no buffers is of no use to user space */ + if (!claimed || !dma->chan || !dma->buf_count) + ret = -EINVAL; + else if (args->dir == RP1_PIO_DIR_TO_SM) + ret = dma->cyclic ? rp1_pio_sm_tx_user_cyclic(pio, dma, args) : + rp1_pio_sm_tx_user(pio, dma, args); else - return dma->cyclic ? rp1_pio_sm_rx_user_cyclic(pio, dma, args) : - rp1_pio_sm_rx_user(pio, dma, args); + ret = dma->cyclic ? rp1_pio_sm_rx_user_cyclic(pio, dma, args) : + rp1_pio_sm_rx_user(pio, dma, args); + + mutex_unlock(&dma->lock); + + return ret; } static int rp1_pio_sm_xfer_data_user(struct rp1_pio_client *client, void *param) @@ -2084,10 +2120,14 @@ void rp1_pio_close(struct rp1_pio_client *client) struct dma_info *dma = &pio->dma_configs[i >> 1][i & 1]; claimed &= ~mask; - /* The SMs have been disabled, so this is safe */ - if (!dma->tx) - rp1_pio_sm_dma_flush_rx(dma); - rp1_pio_sm_dma_free(dma); + mutex_lock(&dma->lock); + if (dma->chan) { + /* The SMs have been disabled, so this is safe */ + if (!dma->tx) + rp1_pio_sm_dma_flush_rx(dma); + rp1_pio_sm_dma_free(dma); + } + mutex_unlock(&dma->lock); } } @@ -2377,6 +2417,18 @@ static int rp1_pio_probe(struct platform_device *pdev) pio->fw = fw; spin_lock_init(&pio->lock); mutex_init(&pio->instr_mutex); + for (i = 0; i < RP1_PIO_SMS_COUNT; i++) { + int dir; + + for (dir = 0; dir < RP1_PIO_DIR_COUNT; dir++) { + struct dma_info *dma = &pio->dma_configs[i][dir]; + + mutex_init(&dma->lock); + /* the waitqueue and spinlock are only used by cyclic DMA */ + init_waitqueue_head(&dma->cyclic_wait); + spin_lock_init(&dma->cyclic_lock); + } + } p = devm_platform_get_and_ioremap_resource(pdev, 0, &ioresource); if (IS_ERR(p)) { From d32ff7d0c6fc3d7866baaa501fbffabc6a0d1d27 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:01:51 +0000 Subject: [PATCH 6/7] misc: rp1-pio: Wait for interrupt handlers before freeing a client The interrupt handler for user space clients dereferences the client that owns the interrupt. When a client is closed, its interrupts are only disabled synchronously if they are still enabled. If the client has already disabled an interrupt, which is done with disable_irq_nosync(), a handler that was running at the time can still be running when the owner is cleared and the client is freed. Always wait for the handler when releasing an interrupt. Do it without holding the device spinlock, because disable_irq() and synchronize_irq() can sleep. While here, check the ownership of an interrupt under the lock when enabling or disabling it. Co-Authored-By: Claude Mythos 5.2 Signed-off-by: Phil Elwell --- drivers/misc/rp1-pio.c | 49 +++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/drivers/misc/rp1-pio.c b/drivers/misc/rp1-pio.c index b8a42e1f948e5f..3c786f12c5d511 100644 --- a/drivers/misc/rp1-pio.c +++ b/drivers/misc/rp1-pio.c @@ -983,10 +983,12 @@ int rp1_pio_irq_set_enabled(struct rp1_pio_client *client, void *param) if (irq_index >= pio->irq_count) return -EINVAL; irqi = &pio->irqs[irq_index]; - if (irqi->owner != client) - return -EBUSY; spin_lock(&pio->lock); + if (irqi->owner != client) { + spin_unlock(&pio->lock); + return -EBUSY; + } if (args->enabled && !irqi->enabled) { enable_irq(irqi->irq); irqi->enabled = true; @@ -2079,26 +2081,39 @@ void rp1_pio_close(struct rp1_pio_client *client) /* Free any allocated resources */ - spin_lock(&pio->lock); for (i = 0; i < pio->irq_count; i++) { + bool enabled; + irqi = &pio->irqs[i]; - if (irqi->owner == client) { - /* - * Disable directly rather than via irq_set_enabled(): - * that helper takes pio->lock itself, and it is - * already held here. - */ - if (irqi->enabled) { - disable_irq(irqi->irq); - irqi->enabled = false; - } - irqi->owner = NULL; - irqi->handler = NULL; - irqi->context = NULL; + spin_lock(&pio->lock); + if (irqi->owner != client) { + spin_unlock(&pio->lock); + continue; } + enabled = irqi->enabled; + irqi->enabled = false; + spin_unlock(&pio->lock); + + /* + * The handler uses the client, so make sure it isn't running + * and can't run again before giving up the interrupt. If the + * interrupt was disabled with disable_irq_nosync() then the + * handler may still be running. Both of these can sleep, so + * call them without the lock - nobody else can claim the + * interrupt while it still has an owner. + */ + if (enabled) + disable_irq(irqi->irq); + else + synchronize_irq(irqi->irq); + + spin_lock(&pio->lock); + irqi->owner = NULL; + irqi->handler = NULL; + irqi->context = NULL; + spin_unlock(&pio->lock); } - spin_unlock(&pio->lock); if (client->claimed_sms) { struct rp1_pio_sm_set_enabled_args se_args = { From b9b30c5780c0f10a92fa6907ede03d1e797de371 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Wed, 18 Nov 2026 11:39:25 +0000 Subject: [PATCH 7/7] misc: rp1-pio: Return the result of IRQ_IS_ENABLED The arguments to an ioctl are only copied back to the caller if the handler returns a positive number of bytes. The handler for IRQ_IS_ENABLED stored the state in its arguments but then returned zero, so user space never saw it and the interrupt always appeared to be disabled. Return the size of the arguments instead, as the other handlers with results do, and read the state under the lock. Co-Authored-By: Claude Mythos 5.2 Signed-off-by: Phil Elwell --- drivers/misc/rp1-pio.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/drivers/misc/rp1-pio.c b/drivers/misc/rp1-pio.c index 3c786f12c5d511..ed6432d48d6047 100644 --- a/drivers/misc/rp1-pio.c +++ b/drivers/misc/rp1-pio.c @@ -1014,12 +1014,17 @@ int rp1_pio_irq_is_enabled(struct rp1_pio_client *client, void *param) if (irq_index >= pio->irq_count) return -EINVAL; irqi = &pio->irqs[irq_index]; - if (irqi->owner != client) - return -EBUSY; + spin_lock(&pio->lock); + if (irqi->owner != client) { + spin_unlock(&pio->lock); + return -EBUSY; + } args->enabled = irqi->enabled; + spin_unlock(&pio->lock); - return 0; + /* Return the size of the arguments, so that the result is copied back */ + return sizeof(*args); } EXPORT_SYMBOL_GPL(rp1_pio_irq_is_enabled);