From patchwork Tue Mar 21 19:14:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13183104 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 897D1C74A5B for ; Tue, 21 Mar 2023 19:15:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230322AbjCUTPY (ORCPT ); Tue, 21 Mar 2023 15:15:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbjCUTPU (ORCPT ); Tue, 21 Mar 2023 15:15:20 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2068.outbound.protection.outlook.com [40.107.96.68]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02205231FD; Tue, 21 Mar 2023 12:15:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RvVrTFWOvnvnbQdSse6isGdUT/gjS/AvsAoyj4an2WNkp7dEKHayD27Wv5rL302Ftcs/ygUnZSY45bSopn+vfqJvlAEB7GwcD9OGIBAuxhlrRWmHcKFr72pZ8F5Mb3f+35Sby3QQvztQeox6/m9t+qac3rgEEfQhCHjNZXTYLDwlROyeqA4lpdZIi3oygJQeuWj3rBvDKXJGnCryNzfx7sxWi4gtCz0pwrFmpbUtitn+qJ0FB7uUtWlE4ZL56Yw/kfEogcT3pfWcAJzGujkSWfRpPRaTy1jCNSCVeAgLua4+XnL+EliJ9eoYTHf8pCdJ3Ii9qNg1HlUAH/dF+97KpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=RFhH+VMaXLDWwRmCU+TbUC9YfLiad4RolEiHONxioxs=; b=BaBnNn9yu7cCu3NnVF+FoPsqslROTSgGD+pg0sgj7L9rIkHboG+E3hofroTrAwYloa4P+zLPtx/Ggc4W+NcYP0sD6H/ybx9FPdamZIVcf7+UzdlxujkbCybzRWZ2LpBma3a9C87o0JAQpHOnDKwIwICDGEfCyVBSLeKXCl10LIfb24p1LwYN6WQaAvlNhiw9T1D/4QfTgd0YyfKWR+xrabmF/OFxPwqMe/rKTN/rjaw9JVNeLIuzssi5K17eK4HD6qTz8GxXqh+iKdYMan8uSLg+BYKsAE3m5L1jzERRiplWCF4Ezhm/TXzER5U/fiUbUJMkLB5YhW89haAfgxn7Nw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=RFhH+VMaXLDWwRmCU+TbUC9YfLiad4RolEiHONxioxs=; b=LBOjfbeZv8WC0BAHjTI8l2ImGOgzZ24MCN1V1BW9XIOsRA8D03wOGitO3N9cKB+W+fl639NTTxcygc2/YIJkTLmW9IpJeXHSPEbG/R0LYHuntn29xuTsrYdn9qNkoFoGRP/+DD1urucJVElzwGqk5mpqWvUVIBVPeQn9YcOpzBlPJ7UhtGWYApzn0CPyvSldvZDuVoCTMBqN31mM+/L7pkeaEMbMx/KCTvspC7X7GYusd5GduNFV+8xo/UgGYs2tABn44tKVZ+wZPRET/8YhZtfiJEJs30GN14ZrXFQetjGEUqim18wJkM9qZkk0Iyu7d3eX4IeCXwGAE+u6WlLnqA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA1PR12MB6604.namprd12.prod.outlook.com (2603:10b6:208:3a0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.37; Tue, 21 Mar 2023 19:14:54 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::ef6d:fdf6:352f:efd1]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::ef6d:fdf6:352f:efd1%3]) with mapi id 15.20.6178.037; Tue, 21 Mar 2023 19:14:54 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org Cc: Lu Baolu , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v3 11/17] iommu: Introduce a new iommu_group_replace_domain() API Date: Tue, 21 Mar 2023 16:14:45 -0300 Message-Id: <11-v3-61d41fd9e13e+1f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v3-61d41fd9e13e+1f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0312.namprd13.prod.outlook.com (2603:10b6:208:2c1::17) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB6604:EE_ X-MS-Office365-Filtering-Correlation-Id: 7cef3af9-0c38-498c-132c-08db2a408c8b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iU4f2o+HxfuP05o5u7aWL9BXwGgqJowoIqEYuMiCKLmRWYgf0WBpEDrpPkaQHEOXfrGy2TIZJo8/eKiNl4kqFxic05YJN5+pCkdjzTNk2jZkF/vkGmOwQ4aO1pq8glGoFgMwa0NlF9mi9+PK8aymeD5xhJmX5aH9npSc4nFOSTAp6r2IfltTPmC44plDuU1Z0Ql+e01y7Y7OFPYfsKwpRoBDQpPX8eXfzKd9faqSziaqdC2UEi7oTGsDtMhZKqpIYUpiO1IPAQE5+MvJ51TM4UCBphOrAJZS5FSDbB2bTBKShUptMOCBSw1VUlQSb2ztkNCTaJ/0WcLQ0Zvj1+fvxOx6GF0pOx7tk+jDAgSdcBa1WrRLHGz20esVRF1eUbFoSnw9d6WxnWD6CC0tcCepD3JW7hLj9GZ/PTr3r0Dne8svIFtSN4MjUIv9YVcylACNG64eBXQ0S5EifJy9tVhkGcVT8uPCY6x7eWKYalpByq6Z7Qg1zLY5Udgl1eniJQHly41rIX+mtSGJ+RmnykAKcSR7eVRq1yK9/9VjJef5+BUqIstNjeJv2nTp8IFuLNk+ZbTKb7q6w0P/qoRcFZmKYCif5iAB+Lvzv9IE41Y92P+YH6i9u6NijH6czz0ryUpranuiIph+ShYuGSZxKAg4sg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(396003)(136003)(376002)(346002)(366004)(39860400002)(451199018)(478600001)(86362001)(83380400001)(26005)(186003)(54906003)(316002)(6486002)(6666004)(6512007)(8676002)(2616005)(6506007)(38100700002)(2906002)(8936002)(66556008)(66476007)(66946007)(5660300002)(4326008)(41300700001)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YAqsebLx9kVBgZVP9WUx4cYEFc4GsOn9+cnJzpHPMEV9tmAwmeYj71/7Os5lzGvRoARcJeZY5NqDbvOJI+5aVI/ggi/jv/ZLyhTGm3iTnaWAcdYIkYEDBwNhb45B/jx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ms3VUVnT8qk9cE4tKKcQAKwyPd8pqYfyN1162UKVJoG4i+NYrblrUyRLgzuCeozhxqzuHjBGiL9tEiT7MySb1tvQzx1d8YwXaN3SXnpiPFSOLnSb67HU+QIBrmGF9mgOP+yTK+vFIbVQr4mDP4cyQfSzdPSqCyiOqLUo/IQe4A3KAXsmdVbDGCeoLSU3AJtREyYXJnOgBNHDrKRMnklhvnQApc0/A7PvM0D/ZGAwYsh1niNK4Z7JD3dSW3MeTPcrcKBwoCgV56qxZES/PXrqawt7Ok8yVHN1LM+RuO9IK5tMX6NXA3bzVVEU0edJvSuZZuwl6OYrDaZqzFchojF+Q/8QxH9/6n1vyyq3Lg5sg2XsayinTGj81YPjGrWnf6aR X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7cef3af9-0c38-498c-132c-08db2a408c8b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Mar 2023 19:14:52.9630 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: WVCLEHcTQgTLYmOpWiMVAvO/ciDkMgCSBHTUkoetjylOS6RC2SroJNnFnbvzijDh X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6604 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Nicolin Chen qemu has a need to replace the translations associated with a domain when the guest does large-scale operations like switching between an IDENTITY domain and, say, dma-iommu.c. Currently, it does this by replacing all the mappings in a single domain, but this is very inefficient and means that domains have to be per-device rather than per-translation. Provide a high-level API to allow replacements of one domain with another. This is similar to a detach/attach cycle except it doesn't force the group to go to the blocking domain in-between. By removing this forced blocking domain the iommu driver has the opportunity to implement a non-disruptive replacement of the domain to the greatest extent its hardware allows. This allows the qemu emulation of the vIOMMU to be more complete, as real hardware often has a non-distruptive replacement capability. It could be possible to address this by simply removing the protection from the iommu_attach_group(), but it is not so clear if that is safe for the few users. Thus, add a new API to serve this new purpose. All drivers are already required to support changing between active UNMANAGED domains when using their attach_dev ops. This API is expected to be used only by IOMMUFD, so add to the iommu-priv header and mark it as IOMMUFD_INTERNAL. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu-priv.h | 10 ++++++++++ drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 drivers/iommu/iommu-priv.h diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h new file mode 100644 index 00000000000000..7c8011bfd15374 --- /dev/null +++ b/drivers/iommu/iommu-priv.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_IOMMU_PRIV_H +#define __LINUX_IOMMU_PRIV_H + +#include + +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain); + +#endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 76969904b93af4..91a948a76db5ee 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -33,6 +33,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2191,6 +2192,35 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) } EXPORT_SYMBOL_GPL(iommu_attach_group); +/** + * iommu_group_replace_domain - replace the domain that a group is attached to + * @new_domain: new IOMMU domain to replace with + * @group: IOMMU group that will be attached to the new domain + * + * This API allows the group to switch domains without being forced to go to + * the blocking domain in-between. + * + * If the currently attached domain is a core domain (e.g. a default_domain), + * it will act just like the iommu_attach_group(). + */ +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain) +{ + int ret; + + if (!new_domain) + return -EINVAL; + + mutex_lock(&group->mutex); + ret = __iommu_group_set_domain(group, new_domain); + if (ret) + __iommu_group_for_each_dev(group, group->domain, + iommu_group_do_attach_device); + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL); + static int iommu_group_do_set_platform_dma(struct device *dev, void *data) { const struct iommu_ops *ops = dev_iommu_ops(dev);