git: d87ebf35c828 - stable/14 - mlx5: Zero DMA memory mlx5_alloc_cmd_msg() and alloc_cmd_page()

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 01 Feb 2024 14:02:57 UTC
The branch stable/14 has been updated by markj:

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

commit d87ebf35c82843874e2b84e454ba4fbcea4b16b7
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-01-18 21:47:52 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-02-01 14:02:48 +0000

    mlx5: Zero DMA memory mlx5_alloc_cmd_msg() and alloc_cmd_page()
    
    These functions may map more memory for DMA than is actually used, since
    the allocator operates on multiples of a 4KB page size.  Thus,
    bus_dmamap_sync() can trigger KMSAN reports when the unused portion of
    a page is not zero-ed.
    
    Reported by:    KMSAN
    Reviewed by:    kib
    MFC after:      2 weeks
    Sponsored by:   Klara, Inc.
    Sponsored by:   Juniper Networks, Inc.
    Differential Revision:  https://reviews.freebsd.org/D43133
    
    (cherry picked from commit 47a6fb9d5a2ebec12114a604053ffbd2929f0021)
---
 sys/dev/mlx5/mlx5_core/mlx5_cmd.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/sys/dev/mlx5/mlx5_core/mlx5_cmd.c b/sys/dev/mlx5/mlx5_core/mlx5_cmd.c
index 721b27b9120c..d46feb4b9e5b 100644
--- a/sys/dev/mlx5/mlx5_core/mlx5_cmd.c
+++ b/sys/dev/mlx5/mlx5_core/mlx5_cmd.c
@@ -1113,11 +1113,16 @@ mlx5_alloc_cmd_msg(struct mlx5_core_dev *dev, gfp_t flags, size_t size)
 
 		block = mlx5_fwp_get_virt(msg, i * MLX5_CMD_MBOX_SIZE);
 
-		memset(block, 0, MLX5_CMD_MBOX_SIZE);
-
 		if (i != (n - 1)) {
+			memset(block, 0, MLX5_CMD_MBOX_SIZE);
+
 			u64 dma = mlx5_fwp_get_dma(msg, (i + 1) * MLX5_CMD_MBOX_SIZE);
 			block->next = cpu_to_be64(dma);
+		} else {
+			/* Zero the rest of the page to satisfy KMSAN. */
+			memset(block, 0, MLX5_ADAPTER_PAGE_SIZE -
+			    (i % MLX5_NUM_CMDS_IN_ADAPTER_PAGE) *
+			    MLX5_CMD_MBOX_SIZE);
 		}
 		block->block_num = cpu_to_be32(i);
 	}
@@ -1508,6 +1513,7 @@ alloc_cmd_page(struct mlx5_core_dev *dev, struct mlx5_cmd *cmd)
 	}
 	cmd->dma = mlx5_fwp_get_dma(cmd->cmd_page, 0);
 	cmd->cmd_buf = mlx5_fwp_get_virt(cmd->cmd_page, 0);
+	memset(cmd->cmd_buf, 0, MLX5_ADAPTER_PAGE_SIZE);
 	return (0);
 
 failure_alloc_page: