public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Nicolin Chen <nicolinc@nvidia.com>
Cc: kevin.tian@intel.com, yi.l.liu@intel.com, joro@8bytes.org,
	will@kernel.org, robin.murphy@arm.com,
	alex.williamson@redhat.com, shuah@kernel.org,
	linux-kernel@vger.kernel.org, iommu@lists.linux.dev,
	kvm@vger.kernel.org, linux-kselftest@vger.kernel.org,
	mjrosato@linux.ibm.com, farman@linux.ibm.com
Subject: Re: [PATCH v9 3/6] iommufd: Add iommufd_access_change_ioas helper
Date: Thu, 27 Jul 2023 11:40:17 -0300	[thread overview]
Message-ID: <ZMKB0XrtGIvR3lzB@nvidia.com> (raw)
In-Reply-To: <5d7d275ff12c1c991ac80392b19f1ebf5214177d.1690440730.git.nicolinc@nvidia.com>

On Thu, Jul 27, 2023 at 12:23:08AM -0700, Nicolin Chen wrote:
> The complication of the mutex and refcount will be amplified after we
> introduce the replace support for access. So, add a preparatory change
> of a constitutive helper iommufd_access_change_ioas(), to take care of
> the existing iommufd_access_attach() and iommufd_access_detach().
> 
> Also, update the unprotect routine in iommufd_access_destroy_object()
> to calling the new iommufd_access_change_ioas() helper.
> 
> Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> ---
>  drivers/iommu/iommufd/device.c | 112 ++++++++++++++++++++-------------
>  1 file changed, 69 insertions(+), 43 deletions(-)
> 
> diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
> index 7a3e8660b902..d9680a247e1c 100644
> --- a/drivers/iommu/iommufd/device.c
> +++ b/drivers/iommu/iommufd/device.c
> @@ -684,17 +684,69 @@ void iommufd_device_detach(struct iommufd_device *idev)
>  }
>  EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD);
>  
> +static int iommufd_access_change_ioas(struct iommufd_access *access,
> +				      struct iommufd_ioas *new_ioas)
> +{
> +	u32 iopt_access_list_id = access->iopt_access_list_id;
> +	struct iommufd_ioas *cur_ioas = access->ioas;
> +	int rc;
> +
> +	lockdep_assert_held(&access->ioas_lock);
> +
> +	/* We are racing with a concurrent detach, bail */
> +	if (cur_ioas != access->ioas_unpin)
> +		return -EBUSY;
> +
> +	if (IS_ERR(new_ioas))
> +		return PTR_ERR(new_ioas);
> +
> +	if (cur_ioas == new_ioas) {
> +		/* Do not forget to put since we allow a duplication */
> +		iommufd_put_object(&new_ioas->obj);
> +		return 0;
> +	}
> +
> +	/*
> +	 * Set ioas to NULL to block any further iommufd_access_pin_pages().
> +	 * iommufd_access_unpin_pages() can continue using access->ioas_unpin.
> +	 */
> +	access->ioas = NULL;
> +
> +	if (new_ioas) {
> +		rc = iopt_add_access(&new_ioas->iopt, access);
> +		if (rc) {
> +			iommufd_put_object(&new_ioas->obj);
> +			access->ioas = cur_ioas;
> +			return rc;
> +		}
> +		iommufd_ref_to_users(&new_ioas->obj);

Kevin's suggestion to just open code the refcount_inc here

And have a wrapper func that does:

iommufd_access_change_ioas_id(struct iommufd_access *access, u32 id)
{
	struct iommufd_ioas *ioas = iommufd_get_ioas(ictx, ioas_id);
	int rc;

	if (IS_ERR(ioas))
		return PTR_ERR(ioas);
	rc = iommufd_access_change_ioas(access, ioas);
	iommufd_put_object(&ioas->obj);
	return rc;
}

Does looks cleaner

Then we delete iommufd_ref_to_users() as there are no users (once all
the branches are merged).

Logic looks OK otherwise

Jason

  reply	other threads:[~2023-07-27 14:40 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-27  7:23 [PATCH v9 0/6] cover-letter: Add IO page table replacement support Nicolin Chen
2023-07-27  7:23 ` [PATCH v9 1/6] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Nicolin Chen
2023-07-27  7:23 ` [PATCH v9 2/6] iommufd: Allow passing in iopt_access_list_id to iopt_remove_access() Nicolin Chen
2023-07-27  9:07   ` Nicolin Chen
2023-07-27 14:34   ` Jason Gunthorpe
2023-07-27  7:23 ` [PATCH v9 3/6] iommufd: Add iommufd_access_change_ioas helper Nicolin Chen
2023-07-27 14:40   ` Jason Gunthorpe [this message]
2023-07-27 18:47     ` Nicolin Chen
2023-07-27  7:23 ` [PATCH v9 4/6] iommufd: Add iommufd_access_replace() API Nicolin Chen
2023-07-27  7:23 ` [PATCH v9 5/6] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Nicolin Chen
2023-07-27  7:23 ` [PATCH v9 6/6] vfio: Support IO page table replacement Nicolin Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZMKB0XrtGIvR3lzB@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=alex.williamson@redhat.com \
    --cc=farman@linux.ibm.com \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mjrosato@linux.ibm.com \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.com \
    --cc=shuah@kernel.org \
    --cc=will@kernel.org \
    --cc=yi.l.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox