From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ua1-f50.google.com (mail-ua1-f50.google.com [209.85.222.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D36FD5D729 for ; Fri, 22 Mar 2024 16:59:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711126771; cv=none; b=OhEHbxhWirAJ85SD2TvMYycixsURXqV92tGBQFcwFYj+lgScTiitY723pObq+bJpwA3D+q0JHZ448Ge0bIIlkz2nlxoveTQoN9mRAPpVOfR0NVmIJXmbwZmnaWHKg4qVNJsVwM5Pkz+VAjCdSEcH8tGxlk5KMi87HtrXnT4KaD0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711126771; c=relaxed/simple; bh=JRf6wUgjrXJBFiOWAu7JY98jun+mmnoci/D+Wpd82GA=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=fL5FJFB3+YrAY5/kmTU7IO+xTZKakJkdItbq/o8Kfjctk3tpaDQfgU0A0voYdeUroymiWb1XVur4jAq7mM3R+3xVR7mLMGz7HvfZz+X6THRKlf0vJVVYKmIjETXSPBfOebUFIbfE59LfE0zqq5uCicL3AS4GUqaOZ4PbMPSoQN0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca; spf=pass smtp.mailfrom=ziepe.ca; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b=TmKpqwey; arc=none smtp.client-ip=209.85.222.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ziepe.ca Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="TmKpqwey" Received: by mail-ua1-f50.google.com with SMTP id a1e0cc1a2514c-7e057fb0b69so802568241.0 for ; Fri, 22 Mar 2024 09:59:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; t=1711126769; x=1711731569; darn=lists.linux.dev; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=j7Ol3p67jPcohmb6YwggOyywONRnit3CwsysWz7vFH8=; b=TmKpqweyFGnYBYkcDO1ecWxN3EbT1BYH+IE0pD9gghrpaYlujsAByeN+9REqq+7I/K B6WAhjulJwbp5WUV68gCz7wHnwGCGDVE9vBm8JLmDzOq6xEtnfg67hkFtortbDNKty+2 YMMr9LLF67rUjNFqZEuwcQxZssefrzff0Eijon4h65bUMfZXCeAPCRCl76/pankERtCU tdRGfNBMNC6E2uuO+zzOj/1I9XCEhnD8nvPYEfVvLxoXhcuVTDZueABkaOHuawFV0YJD hXgITIWgl1bOoNHWOQRvqPOOg0ZuEwkk7Lfr4uMN15ZA4782YHMfk1LxITyIdrci27tX /Hbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711126769; x=1711731569; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=j7Ol3p67jPcohmb6YwggOyywONRnit3CwsysWz7vFH8=; b=Rcd96d+dfPBA7YrkXI/wVzTVsrYmXQi3InadLlz6Bkfawmx3SHNsCV+o8K6tzCHiW+ MuoN+X6W+BybZnC6qyB3Ly855iiYk6qdpd/gKqr9dKSMgLKwVH++10oCoVz0v7pjUJVR Je6fpwPwaV09T9K8g+/Dt4EVjNVeNfrIkspqyFgJjfhoTZxEOckE+zUeAe9MasQ9F/Ys t3E3uChHVXZGze3siVWx9Lw266dkJTK44Ar/yQgeyBthdld1rUJ49ea5cHyuq/WLjWOM oX8egBMELJz0zcY9GFZAWKC6uIXqiuQsAgHwyZIKjw805XCBiHUbQ9LvpdwSoD3RolyI jRjg== X-Forwarded-Encrypted: i=1; AJvYcCURP9qNYPelTb3vjKFp+sM7qCAgOzYhGz+NrnMRVyqGtr691LIE63j0ZaDorQ1Rf9BvBAJvGIPSV9TxImPYE1MkIuWg/wg= X-Gm-Message-State: AOJu0YyZvivng4jClJNat55mTL8Sexc7Nn4Mb8NhQww53lQ2etzpkYOB UJ2ITBZyDYm1/Dy2iWQ8CIJLMvCfPA5b6K68aBhDQeRNTmnXe0fll+XTD3SuJOE= X-Google-Smtp-Source: AGHT+IHAf5d0OiElf7D7znk8VZdW2wioTOERaPZGoG1/pAzXS9lgoTWLtbuNAygdNxnKoesueRU3Ew== X-Received: by 2002:a05:6102:30a9:b0:476:c875:33b8 with SMTP id y9-20020a05610230a900b00476c87533b8mr156043vsd.3.1711126768661; Fri, 22 Mar 2024 09:59:28 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-142-68-80-239.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.68.80.239]) by smtp.gmail.com with ESMTPSA id wl3-20020a05620a57c300b00789e6cc73e6sm921167qkn.48.2024.03.22.09.59.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 09:59:27 -0700 (PDT) Received: from jgg by wakko with local (Exim 4.95) (envelope-from ) id 1rniEp-00CRKv-5I; Fri, 22 Mar 2024 13:59:27 -0300 Date: Fri, 22 Mar 2024 13:59:27 -0300 From: Jason Gunthorpe To: Baolu Lu Cc: Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan , Joel Granados , iommu@lists.linux.dev, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 2/8] iommu/sva: Use iopf domain attach/detach interface Message-ID: <20240322165927.GG66976@ziepe.ca> References: <20240122073903.24406-1-baolu.lu@linux.intel.com> <20240122073903.24406-3-baolu.lu@linux.intel.com> <20240308174605.GV9225@ziepe.ca> Precedence: bulk X-Mailing-List: iommu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Mar 14, 2024 at 03:41:23PM +0800, Baolu Lu wrote: > The whole cookie mechanism aims to address two things: > > - Extend the domain lifetime until all pending page faults are > resolved. Like you answered, I think the flush is a simpler scheme.. > - Associate information about the iommu device with each attachment of > the domain so that the iommufd device object ID could be quickly > retrieved in the fault delivering path. > > I see we also need to stick a pointer in the domain for iommufd to get > > back to the hwpt, but that doesn't seem to need such a big system to > > accomplish - just add a void *. It would make sense for the domain to > > have some optional pointer to a struct for all the fault related data > > that becomes allocated when a PRI domain is created.. > > It's not getting back hwpt from domain, just getting the iommufd_device > in the fault delivering path. The iommufd_device is not per-domain, but > per-domain-attachment. It does make sense you'd need that, but I think something like this is a more direct way to get it. Caller allocates the handle struct. The iopf will provide the handle from the XA to the callback. container_of() not void * is used to in the caller's API. diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 68e648b5576706..0d29d8f0847cd9 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -46,6 +46,8 @@ static unsigned int iommu_def_domain_type __read_mostly; static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT); static u32 iommu_cmd_line __read_mostly; +enum { IOMMU_PASID_ARRAY_DOMAIN, IOMMU_PASID_ARRAY_HANDLE }; + struct iommu_group { struct kobject kobj; struct kobject *devices_kobj; @@ -3516,18 +3518,20 @@ static void __iommu_remove_group_pasid(struct iommu_group *group, } /* - * iommu_attach_device_pasid() - Attach a domain to pasid of device + * __iommu_attach_device_pasid() - Attach a domain to pasid of device * @domain: the iommu domain. * @dev: the attached device. * @pasid: the pasid of the device. * * Return: 0 on success, or an error. */ -int iommu_attach_device_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid) +int __iommu_attach_device_pasid(struct iommu_domain *domain, struct device *dev, + ioasid_t pasid, + struct iommu_pasid_handle *handle) { /* Caller must be a probed driver on dev */ struct iommu_group *group = dev->iommu_group; + void *xa_entry; void *curr; int ret; @@ -3541,9 +3545,14 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, return -EINVAL; mutex_lock(&group->mutex); - curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL); + if (handle) + xa_entry = xa_tag_pointer(handle, IOMMU_PASID_ARRAY_HANDLE); + else + xa_entry = xa_tag_pointer(domain, IOMMU_PASID_ARRAY_DOMAIN); + curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, xa_entry, + GFP_KERNEL); if (curr) { - ret = xa_err(curr) ? : -EBUSY; + ret = xa_err(curr) ?: -EBUSY; goto out_unlock; } @@ -3556,7 +3565,7 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, mutex_unlock(&group->mutex); return ret; } -EXPORT_SYMBOL_GPL(iommu_attach_device_pasid); +EXPORT_SYMBOL_GPL(__iommu_attach_device_pasid); /* * iommu_detach_device_pasid() - Detach the domain from pasid of device @@ -3600,13 +3609,23 @@ struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev, { /* Caller must be a probed driver on dev */ struct iommu_group *group = dev->iommu_group; + struct iommu_pasid_handle *handle; struct iommu_domain *domain; + void *xa_entry; if (!group) return NULL; xa_lock(&group->pasid_array); - domain = xa_load(&group->pasid_array, pasid); + xa_entry = xa_load(&group->pasid_array, pasid); + if (xa_pointer_tag(xa_entry) == IOMMU_PASID_ARRAY_HANDLE) { + handle = xa_untag_pointer(xa_entry); + domain = handle->domain; + } else if (xa_pointer_tag(xa_entry) == IOMMU_PASID_ARRAY_DOMAIN) { + domain = xa_untag_pointer(xa_entry); + } else { + domain = NULL; + } if (type && domain && domain->type != type) domain = ERR_PTR(-EBUSY); xa_unlock(&group->pasid_array); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 1ea2a820e1eb03..47c121d4e13f65 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -947,8 +947,27 @@ void iommu_device_release_dma_owner(struct device *dev); struct iommu_domain *iommu_sva_domain_alloc(struct device *dev, struct mm_struct *mm); -int iommu_attach_device_pasid(struct iommu_domain *domain, - struct device *dev, ioasid_t pasid); + +struct iommu_pasid_handle { + struct iommu_domain *domain; +}; + +int __iommu_attach_device_pasid(struct iommu_domain *domain, struct device *dev, + ioasid_t pasid, + struct iommu_pasid_handle *handle); + +static inline int iommu_attach_device_pasid(struct iommu_domain *domain, + struct device *dev, ioasid_t pasid) +{ + return __iommu_attach_device_pasid(domain, dev, pasid, NULL); +} +static inline int +iommu_attach_device_pasid_handle(struct iommu_pasid_handle *handle, + struct device *dev, ioasid_t pasid) +{ + return __iommu_attach_device_pasid(handle->domain, dev, pasid, handle); +} + void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid); struct iommu_domain *