Skip to content

Commit

Permalink
Fix DMA message size calculation
Browse files Browse the repository at this point in the history
When performing DMA via VFIO-user commands over the socket,
vfu_dma_transfer breaks large requests into chunks according to the
client's maximum data transfer size negotiated at connection setup time.
This change fixes the calculation of the chunk size for the case where
the last chunk is less than the maximum transfer size.

Unfortunately, the existing test didn't catch this due to the request
size being a multiple of that maximum data transfer size. Adjust the
test to make the last chunk size a true remainder.

Signed-off-by: Mattias Nissler <[email protected]>
  • Loading branch information
mnissler-rivos committed Jan 24, 2024
1 parent 2c6239a commit 70dcf91
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions lib/libvfio-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -2312,13 +2312,14 @@ vfu_dma_transfer(vfu_ctx_t *vfu_ctx, enum vfio_user_command cmd,
if (cmd == VFIO_USER_DMA_WRITE) {
memcpy(rbuf + sizeof(*dma_req), data + count, dma_req->count);

ret = vfu_ctx->tran->send_msg(vfu_ctx, msg_id++, VFIO_USER_DMA_WRITE,
rbuf, rlen, NULL,
ret = vfu_ctx->tran->send_msg(vfu_ctx, msg_id++,
VFIO_USER_DMA_WRITE, rbuf,
dma_req->count + sizeof(*dma_req), NULL,
dma_reply, sizeof(*dma_reply));
} else {
ret = vfu_ctx->tran->send_msg(vfu_ctx, msg_id++, VFIO_USER_DMA_READ,
dma_req, sizeof(*dma_req), NULL,
rbuf, rlen);
dma_req, sizeof(*dma_req), NULL, rbuf,
dma_req->count + sizeof(*dma_reply));
}

if (ret < 0) {
Expand Down
2 changes: 1 addition & 1 deletion test/py/test_sgl_read_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_dma_read_write():
def test_dma_read_write_large():
ret, sg = vfu_addr_to_sgl(ctx,
dma_addr=MAP_ADDR + 0x1000,
length=2 * PAGE_SIZE,
length=2 * PAGE_SIZE + 42,
max_nr_sgs=1,
prot=mmap.PROT_READ | mmap.PROT_WRITE)
assert ret == 1
Expand Down

0 comments on commit 70dcf91

Please sign in to comment.