From patchwork Sat Jan 28 02:29:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 49772 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1156500wrn; Fri, 27 Jan 2023 18:40:34 -0800 (PST) X-Google-Smtp-Source: AK7set8mB7XtUpGLCX6xIYuHbsbokH1LB+SXiHm9s2GmhB0MB70RtzpJddRcquD4lMGIcRtJcfvx X-Received: by 2002:a05:6a20:42a7:b0:af:6d54:239a with SMTP id o39-20020a056a2042a700b000af6d54239amr955879pzj.38.1674873634330; Fri, 27 Jan 2023 18:40:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674873634; cv=pass; d=google.com; s=arc-20160816; b=x5DHtlnB4qVgwMO19xpcsW+++S0YauI3Muz0UR7+9srPXceWKKhcFWsWujcHWg+c48 VcuDp+P/EVtTS7h7LxAQFuCmKWMWwJVkf9qHYzQdKmth17Aa8onHotSSzC2gda78Xzta L0FY8N2dEzbYCWGwV8432MKP/pdo0LspzvLQ0j9ICN7wXB1c4F2iQv993mSjrk554eEW f8hU/jUE4ZYwculz8qT2Zw6sfmN5zEDjj7yS5S0Fg8yBJQWjgDnk35CjBzAvcIGsPiif ZNUsC2rJdVZJ988cmJMUYIS7H0DCzUODUcP+l/ZXovgSXp9c5YGmsv/O2TBgz0d9fbV+ 2bjg== 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=ex/kze9Jp9Uvxd+55doVDljZlqtkg6Vf1GWKXP0PvfX9Lhr3tLH2IIm9Zy0RIVXkUT dni0DzmrcBUQpDJuyVbzDHnijkMbzF01ZcNz1sOIuEL6qqIY7KSzJHq65wK8tcTYwbd7 ppebWD6j5jIexWl9EJqVdTsoHGE3TRbAu+E38EY0FqhHY8X7hPG4dgtAoRtNczSm58+P 80tWrnP2A/XEoR2vck/6hJN4cHCh7gD54nOoz5jlYv2EpfWifCAQPhWASXVM7wVRlZv/ YdCZdhV4U9GH3yQsUGZFBisGy+z1FjLFdomMVM9ogsw3TCMRldDlZc8hOxNkZp5p36p1 GHEw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=lpuYtYkM; 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 bv11-20020a17090af18b00b002294419acd5si6654560pjb.85.2023.01.27.18.40.21; Fri, 27 Jan 2023 18:40:34 -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=lpuYtYkM; 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 S232670AbjA1CaS (ORCPT + 99 others); Fri, 27 Jan 2023 21:30:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232036AbjA1CaK (ORCPT ); Fri, 27 Jan 2023 21:30:10 -0500 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2073.outbound.protection.outlook.com [40.107.92.73]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C5C88CAAB for ; Fri, 27 Jan 2023 18:30:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I6tKitHNous9gkS+mWidlJEQDL9iOPj9yDOOs5qxSzfuf6OPZFES/2JThzRrhsSP6RccOIrcKasvlGqKKgQz5oEKYbiaqNxgJ1vqyKnPBBDeR4VlBAV7EJWNl51vhlF8PwUMC6MrIX/cM3iQ1aN5666IQDXCbXiyxbWVCkF2+HOOK9GK7hZMaz1ABwNb6z6fs31R/CaFv7+JXKYmDJ4gwDQSLbDtlPd+noqG7gTZlOSSAzRbWqfzskupzxChs/9M1BrQyvdgY2dY+A0v928OGA8cw4fM7EnNDDPm4W9LTILg1iE4R1SiAOZr/8KIABKXpIB68ilw0fTUtQHTPnbEsw== 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=MpRO8AaKPQmRex2z5/PvraYx/tV5RNBeNet97MijsTTpXkFkApL96TWM9X6fZJCO2hpb7D7u7n+FQJ+o8gfna4R0Q/017MGQUD95FyuPsXeKmwmP6uyp8ebYEzA5B9kRAExhREAeTEhp/30iq76z9yMYlMBOG3ZvwEM8YO7mgzL8WzjOmm+llHTVjQ5/Vvl/KSA5rmLC0ZJHtZ2PJfljGPwyeXX8yySBuzxBQ2hhLSqq20THPtqEWQLEAw+epiZ63mon9GNs9fMMpHiANvxd1Ieg9fpHQ2IA8R5KMl4m/YYNwPminGjM/HkfhcrW6UZRQ91Ykq6+KGw/PpkSlW0X8g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) 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=lpuYtYkMJpvarmPqc2QZViZgdArKwjC0G9QbzKbfilLhM6LC8CRpmIhHeotOFtdByMvIfdiu28DTF4WValu8lFwGoCdZ9WHDfexPHxOQDJmqgBL2nwKoj5zJPT5XXyAHfQBE57HKSiYigb+ABOpTCBeKpjZ62sYnAv6EkDXZIlHz1pOjpLnt1kfe2hBU16ntqsPJvfQi1a4mn8K8TXmL5vXgXCyhQM8g8Xa+b7b9SHWJ6TIV4Yt7qPmMbC0f6+n8iOpMTkeQ93jA3gPx782cYX+CnSDvHcHBZkoMKWBgbCR2v5IC9tGgmggknyNsVpzM986iZBsqH5K78YOgACBwzw== Received: from MW4PR03CA0119.namprd03.prod.outlook.com (2603:10b6:303:b7::34) by BN9PR12MB5196.namprd12.prod.outlook.com (2603:10b6:408:11d::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.22; Sat, 28 Jan 2023 02:30:07 +0000 Received: from CO1NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:303:b7:cafe::3b) by MW4PR03CA0119.outlook.office365.com (2603:10b6:303:b7::34) 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 02:30:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) 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.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by CO1NAM11FT041.mail.protection.outlook.com (10.13.174.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.23 via Frontend Transport; Sat, 28 Jan 2023 02:30:06 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Fri, 27 Jan 2023 18:30:00 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Fri, 27 Jan 2023 18:30:00 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Fri, 27 Jan 2023 18:29:59 -0800 From: Nicolin Chen To: , CC: , , Subject: [PATCH 1/3] iommufd: Add devices_users to track the hw_pagetable usage by device Date: Fri, 27 Jan 2023 18:29:40 -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: CO1NAM11FT041:EE_|BN9PR12MB5196:EE_ X-MS-Office365-Filtering-Correlation-Id: b0ef947f-d985-4948-d1eb-08db00d791ce X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: P5IOeQykiHF1x/HYORjve4tbNx8OjjrHF0smMqv6KzH06xPRAQV/JhdGHkfXqyD9vspcSwWmGei3vSV4Z7AvnZc6KXVfBWSYGUKkS5Nt+DwUc3MGmXDU5g1xZIXgmJ7pePojkhR6Cqzdiurhn9bIjF6j9ps5xV+QtmAlqp7WaqRVpASZmF5NVAeA25nVWI3kOJxzdrlOFRPVJzxqBZ0S0p3g2dEvD+mbVtCcCGPzOCv0s/Iho+eORjdgqGJ4O6yBy6OnK++b8jkab3oCtfrKi7KAIFvrsv62CIYkOLkCPBhOmzY+G4ovAekgrH6KzM0HdoH2JOsIiQK0Oof7HezY4+r45KvE+E3LqNoN2m90pIA086YK0I6D41nm4arVzqw0QaNfAehgNPevMA1GZUhqVaGke90wIAAAI+zWUI3b2IJSChTTQIQdgpF4IYup9DDSeZdO56uYlEINvw00cBed3otwYuhnj3ld0RU2p6/4aJYgwpY9uciHXiVRmAvDf4K5hXUNpObHL/FP7mZSJ6l2uWlgJ132MiOwMX/3aXdQn926sMXkBpReewTjcUYYz1cqAEaTxNe8rsYguC1HFWvQRvIJaFeAiYI9zMNlGZlMVM/eHkDoNxhTMzhQQNtU82uWco2MnZqjAVVaGRH82miEvjRFUrJE+c55fItt/CIXf0omyIpmnOYoDgVEde6vYIRBHoAkQXsfIL8jHYZZDVoBNA== X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230025)(4636009)(39860400002)(136003)(396003)(346002)(376002)(451199018)(40470700004)(36840700001)(46966006)(7696005)(186003)(26005)(478600001)(47076005)(2616005)(336012)(82310400005)(426003)(83380400001)(6666004)(316002)(54906003)(41300700001)(7636003)(8676002)(356005)(70206006)(82740400003)(110136005)(8936002)(40460700003)(86362001)(5660300002)(4326008)(70586007)(36756003)(2906002)(40480700001)(36860700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2023 02:30:06.6182 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b0ef947f-d985-4948-d1eb-08db00d791ce X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5196 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?1756232295629746284?= X-GMAIL-MSGID: =?utf-8?q?1756232295629746284?= 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 02:29:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 49770 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1155560wrn; Fri, 27 Jan 2023 18:36:38 -0800 (PST) X-Google-Smtp-Source: AMrXdXsCpETBdUJ/o6wx2C15sPa/HEo+QHTnNWVVWlCNdAeat0zXnY93vLcCLnm5bCuDegeioSTs X-Received: by 2002:a05:6a20:6989:b0:a4:486c:568f with SMTP id t9-20020a056a20698900b000a4486c568fmr60013594pzk.59.1674873397872; Fri, 27 Jan 2023 18:36:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674873397; cv=pass; d=google.com; s=arc-20160816; b=uO+JgvBp8CvPwh2QX8aYp0EtbIlQuRBOdBN08UBKB043V7UaECldaz/0c7Ax2j6H1a DNGLqfngZGXu7Ded/6Dfh4nysl+RD+6jkuwkB+l0/Y96pJBfBJGO8G5QkSsueomSYW25 i8J0iMTRC/A7SOqfU1oi2zg10EvByG42GNhJnScu2aBR7Ik8Vyp00V6vIxr2zq98e+iu HxgwFXHCDpeZpKOlGFPjxiDhQQKRTGtO3pSLHK9jMwQepn7Q8+saL7g/lDggzXlJTag3 xB9nNNTWd1XQ2Lo+aZ2zwL/KkxDP8eOTz0K35MErXwDoYDs19GpmPyhmXhDmcdJVtMmt 4/nw== 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=Sy8OoCaxDiGvWsvGp05HRDhS0I8R3PAh8VFw1URM8NQ=; b=aggkptkcbf++1wLnuAIVm77bhJNV6I1NbePhGZ7vbvnRi/bhk5qCVwARRIu6pNJ3CF wZxgjpQ6X4n7ZSmUtWDD+Gw39/0Qud3Src0dbvS+QGSgBPtjwOFsQn5DwNDFvOD5D0Fk NZK9ulLFm8Akz9wgY6lMPq9wv9oxwKQd6rBAYsS3fcJWmWb5Sx6x3JR0dtk+vFnHF7J+ knGm9f9ZMGCNMmn274qWc448eHsE5oG1leuT7P3lpQSQ85mMRy/zTNNADQ2fS/PT84aP e6YI4avTnL1yQ/smdCOlmgldSCgtjQinPWBCf8UyAEsyReoCfjJd/hFv0ztROybvglBh WTWQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=oGI0v3xJ; 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 m18-20020a637112000000b004df4af6841asi4845131pgc.84.2023.01.27.18.36.25; Fri, 27 Jan 2023 18:36:37 -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=oGI0v3xJ; 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 S232043AbjA1CaI (ORCPT + 99 others); Fri, 27 Jan 2023 21:30:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229464AbjA1CaH (ORCPT ); Fri, 27 Jan 2023 21:30:07 -0500 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2081.outbound.protection.outlook.com [40.107.212.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10F0C125A7 for ; Fri, 27 Jan 2023 18:30:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gceOGWsEyQX1zBfuSuoPdMzFcD16jFqQ2jtStRhvO1VRpeAJkKgGqmIy/solH0uIX0i6zo607kTNUhxTEtpWGrYTjpd0poab9NHmXEoX4d1hdiau2RR1hSmo/vtgTbbWogNSDqHxuMpZdyzDd9rbxZ89zb+m8WCYunPCfZTxWi9L+3+ugbc9g4Gj6oFoOz7s/xk/3rVwU70rqnwsAggZgVAwoKMAPj5HDfv74SgWSYNR24CmtpgWPfoSP58BAJONCZfQYx+80ZDSAmp6CmIpQxuqjWOkyUaCWJklnv/ikK27GBTvc5dCuFfFUeVtvnodllwK62lwLlQ5k1+s+tfzdw== 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=Sy8OoCaxDiGvWsvGp05HRDhS0I8R3PAh8VFw1URM8NQ=; b=Qx2fB87QI67psAsOYW0OB5npwDqckcDt4xeqytBEA3r3wZsSLIFSaC5Er4uDI/ZBxdQbbo3GzVpZ/wwQTv1lRmby3OfFLNGyqL/EnUCss97UYBhhSuQrT9truN70UUnpdOqeZAYGz3i7Z/vdKCBBlym2hiwli0k0CjiQO0rklTUW68m+eTcSk4atI11wFiktkVironnB9g9hBKfZBhHbQ33xC/pgmLDSep51y6WOGWdbfsIyCm/KrhKILqn3dtigFf2iU6dpJsb8Md46rGbqKxND0xPgGcvxd2z0fHKDNhDmANwokGKN5ytGTChq+CpozMuKHXUtx5J5HutPqZPcew== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) 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=Sy8OoCaxDiGvWsvGp05HRDhS0I8R3PAh8VFw1URM8NQ=; b=oGI0v3xJ6OpXMoQ7BMNu9DuqTtYm+7WKyuKEMP1qP44nElkh6Cp9qxLltPBGSrkf6/CEeXYtoAxlzINHNVxSI8Ma1RydVrA31sHYH+zl7wpb5IihRu7yJCxIZXtp/IPVb2jVENFiyenJwqvgKNFuR7gg/Kub+anm9rFh09cSRiGnIcyfIAp22gdmR80AlhkyFaFczQf0nLILvi562EAwvX8tNESeqpEiFZVWHQBXqxkdCYmeDHeQoX5JT+FXyR+X/dIEpxv5A8eIN4Se+p/jTDgDBJNZdoGAmxkLxkLjn9HxlFz81Y3VH1/dLdzPkxof7TBV25AjZtN2CEi7mKS7Nw== Received: from CY5PR14CA0018.namprd14.prod.outlook.com (2603:10b6:930:2::18) by PH7PR12MB6612.namprd12.prod.outlook.com (2603:10b6:510:210::14) 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 02:30:02 +0000 Received: from CY4PEPF0000C96C.namprd02.prod.outlook.com (2603:10b6:930:2:cafe::72) by CY5PR14CA0018.outlook.office365.com (2603:10b6:930:2::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.28 via Frontend Transport; Sat, 28 Jan 2023 02:30:02 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) 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.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000C96C.mail.protection.outlook.com (10.167.242.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.16 via Frontend Transport; Sat, 28 Jan 2023 02:30:02 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Fri, 27 Jan 2023 18:30:01 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Fri, 27 Jan 2023 18:30:00 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Fri, 27 Jan 2023 18:30:00 -0800 From: Nicolin Chen To: , CC: , , Subject: [PATCH 2/3] iommufd/device: Make hwpt_list list_add/del symmetric Date: Fri, 27 Jan 2023 18:29:41 -0800 Message-ID: <25b3b85b03fc2a7968c476b0533f451acecdfd13.1674872884.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: CY4PEPF0000C96C:EE_|PH7PR12MB6612:EE_ X-MS-Office365-Filtering-Correlation-Id: 8d016266-2ef6-42c8-c7d6-08db00d78f40 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YHKNdfOXLsmmNrvdw5205Zgary/k58gnm9VUqLE4ZE3A8S3kNTlLmoQTjDfXRNxf50xtERMU3IoDJHoCfbQWK7i3ULt+zzBrJLPDRZKH0k5aLEqLM4FDEzMalq20ooZIZ3nMlsbjvjXumCUJ30TbgERZ+jyl2BZpV71IzyA9TXvPGjcpm3KyBFuyCluB2i+S3JKTSvavKBqFLKBMcddgLMG8/OBm1twDAUayK+QUzfBLtad+OzJi9E0OP1A7Tc+q6i/93/AZ9PomU5TMzZPHCX25Pm5t3j+krHP17Cc71syEB384lhKvOcp+EwmDJ7gu6bW9wDeSmUXAzjkppNwrtpGRsveJScVWpCP6V0XU8uTfrFXxPbJXvmu2pQMNG92W/mwEvJfQJejoUdSseihX601R4qnwLLT13bvato3Y0WaJaaYcpzz+FuxIqIZ1xX/IEB36jahoULYDa7CgeeidMtbWFoNP5gXY4msq10ZlPMXRdlm44DxI9pOgbBK/g1T3oGqVmMBwASFkP5aV2BCrRWSsH8iIsJuEWFFFeNFZgkyNA/7XT4kbRdo2RT28HqlS+A3coFR1QYbCn/R74/6tXpd6tuw2iW9Qkln5DgK6ahMFWft1XWtRwgwcBUmBQvkvr6JkwVSQwcxkpgNXWMvT+IKCJkw0c0SNQV2sQVeQ6RLPVR/gFF84r/rExzKpwgq1inc1wqQ6w9cd2Uocb0BhAQ== X-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230025)(4636009)(376002)(396003)(136003)(39860400002)(346002)(451199018)(46966006)(36840700001)(40470700004)(47076005)(82310400005)(86362001)(40480700001)(40460700003)(356005)(36756003)(36860700001)(7636003)(110136005)(54906003)(82740400003)(2906002)(70206006)(8936002)(41300700001)(4326008)(186003)(5660300002)(8676002)(2616005)(70586007)(26005)(316002)(478600001)(426003)(7696005)(83380400001)(6666004)(336012);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2023 02:30:02.3319 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8d016266-2ef6-42c8-c7d6-08db00d78f40 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000C96C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6612 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?1756232048110483754?= X-GMAIL-MSGID: =?utf-8?q?1756232048110483754?= 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 --- 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..0248073e3169 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(&hwpt->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 02:29:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 49771 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1155566wrn; Fri, 27 Jan 2023 18:36:39 -0800 (PST) X-Google-Smtp-Source: AK7set+Jtnr/HEoMAtC/2Dj17CC3iLMC2wK4pGbYvcUD/GAWmxl9xw5qut5NoaQZknXWxmb8lzwf X-Received: by 2002:a17:90a:1917:b0:22c:3a4f:83bf with SMTP id 23-20020a17090a191700b0022c3a4f83bfmr6422840pjg.9.1674873398869; Fri, 27 Jan 2023 18:36:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1674873398; cv=pass; d=google.com; s=arc-20160816; b=ebNEpAR+CyzZlwgQQ1DwWWxQEYFNQltCRchFh72sOD17O5naz51T/wB6CMGAesiW2L D0Rag8i4GogKLLeFE36SGMbuOw6wnmp2ogxxHQamrYLO9gqccB1xsdoD4EteUsC/94GH FZB9qktNik910zsSBpIZmjjEHnunIKk+p3HyXMbKzrN+VijKPlJGmCP88HLtwrypmBZw EsWAVoQnhU7Qc3w3DQFdddvOEklJfXkua5wUT4N931wxNzRc2LTZxZtPg81f2QsvMP4p Je78cBaIgp59UDQEYMOB1/KLIdXtEPObmsKK+NOelurFL/BkluEBjGwZMk+f3LJDbd65 9sKA== 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=4t2ZR0hLSHK2a5Qm+twx7zepvhYl9ngyuAURrM04Xo0=; b=rW7njUKq+DOmAk/pDfykuilpCzUsPIJFrTlo3ASDKd+vm4XedUBpBr2CZ0+mpJKwia 9I3FCgZOOST+n1K2ku9j1EqrNNPqDpsWLWum+sx/VyUbanDukPcc0FK0i5zvCsYnobhx wves1tgCOTcoVTgOccafujTVNK/Rc09vEuxJs0gZEVdLgzyO4S8+NrnMPpSOWlJBj+Ve BcZ1SqEvNDoLm9Gb0zj/kyG2wqLy1bJjKdjawTFe5nWu8JlSSmTlAjEOPLUnTdTkEscE 4WOKNDBjFiPVUMaz/BN7LyCh7k8wVU8D5ir+q5qSdc1eKq7/C50EUEk3dZwtQJaMalMv MiPw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=rrEoMA11; 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 22-20020a17090a0b9600b00200435da17asi9092976pjr.128.2023.01.27.18.36.27; Fri, 27 Jan 2023 18:36:38 -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=rrEoMA11; 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 S231951AbjA1CaN (ORCPT + 99 others); Fri, 27 Jan 2023 21:30:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229890AbjA1CaH (ORCPT ); Fri, 27 Jan 2023 21:30:07 -0500 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2071.outbound.protection.outlook.com [40.107.100.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C0CC8BBB9 for ; Fri, 27 Jan 2023 18:30:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Sh+PgJGsngVaCHk76AgL97VJQESX11IsTB1rKGXNqVSaQ6K82+65NgrWmIClgG1TLlEAc/0zN7oc9Q1jtslQ5tiWebTB8I3KLUk3u5S3YCVe0eenJujBTK01bVknEiK0NAjHKbGIK/tIr2b/+TXbNjYqaSgDT+TtNrLyHn6WJqA9vcQy5a82Zd/pVcw/vtSAFAPJf/nJ98EZYmjzQzx1uj8Rl8ZNapw7wcLYCQYKpx1LTpYTWymfGlFch9Vy+0DLp97XwC2XeTqJIRqcXq157SjEBsC1/eShab5JtO2BAx1eLZGjq0KuJ/HbnEoPwXV4leEooNIN1Hh5oN/i1AxYaA== 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=4t2ZR0hLSHK2a5Qm+twx7zepvhYl9ngyuAURrM04Xo0=; b=Cfl88Hi0lpplPtj/ADW35axzfDfrFs4L/ZV4ug1NvnFB572XlMX0MqPSXcU3GGlO3rOHY2RufbUG/fUmfp/IX+KyK+YMr7nSHzyBThOA3vuUOSchw+7hihqYMY9sMNf1FNbs0iFrXIKtIx6esf3dzFb2+735YznJu0yvQPWzNRwgkZqUwVjr8t+N4JDmSubgC1FPNj/qKrnZRp6VyEZiJw20Ddr635UYahA+vmjJ698c+ct8tVjE2SI4OiwGxBsvmCGfjA9mrpWIaMaISyfa0brOlZy+MKI7MH+CrUUgeHAxyQIEClm2KiPkWSoa7kWuhRroTF6HjqnzX8KABvk5+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.233) 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=4t2ZR0hLSHK2a5Qm+twx7zepvhYl9ngyuAURrM04Xo0=; b=rrEoMA11tWJejPHJeUWHQDKl3MiRFFKzSdgLpUTLneCc2N9+aLYSU78HUTMBpjmeqhXnki+0hsEoiWjwSnUXdWcTkuXj5CI2p6U2lWKN8GU+lSyb7xz1AIypb1tpvOy6BERXomylZYzyfAzifxOPsDm0oQIKhr0RsiLQRKJbHjaaynMVRtaxeKx7BJE9V9FYvHBQZa7lwYDdwr1j3lkwxAwkVg6cvDAn0H8Cbmp6cAA/MQ86LY3oIZnwMcagAP4hsaIhr4iDMVyVeTeF5dd7POQLLr/PyBzpyYgKr8mg8rom+8hEd7T5OtLDlSkDKZbCnB7eMZ9vo/FrA+OzdVrUEA== Received: from CY5PR14CA0030.namprd14.prod.outlook.com (2603:10b6:930:2::24) by CY8PR12MB7313.namprd12.prod.outlook.com (2603:10b6:930:53::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 02:30:04 +0000 Received: from CY4PEPF0000C96C.namprd02.prod.outlook.com (2603:10b6:930:2:cafe::c6) by CY5PR14CA0030.outlook.office365.com (2603:10b6:930:2::24) 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 02:30:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.233) 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.118.233 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.233; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.233) by CY4PEPF0000C96C.mail.protection.outlook.com (10.167.242.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6043.16 via Frontend Transport; Sat, 28 Jan 2023 02:30:04 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by mail.nvidia.com (10.127.129.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Fri, 27 Jan 2023 18:30:01 -0800 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.36; Fri, 27 Jan 2023 18:30:01 -0800 Received: from Asurada-Nvidia.nvidia.com (10.127.8.12) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.36 via Frontend Transport; Fri, 27 Jan 2023 18:30:01 -0800 From: Nicolin Chen To: , CC: , , Subject: [PATCH 3/3] iommufd/device: Change iommufd_hw_pagetable_has_group to device centric Date: Fri, 27 Jan 2023 18:29:42 -0800 Message-ID: <5c706a533bb4fd145614d630358fb21c215f8eca.1674872884.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: CY4PEPF0000C96C:EE_|CY8PR12MB7313:EE_ X-MS-Office365-Filtering-Correlation-Id: 92e36187-9ab2-4619-0bd1-08db00d79047 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ne07qw7/aKlHUa//ibe5LR19aQq0Cx+c/8q1UdObzikN1qaWi02+vKIGG+TAfZ2x09dD4Vsj6c92/Vq2h4VCt/HOX7bXOrS5vJkRg6Ifzb1qb8WPesGdQURzIAx8EMI3ifxyzrWpAoL86WJkNUcdwEkudh0BjJpHL9HJ4V3qgNjf5N64M/sdMVCPpRb/0bEZbJf7bzJUXYCnfy4FvHmyNp+iJpT9WS7TSwVCBkOChXAWqvdycWw+w+0qxpeju+dAQPSVCyzHcpbtRgNgx0kOXaU7E7JlBRCBghuVtxdqWKvWAm03elv8UNeQPyM5Rt7m68R0Oengp4itIOs5ZmPvXAltFvgJ1/2wvswwFh0SUI4TsL/DCcHAKL0qeoU0nluLJ9p6EenvmMeDY1MHL9QwB3VNlAlh+fx4/IBFGZQ3OWK+hilAAKUGMyyhJqJ4iMlZdAV2v/1gVk4rCS2Tid6dvYUDwNNIl/RNVqyMBbM6hPHO0OtjKZy3U3tiKZCV3tGkAdyhn8T2Wx+NHOWOTNDUYasEZf2LxKxrO56wwNb+VOWD+CaiaXhLcTRu0QT4zvSTDT4hnDy/Cb2XNEHikaeFpPO8Q6v9CXP7x2yC1AU93P29UpAk7ndWUx++gM1AOP1gUIt8uPC8mrA/UeJ9LwYvRz5sTKJt+4ZTHB6PEDb7S5girJxwOATUmVgvJHUyJvRNcEMnvbjvuVe5ipProFmCSg== X-Forefront-Antispam-Report: CIP:216.228.118.233;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge2.nvidia.com;CAT:NONE;SFS:(13230025)(4636009)(136003)(346002)(376002)(396003)(39860400002)(451199018)(46966006)(36840700001)(40470700004)(54906003)(110136005)(5660300002)(47076005)(426003)(40480700001)(2616005)(36860700001)(7696005)(40460700003)(336012)(356005)(36756003)(2906002)(478600001)(83380400001)(41300700001)(26005)(7636003)(82310400005)(186003)(8676002)(4326008)(70586007)(70206006)(86362001)(82740400003)(6666004)(8936002)(316002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2023 02:30:04.0507 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 92e36187-9ab2-4619-0bd1-08db00d79047 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.233];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CY4PEPF0000C96C.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY8PR12MB7313 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?1756232049268879787?= X-GMAIL-MSGID: =?utf-8?q?1756232049268879787?= 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 0248073e3169..5a5dfc0c4ca0 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 *