firmware: arm_ffa: Fix cleanup, notification, and discovery paths
Sudeep Holla
maintainercover firmware: arm_ffa: Fix cleanup, notification, and discovery paths
Hi all,
This series fixes a set of issues in the FF-A driver around init cleanup, framework notification handling, v1.0 notifier lifetime, and partition discovery.
The fixes are all small and localized, but together they tighten a few important paths:
- fix the early init unwind path when RX buffer allocation fails
- align the stored RX/TX buffer size with the size actually mapped to firmware
- ensure the framework notification handler always releases the RX buffer correctly
- validate framework notification payload bounds before copying data out of the shared RX buffer
- fix the partition lookup used for sched-recv callback registration
- unregister the FF-A v1.0 bus notifier during teardown
- bound the register-based partition discovery copies against the caller buffer
- reject FF-A driver registration without an ID table
This is the outcome of the self-initiated review of the entire driver following the oversight of Sashiko’s review on one of the patches that was merged.
https://sashiko.dev/#/patchset/[email protected]
---
Sudeep Holla (8):
firmware: arm_ffa: Check for NULL FF-A ID table while driver registration
firmware: arm_ffa: Skip free_pages on RX buffer alloc failure
firmware: arm_ffa: Align RxTx buffer size before mapping
firmware: arm_ffa: Fix Rx buffer release in fwk notification handler
firmware: arm_ffa: Validate framework notification payload bounds
firmware: arm_ffa: Unregister v1.0 bus notifier on teardown
firmware: arm_ffa: Fix sched-recv callback partition lookup
firmware: arm_ffa: Bound PARTITION_INFO_GET_REGS copies
drivers/firmware/arm_ffa/bus.c | 4 +- drivers/firmware/arm_ffa/driver.c | 79 ++++++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 23 deletions(-)
---
base-commit: 2e68039281932e6dc37718a1ea7cbb8e2cda42e6
change-id: 20260423-ffa_fixes-4ad33f0ee250
Signature · 2 lines
Regards, Sudeep
Sudeep Holla
maintainerPATCH 1/8 firmware: arm_ffa: Check for NULL FF-A ID table while driver registration
The bus match callback assumes that every FF-A driver provides an id_table and dereferences it unconditionally. Enforce that contract at registration time so a buggy client driver cannot crash the bus during match.
--- drivers/firmware/arm_ffa/bus.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/firmware/arm_ffa/bus.c +3 -1
@@ -26,6 +26,8 @@ static int ffa_device_match(struct device *dev, const struct device_driver *drv) id_table =to_ffa_driver (drv)->id_table; ffa_dev =to_ffa_dev (dev); +if (!id_table) +return 0 ;while (!uuid_is_null (&id_table->uuid)) {/*
@@ -123,7 +125,7 @@ int ffa_driver_register(struct ffa_driver *driver, struct module *owner, {int ret; -if (!driver->probe) +if (!driver->probe || !driver->id_table)return -EINVAL ; driver->driver.bus = &ffa_bus_type;
Signature · 1 line
2.43.0
Sudeep Holla
maintainerPATCH 2/8 firmware: arm_ffa: Skip free_pages on RX buffer alloc failure
In reply to Sudeep HollaIf the RX buffer allocation fails in ffa_init(), the error path jumps to free_pages even though no buffer has been allocated yet. Route that case directly to free_drv_info so the cleanup path is only used after at least one RX/TX buffer allocation has succeeded.
--- drivers/firmware/arm_ffa/driver.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/arm_ffa/driver.c +1 -1
@@ -2067,7 +2067,7 @@ static int __init ffa_init(void) drv_info->rx_buffer =alloc_pages_exact (rxtx_bufsz,GFP_KERNEL );if (!drv_info->rx_buffer) { ret = -ENOMEM ; - goto free_pages; + goto free_drv_info; } drv_info->tx_buffer =alloc_pages_exact (rxtx_bufsz,GFP_KERNEL );
Signature · 1 line
2.43.0
Sudeep Holla
maintainerPATCH 3/8 firmware: arm_ffa: Align RxTx buffer size before mapping
In reply to Sudeep HollaCommit 83210251fd70 ("firmware: arm_ffa: Use the correct buffer size during RXTX_MAP") advertises PAGE_ALIGN(rxtx_bufsz) to firmware when mapping the buffers but the driver continues to stores the minimum FF-A buffer size in drv_info->rxtx_bufsz which is used elsewhere in the driver.
Align the size before storing it so that the allocation, validation and FFA_RXTX_MAP all use the same buffer size.
--- drivers/firmware/arm_ffa/driver.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/firmware/arm_ffa/driver.c +2 -1
@@ -2063,6 +2063,7 @@ static int __init ffa_init(void) rxtx_bufsz =SZ_4K ; } + rxtx_bufsz =PAGE_ALIGN (rxtx_bufsz); drv_info->rxtx_bufsz = rxtx_bufsz; drv_info->rx_buffer =alloc_pages_exact (rxtx_bufsz,GFP_KERNEL );if (!drv_info->rx_buffer) {
@@ -2078,7 +2079,7 @@ static int __init ffa_init(void) ret =ffa_rxtx_map (virt_to_phys (drv_info->tx_buffer),virt_to_phys (drv_info->rx_buffer), -PAGE_ALIGN (rxtx_bufsz) /FFA_PAGE_SIZE ); + rxtx_bufsz /FFA_PAGE_SIZE );if (ret) {pr_err ("failed to register FFA RxTx buffers\n" ); goto free_pages;
Signature · 1 line
2.43.0
Re: [PATCH 3/8] firmware: arm_ffa: Align RxTx buffer size before mapping
Quoted 4 lines · Sudeep Holla
Commit
83210251fd70("firmware:arm_ffa: Use the correct buffer size duringRXTX_MAP") advertisesPAGE_ALIGN(rxtx_bufsz)to firmware when mapping the buffers but the driver continues to stores the minimum FF-A buffer size indrv_info->rxtx_bufszwhich is used elsewhere in the driver.
Hello Sudeep,
Quoted 4 lines
Align the size before storing it so that the allocation, validation and
FFA_RXTX_MAPall use the same buffer size.
Thanks for fixing this,
Quoted 33 lines
Fixes 83210251fd70 ("firmware: arm_ffa: Use the correct buffer size during RXTX_MAP")Cc Sebastian Ene <[email protected]>Signed-off-by Sudeep Holla <[email protected]>--- drivers/firmware/arm_ffa/driver.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)drivers/firmware/arm_ffa/driver.c +2
-1@@ -2063,6 +2063,7 @@ static int __init ffa_init(void) rxtx_bufsz =SZ_4K ; } + rxtx_bufsz =PAGE_ALIGN (rxtx_bufsz); drv_info->rxtx_bufsz = rxtx_bufsz; drv_info->rx_buffer =alloc_pages_exact (rxtx_bufsz,GFP_KERNEL );if (!drv_info->rx_buffer) {@@ -2078,7 +2079,7 @@ static int __init ffa_init(void) ret =ffa_rxtx_map (virt_to_phys (drv_info->tx_buffer),virt_to_phys (drv_info->rx_buffer), -PAGE_ALIGN (rxtx_bufsz) /FFA_PAGE_SIZE ); + rxtx_bufsz /FFA_PAGE_SIZE );if (ret) {pr_err ("failed to register FFA RxTx buffers\n" ); goto free_pages;Signature · 2 lines
-- 2.43.0
Sudeep Holla
maintainerRe: [PATCH 3/8] firmware: arm_ffa: Align RxTx buffer size before mapping
Quoted 6 lines · Sebastian Ene
Quoted 4 lines · Sudeep Holla
Commit
83210251fd70("firmware:arm_ffa: Use the correct buffer size duringRXTX_MAP") advertisesPAGE_ALIGN(rxtx_bufsz)to firmware when mapping the buffers but the driver continues to stores the minimum FF-A buffer size indrv_info->rxtx_bufszwhich is used elsewhere in the driver.
Quoted 8 lines
Hello Sudeep,
Quoted 4 lines
Align the size before storing it so that the allocation, validation and
FFA_RXTX_MAPall use the same buffer size.Thanks for fixing this,
It was my fault, I wasn't expecting sashiko was covering most of the lists now, so didn't check for it in time.
Quoted 2 lines
Reviewed-by Sebastian Ene <[email protected]>.
Thanks!
Signature · 2 lines
Regards, Sudeep
Sudeep Holla
maintainerPATCH 4/8 firmware: arm_ffa: Fix Rx buffer release in fwk notification handler
In reply to Sudeep HollaRefactor handle_fwk_notif_callbacks() so that all exit paths funnel through a single FFA_RX_RELEASE call. While doing that, use scoped_guard() for the Rx buffer lock and keep the message parsing under the lock scope.
This makes the Rx buffer release explicit for the kmemdup() failure path and for the early exit when the framework notification bit is not set.
This will ensure the Rx buffer is always release in the framework notification handler.
--- drivers/firmware/arm_ffa/driver.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/firmware/arm_ffa/driver.c +16 -15
@@ -1472,25 +1472,21 @@ static void handle_fwk_notif_callbacks(u32 bitmap)/* Only one framework notification defined and supported for now */ if (!(bitmap &FRAMEWORK_NOTIFY_RX_BUFFER_FULL )) -return ; + goto release_rx; -mutex_lock (&drv_info->rx_lock); + scoped_guard(mutex, &drv_info->rx_lock) { + msg = drv_info->rx_buffer; + buf =kmemdup ((void *)msg + msg->offset, msg->size,GFP_KERNEL ); +if (!buf) + goto release_rx; - msg = drv_info->rx_buffer; - buf =kmemdup ((void *)msg + msg->offset, msg->size,GFP_KERNEL ); -if (!buf) { -mutex_unlock (&drv_info->rx_lock); -return ; + target =SENDER_ID (msg->send_recv_id); +if (msg->offset >=sizeof (*msg)) +uuid_copy (&uuid, &msg->uuid); +else +uuid_copy (&uuid, &uuid_null); } - target =SENDER_ID (msg->send_recv_id); -if (msg->offset >=sizeof (*msg)) -uuid_copy (&uuid, &msg->uuid); -else -uuid_copy (&uuid, &uuid_null); - -mutex_unlock (&drv_info->rx_lock); -ffa_rx_release ();read_lock (&drv_info->notify_lock);
@@ -1500,6 +1496,11 @@ static void handle_fwk_notif_callbacks(u32 bitmap)if (cb_info && cb_info->fwk_cb) cb_info->fwk_cb (notify_id, cb_info->cb_data, buf);kfree (buf); + +return ; + +release_rx: +ffa_rx_release (); }static void notif_get_and_handle (void *cb_data)
Signature · 1 line
2.43.0
Sudeep Holla
maintainerPATCH 5/8 firmware: arm_ffa: Validate framework notification payload bounds
In reply to Sudeep HollaFramework notification callbacks copy an indirect message payload out of the shared Rx buffer. Validate the reported offset and size before kmemdup() so malformed firmware data cannot drive an out-of-bounds read or an oversized allocation.
--- drivers/firmware/arm_ffa/driver.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/firmware/arm_ffa/driver.c +8 -0
@@ -1469,6 +1469,7 @@ static void handle_fwk_notif_callbacks(u32 bitmap)int notify_id =0 , target;struct ffa_indirect_msg_hdr *msg;struct notifier_cb_info *cb_info =NULL ; +size_t min_offset = offsetof(struct ffa_indirect_msg_hdr , uuid);/* Only one framework notification defined and supported for now */ if (!(bitmap &FRAMEWORK_NOTIFY_RX_BUFFER_FULL ))
@@ -1476,6 +1477,13 @@ static void handle_fwk_notif_callbacks(u32 bitmap) scoped_guard(mutex, &drv_info->rx_lock) { msg = drv_info->rx_buffer; +if ((msg->offset != min_offset && msg->offset <sizeof (*msg)) || + msg->offset > drv_info->rxtx_bufsz || + msg->size > drv_info->rxtx_bufsz - msg->offset) { +pr_err ("invalid framework notification message\n" ); + goto release_rx; + } + buf =kmemdup ((void *)msg + msg->offset, msg->size,GFP_KERNEL );if (!buf) goto release_rx;
Signature · 1 line
2.43.0
Sudeep Holla
maintainerPATCH 6/8 firmware: arm_ffa: Unregister v1.0 bus notifier on teardown
In reply to Sudeep HollaFor FF-A v1.0 the driver registers a bus notifier to backfill UUID matching, but the notifier was never unregistered on cleanup paths. Track the registration state and unregister it during teardown and early partition-setup failure.
--- drivers/firmware/arm_ffa/driver.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/firmware/arm_ffa/driver.c +15 -0
@@ -100,6 +100,7 @@ struct ffa_drv_info {bool mem_ops_native;bool msg_direct_req2_supp;bool bitmap_created; +bool bus_notifier_registered;bool notif_enabled;unsigned int sched_recv_irq;unsigned int notif_pend_irq;
@@ -1638,6 +1639,15 @@ static struct notifier_block ffa_bus_nb = { .notifier_call = ffa_bus_notifier, }; +static void ffa_bus_notifier_unregister (void ) +{ +if (!drv_info->bus_notifier_registered) +return ; + +bus_unregister_notifier (&ffa_bus_type, &ffa_bus_nb); + drv_info->bus_notifier_registered = false; +} +static int ffa_xa_add_partition_info (struct ffa_device *dev) {struct ffa_dev_part_info *info;
@@ -1721,6 +1731,8 @@ static void ffa_partitions_cleanup(void)struct list_head *phead;unsigned long idx; +ffa_bus_notifier_unregister (); +/* Clean up/free all registered devices */ ffa_devices_unregister ();
@@ -1748,11 +1760,14 @@ static int ffa_setup_partitions(void) ret =bus_register_notifier (&ffa_bus_type, &ffa_bus_nb);if (ret)pr_err ("Failed to register FF-A bus notifiers\n" ); +else + drv_info->bus_notifier_registered = true; } count =ffa_partition_probe (&uuid_null, &pbuf);if (count <=0 ) {pr_info ("%s: No partitions found, error %d\n" , __func__, count); +ffa_bus_notifier_unregister ();return -EINVAL ; }
Signature · 1 line
2.43.0
Sudeep Holla
maintainerPATCH 7/8 firmware: arm_ffa: Fix sched-recv callback partition lookup
In reply to Sudeep Hollaffa_sched_recv_cb_update() used list_for_each_entry_safe() to search for a matching partition and then tested the iterator against NULL. That is not a valid end-of-list check for circular lists and can fall through with an invalid pointer. Use a normal iterator and detect the not-found case correctly before touching the partition state.
--- drivers/firmware/arm_ffa/driver.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/firmware/arm_ffa/driver.c +3 -3
@@ -1190,7 +1190,7 @@ static int ffa_sched_recv_cb_update(struct ffa_device *dev, ffa_sched_recv_cb callback, void *cb_data, bool is_registration) { - struct ffa_dev_part_info *partition = NULL, *tmp; + struct ffa_dev_part_info *partition = NULL;struct list_head *phead;bool cb_valid;
@@ -1203,11 +1203,11 @@ ffa_sched_recv_cb_update(struct ffa_device *dev, ffa_sched_recv_cb callback,return -EINVAL ; } -list_for_each_entry_safe (partition, tmp, phead, node) +list_for_each_entry (partition, phead, node)if (partition->dev == dev)break ; -if (!partition) { +if (&partition->node == phead) {pr_err ("%s: No such partition ID 0x%x\n" , __func__, dev->vm_id);return -EINVAL ; }
Signature · 1 line
2.43.0
Sudeep Holla
maintainerPATCH 8/8 firmware: arm_ffa: Bound PARTITION_INFO_GET_REGS copies
In reply to Sudeep HollaThe register-based PARTITION_INFO_GET path trusted the firmware-provided indices when copying partition descriptors into the caller buffer. Reject inconsistent counts or index progressions so the copy loop cannot write past the allocated array.
--- drivers/firmware/arm_ffa/driver.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/firmware/arm_ffa/driver.c +12 -2
@@ -336,7 +336,7 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, do { __le64 *regs; - int idx; + int idx, nr_desc, buf_idx; start_idx = prev_idx ? prev_idx + 1 : 0;
@@ -354,15 +354,25 @@ __ffa_partition_info_get_regs(u32 uuid0, u32 uuid1, u32 uuid2, u32 uuid3, count =PARTITION_COUNT (partition_info.a2);if (!buffer || !num_parts)/* count only */ return count; +if (count > num_parts) +return -EINVAL ; cur_idx =CURRENT_INDEX (partition_info.a2); +if (cur_idx < start_idx || cur_idx >= count) +return -EINVAL ; + + nr_desc = cur_idx - start_idx +1 ; + buf_idx = buf - buffer; +if (buf_idx + nr_desc > num_parts) +return -EINVAL ; + tag =UUID_INFO_TAG (partition_info.a2); buf_sz =PARTITION_INFO_SZ (partition_info.a2);if (buf_sz >sizeof (*buffer)) buf_sz =sizeof (*buffer); regs = (void *)&partition_info.a3; -for (idx =0 ; idx < cur_idx - start_idx +1 ; idx++, buf++) { +for (idx =0 ; idx < nr_desc; idx++, buf++) {union {uuid_t uuid;u64 regs[2 ];
Signature · 1 line
2.43.0