From nobody Sun Dec 22 06:20:48 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1724773852; cv=none; d=zohomail.com; s=zohoarc; b=cRkd6b+C2DDnIvvE90xhhi7iU0FiciOzQwXLl95dJ17auIf2yXDJJXlLPJtYMJHo1nYl0WvTiAbklK6RG7qIH8susLkcT+JHrbTZG1W3UakmLSoxK3vUX3urz/DxZMUjYAV195RgyVaal/3bQI+Mg2y2K0jxlb2/tPTg028U6hY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1724773852; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=t+hjeHCHtdkd2RYRfSgVvQKkJ7Af9cnuMXy3JgW7EfI=; b=mwPd7oDd+JXpg1MJcHT4Mpi51YkgOL4ILQ5tYzmn988IVz+dUoeC4pnVxG2ScVLekBXRgXj8/WwnhPiqm01EkZiAPQVO6B9EIpirpSk5MKRQvfdmhbYkUhGLuo3NVwoffrpejprWpOQdwjmFa+FM7OuPgX382cIXJNMW9l3aPNo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTP id 1724773852392566.7436261788771; Tue, 27 Aug 2024 08:50:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1siyRj-0003Q5-Ph; Tue, 27 Aug 2024 11:49:27 -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 1siyRh-0003Ok-Js for qemu-devel@nongnu.org; Tue, 27 Aug 2024 11:49:25 -0400 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1siyRf-0004md-80 for qemu-devel@nongnu.org; Tue, 27 Aug 2024 11:49:25 -0400 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-2d3da6d3f22so4403435a91.0 for ; Tue, 27 Aug 2024 08:49:22 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb9037a2sm14624478a91.13.2024.08.27.08.49.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 08:49:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724773762; x=1725378562; 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=rXEJttXXRMF1F6dv0oMdwJqQvc4Dh+9Lbp1kEkkplwi+xX0FTwjkYId9ivshc9MneP mUkzmnei68byi0eFqMknN48Q4Tz7uExscZQh67qRXSXGd5ax73KV7pCz3RB+KnEtYhpB /2Ccvpm6XseD+Dtjf8CHltJEorJ+oGvjdJqgZrwzz6oicJ7FsVrsP2E5ktTz0od0nUKI WCLuRbqO1UtpWv69012vf2p++2Y2nJchTowipHLHlTYm/DdyJD15xPlab2A1HVrQyD40 IEQ4c5CoGXfoXzEWfmu+sNOzmd3W6XyGII+gX5eIy8qXJW//mdQ493agvQZ5okAZspWQ euhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724773762; x=1725378562; 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=tMSA9uiHN3j8NXCNwwt7c+qBVFHY2x0XKtQgS3qqpdBOXSD9xQNSPMSf556coY9sik pvdWix029RyYonCaZNUaFXCDKhE6taYMCrPcovuGhTHRmMHbKxcwrIAWDOEhtWgN7aqD gMdLv9StQIfUno6LW9xJq7sDfDblaAkoL2GeV6GXF6SJjjiDeeBzURImCO39hfRPm2c8 NCoquLUmlF1qvdrfea4R0bLXOykDWW9oE5rojVgAvX36bo/InaKLYDqVazjXnk+ih1Es mtZee8mJoSgwQYQ2uUtUxTjpnC+zI7vZxJBCzF+3Zl5EBUOUhGMMPb3zldbrJdjGIh8A phoA== X-Gm-Message-State: AOJu0Yw9NQmPOl6Y6UD30Um+sjVxm7a1SQWK3/lpND7e3bY5D1EH+7YR ouqhucFc8t3YTaKiY6dc3K1K2t9itHnJAUzcyHtfdNaZoeDRAfChXYOaPE9Vd1umFJXk+PG/sZq j X-Google-Smtp-Source: AGHT+IF7rjXDGR6GqfE+t6LDWFixtz4mDvXzXd5QOt1Rz6yfrwz8vpprxFC9zMy4emKhfHAoErWBzw== X-Received: by 2002:a17:90b:2390:b0:2cf:7388:ad9e with SMTP id 98e67ed59e1d1-2d646bcd7bamr13133308a91.2.1724773761493; Tue, 27 Aug 2024 08:49:21 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org, jag.raman@oracle.com Cc: Elena Ufimtseva , john.levon@nutanix.com, Thanos Makatos , Mattias Nissler Subject: [PATCH v12 1/3] Update subprojects/libvfio-user Date: Tue, 27 Aug 2024 08:48:59 -0700 Message-Id: <20240827154902.607926-2-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240827154902.607926-1-mnissler@rivosinc.com> References: <20240827154902.607926-1-mnissler@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=mnissler@rivosinc.com; helo=mail-pj1-x102f.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1724773853882116600 Content-Type: text/plain; charset="utf-8" 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 =3D https://gitlab.com/qemu-project/libvfio-user.git -revision =3D 0b28d205572c80b568a1003db2c8f37ca333e4d7 +revision =3D b1a156d86f55a8fa3f78ece5bee7748ec75e7b82 depth =3D 1 --=20 2.34.1 From nobody Sun Dec 22 06:20:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1724773844; cv=none; d=zohomail.com; s=zohoarc; b=CmCeACl6f9vd/SvWJbCV/C45GesqmMgAUVIZ4Fbl8nxhTgqHWQFxQx+mXlv3XUyQ3gd6E8IW46+6i9H00g00h22dAFaqEB89DCfvRjyDJMhi2d3wTgGrTO9b0ZQMveV7PiYv5yoQNZ/a7RPkJaMTRzZg5kd08Q4lRNEjyafrx7s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1724773844; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=EMvjgF3oAVY7GBl6lGcISH8txA+B5nR6CJ8hZiXV7Hg=; b=IFuSnsiDnp3yVv/h5/Rvd2rRQgTbTihccS4qsACi1bCPKV3MPs7W8HxaoSE8Ys7VuNkHIz7ot7Lf4wfGqPEbVHQKck4j9AvSCKFRnT2SWwkyvxJ3vkTK55dKNtbTNW3PHy9047+FVRiOEutAFtQiPWQ1GHGKrumwb8XN9wM0bA8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1724773844660852.3249692676785; Tue, 27 Aug 2024 08:50:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1siyRm-0003YG-9R; Tue, 27 Aug 2024 11:49:30 -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 1siyRj-0003Q6-Hd for qemu-devel@nongnu.org; Tue, 27 Aug 2024 11:49:27 -0400 Received: from mail-pg1-x52d.google.com ([2607:f8b0:4864:20::52d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1siyRg-0004mi-8J for qemu-devel@nongnu.org; Tue, 27 Aug 2024 11:49:27 -0400 Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-7bcf8077742so3945270a12.0 for ; Tue, 27 Aug 2024 08:49:23 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb9037a2sm14624478a91.13.2024.08.27.08.49.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 08:49:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724773762; x=1725378562; 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=sQBXzGnAYaJvimR83/hRDS3tZdxQ/fpUuNLCGT/Boa0j1pGGteW/AxHi0BTPysIn6l lf1HygvZ+v4NSqNM/64N7hMiD9vkEYQ1W5/BVhGAr2mzTgS/wLnKQYL2K2ZAgeVZAl22 ZVoWy8YS1IShZNlwSCJGrzQS9ut0snoolpM1qGxhp39h8tASEVCzeu1230KPaoM7MLBI s4usRtjHMfXtcT6sPu07ftAHo9AMh05uaa9sD7cEbRHh2jM3e8V+eXHltxPm998/sD2v 9UhALyWe49BhjdgvvCu0AbLPakV+4PDOECSzsRxyqP9/vjK9m8qcWyPZtrmIJHkSO4Sa uF8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724773762; x=1725378562; 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=ZsTGTCKMafo6I5E5ufnstOrJMLZbDeaLXtOTDKCcF/KbW/ascyTWcuiR8atdq8VqwJ +IrTU/b8/iiMcKZoYh1dMibcaHEctXuBJRUMcuZW7IxytXrepzrpFnL6MsYyqIjwyouS ywzr0E5vyjWGHOhbCDfflQiSbQqh4q1Ks/65+kR0nOrGKch7s7TM559fpF9rd4NoUKwh 5dOs+J/C6pqqk/kl4FK3LxQ6GqcnS1ofVtjSJNj2iSoCgEmYBztTY9BmP/MvB5oLIx8J x64uPe29C0wFap1t9av8GAsKipDhJ0ZEIy0DvCj6c27rg0Kno/hAGONZnenKjzsBlchz IaDg== X-Gm-Message-State: AOJu0YyXlyyNH3LblSAW2AYnQgtOaUF6GtTv8N0Ja6FxlN4pbuV4JC1T YDIvo8L8gnN7pTgGiRfYwivNGwZ03ji9QCI48dCuN7A2L/piMM/41SyJeVV7d1OA2BMYCDccJEJ c X-Google-Smtp-Source: AGHT+IGNElYyGPXl+0tNG1aw0cWVY5Y86iM1aLeEkILu88pDp3PuVmBk/KbkpRqG0up7A2v23Hd1TA== X-Received: by 2002:a17:90b:4c06:b0:2d3:90e1:41c7 with SMTP id 98e67ed59e1d1-2d646d3040emr14609809a91.31.1724773762307; Tue, 27 Aug 2024 08:49:22 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org, jag.raman@oracle.com Cc: Elena Ufimtseva , john.levon@nutanix.com, Thanos Makatos , Mattias Nissler Subject: [PATCH v12 2/3] vfio-user: Message-based DMA support Date: Tue, 27 Aug 2024 08:49:00 -0700 Message-Id: <20240827154902.607926-3-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240827154902.607926-1-mnissler@rivosinc.com> References: <20240827154902.607926-1-mnissler@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::52d; envelope-from=mnissler@rivosinc.com; helo=mail-pg1-x52d.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1724773846001116600 Content-Type: text/plain; charset="utf-8" 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%"PRIx6= 4", %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 byt= es" +vfu_dma_write(uint64_t gpa, size_t len) "vfu: DMA write 0x%"PRIx64", %zu b= ytes" vfu_bar_register(int i, uint64_t addr, uint64_t size) "vfu: BAR %d: addr 0= x%"PRIx64" size 0x%"PRIx64"" vfu_bar_rw_enter(const char *op, uint64_t addr) "vfu: %s request for BAR a= ddress 0x%"PRIx64"" vfu_bar_rw_exit(const char *op, uint64_t addr) "vfu: Finished %s of BAR ad= dress 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_ct= x, char * const buf, return count; } =20 +static MemTxResult vfu_dma_read(void *opaque, hwaddr addr, uint64_t *val, + unsigned size, MemTxAttrs attrs) +{ + MemoryRegion *region =3D opaque; + vfu_ctx_t *vfu_ctx =3D 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 =3D g_malloc0(dma_sg_size()); + vfu_dma_addr_t vfu_addr =3D (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) !=3D 0) { + return MEMTX_ERROR; + } + + *val =3D 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 =3D opaque; + vfu_ctx_t *vfu_ctx =3D 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 =3D g_malloc0(dma_sg_size()); + vfu_dma_addr_t vfu_addr =3D (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) !=3D 0) { + return MEMTX_ERROR; + } + + return MEMTX_OK; +} + +static const MemoryRegionOps vfu_dma_ops =3D { + .read_with_attrs =3D vfu_dma_read, + .write_with_attrs =3D vfu_dma_write, + .endianness =3D DEVICE_HOST_ENDIAN, + .valid =3D { + .min_access_size =3D 1, + .max_access_size =3D 8, + .unaligned =3D true, + }, + .impl =3D { + .min_access_size =3D 1, + .max_access_size =3D 8, + }, +}; + static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) { VfuObject *o =3D 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 =3D NULL; struct iovec *iov =3D &info->iova; =20 - if (!info->vaddr) { - return; - } - name =3D g_strdup_printf("mem-%s-%"PRIx64"", o->device, - (uint64_t)info->vaddr); + (uint64_t)iov->iov_base); =20 subregion =3D g_new0(MemoryRegion, 1); =20 - 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 mo= st 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, subregio= n, + name, iov->iov_len); + } =20 dma_as =3D pci_device_iommu_address_space(o->pci_dev); =20 @@ -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 =3D vfu_get_private(vfu_ctx); + MemoryRegionSection mr_section; AddressSpace *dma_as =3D NULL; - MemoryRegion *mr =3D NULL; - ram_addr_t offset; =20 - mr =3D memory_region_from_host(info->vaddr, &offset); - if (!mr) { + dma_as =3D pci_device_iommu_address_space(o->pci_dev); + + mr_section =3D + memory_region_find(dma_as->root, (hwaddr)info->iova.iov_base, 1); + if (!mr_section.mr) { return; } =20 - dma_as =3D 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); =20 - object_unparent((OBJECT(mr))); + object_unparent((OBJECT(mr_section.mr))); =20 trace_vfu_dma_unregister((uint64_t)info->iova.iov_base); } --=20 2.34.1 From nobody Sun Dec 22 06:20:49 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1724773841; cv=none; d=zohomail.com; s=zohoarc; b=hqB8zfJQgZOJAW4Zq1GgUa4f4tcZmEd2bRxUIYcTpJ9Bokj91EGNRNOG0RqkiT+xSYrVpbVZ1DE795YIdFi52qPEIv6Vik5t56PSoOmXk84uCyE4lyNj5R8OwclEcsLPvBO89ztpq7Z+VGEMcO299BalYkd2722EjY6PqrMDa9g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1724773841; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=NGFZZzfnU9aEDN0fiH1YTSYbYKUYKCcBm7Y4zenqIsg=; b=mB4PnuNkALU1bHzkOHVvGXUnnW1yj811cIFkLY8BWUzCONH2Mv1MisCTGhQRuqZ8l+WXJsRM40WXYOVSYBEkLsb1Zq31Fois4y41aPmZEt0twiaRMUXTmseoriIcmXZwH+BPBAL9E+njtVvHRnK26PgJdHTdKsevMirzZ/ry17Q= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1724773841769147.36714884120738; Tue, 27 Aug 2024 08:50:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1siyRk-0003Ri-AQ; Tue, 27 Aug 2024 11:49:28 -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 1siyRj-0003Pb-3Y for qemu-devel@nongnu.org; Tue, 27 Aug 2024 11:49:27 -0400 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1siyRh-0004mo-09 for qemu-devel@nongnu.org; Tue, 27 Aug 2024 11:49:26 -0400 Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-7cd9e4f550dso1780663a12.0 for ; Tue, 27 Aug 2024 08:49:24 -0700 (PDT) Received: from mnissler.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2d5eb9037a2sm14624478a91.13.2024.08.27.08.49.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Aug 2024 08:49:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1724773763; x=1725378563; 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=NGFZZzfnU9aEDN0fiH1YTSYbYKUYKCcBm7Y4zenqIsg=; b=pFbdGlpFZKZeW27qJxbmasOhQ+CA31AxV1MWIGyJHPyLt0awnZ002g9ounnUIUMKKV HLj5NdTed0naZ0Ba3DEUgpJCuh0fQvM4tSCZ6qVi2E0k64ZWT2CzyER5LbcoNYiJhXwK NmTJE+YsUSqd/LLMjXFnOHG+KgedR9Z33/mEWfgiqx8SFKzeMy4Z9HNRkEg+GpjWylkK dKDIa0eFOqrSXAhSNGk9aYUw8UQ1Y6gH5DcUO5+zFJwBQDzzP8ioEX0PmU8yCU1fzZi+ OeRXjxiLeKeqRHaAhglcQWQbcGlKm/ZKMQabejtLlfwwEt6XqT7BdwHZh4/RNLmyYs+g QMMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724773763; x=1725378563; 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=NGFZZzfnU9aEDN0fiH1YTSYbYKUYKCcBm7Y4zenqIsg=; b=h92+F/2h07deTLNw3CkVfT0/zIr478l0sUxRSjuezVI4Oz8LxVOw2VJq31MWj7+94A +EJdkrbx3VlCFqEXMTS8JKbKUcWVV2k3ngiRe1SyjcQTScC7TEYDKFzmVZJPuGHP5XKC QAjoZoBf27XvTfQHRPUPIqk7yzKfRW7RPKsP498A1PmlLrNOeSrdW/zHvX6agwXsNFWF z/aBCMbKDCCawwBafiJdqoFCg0uvKbSRqPo1ECFVgCZ+lff4rcWgf9vHDT8tlOrE4INM ADx0AA25R9vOWPcRhb6mJxkMqpzt2WwYh466N4UhIRxXVhEFsjsnTZvFvPZhA7yhvG/R Kb2w== X-Gm-Message-State: AOJu0YyW1dQZVFj8x3uKSELaUtt3/YqlxN4sNQ1JwyN21oJQ8o6jaAod 2IYX+k6IPT3pcr/7rXKNDuMqbMjVpr+zXAK6ODTc6EwXJy9tIZlnyeHJ8ijL7yBrLpnPPeKx/tR /73w= X-Google-Smtp-Source: AGHT+IEUG8DcTDFZ+mJtyk36vbFQTW0DHxJ+giwo7IUxQTERl5axUdHT0YvxN2L4uPWZa6vellb1kA== X-Received: by 2002:a17:90a:98b:b0:2c9:888a:7a7b with SMTP id 98e67ed59e1d1-2d646c197eamr13142368a91.25.1724773763211; Tue, 27 Aug 2024 08:49:23 -0700 (PDT) From: Mattias Nissler To: qemu-devel@nongnu.org, jag.raman@oracle.com Cc: Elena Ufimtseva , john.levon@nutanix.com, Thanos Makatos , Mattias Nissler Subject: [PATCH v12 3/3] vfio-user: Fix memory region reference accounting Date: Tue, 27 Aug 2024 08:49:01 -0700 Message-Id: <20240827154902.607926-4-mnissler@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240827154902.607926-1-mnissler@rivosinc.com> References: <20240827154902.607926-1-mnissler@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::534; envelope-from=mnissler@rivosinc.com; helo=mail-pg1-x534.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1724773844185116600 Content-Type: text/plain; charset="utf-8" The memory regions created for DMA regions where leaking the original reference the object is initialized with. This happened since we insert the memory region as a subregion, but don't keep the reference obtained when creating the object. Thus, drop the reference after inserting the DMA memory region into the address space. This fixes auto-shutdown behavior: Due to the leaked references, the memory regions would never be released, and indirectly keep the VFU object as their owner alive. Thus, vfu_object_finalize didn't get invoked, and qemu wouldn't terminate. With this fix, this is now working as originally intended. Signed-off-by: Mattias Nissler --- hw/remote/vfio-user-obj.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c index 0e93d7a7b4..d347a96e39 100644 --- a/hw/remote/vfio-user-obj.c +++ b/hw/remote/vfio-user-obj.c @@ -394,6 +394,14 @@ static void dma_register(vfu_ctx_t *vfu_ctx, vfu_dma_i= nfo_t *info) =20 memory_region_add_subregion(dma_as->root, (hwaddr)iov->iov_base, subre= gion); =20 + /* + * Insertion into the address space grabbed a reference to keep the me= mory + * region alive. However, the memory region object was created with an + * original reference count of 1, so we must unref since we don't keep= that + * reference. + */ + memory_region_unref(subregion); + trace_vfu_dma_register((uint64_t)iov->iov_base, iov->iov_len); } =20 --=20 2.34.1