Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp536466pxk; Thu, 24 Sep 2020 11:33:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyp/Ibqom9P87U9zTjzROJPMWTbiPYQ85+FX8i219xWMioeXYkpA1sAGSNCGdd1E6zUDeSr X-Received: by 2002:aa7:d4d2:: with SMTP id t18mr124431edr.55.1600972404457; Thu, 24 Sep 2020 11:33:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600972404; cv=none; d=google.com; s=arc-20160816; b=nNh10xYMbJIFc+KXhCqaw2csWmxD7bvQYSP5a60eI5UMhUL+zNBhzfQHy5I4CBukts nSGu+yw2L3CvsKZyFlElPR1vx9UBqbY2KBNVHpsoM4eZftXdcNoO7snu9yKOHdLoMzlP RMzVc3McArmrJ+NQ539QwABWMJID3aOuWzIKcX1hdaMC2RQvy/1i2QA+BsWdUBbyCSxs cDM/mjvqjEaFna3zQXKuVP9kedfagJZv+GaVJwYXeiMl6Zsp1QzfUwZtR4eRTG35eaMy ukJYEhe1ZbZ2qMCz1usFHVH4Q3KWP++rbX0y4lmPi9hJMWvDPgAOX0+YYmfIipkCW6mT lAtQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=sMS4Rqgegz1ELdI9nVbVfuQcZ2zeRiL+gTaxWaPr3Dg=; b=n/j81bOmfHBsoUGBpla/gyxKmrqPMMhQY/2x3gN3RTfFK2iv4WswxQA/mmpEDrqDI9 ynl8veGcOEumBfPLCtqsxh4h4BkU6nQymWGrfgbTbFBQex15NiYs1Kxdc91K0cOibU/3 ltWotIb6ElnTZYzUafwGKQIv+v1LjZkpfTgZMerpZ54i7D7ZVLZGxs7ziz2sTkScxpOi jNby/uxKFrmxLFR4vq2FiBMXq8bgN7gdjDhSlc7ETUlvbD7v9VQwCpAUYjfgLnNQK4oI xnhXEwiqykTxjVTmNXNrus3+IlDfCUzKYJlNVYAMyqxZW4TcOB404ERemvR+zjmX8pUA MP6Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j33si122371edc.537.2020.09.24.11.33.01; Thu, 24 Sep 2020 11:33:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728742AbgIXS3p convert rfc822-to-8bit (ORCPT + 99 others); Thu, 24 Sep 2020 14:29:45 -0400 Received: from lhrrgout.huawei.com ([185.176.76.210]:2916 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728707AbgIXS3p (ORCPT ); Thu, 24 Sep 2020 14:29:45 -0400 Received: from lhreml721-chm.china.huawei.com (unknown [172.18.7.106]) by Forcepoint Email with ESMTP id F180640490732B561407; Thu, 24 Sep 2020 19:29:43 +0100 (IST) Received: from lhreml710-chm.china.huawei.com (10.201.108.61) by lhreml721-chm.china.huawei.com (10.201.108.72) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1913.5; Thu, 24 Sep 2020 19:29:43 +0100 Received: from lhreml710-chm.china.huawei.com ([169.254.81.184]) by lhreml710-chm.china.huawei.com ([169.254.81.184]) with mapi id 15.01.1913.007; Thu, 24 Sep 2020 19:29:43 +0100 From: Shameerali Kolothum Thodi To: Jacob Pan , "iommu@lists.linux-foundation.org" , LKML , Jean-Philippe Brucker , Lu Baolu , Joerg Roedel , David Woodhouse CC: "Tian, Kevin" , Raj Ashok , "Wu Hao" Subject: RE: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions Thread-Topic: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions Thread-Index: AQHWeFM+3LMH6JurcEu9bAF2eVkkYql4RlcA Date: Thu, 24 Sep 2020 18:29:43 +0000 Message-ID: References: <1598070918-21321-1-git-send-email-jacob.jun.pan@linux.intel.com> <1598070918-21321-5-git-send-email-jacob.jun.pan@linux.intel.com> In-Reply-To: <1598070918-21321-5-git-send-email-jacob.jun.pan@linux.intel.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.47.86.144] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jacob, > -----Original Message----- > From: iommu [mailto:iommu-bounces@lists.linux-foundation.org] On Behalf Of > Jacob Pan > Sent: 22 August 2020 05:35 > To: iommu@lists.linux-foundation.org; LKML ; > Jean-Philippe Brucker ; Lu Baolu > ; Joerg Roedel ; David > Woodhouse > Cc: Tian, Kevin ; Raj Ashok ; Wu > Hao > Subject: [PATCH v2 4/9] iommu/ioasid: Add reference couting functions > > There can be multiple users of an IOASID, each user could have hardware > contexts associated with the IOASID. In order to align lifecycles, > reference counting is introduced in this patch. It is expected that when > an IOASID is being freed, each user will drop a reference only after its > context is cleared. > > Signed-off-by: Jacob Pan > --- > drivers/iommu/ioasid.c | 113 > +++++++++++++++++++++++++++++++++++++++++++++++++ > include/linux/ioasid.h | 4 ++ > 2 files changed, 117 insertions(+) > > diff --git a/drivers/iommu/ioasid.c b/drivers/iommu/ioasid.c > index f73b3dbfc37a..5f31d63c75b1 100644 > --- a/drivers/iommu/ioasid.c > +++ b/drivers/iommu/ioasid.c > @@ -717,6 +717,119 @@ int ioasid_set_for_each_ioasid(struct ioasid_set > *set, > EXPORT_SYMBOL_GPL(ioasid_set_for_each_ioasid); > > /** > + * IOASID refcounting rules > + * - ioasid_alloc() set initial refcount to 1 > + * > + * - ioasid_free() decrement and test refcount. > + * If refcount is 0, ioasid will be freed. Deleted from the system-wide > + * xarray as well as per set xarray. The IOASID will be returned to the > + * pool and available for new allocations. > + * > + * If recount is non-zero, mark IOASID as > IOASID_STATE_FREE_PENDING. > + * No new reference can be added. The IOASID is not returned to the > pool > + * for reuse. > + * After free, ioasid_get() will return error but ioasid_find() and other > + * non refcount adding APIs will continue to work until the last reference > + * is dropped > + * > + * - ioasid_get() get a reference on an active IOASID > + * > + * - ioasid_put() decrement and test refcount of the IOASID. > + * If refcount is 0, ioasid will be freed. Deleted from the system-wide > + * xarray as well as per set xarray. The IOASID will be returned to the > + * pool and available for new allocations. > + * Do nothing if refcount is non-zero. > + * Is it better to have a return for this based on whether ioasid is freed or not? I was going through Jean's SMMUv3 SVA patches[1] and that one returns true if ioasid was freed. And that info is subsequently used to reset the pasid associated with a mm. Though, not sure that is still relevant or not. Thanks, Shameer 1. https://lore.kernel.org/linux-iommu/20200918101852.582559-3-jean-philippe@linaro.org/ > + * - ioasid_find() does not take reference, caller must hold reference > + * > + * ioasid_free() can be called multiple times without error until all refs are > + * dropped. > + */ > + > +int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid) > +{ > + struct ioasid_data *data; > + > + data = xa_load(&active_allocator->xa, ioasid); > + if (!data) { > + pr_err("Trying to get unknown IOASID %u\n", ioasid); > + return -EINVAL; > + } > + if (data->state == IOASID_STATE_FREE_PENDING) { > + pr_err("Trying to get IOASID being freed%u\n", ioasid); > + return -EBUSY; > + } > + > + if (set && data->set != set) { > + pr_err("Trying to get IOASID not in set%u\n", ioasid); > + /* data found but does not belong to the set */ > + return -EACCES; > + } > + refcount_inc(&data->users); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(ioasid_get_locked); > + > +/** > + * ioasid_get - Obtain a reference of an ioasid > + * @set > + * @ioasid > + * > + * Check set ownership if @set is non-null. > + */ > +int ioasid_get(struct ioasid_set *set, ioasid_t ioasid) > +{ > + int ret = 0; > + > + spin_lock(&ioasid_allocator_lock); > + ret = ioasid_get_locked(set, ioasid); > + spin_unlock(&ioasid_allocator_lock); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(ioasid_get); > + > +void ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid) > +{ > + struct ioasid_data *data; > + > + data = xa_load(&active_allocator->xa, ioasid); > + if (!data) { > + pr_err("Trying to put unknown IOASID %u\n", ioasid); > + return; > + } > + > + if (set && data->set != set) { > + pr_err("Trying to drop IOASID not in the set %u\n", ioasid); > + return; > + } > + > + if (!refcount_dec_and_test(&data->users)) { > + pr_debug("%s: IOASID %d has %d remainning users\n", > + __func__, ioasid, refcount_read(&data->users)); > + return; > + } > + ioasid_do_free(data); > +} > +EXPORT_SYMBOL_GPL(ioasid_put_locked); > + > +/** > + * ioasid_put - Drop a reference of an ioasid > + * @set > + * @ioasid > + * > + * Check set ownership if @set is non-null. > + */ > +void ioasid_put(struct ioasid_set *set, ioasid_t ioasid) > +{ > + spin_lock(&ioasid_allocator_lock); > + ioasid_put_locked(set, ioasid); > + spin_unlock(&ioasid_allocator_lock); > +} > +EXPORT_SYMBOL_GPL(ioasid_put); > + > +/** > * ioasid_find - Find IOASID data > * @set: the IOASID set > * @ioasid: the IOASID to find > diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h > index 412d025d440e..310abe4187a3 100644 > --- a/include/linux/ioasid.h > +++ b/include/linux/ioasid.h > @@ -76,6 +76,10 @@ int ioasid_attach_data(ioasid_t ioasid, void *data); > int ioasid_register_allocator(struct ioasid_allocator_ops *allocator); > void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); > void ioasid_is_in_set(struct ioasid_set *set, ioasid_t ioasid); > +int ioasid_get(struct ioasid_set *set, ioasid_t ioasid); > +int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid); > +void ioasid_put(struct ioasid_set *set, ioasid_t ioasid); > +void ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid); > int ioasid_set_for_each_ioasid(struct ioasid_set *sdata, > void (*fn)(ioasid_t id, void *data), > void *data); > -- > 2.7.4 > > _______________________________________________ > iommu mailing list > iommu@lists.linux-foundation.org > https://lists.linuxfoundation.org/mailman/listinfo/iommu