Skip to contentSkip to conversation

misc: fastrpc: Add cache maintenance for non-coherent platforms

PATCH In review Abhinav Parihar 3 participants 3 messages Nov 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 misc: fastrpc: Add cache maintenance for non-coherent platforms

Some platforms using fastrpc do not support DMA coherency on HLOS. On such systems, explicit cache maintenance is required to ensure data consistency for RPC argument buffers.

Add cache maintenance for argument buffers when operating on non-coherent platforms:
- Flush input buffers before invoking RPC to ensure CPU writes are visible to the DSP
- Invalidate output buffers after RPC completion to ensure DSP writes are visible to the CPU

Introduce helper functions fastrpc_flush_args() and fastrpc_inv_args() to perform the required dma-buf cache operations. These are invoked only when the device is not marked as DMA coherent.

The coherency capability is determined using the "dma-coherent" device tree property and stored per session context.

This ensures correct data synchronization on platforms lacking DMA coherency, while avoiding unnecessary overhead on coherent systems.

---

Patch [v2]: https://lore.kernel.org/all/[email protected]/

Changes in v3:
- Fix missing max_raix update causes incorrect do_cmo evaluation.
- Use of_dma_is_coherent() to determine device coherency.
- Simplify input/output buffer classification and overlap checks for cache maintenance.

Changes in v2:
- Fix buffer overlap handling to ensure cache maintenance is performed when input and output buffers overlap and the overlap calculation suppresses the buffer's mstart/mend range.
- Add a do_cmo flag to track buffers that require cache maintenance even when their overlap range is clipped.
- Invalidate output buffers before sending the RPC request, preventing stale CPU cache lines from being written back while the DSP updates the buffers.
- Use DMA_FROM_DEVICE consistently for both begin_cpu_access() and end_cpu_access() when invalidating output buffers.

 drivers/misc/fastrpc.c | 67 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)
drivers/misc/fastrpc.c +66 -1
index 9e760f68bcaf..39ad0ab88aec 100644
@@ -222,6 +222,7 @@ struct fastrpc_buf_overlap {
 	u64 mstart;
 	u64 mend;
 	u64 offset;
+	bool do_cmo;
 };
 
 struct fastrpc_buf {
@@ -297,6 +298,7 @@ struct fastrpc_session_ctx {
 	int sid;
 	bool used;
 	bool valid;
+	bool coherent;
 };
 
 struct fastrpc_soc_data {
@@ -666,7 +668,9 @@ static int olaps_cmp(const void *a, const void *b)
 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
 {
 	u64 max_end = 0;
+	int max_raix = -1;
 	int i;
+	int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
 
 	for (i = 0; i < ctx->nbufs; ++i) {
 		ctx->olaps[i].start = ctx->args[i].ptr;
@@ -685,7 +689,10 @@ static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
 
 			if (ctx->olaps[i].end > max_end) {
 				max_end = ctx->olaps[i].end;
+				max_raix = ctx->olaps[i].raix;
 			} else {
+				if ((max_raix < inbufs) != (ctx->olaps[i].raix < inbufs))
+					ctx->olaps[i].do_cmo = true;
 				ctx->olaps[i].mend = 0;
 				ctx->olaps[i].mstart = 0;
 			}
@@ -695,6 +702,7 @@ static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
 			ctx->olaps[i].mstart = ctx->olaps[i].start;
 			ctx->olaps[i].offset = 0;
 			max_end = ctx->olaps[i].end;
+			max_raix = ctx->olaps[i].raix;
 		}
 	}
 }
@@ -714,6 +722,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 
 	INIT_LIST_HEAD(&ctx->node);
 	ctx->fl = user;
+	ctx->sc = sc;
 	ctx->nscalars = REMOTE_SCALARS_LENGTH(sc);
 	ctx->nbufs = REMOTE_SCALARS_INBUFS(sc) +
 		     REMOTE_SCALARS_OUTBUFS(sc);
@@ -739,7 +748,6 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 	/* Take a reference to user, released in fastrpc_context_free() */
 	fastrpc_user_get(user);
 
-	ctx->sc = sc;
 	ctx->retval = -1;
 	ctx->pid = current->pid;
 	ctx->client_id = user->client_id;
@@ -1078,6 +1086,54 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
 	return 0;
 }
 
+static void fastrpc_flush_args(struct fastrpc_invoke_ctx *ctx)
+{
+	union fastrpc_remote_arg *rpra = ctx->rpra;
+	int i, inbufs;
+
+	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
+
+	for (i = 0; i < ctx->nbufs; ++i) {
+		int raix = ctx->olaps[i].raix;
+		struct fastrpc_map *map = ctx->maps[raix];
+
+		if (raix + 1 > inbufs)
+			continue;
+		if (!map || !map->buf)
+			continue;
+
+		if (rpra[raix].buf.len && (ctx->olaps[i].mstart || ctx->olaps[i].do_cmo)) {
+			dma_buf_begin_cpu_access(map->buf, DMA_TO_DEVICE);
+			dma_buf_end_cpu_access(map->buf, DMA_TO_DEVICE);
+		}
+	}
+}
+
+static void fastrpc_inv_args(struct fastrpc_invoke_ctx *ctx)
+{
+	union fastrpc_remote_arg *rpra = ctx->rpra;
+	int i, inbufs;
+
+	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
+
+	for (i = 0; i < ctx->nbufs; ++i) {
+		int raix = ctx->olaps[i].raix;
+		struct fastrpc_map *map = ctx->maps[raix];
+
+		if (raix + 1 <= inbufs)
+			continue;
+		if (!rpra[raix].buf.len)
+			continue;
+		if (!map || !map->buf)
+			continue;
+
+		if (ctx->olaps[i].mstart || ctx->olaps[i].do_cmo) {
+			dma_buf_begin_cpu_access(map->buf, DMA_FROM_DEVICE);
+			dma_buf_end_cpu_access(map->buf, DMA_FROM_DEVICE);
+		}
+	}
+}
+
 static struct fastrpc_invoke_buf *fastrpc_invoke_buf_start(union fastrpc_remote_arg *pra, int len)
 {
 	return (struct fastrpc_invoke_buf *)(&pra[len]);
@@ -1202,6 +1258,9 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
 		}
 	}
 
+	if (!ctx->fl->sctx->coherent)
+		fastrpc_flush_args(ctx);
+
 	for (i = ctx->nbufs; i < ctx->nscalars; ++i) {
 		list[i].num = ctx->args[i].length ? 1 : 0;
 		list[i].pgidx = i;
@@ -1387,6 +1446,8 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
 	if (err)
 		goto bail;
 
+	if (!fl->sctx->coherent)
+		fastrpc_inv_args(ctx);
 	/* make sure that all CPU memory writes are seen by DSP */
 	dma_wmb();
 	/* Send invoke buffer to remote dsp */
@@ -1407,6 +1468,9 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
 
 	/* make sure that all memory writes by DSP are seen by CPU */
 	dma_rmb();
+	if (!fl->sctx->coherent)
+		fastrpc_inv_args(ctx);
+
 	/* populate all the output buffers with results */
 	err = fastrpc_put_args(ctx, kernel);
 	if (err)
@@ -2370,6 +2434,7 @@ static int fastrpc_cb_init(struct platform_device *pdev)
 	sess->used = false;
 	sess->valid = true;
 	sess->dev = dev;
+	sess->coherent = of_dma_is_coherent(dev->of_node);
 	sess->sid = sid;
 
 	if (cctx->domain_id == CDSP_DOMAIN_ID)
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 11 lines · Abhinav Parihar

Some platforms using fastrpc do not support DMA coherency on HLOS. On such systems, explicit cache maintenance is required to ensure data consistency for RPC argument buffers.

Add cache maintenance for argument buffers when operating on non-coherent platforms:
- Flush input buffers before invoking RPC to ensure CPU writes are visible to the DSP
- Invalidate output buffers after RPC completion to ensure DSP writes are visible to the CPU

Quoted 7 lines

Introduce helper functions fastrpc_flush_args() and fastrpc_inv_args() to perform the required dma-buf cache operations. These are invoked only when the device is not marked as DMA coherent.

The coherency capability is determined using the "dma-coherent" device tree property and stored per session context.

The dma-mapping abstraction is meant to hide this from drivers, why does this driver need to know?

Quoted 18 lines
drivers/misc/fastrpc.c +13 -0
@@ -1078,6 +1086,54 @@ static int fastrpc_create_maps(struct 
 fastrpc_invoke_ctx *ctx)
 	return 0;
 }
 
+static void fastrpc_flush_args(struct fastrpc_invoke_ctx *ctx)
+{
+	union fastrpc_remote_arg *rpra = ctx->rpra;
+	int i, inbufs;
+
+	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
+
+	for (i = 0; i < ctx->nbufs; ++i) {
+		int raix = ctx->olaps[i].raix;
+		struct fastrpc_map *map = ctx->maps[raix];
+
+		if (raix + 1 > inbufs)
+			continue;

drivers/misc/fastrpc.c static int fastrpc_create_maps(struct · line 1112

Quoted 10 lines
drivers/misc/fastrpc.c +9 -0
+		if (!map || !map->buf)
+			continue;
+
+		if (rpra[raix].buf.len && (ctx->olaps[i].mstart || 
 ctx->olaps[i].do_cmo)) {
+			dma_buf_begin_cpu_access(map->buf, DMA_TO_DEVICE);
+			dma_buf_end_cpu_access(map->buf, DMA_TO_DEVICE);
+		}
+	}
+}

This does not look like an appropriate interface: the driver should not have to worry about platform specific details of 'flush' or 'invalidate' and only access the data between 'begin' and 'end' here. E.g. if there is an iommu or swiotlb, the buffer may become inaccessible again after 'end'.

Can you change this to have separate loops to manage ownership before and after the access?

Arnd

#2 lore raw
In reply to Abhinav Parihar
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] misc: fastrpc: skipped dma_buf cache maintenance due to mstart and overlap - [Medium] misc: fastrpc…

Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] misc: fastrpc: skipped dma_buf cache maintenance due to mstart and overlap
- [Medium] misc: fastrpc: unchecked dma_buf_begin_cpu_access() in cache helpers
- [Medium] misc: fastrpc: redundant full-buffer cache passes in fastrpc_inv_args()
- [Medium] misc: fastrpc: repeated full-dma_buf cache flushes for shared dma_buf args
--

commit 38d28b3641886dcb390560c25b96a33e1bd0dc19
Author: Abhinav Parihar <[email protected]>

misc: fastrpc: Add cache maintenance for non-coherent platforms

This commit adds DMA-buf cache flush and invalidation helpers for RPC argument buffers on platforms whose session device is not DMA-coherent.

Quoted 4 lines
drivers/misc/fastrpc.c +0 -0
index 9e760f68bcaf2..39ad0ab88aec3 100644

[ ... ]

Quoted 22 lines
drivers/misc/fastrpc.c +5 -0
@@ -666,7 +668,9 @@ static int olaps_cmp(const void *a, const void *b)
 static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
 {
 	u64 max_end = 0;
+	int max_raix = -1;
 	int i;
+	int inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
 
 	for (i = 0; i < ctx->nbufs; ++i) {
 		ctx->olaps[i].start = ctx->args[i].ptr;
@@ -685,7 +689,10 @@ static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
 
 			if (ctx->olaps[i].end > max_end) {
 				max_end = ctx->olaps[i].end;
+				max_raix = ctx->olaps[i].raix;
 			} else {
+				if ((max_raix < inbufs) != (ctx->olaps[i].raix < inbufs))
+					ctx->olaps[i].do_cmo = true;
 				ctx->olaps[i].mend = 0;
 				ctx->olaps[i].mstart = 0;
 			}

drivers/misc/fastrpc.c static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx) · line 708

Quoted 7 lines
drivers/misc/fastrpc.c +1 -0
@@ -695,6 +702,7 @@ static void fastrpc_get_buff_overlaps(struct fastrpc_invoke_ctx *ctx)
 			ctx->olaps[i].mstart = ctx->olaps[i].start;
 			ctx->olaps[i].offset = 0;
 			max_end = ctx->olaps[i].end;
+			max_raix = ctx->olaps[i].raix;
 		}
 	}
 }

[Severity: High]
Can fastrpc_get_buff_overlaps() leave both mstart and do_cmo as zero for mapped DMA-buf arguments that still require cache maintenance in fastrpc_flush_args() and fastrpc_inv_args()?

First, for a mapped DMA-buf argument passed with args[i].ptr == 0 and a non-zero length (such as in fastrpc_init_create_process() when init.file is 0, or in fastrpc_get_args() where vma_lookup() is optional), the non-overlap branch sets mstart = start = 0 and leaves do_cmo = false. Because fastrpc_flush_args() and fastrpc_inv_args() check (ctx->olaps[i].mstart || ctx->olaps[i].do_cmo) rather than a non-empty range, cache maintenance is skipped whenever mstart is 0.

Second, overlaps are computed solely from user virtual addresses across all arguments without checking whether overlapping entries share the same DMA-buf FD or even have a map, and only a single max_raix is tracked. If an inline buffer or a different DMA-buf of the same direction encloses a mapped DMA-buf range, or if an input DMA-buf is partially overlapped by an output buffer that advances max_end and overwrites max_raix before an enclosed output range referencing the first DMA-buf is checked, mstart is zeroed and do_cmo remains false, skipping the flush or invalidation and leaving stale data between the CPU and DSP.

[ ... ]

Quoted 13 lines
drivers/misc/fastrpc.c +9 -0
@@ -1078,6 +1086,54 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
 	return 0;
 }
 
+static void fastrpc_flush_args(struct fastrpc_invoke_ctx *ctx)
+{
+	union fastrpc_remote_arg *rpra = ctx->rpra;
+	int i, inbufs;
+
+	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
+
+	for (i = 0; i < ctx->nbufs; ++i) {
+		int raix = ctx->olaps[i].raix;

drivers/misc/fastrpc.c static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) · line 1107

Quoted 10 lines
drivers/misc/fastrpc.c +10 -0
+		struct fastrpc_map *map = ctx->maps[raix];
+
+		if (raix + 1 > inbufs)
+			continue;
+		if (!map || !map->buf)
+			continue;
+
+		if (rpra[raix].buf.len && (ctx->olaps[i].mstart || ctx->olaps[i].do_cmo)) {
+			dma_buf_begin_cpu_access(map->buf, DMA_TO_DEVICE);
+			dma_buf_end_cpu_access(map->buf, DMA_TO_DEVICE);

[Severity: Medium]
Should fastrpc_flush_args() and fastrpc_inv_args() check the return value of dma_buf_begin_cpu_access() and propagate errors to their callers? dma_buf_begin_cpu_access() can fail (for example with -ERESTARTSYS while waiting interruptibly on reservation fences in __dma_buf_begin_cpu_access()), and ignoring the failure unconditionally calls dma_buf_end_cpu_access() and proceeds with the RPC on unsynchronized buffers.

drivers/misc/fastrpc.c static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) · line 1110

Quoted 3 lines
drivers/misc/fastrpc.c +3 -0
+		}
+	}
+}

[Severity: Medium]
Does iterating over ctx->nbufs without deduplicating by map->buf flush or invalidate the entire parent dma_buf multiple times when several argument slices share one DMA-buf FD?

dma_buf_begin_cpu_access() and dma_buf_end_cpu_access() synchronize the full dma_buf scatterlist rather than the argument sub-range, so K non-enclosed (or do_cmo) slices carved from the same dma_buf trigger K full-buffer cache walks in fastrpc_flush_args() and fastrpc_inv_args().

Quoted 25 lines
drivers/misc/fastrpc.c +25 -0
+
+static void fastrpc_inv_args(struct fastrpc_invoke_ctx *ctx)
+{
+	union fastrpc_remote_arg *rpra = ctx->rpra;
+	int i, inbufs;
+
+	inbufs = REMOTE_SCALARS_INBUFS(ctx->sc);
+
+	for (i = 0; i < ctx->nbufs; ++i) {
+		int raix = ctx->olaps[i].raix;
+		struct fastrpc_map *map = ctx->maps[raix];
+
+		if (raix + 1 <= inbufs)
+			continue;
+		if (!rpra[raix].buf.len)
+			continue;
+		if (!map || !map->buf)
+			continue;
+
+		if (ctx->olaps[i].mstart || ctx->olaps[i].do_cmo) {
+			dma_buf_begin_cpu_access(map->buf, DMA_FROM_DEVICE);
+			dma_buf_end_cpu_access(map->buf, DMA_FROM_DEVICE);
+		}
+	}
+}

[ ... ]

Quoted 10 lines
drivers/misc/fastrpc.c +2 -0
@@ -1387,6 +1446,8 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
 	if (err)
 		goto bail;
 
+	if (!fl->sctx->coherent)
+		fastrpc_inv_args(ctx);
 	/* make sure that all CPU memory writes are seen by DSP */
 	dma_wmb();
 	/* Send invoke buffer to remote dsp */

drivers/misc/fastrpc.c static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, · line 1472

Quoted 5 lines
drivers/misc/fastrpc.c +2 -0
@@ -1407,6 +1468,9 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
 
 	/* make sure that all memory writes by DSP are seen by CPU */
 	dma_rmb();
+	if (!fl->sctx->coherent)
+		fastrpc_inv_args(ctx);

[Severity: Medium]
Why does fastrpc_inv_args() call both dma_buf_begin_cpu_access() and dma_buf_end_cpu_access() both before and after the RPC invocation?

In standard DMA-buf exporters (such as system_heap and cma_heap), begin_cpu_access(DMA_FROM_DEVICE) syncs the buffer for the CPU (cache invalidate on arm64) and end_cpu_access(DMA_FROM_DEVICE) syncs it for the device (cache clean on arm64). Calling fastrpc_inv_args() before fastrpc_invoke_send() and again after fastrpc_wait_for_completion() performs four full-buffer cache passes per output buffer instead of syncing for the device before the RPC and syncing for the CPU after completion.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1