From patchwork Sat Jan 28 21:18:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 49908 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1504180wrn; Sat, 28 Jan 2023 13:19:31 -0800 (PST) X-Google-Smtp-Source: AMrXdXuy1LmWM8wyr902YTniwrosoCLpoj0RVQcqk/bGecKYCPDqeTF9Thtvbdy8LWdTVJpN4i9R X-Received: by 2002:a17:90b:1d0f:b0:229:f975:9c90 with SMTP id on15-20020a17090b1d0f00b00229f9759c90mr34503415pjb.5.1674940771449; Sat, 28 Jan 2023 13:19:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674940771; cv=pass; d=google.com; s=arc-20160816; b=gvFf3p+0zuTTLhYyezaKVW1Zmpty4BC7vBPAo1DfaoyHMfNve8a5enE6ZUWmZF5uZA hoFBENCYIrDVl/HNEwlwKt+mgFdaEsEDcAcoTBhKw9NMP7kiNxiO3mcCuSbVpo4AxxHn avWprTB6YGDZNS96nc+mGdZvJViVjZQhBxa6H7RdWoQ1R1GwTkAXXMp31X8mEFroPJVW V/pj4LmQwl/2BIXvxv8iaAfDwpThQhhXkd5p4PD2qC7ucrANFVGfA6JUSoyeBPysI4rZ Ep4cPxbKZLtaNs8dFKUPmlJdD6BuauHnImx5Bq6aGQL42a3XWdNoTp1QB/OB1diM7peU h9zw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5aWMGcXQVNAN9lZL32+cKA11taZBiVCr7fCTxeRXQ+0=; b=vQW/ZvUcpN4P672q3uB4McNi1wmtEB4iBZ7jn+wbRTc6WPCAnlx02iXyleA8X0RY+m YhvfuYgWsoBeLevShtacvIDuaNzAR+OGyNqH5kJ4hh9RLGdxgWotUddtOx+biB8Y1pEm 1baEaKkVDvhGvSIcAipt64LM/yQLwBDnwWkOhytLl2hHBuQ9B0QyX+GA0d+E8ns/3I+b vq7kzZvbPcO2mKObKCkxvkPyf+XpEsgwVbOSzFM5vMlWOsw+TglSrxqZTJyJ3rFUdtOC OXUQXCD2rGeFZVYYgc6Ur5vcC51aqBKRAmrkG0WIxJdqlnJJhn1SSsDQvl8swZMWo/E2 CxmQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=D8GzNgDj; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a22-20020a17090ad81600b0022c0912029esi8758298pjv.30.2023.01.28.13.19.18; Sat, 28 Jan 2023 13:19:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=D8GzNgDj; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233821AbjA1VSm (ORCPT + 99 others); Sat, 28 Jan 2023 16:18:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231236AbjA1VSg (ORCPT ); Sat, 28 Jan 2023 16:18:36 -0500 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2048.outbound.protection.outlook.com [40.107.244.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38CE85594 for ; Sat, 28 Jan 2023 13:18:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V2uJoPscV4EakUceoZ5gQcvZKK8iYr/5axakidVaxr6nYTdsQCrOiJMp+rgymuayPMbB1KUlZb+YtGGb8W2h4BTfdDb6RugwmW4VwKIT0ri4eZIjv+t2O+6vJgplCeZbSOcrDGMuH6i6H7zjafHYusxHjOJ/0l7uVktj7mYCbNZJhwsmFy5QMb7z8GzFAroN6+lfQPC01HMHxNBjUo9e7h02eXp4hQryTLmLHKyxg9whQePQKkNd37FJvBnmDE+aC1q0qx9l2vdhvsw8wqhDxz0yBLYifvCy0ebR0noHq1kJcqDjKbwhkE/Zxj6hs8ohakLSGAAouTIwRayxXhKM1Q== 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=5aWMGcXQVNAN9lZL32+cKA11taZBiVCr7fCTxeRXQ+0=; b=g5/qZxSvFoEzciy5Cr0rSDnwC7t48dk2ILKkBbteA05U3tiqnmSyZijo05uaaypR/JLFfgk1rHITrPdUmFBdhW4ozNRC+c9MYPjuL38+tj6u3tqEDHekhWvWxnIDKdxunL3f8gYL+TK9P9MNIidu7Gz8B4UYjQPIzMhPJYbsr1VXluf+kVdvFIz8suViRON9VFLkpWP7q41QctrDtRLHfABFby6YrBLVljm4zgXABBEUekqK4I0Td5edYJQcuXGN/6988CDy8D9lFWF0TzWcgvMsCs0HZnc8HX8VaM+dne7gtbSd5bmZCSkgItdl4cj1J9hmWs9zNV8eN1weljxTHQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); 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=5aWMGcXQVNAN9lZL32+cKA11taZBiVCr7fCTxeRXQ+0=; b=D8GzNgDjH6wONJ78t1Lo7yV3SHgduBHMmfFerrozbiYTwcEualzbuXc9wZG7vlTrvzRoL57O8zeEaZNmFv1Qypr70tBjITlXyLLYnd8OckkY+6mg4U+LthzEe3fzKtRaB8ZgsYCwOBOFNCSdWd94eM63tikUQTkZLGj0X7bO5cC5g1BtSFh1j2ICmIOx8jF5K9TiNSsh3kueG6k0zuz+sjGtzbyHQ0Ahm+0qNk/aj0Npt33/6+S9ulL7RUC+GEwG6n3X4OfeCyK2bCROkfwEjiQicsyTAPqhMt/Fycxy8f639WlH5Wm6RX3UY0jmv1NxOWuZBKVEPIyp6NCMQ7rhNw== Received: from DM6PR21CA0029.namprd21.prod.outlook.com (2603:10b6:5:174::39) by DS7PR12MB6005.namprd12.prod.outlook.com (2603:10b6:8:7c::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.28; Sat, 28 Jan 2023 21:18:32 +0000 Received: from DS1PEPF0000E639.namprd02.prod.outlook.com (2603:10b6:5:174:cafe::7c) by DM6PR21CA0029.outlook.office365.com (2603:10b6:5:174::39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.0 via Frontend Transport; Sat, 28 Jan 2023 21:18:32 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS1PEPF0000E639.mail.protection.outlook.com (10.167.17.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.17 via Frontend Transport; Sat, 28 Jan 2023 21:18:32 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Sat, 28 Jan 2023 13:18:25 -0800 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Sat, 28 Jan 2023 13:18:24 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Sat, 28 Jan 2023 13:18:24 -0800 From: Nicolin Chen To: , CC: , , Subject: [PATCH v2 1/3] iommufd: Add devices_users to track the hw_pagetable usage by device Date: Sat, 28 Jan 2023 13:18:09 -0800 Message-ID: X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E639:EE_|DS7PR12MB6005:EE_ X-MS-Office365-Filtering-Correlation-Id: 290459a8-780f-467a-d9aa-08db01753596 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YJ2zW8pBwY8qdZTSNWN3F2Hv1Ewj9S9IOc1Mvk6WvknCYwFk1lCy2ZbQoc+DOBJeKifx/dXBdHaiGY3PVz8L0AEQfxv1wSZruxk1vzp2A1cRdjWlWaivEl6kdGOgPYMpgcxUM0Hb5d7mJw+mrT938L8wiQV6In55HkzhqmRtw+PEDTv7OtML+V2eTRB5jEdenKBolSs+/tgwrzMbmNTMWialrocDxK39RM6pLpTYlbI/MMvtU1qSiBiPUmXdKkNPUrtZdFSodoGUMn4KQ3rHsfHj9MX6O5deCitUjEFWOYTmnICmg+QheXa4v7nSwGCmYfTCWtZMCTwt1TMC5k2fk8Azy/DVDkX3GIYYC5kZDMWx6RQis4vfX+ZjpSrtSQuozc48nu9rhMgDzj4HHBeHAeegpuSwEXDU1Om0MZkqe4gTcGyd7R44luFoaMlaf6yAn1Ltys4LaRnmRZy1HGraHMeYfubg6CBImAyUyNgTG9XFBltVpyOM+wVzOBhMI2T6GAgSv0RcHXkaS3TZaNAKkZD1m2RFMXx9cZFSa7LC+wfkx8dAqoTsnXfuDulOZ7RYr1sDfmOyZgYk2mYGCSjxKWGscT9LeKZf899inI8PG110PQemOaj6cXCat+Tk2aNsJYz6XTCX+sxiJOzFD2fQee3ze/AtqPxpcbe1L/pl2UNA2tyxjdLSzpwnJgpH5cHQqMqM18PobfjWWyJ77QtSHg== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230025)(4636009)(39860400002)(136003)(396003)(346002)(376002)(451199018)(36840700001)(40470700004)(46966006)(40460700003)(2906002)(5660300002)(54906003)(316002)(110136005)(478600001)(6666004)(7696005)(41300700001)(8936002)(8676002)(4326008)(70586007)(70206006)(40480700001)(82310400005)(186003)(36860700001)(82740400003)(7636003)(26005)(86362001)(336012)(83380400001)(47076005)(426003)(2616005)(36756003)(356005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2023 21:18:32.3374 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 290459a8-780f-467a-d9aa-08db01753596 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E639.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB6005 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756302694373833856?= X-GMAIL-MSGID: =?utf-8?q?1756302694373833856?= From: Yi Liu Currently, hw_pagetable tracks the attached devices using a device list. When attaching the first device to the kernel-managed hw_pagetable, it should be linked to IOAS. When detaching the last device from this hwpt, the link with IOAS should be removed too. And this first-or-last device check is done with list_empty(hwpt->devices). However, with a nested configuration, when a device is attached to the user-managed stage-1 hw_pagetable, it will be added to this user-managed hwpt's device list instead of the kernel-managed stage-2 hwpt's one. And this breaks the logic for a kernel-managed hw_pagetable link/disconnect to/from IOAS/IOPT. e.g. the stage-2 hw_pagetable would be linked to IOAS multiple times if multiple device is attached, but it will become empty as soon as one device detached. Add a devices_users in struct iommufd_hw_pagetable to track the users of hw_pagetable by the attached devices. Make this field as a pointer, only allocate for a stage-2 hw_pagetable. A stage-1 hw_pagetable should reuse the stage-2 hw_pagetable's devices_users, because when a device attaches to a stage-1 hw_pagetable, linking the stage-2 hwpt to the IOAS is still required. So, with a nested configuration, increase the devices_users on the stage-2 (parent) hwpt, no matter a device is attached to the stage-1 or the stage-2 hwpt. Adding this devices_users also reduces the dependency on the device list, so it helps the following patch to remove the device list completely. Signed-off-by: Yi Liu Co-developed-by: Nicolin Chen Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 8 +++++--- drivers/iommu/iommufd/hw_pagetable.c | 11 +++++++++++ drivers/iommu/iommufd/iommufd_private.h | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 9f3b9674d72e..208757c39c90 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -212,7 +212,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, hwpt->domain->ops->enforce_cache_coherency( hwpt->domain); if (!hwpt->enforce_cache_coherency) { - WARN_ON(list_empty(&hwpt->devices)); + WARN_ON(refcount_read(hwpt->devices_users) == 1); rc = -EINVAL; goto out_unlock; } @@ -236,7 +236,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, if (rc) goto out_iova; - if (list_empty(&hwpt->devices)) { + if (refcount_read(hwpt->devices_users) == 1) { rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain); if (rc) @@ -246,6 +246,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, idev->hwpt = hwpt; refcount_inc(&hwpt->obj.users); + refcount_inc(hwpt->devices_users); list_add(&idev->devices_item, &hwpt->devices); mutex_unlock(&hwpt->devices_lock); return 0; @@ -387,9 +388,10 @@ void iommufd_device_detach(struct iommufd_device *idev) mutex_lock(&hwpt->ioas->mutex); mutex_lock(&hwpt->devices_lock); + refcount_dec(hwpt->devices_users); list_del(&idev->devices_item); if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { - if (list_empty(&hwpt->devices)) { + if (refcount_read(hwpt->devices_users) == 1) { iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain); list_del(&hwpt->hwpt_item); diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 43d473989a06..910e759ffeac 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -16,6 +16,8 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) iommu_domain_free(hwpt->domain); refcount_dec(&hwpt->ioas->obj.users); mutex_destroy(&hwpt->devices_lock); + WARN_ON(!refcount_dec_if_one(hwpt->devices_users)); + kfree(hwpt->devices_users); } /** @@ -46,11 +48,20 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, INIT_LIST_HEAD(&hwpt->devices); INIT_LIST_HEAD(&hwpt->hwpt_item); mutex_init(&hwpt->devices_lock); + hwpt->devices_users = kzalloc(sizeof(*hwpt->devices_users), GFP_KERNEL); + if (!hwpt->devices_users) { + rc = -ENOMEM; + goto out_free_domain; + } + refcount_set(hwpt->devices_users, 1); + /* Pairs with iommufd_hw_pagetable_destroy() */ refcount_inc(&ioas->obj.users); hwpt->ioas = ioas; return hwpt; +out_free_domain: + iommu_domain_free(hwpt->domain); out_abort: iommufd_object_abort(ictx, &hwpt->obj); return ERR_PTR(rc); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index 222e86591f8a..f128d77fb076 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -247,6 +247,7 @@ struct iommufd_hw_pagetable { /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; struct mutex devices_lock; + refcount_t *devices_users; struct list_head devices; }; From patchwork Sat Jan 28 21:18:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 49909 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1504258wrn; Sat, 28 Jan 2023 13:19:49 -0800 (PST) X-Google-Smtp-Source: AK7set+PFcYUJjSfFdthy/YGhQIxMLsWMHUQjwkO3Zo8ULvh5iWfrTWED8TAzFB+WmZ6S0yiSvT1 X-Received: by 2002:a17:903:2013:b0:196:37db:b4b7 with SMTP id s19-20020a170903201300b0019637dbb4b7mr8421644pla.62.1674940789050; Sat, 28 Jan 2023 13:19:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674940789; cv=pass; d=google.com; s=arc-20160816; b=Hp/OOq8gWSo+r6TyF/RcV8418aFTq1Y4Y4NWz2dBiqbywaQFDUF0vWAFlPs2uj6x6Y fEhCJMKI7SdoTDUk8ksnwQjrkmCvED5Wq2ObuEfBKVVzjvCFgM6gJIOYYjZhUGJh4m63 F8tchL39ZxmVjM+xZic3fwoCNcZMO0gsCgqR84XYvPNumqxxdtJsrA/OIWaLBNxDz1cM VIAQ8Ot5j/DLFROwuPYzIaGfaqZbX+eK1U8wmrWbEcrfX399PPXAHYwK+D192HuuztFr OFWoOgo4hvjF2Xytuo/ZoMkoo9txBMdVZcMCPoRP+tTvGnopcgda4tB6vtguN3SbTeI+ STPg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=iIipKpqrcQaTmexwsgwIspS2mdrDvb9370ZdA/njTBU=; b=IeMt93lELT536gNzbaHuAioYdBH5eq2Ad6uKJa4QVejIfy1vEdDFLLNquM0F6VcG/5 bZZYbpDNPY8H/YpdotYe1r1M5jfNXcinl1GhfZMx9/BsOlTcIvff9FN8YoneNffifynt GU5/sK0UW9OMxrhZVbe7zl9961u4RvJfa1kgFIQeo7O9V0aQ8krk9Sd1C20CDNxRliTd NPGYO2Bc25dU9rtOhojYNWjETBOQzL7DDS+w8X78uwitHyDJmLPkZUdZb2m0LsIt2dpf 4vRQY4jTCh8QUMX92+HcfS/QwFYFF8LLf3BDEH9IRDQ5aucBA4ft0w5AJvFZqofceAjP lPPg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=W17xJuv0; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h1-20020a170902f54100b001929fa56a48si9658382plf.394.2023.01.28.13.19.36; Sat, 28 Jan 2023 13:19:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=W17xJuv0; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233570AbjA1VSy (ORCPT + 99 others); Sat, 28 Jan 2023 16:18:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231586AbjA1VSi (ORCPT ); Sat, 28 Jan 2023 16:18:38 -0500 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2055.outbound.protection.outlook.com [40.107.220.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90E253C37 for ; Sat, 28 Jan 2023 13:18:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ezel19QnPKE8dDF5KMql0+HhsVpQanqFEjmcWV2vjZLYoSISlcX/uaMmKhtjQ5P4LThbla/bOgrUdzy+keKqTX+vJqGoj9ig8hGLXHpIZKNCOw7adtzU1HYYiumr/YM0U5qhpAxIF6TZhWlY4MFobayalpJf7R6Nff6kcu+Kia10mLRm4FglAq7tTdxvvRBRoRNy8397gahlUsMkwfzOny9AvxQve6N43Hr8AK7OIHW56H/o30I3wjiXQUg4u2HiYUcCNJcXpvEpj3k9/xce0C3MaloeQtXMMVWpHGv9grr/nl2GD7EcPhSONPV8nRxiINfyDUM9h5YX5in/3YsklA== 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=iIipKpqrcQaTmexwsgwIspS2mdrDvb9370ZdA/njTBU=; b=nobznFNhaM9Bg+JFjOE0c1qMg2DrSRA1Vt84Ss7ShLZ+97oyLiPo/TmELj0hUwk+ZiFlfEe6K4YR+GHMWlTlhEDQt5Je3fLd6DZ3nkAdgxtWGYWkbpVJC/aJPuCxkSrNNJrRtmViBbNozQPSfFnZbwZdfAgxeB8er+xIihTLbVAqMwqqLw95jMCIX1gielNF11y1CHBLUWqJ0vDrM9zNXf9Eqx2F1hTHl1f18gaH2Yn7Pd76EJwVIWqnLlphIamgkvWeInMkWLBFZJxz5Bpg+ITH/wHols15mefkGE6OI/6g4NxPbxoKvgXXY6ItSJ8iWTKyKfNbszJdP5JO7KhtJw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); 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=iIipKpqrcQaTmexwsgwIspS2mdrDvb9370ZdA/njTBU=; b=W17xJuv0fMvHOSDXNS6qspmpXwN+O0HbQ5Gg6bG8MlgvjnST1RTlDuQJDlUVHWxlnhh6MNoP85K6EcPT2jcPb3TaBQ+okDHJbVdTwAV6T048vvY3JKAQ45TW20MhRBjUox4Lc/lu3JrHMMv8RwyRE4RVCB41HISd+15Lr/AWQMDaGhkU1r66MNxULapDiWVaolidZBWw7KGmqWWZFlDIXeKnrPwQYh1ktWM6nOdH3lOGgUYcoDl6/MIPmp74UsG6Z8SsdavyedyzmWJCJut8Zh436Usj3g6NyCnaZssKsHLhrT5HNzBUETatsG08rFBTSIssnWhQ3UWIVuMF2QBHew== Received: from DS7PR03CA0127.namprd03.prod.outlook.com (2603:10b6:5:3b4::12) by SN7PR12MB7835.namprd12.prod.outlook.com (2603:10b6:806:328::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.21; Sat, 28 Jan 2023 21:18:36 +0000 Received: from DS1PEPF0000E636.namprd02.prod.outlook.com (2603:10b6:5:3b4:cafe::ea) by DS7PR03CA0127.outlook.office365.com (2603:10b6:5:3b4::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.30 via Frontend Transport; Sat, 28 Jan 2023 21:18:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS1PEPF0000E636.mail.protection.outlook.com (10.167.17.68) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.17 via Frontend Transport; Sat, 28 Jan 2023 21:18:35 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Sat, 28 Jan 2023 13:18:26 -0800 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Sat, 28 Jan 2023 13:18:25 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Sat, 28 Jan 2023 13:18:25 -0800 From: Nicolin Chen To: , CC: , , Subject: [PATCH v2 2/3] iommufd/device: Make hwpt_list list_add/del symmetric Date: Sat, 28 Jan 2023 13:18:10 -0800 Message-ID: <6f388b1f20622957518ec5a9ddc7f0037e7671c4.1674939002.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0000E636:EE_|SN7PR12MB7835:EE_ X-MS-Office365-Filtering-Correlation-Id: 2f34f023-f9bf-4dbd-5295-08db0175377b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rYOcQ2oesGF/62gSEkCity2QT2wMBquzNUGzMs86AvD/KBLjHQYIjD2R30O54VYzHVXa7JGzoFqoz/YPlb5lw+yoX4JFlnfLzC/MnIP2sWiRSO0cq2rQxsTZyAkfCB+2k5q03EKs2Id8mYwcSi58u7ygIJoLHFhRR4HCp9C/+wNBdgkOfMFWTH4DKRhC4eXXYY4sbP6ji4ztbD+RBsLgvu1WDKj7cQzA3wGBoOciYTHIBcr3fSeKPGwV6JKB4H8jf6kHnqXjiBzete6B7PvVwkt057BFWOg02WX4Od2LFAiqna5TYlnjrJLDMAiKr6WAIlLpznjIr4fji1aMedaZvkmnoBAsYpZbN+q46q1tuAhWwzurC5HmA9xZXAyT+ao6OoB+PWUA9ZSow54g0iXSKk4OnsIs48cryHxPK/k++U5TPGDniz10MhNUz9voCeD8XPH2oSZPuz8jVbhMuooat0hXdaQh93MUdTBMZA3FNX7sAMa3z44Mhldi2tK4xu7lfHVNwXP7wpOKnQSvQzdRKBDvRDxZHVEhXT4nUlMZMX4zKM/XNd8VP0q2rQYjtmtHsNyNcqRQ8RkVhNfPZEPbYzig6e8tvgGj1/9eixK41/rl3evI1aRQHeOUOjWXKQRRAtCRXnKSMTp6KLQSCPV/ZZOcKjEp047KwRC4w7iobVNaSt/Mj8JVvNVdpD7clFObtb7w0pgZHZTS25C2QdoE9w== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230025)(4636009)(346002)(39860400002)(396003)(136003)(376002)(451199018)(40470700004)(36840700001)(46966006)(70586007)(7696005)(82310400005)(26005)(186003)(86362001)(478600001)(6666004)(36860700001)(40460700003)(316002)(110136005)(54906003)(40480700001)(8676002)(4326008)(70206006)(356005)(336012)(41300700001)(2616005)(47076005)(426003)(8936002)(36756003)(83380400001)(82740400003)(5660300002)(2906002)(7636003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2023 21:18:35.5125 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2f34f023-f9bf-4dbd-5295-08db0175377b X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0000E636.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7835 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756302712592698670?= X-GMAIL-MSGID: =?utf-8?q?1756302712592698670?= Since the list_del() of hwpt_item is done in iommufd_device_detach(), move its list_add_tail() to a similar place in iommufd_device_do_attach(). Also move and place the mutex outside the iommufd_device_auto_get_domain() and iommufd_device_do_attach() calls, to serialize attach/detach routines. This adds an additional locking protection so that the following patch can safely remove devices_lock. Co-developed-by: Yi Liu Signed-off-by: Yi Liu Signed-off-by: Nicolin Chen Reviewed-by: Kevin Tian --- drivers/iommu/iommufd/device.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 208757c39c90..9375fcac884c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -198,6 +198,8 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, phys_addr_t sw_msi_start = PHYS_ADDR_MAX; int rc; + lockdep_assert_held(&hwpt->ioas->mutex); + mutex_lock(&hwpt->devices_lock); /* @@ -241,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); } } @@ -271,12 +274,13 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, struct iommufd_hw_pagetable *hwpt; int rc; + lockdep_assert_held(&ioas->mutex); + /* * There is no differentiation when domains are allocated, so any domain * that is willing to attach to the device is interchangeable with any * other. */ - mutex_lock(&ioas->mutex); list_for_each_entry(hwpt, &ioas->hwpt_list, hwpt_item) { if (!hwpt->auto_domain) continue; @@ -290,29 +294,23 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev, */ if (rc == -EINVAL) continue; - goto out_unlock; + return rc; } hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev->dev); - if (IS_ERR(hwpt)) { - rc = PTR_ERR(hwpt); - goto out_unlock; - } + if (IS_ERR(hwpt)) + return PTR_ERR(hwpt); hwpt->auto_domain = true; 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); return 0; out_abort: iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj); -out_unlock: - mutex_unlock(&ioas->mutex); return rc; } @@ -342,20 +340,20 @@ int iommufd_device_attach(struct iommufd_device *idev, u32 *pt_id) struct iommufd_hw_pagetable *hwpt = container_of(pt_obj, struct iommufd_hw_pagetable, obj); + mutex_lock(&hwpt->ioas->mutex); rc = iommufd_device_do_attach(idev, hwpt); + mutex_unlock(&hwpt->ioas->mutex); if (rc) goto out_put_pt_obj; - - mutex_lock(&hwpt->ioas->mutex); - list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list); - mutex_unlock(&hwpt->ioas->mutex); break; } case IOMMUFD_OBJ_IOAS: { struct iommufd_ioas *ioas = container_of(pt_obj, struct iommufd_ioas, obj); + mutex_lock(&ioas->mutex); rc = iommufd_device_auto_get_domain(idev, ioas); + mutex_unlock(&ioas->mutex); if (rc) goto out_put_pt_obj; break; From patchwork Sat Jan 28 21:18:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 49910 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1504422wrn; Sat, 28 Jan 2023 13:20:27 -0800 (PST) X-Google-Smtp-Source: AMrXdXssM+Cgxoe9Hyx76I2bpR5x0h7nGiuPJjHVBglN3dh1V6UiT1LcUHZcUIm/qu2VLRA4uQDE X-Received: by 2002:a05:6a20:9c8b:b0:b8:bc13:c838 with SMTP id mj11-20020a056a209c8b00b000b8bc13c838mr46874380pzb.53.1674940827523; Sat, 28 Jan 2023 13:20:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674940827; cv=pass; d=google.com; s=arc-20160816; b=O837Po8H6x3CI9s5pnIoZm2F25ETygB5rTKnQ1bV5avYoKMX8mR8PHVgRU3X9f3fb+ MftE5BQCuUUN1vSM/SlK1igY2EWtAT3Ikj0x0tgfxbS9MtzmVEopIr9B/6/Jw9Rjlscc FAWncmudsnexMVGH8bCgA9wn/OkZKZbrJOP1idNZsB/w88tCEqyVgbRXhFefNdNDqfHC fT7IX4vBP3a6Zd2xSkGyouAk/1ADCa27qlI4SxbisY5RD8q5dEQcaU8/HjyBtNDFIKWl qPjcfdmZ388hEF++afLwR5f2CJ6JNk5+fIJ01kOYH6ixrcdTAaBgmdDW1fsXVF545f4e Rzzg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Elr8tRH0Mk5pSvopf14vfXOYoAvYzfwkw58ucbxy0/s=; b=x3gvDWAmus8QHDJZ2GM0aPjmyxdQl3IKHAElnt7BtX/oXY7wUsYl4a5UCJUJ0WpKO9 0bTJYJ4gSm6tj/FMYjEgZx9rNd8y3FQzjZyHFBsS7c0arXQf63AEcSrI7bb8eXlfRbBY OETGPASjhUJzs5TPjnKNE7gBbanWMKupiAQudSROlyGVhNGh5GJXJlrwf7T/SjdqEX40 oOOhd2T1xp3xyW4WW2iR7SoKuWJ8KOlcA3Uu3klhPbpe2O+rSgl8Jo7v8mrWnGgviCNN KfeFW22M5tLvVhYss0n6be1Fn7GicFT9YcpVr/ARFOZuEthysOWHPohkUiv+wT78KNkZ c/Yg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=dBX4Dp9i; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y19-20020a170902cad300b00186aad06b52si8036588pld.290.2023.01.28.13.20.15; Sat, 28 Jan 2023 13:20:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=dBX4Dp9i; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234556AbjA1VS7 (ORCPT + 99 others); Sat, 28 Jan 2023 16:18:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233582AbjA1VSk (ORCPT ); Sat, 28 Jan 2023 16:18:40 -0500 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2047.outbound.protection.outlook.com [40.107.220.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 445E16EA9 for ; Sat, 28 Jan 2023 13:18:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hVyPwMwxFBGvyrsjujhLW+a1FTntg+g/2mQL0fyAW1AewOxE4JjTGw9X3eoJjxUdxX5u34Ftvqge2LkQMeOhY8iMRoUp+9RdvYektg8RMgcWbn4BTMPXWe1v1wrb1HyOSIZqmGINvzLC6FbW6MEt+Cb6EDtkLWpOlHFLXd6ONpL+ODMInRhe/jdYMbeqmuyfdhBDNVlQAKc/68TP9LHU2Zkhg73fNq3LG2ncZp1maGvxx+Em3Z/Rwnc+RCB6cwxE0q+VzatkdrZ92qK0rmA6jYXgd7fhomMaKNIOPDfnIbHTSZCN8BtO1Q+QEvRdFW7K1e34n5lHpVAxKjLaGWDtIg== 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=Elr8tRH0Mk5pSvopf14vfXOYoAvYzfwkw58ucbxy0/s=; b=nGZjFaQkDturyBDiwEuuGMPxx34XvNOBWHyvVgCsdleDfsMrvOeKZbY2XndIcLNxlCUjhG0/ZOXm8It0Y4dxlFyGJvy0K/xfVvpGQH1cRIfIL6t5ZE8Pbu2wgxz+WtjVzlChIN4iSR9meQg+1E633pXe43XkkjYLHBFeUQ+wb2xYvRWxiSYk+33l+Hfp7tyLFd5Wafm1LBd74KTiHW6zhyn+FYy9Rx4Gn4gAWgVFysvg/rSuy9157ndyPuM6vzBzK8MYWiC9vPV2Trkd1z5Dt+fCOi+AwS1QnsM8cJCOKaWb5GHb05DIu1WOjke8A44BvrNiw8nm1GkbdZbmMLXBGg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); 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=Elr8tRH0Mk5pSvopf14vfXOYoAvYzfwkw58ucbxy0/s=; b=dBX4Dp9iaARNHCP8UBq0z9HyNo9GQ+rznAZhmF2ogrR8yE1aphSW1BiGnelkIjslcgtrl/SAgdIGyc1OrB3K24EVopPrvW/7U8IMh3nASJ+ln9kfWfG8qic3FC9KkO9sMXkkGxD+9tjjrZSTEsIEfSZLbCw5zBn5Fnq0Vfw8EAadtceP9vd/LguON8HX/2Bw0IgQm7nzIwCaNsbRy9xiODs1/XOoBvN/emQ+f2SIqHRxjoFxKw9H3YHsoiYTETVMuoU3+7gVO8rrrcx5gQ8hs20hlVq2zM0FmyJdfeSveE00g0yzYHaB+a1xTjR8ilLjik0YdwKCrpIRZ7odl/qqwQ== Received: from MW4PR04CA0381.namprd04.prod.outlook.com (2603:10b6:303:81::26) by MW3PR12MB4524.namprd12.prod.outlook.com (2603:10b6:303:2d::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.30; Sat, 28 Jan 2023 21:18:37 +0000 Received: from CO1NAM11FT061.eop-nam11.prod.protection.outlook.com (2603:10b6:303:81:cafe::8c) by MW4PR04CA0381.outlook.office365.com (2603:10b6:303:81::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.25 via Frontend Transport; Sat, 28 Jan 2023 21:18:37 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT061.mail.protection.outlook.com (10.13.175.200) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.22 via Frontend Transport; Sat, 28 Jan 2023 21:18:37 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Sat, 28 Jan 2023 13:18:26 -0800 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Sat, 28 Jan 2023 13:18:26 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Sat, 28 Jan 2023 13:18:26 -0800 From: Nicolin Chen To: , CC: , , Subject: [PATCH v2 3/3] iommufd/device: Change iommufd_hw_pagetable_has_group to device centric Date: Sat, 28 Jan 2023 13:18:11 -0800 Message-ID: <002911839dd30990d5e3135f8a0f8d41f14e856b.1674939002.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT061:EE_|MW3PR12MB4524:EE_ X-MS-Office365-Filtering-Correlation-Id: b5e73893-a083-49bd-e2e1-08db017538a7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xR75AOUJhTCk22XmsqJw9qOBriRniXiTTTiE2Ex7Y4jqkF4wvhyIzEzRwmVgBNR8LYlYOPmlnuFnLg5S/X7QEI5kF3eSj1yAT4CmS5Ba2AbL4CTU+ZD9MVw37BVRzf2QLyBwrZsrFTKrterTvi+8GJPwFA2yqGAxPdMIMM17dV8kDTwpWsQ0OM6FCe978Zm7uGp3UP+4rTm8LEbSQ10lKsoYZJ1eUy5u7epacAoFt40qASRJFGae86zcCd+ftM8aY1DYX6cQga8BZ+d7Jj8PaG7ZoB2wJ4ax2uLp1RmbUbzjY8NLoG+/Xlgec2QqX/nzQY+rAqJ1Qcr/g8K+0CbHltwkUcDNAvU/Dup9lK+Dyk2z4DFUW2DdZosM+rfGe+gTu1BTEkIH00exQnH06ug5R50+ymNfkIC/Y1oEjoy4bYAjHEOXtNrdneQzc2Yri0RIPseRd0hgflE5hKit8Uz0ZxXOhgLiLrJjRpDm4WerHXYlRiNHnmLlmGoSPvHJLWS0Yb+0IE5pPP2hh4eZeZJHodwuQOqBH+Pzo2E/5LILLdNS2YTxmscIMfKM8M76luSY9yzk+zSZM/DwnbvEqPjt13lfiLwGIIqKAoNgd/xcXsvJEJ6yx7RJXooLMBUyvcID7q79n+OEqJ4qm1Sz67xNLn8DQrH9YNfNciy+pZ6cdzkN2GHtgjOO8kE9prNyGhefAvm/JXd5e/zyPfjt01EQeQ== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230025)(4636009)(39860400002)(346002)(376002)(136003)(396003)(451199018)(46966006)(40470700004)(36840700001)(2616005)(7636003)(82740400003)(40460700003)(36756003)(186003)(356005)(26005)(40480700001)(7696005)(6666004)(478600001)(86362001)(2906002)(82310400005)(316002)(110136005)(54906003)(5660300002)(70206006)(47076005)(36860700001)(41300700001)(70586007)(4326008)(426003)(83380400001)(8676002)(8936002)(336012);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2023 21:18:37.3572 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b5e73893-a083-49bd-e2e1-08db017538a7 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT061.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4524 X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FORGED_SPF_HELO, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756302752959835019?= X-GMAIL-MSGID: =?utf-8?q?1756302752959835019?= The iommufd_hw_pagetable_has_group() is a little hack to check whether a hw_pagetable has an idev->group attached. This isn't device-centric firstly, but also requires the hw_pagetable to maintain a device list with a devices_lock, which gets overcomplicated among the routines for different use cases, upcoming nested hwpts especially. Since we can compare the iommu_group->domain and the hwpt->domain to serve the same purpose while being device centric, change it and drop the device list with the devices_lock accordingly. Note that, in the detach routine, it previously checked !has_group as there was a list_del on the device list. But now, the device is still being attached to the hwpt, so the if logic gets inverted. Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 40 ++++++++----------------- drivers/iommu/iommufd/hw_pagetable.c | 5 ---- drivers/iommu/iommufd/iommufd_private.h | 2 -- 3 files changed, 12 insertions(+), 35 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 9375fcac884c..f582e59cc51c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -24,8 +24,6 @@ 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; @@ -181,15 +179,15 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev, return 0; } -static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt, - struct iommu_group *group) +static bool iommufd_hw_pagetable_has_device(struct iommufd_hw_pagetable *hwpt, + struct device *dev) { - struct iommufd_device *cur_dev; - - list_for_each_entry(cur_dev, &hwpt->devices, devices_item) - if (cur_dev->group == group) - return true; - return false; + /* + * iommu_get_domain_for_dev() returns an iommu_group->domain ptr, if it + * is the same domain as the hwpt->domain, it means that this hwpt has + * the iommu_group/device. + */ + return hwpt->domain == iommu_get_domain_for_dev(dev); } static int iommufd_device_do_attach(struct iommufd_device *idev, @@ -200,8 +198,6 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, lockdep_assert_held(&hwpt->ioas->mutex); - mutex_lock(&hwpt->devices_lock); - /* * Try to upgrade the domain we have, it is an iommu driver bug to * report IOMMU_CAP_ENFORCE_CACHE_COHERENCY but fail @@ -215,25 +211,20 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, hwpt->domain); if (!hwpt->enforce_cache_coherency) { WARN_ON(refcount_read(hwpt->devices_users) == 1); - 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; - /* - * FIXME: Hack around missing a device-centric iommu api, only attach to - * the group once for the first device that is in the group. - */ - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { + if (!iommufd_hw_pagetable_has_device(hwpt, idev->dev)) { rc = iommu_attach_group(hwpt->domain, idev->group); if (rc) goto out_iova; @@ -250,16 +241,12 @@ static int iommufd_device_do_attach(struct iommufd_device *idev, idev->hwpt = hwpt; refcount_inc(&hwpt->obj.users); refcount_inc(hwpt->devices_users); - 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; } @@ -385,10 +372,8 @@ 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); refcount_dec(hwpt->devices_users); - list_del(&idev->devices_item); - if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) { + if (iommufd_hw_pagetable_has_device(hwpt, idev->dev)) { if (refcount_read(hwpt->devices_users) == 1) { iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain); @@ -397,7 +382,6 @@ void iommufd_device_detach(struct iommufd_device *idev) iommu_detach_group(hwpt->domain, idev->group); } iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev); - mutex_unlock(&hwpt->devices_lock); mutex_unlock(&hwpt->ioas->mutex); if (hwpt->auto_domain) diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c index 910e759ffeac..868a126ff37d 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -11,11 +11,8 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj) struct iommufd_hw_pagetable *hwpt = container_of(obj, struct iommufd_hw_pagetable, obj); - WARN_ON(!list_empty(&hwpt->devices)); - iommu_domain_free(hwpt->domain); refcount_dec(&hwpt->ioas->obj.users); - mutex_destroy(&hwpt->devices_lock); WARN_ON(!refcount_dec_if_one(hwpt->devices_users)); kfree(hwpt->devices_users); } @@ -45,9 +42,7 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas, goto out_abort; } - INIT_LIST_HEAD(&hwpt->devices); INIT_LIST_HEAD(&hwpt->hwpt_item); - mutex_init(&hwpt->devices_lock); hwpt->devices_users = kzalloc(sizeof(*hwpt->devices_users), GFP_KERNEL); if (!hwpt->devices_users) { rc = -ENOMEM; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index f128d77fb076..1c8e59b37f46 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -246,9 +246,7 @@ struct iommufd_hw_pagetable { bool msi_cookie : 1; /* Head at iommufd_ioas::hwpt_list */ struct list_head hwpt_item; - struct mutex devices_lock; refcount_t *devices_users; - struct list_head devices; }; struct iommufd_hw_pagetable *