From patchwork Sat Dec 2 09:21:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yan Zhao X-Patchwork-Id: 172771 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp1674665vqy; Sat, 2 Dec 2023 01:50:58 -0800 (PST) X-Google-Smtp-Source: AGHT+IF535ztZqXMbMUVGCChwZ85CfYhx1OtWcHQ1YtwOj3A5XrprLIQYM/8yBl7KYvc1KnLIW7b X-Received: by 2002:a05:6a21:3391:b0:18d:9ac:7964 with SMTP id yy17-20020a056a21339100b0018d09ac7964mr1077695pzb.26.1701510657827; Sat, 02 Dec 2023 01:50:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701510657; cv=none; d=google.com; s=arc-20160816; b=M4gHnksdR145USEDkrjItmVua6gat+5dverzu6sPK6O824mSNAT5j9UPolOv7yGgxr 9H6LrctggUKjD/5qGKdrnXlk4A4HHFIMNP6kcLAuTIrZAdKqxLbA6dedT7HUj7M8hgjd I2yEhO+Pp2FxKAwU2kEnqephnutNRR7rLyaE/zaysA+PQaME2ZMoGAtUo6UoxmQ1LvSW FwlEc32LRsx9r93fhLcAMh056g2SX9+JKKR8dpoOWtDxorEqFcvt2CYDxDYkK/8v6mFS k9Aoxqp5uyi6moD8MANBoXOcw3mRAyaWhiEPMSI7pHYPVcWeyK9RFYnf9unnYKz3x/Y+ 15Xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=vcMA8ACn9L7oxDdq086Q4kFzyjfXgapqGH7IaQdpZNI=; fh=+WI4m5k3dRLR+dR3neThuZkNBTzIm/a8HgtddERL9fA=; b=0B27i+MurOCr3h5gf/sJ215zMPfDw3CIHI/hLUZaR4QkXq4RsWLYJzrGTLhvFEHDA6 LAV/PXKHtVoUvZwpMmoUlizGgE9P7KPgXG3HN1ONq0wnUphuB5jOJQUJbtZQX+X46KQ1 ImrHjgjuLtXYPvofdsM1JwBa54yIBbSlamlZRi7+dzJkDaSb0ycDJtE4YdqBCAm00x9/ whHlfG5yoKeB2jjoSP+PW7cUDj7R/KTOQBKj0+K0TQbQcEKxCLcRbJ+YxAEunzmYD3gA qaMyg9n18f52Z+p0hDXKkTggoSY/TNIvaPpy7mhO1BKhs5nOeYqqgKqRZquymWHRq7FB 4/Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="IEVmV/1g"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id r5-20020a632045000000b005bd0f2c2671si4876268pgm.626.2023.12.02.01.50.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 01:50:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="IEVmV/1g"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id BC47E80EA785; Sat, 2 Dec 2023 01:50:53 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232310AbjLBJuo (ORCPT + 99 others); Sat, 2 Dec 2023 04:50:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232405AbjLBJuk (ORCPT ); Sat, 2 Dec 2023 04:50:40 -0500 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 236C81A4; Sat, 2 Dec 2023 01:50:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1701510647; x=1733046647; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=rqH35GdOrKoZEkvGITnBvH+ikDglRlVlt01Gqn8iCuQ=; b=IEVmV/1gdxf356DBwO04fRRnMuNVHGI2FFm259u7fyHoV3w9F0Xt5KLg IlVhnvkJsXcDPS60fWmJLQ/OU2eTm3JusESpxxuII8RJ/LePJCqXHqVGH /p9eFpDJdnDciK/N8xGG+GEANlJL6bYQ+D/6GVUtn2pHWe/iODzI8Pg98 lBtNTlGNr2FedY/wpwWIqKx35TRi1AOJC6K896JJlc1Q2huCE7NT950Sc r5hahkRLUP+OzWlBaWDEaGikURFzq/2uhtx1NzhHbdBQ34+tUHiapl7+9 5gDvK8sJKvIZUpVJiLLH3QEUmCVVDGcej+r9yjK+YQ27wu3mSHDpsyk1X w==; X-IronPort-AV: E=McAfee;i="6600,9927,10911"; a="479794143" X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="479794143" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:50:46 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.04,245,1695711600"; d="scan'208";a="11414277" Received: from yzhao56-desk.sh.intel.com ([10.239.159.62]) by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2023 01:50:43 -0800 From: Yan Zhao To: iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: alex.williamson@redhat.com, jgg@nvidia.com, pbonzini@redhat.com, seanjc@google.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, dwmw2@infradead.org, yi.l.liu@intel.com, Yan Zhao Subject: [RFC PATCH 13/42] iommufd: Add a KVM HW pagetable object Date: Sat, 2 Dec 2023 17:21:47 +0800 Message-Id: <20231202092147.14208-1-yan.y.zhao@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231202091211.13376-1-yan.y.zhao@intel.com> References: <20231202091211.13376-1-yan.y.zhao@intel.com> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,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 fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Sat, 02 Dec 2023 01:50:54 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784163239365292166 X-GMAIL-MSGID: 1784163239365292166 Add new obj type IOMMUFD_OBJ_HWPT_KVM for KVM HW page tables, which correspond to iommu stage 2 domains whose paging strcutures and mappings are managed by KVM. Extend the IOMMU_HWPT_ALLOC ioctl to accept KVM HW page table specific data of "struct iommu_hwpt_kvm_info". The real allocator iommufd_hwpt_kvm_alloc() is now an empty function and will be implemented in next patch when config IOMMUFD_KVM_HWPT is on. Signed-off-by: Yan Zhao --- drivers/iommu/iommufd/device.c | 13 +++++---- drivers/iommu/iommufd/hw_pagetable.c | 29 +++++++++++++++++++- drivers/iommu/iommufd/iommufd_private.h | 35 +++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 4 +++ 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 59d3a07300d93..83af6b7e2784b 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -629,7 +629,8 @@ static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id, switch (pt_obj->type) { case IOMMUFD_OBJ_HWPT_NESTED: - case IOMMUFD_OBJ_HWPT_PAGING: { + case IOMMUFD_OBJ_HWPT_PAGING: + case IOMMUFD_OBJ_HWPT_KVM: { struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); @@ -667,8 +668,9 @@ static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id, /** * iommufd_device_attach - Connect a device to an iommu_domain * @idev: device to attach - * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING - * Output the IOMMUFD_OBJ_HWPT_PAGING ID + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING, or + * IOMMUFD_OBJ_HWPT_KVM + * Output the IOMMUFD_OBJ_HWPT_PAGING ID or IOMMUFD_OBJ_HWPT_KVM ID * * This connects the device to an iommu_domain, either automatically or manually * selected. Once this completes the device could do DMA. @@ -696,8 +698,9 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); /** * iommufd_device_replace - Change the device's iommu_domain * @idev: device to change - * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING - * Output the IOMMUFD_OBJ_HWPT_PAGING ID + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HWPT_PAGING, or + * IOMMUFD_OBJ_HWPT_KVM + * Output the IOMMUFD_OBJ_HWPT_PAGING ID or IOMMUFD_OBJ_HWPT_KVM ID * * This is the same as:: * diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 367459d92f696..c8430ec42cdf8 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -273,6 +273,31 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) if (IS_ERR(idev)) return PTR_ERR(idev); + if (cmd->data_type == IOMMU_HWPT_DATA_KVM) { + struct iommu_hwpt_kvm_info kvm_data; + struct iommufd_hwpt_kvm *hwpt_kvm; + + if (!cmd->data_len || cmd->data_len != sizeof(kvm_data) || + !cmd->data_uptr) { + rc = -EINVAL; + goto out_put_idev; + } + rc = copy_struct_from_user(&kvm_data, sizeof(kvm_data), + u64_to_user_ptr(cmd->data_uptr), + cmd->data_len); + if (rc) + goto out_put_idev; + + hwpt_kvm = iommufd_hwpt_kvm_alloc(ucmd->ictx, idev, cmd->flags, + &kvm_data); + if (IS_ERR(hwpt_kvm)) { + rc = PTR_ERR(hwpt_kvm); + goto out_put_idev; + } + hwpt = &hwpt_kvm->common; + goto out_respond; + } + pt_obj = iommufd_get_object(ucmd->ictx, cmd->pt_id, IOMMUFD_OBJ_ANY); if (IS_ERR(pt_obj)) { rc = -EINVAL; @@ -310,6 +335,7 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) goto out_put_pt; } +out_respond: cmd->out_hwpt_id = hwpt->obj.id; rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); if (rc) @@ -323,7 +349,8 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) if (ioas) mutex_unlock(&ioas->mutex); out_put_pt: - iommufd_put_object(pt_obj); + if (cmd->data_type != IOMMU_HWPT_DATA_KVM) + iommufd_put_object(pt_obj); out_put_idev: iommufd_put_object(&idev->obj); return rc; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 160521800d9b4..a46a6e3e537f9 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -125,6 +125,7 @@ enum iommufd_object_type { IOMMUFD_OBJ_DEVICE, IOMMUFD_OBJ_HWPT_PAGING, IOMMUFD_OBJ_HWPT_NESTED, + IOMMUFD_OBJ_HWPT_KVM, IOMMUFD_OBJ_IOAS, IOMMUFD_OBJ_ACCESS, #ifdef CONFIG_IOMMUFD_TEST @@ -266,17 +267,33 @@ struct iommufd_hwpt_nested { struct iommufd_hwpt_paging *parent; }; +struct iommufd_hwpt_kvm { + struct iommufd_hw_pagetable common; + void *context; +}; + static inline bool hwpt_is_paging(struct iommufd_hw_pagetable *hwpt) { return hwpt->obj.type == IOMMUFD_OBJ_HWPT_PAGING; } +static inline bool hwpt_is_kvm(struct iommufd_hw_pagetable *hwpt) +{ + return hwpt->obj.type == IOMMUFD_OBJ_HWPT_KVM; +} + static inline struct iommufd_hwpt_paging * to_hwpt_paging(struct iommufd_hw_pagetable *hwpt) { return container_of(hwpt, struct iommufd_hwpt_paging, common); } +static inline struct iommufd_hwpt_kvm * +to_hwpt_kvm(struct iommufd_hw_pagetable *hwpt) +{ + return container_of(hwpt, struct iommufd_hwpt_kvm, common); +} + static inline struct iommufd_hwpt_paging * iommufd_get_hwpt_paging(struct iommufd_ucmd *ucmd, u32 id) { @@ -413,4 +430,22 @@ static inline bool iommufd_selftest_is_mock_dev(struct device *dev) return false; } #endif + +struct iommu_hwpt_kvm_info; +static inline struct iommufd_hwpt_kvm * +iommufd_hwpt_kvm_alloc(struct iommufd_ctx *ictx, + struct iommufd_device *idev, u32 flags, + const struct iommu_hwpt_kvm_info *kvm_data) +{ + return ERR_PTR(-EOPNOTSUPP); +} + +static inline void iommufd_hwpt_kvm_abort(struct iommufd_object *obj) +{ +} + +static inline void iommufd_hwpt_kvm_destroy(struct iommufd_object *obj) +{ +} + #endif diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 6edef860f91cc..0798c1279133f 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -499,6 +499,10 @@ static const struct iommufd_object_ops iommufd_object_ops[] = { .destroy = iommufd_hwpt_nested_destroy, .abort = iommufd_hwpt_nested_abort, }, + [IOMMUFD_OBJ_HWPT_KVM] = { + .destroy = iommufd_hwpt_kvm_destroy, + .abort = iommufd_hwpt_kvm_abort, + }, #ifdef CONFIG_IOMMUFD_TEST [IOMMUFD_OBJ_SELFTEST] = { .destroy = iommufd_selftest_destroy,