From nobody Tue Dec 30 15:11:24 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8A838C47071 for ; Wed, 15 Nov 2023 03:07:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234366AbjKODHS (ORCPT ); Tue, 14 Nov 2023 22:07:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234339AbjKODHK (ORCPT ); Tue, 14 Nov 2023 22:07:10 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31FF710F; Tue, 14 Nov 2023 19:07:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700017623; x=1731553623; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IJZIZgbDB/RGsh8p7+wplfTs/rR9s/vLQ+3vq6I7Tfc=; b=dY878dU3ceiymJhKjkYZk1YBxNinQojZpv+rLIWRllpy7Eut0/xLsT42 izKXjZIXCeqPnqiUGE5wRE7TiIBK0kouNBbXh7Cib0KR0OPhCY3f1TTOI uz0rO/PCi5vEOcxTJg2aR4YACMFEAug9S9eHaKUvuUBS75UK7tKBSvr15 UAzZxbA/MENsSJIDJ77RLVlfCcpik7/ehU3UFdKs9O+i5LgQ2KyCR/J6+ oR77hwUYGLZkoRjTAtZWQLwxmXBlougctigosJNCUY2ANto9mDw1H+z7c JHmDwrWChzL9m0Jl2Bi06MOgRXYEsIdRcMaZrnKlvW9VBaFosUfwoddiv g==; X-IronPort-AV: E=McAfee;i="6600,9927,10894"; a="394715400" X-IronPort-AV: E=Sophos;i="6.03,303,1694761200"; d="scan'208";a="394715400" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Nov 2023 19:07:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10894"; a="1012128808" X-IronPort-AV: E=Sophos;i="6.03,303,1694761200"; d="scan'208";a="1012128808" Received: from allen-box.sh.intel.com ([10.239.159.127]) by fmsmga006.fm.intel.com with ESMTP; 14 Nov 2023 19:06:58 -0800 From: Lu Baolu To: Joerg Roedel , Will Deacon , Robin Murphy , Jason Gunthorpe , Kevin Tian , Jean-Philippe Brucker , Nicolin Chen Cc: Yi Liu , Jacob Pan , Yan Zhao , iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Lu Baolu , Jason Gunthorpe Subject: [PATCH v7 05/12] iommu: Merge iopf_device_param into iommu_fault_param Date: Wed, 15 Nov 2023 11:02:19 +0800 Message-Id: <20231115030226.16700-6-baolu.lu@linux.intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231115030226.16700-1-baolu.lu@linux.intel.com> References: <20231115030226.16700-1-baolu.lu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The struct dev_iommu contains two pointers, fault_param and iopf_param. The fault_param pointer points to a data structure that is used to store pending faults that are awaiting responses. The iopf_param pointer points to a data structure that is used to store partial faults that are part of a Page Request Group. The fault_param and iopf_param pointers are essentially duplicate. This causes memory waste. Merge the iopf_device_param pointer into the iommu_fault_param pointer to consolidate the code and save memory. The consolidated pointer would be allocated on demand when the device driver enables the iopf on device, and would be freed after iopf is disabled. Signed-off-by: Lu Baolu Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Tested-by: Yan Zhao --- include/linux/iommu.h | 18 ++++-- drivers/iommu/io-pgfault.c | 113 ++++++++++++++++++------------------- drivers/iommu/iommu.c | 34 ++--------- 3 files changed, 75 insertions(+), 90 deletions(-) diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 79775859af42..108ab50da1ad 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -42,6 +42,7 @@ struct notifier_block; struct iommu_sva; struct iommu_fault_event; struct iommu_dma_cookie; +struct iopf_queue; =20 #define IOMMU_FAULT_PERM_READ (1 << 0) /* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1) /* write */ @@ -590,21 +591,31 @@ struct iommu_fault_event { * struct iommu_fault_param - per-device IOMMU fault data * @handler: Callback function to handle IOMMU faults at device level * @data: handler private data - * @faults: holds the pending faults which needs response * @lock: protect pending faults list + * @dev: the device that owns this param + * @queue: IOPF queue + * @queue_list: index into queue->devices + * @partial: faults that are part of a Page Request Group for which the la= st + * request hasn't been submitted yet. + * @faults: holds the pending faults which needs response */ struct iommu_fault_param { iommu_dev_fault_handler_t handler; void *data; + struct mutex lock; + + struct device *dev; + struct iopf_queue *queue; + struct list_head queue_list; + + struct list_head partial; struct list_head faults; - struct mutex lock; }; =20 /** * struct dev_iommu - Collection of per-device IOMMU data * * @fault_param: IOMMU detected device fault reporting data - * @iopf_param: I/O Page Fault queue and data * @fwspec: IOMMU fwspec data * @iommu_dev: IOMMU device this device is linked to * @priv: IOMMU Driver private data @@ -620,7 +631,6 @@ struct iommu_fault_param { struct dev_iommu { struct mutex lock; struct iommu_fault_param *fault_param; - struct iopf_device_param *iopf_param; struct iommu_fwspec *fwspec; struct iommu_device *iommu_dev; void *priv; diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index 24b5545352ae..b1cf28055525 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -25,21 +25,6 @@ struct iopf_queue { struct mutex lock; }; =20 -/** - * struct iopf_device_param - IO Page Fault data attached to a device - * @dev: the device that owns this param - * @queue: IOPF queue - * @queue_list: index into queue->devices - * @partial: faults that are part of a Page Request Group for which the la= st - * request hasn't been submitted yet. - */ -struct iopf_device_param { - struct device *dev; - struct iopf_queue *queue; - struct list_head queue_list; - struct list_head partial; -}; - struct iopf_fault { struct iommu_fault fault; struct list_head list; @@ -144,7 +129,7 @@ int iommu_queue_iopf(struct iommu_fault *fault, void *c= ookie) int ret; struct iopf_group *group; struct iopf_fault *iopf, *next; - struct iopf_device_param *iopf_param; + struct iommu_fault_param *iopf_param; =20 struct device *dev =3D cookie; struct dev_iommu *param =3D dev->iommu; @@ -159,7 +144,7 @@ int iommu_queue_iopf(struct iommu_fault *fault, void *c= ookie) * As long as we're holding param->lock, the queue can't be unlinked * from the device and therefore cannot disappear. */ - iopf_param =3D param->iopf_param; + iopf_param =3D param->fault_param; if (!iopf_param) return -ENODEV; =20 @@ -229,14 +214,14 @@ EXPORT_SYMBOL_GPL(iommu_queue_iopf); int iopf_queue_flush_dev(struct device *dev) { int ret =3D 0; - struct iopf_device_param *iopf_param; + struct iommu_fault_param *iopf_param; struct dev_iommu *param =3D dev->iommu; =20 if (!param) return -ENODEV; =20 mutex_lock(¶m->lock); - iopf_param =3D param->iopf_param; + iopf_param =3D param->fault_param; if (iopf_param) flush_workqueue(iopf_param->queue->wq); else @@ -260,7 +245,7 @@ EXPORT_SYMBOL_GPL(iopf_queue_flush_dev); int iopf_queue_discard_partial(struct iopf_queue *queue) { struct iopf_fault *iopf, *next; - struct iopf_device_param *iopf_param; + struct iommu_fault_param *iopf_param; =20 if (!queue) return -EINVAL; @@ -287,34 +272,38 @@ EXPORT_SYMBOL_GPL(iopf_queue_discard_partial); */ int iopf_queue_add_device(struct iopf_queue *queue, struct device *dev) { - int ret =3D -EBUSY; - struct iopf_device_param *iopf_param; + int ret =3D 0; struct dev_iommu *param =3D dev->iommu; - - if (!param) - return -ENODEV; - - iopf_param =3D kzalloc(sizeof(*iopf_param), GFP_KERNEL); - if (!iopf_param) - return -ENOMEM; - - INIT_LIST_HEAD(&iopf_param->partial); - iopf_param->queue =3D queue; - iopf_param->dev =3D dev; + struct iommu_fault_param *fault_param; =20 mutex_lock(&queue->lock); mutex_lock(¶m->lock); - if (!param->iopf_param) { - list_add(&iopf_param->queue_list, &queue->devices); - param->iopf_param =3D iopf_param; - ret =3D 0; + if (param->fault_param) { + ret =3D -EBUSY; + goto done_unlock; } + + get_device(dev); + fault_param =3D kzalloc(sizeof(*fault_param), GFP_KERNEL); + if (!fault_param) { + put_device(dev); + ret =3D -ENOMEM; + goto done_unlock; + } + + mutex_init(&fault_param->lock); + INIT_LIST_HEAD(&fault_param->faults); + INIT_LIST_HEAD(&fault_param->partial); + fault_param->dev =3D dev; + list_add(&fault_param->queue_list, &queue->devices); + fault_param->queue =3D queue; + + param->fault_param =3D fault_param; + +done_unlock: mutex_unlock(¶m->lock); mutex_unlock(&queue->lock); =20 - if (ret) - kfree(iopf_param); - return ret; } EXPORT_SYMBOL_GPL(iopf_queue_add_device); @@ -330,34 +319,42 @@ EXPORT_SYMBOL_GPL(iopf_queue_add_device); */ int iopf_queue_remove_device(struct iopf_queue *queue, struct device *dev) { - int ret =3D -EINVAL; + int ret =3D 0; struct iopf_fault *iopf, *next; - struct iopf_device_param *iopf_param; struct dev_iommu *param =3D dev->iommu; - - if (!param || !queue) - return -EINVAL; + struct iommu_fault_param *fault_param =3D param->fault_param; =20 mutex_lock(&queue->lock); mutex_lock(¶m->lock); - iopf_param =3D param->iopf_param; - if (iopf_param && iopf_param->queue =3D=3D queue) { - list_del(&iopf_param->queue_list); - param->iopf_param =3D NULL; - ret =3D 0; + if (!fault_param) { + ret =3D -ENODEV; + goto unlock; } - mutex_unlock(¶m->lock); - mutex_unlock(&queue->lock); - if (ret) - return ret; + + if (fault_param->queue !=3D queue) { + ret =3D -EINVAL; + goto unlock; + } + + if (!list_empty(&fault_param->faults)) { + ret =3D -EBUSY; + goto unlock; + } + + list_del(&fault_param->queue_list); =20 /* Just in case some faults are still stuck */ - list_for_each_entry_safe(iopf, next, &iopf_param->partial, list) + list_for_each_entry_safe(iopf, next, &fault_param->partial, list) kfree(iopf); =20 - kfree(iopf_param); + param->fault_param =3D NULL; + kfree(fault_param); + put_device(dev); +unlock: + mutex_unlock(¶m->lock); + mutex_unlock(&queue->lock); =20 - return 0; + return ret; } EXPORT_SYMBOL_GPL(iopf_queue_remove_device); =20 @@ -403,7 +400,7 @@ EXPORT_SYMBOL_GPL(iopf_queue_alloc); */ void iopf_queue_free(struct iopf_queue *queue) { - struct iopf_device_param *iopf_param, *next; + struct iommu_fault_param *iopf_param, *next; =20 if (!queue) return; diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index f24513e2b025..9c9eacfa6761 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -1326,27 +1326,18 @@ int iommu_register_device_fault_handler(struct devi= ce *dev, struct dev_iommu *param =3D dev->iommu; int ret =3D 0; =20 - if (!param) + if (!param || !param->fault_param) return -EINVAL; =20 mutex_lock(¶m->lock); /* Only allow one fault handler registered for each device */ - if (param->fault_param) { + if (param->fault_param->handler) { ret =3D -EBUSY; goto done_unlock; } =20 - get_device(dev); - param->fault_param =3D kzalloc(sizeof(*param->fault_param), GFP_KERNEL); - if (!param->fault_param) { - put_device(dev); - ret =3D -ENOMEM; - goto done_unlock; - } param->fault_param->handler =3D handler; param->fault_param->data =3D data; - mutex_init(¶m->fault_param->lock); - INIT_LIST_HEAD(¶m->fault_param->faults); =20 done_unlock: mutex_unlock(¶m->lock); @@ -1367,29 +1358,16 @@ EXPORT_SYMBOL_GPL(iommu_register_device_fault_handl= er); int iommu_unregister_device_fault_handler(struct device *dev) { struct dev_iommu *param =3D dev->iommu; - int ret =3D 0; =20 - if (!param) + if (!param || !param->fault_param) return -EINVAL; =20 mutex_lock(¶m->lock); - - if (!param->fault_param) - goto unlock; - - /* we cannot unregister handler if there are pending faults */ - if (!list_empty(¶m->fault_param->faults)) { - ret =3D -EBUSY; - goto unlock; - } - - kfree(param->fault_param); - param->fault_param =3D NULL; - put_device(dev); -unlock: + param->fault_param->handler =3D NULL; + param->fault_param->data =3D NULL; mutex_unlock(¶m->lock); =20 - return ret; + return 0; } EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler); =20 --=20 2.34.1