From patchwork Thu Apr 20 18:51: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: 13219108 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 ABAFCC77B73 for ; Thu, 20 Apr 2023 18:53:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231491AbjDTSw5 (ORCPT ); Thu, 20 Apr 2023 14:52:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57556 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232036AbjDTSwm (ORCPT ); Thu, 20 Apr 2023 14:52:42 -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 74D3244BA; Thu, 20 Apr 2023 11:52:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DQa9c+OY1jJHMf3qRpoUkegQAJuUhkHNqrBI0pFyudkcQUcli12DyW9Yr717riwsKnmK46fAhJQuwokZfRUW28qxWah3DlbePPkaBZ7bwDBXe9w+clF5aZT/Fo+AbcVuemWfStyE4iLbQZf+d8Noacy4MPCJlMDcMvY5mRKRR9Pht49IN8CfMkuduaY6cUe/gR3Y1kZOmo6X5JbVtS+/cVcFIafnv6JickDvH3rAE+4kGdTcvdYYxtqrNar/eFMvdCHMKDLiGwlYx9v9iJ3wUgFTaLuWuU5cphGTIkKy6KzKxtuNUfjNk2F+qjDhlkKIyQmR5iJPo9DpyYmgh5HngQ== 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=HftrH7S+VAVOKLIlNQUSU3CyFP8OTnydiJHBxPGzP3SqMYNcupC7yeabLQWa8P4nJNox+2xPjOrCktItRwuqrdMZY6nTiAR4KxPaf/R3n1rSeQWmdzjGZ18K8IMJ81w0XYg56vTeGhM0BDIfKgzp7ijPIhtLYZtr6jbHmKppAeOIgMAwLOoKPkGtuBM+gtaxcfV9gtMWeq/Zw6+claJIEuPqw5zBNHt7hrC3t1R5DRci1TUEjo1ysGowdO7poA32uqRXVnYONJ2J7X9wxvyMSoQMDiO4XhmfSrJMwRehSjfPmzDNMRtCtvTKwokxW7nH+OLGn+YR2sozVXM4yoZNbw== 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=BAY3YUFVT33oOZ47Bn49fiHEdhUWf5BNG/P1k0+ihKtPBEk5FxfF41LF6iBgyRsNtW/oos/Ex7daXxUfJmmqdggEa3jEEMbFjZZBQ1F1PS0F+cSzWK1F68by0Wt+alZJBmbi4FsSIxB5aGo7VRM1q8cLdCR0lvX3+BznJkNnvMyzzmQBc4b9rW1ENsbae/NNzPap96bhh13CxNX+KduV6dFLz1OXO9fpadY75BA7yCyEPb9KuFOYhWWiTrjM50/+iv065Aa3K5TIQUj2DplrynbsUO/mJardhXkYRNwD2RHXbFljVPvlyg9oIfaW6c1hDqChzZXuGBsNfN8ELwdkXQ== 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 MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6319.22; Thu, 20 Apr 2023 18:51:50 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f7a7:a561:87e9:5fab%6]) with mapi id 15.20.6319.020; Thu, 20 Apr 2023 18:51:50 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org Cc: Lu Baolu , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu Subject: [PATCH v6 01/19] iommufd: Move isolated msi enforcement to iommufd_device_bind() Date: Thu, 20 Apr 2023 15:51:16 -0300 Message-Id: <1-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> In-Reply-To: <0-v6-fdb604df649a+369-iommufd_alloc_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR03CA0013.namprd03.prod.outlook.com (2603:10b6:208:2d::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_|MW3PR12MB4553:EE_ X-MS-Office365-Filtering-Correlation-Id: 3be85793-1341-4cdb-90f0-08db41d04738 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8znuAU2rc5bnucutZqWqoEvHStBSWRmEb5KDLypr0rsjVwHSJoekGUqtv3SVbqEK1S/kv/Eoq2E3RLypdJ8cs3egi3fcJtfeofHN28OuLZ8dv8yqlG51HEqXWYWL33udbTCHrp1Tk683qYAjlDKWu/ktWo+nkt3rZ6OmboQ7sXEsX2b/iGNuJ74mWwTowqvulUJ/zQv8IXhfkrgRpOXMZjvCytcJET3vH968JVUvhqnjlHSSOBcs0859LAetEBuUzh9NhsLtXkstNw/6rdmFyOP21Wx7AkUhd+FlsOZ1FvfsgQKXcSM5rKjfzrHsLcki0i4RKiQtzikrEISKJkMwte+m5px8pl5sdDCQKmvXPJjayESpUW1a1+F2lTCvBeyLVAuogpNfMhg9OKDLQLwwCy9hPtk2a/FhYwC1RTp+RZDpVCgbUdQYNFnMejPWwLZY597yZ5tFcjJOpA9fYL1y8mEN7zZpHjm2m3s7KtXjvQAHywU/+XE7aRix1qvuHXInaNAVJv2pCoWWzQ9wuHgo+RO0DHRoVu/JPWn+keSKN527VvxI9l1BC5WYhIvnOh2xR2dYf4bmkkL2ULbE5qw3UH8FsiyJtKB4bYeRezFntVI= 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)(366004)(396003)(39860400002)(346002)(376002)(136003)(451199021)(54906003)(4326008)(316002)(66946007)(66556008)(66476007)(478600001)(6666004)(6486002)(5660300002)(8936002)(41300700001)(8676002)(2906002)(86362001)(36756003)(38100700002)(2616005)(6512007)(186003)(6506007)(26005)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: f6SyglhUzw/YpbAEJKQgALHl+Xls1XI3RuPCgAw1bXFqUQ5lnx6PkTidQXo9U4YYsbzBfFaL6vayNm4UIz0TLjRhsdeH8NnJOcf/ikLbfb/aU/apa/oNve+M2k2uy9S0SGIxtR9Gh0xF44p77j04Wp4JB7bZE4nYSuQ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qolX22cXHnU4urnnEqyObVAoBKDvvB81go1n/7Jrh2B0tzid+CPplPbw6Sc3bBQLiULYJWaZjmUVOH9Z6lVBLz3zJJFMhoQtl/2CEmgLYU3Mu3WLEkbgGMSDYITkUSU8xsq2pdfKkOj34tJC8WnHZsw/1ZrqaYG32GqsyE6Q27ZMkAwhvaF9404FVqLh1kDE8UOdi51ZTdQ59fgptzk9cV1yrvXWRnNGR14IVR+d4SBMzGMxaBBHFFLokj/vCRUOtM4iJwwu1Tho1ygAGzhTGX2diUnxzm6Pe7Xz4NXnTp4oN42yCGHckFt3CUITcz79/ZA6zJRSrlg0BRJ+UOyzoQso8/X35g X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3be85793-1341-4cdb-90f0-08db41d04738 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Apr 2023 18:51:41.4215 (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: vivszOU8MAxWsM/17eN2qB+HOGDMujFh111hLLakT/gngLE+EcUmnISdHEyMH9Tq X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4553 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org With the recent rework this no longer needs to be done at domain attachment time, we know if the device is usable by iommufd when we bind it. The value of msi_device_has_isolated_msi() is not allowed to change while a driver is bound. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 38 ++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index c6f4852a8a0c08..e66303c17c894c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -60,6 +60,26 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, if (!group) return ERR_PTR(-ENODEV); + /* + * For historical compat with VFIO the insecure interrupt path is + * allowed if the module parameter is set. Secure/Isolated means that a + * MemWr operation from the device (eg a simple DMA) cannot trigger an + * interrupt outside this iommufd context. + */ + if (!iommufd_selftest_is_mock_dev(dev) && + !iommu_group_has_isolated_msi(group)) { + if (!allow_unsafe_interrupts) { + rc = -EPERM; + goto out_group_put; + } + + dev_warn( + dev, + "MSI interrupts are not secure, they cannot be isolated by the platform. " + "Check that platform features like interrupt remapping are enabled. " + "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); + } + rc = iommu_device_claim_dma_owner(dev, ictx); if (rc) goto out_group_put; @@ -146,24 +166,6 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, */ hwpt->msi_cookie = true; } - - /* - * For historical compat with VFIO the insecure interrupt path is - * allowed if the module parameter is set. Insecure means that a MemWr - * operation from the device (eg a simple DMA) cannot trigger an - * interrupt outside this iommufd context. - */ - if (!iommufd_selftest_is_mock_dev(idev->dev) && - !iommu_group_has_isolated_msi(idev->group)) { - if (!allow_unsafe_interrupts) - return -EPERM; - - dev_warn( - idev->dev, - "MSI interrupts are not secure, they cannot be isolated by the platform. " - "Check that platform features like interrupt remapping are enabled. " - "Use the \"allow_unsafe_interrupts\" module parameter to override\n"); - } return 0; }