Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp500951pxb; Wed, 3 Mar 2021 08:21:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJw7u71QTPsdjQhzm2Ou+58xvmNm9HvwjoA6qcQ7x5XOcOSSetxZ3xn3KN6d4BkE6OFT4u3T X-Received: by 2002:a17:906:8043:: with SMTP id x3mr25887049ejw.149.1614788476442; Wed, 03 Mar 2021 08:21:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614788476; cv=none; d=google.com; s=arc-20160816; b=Cu7Q6C3/13gC3F1yVaK34t1BIcl8eFIKqwjiHwG1LQDhQdMKQU2Gg/CWxDuo3jft4r QF4xcBqLEYoTog6nFhA1KQAPtCL+cRHZn0QGcuUPMJ7FkBWB6d51od8oOLQA0UiYemAV hnWshhd9fk+/LTDfGn5y0rcDuxV6nr5pTAi86JaSO3J0xCJLgP8aXq/UTtrTLw5FfsF1 YY6E3Y4IAYqQt7Kt1w3DXLms39nFz+saAKdglmzOjegt98MnExlBjK7Sto7SBn7NUrbb swzvSToxOSeZuzi7WLmrRb94QHdXKRIwZuzrEuG4qVdCYX8zVpemoR3RkQ3lxiB/StpT OjIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ZY1LsqtFqkF0FGojcQ+pPipU2OQjQuUIXFFJOKbtHQQ=; b=HFwAyZt/llhJjv41sErtUCQ9/9FpnRkntmkDkiVOmoimqlz+xpm/RSOsoeZ3g4BmzE Y+cEENVF6UqMokoSMNUFxcBKvGx09ksA3u1G8lxIk38gMPSzjDXkpiRaXlMWk8hJ9zvt VoyqYUbxRRSHpd9PJt13Sy6hhyLVtjPYXr7xiJUtI4P4iSA0dTWq1ru9JDKbY3I3aFTj in7tW77WVR1xjM+0cmwGjA++lqVOo6YxjC+YtN3h24JSfLzsyts7vIgYSBWW7BxtGnnN vkI/Jr1EPY9MvhygvSxgziTBBbVonmWTKR+QhUDwnlbRpxXJGjhaZ/ER8KHAjeFthwih 4R5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SzKELfIE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f11si522602ejw.277.2021.03.03.08.20.19; Wed, 03 Mar 2021 08:21:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SzKELfIE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346495AbhCAXsf (ORCPT + 99 others); Mon, 1 Mar 2021 18:48:35 -0500 Received: from mail.kernel.org ([198.145.29.99]:60786 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238828AbhCASJT (ORCPT ); Mon, 1 Mar 2021 13:09:19 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7BBF765210; Mon, 1 Mar 2021 17:22:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614619329; bh=a4msSb0TMRoTeTWoGHvLNmkLmL172eHiFrYy/xo0/O4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SzKELfIEZyO8lEWuUKGkkDPfyK/sTUErWJ/kwsTB6v8oZACyw7BUu9FOyR8FNDDc3 GwkvYYdhep8Ffv8lLC9XRu/wwhiYt/8/ItZ9+ac61bFwGhzuI9SJNT0jUg2H23txgK iJhkqFcp0Yq13B6uzuZV0BWmuiV+AXQlSm46/4h0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Keqian Zhu , Alex Williamson , Sasha Levin Subject: [PATCH 5.10 415/663] vfio/iommu_type1: Fix some sanity checks in detach group Date: Mon, 1 Mar 2021 17:11:03 +0100 Message-Id: <20210301161202.406080057@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161141.760350206@linuxfoundation.org> References: <20210301161141.760350206@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Keqian Zhu [ Upstream commit 4a19f37a3dd3f29997735e61b25ddad24b8abe73 ] vfio_sanity_check_pfn_list() is used to check whether pfn_list and notifier are empty when remove the external domain, so it makes a wrong assumption that only external domain will use the pinning interface. Now we apply the pfn_list check when a vfio_dma is removed and apply the notifier check when all domains are removed. Fixes: a54eb55045ae ("vfio iommu type1: Add support for mediated devices") Signed-off-by: Keqian Zhu Signed-off-by: Alex Williamson Signed-off-by: Sasha Levin --- drivers/vfio/vfio_iommu_type1.c | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index a08b2cb4ec66e..c12fc0d190627 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -957,6 +957,7 @@ static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dma, static void vfio_remove_dma(struct vfio_iommu *iommu, struct vfio_dma *dma) { + WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list)); vfio_unmap_unpin(iommu, dma, true); vfio_unlink_dma(iommu, dma); put_task_struct(dma->task); @@ -2250,23 +2251,6 @@ static void vfio_iommu_unmap_unpin_reaccount(struct vfio_iommu *iommu) } } -static void vfio_sanity_check_pfn_list(struct vfio_iommu *iommu) -{ - struct rb_node *n; - - n = rb_first(&iommu->dma_list); - for (; n; n = rb_next(n)) { - struct vfio_dma *dma; - - dma = rb_entry(n, struct vfio_dma, node); - - if (WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list))) - break; - } - /* mdev vendor driver must unregister notifier */ - WARN_ON(iommu->notifier.head); -} - /* * Called when a domain is removed in detach. It is possible that * the removed domain decided the iova aperture window. Modify the @@ -2366,10 +2350,10 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, kfree(group); if (list_empty(&iommu->external_domain->group_list)) { - vfio_sanity_check_pfn_list(iommu); - - if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu)) + if (!IS_IOMMU_CAP_DOMAIN_IN_CONTAINER(iommu)) { + WARN_ON(iommu->notifier.head); vfio_iommu_unmap_unpin_all(iommu); + } kfree(iommu->external_domain); iommu->external_domain = NULL; @@ -2403,10 +2387,12 @@ static void vfio_iommu_type1_detach_group(void *iommu_data, */ if (list_empty(&domain->group_list)) { if (list_is_singular(&iommu->domain_list)) { - if (!iommu->external_domain) + if (!iommu->external_domain) { + WARN_ON(iommu->notifier.head); vfio_iommu_unmap_unpin_all(iommu); - else + } else { vfio_iommu_unmap_unpin_reaccount(iommu); + } } iommu_domain_free(domain->domain); list_del(&domain->next); @@ -2490,7 +2476,6 @@ static void vfio_iommu_type1_release(void *iommu_data) if (iommu->external_domain) { vfio_release_domain(iommu->external_domain, true); - vfio_sanity_check_pfn_list(iommu); kfree(iommu->external_domain); } -- 2.27.0