From patchwork Mon Mar 27 18:37:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668437 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 85690C76195 for ; Mon, 27 Mar 2023 18:40:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232626AbjC0SkD (ORCPT ); Mon, 27 Mar 2023 14:40:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232565AbjC0SkC (ORCPT ); Mon, 27 Mar 2023 14:40:02 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2047.outbound.protection.outlook.com [40.107.95.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 993DF448E; Mon, 27 Mar 2023 11:39:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XTmcAfhOmKcpfrL2A/fU/VbdBBuXsiMTiFK0T3j8H70YsRCxEf/Ix/IDP0rQrU2trTfY560LF72+6qEnDLwhcYmLp9jzAkWc8LUOPHQHjJkZ01ZpOL5noHN6KSBc1cMC0H4u/QVea5WxQTYmnwCzoj8eRKc9tJiZvbghBN5xZmTjVZNUIBSYBx+i1fSF0SY7BcCJPkcTc8lUL5NEk+nhe+iFdXHHUPzTmNTUtdAL9WdDRQ4A9fwDjQVrqacXdAs3El9c4yMjZDioHb5O++ZYANBidcH8nOY5y9D/l6je0iAizY1qGP0KSa8gvCKRgLHmlQd4i4f0AR5k5QLKfFS/Rw== 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=3blGAHefJ4aP70tNrJtt6rGHVG5EmtnNNBaQcgWD0rI=; b=ZPMG6c2CDZf7ZvANVOUPuZU0E97Aray4wMcGaOlNCPVEvrU+8lKZglvw3sN2WZO66zHaGNYR0FtUHRAeh6jas5nOWFqiJ1D7WgOeBEax18uMizhSvu5TRAkeN/m6+J4t72WUij9bVh6MyiWHVR0xoBFm3WF2fAT4AUkXDDKTVKQNlM9Qpbs6tqnqOlblkQA6hBxsYoWrurRjCyLk2+l0xeOA4yInTZnTU3cMQ6woJxpC7yFGLM4n8IH7fjEJmI9ESftMcogtOV72mVCfBkbKzmK/re53TZ2qbvktfNk2ox6EoD4sX8rRwg+kVz18otCCMWval6HsElmFfrYl8fXpTg== 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=3blGAHefJ4aP70tNrJtt6rGHVG5EmtnNNBaQcgWD0rI=; b=TryaW87/qaUPKkyyRohuWBmUhAPSXeysCP9BUiSAxnBjEMSBxcPZo1ZECgV0Qi+kzA3fRgOs8h0dr5U/oIz2fnY9Pf4L0b0WxzRSQv3T+fDqbB3SlANBgzBtUMwFjTLdL+TlpTMn2nxBf6YMw2W7Q9ePmBdviwUPALvMZTQJQ9dh7vqMbQL8xdKiZRohOHfkjSquhThxJ9gGlmrJMsmtyw5pZV9ENpSBK5IVCVqbNoX6y6U93b4jri8qFuzxZJeOsm0zPK+GDpjz8XcS3rRSrkYyPSqNx07ZmXyi78uyfRdYqsPWm1KHYqfuIosH/kOrBefJwtkK+zGVI3U+KY7bRA== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:24 +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; Mon, 27 Mar 2023 18:38:24 +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 v4 01/17] iommufd: Move isolated msi enforcement to iommufd_device_bind() Date: Mon, 27 Mar 2023 15:37:55 -0300 Message-Id: <1-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0191.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:110::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 86ccb694-c313-4145-c1b6-08db2ef26cd3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jOsbJ058fndHmAMZA/JMhAtOjTezwsODTr+L+6TJFTX9l2LitLiSDlFbvcRfNZXvgNjfMPtbcL2rg7n4Keyu5j735SNk90TlpPcP58XgDA3+I66IxQ0f6SCGKpMOd8Yj/ixAporRGzzTZzXHLlmv84TA2eUzSR0xVQvoe+6PVqM5LRUZeSwt+jLO8M/OwGnSYkSV8Ocyo6ndP3B0EG343v0yL1aQNyJLMmFuNEAVttDq7jabuG+V27MxnZ5nTgxoYL5EDEoCSfHY37B5GWhB8O8+hqGrv+Hlua1N+OPYkN2af9AWJl/ci5iGJ437+iPDrokr1klUNh27zSLw3nprx0jFGAKbbY+RWgw64LB10KPG0WNMQ4Uzs/gVMSNB+FaWnDn+8/S1pc1ga3CgN8fygxp+cr6FQtyQLU8N4kujDMcRqOQUKom1tMYSUbi0XiZQiMqguuX0pLSrdhz6+8ljUCyQdMJkncsF4a/WwYd8Qkc641ZxWPvrq1QuqixvEuZTYiG9zfBISXLnQm+9u7l0ROQ3pvyrT61ADUolI8ZkuoJm/pHzAWBfat7TroCJfEM4NsBCQRT+E6/3tzSdxObOHgR+ZkWkUEY2t40ArJMEemo= 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7z7fDKH8DJdrZt7CoW5mvQd7NhdnVt6LCQL+ktvkBQNqIHFF4gWQwfe0BqYrpGP62MoTxXze1rFdghE8at8zKCm8Bl0ZRbfMYeio8wNQoCGtj7GxPFwS83vj4k/kVYhaGvpnsNVyjOB/+G4WFvzJWu2TEvnKpRJBzHxEeucg2MH8wx7L8c09hDIjDjPMeqNKJ4FyGVqZQfna77swG7lAfovogiIuaiISoYrn5baOYn4rSOFHKijFt7hhE9aLVoPQhXsl1CyHkHJpIE68IpVp4VmLtPAl/13LFG52jqic+NDtnBr8a+cx//LhhsJmoX87cyCIthmfGyvbFdZ2V1OBfWScyqCNlPWDY3NvsgibNaYfA4N+m9XnRL/tjnZdqy5WZMO3CjFHlf/aOZNNEUs2upw4VY+fl5eVvAPFJH9RgYo54mQbol5sCuklV3SegdZldgetQprb6scldGzudg/Y7UrkO7DDL9v7bbGBbvKq17mf0T40K3KkwDlPe70szKZMfcVP8gT97rfPYw8wsKSn52lnC+wYsW7ERdhYPv0kztl2lp/yaoVykeMP6Aci60Yx5NrWGPWd352psnfXTaat6wPiMLPlE9oQcAVaHGcu+UyVzM+TqpXSq9EHnKmDe3MxLlnGH4IDE3+IJbtm1Lgskf4kxOTC+MUi7BeGBBMMD7af5KOZMEdRXtJsikQLGlDJaaU6xL8Mm35IjwmvXCZ9nv9+K0FygcS4TKRfzstoJ+NYjgiE9RzrfllacmRZfE6AZyHYfnVCnTsOQ25/0rWU6g8tGlpc0aVVi/xyfkBksYtADTQq5thYNS+Bj4Dt/WWi/A54nnu8HjtrGw8fSW6UYhrYsOc08TUvbXPITKFNEUimTnvZ/l+X/SXmI82chZXMhAjIyRGzx96bg9QoG9kuJMnuxH88JitC5zbuUbvwtnItRbued/2n4UXGpQcHsJS9Qo68okGZ5gQGnHxDeg1rsfk2PGR7ciPJ4deSrEzKwtz2/K23WU8kGf8PJ7otsErvJrCj8OQzipjuvxC3JCRBjPgt0eNDvH0vmOl4Bpv98J8bu1nagYS4jps2wMIPS4TK99oHXN2WLbgM8ASLaFJGwAqdHYz5f02GQh34Cy6Yb5cFueAbOX70byD+rtjhOuvjl3LwWazLVxfileOA8fsyd1WrHbnAOwcRU8/Y3Kmv3K2QyFgIUrdiB7uUzsfRDlgkdi9Pwlk+SKFb9+/noFfPQZperPYVeAVYHwScXP8YugP1yu1p661NwwY8xqkwkFcGIrPmkK5IMd7LsnuJrpJUlbiOWCQ55WDUEW+IRmPjjbakUjTKyvXeW+iiHvJWhjjla8hQOYxyGwu4aNfyQZ4KiOkByJTP08WWsVHP62VyE18FTSt/zxnvOZh+FlBNy6Zs2i/4CRkURKW4/Hu5Lr6Rsfi6ntp4kQ6KwwUZ7hheNWWHy+sAqWMEKJ18ucJCKkeW31JQSjByvR6B3QSwYSBQhIjtZhUBKFCnjV4/fpP6iiInzswjn9pjg/9c0BG5uDZBeesf0Dwh5+8Lrt8nLxGFiA2GLqsc0uUMILSfRTJfdB3umHoOgx3c6JiJ0gh6yw1t X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 86ccb694-c313-4145-c1b6-08db2ef26cd3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:14.8649 (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: ysptkFioZZ7/EaQOVmiVCtn9pIWJW2gnYInlscvt+iePLaZaBHTkgJEax+4ur3Ho X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org With the recent rework this no longer needs to be done at domain attachment time, we know if the device is usable by iommufd when we bind it. The value of msi_device_has_isolated_msi() is not allowed to change while a driver is bound. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 38 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index c6f4852a8a0c08..e66303c17c894c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -60,6 +60,26 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!group) return ERR_PTR(-ENODEV); + /* + * For historical compat with VFIO the insecure interrupt path is + * allowed if the module parameter is set. Secure/Isolated means that a + * MemWr operation from the device (eg a simple DMA) cannot trigger an + * interrupt outside this iommufd context. + */ + if (!iommufd_selftest_is_mock_dev(dev) && + !iommu_group_has_isolated_msi(group)) { + if (!allow_unsafe_interrupts) { + rc = -EPERM; + goto out_group_put; + } + + dev_warn( + dev, + "MSI interrupts are not secure, they cannot be isolated by the platform. " + "Check that platform features like interrupt remapping are enabled. " + "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); + } + rc = iommu_device_claim_dma_owner(dev, ictx); if (rc) goto out_group_put; @@ -146,24 +166,6 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, */ hwpt->msi_cookie = true; } - - /* - * For historical compat with VFIO the insecure interrupt path is - * allowed if the module parameter is set. Insecure means that a MemWr - * operation from the device (eg a simple DMA) cannot trigger an - * interrupt outside this iommufd context. - */ - if (!iommufd_selftest_is_mock_dev(idev->dev) && - !iommu_group_has_isolated_msi(idev->group)) { - if (!allow_unsafe_interrupts) - return -EPERM; - - dev_warn( - idev->dev, - "MSI interrupts are not secure, they cannot be isolated by the platform. " - "Check that platform features like interrupt remapping are enabled. " - "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); - } return 0; } From patchwork Mon Mar 27 18:37:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668444 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 30497C761A6 for ; Mon, 27 Mar 2023 18:38:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230339AbjC0Siv (ORCPT ); Mon, 27 Mar 2023 14:38:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232486AbjC0Sir (ORCPT ); Mon, 27 Mar 2023 14:38:47 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2047.outbound.protection.outlook.com [40.107.95.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7DAE448A; Mon, 27 Mar 2023 11:38:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XJzik+79MxRzQowuGLMBCUEP+yxjW1mlGEY9mWmDUdGLWGPUEqh04a6wsmGPdobIeoFugGi8lXAoHbFluP4vhsFcKtcEUfjptEUmA+kiVYGaYSpe6rsBsWGsUv4go3xqjzxEE/A1tQDkAsjkvPxZcNlvHXfeRPODwC1P71p13ohQRquOweEocTXOalKhB0j/X6M1jRqXwcRtVut18Mr7AenA9KssZeRZoLJyHyVfyUrDm9Qog0/8puQcZ6kby8FAbh8uNuiMgTiWky3lxzZDCiAiUBHvcNbHaHU7E7aI7OvVnrhFBXU5y+Xp/bvt9vW1KnjoHXcuVNvi+khUBgPukQ== 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=CZKnolzfT66UVjhCW5P9kzq/7vlXczbFBSK46RGNX3A=; b=bWvNfGN8yUXm97754uRrnaoaM7ZehKzRw1LX4WRP15qXPidEIk0tlpN/6ZbP00JPFCSSPxr9s5O3V1/3GQF9aTKW52/TLnyKCrcv/eV9WFle6TqlYFq9FPM7CTCAw5ZGAMhgPs2JSCR7v2E3Z/cVBWMPXFU30i/KxTp5nZ6V1CWizwqg1BT3POJPirmvAl54TBCOlUghImSCDZ9eLeCEoLxjxqMSy4EEXGW0+X+nyrCOqiFD8B7UbnQIgb1MzKdtvPgsurO8SLYWdgG1d0ocBKLb6tSrPmtv3vpCds2S4qgn+McqC7dBc2kN8mBnvrfGpJOFE36imy9RGobLrhS91A== 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=CZKnolzfT66UVjhCW5P9kzq/7vlXczbFBSK46RGNX3A=; b=bb/bkronb8bkV0yb+MaEFu0fOeGO+xmbtNTnlR2m1b76weEccimZJCrQF9CmIhTjqR5LQAG5mf0paSnCjIQY7eEzsLXeNEc9TmpBKUDrcYeZVznS3eGoJRPW27tH41MLdBZrfYmDeq4c0xmEJNDSBPChvfKFehPMnfppk20aUmq2z7GqjET3YyE0uUmx8jgBJsZZLCZJ6Ud2+uwIBieHdpfjrrbpW8/itTw3WlPdB9924Ew2GtmbXmWCSBJfY0XXbOke45R7YWRt8B6+RwsEgJesXQinR3k0NVhp6a+n9gS8D3MpDFwnibjKiOyvXRAc3SvKvbfQGcPNVR+ySakQ6g== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:14 +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; Mon, 27 Mar 2023 18:38:14 +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 v4 02/17] iommufd: Add iommufd_group Date: Mon, 27 Mar 2023 15:37:56 -0300 Message-Id: <2-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0459.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 7e574cd8-e3d0-45ce-3aa4-08db2ef26b70 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Gzy98e0GFPDZGgRSu+PTVDBHI8ceebWHGCHiSvHcx5RniTiDnYcuSolg5aBRTxgcrLq5oI+zJkTNWBMXNbD3IOv6R6R59MkyreIdH3Ab/9jghzHEUy3mfSghh1Il1uLkIaCCmcdOP8iHus4hKvb4OHj7nJuGCNWr2RNMAWQqHW8DSoQNvRysQvjBnN9goG/1lK6R50kDN7o36mMXexu/UqNqG36SxYb0TN4O46qwtHrGQ4Qfjx1j4IN9r3PO27VXDzd/FrSrXu6gHmlBUdlfxLtzX3V0wnz2el8/LFtZ0eaYVAy9xdVC+DdbHdfXEkOzCNkArAcFxDZCEyhpUMOmyOhPnta/sYUQm5lSxZwPW4Hqv+I+uviLtxbYz5h9Ro4UrcDwSb9fu3qTHj3FKzIGQzc5IagXGcXig1dHPQJKm0LFUtKSzT0d+F2xwjc9QhUBMPGqTEJJDtvRquE0Q1U4xj/2Ry0/m9B6X+0Ji1W+VbTlXrF+tskf8AY9NDkzzjbSCGvnoM2t2Hgf/dKsTFt4Hd5rRi74S1T2pfuH1jdOduGDAZDIzZ7au8bULQsdsZudh2zJfNyu4Fwn2RUWRepVG2/yTVRU/pQkMUIFJz20dec= 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: A0GMOh9FqE3CKJ+y/8B+UGL2UaGxbRGSAKlHzsHoadZ0HEbxqLL3M5S+Fk8c2IOutIQABcxApRm98dy/78XfMJ69HLE5+X74hMi40Cuu/lue67XnQJU7opDsQVu2edDgBKEb1TLFgguMr3xHWP+wlWymfBKfbphoZ7d0pee7FqbQeNinPc8iMfc/5CxlMlzrB1KEt7M5k7Dlj1AIVTBLjKNKcnJ/qrgstkKuHU7zX3hXRukn2B3jRe8k0dZ8FW2Fgs//jvVdnKZv/l6T4CcSDoVx8x0ytPVI89/LmiC6IpU/v6LkWi/WYtms5/rJnrCInUzm5ENMmlyu35hxYVY3hG4aq3AH2gz8HaoZwvqlMzDiZ3IUYYrqbD99G0bGizrii0vZgJNmF3Sccpi3kLEAY1OTekh04pI03dsWxobeWmEPIORuWW899UO/yhYGJ058CqTHAPIF68lUYqM6qePdphGU6Ruq+//6E5p2HYaOH/9zdj+q0sYLdYn3y37+DR37W62enzRqdEsEJqteLmbPjspByEZ07/Uq+h0qcPa/I3ziMo24ESbLKc3b4HkRvkdil5fKddMupU16cIuu9fMnZOppXaGdxXVe3NGHj3tUGoOKqiavRNR5ZjpCMgNYFhvnGOP3pr41c7uwb7SAOFissZc+CoI+PVtI6gDM/rwm6U60oOtDshegMAKnCteAK43sZ1BuhJkt7smll7TXAC+z8Q8Flwc6pLSM82Clt2fVQN/jCNvulG4dOO/N+ResoeeFjq/r9MHHVX2HyzhdIFdPCAo1BpTUvuYOrJXxz1A8mDxU1MW8NyGF71kPF74guvYPO+nPIMukpxtH0sMQwIgFOBXxJOTvRGcBrcm/uxxAA1easCaYB/tvcxv1vTwwjtAmlDRUjq3Jc3nOv5HuBebzMlGHC6IwgNSb3St0h7l9IY1aJkRrF7kA9GM/bqAh9m8or7PdHCiv+3k8AkBu4D53BT/obXx85/xCWzpUa13HyBMFAN3+CqYJ7crsyDICVP6Sj6Wp61HEHFM7TfL8PrZ/ET1RzNvdtjfHhkp6R/YlNvefZgFAjpYfk03vNc3/P6hZZ94TckfN/+subq7InompARAyKHdbe5MebSI7ERdjSp60nai1FR+z27yG5cRkvAbEg6wvfQoW6DlICAgNsxwtMWLmMVv8SQFuJ1u0XzjLHLK4MljAzweY+PLULFqSRyP5g9U5XDCjCgqZZZu61ADQPXu8Ly9sElZlY+ofZBO4LVW8YLnDfaUfDMpXVaiG1dO9c+2B/+d2n72rQxRnnSkks8ptNQTkU1Zi4Wo9jDHdT+rMesDi7TQp4+zDGZpZW3ZLHRPflDp+d1Z8nSuwtB8ek7GccGUjC9FOoKywo9Xq9uOOYuHtapPisv1gOHLnHSN13WY8xyKYCtF6dbT5CPYFVIRvavoCYQH46eV39daKE1oVMqRQeGkcgO7wqpfsP/6hneo0SNMhud8KOk5yCMNEHHghb+6yWlOIW/9IuFotnbGZ9EU9AnnTSplfDCv8nWhzPXEoejPR0yjHywMnn7asiJ13Fb8Gk/iQW1H7CbUKM1k/pLILHZ5uk4PA0rtsqqFK X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7e574cd8-e3d0-45ce-3aa4-08db2ef26b70 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:12.5505 (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: E+YurZomimIF1olYqs6gE6t4XpDI1mLobNnLOqYK/9imlQDOV8Hrh14zuESONaEd X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org When the hwpt to device attachment is fairly static we could get away with the simple approach of keeping track of the groups via a device list. But with replace this is infeasible. Add an automatically managed struct that is 1:1 with the iommu_group per-ictx so we can store the necessary tracking information there. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 141 +++++++++++++++++++++--- drivers/iommu/iommufd/iommufd_private.h | 9 +- drivers/iommu/iommufd/main.c | 2 + 3 files changed, 135 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index e66303c17c894c..3fd623208c691f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,13 +15,121 @@ MODULE_PARM_DESC( "Allow IOMMUFD to bind to devices even if the platform cannot isolate " "the MSI interrupt window. Enabling this is a security weakness."); +static void iommufd_group_release(struct kref *kref) +{ + struct iommufd_group *igroup = + container_of(kref, struct iommufd_group, ref); + + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, + NULL, GFP_KERNEL); + iommu_group_put(igroup->group); + kfree(igroup); +} + +static void iommufd_put_group(struct iommufd_group *group) +{ + kref_put(&group->ref, iommufd_group_release); +} + +static bool iommufd_group_try_get(struct iommufd_group *igroup, + struct iommu_group *group) +{ + if (!igroup) + return false; + /* + * group ID's cannot be re-used until the group is put back which does + * not happen if we could get an igroup pointer under the xa_lock. + */ + if (WARN_ON(igroup->group != group)) + return false; + return kref_get_unless_zero(&igroup->ref); +} + +/* + * iommufd needs to store some more data for each iommu_group, we keep a + * parallel xarray indexed by iommu_group id to hold this instead of putting it + * in the core structure. To keep things simple the iommufd_group memory is + * unique within the iommufd_ctx. This makes it easy to check there are no + * memory leaks. + */ +static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, + struct device *dev) +{ + struct iommufd_group *new_igroup; + struct iommufd_group *cur_igroup; + struct iommufd_group *igroup; + struct iommu_group *group; + unsigned int id; + + group = iommu_group_get(dev); + if (!group) + return ERR_PTR(-ENODEV); + + id = iommu_group_id(group); + + xa_lock(&ictx->groups); + igroup = xa_load(&ictx->groups, id); + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommu_group_put(group); + return igroup; + } + xa_unlock(&ictx->groups); + + new_igroup = kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) { + iommu_group_put(group); + return ERR_PTR(-ENOMEM); + } + + kref_init(&new_igroup->ref); + /* group reference moves into new_igroup */ + new_igroup->group = group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx = ictx; + + /* + * We dropped the lock so igroup is invalid. NULL is a safe and likely + * value to assume for the xa_cmpxchg algorithm. + */ + cur_igroup = NULL; + xa_lock(&ictx->groups); + while (true) { + igroup = __xa_cmpxchg(&ictx->groups, id, cur_igroup, new_igroup, + GFP_KERNEL); + if (xa_is_err(igroup)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return ERR_PTR(xa_err(igroup)); + } + + /* new_group was successfully installed */ + if (cur_igroup == igroup) { + xa_unlock(&ictx->groups); + return new_igroup; + } + + /* Check again if the current group is any good */ + if (iommufd_group_try_get(igroup, group)) { + xa_unlock(&ictx->groups); + iommufd_put_group(new_igroup); + return igroup; + } + cur_igroup = igroup; + } +} + void iommufd_device_destroy(struct iommufd_object *obj) { struct iommufd_device *idev = container_of(obj, struct iommufd_device, obj); iommu_device_release_dma_owner(idev->dev); - iommu_group_put(idev->group); + iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } @@ -46,7 +154,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, struct device *dev, u32 *id) { struct iommufd_device *idev; - struct iommu_group *group; + struct iommufd_group *igroup; int rc; /* @@ -56,9 +164,9 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) return ERR_PTR(-EINVAL); - group = iommu_group_get(dev); - if (!group) - return ERR_PTR(-ENODEV); + igroup = iommufd_get_group(ictx, dev); + if (IS_ERR(igroup)) + return ERR_CAST(igroup); /* * For historical compat with VFIO the insecure interrupt path is @@ -67,7 +175,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, * interrupt outside this iommufd context. */ if (!iommufd_selftest_is_mock_dev(dev) && - !iommu_group_has_isolated_msi(group)) { + !iommu_group_has_isolated_msi(igroup->group)) { if (!allow_unsafe_interrupts) { rc = -EPERM; goto out_group_put; @@ -97,8 +205,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); - /* group refcount moves into iommufd_device */ - idev->group = group; + /* igroup refcount moves into iommufd_device */ + idev->igroup = igroup; /* * If the caller fails after this success it must call @@ -113,7 +221,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, out_release_owner: iommu_device_release_dma_owner(dev); out_group_put: - iommu_group_put(group); + iommufd_put_group(igroup); return ERR_PTR(rc); } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, IOMMUFD); @@ -170,14 +278,14 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, } static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommu_group *group) + struct iommufd_group *igroup) { struct iommufd_device *cur_dev; lockdep_assert_held(&hwpt->devices_lock); list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->group == group) + if (cur_dev->igroup->group == igroup->group) return true; return false; } @@ -211,7 +319,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->group, &sw_msi_start); + idev->igroup->group, + &sw_msi_start); if (rc) return rc; @@ -223,8 +332,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * FIXME: Hack around missing a device-centric iommu api, only attach to * the group once for the first device that is in the group. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - rc = iommu_attach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; } @@ -237,8 +346,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); + if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + iommu_detach_group(hwpt->domain, idev->igroup->group); iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index d523ef12890e1e..2544f10dae9aef 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -17,6 +17,7 @@ struct iommufd_device; struct iommufd_ctx { struct file *file; struct xarray objects; + struct xarray groups; u8 account_mode; /* Compatibility with VFIO no iommu */ @@ -262,6 +263,12 @@ void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +struct iommufd_group { + struct kref ref; + struct iommufd_ctx *ictx; + struct iommu_group *group; +}; + /* * A iommufd_device object represents the binding relationship between a * consuming driver and the iommufd. These objects are created/destroyed by @@ -270,12 +277,12 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; + struct iommufd_group *igroup; struct iommufd_hw_pagetable *hwpt; /* Head at iommufd_hw_pagetable::devices */ struct list_head devices_item; /* always the physical device */ struct device *dev; - struct iommu_group *group; bool enforce_cache_coherency; }; diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 3fbe636c3d8a69..e5ed5dfa91a0b5 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -183,6 +183,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) } xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); + xa_init(&ictx->groups); ictx->file = filp; filp->private_data = ictx; return 0; @@ -218,6 +219,7 @@ static int iommufd_fops_release(struct inode *inode, struct file *filp) if (WARN_ON(!destroyed)) break; } + WARN_ON(!xa_empty(&ictx->groups)); kfree(ictx); return 0; } From patchwork Mon Mar 27 18:37:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667697 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 8364AC6FD1D for ; Mon, 27 Mar 2023 18:40:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232548AbjC0SkF (ORCPT ); Mon, 27 Mar 2023 14:40:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232642AbjC0SkD (ORCPT ); Mon, 27 Mar 2023 14:40:03 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2087.outbound.protection.outlook.com [40.107.95.87]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33CCA3AB0; Mon, 27 Mar 2023 11:39:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=USjHit39ETv0qWe76++lMpMuaaoq238q+pXGEoHHUnqZrNYx4wak5O/i9QpjY5MhBJpJESwCRPxdOH7F1AurqmMyrWq+xv9aL3+DZB3VtLYEhI4cWFWIipP+1X3mQ35z53PTDdGuSSB5oWOQstujl8EyCCP8zMlvsVXZ3Wmhc+ixWiEnDCFoSCNvWdgwj2w9ZiJGkcPN8x6n3HgGKohqnzUMqWDszacV+qfExV6bWOpywCW3yKKyzJz9y/MU+XmzbkORxSXBJi4/FTd9RbWlUPCQYvnZDEJ04GolWCP+e4rwhCpj0nEh4y39DbMKrTWG/tztB/m7NVUX6fcePoKGKw== 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=+6nDYQUsNfWAbmgRPmdhWZsdYJVwxkTOgAlxuEwaR4U=; b=IqR/7t+4Wtl79bmQyLYsmDOBuaH75zDPVRaQHvc3H91fXTTdbBTxb4ymanaPr7oZh6lFiTcwfSnL4q8CvzhhBMXBTBuoJQc95dBVI7mTsPPh+fPHHzehPm5L8V2SNn7fa3eGndFl3UpY0dNcl/pf6W3fqd4xaPuh+MtN4gbj9/qM5QYTl7pmlC7Wk+3mneVR9U4UV3e5ZfeLIQQWpDoLvZpMf2wZz5n/zxmB5jBpo/RMyWXP1wz33nYjO7nsLiVq5AcOqQMwOhLRVlcgMoLRlA0xQNJcjm/Ebzd9KD1vOUooSCLmlZ6VKpnz5nWOSx/uPea9/d2AxuL2CpwdYda7hw== 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=+6nDYQUsNfWAbmgRPmdhWZsdYJVwxkTOgAlxuEwaR4U=; b=DmyZ7i+6Dg6afdJ6OLPP4tUdNyuFPY3HcZLZZVP9hK2EtN9CpvNv9eHV04FZpMUFC4sbTSJLg7XeYIZhhnR7WG9QeG/w8sBrHzCONe2eMrOiCzJ/B9PEL2WjcaCUPimZkQ2N2RJC1KJ3QQs3jbyCkI0WKU2O3SRR9/RtGv4cXwnPVbl2mXY9Kzl2lBSkehBQXL6VEiijPiYHnNKYWRWvz+SVC3mVn7tf0+DWDZdtHIYoqRywlpqcm+uJfcw1PGiu7kV9CVmrSCrjk7kzOxKxWr67Urk+dml+1y9eOw7lYLof6y16ZnaS5+EYvbXDNcXBCyaJRM9YE81Ryj2e5Vr17A== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:24 +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; Mon, 27 Mar 2023 18:38:24 +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 v4 03/17] iommufd: Replace the hwpt->devices list with iommufd_group Date: Mon, 27 Mar 2023 15:37:57 -0300 Message-Id: <3-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT2PR01CA0020.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::25) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 6157340d-b8f3-44c5-0936-08db2ef26d20 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: S6Vkec+ZGDHa2zrbCW4oXkMEeimeCLXk/1beza0BPevLZfnLaGUyeoFLY+3ea/QvYUk6wLWD43gqJAwPI7NkMt79T5qWHw6Oy9Q+agPc3Lfdl1DadexwFHiI1PHbi5gnHhghUZ0E5fEEFwzFiJDD1fyuiTD57AlyNVuYdXyyH6++x+BVR77CKprN88OX8S9V1qbJyATEpVY7wHJNOC8yEXwnOwMxnE8J0iwMSSbwVYM4MHHcr57WruS/iLffeE6KoDokAu2I8HwcxByPF0kBnS5qvE6rzRdUR7R2ayY4nUE2e3QmK1UECLnjEb73TOfUGDCZZI8SQTHzRdoANM1Sa0RdtmYlSuHHNoFSYE6kXBUDu/ukxGu8/3liy9lORnux6d8+a21OD4G642fYKNIREniTNDmAtn0NyuHMF10Cb/75ymkw9ZvvFti544d1qwuPsHAUCoI3nYD1j5m1qRc3pWdtjbgybJsX0bWtjGXnvponBe/5S0Ho4d4DZtrYqkGnNRtIep3ienP5oOJNRrva678anQcY60Bfn12f636oBUXWZV+NkOkXnIV7SB8DCvIKvhRWPy1Yf5j/gLu0TAB3654y0ui5trkHrQrqzfkKsao= 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(30864003)(2616005)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: JQt+6bYcQLRvgHJH95xgzAt4VCdeEWo9mJ4HJ9HpYhklhd8zU+S7AnH2xS4JlQuX+xTpJu984xBDtbEfOwbTW3zZ5jXCQG4Bg8Jj00OVTfEtV6oIbnKHJ7HBIh4a9I64dCaJ5qlomICCSa12fJkC0CdsNRdPvbHW/9zQmDAHd2PgEQhPJ1T5tCwfGD9A5SAhthiedOv20gvfocZ5R0A7bR+CZrz4DPQvdr/h+ryigsSU4e8E/Dt4zcUEX9sp0YJ8BBHhplDfIOJISs43NkfiCiGiYgjAbpFYK+kxruUPEu9OzbU7s5yD5RBQjTju2NksiADHVgbXzRmSRLl4TWpXCN4KSOI0NuR4IETCO+AjluVWWZ7WISazXtS174OXXSHesBvpCVDdCtNczDwbW98uI90UwZLkzmYsE9rpMgkF3tAN+clDBFS7n8WxXJZ7Y9hLRonuNQqVMJPjYtJG5ZonyrzdWjYvKU1bYayKHHanEKSwuM/S8qa+qnbbdw1emQU5KXIysldJBiyu1Oq66Su3qjqxD+UQNuzTex3XIAk+jOemFK2y130pKjrilriBeoklJr6fKKRwRETMHl+z/WVVgKWjGjBPZt9le/KA5tIzfnzkqnjYa2S5GDC9vnZdHooocoSYTVJonH8BjWeKcR6/vdW6Hd49r70na4NMQhKeW02dIkPQdmYnbcS9vXVhxdzN5QtFP8z/5PMIVs6tfgbbUUwwM8R8I9cniJzoiYKbBjniN4D43CaqkryCmQyAaUeppOUcOqrctY2VZDz3SvWgg8UG1AtftIV9A6zPZ53CfN+4AJluFSymhLx5WMdRrKKFfv8Z1OBLvzKD/jrZTduekdYPsH8UKMeZzXYAvNCxh6EXe2K+juJbEMyStKXXH2gJLetHTwV7rOjQVWabkSU0d0RYlH9m731bOvZ3n5BxV+xpnhJzf0s56euEsJWDSsdr6L+0cvYQGNywTUqmfP84L2uBTWkl52EpNx2jIAbkqWupfPcSXjsbvEBPl/Mdmdmqs3zknI/HuYJU6bdBlkCJTPai14eb+jdJcyqL6ZPmq2yKpUDUistFPCHSGJDldaBOPqxxWnvIeU1Zo1EpholvjyuWkws3bcjz/aH2Mqhe5M9h/z/A+5lO/enfjy3f+jsD+C2/0h0Ej5uvUKUQ8oTIcju0HfIQemnfSTY6SsWwcbF6w7R0FpsYIf07mdIhFweWcSaa/GMiEr0x/668Xc/VL6SSnn7NDLtmBKsGnQOjmv9WEnV3qWXif8/aIgPJ/LYu04y2MEBzyF01j++yLDFZOwnM0t/SfrU8uvF/pnZLoRqdy3exWrLa+uLnWiHNDDmmfbpcP5ofAiMlGm3IyxLcI8EB+g1zEhkAT7+gOxZtcBt8CGID307t02yqbtmm+GcJGlhkIQ/aa78K1A8j4QVBiWDu9ffmuk7EKc9xZVUX0zgESvi+8hO82qzQXbMLunilwiTOivt52ESVtG4y/MrgSAM8VpytyfrZkkyEFNWdxgUq/SCJXLqOCWNYTgoLUhiI6oYC/p2kI1aLmr8gX5OjD/wtR0HiKpWvr7lr/utohKz+LKgqmvslOicOUAUfLluf X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6157340d-b8f3-44c5-0936-08db2ef26d20 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:15.5070 (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: z8c5jbnuVgfN0iVBmG+vU5kVCPKPVMCy807jgT/mHu5FLtU4P0t3s+GOxH3qfeml X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The devices list was used as a simple way to avoid having per-group information. Now that this seems to be unavoidable, just commit to per-group information fully and remove the devices list from the HWPT. The iommufd_group stores the currently assigned HWPT for the entire group and we can manage the per-device attach/detach with a list in the iommufd_group. For destruction the flow is organized to make the following patches easier, the actual call to iommufd_object_destroy_user() is done at the top of the call chain without holding any locks. The HWPT to be destroyed is returned out from the locked region to make this possible. Later patches create locking that requires this. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 102 +++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 25 ++---- drivers/iommu/iommufd/iommufd_private.h | 13 ++- 3 files changed, 59 insertions(+), 81 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 3fd623208c691f..58ad98de1914dd 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -20,9 +20,12 @@ static void iommufd_group_release(struct kref *kref) struct iommufd_group *igroup = container_of(kref, struct iommufd_group, ref); + WARN_ON(igroup->hwpt || !list_empty(&igroup->device_list)); + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, NULL, GFP_KERNEL); iommu_group_put(igroup->group); + mutex_destroy(&igroup->lock); kfree(igroup); } @@ -83,6 +86,8 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, } kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); + INIT_LIST_HEAD(&new_igroup->device_list); /* group reference moves into new_igroup */ new_igroup->group = group; @@ -277,29 +282,18 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, return 0; } -static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommufd_group *igroup) -{ - struct iommufd_device *cur_dev; - - lockdep_assert_held(&hwpt->devices_lock); - - list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->igroup->group == igroup->group) - return true; - return false; -} - int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; - lockdep_assert_held(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); - if (WARN_ON(idev->hwpt)) - return -EINVAL; + if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) { + rc = -EINVAL; + goto err_unlock; + } /* * Try to upgrade the domain we have, it is an iommu driver bug to @@ -313,8 +307,9 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, hwpt->domain->ops->enforce_cache_coherency( hwpt->domain); if (!hwpt->enforce_cache_coherency) { - WARN_ON(list_empty(&hwpt->devices)); - return -EINVAL; + WARN_ON(list_empty(&idev->igroup->device_list)); + rc = -EINVAL; + goto err_unlock; } } @@ -322,51 +317,51 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, idev->igroup->group, &sw_msi_start); if (rc) - return rc; + goto err_unlock; rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); if (rc) goto err_unresv; /* - * FIXME: Hack around missing a device-centric iommu api, only attach to - * the group once for the first device that is in the group. + * Only attach to the group once for the first device that is in the + * group. All the other devices will follow this attachment. The user + * should attach every device individually to the hwpt as the per-device + * reserved regions are only updated during individual device + * attachment. */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) { + if (list_empty(&idev->igroup->device_list)) { rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; + idev->igroup->hwpt = hwpt; } + refcount_inc(&hwpt->obj.users); + list_add_tail(&idev->group_item, &idev->igroup->device_list); + mutex_unlock(&idev->igroup->lock); return 0; err_unresv: iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); +err_unlock: + mutex_unlock(&idev->igroup->lock); return rc; } -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev) -{ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) - iommu_detach_group(hwpt->domain, idev->igroup->group); - iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); -} - -static int iommufd_device_do_attach(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt) +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev) { - int rc; + struct iommufd_hw_pagetable *hwpt = idev->igroup->hwpt; - mutex_lock(&hwpt->devices_lock); - rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) - goto out_unlock; + lockdep_assert_held(&idev->igroup->lock); - idev->hwpt = hwpt; - refcount_inc(&hwpt->obj.users); - list_add(&idev->devices_item, &hwpt->devices); -out_unlock: - mutex_unlock(&hwpt->devices_lock); - return rc; + list_del(&idev->group_item); + if (list_empty(&idev->igroup->device_list)) { + iommu_detach_group(hwpt->domain, idev->igroup->group); + idev->igroup->hwpt = NULL; + } + iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); + /* Caller must destroy hwpt */ + return hwpt; } /* @@ -375,7 +370,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, * Automatic domain selection will never pick a manually created domain. */ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, - struct iommufd_ioas *ioas) + struct iommufd_ioas *ioas, u32 *pt_id) { struct iommufd_hw_pagetable *hwpt; int rc; @@ -392,7 +387,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, if (!iommufd_lock_obj(&hwpt->obj)) continue; - rc = iommufd_device_do_attach(idev, hwpt); + rc = iommufd_hw_pagetable_attach(hwpt, idev); iommufd_put_object(&hwpt->obj); /* @@ -402,6 +397,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ if (rc == -EINVAL) continue; + *pt_id = hwpt->obj.id; goto out_unlock; } @@ -411,6 +407,7 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, goto out_unlock; } hwpt->auto_domain = true; + *pt_id = hwpt->obj.id; mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); @@ -446,7 +443,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); - rc = iommufd_device_do_attach(idev, hwpt); + rc = iommufd_hw_pagetable_attach(hwpt, idev); if (rc) goto out_put_pt_obj; break; @@ -455,7 +452,7 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas); + rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); if (rc) goto out_put_pt_obj; break; @@ -466,7 +463,6 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } refcount_inc(&idev->obj.users); - *pt_id = idev->hwpt->obj.id; rc = 0; out_put_pt_obj: @@ -484,13 +480,11 @@ EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); */ void iommufd_device_detach(struct iommufd_device *idev) { - struct iommufd_hw_pagetable *hwpt = idev->hwpt; + struct iommufd_hw_pagetable *hwpt; - mutex_lock(&hwpt->devices_lock); - list_del(&idev->devices_item); - idev->hwpt = NULL; - iommufd_hw_pagetable_detach(hwpt, idev); - mutex_unlock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); + hwpt = iommufd_hw_pagetable_detach(idev); + mutex_unlock(&idev->igroup->lock); if (hwpt->auto_domain) iommufd_object_destroy_user(idev->ictx, &hwpt->obj); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 6cdb6749d359f3..a538ceb13a6f22 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -11,8 +11,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) struct iommufd_hw_pagetable *hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); - WARN_ON(!list_empty(&hwpt->devices)); - if (!list_empty(&hwpt->hwpt_item)) { mutex_lock(&hwpt->ioas->mutex); list_del(&hwpt->hwpt_item); @@ -25,7 +23,6 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) iommu_domain_free(hwpt->domain); refcount_dec(&hwpt->ioas->obj.users); - mutex_destroy(&hwpt->devices_lock); } /** @@ -52,9 +49,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (IS_ERR(hwpt)) return hwpt; - INIT_LIST_HEAD(&hwpt->devices); INIT_LIST_HEAD(&hwpt->hwpt_item); - mutex_init(&hwpt->devices_lock); /* Pairs with iommufd_hw_pagetable_destroy() */ refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; @@ -65,40 +60,30 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - mutex_lock(&hwpt->devices_lock); - /* * immediate_attach exists only to accommodate iommu drivers that cannot * directly allocate a domain. These drivers do not finish creating the * domain until attach is completed. Thus we must have this call * sequence. Once those drivers are fixed this should be removed. + * + * Note we hold the igroup->lock here which prevents any other thread + * from observing igroup->hwpt until we finish setting it up. */ if (immediate_attach) { rc = iommufd_hw_pagetable_attach(hwpt, idev); if (rc) - goto out_unlock; + goto out_abort; } rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); if (rc) goto out_detach; list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); - - if (immediate_attach) { - /* See iommufd_device_do_attach() */ - refcount_inc(&hwpt->obj.users); - idev->hwpt = hwpt; - list_add(&idev->devices_item, &hwpt->devices); - } - - mutex_unlock(&hwpt->devices_lock); return hwpt; out_detach: if (immediate_attach) - iommufd_hw_pagetable_detach(hwpt, idev); -out_unlock: - mutex_unlock(&hwpt->devices_lock); + iommufd_hw_pagetable_detach(idev); out_abort: iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2544f10dae9aef..2ff192777f27d3 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -250,8 +250,6 @@ struct iommufd_hw_pagetable { bool msi_cookie : 1; /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; - struct mutex devices_lock; - struct list_head devices; }; struct iommufd_hw_pagetable * @@ -259,14 +257,17 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); -void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt, - struct iommufd_device *idev); +struct iommufd_hw_pagetable * +iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); struct iommufd_group { struct kref ref; + struct mutex lock; struct iommufd_ctx *ictx; struct iommu_group *group; + struct iommufd_hw_pagetable *hwpt; + struct list_head device_list; }; /* @@ -278,9 +279,7 @@ struct iommufd_device { struct iommufd_object obj; struct iommufd_ctx *ictx; struct iommufd_group *igroup; - struct iommufd_hw_pagetable *hwpt; - /* Head at iommufd_hw_pagetable::devices */ - struct list_head devices_item; + struct list_head group_item; /* always the physical device */ struct device *dev; bool enforce_cache_coherency; From patchwork Mon Mar 27 18:37:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668445 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 6DDE4C761A6 for ; Mon, 27 Mar 2023 18:38:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232537AbjC0Si3 (ORCPT ); Mon, 27 Mar 2023 14:38:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232287AbjC0SiX (ORCPT ); Mon, 27 Mar 2023 14:38:23 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2047.outbound.protection.outlook.com [40.107.95.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17E114223; Mon, 27 Mar 2023 11:38:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=muuzVwUDZ/lX/DzMOvszKxCl3DCG+5tqIp3qJ3/igCx0eOhNb62MOzfvw/iDTSzDOCAcD58/geOkDOCKMNOeFv1OZLZ7E7xN8i+abnS3j1T0h//9h5bmLCPpdAaxei5+tH1MKraD72+wH1WLXObjxr2n97ajXr9poEm/yNaHFw9sFZNin6ltgyhEBUQwwWGuaawrqjUzphyQ20cyaPoXN1eJf6S5LwEIjbBXLp19vZoY4TOtGV3hRATBeQ8VFincFvs0IFX9LHE7pZcE3wkj8nTV2lWVjEaGxoV89lnacKUrt88XwBUNdJTagTlb5F8x/iY2JWxtRPStNy4j8zp1Zg== 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=MhnQUk2S8XQINgCIGa8jmbCAbr0NSnVgaNKgYsPyH9U=; b=Z3t6ErLzj4KGAXHFvU8Ubko3tyGzHq88a/osH99O/JqZjVYmlYJHgZN8R1y0TlbGGSzkKJ+J+SIZaowf3FqGNtCNl8Fc0HhH2cw4xKA01A5ZfbktSrHT6dfzq8nNeMBORTnG9sJtV3ZapRhXwaX8SMV45PBIZK0UEk4i46K0vdqtLsplHf8okVzgHa7rzIAXpsgvtUbT8s+lDax3x44BGuoXmtOQdPF/+IdFEYf2RLY1xGhWsA+jH5FY0GicCVHCsbCSEys/MMsFbCnFtVQQ7GZFmjg2/pM5WjcRbPzp5qfUVdMBNj72VU54VCSqtQP4fmdeQ2GUnQGo1We8IgRbAA== 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=MhnQUk2S8XQINgCIGa8jmbCAbr0NSnVgaNKgYsPyH9U=; b=QAnlR359c/pLULQS5yTsjLEo/LKTTplvDUIolQFAt+9sv+/qPSC4XCB/ogBxcY5RcQL3qVjodww+Qj2Nre9RWt1Qqr0wGAR/9kHjLSrD2SUiChJayF5Jem00Lnc/Y7bbF3LxmGcQYAMjAMeD6a/o/hOfyHz4YsJCZE2OgOZTv6eoBOsQBmBTEGJBZ6hEZdXe+bS/YtrVDWZlFlO/ECip+6VPCqyqQ41jOEFYRs8ufEycL2kaQBForpvGgTXuEVCkGpy3/i/HIuTGt7aT8h13MS2YVJjjZsytGIgJjVZSZl2SZhzq7OEOyM0WchXDfyDj5zTGUQ/Kac8fX7NSXw9pEA== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:12 +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; Mon, 27 Mar 2023 18:38:12 +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 v4 04/17] iommu: Export iommu_get_resv_regions() Date: Mon, 27 Mar 2023 15:37:58 -0300 Message-Id: <4-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT2PR01CA0019.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: e8791fdf-27d1-47a4-3aa4-08db2ef26b70 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GgCe7Z1PA7OkuYXUlB4F5GBo5k1iKVUlplE2F74TRWVaZvU0eb/9kyJCYC/y+gY3Ez4V5xdadShU87hqUiMHzVw9Vl4eff2rts6wsShl6cq93L3zShTZWRLE7aBCEzO7UGhlraZ/vECZ1iYpU3kq5tOoF++gbrZM6svtDcq87ZBf2IolKOMgcVkGvhX5+xmltn7wJbHd4qc8MRU5Szdp+JHYAvySTF59NIa1XU7vxmR4EueJv63kxQ879e7eyuGLL8+GA9x79KEatFNaMQQfchuHgcTQ8BsB8D7z2Iq2FMAoyh1R/5OTxFSJxQq9p5xvvPCa4TTPwEbNjfPxUPX6gZ4lGqShLAQnuvCbUYV+cX8VkYc9VL3GMfcK9klTU0GFmD+HW/M/QHtI7Xg7w2ZRDpcbycwL1C7QGshTTxBAkT592Q4XzLBguz38i3UDS54gnt7JenbhCo+8I397uiTs3wlaiwHxnG/IcBhyTwDxbDWkdsI0uyVuBSLeOEjC6pjClZ07jCta8JeO//cdQCx28cy/6giokt27oQfFsak03W77tyUjchMJ4TbWosBR3HGePjbhfbcO05zLoH0udla8++/9ZVV8RRnSEwwdFgvqUM0= 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AvBP06QpBl8Xp882sjng+Q+4TnZM/v4R3EDRYD8S1cg+XSx2hiYAwxrwRRHNf/HaxBqU3xu2H16BGQ9y1XQFH/LTv/mH9Rtk2KOl2ULwboelZa3Qa0EcEF04OzIKSxB46wyXXXfffyVCBI/k8UgLD0ye+mj5m5gs3U8VCBdofw2qw7EnPbB47oo0yBjaNLQJ8gZfpAxrEMVvtrusS3hYD/YemTAeujpIEHdA0CWPHFcQU1cin/mYqbxCgQ8pAk6u6DCk4TAeaJhgdQ96cH/x3vo7B4exnnnlMQADPCjhy9cNp57cDEEmuTe7N4JDAL8BsN9Hvw64q5yTPXNmSYpqT+cY01aBeHka5Umz4LTQ/pWlh9guU0u4a99/2dCtDiklwuZVYjMS8nc5ZUVTpnhJgf5VMZghHSdwqhwUVnV6oUrZzrimQNc1+xuflCmNKMUZkTCaPs9zgB+LPlS5WvFelf3q8o/cv1YvT+Vqj4KL/gUm3S3Vl1GECFIeHA/+6fmvs+nSL4WCytDNPvtKJ0HZx3k3n6Sn0HUK7zcX3QY+Tt0V3q4oweTTMFvpWsUl9SOgFUV7347RMypHRRj3B9ll9ECKwsBwFYavBt6hwTJvS6S6rQMCTnEfgJjaKUSHQeI+KiKRsV9P2DBFan3p0bLug1NWwOx7GCcNMW6yZ1KSorN3xsDtXab0WyUU2r6iguN4SeMUqL9ETblX3LRUAyCPKGHvO0teh4qtj3daWBYB4EJ2XwP7w0+FS4LGCWS/gPespq/6Qr5A6IbIO59VgWCIHQ8sBF79lmsgN/SZyRdfWNC/Lcm/+AismIxmvbcxMQTHJOPUrgYQNkvYUkxZlAwpDYEUoJ4puWYKb3qbLNITPr7PBGSaf1Je6Y2UOCW+++AqIGmRddJc9Q0j/Kq8GLycnUqDSbPs0lX/GMLlTAYAoM+whulGFe4RO97qWkIhmV2Z67i6dCyn/xsq1ON2a20fXg/xW/+4RwpFLtWdkxZMS857b9GfPn1FtbQnOS/oYBs7/Z1jI5NstC6obg9RQ0H+POpyhF3pOrkK5+xKf82iue/7SneN0RbKo4CLiJcxNoOJNXj1ivTewxTyCjbJ8DXGu7EOomzdw/SNm+Zxs1zBwkbc6WnVMvPOWSohvmRWen4CZLMZt9dkCcaveu6xxYoV+EbBrrXaiugHtcey98JWrHeGCC51dzN2gHpEiPo0ZYDDSvHgxUvpEXM/qOUjt2zOPuWjfrasxLDY1DjXaT6Bw5vwSSpcqsOzp5mfeLTFxqUCrNMCAMIjq46eMc4UCS9/8PVeuwbr23IO3PbT4pBFsYmNh3Sqx/Or+hebdGV7Fh10VQGvJa95SP4/Plj0y8SHcDxXb4sfTtQZr9HENHXxUavCrJCSUPIH9C93ZfBItpxfaOqbXuCZ1gIN8iL/dBwqNvPjtelwtyDaRHKO4n/YixwTBWLUPkMQsdqYTox8eY7H3UOPQqAQVWU7IUKMDZBgLRb2Z+PFzTjd39w96Iu7Ze4ZISZOb2N1ABUo7o9BJ1eW2+lx2yopmnWBDrLTHz91joG5LTU/ioM4OxDzfad/2P5aPA1Ho+5nJGKATcYC2LLB X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e8791fdf-27d1-47a4-3aa4-08db2ef26b70 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:12.3943 (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: 7fuJiAi4hxw+km1xLhyhuCWasENtsIrHVo7lhBPy/nq1KBK2Z/BdPL6u+UcqVYOt X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org iommufd wants to use this in the next patch. For some reason the iommu_put_resv_regions() was already exported. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 10db680acaed5a..76969904b93af4 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -2639,6 +2639,14 @@ int iommu_set_pgtable_quirks(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks); +/** + * iommu_get_resv_regions - get reserved regions + * @dev: device for which to get reserved regions + * @list: reserved region list for device + * + * This returns a list of reserved IOVA regions specific to this device. + * A domain user should not map IOVA in these ranges. + */ void iommu_get_resv_regions(struct device *dev, struct list_head *list) { const struct iommu_ops *ops = dev_iommu_ops(dev); @@ -2646,9 +2654,10 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list) if (ops->get_resv_regions) ops->get_resv_regions(dev, list); } +EXPORT_SYMBOL_GPL(iommu_get_resv_regions); /** - * iommu_put_resv_regions - release resered regions + * iommu_put_resv_regions - release reserved regions * @dev: device for which to free reserved regions * @list: reserved region list for device * From patchwork Mon Mar 27 18:37:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667705 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 24145C6FD1D for ; Mon, 27 Mar 2023 18:38:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229663AbjC0Siu (ORCPT ); Mon, 27 Mar 2023 14:38:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232428AbjC0Sin (ORCPT ); Mon, 27 Mar 2023 14:38:43 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2076.outbound.protection.outlook.com [40.107.95.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDEBD4480; Mon, 27 Mar 2023 11:38:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jdQRgqSkKZtkFWg7sGZWjkWwnAn59E/c2XYYY5S6jAmGb4Y9wPmlNLY6edBTOEx/zhiBIE3JYTW+YkxvEQjp/uBPJKrK2v2Vs//0zDVG2/Sy+oMBlhI5xt3B2j0wQ3hotGphu/ohW86fEFAhtrC+dTSw7tx12LKxZ5ZeaA6tD9J4ccXt7Buoy74h+OY5hUnLqt4bcf77kYB6xtFzi+9TTMhhbi2vNLrKvqfZZhJ49ixaqOC9xkvYC9oxyvDC9BrZZYahNGesPlyXCaDFOq7EbNHVLH6m8Z3vLgXvh1/vfO5S4ba/kBlfRP1SDp9L1GFMiinNLdjFa2nSsCNm/fX5cQ== 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=WpEHs9fKeNh95f/0/goNyHkftAxd/LJsiY50q4cKulw=; b=j7BXGqzSBaKutSR9PAKEDm+s0gmzRWYwpbHqP1y1vNSzWLSz2x0TU608iLbOHziegVGxGAT7nVxLmpqsPiWOLMMAE71iJEfezfWlivOdiTI/pOjzWTvO/0emMJc1/oRFuuft1cVJmKNncpYUhtza0Lh0FErCGsLLvKcR1io6HH+jV+XKy2IygEjEJ2x8xVFSXQdvjEcS5gk65SzZlpSGA5sKAmmmE/jBv4ifr/k8o8xyi/CRa+yrT86jQENTPgb3LTHuHTm+4Y+CeSgJqQiLalvBmlr1coKA+TMZt3Ow44MbV+1saaWYmhvJ4iwnL6So8XBNlZCigWhoG75757Ymbw== 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=WpEHs9fKeNh95f/0/goNyHkftAxd/LJsiY50q4cKulw=; b=b6LSIRNZmhb+XXLyPwHm1RY92Iajpr9SyP1iHQAGMVDPh5kHRUCc917B7T1OuytlAOWWglwiukLZh5t/erC+bfTPyOCX7M7UnmUBcYxakFPKSZXjOj2aN9Xe0Bgr4Tqqi466ocg3oaR3m4ifOPE1gW2ygP658gujf+vsebyCr6VK0TOgSuk66aF/Ntj3c9NuyYDSZLAdkVRmWEuaTaqab7rPtBSJS0YLU5X9iGh79dIcZUX411K410Fl3DsCOwgFcFSulrKpLdR9ankYrPemcByB1KM6iQckwcoXc/E0vh6HbaS1a6WnCNP479pLJx9svTflZ+x8Y/OlwDhKXma1KQ== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:13 +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; Mon, 27 Mar 2023 18:38:13 +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 v4 05/17] iommufd: Keep track of each device's reserved regions instead of groups Date: Mon, 27 Mar 2023 15:37:59 -0300 Message-Id: <5-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0386.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:108::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 9af7718d-01a7-4040-761d-08db2ef26b6b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YzoaH7QJfjIQ7DPwdm3am1VMf6JJC3w0iv5GKObcQd6FwRut0vb6domZdYYwqGnsrLCFdkbOuZkI19ig/DmlexU/aHCx6xJyMSZ/qLkIIEF7gOlbLm3dECipssIETMnizoV2GNBNOWqBGcF5oQqUmYAsDypaLTWDdrs1ueMMTCDndXfw4RqmeGqd7Yio+3mX8WFKMOHlirZX1GoAJe4ZhuPaj6ZdKceDcDUxITUDqGF93r5w/KKNYRnI0JMt0fJjnGw7ICHoUfdJ9jzoT0NbWdaXHzwSZDnyBgOOpFy8wJrhygu95HK3Nj9uFWVVDsRRB7xkPMyuy3a4KVuhPRIs0FJ5ckPZnl4lixGcH2ifjS3GI9eG1ZR35S7fzizKetJldbr9VTJQW8110hzsaO3D8HYa13XW0HfmWHZ1jTVfvlpj8JviuaBMZpHs51OxIt9Ty+k8EdZQp6zbG14EG8xPcu57C7DA3HppAi40DTmVBLCaH1eBJb0tAZH1ev4Nqecce4D/aXrnXlm0Rh1YTpt07L0FiUyoDHreSKD++eEQlzq6B1avGl3vFeKu17/gLgGobMJBTCEmlPOV1cmAA8Uym8+JM0jJL3qEc7CKpqzuP+0= 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002)(66899021)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: awsxuQYpTZDeEmWK1P5TAu5XH7uuPGRa1Pkjge0M9kc5lysrOmr+iVQ3xpzh62aMX/ptulYvbBAro7fb2TULLPIi7WeozOmFnM/s7JsPgxFZYKwVGvkgfiYx3yhqF5xndfh5QAurntufxaoPPBz1DtqyOCHPo2YIStRPz6lpOxY9XrL6oIh5Xy2JXbLPKJDJqWLMETFbc4ykss8g2qwKSJ+NzqkDRfDEhyp/ZXmADyM3yCNBpTLjzFppTYnGhAfjmnhxPop3P91QMUkyIrldzzFZJKRIxrVciZAE+h5wkRC5TdWtaG6kvJq13j8CvVtbVQd4ON1NbT/x3pSKJE6b8jeS7xqObSbdujstwxpo2wKWpRekkRo0g6og78ojyb9y/8EHBtukzBB0Xea1QOL9TYy03qGVT2t0VTI1Cy+L7jn+AnmbytSQfW647sBNmalSBaeBPHGu3sKHgm52yZW/K0peBPQapKjDVBC4RJ6upXVQTKTk490VB+mMelXGKWDx+7KQsSuSGK6fiTPtuTKHiOFMfWl7JsK8YDnJS+UMkE6qxS+RNCZn05vyQEApy27aqiPWF+ecoNewbRdHuS6TITCezKk9sOTfjDMpTKdsUGvss6pVHVCXv2x1ba7wSpVP6GeNk1swF8yUylSfuKmTkLAgAqgGXW//f5ubuPBc0m/8Tvu7gdf0dS+EzNuU5gtwHHdX2JkgYvCHMGYIc2Anwzyq29qhGrA+BdF40mFXzjhxGrc0hkfEdwMJzwFJHDtjqR+F0H0jLu8YLD/f5tcF3ItVTmx6Y0QH+E0wJ6tXAC+CYA8zMutvrdvYZwCvqOAMdimJciVRyylBMvu23E5GhMoXpkR+E6JjFSaCM0qFfA/EZ8fTeyTE8yDg1CxhqPep+loMIJMGGneGQVpSohXyLkGicnOnV2bg+cL4Ugr3mxLGmFq8e2VI2Ti23O4mFf9MP33NKHaWJnj1mjfGumcdvnPtOTxLpeelelJWPmkeD/7AUZlInKXI25FgErwFwGDZ+ToeCNVD3ykHseKQAylytfwg1pMNbt5AHEVvpK5KIXzqzAMFXdDeglsk3n1itVvmK6fOEqOYQepolZ9qEKUCoK6+3nYQD1kM8nmOznEWBW37YyT8Kf6QU5PBt1KQav4TZ9Rs82KjMYihXoX0SrGgHE2JlOXqJ9wey/mEDRF1gDHJ7/jS9n1ib1VmQGjvkiOCNCPQDxupX6c6/R83npjONaH46kuM/lU09aBo+tF+6uaG/XtsRCOaMWJYRhwMWCNZ9O89Bfv/jlvClkweJyH1UJ8CLysKs7rD2WiffeDr8K15pgA45G42hj+QiM1QeK6qw2TlZjmvdRsY0ooeDrtEPI2uw/t4ize2BPQPnjYqvINgoWH78JnRJSc6N+EnF49hBX1AGrFKBumkWTxfXjeM/BhPpDrCqg7M8guv+R0K0fDlHgXx4nSnQJ0LfrfHi9WC5SBwqBdT1R8lTIpg+s8R2/xO0daMpXdq9rTJjHSKmTbAtrnhszgPcQuOVgcBqO1TWa4jeeSzacJUwzHSriWJtwCRRtcvhZXVYvKSXWs53lnJdbUN3VhI7jf4cev6AajR X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9af7718d-01a7-4040-761d-08db2ef26b6b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:12.4256 (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: JWl+M0/mL6n8K8/uj8ws+twdi+ufUmrRayY2gSZoF8Q9LbLnpfCKPoZo7z0qTANZ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The driver facing API in the iommu core makes the reserved regions per-device. An algorithm in the core code consolidates the regions of all the devices in a group to return the group view. To allow for devices to be hotplugged into the group iommufd would re-load the entire group's reserved regions for each device, just in case they changed. Further iommufd already has to deal with duplicated/overlapping reserved regions as it must union all the groups together. Thus simplify all of this to just use the device reserved regions interface directly from the iommu driver. Suggested-by: Kevin Tian Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 5 ++--- drivers/iommu/iommufd/io_pagetable.c | 27 ++++++++++--------------- drivers/iommu/iommufd/iommufd_private.h | 7 +++---- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 58ad98de1914dd..112b6c541c47e4 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -313,9 +313,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, - idev->igroup->group, - &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, idev->dev, &sw_msi_start); if (rc) goto err_unlock; diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index e0ae72b9e67f86..f842768b2e250b 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1161,25 +1161,22 @@ void iopt_remove_access(struct io_pagetable *iopt, up_write(&iopt->domains_rwsem); } -/* Narrow the valid_iova_itree to include reserved ranges from a group. */ -int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, - struct device *device, - struct iommu_group *group, - phys_addr_t *sw_msi_start) +/* Narrow the valid_iova_itree to include reserved ranges from a device. */ +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start) { struct iommu_resv_region *resv; - struct iommu_resv_region *tmp; - LIST_HEAD(group_resv_regions); + LIST_HEAD(resv_regions); unsigned int num_hw_msi = 0; unsigned int num_sw_msi = 0; int rc; down_write(&iopt->iova_rwsem); - rc = iommu_get_group_resv_regions(group, &group_resv_regions); - if (rc) - goto out_unlock; + /* FIXME: drivers allocate memory but there is no failure propogated */ + iommu_get_resv_regions(dev, &resv_regions); - list_for_each_entry(resv, &group_resv_regions, list) { + list_for_each_entry(resv, &resv_regions, list) { if (resv->type == IOMMU_RESV_DIRECT_RELAXABLE) continue; @@ -1191,7 +1188,7 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, } rc = iopt_reserve_iova(iopt, resv->start, - resv->length - 1 + resv->start, device); + resv->length - 1 + resv->start, dev); if (rc) goto out_reserved; } @@ -1206,11 +1203,9 @@ int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, goto out_free_resv; out_reserved: - __iopt_remove_reserved_iova(iopt, device); + __iopt_remove_reserved_iova(iopt, dev); out_free_resv: - list_for_each_entry_safe(resv, tmp, &group_resv_regions, list) - kfree(resv); -out_unlock: + iommu_put_resv_regions(dev, &resv_regions); up_write(&iopt->iova_rwsem); return rc; } diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 2ff192777f27d3..22863759c3bfb0 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -76,10 +76,9 @@ int iopt_table_add_domain(struct io_pagetable *iopt, struct iommu_domain *domain); void iopt_table_remove_domain(struct io_pagetable *iopt, struct iommu_domain *domain); -int iopt_table_enforce_group_resv_regions(struct io_pagetable *iopt, - struct device *device, - struct iommu_group *group, - phys_addr_t *sw_msi_start); +int iopt_table_enforce_dev_resv_regions(struct io_pagetable *iopt, + struct device *dev, + phys_addr_t *sw_msi_start); int iopt_set_allow_iova(struct io_pagetable *iopt, struct rb_root_cached *allowed_iova); int iopt_reserve_iova(struct io_pagetable *iopt, unsigned long start, From patchwork Mon Mar 27 18:38:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667698 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 33588C6FD1D for ; Mon, 27 Mar 2023 18:40:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232617AbjC0SkC (ORCPT ); Mon, 27 Mar 2023 14:40:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232548AbjC0Sj7 (ORCPT ); Mon, 27 Mar 2023 14:39:59 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2076.outbound.protection.outlook.com [40.107.95.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8ED704693; Mon, 27 Mar 2023 11:39:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kA2iMZRrBxownrOC3UfuzA7sG4aOajP1KDJDQnBCLRqpwp7rgDJrOK2kgIJhieisPMvk5yNr3TvoOUMp8zwUal4QbLIfKbEq3ily7zridibdRE09WFOH3bI4KMVIh6Bi0DmCBv+q4+SGVWs+LYUiC5tLMGMID7m52X5xEJRSt6eIOxkHJu8zgdPKrUIWiXewYiodOgsdAMyPSg3dwK5sWBwHJTriBgAnXt8Gf9u4XtunE5/Di/SdiF5kMS3o+0CPo314U4QRQRdgbiGEklGRY7g6xvbGP10neXmZzGES8wj/12EFfu72oX8EqNmqhpI8zFip54aRYIepnIyW9C9nng== 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=FfKinqvF+fUlGnIibM7uDNiQvHgsMRmgf9gVZYmQiTc=; b=fjin3Ok5w3hGYFsrzp/EaoEwco/XMriHaRzR/jNsp+wOBosrJNFLGBE/l2uu3WortN7NKEF6UqtcvwAwv0yXQGkaZ6dpfYVmgM96mT2+cFrqXM2IEzO46+fSP8ws2FFdp0/ZvxjXau2VjU+uQ5CslNqZZWm2ef3aQEDdBwqvEat9HYY7gOOvxjJrNQugjviW5ho9h3m2S0Z1wBN27HkL0akbQf1CeKyz8VRc6fppjS7zrcKhtO9mWwOlDHBkkkMQFX7RxQw/4HhGo0fVhBLsadSXQbXtXsDtGeDZPos9B7xbpW9TExEOpwLkE6/UthcOyNlJjzdSmcZoni44yPa0eQ== 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=FfKinqvF+fUlGnIibM7uDNiQvHgsMRmgf9gVZYmQiTc=; b=hh8avczQlGI83xphlbI9ayD/tA0dpHcWNe+mB2RBbsr4cyFwBrczLyVCUQ/pPfeDFPrl2QHvwrD5R9rYzRx3+3TuC0j+AGshs+vWD3QdvjvhdgbztUIE62JlwJiZMaTcyV0TYTUKwyeS9znDSwG8Cc6Jyj8ZjokU3nq9+KJmA9hCDuTQtmho0QpSCjBJ8a4URCLJlp82H3TptooEiIYzbMYj7UJxWmXWziBnFO6rus70rrVdcBmYJS/toxTOoRXExcXWO16inb/1A1MKogswy5PRCCoIlyMJ0ac6KW/DSVFw8b0nRzQ4y+CRfa4gDcxNAm2NKliew40NsD34URLR8g== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:23 +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; Mon, 27 Mar 2023 18:38:23 +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 v4 06/17] iommufd: Use the iommufd_group to avoid duplicate MSI setup Date: Mon, 27 Mar 2023 15:38:00 -0300 Message-Id: <6-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT2PR01CA0014.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::19) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: de32c114-afdb-461a-38dc-08db2ef26cca X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EJZ0R8fAIjnCSQN1n4BfJMB6zemDyEPoQt5HeMjejYs+uyUHJbjbE5RrD2iJ0Co7nF5uekWaAViZ7CWIy+W5ELCA3wNBSyyj25CqODcsTv+4Tu6k5xnCNHzl/vCz9kiP86h0cEC8IL2dVpBJIUaN+/i75NOMtM9WJ+8AeSW9odty9rLZLcW6fDOZVhLt2s7OME0ziJcpZjCxONkpthvIfYRop/19kXkc8jzNuEy932vT+euLX2/cIEsE0mE0qafn4o3MAJDT8XqhBrWauifzVK1LN1I/MrAGHmQZ2EsR64jpjYIR4wvDy1JRMDdanmo80l88BB7mUiDHy5gyc+XKRebeqyoL34PPjxq79fytkS/LUeD38jMDgSE11F7nrAfBeou5fLS5XvpS5XSmiSvaV/XNmxcYh8ghlvfjqUGoSpt8xkVtHpyczwDfwp/8GLyvuAf9vF5hpuJHVOYzMPQL6fV2VHUYikLYK8YvksdVi/CoMUZ5j4JUSENXtKetlw/gj/XoaCGTwqQx4qKgvdsu3WXng3wuZ7HZgmg+JK4sv1RtIh+5HMwNl5Up1IO4Whmj/DVCje0pEqN7kBntCLGrFCGMDOAhbf9T0lf20YSJbLA= 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: PLPCEJXq5aEG3GV6r2M1eWfZmRqrl1Aod/RgqKSsUArGArcwgf2vFFuBzvsas2se0lh2ybNzormy0LhupsFQXDu8ZVIX/ba1PBiBZRdV7v2Zh6Gf9WvxuxjJQV56P/lbXJYsXlrZGjcrpvRRxecj8TtA1BekZ/LghpzapSJ14W4xz//QVRn+TyJ1UfoehKHl46z8k4NMdVdO66rkzDYH3+oJ9hXN8MZULUt13Xm0xljWDqVZkWFGDxYkLPBhIEV5gSwCJ6hbcTxVKL5Ss0Y2YuHJljUjbY+ydsO4tmfDqJNjlFWZCOCUjx6TRTq/DK3XSkNqdaPBLZ+wm8SDKRF4tmYRp5/L9lKXTgyO8rrawQVKddkXxhfl/93O02KRp1OxQyrpWAsvmoz8g9/BcxYuy2LiTcN8h+njh0bOZO2LuozA+APE5KOs5vttNt9v1/l6vkI7JS4BpTKOsygm7jq2QBifY0t0AA/woE7fJcpl7ckOo7UnRAZVsoGGOOjwlS2tmSYqhX8X3p0YEgv+GnM/LVVIF0FXXpFMAsdUIX0GY+OLFbD0Kcq/FzSVNgsuSdjpi/ZhCEQiJFcLs8SCrAQZCQXH1W6+NtIo0EeNzlU7O1svSMvT5pAmrlPkXtPQWd7ZpoTfp9g7DMsOKqYD+zSGSDDW91lVA9LBUCFrn7p0kfwwHQbYwoTlphpgSR8lItens42F10kmopGYNDowWEseH2hklF0DYuDSDrcSpXnvSnpT5ia9Xx/jhKjyEtb5qu1zOqSmF7RNTavDRQxw8DxxvpxHkOkHSPwrDKMmazztMOyFp/04qVHswawmeeLPwztR8H+BBG4w9YL3J75RWbiTcN23XKvZ1ZCIZD/m7XNtSesQdfgDZUTwTLfa3tWGwKSqtR1Dw6/XKdNYr+UCkpQ5UuSIp8QAnmS0yEZkqNtiJsqHGtST/7WpcMBueMgNwCgQTohKnRwgW7aved1/iACFUoPQ3W/jlqRtvCuV1aXbgx+ZvGYe0hp0eyb2b272MvRWd74US12EL78X4WJLzyS3x4N4bg1anFcFZbu4b5HJch2tFKIqLfomBI4fcMkisuwJ65xlOftGPgDeitK3BcAwzhelLP5gz6Z/KzBJUoYIcRrjmtV2+1S/GxxUoZpE4cp5N97/ibQnzjo9e7ltDdQIWRwoPN8QbENEvBwayhD6s4NwjpY0eCoWjZeAR0PTQjt7SC5wHPW4hSFwFFfYEybVxuSWuHAus6xzeLFQkdox85HA0WNgrO3uFA6nSitPxeri6DJtu4k0ZjYIdzRGuLmk0CoNwKjszvsP+nPn/5iHyxeWeRNKxaGnNGZU8g/1xGCF9M1desp8t+vD1SNk+CkUmMnfFn5//eYBQgoEPUzlU2Bg+2ZKVjsfi+9CzOL+x8ChVdk0CXKXWNm0HOSgBYF32nv3P39KaFk2wkmJNGYOYKRMCrvxKE74YuFVULCiP0eMY8dZptQ35WKSWDNJaBAB6zCAzk648JXRkMiwWBSYRRkXAzVUi/hFyj0VTSM+u9mZLc2024GuGhSouXmRNXWHkm5ul9wrnT62nDJYt96VcsAChihU9nmuDJxdo7507SeV X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: de32c114-afdb-461a-38dc-08db2ef26cca X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:14.7243 (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: bcxmmA3Qmya3nDxit+UHjJxasnHdikLHUgSL9btHZ6a06deGaVXGt9eToxlN+ouX X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This only needs to be done once per group, not once per device. The once per device was a way to make the device list work. Since we are abandoning this we can optimize things a bit. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 112b6c541c47e4..0aac63742437aa 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -318,10 +318,6 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (rc) goto err_unlock; - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); - if (rc) - goto err_unresv; - /* * Only attach to the group once for the first device that is in the * group. All the other devices will follow this attachment. The user @@ -330,6 +326,10 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { + rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + if (rc) + goto err_unresv; + rc = iommu_attach_group(hwpt->domain, idev->igroup->group); if (rc) goto err_unresv; From patchwork Mon Mar 27 18:38:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667702 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 0010CC77B60 for ; Mon, 27 Mar 2023 18:39:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232488AbjC0SjL (ORCPT ); Mon, 27 Mar 2023 14:39:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232170AbjC0SjJ (ORCPT ); Mon, 27 Mar 2023 14:39:09 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2065.outbound.protection.outlook.com [40.107.102.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 339C126BF; Mon, 27 Mar 2023 11:38:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PRc/WgliuYKSCZmScvq9NNAi+K/9MyQoMNT1AXk1Xk/pKrxjzmfNZ0BWL4EQsCIKc7KRE5RmcOc9IvJoKN9RwiBNinRdi1g2aKKXHklIxHlRSRUSuo/p0SkUVEKfDRhkuVWl0m/5ADnqbfRS/f5Txx02U1L9msBGnKwQJSYIGHmkYCLSpwhK5YY85jKsK2a85sJQkcb36JoKO18fjqNHYtpxVpuzxetgFYOrodI0TBRpnU3OSI4DS8iQv+DyffOOFMb9CrZEpz5F/ZY8L7iclldEqSxN5dBD9bEvcNRfw286mxBHCY+uxdLAf013R+ZhlngjGriR7d+BoARpA5qfdw== 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=OqEdBCvKS3kb6/zXnbyqH6NHhzeWWtIOLCvDKB92FM0=; b=hL8JjWlkHYAawbRx3QgfBq40x05Y2cPzX0f/b9+W6d2DJIFc+alFodFBIVE6Y5qBKkTjeXmy1fjt/K7tghytN++VUdridpB05nXtGK2dX7keXKfHQRwQR2RiK+tleY5pwQemu9wefHokfwtXPpNeCynGTnN1w69GrC+iXIgjo7S2JEKTNeaYgML7N8gc5/tGAHF/jQrPW8Kc8Zznkr9inaG4qtBhTgQFflDr/fEMGxh0OwQaENscG2QoygHJPzCNC06D9lYcvulzFQCTEnypOWqBP2dMIz30yJiueeMoFn62tzzHDtsyg3PJTQq3YGvA71aZsVDe/QxEy+pKCWfkaA== 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=OqEdBCvKS3kb6/zXnbyqH6NHhzeWWtIOLCvDKB92FM0=; b=AJolqEhgrdtehTHurqgh8Gf8qR+LRL4m21bongVZVC8JwZdqlDwO4bSOvKb/G0xP4Zsxem6Ws71sVi93WOX9goiztBT9gcFcmw/TXT8PtD1ZmsiH1Wri4fJHkKqzszYkpe0aCVtYGsdGxCySXsS1geQFyQ4NysLKN/TB9gfkBH4uqvgyKqMofZisvpUDTt2rDBquQe7zvjdSE90wSenjacnw/cJQ5+Qs+GsEzzZIQC+DFpMUxKXSFT0Sgf7WTHHCgTXSTg0SG62D+xx5XHQFJJGJ5wpnDqkUqbQPHEctRkjdQlQvfUbVG5CGRqD9kYJY1L6gVpzvUyZu8lsy8ihPAw== 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 IA1PR12MB6650.namprd12.prod.outlook.com (2603:10b6:208:3a1::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:26 +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; Mon, 27 Mar 2023 18:38:26 +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 v4 07/17] iommufd: Make sw_msi_start a group global Date: Mon, 27 Mar 2023 15:38:01 -0300 Message-Id: <7-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0174.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:110::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB6650:EE_ X-MS-Office365-Filtering-Correlation-Id: 8a8b5eea-778e-4b26-9469-08db2ef26e15 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pLplYV/wyyIC6eHi5xrHxrQUBgIBVXWNTyHM5G98aGUUrn/IRASonFZIGZ6D86THFiMkjAL7iNSZzALYO5l7OfxpM3R2SSKqMoYRDxccP0gnRYOrxOK08aM9d2o5MyhSO/a725Q8mdjaL38KGvKukHlwpwPganc9cV3jgYg4Cluw5Ux8NP3ZaeVoZAN1q1qe+KLIRyZo8G19VrFnHlbCtkb2r8ZKTdXVGBAx8tTw9Cs1/CkZDw3Qo1xoLckvRY83Y8eHbwQep0B+6NNzpleo8EnUtZtmRCg/wZUkmBUvk/CD7GaUVmf9bsR8+KXJidgxIjjaQvftAV3CDNF2/8hADjU1yXwHfsdT/AhWaZg7KqhivxTUrIwSCEDYQiuFpaGonkM5dWgAogLrZEEGy9nrKlopvWSdvHv+BdVA/6cQdmjfCHfEtQgCOzwRH+QHWCXUonWBrPMKy5gVEwx8ZnyQ09tnjiFhTBGTU15BTnMWK57Y2+dGINWmUCdPL1VBZ0kuI0IS63u0br/IXDq2vTrCTb4neIxsWGQskAuHFDMz55y/RqlylTNB+0t66+rF89rC9rWXYEwqXEH3yy0CPvlod914MxL9Dydsm3wwKaIinak= 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:(13230028)(4636009)(396003)(39860400002)(136003)(346002)(376002)(366004)(451199021)(36756003)(86362001)(26005)(6506007)(186003)(2616005)(83380400001)(6512007)(8936002)(5660300002)(6666004)(6486002)(41300700001)(4326008)(54906003)(2906002)(38100700002)(316002)(66556008)(66946007)(8676002)(66476007)(478600001)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tUdKF0fCCXR+tta7FLTPw3+ayzB0EuqBoAs3Z1GgOPQfmLcc0tNBfHV6aWMTuAFiRBB5nNyazW1ZnpDjbJ2csSOwzZhhqxW8gV1B1WNwc4f/71OA7iZjmk8pX4wSFz+Hg/qfTSgyLq03ogrxxskWz6J0IXapfzCheSbJflnjqIl50vRNqfmtKxo6NViyXyhaYbdwgx4LPSpDy/MomjjpURqFjKs7kMMjiILQIF7IMW54SbQlkUyomXZRirLKz4AxhpT+Qptq0ys2lgMoE1dnozz+a4CWGXc/RGoOlGcnLN818cKcPri7HK4m/EULBaOzMYZqqmjoticlt+6HKz72UnBT+6zrw1iv9PDskRh8j6EXuruEzYVYyaPx1SOj6YXGFSqoBleIAv97z+hPE+D5nlq9HyG/90LWkWViBpX5yQPi+R5y28qEgXaD/7n8TO/4lxzDL3/a+0709EIVpvyxbsMfSoyK5y6J0ADU/nh2qQQe0+xVpAzdnkU/8ze865y2CtebDmEcgnGonjOmLPX9QuYc/Csv5Hu7QJpvGU71oa/zmq4knK2DWEvAWg89cnvQA2sqnHXvbiYP8ZuWqhxH77e7RVqCMp4atg9aY4eGqK9OTLJWAI3I6ZrTwahCddrhXvxzKWRaiRho5evfujnw8SKQqxkg+2ei5CD0DfLToUBKeJm2D2B6WIIQl3CCVi6hzAylSj40iLhs0aj/FuMltFMfhz9o9f3AOu0o/6aaoRaKvXphhFQ1M0d839L/wlNzJFaC3dX0Wmdm97CnSdxA74hY1ksHqSOijvx2M8b6KP+N0x00fa/nwXV734xIlI7RZ0VhPAElNWbxx3p9dPCw/IrpcxTBngm4pekOQUU+cmi5Jb72G/z8L6FoUlVjYKqTtw4kr++T6fo25rcEwBG9rWZiXRFRNW1EQnKVqDDcyVFa71lwBY4FTRC95RChwYuAYeEnk/MUCCCawChp01q9KFbjChbp6tBuTcuY6KlF8lQzT7kvbTYKFYdjJBOfzYy8FddNGO8B/5mer1OjwWDhUwstDzeY1MVWBCSpPNrJyIpkvD8fdlp41WteBlNd9qfi1F9EKXVqVGAVn50Uc8X6JbLBF9qvVuG08+t/H0+QffFFZhfcxIO8g3SalbmlXTBD+P5zEvIct8KHh99vhOu/BxyeTFmO9H6t9xQLsxePQp5QWgnRDfjFHVqhAue3g8IlMog8lBJx5RytoX0GapQ/q68eBxauMCwjkRnipAmkvrnxBD258sgFXe49MvogUBM32W5+6PiKTf5Vj9vdFYkNS4kJp9yIgTwXV85A0yznf/8IMSzo+zEoHggGoHtxnfZCfJsp4BfrWkb8G602xr/0qzRD93K41pIekntclx+zhvONxmWQ4qDKOqqLWbH8l4KtC0n4WY3Aju5Ka+PqgfhkHqH8IEUzzIahFw2KJsxAZtzwdKzLAofjIv/kq1usgcv9t1mjQOpRFUCBRl0P0+dR5TpQHEYYyKjtW53xFEz+Rh7htRZLrS3CxKaSVEXcBopinBSfbcyl2YXTwEB13QJfwv/bp0KCsnWdLurYYifnGyzRNZF8hhsYGMcCQ1xdKUD1 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8a8b5eea-778e-4b26-9469-08db2ef26e15 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:16.9115 (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: KFXW7BJCW1T7c5DlJVdhKYjUPBOa7So+QkHMw411g/Dtv0ZcyV+81fzYF6EApnIB X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6650 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The sw_msi_start is only set by the ARM drivers and it is always constant. Due to the way vfio/iommufd allow domains to be re-used between devices we have a built in assumption that there is only one value for sw_msi_start and it is global to the system. To make replace simpler where we may not reparse the iommu_get_resv_regions() move the sw_msi_start to the iommufd_group so it is always available once any HWPT has been attached. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 14 +++++++------- drivers/iommu/iommufd/iommufd_private.h | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 0aac63742437aa..866b28d24d4ee9 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -88,6 +88,7 @@ static struct iommufd_group *iommufd_get_group(struct iommufd_ctx *ictx, kref_init(&new_igroup->ref); mutex_init(&new_igroup->lock); INIT_LIST_HEAD(&new_igroup->device_list); + new_igroup->sw_msi_start = PHYS_ADDR_MAX; /* group reference moves into new_igroup */ new_igroup->group = group; @@ -249,10 +250,10 @@ void iommufd_device_unbind(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_unbind, IOMMUFD); -static int iommufd_device_setup_msi(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt, - phys_addr_t sw_msi_start) +static int iommufd_group_setup_msi(struct iommufd_group *igroup, + struct iommufd_hw_pagetable *hwpt) { + phys_addr_t sw_msi_start = igroup->sw_msi_start; int rc; /* @@ -285,7 +286,6 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev) { - phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; mutex_lock(&idev->igroup->lock); @@ -313,8 +313,8 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, } } - rc = iopt_table_enforce_dev_resv_regions( - &hwpt->ioas->iopt, idev->dev, &sw_msi_start); + rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, + &idev->igroup->sw_msi_start); if (rc) goto err_unlock; @@ -326,7 +326,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, * attachment. */ if (list_empty(&idev->igroup->device_list)) { - rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); + rc = iommufd_group_setup_msi(idev->igroup, hwpt); if (rc) goto err_unresv; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 22863759c3bfb0..7f4936cf537be4 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -267,6 +267,7 @@ struct iommufd_group { struct iommu_group *group; struct iommufd_hw_pagetable *hwpt; struct list_head device_list; + phys_addr_t sw_msi_start; }; /* From patchwork Mon Mar 27 18:38:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668438 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 54E27C77B61 for ; Mon, 27 Mar 2023 18:39:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231375AbjC0Sjy (ORCPT ); Mon, 27 Mar 2023 14:39:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232664AbjC0Sju (ORCPT ); Mon, 27 Mar 2023 14:39:50 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2087.outbound.protection.outlook.com [40.107.95.87]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4836444B0; Mon, 27 Mar 2023 11:39:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N3HRFr9y7+7v+4/SLmYoWJifszhEeATRSFI/6iTxXbrESOZFgvug2zaElj8/MOjd13ML2zCJqD4gJa0aZW/A0mpj3B9CogbNf3qiVlH9rnOr3R3DMgkmS1ijuKL8r2qBcinyVC+bG+zXlX+bKVYGO/qyl8hdIChER1bC/XGcHES69kXu7Ae4uxxQ0XJIhje8/g+hLuiHTKkhn4sok4S4lzF2QenKfp/cScahkfOTzKETwit01GCNV+Uk+Z0SWZVn3vtZK+8bvvMsUKGPZQApghH537/uIuPEMkD6DLRpKmvK8REK6VfUrLqEJlJXYCB5Yyhnss5uo4j/94tN+krPhw== 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=lOCHnjvnEOfCJHUxzahLBw9ida6GQFd0G5kWiFXbXtQ=; b=LbHIhn5fmpmq84txhRn2ruTVq4Bb8OHGhnZ79deqzPm9TekmH75XzOzGMPWk5f+TqJnosGStwoZ52igusXVh47Ben/Vjhx7Y5YzlHXZcF/jHCBkuolVrnliKvLFhnSVF7jy2lV5M+a6rloruU1fUUStivPsYKkkeU5mS2PPKlfmhFED+r+KinxbNxXiOlzorRPk4atF4OkTdO9VLN9aWgeeQi0Y9w5sl05BKZDkbQou2rY9+/lI4H4dbF1gVUKLWLJqxiPKl6iZWD9wRRtBh6ef9uWEjWMWvDWgpf1CpnA+HKGQzogZY07Nd/w05jjrSFEPvaRoCzkVsEFVVN61gGA== 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=lOCHnjvnEOfCJHUxzahLBw9ida6GQFd0G5kWiFXbXtQ=; b=MCshqETPTZisGayM37d7fyrL00dw8QHOuBxy/6oySgahqm5TE4qivYftfotYRykZeDQwRXkhi1yr+7F8bDvZB4jH5yCEdIRMWQyZkN2BD2wsj6HqSAa7zGC4gh6vQ7BjX0vYfLzUwGkqCkjtr471fJTyTN/rKlTKakIlP97hBBQNCQJ4KanDEjVitAUt+Ai9QjjJfyg0niTptQbBgoTVAeh22x4zDhvZs6f2XwxiIaF2/atI4PUNFAHGGeAaoNKHm/wvPqjV55Xpcanqfsfyb/Q82hQTbalzIms+1aGxuf/PLF76jnNmyTS+o5JizqNy8AhURBgL1jr5NncI7rGvKg== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:22 +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; Mon, 27 Mar 2023 18:38:22 +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 v4 08/17] iommufd: Move putting a hwpt to a helper function Date: Mon, 27 Mar 2023 15:38:02 -0300 Message-Id: <8-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0476.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 54f13032-d75d-4bc2-38dc-08db2ef26cca X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cmoJ/ekZ89jSJDHhTTtrjR9BeRxYCH5661w3DSne5quoxcXYCiapZDoz1nCksPOXL4C3ZsOBQKAsoDw/MvnnOCpEMy7D0bYPgyQ0HxCZ0qvH8jfHR+cvf4G2PIiFblBbDQWw2HVpxoiCFviL9jr6Th0OPGOSlhiLSP9vZlfdxP/ITKHd0O+kJcZ6uBswKX+ibBgg3dubFahls7rOMG3BJsFYgkO2Svr2WeIzmwWVt92XxBv44WIFlUzOfqURp5OmxWn0ws9jzXOMpPSRQY+6xHTfIkATSxNOxXQTJ/yHHdTDqYqxEy0P7vkWwZOEUGr6yawCAj8g84OVjn8S7k1ppXNeedVaaPy1aSZp83sOZXVN5fkE4PGfChnNtn+RTD8JGNljTmbYo1pNCQoJhsoZKyt0RF7LS+OEOR6WfoNOF8aU9BFY7hq5s4uAyiGzuzfn2vn1MBm1iSiPuYdC5LVFrGVbUYJxgFESrKr2UNhtiDCi5KG0q89d8V6duydKzeCTMYGqHk8Hu35MHMIt+wyAbMS1zYBAqt+jMTTg9TolcKpH3cEdyZ4zMBm3XMh6ItrVFDYm+Z9Pwi05Czn9PxzEJ+QXAt8i8DiZTj6aI1GrGfs= 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uMCvlglNK/VKzQNtaUleaKDkFA4lDOcFLN5cGMkDVoVT3RrRs9rVIwinwdagvRaI13BJ8J2IRLzLYss88nogwFQ0UlhrHq9YD/OnLTUIi5B9gQNiCfkt3BVD2cnMy3Kz9d20ZSTth130P82mhcCoYOb2ZU6FMYQfh5eKLm6bQT95I/If92aOll2fNKXLLXBleInN8VA0DJHL3dWPJWJJK1eYCVlSGDlq9RWrjjeLvMt9yljqp6RiUB50eu83C97Kd8aUMm/+ikFWfLoC7EC67BwvZbkd+BcxHukpfuaIkNOR1e/IjxmeTGXyl/l7gXkaVMs2C5EJIVebuTrj+cFb/sPmeY5+BJOQs+SDlOlVbC9YxAz+wIQXseDjKA/ylK+v6lYpkqf6pi+Z9DigyvotrT42Ghh9n8D1+OIvRh5q0nZo7gdO6Cx1e2AYQgv7dXvu0ClRt8XlC3X+2JeU1zXkbXuU9fM4D+MswSFiSJ3GmtE9qYxkt5AY8c4EJRaB/impVwiW2SLuR5EFgdjhCoWxPXeqzXE9BfPrTzNBqtrmPRuwyJvUU7kAu39KiQZyYePMItooJmo/EdkNOLsDJt4vCwwzWYZNCMd7GW29o1LWjJ36GUltxYpon7Pmn+aB14vEvbQpXxPLa6H2Y+XK850BC+K8aW0x5eqrADcrDo3thwbe2WLLySuVGZbjijnfrt34U2iWQv2ptlI64nLWtAfPuwGsIX3xYn10R3lfZytR/TsdGECQpdDE7zG1OyCa/umPBDTlP+PzOMGE1yg5K/yAnIt6yvVzK64N9PH/3/gpkimv08M8WOxO/aMt0wW19pIn18KaW/j91PfWv6hVTCpH8+VEY+QXOq+NmMiAsvvBd+YXRA+bJUfDwjY8r2qBP2v8mQSmuCvRDECRIxSloCU1i3yKI5AuLgGt9R12YkrfD/3geD0oLasErgzryIuwnWwdzRn49gLtWXxngsgJJwAbri/1t+W/s+rain2LJNR5fVy1BnjIWXp5i11qgEHWQbXYyub+avvNmKshzPVscci/HwoNZsuHDFiY3RNhZGd4P302JvgvWftw9JAcU4AcTFJGZF4492X46AsFEXEyqY+jl+zm4eIjpIOTx4D6qpFR/4s8HBCs4WBzJxvdFmttWU2wz42eKO3OaY0zUbUva74EfqOu86JGs50zHEhAT/J2tNhVyIFmCGPR6c+cd/jlPOE+Wd8W7F3Fk6Zqhr9ThTdPbwDUc3wa8FP/i5QlGsshattaIL4r2wXy6uV/KwD1ymI5D7BXYixGBceAmNse7ikRFXdMlNxN8fygqXwV7kwpjqz8mtqAqX2x+GLjCWIr1N7f0IpwD93azzef4I3RCvrdk5WmTDUQ7N9lkv0Cy1PdRHcx3Hnec6+aylzLrVCnje3nJwdBkiIZ31uePyFKOeFkhWW7WCt96qZsI3a72aN6PLmJcmEfoEoOgThFGOsIu+W4WPwO+v+AdlBrj1KXln0CDG+YG0NQneT59uLEyA9u3+ILWvqNRphXSn8Wb7J0jULs+SbMT03p5s3CJ+14e+kNoXHT4Fym9nQepKkD1wzG2vW779xWEzUSUqMfIz8AdFK2 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 54f13032-d75d-4bc2-38dc-08db2ef26cca X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:14.6774 (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: Rn64BqB0JqvcLYCKPkYBuPaIUrexjLOxkeqi2Uo3cSfhqlqP3Emt74eW1rTlxCiH X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Next patch will need to call this from two places. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 6 +----- drivers/iommu/iommufd/iommufd_private.h | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 866b28d24d4ee9..a77acaf8309167 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -485,11 +485,7 @@ void iommufd_device_detach(struct iommufd_device *idev) hwpt = iommufd_hw_pagetable_detach(idev); mutex_unlock(&idev->igroup->lock); - if (hwpt->auto_domain) - iommufd_object_destroy_user(idev->ictx, &hwpt->obj); - else - refcount_dec(&hwpt->obj.users); - + iommufd_hw_pagetable_put(idev->ictx, hwpt); refcount_dec(&idev->obj.users); } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 7f4936cf537be4..8bf053f4d4a9ce 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -260,6 +260,16 @@ struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, + struct iommufd_hw_pagetable *hwpt) +{ + lockdep_assert_not_held(&hwpt->ioas->mutex); + if (hwpt->auto_domain) + iommufd_object_destroy_user(ictx, &hwpt->obj); + else + refcount_dec(&hwpt->obj.users); +} + struct iommufd_group { struct kref ref; struct mutex lock; From patchwork Mon Mar 27 18:38:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667704 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 5A8DBC76195 for ; Mon, 27 Mar 2023 18:38:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232287AbjC0Siy (ORCPT ); Mon, 27 Mar 2023 14:38:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232521AbjC0Siv (ORCPT ); Mon, 27 Mar 2023 14:38:51 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2065.outbound.protection.outlook.com [40.107.102.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBFB44498; Mon, 27 Mar 2023 11:38:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=b+JNord05zj27bWL8KStWy+HfRVrERUHGIpg3RX9X7ghFNtstJFzFCcuD/vHsPXhulpl+tu3j88Xc880tcvCkQ59y2Qqw5K9M3KHHQjuyYmY4TOyhaV60NYVFhVVyc9I4m3yHKrcHVb10CMmG3zhKQaHPCobu/PJW3yFHstVxNWWmVVh7Hqb2lPQRd203q9dOiHVDViqrNnuKjsEssMu1N00M+tMM2PRwXHqMd48w+6+bcOUwnCNCtAd0741dmdVHfZcOV+JVMcPzA0KOgHNx+pEPYtGT5McIA4HMQj5u857PeUUDJLKtziv3MvrZmfeDuD/Anxu6byDZ3xlQ42W4Q== 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=ph60WDgI8p43IHe41JBnY0V+WVnJzd2+J7jnaoas5yc=; b=AXz50o8Gt+fb9yxi+fGbMJxRy7KWtBT4LzDj1cI44oZwnl80nNR5yPviwmmMsb7ujfLeqxEKMpA8tWm396SaVrfKLUBOqojgD8tNd5ku5zEqiyCZhrBvXJAvX0ckpn9sSOsPCNyXChxTBNTLm7gA1mAnN+hSZUfaCY4vlfdN9xFZlB6Z5lNESPDb81BbXzDhGFc+BLbQxYUGrJLWnTn3mN896lQZrSQeu/+TRpT0YqeVPDaDhj11hrjVPTFYVAnDbWe1W7ZuAmqIWSSHAEOcOQkvEWAHWarvPyasZyry70/ebHSF8lRjibjwFZUvg2ccUr0Qd9YJFDwcPf8N12rcDA== 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=ph60WDgI8p43IHe41JBnY0V+WVnJzd2+J7jnaoas5yc=; b=P6zdopBHTcszNc+MMqbFf+b8hyVgxvlPrFnXZ4MwZdmCsYgYcLjVjfre3rBNywLM2gR0YFBOadoAeXkZXuIn4WlEa6yllu5HdU27CUpqbg6qtMq0FqBa2UgJsl7UPSXM0na0euVGbFeKxVWLu2xSfFH8Vynwj/wnDPX6IIJ0IkDEXqm8GOL8lAfHRe7otydGPOH5MojqWRX3QS/YOB1s4XrRQGhjExk2A+l5ZXbi218xUkEzxY0QRj1fHzyesiSBpd+i0y7I23LCNxd40ujZv+JJSNtvEb/6fkG6TJnF5PuxPxaOZSqX0vvBOyApDhRC7P/vWbRer5n4IgcdFIzULg== 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 IA1PR12MB6650.namprd12.prod.outlook.com (2603:10b6:208:3a1::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:25 +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; Mon, 27 Mar 2023 18:38:25 +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 v4 09/17] iommufd: Add enforced_cache_coherency to iommufd_hw_pagetable_alloc() Date: Mon, 27 Mar 2023 15:38:03 -0300 Message-Id: <9-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0473.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::6) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB6650:EE_ X-MS-Office365-Filtering-Correlation-Id: 85e928ef-9438-472a-9186-08db2ef26d4d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RRjXDdzOVKsfUkfJogcCWE4KWmHcuN2rJiyEQcmWSoxRgt7WsOCPlOR1+JBJX0gQTNm8QNk7/l3cjWhJRRp/xc60c0/Bf+BnENYKgSNmk5DGV46rfcpprQcWp0FuElRooBCP3qhu7u+kOztD1VdEJhStDTMrwaDQteTnUfPrED13bYZRobnWxpfNpmAu8b2Fhu56b/cNxLk43BNpvD3Q3vTXtf8HXYn5c7liOGWvWAxUv0AwuyQifObgqh0ccaiIT2aLBHi3xQETsj1i3/lW1MuoZZNR/JWB7LsQmZbB3eEdKUrc8k5keibQHHJc9MfbYby8Se+u7kMh6Os2oWQu+0wnoph2mbx5Uv0heJmC7F6CaME8Pb0CcaIs4ar7LeY4qIeKxYERh1g7oiVHe3/lmIdP/XSsqgscKl8fAUtYcmyHTPlmmMjasEdLmVEcHLKhvKXoa/uGNRk4BdHumqOsthZ4y6JmeBF1rs9umLPVAz/OODuxR80GKB1ir5rN9SjNt+61Bo00MXK7Pz7QGUHo4I+iJQo4+3ZaZTK6BC5hEtA9gIgREuh95gl8Xr3LRu6lJoQvevfPgdUCNDuZ+VtFb8bIgSMhPUyB0TVY1uEoAiA= 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:(13230028)(4636009)(396003)(39860400002)(136003)(346002)(376002)(366004)(451199021)(36756003)(86362001)(26005)(6506007)(186003)(2616005)(83380400001)(6512007)(8936002)(5660300002)(6666004)(6486002)(41300700001)(4326008)(54906003)(2906002)(38100700002)(316002)(66556008)(66946007)(8676002)(66476007)(478600001)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m3+QK3tkM7cFT7A1TlmAVbITdeP3VIkm1mdWcwpFeeKYE/JhUoJhirQKPUhAauuc/hkwFSjZ70CZ0F8aq5pGeBXVpEZXqOYsqapCQciyqT2eovlv8WpjUxKvXTaI7fI1C+ch/pPbOblzWTnVAXIlsyyA+M/Ym6pb1aXFGpLTniYv1888hOzxUj2wKGpwfnQCOeClE4lx4FVp8vKTd8cErsRwH/nJuDvy469A20JgELwnFUmvAr0Opm+vlfWxf3rrG9UnuFKR8twWUcbtEDtL/HlkHXxLJ9gRiK2EhNS8tlahZeV5L7EHs9jTeu6gq4Gj/UrRL+fBc5lQu237LZ5Zab5ZPejNbg8N6fv+Gx0kCU0i2OY4I7IIKBOui3vubcEUxfMDMuftc/RKSMykqh12FsQxJHMHJLLO4mJdnN/b/HJM76gTA2qGW+ZypmfOfYTriqnpx/VWaIWWewRuPe5zMyA3Ah+/HS+ub+ONcbkvm/CEVwYKoXJ/6hhhoTfhIsEv824zL6RI9EtaKMNma0Hb7aQd7H6UH+JsOTCz5A+LWH2gB4XxggC/TUl0qm69kvC8tpfbkoacNmFGOBbWJCMGaBYXejy4ol/a6UVYKiwoADdwuwpG+remuWxRpjaXSDPdnRcW7XT8M2X/X13jXzYhlPrm9qqKzu9XRe2Y1J3h+bVxybgqgz/OltXz4eFNXP3ZZRvgbnPEIZPX9cuCwUoGR1RmcP+16KDe8cEQes7J57cXOx7aKWg8skTfECULgoqgh6gaJcx3G18nsH6lbQET8iSDwrxfZpwaD4BPC+Eiw6ij5AgnYVC8Sqgaj5NENT/iwkxzPJi53C4PRrtttWDKOStfA5pl4ub2Dgl+AqwPkEIo/iPlEbhLByvoXXQFuODXDgzQcDXIrs9nqXdtQgsA/sy6yNcjJVkmHYg8OK3uE0mJ6eGeg5G7eG3Aq7BoErF6Je4uxfaDeUG3jQzMbIMZ5NRNII2L1baPHSQoBVYUhZY9CnwaUsjLPcVKdd6TAuGDHwLPXF25lO/+KahET2fx/ILdWo4mUy9T/5cnCHcwPg05RLQQLmZWwbxASmDhY/hsVMkbJYLK7u+IohcQsInULgTFwWRQjkRxYwYkCpT4g06pe6SJg3UfgSsASgAvWf214duvamc306N7fkh7tTC8qS5lzI7OFQKDZ/QSDOW7H8vXQIy9SPm0ZgKWLS8RG6JLdFoO0kd8Al8slM6sEnysa0eJrCuFzLCcQ0lD+GwpmZ+LUJb8GA/ilzDYr7mJwMLCbGzi9mgAgJxkHP5asln0dPTijcJHP7x6M7whRlO/bfcE1NsQR1JdT9cGOGIJguUvnVIpTuvO6bks5Sc3V5o7NM6XcmuK+2yC9pdd4+/9V7Xw2Vh2VkT7ncVnrFs6EcOfDzQlvJtCKsJT5lUK2ghYnfduZ7e+EMVmxladYlJWSuQmFvSdnaqjzYkU7mj/3+1UQSsipXrhvL9GDKaGOVgfm5iWib/XYEyL90xscQQq1mhTUBGH6PZZGnW/1vnTRhZswJgY4OQq3NeckFUnOdKE/Lh0muFsXSe2lWg9t1R/pOLITUcAP28PdvrtEb5Ew6wy X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 85e928ef-9438-472a-9186-08db2ef26d4d X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:15.5680 (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: fJqHL6SWwlb0/4/rNnt8sXzT5oEZq97HOd1Y6ojvmBTKcMzmojXxJHQdDFL6j8V7 X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6650 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Logically the HWPT should have the coherency set properly for the device that it is being created for when it is created. This was happening implicitly if the immediate_attach was set because iommufd_hw_pagetable_attach() does it as the first thing. Do it unconditionally so !immediate_attach works properly. Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 19 ++++------------- drivers/iommu/iommufd/hw_pagetable.c | 27 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index a77acaf8309167..7008cea5158166 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -295,22 +295,11 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, goto err_unlock; } - /* - * Try to upgrade the domain we have, it is an iommu driver bug to - * report IOMMU_CAP_ENFORCE_CACHE_COHERENCY but fail - * enforce_cache_coherency when there are no devices attached to the - * domain. - */ - if (idev->enforce_cache_coherency && !hwpt->enforce_cache_coherency) { - if (hwpt->domain->ops->enforce_cache_coherency) - hwpt->enforce_cache_coherency = - hwpt->domain->ops->enforce_cache_coherency( - hwpt->domain); - if (!hwpt->enforce_cache_coherency) { - WARN_ON(list_empty(&idev->igroup->device_list)); - rc = -EINVAL; + /* Try to upgrade the domain we have */ + if (idev->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) goto err_unlock; - } } rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev, diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index a538ceb13a6f22..c5c0ef9912a13e 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -25,6 +25,20 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) refcount_dec(&hwpt->ioas->obj.users); } +int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt) +{ + if (hwpt->enforce_cache_coherency) + return 0; + + if (hwpt->domain->ops->enforce_cache_coherency) + hwpt->enforce_cache_coherency = + hwpt->domain->ops->enforce_cache_coherency( + hwpt->domain); + if (!hwpt->enforce_cache_coherency) + return -EINVAL; + return 0; +} + /** * iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device * @ictx: iommufd context @@ -60,6 +74,19 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } + /* + * Set the coherency mode before we do iopt_table_add_domain() as some + * iommus have a per-PTE bit that controls it and need to decide before + * doing any maps. It is an iommu driver bug to report + * IOMMU_CAP_ENFORCE_CACHE_COHERENCY but fail enforce_cache_coherency on + * a new domain. + */ + if (idev->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (WARN_ON(rc)) + goto out_abort; + } + /* * immediate_attach exists only to accommodate iommu drivers that cannot * directly allocate a domain. These drivers do not finish creating the diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 8bf053f4d4a9ce..471a3fdff1e0b6 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -254,6 +254,7 @@ struct iommufd_hw_pagetable { struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct iommufd_device *idev, bool immediate_attach); +int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt); int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_device *idev); struct iommufd_hw_pagetable * From patchwork Mon Mar 27 18:38:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667700 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 0B67CC761A6 for ; Mon, 27 Mar 2023 18:39:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232588AbjC0Sjd (ORCPT ); Mon, 27 Mar 2023 14:39:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232583AbjC0Sja (ORCPT ); Mon, 27 Mar 2023 14:39:30 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2087.outbound.protection.outlook.com [40.107.95.87]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E84126BF; Mon, 27 Mar 2023 11:39:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=G34DzS2+gHayw/kZiJNmwg/GaAVtxI1sI6MRqn5cdBPWW/ccpjVMS8CeQQpd36wjhCUOcIzOaTeq+7req0T+40uK65925c7PSngvKRtz8G0C5rLZv7auPqYwr3ywm1FHZQoh3EYquMX08884gaIhyfjevLnHX8kTnV+T5jjNuxTjz2oTVN9bvZoKqdKUbfid5qFKyvHTm6CbEx2a2QFZ7YuIDabLCeisUj7QBVeyCj2QOfIn7q2XQ+CoNPC7G4L22/TUTtdxKQa5fzHu//jvsCn/ZswWt3VQnX+QWjObOxgbi5aU0cPfqFniiSLqS/ij6RXLig+W32WGMQcQULdydA== 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=Y6ratW58MvYNk3140JJSanR+MNsF2UIbkMWOecVyehA=; b=LNWPLo5/dbT2HkdZUYel4kHFOhaN4FtBBelQpwwlJvM8qPWNSQTdaRvRiJLEXKeh4kJg+P+GjgjZbeHtaPVAUFmDLAXPOyhafAAUD5tU7Ov69OmACd5z0CXi6dFCufbVU/JA9WYfoWb5/SzfdThfGnDpeWthyxUdRhQdESb0t4X2HB2VbCZPBMJI4Pd6LoLfFvBR1wf8/An2x8OPucJFXJKOwjzqWqURwDyj9EG/3MqThHPgyt6VOHjtxdUpTzHsyWFAkdT2zpYFPh5nZ9njRww6tGVoWltdesiMrZ7igZ1EgnkZsfM1ZrLBGHDDyJi9o/T7WMBDpV/vlHJIt/XtoQ== 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=Y6ratW58MvYNk3140JJSanR+MNsF2UIbkMWOecVyehA=; b=QFzgskumyzn9zlPJRVPX9PqCfq/MmMjBEwrY7aseVk1gAER2i5JzS9pR5LXih/FOqdhEb9uRI6Jst+GyjVAI4T0k4F344MRt6Eww3IwJHWL+nv3Yza63NOOQWrm9avaYvDgFasxBHYVVDerV4pdKn1tmGFrxTbek88u0RQJA2I5J16PP2yBy/AG3BmvH1V7fZSDibYo/h9RqjB6SpD2Yu5pCBZj+RTr0wQrwesdlDfMTpMToKxXlAn8ecbSa9zeK2lwbFM+coRcll93Pi9DlI0uQhvt+L/POaLGHgjfNUzy/xZeJpwmaVxhp2stx7OBdoSuW8ZHXX5u3mfSQ0gTq1Q== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:19 +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; Mon, 27 Mar 2023 18:38:19 +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 v4 10/17] iommufd: Reorganize iommufd_device_attach into iommufd_device_change_pt Date: Mon, 27 Mar 2023 15:38:04 -0300 Message-Id: <10-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0470.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::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_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: eac66da5-eaa5-4724-cd78-08db2ef26c57 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VniXnPUGlYJo0jRy9Mz4CBwEbSM32ayZojOixohmrSXpSiX/B+/8SOwB3il2VMpyNpUbUnrD+jEVu84PszRl67mmOtEFaj9KMwEXtxu4mHdDGgONMhQi05tvIhXn6Mji/miGTN74r9gr94P9p0rr4KW+PpUfxPiKzIAL3OlDqIXhC7Y3UOvKkXlV1yDnOS77AdxNxWA/74TTo4ehWgtH4/5DYkPCzUU6mIFsPFraglHAJOlD0kyF9KUjjxnKaSc005iqPMXWL+ZsdIzs0fzRk2M7jKWtczSQIkpSKuSw+8KSEyX0mSFjz28IBCV06axxB93jJMz5U4qVuB5YlolUU3kQFksRt7CrcI+36ctXzdryD3v8ybvj5f6ieHN4G7a0Bn5zxCjkF+oMImCoA2ft/1gOnfL9tLGcr/kgPeS8b0GUxPInKMB6pCy9GA9HbvQjmGsZgtTM3+KBqiXvjlvTROZgaSxmZD6Tv/j2RVXoVezGS+D9zuiyVvE6eaXqPvlrHO3oxzv2Dt9veCFV5m28kGucm46VrjkpItgMQy5I56GSCddTLIaU1PRFg3CU8gl5 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5z1ExAPjLPhEovcwG/1LEBnSRBWPurc82HyUOU0n9AqWADTv8IIrd0khN0YDWW/rX8CKct3nQfP77EXGZMTEEURFKPcZaPrkam5ryA0gfPi/89/KRuQYAk4ikeT9kzGXLga3QDW+DgIGgZKfkMRsJz6pKKLEZrRqaNPUtpTSb0eLwo98+7JraQGmG7U4blS+c0NiLkrIbn7v7Tp+AWCJ/1H4HUR1fx+lfgjylSxfi54Eb2zJ1aFPdyouADFa/vSrQWpMfeUpTpb5ioFjXQwwwfQHdzuR/65Lqxhhx8BbiRvCuI0NqsZVxNmizHpbKwXAwTC4STvLcSnVve6tTsdNzQqsA9A5QcJb+CrO6yJL/W7WFM8KBz4aydpq2UwJ/f1Ca149Nli5foiWaPkBUEbmgs8rQIEnurudjkyN+xoRl2h3iShfnNX61sAn8jjTpL0yO2ofmuhOsVfWM3OUqh8Ef/QSXTQfWf8bUF6R0QNUES1IZxe0R55LcOddtbwnPVO9REgJA8MGEK0iO40a5pUK8gNONjMd74glAwfVKpDQWWID8m9WzzbXOSai8uCPNqlCBdXXz6MVaw+MD+LmuxEbnTOfwc5LSj8BETwBt4Lp7M4aFtIMH2DMkUkpbwylRKVHqxERoe3q16jNZJhkGJg09LpILlA4rPySq6ngz6n3/2kDB5myBmr5/6bZ++qDs5QP5k68JujV/hfV8SKhrWqN4ORFUegQ49W6X3pMggtVQeOETpthr8buoZQDKKUW8psTFEtDDtm9Eoo0JFtfwtX2XotOfTNANM1Noj+63LKfEqj8UDyegsxQEE/gGS6Pm/7hdl4GWCA0ryo/NV6ucz1/XUjHd7k7+ptP9n919jeEdKfzIIkOET5gtfAQzzZVIDkoNZUcbilCWzGjkYqSD6fazdG2k/SDU9FruMwFN/dj8xBR28KGzAiQ9VoPvpg7jOII41FWLWfLm/pQX6ZGlUioe7ZrYS9kP4DqmT8LRR0eyQ+4Nnr4MY3Dm7H33XFdQamiTYlJ19p1QjguE21AvSqS/d7OnsTicP89aNM7uZO3STjF0wnEtMy/fTIhFcia+ocnlNUzHOykCHuKcogEcg40QJe4UPtNytqpJhba08Vmu6XqBGZmB6exhs/7SabaYoqdVWVUEHcNtVCFmwImzt9po+/9UfxbwE++uvGkdmm9kQHfAHnz7uIQmclr3zftDhUutQF0z10mx9Z5IwJh6E0SlG1iFEeMdhWdkTSdcxVAEyCwz70GZGbeseWwRyF3yyjskM+o/uQQT6rqIPbjy2gXUg0yzCNGCG3BkwHDUczGzhICzSQxDVub6YS9WENdoFrknZ6YR+OAWFbkitlWfNCT/XuwhqJWprZIUQ1RNuahB/p6UDPxzCHhGA2FxW8WCaijqlMAC8ZSkDlev6UEi+QBuOUP2wDyxetl8RUxVGZAloAdUGReirwfvw/o0UXKCBjeNLp0OcSvGnVFlWUqJM5+QbbbmeJA87M9RyFcnr+fG2IH85qhWgMQOBYw1TP8UqjXjRpyyN0USIpE46Va3Uwsq+5CQnPlQxOM+b3ZCvHcIvl8FShMPNrwjySAPasq6i/h X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: eac66da5-eaa5-4724-cd78-08db2ef26c57 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:14.0213 (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: z9OFR8JZAj8nVHnHSnYC744YDNgjBTd1KlJsC/C9uhijvLAMCfjJKHUlW5DcOima X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The code flow for first time attaching a PT and replacing a PT is very similar except for the lowest do_attach step. Reorganize this so that the do_attach step is a function pointer. Replace requires destroying the old HWPT once it is replaced. This destruction cannot be done under all the locks that are held in the function pointer, so the signature allows returning a HWPT which will be destroyed by the caller after everything is unlocked. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 141 ++++++++++++++++++++++++--------- 1 file changed, 102 insertions(+), 39 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 7008cea5158166..f931d3b95d6d0d 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -352,16 +352,41 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev) return hwpt; } +static struct iommufd_hw_pagetable * +iommufd_device_do_attach(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + int rc; + + rc = iommufd_hw_pagetable_attach(hwpt, idev); + if (rc) + return ERR_PTR(rc); + return NULL; +} + +typedef struct iommufd_hw_pagetable *(*attach_fn)( + struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt); + /* * When automatically managing the domains we search for a compatible domain in * the iopt and if one is found use it, otherwise create a new domain. * Automatic domain selection will never pick a manually created domain. */ -static int iommufd_device_auto_get_domain(struct iommufd_device *idev, - struct iommufd_ioas *ioas, u32 *pt_id) +static struct iommufd_hw_pagetable * +iommufd_device_auto_get_domain(struct iommufd_device *idev, + struct iommufd_ioas *ioas, u32 *pt_id, + attach_fn do_attach) { + /* + * iommufd_hw_pagetable_attach() is called by + * iommufd_hw_pagetable_alloc() in immediate attachment mode, same as + * iommufd_device_do_attach(). So if we are in this mode then we prefer + * to use the immediate_attach path as it supports drivers that can't + * directly allocate a domain. + */ + bool immediate_attach = do_attach == iommufd_device_do_attach; + struct iommufd_hw_pagetable *destroy_hwpt; struct iommufd_hw_pagetable *hwpt; - int rc; /* * There is no differentiation when domains are allocated, so any domain @@ -375,52 +400,58 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, if (!iommufd_lock_obj(&hwpt->obj)) continue; - rc = iommufd_hw_pagetable_attach(hwpt, idev); - iommufd_put_object(&hwpt->obj); - - /* - * -EINVAL means the domain is incompatible with the device. - * Other error codes should propagate to userspace as failure. - * Success means the domain is attached. - */ - if (rc == -EINVAL) - continue; + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) { + iommufd_put_object(&hwpt->obj); + /* + * -EINVAL means the domain is incompatible with the + * device. Other error codes should propagate to + * userspace as failure. Success means the domain is + * attached. + */ + if (PTR_ERR(destroy_hwpt) == -EINVAL) + continue; + goto out_unlock; + } *pt_id = hwpt->obj.id; + iommufd_put_object(&hwpt->obj); goto out_unlock; } - hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, true); + hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, + immediate_attach); if (IS_ERR(hwpt)) { - rc = PTR_ERR(hwpt); + destroy_hwpt = ERR_CAST(hwpt); goto out_unlock; } + + if (!immediate_attach) { + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) + goto out_abort; + } else { + destroy_hwpt = NULL; + } + hwpt->auto_domain = true; *pt_id = hwpt->obj.id; mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); - return 0; + return destroy_hwpt; + +out_abort: + iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj); out_unlock: mutex_unlock(&ioas->mutex); - return rc; + return destroy_hwpt; } -/** - * iommufd_device_attach - Connect a device from an iommu_domain - * @idev: device to attach - * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE - * Output the IOMMUFD_OBJ_HW_PAGETABLE ID - * - * This connects the device to an iommu_domain, either automatically or manually - * selected. Once this completes the device could do DMA. - * - * The caller should return the resulting pt_id back to userspace. - * This function is undone by calling iommufd_device_detach(). - */ -int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) +static int iommufd_device_change_pt(struct iommufd_device *idev, u32 *pt_id, + attach_fn do_attach) { + struct iommufd_hw_pagetable *destroy_hwpt; struct iommufd_object *pt_obj; - int rc; pt_obj = iommufd_get_object(idev->ictx, *pt_id, IOMMUFD_OBJ_ANY); if (IS_ERR(pt_obj)) @@ -431,8 +462,8 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); - rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } @@ -440,22 +471,54 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); - rc = iommufd_device_auto_get_domain(idev, ioas, pt_id); - if (rc) + destroy_hwpt = iommufd_device_auto_get_domain(idev, ioas, pt_id, + do_attach); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } default: - rc = -EINVAL; + destroy_hwpt = ERR_PTR(-EINVAL); goto out_put_pt_obj; } + iommufd_put_object(pt_obj); - refcount_inc(&idev->obj.users); - rc = 0; + /* This destruction has to be after we unlock everything */ + if (destroy_hwpt) + iommufd_hw_pagetable_put(idev->ictx, destroy_hwpt); + return 0; out_put_pt_obj: iommufd_put_object(pt_obj); - return rc; + return PTR_ERR(destroy_hwpt); +} + +/** + * iommufd_device_attach - Connect a device to an iommu_domain + * @idev: device to attach + * @pt_id: Input a IOMMUFD_OBJ_IOAS, or IOMMUFD_OBJ_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This connects the device to an iommu_domain, either automatically or manually + * selected. Once this completes the device could do DMA. + * + * The caller should return the resulting pt_id back to userspace. + * This function is undone by calling iommufd_device_detach(). + */ +int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) +{ + int rc; + + rc = iommufd_device_change_pt(idev, pt_id, &iommufd_device_do_attach); + if (rc) + return rc; + + /* + * Pairs with iommufd_device_detach() - catches caller bugs attempting + * to destroy a device with an attachment. + */ + refcount_inc(&idev->obj.users); + return 0; } EXPORT_SYMBOL_NS_GPL(iommufd_device_attach, IOMMUFD); From patchwork Mon Mar 27 18:38:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668443 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 EADD9C761A6 for ; Mon, 27 Mar 2023 18:38:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232546AbjC0Siz (ORCPT ); Mon, 27 Mar 2023 14:38:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232475AbjC0Siv (ORCPT ); Mon, 27 Mar 2023 14:38:51 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2087.outbound.protection.outlook.com [40.107.95.87]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 21D0544A3; Mon, 27 Mar 2023 11:38:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BAZmOHKGvFGImtDtUMaPhLeatEDopDSYgj0CmsqIMXW/EXsw44NICxSG8FAYrmO33wuwrDBQum69AqvVWZEJ/jkve5/dpPO80qPnVvlpXHnUn6D2JiHY0040qVHwtu2ZM4gmLqCD2ViIT0KBdjSEhXQtrEZAOI1XpU/qbAsbzQAu4HcVY15B4QlfHUFC+asQ0Hsp9KMAWL/wk4qO+X8khz9pIZc8ghxcVVK0O78LZnJ9qUPOvtBBhho1aeUz3i9yRsCgey8gDG019X468mbrKQWDBlY8UuM9YXHESD1gy4ZQz0010BlvyfMQs2I4mHjhexCpGv/oh07ynJCa1NgUXw== 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=eZvIAHVIm4OoX2pc8gPWyHKTEEaDNAwf/sX1+Z55dLVnjSrO+nR4yIFPrLFITfMhJCYdBbjax59SNlB038R8TDuTVkumVGq+XUixt7Xpq0RhIVo3yNOde/O+Qu+kT76JElh18ACEvupHWYNx3106YP/L3h/xXD6aRfbhMIiapVRoeLWByvVWkXFvyKl3dqAxMk3qperO+eUUjnYOzc4PFSbjiOoZ0/CYJ5BuiMWfbyvFb8IZZj06H2HdB+snAw2jDU7RmyrDPg2PYz1yGKnDTa8jPDmekFzU2YmNmf3YAav7KUZ2X/ZcqhqUqjqOXScdZyQcvo4Moio66RRzhWVtsQ== 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=GtHJbodQKFLOInR4qdpOY4sBqbCGD36d5XooTssrkHDouO1RpcbxP/6qoRH9YVgE0IzRwVGD5rQ7KJUVivoejxSnDUCIPfqC6u/0VzsZBVHYfQDhuHnM22TfKJDd4qyq9SPRYvm39w/gdZ3JfQWhuqwk9udOW02234NGU+M7iizwajqB/6yf8pqp5bMnV6jzghCCOxa/DVHsBBt30194Z1j1oLBUx6unUq9tuYVjd18oSkYHCIvbuj9SA+id/g65c1rF+7QmKCiWeljzkEEX8xHK/lrCbmKYaRF9YTtwLLGY9qIXZjTiFlZX4x2hSynkPpBIb9pKpbeCVwMe3vnyCw== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:15 +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; Mon, 27 Mar 2023 18:38:15 +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 v4 11/17] iommu: Introduce a new iommu_group_replace_domain() API Date: Mon, 27 Mar 2023 15:38:05 -0300 Message-Id: <11-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0478.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 980f5705-c891-44d0-7041-08db2ef26ba9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yfL/xJMVhubeNXuDUfFhFo9/6RE6oSCuksWu/bxUj4rKyhRqcG5v7ExRtCvZfzGIjYf6XAo5/gcwZlwFSRVCJbpcZGwd1YnjcvERYk24/kp9/TS5liMSRA3+NG49XOMGD8qdczoQsaW3a5kjt9f+CdE0QTcHTRnbjnXUoauzRncUE6Rkj+agfit3RaH2qR/OQu1/a8LS4oTzVAFMAHQT4TXUwJL87Yz45lPwkE+gblXEhCLbBYoDbn/VxuEHFr+qqAynOVZvZiECRczZciqr13nELf7I9jgGUnMXwJDqXzawOJRtLstRNnI9xS7VT1tomxGCIl66Ox7d5dfU+l/5eE4hy6pYZZLnxdQR8hGoCLzxM0lSnT6L3rmShXgZkk11pXUvCIulOI7A7b1mesLVdooxmqiYd6EGqj2ljvla3PfeaRq6PK55PHEUAwIn3lIjldfz3LeP5X24rx1QwWxoyLSWDFXNrXze+hZWdWK7rUBhEf7/VbVH0t12MQEwqKVJFYsebrGg7spbQUOyZFSNSDrlW3zn+nGJIS5EO/Hspi4qF1Uf6CF9yw5M7n3ymHl/ 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 1vnevkU00dJuFj9BGSTFnCygNJ5iDgIwxIH45+4nxdPPl6t4QwJKfOa1CfA1+QkGDGrchweTMzF8AIjWfQeHMnsuEyNDEsyyfCwwIQM9xmhDobLoPFJxHXeXh8Qfv+sBvVlS10Yw4Gl83+XCuuge3etW6kRn4LxohKXUek3x0nqvtVrYJPWoNTt6EwWyeKs5vbSBQ2MsOZF0/RLkV90nw98UmkYIuSqIzKU9kKIEQU81yIk2ybkyd+9ydl7IZXf/eahkoxt5guxerSNVS4EbqXVffcHJuYtaVWmmvDXZN08thsrg+3XOVT1DVWkpSK1L6t/OJVMfYN+uWbcrlWlITsshi2ZLzOqOM0ks4Fg9TvOjn3I9/PTNrmhtlvhYYnYSSZEuxA6N4fx127ycaTXoXKq3OyWW3/3P8iisRd7Jid1tukGo3DCokeCE4SnYnl4XsULl/K5iu9h5PHH1li6EbvOKr80/Xd5PF8QnGa+pD3Y6vLRS39Y0wwIsPo2ywYtjS/bbB2Q0c8t7GmFswUrIeNzAXfsniXYXy5uV575B9DohLY9iYcL86pkitIbkpFXw4Dj8tsIgmu3Q5JVGFdqdIXXY8o884kBpFGt0JK6xKfgYJ0krtSs7Kkhw1JcIo8xwVAg6r0bcnGzpDWYPKOOCwDJxg2OQAuL7V2B3HHhPp4+O0AzZMVMahwzvq4C83EfLTsosHBU0NJhzYo1Ee7i+29Bu9jT+vpXsRv11AXf4a/uZflTEwQNRIuCBlsDQXFxCU9jjxV+ZX2c6amvTP/iegO66cJLXEzFQRXsMm6V3psH3bY0zrs+34Lhw0qCGslJfNczqLvm7z6jnhtYkDoSJqavMTjay8ZqT6T2AXvQW0uZd4RWB1C5qo+hH9+dgo/RUbgvaH7/RJIDkAWmUlc5L01R4VbBoH9kFt/5WLwCEXNEZbHlJ74VX5agdcTiEv7S74Exk6dCl9R3TIkzlFgn8J7fyVGf9styramqS4AteBnB23Sofx6XThz5xn6k3CodzQRcxz4CUnm3s518EOpgDYR6bumXII0uLD5l6ynrK7UBsPKtzPZVwcpn+9HoJDKyuzVHlBwX5Yw3zBv3lQbaL3WWmNeEsgI2hmECeQgRJ+4NMV9jcxF3lGAEWEVbpMvONwmDBXs6cisqUTnPhLqZwE8ZO8ImzpAMpp1m+I589UvzFFDjopJYpAMq5aFjhib4kEA0Bp8iib08ekiTGQsSI3ldztM3ccEkkxXebG3XurcKYxCqD0yWyrh0buWCEGEls0kR+eGZC13skp1nCaj8/gmrHeRo+PDeytF6aOLxwDLXAlAC7tsgTdtwhapHlUQwFumIo7WMLozGRndFIPPtaLnAjmuXtIJI4cV/BPsuBtnGco/JYjJtf4VLk7mgfRU2HCglsc9UtmwQ41rOkHvJLY4WHLKuU+tuELwIaT131wFjGKYVJYwjar0e9plgNfHC5zwCwNzb1YNzLoCQOXa2epidLcPQ6POuUykThrwAvqeikZ4l+LiGB1uWoyK6XNt71vfWF63m3t36sw+fAIiYvZRl2BRefbLp5mJl4Mij+y2YXR/0edlPaOW6WzUdGK7Vg X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 980f5705-c891-44d0-7041-08db2ef26ba9 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:12.7380 (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: FBCpDi7EDYoV3HIMmUnjR/YDA/vxde/js+twdjF6g+BAyEBksVbhz5XcDC6VquzM X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@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); From patchwork Mon Mar 27 18:38:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667699 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 25945C6FD1D for ; Mon, 27 Mar 2023 18:39:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232657AbjC0Sjx (ORCPT ); Mon, 27 Mar 2023 14:39:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232659AbjC0Sju (ORCPT ); Mon, 27 Mar 2023 14:39:50 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2047.outbound.protection.outlook.com [40.107.95.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44B693ABD; Mon, 27 Mar 2023 11:39:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WcCuuumU0jcU9pmklAEXk5QreeWGO8GwocFKruWA9rNhcI5y+fpJrsZt7cpnr8Xw1wyr7kdzBoomIiL/LDdfV1xvHNfki1N9JBZwYYUPGbGnn02aDZlAXQvXgqi5nTmoYjQDCSBAlXQRo6N/xHiy+J6fwe5nHzHc9x7nNmz8rK9fPFnBMMJWnqFQjE+z9sR1Fjo6r7bE2dnFBbicjG/oZsfOBvBV9iz3wcSwzq5MWkZyUZpOoZLlhQvPzZLVvBt71pjQDtiiHhLrYkQiFwrbl1t1w/5heJ0qQ1pRbEg3FhhwVkM0dY+B9agdoD+hwqMX//HyHN+o0UF63whlJD8v7A== 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=/ZPq7jwToTroSMRf2OwvyP0SHuAPEXc32RxnsRiw/sE=; b=Y0Yzg2ry35QOfou8yEIu9aRIXLunqF2uDkpUXrv26ZzPsaYeZGqQFP8cLWhZH6yBX6d+ftQGF+wv8KsjXLLGXaM9d7af/5bVJjB7s2cu74Fg32KOrLSo0nz5V5JHEQV+wrqrArg2aI+0ZZLnom79ZZ7EbvS8H5ImNfoGXE3Vc64/6t0Lz1tfY9Dlegtgo1TJ3javlDiFEkFuYIYYYKplEL0ODsKvLfxiluxZJ/ubmcZCW7b3Z/ZrX69UA1LjhGXAwk05TZuqqIEwTuY7ZpvsBktGwdFbolMyG/2id8xHfPkDC4Toz0oVQLh/Zc2smD77FoBoD/aKO8AeIUYQgzii5w== 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=/ZPq7jwToTroSMRf2OwvyP0SHuAPEXc32RxnsRiw/sE=; b=X0M0q3G/1d+ZA5kwHeUtvTm1FM2ASXDDnGJFDbAQeVcjYxpqg8+4w0HpF31ZuBiEBJ9GNI2jSoW8ng/IKDLPtA0FPy05ZuOIWM9gMiupg1K4yvGX8durVXjm4N+6mH4cBeveUr1uCUxG2eHZB7HYxLxPJzKSHT4j42H9LD9Jxzsh4olWrqtMPT8WovYUrfIvgpIWxXNpOiiPLvnArWf5dliaTwv8UbPlCGzdOPcGuS6k3NUkn5USdX7lrV2CtIXuRSVwNCffZ0gXmijf6bN4zfmT7+WmeblcwnZlNI21WhDBc+2fcCm2HIVrkwy930yIOELQXAeQ15m3buzm8zZMqQ== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:21 +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; Mon, 27 Mar 2023 18:38:21 +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 v4 12/17] iommufd: Add iommufd_device_replace() Date: Mon, 27 Mar 2023 15:38:06 -0300 Message-Id: <12-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0391.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:108::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 0aa828a1-9fb5-4b58-f147-08db2ef26c7d X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xzLgszrd5vVGtbl8n9tC2LaIs1lTXD3fb5uiLsH6QweqAARNZqCz0ztTDF1AkOkC++RrUtr8U+UfwAPHu3piscMuZDCo8imP2qqQIvnLYYSVTonfSvegDD81741zb1Eed4xQNhgPK5q6X/wzZLnsaW22/j9EPTLjyKQGVe2XIpPA7nHXlZFfsS4m/pzyjhkygFFk3bgRlymdua6UidYF8PFlGhvjdmTgeEKgdTCCbgQF+EQPkplEh2dA8+YZqWrUgSBF9FIRB5MvhQ9/aXQ4O1DqBAOuBG4Rq/nevZrQ/cB6Hahq34mDg0fM6XoKlJB/qHwzNBg9RmW5S+TtFLTxRfmErmQVypGftbGNICmaqwTcMlJBRWQm7hLPIMXFKmG5tQkPUvTVEMdSt8wI/lAJg1KM4d7Zjvy+KM7/R90pQUjUPlxvCZAhmGleAI9BUudrxtEoxwpLC6wCZ8dlmwlMPZz98/rh7ocJN3Vy3c/UhzgB0Qo+nLmdsSn7JXoIxlZ3rW8zQ+CkYgzRH8ziXa6EMa2nbMJU7+shsUxlEm6ZK5/aNAprIO57jzyjbjkhHT7z 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3EvSEYZvV3RjcB5SFMPFu/ws9fnYS2R0kOdgjD2k06hE1lSBgfkaDX7ThdaGDa33GYfdkHQfIIIgj3rws5sqIXMkXJ5+O4+DefxlXuTfyoKHWPbgj1/f0YK/yNBfjvJreuAw1TIvjgj/nB+4vHeGXLG1z4iVgd0ckIE8zdGAxCkQvt42dfZL5jqlCJ5FeiAUb1mh6tIT5l6xd+mw0XRT7n74MelTF+PArKUeB4QOEjRSsgY0+BgnJ2j5ZoewYQfIpHH7UN9mhJiwDX3luEvqTaIVwnSYPIVq6FXZLVDeGosCsSLkxfQkZx15dbYui9i6hA/yl+zSgDu9omdgZ70CbohF3jxBrjYDJ3nrbCZ2sHzmAU9liidqoUswqCEk84HzygP6bE4wPLqjNgT4G3DRMyETtinVG68fpI0h/GgDIHHK+0DsZ0BcgOocff+pz9U2FO1adsKTTeCADc3bPk2qUzgcH/nrUAlb2F5VklcIJpblhs7Dt9wb65tqNdwAKhTjXjVCb2IEX1DlTS1r7XEd84YhWwl2bUOVzUZryfSpIGHDBGHpFZIhwB8bkyZweej578q5TLD1fUsMOi2bDKEtz6ZPRi/EWmjNz+YeKFeDJ5yalJUAuXVYXLyMNe6RgDOWJBUbetdDS1fN0NGKrzX69TRoeH2mmHgmI6HCku+LKL8p6sBnukQwxt2oWCZjVePlgq9oFS4sOW6DSKtEaHW32zRF+PQzvhLVSTaA/ogYwD9r6qWKtgq6TO/UyLv6k1OGoB4cEbOoJoDjG95m9+4us9/juRjfjkxKpQwVhQq7Zyy9410vxIW36qGSlZj2sQl/yKwsZBCb01J8lZka/9TsK0MsYE6LbzXLVBZEf5DRPDbFTA0/VvNCfda+juIPR+BSKStIacmhAafoluJdeHLiVSP3EHXXTrPwzP86T60VvHJdp2ydZ8XrTJesM9GS/OLNETb9CEFfO1TL9P3MX3G22rOy7QgocXhzYbj4FjFg/XgBjiPyAQiH8g2HaXSoaGVpYp4Z12wH6xFzyHd7ImI/Z7NOxp/w9gwM2UDPvWSWNHSaPBjzZloKcxU+Diswmb64rZOrNpnTO7pvRPuCuM8FLog9B+3vDhBTRYebDz02vgZKHpSjbmubRIEk4nPMt9N3XNARAPmp3gx7bqK58FQDd7O0tkNP8BVX3r44lh83xIcFhrfFUhYfDeOeKc91s32+sR4inSrZNsB3AgTg4BRvEvDs0xv2cYVfVPr8BRHlZQvExEwPAYK4Ysy5ED0XTKOMOPkcG7waHqPPStv4k2ai8woBv0exBXBISfiilX4a8VWjAsM+/kTCfOmr4iI3q0lhq6jaHQu9WmAOMQ1ibM0mbxsm1OOKGb4W9d78K827j78Clb+jfcNHgM/+uVD4FJp47uhfbBRU2Y4jhajGtB56Z7OtW7O7WiSFL7AUf9jwZdimetgv0mKcDLbRhAwtx9j5QN+1tJpIQnhjCU3Rhu5v/TPRvOqXhqB2fRbjdVKAnWnWP+1t7crGZhuYEpScHICmWEZiEQU3QUm+FkoysxPGtqkO8ohozGZDbAmdPV0jIKeRkSMNc3JaLQPvICUee9NJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0aa828a1-9fb5-4b58-f147-08db2ef26c7d X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:14.2556 (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: UJuwzeKCirB0dfJ8tiQxhhZ7yWpX7C872GPT9VN9IcCbS1gsfGg+8Ld14aK2WeRN X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Replace allows all the devices in a group to move in one step to a new HWPT. Further, the HWPT move is done without going through a blocking domain so that the IOMMU driver can implement some level of non-distruption to ongoing DMA if that has meaning for it (eg for future special driver domains) Replace uses a lot of the same logic as normal attach, except the actual domain change over has different restrictions, and we are careful to sequence things so that failure is going to leave everything the way it was, and not get trapped in a blocking domain or something if there is ENOMEM. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 99 ++++++++++++++++++++++++++++++++++ drivers/iommu/iommufd/main.c | 1 + 2 files changed, 100 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index f931d3b95d6d0d..397947554bae49 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -4,6 +4,7 @@ #include #include #include +#include "../iommu-priv.h" #include "io_pagetable.h" #include "iommufd_private.h" @@ -364,6 +365,84 @@ iommufd_device_do_attach(struct iommufd_device *idev, return NULL; } +static struct iommufd_hw_pagetable * +iommufd_device_do_replace(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + struct iommufd_group *igroup = idev->igroup; + struct iommufd_hw_pagetable *old_hwpt; + unsigned int num_devices = 0; + struct iommufd_device *cur; + int rc; + + mutex_lock(&idev->igroup->lock); + + if (igroup->hwpt == NULL) { + rc = -EINVAL; + goto err_unlock; + } + + if (hwpt == igroup->hwpt) { + mutex_unlock(&idev->igroup->lock); + return NULL; + } + + /* Try to upgrade the domain we have */ + list_for_each_entry(cur, &igroup->device_list, group_item) { + num_devices++; + if (cur->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) + goto err_unlock; + } + } + + old_hwpt = igroup->hwpt; + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) { + rc = iopt_table_enforce_dev_resv_regions( + &hwpt->ioas->iopt, cur->dev, NULL); + if (rc) + goto err_unresv; + } + } + + rc = iommufd_group_setup_msi(idev->igroup, hwpt); + if (rc) + goto err_unresv; + + rc = iommu_group_replace_domain(igroup->group, hwpt->domain); + if (rc) + goto err_unresv; + + if (hwpt->ioas != old_hwpt->ioas) { + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&old_hwpt->ioas->iopt, + cur->dev); + } + + igroup->hwpt = hwpt; + + /* + * Move the refcounts held by the device_list to the new hwpt. Retain a + * refcount for this thread as the caller will free it. + */ + refcount_add(num_devices, &hwpt->obj.users); + if (num_devices > 1) + WARN_ON(refcount_sub_and_test(num_devices - 1, + &old_hwpt->obj.users)); + mutex_unlock(&idev->igroup->lock); + + /* Caller must destroy old_hwpt */ + return old_hwpt; +err_unresv: + list_for_each_entry(cur, &igroup->device_list, group_item) + iopt_remove_reserved_iova(&hwpt->ioas->iopt, cur->dev); +err_unlock: + mutex_unlock(&idev->igroup->lock); + return ERR_PTR(rc); +} + typedef struct iommufd_hw_pagetable *(*attach_fn)( struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt); @@ -522,6 +601,26 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) } 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_HW_PAGETABLE + * Output the IOMMUFD_OBJ_HW_PAGETABLE ID + * + * This is the same as + * iommufd_device_detach(); + * iommufd_device_attach(); + * If it fails then no change is made to the attachment. The iommu driver may + * implement this so there is no disruption in translation. This can only be + * called if iommufd_device_attach() has already succeeded. + */ +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id) +{ + return iommufd_device_change_pt(idev, pt_id, + &iommufd_device_do_replace); +} +EXPORT_SYMBOL_NS_GPL(iommufd_device_replace, IOMMUFD); + /** * iommufd_device_detach - Disconnect a device to an iommu_domain * @idev: device to detach diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index e5ed5dfa91a0b5..8597f2fb88da3a 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -461,5 +461,6 @@ module_exit(iommufd_exit); MODULE_ALIAS_MISCDEV(VFIO_MINOR); MODULE_ALIAS("devname:vfio/vfio"); #endif +MODULE_IMPORT_NS(IOMMUFD_INTERNAL); MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices"); MODULE_LICENSE("GPL"); From patchwork Mon Mar 27 18:38:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668442 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 B3F6AC76195 for ; Mon, 27 Mar 2023 18:39:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232486AbjC0SjJ (ORCPT ); Mon, 27 Mar 2023 14:39:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232488AbjC0SjI (ORCPT ); Mon, 27 Mar 2023 14:39:08 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2047.outbound.protection.outlook.com [40.107.95.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF7B32736; Mon, 27 Mar 2023 11:38:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cyvbWSVbWs3j/mi2V1zsJFyip7GJrBxrNRHGFtbW4ANFF5jFhRCddMBEHdNd04WPjo+AJThZralT8J6P5XmW/qUfKO4cgJYV7zmtx1vTkT/WBxFed/E3wfR9AvtQE1HPuQ6U2S35hiCNtDPSZY4cgWHhhujHAwKgD39mlfgbBELw5v3Fncamz/kA8yyz3LbMdoYCc//IeAMz3VOxkHKLxShr65qpxgAoD/lnCAjH6Vm107oL+LRLNvp63Pmxel4Iq1VAuUOZUJigSo137lFYuc2k61JKSwzac8IANoCQVof7IwCi8dSkEj7UqyDz9pNicbGPYQsVgXEi8fqyjxDV3Q== 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=1CNS2HSmgh3e1COJZn4Kj1NyOKZJ52hw9Q0p1GlmnsY=; b=hc/GXSqeI/mhp2o/1hDvlTIsveiGrRuFfEAVbUVQ43Sf0t6JflQk+gvz8PcwC/PBYD0JVjCFbyPb5qeZclMZPXnx2ws9GbX8m9fho8KsVyDjqdWoZU4pn8E9DI27uOgzGUtTqN9u8sTLZY8R3hXm2EVJo0/4ajbgyB+8b7Lp/kJLDccazagsko0tWyPl8FnYNGfR/Bmi9CNkCZtbHN/kiKVRzevc8GlV63L6qSXmQPMLwwDi61LdvuEtHSeYGsDL6173Nsna4j/Vf0YlcwQ/fS4+kndQkc1yOIY+GM1eeENnPA7j4lGdhY2ipGkzSiVhUnq9xir5iFFbiUQUg3b5mQ== 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=1CNS2HSmgh3e1COJZn4Kj1NyOKZJ52hw9Q0p1GlmnsY=; b=lI1O1itLs6aPbicecKaeGzp+oeGoXqIp3NUpvGFWENYrJcaKM0ADe5db/IylYjvSEIO0UCwyYhtO5phJcHTxzs+4Mj9Z4qlA+WS79p0tl3OHEZr4QSvh7fN6yVv+xQEYcNNSiKn2MAC03Kjl78Z4tgSSs7MRUF37am5kRstkJ1/ney6ACoMdKWax5EnsbSWQyKTkhy2H1SMIzxY4FXfB5SZPvwyuyLYEWzbBmETzouj2922+2tvOITXSjZ8jbS36l7KLlTyP9fwU4Z7onOYgZNaI3eslrTUgRrRNGVPCfNr/vvONW4VEj5XxQBplO5WXo3sMdsLUYjty4PgyKyLmnA== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:16 +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; Mon, 27 Mar 2023 18:38:16 +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 v4 13/17] iommufd: Make destroy_rwsem use a lock class per object type Date: Mon, 27 Mar 2023 15:38:07 -0300 Message-Id: <13-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0181.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:110::8) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 84e5c59b-871c-4fe1-5b6f-08db2ef26bb5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: SJEbcgCf/dIDZiuukmENVAma75XuxcA3sNiM8ffp0LD57/TDxQtQwnqODDyGwR5j7eLp1upWwRbGwfRsY0rbtq4WXXOSnVs3rxg3eoItBviVJ0T28712RWSzChHDSrZwiDjNzfaP8JQ2nSCLEDp7BwxLWb3DaftqIaFLQbFUn6qKdY0j1FnpEjNitNF1Y/zOhbpastt6+OpDnE/fk5TKcDDNGrfqiOvcX2rs6TjyNyXhXSgdPjxqxVDdhztn3iPikCP1dE2eKb1OfqYRiWuJzQ8gjpiYZ6eA3Kgog/4+2HFLVgSjAiRE/WGczv4JWowTW548XxnosWi6b1klWtAZcg3W/LDwVCEyo6Ji85Wo4eCq72WWd+tTq5bPUdCgoHzYaVc1hqG1rapYF1NizHQnqhJGwd7j+w6/dCZ8gmB9duWpns/dHs2SfNBdhNitO2CweA6KKARWcbpaweI5RYBcszTTYaAy59gGIggImgyPxyhsHfCzP+m4L350yFUAfh2GXXg5SP+loSc6ZMgDxrGMDTSZuOUBQw/X6m6Fgp+pbjcmdFBlhOIiIC6ADB4YVNFJ 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: fioGnzBRuomCqWA6u6Q4vDXtbVzsKsnk5rl/nM680/wQz2sMu+OM8e+hGOfI0B5gqd9Jc+iT/MS8z2yr9f8KKcc/PQ4GbQD0C1VN2OHJbd6caFQkgEnKzH3IuczrLmb0W7oRx3BqfE2Vo0uTE0Af2wV5KnLoaLuAI3CtExKjU9V/eOOIlgk0zx46HSobfEWhwELc9K9deHyLVzoQxxMCLRbX5kC+07HCOqwpZxxy1YGsQ9PuNHR7djdmKv90eVw15568zBpD88bn0ih17UyKZso1iioVuftgipFesRl69E7ZZzkUTFxqI3tAITeYSFFthOk3Z+xDzIxpP1YvDKpQ4CTeZ+WplEGjUZ+KCy7LjKP44+VCkPnceeVcFkHGQMVw34z76i9zYj8TbYLwBu9n6wmzT8kz1fAkXnogOHgCtHUiJz33tKT1FMVOfwp0x/GR5JpJQ6XEU0dCCSqQqwskRm5c0jDc/XJEQu1I4DZT2kvp269FaDqO82vs9Q/rNV4mdzGkZOHjYFkgZ7li05leRN19PRTX3rPGecHgrDcC7WFbuo0ateTecbnfPaHtU3oriWtr24Kq3Qm73taM+8Zp+PriNM39+qpKASCAIwiZ3e6YyR4enimLDWfI0cRZduP2dgZpVHwp3GIoUNFUcZ6qAvXMns6oTMYgZJbXfbEWqgrSqdRW7X6315GfD553X0z0/vf+8BavIks5FK9XUKN0RE5m2u+npeYIwTrtQTBNMcg/oJO9vVlLin7eDmItq8mxwtT/UugiXH16lgBx/MPS37WNg1QPnG9mBr3fTh9bZlK5oCA/6YG+/ELSC9TYyUxJK3qpWfkD0g28C/bUojFjbvVlq3B/HLoXcbfOs6JiYSwB+iOAfTANRnij+Ou4CclaAVZa33BGx5gMAw62GSkE5H3SzVWLFxR0DIUquAwn2Cer2amioT06tn6RPuccIckSskVX6wz8q765bz6Ld1TzhBUvQ1mDFs2pq0XDXiuNHuWd0cKaAPh0IHgptZDv2yEaW9+XNWBTFln/Uh6V0O6fGGmV6pxRQ4Zk1dzNi5XGgTRHLiZ9xp0+NaYC2+oxdT4lgI733RXnD4Y7IzFj4u2t22Vc1Tgf2jBvpUYkBuKYqfi93E8X3lPz8+hTmjX4V8ycog6ObBv+wYYjvx+P153SQqDPHz/r8Xbwe6eUqPbG7cJmuz8pnFW4llPcP7wL128wAt6PJAAxNcB8yaUdFas6zhlQ4BnHoscuJoiy1liudYTHcmIyK9kV4rebcjeHuQQQ4b3E2TuQf2qqTN2bqpkRrvpg5tJWxZFGN0PIzz401GqpDmtLQftDG2NC8Zh4L6taCvC1dCyHHsuzP3vlexRqfm7GucIRB5Iw+AX0eWzoFR0S/wWZMBsikyHdyA1Ci8tBQu9iEy+svEAEAEtBLrU302+OJOUla+QjuIPsmWW8Rc2o/JE0sXf4+4qtpESwuBgIaK9X23/Nt2GxkIpZQhkArOL09RjYG3uEUx5qhwWDIhOIEa+PwT9RdxB3a2BYzeUyiWVjZQPGOLHED996SO9SRPl/Zp4q5q+77LzAvyWtKRuDziWvV3B+1DtueI6K0GZd X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 84e5c59b-871c-4fe1-5b6f-08db2ef26bb5 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:12.9255 (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: lYVCJ4sLVeRQkiMBP5gi/67csLViHZs7gKjSrepBAteGqMInUYlQX6DgNVRavVpJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The selftest invokes things like replace under the object lock of its idev which protects the idev in a similar way to a real user. Unfortunately this triggers lockdep. A lock class per type will solve the problem. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_private.h | 1 + drivers/iommu/iommufd/main.c | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 471a3fdff1e0b6..f80b012e1bc200 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -119,6 +119,7 @@ enum iommufd_object_type { #ifdef CONFIG_IOMMUFD_TEST IOMMUFD_OBJ_SELFTEST, #endif + IOMMUFD_OBJ_MAX, }; /* Base struct for all objects with a userspace ID handle. */ diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 8597f2fb88da3a..9cba592d0482e7 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -32,6 +32,7 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size, enum iommufd_object_type type) { + static struct lock_class_key obj_keys[IOMMUFD_OBJ_MAX]; struct iommufd_object *obj; int rc; @@ -39,7 +40,15 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, if (!obj) return ERR_PTR(-ENOMEM); obj->type = type; - init_rwsem(&obj->destroy_rwsem); + /* + * In most cases the destroy_rwsem is obtained with try so it doesn't + * interact with lockdep, however on destroy we have to sleep. This + * means if we have to destroy an object while holding a get on another + * object it triggers lockdep. Using one locking class per object type + * is a simple and reasonable way to avoid this. + */ + __init_rwsem(&obj->destroy_rwsem, "iommufd_object::destroy_rwsem", + &obj_keys[type]); refcount_set(&obj->users, 1); /* From patchwork Mon Mar 27 18:38:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668439 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 8DDE9C76195 for ; Mon, 27 Mar 2023 18:39:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232631AbjC0Sjw (ORCPT ); Mon, 27 Mar 2023 14:39:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232645AbjC0Sjt (ORCPT ); Mon, 27 Mar 2023 14:39:49 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2076.outbound.protection.outlook.com [40.107.95.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1900F271C; Mon, 27 Mar 2023 11:39:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ja/5UyaA3Wn4QVpnsXV1lwBMzWIAe+1EwUS+syWs4ewa2Rvs4htFoBFCaSy3vUSpbPECuGoaY46Y6KD58euoQp/5Wjpgix/XniS7cOGhp8l2hYB1fnpOr6UFPlXulkWTw5opxiedod+pPPJJ1Mn6TmkZacHmRBYbWoAcqJEuBrxEG4zH17ebhf2XO88XK1eUOkKNCQVNf2jC9wTYqbcsZvN+sPwyJ8Lxsnvfdqcbzw5OVzFs/kY3i32TYCC94e/zI1TQRi1ayQJaweeZiIlGV1FDG4bpefAfrs4+npvJJBenMru5ylKCV+eLGl+NXET2Bhgn2ATHw5wK4q6iEfIb8w== 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=v9NAWuhQYRmr0aJbsrLbSRT+BQzLs2Y7zxvm9cTGI8k=; b=RWeMF4v7NilxXQKaAI2krpwV8T322u8DZ8pLjKEFUYMARUYYFui1rfBAP0ud7CRR5o/nZsL07v3BvGeMSFaWsPwTNVKVE2irCnE30ifigQXzAW9crwMZLJ4tETUsMCv4XoWajv+JuHl88tLKDEIths44hd10Fe/VyrZsk8jKIbvN9ymCjTIA2+W12O0eYeUoUOZyveBJac0ETpdC39UFgJSvlXLagReXRIC7xKXYc5tv/yrXHRfSw2dw6rTDGDv4o2essKtk428CT+hhofQJk3Xe7VmpmOel/grxrudRThl/7V7VIdCbG8NsF5iC2nbI6tm2DaQkZ9w4QSk3YOg2Mw== 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=v9NAWuhQYRmr0aJbsrLbSRT+BQzLs2Y7zxvm9cTGI8k=; b=UUjIM0TlD+lSwWKDCvPYcb0rLU9qVUEDdg7fG3oP1/xirq8MbCRFvPjmwIaHHn6JESE8PHc3kLLxXYSBpCev6XMcA+ZLoNuKlzyq5VL1t5epaIZz/SLbmsGSqyLJlNY+eBc5xCeGg3hP0VuIwkxAukcvV1/BakV/h64PyNaQ/DrzQHF9JIVDyS4yCgWoJVIjMr8qbscRPRpY/DsjZRKmGaxrMt/FLMPGXT14JbzICYiExq5IfjXWJzxqV/iehrbHeOzulJuKZBlFf15eavv5v5YLOIzB+wTNDKR/0JW1k7s/Q0zwjIzHxrLQsME06ItFFvK0e3kth0cP2VZ4nRuCCg== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:20 +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; Mon, 27 Mar 2023 18:38:20 +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 v4 14/17] iommufd/selftest: Test iommufd_device_replace() Date: Mon, 27 Mar 2023 15:38:08 -0300 Message-Id: <14-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT2PR01CA0015.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::20) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f218e1f-9d0f-49a5-f450-08db2ef26c5e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 37TBVVQk3EiYVzdQwO0ilkNRTcZaMnA5Obn46NDQEeQPZ6qqscURRHYfxFjwSPHPQvPY4g5ek9kcGkujUDEiByJDAP5I2n5y4Cg75lr10k47bMjo6IjUfDG3u7Iml7wFEGs/dj9xcI1YhhuMNvr/VrxYI4UcdSmO8gFowj5/kQl9pKPN/bS/tZJEkwyC2Zp4ZAL2OsI/YOnE151UhSDYOUYkvkf5jWuHGCKENPGURlQGbn6aQ6npyZ7S2PITZv6cCDv8MCuW89MsccW33eyPveMVdJpldiUThOUxvNVFeFG9Ch8U29N9mLLd8nEk1cTHHV7o+27ICffUZs9lnVgGjKl1CBCqlR1mrIHUJgigkFvSmB3X+fDw0SiukIJyYyL3WQIT8/JYJKCbAoQBYjuodSV4bAU42NNareZ2TceGlsOTkeotjUgg5zqG+ciPDAxbGkxl9i2DL1azU6YFeppbwNKRT7uiet4fQpqrKFQeyS3MWMQXv0aP6V9UHGSPo+JiJUB6xiZFJSZirebGSbNaB3SLSvKKie3XYcydLX39SJKaRSy773OMBED/89/d0BNx 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002)(66899021); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: gzBBPkQ/8/xV7Vr3UpONeeJEZSucmQaLRAexXVB4QIVQnuWPSeKtkbfsa3yhzKu4EO5GTr6l/0XNXz0Xhu80KnIoQBtRRAKMszieWtzEXgHu+R01E3PFkpNKnmSL0InaF8M2E4/bQER4Tm61+fCmo/WszUSvPaVsp8KERAkcVK1DA4BKhe+dq/Wnvt3w4DwpbUkDnDs04+dmaVa1/xR2znkiPaTM6AvFHTWiV1/nVkFj7RbnROc/0/98ulaieZAApF+Td8qw+dg/SphkCNNS56NAQHUmbUTrEgkq5peiStNwPXE4kq83UX+BTTb1un7OU5DnfC0bkcgDcuFUA2h8enmLC5k1sdbVBF0whderUs4ntGdLkwSPgZzN1YljTHURrxXv3mhOHC0WhNHYPW+5Bn7UicNO/a3AWVgUz5SUTjM1KL6p+iF6Ij78GoW2WF5LZM3vfzKBUToJaHB3LxZQlSRdR3vcCBhdT2rNWCppuKVxZbuGswXU/fuxSauOVPKZsN48vwlZWUgzEicyn8fJon6Xo5P1TNLx21A7bnhQGa7oOp4rrUDUpBhQDXjYs+uCswyGyzzeatxVD9nEeM3NFYUbVk2Yl2Vy+xTbStO9KxnFwZanEa3yWgF11sUU67cOaIZGXeOltTLNxyeLviTrAEnC628XrzIbNcj3Kvoz/PkRb5JVxg1ZW8Yz0HYT+3+kHyuFda/gn1XdYISteM/P7e2o8Pu6KlB/bjZ41SOY/6R5kzNxTQJiA76yqZ2ApKtWQs8wADss+jMZ1i2pEKDseDcDYptyK9+4PP1aIDxdOofsX5KsJxhN1h6vttwAmjZJL+QNVBM5iArsr/bxTL1N4yfCC7MvueSvJcZC1ZQ0JgLcJhLfcIhMwNaxiGa/auJzO8IB/FzfdSd7A89ArxdGQXnli+5W5AEtD2GuOLCXOb8EUcyAexWugp66M3VdWbInyBYj1mzb9WpK0tDlI4yKXD/bSeaEhGHCy4bwxtyCdVhT+cRr3k/P4AZ2Do7lmqzvEp6DpSqTNmcH8iPoiuVy9Do5/n5O3MvyKszWyjCnumEPNJzgAwW7dXmVJVRpcOl8X80zZZfy+K6hlfXshRQzetMP95CZqUOSH5l0ZRvIU5qrcxQJteQc/0bvDSt5iuONFlRohbwhNjQfYxJpsTxQLg8KlCJNQ7aGQ2kpTWFP44aJyIqJ5GtzyANOQekk3StO5ADzyMGi8mRjFheVg6Jq2bBNcMLcIs4p6luGxYxIqUF3Y+LBPEUqSWdcjDWdp1q36AFDW1J2myi7n2d3vwRtuHEGzxS7boYVkjm7xXKYTq2Ao2kvYvTiGZVlif7vy48HIwTVCg8EW/X36YLL4ppva46LeYXcN6cPMN4mg4gaZKQqX0JdF6Lq3SO76FsRSee/773IV5Ga4qJOhKSRXLReHtOB9MGuSeZTnc8kfOv3cS4nJJylZB4HACVAGk3o20RttffC+bkbMz5Qi624IRs0UrLQXNheoIUsH3he2I4vKIKdMJA0pI1ndOh9/aD9voclTer8duiwj9ZkIuzp5DYjCFSWrmCZuVvGj7nSFpV5mp/dOocSMwu4remsJkdO0DtG X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0f218e1f-9d0f-49a5-f450-08db2ef26c5e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:14.2088 (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: Nktdu0yj2wQwnMvA09HuiGjFyXok9GgBzmda3UCVSklUVHp1khTV0nDcucoAOfnO X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org From: Nicolin Chen Allow the selftest to call the function on the mock idev, add some tests to exercise it. Signed-off-by: Nicolin Chen Signed-off-by: Yi Liu Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_test.h | 4 ++ drivers/iommu/iommufd/selftest.c | 39 +++++++++++++++++++ include/linux/iommufd.h | 1 + tools/testing/selftests/iommu/iommufd.c | 34 +++++++++++++++- .../selftests/iommu/iommufd_fail_nth.c | 27 +++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 28 +++++++++++++ 6 files changed, 132 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index b3d69cca77295c..e3f1035cbd6464 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -17,6 +17,7 @@ enum { IOMMU_TEST_OP_ACCESS_PAGES, IOMMU_TEST_OP_ACCESS_RW, IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, + IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, }; enum { @@ -52,6 +53,9 @@ struct iommu_test_cmd { __u32 out_stdev_id; __u32 out_hwpt_id; } mock_domain; + struct { + __u32 pt_id; + } mock_domain_replace; struct { __aligned_u64 iova; __aligned_u64 length; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 58471f9452be55..dd792294298ffd 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -455,6 +455,42 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, return rc; } +/* Replace the mock domain with a manually allocated hw_pagetable */ +static int iommufd_test_mock_domain_replace(struct iommufd_ucmd *ucmd, + unsigned int device_id, u32 pt_id, + struct iommu_test_cmd *cmd) +{ + struct iommufd_object *dev_obj; + struct selftest_obj *sobj; + int rc; + + /* + * Prefer to use the OBJ_SELFTEST because the destroy_rwsem will ensure + * it doesn't race with detach, which is not allowed. + */ + dev_obj = + iommufd_get_object(ucmd->ictx, device_id, IOMMUFD_OBJ_SELFTEST); + if (IS_ERR(dev_obj)) + return PTR_ERR(dev_obj); + + sobj = container_of(dev_obj, struct selftest_obj, obj); + if (sobj->type != TYPE_IDEV) { + rc = -EINVAL; + goto out_dev_obj; + } + + rc = iommufd_device_replace(sobj->idev.idev, &pt_id); + if (rc) + goto out_dev_obj; + + cmd->mock_domain_replace.pt_id = pt_id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + +out_dev_obj: + iommufd_put_object(dev_obj); + return rc; +} + /* Add an additional reserved IOVA to the IOAS */ static int iommufd_test_add_reserved(struct iommufd_ucmd *ucmd, unsigned int mockpt_id, @@ -944,6 +980,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) cmd->add_reserved.length); case IOMMU_TEST_OP_MOCK_DOMAIN: return iommufd_test_mock_domain(ucmd, cmd); + case IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE: + return iommufd_test_mock_domain_replace( + ucmd, cmd->id, cmd->mock_domain_replace.pt_id, cmd); case IOMMU_TEST_OP_MD_CHECK_MAP: return iommufd_test_md_check_pa( ucmd, cmd->id, cmd->check_map.iova, diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index c0b5b3ac34f1e0..3044a432a83e22 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -22,6 +22,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, void iommufd_device_unbind(struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id); +int iommufd_device_replace(struct iommufd_device *idev, u32 *pt_id); void iommufd_device_detach(struct iommufd_device *idev); struct iommufd_access_ops { diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index da0443ba16830f..77b0601fd13a71 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1001,6 +1001,7 @@ FIXTURE(iommufd_mock_domain) uint32_t ioas_id; uint32_t hwpt_id; uint32_t hwpt_ids[2]; + uint32_t stdev_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1022,7 +1023,8 @@ FIXTURE_SETUP(iommufd_mock_domain) ASSERT_GE(ARRAY_SIZE(self->hwpt_ids), variant->mock_domains); for (i = 0; i != variant->mock_domains; i++) - test_cmd_mock_domain(self->ioas_id, NULL, &self->hwpt_ids[i]); + test_cmd_mock_domain(self->ioas_id, &self->stdev_ids[i], + &self->hwpt_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -1274,6 +1276,36 @@ TEST_F(iommufd_mock_domain, user_copy) test_ioctl_destroy(ioas_id); } +TEST_F(iommufd_mock_domain, replace) +{ + uint32_t ioas_id; + + test_ioctl_ioas_alloc(&ioas_id); + + test_cmd_mock_domain_replace(self->stdev_ids[0], ioas_id); + + /* + * Replacing the IOAS causes the prior HWPT to be deallocated, thus we + * should get enoent when we try to use it. + */ + if (variant->mock_domains == 1) + test_err_mock_domain_replace(ENOENT, self->stdev_ids[0], + self->hwpt_ids[0]); + + test_cmd_mock_domain_replace(self->stdev_ids[0], ioas_id); + if (variant->mock_domains >= 2) { + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[1]); + test_cmd_mock_domain_replace(self->stdev_ids[0], + self->hwpt_ids[0]); + } + + test_cmd_mock_domain_replace(self->stdev_ids[0], self->ioas_id); + test_ioctl_destroy(ioas_id); +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index d9afcb23810e1a..baaea15b355297 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -569,4 +569,31 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) return 0; } +/* device.c */ +TEST_FAIL_NTH(basic_fail_nth, device) +{ + uint32_t ioas_id; + uint32_t ioas_id2; + uint32_t stdev_id; + + self->fd = open("/dev/iommu", O_RDWR); + if (self->fd == -1) + return -1; + + if (_test_ioctl_ioas_alloc(self->fd, &ioas_id)) + return -1; + + if (_test_ioctl_ioas_alloc(self->fd, &ioas_id2)) + return -1; + + fail_nth_enable(); + + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL)) + return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) + return -1; + return 0; +} + TEST_HARNESS_MAIN diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 85d6662ef8e867..3222f246600422 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -66,6 +66,34 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ stdev_id, hwpt_id)) +static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, + .id = stdev_id, + .mock_domain_replace = { + .pt_id = pt_id, + }, + }; + int ret; + + ret = ioctl(fd, IOMMU_TEST_CMD, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.mock_domain_replace.pt_id; + return 0; +} + +#define test_cmd_mock_domain_replace(stdev_id, pt_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain_replace(self->fd, stdev_id, pt_id, \ + NULL)) +#define test_err_mock_domain_replace(_errno, stdev_id, pt_id) \ + EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ + pt_id, NULL)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) { From patchwork Mon Mar 27 18:38:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667701 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 AC142C76195 for ; Mon, 27 Mar 2023 18:39:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232593AbjC0Sj2 (ORCPT ); Mon, 27 Mar 2023 14:39:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230424AbjC0Sj1 (ORCPT ); Mon, 27 Mar 2023 14:39:27 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2076.outbound.protection.outlook.com [40.107.95.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B41A3C0A; Mon, 27 Mar 2023 11:39:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cAiBgDY2rItLbNkVgn374syUOeC9oz/nLU1nkpjKgfZYKUO231CWI9SlM4PYy0vrM/r6hpD/iuOLqfujLSESL46Wfu4Uzm12NOEHwTtVnLBZfW021Ku9XmA5aYPHBDWiNpCX+bQO+1HPtngOVrZOaatv8vk+i2Kd7u6DXAAZfaci4OlrzOGzMa9N6wKE3SrLCuvgiUcoJ2mjdIFdpP4ebLNryApAIaCa5M4J9LQtXw7WWO3X8W3WkgDxiddkiiM2djh8VNuhyGge2IurygOuAjbNtM1hQs1KJI8kNU/LLVQd3mh/ct93KtkEXc2+F7MxQ7745DQgqQvGW8/FKWfxEA== 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=h6of8m/zmUIqjFvL5V0aE+oT9tA72MyKN4SFV097iUw=; b=ir3gbmjXB8PPDclxiR8twkHbOem8/ucY4D6RjZwFQicPzEYrMAg+lAKL27bigXx0GB/pAtM1ztzj33RKmPD9uFAnkNQMqG9V9ObW9oZUd4nxl8jwcNSbcHyjgeZflnrqiOnXizrZl18VviQ+oqhpyMPY7xe5ls5REVlcFgaV+sgk6hAGSGrVGkKeSo5407mb0eQofy1d2s8Thkr4ear6LZIjZxvLBn2RY0PkSBfVjvevJqMjlgAta0g+Fynp6I5DKbpXEx9W/1wbVkfQ8rFnXRg/BrGE9xyVXWwCKE4WUaPDJgLiEPepOoQlcOe9BKJpHijnZgodfbGqKgUkFW+vHA== 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=h6of8m/zmUIqjFvL5V0aE+oT9tA72MyKN4SFV097iUw=; b=L/NWUpVtwpChiDe0GSyQz3Gs7PfIXyfIWVHQEpQErdMR7DnUFEHsGzDbrJSUNWC96K0DWpP0zmzZ/feXFPVvJ7x1RVhOKJvg/3j0ALCbR/A+LVouOv/kQOmeyDv2N+yifKIOGiTiNBP6U3BGDn88CW/ZpTNr8vQ3FKSBc2ytUCHyuS9a3JRn1maLUuodfGhUaQUdzorMyd0gGlF3jtBSL7uziipAVpQk1Dv+vrUffehNDKB0FVSNp+b01ArkP9gdOzsNP7O9NNeO0Nlcxxo5SA5asrwhojLCJj8/sJt4Qa+Je2myEIfknGQbdJB9q1PhqF6mzcWQAIJ049B6mYLg8Q== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:18 +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; Mon, 27 Mar 2023 18:38:18 +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 v4 15/17] iommufd: Add IOMMU_HWPT_ALLOC Date: Mon, 27 Mar 2023 15:38:09 -0300 Message-Id: <15-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0470.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::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_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: ce00a320-7eae-4982-742b-08db2ef26bf8 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: O9XswjEXKB1kNyHqvM4jQHwT3Sevg15kxxvbksqyiNsDKX3xCqBk+5ff6Uif5UOde0A8CtvMl3mEYgNzRjyWMu6nozFZQ9cMhAwBAMUDxnEPPyr0sDFmfUhukBUCK4Q9sxny6VYQ2mxV+re4BkyYtkpUWuY6BCMeSnjbodC8SjmWyqcPJlsIspDvcYvXM2Cvw7yNCwFH7AXWFGQSoFi7END+jHLsbFwrxyJ6WAqx0wqnJcguc9OLOuTpga0E2/S/B2ywN0goh13Pwia0r1wUn+sJ0DfVEcQmihTFSpEoB/hst4xRwLH+xgmAJijDtStpSgMlbaqk+vRKaXOCgxAeKhWdJ8CDR7aZV4qQIc/eH28Ci4zBQ4u76+ZGG8yB2Af3+OZRcF/Cl8tG/Nn4MEGTsG35unTny2+dMp2TVAOYIqd3o7NIiYSE9ZwysAccUUaztPbtgTwhLTZsuoswDT06M3gtDB+C9VroiN5Vab9y+LnevnoGjyCiT0bhNwd5PRJX3644sCCobxIlr/62uKsrJPD+3nl+bd/Zj5Hx2ooLcEsxaytI+bbJLRSPz1a6X5OB 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: C3CLQpbqGHOhrx+xSIZre0RDH6YCS05XIuDFsFe8wapiqCwSlxh26F795J3nt4qWKyV1NMx/m32QQNNKMY1odA3vqkdC8TND6EclSYtpBpOIyVahyN+26zsfTWlGYN2+9mzPnd2KKvyxUJBc0RoCHLiyKou6rLeFnhiNu4TFasAiXjrU7TJ+MGkiZYXpvsqQzqX/x7cBoCZgrtY+cJmnSfT6AQPDv4bnNfYCJvTIru2BWXS7NPrp0RHskQ2oyA8x4N8NmE95oA1KzD1hLblxu5BGABPpQZEsjK7oE/icFFHXAO7dm/IE9DQfs2m82q47Ptch/ruw3WsxgyJV0VrJoJEDH3MvCW0kj2FYF/2H00vCIFvBWEbMA/I5T6hBmAB6b+6qhCaWS5vmxWPUeYTIz7qaBrb84szWNzhgfwS5p2/I3KT0BhbwqROplvZPbS3SBDLVFdqKMMh3TA0jaV8VjIXL+2xENO5ZGNzAM8GDzerVrQBxz1+aZOxsim01p9TQxvgRZhW9pDkZBvYOHxa24T7WXBVkdOb6YpOIl6/OACpcKwaV2lrSMTUvhbyby+T4zHJts6zy+s6fzkFeZRQ0+7FS3uIocIweKLAIFpHsby6g4gBE3yQAOmWNm4ApdV/Xur1/tSe83D5sp+25TqxO6iafrQiltSudnDKiHiwH8oKun6nYtUkCRqy/93BOlUGjkOsXVIvjtb0o/UP2nMNVGaXdMthFboZne9ahivdiNj1sOTOQE3VJKaUXnYqmk6VpMS/xRNAWa0hikgxHgQ82ybQnvbW+q8OhKWRo4eTxVD86kR/zbWsUsBDg0gaOIIBbPueiycxLlNNmqKSBC7aPBO+D8OGLHNJiDmzqVPXP+b4+8TojmD+tGtvhR9H3sB+MLwm19K4Azr+Pd6JWcr7oGwX6nTKWlu3rYXF/QLXvVkbwwQzxulI1dLyFnW4ZpqV+qjRQ9IICfyP/deGMcxPVMPUaG86FoGg4WF9rFluFLC1cNWvABJu8Kin8TKiRYiYZruQBFtZUjWXALOwRHSI4nfMc9fJtzrH/cNveZ1zhyoPUdR+0Mr5oV+EQZmWTbYc8g3Q/gDacuqX8/mRqxACaEROpykE9L1O5P3P0I1jGsiXw8w7ELfD35RCQ2/hWiz9+Op2f8jlgiNoP9CXnDOK/k5dxjoFd3QrCT4tACBybetoOWCKOZWxMVFjU1ZkubgFwnhHt0bpRa3JJWnpcwZkq5c0nhoBFMdZwyUxGi6g0PMq2p72nxBY3YA5I8lUsuM2KTB3FAKxRqscwD1wRWvG/YRIZ9ZPs5WtLVOqFSXlcvPkffizS6oAir6ZBARcEV4HJfv8wWprrUdiwLnlvm/Kvj1Y/7ZMNrfafN7eSi87MoLuOeL6bSzx1Sos3RNuRlIEj2qy9LajNArDxYGYxKRixN5bjh4Ie6hM8728QULM/F4XeCaxFU1n8RX1tuNmkuTe6NRkq+URarQjtJh2gUT0LbrXrN8jM/zAlpd0ZK6LLtbfagGioSxucVq0xhDJiJZFdh8mp/kfVVpsTd3vft3HxASDvj+kwoxO46mv+xIrBg/spw+dLEhdETIebBHm/zf5B X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce00a320-7eae-4982-742b-08db2ef26bf8 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:13.4902 (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: xd8I3iwRu+Z7ibMMmbAYJo2xloA65CzoYxs2/4gTMmVpqmzXi9SH7VaGOz1KFLKs X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This allows userspace to manually create HWPTs on IOAS's and then use those HWPTs as inputs to iommufd_device_attach/replace(). Following series will extend this to allow creating iommu_domains with driver specific parameters. Reviewed-by: Lu Baolu Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/hw_pagetable.c | 46 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 9 +++++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 26 ++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index c5c0ef9912a13e..8aa9ac130b5960 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -3,6 +3,7 @@ * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES */ #include +#include #include "iommufd_private.h" @@ -115,3 +116,48 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); } + +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd) +{ + struct iommu_hwpt_alloc *cmd = ucmd->cmd; + struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; + struct iommufd_ioas *ioas; + int rc; + + if (cmd->flags || cmd->__reserved) + return -EOPNOTSUPP; + + idev = iommufd_get_device(ucmd, cmd->dev_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + + ioas = iommufd_get_ioas(ucmd, cmd->pt_id); + if (IS_ERR(ioas)) { + rc = PTR_ERR(ioas); + goto out_put_idev; + } + + mutex_lock(&ioas->mutex); + hwpt = iommufd_hw_pagetable_alloc(ucmd->ictx, ioas, idev, false); + mutex_unlock(&ioas->mutex); + if (IS_ERR(hwpt)) { + rc = PTR_ERR(hwpt); + goto out_put_ioas; + } + + cmd->out_hwpt_id = hwpt->obj.id; + rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd)); + if (rc) + goto out_hwpt; + iommufd_object_finalize(ucmd->ictx, &hwpt->obj); + goto out_put_ioas; + +out_hwpt: + iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj); +out_put_ioas: + iommufd_put_object(&ioas->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 f80b012e1bc200..cb693190bf51c5 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -261,6 +261,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, struct iommufd_hw_pagetable * iommufd_hw_pagetable_detach(struct iommufd_device *idev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd); static inline void iommufd_hw_pagetable_put(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) @@ -297,6 +298,14 @@ struct iommufd_device { bool enforce_cache_coherency; }; +static inline struct iommufd_device * +iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id) +{ + return container_of(iommufd_get_object(ucmd->ictx, id, + IOMMUFD_OBJ_DEVICE), + struct iommufd_device, obj); +} + void iommufd_device_destroy(struct iommufd_object *obj); struct iommufd_access { diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 9cba592d0482e7..694da191e4b155 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -261,6 +261,7 @@ static int iommufd_option(struct iommufd_ucmd *ucmd) union ucmd_buffer { struct iommu_destroy destroy; + struct iommu_hwpt_alloc hwpt; struct iommu_ioas_alloc alloc; struct iommu_ioas_allow_iovas allow_iovas; struct iommu_ioas_copy ioas_copy; @@ -292,6 +293,8 @@ struct iommufd_ioctl_op { } static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { IOCTL_OP(IOMMU_DESTROY, iommufd_destroy, struct iommu_destroy, id), + IOCTL_OP(IOMMU_HWPT_ALLOC, iommufd_hwpt_alloc, struct iommu_hwpt_alloc, + __reserved), IOCTL_OP(IOMMU_IOAS_ALLOC, iommufd_ioas_alloc_ioctl, struct iommu_ioas_alloc, out_ioas_id), IOCTL_OP(IOMMU_IOAS_ALLOW_IOVAS, iommufd_ioas_allow_iovas, diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 98ebba80cfa1fc..8245c01adca673 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -45,6 +45,7 @@ enum { IOMMUFD_CMD_IOAS_UNMAP, IOMMUFD_CMD_OPTION, IOMMUFD_CMD_VFIO_IOAS, + IOMMUFD_CMD_HWPT_ALLOC, }; /** @@ -344,4 +345,29 @@ struct iommu_vfio_ioas { __u16 __reserved; }; #define IOMMU_VFIO_IOAS _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VFIO_IOAS) + +/** + * struct iommu_hwpt_alloc - ioctl(IOMMU_HWPT_ALLOC) + * @size: sizeof(struct iommu_hwpt_alloc) + * @flags: Must be 0 + * @dev_id: The device to allocate this HWPT for + * @pt_id: The IOAS to connect this HWPT to + * @out_hwpt_id: The ID of the new HWPT + * @__reserved: Must be 0 + * + * Explicitly allocate a hardware page table object. This is the same object + * type that is returned by iommufd_device_attach() and represents the + * underlying iommu driver's iommu_domain kernel object. + * + * A HWPT will be created with the IOVA mappings from the given IOAS. + */ +struct iommu_hwpt_alloc { + __u32 size; + __u32 flags; + __u32 dev_id; + __u32 pt_id; + __u32 out_hwpt_id; + __u32 __reserved; +}; +#define IOMMU_HWPT_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HWPT_ALLOC) #endif From patchwork Mon Mar 27 18:38:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 668440 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 9683CC6FD1D for ; Mon, 27 Mar 2023 18:39:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232573AbjC0Sjc (ORCPT ); Mon, 27 Mar 2023 14:39:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232578AbjC0Sj3 (ORCPT ); Mon, 27 Mar 2023 14:39:29 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2047.outbound.protection.outlook.com [40.107.95.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7188E46A8; Mon, 27 Mar 2023 11:39:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UX64sHWmHEf98yTpBqsXsjc2pDlmBebjQ4tSleLd8gGG5oQM7K3NTrk2u/JcPL+qCYc0RbB4ONPHL2oCZwAFBLuL3Y+3P2/SXW/WKvVrTpEumbY/kzNPBfOwommvqRj6i6dj4+dwJFnkd0ojfNiPlrSQfDwKXwRDsPeDa6GprQ1hV58P5W6dYRxyuJQwBaFqAeOhbidNdmAFRKZ94lribVEgRltOcY1cP6tP0Jo+siHU5bkNGOdg4AktyYDydqu/DnTLJD+kXRdImX6xsIwfqczLJaEGGhOQ9GZO4Qu3qnWuXSns12UIw0RiMVcYRAjpIgIRZ/8DwRxzF3Kn1OL7JQ== 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=KOQxQdd+wigXZPIqPyxdLQUDfhoEImkXV1bAoVhAh48=; b=bNsgI87cXGH8Y9dAKx0WjBcMmHb0LfHBJSGamgF28y14MyG7SOjfJvrgNL0tPaZPki4EVy5gy2bxSAkCeUD9sJXOT7suZNF1h76uhJrmXHno16n/xe2IfogzpVvOiB8irfKUeATKuV7EGk0jF6YfgdfgUfc2UfeGr6qaCAe/nYWHoUfgJLNKw/Yi9oMj+eki77LxmRfTS+SlqeI9W/zQK8teXMVyv6SlWU//Qn8z4MZveJtfIDdgVICDYWdiTYd44VA2hl/02MgYxadJkB1I6hCT4sJlRH1fHjFYzzMKowbuOQetkH1c1lb1A04mtb6YWJhnaFlWfn/K/dUZK4Z+2Q== 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=KOQxQdd+wigXZPIqPyxdLQUDfhoEImkXV1bAoVhAh48=; b=DR8l8FzIeQicDI33p1ucVU+NHdlreGlyH4Sqowjyb3wMR0QSAATBisNqDx4ladnkukzhF4GnZPTCkot3GP0DKsSK0WCD5ldVmjSd2byu+MLPwemrYDvrwAGilPU80f2oD1cRSa0oi9MvW/0QPMINhkoo5T0A6q9ijdjvST87uPtvsLBhhfblj5KDhBEpmbQKFItV5xE+lb6aNtjUscS0P8IuRDAUOJqO3nDh4MfLpJFxVqQCnIaJnA9CFNQBsLFYYZamZ6U+y7XJYa4831JBG/gxx5IMn8lBLRMfRBOoluakah6uf1TlWPP26khAQA3i1XOW7s41pXruHBt06HCtqw== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:19 +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; Mon, 27 Mar 2023 18:38:19 +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 v4 16/17] iommufd/selftest: Return the real idev id from selftest mock_domain Date: Mon, 27 Mar 2023 15:38:10 -0300 Message-Id: <16-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0180.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:110::26) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: b00027a1-d862-4e0e-9116-08db2ef26bee X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hQwedcHDzQkVoPSyJp5IzR46JGeVFuaLr8+Fki39L0oSa2mSu3636GxKY08RprmnGqivj3lWkjkDMB4zsEpoPQxwkJmLBBKRFvqqkm0eZgxvcD98mwewXpoH5WBN1rnpCZQjbNThNm7VybMoWiuQMet6Z/kgphKvhrK3iC9yrlwZgfO2HusaEj+awADcY2Iv3uPPvCweNMXnKDGzqDgrrYXAFaL743HK70hc8RoPqJBDqWH/KabGn69KJiewn7J1gNDNmukW6BRTiP9cxYR9hInTNdk4QQd0KduWXpBTiESdrBs2czqyygM9UJAdoEmYVrFH9e6jz3aNim6Uslp6jRrEce4ooNyup/X2FnBfbbHnj/EQpW2AoDY/UepHmvIJSbqrGlbOOFs2afGkHo9QQP2GyqlYmYksISyCIeQbhYmqI7vDSEP6o8eCf+vepAN3U9jGvlkDlVx1QlYE89qamLOLWehyppHljP6ZBDEJbmGUbtOYOTpIZafjKdDLio9dnaplLyuyoywW7dl+H9Je5M8gEZinUfaUtDF213gum+HfmUVvtnaSBZfCMfE90rT9 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m35F7WIuQjgQq3TIv+a86Axr3oWJNWb8tGO2qMA1YBMeool1BZLbiCQ2lMurWaRACguZVQNnRuos5zACj/bjJHZt8c0PYha+Uysaz8O27Evt1OdfY+iHrKGzB9qkn5R+C6moEjSyoaWOSRVyULnXo0maUEXaHv8pqPuKLrxSALdkUKrUNeguqpJConF0bznCWly5LJPTUGhNLMrWlHK6jMzYLApDy/B/5Kb9h51qS7AqIIxbaJHYQ/5Ii9Nxgej9p8qaMtg99iRZ6+aQ1YCAlbXbapj/bPKRtNFkZeHfaLr1+m+WNVUUgsL/aaQqGWDgxS69l4BO6vRkuukZ1qND8Bf318BIUydEu9Z+6sKv/8TilTNPDe96rDdvCJ8b9JWW6IIW+5UGtyy+A4JrL5UFTqRS/uvhtuZ3Rm3IqZ+69A8u1jPEaR8ZBx2AIdhD0sA8A9fC9u9mGY27KTnKm2D2dvFCqBhQkudYih3SI1gq9vrMw2+6hHq/717ZC9T8THTEv9aHax4FrfgfZgJQZZKNwAxhvmZzc+UFUd+vIO7hJ1ZJ4uiONOf2Ohmtn5WwL6v7gcLj9Y4X89fZylpMQlGDiRmg9vtMdw3ZKAZaoIScO4CyyuhNYqk9CmuPsv1F2w9dROVRn2FrSjOtCpXzFK3YJ4b3TmnaLlUeKd+lxA5/oj1rCmY4emov7IPSiuK5nlCUHtYbX38d1VvzOe1yYQyYCBq9eyaAQwPFDkJWjBYswLbmZgokpyrBkMMWyEUr3OdVhmE0OsQjBCLyZWf6Evj6b/65uK4/8e4E4t2ooxEnfNEho9OO5YWlIFe51BPfhDN2we106P8QeeRRu/WcKVBGVq9w/AHchMc7l4gkgQQzWhLEqKGWnJmesfkaCW1wILSAOHiSUlKbP21Z+bBWXXWm+aUndlKFnhkXCbkyPY07p/lGp7uaL/Dp9vsTCglGhW4L2a8SiLzaDAmUPC1TBT4GdOs4bfV+kKQuD+HiZHMlac6A0f+XxjD6BsJX49C43ktIdfRyr5+Wecwiq93pNmR/eOXUl9r1RTrBiHI5swVbloZ15ANEvX4mdvMI8Bb9LJFhUx+/flUwLOd1ga7fT3e/tt4qUxH4OHRjtDdJDjPKr5rzDHjKxZKLdV8lNlYOllaAliKG69Y98rcg8uXk7OiZVkH2pUJIqrfbCfPQbI7oyh8G9bHYnfs3wcFt4pDQ2CKy0c7Yp8CYDYKfVxRhrSo+GYywJiJHthS2Nj10vIDkKC6FKwPAgSwaKgVnP7W6ZoqhOM99GB+0wAlTOUwEkcvF+2ESE/41ac5rEUXYkbSjFQZ8KG9EKDcMorh3mTw7cCgIiQ6S+xIG7PW2cp4xMNDCNm/3g8N4sUseRCzBmMvsqgk0/f8QgPIvtW7b2jGz/uEYJglWLwPOBxOJCAqwnUbMmpRhsklh5iX7Rjrp4LqBV6GQPi11KtjfdGp9bAbNzFoN0UB4mbTJL/RgdxbDX+i2/59YAI007kjIcX5kVCHPjBiz853skyCRpewRiKjajl+jn2VSSpwOovASM4nhZBALyEQz3OFgwuh18XFSNs5rOx3Z7llGfib2K8C27T4zn/6b X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b00027a1-d862-4e0e-9116-08db2ef26bee X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:13.5214 (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: cErLpWSOKyjwBmAi3+4Pbx2cg6Z+JWpmpCSOJAF/62AFGJ7sd7jiTuHeWXs9tyGb X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Now that we actually call iommufd_device_bind() we can return the idev_id from that function to userspace for use in other APIs. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/iommufd_test.h | 2 ++ drivers/iommu/iommufd/selftest.c | 1 + tools/testing/selftests/iommu/iommufd.c | 17 +++++++++-------- .../testing/selftests/iommu/iommufd_fail_nth.c | 18 ++++++++++-------- tools/testing/selftests/iommu/iommufd_utils.h | 12 +++++++----- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index e3f1035cbd6464..dd9168a20ddf4c 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -52,6 +52,8 @@ struct iommu_test_cmd { struct { __u32 out_stdev_id; __u32 out_hwpt_id; + /* out_idev_id is the standard iommufd_bind object */ + __u32 out_idev_id; } mock_domain; struct { __u32 pt_id; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index dd792294298ffd..a9c22560f51aa9 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -443,6 +443,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, /* Userspace must destroy the device_id to destroy the object */ cmd->mock_domain.out_hwpt_id = pt_id; cmd->mock_domain.out_stdev_id = sobj->obj.id; + cmd->mock_domain.out_idev_id = idev_id; iommufd_object_finalize(ucmd->ictx, &sobj->obj); return iommufd_ucmd_respond(ucmd, sizeof(*cmd)); diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 77b0601fd13a71..83c98a371f9e7a 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -214,7 +214,7 @@ FIXTURE_SETUP(iommufd_ioas) for (i = 0; i != variant->mock_domains; i++) { test_cmd_mock_domain(self->ioas_id, &self->stdev_id, - &self->hwpt_id); + &self->hwpt_id, NULL); self->base_iova = MOCK_APERTURE_START; } } @@ -265,7 +265,7 @@ TEST_F(iommufd_ioas, hwpt_attach) { /* Create a device attached directly to a hwpt */ if (self->stdev_id) { - test_cmd_mock_domain(self->hwpt_id, NULL, NULL); + test_cmd_mock_domain(self->hwpt_id, NULL, NULL, NULL); } else { test_err_mock_domain(ENOENT, self->hwpt_id, NULL, NULL); } @@ -650,7 +650,7 @@ TEST_F(iommufd_ioas, access_pin) _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_PAGES), &access_cmd)); test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &mock_hwpt_id); + &mock_hwpt_id, NULL); check_map_cmd.id = mock_hwpt_id; ASSERT_EQ(0, ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_MD_CHECK_MAP), @@ -805,7 +805,7 @@ TEST_F(iommufd_ioas, fork_gone) * If a domain already existed then everything was pinned within * the fork, so this copies from one domain to another. */ - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); check_access_rw(_metadata, self->fd, access_id, MOCK_APERTURE_START, 0); @@ -854,7 +854,7 @@ TEST_F(iommufd_ioas, fork_present) ASSERT_EQ(8, read(efd, &tmp, sizeof(tmp))); /* Read pages from the remote process */ - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); check_access_rw(_metadata, self->fd, access_id, MOCK_APERTURE_START, 0); ASSERT_EQ(0, close(pipefds[1])); @@ -1002,6 +1002,7 @@ FIXTURE(iommufd_mock_domain) uint32_t hwpt_id; uint32_t hwpt_ids[2]; uint32_t stdev_ids[2]; + uint32_t idev_ids[2]; int mmap_flags; size_t mmap_buf_size; }; @@ -1024,7 +1025,7 @@ FIXTURE_SETUP(iommufd_mock_domain) for (i = 0; i != variant->mock_domains; i++) test_cmd_mock_domain(self->ioas_id, &self->stdev_ids[i], - &self->hwpt_ids[i]); + &self->hwpt_ids[i], &self->idev_ids[i]); self->hwpt_id = self->hwpt_ids[0]; self->mmap_flags = MAP_SHARED | MAP_ANONYMOUS; @@ -1218,7 +1219,7 @@ TEST_F(iommufd_mock_domain, all_aligns_copy) /* Add and destroy a domain while the area exists */ old_id = self->hwpt_ids[1]; test_cmd_mock_domain(self->ioas_id, &mock_stdev_id, - &self->hwpt_ids[1]); + &self->hwpt_ids[1], NULL); check_mock_iova(buf + start, iova, length); check_refs(buf + start / PAGE_SIZE * PAGE_SIZE, @@ -1427,7 +1428,7 @@ FIXTURE_SETUP(vfio_compat_mock_domain) /* Create what VFIO would consider a group */ test_ioctl_ioas_alloc(&self->ioas_id); - test_cmd_mock_domain(self->ioas_id, NULL, NULL); + test_cmd_mock_domain(self->ioas_id, NULL, NULL, NULL); /* Attach it to the vfio compat */ vfio_ioas_cmd.ioas_id = self->ioas_id; diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index baaea15b355297..7a3d149019da92 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -313,7 +313,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -324,7 +324,7 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) if (_test_ioctl_destroy(self->fd, stdev_id)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; return 0; } @@ -348,12 +348,13 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, 262144, &iova, @@ -367,9 +368,10 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_destroy(self->fd, stdev_id2)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id2, &hwpt_id2, + NULL)) return -1; return 0; } @@ -526,7 +528,7 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) if (_test_ioctl_set_temp_memory_limit(self->fd, 32)) return -1; - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, &hwpt_id, NULL)) return -1; if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, BUFFER_SIZE, &iova, @@ -588,7 +590,7 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 3222f246600422..e67a929a5c87d3 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -39,7 +39,7 @@ static unsigned long BUFFER_SIZE; }) static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, - __u32 *hwpt_id) + __u32 *hwpt_id, __u32 *idev_id) { struct iommu_test_cmd cmd = { .size = sizeof(cmd), @@ -57,14 +57,16 @@ static int _test_cmd_mock_domain(int fd, unsigned int ioas_id, __u32 *stdev_id, assert(cmd.id != 0); if (hwpt_id) *hwpt_id = cmd.mock_domain.out_hwpt_id; + if (idev_id) + *idev_id = cmd.mock_domain.out_idev_id; return 0; } -#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id) \ - ASSERT_EQ(0, \ - _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, hwpt_id)) +#define test_cmd_mock_domain(ioas_id, stdev_id, hwpt_id, idev_id) \ + ASSERT_EQ(0, _test_cmd_mock_domain(self->fd, ioas_id, stdev_id, \ + hwpt_id, idev_id)) #define test_err_mock_domain(_errno, ioas_id, stdev_id, hwpt_id) \ EXPECT_ERRNO(_errno, _test_cmd_mock_domain(self->fd, ioas_id, \ - stdev_id, hwpt_id)) + stdev_id, hwpt_id, NULL)) static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, __u32 *hwpt_id) From patchwork Mon Mar 27 18:38:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 667703 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 56FC5C76195 for ; Mon, 27 Mar 2023 18:39:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232475AbjC0SjG (ORCPT ); Mon, 27 Mar 2023 14:39:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232170AbjC0SjF (ORCPT ); Mon, 27 Mar 2023 14:39:05 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2076.outbound.protection.outlook.com [40.107.95.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61C221AD; Mon, 27 Mar 2023 11:38:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mxYW5GjUtnzzSHOrtZuuIbvHqn2Cy45EAb0LQ8jajOwYSsQ7CrJXjQV70yLnfLUmRP+2rf/XnBWWhq5NDdSvGKfeY9FL5d0vgL1kckf+K3E/vnhzFnOsYxh7dXPbIk6lqmxrkb5xZaAjU9JqYqXte/EZpTFP7kzTKED+aOkUfAcOuEt2DgtqpwO+AEPAAZLgIvZNzozKK8EfIImplZgyg4xGe1QgWaEDG7xRAkwkPjC1TSfakmCf97Mw6r/JANifioTFUh7Li1uvjcSAe2k0iA/0Z5pagb9F8TaXRt1iJUrI1jNvILPEucwF35XKwshlJ/I37CXTMG+quc2IbS/mPw== 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=YPFv55G9dMElzX1sVD7XmlLL1r1w5TkbGUmLhCo4NDs=; b=mOMsbhdqBdjRZvMwjV0FYdYxTTiDypqmhYNWlxMhVsGp8E5v267IWKFSRNyBB1GvoylNXFkdsRqzAgapzqtKoxvrhxbL2MYSzdbdxSr9JF/7V2mg3OXJVjUQzuDKScb/4h1HH55WTQoNUx5+1Xi2OJkIfluWKFnmojhvK2wAYqhN0ujNOywIu5cwVw1qFI9JJIZL6k2JZcpJYJiXipbx+JiWEAofQgI7vplz//qklylrmtINXxV1VXcswVXWLw6pZYDzUrXxVBAQpJaEIPkh9xBFt/EkxwGmFJKGD/qAdZunRluzXz4L9y4sc4P07kNhJ9CxqZomGpcEMRvuxIqvpQ== 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=YPFv55G9dMElzX1sVD7XmlLL1r1w5TkbGUmLhCo4NDs=; b=VFf9nwu1HQEecT5OKBc5c8Kqmh54RcIO6JMixRL8Didgw1rP0mypMZooU3pYaWxOAXRzRiF/MFnpSpMNHQyg0B+hh9yfBJpoDHEKLyp6e4Dwj07bjh+XNImxlax9S4kfAZstmDCWRrasx5DxH8LAEHyZFrJXidT/JWADIIxWEmulLmoIJexX6ieL6mcHkP1VOJKHE9407jvBg3nHCbcpLZ4JRcc7Ft2StjRlmDJRNJ7sB6haqXKRDJ1ggUmAW5YCganWirSPdAEkhexvcB9oB89J7kNf221ao2v8fWkLNh4z+gMsDUColB5XBZSp+gyLYCsCdeag5UTPwNFxUXycIg== 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 SJ2PR12MB9008.namprd12.prod.outlook.com (2603:10b6:a03:543::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6222.30; Mon, 27 Mar 2023 18:38:16 +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; Mon, 27 Mar 2023 18:38:16 +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 v4 17/17] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Mon, 27 Mar 2023 15:38:11 -0300 Message-Id: <17-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v4-9cd79ad52ee8+13f5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: YT4PR01CA0391.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:108::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ2PR12MB9008:EE_ X-MS-Office365-Filtering-Correlation-Id: 2b8cad41-6fdf-4b63-f92c-08db2ef26bb2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KoHjRHjJc8/FuG9uZzVfjl/mczq101Qh9p0U4ZkU/MV6zJ/YyIaFurFxHtHNvzZFdkgzSnLJfMqWMF51Dh6m12RxLVyazgQooShWQ51pzvV1QcYohF4VSw4xwOawQVWPkFQPeX4eXwGI9rlmswIm8ZlXe9ajsPflVZB8rVI4urx7IjVnWyG5YWd1d470BVDz/gaLKPPJ85sJ3uTqfDPtVIzdefCgR2wuVj0AiakDx0C7Ef2xsdwZ3+HKBHklsrRBtAMYgj32H/DnUHrrMbisRnVsbyNBU8QP3h1wBj87qEPYfeEtBr24ZYeB8sFMuWOw/0eTA8/wb3acBOVW2EGSJ9NlxaEBRE7EuMKaA6KcIg03hcyTBpZHZchTlfFnZsd+q3F7VHVvXygXvx/etFt82XWjgmiOIJuz+9ZjJ1EUhkpBDQiHEDFVRqhLhSLmc4o9Q81UjbMsmHDVB8E1JjhBUTMOfQmkqRNJ2ZtrKxBOERU3VmmKRAb9ekttou/Y0qG75+41TN4pgAtUEBLwPq2xfHUc7m/4PE0UL1L0Mrz5Y83l9jHOA+bdJDlJk3Bsprbc 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:(13230028)(4636009)(346002)(396003)(366004)(136003)(39860400002)(376002)(451199021)(6512007)(6506007)(26005)(54906003)(6486002)(316002)(6666004)(4326008)(186003)(66476007)(66556008)(66946007)(8676002)(478600001)(41300700001)(86362001)(5660300002)(38100700002)(2906002)(83380400001)(36756003)(2616005)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2r/K4ryyMCUGiYY1hebSMFM3wkxklHpVNpqdTPa3XCQ8o6nDvit+DLLUtr3StfEZ+BXbOIE3GZUcI7aMZ81i2th0X1F+ku1J9mzElUi0DC+CJTQfZA/wdIICYZeZpr7xovxNKu4Gw5graioynwJFQMor7zZXZdVwdbeg6oADl5fwnGapnuY1ekppziCg6tRXUMxRFsrrCEhKbSKuL/XWQjFmArwiDL3rxKiVm/dKtDyQJa7T+hCU44jqz8iFtVKsLcJ8EEKnBFtQYtwRs4qcySK321YKlz53Ftdf+CLtpJOP07CYlk/+HLsl06rvZLMBYQaXCH+QkbOJumdXQUKmmJOmARZfiBl4kWQRfInJYUSJCQThOGOx0jTsPGjTAd+ToloV+iNgVhr9G6a0siYYuUefWv7Uo5DAAjuvld0Gvuve+pXej5DXvLWA4pyrkxIFIajdWHF/FoW/2AlmCK6NaAVKpuCSx83EQunEDCM9c+IXpHRAfGDHZG4PVK8rU7zuadFVTO7kXncRri0lwzMgoC55ZuMErkfmkke2Dcw1esJOh0z9UGJjWd7rBanDcfXLi9n7zPvIbfGhZGCFT6JE5peIGgbZT8hqy1aVc4z30RsA0utHqiv4ljBnUNAJGpJE566JToUjPzJhUJdmqNJEuTQTMOVUTFfh4gnCRJppolJNaXYboO8m4SF33aVBhY8PERZKSu6N4+u73JIta7hIRvudAGD3Q9Kn4EMwxM06y9ea7oFUWbyTKjiVTValOM4PjdTvQydmUSgOjeaNXrHgbAxmVJGXAJelXPIeq2g/5ZZv241DKKYvx0UMfDzPeJUmDflHA5/P5X16vmIApFkNg0os5dhiAbSrlCHSiQ2XtTe+QyP5Cbv7IsH5y+o4FdtrDHAKTjchPZLecxDzVMTb0TKWZu14CX4nbU9KqtNRHaw1ek6wFdi/pNDpCTu7xMklwhNPxTohFKMCiGW8qtX409Z2DEgWuZ7QKlGRx9dRYYZduFZiTBn4OXIr3/bo7tFQ6NfGFWD4vrm418ErU5w+2uMGjR/8XchjwWBVTnvNm1VUXF6GSpuiaHhTZZHy3fk+X4Wkkv7IHFmOFeO/1KB0z1hU7pJNQoNttAAL5htrmoiVmLACu2RMAvKJOY0mGXYLt8uCNmm+u9pe1nDSHg/6ePyICECC0HcWK4vOf48auYg7iSYek1ikVGWWgWCcUDpyf79fVqt5vmh4zuaFJEra35f4lbO9KYTOb8RN+hqlOGFfVNLA10uFedWMKNCoA6bvplwLucXwhvhjBHGMprMzYWdRTbTncVwiE9PbKC/Mlf7KIEOcw/4MrLVhZAI+XIQA6TMWDEsPS/VaXOlsP7mh2dkSrQaO8AfhYiY4qciH8uW4c2/NVsjgv3GCioUmAuCKy326js5JLAIYpMYihzNRbxw1wrcuO8SJKzgwhUPODFsOCa7r2eTotYF6qBFYyLRyyGmlFPp+oRitor2A94+MfugUkFrAQE+OVuL0Kyqln5MOs34ZkqIaesr68lKIwQ+ApzylhStHzqBILcBK5Wf6k0uCqyfffTS21EWJwEmR0Mmzo5KlnE125kM8pi9eajAJ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2b8cad41-6fdf-4b63-f92c-08db2ef26bb2 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Mar 2023 18:38:12.8630 (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: jJNVv0bSDJ3PTa9JLooq9cRRFyNDiMfkeJG0Rjx6ZPYmYm3FSnL+WdStjF5sonun X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB9008 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Test the basic flow. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- tools/testing/selftests/iommu/iommufd.c | 15 +++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++++++++- tools/testing/selftests/iommu/iommufd_utils.h | 21 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 83c98a371f9e7a..c07252dbf62d72 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1307,6 +1307,21 @@ TEST_F(iommufd_mock_domain, replace) test_ioctl_destroy(ioas_id); } +TEST_F(iommufd_mock_domain, alloc_hwpt) +{ + int i; + + for (i = 0; i != variant->mock_domains; i++) { + uint32_t stddev_id; + uint32_t hwpt_id; + + test_cmd_hwpt_alloc(self->idev_ids[0], self->ioas_id, &hwpt_id); + test_cmd_mock_domain(hwpt_id, &stddev_id, NULL, NULL); + test_ioctl_destroy(stddev_id); + test_ioctl_destroy(hwpt_id); + } +} + /* VFIO compatibility IOCTLs */ TEST_F(iommufd, simple_ioctls) diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 7a3d149019da92..7e1afb6ff9bd8d 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -577,6 +577,8 @@ TEST_FAIL_NTH(basic_fail_nth, device) uint32_t ioas_id; uint32_t ioas_id2; uint32_t stdev_id; + uint32_t idev_id; + uint32_t hwpt_id; self->fd = open("/dev/iommu", O_RDWR); if (self->fd == -1) @@ -590,11 +592,18 @@ TEST_FAIL_NTH(basic_fail_nth, device) fail_nth_enable(); - if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, NULL)) + if (_test_cmd_mock_domain(self->fd, ioas_id, &stdev_id, NULL, + &idev_id)) + return -1; + + if (_test_cmd_hwpt_alloc(self->fd, idev_id, ioas_id, &hwpt_id)) return -1; if (_test_cmd_mock_domain_replace(self->fd, stdev_id, ioas_id2, NULL)) return -1; + + if (_test_cmd_mock_domain_replace(self->fd, stdev_id, hwpt_id, NULL)) + return -1; return 0; } diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index e67a929a5c87d3..9b6dcb921750b6 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -96,6 +96,27 @@ static int _test_cmd_mock_domain_replace(int fd, __u32 stdev_id, __u32 pt_id, EXPECT_ERRNO(_errno, _test_cmd_mock_domain_replace(self->fd, stdev_id, \ pt_id, NULL)) +static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, + __u32 *hwpt_id) +{ + struct iommu_hwpt_alloc cmd = { + .size = sizeof(cmd), + .dev_id = device_id, + .pt_id = pt_id, + }; + int ret; + + ret = ioctl(fd, IOMMU_HWPT_ALLOC, &cmd); + if (ret) + return ret; + if (hwpt_id) + *hwpt_id = cmd.out_hwpt_id; + return 0; +} + +#define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ + ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) {