ib_map_mr_sg() uses the DMA address of the first page as iova of the MR and smbdirect sends that iova to the peer as offset of the buffer descriptor, so the peer learns our DMA addresses, without an IOMMU these are the physical addresses of the buffers. The peer just uses the offset as base address of its RDMA READs and WRITEs, so the value is up to us, the hardware only needs the offset within the first page in the low bits of the iova. So keep these bits, leave the rest of the low 24 bits zero (more for a page size above 16 MiB), so that the addresses the peer uses show the position within the MR in traces, and put random bits above them up to bit 55, which also makes it harder to reuse an old offset with a token whose key wrapped around. The top byte is always 0, so iova + length stays far below 2^64 even for a length of U32_MAX. This also avoids a problem with irdma: with an IOMMU that allocates IOVAs top-down, the first 64-bit IOVA range of a device ends exactly at 2^64. If that is the buffer of an MR, iova + length is 0 and irdma rejects the RDMA READ of the peer with IRDMA_AE_AMP_TO_WRAP (ae_id 0x10a), even if the last byte of the MR is at 2^64 - 1, so the connection is torn down. Fixes: c7398583340a ("CIFS: SMBD: Implement RDMA memory registration") Cc: stable@vger.kernel.org Cc: Namjae Jeon Cc: Paulo Alcantara Cc: Tom Talpey Cc: Tatyana Nikolova Cc: Jacob Moroni Cc: linux-cifs@vger.kernel.org Cc: samba-technical@lists.samba.org Cc: linux-rdma@vger.kernel.org Reviewed-by: Paulo Alcantara Signed-off-by: Stefan Metzmacher --- fs/smb/smbdirect/mr.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/fs/smb/smbdirect/mr.c b/fs/smb/smbdirect/mr.c index 6c37206474bb..dd64ec50bec1 100644 --- a/fs/smb/smbdirect/mr.c +++ b/fs/smb/smbdirect/mr.c @@ -5,6 +5,7 @@ */ #include "internal.h" +#include /* * Allocate MRs used for RDMA read/write @@ -314,6 +315,8 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, struct smbdirect_mr_io *mr; int ret, num_pages, num_mapped; struct ib_reg_wr *reg_wr; + u64 random_bits, page_offset; + unsigned int shift; unsigned long flags; num_pages = iov_iter_npages(iter, sp->max_frmr_depth + 1); @@ -393,6 +396,32 @@ smbdirect_connection_register_mr_io(struct smbdirect_socket *sc, goto map_mr_error; } + /* + * ib_map_mr_sg() uses the DMA address of the first page as + * iova, which goes to the peer as offset of the buffer + * descriptor. Don't expose DMA addresses (without an IOMMU + * physical addresses) to the peer, the hardware only needs + * the offset within the first page in the low bits, all + * other bits are up to us. + * + * The low 24 bits (or more for a page size above 16 MiB) are + * only that page offset, so in traces they show the position + * within the MR plus that offset (an SMB3 READ/WRITE is at + * most 16 MiB). The bits above up to bit 55 are random, which + * makes it harder to reuse an old offset with a token whose + * key wrapped around. + * + * The top byte is always 0, so iova + length stays far below + * 2^64 even for a length of U32_MAX. irdma rejects the access + * to an MR ending exactly at 2^64 with IRDMA_AE_AMP_TO_WRAP, + * which happened with an IOMMU that allocates the first + * 64-bit IOVA range at the very top. + */ + shift = max_t(unsigned int, 24, ilog2(mr->mr->page_size)); + random_bits = ((u64)get_random_u32() << shift) & GENMASK_ULL(55, 24); + page_offset = mr->mr->iova & (mr->mr->page_size - 1); + mr->mr->iova = random_bits | page_offset; + ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey)); if (mr->mr->rkey == mr->base_tag) ib_update_fast_reg_key(mr->mr, ib_inc_rkey(mr->mr->rkey)); -- 2.43.0