Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2871291pxa; Tue, 25 Aug 2020 05:49:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJiDnoD6yea4qMJ1s8IPRYmv2yX+eruhxFl3L7sbPBtRF2xqthsnpQrKfnH0+vdIFxF/Fm X-Received: by 2002:a05:6402:a51:: with SMTP id bt17mr9893034edb.382.1598359788585; Tue, 25 Aug 2020 05:49:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598359788; cv=none; d=google.com; s=arc-20160816; b=s/1Usd00d2ayY6e92Ka5gLysPkUWfU0g0038iYR0mwvP3vSb86wQRmM3CD+0B4rFv+ Po4UEX1cd3LqJaAO+qpmdEHpLkEi8EmtxyzT8PHzgtou3Fzk7pmGD6VVi8xdsU86V2X7 tcRDbeeTiAf57jtLyA3v7kscpF+DnyzBIE4wzwGQM+5HsXcR9QSotykfj56v0BdbAaVb dYeNoLJq1UgB+2r2Cz0IunQ/oXObomdeSFwKr2+ojDbUro3HFUUr6TkrYF2feO8+vDN7 Ien7bp0bMhiyZlqCj1STZG1VZ/V1iTO8Hlsoo5FP3ZkyDMTA226dcQ2ed/6/9IyL6WXa 4/1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=orjn/VuUmFAKu7SvmsNFkE/hoRSrEyeiukqJjo9MKuA=; b=Pse86Snwqh+G8VfKqbOsZxdrhnNI157sCU9y4pul/cfVawJdH6G0tC/DIDej40CfPm OfLgKHAzPw4m3fMBMuY6xKJoFQdK9rOKkzOI9vRS1CLY1K/S5mKcUXDJUtlJo4fLDCg6 4+iKsNLwc0YwxOjO9Mktq8g6MwXMre3l1J9DNB0DOIIm6NBy8pLgTY/Xq/z4RJ1kaIJF SIWNlIqy4ThNONuEunEQ54p/FjjVU79HGw/hPPJblqIyhCqkdmJSNAnhg389azwSlCH/ j99iR5TJV//lndqhzyi9noFQz1Op8eKG7VMhyCs2anY0GBjH9o1QiRmtV57LDMNr8Y/F rkMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ao1qF1KY; 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 lx7si8342158ejb.752.2020.08.25.05.48.53; Tue, 25 Aug 2020 05:49:48 -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=ao1qF1KY; 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 S1729761AbgHYKWc (ORCPT + 99 others); Tue, 25 Aug 2020 06:22:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50536 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729698AbgHYKWb (ORCPT ); Tue, 25 Aug 2020 06:22:31 -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 BE9F3C061574 for ; Tue, 25 Aug 2020 03:22:27 -0700 (PDT) Received: by mail-ed1-x542.google.com with SMTP id l63so7690443edl.9 for ; Tue, 25 Aug 2020 03:22:27 -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=orjn/VuUmFAKu7SvmsNFkE/hoRSrEyeiukqJjo9MKuA=; b=ao1qF1KYWbLmB8Rvob7GVVtTIKVV5d6PFFqIHS9ydk3k+eRpYe7cioZwJgj1wdwvGN zhVKELt8vhbVDUCm2Hk2+KrSmb+RdQswCx1p8t7Qgl1MZJtG1cYZtfPNHtr1YFICMY4V ECoDQLEDXGwAL8e6aAurywWHwC3Q+K+X+1EiJGUHgJC+GE4Xu3S6BSNFY4ILv35UOtsB i0UY+g7rPqMJ+JHkyPYpINjy+lT7OD1Z39m0SJaLkeiZ/Ivn1rK0gPrly/omtL5Wua9V 6KiMMBzL5OlvEK9hWENUzJWRFgBqnvMsOfLJXYytvU4KK3VT/woJR9mOwEuuH5H/sjIk 77cA== 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=orjn/VuUmFAKu7SvmsNFkE/hoRSrEyeiukqJjo9MKuA=; b=gDzSv03Rw/p7HQFl+QnnqebvNKPCPzvnCTvQPW1RY9qnH+XLUrOtRnZLbbkH4AzL+t zqx5ZJG0sZdap4J8+kAwW1rSpukUKl+9Dzh5FgCo2f6mW7HfSZpHWskVckkQ0ub7xDi+ 4q//QBzH25ZMpwra/1FRwlTKV4aY3iyb3wzyxo34EDxYSEAgCtHKz02I7QTvgYE1T5RZ rzTjnvTjviGSUy4G35SSOWWNps/sWNNHyaMjQDe8VVAM1QsRyc34wKHb7JK8ndbuptyJ Fs7kji0Qa/57HjOizMvKVv7jOZYHEhUhOlhx/07z/PvKKkPPpj6WGlJZ7rMFBrsLrlvz vX5w== X-Gm-Message-State: AOAM533SHOByyFnXLQg4GZwu7pxDO+CVX1ZdVqN1U+/2BRrQOm7p616j uFdeJ8HNwXbn29g3RJbGOMETrA== X-Received: by 2002:a50:ee92:: with SMTP id f18mr9021140edr.191.1598350946357; Tue, 25 Aug 2020 03:22:26 -0700 (PDT) Received: from myrica ([2001:1715:4e26:a7e0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id df22sm12730912edb.93.2020.08.25.03.22.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2020 03:22:25 -0700 (PDT) Date: Tue, 25 Aug 2020 12:22:09 +0200 From: Jean-Philippe Brucker To: Jacob Pan Cc: iommu@lists.linux-foundation.org, LKML , Jean-Philippe Brucker , Lu Baolu , Joerg Roedel , David Woodhouse , Yi Liu , "Tian, Kevin" , Raj Ashok , Eric Auger , Wu Hao Subject: Re: [PATCH v2 5/9] iommu/ioasid: Introduce ioasid_set private ID Message-ID: <20200825102209.GC3252704@myrica> References: <1598070918-21321-1-git-send-email-jacob.jun.pan@linux.intel.com> <1598070918-21321-6-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: <1598070918-21321-6-git-send-email-jacob.jun.pan@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Aug 21, 2020 at 09:35:14PM -0700, Jacob Pan wrote: > When an IOASID set is used for guest SVA, each VM will acquire its > ioasid_set for IOASID allocations. IOASIDs within the VM must have a > host/physical IOASID backing, mapping between guest and host IOASIDs can > be non-identical. IOASID set private ID (SPID) is introduced in this > patch to be used as guest IOASID. However, the concept of ioasid_set > specific namespace is generic, thus named SPID. > > As SPID namespace is within the IOASID set, the IOASID core can provide > lookup services at both directions. SPIDs may not be allocated when its > IOASID is allocated, the mapping between SPID and IOASID is usually > established when a guest page table is bound to a host PASID. > > Signed-off-by: Jacob Pan > --- > drivers/iommu/ioasid.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ > include/linux/ioasid.h | 12 +++++++++++ > 2 files changed, 66 insertions(+) > > diff --git a/drivers/iommu/ioasid.c b/drivers/iommu/ioasid.c > index 5f31d63c75b1..c0aef38a4fde 100644 > --- a/drivers/iommu/ioasid.c > +++ b/drivers/iommu/ioasid.c > @@ -21,6 +21,7 @@ enum ioasid_state { > * struct ioasid_data - Meta data about ioasid > * > * @id: Unique ID > + * @spid: Private ID unique within a set > * @users Number of active users > * @state Track state of the IOASID > * @set Meta data of the set this IOASID belongs to > @@ -29,6 +30,7 @@ enum ioasid_state { > */ > struct ioasid_data { > ioasid_t id; > + ioasid_t spid; > struct ioasid_set *set; > refcount_t users; > enum ioasid_state state; > @@ -326,6 +328,58 @@ int ioasid_attach_data(ioasid_t ioasid, void *data) > EXPORT_SYMBOL_GPL(ioasid_attach_data); > > /** > + * ioasid_attach_spid - Attach ioasid_set private ID to an IOASID > + * > + * @ioasid: the ID to attach > + * @spid: the ioasid_set private ID of @ioasid > + * > + * For IOASID that is already allocated, private ID within the set can be > + * attached via this API. Future lookup can be done via ioasid_find. via ioasid_find_by_spid()? > + */ > +int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) > +{ > + struct ioasid_data *ioasid_data; > + int ret = 0; > + > + spin_lock(&ioasid_allocator_lock); > + ioasid_data = xa_load(&active_allocator->xa, ioasid); > + > + if (!ioasid_data) { > + pr_err("No IOASID entry %d to attach SPID %d\n", > + ioasid, spid); > + ret = -ENOENT; > + goto done_unlock; > + } > + ioasid_data->spid = spid; > + > +done_unlock: > + spin_unlock(&ioasid_allocator_lock); > + return ret; > +} > +EXPORT_SYMBOL_GPL(ioasid_attach_spid); > + > +ioasid_t ioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid) Maybe add a bit of documentation as this is public-facing. > +{ > + struct ioasid_data *entry; > + unsigned long index; > + > + if (!xa_load(&ioasid_sets, set->sid)) { > + pr_warn("Invalid set\n"); > + return INVALID_IOASID; > + } > + > + xa_for_each(&set->xa, index, entry) { > + if (spid == entry->spid) { > + pr_debug("Found ioasid %lu by spid %u\n", index, spid); > + refcount_inc(&entry->users); Nothing prevents ioasid_free() from concurrently dropping the refcount to zero and calling ioasid_do_free(). The caller will later call ioasid_put() on a stale/reallocated index. > + return index; > + } > + } > + return INVALID_IOASID; > +} > +EXPORT_SYMBOL_GPL(ioasid_find_by_spid); > + > +/** > * ioasid_alloc - Allocate an IOASID > * @set: the IOASID set > * @min: the minimum ID (inclusive) > diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h > index 310abe4187a3..d4b3e83672f6 100644 > --- a/include/linux/ioasid.h > +++ b/include/linux/ioasid.h > @@ -73,6 +73,8 @@ bool ioasid_is_active(ioasid_t ioasid); > > void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid, bool (*getter)(void *)); > int ioasid_attach_data(ioasid_t ioasid, void *data); > +int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid); > +ioasid_t ioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid); > 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); > @@ -136,5 +138,15 @@ static inline int ioasid_attach_data(ioasid_t ioasid, void *data) > return -ENOTSUPP; > } > > +staic inline int ioasid_attach_spid(ioasid_t ioasid, ioasid_t spid) > +{ > + return -ENOTSUPP; > +} > + > +static inline ioasid_t ioasid_find_by_spid(struct ioasid_set *set, ioasid_t spid) > +{ > + return -ENOTSUPP; INVALID_IOASID Thanks, Jean > +} > + > #endif /* CONFIG_IOASID */ > #endif /* __LINUX_IOASID_H */ > -- > 2.7.4 >