From patchwork Mon Aug 19 14:03:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattias Nissler X-Patchwork-Id: 13768413 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 501D7C52D6F for ; Mon, 19 Aug 2024 14:04:17 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sg2zO-0003yD-Lo; Mon, 19 Aug 2024 10:04:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sg2zN-0003xN-EJ for qemu-devel@nongnu.org; Mon, 19 Aug 2024 10:04:05 -0400 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sg2zK-00072u-Fr for qemu-devel@nongnu.org; Mon, 19 Aug 2024 10:04:05 -0400 Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2d3c0d587e4so2937103a91.2 for ; Mon, 19 Aug 2024 07:04:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724076241; x=1724681041; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=t+hjeHCHtdkd2RYRfSgVvQKkJ7Af9cnuMXy3JgW7EfI=; b=MH2WaFpnVvHbab6ZzSX204oQLvhZXLrfAxRLt6SZfJSg3ZekqcjpDfuoYBoDVotLiw sG5bXbDMpH2iYfz0QimhhapWM1JUwtA52sZBEGcP+XarF4mPQEJ42ALkWbdC9iFrL3MF eA8ZfbBZ83xYGEMZ7MYaiJQle3HdNnBhlkHr8kEAVR6PDX03d99qoP0Vt/JMGnmSHM5x lG1eSb7g1qjFeOlviimlzAUH0xPBL8Rm6lgv0zIT+ibm/VJ0G8fGOkA0sZGGGT9jIunx mFPedRRQvoH8Q/F7NWUL3LzkoxeOjEsGoOZWcD6cEs2//iGLr4MYdkb2qBRt6TTVfPQc SL/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724076241; x=1724681041; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=t+hjeHCHtdkd2RYRfSgVvQKkJ7Af9cnuMXy3JgW7EfI=; b=qBtAuEf4/eawK9vyZ2qZL7PJd7esfaSlyRqDuFR2amT24K7cKo6HRoGW67mVtYFAUV y2gKrbQxK1GSjS5KavX6NR9HSFw3Idnz/SJG0hQEj2WAyN8SdpYb797WHVQ2R0ZjRsPT XYzJcRLpjReEXLyKLEpvT0C4yFu9BDk2akQlURXTFzaKkIoy75GxLf7zyS684GhqXGWD IW7d5JgAkxE6AtX8MPVLgRIXDws+XpBudLuw1PHCoOYaTpnehNlVopwD9sLxTa9d4AQd jXgPtV8sfroHR/GBoElWcHtJezFa531SYGHQrYYSr/YWNcsms/mGFYW7oDtQ1LqtY3x4 ChVw== X-Forwarded-Encrypted: i=1; AJvYcCXldM/1p9c9nYW1crcf35+fAvgRuF0Nrdp4OpBWj3kUXEtcA/GiOfiNZLgx5wvH1Rzjgy0kqsM5b5RcwzSSMeEIDHtequY= X-Gm-Message-State: AOJu0YzfmWH4lW6TOFeXnEbs8LjIRMXTAoJXH5jjk7pqkvrMXJSkS+9t P7zNSgB3kxrrK4hdHS9uPyyAYTXlDzau9EeXJRZP1KP3Cw8oeQtpTvYMM8rJTqc= X-Google-Smtp-Source: AGHT+IHIK46eRb2N1D/vSf1jCS5eHuWV8eZSxFwv2JYV7ANzzFlfKWrR7nzTyGLgfqa5VhSJ/f6kgg== X-Received: by 2002:a17:90a:3986:b0:2d3:ba42:775c with SMTP id 98e67ed59e1d1-2d3dfc368e8mr10120665a91.1.1724076240900; Mon, 19 Aug 2024 07:04:00 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3d0b3b6fcsm8341174a91.43.2024.08.19.07.04.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2024 07:04:00 -0700 (PDT) From: Mattias Nissler To: jag.raman@oracle.com, qemu-devel@nongnu.org Cc: Elena Ufimtseva , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-Daud=C3=A9?= , Peter Xu , john.levon@nutanix.com, Marcel Apfelbaum , David Hildenbrand , "Michael S. Tsirkin" , Paolo Bonzini , Mattias Nissler Subject: [PATCH v11 1/2] Update subprojects/libvfio-user Date: Mon, 19 Aug 2024 07:03:54 -0700 Message-Id: <20240819140355.2958821-2-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240819140355.2958821-1-mnissler@rivosinc.com> References: <20240819140355.2958821-1-mnissler@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=mnissler@rivosinc.com; helo=mail-pj1-x1036.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Brings in assorted bug fixes. The following are of particular interest with respect to message-based DMA support: * bb308a2 "Fix address calculation for message-based DMA" Corrects a bug in DMA address calculation. * 1569a37 "Pass server->client command over a separate socket pair" Adds support for separate sockets for either command direction, addressing a bug where libvfio-user gets confused if both client and server send commands concurrently. Reviewed-by: Jagannathan Raman Signed-off-by: Mattias Nissler --- subprojects/libvfio-user.wrap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subprojects/libvfio-user.wrap b/subprojects/libvfio-user.wrap index 416955ca45..3dd08768ed 100644 --- a/subprojects/libvfio-user.wrap +++ b/subprojects/libvfio-user.wrap @@ -1,4 +1,4 @@ [wrap-git] url = https://gitlab.com/qemu-project/libvfio-user.git -revision = 0b28d205572c80b568a1003db2c8f37ca333e4d7 +revision = b1a156d86f55a8fa3f78ece5bee7748ec75e7b82 depth = 1 From patchwork Mon Aug 19 14:03:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mattias Nissler X-Patchwork-Id: 13768415 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 206D1C3DA4A for ; Mon, 19 Aug 2024 14:04:46 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sg2zQ-000453-8R; Mon, 19 Aug 2024 10:04:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sg2zO-0003yK-JM for qemu-devel@nongnu.org; Mon, 19 Aug 2024 10:04:06 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sg2zM-000731-1C for qemu-devel@nongnu.org; Mon, 19 Aug 2024 10:04:06 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-2d3c05ec278so2940356a91.0 for ; Mon, 19 Aug 2024 07:04:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724076242; x=1724681042; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=EMvjgF3oAVY7GBl6lGcISH8txA+B5nR6CJ8hZiXV7Hg=; b=GxDoU92jXhK7O0wdxgxOOiM3o5uAmJW6zhbELQ+4WM8sQQeG1TK/zS99rUPBOROYMw YKKeTR3RZiC/8pnIuwNSEgEmcr8E7nJ3N/DWChlR/tZQiKMfQ2WfsYqJU2h9TEy6w4Cl thLnOAoqxMLUjTPN+Ztdy0/tufG2Zhyc03sD7F74Bn+9Qx6jEFkMKRiiRtDiKKkHUGd/ /hNH8ArKiVUc/awSLeJo8EwxXwUDIASI+LTGz9Ghu3j2hD0X+9AzXafAJg1wCaE4nt1U 6/j6Za4aYbR85q3X4t8ClB0wUXyRZFbNazRgwdYF+36VOqCu/Bqct7qOvgQblQ1juPzu GkQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724076242; x=1724681042; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=EMvjgF3oAVY7GBl6lGcISH8txA+B5nR6CJ8hZiXV7Hg=; b=xMZ6VVZZ7pstKUQ2FprJdLVwFC2oa/NzinocxfNN4DN8zf492wwgpm+m+FDR9zkisG BsipVRe2BLVdB+ZrVYhlIys0zkpAKEOuJbZ+z43DEcQuXF87qe3Uz9gg5g1S8fdQbW/Q KzsX/h87Qj+zfxwFaPql0RPsZ+PzCssyDmOST4UWIZuqn1D8lsQQuXHgAt3g6y80pN1j 7taS+Nx9vxWaeXGOqMUasiv07u6JUjysf8CblZTCkzjN3DNLw6rUompsDGE97SUTv1bo fMhlSJxwH2PJFgVAoVJgdO3XGHIJFVQy567M7qi3oFlcwfA8XDd3Q2eKhhbNikugYAyz I/8g== X-Forwarded-Encrypted: i=1; AJvYcCUD7GrJAZoL7/XXvxANrxiCvfEKwcvTWQVSTYilfMJH2UZPlmxXTUVlL2kEBk+LH2MGMAloFfc1tiSBdTG8Q8qcHdT4NL4= X-Gm-Message-State: AOJu0YzH97zz58lAY4u7P+zdRMjMXaO1xpP3tvCW3Kwz8xZ2GMtNNi/s 91oy9tx0zLOUBanksHXtB2CgHRNI9SS/94KsTWypMRtJ2D/wvAO8hASy+V+2dRW+4C9J97HgKqV ZOWM= X-Google-Smtp-Source: AGHT+IHCVhBvw9tPBVtcjrlisdNgtbq0xGQlkfJNmOs+H2TegHqckkHKz0RToJ60Gl6QBJI2GN2SsQ== X-Received: by 2002:a17:90b:4004:b0:2d3:cd41:77a7 with SMTP id 98e67ed59e1d1-2d3dfc37cb0mr9980717a91.3.1724076242038; Mon, 19 Aug 2024 07:04:02 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d3d0b3b6fcsm8341174a91.43.2024.08.19.07.04.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2024 07:04:01 -0700 (PDT) From: Mattias Nissler To: jag.raman@oracle.com, qemu-devel@nongnu.org Cc: Elena Ufimtseva , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-Daud=C3=A9?= , Peter Xu , john.levon@nutanix.com, Marcel Apfelbaum , David Hildenbrand , "Michael S. Tsirkin" , Paolo Bonzini , Mattias Nissler Subject: [PATCH v11 2/2] vfio-user: Message-based DMA support Date: Mon, 19 Aug 2024 07:03:55 -0700 Message-Id: <20240819140355.2958821-3-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240819140355.2958821-1-mnissler@rivosinc.com> References: <20240819140355.2958821-1-mnissler@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=mnissler@rivosinc.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Wire up support for DMA for the case where the vfio-user client does not provide mmap()-able file descriptors, but DMA requests must be performed via the VFIO-user protocol. This installs an indirect memory region, which already works for pci_dma_{read,write}, and pci_dma_map works thanks to the existing DMA bounce buffering support. Note that while simple scenarios work with this patch, there's a known race condition in libvfio-user that will mess up the communication channel. See https://github.com/nutanix/libvfio-user/issues/279 for details as well as a proposed fix. Reviewed-by: Jagannathan Raman Signed-off-by: Mattias Nissler --- hw/remote/trace-events | 2 + hw/remote/vfio-user-obj.c | 100 ++++++++++++++++++++++++++++++++------ 2 files changed, 87 insertions(+), 15 deletions(-) diff --git a/hw/remote/trace-events b/hw/remote/trace-events index 0d1b7d56a5..358a68fb34 100644 --- a/hw/remote/trace-events +++ b/hw/remote/trace-events @@ -9,6 +9,8 @@ vfu_cfg_read(uint32_t offset, uint32_t val) "vfu: cfg: 0x%x -> 0x%x" vfu_cfg_write(uint32_t offset, uint32_t val) "vfu: cfg: 0x%x <- 0x%x" vfu_dma_register(uint64_t gpa, size_t len) "vfu: registering GPA 0x%"PRIx64", %zu bytes" vfu_dma_unregister(uint64_t gpa) "vfu: unregistering GPA 0x%"PRIx64"" +vfu_dma_read(uint64_t gpa, size_t len) "vfu: DMA read 0x%"PRIx64", %zu bytes" +vfu_dma_write(uint64_t gpa, size_t len) "vfu: DMA write 0x%"PRIx64", %zu bytes" vfu_bar_register(int i, uint64_t addr, uint64_t size) "vfu: BAR %d: addr 0x%"PRIx64" size 0x%"PRIx64"" vfu_bar_rw_enter(const char *op, uint64_t addr) "vfu: %s request for BAR address 0x%"PRIx64"" vfu_bar_rw_exit(const char *op, uint64_t addr) "vfu: Finished %s of BAR address 0x%"PRIx64"" diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c index 8dbafafb9e..0e93d7a7b4 100644 --- a/hw/remote/vfio-user-obj.c +++ b/hw/remote/vfio-user-obj.c @@ -300,6 +300,63 @@ static ssize_t vfu_object_cfg_access(vfu_ctx_t *vfu_ctx, char * const buf, return count; } +static MemTxResult vfu_dma_read(void *opaque, hwaddr addr, uint64_t *val, + unsigned size, MemTxAttrs attrs) +{ + MemoryRegion *region = opaque; + vfu_ctx_t *vfu_ctx = VFU_OBJECT(region->owner)->vfu_ctx; + uint8_t buf[sizeof(uint64_t)]; + + trace_vfu_dma_read(region->addr + addr, size); + + g_autofree dma_sg_t *sg = g_malloc0(dma_sg_size()); + vfu_dma_addr_t vfu_addr = (vfu_dma_addr_t)(region->addr + addr); + if (vfu_addr_to_sgl(vfu_ctx, vfu_addr, size, sg, 1, PROT_READ) < 0 || + vfu_sgl_read(vfu_ctx, sg, 1, buf) != 0) { + return MEMTX_ERROR; + } + + *val = ldn_he_p(buf, size); + + return MEMTX_OK; +} + +static MemTxResult vfu_dma_write(void *opaque, hwaddr addr, uint64_t val, + unsigned size, MemTxAttrs attrs) +{ + MemoryRegion *region = opaque; + vfu_ctx_t *vfu_ctx = VFU_OBJECT(region->owner)->vfu_ctx; + uint8_t buf[sizeof(uint64_t)]; + + trace_vfu_dma_write(region->addr + addr, size); + + stn_he_p(buf, size, val); + + g_autofree dma_sg_t *sg = g_malloc0(dma_sg_size()); + vfu_dma_addr_t vfu_addr = (vfu_dma_addr_t)(region->addr + addr); + if (vfu_addr_to_sgl(vfu_ctx, vfu_addr, size, sg, 1, PROT_WRITE) < 0 || + vfu_sgl_write(vfu_ctx, sg, 1, buf) != 0) { + return MEMTX_ERROR; + } + + return MEMTX_OK; +} + +static const MemoryRegionOps vfu_dma_ops = { + .read_with_attrs = vfu_dma_read, + .write_with_attrs = vfu_dma_write, + .endianness = DEVICE_HOST_ENDIAN, + .valid = { + .min_access_size = 1, + .max_access_size = 8, + .unaligned = true, + }, + .impl = { + .min_access_size = 1, + .max_access_size = 8, + }, +}; + static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) { VfuObject *o = vfu_get_private(vfu_ctx); @@ -308,17 +365,30 @@ static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) g_autofree char *name = NULL; struct iovec *iov = &info->iova; - if (!info->vaddr) { - return; - } - name = g_strdup_printf("mem-%s-%"PRIx64"", o->device, - (uint64_t)info->vaddr); + (uint64_t)iov->iov_base); subregion = g_new0(MemoryRegion, 1); - memory_region_init_ram_ptr(subregion, NULL, name, - iov->iov_len, info->vaddr); + if (info->vaddr) { + memory_region_init_ram_ptr(subregion, OBJECT(o), name, + iov->iov_len, info->vaddr); + } else { + /* + * Note that I/O regions' MemoryRegionOps handle accesses of at most 8 + * bytes at a time, and larger accesses are broken down. However, + * many/most DMA accesses are larger than 8 bytes and VFIO-user can + * handle large DMA accesses just fine, thus this size restriction + * unnecessarily hurts performance, in particular given that each + * access causes a round trip on the VFIO-user socket. + * + * TODO: Investigate how to plumb larger accesses through memory + * regions, possibly by amending MemoryRegionOps or by creating a new + * memory region type. + */ + memory_region_init_io(subregion, OBJECT(o), &vfu_dma_ops, subregion, + name, iov->iov_len); + } dma_as = pci_device_iommu_address_space(o->pci_dev); @@ -330,20 +400,20 @@ static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) static void dma_unregister(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) { VfuObject *o = vfu_get_private(vfu_ctx); + MemoryRegionSection mr_section; AddressSpace *dma_as = NULL; - MemoryRegion *mr = NULL; - ram_addr_t offset; - mr = memory_region_from_host(info->vaddr, &offset); - if (!mr) { + dma_as = pci_device_iommu_address_space(o->pci_dev); + + mr_section = + memory_region_find(dma_as->root, (hwaddr)info->iova.iov_base, 1); + if (!mr_section.mr) { return; } - dma_as = pci_device_iommu_address_space(o->pci_dev); - - memory_region_del_subregion(dma_as->root, mr); + memory_region_del_subregion(dma_as->root, mr_section.mr); - object_unparent((OBJECT(mr))); + object_unparent((OBJECT(mr_section.mr))); trace_vfu_dma_unregister((uint64_t)info->iova.iov_base); }