From patchwork Mon Sep 13 07:15:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 12488005 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22662C433EF for ; Mon, 13 Sep 2021 07:20:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 021CB60F6C for ; Mon, 13 Sep 2021 07:20:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237535AbhIMHV4 (ORCPT ); Mon, 13 Sep 2021 03:21:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59492 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235199AbhIMHVz (ORCPT ); Mon, 13 Sep 2021 03:21:55 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A3D7C061574 for ; Mon, 13 Sep 2021 00:20:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=X2oYrxks7Zsz5CgeW93hpw5CqYvSaHa65IC7k5g26MQ=; b=Li0maAWraaiAgA7C3QrfY5KIVe 57NUvtZzokUCnR6zhDF5BPHAa5qIK0jS1WL18oMrZO909q7ZA1wz0SzmjI/45ovmsS/cVfv+86gQX eip4s82TyEd3NfnoSdmc7nAkKxTE4hrAiw9eODx73Jw/5zDBRdWz0k2lIHHoJk8+jPaTVaO4Yz6dW Vuh1ZdEm25KcFht/RKBY1gBjP/SUAwz64IvIZwyeS3YTWEaab60905DDlltzwUz4SFx+w9XsEzrgo syPViuIw+sHpLsJMaDJrbOyzhJNt0F5EkzVGYWImBi5exYjaA8CisPFajHIyVKfKcb+tIYIrgjAph uGMFhlDQ==; Received: from 213-225-6-64.nat.highway.a1.net ([213.225.6.64] helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.94.2 #2 (Red Hat Linux)) id 1mPgEY-00DGJc-V1; Mon, 13 Sep 2021 07:18:44 +0000 From: Christoph Hellwig To: Alex Williamson Cc: Diana Craciun , Cornelia Huck , Kirti Wankhede , Eric Auger , Jason Gunthorpe , Terrence Xu , kvm@vger.kernel.org, Jason Gunthorpe , Kevin Tian Subject: [PATCH 02/14] vfio: factor out a vfio_iommu_driver_allowed helper Date: Mon, 13 Sep 2021 09:15:54 +0200 Message-Id: <20210913071606.2966-3-hch@lst.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210913071606.2966-1-hch@lst.de> References: <20210913071606.2966-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Factor out a little helper to make the checks for the noiommu driver less ugly. Signed-off-by: Christoph Hellwig Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/vfio/vfio.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index b39da9b90c95bc..faf8e0d637bb94 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -257,8 +257,23 @@ static const struct vfio_iommu_driver_ops vfio_noiommu_ops = { .attach_group = vfio_noiommu_attach_group, .detach_group = vfio_noiommu_detach_group, }; -#endif +/* + * Only noiommu containers can use vfio-noiommu and noiommu containers can only + * use vfio-noiommu. + */ +static inline bool vfio_iommu_driver_allowed(struct vfio_container *container, + const struct vfio_iommu_driver *driver) +{ + return container->noiommu == (driver->ops == &vfio_noiommu_ops); +} +#else +static inline bool vfio_iommu_driver_allowed(struct vfio_container *container, + const struct vfio_iommu_driver *driver) +{ + return true; +} +#endif /* CONFIG_VFIO_NOIOMMU */ /** * IOMMU driver registration @@ -999,8 +1014,8 @@ void vfio_unregister_group_dev(struct vfio_device *device) wait_event(group->container_q, !group->container); #ifdef CONFIG_VFIO_NOIOMMU - if (iommu_group_get_iommudata(group) == &noiommu) - iommu_group_remove_device(dev); + if (iommu_group_get_iommudata(group->iommu_group) == &noiommu) + iommu_group_remove_device(device->dev); #endif /* Matches the get in vfio_register_group_dev() */ vfio_group_put(group); @@ -1034,13 +1049,10 @@ static long vfio_ioctl_check_extension(struct vfio_container *container, list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { -#ifdef CONFIG_VFIO_NOIOMMU if (!list_empty(&container->group_list) && - (container->noiommu != - (driver->ops == &vfio_noiommu_ops))) + !vfio_iommu_driver_allowed(container, + driver)) continue; -#endif - if (!try_module_get(driver->ops->owner)) continue; @@ -1112,15 +1124,8 @@ static long vfio_ioctl_set_iommu(struct vfio_container *container, list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { void *data; -#ifdef CONFIG_VFIO_NOIOMMU - /* - * Only noiommu containers can use vfio-noiommu and noiommu - * containers can only use vfio-noiommu. - */ - if (container->noiommu != (driver->ops == &vfio_noiommu_ops)) + if (!vfio_iommu_driver_allowed(container, driver)) continue; -#endif - if (!try_module_get(driver->ops->owner)) continue;