Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Seth Forshee <sforshee@nvidia.com>
To: Sudeep Holla <sudeep.holla@kernel.org>,
	 Sebastian Ene <sebastianene@google.com>
Cc: linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,  Seth Forshee <sforshee@nvidia.com>
Subject: [PATCH 2/2] firmware: arm_ffa: Fall back to minimum buffer size if RXTX_MAP fails
Date: Mon, 01 Jun 2026 15:45:12 -0500	[thread overview]
Message-ID: <20260601-b4-ffa-rxtx-map-fixes-v1-2-c071b12ae05c@nvidia.com> (raw)
In-Reply-To: <20260601-b4-ffa-rxtx-map-fixes-v1-0-c071b12ae05c@nvidia.com>

FFA_FEATURES only supports a maximum size value for RXTX_MAP in v1.2+.
Older implementations may still reject larger sizes, which can happen
when the minimum size is rounded up to the page size when PAGE_SIZE >
4K.

Fall back to the minimum size when RXTX_MAP fails with
INVALID_PARAMETERS to fix the regression on these platforms. This also
has the side effect of falling back for v1.2+ with a maximum of 0 (i.e.
no maximum), where the rounded-up value isn't expected to be rejected,
but there's no harm in retrying with a smaller size and it might even be
of benefit for a non-compliant SPMC.

Note that this may result in passing a size smaller than the allocated
size to free_pages_exact(). This does not result in any leaked memory,
since the size extends into all allocated pages, and free_pages_exact()
correctly handles sizes which are not page aligned.

Fixes: 83210251fd70 ("firmware: arm_ffa: Use the correct buffer size during RXTX_MAP")
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Seth Forshee <sforshee@nvidia.com>
---
 drivers/firmware/arm_ffa/driver.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/drivers/firmware/arm_ffa/driver.c b/drivers/firmware/arm_ffa/driver.c
index dc45724a29ba..60ad58e229ce 100644
--- a/drivers/firmware/arm_ffa/driver.c
+++ b/drivers/firmware/arm_ffa/driver.c
@@ -2095,7 +2095,7 @@ static int __init ffa_init(void)
 {
 	int ret;
 	u32 buf_sz;
-	size_t rxtx_bufsz = SZ_4K, rxtx_max_bufsz = 0;
+	size_t rxtx_min_bufsz = SZ_4K, rxtx_max_bufsz = 0, rxtx_bufsz;
 
 	ret = ffa_transport_init(&invoke_ffa_fn);
 	if (ret)
@@ -2118,22 +2118,22 @@ static int __init ffa_init(void)
 	ret = ffa_features(FFA_FN_NATIVE(RXTX_MAP), 0, &buf_sz, NULL);
 	if (!ret) {
 		if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 1)
-			rxtx_bufsz = SZ_64K;
+			rxtx_min_bufsz = SZ_64K;
 		else if (RXTX_MAP_MIN_BUFSZ(buf_sz) == 2)
-			rxtx_bufsz = SZ_16K;
+			rxtx_min_bufsz = SZ_16K;
 		else
-			rxtx_bufsz = SZ_4K;
+			rxtx_min_bufsz = SZ_4K;
 
 		if (FFA_SUPPORTS_RXTX_MAX_BUFSZ(drv_info->version)) {
 			rxtx_max_bufsz = (size_t)RXTX_MAP_MAX_BUFSZ(buf_sz) * SZ_4K;
-			if (rxtx_max_bufsz != 0 && rxtx_max_bufsz < rxtx_bufsz) {
+			if (rxtx_max_bufsz != 0 && rxtx_max_bufsz < rxtx_min_bufsz) {
 				/*
 				 * Per spec the maximum must be >= the minimum, or
 				 * else zero if there is no size limit. If the SPMC
 				 * violates this constraint, use the minimum as the
 				 * effective maximum.
 				 */
-				rxtx_max_bufsz = rxtx_bufsz;
+				rxtx_max_bufsz = rxtx_min_bufsz;
 			}
 		}
 	}
@@ -2142,7 +2142,7 @@ static int __init ffa_init(void)
 	 * alloc_pages_exact() allocates full pages. Use the full allocated
 	 * space up to the max supported by the SPMC.
 	 */
-	rxtx_bufsz = PAGE_ALIGN(rxtx_bufsz);
+	rxtx_bufsz = PAGE_ALIGN(rxtx_min_bufsz);
 	if (rxtx_max_bufsz)
 		rxtx_bufsz = min(rxtx_bufsz, rxtx_max_bufsz);
 
@@ -2162,6 +2162,20 @@ static int __init ffa_init(void)
 	ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
 			   virt_to_phys(drv_info->rx_buffer),
 			   rxtx_bufsz / FFA_PAGE_SIZE);
+	if (ret == -EINVAL && rxtx_max_bufsz == 0 && rxtx_min_bufsz < rxtx_bufsz) {
+		/*
+		 * FFA_FEATURES only supports maximum buffer size in v1.2+,
+		 * and some implementations without it may fail when the
+		 * buffer size is rounded up to a larger page size. If
+		 * RXTX_MAP fails due to invalid parameters, try again with
+		 * the minimum buffer size.
+		 */
+		rxtx_bufsz = rxtx_min_bufsz;
+		drv_info->rxtx_bufsz = rxtx_bufsz;
+		ret = ffa_rxtx_map(virt_to_phys(drv_info->tx_buffer),
+				   virt_to_phys(drv_info->rx_buffer),
+				   rxtx_bufsz / FFA_PAGE_SIZE);
+	}
 	if (ret) {
 		pr_err("failed to register FFA RxTx buffers\n");
 		goto free_pages;

-- 
2.43.0



  parent reply	other threads:[~2026-06-01 20:45 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-01 20:45 [PATCH 0/2] firmware: arm_ffa: Fix RXTX_MAP buffer size regressions Seth Forshee
2026-06-01 20:45 ` [PATCH 1/2] firmware: arm_ffa: Honor maximum RX/TX buffer size Seth Forshee
2026-06-02 15:56   ` Sudeep Holla
2026-06-02 16:12     ` Seth Forshee
2026-06-02 16:27       ` Sudeep Holla
2026-06-02 17:05         ` Seth Forshee
2026-06-02 21:58           ` Seth Forshee
2026-06-01 20:45 ` Seth Forshee [this message]
2026-06-02 15:48   ` [PATCH 2/2] firmware: arm_ffa: Fall back to minimum buffer size if RXTX_MAP fails Sudeep Holla
2026-06-02 16:21     ` Seth Forshee
2026-06-02 16:29       ` Sudeep Holla
2026-06-02 17:12         ` Seth Forshee
2026-06-02 16:51 ` [PATCH 0/2] firmware: arm_ffa: Fix RXTX_MAP buffer size regressions Sudeep Holla
2026-06-02 17:19   ` Seth Forshee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260601-b4-ffa-rxtx-map-fixes-v1-2-c071b12ae05c@nvidia.com \
    --to=sforshee@nvidia.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sebastianene@google.com \
    --cc=sudeep.holla@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox