From patchwork Wed Feb 22 21:02:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13149575 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 A0A57C61DA4 for ; Wed, 22 Feb 2023 21:02:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229461AbjBVVCt (ORCPT ); Wed, 22 Feb 2023 16:02:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229502AbjBVVCs (ORCPT ); Wed, 22 Feb 2023 16:02:48 -0500 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2046.outbound.protection.outlook.com [40.107.101.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 571002A15A for ; Wed, 22 Feb 2023 13:02:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QI3C7tzAjvI9JciDAQC9B+xAgCF5SJfW13XkFJKyBmGsrYprz0IHCRvkrKuNeZGdrj6lIyKI6E1CTymTZ9JDUvrPADq9hDG3psJuBw5k4pvJYC3B/B3jvflbGEPaOi17UQQlkQ+D403yp6+nMl5e18UoRoxCS/IJEHa4kDSbe2TSTR5UGOM6N0qyoCszLePKwHZl/eMFKPNYLHr/4x7b8pyHf3+MTkc7kNX7LGh2ArWp4DKJ/GbtZglIQZLYzGGJyr6HFgvX6Qi3fAsq7zh6/JRIqDeuKtZANTdGgDoJFc6FklGOXP5gGajWHpspQnis/iPRBYtWrLOUf8jXlegsOA== 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=Nu5yBrrs6yqv2fqZNLDU92PUUel9kRO9VoOiR8+0+DQ=; b=OXveeQdhSUaYmfL26945ozzBHubG9pdSwQqBg3ArirBVtZxPzlNrTluDADWJbpbVdEwsVCtVsbNDd6rYLNsd+PVI/MIWfVMcYoPmD+jni2l8N7FYOtlxacKFMco2Qv1pn2oF8rSrkN0VQE+dn2DFFeDg/BMVLYd/j1B7ow2g4EhSar2tr3vFcYhECWsTFQu7fI+TfT2rc1VWpMZDnnhvn/hore9m8klwSjQHJSNTZP0l+LOB70AHO58jVXFkEsHhkFUsPlfzk4O5U+kNh9RmSHZha8BYrr+OdGIju33rARvBAbom6p7zH9Cjvh39u/x0FVuxj42AcJhUXM4l45+Z4Q== 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=Nu5yBrrs6yqv2fqZNLDU92PUUel9kRO9VoOiR8+0+DQ=; b=SHlA4CAk9Svc52yYWsS88iwHwc43DgBBdPgenMyUvHQsHMN9sY8oZO14aD+Vr+m29f8NxgjQJ0SyVOwCRb7AyUxIj3jsRE/iPdCGwlocpuzeVUQQzabRZrKGCndFemmxfHwvzP0j8W1VLugrKQ7dIkDiRi1moHFWi5BOLZVr6vAYgT9MTUekV/wZmYj5098gFt8iXbQoFeGwdf9cAi72Y/vy0a/rVIHbVvb9TKcQTvaoamSZuA9izxP5AMsHRb3fB60HdljuxCZjGm/6X/FZaQOiphbnSXFcOOd3EvsXcNp0dA61A6Jo3ty+mNLSuh6TxmrUy9J8qu8JVY9xla/DPQ== 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 DS0PR12MB8248.namprd12.prod.outlook.com (2603:10b6:8:f3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 21:02:42 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6111.021; Wed, 22 Feb 2023 21:02:42 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org, Shuah Khan Cc: Nicolin Chen , Yi Liu Subject: [PATCH v2 1/7] iommufd: Assert devices_lock for iommufd_hw_pagetable_has_group() Date: Wed, 22 Feb 2023 17:02:34 -0400 Message-Id: <1-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> In-Reply-To: <0-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR10CA0020.namprd10.prod.outlook.com (2603:10b6:208:120::33) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS0PR12MB8248:EE_ X-MS-Office365-Filtering-Correlation-Id: 090735b9-ec44-47dd-0ffc-08db1518233b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: K+/GeCYHxusnkdFqgxtQtF/BSnZcwyaAj8BCaoc353K4y6R+Fd237KhTT3rn6nWNq56ukga2ZBovXPB/FHSzRbViTNSoSceeLo4DOzQVID+lFke7/le+ld8+rV8tl/WGIjntYNqxZx87iL1m3BA/ZS4sQdYIaVO8CPPT5JvXv5mivzDdqWdXoQCa9FNbmHD0SkAIg6ftMAxNP26hcsyJyMqJBdBTBLdhXm+swdsc6kGSASI+T3kC1YxsG6sUG0JNUcJnlJl1U+mat24T2aGP/OSNlnbSmO6jpcvIWAmu020TYihnf5l5GgAuQrjEIY7jVMn0UxAYiV3PNogni9zGU/Z/DtqaDWeTNtrh4BwThRYxyA8aZ3MtZcUhkBOjt4e/iSUbDd9W4XvcgTfu5jFWdiWFTutCRUNqqkEN6UfCz1C0eZI+Kaj5zIDyP699dXJ/pK/TDNxpztdD8wUISLoQO5OBWFKmzvO+7z7qkn5O51DWI1SHy1wYzDCPu5u/dRxFvtKP9ps5bOEA6TG9197M4VaUaeo9E+VL0oj1hI7S63W+886HFkDRlMsEnRyxPjZqK5aTtZMRBv7nlo+vq4sk60IRxkvarXk9VBgbNW8eijyevA2dZTMzNYuXVZ+qek4/j7gdV8e7/79NxL63H8VXpqbTMjCnZxKLQcbNfnlUWwJWp6w4Ms3CUh/MIYoMGo4D X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(39860400002)(346002)(451199018)(86362001)(316002)(5660300002)(36756003)(66556008)(8676002)(6512007)(66476007)(26005)(186003)(4326008)(8936002)(2616005)(41300700001)(54906003)(66946007)(478600001)(6666004)(6506007)(110136005)(38100700002)(4744005)(83380400001)(2906002)(6486002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: XFbjRaZKiwwIeTRquObL6kBcJ8UW0VKMSHRhr8CipGIMeiZvUds1wvJ26g64shEI7j3HEJfAj/91Y5QwsmQ4vMlOJr3DgXFzMgyNGBo2r+ElrsogzfjK45veyG6sqAB/OP2oR5g9G/Sv5Kcten8Wmuf73slNc4StWsJLdE15hHWhLJt0qKEDPS5AhDN4VLsx9p1pOKKxQu6nAk90sUX62IfJ7SYXwmXu6xgN4mpcIwCqBbIMS2AXb2y7X99QWxwjPORBT0MZDuEIr9Msjg6vbHvoMrFowF8qArQPWR6fagipce6uNHoRzNKilIKlcbIYzdkmSh6AcEAirbJgcv2PMJhMinID+Wh7f+MTEpD37t+a2CQyO3rxADXVwAg9mOteCLp2IfTivYVw9b64wdjRZQZItVTPWfSK1xOYDZIH+3pP7fVn1a+8u69EjcoOZ3FpTdTFv56aLERxK1b3+iz9yQPqqjFtGbQvAx+Bf8QR1WfXJfzXDSoZZkXP9cbv/EHKjJa5ohXkKzWOkT0p4AJ6aIkMy45xuomrTuj/jAD3jod23DkCwV4D6hX2Zmuf4XTgLz+4SD7FT7yxJoUe1p0IL+D41bBB59PXQmrrAO7z4jQau8I2tYvbu7huO6aJGEgbvrhSSDc1nrRceZfw6NodpRVSdPhIkGaQ5uwEK/QJYm30/lfJStUGAhTunvPul4mIp03O8Gowqx7I3yF9eEXYJSqX1/cU2/1i5MULNYANxpRm9Bt6HBu1cc5N9m1Z0OKcpY9PSsS9C9OtrJ7lzvRF8hF4rCZ4fg2r1lIp3ffDSLnmniJu3KD1lN1fXgq6m5fPfQT4GL4fq+wdqtRPaRqPUP7Gto7BhI/EvhOqxOiyuG8T+woU8XGDjNnefmHPqbSR5mxe8gMLNi4Cvts9gZE3lTomMtajfNHd3cuVekz3B8b4SP2v0WlKBKqtsl8GN4Wbzc0RkyPbgi9qYALWrtNso2ZYTJKAHCHDgTyV8SnwDBMWwocaTEvwV8iROoJ/MSaeF0Sin6h8/PFDXLeKNe0CHi1kNPOZ/rN09x9MiXqmpu3AXp54yLPSFjtJs/8u1S3wfmiBmdm42QAsvXZrYwGnqVFsW4FNWm5QgIMwLrYhqNhKlkzOfJ/tGEtN9z3KlKF/vFzZkga65MDXj+PUCVwmnRUeizfPm2aRtqtU9LP2+1/9SB4tTXMObpi8UZaZwmLWXP+clFMKYEJGuFYKSMjSXFwYjmN8Q535lhq2yq0cbK8RU5UBGREFtkUCVSF1/fmspLr+JKo7DD2nZz8mOXgxNBBFKBzPhKI5isHiQN8l+I7DtptNXLg/dAfkhIa2bhmjROkCsd7WLZis3nG1ZxaAsiFib2jJ/TLCjXNWMUopQZUurzGI8ZWSTbhIheLN5ahnC7g7qkKblpfOckCsbJVQ4O5AJJIoRJpC7Czk7oPjcCV7Fq+d/2/QHvnl4DDspjkng7uuT34WU5YGid9mqkgVDcBrCGRby+Fm2xmbHkJPwqrAwHN7clzCvx6BNbt8egL4JV4ewu7xrnQmxvR74dVrYdM/9Do2QJfVWRaZvG5PTQBmzY3UmtIN8pY8x6eeZezz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 090735b9-ec44-47dd-0ffc-08db1518233b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 21:02:41.9559 (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: rxU2lpBEavxk3yOQIy96a2eJAVy64h0n9XE5eFZ8ZcOYc9/aUGDwi18TtXGcR3jj X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8248 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The hwpt->devices list is locked by this, make it clearer. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index a0c66f47a65ada..dcfaf6567420e0 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -186,6 +186,8 @@ static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, { 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) return true; From patchwork Wed Feb 22 21:02:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13149574 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 22E57C636D6 for ; Wed, 22 Feb 2023 21:02:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229755AbjBVVCs (ORCPT ); Wed, 22 Feb 2023 16:02:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229461AbjBVVCr (ORCPT ); Wed, 22 Feb 2023 16:02:47 -0500 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2046.outbound.protection.outlook.com [40.107.101.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0831E244A1 for ; Wed, 22 Feb 2023 13:02:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K3xhv3GAsDEkTV3F65xMIGc9bu5vX0bhkRM1SfYugDM12u4JemqZU3QYtY8Pl0B5j7WuraTdbdcgdthS803Zi2//IffmIW6MQbK/ipG87XShiZzZmOX5U36T+2rjxxmXxXOq19QRy+rBLoNIMjxqxIYCxmmYtM3zkNXuLffnwilSxCUcD2oyXMlVAiURIQ52hxPure8VoieyuVC60z756yylQUsx3xerYKi84XZB/wY4szf7kpX7ig9J3jbfl9LLpbxZSaquEyNRdhbOUcpo0iA/7YCV/eHpMPP8QzDoKFmcZv7/u9XleP+rK/ybyc02PTrz9W6h0FR2jSvgmU7KEw== 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=5xLbaPPoXrC5BHkxxXBQcdeotQMHGm+J17RJ6BKFOpk=; b=j0zqRI5qwwI63/MT5lnRdtRqFbSG7RgmQITLEicwbF3nB2xAbSbjY65CUZwqqthvXjba53JnzJJMy6II8Kr4837/HyyieGz/vaB0Dw8B9WTsn1yHEphCUOtLhChwNJye+dzct6l5LKeptaeiQVVFeBx0m/9YAoScDkIv7PWKMgoo4mKERoJQHie73bN/JpMoEMa6vSdzE76mdH+B1Rl0S09JoBESIXL9aSuoJFKmjx6P18mPsjxOzXrMK6A8r/XILQess1vei+bmz98OY6T0hGtkUn6F7Ype9/wypI+4U41nR7d1LGQfVs+lhGSgICiCu12JbWD3a7QlcBMgqDS5sQ== 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=5xLbaPPoXrC5BHkxxXBQcdeotQMHGm+J17RJ6BKFOpk=; b=MvQDLoBnPtb0o7FQxj06UbZCAUFYoblWnewkjjVhaeffZvm80kzZK15uFGHQxQ7zXDubb2F7FW7MOIqaMOAAYW64fnYKN6GCFrEgByS+yju12Yde/0Ig4Z5BoN73j3m4EdPmdccwjfBzZbRGeM8/eTTBqJQWcSCShgwbvvQYMMBHKR8TI5kL7zZAKQb3NabW8zR7w1nifRSXVJa8K4JAEAPnVRto9mEapiIuijTFyA8Food0dVy7fsg+46kjxY/txoKNNVz5ndJunO6Z0wNKsup5RhEAvhpI7aK4wIVw9n7R6oukn64JsMuJqS71C9UwOBrEKw4nwPt6kPB+8qwm+A== 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 DS0PR12MB8248.namprd12.prod.outlook.com (2603:10b6:8:f3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 21:02:42 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6111.021; Wed, 22 Feb 2023 21:02:42 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org, Shuah Khan Cc: Nicolin Chen , Yi Liu Subject: [PATCH v2 2/7] iommufd: Add iommufd_lock_obj() around the auto-domains hwpts Date: Wed, 22 Feb 2023 17:02:35 -0400 Message-Id: <2-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> In-Reply-To: <0-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR10CA0011.namprd10.prod.outlook.com (2603:10b6:208:120::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_|DS0PR12MB8248:EE_ X-MS-Office365-Filtering-Correlation-Id: 3589ed71-8a98-4d7f-33c4-08db15182361 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: S53LXgIWMZ8d9MPX856DkA3ae0oDOaoPCr1HoQ+s1YuwDDCpjcoKmNXuh5Ced6NSDFrahvXFxCCSDq4PTvEM5/KIp97IjJczjOyRjU3+BwHITZrcrv45uDgHCfMMAX+pRZhQBGdk7DW6l68NTAwUtkKZBD90t8MhwFOb1ysf1QjjrT9XTlR5wS5uPjKw3dxDLbG07MA2Lox25A+Tja58uUxJV/MthrYCV+UYxfNkTlmRf2OSWzPp7yAGD4dhma3XgU7KehcOB8uX5j/BWfA29P/QlQUgxM/z+2BYlXHrhKfiSIUycp2jg1nUz+4zB7QVVJrfzlttSefEQU2k1vuM8pzTFGrLNS+I4Gg5ObeDUTLiNw9bVGakIw7pzpcOd2cKqQyuCEFn5hKEX1pGvGlG4XlnwMnR+2zSoVTSlHievIfCiHfwAV8k/DWDBkbD8LTnucF1RDPjbRDHfo8Yq4FeNbuxMGDxaA05lub8WVaic7GQwmyAIZGAy+yDpbY8RlsAslfpd2MVcUuv2SkFtlrOn1mS19pmqFQtlLCyJp1GcJ1uKTz5fTNiCuNpaVTm1j9VkV+mShfddGtUZCosBuUO53CWJ9sdkc+cqnE86khN/gUcDaCpnKoVuJUErV/z+uZ6kEdRHGCOukFiXtyFOVsZtjkrv0tdDzA6I68Kk91SkShPJfRBgmB7qo2eiO9ld9+zQlfnSFyNm/EEaJxzG/K+eA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(39860400002)(346002)(451199018)(86362001)(316002)(5660300002)(36756003)(66556008)(8676002)(6512007)(66476007)(26005)(186003)(4326008)(8936002)(2616005)(41300700001)(54906003)(66946007)(478600001)(6666004)(6506007)(110136005)(38100700002)(4744005)(83380400001)(2906002)(6486002)(4216001)(17423001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VLmiyL+CUBYgvARFy+4j3Org3P78oS+J06TeTsGUDHSGt/yem5hoRM/RGym2Z7CPjneCzq8j84Lhd+/46e+rju35ganV47ZcZmCcSUfMjL7bX8bO6QxCfsCi4gkF3/YeCIm1mDihLXaeAMGmSaPpUZNDMrFj0CPgO6kQLGMqOFFlYOBLXX+OLBST+gVM3HvWB42YJTqBv6/MrpGnlbrsKP0KaI6GSqndb5/6KPWmJIAJPUUHAE0N+74tTOui7pRS01rTGchKX4/YtsGMj52fsDhoTz5bKA1xtzkBBYwMlBZOSjmB0AkWs9u/s74JVxqpD6dCCpfbw2bA1DV39KGU67DS4wah9cVw9H5qeSd7AwK6JwKgXiJ3sd3vyET9BIq9LagijP8qejkPKquYpiscoCMXyQwOxN3p8/2MMCvhHwo5YIP3pmlOBM+ZM44z0AQhyNkDkP87vsxrL7H2in7+Gc911j06ATZFzA+hNgr2q7lPeLe15J82xF54ljJxaqq1yQ2ueUaOdULDAO2ToWrnDRs1H1ohbCgo40YsU/wr3GbSsmuEypttVtZHwU0PyQHjs/LlCOm4HidCsgMFtAoL6aiE2uZ1SklKkZ1WYbse5i7O8n6cWbxagA0DH49kgWvZP9HM5oJ1SPoVlBrcafziSSxYGKexsEnhdyGaQyFIY2mBsf6WdTdbcE74C2m1LttCCcKJB5sTMytCRtpXYziWYRUdkLwa/e9B14QL9WNu6IoLJkFELqcobJVn+vEKIBORoGvNNywG6DoB3jtclT0/TN0e3QggpcDnydG1UGJN1+zRTcqHg9g6EvUBPaooYYvI79LcmIwkHscmPbq7eqRbe3mlDBUfvKJaT5IkWbjaGiAnZ/qwQOikHnjCcMIxK65joQo1pffmrmaIlAD+qCPVe9aVNojvAWDdcG9KvSr/x/wQ6gZ4BDFJ3Ds7dhHklUFiGHlu57BRAFGXqpqC4H9FOR/y5/UmGzRPMzgXSv4C1l4o+diccBDLvNecglQ3OFxjYk1QhJ4VVtQ/no31OcbMlqoSEHAjEsjbyFD+MNXiYZ6OcCEF2MKqMHuwVu1I3BTjjqnWrMfqEiiJiQ4W00aTA4WrCCz+VDjp6hx7H4Woj7d0KVe0Ua7dbksq/lgQ5cSfUPHnOJ1b7hhHaEhh6Vc4A6IDV+uimdJmPMPiQFAQVR86S5Iioy7BkdZbBv4jIRuOiCbyPvw6j40E0cF4IVqikO6Uc5UXcWg99al0OGw1FRLa5u25cNGETuFUMtB4GSlVYVzZ3YxT0KHT3gqCIb6u9onh63Fe20cAV8Tmn2WoxBAv9yuawIUCIBjsd1gUubKMTnHfKttGkFv/FWutP69JleJELgzcWq+vxuDR98a/KeS6eGjEINUMKvNRGGHHqvoLzINEalLH8zO1jD4XnGT3Cu1c4M99whGzA7uCBHDW50T0hnyH+MFvxaBiimXDQrUVQIf1lnNZo+Ayh34HU+XT1VFNqZfmKp+HoD1K6ucRmQ2Izh8ordN5lwgWLTGje1LiiM8FM1Yil8gzmWAAD0armNDp/u/yEBe1pSRJW1j9qkFsNzUWo0xmHUsKVKx0JsqD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3589ed71-8a98-4d7f-33c4-08db15182361 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 21:02:42.1590 (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: Ns0LK5x1O4AHSXUQxCOMi15WSt20vOiuoqiJIscmiK8ZLUqACWr0cRv0OTk/6tcQ X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8248 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org A later patch will require this locking - currently under the ioas mutex the hwpt can not have a 0 reference and be on the list. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index dcfaf6567420e0..0a80ff7b2e0d79 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -282,7 +282,10 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, if (!hwpt->auto_domain) continue; + 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. From patchwork Wed Feb 22 21:02:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13149580 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 E0BF3C636D6 for ; Wed, 22 Feb 2023 21:02:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229844AbjBVVCz (ORCPT ); Wed, 22 Feb 2023 16:02:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231219AbjBVVCx (ORCPT ); Wed, 22 Feb 2023 16:02:53 -0500 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2073.outbound.protection.outlook.com [40.107.96.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BC982D70 for ; Wed, 22 Feb 2023 13:02:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BOkINtIWdZrxKIj3+YabFdxn8+ymqOha3hx/ggf1eeiMVoGW3NVAeu6RjoFmCgogi849jmL9b9A0Hs+zrESRdrB1A11ZfGh7MaLnUAOL4l+WzmzxtVICPVbf8cI5f7eyEavoJmft0xcdDvUhrEMnAihQQF2seEWIe8UnQgfKhPy03kEtVKijl9afcWSTztnTjdPb3nx45EBL0qwgLQimdclS4xSu9gVm6MW5uxtlBobD/JsjPtPuYVQU2/uq/OmT9L42vA9KMX8t4ZyGZ2DDYt4IzmaZy2tyZnshaMgJJ7fC+ieAiJ81FqPROsbIiO1gYJu3vEUg/YM7psfwGtjmLA== 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=ENMQXKEwZgh9Wzw4r/GXv6usySs+YiwlE90pmMNvfRk=; b=fTbMEqzQjZhmVxgstGGMjw65pj1ly00YZZ5ZCrWMIYEBEgqTEO597hyfC5Al7C8E4B2//9hkSFGH2NTcDyGcvua7I1tRA+1vQ/R7tlzPVRoIetUNqjivRUVNPXbPMYdqpc1xyZOpywZiUoVs+UqUvulbuLe4OpcyXtcy6elEQXrkBDH8TEpcFQ/nyLNQYP0qlNlajruKOZwzmTJq02kXhgI0DSVBRMVNtF7MytTTO2lgjAuilxAkyu6A14/TtY8deGUnVHpBfBMkjAVOW3m63wM0gV2O1Q3cGarHSU4FdBEBIE2P2XfMk+c+WDJaFzi5an7IIS04bpIh+rof7W/jug== 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=ENMQXKEwZgh9Wzw4r/GXv6usySs+YiwlE90pmMNvfRk=; b=RMoN8cH7vnE2RovQl5MiF7WdP1SvrDnDm51/z3UUwX01dflTvRixG1WAHEKtvru6Af9mKf7oZsL3v8M7Rh69ZvxZiplHAFkFjjZJk7TIlqrcpcCr9KRjOvnnGCGOVjd1Az/gKjS4qN28eKS5hKJ7iPfInWIcVNSh6x378bKpU+MQ91SVCVeY3lEGhdXJKplrFLFCrG+0GqKYoTK48OxD58nfO5aX3NnlefMl12rmrLLQ/LfOJG2mJcOj/ChxAhTJi9RwY+WgjGwOZwb3NOrZhJJwG1/bgHcaGlkbWfi3nzrkwusY64JpjfSakqDfH51DMy2d9me4kmHQ0uu+9Ytu9g== 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 DS0PR12MB8248.namprd12.prod.outlook.com (2603:10b6:8:f3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 21:02:44 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6111.021; Wed, 22 Feb 2023 21:02:44 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org, Shuah Khan Cc: Nicolin Chen , Yi Liu Subject: [PATCH v2 3/7] iommufd: Move ioas related HWPT destruction into iommufd_hw_pagetable_destroy() Date: Wed, 22 Feb 2023 17:02:36 -0400 Message-Id: <3-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> In-Reply-To: <0-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR20CA0012.namprd20.prod.outlook.com (2603:10b6:208:e8::25) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS0PR12MB8248:EE_ X-MS-Office365-Filtering-Correlation-Id: ab801a12-20b9-433b-3fe4-08db151823b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JQkAZ9U4YukQkWEwU4ZR1wuFRINXuE9C5jCYR52WEVE8zdvM5HMyOy0rVgYZxD47xvJknAxJds//qKXuOpspBiHD6LdFHex9NobaE+zCdvRYywg3AzHPRfKoDNNBO8kkpeiWAnsi8b+yvCbl29mHZNuW6PrULv7zwsq1ZDwcYxOtRTPqTNerfTmnltHA7thUZzw9TEPv+/+8ubNcodeaVCH+bAdT122NnaB9AgCRX4LKBJvEh2s5k5CUg6P+sQp9PTy5pj8xCF/+z8mmthSGo46yOCmBW09mUvLlzPff+99FAxEQEIgjjHxe0xtNOmp1TKO2QHQ6D3ZcYcpDsPZ6n0gVQVzz2e3aELA6yeF3bPyg7i1OoH5PVkkQnpIT5qeanDQjH1Rb0CXpXTrRSywkoEG0OT3G+6AN7VwjfbzqGmQXtfe8W/Lp/LmSd/s7WW6v7IIf9vuFKm0FspdbGr7nimiAgdUoz88nEHIvVrB+kGBZsClaTCVMQJExGKlqXdoZ4HG89jWaIFlan39mqRoIFAf4zoo6uLXbeS+tT/v81hHNPD39vx3uP3BP4+9iD/MBDozObnYredw2TenEbgSdsjPUU13gzw6yoftVIm9oYPX+0CxCXY3f9JkGbffJbRc0ZtD/8lecggaiSCnAxJSmx3JKKSpk8ek4MN5E4LyRShtQFKqxxdw0Ptnsn7XKNA0b X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(39860400002)(346002)(451199018)(86362001)(316002)(5660300002)(36756003)(66556008)(8676002)(6512007)(66476007)(26005)(186003)(4326008)(8936002)(2616005)(41300700001)(54906003)(66946007)(478600001)(6666004)(6506007)(110136005)(38100700002)(83380400001)(2906002)(6486002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LjBfX5kLtQRH2vgjeUfoGJv+rarQL63MWWH1+Wgq7PErPmeuvCAaK0DIGFhychCWdPanvkgaEFVHBd4fuGTK3CsFNEGYsqrfjHuT85t+qUb9u2NgoAVskZPkr6JStuyD5O7CyNIMSg6ticyqXe7d3C3tff5v9HwWlXuOlKJvnQdIr94o7tnDu2//CuyBOp5qCUhKlawgiqEYosZMRVuGQvL2vcboHOPlJGVKRIrQshvVzaOczITUjCzY61jPJDXZCTgBmFwC4qUqzxeH7MCBh5xvLf44RxRgx3N7HgzTLha4HReHdG8Gzw4tRAYoIlxpXU+DiFRhaK3gp4JOIKQWMOuV1/dl8/uaP5WoaxWbfY9vnC8zyax2Pk/jG3GRVdtqCi5oaWrgwOXyk1W58y4jjQEH5+q9vyGPVOyzcoynMhbW2EQ5WsyDFtRBGzyRcbGvRulvtt8KRTJAVKz0+PV6wzqWT1e0+Vau8W6kPeJ7J7Bb3HuMdCm4Qn213hPjejEmBAu0qlzNUQ1QTLD89tR7YLqj/mtUt96w1ViFOOZljKRHWwIaZ5iW8L5smVD3bDuiT9HsDJBrsQBNTJBowYkkuf9F3n9ogmKTjtncNGHV0RAjtw3mYu6X8rvjJ8zDo9A3GzWepcXQvN/oCBLVON73+fWHNd/J8UO9T8QLF8/E/Nn2Z68gNdGnOB/Kh7O7fIQ1Vd8n5OJK7Ra89qaozX0BAM2IaHaw4TW5jzuK41XUF+8jSQbYJUHei4zc2glSJxBOo8AfsjyvMd53N6DgPf28MXZPXtS30jpTZ/qY1i8XFDSjR6l6ckuPQQmaNb+oY8RpTyUZeyF3yNu4IcmzEFr7rLi1lI0ZKNv4+mGxQqAX/uPRmwqIhVKzW4qupj+LWiRbXHcqde3mRQhaFrLsJgx/c29CR72tCa3asILt0L5kyzYy0eMUVlckaG7ZjLWAyEg0i88jopbcGmseo6GOUaeLRBwIC3yRStFqs07xKMPWJtha0sGX8YTTx5lYoq573u+cNuZ3K56QXfNjAUNz3G98ga2hQxoM0fH1nJmnMHrYhZOL2hNAgHChb+1AEzRLn11JTsbMJ00HL5UDXzT24ixMl+egWeoNJ4oCP4ZXIEjq+zEXyWLIM42jnMccu87u6v3cuvk+VwjqxeuDCQ9EeVuslfDRwDtZmCXpFTo5RfG0Z4TMlpC2bf885yLfT06gOz5l2X4tRmEaxup9+uPpVsjb/sIUz+KDdTbKAFzL1IN65tdIg+tUJ3H8nibv1MzzdrYeNYDlPgb1speL4uWezrUSqB4lT/17R39WCavfRmy7KpdLXkLv4K6FehBpDJ2skcuhJqalboRrX2dRJObXRFlA4oHXwpaTX9C41xo4K3ajirDyJBmRWNq0PU9xL+yTwkBiiJf7lvnE1xjIWluzUgsmhQppKy08MORTY1Di/LKPhVZYedZL5UotpVCBINI1nhmFOyXjSGt/XhLGdxZwpg3Z8PuMeM5dYubRBrGLczo0+mKXe1MAjcDyban5mTLZb2khW5Yzq58rk5PzgQWH0hahYBYCyFjN9Xm+JTxBcKLegL9qy93UV37YBPATJMVVmIwv X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ab801a12-20b9-433b-3fe4-08db151823b2 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 21:02:42.7214 (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: 9MLnsSRMO9GYUNovWsAlMAYyRGzddTuomyLyX9iPku6X6CE0gP2/89CLYgbVBD1j X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8248 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org A HWPT is permanently associated with an IOAS when it is created, remove the strange situation where a refcount != 0 HWPT can have been disconnected from the IOAS by putting all the IOAS related destruction in the object destroy function. Initializing a HWPT is two stages, we have to allocate it, attach it to a device and then populate the domain. Once the domain is populated it is fully linked to the IOAS. Arrange things so that all the error unwinds flow through the iommufd_hw_pagetable_destroy() and allow it to handle all cases. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 20 +++++++------------- drivers/iommu/iommufd/hw_pagetable.c | 27 +++++++++++++++++++-------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 0a80ff7b2e0d79..6787a0d8d6e9f0 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -243,6 +243,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, hwpt->domain); if (rc) goto out_detach; + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); } } @@ -307,7 +308,6 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, rc = iommufd_device_do_attach(idev, hwpt); if (rc) goto out_abort; - list_add_tail(&hwpt->hwpt_item, &ioas->hwpt_list); mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); @@ -386,28 +386,19 @@ void iommufd_device_detach(struct iommufd_device *idev) { struct iommufd_hw_pagetable *hwpt = idev->hwpt; - mutex_lock(&hwpt->ioas->mutex); mutex_lock(&hwpt->devices_lock); list_del(&idev->devices_item); - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - if (list_empty(&hwpt->devices)) { - iopt_table_remove_domain(&hwpt->ioas->iopt, - hwpt->domain); - list_del(&hwpt->hwpt_item); - } + if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) iommu_detach_group(hwpt->domain, idev->group); - } iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); + idev->hwpt = NULL; mutex_unlock(&hwpt->devices_lock); - mutex_unlock(&hwpt->ioas->mutex); if (hwpt->auto_domain) iommufd_object_destroy_user(idev->ictx, &hwpt->obj); else refcount_dec(&hwpt->obj.users); - idev->hwpt = NULL; - refcount_dec(&idev->obj.users); } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); @@ -753,6 +744,10 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx, if (rc) goto out_hwpt; + mutex_lock(&ioas->mutex); + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); + mutex_unlock(&ioas->mutex); + refcount_inc(&hwpt->obj.users); iommufd_object_finalize(ictx, &hwpt->obj); return hwpt; @@ -765,7 +760,6 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx, void iommufd_device_selftest_detach(struct iommufd_ctx *ictx, struct iommufd_hw_pagetable *hwpt) { - iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain); refcount_dec(&hwpt->obj.users); } #endif diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 43d473989a0667..10db1359c067c1 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -13,7 +13,17 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) WARN_ON(!list_empty(&hwpt->devices)); - iommu_domain_free(hwpt->domain); + if (!list_empty(&hwpt->hwpt_item)) { + mutex_lock(&hwpt->ioas->mutex); + list_del(&hwpt->hwpt_item); + mutex_unlock(&hwpt->ioas->mutex); + + iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain); + } + + if (hwpt->domain) + iommu_domain_free(hwpt->domain); + refcount_dec(&hwpt->ioas->obj.users); mutex_destroy(&hwpt->devices_lock); } @@ -37,21 +47,22 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, if (IS_ERR(hwpt)) return hwpt; - hwpt->domain = iommu_domain_alloc(dev->bus); - if (!hwpt->domain) { - rc = -ENOMEM; - goto out_abort; - } - 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; + + hwpt->domain = iommu_domain_alloc(dev->bus); + if (!hwpt->domain) { + rc = -ENOMEM; + goto out_abort; + } + return hwpt; out_abort: - iommufd_object_abort(ictx, &hwpt->obj); + iommufd_object_abort_and_destroy(ictx, &hwpt->obj); return ERR_PTR(rc); } From patchwork Wed Feb 22 21:02:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13149578 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 B66FCC61DA4 for ; Wed, 22 Feb 2023 21:02:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229828AbjBVVCy (ORCPT ); Wed, 22 Feb 2023 16:02:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230217AbjBVVCv (ORCPT ); Wed, 22 Feb 2023 16:02:51 -0500 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2073.outbound.protection.outlook.com [40.107.96.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 485B191 for ; Wed, 22 Feb 2023 13:02:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W0IeOTCBDegFU8SM1DWgLu5KfpqKbKXr36uJ2YzrJnB9U3bDzvlsicQ9OV+QT0VRgAKmvUnYeMOHdnqacCmFwGxo6pQO59jn8hs1jUYNty+KyfYo3etAvObjJz7kyVUdF1gsr0JuI1ynLUwv+m4Y34xz4yNFKQKeM/9bsegBuzTLw58VXbVu0Fp22H84Wd9iHXiE93VfQgdatynECGFJ9LqMvy/XK31o3242Lqcm16T/Dyii3d8ecm/KQRTRVU/2xZHPS3q7DBG86p5qzoVPAjtW3RdEtcINDfxKsAzttvGVegCWdd0GY/Wam0b3yb8LBnAfKERaltUbhzCMKtM6IA== 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=o+vy6QB169xpVh0cvhlxe3tTYvCoCop2P2aKhq7vKaA=; b=lrPrHY/SARTAItpnqpXKtEqZV8jE0Wzpnmij9SxJs1QycNf/k1dY5kinjIA1vaSTJe9y/yriFJfeSwWpycMRpI0Lu48qJu2dSVgLMVRWN8TKpm7NVK6IxF6P1ND/u5HI3vx/UF/FE51P4EmT51i7+J4FeQs0fHiOjuZNzAcXjwWSHc9zx1NcVTtxDP6NJnhcNRwCaLNOJARZAfiaue7iYooC60QsugthfnTiexp3u7prqXdegrHfl9gRnEl1DYuIQayvxest5R3aHsJ78BZpR30m5bjXdLD2shDCqkUiSNP8EI2KNhgKNIPOuUTwkrHoIyeFsRquZuyXpyWQ5nOEsA== 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=o+vy6QB169xpVh0cvhlxe3tTYvCoCop2P2aKhq7vKaA=; b=AGzWa6Y3toW1iYzQ+Cx8F+Zncu2IAkK2X71JeJ2kkEjGkVNnuOF00HgatZIOtmW5iWisVSciKMoYgYi0GGG8GVmaiWIh4YTcek0BDAF75/iogguvSyvKPoqmiDqhNc8AI+rGGDLZeSW8Rb+VlFvOWZkxVOP8CNvWRYoyF9K5myDZ4MYWD16CsLhpn4FGnvBQnUROjlhv7j+InG2/CI8nKhxeu377OhnbrRFp+OB8S3K1uEARKWuGHOLT6/NYuxKpwaHDZx2aI74eUQZNl9MGKng9yE0KH3NFzVJ8NGqNIP7fQ0jaLWPhLYad8twS5Tx9xr0RjtasjkwOkMN898qT5g== 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 DS0PR12MB8248.namprd12.prod.outlook.com (2603:10b6:8:f3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 21:02:43 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6111.021; Wed, 22 Feb 2023 21:02:43 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org, Shuah Khan Cc: Nicolin Chen , Yi Liu Subject: [PATCH v2 4/7] iommufd: Move iommufd_device to iommufd_private.h Date: Wed, 22 Feb 2023 17:02:37 -0400 Message-Id: <4-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> In-Reply-To: <0-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR12CA0009.namprd12.prod.outlook.com (2603:10b6:208:a8::22) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|DS0PR12MB8248:EE_ X-MS-Office365-Filtering-Correlation-Id: 38deba6d-6b4a-4c05-bcb4-08db1518236a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9Y8OVRL0/8nPlQXv27x3gQECbGfgnZu+jeFrmuJ8oRcUU0NzCSkpP3jajnvZAxSmzu69ZCcEv7uoAROSTaTwpxj/wTus65JvhBBCAq7h/HzXtfEDS6qgCSj14cKd/GghtoaTtF2n9K8+mRr0IC4p/imBPaJlYDPXL96KXomzmdIKmmXF6We99kYT/IOq06M9Nrrt6MGswgHd78U+YcV1xo8eAv0G7o8Dq6S2s1xqcxI7Q4Sb1CVR1H+JL39ANVFD67xWlAhB6r+U0XwMHqWvNiWns/jUnPtN6pLyJkERbraQxFJyIg4+DRXmnHDTzxPXUfO3K4kHT3N5a5kQvllWaGavvkeNnCay31O7eVLLprSUGyg0o/iP+Ju8EezCaZhJ8lioa3NejATEeUTKk0JOa7LnWjWqEMrnix8BHStEddaNTh081NR+WGT1Cc0qnRXiv8I1GBtx2Dcbl7pgiA3m74UM+zxERZLbsCQ0ahwOs1wrCArKYAuExZTfTGjUbP4/hlfL1YT9jLbZqGDf1zW7azd9VFoZFC2w5XVRwWnW3RN178/YpBuKfn5bgQemOKbeRCemZXPvUFZU+ZBithNfQWiOyGfCfRJRjPmwbzlRHOHzBFa56V0xwuQ3uBj1fDA5JPhsoXCyRVkNRaf9tTlE5xPYKdfCeqgZuSjzLBEt35Uguj5JXAZpxqE2pGd5qpX3 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(39860400002)(346002)(451199018)(86362001)(316002)(5660300002)(36756003)(66556008)(8676002)(6512007)(66476007)(26005)(186003)(4326008)(8936002)(2616005)(41300700001)(54906003)(66946007)(478600001)(6666004)(6506007)(110136005)(38100700002)(83380400001)(2906002)(6486002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: XdZdVH3+o4AvYIkqI8keoR6tlIk4ROXcXyDrFxFCwzDzvi8hwDrWofRyFAZ6PQqAPTmoDdz6tQySEgZOnW6qwh76nA0v0tw702UGyCs52UBP6v4QMYf0rXzb7IwCBI0PG/UlyNHuMJo0SppXFxMmJhZXLAcyxUKd8DsuS2mT4N5PQXgh01QR+fp5ot6p67wqnBT+BR2iAuPGf+NVVQSZcw5dR5plfpFomk+29Llx3r/QE1PY7iNMSiyF1CPx/AT1oR6SB7idqpQXbL6kUGzC03zB0xBh6/dfzA090DeD/LstAG5LNgA79+aUc28gg8Uz1BJQb+4/zZCW8O3gzURQqOLFSG5t9SSpPuUnytRyNETewo6AwO7hZB7RUQN1v1szlsrviMpc9fYXSa7Mygox3zHh7oYTl33wbkSMAVDzFjerVDYanVzdi7Q+95XU6lAGMLkmWTnnAmXGL8mu128vXKR16cKw0fuGqVhVLJYZnpAG2fE9MXAstseiGmLHRIArTNSUa9AjbRHz46mcoYR+IYgFY8pHG2VQM32zlh50jZ8/JNrSdLHn3JWZl8E2IZCkTe84fnYOBrTJ37fKPGkkmNnHOMZuBV17nCjR9MeuIKyeMq6B4+3YHVTRWR6CPu13oNd4u5/UmNXjBhhQuJMnVv94keQnHCeBMtNE00TwuvfqK2AG6H8qNkRLRWN8IumUfeMD0hxwukQob04VICzkRpLniv+5Yktp7b6ud1nX6y69j2BPvjk3p/Xz1ATwkVxG5Z7aOzUTmnLnYAVY8NTDTdgUpW698Gl/OUgNxeNdOVxcKsww+jZQ2M4W6IvfHkORDsHSB3fUv4JdqLcqk1cM40Asgte13tGCG3PZgfHGNaRvGYlqX91YRmrH5pnegpu7fNqEgOnTT3yVVUzLB6DRIrLiJKsQPRn47ZVLgUhoQaDcQjnk731ezxy4jq3rHoKpbBUdFE3RF1DNP51x9PxI4XdFjvULnQGhctP7qLDinBRyGlGSFc3iF3BwJIs3OJnPJupbMEequ/6B0fan12oJBh+CleoGLc+7vbUUM/34LMOTCo/Gl4hDBmVfLE9Npo7gV/0ORzpNPh+1XhnMmgGR3h4F+42/XCSQu7Ofzwjl3Q2UfwRboTNRhERrViEADaEoMQRCI7YIRoYHjlULZREiLVhhgb68T3p9P0Sx7ZoZYviP9XW+eZPlKpqdWay/r4GUJlnNAlZIIy6kWt9ZNM5NEtCx0HHwRZ47GhuhSPALrf+tuns7oon813vg6Q2AEImT7oovsQR7zJ8NrxR5TC4Iv+ACORgOEQJOhWET0I5HgmuKA8vDZBFV/UUaApCKIY6O8ZwZ394QVWS571+2Ci7D771LivAVk10SbtBQn3j74w8YLHoLvJvHg8kpoGAOmxzayjVeFnKrl1VTWEVvaDhyaqX7Y7yIfI0O/SQR3cF8BtKLgOGhkAW4LTOqark+CZNQPu8DvHwjwKiuDI/Rcg4IuJ/7H2GgG9lKc0nq06Bes0awwLEru86q0U2WXx1Z/9/xgN4ZLfEhVr2wa8FQhCiLlCE+vNo0AkwwEs1eXLP2mJoLZWJXL4FEabOV7w1fw5w3 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 38deba6d-6b4a-4c05-bcb4-08db1518236a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 21:02:42.3621 (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: b7mWRPVBNrdPQvLguTVX2VbZ3hYjWJS2oyyreDr4BlKgmTMTcTWybIaXQNhd5fuf X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8248 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org hw_pagetable.c will need this in the next patches. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 17 ----------------- drivers/iommu/iommufd/iommufd_private.h | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 6787a0d8d6e9f0..4483c06e0ec38b 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -15,23 +15,6 @@ 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."); -/* - * A iommufd_device object represents the binding relationship between a - * consuming driver and the iommufd. These objects are created/destroyed by - * external drivers, not by userspace. - */ -struct iommufd_device { - struct iommufd_object obj; - struct iommufd_ctx *ictx; - 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; -}; - void iommufd_device_destroy(struct iommufd_object *obj) { struct iommufd_device *idev = diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 9d7f71510ca1bc..84fe19a195466b 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -12,6 +12,7 @@ struct iommu_domain; struct iommu_group; struct iommu_option; +struct iommufd_device; struct iommufd_ctx { struct file *file; @@ -257,6 +258,23 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct device *dev); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); +/* + * A iommufd_device object represents the binding relationship between a + * consuming driver and the iommufd. These objects are created/destroyed by + * external drivers, not by userspace. + */ +struct iommufd_device { + struct iommufd_object obj; + struct iommufd_ctx *ictx; + 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; +}; + void iommufd_device_destroy(struct iommufd_object *obj); struct iommufd_access { From patchwork Wed Feb 22 21:02:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13149579 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 72AF5C64ED6 for ; Wed, 22 Feb 2023 21:02:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229762AbjBVVCy (ORCPT ); Wed, 22 Feb 2023 16:02:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229844AbjBVVCx (ORCPT ); Wed, 22 Feb 2023 16:02:53 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2062.outbound.protection.outlook.com [40.107.93.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9B2E449E for ; Wed, 22 Feb 2023 13:02:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oLyOanHSsvniBnc0Xk2VBB4KOdn4eROONJCQyJv4IVeGkyIDJMio8jIH2ctCAy7EMf0SRUemkfxHAIzFJseQ+GFAyLSfDivV6IBWYy+0xRwbvDJEL0pVsaGhX3kc5zUmK6m2DPX9TS4s60BYTX6U7811P6PLk8cKSN1DFUAFdvv/uDc+D94go5bYOwLXshy1oj5TBOM0KUSZaZPzlDbc1ErFc22mN/8Gdl3e/wt2RwoPc2O2VzZt1qjZWZvQR8P2DQ4PDUq1YerbUO2s6MOGhxfvcpno3VOVGiLl5gQtZy1GCXCnnq/8drQyPZi0c2kJ2fIhHz7PAoevwAzhYxCIIA== 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=fm7onPlIlwvUJiAOC+X/bjrbIfdQi/Bx/CsfkN+a6sg=; b=iw6Nycc5N87TZk1EdkAqt/vZD76YXNZX8sf8IiRveMsLpd7V/sMQwa9D6zyJ3s1Hfgd5WjQVb90zyjPz4k+QCwd/TYAS+xb8+Zq3Fswuue3L0/YEc50SGBDP4wNWcC+hg1YYpiTUi5LXelGsBBymy7jeAZu6WePqtrNpCbUr71EiC68PgsPGMvqy4iH3blLm2WowN0b59si+ygk+rhziaMopo0ePEPuBkG2KNvflhADOEGbw36JW52F0J+5JrW/lA8+jQUC3xQ/6W6xAp8g6qdq4lTUxiHILySazmFgbmWAc4nJPDXEkwnhg2Op6Kgbbzd6h2TWQjaacmF8rNBzoog== 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=fm7onPlIlwvUJiAOC+X/bjrbIfdQi/Bx/CsfkN+a6sg=; b=pxSNrDNKzMhpFiO1g+Fjmw9QVEjmb5R13mH11q37xMqp7Rh/URSoxSJtbATVO8UiJt1VogrlTPRDJbDcVkBoQqOfFzAiKPF3Gc6gM0+xa8643UUSCKdpgFCYt5bquZHw2RkEuuG+v+fTkrcm9SxwwM/WTBkRoMeXyM2A6W6hykvtPcL97oH313e0ZtHfZuHk5Llh6pxQz2rVkPN2j6wxBtdMZ31u8OLA/sPonnshbMs8xRIwtnvitzPOptZJdhm47PF6tMaS1+ktDyd6OXhVIK2vSio1D0Pihc7/n0Z4bFn00HIQXlGYgyj+7yt7OujBeG5LtS57o8vJh7yRnn0gIQ== 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 CO6PR12MB5395.namprd12.prod.outlook.com (2603:10b6:303:13a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 21:02:46 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6111.021; Wed, 22 Feb 2023 21:02:46 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org, Shuah Khan Cc: Nicolin Chen , Yi Liu Subject: [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain() Date: Wed, 22 Feb 2023 17:02:38 -0400 Message-Id: <5-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> In-Reply-To: <0-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR20CA0026.namprd20.prod.outlook.com (2603:10b6:208:e8::39) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|CO6PR12MB5395:EE_ X-MS-Office365-Filtering-Correlation-Id: ecb14810-9f72-4967-55f2-08db1518245e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qqP3Ep3Yp/+dBkDxaEpnR+AHcse5lcjYvBMpG/y9OXTRjMtSZsP2Nn0VSYf7G3AG+NPs7Etax/Ja1uAMlXMYAWL3+0reP+3SyIZFLW57QS/26k9AUHwVERaWXYf9TzHR4ujpAL3S1HXSNPrGgO+HiS7TDyqyIuRQ5ut2KjF7VGfflFsAwy2s/uMaTiHKloB7d9nPeoWsFuZi6iSYBPkc/v5lY7q5fTBJpqgdAeKIxlyyFybIj31GFzOMM5Np94Ys5YrsRGlEfmBEfiqzV6EEGPEmuadaJWBiDXmeW1gMXOFrFzuaU9NzjQ4fViLYIZ6ka+0hRQyB3JrOq+qRiz+knvjZGMmWPVczsPUzXjT2T1lLElexOQRqtKHxUjEZF15I+c/d3mCQ+trjNm16oXTfgSD5RwCeBpawHZ4KaPHw6CAO8Dgk5q1lL3GG5pZFxLegKd4Emk+n8QHzVs50CmvIybRCRzd2iByGxhIJDOPFctf75N9JbqbBhjCaNSD7plvvHR9eCwm6kBWoccbgz/XGaE9SFNlstlF8E8WwMiH7ARTSM2M5Dr7BPcjvGsDKfFf2TPBucYiYHFGspD8a09NaLKfe60VitZP+y2+nw1lPUmycY/wmZk3rjb9upLk2cjMcUAQyVvGvWUQzOuSXal/zkXum95xb6emNpb6djemd4vw4l53vCn1It9MASoo72uqn X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(136003)(376002)(366004)(396003)(39860400002)(346002)(451199018)(478600001)(38100700002)(5660300002)(6486002)(2906002)(8936002)(41300700001)(2616005)(110136005)(54906003)(186003)(6666004)(8676002)(66556008)(66476007)(26005)(66946007)(6506007)(6512007)(316002)(86362001)(4326008)(36756003)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LJ2jLeSTZlLGJJAP+TlhsdL+auzyr1WZGGtGHZp2YIwZV2pWUDHySpKm6ixQ23ji8/qusUGrslvU5ImVNO8rf+cSDklHjnSQMsIiJ8HdM/rNAzfVy2WrlTN6E76LWZ8Kxz/etdqh8T+bCSvRbMXxz9UKKGQq0och71KnSKvxcAywbDRBSwwLwGmIoV2g/fzbCBfiF+HTz1Vocw40ijPpzDB4zlvLo/mNbRj7ZhpIDDFRw+jeFWFNRnm+YZwpkniDKKwLlnFOJK4WrhS0Y505GFhvVcdc7tqZ4bmJl8CeSdrQ8K0Jlg0zfUkDc3GcogVFLY2N8YReiDK5blX31PzsEEFZrv6dmvVFUzvC8lNdl7MxuO93SILGGEpGkGF1/9ViDxJ74BSEyk8eepMbj5tOu4MLTyL7PeM5siX3FjoYv9F+kbE0HS7rcDYCSo3yVI0qFC6RSI3wN06F6MqBuRWmUHAeibcPHR3UJXQEnsqNH/i917+lBYbHKWhJenDi9WwB4RbwmDj0glIVk9LoVltYb5cargymF3K3MIQvJZmPAIe2HVWofzCol3SoLmZIQUKPpinBuVNzmmCEpoO/BEsF0IZinreVTCAV+rd4i2ghD9kWVFisH0oS8Oi24/lpxOlVLEaQ6bEmBb6T89+RviAEofsCw3DqrsZNGel16pSCeXSsMEmVzYg2DBm2yKM7Y8w5lTHQb6x4VsTo0vMiubtgUCdALG1x7RtDx32z6qZp0nRE2aK0TaU4f937lAIQuyxyGUgHX0j3xW0DCGd+Tj+5NX46kdcnz3rXuAmUYkCnRp3/y0tO5auCi3F+qw/q7ULMkesRrzzXl6WAdVOQpj//5HHBJSr3JA7cJrmZ/skIDOdGH85i1a7DZmax9K4wGrKHuV1k/qb1o6KzfIhUFRldwJtooDqyxcRLBYATnEPu7kbADeyTuO9EZrLmXHKnBSAAFoLaDcEExBlDRyRD+NP7iAykzjp4dH5cWZ/l1LynSTCFPuKeCe1gs0q9v2JcsvJ3F2yjtn7f+n8KQgE2YpmIo3XDW8w/MhFzFnRbUllMd5UxPUX1bt6ljMdQLAKonnA22acsDByMl/xWnRnCD1u+T6H/NtBjd6sN0Wbp0rL/UmcetgfJ3HG1yfIT/ojyJRd0tOskTFWFwiJklAEeS5I8TEWo3WxSQBa2/9ycgBB2fxJQTj1lpr9GtXbJPktLKPPgdEq+tr8GFWtYUhlddJGUCmBBLiOGQoiMYDfEZRVtPpHyiM7qt5qN6Fz8Pw0pToVLm94CirEWhzFWyJWzx+/D64zo1df3OUz6e567f1aemsDvmG1/mWzIGpqxJe9soZp2VB4rowFgZL+CR/Dk4NVq5TAyih8Xgq+m9h+1gQKoNe4eXSmbuWZTPOAimbGM8ZfQUtAM4lsfDgTyWP74jZZqlTX3nSk1HoX85PEZsXvMh5OcFV7GzqrmSJ2q4J7UeSgWN1eDGF8xipneaVH0jbOVIik+7OuDjzZXAxbrjzg6udmXQqA28EdmTaxwDefHFFOXJ7eS3njoI6BOhxJ+UDdctiW5oLorDRX9O31gG5lNYb+3KagK3tupyTVkOIle7Hzy X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ecb14810-9f72-4967-55f2-08db1518245e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 21:02:43.9273 (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: 2Xex2oSsUUdzxS/c1aUXw79AVGpQH4Yg0Dv4I5fK3uPshUf/z0mcwYg73a4qMa4I X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR12MB5395 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org The HWPT is always linked to an IOAS and once a HWPT exists its domain should be fully mapped. This ended up being split up into device.c during a two phase creation that was a bit confusing. Move the iopt_table_add_domain() into iommufd_hw_pagetable_alloc() by having it call back to device.c to complete the domain attach in the required order. Calling iommufd_hw_pagetable_alloc() with immediate_attach = false will work on most drivers, but notably the SMMU drivers will fail because they can't decide what kind of domain to create until they are attached. This will be fixed when the domain_alloc function can take in a struct device. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 88 +++++++++++-------------- drivers/iommu/iommufd/hw_pagetable.c | 47 +++++++++++-- drivers/iommu/iommufd/iommufd_private.h | 6 +- 3 files changed, 88 insertions(+), 53 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 4483c06e0ec38b..cf2517a6472ff8 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -177,13 +177,16 @@ static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, return false; } -static int iommufd_device_do_attach(struct iommufd_device *idev, - struct iommufd_hw_pagetable *hwpt) +int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt, + struct iommufd_device *idev) { phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; - mutex_lock(&hwpt->devices_lock); + lockdep_assert_held(&hwpt->devices_lock); + + if (WARN_ON(idev->hwpt)) + return -EINVAL; /* * Try to upgrade the domain we have, it is an iommu driver bug to @@ -198,19 +201,18 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, hwpt->domain); if (!hwpt->enforce_cache_coherency) { WARN_ON(list_empty(&hwpt->devices)); - rc = -EINVAL; - goto out_unlock; + return -EINVAL; } } rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev, idev->group, &sw_msi_start); if (rc) - goto out_unlock; + return rc; rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start); if (rc) - goto out_iova; + goto err_unresv; /* * FIXME: Hack around missing a device-centric iommu api, only attach to @@ -219,27 +221,36 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { rc = iommu_attach_group(hwpt->domain, idev->group); if (rc) - goto out_iova; - - if (list_empty(&hwpt->devices)) { - rc = iopt_table_add_domain(&hwpt->ioas->iopt, - hwpt->domain); - if (rc) - goto out_detach; - list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); - } + goto err_unresv; } + 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) +{ + if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) + iommu_detach_group(hwpt->domain, idev->group); + iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); +} + +static int iommufd_device_do_attach(struct iommufd_device *idev, + struct iommufd_hw_pagetable *hwpt) +{ + int rc; + + mutex_lock(&hwpt->devices_lock); + rc = iommufd_hw_pagetable_attach(hwpt, idev); + if (rc) + goto out_unlock; idev->hwpt = hwpt; refcount_inc(&hwpt->obj.users); + /* hwpt->devices is all domains that have been attached */ list_add(&idev->devices_item, &hwpt->devices); - mutex_unlock(&hwpt->devices_lock); - return 0; - -out_detach: - iommu_detach_group(hwpt->domain, idev->group); -out_iova: - iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); out_unlock: mutex_unlock(&hwpt->devices_lock); return rc; @@ -281,23 +292,16 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, goto out_unlock; } - hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev->dev); + hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, true); if (IS_ERR(hwpt)) { rc = PTR_ERR(hwpt); goto out_unlock; } hwpt->auto_domain = true; - rc = iommufd_device_do_attach(idev, hwpt); - if (rc) - goto out_abort; - mutex_unlock(&ioas->mutex); iommufd_object_finalize(idev->ictx, &hwpt->obj); return 0; - -out_abort: - iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj); out_unlock: mutex_unlock(&ioas->mutex); return rc; @@ -371,10 +375,8 @@ void iommufd_device_detach(struct iommufd_device *idev) mutex_lock(&hwpt->devices_lock); list_del(&idev->devices_item); - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) - iommu_detach_group(hwpt->domain, idev->group); - iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); idev->hwpt = NULL; + iommufd_hw_pagetable_detach(hwpt, idev); mutex_unlock(&hwpt->devices_lock); if (hwpt->auto_domain) @@ -716,28 +718,18 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, struct device *mock_dev) { + struct iommufd_device tmp_idev = { .dev = mock_dev }; struct iommufd_hw_pagetable *hwpt; - int rc; - - hwpt = iommufd_hw_pagetable_alloc(ictx, ioas, mock_dev); - if (IS_ERR(hwpt)) - return hwpt; - - rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); - if (rc) - goto out_hwpt; mutex_lock(&ioas->mutex); - list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); + hwpt = iommufd_hw_pagetable_alloc(ictx, ioas, &tmp_idev, false); mutex_unlock(&ioas->mutex); + if (IS_ERR(hwpt)) + return hwpt; refcount_inc(&hwpt->obj.users); iommufd_object_finalize(ictx, &hwpt->obj); return hwpt; - -out_hwpt: - iommufd_object_abort_and_destroy(ictx, &hwpt->obj); - return ERR_PTR(rc); } void iommufd_device_selftest_detach(struct iommufd_ctx *ictx, diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 10db1359c067c1..da1df54877f43e 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -32,17 +32,22 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) * iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device * @ictx: iommufd context * @ioas: IOAS to associate the domain with - * @dev: Device to get an iommu_domain for + * @idev: Device to get an iommu_domain for + * @immediate_attach: True if idev should be attached to the hwpt * - * Allocate a new iommu_domain and return it as a hw_pagetable. + * Allocate a new iommu_domain and return it as a hw_pagetable. The HWPT + * will be linked to the given ioas and upon return the underlying iommu_domain + * is fully popoulated. */ struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, - struct device *dev) + struct iommufd_device *idev, bool immediate_attach) { struct iommufd_hw_pagetable *hwpt; int rc; + lockdep_assert_held(&ioas->mutex); + hwpt = iommufd_object_alloc(ictx, hwpt, IOMMUFD_OBJ_HW_PAGETABLE); if (IS_ERR(hwpt)) return hwpt; @@ -54,14 +59,48 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; - hwpt->domain = iommu_domain_alloc(dev->bus); + hwpt->domain = iommu_domain_alloc(idev->dev->bus); if (!hwpt->domain) { rc = -ENOMEM; goto out_abort; } + mutex_lock(&hwpt->devices_lock); + + /* + * immediate_attach exists only to accommodate iommu drivers that cannot + * directly allocate a domain. These drivers do not finish creating the + * domain until attach is completed. Thus we must have this call + * sequence. Once those drivers are fixed this should be removed. + */ + if (immediate_attach) { + rc = iommufd_hw_pagetable_attach(hwpt, idev); + if (rc) + goto out_unlock; + } + + rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); + if (rc) + goto out_detach; + + /* ioas->hwpt_list is hwpts after iopt_table_add_domain() */ + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); + + if (immediate_attach) { + /* See iommufd_device_do_attach() */ + refcount_inc(&hwpt->obj.users); + idev->hwpt = hwpt; + list_add(&idev->devices_item, &hwpt->devices); + } + + mutex_unlock(&hwpt->devices_lock); return hwpt; +out_detach: + if (immediate_attach) + iommufd_hw_pagetable_detach(hwpt, idev); +out_unlock: + mutex_unlock(&hwpt->devices_lock); 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 84fe19a195466b..331664e917b771 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -255,7 +255,11 @@ struct iommufd_hw_pagetable { struct iommufd_hw_pagetable * iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, - struct device *dev); + 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); void iommufd_hw_pagetable_destroy(struct iommufd_object *obj); /* From patchwork Wed Feb 22 21:02:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13149581 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 4CD53C64EC7 for ; Wed, 22 Feb 2023 21:02:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229922AbjBVVCz (ORCPT ); Wed, 22 Feb 2023 16:02:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231308AbjBVVCx (ORCPT ); Wed, 22 Feb 2023 16:02:53 -0500 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam04on2046.outbound.protection.outlook.com [40.107.101.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5152146A1 for ; Wed, 22 Feb 2023 13:02:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eiVlCH9ZmNF6lthoA7m3Am2nrXuZ/41XXuq2jfTcsGZNOyQSPUP0pv1JDo6ZuL8KB/zipv8FjpU2e65IdxWMgqftPagiNXiOu79GTJtqKxPD2k++0yen1EIiCXmFC/NiZnPMIPt7a6vhx/igR7tbcowwnYpYcIodvRsCR0HgscFcKqEOF/j5OKBH8NGIcMbsSeCk8Z/YQCAkLs/WvnkWMfJZJBpt2+XqpkBBbsFO4SWfsqbKK7JxH7030/3lGbHdNzwJaeGYe8yYofs1eao0/w+sLafYBV9fxYsDqlhyelxzxmn8MGVtEqiviTNXBKF9nFm1wZ9M7DnMnybKIHtBFA== 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=BIbZPEi40haaohP5cRK05QnG+s+96VhqbULjGDDm2As=; b=ZPwZ9vxnWeY5i2uA4pB/kYdUuM5owiw+w7LNId6FUHUs5JiDgTQbFBIMEOIe2Tfvc3iZlNLO/yb4pNKgXnF7pi6lGEcjZRBZMD9ULvg2vcWTubHe/hXB/4DqUfdl/hdJj99WCKNWdtSdfRh5q6nsanHBNHK37ehK56BKbQxKpc/5BcnQJfepDnkBOIKNsQRY6v9qlIZfsMj/L4RpIuFogUYrjtuRnnvb0+Fy/agYxV5p9Uv2Tq2qQhExcxg2C2xuniQdkH3BfU46BMKiYJPSJkzNl/Tg3KWLY9rMt96uDLNExyn+3LBD3CZMbMBpyZV2ueRgmyuEdiu5+vbbT2wc9g== 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=BIbZPEi40haaohP5cRK05QnG+s+96VhqbULjGDDm2As=; b=TD9/yOeGy3eG08/jQQojIaYOmpdmTsvSe0tRBzGpVtVcXb7E4ukA6d0siJPx2wrNd3TD7rBNSdUe0zJahs9aQcmKz57c6G4X+6yUlxlRLB8pPmCjl+so49NlxxRrp1TfiMFyUDLrklBlWT+qnHiv2iiLXrB6fMPLvY/i1Ee6KmCgYKo2mLZ0ia05x0KAhhG1vBzpFdKj/hCtsxOr/Y92VI4t03lBYROkiDGCxKMp7DcNSxRBNs1yhmJL4Q1tEjbDdpoGzFOTRdW2YvYU5gQ7jzzHHVNWSNnO+o6bC/kk+fOZFLxEubjoDpvy38hLosbh84HqRw2NKuyddFd+uGo6xA== 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 DS0PR12MB8248.namprd12.prod.outlook.com (2603:10b6:8:f3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 21:02:44 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6111.021; Wed, 22 Feb 2023 21:02:44 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org, Shuah Khan Cc: Nicolin Chen , Yi Liu Subject: [PATCH v2 6/7] iommufd/selftest: Make selftest create a more complete mock device Date: Wed, 22 Feb 2023 17:02:39 -0400 Message-Id: <6-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> In-Reply-To: <0-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR10CA0017.namprd10.prod.outlook.com (2603:10b6:208:120::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_|DS0PR12MB8248:EE_ X-MS-Office365-Filtering-Correlation-Id: e8833741-8a9e-4528-ae42-08db15182405 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Eowigl0xpR3iezP4OtHQQ2KBQaVpt8esgMBkzFB7jtATKxoXBvjxBeEOOApG6u+dxBoGFc5x3k3Cn96QH5NxmiZSZgLPZPnf5YMJFgguCbPud54hGN1LAHySqkpqp5Tiugu1f2+YMvbHn/BZlYWMHfm/zAmTUB42Taa1v/9eZyE/lnYz31IDfw8w38G0i6yVB8bFTG6RITvM24roS3q5rFVm/DtI9DAVtTWMlNJ6rVZxNrC1/dBeZ2l5zY+wcyBWRD/g9W3aF72BHlrf9PyUf5bgj8f0eooukWXhOzfxqD4o6E/kRCQ7lyt2HTVjU/qk1JK3F7Q8cKnGTl6vhGRcs6KfHcNV9dpN+Oal8HwSUqwtxrTEmekp4+IzSXUKwNEi0U34NdQm1s6cAFlAC3kP/Y6+TFVjrKQI2wUQcABOOgDzXf5yPb7cLnpBx7wpVDi1uuHDzeiHbBikBXOZDJ3Pc2Zm02l25gHdCqHK+BXwmYhbkIUtw1Y0oxrVHdTnWfY8wNHIeu5P08G2YJTHEN2XlXpnsK4VV0DEGKyyGpp0TxoopsPoXlIml6W0Ehnnu1Yc6Iz+XJxweqtOIATRIG9W9ssUjXsXd6IrQz1Eyv2VtjlzpIbsPED2DFdpmacGER2yteWraaBmQW+cG4JkC7zlPkR9XmopY0N3g/t3rt7uuKIjXvWQ+/zy97DjQbT9itQH X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(376002)(136003)(396003)(366004)(39860400002)(346002)(451199018)(86362001)(316002)(5660300002)(36756003)(66556008)(8676002)(6512007)(66476007)(26005)(186003)(4326008)(8936002)(2616005)(41300700001)(54906003)(66946007)(478600001)(6666004)(6506007)(110136005)(38100700002)(30864003)(83380400001)(2906002)(6486002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ptw3Nn67o8Odw1TEBpFDM8LNttGDQeOJqQKMYkFBX40KyKO1xrLZIXlCVlgg6Sjy+TXrV46DLSCxvZ7olvDH8N8pP2Xc3snslSfG80LBOlG+Kkjw6Bl0OGrebWSEGSs0D3ZOANL4F4kfvQaWAtW5gzbFjnPd5mJ3Nk5jgLmHjE4PwTSQQwRTB8DmJ/Z7GYLn/Dn2YMB024YJf7TdGvtxMfk9yeusDK1lTGSOw1sAorzo2xvzwgy7P83THVTT+kGrDuHtiKi8nK3OrD+CKdVTaLeCGEUugNrViduYZ8X8hdwohSix90mEWCkYLnoQ9lfdnMYkMq2F+SQj8d9a4/7xwrO1F6RsZCGvXwVbWMTM4X/pGrahgd0Zdc3/4Ac6PEb8LudHZrySb2vN1s9SYkP1Zw31Y9yFW4eT5IijGesEMoja8/IE5S1EqDPcduwTPkDAwQxyNpD8sa/2YMNmiUALTzm57zRKfomXBnreabJWWcHY08pZBhH7xfeaNrMoA/JA3NfS8338a+7WwNsukfVsD3sNo/4VcxSwNnATsahJWx9dYQ/LAy0wuPoMfRAgOM4AbsGb5AjoEPER/bFYIynkQRCer+XK3sW3B052SKz2zqKsOvfRSd5sDhA7sFRi49m3mmV58t6yM30I7bAiBrnNncYt5lp8WcvALMdUTThp/mphqeHIpT0vTNVlKevdOPHm33GbGQzFNasW2u4yYD1YPk0fKwSSOat0v/R4/RTxU418i0YP9c7ZabaFqbXh9f6lV3oQ80612JCXuBL6npKTWCtJX26A2rIozvj/jEGy8RvgAd7bCz+LAbn9vErFxO3HEinaelIXe4T5XHP6DrvUaPct2SuWSItRhjBJ07JL63//69kkaD5MFYv9WeG/f+apRgPdR7ooTaFsSUPKZuOwanFMwUliSyLRaA28RSBidb1cW0TTbUOnVL5bTkydD6rIMt6aGRO3Zu2tUbWyokCdGCKpbzLSLLRrUOmfZf4YYXfdZ/R6IAb+p4fMVzjmSFESMEvXvrSseSgLNHok9xv4fVQXPrPYWbNlP/03jRllzxiiorkf5fVkSyucKlBhpl0c4to9UtwhpXLr+O2Y735+9rLvJXS+fRYprwQ30sT/gpz3SKlzznfwcspw8DeOqkAWSs528sHkMM2iUlG65DR8Jb9qIEXGYbg5tXw5NXw72ej4AwP2vRzraBMxgjWdNgw7HutmgzWmiaFALmpzvYZiW412MdZOtx54m9cGRfF2NN/mBUim7QDh8sLNOv9NNe76MSU4z/6ZwcEY0xyIt52udCweXcp3ulHy9rqVJjP8ANaL1O5p0O7YiK4hyBF0gHf0MzcN+s1Nc3Hes8WC+ISnQU1OBePGI2A9u9CX5/oUoHlkpHvOLSrY+5uESU7rd/9iQWGVMHQqFPjP4q0uiu4mZZFQe6HX0OaMIHlgERnQpy4EaV728G5wxW7FCfZnisxa4ckaECk7SNQJlDjTsuksIfh6kbObsq9poMhNwEtCGZc/87KOIIl7PxW9XRgQiORnnFN1iv1qTMEUX80dbMcg0kASyTIuuQsUpSF7oJJDGmRlMLp4Iymdc/gfLM75Z5KW X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e8833741-8a9e-4528-ae42-08db15182405 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 21:02:43.3463 (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: LS6+oLYoQACrglm7iEfYNiKvWV2aX3DkqHXOT3/emmPT0MbsupWR8+e3UZaVG/GE X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8248 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org iommufd wants to use more infrastructure, like the iommu_group, that the mock device does not support. Create a more complete mock device that can go through the whole cycle of ownership, blocking domain, and has an iommu_group. This requires creating a real struct device on a real bus to be able to connect it to a iommu_group. Unfortunately we cannot formally attach the mock iommu driver as an actual driver as the iommu core does not allow more than one driver or provide a general way for busses to link to iommus. This can be solved with a little hack to open code the dev_iommus struct. With this infrastructure things work exactly the same as the normal domain path, including the auto domains mechanism and direct attach of hwpts. As the created hwpt is now an autodomain it is no longer required to destroy it and trying to do so will trigger a failure. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 40 +--- drivers/iommu/iommufd/iommufd_private.h | 11 +- drivers/iommu/iommufd/selftest.c | 196 +++++++++++++++--- tools/testing/selftests/iommu/iommufd.c | 2 - .../selftests/iommu/iommufd_fail_nth.c | 8 - 5 files changed, 181 insertions(+), 76 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index cf2517a6472ff8..41cd65c758d3bc 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -22,7 +22,8 @@ void iommufd_device_destroy(struct iommufd_object *obj) iommu_device_release_dma_owner(idev->dev); iommu_group_put(idev->group); - iommufd_ctx_put(idev->ictx); + if (!iommufd_selftest_is_mock_dev(idev->dev)) + iommufd_ctx_put(idev->ictx); } /** @@ -69,7 +70,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, goto out_release_owner; } idev->ictx = ictx; - iommufd_ctx_get(ictx); + if (!iommufd_selftest_is_mock_dev(dev)) + iommufd_ctx_get(ictx); idev->dev = dev; idev->enforce_cache_coherency = device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); @@ -151,7 +153,8 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, * operation from the device (eg a simple DMA) cannot trigger an * interrupt outside this iommufd context. */ - if (!iommu_group_has_isolated_msi(idev->group)) { + if (!iommufd_selftest_is_mock_dev(idev->dev) && + !iommu_group_has_isolated_msi(idev->group)) { if (!allow_unsafe_interrupts) return -EPERM; @@ -707,34 +710,3 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova, return rc; } EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, IOMMUFD); - -#ifdef CONFIG_IOMMUFD_TEST -/* - * Creating a real iommufd_device is too hard, bypass creating a iommufd_device - * and go directly to attaching a domain. - */ -struct iommufd_hw_pagetable * -iommufd_device_selftest_attach(struct iommufd_ctx *ictx, - struct iommufd_ioas *ioas, - struct device *mock_dev) -{ - struct iommufd_device tmp_idev = { .dev = mock_dev }; - struct iommufd_hw_pagetable *hwpt; - - mutex_lock(&ioas->mutex); - hwpt = iommufd_hw_pagetable_alloc(ictx, ioas, &tmp_idev, false); - mutex_unlock(&ioas->mutex); - if (IS_ERR(hwpt)) - return hwpt; - - refcount_inc(&hwpt->obj.users); - iommufd_object_finalize(ictx, &hwpt->obj); - return hwpt; -} - -void iommufd_device_selftest_detach(struct iommufd_ctx *ictx, - struct iommufd_hw_pagetable *hwpt) -{ - refcount_dec(&hwpt->obj.users); -} -#endif diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 331664e917b771..d523ef12890e1e 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -297,12 +297,6 @@ void iopt_remove_access(struct io_pagetable *iopt, void iommufd_access_destroy_object(struct iommufd_object *obj); #ifdef CONFIG_IOMMUFD_TEST -struct iommufd_hw_pagetable * -iommufd_device_selftest_attach(struct iommufd_ctx *ictx, - struct iommufd_ioas *ioas, - struct device *mock_dev); -void iommufd_device_selftest_detach(struct iommufd_ctx *ictx, - struct iommufd_hw_pagetable *hwpt); int iommufd_test(struct iommufd_ucmd *ucmd); void iommufd_selftest_destroy(struct iommufd_object *obj); extern size_t iommufd_test_memory_limit; @@ -311,6 +305,7 @@ void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd, bool iommufd_should_fail(void); void __init iommufd_test_init(void); void iommufd_test_exit(void); +bool iommufd_selftest_is_mock_dev(struct device *dev); #else static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd, unsigned int ioas_id, @@ -327,5 +322,9 @@ static inline void __init iommufd_test_init(void) static inline void iommufd_test_exit(void) { } +static inline bool iommufd_selftest_is_mock_dev(struct device *dev) +{ + return false; +} #endif #endif diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index cfb5fe9a5e0ee8..8540adcb68f1f0 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -91,23 +91,50 @@ enum selftest_obj_type { TYPE_IDEV, }; +struct mock_dev { + struct device dev; +}; + struct selftest_obj { struct iommufd_object obj; enum selftest_obj_type type; union { struct { - struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; struct iommufd_ctx *ictx; - struct device mock_dev; + struct mock_dev *mock_dev; } idev; }; }; +static void mock_domain_blocking_free(struct iommu_domain *domain) +{ +} + +static int mock_domain_nop_attach(struct iommu_domain *domain, + struct device *dev) +{ + return 0; +} + +static const struct iommu_domain_ops mock_blocking_ops = { + .free = mock_domain_blocking_free, + .attach_dev = mock_domain_nop_attach, +}; + +static struct iommu_domain mock_blocking_domain = { + .type = IOMMU_DOMAIN_BLOCKED, + .ops = &mock_blocking_ops, +}; + static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type) { struct mock_iommu_domain *mock; + if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED) + return &mock_blocking_domain; + if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_UNMANAGED)) return NULL; @@ -236,19 +263,39 @@ static phys_addr_t mock_domain_iova_to_phys(struct iommu_domain *domain, return (xa_to_value(ent) & MOCK_PFN_MASK) * MOCK_IO_PAGE_SIZE; } +static bool mock_domain_capable(struct device *dev, enum iommu_cap cap) +{ + return cap == IOMMU_CAP_CACHE_COHERENCY; +} + +static void mock_domain_set_plaform_dma_ops(struct device *dev) +{ + /* + * mock doesn't setup default domains because we can't hook into the + * normal probe path + */ +} + static const struct iommu_ops mock_ops = { .owner = THIS_MODULE, .pgsize_bitmap = MOCK_IO_PAGE_SIZE, .domain_alloc = mock_domain_alloc, + .capable = mock_domain_capable, + .set_platform_dma_ops = mock_domain_set_plaform_dma_ops, .default_domain_ops = &(struct iommu_domain_ops){ .free = mock_domain_free, + .attach_dev = mock_domain_nop_attach, .map_pages = mock_domain_map_pages, .unmap_pages = mock_domain_unmap_pages, .iova_to_phys = mock_domain_iova_to_phys, }, }; +struct iommu_device mock_iommu_device = { + .ops = &mock_ops, +}; + static inline struct iommufd_hw_pagetable * get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, struct mock_iommu_domain **mock) @@ -269,48 +316,142 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id, return hwpt; } +static struct bus_type iommufd_mock_bus_type = { + .name = "iommufd_mock", + .iommu_ops = &mock_ops, +}; + +static void mock_dev_release(struct device *dev) +{ + struct mock_dev *mdev = container_of(dev, struct mock_dev, dev); + + kfree(mdev); +} + +static struct mock_dev *mock_dev_create(void) +{ + struct iommu_group *iommu_group; + struct dev_iommu *dev_iommu; + struct mock_dev *mdev; + int rc; + + mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); + if (!mdev) + return ERR_PTR(-ENOMEM); + + device_initialize(&mdev->dev); + mdev->dev.release = mock_dev_release; + mdev->dev.bus = &iommufd_mock_bus_type; + + iommu_group = iommu_group_alloc(); + if (IS_ERR(iommu_group)) { + rc = PTR_ERR(iommu_group); + goto err_put; + } + + rc = dev_set_name(&mdev->dev, "iommufd_mock%u", + iommu_group_id(iommu_group)); + if (rc) + goto err_group; + + /* + * The iommu core has no way to associate a single device with an iommu + * driver (heck currently it can't even support two iommu_drivers + * registering). Hack it together with an open coded dev_iommu_get(). + * Notice that the normal notifier triggered iommu release process also + * does not work here because this bus is not in iommu_buses. + */ + mdev->dev.iommu = kzalloc(sizeof(*dev_iommu), GFP_KERNEL); + if (!mdev->dev.iommu) { + rc = -ENOMEM; + goto err_group; + } + mutex_init(&mdev->dev.iommu->lock); + mdev->dev.iommu->iommu_dev = &mock_iommu_device; + + rc = device_add(&mdev->dev); + if (rc) + goto err_dev_iommu; + + rc = iommu_group_add_device(iommu_group, &mdev->dev); + if (rc) + goto err_del; + iommu_group_put(iommu_group); + return mdev; + +err_del: + device_del(&mdev->dev); +err_dev_iommu: + kfree(mdev->dev.iommu); + mdev->dev.iommu = NULL; +err_group: + iommu_group_put(iommu_group); +err_put: + put_device(&mdev->dev); + return ERR_PTR(rc); +} + +static void mock_dev_destroy(struct mock_dev *mdev) +{ + iommu_group_remove_device(&mdev->dev); + device_del(&mdev->dev); + kfree(mdev->dev.iommu); + mdev->dev.iommu = NULL; + put_device(&mdev->dev); +} + +bool iommufd_selftest_is_mock_dev(struct device *dev) +{ + return dev->release == mock_dev_release; +} + /* Create an hw_pagetable with the mock domain so we can test the domain ops */ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd, struct iommu_test_cmd *cmd) { - static struct bus_type mock_bus = { .iommu_ops = &mock_ops }; - struct iommufd_hw_pagetable *hwpt; + struct iommufd_device *idev; struct selftest_obj *sobj; - struct iommufd_ioas *ioas; + u32 pt_id = cmd->id; + u32 idev_id; int rc; - ioas = iommufd_get_ioas(ucmd, cmd->id); - if (IS_ERR(ioas)) - return PTR_ERR(ioas); - sobj = iommufd_object_alloc(ucmd->ictx, sobj, IOMMUFD_OBJ_SELFTEST); - if (IS_ERR(sobj)) { - rc = PTR_ERR(sobj); - goto out_ioas; - } + if (IS_ERR(sobj)) + return PTR_ERR(sobj); + sobj->idev.ictx = ucmd->ictx; sobj->type = TYPE_IDEV; - sobj->idev.mock_dev.bus = &mock_bus; - hwpt = iommufd_device_selftest_attach(ucmd->ictx, ioas, - &sobj->idev.mock_dev); - if (IS_ERR(hwpt)) { - rc = PTR_ERR(hwpt); + sobj->idev.mock_dev = mock_dev_create(); + if (IS_ERR(sobj->idev.mock_dev)) { + rc = PTR_ERR(sobj->idev.mock_dev); goto out_sobj; } - sobj->idev.hwpt = hwpt; - /* Userspace must destroy both of these IDs to destroy the object */ - cmd->mock_domain.out_hwpt_id = hwpt->obj.id; + idev = iommufd_device_bind(ucmd->ictx, &sobj->idev.mock_dev->dev, + &idev_id); + if (IS_ERR(idev)) { + rc = PTR_ERR(idev); + goto out_mdev; + } + sobj->idev.idev = idev; + + rc = iommufd_device_attach(idev, &pt_id); + if (rc) + goto out_unbind; + + /* Userspace must destroy the device_id to destroy the object */ + cmd->mock_domain.out_hwpt_id = pt_id; cmd->mock_domain.out_device_id = sobj->obj.id; iommufd_object_finalize(ucmd->ictx, &sobj->obj); - iommufd_put_object(&ioas->obj); return iommufd_ucmd_respond(ucmd, sizeof(*cmd)); +out_unbind: + iommufd_device_unbind(idev); +out_mdev: + mock_dev_destroy(sobj->idev.mock_dev); out_sobj: iommufd_object_abort(ucmd->ictx, &sobj->obj); -out_ioas: - iommufd_put_object(&ioas->obj); return rc; } @@ -780,8 +921,9 @@ void iommufd_selftest_destroy(struct iommufd_object *obj) switch (sobj->type) { case TYPE_IDEV: - iommufd_device_selftest_detach(sobj->idev.ictx, - sobj->idev.hwpt); + iommufd_device_detach(sobj->idev.idev); + iommufd_device_unbind(sobj->idev.idev); + mock_dev_destroy(sobj->idev.mock_dev); break; } } @@ -845,9 +987,11 @@ void __init iommufd_test_init(void) { dbgfs_root = fault_create_debugfs_attr("fail_iommufd", NULL, &fail_iommufd); + WARN_ON(bus_register(&iommufd_mock_bus_type)); } void iommufd_test_exit(void) { debugfs_remove_recursive(dbgfs_root); + bus_unregister(&iommufd_mock_bus_type); } diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index fa08209268c42f..0c4bbd4079a425 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -645,7 +645,6 @@ TEST_F(iommufd_ioas, access_pin) &check_map_cmd)); test_ioctl_destroy(mock_device_id); - test_ioctl_destroy(mock_hwpt_id); test_cmd_destroy_access_pages( access_cmd.id, access_cmd.access_pages.out_access_pages_id); @@ -1214,7 +1213,6 @@ TEST_F(iommufd_mock_domain, all_aligns_copy) 1); test_ioctl_destroy(mock_device_id); - test_ioctl_destroy(self->domain_ids[1]); self->domain_ids[1] = old_id; test_ioctl_ioas_unmap(iova, length); diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c index 9713111b820dd7..dda8f0187cd88b 100644 --- a/tools/testing/selftests/iommu/iommufd_fail_nth.c +++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c @@ -323,8 +323,6 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain) if (_test_ioctl_destroy(self->fd, device_id)) return -1; - if (_test_ioctl_destroy(self->fd, hwpt_id)) - return -1; if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id)) return -1; @@ -365,13 +363,9 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains) if (_test_ioctl_destroy(self->fd, device_id)) return -1; - if (_test_ioctl_destroy(self->fd, hwpt_id)) - return -1; if (_test_ioctl_destroy(self->fd, device_id2)) return -1; - if (_test_ioctl_destroy(self->fd, hwpt_id2)) - return -1; if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id)) return -1; @@ -572,8 +566,6 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain) if (_test_ioctl_destroy(self->fd, device_id)) return -1; - if (_test_ioctl_destroy(self->fd, hwpt_id)) - return -1; return 0; } From patchwork Wed Feb 22 21:02:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13149576 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 09954C636D6 for ; Wed, 22 Feb 2023 21:02:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229502AbjBVVCw (ORCPT ); Wed, 22 Feb 2023 16:02:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35958 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229828AbjBVVCv (ORCPT ); Wed, 22 Feb 2023 16:02:51 -0500 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2062.outbound.protection.outlook.com [40.107.93.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C1F12A999 for ; Wed, 22 Feb 2023 13:02:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TdyInJ1SDWKVIwOVBgGUQFuY8SDDfJrJtgV3ce9P8nnYGbj6JOieiUMtGHjtofOl1tpka+sOL7bMTgo1KfnexZSiJEQvwgpKD8eyIeMOLopgFngDpNXoS4B/dPF5qavoXVbcCYHoNCgMG/ZLNVPBTiaT8sucTAl8+zqijNvYV+U0jsBL83u3GuiQQ2cXdm9c2AYPcPf11RdcuPMX/ZPG2aDD5PI6pZjueVly26zKuhnr7EDKAq9qHFi85eVOcZsUVkFEONbW97R9PMyw6IDHTV7+PHHNMXITxQ0UClPyXIV0LmggAo7jrgw5G9x9ILkcvyZboTehW2a9zpOzogixaQ== 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=cHDOod6wc4BoYe9V10lD/rg7/WdZq7f9tpS/HuxZuGU=; b=KnZAk/f5wu9XSqqSFx6jAYX6vHdM6cmdF8FpYjvTDKPtbj5fJ11aIafmZPTn9q3VY+ztKhQHnCCEbiyg8W5b5BXpJzx+w/av0NgwMIbFhc280Wq3oUpXdMDckxUOXJAMiZlOeqyCHpRQ+TEKtNZXiZLtkpaHUzyl/IxbW3fUX+C38hhTwZmjw4qt33LF3+YNpuFRPu77DWcvW4/gyao8GL304wED4BCsyOv4VnTLY/q3NjB+E82hfxfvKYxCbLg8jFeiO8dWIOf44GDrkGePeE3rLgDWjWoJgeS4uduQFAiZiyRhBRao4AlJ4qHmgwVC2WypyEPWoRXdtu9FgTofCg== 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=cHDOod6wc4BoYe9V10lD/rg7/WdZq7f9tpS/HuxZuGU=; b=nw8gk3ci7HDJMF8g+Lm/P/fmkG1dhysCAhTTobnbvtgFkhR+IwAYZABfe4P5XMICLbnwrwO6f4bvTOzLDQ7lGziijxASNlywMON1oBWh/RvGr9yHVlNDr8WrMj80N9rC91eLdAtgbHQAIDyBFgF6f41zl52lI4PUUeiyDI6HNtNPlJcEMuX1HKBXSz6ueNd76JalW0T9M18+mLFTj9fic+Ql91jFnBvu8tanXsunev4BLm7rS87czcV/s6y03qnbwjo7qDjZcVVKCB/zcIqz+xNkvdaq1sahYf/HvzM6Ejx/sOPg5VzThmjPi9ROWhRH7s7LmakBYwcBZTaHOHgVPw== 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 CO6PR12MB5395.namprd12.prod.outlook.com (2603:10b6:303:13a::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6111.21; Wed, 22 Feb 2023 21:02:45 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6111.021; Wed, 22 Feb 2023 21:02:45 +0000 From: Jason Gunthorpe To: iommu@lists.linux.dev, Kevin Tian , linux-kselftest@vger.kernel.org, Shuah Khan Cc: Nicolin Chen , Yi Liu Subject: [PATCH v2 7/7] iommufd/selftest: Add a selftest for iommufd_device_attach() with a hwpt argument Date: Wed, 22 Feb 2023 17:02:40 -0400 Message-Id: <7-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> In-Reply-To: <0-v2-406f7ac07936+6a-iommufd_hwpt_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR10CA0010.namprd10.prod.outlook.com (2603:10b6:208:120::23) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|CO6PR12MB5395:EE_ X-MS-Office365-Filtering-Correlation-Id: 76069f90-58bb-4830-6fb6-08db15182429 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RrnShVhadC2khrKLPDJrToVPhgrnrHMi5/FSRkBmySTdnutc1v35t435wXVvqxWjtlMKaRqEOroUzJgzKdL6H2bxwsf3ifRTIMMIO7J+kAJfsOt/VLRZTw1CMtflwXKhtkZb4K2JO/OW5mesoOLHWuHzWPoap82kaKq9dB0E+Vg3adxDvdQHEZfwMFZflbAh36uEhV1wjHNkJthvvKk+S7v5XVvtCUGK7SXp/4eE+UQEekPsw2+IprwXbJKZBhXP/ZSjFLStJA3SCxgnZ8S47PlghdhDt9ni4ZcmC6hjA+mEg9HRfZH1zclNKdDb3hLcw/hpExlVJWCKgZVIndFYlmRfVgV3eJ890Vivw+qs0mzvETdIB9tpLvZnAkUo+znvH6WEkOo7j7Pvcs1G5iS/VlNDeuURfcZDSNgNBxpYev38VeEVYR8U/2r3PfNaCFXra+ij1hAebwVuW9aYP6xGyOStyAnfVbKarNeigkBHfEDStK3H33WaYCOdRW60NTC91ff8fyalLCf2EAFafQI8ruAb3GuPF3whOElF0KFFHmDGnbCKiZ3VlWoOQL0L+iH3QvYUFpS+4c5VXsyMhsdoe6VODmji3cA9EsgC/YJ7dA4AUlrkC8rysIm7M2K0wdqA1FdoNavFDPnEAjJANADr5IB/ZkjMj3dLkIrYFDOFFQgEzKMox+MCkuABppZbnjot X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(136003)(376002)(366004)(396003)(39860400002)(346002)(451199018)(478600001)(38100700002)(5660300002)(6486002)(2906002)(8936002)(41300700001)(2616005)(110136005)(54906003)(186003)(6666004)(8676002)(66556008)(66476007)(26005)(66946007)(6506007)(6512007)(316002)(86362001)(4326008)(36756003)(83380400001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Eqn+rLJypVcWMcg50OJjRJIbrYC/oYv0kER4tyLbIXPv/Do2FOFGXSp84jDiqxj05Vfj5VXjl9ntg/yjRxfURpq8OhMNl2RJyMeGuTxQoCU/RtBSkJlQdUfA/lZbuWnRk+zDFM41otQBZthn+o1PasQfkw2v6K2TZQhawvhLNy6bI/g/xExX5WsOfTBIk8bhJt4pOffTxnIDp9rdSyNNkikMcE2CXJgF06M5I57FNrzYdXemWiY1A+TkP0RT++msF1x+u+zDFKE0Ded+byScyV96u1mIaGjC4wnWfb6RvPbIA+FjRIVuNMxyNpTrERK6kCDPLyr8XvnCn0BuibMCCK+2pkjBZOsv3ULJNGWnqaK6I/Td3SAOJl0OP64do4OeVqm7qc//osEHVyJY1C8POixrQGRJf6FQi+lt9H0tP8F3V+F+WaXgovn0S4kfawxM2eXZmOCbgTv9R/o0JJ2zOX0TgehuPncpinR8CUTfEmvqfM9LSock0fEDpzWwAChILMaNK0xuVxeFV/nSQ2MB8bxePB6+6f8sQPGpp8i1+o9zbij+QvmK4ptYQ0uSUcHXYTsVqvmDWXqrRgSUtB8sovJiC2DLImjDYr7MJURoSk2pxGpM6Re9GQnnI8nesbY4neey4W06p0RkV1w9ARZrdk0Nri7OllqvUqSyw60pOtTcY6uuE9VmjVN8nXDOmqd0TNticRsTCOZQ3v36em2ly60WfXYBObBUFE7SaGfV05nG8XSssa6srMowF5+N6jiSAyYoNuDkGMtaKSp2BRCviM2/OWG71Pg0pU10lCA3rLZG2l8CFl0mADdZGbswB6OUj8Nq6KmoOXigvbw/Uf3JH9Q0ACVohmP1qudZ/UFdL1tj9yCvkphAAixMHtTfNtoCSg5tZdzlpilmzBm/aaZKy3cfRqjYWQPMx4UJ+d2Tpcbd7/iGfmDtGx0+Os7RajFiuos5MmL8a0yqwImA/8Wce8jMVtyh+bh8Fp5QzzvY+mBrhiHTVJlSV7fQAXpqtM17EJTM0mXcen0ie6i9M0gEiMJ/Ua+ygPnWv8yRy9QyKoV744SqJP1COLDbn6jz832u1aDT2gAg1hrvotihcXRlGH2HYzLrhwBk5vzsswGqNzzbnigtt6WMLgrHGQqucO76+cUoJ9Pe58fVF9x1rZzUuyf1ub+A3UxW9kxiMMBwdH8AhAHTKlf7J6w2DnJF8VRA0UTs4gTQr5dPHI5h4UsKD4jZWKxq6r33JVzMqyjWN0Wz6cH/kNhKC+CDLUb9ZrA5OZPa66Wwv6hOXn0EgIBqzuNDMdlYHVK8dBdXe62vA9RrR7+EBAmzNLZJyPPNwUf0GwqYgk56f91iY1rkz3McGX4oW8HC9tQB2ksvBwv4Lx4X8WGYOnhOTsCKZgWQBHWWRuURvGfbZcaFePCO+ZEj8rjJOfkkhxBWTo6LUKP4HXu8WU4HHYyKEEmkQoAinS0xwQ6TGdcQnlUyyQBLJdamiWipnMY9hCmY+Dl2+csMtUWCqapENFYvBaQLn80SHbTVunKoSg9OJnto4nx512RlVh8pLiUTIljsVwAaVv/oF09uugF+QldFN2ay9+svJD1k X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 76069f90-58bb-4830-6fb6-08db15182429 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2023 21:02:43.4899 (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: jGtJVhHjscg3IvlLc7jkio2tghY4oYyehjp+ozgwl8/tw6/Km0iur/hKCeRgxVfd X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR12MB5395 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org This can now be covered since we have a full struct device. Signed-off-by: Jason Gunthorpe --- tools/testing/selftests/iommu/iommufd.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index 0c4bbd4079a425..69cb79d6a0711d 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -187,6 +187,7 @@ FIXTURE(iommufd_ioas) int fd; uint32_t ioas_id; uint32_t domain_id; + uint32_t hwpt_id; uint64_t base_iova; }; @@ -212,7 +213,7 @@ FIXTURE_SETUP(iommufd_ioas) } for (i = 0; i != variant->mock_domains; i++) { - test_cmd_mock_domain(self->ioas_id, NULL, &self->domain_id); + test_cmd_mock_domain(self->ioas_id, &self->domain_id, &self->hwpt_id); self->base_iova = MOCK_APERTURE_START; } } @@ -259,6 +260,16 @@ TEST_F(iommufd_ioas, ioas_destroy) } } +TEST_F(iommufd_ioas, hwpt_attach) +{ + /* Create a device attached directly to a hwpt */ + if (self->domain_id) { + test_cmd_mock_domain(self->hwpt_id, NULL, NULL); + } else { + test_err_mock_domain(ENOENT, self->hwpt_id, NULL, NULL); + } +} + TEST_F(iommufd_ioas, ioas_area_destroy) { /* Adding an area does not change ability to destroy */