From patchwork Mon Feb 5 19:28:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 869478 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="M/s20Gv0"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zZyfh4NXDz9s8J for ; Tue, 6 Feb 2018 06:42:36 +1100 (AEDT) Received: from localhost ([::1]:54834 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eimew-0008KC-Gi for incoming@patchwork.ozlabs.org; Mon, 05 Feb 2018 14:42:34 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44801) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eimSM-0004XJ-Bm for qemu-devel@nongnu.org; Mon, 05 Feb 2018 14:29:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eimSL-00052L-FW for qemu-devel@nongnu.org; Mon, 05 Feb 2018 14:29:34 -0500 Received: from mail-wm0-x242.google.com ([2a00:1450:400c:c09::242]:51311) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eimSL-00051w-8l for qemu-devel@nongnu.org; Mon, 05 Feb 2018 14:29:33 -0500 Received: by mail-wm0-x242.google.com with SMTP id r71so28444914wmd.1 for ; Mon, 05 Feb 2018 11:29:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=zyY3NTlfOa8/ThsZ8be8KK/ujZm7cAClR+FjBfoWCUU=; b=M/s20Gv0D3Igghn8mUgDiGCJdlb9i3lr7+4lQIoqjE2rh19SuWoFd1s88tqtyFICoM tmKH7B5Lvbe5PCH9HZHfzcHv0U9oy3VjM27Li6Mw8uAbnQ1ZHXdncgC6i7/8XkkYKM1v HAaakaKEf2rJZ+gx99rzIHleGUIr0DmFDUL60hh1jYrAha3wgNgYWV5baHlKYaMvPpTS WvtG/X6DqS35dfi89IpIPaHDysEgeqAZiA+3SZLPr+LOcwOXCMsRnZXWsyKQfZQAZqtU G94lUtIuEsi1byAp0uSu7DMBNhc1OR8k2JnDJ/juQARy2BO9tfhVaRakUpRkm7vTnbCv Us/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=zyY3NTlfOa8/ThsZ8be8KK/ujZm7cAClR+FjBfoWCUU=; b=CkAg1IL2ymqyZN5qZrMytiw4nrzos95FkIXjNxtw0PyWoiWXEr1UM1M/4fZsJmGH9j y+iVKvAQWsRDvqjgWqE/Ib8FtqPV8QYo3XVPvy0ruMTU6dkC3H2WJx3nnsa3UJVN25S3 XgaLVwNoDvO1f07R4eTnODcTbgVg5wnisbv03/yiTqic9Q8svDqbx9zNMJyx30o7h3YQ Vi5t0wPiRUP5f1cpKstMqxba/TzoF91LpD79etqVzE6dwFNRFmtua6ZTW1f5l0VO8VTr 7Em8ySC+RzsN+x/6Iz3kKBGuiZXsW6Xu1h9/XWFPAaNgeCnZQ2Rpw3J/GyqlC8JPGg06 YZsQ== X-Gm-Message-State: APf1xPARbgDSAMehd6yQzXSadu52MsAbdm3kOhcSDjVBZkB32I4D57Zv vhn+QOcDNBfDLmvx0LJVO+QIC/ld X-Google-Smtp-Source: AH8x224ybpqukpRUNFSKTt2KZPEUPvamjehoBJwMWn6o8X6j2pbuZBGCFAPfBWYVi7JBRm+DKB1l5w== X-Received: by 10.28.22.201 with SMTP id 192mr295400wmw.45.1517858971903; Mon, 05 Feb 2018 11:29:31 -0800 (PST) Received: from 640k.lan ([82.84.122.246]) by smtp.gmail.com with ESMTPSA id g8sm20029891wra.18.2018.02.05.11.29.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 11:29:31 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 5 Feb 2018 20:28:37 +0100 Message-Id: <1517858941-5538-24-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1517858941-5538-1-git-send-email-pbonzini@redhat.com> References: <1517858941-5538-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c09::242 Subject: [Qemu-devel] [PULL 23/47] vfio: listener unregister before unset container X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Xu Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Peter Xu After next patch, listener unregister will need the container to be alive. Let's move this unregister phase to be before unset container, since that operation will free the backend container in kernel, otherwise we'll get these after next patch: qemu-system-x86_64: VFIO_UNMAP_DMA: -22 qemu-system-x86_64: vfio_dma_unmap(0x559bf53a4590, 0x0, 0xa0000) = -22 (Invalid argument) Signed-off-by: Peter Xu Message-Id: <20180122060244.29368-4-peterx@redhat.com> Reviewed-by: Paolo Bonzini Acked-by: Alex Williamson Signed-off-by: Paolo Bonzini --- hw/vfio/common.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index b77be3a..76cf28d 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -1161,19 +1161,27 @@ static void vfio_disconnect_container(VFIOGroup *group) { VFIOContainer *container = group->container; + QLIST_REMOVE(group, container_next); + group->container = NULL; + + /* + * Explicitly release the listener first before unset container, + * since unset may destroy the backend container if it's the last + * group. + */ + if (QLIST_EMPTY(&container->group_list)) { + vfio_listener_release(container); + } + if (ioctl(group->fd, VFIO_GROUP_UNSET_CONTAINER, &container->fd)) { error_report("vfio: error disconnecting group %d from container", group->groupid); } - QLIST_REMOVE(group, container_next); - group->container = NULL; - if (QLIST_EMPTY(&container->group_list)) { VFIOAddressSpace *space = container->space; VFIOGuestIOMMU *giommu, *tmp; - vfio_listener_release(container); QLIST_REMOVE(container, next); QLIST_FOREACH_SAFE(giommu, &container->giommu_list, giommu_next, tmp) {