Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4054694rwd; Mon, 29 May 2023 23:03:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6rmMjtvvNj9xTF77NnjeCKWm9AwzMTfYlvFcz0TIhtHg/xvaSvccZ/xY1k2Tf/aiynI/TA X-Received: by 2002:a17:902:eacb:b0:1ad:f407:37d1 with SMTP id p11-20020a170902eacb00b001adf40737d1mr1244115pld.52.1685426605565; Mon, 29 May 2023 23:03:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685426605; cv=none; d=google.com; s=arc-20160816; b=VjklVPvFrDGpEbaXFeaSRSrfzFW1D2jZk/bX3SM6ecpdUS1Lpw/ahvZETt8NgVA+ri +1LjKqL69+jnx6oVW9kLocC3bGuF6G3Us2EEOAEiObg7PiMgEmpAAewBIpRH5mHjhak3 cV3Uq+EImIEta1SwL4PRblyBo3xhwbeBkbQxTS0lvYRX1J1yYzYCUHAirenRdSybQx75 13DUXueWaixMeT/v8T/zNa3yboWMa4Dc8KfQykMw5woRqwpedpu3w9ucY5AQeyMkYWXH cY2HF3epSkaA/NW331uO1C5UwsDduKXkqeJjHisvwfpboHWgLZfz+IwefiL+p1x44EZd NjRw== 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=hkAYm5U5LAntypxtV2nF6PJ9P4UuRt8Gutq94JTJwYE=; b=Tt9SdqwhPI+vpOBx2GaurG255Ya4wtzTUui9sGze7h/WnKcxSnVbPRxDcuuRyZhpmD /cxcg9jbKXdASjGnkCCt8UcynVILpggNNlPsAgLD9WrhXOXrV9UnibpvxnKFnSCKGysT 8p6bwk4mmfhLQh/AiX7YsZWsslkwSUBFHRRhJJELs8OT8tU7EcDgEFrr4eSRl+T56tuK vTC9ha2zLoOyLlwwfM9HoF7/CcjHzQGfXB/LUN/AUw6+M9WVDR16L1/QyrUPBIzKSDME te0uAMUIT5szaKOisYOfi2nIYQLl8s8879OAsAaKr5vXvIynor2b1/J0rYhRwZMSAsup iwXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BhZDgvEK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j22-20020a170902759600b001aaef930752si1089819pll.647.2023.05.29.23.03.13; Mon, 29 May 2023 23:03:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BhZDgvEK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230240AbjE3FkR (ORCPT + 99 others); Tue, 30 May 2023 01:40:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230313AbjE3Fjr (ORCPT ); Tue, 30 May 2023 01:39:47 -0400 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0729FEC; Mon, 29 May 2023 22:39:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685425172; x=1716961172; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Y6jgEaCBH1za2zv3I11ejxRFyRn62+jMYqJd6rX08Vs=; b=BhZDgvEKW3OfNlunv3CyGbDPaAvQUrhY4KO35oqlvKGvvLlpaTb2nh+H jmh3auU/Yrj82o7VEK2DjyGBHLb0S/L5IL9+IhV/QNZyg7Sww2KTHYmyI QE3/tIVnLOfvRFKzDI/+OaZ1/SgUweYdBJelqoP8c4P/DaMAensgzfG1v fbj6tGBC/7799MKqWPg4a6v5vCczPG3nFd8aWB/IaJFh0mEFhxpRoyD7L mwnMFDDiPXkxM6XxTqy+48r29UXH5jvOjm2Vqa4lpwZnT5sihef6vvNSX 7VtlBuoGYbybH4rSlKl3kIknLjdCe7wfNg5QjWUf1gQIWqzxLxYKaFRf+ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="420579717" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="420579717" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 May 2023 22:38:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10725"; a="739369579" X-IronPort-AV: E=Sophos;i="6.00,203,1681196400"; d="scan'208";a="739369579" Received: from allen-box.sh.intel.com ([10.239.159.127]) by orsmga001.jf.intel.com with ESMTP; 29 May 2023 22:38:43 -0700 From: Lu Baolu To: Jason Gunthorpe , Kevin Tian , Joerg Roedel , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Nicolin Chen , Yi Liu , Jacob Pan Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org, virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org, Lu Baolu Subject: [RFC PATCHES 06/17] iommu: Add iommu page fault cookie helpers Date: Tue, 30 May 2023 13:37:13 +0800 Message-Id: <20230530053724.232765-7-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230530053724.232765-1-baolu.lu@linux.intel.com> References: <20230530053724.232765-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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 Add an xarray in iommu_fault_param as place holder for per-{device, pasid} fault cookie. The iommufd will use it to store the mapping of device object ID and the device pointer. This allows the iommufd to quickly retrieve the device object ID for a given {device, pasid} pair in the hot path of IO page fault delivery. Otherwise, the iommufd would have to maintain its own data structures to map {device, pasid} pairs to object IDs, and then look up the object ID on the critical path. This is not performance friendly. The iommufd is supposed to set the cookie when a fault capable domain is attached to the physical device or pasid, and clear the fault cookie when the domain is removed. Signed-off-by: Lu Baolu --- include/linux/iommu.h | 2 ++ drivers/iommu/iommu-priv.h | 3 +++ drivers/iommu/iommu.c | 45 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index c201704f9aea..9b0058ac971c 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -617,12 +617,14 @@ struct iommu_fault_event { * @data: handler private data * @faults: holds the pending faults which needs response * @lock: protect pending faults list + * @pasid_cookie: per-pasid fault cookie */ struct iommu_fault_param { iommu_dev_fault_handler_t handler; void *data; struct list_head faults; struct mutex lock; + struct xarray pasid_cookie; }; /** diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h index a6e694f59f64..17ab989702a0 100644 --- a/drivers/iommu/iommu-priv.h +++ b/drivers/iommu/iommu-priv.h @@ -17,5 +17,8 @@ static inline const struct iommu_ops *dev_iommu_ops(struct device *dev) int iommu_group_replace_domain(struct iommu_group *group, struct iommu_domain *new_domain); +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, + void *cookie); +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid); #endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index cace57c066f4..2f81be7f3a90 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1270,6 +1270,7 @@ int iommu_register_device_fault_handler(struct device *dev, param->fault_param->data = data; mutex_init(¶m->fault_param->lock); INIT_LIST_HEAD(¶m->fault_param->faults); + xa_init(¶m->fault_param->pasid_cookie); done_unlock: mutex_unlock(¶m->lock); @@ -1435,6 +1436,50 @@ int iommu_page_response(struct device *dev, } EXPORT_SYMBOL_GPL(iommu_page_response); +/** + * iommu_set_device_fault_cookie - Set a fault cookie for per-{device, pasid} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * @cookie: the opaque data + * + * Return the old cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_set_device_fault_cookie(struct device *dev, ioasid_t pasid, + void *cookie) +{ + struct iommu_fault_param *fault_param; + void *curr; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param = dev->iommu->fault_param; + curr = xa_store(&fault_param->pasid_cookie, pasid, cookie, GFP_KERNEL); + + return xa_is_err(curr) ? ERR_PTR(xa_err(curr)) : curr; +} +EXPORT_SYMBOL_NS_GPL(iommu_set_device_fault_cookie, IOMMUFD_INTERNAL); + +/** + * iommu_get_device_fault_cookie - Get the fault cookie for {device, pasid} + * @dev: the device to set the cookie + * @pasid: the pasid on this device + * + * Return the cookie on success, or ERR_PTR(err#) on failure. + */ +void *iommu_get_device_fault_cookie(struct device *dev, ioasid_t pasid) +{ + struct iommu_fault_param *fault_param; + + if (!dev->iommu || !dev->iommu->fault_param) + return ERR_PTR(-ENODEV); + + fault_param = dev->iommu->fault_param; + + return xa_load(&fault_param->pasid_cookie, pasid); +} +EXPORT_SYMBOL_NS_GPL(iommu_get_device_fault_cookie, IOMMUFD_INTERNAL); + /** * iommu_group_id - Return ID for a group * @group: the group to ID -- 2.34.1