Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 198F7C433FE for ; Wed, 17 Nov 2021 05:26:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F058061A3A for ; Wed, 17 Nov 2021 05:26:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233219AbhKQF3v (ORCPT ); Wed, 17 Nov 2021 00:29:51 -0500 Received: from mga09.intel.com ([134.134.136.24]:49283 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229585AbhKQF3t (ORCPT ); Wed, 17 Nov 2021 00:29:49 -0500 X-IronPort-AV: E=McAfee;i="6200,9189,10170"; a="233717991" X-IronPort-AV: E=Sophos;i="5.87,240,1631602800"; d="scan'208";a="233717991" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Nov 2021 21:26:51 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.87,240,1631602800"; d="scan'208";a="494772632" Received: from allen-box.sh.intel.com (HELO [10.239.159.118]) ([10.239.159.118]) by orsmga007.jf.intel.com with ESMTP; 16 Nov 2021 21:26:46 -0800 Cc: baolu.lu@linux.intel.com, Christoph Hellwig , Greg Kroah-Hartman , Joerg Roedel , Alex Williamson , Bjorn Helgaas , Kevin Tian , Ashok Raj , Chaitanya Kulkarni , kvm@vger.kernel.org, rafael@kernel.org, linux-pci@vger.kernel.org, Cornelia Huck , linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org, Jacob jun Pan , Diana Craciun , Will Deacon Subject: Re: [PATCH 01/11] iommu: Add device dma ownership set/release interfaces To: Jason Gunthorpe References: <20211115020552.2378167-1-baolu.lu@linux.intel.com> <20211115020552.2378167-2-baolu.lu@linux.intel.com> <20211116134603.GA2105516@nvidia.com> From: Lu Baolu Message-ID: Date: Wed, 17 Nov 2021 13:22:19 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20211116134603.GA2105516@nvidia.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jason, On 11/16/21 9:46 PM, Jason Gunthorpe wrote: > On Tue, Nov 16, 2021 at 09:57:30AM +0800, Lu Baolu wrote: >> Hi Christoph, >> >> On 11/15/21 9:14 PM, Christoph Hellwig wrote: >>> On Mon, Nov 15, 2021 at 10:05:42AM +0800, Lu Baolu wrote: >>>> +enum iommu_dma_owner { >>>> + DMA_OWNER_NONE, >>>> + DMA_OWNER_KERNEL, >>>> + DMA_OWNER_USER, >>>> +}; >>>> + >>> >>>> + enum iommu_dma_owner dma_owner; >>>> + refcount_t owner_cnt; >>>> + struct file *owner_user_file; >>> >>> I'd just overload the ownership into owner_user_file, >>> >>> NULL -> no owner >>> (struct file *)1UL) -> kernel >>> real pointer -> user >>> >>> Which could simplify a lot of the code dealing with the owner. >>> >> >> Yeah! Sounds reasonable. I will make this in the next version. > > It would be good to figure out how to make iommu_attach_device() > enforce no other driver binding as a kernel user without a file *, as > Robin pointed to, before optimizing this. > > This fixes an existing bug where iommu_attach_device() only checks the > group size and is vunerable to a hot plug increasing the group size > after it returns. That check should be replaced by this series's logic > instead. As my my understanding, the essence of this problem is that only the user owner of the iommu_group could attach an UNMANAGED domain to it. If I understand it right, how about introducing a new interface to allocate a user managed domain and storing the user file pointer in it. --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -94,6 +94,7 @@ struct iommu_domain { void *handler_token; struct iommu_domain_geometry geometry; struct iommu_dma_cookie *iova_cookie; + struct file *owner_user_file; }; --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1902,6 +1902,18 @@ struct iommu_domain *iommu_domain_alloc(struct bus_type *bus) } EXPORT_SYMBOL_GPL(iommu_domain_alloc); +struct iommu_domain *iommu_domain_alloc_user(struct bus_type *bus, + struct file *filep) +{ + struct iommu_domain *domain; + + domain = __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED); + if (domain) + domain->owner_user_file = filep; + + return domain; +} When attaching a domain to an user-owned iommu_group, both group and domain should have matched user fd. Does above help here? Best regards, baolu