From nobody Fri Nov 8 18:42:11 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