Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp427653pxu; Fri, 23 Oct 2020 04:47:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxwuMGNngV5YAs2FEgccZ67Baoe+AB8/F6y5AOzE3oXpGARZnODfQfQVfUs2e2NQB/CbSWL X-Received: by 2002:aa7:c38b:: with SMTP id k11mr1749508edq.33.1603453620466; Fri, 23 Oct 2020 04:47:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603453620; cv=none; d=google.com; s=arc-20160816; b=wJNwwOgEZfmO30jmA6gVpfLoI0Ik/LPa8crpb7wgsngU1Z1J77Zyvr6Ajr6yF1sRz+ ysHnXeMxggP5fvGmjA/k9n2QmtYUCvmi2TQ8p04mEALc/7AFkUOZQvOhCTStqNtGKiBb t2eoSpG4qUoctRKoNv51Y+kaRnuSplpMrUl5xpXHgUi40EdbMozvySrLsJapyYKXOWCv W8gG72drGCtAZ3+zWUVJxjzNp9sCUqHACr026gtFY7eTvevphE8gdGG624Fz2qvvpdwH cOIkQX2M4/v3den27zOtZg7sdK3qRbdiYyu3gheS1gl4D3/78ztMWtJGdbwZVMGcnOZ7 1ZtQ== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=w1hT410pcvPp1e/jISfMDxaGMiE6qEWo/7L0amMfRgU=; b=zMMaqwsycNa7fnWfAZ/IezCwZfdewUE4jqGodnw2vxTbuhQx9Jr27gsPvDtXGo0d1H B80ybKkWOT6WDEMR6Lops6uTkYKhSdmL1e7nR+dlewpFfBRaOKYnDSz3G9ztIQEqxNV4 XfLFiIBzCBUXOuQgKVUJhowTuAS1cNa1a28ciQInWC3hPViRy3p2F0ibBu6AOdrlXm06 L4en92/9dKYGbcZn4kPIs8duQS+4htxUHa4V7WJ4hzJV2reG/Pjo6/kclhApIS/J2c16 Xg7tb2e7mjCHlVJdvgTKUIgMRVrIqu1xIjGNP03Ka2WqDpMQZmbw1pKv5hB8SjbXkinh KUHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cJLW7KXh; 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 s17si567884edw.200.2020.10.23.04.46.37; Fri, 23 Oct 2020 04:47:00 -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=cJLW7KXh; 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 S463176AbgJWLnz (ORCPT + 99 others); Fri, 23 Oct 2020 07:43:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S463128AbgJWLny (ORCPT ); Fri, 23 Oct 2020 07:43:54 -0400 Received: from mail-ej1-x644.google.com (mail-ej1-x644.google.com [IPv6:2a00:1450:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 22D23C0613D2 for ; Fri, 23 Oct 2020 04:43:54 -0700 (PDT) Received: by mail-ej1-x644.google.com with SMTP id p5so1960517ejj.2 for ; Fri, 23 Oct 2020 04:43:54 -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:content-transfer-encoding:in-reply-to; bh=w1hT410pcvPp1e/jISfMDxaGMiE6qEWo/7L0amMfRgU=; b=cJLW7KXhZrozobsjIxLY2+buIwLvxqKyr27AnZ7HYQWa0uUdukWbasKJ2czJU24TAv +xOQlWLO6+S1xUJvXWypAbTgXaMAaueeDEY79s+fzRprkFavmAc4GWPNEArbGHty9Gen X9E+dVy9ofbqsgurh6cROggxU5YEL5D66l3f384zkftR4EosrgAMXRSRkMByyTKWVveo biP2CbDeOAiFRBbE/JyiUlPoN3H+aGVTUr5eNfYhM/yyLuc32ibRrp/B5bQyt08h6B4O hCYerAR9pgUKtxCUaLnWYi4cGUe26zmp9KDA1qKweDdurQd8Oe3cqfTdLeoeKCXXyOJv G7Zw== 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:content-transfer-encoding :in-reply-to; bh=w1hT410pcvPp1e/jISfMDxaGMiE6qEWo/7L0amMfRgU=; b=A3B5rS9wdot6KdnqjPx9MXxClU+UB2QKzx7AJscd1KttuifWo8rAxZsIVVxeQdpHWb vCRBfyTz0G7d2sLGP2JwvZ+/g92L/4lsUW6FnAiWiXZCPq7hl9QIVmEWLSoKjALEQUWk 8w7dHJxlOAFSWFSp8l1I6/jtcyTMmt0tM19+wNnTb7bZSYsIimMeKTDoVWMzvXx6n1I/ RPJEpd6Zxd1LzFdgbu63fS2vOgXTQ/VyUPoyyrK/rBZbsKAOPzDj6TNNMdbr+AhSqIfU MCO5h8GW7SQIo3pb2jW/8qjO4gSCOauq5Z4Me+zVrRDcphnUN9pkn73eTAugsprz8Hjp IyCQ== X-Gm-Message-State: AOAM532hSeOoBkH9zuC5DVhTvhWPonPtQvdjsWjxlB/xo0y5cOaaWcwt MHx3BP2giqnOuHq80UjzjX1v+A== X-Received: by 2002:a17:906:66d2:: with SMTP id k18mr1469410ejp.113.1603453432716; Fri, 23 Oct 2020 04:43:52 -0700 (PDT) Received: from myrica ([2001:1715:4e26:a7e0:116c:c27a:3e7f:5eaf]) by smtp.gmail.com with ESMTPSA id sb29sm678261ejb.76.2020.10.23.04.43.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Oct 2020 04:43:52 -0700 (PDT) Date: Fri, 23 Oct 2020 13:43:32 +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 11/14] iommu/ioasid: Support mm type ioasid_set notifications Message-ID: <20201023114332.GD2265982@myrica> References: <1601329121-36979-1-git-send-email-jacob.jun.pan@linux.intel.com> <1601329121-36979-12-git-send-email-jacob.jun.pan@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1601329121-36979-12-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:38PM -0700, Jacob Pan wrote: > As a system-wide resource, IOASID is often shared by multiple kernel > subsystems that are independent of each other. However, at the > ioasid_set level, these kernel subsystems must communicate with each > other for ownership checking, event notifications, etc. For example, on > Intel Scalable IO Virtualization (SIOV) enabled platforms, KVM and VFIO > instances under the same process/guest must be aware of a shared IOASID > set. > IOASID_SET_TYPE_MM token type was introduced to explicitly mark an > IOASID set that belongs to a process, thus use the same mm_struct > pointer as a token. Users of the same process can then identify with > each other based on this token. > > This patch introduces MM token specific event registration APIs. Event > subscribers such as KVM instances can register IOASID event handler > without the knowledge of its ioasid_set. Event handlers are registered > based on its mm_struct pointer as a token. In case when subscribers > register handler *prior* to the creation of the ioasid_set, the > handler’s notification block is stored in a pending list within IOASID > core. Once the ioasid_set of the MM token is created, the notification > block will be registered by the IOASID core. > > Signed-off-by: Liu Yi L > Signed-off-by: Wu Hao > Signed-off-by: Jacob Pan > --- > drivers/iommu/ioasid.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ > include/linux/ioasid.h | 15 +++++++ > 2 files changed, 132 insertions(+) > > diff --git a/drivers/iommu/ioasid.c b/drivers/iommu/ioasid.c > index 894b17c06ead..d5faeb559a43 100644 > --- a/drivers/iommu/ioasid.c > +++ b/drivers/iommu/ioasid.c > @@ -889,6 +889,29 @@ void ioasid_set_put(struct ioasid_set *set) > } > EXPORT_SYMBOL_GPL(ioasid_set_put); > > +/* > + * ioasid_find_mm_set - Retrieve IOASID set with mm token > + * Take a reference of the set if found. > + */ > +static struct ioasid_set *ioasid_find_mm_set(struct mm_struct *token) > +{ > + struct ioasid_set *set; > + unsigned long index; > + > + spin_lock(&ioasid_allocator_lock); This could deadlock since ioasid_set_put() takes the nb_lock while holding the allocator_lock. > + > + xa_for_each(&ioasid_sets, index, set) { > + if (set->type == IOASID_SET_TYPE_MM && set->token == token) { > + refcount_inc(&set->ref); > + goto exit_unlock; > + } > + } > + set = NULL; > +exit_unlock: > + spin_unlock(&ioasid_allocator_lock); > + return set; > +} > + > /** > * ioasid_adjust_set - Adjust the quota of an IOASID set > * @set: IOASID set to be assigned > @@ -1121,6 +1144,100 @@ void ioasid_unregister_notifier(struct ioasid_set *set, > } > EXPORT_SYMBOL_GPL(ioasid_unregister_notifier); > > +/** > + * ioasid_register_notifier_mm - Register a notifier block on the IOASID set > + * created by the mm_struct pointer as the token > + * > + * @mm: the mm_struct token of the ioasid_set > + * @nb: notfier block to be registered on the ioasid_set Maybe add that the priority in @nb needs to be set by the caller using a ioasid_notifier_priority value > + * > + * This a variant of ioasid_register_notifier() where the caller intends to > + * listen to IOASID events belong the ioasid_set created under the same that belong to > + * process. Caller is not aware of the ioasid_set, no need to hold reference > + * of the ioasid_set. > + */ > +int ioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb) > +{ > + struct ioasid_set_nb *curr; > + struct ioasid_set *set; > + int ret = 0; > + > + if (!mm) > + return -EINVAL; > + > + spin_lock(&ioasid_nb_lock); > + > + /* Check for duplicates, nb is unique per set */ > + list_for_each_entry(curr, &ioasid_nb_pending_list, list) { > + if (curr->token == mm && curr->nb == nb) { > + ret = -EBUSY; > + goto exit_unlock; > + } > + } > + > + /* Check if the token has an existing set */ > + set = ioasid_find_mm_set(mm); > + if (!set) { > + /* Add to the rsvd list as inactive */ > + curr->active = false; curr is invalid here > + } else { > + /* REVISIT: Only register empty set for now. Can add an option > + * in the future to playback existing PASIDs. > + */ You can drop this comment > + if (set->nr_ioasids) { > + pr_warn("IOASID set %d not empty\n", set->id); > + ret = -EBUSY; > + goto exit_unlock; > + } > + curr = kzalloc(sizeof(*curr), GFP_ATOMIC); Needs to be in the common path, before this block > + if (!curr) { > + ret = -ENOMEM; > + goto exit_unlock; > + } > + curr->token = mm; > + curr->nb = nb; > + curr->active = true; > + curr->set = set; > + > + /* Set already created, add to the notifier chain */ > + atomic_notifier_chain_register(&set->nh, nb); > + /* > + * Do not hold a reference, if the set gets destroyed, the nb > + * entry will be marked inactive. > + */ > + ioasid_set_put(set); > + } > + > + list_add(&curr->list, &ioasid_nb_pending_list); > + > +exit_unlock: > + spin_unlock(&ioasid_nb_lock); > + return ret; > +} > +EXPORT_SYMBOL_GPL(ioasid_register_notifier_mm); > + > +void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb) > +{ > + struct ioasid_set_nb *curr; > + > + spin_lock(&ioasid_nb_lock); > + list_for_each_entry(curr, &ioasid_nb_pending_list, list) { > + if (curr->token == mm && curr->nb == nb) { > + list_del(&curr->list); > + spin_unlock(&ioasid_nb_lock); > + if (curr->active) { > + atomic_notifier_chain_unregister(&curr->set->nh, > + nb); I think it's possible for ioasid_set_put_locked() to free the set right after we release the lock, then this unregister() will be use-after-free. Best keep holding the lock for this. > + } > + kfree(curr); > + return; > + } > + } > + pr_warn("No ioasid set found for mm token %llx\n", (u64)mm); Use %p Overall I think error messages in this series could be demoted to pr_debug, since they're useful when developing users to this API, but figuring out whether they can be user-triggered and need care is too much work. Thanks, Jean > + spin_unlock(&ioasid_nb_lock); > +} > +EXPORT_SYMBOL_GPL(ioasid_unregister_notifier_mm); > + > MODULE_AUTHOR("Jean-Philippe Brucker "); > MODULE_AUTHOR("Jacob Pan "); > MODULE_DESCRIPTION("IO Address Space ID (IOASID) allocator"); > diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h > index 1b551c99d568..c6cc855aadb6 100644 > --- a/include/linux/ioasid.h > +++ b/include/linux/ioasid.h > @@ -132,6 +132,9 @@ void ioasid_unregister_notifier(struct ioasid_set *set, > void ioasid_set_for_each_ioasid(struct ioasid_set *sdata, > void (*fn)(ioasid_t id, void *data), > void *data); > +int ioasid_register_notifier_mm(struct mm_struct *mm, struct notifier_block *nb); > +void ioasid_unregister_notifier_mm(struct mm_struct *mm, struct notifier_block *nb); > + > #else /* !CONFIG_IOASID */ > static inline void ioasid_install_capacity(ioasid_t total) > { > @@ -238,5 +241,17 @@ static inline void ioasid_set_for_each_ioasid(struct ioasid_set *sdata, > void *data) > { > } > + > +static inline int ioasid_register_notifier_mm(struct mm_struct *mm, > + struct notifier_block *nb) > +{ > + return -ENOTSUPP; > +} > + > +static inline void ioasid_unregister_notifier_mm(struct mm_struct *mm, > + struct notifier_block *nb) > +{ > +} > + > #endif /* CONFIG_IOASID */ > #endif /* __LINUX_IOASID_H */ > -- > 2.7.4 >