Message ID | 20230309080910.607396-6-yi.l.liu@intel.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp163052wrd; Thu, 9 Mar 2023 00:12:28 -0800 (PST) X-Google-Smtp-Source: AK7set9OMylvarKDBKNrGTtoisfr2BFxnwnvh30Nk31xnnu+vQ1o1V4x0BL8d9zoihAJgO/KX/Xn X-Received: by 2002:a62:184d:0:b0:5e3:ba14:8566 with SMTP id 74-20020a62184d000000b005e3ba148566mr13443310pfy.28.1678349548089; Thu, 09 Mar 2023 00:12:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678349548; cv=none; d=google.com; s=arc-20160816; b=OEBvpxnodRCUnYJUi3ikjaG+gF+tM/M6DkMhtLqNKZMSnW+2PvXmkNk+EQxssVKQda tyW0Xx99eTpeACmZcfGQc7KPXQN+GNao0t5pplh4Paq+0g/kAjq13Q66I62UldWF8e/E vJLb62EFceNLvgmhrv10CwrkQHLcFNPfeRymYfxVz7XoDdRIwgcYcqWB9XxPXprXfoFG zvapwnOC2Uvp4n/XMmnN1OE++4+cdZ7ZZHan9/XNi7JmymhPCO6d6cpmAonHH9PW0BFp 1ketiijGnf4Dovl5WHeIVK1FbsiS9VcBC5jCTbOoTlI35mPtOjdKB1ncSVDMSNtCOG83 XAOw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kzyH4dHkXLiAP7gbZYHNlR9e0Fiy4Cqnz2jAjZj1o3g=; b=KaJlQLUEyqNeDjmhYZgT0TRmgxmopOM7dOzzdOpJYO0KEycEQoMfDPYo9bihQlVWD5 STxX0W1bHY7E0pL3Q4/uOitrHQdzCu67LX6glJ02dwoSX4L6FfhPMNvw3mOII1KRE5Bv k4yBlukgsWa2NuxYkqGsIWLwLNe6UptTRA23b0lkulpBhbgfr9ZAcxOFxUA6IjtyUsA6 rspMgTn2eUPV8prtAZEeA67EOjjonvtNCasg+N5bV2mojgohlroPeiYna9Yv/Edy8TwO dphFhw+Gm6rkur7RAhnIG3+waqVu8sBONnz3RGSP536CzBoVRVz2/qZrP2Uw5K+vZVPm 8/JQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gUaae1S1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m2-20020a63fd42000000b004fcc0092a06si16322929pgj.663.2023.03.09.00.12.15; Thu, 09 Mar 2023 00:12:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=gUaae1S1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230002AbjCIIKK (ORCPT <rfc822;carlos.wei.hk@gmail.com> + 99 others); Thu, 9 Mar 2023 03:10:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229943AbjCIIJe (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 9 Mar 2023 03:09:34 -0500 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52F657302F; Thu, 9 Mar 2023 00:09:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1678349373; x=1709885373; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ze+YqyOq2FcrfezTmpCROik4OQxGxrLylWyUGP+0lZs=; b=gUaae1S1QdkkM12C3plJrp3gD35ZIuOuIl0wqjG6ppAaRe8HxEABnC2z Jrrfh64Az5sC4l+j3d7I2IRhNcn5OBwaykka1Z+/DwFzmDgrU0ZSiynWN 2yH0fsBwNf+zmALZFX42BtlOxbU6ChxFC+Z3pX2qs3xg7vu2YJwnbCH6L +9aYgtrFkHVyzCSheMdGJPfAyglR470sUSa9b6OsvOw/jqkFp1dg4atmm 3zkKTHG6ffNa0eocEt/o66AIdd9IDwTU1r6AIwkaFV6SvAKxasksubk7l wb8UHAwjwWl5kMELY1Q/iVI5vd5sZcv6RiB4ldIiaLf3lk5mOixZ70nRz w==; X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="364023072" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="364023072" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Mar 2023 00:09:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10643"; a="787471382" X-IronPort-AV: E=Sophos;i="5.98,245,1673942400"; d="scan'208";a="787471382" Received: from 984fee00a4c6.jf.intel.com ([10.165.58.231]) by fmsmga002.fm.intel.com with ESMTP; 09 Mar 2023 00:09:26 -0800 From: Yi Liu <yi.l.liu@intel.com> To: joro@8bytes.org, alex.williamson@redhat.com, jgg@nvidia.com, kevin.tian@intel.com, robin.murphy@arm.com, baolu.lu@linux.intel.com Cc: cohuck@redhat.com, eric.auger@redhat.com, nicolinc@nvidia.com, kvm@vger.kernel.org, mjrosato@linux.ibm.com, chao.p.peng@linux.intel.com, yi.l.liu@intel.com, yi.y.sun@linux.intel.com, peterx@redhat.com, jasowang@redhat.com, shameerali.kolothum.thodi@huawei.com, lulu@redhat.com, suravee.suthikulpanit@amd.com, iommu@lists.linux.dev, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 05/12] iommufd/hw_pagetable: Do not populate user-managed hw_pagetables Date: Thu, 9 Mar 2023 00:09:03 -0800 Message-Id: <20230309080910.607396-6-yi.l.liu@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230309080910.607396-1-yi.l.liu@intel.com> References: <20230309080910.607396-1-yi.l.liu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759877055678548291?= X-GMAIL-MSGID: =?utf-8?q?1759877055678548291?= |
Series |
iommufd: Add nesting infrastructure
|
|
Commit Message
Yi Liu
March 9, 2023, 8:09 a.m. UTC
From: Nicolin Chen <nicolinc@nvidia.com> A user-managed hw_pagetable does not need to get populated, since it is managed by a guest OS. Move the iopt_table_add_domain and list_add_tail calls into a helper, where the hwpt pointer will be redirected to its hwpt->parent if it's available. Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> --- drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-)
Comments
On 3/9/23 4:09 PM, Yi Liu wrote: > From: Nicolin Chen <nicolinc@nvidia.com> > > A user-managed hw_pagetable does not need to get populated, since it is > managed by a guest OS. Move the iopt_table_add_domain and list_add_tail > calls into a helper, where the hwpt pointer will be redirected to its > hwpt->parent if it's available. > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> > Signed-off-by: Yi Liu <yi.l.liu@intel.com> > --- > drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- > 1 file changed, 18 insertions(+), 2 deletions(-) > > diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c > index 16e92a1c150b..6e45ec0a66fa 100644 > --- a/drivers/iommu/iommufd/hw_pagetable.c > +++ b/drivers/iommu/iommufd/hw_pagetable.c > @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) > return 0; > } > > +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) > +{ > + int rc; > + > + if (hwpt->parent) > + hwpt = hwpt->parent; > + > + if (!list_empty(&hwpt->hwpt_item)) > + return 0; What is above check for? Is it "the hwpt has already been inserted into the hwpt list of its ioas in another place"? If so, is it possible that hwpt will be deleted from the list even when this user hwpt is still linked to the ioas? > + > + rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); > + if (rc) > + return rc; > + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); > + return 0; > +} > + > /** > * iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device > * @ictx: iommufd context > @@ -131,10 +148,9 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, > goto out_unlock; > } > > - rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); > + rc = iommufd_hw_pagetable_link_ioas(hwpt); > if (rc) > goto out_detach; > - list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); > > mutex_unlock(&idev->igroup->lock); > return hwpt; Best regards, baolu
On Fri, Mar 10, 2023 at 10:25:10AM +0800, Baolu Lu wrote: > External email: Use caution opening links or attachments > > > On 3/9/23 4:09 PM, Yi Liu wrote: > > From: Nicolin Chen <nicolinc@nvidia.com> > > > > A user-managed hw_pagetable does not need to get populated, since it is > > managed by a guest OS. Move the iopt_table_add_domain and list_add_tail > > calls into a helper, where the hwpt pointer will be redirected to its > > hwpt->parent if it's available. > > > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> > > Signed-off-by: Yi Liu <yi.l.liu@intel.com> > > --- > > drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- > > 1 file changed, 18 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c > > index 16e92a1c150b..6e45ec0a66fa 100644 > > --- a/drivers/iommu/iommufd/hw_pagetable.c > > +++ b/drivers/iommu/iommufd/hw_pagetable.c > > @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) > > return 0; > > } > > > > +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) > > +{ > > + int rc; > > + > > + if (hwpt->parent) > > + hwpt = hwpt->parent; > > + > > + if (!list_empty(&hwpt->hwpt_item)) > > + return 0; > > What is above check for? Is it "the hwpt has already been inserted into > the hwpt list of its ioas in another place"? > > If so, is it possible that hwpt will be deleted from the list even when > this user hwpt is still linked to the ioas? It means that the hwpt is already linked to the ioas. And the hwpt_item can be only empty after a destroy(). With that being said, after I think it through, perhaps Yi's previous change removing it might be better. So, it could be: ------------------------------------------------------------------------------- + /* + * Only a parent hwpt needs to be linked to the IOAS. And a hwpt->parent + * must be linked to the IOAS already, when it's being allocated. + */ if (hwpt->parent) - hwpt = hwpt->parent; - - if (!list_empty(&hwpt->hwpt_item)) return 0; ------------------------------------------------------------------------------- I was concerned about the case where a device gets attached to the nested hwpt without staging at the parent hwpt first. But, the link between the parent hwpt and the IOAS happened inside the allocation function now, not attach() any more. Thanks Nic
On 2023/3/10 14:50, Nicolin Chen wrote: > On Fri, Mar 10, 2023 at 10:25:10AM +0800, Baolu Lu wrote: >> External email: Use caution opening links or attachments >> >> >> On 3/9/23 4:09 PM, Yi Liu wrote: >>> From: Nicolin Chen <nicolinc@nvidia.com> >>> >>> A user-managed hw_pagetable does not need to get populated, since it is >>> managed by a guest OS. Move the iopt_table_add_domain and list_add_tail >>> calls into a helper, where the hwpt pointer will be redirected to its >>> hwpt->parent if it's available. >>> >>> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> >>> Signed-off-by: Yi Liu <yi.l.liu@intel.com> >>> --- >>> drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- >>> 1 file changed, 18 insertions(+), 2 deletions(-) >>> >>> diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c >>> index 16e92a1c150b..6e45ec0a66fa 100644 >>> --- a/drivers/iommu/iommufd/hw_pagetable.c >>> +++ b/drivers/iommu/iommufd/hw_pagetable.c >>> @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) >>> return 0; >>> } >>> >>> +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) >>> +{ >>> + int rc; >>> + >>> + if (hwpt->parent) >>> + hwpt = hwpt->parent; >>> + >>> + if (!list_empty(&hwpt->hwpt_item)) >>> + return 0; >> >> What is above check for? Is it "the hwpt has already been inserted into >> the hwpt list of its ioas in another place"? >> >> If so, is it possible that hwpt will be deleted from the list even when >> this user hwpt is still linked to the ioas? > > It means that the hwpt is already linked to the ioas. And the > hwpt_item can be only empty after a destroy(). > > With that being said, after I think it through, perhaps Yi's > previous change removing it might be better. So, it could be: > > ------------------------------------------------------------------------------- > + /* > + * Only a parent hwpt needs to be linked to the IOAS. And a hwpt->parent > + * must be linked to the IOAS already, when it's being allocated. > + */ > if (hwpt->parent) > - hwpt = hwpt->parent; > - > - if (!list_empty(&hwpt->hwpt_item)) > return 0; > > ------------------------------------------------------------------------------- > > I was concerned about the case where a device gets attached to > the nested hwpt without staging at the parent hwpt first. But, > the link between the parent hwpt and the IOAS happened inside > the allocation function now, not attach() any more. Yes, it's clearer. Best regards, baolu
On Thu, Mar 09, 2023 at 12:09:03AM -0800, Yi Liu wrote: > From: Nicolin Chen <nicolinc@nvidia.com> > > A user-managed hw_pagetable does not need to get populated, since it is > managed by a guest OS. Move the iopt_table_add_domain and list_add_tail > calls into a helper, where the hwpt pointer will be redirected to its > hwpt->parent if it's available. > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> > Signed-off-by: Yi Liu <yi.l.liu@intel.com> > --- > drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- > 1 file changed, 18 insertions(+), 2 deletions(-) > > diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c > index 16e92a1c150b..6e45ec0a66fa 100644 > --- a/drivers/iommu/iommufd/hw_pagetable.c > +++ b/drivers/iommu/iommufd/hw_pagetable.c > @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) > return 0; > } > > +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) > +{ > + int rc; > + > + if (hwpt->parent) This should be: hwpt->domain->type != IOMMU_DOMAIN_UNMANAGED Ie if we asked the driver to alloc a domain and it allocated an UNMANAGED domain then it means IOMMUFD manages the mappings and it should be populated from the IOAS. Arguably drivers should EOPNOTSUPP if presented with a parent in this situation, but still this code should be clear about the purpose. > + hwpt = hwpt->parent; And we definately shouldn't touch the parent. That is already setup and owned by someone else. Just return and don't do anything. Jason
On Fri, Mar 10, 2023 at 11:29:14AM -0400, Jason Gunthorpe wrote: > On Thu, Mar 09, 2023 at 12:09:03AM -0800, Yi Liu wrote: > > From: Nicolin Chen <nicolinc@nvidia.com> > > > > A user-managed hw_pagetable does not need to get populated, since it is > > managed by a guest OS. Move the iopt_table_add_domain and list_add_tail > > calls into a helper, where the hwpt pointer will be redirected to its > > hwpt->parent if it's available. > > > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> > > Signed-off-by: Yi Liu <yi.l.liu@intel.com> > > --- > > drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- > > 1 file changed, 18 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c > > index 16e92a1c150b..6e45ec0a66fa 100644 > > --- a/drivers/iommu/iommufd/hw_pagetable.c > > +++ b/drivers/iommu/iommufd/hw_pagetable.c > > @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) > > return 0; > > } > > > > +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) > > +{ > > + int rc; > > + > > + if (hwpt->parent) > > This should be: > > hwpt->domain->type != IOMMU_DOMAIN_UNMANAGED > > Ie if we asked the driver to alloc a domain and it allocated an > UNMANAGED domain then it means IOMMUFD manages the mappings and it > should be populated from the IOAS. OK. That looks better to me. > Arguably drivers should EOPNOTSUPP if presented with a parent in this > situation, but still this code should be clear about the purpose. > > > + hwpt = hwpt->parent; > > And we definately shouldn't touch the parent. That is already setup > and owned by someone else. Just return and don't do anything. Yes. Nic
> From: Nicolin Chen <nicolinc@nvidia.com> > Sent: Friday, March 10, 2023 2:51 PM > > On Fri, Mar 10, 2023 at 10:25:10AM +0800, Baolu Lu wrote: > > External email: Use caution opening links or attachments > > > > > > On 3/9/23 4:09 PM, Yi Liu wrote: > > > From: Nicolin Chen <nicolinc@nvidia.com> > > > > > > A user-managed hw_pagetable does not need to get populated, since it > is > > > managed by a guest OS. Move the iopt_table_add_domain and > list_add_tail > > > calls into a helper, where the hwpt pointer will be redirected to its > > > hwpt->parent if it's available. > > > > > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> > > > Signed-off-by: Yi Liu <yi.l.liu@intel.com> > > > --- > > > drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- > > > 1 file changed, 18 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/iommu/iommufd/hw_pagetable.c > b/drivers/iommu/iommufd/hw_pagetable.c > > > index 16e92a1c150b..6e45ec0a66fa 100644 > > > --- a/drivers/iommu/iommufd/hw_pagetable.c > > > +++ b/drivers/iommu/iommufd/hw_pagetable.c > > > @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct > iommufd_hw_pagetable *hwpt) > > > return 0; > > > } > > > > > > +static int iommufd_hw_pagetable_link_ioas(struct > iommufd_hw_pagetable *hwpt) > > > +{ > > > + int rc; > > > + > > > + if (hwpt->parent) > > > + hwpt = hwpt->parent; > > > + > > > + if (!list_empty(&hwpt->hwpt_item)) > > > + return 0; > > > > What is above check for? Is it "the hwpt has already been inserted into > > the hwpt list of its ioas in another place"? > > > > If so, is it possible that hwpt will be deleted from the list even when > > this user hwpt is still linked to the ioas? > > It means that the hwpt is already linked to the ioas. And the > hwpt_item can be only empty after a destroy(). > > With that being said, after I think it through, perhaps Yi's > previous change removing it might be better. So, it could be: > > ------------------------------------------------------------------------------- > + /* > + * Only a parent hwpt needs to be linked to the IOAS. And a hwpt- > >parent > + * must be linked to the IOAS already, when it's being allocated. > + */ > if (hwpt->parent) > - hwpt = hwpt->parent; > - > - if (!list_empty(&hwpt->hwpt_item)) > return 0; > > ------------------------------------------------------------------------------- > > I was concerned about the case where a device gets attached to > the nested hwpt without staging at the parent hwpt first. I think I was convinced with the reason that this helper may be called by allocation for both standalone s2 hwpt and the nested hwpt. So my change was not enough. Yours covers both cases. > But, > the link between the parent hwpt and the IOAS happened inside > the allocation function now, not attach() any more. Not quite get. This helper is also called in the allocation path. Is it? Anyhow, with Jason's comment, this helper may be reworked. We can sync later on the next version. Regards, Yi Liu
On Thu, Mar 23, 2023 at 08:06:26AM +0000, Liu, Yi L wrote: > External email: Use caution opening links or attachments > > > > From: Nicolin Chen <nicolinc@nvidia.com> > > Sent: Friday, March 10, 2023 2:51 PM > > > > On Fri, Mar 10, 2023 at 10:25:10AM +0800, Baolu Lu wrote: > > > External email: Use caution opening links or attachments > > > > > > > > > On 3/9/23 4:09 PM, Yi Liu wrote: > > > > From: Nicolin Chen <nicolinc@nvidia.com> > > > > > > > > A user-managed hw_pagetable does not need to get populated, since it > > is > > > > managed by a guest OS. Move the iopt_table_add_domain and > > list_add_tail > > > > calls into a helper, where the hwpt pointer will be redirected to its > > > > hwpt->parent if it's available. > > > > > > > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> > > > > Signed-off-by: Yi Liu <yi.l.liu@intel.com> > > > > --- > > > > drivers/iommu/iommufd/hw_pagetable.c | 20 ++++++++++++++++++-- > > > > 1 file changed, 18 insertions(+), 2 deletions(-) > > > > > > > > diff --git a/drivers/iommu/iommufd/hw_pagetable.c > > b/drivers/iommu/iommufd/hw_pagetable.c > > > > index 16e92a1c150b..6e45ec0a66fa 100644 > > > > --- a/drivers/iommu/iommufd/hw_pagetable.c > > > > +++ b/drivers/iommu/iommufd/hw_pagetable.c > > > > @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct > > iommufd_hw_pagetable *hwpt) > > > > return 0; > > > > } > > > > > > > > +static int iommufd_hw_pagetable_link_ioas(struct > > iommufd_hw_pagetable *hwpt) > > > > +{ > > > > + int rc; > > > > + > > > > + if (hwpt->parent) > > > > + hwpt = hwpt->parent; > > > > + > > > > + if (!list_empty(&hwpt->hwpt_item)) > > > > + return 0; > > > > > > What is above check for? Is it "the hwpt has already been inserted into > > > the hwpt list of its ioas in another place"? > > > > > > If so, is it possible that hwpt will be deleted from the list even when > > > this user hwpt is still linked to the ioas? > > > > It means that the hwpt is already linked to the ioas. And the > > hwpt_item can be only empty after a destroy(). > > > > With that being said, after I think it through, perhaps Yi's > > previous change removing it might be better. So, it could be: > > > > ------------------------------------------------------------------------------- > > + /* > > + * Only a parent hwpt needs to be linked to the IOAS. And a hwpt- > > >parent > > + * must be linked to the IOAS already, when it's being allocated. > > + */ > > if (hwpt->parent) > > - hwpt = hwpt->parent; > > - > > - if (!list_empty(&hwpt->hwpt_item)) > > return 0; > > > > ------------------------------------------------------------------------------- > > > > I was concerned about the case where a device gets attached to > > the nested hwpt without staging at the parent hwpt first. > > I think I was convinced with the reason that this helper may be > called by allocation for both standalone s2 hwpt and the nested > hwpt. So my change was not enough. Yours covers both cases. > > > But, > > the link between the parent hwpt and the IOAS happened inside > > the allocation function now, not attach() any more. > > Not quite get. This helper is also called in the allocation path. Is > it? Anyhow, with Jason's comment, this helper may be reworked. > We can sync later on the next version. We previously had this link_ioas() in attach() routine so we needed to make sure hwpt->parent got populated, because the device could be attached to an S1 HWPT directly. But now this is in the alloc() routine, so by the time an S1 HWPT is being allocated, an S2 HWPT must be allocated first and populated already. Nic
> From: Nicolin Chen <nicolinc@nvidia.com> > Sent: Thursday, March 23, 2023 4:12 PM > > On Thu, Mar 23, 2023 at 08:06:26AM +0000, Liu, Yi L wrote: > > External email: Use caution opening links or attachments > > > > > > > From: Nicolin Chen <nicolinc@nvidia.com> > > > Sent: Friday, March 10, 2023 2:51 PM > > > > > > On Fri, Mar 10, 2023 at 10:25:10AM +0800, Baolu Lu wrote: > > > > External email: Use caution opening links or attachments > > > > > > > > > > > > On 3/9/23 4:09 PM, Yi Liu wrote: > > > > > From: Nicolin Chen <nicolinc@nvidia.com> > > > > > > > > > > A user-managed hw_pagetable does not need to get populated, > since it > > > is > > > > > managed by a guest OS. Move the iopt_table_add_domain and > > > list_add_tail > > > > > calls into a helper, where the hwpt pointer will be redirected to its > > > > > hwpt->parent if it's available. > > > > > > > > > > Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> > > > > > Signed-off-by: Yi Liu <yi.l.liu@intel.com> > > > > > --- > > > > > drivers/iommu/iommufd/hw_pagetable.c | 20 > ++++++++++++++++++-- > > > > > 1 file changed, 18 insertions(+), 2 deletions(-) > > > > > > > > > > diff --git a/drivers/iommu/iommufd/hw_pagetable.c > > > b/drivers/iommu/iommufd/hw_pagetable.c > > > > > index 16e92a1c150b..6e45ec0a66fa 100644 > > > > > --- a/drivers/iommu/iommufd/hw_pagetable.c > > > > > +++ b/drivers/iommu/iommufd/hw_pagetable.c > > > > > @@ -43,6 +43,23 @@ int > iommufd_hw_pagetable_enforce_cc(struct > > > iommufd_hw_pagetable *hwpt) > > > > > return 0; > > > > > } > > > > > > > > > > +static int iommufd_hw_pagetable_link_ioas(struct > > > iommufd_hw_pagetable *hwpt) > > > > > +{ > > > > > + int rc; > > > > > + > > > > > + if (hwpt->parent) > > > > > + hwpt = hwpt->parent; > > > > > + > > > > > + if (!list_empty(&hwpt->hwpt_item)) > > > > > + return 0; > > > > > > > > What is above check for? Is it "the hwpt has already been inserted into > > > > the hwpt list of its ioas in another place"? > > > > > > > > If so, is it possible that hwpt will be deleted from the list even when > > > > this user hwpt is still linked to the ioas? > > > > > > It means that the hwpt is already linked to the ioas. And the > > > hwpt_item can be only empty after a destroy(). > > > > > > With that being said, after I think it through, perhaps Yi's > > > previous change removing it might be better. So, it could be: > > > > > > ------------------------------------------------------------------------------- > > > + /* > > > + * Only a parent hwpt needs to be linked to the IOAS. And a hwpt- > > > >parent > > > + * must be linked to the IOAS already, when it's being allocated. > > > + */ > > > if (hwpt->parent) > > > - hwpt = hwpt->parent; > > > - > > > - if (!list_empty(&hwpt->hwpt_item)) > > > return 0; > > > > > > ------------------------------------------------------------------------------- > > > > > > I was concerned about the case where a device gets attached to > > > the nested hwpt without staging at the parent hwpt first. > > > > I think I was convinced with the reason that this helper may be > > called by allocation for both standalone s2 hwpt and the nested > > hwpt. So my change was not enough. Yours covers both cases. > > > > > But, > > > the link between the parent hwpt and the IOAS happened inside > > > the allocation function now, not attach() any more. > > > > Not quite get. This helper is also called in the allocation path. Is > > it? Anyhow, with Jason's comment, this helper may be reworked. > > We can sync later on the next version. > > We previously had this link_ioas() in attach() routine so we > needed to make sure hwpt->parent got populated, because the > device could be attached to an S1 HWPT directly. But now this > is in the alloc() routine, so by the time an S1 HWPT is being > allocated, an S2 HWPT must be allocated first and populated > already. Aha, yes. 😊
diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 16e92a1c150b..6e45ec0a66fa 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -43,6 +43,23 @@ int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) return 0; } +static int iommufd_hw_pagetable_link_ioas(struct iommufd_hw_pagetable *hwpt) +{ + int rc; + + if (hwpt->parent) + hwpt = hwpt->parent; + + if (!list_empty(&hwpt->hwpt_item)) + return 0; + + rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); + if (rc) + return rc; + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); + return 0; +} + /** * iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device * @ictx: iommufd context @@ -131,10 +148,9 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_unlock; } - rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); + rc = iommufd_hw_pagetable_link_ioas(hwpt); if (rc) goto out_detach; - list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); mutex_unlock(&idev->igroup->lock); return hwpt;