git: 4085bde9fa2e - main - linuxkpi: Fix return value of dma_map_sgtable

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Tue, 06 Dec 2022 15:27:40 UTC
The branch main has been updated by manu:

URL: https://cgit.FreeBSD.org/src/commit/?id=4085bde9fa2ea7ebe861a2e4a07312aac00a26fc

commit 4085bde9fa2ea7ebe861a2e4a07312aac00a26fc
Author:     Austin Shafer <ashafer@badland.io>
AuthorDate: 2022-12-06 15:25:53 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2022-12-06 15:25:53 +0000

    linuxkpi: Fix return value of dma_map_sgtable
    
    dma_map_sgtable internally uses the dma_map_sg_attrs helper. The problem is
    that dma_map_sg_attrs returns the number of entries mapped, whereas
    dma_map_sgtable returns nonzero on failure. This leads to dma_map_sgtable
    returning non-zero-but-positive values which tricks other areas of the stack
    into thinking nents is a valid pointer.
    
    This checks if nents is valid and returns zero if so, updating the nents field
    in sgt. This fixes PRIME render offload with nvidia-drm.
    
    Fixes:  9202c95f47c2 ("linuxkpi: Add dma_{un,}map_sgtable")
---
 sys/compat/linuxkpi/common/include/linux/dma-mapping.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/sys/compat/linuxkpi/common/include/linux/dma-mapping.h b/sys/compat/linuxkpi/common/include/linux/dma-mapping.h
index d3d25fcce857..7f2579787b4b 100644
--- a/sys/compat/linuxkpi/common/include/linux/dma-mapping.h
+++ b/sys/compat/linuxkpi/common/include/linux/dma-mapping.h
@@ -362,7 +362,14 @@ dma_map_sgtable(struct device *dev, struct sg_table *sgt,
     unsigned long attrs)
 {
 
-	return (dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs));
+	int nents = dma_map_sg_attrs(dev, sgt->sgl, sgt->nents, dir, attrs);
+
+	if (nents < 0) {
+		return nents;
+	} else {
+		sgt->nents = nents;
+		return 0;
+	}
 }
 
 static inline void