Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2285484pxp; Mon, 21 Mar 2022 15:54:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1uvBcoRjhZS9IvLmulJoUH3yVmP87+oz8ub83xc023FMceRDW+4sPSRDKkoiGLjX9r+C3 X-Received: by 2002:a17:90a:8d08:b0:1c6:5ada:9919 with SMTP id c8-20020a17090a8d0800b001c65ada9919mr1511372pjo.90.1647903262870; Mon, 21 Mar 2022 15:54:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647903262; cv=none; d=google.com; s=arc-20160816; b=A/GVe1Yudre9ksXLOV5Uu7M909ZEqiScuaoYZ+Cm+CtHEe1sFo0wy6mLC9wG4MFIkd T7d3Bfmc0uvsksj263gXvicURZYiJAL3930KZflOCLFdmGr92bpp2eN6uFL83WodKR/H yjS/ENhZYlbfTNqPCQCtaicFe6+EP74cfoxsKHhCPukOR7qmxMxx18fV8KTDlpvApQul YA82QGRQzPmePiNul6p5mcJpxWHel6ZB0cnKNLPp/cO7XkthHkKbWgIdGDzNiFFY7WOh OjskuYvWUC4d5FHVq4O7h/rvZZNu1brtQZI14xxR5FZwtuqJC3EGgBCxry4SvrP+k5P6 wozA== 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=T62WiXTTaZvD7ZDEDwHERwO3bV+saoawZBmMVu90WD4=; b=KDNg5HG2PCU+i6HqRPkahaUCdy+i1xN6ChmGFoQjy6/4Jlw2zUacgKpRHzjKIFmYce TrLqcicnjT+Vp4Mk91fr0SFNWEXfczpiYuLWuEyajaQGzO5C5a+knCzHOE/0epOTCchj gZuqQJS+lm1flvwUzJsDm3KDI2PRNVEYWrWVT0BS0Djri7VoJQ5jm+IJr175q9I1MTb6 vBo41ID+YVKdhcm/2qUsxy2Z3GrcX/kdTQGGvL/xfJ3DvvlAetRFqFYyW7jgRNWufMGG U/nnr0M5KoKkKHzq9rVWUlYHIewzn4Bh+Fz9zLarHEbOZca+P62XudaWv1vx3znacy5I fLWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QuT8h2ro; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id f3-20020a170902ab8300b001542cf5c840si8702288plr.549.2022.03.21.15.54.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 15:54:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QuT8h2ro; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B8616353C41; Mon, 21 Mar 2022 15:04:16 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244954AbiCTGpQ (ORCPT + 99 others); Sun, 20 Mar 2022 02:45:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244948AbiCTGo4 (ORCPT ); Sun, 20 Mar 2022 02:44:56 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2227A11156 for ; Sat, 19 Mar 2022 23:43:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1647758609; x=1679294609; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vKdoL7uiyvYLFOvydHSnd0GHZVBK2y8vbn3OihTxHw0=; b=QuT8h2ropqwAycsaejVcUIsapHkKqLPU6vpjMjp3KOaLkfI5Q7V/Ir0c 0jSfR7biiskho1YDz68QhVMmPZuKk7fX4IbwmdLPcSNhTdnfclPkfh1Ck cEix/uFYpGMlYJw5W7hgLlpDmFinETbptZRaothmXiAj64QAU4GZcUOPF 3VxR+EOuMyhUoFqumTS4FZMTOVANmWFrWvqCgZTNN/b5/q5QDo+JBtXSk Ddq7u9nk+LZ7sLaK0jzi05HZdubqV3wQjrnyGB+isZwOvPnwC4L8Iny3t fCugq/rodJ8LvnFpMTAUA83+lgrG2ZgxWXYxLYE94RCIao3OHcEc52luW g==; X-IronPort-AV: E=McAfee;i="6200,9189,10291"; a="254924040" X-IronPort-AV: E=Sophos;i="5.90,195,1643702400"; d="scan'208";a="254924040" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Mar 2022 23:43:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,195,1643702400"; d="scan'208";a="691839965" Received: from allen-box.sh.intel.com ([10.239.159.48]) by fmsmga001.fm.intel.com with ESMTP; 19 Mar 2022 23:43:07 -0700 From: Lu Baolu To: Joerg Roedel , Jason Gunthorpe , Christoph Hellwig , Kevin Tian , Ashok Raj , Will Deacon , Robin Murphy , Jean-Philippe Brucker Cc: Eric Auger , Liu Yi L , Jacob jun Pan , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [PATCH RFC 09/11] iommu: Add iommu_get_domain_for_dev_pasid() Date: Sun, 20 Mar 2022 14:40:28 +0800 Message-Id: <20220320064030.2936936-10-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220320064030.2936936-1-baolu.lu@linux.intel.com> References: <20220320064030.2936936-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org This adds a helper to retrieve the iommu_domain which has been attached to a {device, pasid}. One usage scenario of this helper exists in the I/O page fault handling framework, where {device, pasid} was reported by hardware, and software needs to retrieve the attached domain for further routing. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 8 ++++++++ drivers/iommu/iommu.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 47c9aa5aa9c8..803e7b07605e 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -676,6 +676,8 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid); void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid); +struct iommu_domain * +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid); #else /* CONFIG_IOMMU_API */ @@ -1041,6 +1043,12 @@ static inline void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev, ioasid_t pasid) { } + +static inline struct iommu_domain * +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid) +{ + return NULL; +} #endif /* CONFIG_IOMMU_API */ #ifdef CONFIG_IOMMU_SVA diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 4f90b71c6f6e..508fdcabda5c 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -40,6 +40,7 @@ struct iommu_group { struct kobject kobj; struct kobject *devices_kobj; struct list_head devices; + struct xarray pasid_array; struct mutex mutex; void *iommu_data; void (*iommu_data_release)(void *iommu_data); @@ -632,6 +633,7 @@ struct iommu_group *iommu_group_alloc(void) mutex_init(&group->mutex); INIT_LIST_HEAD(&group->devices); INIT_LIST_HEAD(&group->entry); + xa_init(&group->pasid_array); ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL); if (ret < 0) { @@ -3086,6 +3088,7 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, { struct iommu_group *group; int ret = -EINVAL; + void *curr; if (!domain->ops->attach_dev_pasid) return -EINVAL; @@ -3098,7 +3101,16 @@ int iommu_attach_device_pasid(struct iommu_domain *domain, if (iommu_group_device_count(group) != 1) goto out_unlock; + xa_lock(&group->pasid_array); + curr = __xa_cmpxchg(&group->pasid_array, pasid, NULL, + domain, GFP_KERNEL); + xa_unlock(&group->pasid_array); + if (curr) + goto out_unlock; + ret = domain->ops->attach_dev_pasid(domain, dev, pasid); + if (ret) + xa_erase(&group->pasid_array, pasid); out_unlock: mutex_unlock(&group->mutex); @@ -3118,6 +3130,25 @@ void iommu_detach_device_pasid(struct iommu_domain *domain, mutex_lock(&group->mutex); domain->ops->detach_dev_pasid(domain, dev, pasid); + xa_erase(&group->pasid_array, pasid); + mutex_unlock(&group->mutex); + iommu_group_put(group); +} + +struct iommu_domain * +iommu_get_domain_for_dev_pasid(struct device *dev, ioasid_t pasid) +{ + struct iommu_domain *domain; + struct iommu_group *group; + + group = iommu_group_get(dev); + if (!group) + return NULL; + + mutex_lock(&group->mutex); + domain = xa_load(&group->pasid_array, pasid); mutex_unlock(&group->mutex); iommu_group_put(group); + + return domain; } -- 2.25.1