From patchwork Tue Apr 11 16:43: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: 13207901 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 AC145C76196 for ; Tue, 11 Apr 2023 16:44:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229503AbjDKQoi (ORCPT ); Tue, 11 Apr 2023 12:44:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbjDKQoe (ORCPT ); Tue, 11 Apr 2023 12:44:34 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F131526A; Tue, 11 Apr 2023 09:44:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cPNSmbPljrsexb5Vy1mkfCLGZiHoa56a0O/N7OhjGihMjdyJho620A+2ugnd2Dszrshx64liM/qKYIwKTJN0HPsAH3+iW+A4khUtkMoguYGhqQcg3IFdlJp7KiOiyzvFHV5vtxfhLxXeCDxuZRD5VRP1cyFr1u4JU+es5qn1klKrOl7uIE3lTMTRHC8owU2PhWD/acf9FfOTZsZtihT3M6Ecwi6ZJsA3Kj5puGXKVw04nPecm8yHoDMDUlbbe4QhZT2/xAAqmdBMUqvrtUypw+91tWuaWKRIr1DY2u+tUa9jyQjfofD6pfgtLHxSbxIa518edKgIi3htgadU05jrEA== 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=XtgGVHRZX3TtHmLPkiQt80F7Qkh9qbegMNa3INx5gbLzh6273q/vuXiOKFHPpAGkGyuG79Qnk7vU83ed6wIIV9/Zh66q668Kk3oFugkN/hMxZTGUBlZRD++dIsJ9osl77X46ijJPtBWnzuTH909b+lbJoPrs8Gy4r0FtPlHXuXCN4yJ8SBoTd+ILFXyd6/GJdHatu5XSv0qeM8d2nRmIMGPr6K3/pw+yKSehRVLN66b0qoowL9TC4meC1EPcc0hKKDeXMVr4T4m4TR8gJGKeDrzyTsU9DVZvJ2G1UCgw7USPi2AEKFEZkJfXzZW2y8BpXJwgugMAKCem5AvfYkE+2w== 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=Bf8KfX52eTYKFjxGmfUsP1bsgDeX/c8+2obxtLxy8ZmNuECpE0V5tmyEyvL3n79SWcsVPcSDj/txLBgbCwq75dPt41M8Dik5FdInEdWBpBThx2Y0MjixZ9OvkIsO3jxpk1glUmS8f3E1FbPbZ041eokxRVtg/IRUKbuC4yCIKvzz+XMWyyW9D6hM5KFFPPn414o+HnEJ5rzGuT+tYZXbsXfB5DAMIdS+KZNLwTYHQeG+GqulsQa/s9KgyDRoHOQsdGdeZnp/rb0aocS7SDQZIWqy0ANhONQ0Pzmp6LnCk40Q1RUUwumDRcWkJW93acJ4NI7TjBDgqPISqydTIUft7Q== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:25 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:25 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 01/17] iommufd: Move isolated msi enforcement to iommufd_device_bind() Date: Tue, 11 Apr 2023 13:43:02 -0300 Message-Id: <1-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR08CA0043.namprd08.prod.outlook.com (2603:10b6:a03:117::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: fe4783e8-8799-4845-7adf-08db3aabde5e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ww7sNjYsWJAXgwPq3hKAGv26ucLSsAB9DEojf2yAT1yO5WirMlMQ6Rst0vd4QAIpnGP5snO/Nx/qqsIp7vU14+IJJOAwo52+9b4frWg0OT+kXS9b1eDC1YGt0osau6uSMZS60UeIGweOfu3lRrH+3q6pNrtFTA2ZXiWslwJxADVyQvejDLyaeG7IPJkkD1YrtS815SF0G+Vjo5wYCWi8FvfOE1pX7eNQcJyHsuoPwaQSUmVBTNVK7jctb3nSbDcEIwLBcfNDVL+AkffCoCs3bd6eQk/1EoYpJugEGSun/3l5Yk19x2d0oNqaMNvUEuay7L+X1Gy7y+RIZLBhHZkDq9u82Hklv6qMGIQNJp09UZE5YM79DGK1ZTQverGcjwR0t0Yvl5/t4+kqRtJgb/3a1sjNPVlWcxpgAnbnP6evsZTySV7e2IKX55VekPJVNUTd8drOGrfHRCPdNzbVUFEDbt2Rg6Udq9v04rC0VXzaEpSmqAqXQ6EgjMrsyG1lHpCVO48oBXe/9Wivl1Cyu3b8KjUn1KZL/9QJiC6foLIwP5sqmuqFI7Hk7Coh7HnbcR/fziUAkyyVu3V/0OSAJ6+0Yvq2BiglmrOThQ2+CHXpZl4= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BRjjHt/QL0f6393QS7KLrtbWj5eI+6JDWwCsy9in/VTZA2ar+NrB/GUrPi81xMZDbAo0DRDi/BapEh58AhshkUFnbpk/IuxzeyS670hzHTVmWPWfXDlaFGTfu8voeqHDlEiytwgdRz0pGYJj6sVJVA4tKSPsBfekdF4ExadT/Nh927gF/0CMeY0uAWbo9lK/xk8YBm1l6qSQI7A7fYBbXDxpVVg+OG5aucxtW8xvTv5I4l7oVZ6SnLHchTGp+zBbzuJknarS9Y05qmzi7/WPZ75+g+IdR8szVL0/vStC1XIY4gi4L0g03LnvAf8ZoWQlZLEOoLha+9NdjFFhI6GKDQFbZrWmYSU3zy13Xm1ieUp7yrJA6yjJT6rCpjKiv5DDLo/26TLiCBuEuQ1htCbKtcN522aBguBwdLRU3S1lwh8M+7+SPHGignXQXRVuYPHbPJ9XrummcHKrf+8p58J78bziyLxoNRu9QdWYKV2IH7+8KKpLH7M/LHV1c1/Cge+++6zYsvGg7Ygo/pdXs0xD143BobpYDhz8jwOSoygyFiybheaNiS3WDFZpE8CErKnZEJxFCeNmvjfXmGIns63l7YfupG3ETvz8hJsWHATkb6kxK8C9qC0PdxTESD/I2PhdepH22eIs4UqSmj1hM1le522zujYG3BXPomdpCmn3Ea5FzVAhR0BSrZj3tLhWWMzBXEDbJg6twQsJxoceFnbJOr9QDb49cOA9+YinL28qC9izaL4W3LKvzPL0N72zcphNAKdmFyxbxTtBUW6/JU41aDA154dtF/DKmR20mEvjpJBlABmArAkvtkJPuzV1yLCJ90fE7D0RzSOSGpARYYsZPHhcckOujfC7afV794aTLMdj1AbG4S2FSMtPFkR6maeMlc6e9eFGPSyOP5GbuV5oqTj896ZweD1BwIhXkrZLJ8RChr5a/Mn2AEEsqld3D0lZNPbNRvNXpqpJFCY6YPDyafHxjSN6B5h2OE2GG8Kbll0HgSXTP4q0QT3askp+Fwks8wXiOC6yFnhmN9Dlrz6DR0I15l8KPQtcgK4S6r1ByXUA7eCVDKzttA2oz3HQ+Q+gnz//6LH9Uah0/ukrVqpCcTMQ5WeP0OmiAUkTd1mPYWdPXRXTeA+HgLM4x8MzZnmMZsTu0f15JnvT1a9anaZexnbNdc4N9w4MhU65EgH3voxRL0uhEylaIVNfkInChMmc2hlwtPMA/FKrY1M5KAXvyYPWxnwdykeDy9vA4aHvAeOFdH4HpQsndWXSvKxePc9OHM4tkseLaamZWIiAhepPhDCY1kSorUmnC57HuW9THZrfKzvqHgzW4qBKBsw69IgCUBNfOl1Uqd7H8GKPCAoTcldfjSkCbbZSj+oEHI7CsIkVDCvXIBJy1s8LTYq95GNt93GN7LFedNZfZGz90Xnha24PmICnvxh+QagH+b46xCsyZzECdE0g6S8E5bpsKO7CPZ6OlauB+KyWlM+avR76hpLgaBzjqAmRQyQg433ww4aThNBBhj5GZC+3vfHhoDe+cnf2vDpRBnOdf8vSjS3gAYKXQ/26N5T/IBFfKb6sXOE+tT0Ac755B3nyw06cbqEo X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fe4783e8-8799-4845-7adf-08db3aabde5e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:24.9766 (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: oeOTJ9QCB+9pxdnhf35+DrFD19YAGFvaKICsRiC73ETuEdflE7Q3p1xphTMpUdQK X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 Tue Apr 11 16:43: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: 13207887 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 2A0BEC76196 for ; Tue, 11 Apr 2023 16:43:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229848AbjDKQnu (ORCPT ); Tue, 11 Apr 2023 12:43:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbjDKQns (ORCPT ); Tue, 11 Apr 2023 12:43:48 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E565526C; Tue, 11 Apr 2023 09:43:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ONRGkcB3D2JDc+eLIhMITRSWtgWzA7G0t19zQ84pOWDBmVb01Ft99tCsfWR90In/jWBbL1fjlyyO3/DWPuZnOefUV9WKK80qZeN4eZoIarZWgR5f1imyyjGqjXcYzc3LAKeUXnyFGvpRjY1j+RK45rONHvS8Ox/R18z82DP/9r1XglhP28dhsTihKixKzElRLmoWQIOJd2n6Y4oYfSS1WWYp1gzFBdkJcRshf82e3S2q9e5nj6Ph0DYVw6OcKPm1NewF2bkQUjiJZn0U4SFFtwAlluJcS66EVPm5yQ4/toVwLzEy91ipjkmAIfbiT26AkaWUc1FpUZ1FCG8KZnETfQ== 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=RqFaqhpKG/tDRJy/u/xsyBBa8GzLFRq1XcuZjSNfSd95E05k1TW7vnQQzaR3DkAOtZt9RITFNcXausWvICcxNjrrlJt/rt3kE93zPjCwyTm/5tIV+tgXt1KLvCF4BkQRW4A7nc5DwGod0UkaU7cMGlW5oKQfum5l+a/HzIXHf2/pcizT0fsU4PiDFMYwpAnB78l99GlMwq68bXPtVGNdbhhk5iW6UvdT8ljkOQYf5JrrKVd/hUZiUGF2lf1SqeOIUxaW7MvHz1f9jr7nMHGB3LRip3ehaaRr/6aJwZb+fxPlFo85dQeA6jgOLwo69F1dmXuI0cb47N5LvlMYlBacHA== 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=WiFhodRLXZ9skqEHkF+6tEcMykjk182FfgK3WfBJ/QQ6xakwqPhBtRqiGDEpXQS1YaNhMqdHuUwAftCTG/7Ss95w+RsD0AlWUTqwMi3jcp0zZNThtlefpFf5/zXESd88zXOtDLcXS8ACKOGQAj/buzG2HnX7amCrJb/oa+gsWN102McZpohqKTKUkYTfwGv+ungDn8klx2VxP3mSCPA8FQSW/krq7qJ4fIPP1mmkmkiOgH30RDodDhVkt9R8xel2yN/gmImBFyC/7ZBeT7lDFGVGpEiDID6XVuDoVJceXeV1NwRcr7p5Tg6/Il8j68IsCwsvMAEhU8EN5tiW6hNljA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 02/17] iommufd: Add iommufd_group Date: Tue, 11 Apr 2023 13:43:03 -0300 Message-Id: <2-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR01CA0040.prod.exchangelabs.com (2603:10b6:a03:94::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 661b45a8-44d2-4efe-cdf3-08db3aabdc9b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: eehLm5lnsBzOGWYVaezJgpenm+sWNFHJwwZ6K2JDmNKpfB495zrOq5RY4ylFTJAvfwUtKvfbWxpFWlT//q/tbUWN3NsnlZyN70ZYLF5jewQ9M6H8hJ7sNf/QTswhOQhzEYAOpcTN1T59MGMaaqNF8CRSzPk2psdJZXEc6kar2rqFAYyfbbDxXQ1Y16MgPI+dl2pMGhSLwA+LOBZvXyGtVbvk6FHv2dpVuKeWEfTmhrtuQVLXi7GyxIkBAbA15jy3OZgBrwY0v/UT4pbfz7srpHRNhz7V7j5O6X0Wi5Zyjh1AepUbsG4rgtCZiFpPOaHSpWm1IUY6XZ/nwhbE7PQa3Sb2SbeUnxFlvX2/0/TPqMCWQL+73Q1bznlMh74PGwSo/5M1MlpGul50wCMePacZv9Mz8PBzmjfTOMEZdplzlp6b67FxXNr/SV4Ef7726NDYCMQL4BEhkADfNL7BN6SVeBpg5voXuaCJAFsmTOt8bwoFnOyctzf+bscVM1LN+32as8XiRpTxdZ/uvHWnQK9Vsxope8ZlxxFkuxUk2P8NLVQ2fI5Zc8CcEqTbrjRbF+0PgxH+ibtjPRtDfu5r3EntUIT5FCeAjw4/O8bmOZG/+Wo= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vatV5KZQsx2BGP2gwDs6D164tQJvmE6H/B3tWJr/RUvf6TxwMydPhNibvU4QFmvS61CF7SMAPZ9bPk+5sYmurV/NLXapwUv+0m5qgEAYiiNYGDkPs+MqX2IlrVxldiyQnyRIxWpMLUXK/mD1xuMcQY0TRCahVRjjdgr4woVRFvl4rRjYq4EyPZ3h80oH7yDZy4pRR2qDPEZY/4Zn2exBPEMfDc7gzPsIRRYHrvGhKnoFktslhOesDfs9iAv8EVR23U4XJrJI1piwDS2yX5m3oJERzUZ6qLf4koanMBfxuHMhS48ZQof2Ifr1l+DwYBdCZ1bF2l80PEAR5uxFWjUjBxCIH3VW2Qk4ZJSDvetIHgjqIeqFim28l+0LHDGO55GOPvgvb7F8aTJaJTh3+VRgceL5LYiKWqM0s4L9WUCcN+tdjJqfmyDn2JLy1TtnQLwkkgffYFlELpbN3n28HCWlsodOGNKcLXHABu0NdFDDAIfRQ19IYisQuCE0MH2S/QWmqjM/ooA6AXAZIw5xnXNZ+fuUU9UzvPNDqwMnTKHOeTk9g/8ei4chzmHa2QSoloVINrUAsXQgpio4v2vUiryAd5spzI/AgBipjq9ezwHTohSQdSMQRm8V2hKP9WUx2cKqAi50CzwTa6lDrFUFSISxa5CiWTfB7yhoFXQCt/RApvlFDcoRlogIZDxd4YOxWad8buV2J6Yac60M7CXvUn6ubDZ9WSmhp5mSDhlNNoqd70MnkCy+lNVKGWtLCbE7nARvw0JkhCJP9039MoMAgnzmqG2bkD5yBvobrRnrMTeBl9VXJzAgQ5CcLhsdQ8pxMvOxH7Pl1vfstNtw12AbXzedjP2k4Rd97F8H840iyH+8ZlsMoSq2thn1JVD80qSy/8G2OEIQl/mhWSlJlCOMx5eMQHF0PCyVh4wN3KFo+g9s1uf6pZ2e+DTXhH5mNS81HqBZ0Nh4BAlK9fuA0hUM+IgoKAGrxN/6dbfde8N4T5Ul2RWTsC9K4dcXOiTFQoQoaYWlO/oX77Q8YIr12N7eqmopQUY8TB35Zg7+yEOhOUblL0HVy9clEJRFrJP5ECxoUVf7nP6VYanifZQpzpURP/URS7+2jiQxGL2w2AVvQ7bx+UKi5WpXGoiIq2yYTARaC7hH282s9klGIKxrSQtKaMGeRsjZsZGiz9UvNfsA4/u5/OYFhViE5h00wxPqi5cl6XhNb4SV6XAb1Rbm5MvvH8TBAxYAn4L3f5J8+jxD8pZmlBJep8tMkBO7e2gPv1IMgb022GwfDpeu9am8m36TXjrfVIhvj05F/mSRy8CaMoI+kD+jeFD2DSIpIYkKOydR1ohgt8j7z7NpIWW+tumra2TGL1FR03kVoxvhVQP08Z8lJXabk6Q5kEnz/1oWU/pfjTScc344Re7MydogmQdsKAiT9XNvAGFqqAeMxPp04dC5LVBwkqnFJyAjra2hWJ4VnBI7pIeLEvzQ6no6i0JieF9rXFMaB2rW5q5KsOBqqpoZ756P64RRY/HU1/tFtjbUteZlf0lX1UawclBT48D6KhFmtFnOQlYTOXNj+vKmmgA3u+di+LwpQ4PaZk0prlO+S1Xp X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 661b45a8-44d2-4efe-cdf3-08db3aabdc9b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:22.1709 (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: FQATDTp6CjwLGTheFxtrQQEJB4nq9G82p41f1q2MooPmNwXk7+jF2ONFbzljX1uR X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 Tue Apr 11 16:43: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: 13207889 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 52620C77B6F for ; Tue, 11 Apr 2023 16:43:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230023AbjDKQn5 (ORCPT ); Tue, 11 Apr 2023 12:43:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjDKQny (ORCPT ); Tue, 11 Apr 2023 12:43:54 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E61649F8; Tue, 11 Apr 2023 09:43:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PqOIFYQNJK+3tEtwpfx63GGMAi2iJ1fj02wWdxbVvPt+j6V9GgA2L+eapcuW/MZybpknFyouhWHyjMYpMWYWMWilFZpJYKi94q+IlnGKUobKPaewFgj+9seiHJdW2zGmBaWbLPRwtZFe6EAO/c0LCrVZZHYcbYSszO3q+KFUBSWdGXHzM8qZaR4FOk0zH2jmBlRQ/dwGbEbcKBEVFXMyfOmb/FBr0+rwhOmIV+MtEh+Pm+zpomXzjgBnHIaa6FCJuqQqW6hBXwuZdbSh6ilUJBrGf8dJL1iAOuc3rhAACvCejfO2FwohJ171yLFsqHpp84pFSojCtHkSqianq2+emw== 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=VMFQ5E4y7XFpx0TNpN5drsychS1gbvFhFrR/KXAmAZM=; b=SG8Nh62OzhwmfFuI6v36uxDHUUCi0HibFgLyQ/aL6YOhJMqVRjumjbLIk0Cvdvl3SF8bKJKlmtd2jc4gDgcBxq+cl0xlj6ami7m74V4u6vpgbr0R4qDxbaiVPG4xrZVLjNJ5YlR7sypabUwGpfIev3SRjh946BTiq0utEMYVE4DbXdRHAXJO5ni/bbgiSi68YY4PBuiIJCSMsSySDMKWdIVQVteKQfpIbvuFZxpnVz+zufHpcu1i9EysZzKB18njhS2WJikqOY5GNBs/vymZ+B0buCgu/xSf+eWrdM0zNURMmJpBp98uP50nWvVEALtwjhl2nW4Bx6An4GLozyAq4A== 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=VMFQ5E4y7XFpx0TNpN5drsychS1gbvFhFrR/KXAmAZM=; b=Y7Hperd20uMsTPyhAhYieXHCT6DO33BNJN8SZuZbvYLlpw5cfBZiRkafkAPeJiR/KrQQ/KLilxD7GubejxeM1KA6k7hOTSvKhRTRPznHqTsn2w00Zm80jaHh3v97q5l+R87XEGWq8RBZVwx5FusxyzrMiEycyED1gnsw9S0qzHj3TMyWelU5wwsiK6p5Cz4gZ+iQM/114RGSOu7+Tu3NxA7wpnkPh4Ecw6lXGGvX/1tvsba2pT39ysUuQiBMR3fZh1y8J/ggyoIEWtAWkZzjDUR6mUxNw4JXrX6u5q5oQPZhuZgFSxjYcYowIr2DFVB8LYCNFi28cO5a5UWtw0ALcw== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 03/17] iommufd: Replace the hwpt->devices list with iommufd_group Date: Tue, 11 Apr 2023 13:43:04 -0300 Message-Id: <3-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR03CA0097.namprd03.prod.outlook.com (2603:10b6:a03:333::12) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 786e2856-3244-4bc4-a448-08db3aabdcb3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: oV/jLNSwcxMkqi7j1Gr+UbRMbeJtRK6CGs3h10rnSC+0Om9kXtM6rdza4h5lBS8Mz/2Pyvpm8BwLcInU4lYgHctPmJkSecsw0BqjOphi7Aa3DEhqi8walXdgidUSs/RINM+P6RcKhwsp3GMXqWJM8SBNaBLy2T8dfhpApwa0miaG7d60o05OW7GcLaIO68FVfQ0cvnEyq/1tS3vOODwgyXtTQdP9m3R5sbOO3BUlPbQ+ClMiU7ZlaabsU3cKAEquzoImBAEFstjNbprVfhZiRiLLOyTb/E0FXTsnytkFslwaePRTRBjHjmZaBkGVPkSQLwitv/TmsIvcrobPmhAXCDLvC3v9KBGzlE18jG7j+TJpOm6OI4Z3KQUbxrZylOoZi8lbZ8Gg6t9EnQpG4Sm8qdGn1ocSv+3be6gKopAXxYcyPU8Vkg9vm4dOrkKjms1BQQIdAaHw2a+k7+LwX/Zpi7z2xuac+bBvZRw2QvGr1Eqe/KhV3VODP27wgvs54dEmZUmSUnNsWDtOUWdvkeI/Q15FzGPj1E0uehH0uNAkGccZbMLE2KOHWpBvDhvS8RjiQYIG8W4glFd7CwXVqyWet7Oc142ysYJVRyyQZDyZ7Y8= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(30864003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4+m5W1FcxsQXIXoa1z4pmuSm80js9PJXHNPNJ4uvRE8K5CL7bC+JfZns+LV0UWPUlmRMQE8QUpyKvMkvyCC4CYLP38QKEIezCgH2RWOC2TzY9gbUN4IHL+GWQzU1AE5PdIh10fVMD/UhzSusVLNuTNWCEc9Y3pamxw45NpatIQZNY+Jo63bCyDK7tNUobaa/hlbEq8h66oTwGBHOB+0PvcgvlYkJl5b90NKRGCgZxUdlYMJU48/3K1xTfY0L6p6oImvYkFroTK7MqMCk8nBiLlviCAzwg6IUqTOaig/4yhEvQNi01QkBOQcIGKEV3anWWlLFFDQf8Cc2GNtBWHF3oiFjrWh2l2UkuSMfYQOMwMn+WLeZCHNE4zKIHZe1FUjbnjmRG2My3hS1xNfMuMpmF/KP8BCfRV9a2yIjg7S5hOzH72GeaObvIaj+ED72h917TvxBjDxH+2ALLuSKE+tG8vkrEjnduDsSyKyHMJciPPOSV95HBPMppzPEdEAhe5zgXneFmi7iBJCXaE8HJjZ+YO8NqtJ04XArjDSFEZ8Sru6teh/eiHk3daO2hdWZqySsorcCAV3wsOHIZhssZuZiHkY3aX2M0J3OVVeJo9JvxhNfsXHpPnK8O80s1Xf8VD+2seIDDEJnn9GF1RpLjX4U3/ECWmdX9Wj1subv2y3MfeJRxjklUceel3gypQc3IFCN2+mgFRxMSH9hNAq74f/t/ufLEfUwNVbj/bE8MFVniaDN+9DKUiNebtxvgN5b0a5hBZ/KlFqc5jCYF2aPlR7JPDfDNqo1EREveGkwn5d9R6O2znwDEGapavN0VR5k44XW7X8s++vmb6YYwCTYU4aAa3IhbaOck5O6sVchSB1fQseREuQiDt1wSu1OWs4cvm9iK4LdCh52TMDc/u9bolsKKh3KQDqgzPTElNiINs2aVhh6Hcfk16yVUXTF6+pnDOqNYflwzd0ScIaxBtRA4BJVVNF7iCGgSKKeDUgi2cz60uaNM3lNMCKjyaugcIgexvYfY2saY2hlAwNbN3hJHHTI36lpWeWL8Am5nDSwVmwiNqg97ajVXPT81U/vYQVpQunj78joN1H0zSvIdhpzd5CBnAsx7wwT5mKUzyVGYub7W8smTWlS9rCEMvZwakrT5U5YW7UW+cQR16Q0vGBXoG9lJzGzxNFKSQPXUsE1cQBtPqFEW+84fS5uq7bfbMmm3Wqo/NJXHvoCsXIDoCw1oSFmOyF/ao/3aC5gJXQRl818yB31V6GVZuwxSjzKb+zhri90I90oqqhP8acnVIfeMPoNmKg1vbr+HTl+hf7iJ6Ad0PmWp/h55cm0emvFN5E9yhS4ld8lhNga73mEGtF3dreXw4Qxe/ExPFk+nh6bD63P8fxzJFpXwQfeWK9EGSJbmfxbxol60dCmx6/+FAFjwKn3SMJvbgltk077HOBYYDqAeQ1/Y/jFLUbnXfsuTkZpX6GKmqoHJck4OmGOjt2KlGTfK0aoA4XWbGW+CddzKShJI4l+V8oBr6GKV6xCBTyGb2gvRKLgaeuTCh/a5EdRmjtlvKKUv3LIt8DjW2uT343a38w+ISISDzl/fxGQqVu0YrYb X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 786e2856-3244-4bc4-a448-08db3aabdcb3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:22.2641 (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: jLporIcboHvjvBDN+6UD16wCMy9BcMkkSpyFt1KQs46hm6ozW8G48c62rJr7myHJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 | 79 ++++++++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 23 +++---- drivers/iommu/iommufd/iommufd_private.h | 13 ++-- 3 files changed, 52 insertions(+), 63 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 3fd623208c691f..366df246d4d1f6 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,28 +282,15 @@ 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); + lockdep_assert_held(&idev->igroup->lock); - if (WARN_ON(idev->hwpt)) + if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) return -EINVAL; /* @@ -313,7 +305,7 @@ 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)); + WARN_ON(list_empty(&idev->igroup->device_list)); return -EINVAL; } } @@ -329,26 +321,41 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, 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); return 0; err_unresv: iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); return rc; } -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) { - if (!iommufd_hw_pagetable_has_group(hwpt, idev->igroup)) + struct iommufd_hw_pagetable *hwpt = idev->igroup->hwpt; + + lockdep_assert_held(&idev->igroup->lock); + + 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; } static int iommufd_device_do_attach(struct iommufd_device *idev, @@ -356,16 +363,9 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, { int rc; - mutex_lock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->lock); rc = iommufd_hw_pagetable_attach(hwpt, idev); - if (rc) - goto out_unlock; - - idev->hwpt = hwpt; - refcount_inc(&hwpt->obj.users); - list_add(&idev->devices_item, &hwpt->devices); -out_unlock: - mutex_unlock(&hwpt->devices_lock); + mutex_unlock(&idev->igroup->lock); return rc; } @@ -375,7 +375,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; @@ -402,6 +402,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 +412,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); @@ -455,7 +457,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 +468,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 +485,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..566eba0cd9b917 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,13 +60,16 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - mutex_lock(&hwpt->devices_lock); + mutex_lock(&idev->igroup->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); @@ -84,21 +82,14 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, 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); + mutex_unlock(&idev->igroup->lock); return hwpt; out_detach: if (immediate_attach) - iommufd_hw_pagetable_detach(hwpt, idev); + iommufd_hw_pagetable_detach(idev); out_unlock: - mutex_unlock(&hwpt->devices_lock); + mutex_unlock(&idev->igroup->lock); 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 Tue Apr 11 16:43: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: 13207902 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 C6CF6C76196 for ; Tue, 11 Apr 2023 16:44:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230094AbjDKQo4 (ORCPT ); Tue, 11 Apr 2023 12:44:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbjDKQoj (ORCPT ); Tue, 11 Apr 2023 12:44:39 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A8C359E7; Tue, 11 Apr 2023 09:44:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fV/Pt3JYrLOsbbp4Qdd2BtGVTWSVTEpja0N7LyXz55qhRpeREOVgj0TmCyVJ3U0iRMM+/XqrSZjbR12r9qjoEzzkDEKFwEYLQMnOwNxHYks9sDefG0Ba+VIHVVYdGyse3Bs4CyM3xvtZJY41CuhXW96Wvc/NigzNqOnyCzD/9uG2hoRjh9qQscduEvOv4vYZIIfKPao3WJx0VDWCWxqGKYPNUSSelL6W/L6JkkKo/YPkfU7c8EOT2NcBW/B9nHDdbVqLLWTrYPWjWZ7hcKAqmsh28e8vJFX52HPrmoWbl+/Dl59UlCPJ1iuseMmTNEgisvis1Q9KxmzxLxqLKotVjw== 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=YnJ9Z068D+cbe/uRfZDN9uMjsh1qVINxf7w5a+mF0SR/xFikgJy7Cxxqph3Eu1G43D/2pCnrWSSGrE4L3IfV0AcT0a+PVEAXnr3yw/xL6H9fEQLTGXC/mUi50VYOLvBP3i/QwBjbRNDV9veJrNNXVJu/ECUu0NBgFixJw0556wJ+3sWeypyY5gs8tY7HYjtp1pLqLh8rKlwESvZeEdaRSA8+W1WUAa4izFGxzoPzLGlefn97raaDXCm9hKMM6bmoYsHNSF8zxjORP0j+WEmOS+0/Qq/dTmvW8IvCBC5XPK0Od4AT9CPBJXhiHYz+WBORZ5LkYpLaCZArZkJEJnhScA== 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=iZd2D2OVGvRcoK/F9ySiLp8YKY5igVYgstJsj7r16iRIWlPdvCcSQNtXmA6x5TsIw0XWUWV04W3/k3nRDVCJZ4UsROgJSKfGqxTrkTSjmGVmn52Dy+/QTucTp3QHeweb+U6ZQMcCcskVE0V/8voH6vfKw8qc9ESWnfM6H0zjbnf99a/zSzPDC7xhiZd/hazadFS6QxGR2EKUeWDPFAnDCCpn52Ncc3GNsMlHXkauKbYNoAtX/OZ3+5Wtyve0VH+1mW0WN/nA9uIyq3sgtmgTcb4iZhKG4G58rPF47W4a4t/dmQSA1Elz3a7+lGv4mvWJ9uA8qE2b3SxPeq9rvAWWgA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:26 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:26 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 04/17] iommu: Export iommu_get_resv_regions() Date: Tue, 11 Apr 2023 13:43:05 -0300 Message-Id: <4-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0169.namprd13.prod.outlook.com (2603:10b6:a03:2c7::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 4fc4b219-5aab-4fd4-3c4a-08db3aabde82 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hk1N4R71Tz4L8gCrXZloZhz9RDXouFDrUMe/Dsau5thfMkFS/rdEbYhta7ks2y0esgFkNczDVaE4Sz766zdueyF7ym2srXh0Pshf3p391+3e8WaF0HWJRIpze9Ic/NnOk/OsZIGDK37hRXM1FaDhgxxUIYpywxCfLltf0jK+mjcnFd3lCjgfe4sHbOX+LiLNGR7m+MUwsPys9QHljO/e3z5RW3TqKlswvRGqkaLR02gY++qO9OYQAiiBR7Vdgzj7NVaxspYWPsEm3BGw+5bUp7p8CI5Zr7BXSV2zm43tNCFt7bU3tHa8g6SFv+fFaZCoa+tw17bfjkAgu6o/M+81JEvuFQqDpwGSfPqex84nMfszZfsWeuavD9lzXNhDiX7kVcJwH/A9P2jFyUizADkA++kg6lOr+XVBJ5RfX5LoBc0jjzVX3LQiOgLBBS10byxHuDnf5fyD4/w4sW0xo6K/iRsvSKrRquJFxusdcICZNA5ODdKFV6PAXkhq2pq7Wfhu5DLEJA5un3xOgAAvv460kh7WGk/6B23FZ5mTrbzioOpBXW3tHtbpeiQtc8TyjCpZg8HNPtnTFwObCXO00UM6P/lIH3HCtjcw3oGEtPSU5ng= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: C9OCN/vM3zNptu0Hn+bDWa+kl3ynZo9K51CUUraKrzia6CNX1Ta7FtRdI5ZJN6G6twrq5OsCAfducMnydUJKUfpTdgE3AdzToer+aadJBUmkYymCvhEKuq+sO1vCmSnjdjDOjoee7qZ++XQvfl2XZ0qyM8Gty3i/WQX0dkWyB7NMtoksiGwIuWf1qU7rT3sr60zY6zl5pC4w5mRd0rpaBGtFAFUmMSuTNACZhc4cTAEBzL7GFATbiTKIwwvGl+0Jf3FsO6GmND1CrWpjMYP+7ynZSPusXXUwLWESLkbox1Omn7tmmFr60tSbNIzvSs3Y3YsBcIWoibvei3FBlmwCg6PUqRJrp9gC1Qp+8F0amSlnU4NndzkL+pz473fEEWwDyzh3EkZhkRDcLwggkCmiBg0FPn3aQDaCNkbro3/L9jtoi1FTYASTGFUEvbMp+55i99nJ45ggNMC/JY4GaDFQxIHPChRBVNqVo2NuL0iMfUVHr7YYLradBo0HhfWsBxVnNXaUwmrTtGaIkNeBYUlWScP3NGh0qUr9X6l4+uAudMnBUYHhq3HbPq+bJPitDX2LiCxMbseT7RUn85t+MaDymiA/lJm8ONFO2AhTp6W8tC2wU52wVu+RZ+HH6hN1h4iPt0w4s79D6SYKVOxtPCwXhrsXSkY01hSC91kDygFMcYmw6MOo/dBCflttrNXLCgbbItc0x1cMcKQsQX8cvG6hbvB/aLe6V/tE1naNslLVySnukXaUFQ15p/RW8S6yh1RQtL3YC7uuHxanynJ6wlNkdcBA7Gj0UnYJEiVeItxJYNTXrcizNnDrDjV7omw7WICSKF0gk8FVPMxdpH+hBpUgfrrqAQmXCfOEXqUCu/d5WpP4CuA+KdsWucvhlFJyytRU+l5YFjszv8FeH3c3x3G2BdgB8D2ECFle1gAg0bNl+m8XgayAQhS+e448XiqpYI8s9nrQQEoLx2Jbwl91au5rOf20NAd+q3Q94VA2eXyYDNetPzviYpIKVxyO0i+Es+qK9/q6TTreAMEiuGkjrYvHTofVI7smkB/sxwUywiqP/PIzYJhLYZZjB2veK8g5FEQUYuBnMuCJdttuB4GNH55OkXfiDe+LicVvhQ7MMzH3wCGnbjw0fQcQnwTV6ATWn0jw5peo1Tq6fmL80z6g++KK/Yyc3SNahmCi5ZNI8zTmeHRsOG1kFdLx26UlRW3MCX09Z93jDPQnRrdOBpff3a9VZ2+Fgi3SZ0bnH4NiXLZhmuUvn9T28b5/wCwx78TIlBq43GyR///jmnrO+x4XazIt5V6/fS3aALj7S2rgrsmrCYfm5jJ96AukjN079ZEouXXId7cb3yHKO5PJLgotb0lTRe67QZUijoLRAIwRD9bGpTDIN/y9+gy8aaqiuTWn/JEy/17lZFmDWpxLDqpzev1dE5jLbpZ/SfpBkG3l8cG56XzNxHDAilc1HyfPx+zjGdGpee34kZr2mEorhzsfALd7j1fGVh1yeDDt5109wDI+mOIExMaoe7+RFgeAUVxSGvMwaVGA+KMa+AOi03gHNMwvSryoTBYUoT3fc5vo7O+UJdKhXENIEkyQYLA8t/QG2jbI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4fc4b219-5aab-4fd4-3c4a-08db3aabde82 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:25.1446 (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: MZTanoVXJx82isBOvruIyTDLEwpGTjPhX7HaTXHu5ep78d1zD6ZUZcv+RkvAH5NQ X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 Tue Apr 11 16:43: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: 13207882 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 12DEEC77B73 for ; Tue, 11 Apr 2023 16:43:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229961AbjDKQnh (ORCPT ); Tue, 11 Apr 2023 12:43:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229957AbjDKQnd (ORCPT ); Tue, 11 Apr 2023 12:43:33 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2069.outbound.protection.outlook.com [40.107.93.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE6BC527A; Tue, 11 Apr 2023 09:43:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kvNvJ5Sgs4u5geT0fX19WyPHiDqYHqNCMnOuH8dMM3O9yAnZtaM3OVdStuyhal1W8zmKhmiBIq8O9VqT77IqA2PWjSClrM3PDpSHRn56R/pNh+RMyEpUwBhJRgEkrBkfm23sMZMZX7l1ykYd0tF/Uwy+6eC6Juy74lo8GZ261N8PRay32uHFtXXTuGvcW+z6fWut4K2+n/TawqcY8Ch/1zVf+DWezWr7JtwT47mZEw5wNaqIjE/i2zuczl6U/WYGo9J+yYy8/iJ9WGOiP+8FN66Gf+LtpsgsgwOY6ZYZhQE7akyuWvHDaHhUHGh+qDZ95uHN8BsYXkgTNK1ioiOyGA== 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=RKMU0vR/J+JJk06Oju6VPCk8ixOBR44n/ZgfU9EoO0U=; b=SsQEhCY6mPa961tBbzfzFPCDVehSe/nLgoRrj78pXtxK2Z+O1EJEDIdBoLXGwy1ZXRhBjhpU2u+lVw+LMHy9/VNQQv2Vdlsz2BMQk3KdTHqW5X16myWXAv/sUB6wKcmEPnlQMedpsl7biId6LHS1BuOb7ZeRCnHvqdUpmwOn36H8jAbq5NP8oEPN9SrlnCv5bbut55XJ69gNEihNhoPVBAi5n5Fov3wzGwtpygbPHB/vWR8Y0UB16eRd+6UOfyy6rOoMtSVNTcvkqsxJfQO5+dpZ60ANw4iP3Gy/Dff/aeIVIvmvRWALuc/BdoSr++7WJA4mmPX94sdPhppIG/P69g== 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=RKMU0vR/J+JJk06Oju6VPCk8ixOBR44n/ZgfU9EoO0U=; b=NKsL7IUghyh4hQ1BL9A/gwA1YF3C9/CsMSuhxamCxnBE7TGw+wRRbrcSNV2yLbFcu1W0iQnuPAU4EU87F7G8cde4iporbW4sV7ouT4SeYIT7gYSIJ16Efyk1OzvmHlcIe5MEpSchRRjuQQ2FwiwJYgSdeyhrNZls8aU45tw0rgx04ib6kOTB2wojMGC2F3u0HU7ehlTf8voeCJ/Tms/2KYSWaq4Xgg1j+aZ30nT713OObuDoI7wVW8mdcgGkbXADrnF2SRnwkvUVfzYt8GXMy/xZBGms9UmZsI2h92qKn+wgSUQ+gOSx7iZeGw28eZ7w11+/vWzMjShIaol/cPZ5lQ== 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 MN0PR12MB6077.namprd12.prod.outlook.com (2603:10b6:208:3cb::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.38; Tue, 11 Apr 2023 16:43:26 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:26 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 05/17] iommufd: Keep track of each device's reserved regions instead of groups Date: Tue, 11 Apr 2023 13:43:06 -0300 Message-Id: <5-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0174.namprd13.prod.outlook.com (2603:10b6:a03:2c7::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6077:EE_ X-MS-Office365-Filtering-Correlation-Id: 3cd7240d-fa09-4bf4-c53e-08db3aabde8b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PX8nHCM6kQfWy7g8y1/ncB1SI6/HlXotH/251E0idHCDkvWuH4mWcCQnONuPykacCMDOOnKzfOda9d5Ftr3CBn4+NWrbavoPbWqyBiX5SxQCLAWPm3yTfFs932quuZfNzYuRfE960YfSx6QTpySsqKL4oe+UAMHAiv2cxBkajK1yCWGkY9xPPY+OZIWmKrno6kpbfgsGLiDrKPVbpgPX4+ihfB7DiB5vypkBKYAWIoUTOmFm8c6FPSekicE2y4e/EsbgOVhK8wYOhh9+CapX90lMmSw89kSCjcCPqa7Jw/wVBF65QcsX7CkoPP0xuQ5T1Xl7QPo/i+z8qiUSHw3e9xtc2Nliq3etf7XluoVZpwWvUquhhWiP8zs8oL+pjZUmylI5zfBXwwB3D6QXP4FZ23QK1tE3EGa7b2cjU9Y0nDjmcNyCJxLr/DHK4l6PmqI+YolU7alYAGv+pd/euXfR82+B007Xrvb/fg22wELoaAYj8+gXLhnR08AH3RAvf2SlflgMaIfKXxg0lLqDTe9UmR0uMF3dzuhANXmTUVJWtsrBtpXInUf+vL3d5fp3insymnKo4UM3yK+rwSuCBOx4iKUTzxHzE1wBbxt/3l6pufY= 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)(136003)(396003)(366004)(39860400002)(376002)(451199021)(478600001)(26005)(316002)(6506007)(186003)(54906003)(2906002)(5660300002)(4326008)(66946007)(8676002)(6666004)(66476007)(6486002)(41300700001)(8936002)(66556008)(83380400001)(86362001)(36756003)(6512007)(2616005)(38100700002)(66899021)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: W7mFRUDdNSJmvCPOpnEKM6Z/OMuIYKYkZriHzPk07uVdNxK+GfjVjolVfX/AMVHrseNq1iRSucAKCYIVmw2diYUakFsZTcXG80iKvi37cOL5Xr2EEeZf3npn+j9ydh93LPW1GomaAO504QMINL7wOtSyiUQCxmU6/QIus7ubV1B8ugtWLk0Xn/1Teuz1vHOl89gV+eDxLD8+FrEx2gfgqYZe5QGdhR19MNXZ+e3U2UpgvqDnET5X4oxVwOLbOWXM/+YERkdH7C393aNThHt1g1OM1c6kdMCZG2mBaG36sXKhdXF/5QViytxbvVCQKKhhRsYJWKqbH+LmJu2tFAnnhDakspLEP+kDksnv5y3tsT/NxR8wqFQwHvkf+9wUV9qLBIS83VKbuhjG5Vrw9DRA5xbJ2lsu5vhG9efm/VtW4bAEj1YoaX3ll9jviVDZ+9OSi2R9XOlnL0BVERmk8ERANmsXc64PwtphRAvMPk0uGLDHUnFH/nOdFLwo9SrrMCyVZuHCZUFBfoh8IipGYgnOialWqIAc3wvfAzhdi+EtNVcdXiBeJvtihepwG1lCGfPMxMqZqsY8OMcwniOzLmST6KrBKLJsN9v0Az5pZVKkAHenW8uYjmz59l0ATbKiicp7obBPAir+7WbtKTA19AFEARswyAKrx25UdTYMOKqb3kIOpHa1c4A0zgoXWqnmz2M8+Pk5TaZhUN3U+UUzIDMP0kQXl6qj9TSBIRac0mrxR9UXj4FDvm6YgnaA2gW5eyer+4wgb7QkBMlP4twaVRVffiLkE3fxOLvI3Jn2zfMfYDBnTBbOX3A/aHm0VHQuheRqYhaWmhdu0Kywd3mYBONKmdaVYsl6wGfI0qQN0BeHiCl6GE1rsIpgLardEUDzEx+TRK9Xd5uLiBDucn9kOj+qXtomhrPyYfSMjA2Hfrv2FZVFHepK+pbhRsgNgF2NgTwMg3pmaYGyk5oHPqg1XM/ZH6QfG9Y/qvZXHmLgLeQiVOC108JgxqufYLHizw7W+BpKen3jse4hrdA/i/KNTasqeNCpczeU0FiZ21culIbhPSIev3mU7RwPYlqmjNOJYo0DKYdpDgHodoEZ5vAw/0bK86aNZTmTlY83o8SLH2LrXSZHbHxRdjyc5rSoX6Adufjfy9g78gNFt0MiI5hmOrUmVFKEi0Z+qcn6gCubPd9q5qCLzLz3K7d+0m0xNIt1RXGH4wVZHdazG+bkLpZGA3yexBcs3BpXi1FoZQBYyFpUm4EA1ZTCjhqC1JAAr383WoPv4aav1kA648N3Q0rawo0jtlC09KZxRI2vEGAQ+tUYWb/VtfsXqmQvaW/dCGAgBTIMxGgWvyWKUUCxYc6/P2BT02k6gbEDV20I391aupVBDCoPGEnfcJfhCz2MUeLX/2v/Y05wCuhsxrSGO7k1x9HBUp6wcrdwJiddxRJIRDUQk8QjOjlkN/Sx2Bi43laJVQ2qrNZbQk5ZUyI5BW8Dn0LmffH5LQX2wBFttBFIkynVK5KoVR3dk4ZZEaGIckTdyIMGXG2+IzhBmzsmRBLuP8ZkL4xFuqffjEr5BanoFbnOSJmJmpo1O+kHcok1XQ3Gbl4g X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3cd7240d-fa09-4bf4-c53e-08db3aabde8b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:25.2054 (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: QngA7CqXX657NEqVX0pp2A38csfdoiRJKN1DgZxhBwC88zheQZE6kGl2RHvRrCzv X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6077 Precedence: bulk List-ID: X-Mailing-List: kvm@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 366df246d4d1f6..d2bcea7d9da815 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -310,9 +310,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) return rc; 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 Tue Apr 11 16:43: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: 13207879 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 9BDBEC76196 for ; Tue, 11 Apr 2023 16:43:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229815AbjDKQn3 (ORCPT ); Tue, 11 Apr 2023 12:43:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46488 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229651AbjDKQn0 (ORCPT ); Tue, 11 Apr 2023 12:43:26 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02B83526A; Tue, 11 Apr 2023 09:43:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=J9Lj8uCruHkD9CgSkeCQXtCw4bCgszVillP95YFkwV0VX60KuYlqJ57g1vfrAy4giKfvtQGlx3fBNAznGrY4KDgkVIz/m/qujf924untYbvxwpO84LJ36KWps9ZGms7tWq3BUx35Qg2D+cSDMks38PAfB5tpBej6Q9YvabbPoHaQEvyO4mkkkL/rc4eYwl0ll58HB4YpRNxu5O3Rt4amrZvTQEf6G5Fz+F6Yicfmt/MBOS8uUJG1bxs15LbnuiJlucl8W/tt09tLab1Qzkb0+NC+bGDGOO86/gm5L/iE4pNQ318bZpY5MaNRFRfkuSVruxUhjkBpwp2Ju4JkhrHWAg== 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=YKjugjFWk+NnRMEu//zOrspSb5+B3eyWSMVIVoXMZug=; b=CvJy/ErX8UWE55wpKf8YBvhrV3w4aCPm1Q+4W1mKX8EuYQH3spP9hKtI3oyE2S1S5zZtoXB0TQMORaHMsZh2Q7Nx/QTKZmpRtVsePc1Z234etgnf6QwZF+ekdUkCgeMLURg7j6NIS1sR3MPB5pAnxGw2tGSo4qrzXFmutjU/7g75dpRxG1vE6PD7vjtzjh4VTc8Xhr98Ab6pmvFDw3CWIlUmrBbqvVMPE2bdqA5N/M+mtF/ucPuzwyw9CbNSFlNuVCSbnfUSCwVo2o/r+/xNrKQX/1BQzBHgKzXM6CwP5C3+31exuBouWKpQJ9LSlSHtjh9M2E75E/L1Nn6motmcGw== 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=YKjugjFWk+NnRMEu//zOrspSb5+B3eyWSMVIVoXMZug=; b=pgUczT0YoNOQJOqzNT+2BasVsS7evDLPDOGcHqBbTK5LhPojHfytYBIanun2u8usOzRLnRA8o+hww/wkDMKM9rizHbS+JntC/g2sMsDuTsQfEaXl3pWjfzvh5EO81QEymMnWUr7mTlStRAZHBUEwyZsnmwKa05Tw325tx/K1Z4gwxPSaVZ/wa3HDnK4tIdDnC/DdW63mDyA9z6ixmh7VZ0GrJzlAhIz2KrPB+sRPsjhUBa9OF+qBpu2QlyHVJvSyzv29RtIRylKp+5FKqzYtU1CHDjMkgCw7rNcK/SC+liReeBI0Vvvq+3AYeh2YPGDs47jedpHflY79R8F32JXxhA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 06/17] iommufd: Use the iommufd_group to avoid duplicate MSI setup Date: Tue, 11 Apr 2023 13:43:07 -0300 Message-Id: <6-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0008.namprd06.prod.outlook.com (2603:10b6:a03:d4::21) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 40dca913-a6fb-4fa9-71b7-08db3aabdc5b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XgojiSt2K5/y7ourEWtwLQpqPMTM2ztljIAFXNY0qDD6sVhQr1beWA3LOV+CsMBa+6hdjfq7qnOtd5mvH4oK7MJ0PgjsqG22xsljfP+iAGkVlA91X7l097gekZhw83bq5+UwIZhp/DZDAM4X/tLgSecrDCcwLAke6ZTBQ5sU4T8O4qzltfOIY1VzGnGcYHz5BL9CWDl5wX9GBpiJDw5TSt/YnU0ZHJYDVsPxDP6wLfGGBFoRjKzhJqcEIEY5HDc96yZ5/4numzWRm+jnyTQhk0s9WgwGXyutJKk4b8DNjtkYl75lo06Uf/K6GKmI0/235eTVCkVCxg7chOBREqcGIMr8uNN8zBHt276c6iF42YxAo+TvX6SLR1jw2o9EAKJcN0P84n8qGezXguC4uZHrQpfYjRoXlKeZlXFRxwpX5Sm2FWNHdy4ytEl12nGWovZDGuF960w3+jUcUgN7vHQrP8fpshmhNjGHkXyJ9zenKgwLPHmbEB4/TFInpsDYvlRT+qa2w2nA6GVF5MqiAQfi9lr1e6MZAc5u4qO1FV618jpPCRtime3cgHZdYdC6RlnLAELIglzk1lToIspA3CZD2B8LdfQE0PSGlLoaVmApYrc= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7MD/m+yJ13jjrraXZbGoDeARikLWgJ3OFUlQva2B2wD1WeCnGPgfmOTyIsFSx7J/vSuryb/DkeEztpbONtGLgB1Y+Cv2bookCyu4caXafScXSJ2UEB8Np2PqqgeD++8LrgMGMMEm6zocmGi8lhXahHKcc9rbG7NWUiZAREfF4yTtX4qRJ3wW/X4N3HibPtAuDKk4Ne0lGE2Z6AjrTNKUOq6sJJ//ykLXSwNgBeubiZJPt74q0EHS9OaJEQ1OiuwOKW53SceQgmVEexBgwGGZJQAImKpFugHvjToCD6WbSyzgaPpmIGksIrFwqScsZVrIEPqmuro+YWoiXJGT4Mxyilq47EM8UUf727Pp9RMx7Y4CmpRAAPq5cKmBjFf1lcZrwyDVRPClY3qlL1lTc+rU+/6vQWK2B0MifSBX1k6sFCS3SW0U4Vhz+mX/onvcnjelPKQhY78D0XoDRKuiDeq1aX7DpEvt9Z6kjwF3usfCbk5GgDeytxvTOPmUmOWePLF3z+UXVcggp8taO14H79oVKsW/RjxED9UYOko5CXCry6CkNzcbmPHzsOlhx6e/Dw2w/RoFiV9TgceMRBbTuufxR6k2JkO/WrkDmhDvBLKTxdYcqYA5ZQV6ZMpD6xv2UQsIMOebEac+5VKSkaAOUizhwPylsTFev5sSXvlScImh0ZX05jMwsX6VPIyGGv7bC2HRjG3AZIOq3yAk6ghxJ3WVABuCgpg5bFlXoM3tqgzMyxhv6Sf0M6Imw+3hx4is3oe550cDMdwPRfdAfQ9hE7khIbwBGQ49HyqA9q7gGXrs+8qjV9vmLJwFMGk/b813N19zYyM/RKCmkXS9zimTWwIF98jNq53Sjoezl3P0gtr7tmfiRXbRE9ZmrcPvlakUdp50N1emwZNUCCp+dUMexCkrYwabqOQi3f/mT7aemEulfcixARGUcV5egfQTn1B7IwpC7IDMt+IZ9TZRi7OcQmvLRWljWv2NHSgEWzUwpFmV8E723G2W5Rj3Ua9tPRWHgZx67TK5As6YZbzQ9jmi2cf3myMiF1v2dqstUzH0e2pHkF4xfVIVviRWHO68MxkhRGDN6VCjewpuzUbjCF7A+5/RV6leo8rcGebZ+w1h2w/v9Bgg3FnUoPwROAtATVaOzGzSTgVLaRHvLblLXNtouascFXcfhVEG5RakGBT715SbaLoTeN7BMPjsawyrg5RVbmQmtFm6dUBn4S3KVYjpf61Rzwtf1I52Yt3PfKjtRgvXXf+U2l5K0J6tW+PmEMiKYNPsBnpkTidXu/06jhEPdP1HHyXf5Z8sUo0/qsk+b6G6FZ8+QpLJgHf5AVkWEKdTfO6dH7wcgPRGWc+WzIYjPSoK7sFft9uxId20mT2pWigFoBwxXc1XHw+6YLJuuCpuGvTZaxep8z3nz4loiLDy93b1hbtT58k+ZAtTZ4czsJy6/qRdPq5XoYs7DsMmNc14MUxz4SLZvIOtV/Edw87NsZmKKFxVjNZp6MCdDp2+Cj2KaNLy07JoaTg7KUcgHHE6dcieo0omNtkKfZ2+zO4XHHO95AiaZOGt96fnOpm+yHIOMKSx6oLzQx52KWW/X4juiiWu X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 40dca913-a6fb-4fa9-71b7-08db3aabdc5b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.5910 (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: HGd2tqN5yO+E7de7tAlnyK1HHpRwOKDxogctmUeTdUuwWOPphpIFOnNZVGxjH4fj X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 d2bcea7d9da815..b02f98dc02c284 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -315,10 +315,6 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (rc) return rc; - 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 @@ -327,6 +323,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 Tue Apr 11 16:43: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: 13207885 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 162C4C77B75 for ; Tue, 11 Apr 2023 16:43:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229982AbjDKQnp (ORCPT ); Tue, 11 Apr 2023 12:43:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjDKQnl (ORCPT ); Tue, 11 Apr 2023 12:43:41 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2069.outbound.protection.outlook.com [40.107.93.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1E80C49F8; Tue, 11 Apr 2023 09:43:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fvmY7uhMbucHNDo6wmCm2ThEDg67K6pptOqPjZJjB+CW215lXixjsajGPj7QdWsKSNeZW3mavm8i3zZGpSbqALWWl4PHN8uZ6qbu2mRrs3IZIt2v45JCI81RpDHcxkMYB+gsbdgQFydv5avGhxHj/0yXQ4YR5npXo0w9Zsc3aGKc4bCuySfKySBV0xdPqoyiw4JZvRoB57vcb3O0bX/aRQvEnAFbpeVUtHKENmttikBiIIfzMkWzxwACAAoaBcjXdXqlYlqAf2tfwF7QCwF3kfkrywZswcPtydM99nCyXvWSxs6hBDswe58m5MTftpE6xIBj3j5UPon+ezQsOvvf+A== 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=zOPaiGig87vimgHT+ArD1kqF9Z2jpRmES37V6Tdvtpg=; b=RkDQNgFWZpKnjeYMA2URAPDj28dvEW2OxXNhqWXvbo1ewmBFwMay0FWHIBYoZKVq4QP+LtlUs+KDuViy3bMr6HojeICRYQRwsQal3f5sSu6tCmUxuGy7YmPFDk3q22VtuRywOHIJdPk638ReM8aJ0jkknZvGICeP/DsC5DDrDr4hgl8pigcR1tvk+c4bm0+Ue0t8S8vMiMP/ZX5E4RNxdmuW424RSBDA6IHyREJIM+9rZV7HC0nBarWqH3xfJ0dcevx+VavI0XMvYmxPBHxv3g1BiRM3SHzIMf44X75U80/B9lkPXmPpcNu8JdHTScPFFRCR2gzIZOmxXOjzzU+Egg== 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=zOPaiGig87vimgHT+ArD1kqF9Z2jpRmES37V6Tdvtpg=; b=rqdXjQkpMjTPp+G4UDq6FChbFsBSsQxfSHFOxx+XgH5uLeBgQ0H1eJBwuhq2Ic4fKlzbsX+m9SSlopwNbGQLruPj5ILC9WKUvqsejm0tSZBfLjd3x9MQ1lJ0ifM4hVX9fx1SGz9fQMNfuRINvsB7ujgAezeiwcqhaC1/seERNHsU0J9ZQ+GICALHKZCVgp1Yjf1Swwt1lN9LtqR9tUcf4wg5owELuPuGSzFes3Xx1ZZrDU9DEqY1hu8eNommqxL/3uEvJHR904tgFXVe0PT6rnP0fyP/+euJpACqPTWokkVpfqaO4xRA1VVq9qUoJ75yEj2xUzmB60TjImPcoPH4Dg== 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 MN0PR12MB6077.namprd12.prod.outlook.com (2603:10b6:208:3cb::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.38; Tue, 11 Apr 2023 16:43:26 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:26 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 07/17] iommufd: Make sw_msi_start a group global Date: Tue, 11 Apr 2023 13:43:08 -0300 Message-Id: <7-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0013.namprd06.prod.outlook.com (2603:10b6:a03:d4::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_|MN0PR12MB6077:EE_ X-MS-Office365-Filtering-Correlation-Id: 1ecadf78-26f7-4790-3956-08db3aabdea1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UoRaL4kEczwbM3Iya8pVAALyFl0S5Lu8qGe35lKGkPpp/nECvdynRuMr6YxzQHszHAL6/CgHNpeW0WTghHtuhgpj8vWDveU9l1W/2rmQF0UMKM1hdyCmDL7K2MiFhils42xSDpp2m4676P0haZ2azj5eyjPFYUinLzO3PxlaU0UsFVkOBncLTSig+n7fBkcaQ/Qm3jS2s1E4Ro6NVgVumxmlY0Fiu7G9/kRPtPKiRDDqUOa0MAA1ugQdfhSmpZAMkX/b11SA4LeEm/dhv0soSzekYd+9TKK0nlEadfqFeQDLvHamnlsHJkPzRKAiZwJ4I2UpQQTOMkhXyp4IOFe1Av2Lw9Lnx6B1Yd7XogzomTBG5CSqPkXUAUrV/6l2z3SitXROglcc4d8Jx9nj7G6yqFQfdLcNAoKLwA1kAtincI5W7Z4aafmMBaODHvbszjOYIFr2M94/Qd16aeLHdA4uD1oJJS6g1N3u947Uo9guW0vPXP4KGn4EPn8JdXPjecitqTI4bBJWZXSRqw+WKrZrY1PP3dRcoc84lzb/VEvXne7XtdB7YbZqbXR6HiBV+Bg2QDHN3/MnqcGRCxqU4eNWTLmejvn7Ss9EKYjBDjomH04= 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)(136003)(396003)(366004)(39860400002)(376002)(451199021)(478600001)(26005)(316002)(6506007)(186003)(54906003)(2906002)(5660300002)(4326008)(66946007)(8676002)(6666004)(66476007)(6486002)(41300700001)(8936002)(66556008)(83380400001)(86362001)(36756003)(6512007)(2616005)(38100700002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: BBhr/1Bv+fWcDzejgjViz3a8d31EePTlzfnzcB78pL2APIpVtJfBmmBmzASBj9gwWBYB0GFGiITk/zJh/CD/PYSN/5z22QTiXqpNt7S6We/c3Lg5/a7/22CcXNrX0g9Q+Scrl1c7DsU3Ne5wn6EDQsRvgccnnlMhGr07FxvkDC//MhWSqj1cXjPxWxeZ+vLDl8FQRfIqjwUSQU7eqqQAFlZkj3tz+BXxHcql8ltwZlrZHBdyut1qDvZR0S6Bn5WGsQDe2bNi5bwKilyG79A+364617IdpdURl7QtEXPcFPH8++vuuAIrDpOUbaRODo0EG1/t4w7eaIG3jIOhmEyy72Igq90P1pLLJ0xyoVtpPW6CXOPSd9q6ahmYLDiO3G01goMwlxGvQ+6w91M++oG8yHHgWRoMJPa2g9lywn8k/qySMkeg/OyUFz43wSOQJZyk/+ij0DSOrHfph6c1vxyPt23imH9OxEyqNku89lgvUdwXm/pw4M+um638NgcwemqU8u9I6E8ix5vVAOCS4+fw7CTwlYohrGDUBp99rET7sJOpQroYWb0dw9xyM08ffLF8h+BUJQ82UMwwDkob+VZif69GiUZ6yXDFLEsqMESd1eDM+TLBKdXsR4rXDbquX5DweDkEr0v6WiRW1Nw+ZGDpf12a8jUs0J3a8t/ywwn1nWOzNP7q6cfKRmXAKXOI9VgXCTWIrvxUN2eoovV0GyZnPvz0skkF4HLqsAcXV2gEKKj3GWJ4ON4v3uGIIc3o/x77whQUoeVCfctOPRs3ovkpiFR1yTrofXUESrCLqq1XxDKcsPdwdRdOfpDh4wv4MtaDoAYFufARA9gpaTbvKPb4+RNjV/5ORP90WJWjQT6mIlutseLSxc/OLDltnXHIPtOtsj4o6OxGmx8vj4sLUoIO4LpdXqDse3ui7VB5vMhPSPf/xl6IH5ZDa6Ep5O3q9CObZFymYllM0O70IqCeLzBUtFJ/8HCGC4n9Rp/GW6T3/0aqG5RRHiI75XJzPFa8Vm1rMcZhvMz6mB95dOMUMlFN2LwvVtb5K3eeSPQnPgDrYRUgWN64htJin7nGl36T6NsWhkOFJDsiC36jUZKw1tLEmGNuS84NZhNvuAFGr7DZ8eEBghaq+4sckL0VDEahY51WwSO4BI/j5NnIW26wi/D63Fcvdt9C93rR8A1iQ1rzWsfVVjKXJvI4JJFud1yis1wWMLhwDBBeyRn3G1ssfpzXAnihUlCSoxK3DZ+p4Y18QxcfMjo9qyzIe5+OeuRjZ66yoEPaJQHrEt/AZ8EpuQPRPpRooYngO5MJaLzTWoKwQBpML5Wlz8JqHjKvBRHDfFAPMqB6ofd0qh9XHv3vzHv0sE7ccZoIb7oZ8N515M3tsU/bETv6H4RFvvbWXBooufzdSGCpNLIsTXM1FD14zzzrxQw/WYUAG9L3d+dsumDq4T0a5qzQLaXh4/6g3q0tSndbGoo/uy7GRNXNH8c/Z/0nW/n2uJ8zLN6ocTconcuw9JJKI44C/uiyFEhav2BnYVPDWdVn7aj4U7t2cqiQpgSGyW6HsffBmZb+ujlPakSCFpm8npUYmKERana3LuzSMzsp X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1ecadf78-26f7-4790-3956-08db3aabdea1 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:25.3470 (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: eQiV3R0FfY3xE36akrt1cUICErxjduFfVy0S43EwIdMfwfDNEluw/Qa5SzhY5stc X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6077 Precedence: bulk List-ID: X-Mailing-List: kvm@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 b02f98dc02c284..b828683f4d45a1 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; lockdep_assert_held(&idev->igroup->lock); @@ -310,8 +310,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) return rc; @@ -323,7 +323,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 Tue Apr 11 16:43: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: 13207883 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 9D58EC76196 for ; Tue, 11 Apr 2023 16:43:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229949AbjDKQnj (ORCPT ); Tue, 11 Apr 2023 12:43:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjDKQnd (ORCPT ); Tue, 11 Apr 2023 12:43:33 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE3604ECE; Tue, 11 Apr 2023 09:43:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Sf2fgey8WuTqshz7/2pre1a7TbKEkt2T66mCgQB+U/Yf1zqAcbUROUzoJ+natXPa9v2oaMtYgtsvVmifE5tfosdH+hduMoJyqOpQ+ESoej+AereiZASoeLs0sxfW+tAQv8DLdnfHlAXvoZCqp1sFwcNpllymsqHD9WN8LfHt6I8wj/gIgVhklgCVv6Xj6525EyDEDBCDhA0lVIk84IzAHf8jQBWnPdOPQrmEuhhJ1dE361a6yC2Tcvf7/PjOw5maUHyXPbNy8azowHoar7vTS1gf3JxmKrYFGYZc3+h227o8zY6yFibra7xW/obxnYLp/M8Kh9dC/CdIii+T8ZA9Rg== 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=FGJkRPbIexIVH+xwRqYapeKZ3Zts1p7lbEGw4jgPNJw=; b=YMKaXVxP00mXV5a75bZddTg5YzBQ2Y4R0PLiIzrbIWQDWu0+iDAucNT6w8THsN9zz1z0kNSOoUbHRdNh6IKcx/v/OvgoCN3P4gZrT5zPj8juvT4j5DtHAT356b/dQbkxmaeKPFrZRPfwp34DEaIwtbNeEfS/8Wg0enXm5Qd1tF5tWJ9zTmsiXj9DbsRk1YQQlPjYKVh+mmx5b6ofICxlNiRJOygP7oHdHQNfFPwMtApYPao12vANWuTFP1pTwl/3jnqsoukC9g5VKMdmMV7qhvD6LRnZEZNociCKzcXDsCw87yodK2f151+24V3jmJaEbxEwNqRHLhkGHbe3i8Vi2g== 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=FGJkRPbIexIVH+xwRqYapeKZ3Zts1p7lbEGw4jgPNJw=; b=RnOsueGARIDuMSm0i1sWJb/iQphery+T2Sm3n7rBUbIGJ8TZDDTTQQAT2VvCE6BULEJ4wcv5jfjsbCUXQnah1NiFmtE7SGt535gAANSkWvikYDJQWC/uZqmUYxIMpguXNo2IHXuLx7ieDzJ6Uc2PvuhZPCcH3FGZy14G2tZhN4XWYWDse2Nch1NAFhKz21cI6icVBifTfDnSvPaD0YUARS9kKUVuSFP45+qMZ+WD2uuDmhfLKlTFbwAcsiig23ezsFNTbz+0jCZxWme9Xf0FkwAtqpZdirhbPuh+6+bE2AQej1fDjcT3qyIffkQv2av8C+JYxJXHf01XbenaCZLW1Q== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 08/17] iommufd: Move putting a hwpt to a helper function Date: Tue, 11 Apr 2023 13:43:09 -0300 Message-Id: <8-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0050.namprd07.prod.outlook.com (2603:10b6:a03:60::27) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d2cd564-ab7c-477e-44fb-08db3aabdc92 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JgEGMnhwtwTLGABf8lLvgBqdW+jy3r76Ads2ythOxqsPRqOYpj8XkZUFCBLtVG+MQ9jhIfhvWFwdSpfpDB/RvZnFa8fhyUx4ddxEf7lef5lHq040FJ9GuTFtrS4Zus95B+lVh+pc+DQu85HXATQ2XV0xxUvhCLe1YG+A7ax7vGnEKbSRtVT4V8W3lvXK5G0FnjxkudyHh0wG+Gd/tqVkxuZoYiNUvzTwYJxDBLEz+dvhY7GcwiB7rZDCJAQXG5YmVDhsB6wRYER63BWqp6deoVZuWQnhT5DOE+7tpfnp8hTnOcbi3uqxL64LIJ6pPtUkGI1RaSSL29KIrGEFk3o7t+rVdujJwiF7vW/8vea4GXf1pdomHCBuC52JOl+Bat3Y1KUhxO+I90YfPQ/rMAYTqdbxXgRypet0FZZyniLrArV80MxtvIHMcdZ5r9Nw289FQSggk8upf91xINobwZZ1lHdTLGXlhqINK7pWHHctwXKZKH51vwF2/Fyejd7YNLnvKtaMFPrQdRRsJM4CBLGpAv8Hx5Fl83rDNMebMjZi4H5OWUr5PX06wshyNQNkOCCgKvuowaBUMG8uvY4Zl5uXewYHb4M5WW5D3QxNBr3cROA= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZsRXv22N++39zszLfgU4sxcgwQJDmRkkfUyZqh93fcHmm7AqVJG5gPclwfyYiBJ5E80ODVe7EQ2SDe5uHbDGEJ7IW3cGd4bDkWejDwIfng9VOsnIlxXvKYzwBgm43uGXafuNR/+dgF+D+o+5DjAV5dGXHzT9cp3sN+XyD9ZGmObL2Ig1KpmsSvEwK/oyRrMoJOSa9MhAE4b6ZQ12+iJf9EGKjX2GTJkxNdY7EdymtMG4jBIJKrMxASEJ1aDKhGAfXcTOcar49YbK6cTIpkY0NBz8GQSQ1W19ZIi16AOXD1e7KPP20jq9Mj7KCvrYl6wMFHEA0wyk7GZxmBhs1O9LDC7z2mIZrDAyVMR4COLlTJ+QocVofipCg+U98D9rL2OK/BGJve3L7l1xf017Hsj9eOsezz9GPPyq7XnWwQ+g+wcmnT4S2olbcZMBLytQfCFIw9PLGfY1OZ0euzSunnxft/SahZ/qLy0rWID8GBVY5sud/JDG2UxIRneZtKs++r32TOfxYTNAbciCyFFKpzGR1RNSH12qCQfdVmjcyCj80J86/INukDt4GxBmVo2M0pzHpB+F8lFwlyFUtpar4x4WNLs3VmMUxuloWL5otE91RO73uIP/4Hj1iM5+32PNOk6ZExfsE5kQgn2ib5CkIYt5erxP63JDuZqZeLN33GdLYGiFn9k1bRkXZAOGFV6TsOy7P3EaSUx8761FssafRYoWnms4C5sQtK9Ebqyrnn9gQQFtx5JgS7OU2QTUPhbQA8KEm16qLqzutTc7GDbMHGSTpXr0Jjf2oNUbePVa3WPfFs7QnC8+7+WS9TElP3EiTVL6/Hkml23aMCTHoJO/lnYsTp7Dk2uIVeAQHWkKQhyh6J/Ueac8F83TETOBFQOduI+c28Kl0z1QMWoMzh126vvSTAJYGa4d4cnzf1trP8QAmsqfL45B9OZLOhbPN1kllU3NViNkwD+AI6+eQcG8e5oaRlFbABAv4oOdxq5VIM5oqqXWinxQpcQsxXqgKWPyTm/9kVYiDiuJ7Jtq3wa8LM+f0r0qnzlkoKybixtFVIPD9tqrCkdQpY3xNmnwOYm2wV4U5HsVThmlYodgBLVBuqP437mz836jjg0lIIxYOgjTjz1bKEx3bJ+YSoozxYVVZBQaGfG6MrkUX698jo8TXtcHZ94480vTbSzxp1EyREZ1kD04onrvA3Vz9PLyoWrAdQoujzQjthyK/1pd+uat5kNJ5q2L26YDsT6aQiuXGSygaWWMBIYwuWEafwHfVXQ7k2GMZjqGA9HGBaawpW7prNhPdiwJ7lqS99b2+DD7b8YhcotZqr1EvgPYi5fS+oAL34mHoBOizI71f07WjutxDZTBaqa78ri+9tBLZcf1gti31+IKn8/6TJmDallvl1u9tb20tDc+9uDoa2utD3pwl0nWSU8+VuqTX3a+QtbbIrr/MjagWk+Rk0KJyt4Ok0WGVZblXzH7M+GB/t5x8kmGL79ruL6ETMgcctTMAeQRvFdu473QDnt3UeGCkORJ9Oa3oEMNonLxSDiFGPpYYm8vftRCsoVzLX7YXCdHHYGFX+Na7iBfRdfRxHC4iLqu+BIyqiRc X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d2cd564-ab7c-477e-44fb-08db3aabdc92 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.8934 (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: hg6bh9WCfli5xDV2K3rZVnFSHCZUVu3vqqgCOZfQhVOedA8/dzj/Nngou8KNU9xz X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 b828683f4d45a1..2ef20fcddbdd62 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -490,11 +490,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 Tue Apr 11 16:43: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: 13207900 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 97CECC77B6F for ; Tue, 11 Apr 2023 16:44:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230085AbjDKQod (ORCPT ); Tue, 11 Apr 2023 12:44:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230099AbjDKQo1 (ORCPT ); Tue, 11 Apr 2023 12:44:27 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7238159CA; Tue, 11 Apr 2023 09:44:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ecoat5a7SogbwScP/aWowjCs6j3TtGfVRxUX9VH5o95JRfvCPrbxMhSjrsu3Ol6o89ENL1kexCuPuDwNdUbneoaosnH3O3xxB29lPXpNDyyv9ZYxk91/SICzPWHme1Yc3d7QgeOj7gUFxV+1Y7ZwtTfkNYJ0kyXWquEPgTGwwfqykSlStSoiPk/eG0jgrcMY8ELrIM4KyeRZ4HjIG/5ZgkyMj9KwySR7FZNW427YELDDgzERBFFCEUrAgMz5Lm6VKrduj9n0n/OZ5szTRranhULoMGBvf3583ONK6ol9e7LudX5Xz+EOUlSwqhuHEhn4N2xCQtQ+rYUFh0kgSDyH5A== 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=y/Ec/lkq3YkLGz7aon4IaNs52pGelXfT9OgoLQGpR9Y=; b=ZwmKXMRY4Bpw9x+uwopRk8LbrM9PXdPTXubF5aJxatRhuHS8mb92ROycM3b4fHp/l5CUbUXNNBJ1rKptvLVKcWTAtsK6KaGMevNhbyytkyFX0g3vjZsHG5jSr/ltZ0eHArulCIRgDruT7GJ2dKIQNPSjRf4ehue2oaSnSxvuOGhp68f3c/7EUg18LwVg44p7J51dFQYh+LUge7TxXEhRrvObT9crYQvGfWAYXJ8rprU+bkxDlIyDSBgJ5Bnvoxk14dRZi4EvSUoL++j/rtmnnkTuZ63vHDZfWbntm88FK1PNkj47Cr+uzXVCiKPnrGx4UaKR8Raa5qUTC6TmPpnDhA== 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=y/Ec/lkq3YkLGz7aon4IaNs52pGelXfT9OgoLQGpR9Y=; b=LJiD5W7FEWWyClaOZpUQLUeffu16+6AKCkUB1yrnRMpXXFfl9SmVWG8gPdt59N4qEaMYgotRifmBb4XqGQ2Wvh4eaxmFiZaVHa8JQilMW6kcyqQJgOHUxyhK0Aetv7LCNavGLQVSJS0/CB8K36Vnm/ow8lFHqCreeOxxLo7TpiHb7R65FUz26kfTAM+bRCodZZPz9TNiifuSQXg5Trugfiu9s1EP5RrNMcUXH8XpwnaniYvFlgyUECsLEyjNTns13qg3skIOdsUPwaSq22gCZvjFYqAg9CyNLT0Dt/Cn/SfASUCpr3B5GWUKm+Py33hLcjLTeXEu0HcZVCNfsHpz2A== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:25 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:25 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 09/17] iommufd: Add enforced_cache_coherency to iommufd_hw_pagetable_alloc() Date: Tue, 11 Apr 2023 13:43:10 -0300 Message-Id: <9-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0070.namprd07.prod.outlook.com (2603:10b6:a03:60::47) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 37194508-d5b2-4ad1-540e-08db3aabde57 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qHjtc7LUiBby8np6lq/XiP9tFPfNa+IPXM3Fwy0sZO5UUaQQSBYW3RfsYZLc+AXQgcLi9g1wqBtvYBvArP346XqRIb14odgWJpYk2UnZEJ36+P/8k9b1hEumOF5UEXfCkAPw7bxacXeO2fLsF4OYovhVqg6So1og4X1Gy0J6XRsR9XBAjd+iNKF4fiaIVs4KWhTK+Y5E3dM4siGIlmbS0BunOyahG4aUCXHQiSSwFQb4eK8U7tZLc9oqiEx3FltRTKDPLxbBe4hIqT2nbDPxhghs+oi/j+q2leTDTxn+jn7/2+KuDBhvX4McIczHQbcwU83dQh6vzo9aw0jY9DJnBKoWNw72gzm15z/fXxeLWE5KAVwA919zhdLjrexqTNV9nMGp9NaIg7ziDaTxBkRVabn1Y9YgoRJj+lElla/XwNfDeH05RYzz4FlT/wzZcOZK1albvNVD9nodvzqanS9GhAo7qfg4WAmx+WFdE4YM53I8Y28mG3JFDX3rwRFtpxp2USaNHzLW7VEjiNb/RAHE3qVmI9vbOv6Bm44j9sPypIJU9zPrix10nbHLCRJBI1gg9J+nKu7i+IGZqX49b/OwS9l2d67dVaiXRBwelW46C2A= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +EEkImLw+dOVo4mLp173emcjXBAwu3+/ebjPNaFYxErB/KQXOMOOb+9l/0hlTChD9nWGizHKIUH/Vm3TftGjRNmjWrp5na/FEZsL23TcTiMbR3iFEKprj2WeGrdL86fkC0ztmDKxTkaQM+aDgqoE0Wr9bKCTekBahdCl3tdba1aAtsnmtGec0Z9EVxysHwEOPJWprGGMmh4iX5QpURYHFm6BOCvRgZ88bo5AJHj1F81+6jmwMuXAvgrgRfWenAYWSYgL98LKO9fRc6pGNZhCw2mM7yfhmVFs0K+GZbp7Q7+oYDWM7z9Ce2lVYnWul0SJPurFk7SzGH/twLvtOGaPHAO+Peh19INBK3qb1W0M+FJk1p5b5G41GDIi2LD9OWTLv9KC4jA0azjUDi2DwNP3rAqD74DKUfTHUYo+9ezONiBz8FvnNeA3i17wS+4ntYJgO0D90kcyM9GKgoDYc2zT/DTiag5fR5Nc2Sm5gsFSd/N7A1moVzHYPVeNQKCgc4UODUXQaL7BhfgUfhgkncjjzAbcZjOWh1Y7smR9Ssyh2GtwAWjU+C2b5+IWKGde0bhy/4QMwGfrr6esAJ2K5v6ujBZ+FpbPzukNKSevvLVIVxVE32NBpA5IXgIBntP2Lu6DHfunmcFvV+Xsy7lgCRv7u2WryRz/7U4JS1XE3yCuXqvaIkbxxe+uEbhSWtGqQ4tVKSSj2Fpph8Nazw5N1aO6j7UdTKQsk3YAwxurxbGdreUZTjw5gLf439mnkzz4K3sUOWs2KzbAWTung83ljvzMBMNhznTKSpvq+P1Kr2noDfY6eme6OB8BfKe+fLF04gKdTQ6oWTOWY/jbn7FTcJliMI45Fvs0FIjyfiCezBkCUOd75gvdxj9mPIS9emM/GwOHwQX3+XLLHyzg/+f4R2jT3/OxvKGA+lrqX2uN7D7Q/qTuEki2F3cTrTf7ObUXjsG2YfYSLx+gt/CUQb1sK/Sgu0Ie2CmXuzAIYs1JX9P1R8oebIxgmCPEj9TvgY9mB5ZVGMHuEqi9OVhRgazcROc6bcM1Z19dCAG1q6mQMdxQjPF0Qk9KwrNXkFghQaSd2nByE/HC4DTYnZh/66mOIt3ao4+oKp9VUvMFVIrAgVVkXKFGJY65YbLnnvhqDdT95MTtojZ1p+duVMcoyofI89sN7zBTXM2Z/4nFRn4Vdx600gXUZPaYiLm8ClUKDf71SOGG9QFDy2xXrqTgM07/hbzO5bF3ms9FLyikMZn+EcWSNkDMDS+7MCFqI7YD4mGOD5tN/hlw5lEvnGk2IHGt3zXjnRpAmt/GmME58dNfvPfAYmxlD9NUfENh7/aEXPEtULc7V438/cA7f2bA2r5T6lK0rV06/e8BjnDQpMdjinUE1s2dfqay5spF+tR+6hJWWA2FqgAZZwK/RpA/cjBNnNATOcUxrxx5IjGC5HzrCxCld9KYhTGQL3X71mDU3s8ru5qltRDdJtkNmc4UzE743Bc/V72kO3scJs5wwX37W+IXG5EW0MlX58if6CbKQcx8npJqmpsth2fmYDmwb0x8zVof6IktLF/oI2NfjFFGm23nrXhZr40y8pWOzQBdeLD3nhyI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 37194508-d5b2-4ad1-540e-08db3aabde57 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:24.9079 (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: kdKbNsL2xr3lfCKxFwXA3oxY6E8JLkhKFj9CTK3+mzeyEJlmD3jbN/mJyaOoQeqT X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 | 20 +++++------------- drivers/iommu/iommufd/hw_pagetable.c | 27 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 2ef20fcddbdd62..8f72a528325f04 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -293,21 +293,11 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, if (idev->igroup->hwpt != NULL && idev->igroup->hwpt != hwpt) return -EINVAL; - /* - * 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)); - return -EINVAL; - } + /* Try to upgrade the domain we have */ + if (idev->enforce_cache_coherency) { + rc = iommufd_hw_pagetable_enforce_cc(hwpt); + if (rc) + return rc; } 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 566eba0cd9b917..2584f9038b29a2 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; + } + mutex_lock(&idev->igroup->lock); /* 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 Tue Apr 11 16:43: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: 13207880 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 CC817C76196 for ; Tue, 11 Apr 2023 16:43:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229878AbjDKQna (ORCPT ); Tue, 11 Apr 2023 12:43:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229848AbjDKQn2 (ORCPT ); Tue, 11 Apr 2023 12:43:28 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62AB0120; Tue, 11 Apr 2023 09:43:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dIKLYQBwrFqSUKEzcUdn9dmjXwsY7AN3hC6XSRrh1vB0IgkB4nTahTpjSLhlhp8/Jow+IYdiSbQD00ampq0xNxaswoHXCm/6bWPMM2aRFm5H6odbCZfixQvlDNrG/PQ96/vqOtM09yym/PWSH5Yboings0bCZHYuFzIPXwjT0zMQ2WUf1f9RRLRAv2Lqh2FeGhDdcSjKKHwwKgui+7iwEX1rZ+1gKwUiRmX9Ya3e8U87yTwrpH0DxzfLrA0eJBcvXpree/r16EPDp4TTAP3bTZ15XwLa3e+9hsl5A46LjvQW4F/WG4swFRZjB/77RigWPFMp1SkwRcWvzZ6vifHoiw== 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=Zlb9W+5EtTwswzSQq518V7RNAu9hwA9wh1V7iPQkOaI=; b=ZGxwpkDVKnTBObuCoXtbyh6jLhflj6FBM5fthAanuWHXAcfHW+/gYqa2x1UZtzPxjINNR0+kiMwQxvzfiDyZeXdIRkYCGCdG5P8LgbfRN26d/w8UpAzV/b+K07GXzyLiBcLGHuZC52CKgGy6mnyMZ2qu+CF5cBOMN3XWUyarTnS8WbHVWvniY6b9CFCae6dgopMCFBL1N2e/q6GFbAUL3Wflocm+gVN574Y/ExNN1CELb9voXUMZ5ZlM9GOrIfkGRrNVDNgXeIXXXpYuBkk76CArwSzFzf1PJg3sLtHKMuqacFAeroU1U2hlwrkPYn/WahIxcIHOrkIHhSLgRsIqiA== 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=Zlb9W+5EtTwswzSQq518V7RNAu9hwA9wh1V7iPQkOaI=; b=Jx8p8q/UShpVR7xnab7hzDyckrv14k88cd/MVeQs+fuELKaH4R/G2WKLOuD5hBaQ9Yyo2OtzSzj4xpdHCZKmusqrY7Hh5YaV3Gd07gG2YAyPhdiEN/csInfAJL1oVkdEa2WrQ6vq80PABBUVzJrY4tPA5GptrPXNPM+gsMC6inF6l18z/ez6tH6YlBWKyJ801Bs+iFJEbnvklCmwVkeLtAiqDIBWv8Edded8ccM4GcM9IWH+p0tUKOTGfjPwRf+R7KU4z73vUnXjaSLPpqI5Bw/sEiQlJF2Xxzbsojnu4kldUA9wur2GPOtYV/0RrQXwH/tnlG0guEVQHB0r0PExPQ== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 10/17] iommufd: Reorganize iommufd_device_attach into iommufd_device_change_pt Date: Tue, 11 Apr 2023 13:43:11 -0300 Message-Id: <10-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0023.namprd06.prod.outlook.com (2603:10b6:a03:d4::36) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 854e06f9-244d-48ac-c7f4-08db3aabdc0c X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YUQHOhBdyMwHqi55jqfjpwJM7wUryJRS96q8oO+a7uQOtDsFDkGjsAg4ztXh2bSvHXcKW6BILWfAEL6zVdDRbeXCCpUuPGrpZkkwRfZXpMmS4nzaYfhdhh7YF7GYsAPxahg+EogMblwL1jMvFO93zCViOfW0GW96VwQoUt7Kr1NocExuNzXdv9boDM2uzHEusDejH+SMn54jM5Mzyc11HgfoVnobjjfN8uSpbdXiObmdVvjN26R+QWASbdcPqlfo//2luZgxVbqxLGgaRsjK70Lard9nMu64oxqme188X7TkdZmfdZ+eQgKP4ZuLSppE+0oYVfHUgwssSmEnXNxbNAmA2XC+jwJwPHDEDt9tegzqibcXQN/rioj7tLgFyhw5S9MAoKeCZrdkigyhJoj9NqIs18h0Qkgw04XDXJFUjmunDk9jzVouSP7w/vkbeL0mHUmXj+C0KZ3ckwQ7ZqXnb7mX28VPIEtEaFxnhGBJZxN8srEPiUo3ooPdQzIwWAZ4+86HgJECTgnrsdHcXgi+3afip0+A6V/QzfhBDWeqI5V1f+GpYidHNJ9bT7AM5Ceb 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Mywoz2urXDPajIt3Ksge/48d1JIGd0C2M8rRUv0aL0hAQLtrDRAKr5xhwcXPanJ3iqSe5+bd5dde1ehe7rO64uRpWOg/Ef/JTgeAhdKL4SbytFM7xYjvKD8bS8Cbm+9YQ2oOtbdVqgMpE/XUA7oI6fegaqkMgdqRUdfDKHITo2xfAjgNZ0mhL1n0WjbC05l3L1vWv4fr+rIpkZFPq5hRKA59a+8QK/JtrSpYzqJ7gPmMBPrM4dBIxoRnYKPj10fHVXyUECCHk5QSJYT/ZkP0Y5dBrjPuols7JiDR5YmSII17vMOQXeRV9vUYK2cq7JPlDbnHjkAZUcLDYRhkjmM0FI1iHLcvfmDa9mZtL0o+RLy2+Rt+k0Mxd271myoz5cw2KVw7MDJGrlwXjimZ/8isoIcTcWXHaZzzMRP+KSYVZgWCp77AfiL23uXCgtHWIRk/rTXIzDSwlBdzqfxrJG6Vf7Phqtqfmge5Xan3pJ6EQfnNsFCzyP7qOB5tJsRCsi5wcvg8r3Cp0G5k3v0cZ+72mr2n69ZVLZcb/c8j3fYlIqYxohTUObe3ieGKd7FyxWoD6SNcbcB6+r1B96IyXzGNeNw9b/Dz5Nr46NcZCizHfaMkXVz/5+NVlUpCIqjKCtnIG87zjk8m3+xLUonKhJ5CtB4af/a85EQCh1Tr9k6LWTqcIzeCSsH4pvoPpcyOfm9ixajZovloxInQ9TcApOlzmRXUSZNeK/oqSTLx8U5TyAj3a/BvkdUDwobgM2swHGSQYNwQcVZ+wkNIWpcjJoJNN4MkqN3cTr/ibrqYYpEeRZopYcSu14g8YCrhlI5wA0zl0OHZqhbFnnoMqWnPrbS791M51VPFohfYqXZoqX+Kh8HtWlZsaU0OHR5mPIJeEs+i7kdBFXzGEPM1kePjmjgmnW9NLFkuwuePNw1xGlpJLDFgH3q73AZ76OX6AiSVCWrdQCqXCr0JkbGFjjiMcu1FLNpNrRnucq/QlNDGrOkOjSEI0YBFTnhsiViPGIpfIFy9bVnGXDvEBt+YfjXcEfmacMEz0W/sBzN1ua88q0DdxqNPrFeRU+1f821gJLvVlJSfyvI8v/Qj2fKw4GSGGtsnlbuKjDbaCSQlCN1GllITSgucvn6u0LplGBP5DvHkeWMH+QJcxnYLc9tgoWvxCw6G49nSYF5hbljcCrx9ex4uLIlZiz4wnDV5Cu8HqqB5eHTp7/CMxm2JmrjyX42XBW2nu2sOWQbGpmGcpVrbKvioGQ+wcxQ+vgdwf6qBVjlINeptz1GeA14k1w2AX/RZ1CPgdj39JuB47plMWUzij9P+M2CyduwIqS27AR5FKCGZvJSlr4cVmQqhFfw6eO6Q3LNvqmwLS6CTdVXuhum44VjWM3RSDhqNlA+5sf+Weuw4AZeEysodwar+XA0vWM+Vhs9LuBqYHpampFC8gzAkVtnJLPzQsWdmrJ2a9AvvxwhXh2qIPzkSeBfqk337Y1nMoHFd40DBF3lwlCFuYKkkvs++XzUrxVxRXl2m6/uOt8F01w3EWiXuSQVUb1S0LUpDMwB5ywi459SslO2p6mHEIrJnKUUEyKemBUlCogtXKiYHQ3ge X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 854e06f9-244d-48ac-c7f4-08db3aabdc0c X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.5737 (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: RvR2ppytzUUltIIESO70t33yjGVyc3OovA9ocREXJ0kvkhGLsD0JsXN+6R7ltxpB X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 | 138 +++++++++++++++++++++++---------- 1 file changed, 96 insertions(+), 42 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 8f72a528325f04..e9979ca2494c48 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -347,27 +347,43 @@ iommufd_hw_pagetable_detach(struct iommufd_device *idev) return hwpt; } -static int iommufd_device_do_attach(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt) +static struct iommufd_hw_pagetable * +iommufd_device_do_attach(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) { int rc; mutex_lock(&idev->igroup->lock); rc = iommufd_hw_pagetable_attach(hwpt, idev); mutex_unlock(&idev->igroup->lock); - return rc; + 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 @@ -381,52 +397,58 @@ 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); - 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)) @@ -437,8 +459,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_device_do_attach(idev, hwpt); - if (rc) + destroy_hwpt = (*do_attach)(idev, hwpt); + if (IS_ERR(destroy_hwpt)) goto out_put_pt_obj; break; } @@ -446,22 +468,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 Tue Apr 11 16:43:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13207891 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 77029C77B70 for ; Tue, 11 Apr 2023 16:44:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230044AbjDKQoR (ORCPT ); Tue, 11 Apr 2023 12:44:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48350 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229639AbjDKQoP (ORCPT ); Tue, 11 Apr 2023 12:44:15 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E76D55AF; Tue, 11 Apr 2023 09:43:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=i7o4pb0DIREuOIBFUJpayfIB/rC0ID8Pt6O9DwsfVp++dwJ4lyytMtib3dkz4HVrH+TqGx9Nfh6KRRZDmM7iVdnSyHwmE20YeZkLbXAhpK6O+5hVnSQPFIn4nHa2Tso0p32mvxFh5RawnFt3gvdRomemgalwc0CN07N4kvzEa0bQRDhfIW8d9QSVGA+4UiSBGkuyF267CLPSVcmtKTQggPhUHrE9+BhoFnyZcUfzWgpX3gCeFzm2hfhz3gWv5Qe+mXnCeCA8E+OcP3m3ra2TJNTTClVq6erxKymda7jpb1xRVeUPfkDCXY1DMGDDm+H6FNrUK7pu7Wqho+irYDuWMA== 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=Croa5QGThs6PZRxpp3Mh8bwlm+uU0lpfXzNyBIDAZwNRPNPMWo0YAefPbUN1mhgO4QlRErTMRGcQIbb584KuKDaJM6tVGjr8NR3KLEl7CQ8zsZRWrX1MU+4Q2/i3/GufDkeZNuAnswz4Ju7HB6KOBCyoMxTKi1cJWHHHiGEn0bn95+knNKbEM3JwL5jxh6076wC+mviuLRJYZfeWuQHhup54z6lfAcw/jyGQLoAaQibiaMeZYyDPuXM94WhaSxynMmlhcJMTwZdBJ5aQJGjg0X+RdHQIGu2Ts9Pp/vEJdkJaG00ZpPpiat+A6zk0iGXbakH17rFiPATfA3hvCRP+Yw== 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=XhZgpf3Ta8bg3zkK3Zzkwf7Y7EsOnpVcMUAfN9pnP2ALk1cI6Ij74jNcEjn7cbRXEzlBDwIFpud6hj1m8qKlRtpZukqviDu9eTnaNH85b7G5KJxpECgNIQAslmrtJTkWIniXkurEfbsMZfX5mFA+qx37kq2cepFNhiPS3r6X9K825ZdbA6qdLu6CBRZAR0zs7oSEfZcJPck39Cm/8d+j5pTx+NlZtAZrC9UdY/Sq3zygkham1XcWNmaNOPg+RZ5j3A8nUUhX84LIwoWsts2wpHxd2cQbkXlWX4anEA8M5xHFdRcWqfQf8TGeio3s+Mt+75XhEZuxV3xvwespsnQDsQ== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:24 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:24 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 11/17] iommu: Introduce a new iommu_group_replace_domain() API Date: Tue, 11 Apr 2023 13:43:12 -0300 Message-Id: <11-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR06CA0011.namprd06.prod.outlook.com (2603:10b6:a03:d4::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: d8190818-4855-451c-6cd1-08db3aabde3b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: M/y5hUeR61qNQDGrdmblI5q+zcvpetl/9sylGSRjx2nlxZs/uUO4yTxRK8MkY0ZBUHQj8fFzifD/zwtuBM/JwfqnqMnY5+OCuzzsOxeTck53rb0OuaUcS1c0M66cejePtqPK4W/w1OtIbtTDPVGjgD7xggKzRLjIbz0S8nhcoIgiUld0+qckYi6CvVNIfUD2rzH/Of3k/tr86JjMknLX2OZSIOjUExk9ZjlElhxItfbVmDIjaL4/dPKdW054wziaVZMFy4YeOetg5gzPChbIo3/FHnxazrKOWa2pFV6Hu8JjU+wo/L6wg35xuLGQuncX6cABscZp9Lu51hTXPYafYBDvVDjVRLMxWvoWbunkDw6nI640/uFHCfzwqlxsiYRiZptcKK4wWY/q3DKFCQtaBoS+k+Xvw9FBcyXmm7NaR2fCqDMayE3RWArTvQaEjTqBTTUBr+YL0xegcbEI6DSj+bY6f54WXNcZLZjvZp7gPNt91ZzFb1RQYaGLZ4S2N0d2jc5EU4kYwxqEQo9XQxfmrZ1miiVJdmyK4fm4D5VHvA5/bqpsrRTDr3mO4leWybLs 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 0QVQNGd2eGRLV2+ITGB8NHtUPi9HDyzLl6AYGmtRad5RUMI1tDtmbGIU1NvnT3+9wiUBISvsWliXdptqu7Z1Fm6zivTUiR/7eZo10xDlEZXJ2Brb9JoKUaIa+S/bXVuIQC73fHfDQDaJYNvxEyC1U/SqvHntIXwsv9CRNQ9kAGVqQTw3X/1CBi4rf9t2xDzfHgCX6YrkZT1BDmfnss7xblD8XPWfR69E1UFiv7uhMqTlr4QHwGsbqig/V7M6/s5jvaURoSx6P8+hKyA+xgkTglIi2qxtkBz8yHa+lJRe1/XrUZhqwVVAKNLmBHk4HS4m4YQEkYuwtuE0EvL3/JB1qeFhB8x2zs0496XD8yE9YK4CcNnbcikATgybQZC4/hlkxEy/b1OtZK5TlEGUn65Sgpa1PheSgvud7TKjgdGmPtu/ELiG5JHAQLuNpOuCiF0VW0VPQS4cHp8tEMkPpMieokn1saFMR8ZTRQbutlWy9iTysn3fnGLBRcbrsTWPAHx70v33nShIGQFjUgtZUlwcjTGG+fW9zMpI/+2QuvdRNF6gxVVdOdkjJIhibq88HAfEUC1+HozC1sJI+zyhtVx0LadtfYb/lX+cXkQRMJEunGNUjtlSPcGOOwMy0xuBeydXVYr1gWLp5d2L4OTVb5qBijE14y0Sg2R0rQjby+XdK4QHOUfbNENw4quouxOPKmK8HY83GnZzihA0/0df8QfVIXpZaYMVwgmUwxR3gWgqtvgaLCg+27mV9gkNC537lx9QlXDJ5cie4qlXIaEtItmP8fmm4beZzTEKiYcg90TemPLjwnkA6GblhtSyFceRDUWfMTu5ALxhGtAmI+g3P5hiw+19AI/5ZMgNiikkIuYpY9rqRbeH/g8tkS8ibOIOI2T8kCM+n6TVkz9lf3C3uZ1PWsu9/9j7SaBHkaMqTFgQ/29WxA2yxNFP8R3ajQmC+sSRlxX3bxvJuZ9DfpkV3Xv7082ejXk6tSopbnps0PBGZzhGu8f9J3jLH/8OgEnkdCUEV9UxspJlGtfkrcwYjSUfGVS+EPjMosc5JT+W6FxWPdP6lZY1KVRRWgiJW1VoDY+RRQD5IGIL3DRPNjVQcDMJOlSqhkTFltMX/5n92RMV7FYJzhezrCM/eYFHIWJG6l6rAAL981wsgqyZuxLwD4Po6Q3Z69PHRl24Ht5SBkf7yXYCkqYZcgJN15wa9sqW1dbo2ErCRYXtEAa3zX85ti4rG4IjYIsQnDxHlNDBld2c64Jeh/BSOTZ45pwch4kI+a/HPQEFNPgTsNDKI6c16U18sNWrBryXlEmKXpsS101S/QlLaE3dycqfi9IMcOAjlRCjz/p3GMUY13wxPpfGRhu7AlIcDTAsH9Fbklr791hmdF3q1ULrbIMMQkwl8ZLTDbIarem9qzuAFIX7TX6LqvXvlVgxuVYIQ+Lb2WXW8AysQ6EK5auCBQdLWl7HCYdQeDsqboPwYkzr+1f9TokqrRMjdEYoCKFTPw4WotUVrfTCBB9Zaf9ADdc5MUlS9Z0zFKjvWFJqu6QKuCsh1CrJRFqp6S8h/JUgJvbWmlQOXpBVp7JvDWQh+T9FVhaVcnwoPOi5 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d8190818-4855-451c-6cd1-08db3aabde3b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:24.6776 (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: OE+ID1B3GCskiDjkk8y16bhyLXwvgnUkPHUAJh3ZeVefbgc63Kf+KmPKYu7FliyO X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Nicolin Chen qemu has a need to replace the translations associated with a domain when the guest does large-scale operations like switching between an IDENTITY domain and, say, dma-iommu.c. Currently, it does this by replacing all the mappings in a single domain, but this is very inefficient and means that domains have to be per-device rather than per-translation. Provide a high-level API to allow replacements of one domain with another. This is similar to a detach/attach cycle except it doesn't force the group to go to the blocking domain in-between. By removing this forced blocking domain the iommu driver has the opportunity to implement a non-disruptive replacement of the domain to the greatest extent its hardware allows. This allows the qemu emulation of the vIOMMU to be more complete, as real hardware often has a non-distruptive replacement capability. It could be possible to address this by simply removing the protection from the iommu_attach_group(), but it is not so clear if that is safe for the few users. Thus, add a new API to serve this new purpose. All drivers are already required to support changing between active UNMANAGED domains when using their attach_dev ops. This API is expected to be used only by IOMMUFD, so add to the iommu-priv header and mark it as IOMMUFD_INTERNAL. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Lu Baolu Signed-off-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommu-priv.h | 10 ++++++++++ drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 drivers/iommu/iommu-priv.h diff --git a/drivers/iommu/iommu-priv.h b/drivers/iommu/iommu-priv.h new file mode 100644 index 00000000000000..7c8011bfd15374 --- /dev/null +++ b/drivers/iommu/iommu-priv.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __LINUX_IOMMU_PRIV_H +#define __LINUX_IOMMU_PRIV_H + +#include + +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain); + +#endif /* __LINUX_IOMMU_PRIV_H */ diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 76969904b93af4..91a948a76db5ee 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -33,6 +33,7 @@ #include #include "dma-iommu.h" +#include "iommu-priv.h" #include "iommu-sva.h" @@ -2191,6 +2192,35 @@ int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) } EXPORT_SYMBOL_GPL(iommu_attach_group); +/** + * iommu_group_replace_domain - replace the domain that a group is attached to + * @new_domain: new IOMMU domain to replace with + * @group: IOMMU group that will be attached to the new domain + * + * This API allows the group to switch domains without being forced to go to + * the blocking domain in-between. + * + * If the currently attached domain is a core domain (e.g. a default_domain), + * it will act just like the iommu_attach_group(). + */ +int iommu_group_replace_domain(struct iommu_group *group, + struct iommu_domain *new_domain) +{ + int ret; + + if (!new_domain) + return -EINVAL; + + mutex_lock(&group->mutex); + ret = __iommu_group_set_domain(group, new_domain); + if (ret) + __iommu_group_for_each_dev(group, group->domain, + iommu_group_do_attach_device); + mutex_unlock(&group->mutex); + return ret; +} +EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL); + static int iommu_group_do_set_platform_dma(struct device *dev, void *data) { const struct iommu_ops *ops = dev_iommu_ops(dev); From patchwork Tue Apr 11 16:43:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13207890 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 44802C76196 for ; Tue, 11 Apr 2023 16:44:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229576AbjDKQoI (ORCPT ); Tue, 11 Apr 2023 12:44:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229503AbjDKQoH (ORCPT ); Tue, 11 Apr 2023 12:44:07 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C178B55BF; Tue, 11 Apr 2023 09:43:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ncNWvSaWTUR/WuvjCE9+4qyv8aT8YzcZ/RSc0Ik0GC3haS3FBifrwUp+u2SeFwrYdT56wqVWAuEgtnIQaR90tTgaPmRfInVVkU3dNmBzdBP1SBC0jjH/AGzuvSsXJ2r81drXKkGMUbVOaUYdXMwl6Jsb5eLencpGm9BQbomiEQusabvfHbJa465AgCxrEFC0YHnR75+A3kyHbRE5ACbaPuX3iASWtMsThCF+dezaNSV7FVV6frKVt8GUtxTVKoVgGyi95XCbkFjVS4SmRryLkOGKIoMjMVdv4em/A+uqqaf10nQoLizFomAu3ZmWEGjf9givw1dHdBGKMWPOzFV0WQ== 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=HZHoMrZPiMXn1kBU9u3/K01xH6uPz7RYUd3HcdEQ7fY=; b=FLij/8GIX67+6gv+Wj6bmbRMVeQMU4YTZPpwvUp8IGP4Jgdw95pXs5dtXjduje0szKmVxduosdrJQ3z1NBjIwudCZR5QVt8PWO6h4v4SIb6O5klgT+u/eCFcGeSXAcQNncmEkz19RKEYx075oU7EZ4aPLSCYKKOFxgUYOeA6/fl5jUJPnNd+eOvF6dTStgNw77q6+0OBfe0BMmGwxl6oRioCB42IAQlgrFmS6Q3yNqdszNIMOXxNJKRDGzUBLvy4xkQA0ntJmdX3I4Vio0zefuW/OY8nVQdBgxTslmva4icAwlD+entgjUhV50JmeNwDCHxOveQ/3HFrz5YetUosyQ== 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=HZHoMrZPiMXn1kBU9u3/K01xH6uPz7RYUd3HcdEQ7fY=; b=HB5ANYvZZ87j4e6Ibzd0RQ01c1V9pG7d+1gdBrnGDGy7/NqFF/TDnQu9cY0btlzxPgIITIIh0pTwzj3ds2uz0L4J2Bght4JZKurU6MXsgLMTBS+zMDE/1+4Zo2yo+f/WgdH7ngsC0us9RXwo/dvs2gpuq8KpLirXmgEFA6o6pifFU7mtXNvIUVUmaqzUoV9KbiWjJHOonttd2AikvTFyMZlTqyyXqaz+FnFnM5PspPFfta6OYYYUgRaeuN4ESTYzwg3K185b1uguM1Lhztbjm//qEUImudhgyQilWNFucGZd9d04CUG8vLLEgUTus2HNlGd7srBZ5qtiKmIeeQetMw== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:24 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:24 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 12/17] iommufd: Add iommufd_device_replace() Date: Tue, 11 Apr 2023 13:43:13 -0300 Message-Id: <12-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0053.namprd07.prod.outlook.com (2603:10b6:a03:60::30) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 2fc21bfd-d654-4f78-904c-08db3aabde25 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: M2o12X6DRX+ddtF4pI52LDz654JiDFxsCtqTFyGZbcC9leKGHYtT+GuDmgQWL8z1rV2l9sY1gJjJkH9s4vqCQ1hSXbWxfPDL7Z6t1+OhKABDw3n7e/u4K7orx+AP2Z18sTDY+OmNKMNOcKwTgvELyQhA9BhDgDJtPmqipw8cotnMvTpvYHj0WCjc7WPDK/aN6+n1XZjVcpqPrM7ZPd82Yvw0Lqo/1Nl82fkn4heYv7YkqtnOGIETvB2Em75b8I0UgJiy+KnY7pRMO9lt8yo951/hQow4aNFaO9hKRZ00juWBJLrXANuLLBKOz0jutP+R9jDNSS/PkjeWIkK3sS7d3lnKbFcp0XtLZFKPw3ZuML6MlZ3ppbmFw4dHMtUijJqeyPtXWVFYWaCdKg+ar7yvVMrkWjsD8+uGq55BG2gfVn4gP8mII6n9YBC31BIlPVEDIn5+atTaVHOGCL1SP1q18CgbT7NOIseZYYoLTJ9jxuEXbei7CLI++btk9bd5A1q1j8w5CVSVN7f4obzS3jzEcVX0KO/IrzR3S5fF8syydWq5jWhH6ZVow39Ho0kaysb1 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TADFkJZ8iXaZZf8LnX/UGFFYl8nEwEBXltI4GZy/LVjk3FGe9v5QodPlnt7P602Lke5c6r7VHuJ+saDlsnM4DDd99p3VmjeOvEmAOQIR1z7zE3tslTVoRNXZRcb6FyHd+IX+5FTnk80R9vuStDCAuu0l537DblZzsHv2acBZtReKMtWGBccW+yijYvUZ6T6TOaTeaVE0H3/O0MvQO1/ugD/1beFbODn3yi6i0c/8aD0HGmKNb3+GHqUTbgGOwf0CLswl5QCW5esY5yeLqG7LMb3MfETeCBsWdOJbu6mFg5LcFSiiJyQpQ8rVgQLiFMVOF4VWHJcMLSHg04kcn1VqgONtL6/8a04Q+9k+TEJtSUnF1ievXGc+m4rQjF5vRIWtMtg1xRkrcR97qkTjA+rYaLQwEpXM6F/HhOqTH0nlR8EMG0jtcS/CBLP6eP3OGsl1yYIkqcN9/OeKi0CuYx5Iw5mK0QrwV1S22MFB4IkCuD8WA0PKyAiQdAPCwlJSSpy40pe9rk1qqOHCrTBDs8ASe1aBk3Mbk6ACsHYHosnXbBpWhEi4mgysa7kAjtFWK3+YBq1NO/pIYcwrDsKVTCZBd7OxUGyBvxkxCXrseIi7RKZ2QyW3MgGw5bvg6PXOTHecymgtAtCpP+vgZSuja2IjKIdVpkJ/o+MGIB4ziFx20fiueoeSk6e8yRI6jX1XE2aD7yz8AfgOTg9uqYx93hsAY5WGy6YJQzK5Or4CwHzUOqkE0TLTJEctJevzK8ryE/40OWYXLfAmTK8zH08sxz7rsb9qVKfjuPCuV5rM33ZPmLz294lINifSBFQEkcMJhfdr3+zW8+PCwRaHiEDiKbj+3fMX9tzVaVPJeYdTgeQ2hOrKuFdlaToNIGieOHu48wgytTf0TSXXdZ5d2trHv0vsFGZ00KpSSjtOvCjlp4WfFqPYNwnBcihM5xAHb8Tgw0HXwRZzMK15aBHVsBdkwmJ5ZdyQaqDmElirYFqLtvMv5P8BlhCxTRhhvGqs8KdS7Rgj745iWzH04SdwXrZfnSfjgRjZ3tzSmTxqTBXRUCqHxgizHTLYhhMhA56RAJ2JT6zLvwUy5VrIHocrlwfp39vTuqw5F+Q833w5iU+6YWLJThylIroN4X2lpigJmnDZOdeY/3/kAo3vAeticr1UoYc+4nvZvhNLuKqLHYPw+D9JCKbaxH5cVpV663vPEJ2FZfQh2YF0bVUeAuwN/FgU1TDZb956asju/895xI16F7SdswzL5w7JUSDf3c+3iKG9PhhOyX6NKn0BBEMYkX/8LZgCf5X43+wI7iyvvwSkWz1xd1rnzXLBkp9oUjctjmx38VvN64VZzdc7UqFTGmATsCU+feaZofxWwZu8R3pdgOsicNannXktyMU0icxTrPFlkwt9+QJMTjiQuu6qehxWQYV2eqW4v+0JnLzwQtOxbLnqK8dKxvnwtdBPidDfguA78kB/1OGghnOE8v3ThAVgE4CL15gbel7PaUGwElpGedM3vNQDKE2jD/qUIqyRlHdD+pT3NVWyo0ZanfjmulGyffO3gMkG3p5uJAfUqmgMG9NmkEq441rvwmQucHqSPAgSGOWi X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2fc21bfd-d654-4f78-904c-08db3aabde25 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:24.5390 (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: gik8RPi5+XJs5d0/xZBkRVw0H7e+3Tlqn7mX8N6ml+CPz4D+pQjZPFhRxQpcuI/d X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 e9979ca2494c48..c4edf2fc740793 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" @@ -361,6 +362,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); @@ -519,6 +598,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 Tue Apr 11 16:43:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13207878 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 BF069C77B73 for ; Tue, 11 Apr 2023 16:43:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229833AbjDKQn1 (ORCPT ); Tue, 11 Apr 2023 12:43:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229712AbjDKQnY (ORCPT ); Tue, 11 Apr 2023 12:43:24 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A27EE4C37; Tue, 11 Apr 2023 09:43:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=P3POoeJEhRXWJKdNbeLbeWQw2GDrsSTf34O/eHNz3yRdGXHSeXlKeor5tEw8E5ZK3FQ8s/UReXcRaGYqHqAtp4uD4qiOYUN13pUdj2cXHP2OV213oJ0NrFuupw2mx9nvXmhw9CuZ3adqSawy4U5DsxazjEQkUlLypZgO7RgHOm+VMJnaop/rzAcJRzDmNXaqzp27Uwl3wNcpeS1OckXIbs6K60TkH+QRq7FS/b44GPxmu7eEezOFrFUrkRlJCBhuRHzBpc+LIEt8URCrry1unXkzd7Ras0t7HhoWnmyeOO3LX850ZwZDHTAf969r+myXvVUek2Ick4ktmXoyOf+5/w== 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=B7Ynt0p9B4uNk2UA7vCpiRWPyRvi6nUgcVCNixhEkg7vZMRbP1PfRTMTSa2CTh0M4BS/wJwMLisbta2v+LSiEBCI7UekotJUjkZmXS9+haF/3CXtDz/d3QBaFuxgJHl9hF+PpHCFGzEKPAGSdB+6w2T3Kc6dQpjOHJ81RbpwDxVd5l8NgoH2LRLRGBLI2COSZ3bPLHWqB70wageUjgMrqk04o0nXCpq81jrJoygcafAyGQpBd2Ssk4M5/rJ7dDxCuK5FM/Q3M4WxALRAi+eXh7dmqJvyqhfuOcn3BjYZTr9dzcz/SUkRfPG57+WQZabePQnfsHNAUmSvJieEXFF/gQ== 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=XPcRT3V9I69Lm8G/nXWwrrSo+JN2spH7GwU9xQ6/GzeTF3+zUOly7HqDT49j0QJxEZwktvIJRhiReoa3SKBx23ClfdQmxnBgq3IMp4v0D3r+fMOxxqg3kW4pHw1YsdU0NNBskTefcbE0EA0hIt6IutjgNmK/SBubTuXOeeYJ2UazArB/dfi+FM2/pjim5HGAcXPS4Awre7ax0YedpNNcmPz9Q5W4Crnjh86lPjkn5OUBfztroYUQ6jnyF12cSsvrwtdbAs1523nSs2eTdbXltgN0GkKxQfQrNfxWcXNyszlbE5lQPmii3az9vM9bB9lMaEP9kVA8IidBO8hFlk9LJw== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:21 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:21 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 13/17] iommufd: Make destroy_rwsem use a lock class per object type Date: Tue, 11 Apr 2023 13:43:14 -0300 Message-Id: <13-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0041.namprd07.prod.outlook.com (2603:10b6:a03:60::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 9c88baa4-27a7-415a-71b7-08db3aabdc5b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xtyReyKaAd7/tS3//vG2AfbPDS7WK75xisrbaaQzWq4o9uVJNXY7bPP7PS/YzJI9w0AUNrtljp/8DnmSHTHC8sBqA7jRwIshU3eMLcqVhbQk0kNDMGe6g1//JZsE4+rThay4g8gs/5h/gI8KgDXx0ZvEewCSh4o5cXBpaKNr5+9fOR5/u8xUvy9KspM1JzIKXNYkW39tE5RDtQxLJk2UlXSLoVU3gYZyRz1JHfWLE7s1j+b4ht0M5S4aD14OjBe1Ak/ULOx/90AkOHvT+LggyvGcg8HTXCEwKiXPBKsoMxJGG/lZbQddLv2nSfvomayFlwdPP7XBe4NlJUEhx1LGFX4AOEO/LccUExGaDL4ZGNpN8mfcFahUKK7jJOnbE5KCg+BgUOZfg9Kn7yPRWv0jwnKAC9TV8dfKIy8heHFr2XPXd0YdikGHMi89bZCNIHqg8IszFr869uh/6QAdpcp8le02hVIqcAUFq56nimC75jk+1QrkEZSldYwm3n8mXTB/Dk303VWr9e9kV4qryZ6AnKW9wFl92Wo3FR/DmOnIJ210zSnTaL9fYfQ2i6ijXbOu 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sBbn2VKsFqHaSOvUXTluWOEWyZl8bjjBeDraG6gx/eNo+0qBHo9bAKVCm29upt7oo7pO5LUMNX/rLee3IPOhbOKyzVScYvmahw/7ZixmdmqdWtLpjqI+0PYcKxvrXpJwPAa1NxhqUu8YVA4b0JCR6K/bNrK8zpyC4YihPknQPKJewVnP8Jd/CiXsfAb7JYR0QPzkmrxNvJKlLVzuTbkCU4EqutVUozbonOwlfGEQFraICplXw0dIQQFqC0e2FeCq5gvmbVa3ooHplwPf0pP/nt3qeWaGwER4qD7actwICQsz/CI+JFCTEjthcLZtd/1JlQvyoQ6T6DY7PehJyp/gj+8kTDmMHrN1WH/mW2nUygE/o2dQZiQKP1Z+H8T7Rz6SWvc2zH3yRY4R6JG1EXTsD7xyi8W4W7KaUWcnQsk9kDos0hIUyIobdfMisgLqSeUL6dxRe9NwtsPMjTSK1iz/Pnm93YpMp5mpC3bEy2NYNxdHNGjWu4yxpbvN9neRAB9H58Fecy1x95EFUMQuiIeFwAdAyH9AkAAZMijLto5jpYQWMj19WL/MbjQZykWToC0J7lNglyxbimGmWFLWXi6SeeoNBhWfjk7B/iV0ItRDgiTqtOXOX/ONO0DW6O/1MDjeJhvpbrBLosG/ojhgksiHZ7kqo49Km3L6wr44QhR42460hwZ0n/FJt7Vn7QxlcUNW8ZsU/T7IP5WeM2T0wQfljie4lyGxNjT801wLiZsKz860CEmkCZQoPHH9yU61b5U+sziiCrvIONSlIAyEUaNlGxBCxqjl3sg/UYqSif4+DrJCd0qPr9PZ9p2VBMFct5VPZ2v/1hQzojP9XWP/ejVARBZzYYok0xRZ1wvRMnFJyY9sIJfT7wRxqruKEUJ0oaNcj/nwvSB1YYvEQxXc4o8vMwKoPmv53/PzPBiv/ysKXdwol6OJU6NmG6xj2KlplZrw/LLGpfNl30BWHOqkh1coIDG4IQlvhECi2VABQHjkQjLLzrglBX0m+OLys8/HEcsudyOll8JDoCUdn761lWkTkZh606jOFSpqg50cToGWJA1doYGKVCYGfTJX/C1qS8UB9hIg1IUsIsstVgQn+oo5UkCcIUxN2vBegFm7E9YPnn0/PBZmJL5mFS6OeMp4eFqoZq01Lib6WP6pFTOv39SRa0g2u5+zjkg1ZF4M4TnHINCic+u3kNTzK08psmTBggPfeohTuFXhuY9fSIuUTvW0E8YS+HGp16H9W9mlOVMQh8rsbCszSmMZ3/001gwRuCOI6KAwZC9vTwrZHTqDHPeTTkTbBr63wRwexLeFt4s7uTMhFLm5Wq2xTvHLa799rjczF1Si9CJ5AluzJIhA85eUx+kWWCpk6T/uEG+YZQDGpAMj7K814X8y9U+Q2IAaWLnT9TCIwIDc1c1uAJS+Xjch8m3Vy6bSQMC9R3yPkfBC1rDsxoPS2zxidbQ/j+UMcqvJiagEqJxM5lVVa4uh68lz5WkeKGYidKjjCj8tRD+NQfYri7FtG1wQ2qh0kYdmuB7WoySkfwn/pylzt6SHDkQfpHmWRG9hx0HnMV+rsvcgFyyezFRPCvcqS4BqTP+ItsJZ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9c88baa4-27a7-415a-71b7-08db3aabdc5b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.5578 (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: xGIj1sOV6Co6NYhHhZf+ctE5HwKXowjfViNn5XLn50NZ/7A2dvwId+W6D4nA0f4Y X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 Tue Apr 11 16:43:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13207888 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 52946C77B70 for ; Tue, 11 Apr 2023 16:43:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229603AbjDKQn4 (ORCPT ); Tue, 11 Apr 2023 12:43:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbjDKQnx (ORCPT ); Tue, 11 Apr 2023 12:43:53 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2069.outbound.protection.outlook.com [40.107.93.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E321755AF; Tue, 11 Apr 2023 09:43:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nyWn910rMBJgIeOmajDZRt5Ho2+urQJQMQKNYeNmoNrxRymrMK4VM1OEOdir4wfkGSMB1Og1dcths3e54r/fdpeb6jf9VaZKXQ3Me/mmziRCdt0NGXK5qnp2rWoozpvU6cULd0K26Ko/dsZgG8Ik1ug5uXYio8NV2r33jwJGD4sNfzloz9Cq9jS4tnu1UOrmVaASHHbwjggECHPyv0c9Z/M68FL+2joxKVFXlM65SQEuaEdpvsRJDmgTV9nv2oGWJf8R4hPhKJlHjZyL78QvJlL5gzzVF7V0Kap7aEc+Ks19F7WAeWFNbiRz1UQoKoz/pl62DTrtx6tzdoDSOUuj7g== 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=1ghKHbRE07T4XrpixzlOMsV4FCPz3qkBGoHREdGoGiI=; b=aOsrVNraIle5kudgChtOG7c9WH061sIpzi7U+pPjY2B4aBbC/b1I5SGg14uB63UtGe6WWKXNOLuLqyk7BlknWUy0++b2GCfEn71fZePU6LaT5qhamAdqcRBUE81QvEMneg3J4JeVNNqi6BnQ7luyYBmvq7oiuvWXfL63SE5jRBbBs+PhD8iD3f77VOPXmHhAKpLlI/mbZN0Rd/tTQGdnMtQBTqe5jCKON6hUSUkKv924yBxJTfe2pByOQz1Baa08CkJcuu36Ahe5LAkfIv+FAXnXOTYkH4Jb3/ZMHi6dA9IZgGO4bqrtjD+qLazTjzbYcMFZnj080m0GAc0ORrQjiA== 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=1ghKHbRE07T4XrpixzlOMsV4FCPz3qkBGoHREdGoGiI=; b=O7JcwHZxlxdAGrZZ9bd6Imj6ovZHRLH5ijcbt3SgJqAHoXet/+yfvIL2FJvXmiKKmYYmxFv999IeTSWUmdHCp9IAWvi0NGikUj13IctGWXccxphbQtQDXOz/OmFvpk9i33B7vUK1n6WZxxsDiYBfQsQ1ElwvY3XaHC7oOa04vwWa/vgNhmK790FbeoxX0VLi+UyCCKodsDO+kTuPZUVJcXK1GTfVL7FXodm5cXdAGuH2Pk4MvQBKLQ7EHfvgJa+v6+BDNAzCOxjI9YvGuHzzrMeqi8KP2II21BBE2DIG9puSQqnxaGqpSmtidOELhw5HccgquRDlV2l3TGqN92hXOw== 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 MN0PR12MB6077.namprd12.prod.outlook.com (2603:10b6:208:3cb::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.38; Tue, 11 Apr 2023 16:43:28 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:27 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 14/17] iommufd/selftest: Test iommufd_device_replace() Date: Tue, 11 Apr 2023 13:43:15 -0300 Message-Id: <14-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR01CA0066.prod.exchangelabs.com (2603:10b6:a03:94::43) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|MN0PR12MB6077:EE_ X-MS-Office365-Filtering-Correlation-Id: c237a09a-2974-4430-bea5-08db3aabe00b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YVEgeHe7PW34tbAP/YzvwZW+b4ukSfYYNYVpYEOwUgDKpHL+XNZM+rXkJEHGcSkVKBzxWFK4/nW+VpVHymA0+K91vMugDEH9W2mqg1iAzx49rsYzLPb+r40fo7z01U1rS2DvVrPVqcKmoYyteAp8VHMCC/3+DFbRQtL17u5Omt5eYg0dpOyfYYCr+LSxIZJUjTzn26dL/JyVJ5GWeNU516bsTbSngHNjaxTORLP4E9HEsdije0Hfq6OLEgB5BmNWMDanX0GbF6lyPmLX15hva8/Ux5xW0WUwTfJeNt6SWeq583Bwf/x1XvdqCh7TtEdC2fAj1WD8qoYgO3VRy3OBkVYfXDHiEr9JdlmXR9WXp8RPbsRL5ahnVgIjuw8PLo8xR9U1ZHBOoUpNgZFbucPeCE7ivM9S20g127BizGmFzow7qufowUfx98bQ6YSsgyN089rBnJP4vLDYpsvv4TDpMw41yV7kGwZWxb2S42iDk4d3rKTf+zm8cwprUegRPSuff8V6dg03QdgiRUE79TRO6u1Qus6fVBsKHhtv6y1FibdKHAMzEqCiRSTqEw3Fa/Eh 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)(136003)(396003)(366004)(39860400002)(376002)(451199021)(478600001)(26005)(316002)(6506007)(186003)(54906003)(2906002)(5660300002)(4326008)(66946007)(8676002)(6666004)(66476007)(6486002)(41300700001)(8936002)(66556008)(83380400001)(86362001)(36756003)(6512007)(2616005)(38100700002)(66899021);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AAG3qhVBDIYNFp5YMQNiKXiv1OT+LO3hLVldwXXctBIBAh3DMeZflW4yX1sxXFdGA70gogR8Bje/cCou5KKNh/1de7LouAVWCWc36U9bw8BO8sdinviecIQkZwfwdpfTVvrj0Dl2c39dIw+eF9XlLrtLChJE2lidPFamLuRFqHY+o7TXFAR0FXmhPv/Q0+hm22CsvoGPoqManZIxR9wt6M3U5LGAM48l+gCZG3tgHs3EbRI+N9YCFqgGfZSAGum4m8MbZzEHJyJYEwpUNS5TxFcZw/A3UTWNp62JIQVLIkfKMt8GtwLC6FRmJr6kVN8FcruZU4gIcSxgYOqbvGQyWfG+SjPXK2a1S4a5Yx3GtG/nHahtueROeFFAayxAZYXROxlxeOkyifSyqx+hfQfEIZXpyJzwxhj1LnKAricWDt7tj5aVXZq99ZjqwQU5d5ZbUDBNS6kGVu+NbHfcU1hklTb8K3KCay6tOMozQPQrqKWfeu4xXDgqlncTZy77utPOeSkSwmGutBUW0klftveuusnDy+EudQG7xD1SZ8mOWH5l+8dimiuTUuvaxuH7SG84+/LoTtHZAqe0YujbmAOoVh6hf2AMP0wB1Zf8RY8IA+1h8ECuNpqIBI7WduE7feMDQIiOJMr2uu3hml9Hwp2t95rguQhHsQWeIWbQMJH3jFPpjFjeCvbN1Escsgtdj9RAfPBjUjyPoMDHrQP+W+C1a5qANvgCZNuObfXnfFTPptx4rdMQGPwIT79Zox9HyXRnr54nxlgjvi/H1ZQPmocg/cQPKdwzBpDT19sB5gTS9WhzZWTHVr3o9wLCToNeT29W1k5Y5UssBL2Furq7wK701Wpwi5itP2hKcAU1KLGvZwhEhekQJqMSd3XGrQI0ne6YQYYVbn557jHlsYz/4F5a5VibWwhQTMiZaetjhYMB3toY8k29epxxwv2e4FYp/3r+FENu1tFaXLzYyI7moIPto4pGJePzL/HoJC0EkK4aeiYnXOkhEG+87lLoTeOza7yloxH2cjw40WzLatmpaUcE0KIyQfIwqRZk+QOOc4REkzAeQCK4rqNAby0vEqrcMjST1d0jt708ayedtNrnRjH59mVUhQJ5DO+CYGaQ3nBaJGxbVWtl6MOo/ufkcqZFAjbXGbXQUTdsrXuXGwfr6Ln91BCFUQSsgNjYGY6wpeRZuYeGV7NXi/0Y2X4FX+LsUJF8FMGIUwOV7Xc2P0CtzyvfblcvHQIPYXLUBa/vskYuSKNS7eDwgGNn14XhYu7YKjkprUeuMtIe+Z/+CjZP1raMU2E7tbvs0WCS0lKM0GqvDuCwSTBLdZPtZSAMbwYgOjSjdhn5Fiwz1xlSAdTwrzjVZw8sX4EYizhHIs2SOxtUC0/MfyGHVa4TWP9zy3xyW6sGqhTmb/4axlK0bHvirUqlqvONYt+zvMauAKXovxB6c6S2RfBpdRsQY9GGz5iJjXTRTaxt6d/CM1aKb4bSLtkwqufaURFBW8KSM+1zO42BdDaW8+oWvfmshEac6AtyCU73W4uVCyUj6HqvyibtAZQFzauEbZN/Lu4uCjJsSa1k4fXdD4pE0tbnAnA06YMQNIma X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c237a09a-2974-4430-bea5-08db3aabe00b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:27.9479 (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: F5/L1VEH57mv2M1kFfeHpupHEGhCwAe9ogGUcy1OzXRWT9V8yfkuwcFAdnStLmf9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6077 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Nicolin Chen Allow the selftest to call the function on the mock idev, add some tests to exercise it. Reviewed-by: Kevin Tian 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 | 37 ++++++++++++++-- .../selftests/iommu/iommufd_fail_nth.c | 42 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd_utils.h | 30 +++++++++++++ 6 files changed, 149 insertions(+), 4 deletions(-) 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..57559f60ca6486 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -9,9 +9,6 @@ #include "iommufd_utils.h" -static void *buffer; - -static unsigned long PAGE_SIZE; static unsigned long HUGEPAGE_SIZE; #define MOCK_PAGE_SIZE (PAGE_SIZE / 2) @@ -1001,6 +998,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 +1020,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 +1273,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..96fb2f04623f23 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -41,6 +41,8 @@ static int writeat(int dfd, const char *fn, const char *val) static __attribute__((constructor)) void setup_buffer(void) { + PAGE_SIZE = sysconf(_SC_PAGE_SIZE); + BUFFER_SIZE = 2*1024*1024; buffer = mmap(0, BUFFER_SIZE, PROT_READ | PROT_WRITE, @@ -569,4 +571,44 @@ 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; + __u64 iova; + + 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; + + iova = MOCK_APERTURE_START; + if (_test_ioctl_ioas_map(self->fd, ioas_id, buffer, PAGE_SIZE, &iova, + IOMMU_IOAS_MAP_FIXED_IOVA | + IOMMU_IOAS_MAP_WRITEABLE | + IOMMU_IOAS_MAP_READABLE)) + return -1; + if (_test_ioctl_ioas_map(self->fd, ioas_id2, buffer, PAGE_SIZE, &iova, + IOMMU_IOAS_MAP_FIXED_IOVA | + IOMMU_IOAS_MAP_WRITEABLE | + IOMMU_IOAS_MAP_READABLE)) + 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..8b11bb70095269 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -19,6 +19,8 @@ static void *buffer; static unsigned long BUFFER_SIZE; +static unsigned long PAGE_SIZE; + /* * Have the kernel check the refcount on pages. I don't know why a freshly * mmap'd anon non-compound page starts out with a ref of 3 @@ -66,6 +68,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 Tue Apr 11 16:43:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13207899 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 3D5B3C77B74 for ; Tue, 11 Apr 2023 16:44:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230090AbjDKQoY (ORCPT ); Tue, 11 Apr 2023 12:44:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229520AbjDKQoW (ORCPT ); Tue, 11 Apr 2023 12:44:22 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0B8A55BD; Tue, 11 Apr 2023 09:44:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BPcrn9BI+JYsXBr3EEXjxSeB9bSuiIYHr4U3AxUr7jZAPJf+WGE9MUCdYro5buN5EPnpf6z8pNz5BfB0a6FYUzVuluZvN615yzI3R/S+cFBaqZWKSD4bbLefWW01c8ZkhJhkA6UNf0PJW2Xzi82RWjPxNw/qgUFqKE8vKLIuB5JNcGbsfkW+18q98rMupFukTU9DJa2tlrl415wc1Xc08H8zh1kbmXoYBKsj2MDhx0BLogll/AWzjlF4gZnnNt7bSH6DaEVupy6DoO52NrBzAys6ZXNtsGm94+Ctyn3eeWIsGeZ3HYqZFxt9dZKD2jN0oyog9OjZ7qvuI7LZEPPhtw== 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=BvrnnwDom4Oqm2knrQDf9JuYE1zB1fnRpGK8g7w9uMs=; b=fPhINz1dX4e6XV5eLhKob8lB72AWZFcZgXnX9uEx44T8iAc+uWNu4Ts35xPSKD4fEF8wePef18P4+E14MGwFieQPvssXFXKR1Zc1T1ut1pngiFve/yTnelIS8ro4EnKt0Kx+aOTvCO0OnvRvS2C8rf+uN6bZ1tpR/AZbfluZ+rvx6rKdysjmBtCyJoysj3k/hdzfYqhayqlA+7kmRxkY3DDkKiF/vBns3L0vuBau5z0DlBKnz6WCb5y1HzgX4jVEMTaY9lZL4Dwr5L4Qzq5kUvgDK+CWJFTrwV5bpXkzAH8iEq+hS2oRtspXHN2a7U2YlEqIjHKsbnRu8W2DhJd1Sg== 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=BvrnnwDom4Oqm2knrQDf9JuYE1zB1fnRpGK8g7w9uMs=; b=bdU0ED+BSuxHsNsTfp5HqZf99HwNwIhOC3yN4nHIl//zTHgHGVVoGAmtBd4NLWD3EfjT+X5Bt4AJyYBIOyGP4MI6HpHCy7FzSxo+RFOO5JvR3c3k9YmbYQBhybPVeV5gI3LKjLVxLCVE2m9BWymdvg+wI4McuL6Y7xva7juRCNw9RnoekPsqhbaKXhd2AxhCnem4GY0HxBVxEqRnCrAjQsBB3jkm5TVhhsdK+VXqOMETS+7O3SncTE9o3DT6ffpqjTwMYXSDcbHYATAeWHrbRy28fqevd4MFFjOefVTsL41tRJuBNKWwoQpWDDblQEdtmrILqSnjnw0yAmKgd3Maqg== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:25 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:25 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 15/17] iommufd: Add IOMMU_HWPT_ALLOC Date: Tue, 11 Apr 2023 13:43:16 -0300 Message-Id: <15-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: SJ0PR13CA0154.namprd13.prod.outlook.com (2603:10b6:a03:2c7::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 2f68ee23-ff1c-415a-4263-08db3aabde44 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: p1CZ1PNt/WRYzvloUHzp0xuyOXwoUqWCHjOPUTiG63IXH6D5Gpa1rvBzzF3uXmhy8KyUKVAOpWjoFtkPzmfWTWima65VdJjlgJ9Dfm8IgihimOglaj48wzEPEIYea/lfVbd+OXfSUrNki2hNC7hTQI4+5c+3olhNBaJWvbuY/0tJAwP7dm42svY9pyQR3lSsQrQvNW9qrelMEr+4luhu0S82R7Ey55nRo8Izi/qmubTyxSdT0n99NwezUSnQQRJYmqfZbs463k6PqhQBfZiZJLeqx5KM2NkiYV1NAjeDY4cUzHtjofsh7/DxNDCKO2gAVgjy+3flO534fSYcixo9jWVePwKuIWnZhw9chbYDqWYbZvWTcn8v/zQF9V72ECTO/ekz8tJLm9C7Ye85+uQstRgi672lGA2KcqNoWnDzkfuUqGwNNDhGvqpuY/SlwduDSz61NbsddlHJSouP2G9Ykskv1WXye6R0/mStoMxF027fYBXfkNVXc+9kVHuZ+OtCYCOlWPM8pKeYJOEiP+wqsyjmTHLQBwCk+VBeiAlVTiYJ+fQ7NydjiON9ycGV6MqD 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qabLqYJM+Rtf9dYgNHk2a6Ajc0HwJy9vCxF04hPr+AmcH3RwffHKIK+2sSFJGdSSP/Sd9jsvrJ7EVPNZC5HJrsoBAsMvqKU3C1wgOyAcsIzI7zevqeVvf+mIKt1uIEgLEeI+TROESSVRIy5eBQEXfad1j9XiET72CqdnH5EdGwhcykkwguMgFo131iewUtEpuEu0nByBOuHPmJuAtq6Rkb9On+mMPU4IocDp7LaxWPTZGInexdhMqMFx0j7bZasbwoLH048+1AbsGPQfH1ZxDjZ4Dom4HjP0hqSjDueXHYZCbaGBNxPiCuav4OFLV8AmcZ/W9thhydR6+tRTp/NOXHztKd+xePKsXMwxTu7BB4uut+kPe1/CYuonOme0bEjTloRy2r01nLSCT9ofXu42Uvr/S5YLAoQl8NWPB1IbFFYEW3IuMkGy3XzYO5Ra7RkSCV1nzfDscKGmyt95+LDOQkraMVsfI4SoEQjkhYpUUKaSnheR+FWRBVBxlhp3c71Bd5YrJRvNmG0lgyC2s1WHaZJFhGGCAFl3TvT+RKGwq4jJZjauj1rt2mMoXnasIqSpN4/ZoB4j8E4wiRSZjCOx0iWHTNvrJ7OhTLB07xBD3Nec0BYse94iLr9g+65qPWj5NUyTzyvJMTPrGNWTkBQCqAYuM+0LOjlnl3R1HbPQmO5txy5b0BWSci5PzDl475O8LA6PkCzxp6m16Uh5guV0omiIWY+fwLTvRCuzohpAOeCbZPuV0LG9wflef6fMKwOiBi9d+1T/ekzpwjiIK1oLk0sbnUlQO/7JYzH3gmfqQwVozHNq3Mbh1e836TlVd9VUOrRfjKy6APy1rkulk0K9/rRhU9e+FYG7yREi+OSFSSRxcIqphsEiypFaLuKhf31G4JcyFI4Lhjt+JeuUTAUY/0eusDrOKY9f4og5H2KIhaGgVFo+XezxiwuGI30hBVd00Oh1fhg/l+UbMakp3K80XgRzIwC0zjse1asjWpF0/ykjZErLSKDFzAxzy/UBKnpqn9T3X7ua1jiVRyqVfgqfAPoQm7MSw2WVupLIsw46/oERQwBvA8VLrFQm0l3BVrcbFqr9qg6duzFzgsApKEdvPRSVXd3LGBkji7dN3puHN00ySW5HTZ0a10aUcidZQlWT1ZZlKWn5el6GHfismJY6sOdqrFL2u4MKe0Jhchq998ckyBIu1++mB5O9gm1qM85SmssuVzp570NlHOj8uiH+q9MDHGYklU4OYjVnBMapcc17WXpFFfwMCWIOtaydaj6+OPmT2aWT4hq5DtJDe2cKJ2412Sf9YiueZW0aruNnrbNuDIvjlZnQdI3jxeKs4pGkVkcwj26yO24FW8AvQQPdZCpCrOcFW32arkvPT2ZzkCipCFR/Xe0VUQJ8aIx0/l2/GE7vwelQhtqS9iHNqySS61bibW5v2BNSzGSjE8QxU74iDfbHuCHPCEw5V72Er95YBC//dp1lfT5IZfa40Ykb5PM+B7PyPXCqs0BtlbCYpqy/Lk1+YvxouJ2hHV1M8EzK8fV6afhq5iaCJ0Xgc8i05t8waW25hw7P+vMGhkGJOd8q6sjG1mNBOU4z6QePN1FQ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2f68ee23-ff1c-415a-4263-08db3aabde44 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:24.7487 (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: tO4N6DwjScEPAP70NLRhvhsiMazpTCbtLRGf2CIDyGQk8KdStXKFUau05L+C/OZe X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 2584f9038b29a2..30c4c44a99b7a9 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" @@ -121,3 +122,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 Tue Apr 11 16:43:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13207886 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 6433DC76196 for ; Tue, 11 Apr 2023 16:43:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229980AbjDKQnr (ORCPT ); Tue, 11 Apr 2023 12:43:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229953AbjDKQnm (ORCPT ); Tue, 11 Apr 2023 12:43:42 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFF785247; Tue, 11 Apr 2023 09:43:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FD4yBoBfg1UxH9C5Ru7mzh3wOdEmg9YT5+h2OHyqrn/5KANKCs9gZWeBgI1ZhHmXWjZwqK7vQvrTAG7vYdkmpcFY0CQ4Vn1urpN9pnY855zXt6CDITl5tA3Z7RCBVgqJ/tIss4JIUOdczYjaCGDy6wiByy36lA/nM5t9ebEMrC4D/UWp5vr9RlBshuGP14BmWc7UiZJRJ5WkwcW94k9eFNTfrLrS0uWfYl58sEj5QkzvCICO3w34irHg0xOpR2YkINfaIRMfSt2wjYGIo9khO7GZOUAirHSf1pYVtB/S+XDiIjpEXEu3pt3PueaY3Ha7wyWC1C+N1VAVGRkeombo0Q== 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=+913O9G4ulLwvk+m50ADYtqHPLOTp0dqAaNi68zgDug=; b=Evm1e/9LCUiJbxwctEghq/GzJ1Kv8DgQbOXN3c6RMI/ThOXhP1VAdpOZbRJr6wB0sapE7ByAMzMDvCJpOpg4fmKSjq599tLCpvXHdQzU7wh14NMbWvQ7lPkU+G9iqyCIVuTvLZQYKlTaKbVUQYvdmtJ3WYJvI4Z82E9jcoC+2rOLoUUi7ZGKEenFBgRJXCxP/GpRzfl6Y2QtuWE6W3xEWvL50mEz9U3HiQRBRfQ1lrqDq61YNJ9oMF+abNA+U89BIct+WR8fs4aO5+G6IBdoUfMiQ+OQ9jTeG3WD2jnQsIfxAm3raJiqPxbye1U6qf/fuqnjbBgK8g5HUHPb6ErY7A== 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=+913O9G4ulLwvk+m50ADYtqHPLOTp0dqAaNi68zgDug=; b=Q/qORTMiE3DvA8+c5EMj51H3D3MNKcrpPT2zqo+nDIcifDjYtOh4AMG+kKNPA6If9qdoyQuvVv4FKSaFLH01bctQoivqbx6Ujxe0bkcsi+tBLW9EAYxGtq+HAulcT4rgC3CJl0y4uu68oiUy5swWA6c1I4FktnzXVfQCuoDn3C3S8gtOnMTL/xB8dYnajulWaVx21GYCsHpqEMEijSWAllkuD0iajTPGfkiucIgaoYradEyI986O65hFK3p/wPJUN/B6uSqXt/5WTeQWp4oUzCuBtu9UMc3CcVQw6kxVFgfdWF6pdK3A4rfm6SzMg3yEi5D5Lf+2q1mm9IUKthIknQ== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:23 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:23 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 16/17] iommufd/selftest: Return the real idev id from selftest mock_domain Date: Tue, 11 Apr 2023 13:43:17 -0300 Message-Id: <16-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0053.namprd07.prod.outlook.com (2603:10b6:a03:60::30) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 38b213af-e413-4245-59fa-08db3aabdc86 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: z5EeHtH+GLIOfqRcehAOX7ZfseO56vtXQUYk3d/aPl1JZaI/pVX5KgPjrqkka20lGL1o370gapv5LnsFOfYteGghDrGskGyMvTy5bjsNKt+pNQ1/jwGZ2aFEla1TLuC9sImh+EqlNO7OLdcIAl8Rep/LgmqC/OJF//2+0IyTJqr1GaAgRJrkg/gA2KPuOjC+DSAK3+dVgt2yGbm+B4P/CRjRgjOU623Npcjb3P3kfQDpbGSU3oNiSmL6lwb3G3vmRDu9Zdz69xwt65hSh0uB8HELVjPOM9Z2eS9aKX1/TthvbJ+DKBnAzmSwooX6oYkqRp4/jdTmWYlEejTK2zLndWZkDw69VBPtKZRAtqx6SVoM2ohyjZRSqnnbkHxsCK0Bo7guC1WAh25ChmXAgeNqN+ayAN56OyK7euzcQkvZqy3dnR51SlVR5K8w3p4UegZXi+ASvVydq85nEkfx2Jfw+wpHSjRFOXQsNEhRP2l5RT/5P55oYuEO+PThTufwDZUl4LOm0+exmoBxI4XwxeR9YOQj5FlewEQycLqY208iW4P5sqVB5H4WkWM/Ir+mGQY8 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UuiF9mgArs/O9+pzHWbMS4J6CqJ9xO41Cph+pVbET0xTwAUDf+Vsx/BRZ11A/pj9v+CVcOtLiAv5dRST26731bNLuktL2aqSqDTTASNVCJOZUcKpLKZ8uSF0338Coxq/4z1dnI1jZ4qXnXAJanK9acR++iMOO46jG+MrctzcVZBVUP2g7aJpfKIj3dW9mYKz+gt/ifzKY4x5d7nV1Z8crtazPMs1MhkLt/Sh9MSTcSg2aDziGHtIKo5/zq/qUIrh5cMACflCA2EKdpBxy4gBZhEtHDWaHXrYpowVVjjvteQe6OK6VlykNP885AB6nctr+wuxTf3aHiznsoDw8Y+ZPBqnFaocGIu8f2AIKgxVFRD99wLAUlffzpUPfZTshHcYbIPwdNIvLXPz4BH/dIlNDAIY7s49wGhyygQHJ3yeNN5WB9gD1YupqRJe3FJuqc2US4PTUU/Ai0Zz8B9RMlpuYsKj3gjhuuqGNkIk88IHlQFSakZNHqslfy2a9urTusEtuZ/R9fg0DtKflnuz7wnVYUMiRbltGKVvdKUSzSWcDhlqZ4AFnaHXXS6fTa/2wAz3KvmJzpYkBIMcGYqPFKQd0BMeMhNnAWs7rXfyeyvRJo/PfW+m3or71embdEwA1qfTjfGTTHot758fgxgyisLsuO2ATvdBsQ0Tr11qoFOpV2TAE+et4gsv51LXS63wDPo+IqzxLyfiywahPJpLWgvK+iPuctfxPE3NPMw6fblCbXo/R4YsB96ulGM+w46yLZ2ZtugM3N3fVs287stgL/NiIffOl3z98jD1F7MOM+CzICMk7CuerVWswFOy6wyHTTzMVScnAquHYTAsy3CD2QXfVqIhZqyhstgTpR6q1dd6K62IjRe47qw00ZYT0Bu8JMTITtICFs3gnG3MN3P5ZYT76kT8+/IM9MOfsmBI/U8/LH/1XxuSFEue//NCkxjxfgJDs2jnpXu3qbUFFJ8pKY3tAq6sAG+N5pRoMs7EXYjjzX6iPabmmpUOg1YL+29eqjxdEPLw7hJGp1WlQUycmAWupLjzthKeMADyiBEzabPrfkDpEJ1nOqtC6TD3bgS4OgsjwiI9ezIn45jbaP2uRxAUSdRWLI1HVRszQY8dLIS5PYEQOF1Jpi5SVo0o5lEcuXK4j4CH1j1A/HP62VW5kgKYtAuM7/Y0KR/01t78nv0lp0ma6XH+ZFcBC7P6Xs7amcFj/vQI5uxy8yB70a9Jzrrzs7k11G76je8weQrUd5/dgY+Bwgs5DdVfMuLzzEiIBTysDijbvMJwVW/GuejeOgnz4L27YZg1F85W6bnb4yXw8LXITqP2jK7xYJOVar35e/lor5gJgbz/rec/3lOs223vHyeZgwFddSWmd6ioNEg6E1AszolfkauEHX8pXXIwMmb5Qp0SdO0Qo4KKWdnoTQdkeJIzmSIXUhVc9q1maP6ZLMAlWzxrFpyQAuzLNB5z25zlaPUB04H8JGYO8GkNqz7kmam0KSmsHnKkNcBYOvDk9BAwWw6V219QS7KokrYSftqkvXUI8wce9RZmTsdk1FkRjxKSFUKxDz/0oDBRj7WU5nlcY8iKjkWt+Fci42t5TJ94 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 38b213af-e413-4245-59fa-08db3aabdc86 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:22.1169 (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: C8150NmdeVAZ6bOKJGi9yMfxbxF+XgxWtFwtPsfcMFZ2sw1wR71g+/hXnp6Yep/3 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 57559f60ca6486..a6bc090d4b38a9 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -211,7 +211,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; } } @@ -262,7 +262,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); } @@ -647,7 +647,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), @@ -802,7 +802,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); @@ -851,7 +851,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])); @@ -999,6 +999,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; }; @@ -1021,7 +1022,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; @@ -1215,7 +1216,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, @@ -1424,7 +1425,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 96fb2f04623f23..8ab20df4edc8c7 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -315,7 +315,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, @@ -326,7 +326,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; } @@ -350,12 +350,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, @@ -369,9 +370,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; } @@ -528,7 +530,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, @@ -603,7 +605,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 8b11bb70095269..62e01412a7679e 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -41,7 +41,7 @@ static unsigned long PAGE_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), @@ -59,14 +59,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 Tue Apr 11 16:43:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13207881 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 1BC51C77B74 for ; Tue, 11 Apr 2023 16:43:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229909AbjDKQnf (ORCPT ); Tue, 11 Apr 2023 12:43:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229949AbjDKQnc (ORCPT ); Tue, 11 Apr 2023 12:43:32 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEF875588; Tue, 11 Apr 2023 09:43:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SoPHg7di7xsWwcFsP6SpQsA2Nnj7yV7qqcHwe9+CMx//+B4U+Sy372RgMvP2FzAuJXJuMq/7enWnb1H2IGvV+X6wONne+F4oll4ufqY13r40JAl6UhIncPGCrnT8Aoyb8QsmLm2VGpT3EsoACS9om3teSmQtX3Uowyz8RyLMC0TBk6olzcL8SV4J8bvHyvUZzuyEzohWLX1iJDfRxfciL/qBFUGaZVzD7v03PyDCeIhboOTGFx+KBC8nbG0QIz1xrw3y9+kkbMKhLtnX2Sligw/vAymAGmNRNbNSbVGVT+IMiztBijvm78sVMcEvxJUGX26UYVZdy+ekRSj0Y+VfOw== 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=uD34Oq8oTy+sonXeAwv4BDmlwZH/r68vKzO3PLkSKu0=; b=jN+ajVNO+20b3IcSUOzwmreuMVIUsWJ36ylRH6ppKUXpeNh3pN4yKGcZGLQbRR/JgwMIJJ3wwfM9bPD2Uhig6LSyfmdElIy12RYy1ATJlZhzAGF8YFaQf/9QxE2zmO6LpwXsGVUt6dXmm8vduFcU8g5vdmz35pSBzrk3StqBEKZSEDg/ggGlEAEj7Bz397+GJWfkaH7qiFWAVEkby4tEqAMrs28lP9EMBO+TWLCvF5fABubZ4DXkvMCXtt6cD4Mvs10eyIX869qk7IsRIrnHvD+eJdgtiuvLklduFNsr2qpwQgvg1fjkWhMDll4lV0byyjl8u/80MrYM1L0D5xpdaQ== 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=uD34Oq8oTy+sonXeAwv4BDmlwZH/r68vKzO3PLkSKu0=; b=XdPJDvuh14kdbPe3Xl1j1SKzoOYm8FbGrd6CG38ZO32forLTjUGWBXoL4LGnjoigY+Hn91X5GQnhnAGVqy02Bn/6ibhkWQRjG4CHmnW5ntRP0Bj/NBLMIw8FWGwxxbhAk9nXhng7kgMQtMWiGEoJAYMVDbPzg93Whq0OsFhyC9Cs5Cba0WVVTh1yk+sI/P+NUnkgj0PmLX938uQuT2NzuZD6NxaY0NFNSgQeQqP2TzQJqNADRJyhok3kbcT75wkT0/rF6d38qpZVQUAbuiPES/YHoM4rYUn1DlzRfZ1CUTsDgAJy20puZ7CZcCJS9I/pWsFbXQ7/Qf2MKhtETW4gsA== 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 BL3PR12MB6596.namprd12.prod.outlook.com (2603:10b6:208:38f::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6277.36; Tue, 11 Apr 2023 16:43:22 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::6045:ad97:10b7:62a2%9]) with mapi id 15.20.6277.038; Tue, 11 Apr 2023 16:43:22 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Nicolin Chen , Yi Liu Subject: [PATCH v5 17/17] iommufd/selftest: Add a selftest for IOMMU_HWPT_ALLOC Date: Tue, 11 Apr 2023 13:43:18 -0300 Message-Id: <17-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v5-6716da355392+c5-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BYAPR07CA0040.namprd07.prod.outlook.com (2603:10b6:a03:60::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_|BL3PR12MB6596:EE_ X-MS-Office365-Filtering-Correlation-Id: 4b102766-c51a-4bbd-bc14-08db3aabdc88 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ea0BkqGZw6jm7O2y7C7J8v1yL3oBdUBIwaAzl7ToRN9rAWqOBVFrus7aCSXLt9qZumV10jNk0VSP1nMsXG9orBLYqCzCWdAKkQNEWo306Yceq8yLSRklGvY3BB+IchpwGGK5A52BjIzJRQlyLWjWVawCqrQ3f65OJnfyxw4KEhrPhTLcAY/oS/8+MFXjAPn97daocY4usIMI1LMwrsNPngcgeu6jm0KOfsN+qflN99kwmevwcg5l17eD9aXMQzp6XfqXgon7vbk+KMQtm6+tCzESt3G7/wM+IXZRtqafxaMGGeVs6vid4kUC6/1roUFivQEC4ldCUgyih/c1asASrcZsu+Edg0XWhmfDjlqRITtj0AoOBIcGmh7UcwXeaESapdN0I4TBV9BfsqemiomQAPBEbO0CPXCcn+7pm8sjSDFoW8EGHRUBqlAfLrn9Q7IctY521FFRBUXtpUXsT39tRtYPdnz19EqPfF5vyRGPJSglSuMIOnXWAro4aEd0upPnCUGuxUVA7LQpb1AkJP7YNoro5fagw36JieOunHJjFAs= 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)(39860400002)(346002)(136003)(396003)(366004)(376002)(451199021)(2906002)(86362001)(36756003)(6486002)(6666004)(2616005)(83380400001)(186003)(6506007)(26005)(6512007)(478600001)(66556008)(66476007)(66946007)(4326008)(316002)(8936002)(41300700001)(38100700002)(8676002)(5660300002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9YdfXIcZMIcy3n9gDyE11s2AG7P/03uizc5rYeEi0BPtMAyhLT4OWybgaOen6XNI3l3IPxFaz+9MWFvGY2Tys51trUI3YYO1mjUJ+ClY0l0K4ae8OvunR6wZaq+aqMsqfgvea/ROIEJoIjeFFb/6JO1CNAqbcZsgd3gRzLm/4XB/8lSSIcEeTleGEjbnfxGBYT9tWbl5g0V6n6cxS9kiTnQpRFawBVjpi7vY6ox/fzMPtkbsaMEu45E5IjAqV1bLBWgEOCeYPNm1smwI9Zex0jva/YXg1Mn2GmNJeodX7O+/9djjfJO6/VLJlaTq598V6SYew2hiMzLLQf/9QZFxGiTybzoPR19OOLpk1iqN7NMLuxqF10zos32QmPw57OoG8E5mVKxtYiVBilCHogCAL8UnhC11Iw6yVBTKDaaDRhCppgqV91ZGM02DUGPHCUGwofWn3K5ITW+y7FkbJyyQ3DEntVywa4254NQEXKezcJCrEsy6pt2LnyTbZ4hAsHFV8Lx2DJAQzyPiMshG8ziGk6rI5C145gIsnpEP4zzv21o6EjiKYanLntmoIHC8ANrYhevBKSzJJkZQfUqzIjfMU/I8ERa6CJXHOcz6i9NjeYYnNafRtas+Wu/5dTc9LT1Li124bGS3VYoJUmJpk04xtjTpezMaKet1eXp7WI5eDm/I2XzNGNtyBaq1opnxDoo1diewpG02kU7JQLr4gvGnhqLh3lEvTi1I9maH49MbPmmJUGzXs5wJ5E3AbFHXEb5+Sm7sFDJXN9ZzfELxA+X2dnOZbma4j8X745gyrUkrJO6eYZKtpkWBH0nXfWYRnqXRPSQojMhTS7MiiKfgR3i5bW9cKPorGwOh/jsk0t7KaaGRIIkvDIUCPUQSOVQXYvbChK4qtur87nXYsqstfBmD6kdq4RVMlva1SehF/pL+8yHdoekGQmvMY9BRR85sIVjYRgknArWkoEAcDBYmu16P1xq1+XUWNhbnvASFpca6KVuVyw078NfLmlY+4W7owsfTKGo2qfVNlr4yRWs8HSALzuBWZ0PEeqGj8GvEShSUQrJSo9XW9A5hmWSAfrGwnb6E3KOXZBV8kFL4Je0gVd9zhS6EATQhGaYOv7N1nKGoG9KsxUyoSq02MLS4VKFkjcXoz3e6MycA2MmQnIsqJqTuJ0VrvAxL6seWCCBPam90/WvAV9ZGd8WgPjeq8q1/9IHB72W0qP8IfWx04OuGDRkwUHorVMqQmQ+K37yQ083n/B3ZQWFUpb5Zs76bhdLOlLY7gbGDctOcjjJzbyWdqhCYblWtI1BYE/0o8A3sqsmX1xzCxPNDxbtQ0FLHN90otAsIslRzdmFHcoo/F9+Jh36olnPe2aIGIqVETJesmmq7aZHd4eKyd7kTt+ueA/rSKE+vwNKUYBsdmvA0xfK6xjO63pGkQPYkgpDxOkIEmxZ/1FAwiiiEU9S/7xnC96kJgTKqBT8gapXZsVpvr86ZUVIFSrjZa4U16Hz5EZk2xIuUeP7KxEowe7KQ1DMo1MAFwh+c+xJ3ccY27J6azGnqm0Ix+C35KzkUE0l9jqtE69glA8QwSwJyp8STME1n+Mx4ELNk X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4b102766-c51a-4bbd-bc14-08db3aabdc88 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 16:43:21.8334 (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: HT1v7AriNyvH+I0dUUUi/pTI6BHccBhaXwkbBhteR4OgJv70lFnSvHg5Imls5EAe X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL3PR12MB6596 Precedence: bulk List-ID: X-Mailing-List: kvm@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 a6bc090d4b38a9..8b2c18ac6a2864 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1304,6 +1304,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 8ab20df4edc8c7..d4c552e5694812 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -579,6 +579,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; __u64 iova; self->fd = open("/dev/iommu", O_RDWR); @@ -605,11 +607,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 62e01412a7679e..53b4d3f2d9fc6c 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -98,6 +98,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) {