From patchwork Thu Jul 27 07:23:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 126785 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp939006vqo; Thu, 27 Jul 2023 01:17:15 -0700 (PDT) X-Google-Smtp-Source: APBJJlHGYetU4PFzLLtj34ce87QUtD+8MmAJpT1ISrYq2fCdhZELHEeWiXFgwIQ057s8ruJkXLQS X-Received: by 2002:a05:6402:1106:b0:522:5a05:6011 with SMTP id u6-20020a056402110600b005225a056011mr1331389edv.0.1690445834868; Thu, 27 Jul 2023 01:17:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690445834; cv=pass; d=google.com; s=arc-20160816; b=IS5Y0SS1/C/uqMrgWm/GlaQ33RgX+3XLJdPbUT3/hjuN9NVlRVEiHfwrioNeIjPb8v q/D4j4W3aId+5ztk+yGj5c0EgZw6XNhQjiMSgeA3+2Loa6Q3ixbPdTCZDWLhtOkgAFEB Z5RmtfnI6dtmBa+OQGG5D8qT3eaQjPgLFUx3yR/JzoAKRZDVK0YaL3NGKSbn2Jberjxm cynEV9kfTP1QDuh1Mw+bPB+s6VkBc7m54a88FnRtEJJ6TSusUJbPUy3joIYa+cpI2N84 8Jl48tKxgeei/HJKyPHMqMiqdpikBRQwfeZI4/W071wxiTTFcEMS1/sGj2cAUCiFmeYd GUpA== 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=mUc06e5xx4kTwBKEnoGTOSehjX6v5imRbufbXUgPcoU=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=Z+uVzcueJPeldp0iU6leuD8k8ZSRKPCZ3Fihlr1XBPVD2/SmE6vf99AqevXFK9VFaC jtyMjB5KsTq0P/lfFCBOYAKkub4w6F6jJJg/fe1pZf0fcyd99LVw4+RMLWLJCr1Bzkik GKN/FL3tERtFZ194u6keh5Ad3cuvCGic8jV2xHH2G3zR76k37P3KfHq/l7PDmfD9N46m DvkZ2g21YAeuTe4Dk4xq4VHFYZcvoA9RAqFI/IYi9Ws6gLnUCRiztjmmuffeHep2W3DU 7PSHjcSzyjxReRxCpqRIwf6eJsY8N4jOfReZNz5FNt2Lsk26tq91eNdxAT/lICK1nTAX iRyQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=fWYP2iIa; 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 d2-20020aa7d5c2000000b005220ee29396si586150eds.52.2023.07.27.01.16.50; Thu, 27 Jul 2023 01:17:14 -0700 (PDT) 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=fWYP2iIa; 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 S233368AbjG0Hef (ORCPT + 99 others); Thu, 27 Jul 2023 03:34:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233291AbjG0HeF (ORCPT ); Thu, 27 Jul 2023 03:34:05 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2069.outbound.protection.outlook.com [40.107.244.69]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72E8A9EED; Thu, 27 Jul 2023 00:23:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MgiBwmFleE5Tm4zglnB4+AWfoAGE2tMZPAZL0vOWHeRhLQTACEh8V9D+F9QOB0emn+YczHfsb7DpFUg0IFzBEobMz4QEDGZ8WoLMgK6g5I3vynm7TUUlkvK2cdJzxCKTr81r2ZrECthQeOl2Xdn7Ie8ns9SRUrxdEvZEOU5yryOSNzUQZPr5vdOdhLi9m0uWr5rMbleje8/bZ5PfgufBs/VcdnObG0q1aSCcIDvkRsY9E8B3PRD4z44CCLoWlhNf0rjMXrYRFpjJF/CXS2UA5EbMa0sCQePbRckCtOMG8X/Q2Dks5tUfJy7LQjPS5eARSSOuz5o64tutQHR78NWVcg== 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=mUc06e5xx4kTwBKEnoGTOSehjX6v5imRbufbXUgPcoU=; b=EmAKUh0TlT26Vm3kRWYIi3JBbWbfbvMPDemgzS47rwU9M7ttS+Saxni+I55K78hdQBqIsm2me/UJKnL3HrQKVDUubyquupWmIUWty5GEwkDHoCf1bBXIHJ21B97ZDxfmb38MNpiFkS6/5Cj7uajM4kdDSUKoZQkOBjnx3NMy/2vDSdAFO+vknJfBBcARKnP4bsBF7QBjsGp6DtsB6eQm4z5tH8ot8ZCiDP1jF9zBNBtjxZSJi6CO0NqphZu2Pyk/9ekeVTfPuQxsha+XitEyhBV78A7YmTrGV/N3gzL4Y8/u4Vydyat/puKCTE60dVKa7SGK0I66BSxqwB6ZWJdNrQ== 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=mUc06e5xx4kTwBKEnoGTOSehjX6v5imRbufbXUgPcoU=; b=fWYP2iIaz0UTtQb+rR/sfQeuZHS+q+di1lRMUpj3Uq2FphTipsoL2UEwWv74gXY1LYASLtJNHpQ8Fitlv0MWRPU44tv7CKSw5f7iwbQrneCOAOWF+BXJQ6P3xCAtEDUn25twBwdejJslJGnJnuaFauxmZcoTSeWydPX88wuzGhGCNVTO6dyA7oViQQrwyAoWDimdRncRcKTztHjx5dH4Ktka+XT8D5ZrukAN0EgfJxB5x5GqZF7t+ZhuyEz+FEDIzRXUX219dlWizJ1LDB2iYAkMM7GvWkNUZcVIry7pUeVnswBghuIbHSDqd3EnN3ObryKDs77no4uTltdw0O/+3w== Received: from MW4PR03CA0057.namprd03.prod.outlook.com (2603:10b6:303:8e::32) by BY5PR12MB4901.namprd12.prod.outlook.com (2603:10b6:a03:1c5::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 07:23:45 +0000 Received: from CO1NAM11FT088.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8e:cafe::df) by MW4PR03CA0057.outlook.office365.com (2603:10b6:303:8e::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:45 +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 CO1NAM11FT088.mail.protection.outlook.com (10.13.175.131) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:44 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) 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.5; Thu, 27 Jul 2023 00:23:29 -0700 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.37; Thu, 27 Jul 2023 00:23:29 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 00:23:28 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v9 1/6] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Date: Thu, 27 Jul 2023 00:23:06 -0700 Message-ID: <85d622729d8f2334b35d42f1c568df1ededb9171.1690440730.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT088:EE_|BY5PR12MB4901:EE_ X-MS-Office365-Filtering-Correlation-Id: 0bf4324a-d679-4e35-eccb-08db8e72696a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MkCXyBpGgqTszVEdrtSMMvxTklDjn7gyplA2OqVClicqlcTMSTRYyMjuqPp00FYVlLst1N+5GkbsOky4NaNORdrE3SpfX1I+hfgAft7+x125fX+5Ed7w71LGb2liKdNPB3r2Fnw4hVu42V8CtGxcT5TTXl2WXovPaLa1ivfU5LWwVKyBIUYYYChTPPSOnE4z9a6goXab5zGlMll4LaInAae06dECgV24kDbSWtRsJjKpIqHM7Rabx6lgPRUiFMI/aVtt5omHr/Lj20mCaymE9Vvdl9rK8x8blgvshJ8+xNX2Hglhyb57AiPmtO4ukJy0Q646JnBYmq50YLcIW8kmaWA/LXERnOHrQcaE/7DTJFNmo8ymTht/oMDlBRehNbV42V8J4s9yO0CXl5/KxvOfO5dD8Hg1+u+UgOxp5eUkrMzTocmz0M4KEf5JHjjoYuO8Er2y28HpDobGAqvIo50b67crZ1YEPRtU14PN1PRxW5l2kY9KGLwC4qubsqpX62WEuHW6FP5ZVLFgT96Q52qrFmH8qVpDkX/+DmV0QEHKzAf6LBaQleQGWq4TNwLvL0V++hCBGJiW0DxrpDjBOI2x4RyOxgaT9WB+biUPvoHmMwZ+rFr0g9WVGWlyyzHpZTk96oF8+RV8ykHOPFSKM+OcB0WT+BHpQBostbdFErdW2vUn/vidAMFh1FTVxz+dlBClUiPQMA9bFfdfIKdnC/BupensY4pr5xneoAoAb/lUDvgt+n62VAa/rMePZX1rBje5 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:(13230028)(4636009)(39860400002)(396003)(346002)(376002)(136003)(451199021)(82310400008)(46966006)(40470700004)(36840700001)(2906002)(426003)(6666004)(7696005)(2616005)(36756003)(336012)(186003)(7416002)(5660300002)(86362001)(83380400001)(36860700001)(47076005)(40460700003)(41300700001)(8676002)(316002)(8936002)(26005)(70206006)(356005)(4326008)(70586007)(478600001)(7636003)(40480700001)(54906003)(110136005)(82740400003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 07:23:44.8150 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0bf4324a-d679-4e35-eccb-08db8e72696a 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: CO1NAM11FT088.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4901 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, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772560931662084471 X-GMAIL-MSGID: 1772560931662084471 A driver that doesn't implement ops->dma_unmap shouldn't be allowed to do vfio_pin/unpin_pages(), though it can use vfio_dma_rw() to access an iova range. Deny !ops->dma_unmap cases in vfio_pin/unpin_pages(). Suggested-by: Kevin Tian Reviewed-by: Kevin Tian Reviewed-by: Yi Liu Reviewed-by: Jason Gunthorpe Reviewed-by: Alex Williamson Signed-off-by: Nicolin Chen --- drivers/vfio/vfio_main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 902f06e52c48..0da8ed81a97d 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1483,6 +1483,8 @@ int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, /* group->container cannot change while a vfio device is open */ if (!pages || !npage || WARN_ON(!vfio_assert_device_open(device))) return -EINVAL; + if (!device->ops->dma_unmap) + return -EINVAL; if (vfio_device_has_container(device)) return vfio_device_container_pin_pages(device, iova, npage, prot, pages); @@ -1520,6 +1522,8 @@ void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage) { if (WARN_ON(!vfio_assert_device_open(device))) return; + if (WARN_ON(!device->ops->dma_unmap)) + return; if (vfio_device_has_container(device)) { vfio_device_container_unpin_pages(device, iova, npage); From patchwork Thu Jul 27 07:23:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 126812 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp957028vqo; Thu, 27 Jul 2023 02:01:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlFIGPCKm79VRz0gWZJJ1YlR1UgjONmPCylhFK8n18kzLSK/mfjmirqZZKzFP7rtuHsJTsR8 X-Received: by 2002:a05:6a20:5511:b0:134:9868:812e with SMTP id ko17-20020a056a20551100b001349868812emr3214274pzb.39.1690448471622; Thu, 27 Jul 2023 02:01:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690448471; cv=pass; d=google.com; s=arc-20160816; b=Gj6rnIOsJANUqsXIGTNAJYjjZ++eAnZHKG366+FUrH8MVOEz11eB6nRAIcSRRfBRcM yky6GLqOC3skG+FSsIslGMi2M0fo5tHKbLksR0CKaZCUdUDMNGVMT3OnykQETwnkvT+P rpJIyqGBWULRkamf9r5aQBbQAnSP7Ct/lWdL+Oewper0Z7A/t/bJEAYxD5GwOpEmhu1z dzKm3Ec7VAhTyPWByQFwTbAf4urmAZ1BUulLV1vR33XYT3Nu8RYFC7f2kpdm8tqiT1mW FtQCgXT0ccvoG5/heXpO4I2aWTk1liORGz9+2DtwsqAvTM5JaHmUK70V7LEOEWlZeRoG K0kw== 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=SrJpkLglUiQF8sYk/kmmxNYlDRc+efpv0dZ1dQ9DIz8=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=mlTqdYwYoGartHmUUodfqBzwAmqC9MbM9vMqu43WSGzjWUYAN4YAxFqSdCSncJc2mo v8krgxBn8sINDvZlLE6lQoh2VcIQ/0xnPZxJN5ciXwJGCVgE8ke7n1RS1gpW4OdVHm7Y +W3fQqHTSFkaVigxLn2oIUxIatZYqTlWjWxjOlfWXW8VLhNggrrX+mjt4flH+BUURYjz 41tBZsSzT9RwDHQoG5VL6jnTjguvxWlyJvGf9uzNaVcP+0zng8fV02ZDYPdFzgLdtinI Cvfqts/LYz2FLN+J1BXORB6eTi5004re/99xTppwhJ1BmuIKSDgBbre01rQN9blxFjH3 Obpw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=UVHIfmCR; 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 c5-20020a6566c5000000b00563b0cbe7fesi880690pgw.880.2023.07.27.02.00.57; Thu, 27 Jul 2023 02:01:11 -0700 (PDT) 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=UVHIfmCR; 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 S232253AbjG0Heq (ORCPT + 99 others); Thu, 27 Jul 2023 03:34:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233392AbjG0HeH (ORCPT ); Thu, 27 Jul 2023 03:34:07 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2078.outbound.protection.outlook.com [40.107.244.78]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3935C9EF8; Thu, 27 Jul 2023 00:23:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lgIVtyZXcf98QuEK0ZKlFYXT01MN2zhUA05yygOKlo/Ydx16x4AuEBioVeRSKgnoIxVkie6cVRwPwmWGVyMba1I9upF25q86lKqnfMov4RNizDmNWAlOQHBjPzd963K7T7EgJXeHVRKT240mKVH9NmPygGuFe7VuUgv9u8CH3ZMrTzlBsmFgeVYZI96p+lCf2Udnmbr+OXbH97r+aN4GBZAtrFi/mtKN9WNwVspFbCCWiftar8Fs3KeqVbbMNVn+BOw5rRHleQfVqyKyZjHD7g6n7jO/FYEeeB//mNyQn630bZshhyHF44kl6EDlBIL6KdeQRGnyXFNqFXJFjXLnfg== 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=SrJpkLglUiQF8sYk/kmmxNYlDRc+efpv0dZ1dQ9DIz8=; b=IXgfP1gUcnV4rjCDH63oYek7n857lpbL7Wtq1l/2xyB6BkNOd4ZqFo3FPb9yQpemvkS2kUbokQ+ypEJ60/VvI4aUqknetT8lVDwaeaGR0Q4A8lAQ1Z8JrZl6QGuKLZQ1/olNB+doW3v102Y7EMrOFrA5nY6bUjX/xYkwI1HzZNe3MHyGgr+O07NYNpxy0XY8iRVX2n3WXHnzPQdEvczafDQlO5+MvefI2DefIonDbDvOq7oWJYQq0xZC6495whn2GFSwT61fHDjh3KpqGYVBK5NYZqJ4E5Tesucr/CQpPEyZ69LzdzMua13BbJbXJnFWlO1P4lH3L1y+SbqiPAesXQ== 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=SrJpkLglUiQF8sYk/kmmxNYlDRc+efpv0dZ1dQ9DIz8=; b=UVHIfmCRj2Lnp+uUEIikMAOY7YLCfFxS5Lm7PScgdYKPePuPX4p070atWyNFLFshNGzdjYdm8mlQ7cKrr5muxQpP/PEePmD6hMcowHFmKVSn44sABCyV4+2865uKv52BmNhuXnrGkKPJ/tVsyLWzI2V5IlA0+zLlOYkvZ5IzucDi8d5TpmhJRhhzXS2Uv84929jRP7E+7FbOIBnVaJQCeXOAfWqRdfkxC2WYjmcbJ5MlqIl5vxWLwooLnX0cd/JDFvKfZaaO9m3JuDrz9XFDxMEtHk5yn+g/wEFVVKhBfL3pBa6nXR7Ei9emK+hKoY9ukUsCp40FngYZ3J7/6LqWAg== Received: from MW4PR03CA0040.namprd03.prod.outlook.com (2603:10b6:303:8e::15) by MN2PR12MB4407.namprd12.prod.outlook.com (2603:10b6:208:260::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 07:23:46 +0000 Received: from CO1NAM11FT088.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8e:cafe::45) by MW4PR03CA0040.outlook.office365.com (2603:10b6:303:8e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:45 +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 CO1NAM11FT088.mail.protection.outlook.com (10.13.175.131) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:45 +0000 Received: from drhqmail202.nvidia.com (10.126.190.181) 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.5; Thu, 27 Jul 2023 00:23:30 -0700 Received: from drhqmail201.nvidia.com (10.126.190.180) by drhqmail202.nvidia.com (10.126.190.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.37; Thu, 27 Jul 2023 00:23:30 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 00:23:29 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v9 2/6] iommufd: Allow passing in iopt_access_list_id to iopt_remove_access() Date: Thu, 27 Jul 2023 00:23:07 -0700 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT088:EE_|MN2PR12MB4407:EE_ X-MS-Office365-Filtering-Correlation-Id: 2a5005ba-7018-451f-452e-08db8e726a08 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8BAnVbGWFaYsCpczfK9qHzXchn/ZZlpSP0MVGPNrmWH4QERIwpT9lXh906/CJ+ylO5o0WmaS0+KmLriW3feHzyheYnidtepg02y/a5ttMOIVVb8HpBDteAhhYPfRVzNVQeB4gVkLAfhHi8ZOs52pn6BPzfeGjk/DTiAzycx0h3hOdeEC7QuYtSZhOgduquRdjeLHOVvlnOdqSipXqpjzRJV4Pr+UXRd5tJWIKHEiVfRY5x+Y0gxVbh2KCiJ0yFQL4NjEhzFga5YAWP+/m2ECSDbSjGA/mfbxj7+s/iheRlkzuAAuOa82qJaF2Wte0MkuLfFRGXr4T7ov1/5sL+MC+H0g6Wn9hDGZfF8HHe4X3owg+4Az4vPXV9Yo/bQ+uhSpWsRqPCaQZD7lRXDvPA1Ol5qdxdmPQZUU2Gk0EGL5v4J36b5ESsGgL2VAEiFuywE+PXAeuTBtv6mdB1PYSruHbvPuvG8b24FjP1ZWfXFpAd0PdQ23Yh41QsOcz9xFPIntGBotWcZfHqeS5I1CYXQui7mCBMp4QYeqsnt4ye2AQg+V7bnzsDY5I/Go5a7ObWi40HGSM84T2ykv5L2+blqQ7aY6mzQtVR0NTy3nmXS1KbOBMEWBP4vtaz/LxcnTanlOFg60yfEsqsoEx000NUYVgAJ1hsKcOdczS2J75Dhh4C4Gucr3tnHmRqremfe/QvFK9gMTIdmLtWZZcmDBvzC6eNGtOumzly2hm5/egbkgX2b2RTkyzlayZwf0XNPLNC27 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:(13230028)(4636009)(39860400002)(396003)(376002)(346002)(136003)(451199021)(82310400008)(40470700004)(46966006)(36840700001)(26005)(336012)(186003)(2906002)(86362001)(4326008)(82740400003)(8676002)(36756003)(7416002)(8936002)(54906003)(7696005)(110136005)(6666004)(478600001)(36860700001)(356005)(316002)(7636003)(41300700001)(5660300002)(47076005)(70586007)(70206006)(40460700003)(83380400001)(2616005)(426003)(40480700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 07:23:45.8462 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2a5005ba-7018-451f-452e-08db8e726a08 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: CO1NAM11FT088.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4407 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, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772563697035380441 X-GMAIL-MSGID: 1772563697035380441 This is a preparatory change for ioas replacement support for access. The replacement routine does an iopt_add_access() at a new IOAS first and then iopt_remove_access() at the old IOAS upon the success of the first call. However, the first call overrides the iopt_access_list_id in the access struct, resulting in that id un-erased in the xarray. Add an iopt_access_list_id in iopt_remove_access, so the replacement routine can save the id before it gets overwritten, and pass the id in iopt_remove_access() for a proper cleanup. The existing callers should just pass in access->iopt_access_list_id. Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 6 ++++-- drivers/iommu/iommufd/io_pagetable.c | 6 +++--- drivers/iommu/iommufd/iommufd_private.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 57c0e81f5073..7a3e8660b902 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -690,7 +690,8 @@ void iommufd_access_destroy_object(struct iommufd_object *obj) container_of(obj, struct iommufd_access, obj); if (access->ioas) { - iopt_remove_access(&access->ioas->iopt, access); + iopt_remove_access(&access->ioas->iopt, access, + access->iopt_access_list_id); refcount_dec(&access->ioas->obj.users); access->ioas = NULL; } @@ -776,7 +777,8 @@ void iommufd_access_detach(struct iommufd_access *access) access->ops->unmap(access->data, 0, ULONG_MAX); mutex_lock(&access->ioas_lock); } - iopt_remove_access(&cur_ioas->iopt, access); + iopt_remove_access(&cur_ioas->iopt, access, + access->iopt_access_list_id); refcount_dec(&cur_ioas->obj.users); out: access->ioas_unpin = NULL; diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c index 4d095115c2d0..3a598182b761 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -1158,12 +1158,12 @@ int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access) } void iopt_remove_access(struct io_pagetable *iopt, - struct iommufd_access *access) + struct iommufd_access *access, + u32 iopt_access_list_id) { down_write(&iopt->domains_rwsem); down_write(&iopt->iova_rwsem); - WARN_ON(xa_erase(&iopt->access_list, access->iopt_access_list_id) != - access); + WARN_ON(xa_erase(&iopt->access_list, iopt_access_list_id) != access); WARN_ON(iopt_calculate_iova_alignment(iopt)); up_write(&iopt->iova_rwsem); up_write(&iopt->domains_rwsem); diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h index dba730129b8c..8ba786bc95ff 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -323,7 +323,8 @@ struct iommufd_access { int iopt_add_access(struct io_pagetable *iopt, struct iommufd_access *access); void iopt_remove_access(struct io_pagetable *iopt, - struct iommufd_access *access); + struct iommufd_access *access, + u32 iopt_access_list_id); void iommufd_access_destroy_object(struct iommufd_object *obj); #ifdef CONFIG_IOMMUFD_TEST From patchwork Thu Jul 27 07:23:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 126782 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp938628vqo; Thu, 27 Jul 2023 01:16:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlEuCDsNHUwbCBqhN2CX0k3UBUImfkH31FL6ZAY4FtTxv5rxlTsTC39jZ3WEgyqgRsEvw6dV X-Received: by 2002:a17:906:5a60:b0:974:1ef7:1e88 with SMTP id my32-20020a1709065a6000b009741ef71e88mr1352808ejc.13.1690445791249; Thu, 27 Jul 2023 01:16:31 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690445791; cv=pass; d=google.com; s=arc-20160816; b=ywBrmImIqtTs2XvQIAXYXB9HXUprFXgDZtdvnUZ03w2nyzQckGjB1nx0nfTERqjrvk IaHz9fQ24uhJ/x7VK9EAX6+CWunZbT+P8EBcACV2MosV+XQxogfUa/uHZQ6dtqwG4AsK gfiI9NIFMex8BYIaCkejqgja3bQL+jmybhcAWf4tCRxdw9ULRYEqlq56LYtSvnyDHTmZ 9ty4/6UlIVWDC0/B70lNSxhYAhet41ORXEoBFnJ+kuEVhv+bONS4JSx5JwXLD6bcV23r dlaqjsPowoYJSZA8MNW9PjwVCMcKrtZdUvrX7eXJ+24cE3sIlxCwnidYpTCGuFkieN3D Miug== 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=GI8W7sCuSj5bc+j+ehaE6KK4a1jR5fH0hNBwUOnbD/U=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=V/vvCTtixOHMDntVEA8noG/J53lBfzvRhjO0KYHLCEim2c+7i3se64fEbs2Hh07xWF /XmfbXY09ck33729MwgdKCzRxY/ATXF3OYrl3ZMFoHVcGjy3py0dB1Qr8Gvz6Ym73CHT Ln0B110wBf8sk2LMRY5MeUvkMA9O1PSTnUz+LtubItwShzzju7Fy+5m5snUCPAlWtUr0 Q1PUQsOGuMDfCiP+BzL8OkFNc6tuZIhGoN2wyULGjTQuycJCQmToid93ajNfeOD2cf/K 0oRWpXAgR/PAi6wd+FXGkSbkDlP1ah/hw3KmeDOuCMQJSyYilgA53xLw5wWST+8Sc/49 rztA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=jJUbR9so; 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 z17-20020a170906815100b0098dcadb4002si689170ejw.492.2023.07.27.01.16.07; Thu, 27 Jul 2023 01:16:31 -0700 (PDT) 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=jJUbR9so; 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 S233171AbjG0Hey (ORCPT + 99 others); Thu, 27 Jul 2023 03:34:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233417AbjG0HeI (ORCPT ); Thu, 27 Jul 2023 03:34:08 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2070.outbound.protection.outlook.com [40.107.93.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 04DFE9EFA; Thu, 27 Jul 2023 00:23:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C7d2JH7oDyJ5hcKSlzEfo79nLLoYFOVweL9BP5CNpTMEoYbQrpHLJ9D/KOrcgm+11aDQA94FzETGTWo93116iBgZqe1BUdS2INJIMdE1dlN3AoMGjup2OAhzqCpDeftkv9c33ECXZ/ovesOAqQyVCrkTd+cIzkw/YDajzawxlOP3tTDRaF1u0uBmrG/Ir/L6M8WtjOPY7PrFqeiaZowywJTFnxkjQ0pGKiNCGgINYjqo+E4ZrShucVnjMCZvvef49Zy07XmG9B9vczoRnEhOtgpQ8FEG5DGk7X3WWuy/y2xZr212wmlzb8SNP7IjTQHDc2p8l2DjaaA4OQiiNugrhg== 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=GI8W7sCuSj5bc+j+ehaE6KK4a1jR5fH0hNBwUOnbD/U=; b=YGIwa7WDYKmsJ96ienTSSwrxp59oDikfuJYSAN1x7FAflvwMZMqxSo5SiyuLIqJtR6aFz0eHIfBxnmMv3Pq5zTf1IN16g806TNliDpHCeZZOUAhTTve5XbZ0BslwSBrRHmTjEMIC52fhHK4ySxJgozT+rhyAIqii7DrfJey+oGJbHbD4GEUIv9chdXyLntdWaIDLo1U5ELvIOzsHVAN1wRCJym5YhZs8EOj5lBdtuaC3+ARDnchdb9GePqVc8fcIo/JEnR3TTZCftTZS8pHs+T/EMu0rucfcNiK6sVbAdWHqYEob7N4LM3HlNJUTNFv9tx2dzL81zj63DENbqALJxg== 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=GI8W7sCuSj5bc+j+ehaE6KK4a1jR5fH0hNBwUOnbD/U=; b=jJUbR9sofenRTBS0rtqresntvVLkehSD3Tm08ldKnrAgmmfp5PgXLixvot4XrCDhMDsc5GnxWOhMD6OmS6ZS3D+QT5wGCnHGOkZQhghpZ6ZnMYBdWsUnWTSSGCBOTPsvxYTAc/Mrl/XYXSahRzpriZDRuPh1o7D+EcHrtEoXqYAZ+8ioOli+W2mLt5h90Ufuws8xWLyPZ9bhAePkrqwnQppAkPIGm157fwrDn45B1Lb1XC3euCB2ig4DBTl7ob997DXSVRMli6utZku31Be2Pm5lYnN0klr4DIyDN8YsI0Ghnxt3jWo5oBYK9jDfIYkAazOYDACudGTUrr+syaQyNw== Received: from MW4PR04CA0379.namprd04.prod.outlook.com (2603:10b6:303:81::24) by LV8PR12MB9184.namprd12.prod.outlook.com (2603:10b6:408:18f::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 07:23:48 +0000 Received: from CO1NAM11FT056.eop-nam11.prod.protection.outlook.com (2603:10b6:303:81:cafe::91) by MW4PR04CA0379.outlook.office365.com (2603:10b6:303:81::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:48 +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 CO1NAM11FT056.mail.protection.outlook.com (10.13.175.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:47 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) 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.5; Thu, 27 Jul 2023 00:23:31 -0700 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.37; Thu, 27 Jul 2023 00:23:31 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 00:23:30 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v9 3/6] iommufd: Add iommufd_access_change_ioas helper Date: Thu, 27 Jul 2023 00:23:08 -0700 Message-ID: <5d7d275ff12c1c991ac80392b19f1ebf5214177d.1690440730.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT056:EE_|LV8PR12MB9184:EE_ X-MS-Office365-Filtering-Correlation-Id: 62e2367d-5063-47f0-9c6f-08db8e726b16 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lsNwwzPGJPWOEfSiQgT39JohCDQ4uR+IrNoM9wS+hCTT6CRGHsKAYZe4x4xOafepwOh/zHNqPEbAa9AINqMjNV02CIIV31GIg5vh3EYWWhnrenFpmxXVGMSBN+wVzeznV3O3nuaJfdES3igN9Dbey5Yme1JO5o9VLcH1GM2SLKcbKnDiRbQNrmxdpGxja5RCCjwyaCwmsYtOWXldlmI2qW5GGtPECzrhnDsx1wB6xcSpkIScqY9W7XyyqY6VqIPsiNinHxhTb0qAoUN+oOzMHItBzGZnPzThTLcExG91Au5BqjHksnsyD7oZCWklW+TlW6a0DkpMJBfFw0IrCqkYNPg253drhSzalksC2bp7pzRH6f/tycZzYAusDlZ8/0RVhOm/vBqvan0RdkwsDrosZZ/kuxCQSS1qbnDjDhkP4j0qsVeD4gsszukdFvc9dJBmJsTq6hVpb3iTtw2tHOuM9pD4BZ2Moxz6tbexvhGkKJ5OGPYMz1T4EWeFnWb+uq2EgumiSKPAHwTEU41N+cUrAgeKquNLtKrqFTaYDJ6zVF6soRCa58+QWP8SWLNLB7qaJaG3tnAT50fj4il7lv60uC2t3c80dCX33Xq8/5f9oulzODS4UNKrf2lUck8PKu4uhG68H7N7hRmu1nui1tn9CH2jsNpnOKVs6I6J4hx07ywtP/JNSPGpSxtPd42nED5e67t4h4AaX7qjcY3eFTd/uG/ncpdfi00b9ceY2yYvrZ3swr5xyIPC3yY6KCey6MQX 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:(13230028)(4636009)(396003)(346002)(39860400002)(136003)(376002)(451199021)(82310400008)(46966006)(40470700004)(36840700001)(7696005)(2616005)(26005)(6666004)(478600001)(356005)(82740400003)(110136005)(54906003)(7636003)(186003)(336012)(426003)(4326008)(83380400001)(36860700001)(47076005)(70206006)(70586007)(5660300002)(8936002)(8676002)(7416002)(40460700003)(2906002)(41300700001)(316002)(86362001)(40480700001)(36756003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 07:23:47.6179 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 62e2367d-5063-47f0-9c6f-08db8e726b16 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: CO1NAM11FT056.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9184 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, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772560886218160022 X-GMAIL-MSGID: 1772560886218160022 The complication of the mutex and refcount will be amplified after we introduce the replace support for access. So, add a preparatory change of a constitutive helper iommufd_access_change_ioas(), to take care of the existing iommufd_access_attach() and iommufd_access_detach(). Also, update the unprotect routine in iommufd_access_destroy_object() to calling the new iommufd_access_change_ioas() helper. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 112 ++++++++++++++++++++------------- 1 file changed, 69 insertions(+), 43 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 7a3e8660b902..d9680a247e1c 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -684,17 +684,69 @@ void iommufd_device_detach(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); +static int iommufd_access_change_ioas(struct iommufd_access *access, + struct iommufd_ioas *new_ioas) +{ + u32 iopt_access_list_id = access->iopt_access_list_id; + struct iommufd_ioas *cur_ioas = access->ioas; + int rc; + + lockdep_assert_held(&access->ioas_lock); + + /* We are racing with a concurrent detach, bail */ + if (cur_ioas != access->ioas_unpin) + return -EBUSY; + + if (IS_ERR(new_ioas)) + return PTR_ERR(new_ioas); + + if (cur_ioas == new_ioas) { + /* Do not forget to put since we allow a duplication */ + iommufd_put_object(&new_ioas->obj); + return 0; + } + + /* + * Set ioas to NULL to block any further iommufd_access_pin_pages(). + * iommufd_access_unpin_pages() can continue using access->ioas_unpin. + */ + access->ioas = NULL; + + if (new_ioas) { + rc = iopt_add_access(&new_ioas->iopt, access); + if (rc) { + iommufd_put_object(&new_ioas->obj); + access->ioas = cur_ioas; + return rc; + } + iommufd_ref_to_users(&new_ioas->obj); + } + + if (cur_ioas) { + if (access->ops->unmap) { + mutex_unlock(&access->ioas_lock); + access->ops->unmap(access->data, 0, ULONG_MAX); + mutex_lock(&access->ioas_lock); + } + iopt_remove_access(&cur_ioas->iopt, access, iopt_access_list_id); + refcount_dec(&cur_ioas->obj.users); + } + + access->ioas = new_ioas; + access->ioas_unpin = new_ioas; + + return 0; +} + void iommufd_access_destroy_object(struct iommufd_object *obj) { struct iommufd_access *access = container_of(obj, struct iommufd_access, obj); - if (access->ioas) { - iopt_remove_access(&access->ioas->iopt, access, - access->iopt_access_list_id); - refcount_dec(&access->ioas->obj.users); - access->ioas = NULL; - } + mutex_lock(&access->ioas_lock); + if (access->ioas) + WARN_ON(iommufd_access_change_ioas(access, NULL)); + mutex_unlock(&access->ioas_lock); iommufd_ctx_put(access->ictx); } @@ -761,60 +813,34 @@ EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, IOMMUFD); void iommufd_access_detach(struct iommufd_access *access) { - struct iommufd_ioas *cur_ioas = access->ioas; + int rc; mutex_lock(&access->ioas_lock); - if (WARN_ON(!access->ioas)) - goto out; - /* - * Set ioas to NULL to block any further iommufd_access_pin_pages(). - * iommufd_access_unpin_pages() can continue using access->ioas_unpin. - */ - access->ioas = NULL; - - if (access->ops->unmap) { + if (WARN_ON(!access->ioas)) { mutex_unlock(&access->ioas_lock); - access->ops->unmap(access->data, 0, ULONG_MAX); - mutex_lock(&access->ioas_lock); + return; } - iopt_remove_access(&cur_ioas->iopt, access, - access->iopt_access_list_id); - refcount_dec(&cur_ioas->obj.users); -out: - access->ioas_unpin = NULL; + rc = iommufd_access_change_ioas(access, NULL); + WARN_ON(rc); mutex_unlock(&access->ioas_lock); } EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD); int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) { - struct iommufd_ioas *new_ioas; - int rc = 0; + struct iommufd_ctx *ictx = access->ictx; + int rc; mutex_lock(&access->ioas_lock); - if (WARN_ON(access->ioas || access->ioas_unpin)) { + if (WARN_ON(access->ioas)) { mutex_unlock(&access->ioas_lock); return -EINVAL; } - new_ioas = iommufd_get_ioas(access->ictx, ioas_id); - if (IS_ERR(new_ioas)) { - mutex_unlock(&access->ioas_lock); - return PTR_ERR(new_ioas); - } - - rc = iopt_add_access(&new_ioas->iopt, access); - if (rc) { - mutex_unlock(&access->ioas_lock); - iommufd_put_object(&new_ioas->obj); - return rc; - } - iommufd_ref_to_users(&new_ioas->obj); - - access->ioas = new_ioas; - access->ioas_unpin = new_ioas; + rc = iommufd_access_change_ioas(access, + iommufd_get_ioas(ictx, ioas_id)); mutex_unlock(&access->ioas_lock); - return 0; + return rc; } EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); From patchwork Thu Jul 27 07:23: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: 126762 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp926883vqo; Thu, 27 Jul 2023 00:50:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlFoy/q+eai2u2/WoANS4pBYSIlvzKIKblDCQ2GaUHTNlvkTaKZK6cfMVGEJ8POPyhFqMYU3 X-Received: by 2002:ac2:520b:0:b0:4fe:d0f:b4f7 with SMTP id a11-20020ac2520b000000b004fe0d0fb4f7mr954913lfl.65.1690444234678; Thu, 27 Jul 2023 00:50:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690444234; cv=pass; d=google.com; s=arc-20160816; b=r5+zHsEjvl47SJPvl+sxPs8kK51SskHUfPoAWznlP3GmnbFSeOhnHimOM89UyTxKL6 nHsgUwapMAd9TQ9etDgCZhaoNZ/oJ+hprNJZ2M7FO3Aj08X2hzYHx73/erV3JrhdTu46 LxFSWw73OWxFH5n7IaUeQyaiOVCe5IDt1SPM+od+x9CHxKfCdUJ9cYfXXLjgK98efK0/ awCyeyQOO7+JXNWrLPBO8cdjC/eizKCG1H4HkOPTdA+7lQWohyvdZ9K3MdRRsGahMhqA IVnzhdOjdhvTVxnU6i1vcd1OiwEYJIjrEq97M21FhzcKfpsi1/wZHb29VYZ/bLPyfu5H mSVQ== 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=RYL3eJbt8G8CnDGcPPANlJZLEdDOJZmnWNcYEYajIsE=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=fwR5IYTb0fE5C/dwA+5YK2Iu/BEGQIOy4cEeIfMoV7pqHnk0bZfdfbKt+Td2xthvsG O79Ss3i0jTKkqeEyba99/G4zLrRf6vBYgdi7vP5FhOxnwVJBj8dpOIg+6s2g2kDgSRh6 uth66Aiiv+qzC9GId3n6R/y/lxRFQ4WUyM13gVm/zTyP7xQWyD/Yb7kGHP67XjX6VCyi qF7cb1tyVkJfbS1VIWIfaalneBJJpB96dk/pRGlktpj8zuraLncZpo0LBXmb5yUzFe0J TLRWm0+51wGWjQg378F69wA0dvHZit03vS4rgqhOB9Lxc6zrgnpOnhVoi3dbdxoK7M2U vGjg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=Q05oFzF5; 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 a23-20020aa7d917000000b0052230deabf5si516665edr.549.2023.07.27.00.50.10; Thu, 27 Jul 2023 00:50:34 -0700 (PDT) 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=Q05oFzF5; 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 S233468AbjG0Heo (ORCPT + 99 others); Thu, 27 Jul 2023 03:34:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233342AbjG0HeH (ORCPT ); Thu, 27 Jul 2023 03:34:07 -0400 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam02on2049.outbound.protection.outlook.com [40.107.212.49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E10CA9EF4; Thu, 27 Jul 2023 00:23:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CkGnuKQqE4qHnQ2uLihJZcV7jotw+A2fx5VRd6hGbqxdyAT/muQL5AhZOhGH9E5hK58q+Dkqb0GsejbQaRd2XNHH7IehP2wJ5v1guwFJ6ztPc0j+EMeqetZF9fhtatWKMgD6h4hxx8LS7/blQ4dhfglSBD748q+usBrT4rRW8RpDnVg+Wb9jjcCl+80dNPLlp45WozdYtoFHoapN4qeM8E/SoIdkDSZPuF/UmjwQWf93XM26O+jOz75asKljbm4omtKlpU9OkN0LIu9DXcoxghEzOAfeQ+XyoAwO1Pt4kloyrSEnptYfUBgTondAR83hUz1Bj1ymrd4HGyo7aJUcKQ== 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=RYL3eJbt8G8CnDGcPPANlJZLEdDOJZmnWNcYEYajIsE=; b=QIozWAqURWk369+yycc1jy7w5nLLGJxNF7XcysBmrFXj0kLAPf3Y9FQ4pyGUqSuR7SNUI6NfvhRxia6f69XkS/AmdBofezl+sWkv0QSdMerlGrBzQlbraqEg9zfRo4xUUFyLvtg6sWSFCnjBbyeYYeacR/iokz6/9LSkWShE+DyMMT4dszqpEKt5+ygYfRzl7JFwBwXCHjCpxRAksir++gipSSA2hUmp5M/nweabGmxdUGtj1FrOavDmn/8JdB3MsTpTo0ZvOfCgqloW7Oxo3XprkyjXWYWW6tTVUH7e7/2HUrzQgWaYEtuu/xaFNzuBwE9ApQ2Zv74JkDNHWi0D4w== 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=RYL3eJbt8G8CnDGcPPANlJZLEdDOJZmnWNcYEYajIsE=; b=Q05oFzF5YnNv9Sp1s/MNU+rMsSkT4JYdi29L0XsC64auSknNaVWdvFyw1lWWZhczziJRRD2kjQpaQ923notRvRgOA5deUgBTZglk0li8X/8cf7B6qpgyTQYiB1IVO6TWZZingCmRVfa9fFGYKEENUHX4J+ti9mr6Co6Yjr+F/w5EkFtvRIUvLno8klegM4xnp1B1ovR4kwjABgmKWhe4jQSiSxjiYMoBWq5Ygnch4PVyb+Xw+xpTlrXWgYFL70UByEUXpdlLToyE2SlAkelqAe8iQ1liIM2YYVnYnckQMtJe1nWIELwuoQGQkFdtmJ69Kx9fb2sM5G2ziZuXIgwP+Q== Received: from MW4PR03CA0304.namprd03.prod.outlook.com (2603:10b6:303:dd::9) by SJ1PR12MB6146.namprd12.prod.outlook.com (2603:10b6:a03:45b::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Thu, 27 Jul 2023 07:23:44 +0000 Received: from CO1NAM11FT027.eop-nam11.prod.protection.outlook.com (2603:10b6:303:dd:cafe::ca) by MW4PR03CA0304.outlook.office365.com (2603:10b6:303:dd::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:44 +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 CO1NAM11FT027.mail.protection.outlook.com (10.13.174.224) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:43 +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.5; Thu, 27 Jul 2023 00:23:32 -0700 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.37; Thu, 27 Jul 2023 00:23:32 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 00:23:31 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v9 4/6] iommufd: Add iommufd_access_replace() API Date: Thu, 27 Jul 2023 00:23:09 -0700 Message-ID: X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT027:EE_|SJ1PR12MB6146:EE_ X-MS-Office365-Filtering-Correlation-Id: ddb42a02-2069-43b0-fd2f-08db8e7268b0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1UVevKcElHe0QdPjh56wyNxCYJt5ABFXvz+QxNXwDcejTPJf9WSQN5EUzOZZH0GzHPUJqDnFrykRwfAsrYPOsc0eMySYKJ3M95E2j1R8/neze+xl0Q9J6y14q8moUCeRgPtKKxYNuQuhplqbsorbVM52gx6PR4eq1G2hpnf9a1dHpaCX+Bt+X8lkXuZUauzAsrMzeKIio9bKqncvccyBG35rKi5N/CQ4iDQYHNzSaAz4FZcbQ6R5r6uWwgv4SXoFI9QhnXtqLe8yohi1ThNHYa1/OvyY/xfaoVmTrV8EGxoxYaUe+EsjZgob84hhDPTpBNbUxyGx8XnUi8yY0/7rC1HfR1Ncs0eZhUH5i59lK1Eckd+44hxgXezsoOiY5kucOmnJw9IoxEvnYg16/3k96yzyNwN817HOLsrfZMXISDNnnYjTJwSdqVk7NMlb7xflsS40YKqfD+uMe/4v68zSuq3loet2Nec+PU2MtXqyKaL24e5YpKPESKRWBYSNjj6902lZWmtgLZFNm9eS5UcJG07aw5yV2sLBnZ4mIKZUJ+1Wic9gDD+pa0TglLT6y/xIv9+2czO0R2Qbjw0i01b52bokAk+MIF+kRnFq6SUqBoYgGRsZNuQOJZYCs8JicH0lV0/Cu+sixVMtRJdkVz8eYWeu+tsSV26hrYhacOa5U7VkXWJiBYWfqdZqlcBCOuhmOlHLZA9EX7dkUTKwOVJZC1rIJ6i7Ffr5dU+OubS8P9gxUS6le9AJF+FCbsnqK+O1 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:(13230028)(4636009)(136003)(39860400002)(376002)(346002)(396003)(82310400008)(451199021)(46966006)(36840700001)(40470700004)(2906002)(316002)(8676002)(8936002)(7416002)(5660300002)(40460700003)(41300700001)(36756003)(40480700001)(86362001)(426003)(7696005)(6666004)(110136005)(54906003)(82740400003)(478600001)(7636003)(356005)(26005)(336012)(186003)(2616005)(83380400001)(70206006)(70586007)(36860700001)(47076005)(4326008);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 07:23:43.5965 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ddb42a02-2069-43b0-fd2f-08db8e7268b0 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: CO1NAM11FT027.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6146 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, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772559253512566146 X-GMAIL-MSGID: 1772559253512566146 Taking advantage of the new iommufd_access_change_ioas helper, add a new iommufd_access_replace() for VFIO emulated pathway to use. Suggested-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 17 +++++++++++++++++ include/linux/iommufd.h | 1 + 2 files changed, 18 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index d9680a247e1c..2dfeb65c7090 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -844,6 +844,23 @@ int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id) } EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id) +{ + struct iommufd_ctx *ictx = access->ictx; + int rc; + + mutex_lock(&access->ioas_lock); + if (!access->ioas) { + mutex_unlock(&access->ioas_lock); + return -ENOENT; + } + rc = iommufd_access_change_ioas(access, + iommufd_get_ioas(ictx, ioas_id)); + mutex_unlock(&access->ioas_lock); + return rc; +} +EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, IOMMUFD); + /** * iommufd_access_notify_unmap - Notify users of an iopt to stop using it * @iopt: iopt to work on diff --git a/include/linux/iommufd.h b/include/linux/iommufd.h index 0ac60256b659..ffc3a949f837 100644 --- a/include/linux/iommufd.h +++ b/include/linux/iommufd.h @@ -49,6 +49,7 @@ iommufd_access_create(struct iommufd_ctx *ictx, const struct iommufd_access_ops *ops, void *data, u32 *id); void iommufd_access_destroy(struct iommufd_access *access); int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id); +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id); void iommufd_access_detach(struct iommufd_access *access); void iommufd_ctx_get(struct iommufd_ctx *ictx); From patchwork Thu Jul 27 07:23: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: 126780 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp938552vqo; Thu, 27 Jul 2023 01:16:21 -0700 (PDT) X-Google-Smtp-Source: APBJJlGPOs9N9b87LsJw8GDgPTC0NHWm7335lnMtpZwVunbY1+c3b68x/Mohipdzv+ioaSN5kYCF X-Received: by 2002:a17:906:6b92:b0:993:d782:a3db with SMTP id l18-20020a1709066b9200b00993d782a3dbmr1341302ejr.16.1690445781591; Thu, 27 Jul 2023 01:16:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690445781; cv=pass; d=google.com; s=arc-20160816; b=cpgd3rnRPLlvaLQu7b3kznH6j7CV/9jWPnpjTqDagYfZAPuczzJ/eFWLJWXmMJiTQ0 ctk1dU68TuDaz2FdgdHtOSiTold6bL9oobZao89ohttC+eOFOAfrQtbiJIp+WHLeZTTv L2UOgJy7OwqWj5ChlgDBkHu9/F4dyk6KaoeGF+Lun8tjOTkMrhmagMp3TzwQs6z5N4p4 fCGNkKPBEvX5Lqiy0JNAIeV95KVgifipDHl5r12jSLfBw6eTF1RKL7DXqY1j3f9QGvqm u4jb9ctD7X89ytXmRiemsyj+zfEnVlAJB0BcRElt/ukxHL7s9vifbuK2MT6WU5iqnDG6 aCWQ== 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=iirMKJG1l4d932PYYOqkXDT5WydmhkNOrAq8wo1jKiw=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=tBF0w4Qt66diX/nbPdgmyF/1zMmO5mv1zDu/IKCYLQhUjx62EMwPF8sWgNxd15BX3a y7Da9TAlL5dXQNaNfvKZrynS6lftlbb1WmnR0anOc9r8pu4yZ6Z6/iuE4GeLPj1o3Jud Yc0JkZg0uAj3R0H72FJc827ZTQRzYas1Qo/RwByiPn8FZmt9rvzF0OmMsactaET5LCTW EEsR2LMf5SzMWBXfcKz3XUToTZV+jfTRgbGsR95xRJcdeXRoTcuYZiQCNXNzm3G3Tyoj qf/leeZRC42lCvP6G/FkyzpSWZiZ9xFBYhNLsVK4gJNEhuzeDMZXvP8EqzBIoXCxXbw3 7k3w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b="e26Ktn/d"; 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 l12-20020a1709066b8c00b0099bc79a3897si654045ejr.886.2023.07.27.01.15.57; Thu, 27 Jul 2023 01:16:21 -0700 (PDT) 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="e26Ktn/d"; 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 S233460AbjG0Hek (ORCPT + 99 others); Thu, 27 Jul 2023 03:34:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233281AbjG0HeF (ORCPT ); Thu, 27 Jul 2023 03:34:05 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2056.outbound.protection.outlook.com [40.107.102.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02D209EEE; Thu, 27 Jul 2023 00:23:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JepxWdGO2yPomhsisjjnzrr+bOf//+4Bizda7Oo/ttMQHThSrULbN2sfIgWGN6nZF4hxCNZrQpkSyf8txxrLVRh1AjzONi1Kxo9L78O6ZTcwuYrN7aS0EXZLappceg8SDJQ9LjefB9cLxgZudzczuMzaZaS4/5Ty4pV8vrlR20frkTGQq5nNaUtNIC0Jxj7d+lPuxMJaquxzbO1bHFOoiWOUPw4OCixhAk8I/nCnaDUyS1w3fVNC+KE2e+7mT167IBxam2ETHn8ghpMA/1sB0ikLkKvSmPCxBhGnNVaXNy4XHEPkxkJQUyKY1P0W49eXUTSi85WZB+s7muvGtZZz+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=iirMKJG1l4d932PYYOqkXDT5WydmhkNOrAq8wo1jKiw=; b=Bqai0eqMMUNPBZtqz7EQhBMIZN2WmER09ssxaK8Law/IxY7y1xvmCIU7fnyKewIJH77+LZIBHJUVkswYJixF92BCowG3zJqj3ULnLtCVKvHXvt/ptVjb2wYunrh6JddEn/FGrUFVMqTVfLH3YytJLxfzMxjygPGjws7rCYBMbwaHXRNLoabEB2HpuBnR7vY7o9GStY/k+IzartZ5YvtQWJy0D6JjGtWKyQYF5+fOIL6EuhOe9ftj1aLzYbGY1NyjQms79+9WrZkShm/g7AoyBljLiHRWjBue52jVcKCVocrGcayK0SfQX7heEW31nUeFjzhfz3Lj8KVIQFFFQPsSbg== 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=iirMKJG1l4d932PYYOqkXDT5WydmhkNOrAq8wo1jKiw=; b=e26Ktn/dxIPEwtqruHJw/sJKgyFKRiS3rPqkC7I+UIE8EYqTPZzRQyjXDCwxmQLXuH++7gym7wZzGzqDoKCW3YMKBSBB/QgDyPDCgPRKgZOdVjabWHy8B/BODEoE6EwjsqCCFRgJWfvDkFFv1d66ihP6wX0ne4XLfLkjaP9MZUnKZRbEBPEMrjtf6yNQ1pRmwTClZjz3FsoT782NY+kgkElEf51o9DPIhlVa1lAjzNhoOrE8Gy54SwA4Wy4BouKJoDOJtbTd3Iczegebe/YGAAn2IqLxrGZpCr5l40jGc4r+AYv+gPeh1CTT9IN/i9ByjErsKYlYxd8wGwO4dgBhXw== Received: from MW4PR03CA0327.namprd03.prod.outlook.com (2603:10b6:303:dd::32) by MW6PR12MB8759.namprd12.prod.outlook.com (2603:10b6:303:243::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.24; Thu, 27 Jul 2023 07:23:45 +0000 Received: from CO1NAM11FT027.eop-nam11.prod.protection.outlook.com (2603:10b6:303:dd:cafe::12) by MW4PR03CA0327.outlook.office365.com (2603:10b6:303:dd::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:45 +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 CO1NAM11FT027.mail.protection.outlook.com (10.13.174.224) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:45 +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.5; Thu, 27 Jul 2023 00:23:33 -0700 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.37; Thu, 27 Jul 2023 00:23:32 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 00:23:32 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v9 5/6] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Date: Thu, 27 Jul 2023 00:23:10 -0700 Message-ID: <3168353d5c77140c5a8d3845ef06f34bac57eada.1690440730.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT027:EE_|MW6PR12MB8759:EE_ X-MS-Office365-Filtering-Correlation-Id: 6bbb8a56-762c-423b-367f-08db8e7269f5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: E2LcpgPWEcKCf2gIw1ZXeSe2cyZiR5/fKJxuFDNI/CKNgCi99MrJxCv5TcEp6TleoJq4zXrwiPqVT2Ve9rlSBWIut1NdtbtxrO/Gfdkq6ItH4rRD5++7PHnVK14BLM5iKIG13QXlEdXTdW0HZ3kHFJLidASwwxCSG8JrvvwdOM7ZDbDa1ldkSUvXN/UjyWPR5fY+CLQxpKDVqjIQ5F2wsM/NtsrFJxMcXDLGu1aF25tDZ6o6j5qW3UgrG2AAbuavgJOh3okm5IlfI22CMTS+uSqeGjlV/YtZgyT4rvdHk0T2GtClh6C3GYVzvt/hiJdi6vmCPOr9XjPuDOoe9SADCMf13c+zmN6VlxOa7Ws7kM2jLESVpBLCL/iex8LIatQ9WpMOcvQpXyY1ip5wCRhgAa1Zs+JSQRLwixAeuXEADQNJTxjedHAZjnUNAPdsJ99vyxgt4Ndh0ayzpA7zmn8USZFSRPYzg1L+hW2IWQptKOoiNSlL+PEMkhADMD4VCLQYTYLrzN1pIWXSkKSPx211zmnPtWIIA8csJTcsCQvmJTL0Fy01E5OCqVCD1iM64vlWB68sWw2z34Tmnrt2tuCKpwDq+Kd4joWY6fp0kMEx/8ifyBX14vlWrUdt2upR0PA7+VW3sE3444a9E0cXsYV1vt/z7AmXdkd87yny8FO2GJZqHD+Aua7HfpS5g6wLg3vcfhAA3ZgG63h0wrDAmhWvLrSxINO4sYHlJIdcDN8R7ZU= 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:(13230028)(4636009)(346002)(376002)(39860400002)(396003)(136003)(451199021)(82310400008)(36840700001)(46966006)(40470700004)(54906003)(36756003)(6666004)(356005)(110136005)(478600001)(7696005)(86362001)(40480700001)(186003)(336012)(26005)(7636003)(40460700003)(316002)(8936002)(70206006)(4326008)(70586007)(8676002)(5660300002)(7416002)(82740400003)(2616005)(47076005)(2906002)(426003)(41300700001)(36860700001)(83380400001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 07:23:45.7213 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6bbb8a56-762c-423b-367f-08db8e7269f5 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: CO1NAM11FT027.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB8759 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, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772560875796336513 X-GMAIL-MSGID: 1772560875796336513 Add a new IOMMU_TEST_OP_ACCESS_REPLACE_IOAS to allow replacing the access->ioas, corresponding to the iommufd_access_replace() helper. Then add a replace coverage as a part of user_copy test case, which basically repeats the copy test after replacing the old ioas with a new one. Reviewed-by: Kevin Tian Reviewed-by: Jason Gunthorpe Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/iommufd_test.h | 4 +++ drivers/iommu/iommufd/selftest.c | 19 ++++++++++++ tools/testing/selftests/iommu/iommufd.c | 29 +++++++++++++++++-- tools/testing/selftests/iommu/iommufd_utils.h | 19 ++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/iommufd/iommufd_test.h b/drivers/iommu/iommufd/iommufd_test.h index dd9168a20ddf..258de2253b61 100644 --- a/drivers/iommu/iommufd/iommufd_test.h +++ b/drivers/iommu/iommufd/iommufd_test.h @@ -18,6 +18,7 @@ enum { IOMMU_TEST_OP_ACCESS_RW, IOMMU_TEST_OP_SET_TEMP_MEMORY_LIMIT, IOMMU_TEST_OP_MOCK_DOMAIN_REPLACE, + IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, }; enum { @@ -91,6 +92,9 @@ struct iommu_test_cmd { struct { __u32 limit; } memory_limit; + struct { + __u32 ioas_id; + } access_replace_ioas; }; __u32 last; }; diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 9d43334e4faf..bb2cd54ca7b6 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -785,6 +785,22 @@ static int iommufd_test_create_access(struct iommufd_ucmd *ucmd, return rc; } +static int iommufd_test_access_replace_ioas(struct iommufd_ucmd *ucmd, + unsigned int access_id, + unsigned int ioas_id) +{ + struct selftest_access *staccess; + int rc; + + staccess = iommufd_access_get(access_id); + if (IS_ERR(staccess)) + return PTR_ERR(staccess); + + rc = iommufd_access_replace(staccess->access, ioas_id); + fput(staccess->file); + return rc; +} + /* Check that the pages in a page array match the pages in the user VA */ static int iommufd_test_check_pages(void __user *uptr, struct page **pages, size_t npages) @@ -1000,6 +1016,9 @@ int iommufd_test(struct iommufd_ucmd *ucmd) case IOMMU_TEST_OP_CREATE_ACCESS: return iommufd_test_create_access(ucmd, cmd->id, cmd->create_access.flags); + case IOMMU_TEST_OP_ACCESS_REPLACE_IOAS: + return iommufd_test_access_replace_ioas( + ucmd, cmd->id, cmd->access_replace_ioas.ioas_id); case IOMMU_TEST_OP_ACCESS_PAGES: return iommufd_test_access_pages( ucmd, cmd->id, cmd->access_pages.iova, diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c index dc09c1de319f..8acd0af37aa5 100644 --- a/tools/testing/selftests/iommu/iommufd.c +++ b/tools/testing/selftests/iommu/iommufd.c @@ -1283,7 +1283,13 @@ TEST_F(iommufd_mock_domain, user_copy) .dst_iova = MOCK_APERTURE_START, .length = BUFFER_SIZE, }; - unsigned int ioas_id; + struct iommu_ioas_unmap unmap_cmd = { + .size = sizeof(unmap_cmd), + .ioas_id = self->ioas_id, + .iova = MOCK_APERTURE_START, + .length = BUFFER_SIZE, + }; + unsigned int new_ioas_id, ioas_id; /* Pin the pages in an IOAS with no domains then copy to an IOAS with domains */ test_ioctl_ioas_alloc(&ioas_id); @@ -1301,11 +1307,30 @@ TEST_F(iommufd_mock_domain, user_copy) ASSERT_EQ(0, ioctl(self->fd, IOMMU_IOAS_COPY, ©_cmd)); check_mock_iova(buffer, MOCK_APERTURE_START, BUFFER_SIZE); + /* Now replace the ioas with a new one */ + test_ioctl_ioas_alloc(&new_ioas_id); + test_ioctl_ioas_map_id(new_ioas_id, buffer, BUFFER_SIZE, + ©_cmd.src_iova); + test_cmd_access_replace_ioas(access_cmd.id, new_ioas_id); + + /* Destroy the old ioas and cleanup copied mapping */ + ASSERT_EQ(0, ioctl(self->fd, IOMMU_IOAS_UNMAP, &unmap_cmd)); + test_ioctl_destroy(ioas_id); + + /* Then run the same test again with the new ioas */ + access_cmd.access_pages.iova = copy_cmd.src_iova; + ASSERT_EQ(0, + ioctl(self->fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_ACCESS_PAGES), + &access_cmd)); + copy_cmd.src_ioas_id = new_ioas_id; + ASSERT_EQ(0, ioctl(self->fd, IOMMU_IOAS_COPY, ©_cmd)); + check_mock_iova(buffer, MOCK_APERTURE_START, BUFFER_SIZE); + test_cmd_destroy_access_pages( access_cmd.id, access_cmd.access_pages.out_access_pages_id); test_cmd_destroy_access(access_cmd.id); - test_ioctl_destroy(ioas_id); + test_ioctl_destroy(new_ioas_id); } TEST_F(iommufd_mock_domain, replace) diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h index 53b4d3f2d9fc..70353e68e599 100644 --- a/tools/testing/selftests/iommu/iommufd_utils.h +++ b/tools/testing/selftests/iommu/iommufd_utils.h @@ -119,6 +119,25 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, #define test_cmd_hwpt_alloc(device_id, pt_id, hwpt_id) \ ASSERT_EQ(0, _test_cmd_hwpt_alloc(self->fd, device_id, pt_id, hwpt_id)) +static int _test_cmd_access_replace_ioas(int fd, __u32 access_id, + unsigned int ioas_id) +{ + struct iommu_test_cmd cmd = { + .size = sizeof(cmd), + .op = IOMMU_TEST_OP_ACCESS_REPLACE_IOAS, + .id = access_id, + .access_replace_ioas = { .ioas_id = ioas_id }, + }; + int ret; + + ret = ioctl(fd, IOMMU_TEST_CMD, &cmd); + if (ret) + return ret; + return 0; +} +#define test_cmd_access_replace_ioas(access_id, ioas_id) \ + ASSERT_EQ(0, _test_cmd_access_replace_ioas(self->fd, access_id, ioas_id)) + static int _test_cmd_create_access(int fd, unsigned int ioas_id, __u32 *access_id, unsigned int flags) { From patchwork Thu Jul 27 07:23: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: 126835 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a985:0:b0:3e4:2afc:c1 with SMTP id t5csp962288vqo; Thu, 27 Jul 2023 02:10:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlEDmeAT5gmmIhA7D4oubDayOJx0J+iex5XkLzYnscpX7aYUV6vO8JjWZfOGZYKKOWNDNX8O X-Received: by 2002:a05:6a21:778b:b0:134:8b50:47cd with SMTP id bd11-20020a056a21778b00b001348b5047cdmr4329358pzc.9.1690449056302; Thu, 27 Jul 2023 02:10:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690449056; cv=pass; d=google.com; s=arc-20160816; b=M6WQwV4gS3m/WTylvJRW32ODS+MRO6kJRJy74QUViTg5BQTe/eqKavqIAeg4BEvCEM GeQtNFuliP3f9joMl/aBvPDHJNVLB1a77JMABG71brR6+iIU9Wbha0sSNxsw54ODTSYH UQSXURdvFANVGTmmXSWxmUj5rSG+QlSdG6XtWRTwIE1ROv/SZRDjIju1jj8N7NKZB7rJ MWdmGuSeDNWe5LVZ+6i5qMv2aEwiGekLe7N86R5N3inez6jnGMHqzuJd1BbJuq7b8ABs 0x+dou7i8moKYt6tQvBw687+FLOmcDq5nIhjhFCuWQ6vIMs15GNkv7X5D3p+CRl8pbeV T0kA== 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=R6aD3dt1PckeOKkxIclyv3iBH0qTiumx46+55bGQRk0=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=slNBjDDkWaBRglLOiPM2BffW4nVmMHqofqR7XpzXksAyMwZ6jwT+ioxqQQ9Op2dD7s nHkpNFwt84I0kQx/aQyfoERr8rBotOwIy6fOPR4xi+U9ZZzjaH4v6FscwCGqJelF7eN8 g7Q8XNwN9XS+MClPpTsEKEx/Fcm9kMThu2buuAcrPyeZ+Q5OmpfnOlsZuAWdcxLpCCn8 ivn+8KT5ylD9UMcJysxpz7kh7HvldM1aWZMRylwPDFh4Ujmm0crGIMdXU4Vuxw5d4Yp1 QCSF6nQIpdANHApZiHJIX3CN1FuBhWJePcsUNLq2tC4ThL/4aWyaIcJQj98WJ7Gj1vsi MlGQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=b2FrvPN8; 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 jz6-20020a170903430600b001bb02e4b739si972614plb.475.2023.07.27.02.10.42; Thu, 27 Jul 2023 02:10:56 -0700 (PDT) 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=b2FrvPN8; 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 S232371AbjG0Heu (ORCPT + 99 others); Thu, 27 Jul 2023 03:34:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233420AbjG0HeI (ORCPT ); Thu, 27 Jul 2023 03:34:08 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2048.outbound.protection.outlook.com [40.107.243.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C88CA240; Thu, 27 Jul 2023 00:23:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N7EhGGAJXZnkWNpuDsIbOm2xMBC2gp5hKcGNQUA2ym+xs/2JAK/TAY9HUuSyOt6JONec7+2TU7s/rMTY9toP+1TxUM1wf1nD9swQH2EW/mz2emVGKtUxcCISxI8kt40292JJecMZwsRjGOKGu/K90+or7bMjkfXNjQBrllQidsl7GIPxXmMi5wIB4wSs+2aNxN3vhr/1FhvMTZaAnlrHFR8GZMc/xNOQDnRMCRbxIjWPfW6mxNverDxgx6tSPch2qXxHWlBN0kFOeSZ1bsQKLg/Vi22JFuJT0CT/rwiJeZGdqm2DwFCImTDIDlettGezf88Ir0YUqxLEgV4SFEkLhA== 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=R6aD3dt1PckeOKkxIclyv3iBH0qTiumx46+55bGQRk0=; b=NiMhsSCXVHZgRx/kimOxK2bPdu4RxTSh0/cUA28gFlc83cKM4E9JzNfyUpt0XDdXMJj6UxLZn+NcDdzLr4HqnPskp4ZrtBxhDOyLmP5wB8w/cnOtpRxa8oE+wczAK+0QhR8T1e21tco/ct57jgFCWpyFH3bzbw3Wp7+SrUtk76vLejPgI2iP6acT0AGlpx25j+7bUWon1GoUkQH0jB0CIjmzWwcCBmMpeva9EeVpt86qEPokZUatFw52qKCvEM3IF74k4Bqus9u3e9NaC/nhpuA3/mkqNaWwgLB1fyxHPfh785ybS0u39ouAmt5aWw7x9paLdADUtX+wDBeALqP+Fw== 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=R6aD3dt1PckeOKkxIclyv3iBH0qTiumx46+55bGQRk0=; b=b2FrvPN8/ZFAYdqdo49fkVXqEQqgh+BfzqHFHqvBOZVktbCtpKgFf8u8VecDN7SO9fwqXewEoaeRDewC2BvgLnMEUZEmqPLTItsl4qeYIwLqjEq3WiP5J//PE0gXeiWDQ3nqyZz0cDLbmGh7kE2cH3NtDb3SDra/nb3nOan+7TokyMUECkD6or/dgrxSfpWp5vvB110hpf8GifbwP+Fpqhwir0o9EY6X8c+lb3Cu7X4NOpsUjvQQdydKe/H6bqWvHx+uv2HcjehVaZ8gw0pVfP1p27CHTYyidqvm4ApKbmNoA3pKaSXQxiehhwCashYIeKwwpuKjDpaXEBIGjs3u6A== Received: from MW4PR04CA0378.namprd04.prod.outlook.com (2603:10b6:303:81::23) by PH7PR12MB6538.namprd12.prod.outlook.com (2603:10b6:510:1f1::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6609.33; Thu, 27 Jul 2023 07:23:50 +0000 Received: from CO1NAM11FT056.eop-nam11.prod.protection.outlook.com (2603:10b6:303:81:cafe::d3) by MW4PR04CA0378.outlook.office365.com (2603:10b6:303:81::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:49 +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 CO1NAM11FT056.mail.protection.outlook.com (10.13.175.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Thu, 27 Jul 2023 07:23:49 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) 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.5; Thu, 27 Jul 2023 00:23:34 -0700 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.37; Thu, 27 Jul 2023 00:23:33 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.10) by mail.nvidia.com (10.126.190.180) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 00:23:33 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v9 6/6] vfio: Support IO page table replacement Date: Thu, 27 Jul 2023 00:23:11 -0700 Message-ID: <864d21e57894ba6fc6248d9380f52d0b424485fb.1690440730.git.nicolinc@nvidia.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CO1NAM11FT056:EE_|PH7PR12MB6538:EE_ X-MS-Office365-Filtering-Correlation-Id: a799da40-f644-40a6-0313-08db8e726c62 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QJ25TrUL7cYHm43q0srIClLXWcETlKd9hGrbezB2xjhczj8nYaRJ+BGsvJBWgnaQxSlU9puLZ1aEHWIXJ3VUQ53JV3eGcy5oTql8aW5bVjJLBtFHB2ONlGmATHcp+pa3HHOKiBz4hu24l3gUZXHSyDPrl9hWUEov9HrCZh/pubBZmLsC48YeykIX5yKrqTXo10KHCfy4vLDKpGKIsJKvqCl7uf3EBCzzQBeVAeqX7j+P19pmD/Y1NU8NZv3+VUMB9LdJ7myg+YJ35O57jk8fSUWsZnR2iCZfMzLiCpmtFwEHCbb/cIaGzBKHnh9hT7Pl0KZLNm3u9NMgZ7eSnTKBctBWQFRHGs/NhLWt0KYp/Qc/Ae2KFltGf0+o60J4n+eBmcCXuKU+MVlLB6B7VGo/IaKSzsJ4pnreMqySoOtXDkAzD00+mZtM1+xXBD4xLJ6Z+qoB2LR92WF3c8ZEXlWUDAZGi3XXsy9Gu5ePoE23Q2YffRK6Xogygcw8StqeQ9HxWHb3vklCs5EMa7XmpHDvgobIcPe23HSS3qjPcdb17kwmmOAg1EHqOtEWnsdr5ONrF+QsKPGOKCS31yLoxYmT1v9nhVnD/1w5QJbZyh7Es+jPMhcLsJIRcuOA7UTzYf7exicrfEeEVTkHfwQfq0LiJuF3GcZTxkzCiZ6ZYNvqN1MFhAZWyPYrhJg6Zr7JS/KSnccJc2rkMZWtid0Myqo1LWu3rdvadmE5h3POooUt8yx7vZHd9NNwZb7WnIdmQLYB 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:(13230028)(4636009)(136003)(376002)(396003)(39860400002)(346002)(82310400008)(451199021)(40470700004)(36840700001)(46966006)(7416002)(8936002)(8676002)(5660300002)(41300700001)(4326008)(316002)(2906002)(54906003)(70206006)(70586007)(26005)(6666004)(110136005)(336012)(186003)(40460700003)(478600001)(86362001)(40480700001)(36756003)(426003)(2616005)(47076005)(36860700001)(7696005)(83380400001)(356005)(7636003)(82740400003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Jul 2023 07:23:49.7898 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a799da40-f644-40a6-0313-08db8e726c62 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: CO1NAM11FT056.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB6538 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, T_SCC_BODY_TEXT_LINE 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: INBOX X-GMAIL-THRID: 1772564309923206348 X-GMAIL-MSGID: 1772564309923206348 Now both the physical path and the emulated path should support an IO page table replacement. Call iommufd_device_replace/iommufd_access_replace(), when vdev->iommufd_attached is true. Also update the VFIO_DEVICE_ATTACH_IOMMUFD_PT kdoc in the uAPI header. Reviewed-by: Kevin Tian Reviewed-by: Jason Gunthorpe Reviewed-by: Alex Williamson Signed-off-by: Nicolin Chen --- drivers/vfio/iommufd.c | 11 ++++++----- include/uapi/linux/vfio.h | 6 ++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index 4d84904fd927..82eba6966fa5 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -146,9 +146,9 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) return -EINVAL; if (vdev->iommufd_attached) - return -EBUSY; - - rc = iommufd_device_attach(vdev->iommufd_device, pt_id); + rc = iommufd_device_replace(vdev->iommufd_device, pt_id); + else + rc = iommufd_device_attach(vdev->iommufd_device, pt_id); if (rc) return rc; vdev->iommufd_attached = true; @@ -223,8 +223,9 @@ int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id) lockdep_assert_held(&vdev->dev_set->lock); if (vdev->iommufd_attached) - return -EBUSY; - rc = iommufd_access_attach(vdev->iommufd_access, *pt_id); + rc = iommufd_access_replace(vdev->iommufd_access, *pt_id); + else + rc = iommufd_access_attach(vdev->iommufd_access, *pt_id); if (rc) return rc; vdev->iommufd_attached = true; diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index fa06e3eb4955..537157ff8670 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -939,6 +939,12 @@ struct vfio_device_bind_iommufd { * Undo by VFIO_DEVICE_DETACH_IOMMUFD_PT or device fd close. This is only * allowed on cdev fds. * + * If a vfio device is currently attached to a valid hw_pagetable, without doing + * a VFIO_DEVICE_DETACH_IOMMUFD_PT, a second VFIO_DEVICE_ATTACH_IOMMUFD_PT ioctl + * passing in another hw_pagetable (hwpt) id is allowed. This action, also known + * as a hw_pagetable replacement, will replace the device's currently attached + * hw_pagetable with a new hw_pagetable corresponding to the given pt_id. + * * Return: 0 on success, -errno on failure. */ struct vfio_device_attach_iommufd_pt {