Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp5291172pxu; Wed, 21 Oct 2020 20:25:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyajKApOVo1zKickJccHAvFIRES0r0vi+Q0yn7yDgst005kCEsiIKoNjTAj1SxRgkLyMK1r X-Received: by 2002:a05:6402:709:: with SMTP id w9mr448407edx.326.1603337114699; Wed, 21 Oct 2020 20:25:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603337114; cv=none; d=google.com; s=arc-20160816; b=VNSvKGYxlzV5ZlNapz+bJVMPnPiVO2BBhJOBnxS/p2vTl/6YKZDvF2T/GQOSXK19+g Z4e9K6mAzSmy/r4YRioBttdG7VXHvf1S6PhLg8KljWfwFzKLL/woczUWUG4PA1PapaFX 8c7d94CKnygjdjhhRd8+I1L2yfJgDxvFrHvn12XFQx1vWz6M8r9edAI9jTnrtSeLv4ks H+clRC9h4VS91dl013EhgceJZEVaDNzZcOm0iqx3TEy4OGrLEgu95RWJaFYCvoGkpxDG pBwopeRgHsJyrYDiSLhngRd0DmwX5tk3qVzOCCLN4xP2daL1pySFaSOD0tYQamOO78mT 3lzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=974kBlJ63qD5EYogdaxTOFg/LSMsXr6xzzQDrgDmhVo=; b=Y2Cy8D3gOe2YyULGeGTw5wBhjleYzlUgi3Cx+8AKn7yiA8Fc7jP80e/HRr5otBmSqX h/ALbJ1OPeIMbpEtFrcuf6iswAkpMAkKYFsn1BI1B0+/59flSfMNfX5mE0c7Wf7wJxEe k/JrnkM7PcxBhsuFieI5Q2YW9sbIck5mLYPOtxE0Dqekd0ahv2dPFEyhNs6LJ1O0p0Kh R076jtSCnkHf6Ql6ftiEiYPcKC47+YIz8u5aT+bXlZcmdv1hxEWVAyHylbpRbEsZ1Os+ EDjq00+2jmXYBsVdhqw7937h4J2d1AS+2OpdXiR6C/vS6z+pTIGzFUSr+SENMB51IRts dWZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TdPu+ofW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z14si84713ejc.750.2020.10.21.20.24.52; Wed, 21 Oct 2020 20:25:14 -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; dkim=pass header.i=@linaro.org header.s=google header.b=TdPu+ofW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2443328AbgJUO6X (ORCPT + 99 others); Wed, 21 Oct 2020 10:58:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2442789AbgJUO6X (ORCPT ); Wed, 21 Oct 2020 10:58:23 -0400 Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40715C0613CE for ; Wed, 21 Oct 2020 07:58:21 -0700 (PDT) Received: by mail-ed1-x542.google.com with SMTP id cq12so2922076edb.2 for ; Wed, 21 Oct 2020 07:58:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=974kBlJ63qD5EYogdaxTOFg/LSMsXr6xzzQDrgDmhVo=; b=TdPu+ofWfrp0hwZWxhUq2KJS2zjbi8MrOCRcvRVvru7pOPrruypz9/Jga2crVESF3W Sgd6gNwn7b7g9HAdaUgq69DALhZenDShkEzj4sDr70Ktq2eS4TH9j3nCiZ+Ibr20uv48 2HdetkUPnrVr5uz+ySBJQPe84nLfS3V5hAPiynmVgfOx/+drK4mGICmVHXYFm89vTnmV bRqtXTZEi2o4y8N/WmwOj1JlaunStuLcIkFSdWKD+ZTiThH7GDgkq+hlLcz6bgp+gVb5 JbOCGDSRnGNAFRWdG7pgjKtDmuvnG3YvittRYtVL7MSYHQsNN/WjH7tiKbJ23OMmJSV2 0qbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=974kBlJ63qD5EYogdaxTOFg/LSMsXr6xzzQDrgDmhVo=; b=WwffmNzaVZEuDY5x1jn+vZc49KH4PQR16FfqqNPg6pXcVhvFpWdVJ0b4wCVKEPd3st tZtLq2SicnZ0yHNXe1pfQ8i4fjeU1uN1/hg4cRWC2RFPAnLrnpGWOkqS6paWvPzW1KUn NeskHMGkvbwwNZuqtqE8DJVRu1G64rqXdzjiE3FcYJsc/th4FniQb7ODKxJ2xikwzCkD kWpipUGNwxxJ2b8VxqRuGcR8wnRzKwmid9BVmTGSp9yFpoP0VGVxISLdzTd9tEqQZxJY hce9Dic9ptMyc7lS3eUh9j677VKj/6DJWCr3l+1xKnzCly+VY99gHqSQaYV2rvvbnpr8 Y/gQ== X-Gm-Message-State: AOAM532f20fREF+UtjkkifO6cAwITR4lFPZ0ybgjToY9gg6uZ5iH1Rn+ 5bCohwnp9WnF57EMSLd0dEizrw== X-Received: by 2002:a05:6402:3191:: with SMTP id di17mr3387453edb.376.1603292299867; Wed, 21 Oct 2020 07:58:19 -0700 (PDT) Received: from myrica ([2001:1715:4e26:a7e0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id n25sm2377864ejd.114.2020.10.21.07.58.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Oct 2020 07:58:19 -0700 (PDT) Date: Wed, 21 Oct 2020 16:58:00 +0200 From: Jean-Philippe Brucker To: Jacob Pan Cc: iommu@lists.linux-foundation.org, LKML , Joerg Roedel , Alex Williamson , Lu Baolu , David Woodhouse , Jonathan Corbet , linux-api@vger.kernel.org, Jean-Philippe Brucker , Eric Auger , Jacob Pan , Yi Liu , "Tian, Kevin" , Raj Ashok , Wu Hao , Yi Sun , Dave Jiang , Randy Dunlap Subject: Re: [PATCH v3 08/14] iommu/ioasid: Add reference couting functions Message-ID: <20201021145800.GF1653231@myrica> References: <1601329121-36979-1-git-send-email-jacob.jun.pan@linux.intel.com> <1601329121-36979-9-git-send-email-jacob.jun.pan@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1601329121-36979-9-git-send-email-jacob.jun.pan@linux.intel.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 28, 2020 at 02:38:35PM -0700, Jacob Pan wrote: > 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 | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ > include/linux/ioasid.h | 24 ++++++++++ > 2 files changed, 141 insertions(+) > > diff --git a/drivers/iommu/ioasid.c b/drivers/iommu/ioasid.c > index 9628e78b2ab4..828cc44b1b1c 100644 > --- a/drivers/iommu/ioasid.c > +++ b/drivers/iommu/ioasid.c > @@ -16,8 +16,26 @@ static ioasid_t ioasid_capacity = PCI_PASID_MAX; > static ioasid_t ioasid_capacity_avail = PCI_PASID_MAX; > static DEFINE_XARRAY_ALLOC(ioasid_sets); > > +enum ioasid_state { > + IOASID_STATE_INACTIVE, > + IOASID_STATE_ACTIVE, > + IOASID_STATE_FREE_PENDING, > +}; > + > +/** > + * struct ioasid_data - Meta data about ioasid > + * > + * @id: Unique ID > + * @users: Number of active users > + * @state: Track state of the IOASID > + * @set: ioasid_set of the IOASID belongs to > + * @private: Private data associated with the IOASID > + * @rcu: For free after RCU grace period > + */ > struct ioasid_data { > ioasid_t id; > + refcount_t users; > + enum ioasid_state state; > struct ioasid_set *set; > void *private; > struct rcu_head rcu; > @@ -511,6 +529,8 @@ ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max, > goto exit_free; > } > data->id = id; > + data->state = IOASID_STATE_ACTIVE; > + refcount_set(&data->users, 1); > > /* Store IOASID in the per set data */ > if (xa_err(xa_store(&set->xa, id, data, GFP_ATOMIC))) { > @@ -560,6 +580,14 @@ static void ioasid_free_locked(struct ioasid_set *set, ioasid_t ioasid) > if (WARN_ON(!xa_load(&ioasid_sets, data->set->id))) > return; > > + /* Free is already in progress */ > + if (data->state == IOASID_STATE_FREE_PENDING) > + return; But the previous call to ioasid_free_locked() dropped a reference, then returned because more refs where held. Shouldn't this call also dec_and_test() the reference and call ioasid_do_free_locked() if necessary? > + > + data->state = IOASID_STATE_FREE_PENDING; > + if (!refcount_dec_and_test(&data->users)) > + return; > + > ioasid_do_free_locked(data); > } > > @@ -717,6 +745,95 @@ void ioasid_set_for_each_ioasid(struct ioasid_set *set, > } > EXPORT_SYMBOL_GPL(ioasid_set_for_each_ioasid); > > +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); Strange placement of the %u > + return -EBUSY; > + } > + > + /* Check set ownership if the set is non-null */ > + if (set && data->set != set) { > + pr_err("Trying to get IOASID %u outside the set\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); If this is a public facing let's add a lockdep_assert_held() to make sure they do hold the right lock. Same for ioasid_put_locked(). Thanks, Jean > + > +/** > + * ioasid_get - Obtain a reference to an ioasid > + * @set: the ioasid_set to check permission against if not NULL > + * @ioasid: the ID to remove > + * > + * > + * Return: 0 on success, error if failed. > + */ > +int ioasid_get(struct ioasid_set *set, ioasid_t ioasid) > +{ > + int ret; > + > + spin_lock(&ioasid_allocator_lock); > + ret = ioasid_get_locked(set, ioasid); > + spin_unlock(&ioasid_allocator_lock); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(ioasid_get); > + > +bool 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 false; > + } > + if (set && data->set != set) { > + pr_err("Trying to drop IOASID %u outside the set\n", ioasid); > + return false; > + } > + if (!refcount_dec_and_test(&data->users)) > + return false; > + > + ioasid_do_free_locked(data); > + > + return true; > +} > +EXPORT_SYMBOL_GPL(ioasid_put_locked); > + > +/** > + * ioasid_put - Release a reference to an ioasid > + * @set: the ioasid_set to check permission against if not NULL > + * @ioasid: the ID to remove > + * > + * Put a reference to the IOASID, free it when the number of references drops to > + * zero. > + * > + * Return: %true if the IOASID was freed, %false otherwise. > + */ > +bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid) > +{ > + bool ret; > + > + spin_lock(&ioasid_allocator_lock); > + ret = ioasid_put_locked(set, ioasid); > + spin_unlock(&ioasid_allocator_lock); > + return ret; > +} > +EXPORT_SYMBOL_GPL(ioasid_put); > + > /** > * ioasid_find - Find IOASID data > * @set: the IOASID set > diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h > index aab58bc26714..16d421357173 100644 > --- a/include/linux/ioasid.h > +++ b/include/linux/ioasid.h > @@ -73,6 +73,10 @@ void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, > bool (*getter)(void *)); > int ioasid_register_allocator(struct ioasid_allocator_ops *allocator); > void ioasid_unregister_allocator(struct ioasid_allocator_ops *allocator); > +int ioasid_get(struct ioasid_set *set, ioasid_t ioasid); > +int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid); > +bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid); > +bool ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid); > int ioasid_attach_data(ioasid_t ioasid, void *data); > void ioasid_detach_data(ioasid_t ioasid); > void ioasid_set_for_each_ioasid(struct ioasid_set *sdata, > @@ -112,6 +116,26 @@ static inline void ioasid_set_put(struct ioasid_set *set) > { > } > > +static inline int ioasid_get(struct ioasid_set *set, ioasid_t ioasid) > +{ > + return -ENOTSUPP; > +} > + > +static inline int ioasid_get_locked(struct ioasid_set *set, ioasid_t ioasid) > +{ > + return -ENOTSUPP; > +} > + > +static inline bool ioasid_put(struct ioasid_set *set, ioasid_t ioasid) > +{ > + return false; > +} > + > +static inline bool ioasid_put_locked(struct ioasid_set *set, ioasid_t ioasid) > +{ > + return false; > +} > + > static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (*getter)(void *)) > { > return NULL; > -- > 2.7.4 >