From patchwork Fri Jul 28 06:33:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 127405 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp271547vqg; Fri, 28 Jul 2023 01:04:33 -0700 (PDT) X-Google-Smtp-Source: APBJJlE4GKGycYB/rJpe2j0xh0eMYf0JIBUY/uAGzUURwDTvHbqD5jyWbtIFEUwHpjia1xtg2HAQ X-Received: by 2002:a17:902:d4ca:b0:1b6:6812:4ede with SMTP id o10-20020a170902d4ca00b001b668124edemr1311060plg.26.1690531473089; Fri, 28 Jul 2023 01:04:33 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690531473; cv=pass; d=google.com; s=arc-20160816; b=oe5PNN3Huo6gpNBTHFg9EClChSAKGUVwxt1axpccrro0+S1zQwdg4fRbmc9HIeMbHu 9JguYR2YsKRAO+tzO2w/DqkAbt6WvICgxz4qtCBrUtAnWwYdeS8FkvAwuRIskEWlrOn0 bCZ3L7hmP5DWYdi0kOfrLQM4ZKjUVzIeKI2mKux4S2bSGtynxozJatoo+xd4Elw370nB hypRmgJdTzWbw8U9oPxVDCONNpZVklPK+jMn4EnsCdehJyK1a/1o7IvxwZMRuhDmi9YE CmfhuEM8FM3F2NRbkJS+mYnoXnA8zl9Ws1kyzsApB7M5i9fu6fpFs5ollAfdd15c9000 mnQg== 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=XRyn+ybXuM1GJa6A+RnCh1OLtUMb9w/tFVDAuG96/zyZETtT2w1aVQg7gQIukZHHm5 AGHtN7bXWgSlQaA+bNksAE2a5lq61DcGevrxTjkNwYsoIHQXdS44zhyGAp0Xn9e4IO08 fx8mZT5HftyL1U9zFqiJJCq7r9UpwVNlZlGSrby2y1HY3aH/EhMB5psZI7WsvgiJct3r /pyJGbM+c+4+NqoRjbke7cEHLIltyBUuRkst9qkvr5nrzSB1a6UKXAbpJ+4wvWfUan7F ifp/qgr31eK2fXM2nI3PtUaOe2+7DzxTOaOhJct4oHyVAS8/sykQlycuFIJlnsD3sUbb xpOQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=TtBFUokr; 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 e5-20020a17090301c500b001bb40ba3ab2si2790647plh.213.2023.07.28.01.04.20; Fri, 28 Jul 2023 01:04:33 -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=TtBFUokr; 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 S233580AbjG1GeJ (ORCPT + 99 others); Fri, 28 Jul 2023 02:34:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231572AbjG1GeH (ORCPT ); Fri, 28 Jul 2023 02:34:07 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2055.outbound.protection.outlook.com [40.107.237.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7170730E2; Thu, 27 Jul 2023 23:34:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nvzsYlrB9RBM59Ycw/oio11ZDn7HF9rW1t+AFKnyutkKXeu59tz92CI2nRsb/szDQACsf57QeP3/q4IiHCgat3gnUvSYNZZsSGcJypb/TUmU9c5aU7O35Bhm3L0GA+X/koq9xdrVI3ickldCh4RhyQETIUwEuP2OP37AZmHYYau+UouMKIjxoJOYiFM5afuVg9xEVyIbL/uavtD6dmOKqDBj1GgolZie3YxGQowYPhJMzk6qkXRjP6Z9wrefdFnKNiuCpL68BdnbkaEYIXC01aYyNGH/Q91QlEe/PeVimITSVQ06VLGL1VWD2Iw7Ec216vQBmLFsnyj+O8xogFVxWw== 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=IxoVStPp/+jMMExBcesolYX+VPOAIthNIv++Ikwviqra8oJJMCmN+KA9WPrz4UvTQxmCL6z49eWvdoU8Bog9z+6dYBdvF4hT0EJDakMLhuWIY2GY7nSVIcdlZletsBSDvVCKys2WNe9z8Wt8yCMfeNv/8RvhSlZexFQLdGx2R2a9+rDWd8xxb2xNMcIwqmFjp2vT2SBkCjdEZ2BP4L5s7aAJqPBBxJflUevRvZ4dlTwR0msVojBYsPxUE7TesK1l3qxn0mdpxCyKGMOYe2AbA/LxIcJpk8nXk7vvlsawY1sdr5iekUNt3uCBpCcMzO37n6pQh7ZuHIAZSDsVf/dyIA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mUc06e5xx4kTwBKEnoGTOSehjX6v5imRbufbXUgPcoU=; b=TtBFUokru634ZMkm5NbYjIyQK5SgInuyvMURDrA44cec0wmH/XuHJrxwIw1VhlpNf5ks94/GTJ9HXisQN9tDgU7Texdjd/8ngzKYsZ2qnEpOWVyKZHOF1Cf8TshBlm4tsThKrDNc+XAazIkXPhdXgwQzFdQFk7k6acssvqtZGr+5JSZWcQyT0tGYAZ2v7tJ3HjyYrJEQ+VY1IWQmTA0WhnCZDMQsY2qhrY7qnaS2H8WhAQ+URtou+42poTBjkXJ2irRRYCDeQS+PH3ULY5f6xU72aSnrVadRCuI91Cy28mrIt3qlvB3xhORsUh4oicZDYXcJrLHztZm8GkDOg2Mp1g== Received: from SJ0PR03CA0051.namprd03.prod.outlook.com (2603:10b6:a03:33e::26) by BN9PR12MB5148.namprd12.prod.outlook.com (2603:10b6:408:119::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Fri, 28 Jul 2023 06:34:04 +0000 Received: from CO1NAM11FT036.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:33e:cafe::a) by SJ0PR03CA0051.outlook.office365.com (2603:10b6:a03:33e::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:04 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT036.mail.protection.outlook.com (10.13.174.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:03 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 27 Jul 2023 23:33:53 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail202.nvidia.com (10.129.68.7) 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 23:33:53 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 23:33:52 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v11 1/7] vfio: Do not allow !ops->dma_unmap in vfio_pin/unpin_pages() Date: Thu, 27 Jul 2023 23:33:23 -0700 Message-ID: <85d622729d8f2334b35d42f1c568df1ededb9171.1690523699.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: CO1NAM11FT036:EE_|BN9PR12MB5148:EE_ X-MS-Office365-Filtering-Correlation-Id: 0fe39433-052a-4fd5-e6b1-08db8f34a319 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0rTD5V1AYD65uEggi31YfYGHYtbuex/PVomchV0rFt+3yzaTVNzwGodtJK3Mz6161EcoqBwfaBgVYtz4384A1lJT0oAvVxrmo4G02bYo3H1+oyF3yxQCRytZi/+z9pQr2mWEk02wdWk8StQq0dQuX7hDc0rVwk46kAcoFHAnfBSkGfKXJ1VKr9NL5DZDS1C+tD7GVATWXR5o+3SKrWbGGpbGUVX4oOFGJjRXTZR7y/kX7VgqXutsg1T9lM+EZQBUMN6+uxlgEZ+k2uCrNJzlPwzN7zqw0R5atKZRrOVFhvbkij8G10HMOD/kXCwqVbY56Vg5U0E72ymuKO1hMuUFKnSSgmWs2oYrZrcvQEsOpggoykhc8vRX91ShoUBlsrweKbW82Un05+10v2Yk+pt2FlS6GOXTwJLk+2tDuKQOnYQA5/pA7I5/WwOFaMKXLmIY19Ot/88Y8I+2Y0bGT0U1FLBPll1BAKspMzhKCS0JufYn/c7LL67jSNy5junRRRLtUNgBw+LHHRu8xEZzvnkzOyPZPIT2uuffkgqXsk/tK+sPIFaUZHYtduaS/D3idnpCuk8DgGLITispbGrkNhdMu1hJ+LsfTfGHWPMhdp7aCt9+L2WAJKEXNFeKhwllK2VQLkOj+X3aELz+895jV+RxrrWg2tJkDLbCgaQPHgzpFcdtiLsnIoBqUw2Nl4ex4bq0BdDrvJ/6UHq1prOr9crrXVQ0MRQsWsC+Z48J2ftGdOQXRHiom7IElNGIfZ4HwA33 X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(396003)(136003)(376002)(346002)(451199021)(82310400008)(36840700001)(40470700004)(46966006)(2616005)(40480700001)(47076005)(426003)(83380400001)(7636003)(356005)(86362001)(36860700001)(82740400003)(36756003)(5660300002)(7416002)(316002)(40460700003)(4326008)(41300700001)(8676002)(8936002)(6666004)(186003)(70206006)(336012)(70586007)(26005)(478600001)(54906003)(110136005)(7696005)(2906002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2023 06:34:03.9574 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0fe39433-052a-4fd5-e6b1-08db8f34a319 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT036.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN9PR12MB5148 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,URIBL_BLOCKED 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: 1772650729855041234 X-GMAIL-MSGID: 1772650729855041234 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 Fri Jul 28 06:33:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 127397 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp268935vqg; Fri, 28 Jul 2023 01:00:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlF/Imln7CRAt1ayN7yF/CmiMePDJFuIMgchMwZ4uTs++IbIZ7UN+jTWxN/oHstAeKaZE0YD X-Received: by 2002:a17:907:a072:b0:99b:c2b2:e498 with SMTP id ia18-20020a170907a07200b0099bc2b2e498mr1324860ejc.52.1690531205338; Fri, 28 Jul 2023 01:00:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690531205; cv=pass; d=google.com; s=arc-20160816; b=0oVJUh5y+XL6pxGIo4ml9HeZwaGuU0TqWGA386fYUCUyuIO0fV0duuz86dtu2iN7wq NNmphmZGxgek1VRAbufBaQ906Jp3NrTeH1xMHmgNx7eJHl0omSnrTyQYATWKD/VgVLSz L6WJU/DVkMwsnnV1XvZLW2E7FYxxliufrWmOHYvDHc05MlmQaKi5i8pWW5iXiOgWkMt8 XCBbE/UjE7XVkDmEg68WXwJ9DXhC6yw/NLVRkI71bXYOwpXNrIjIt7ZTe+OouAEsnPq3 eVs29HvHZoY7WWoROKuvi6pC0nMPR4buZlIlNmKBeT67TF2qHU45Qh8IzBavnZDYAPlO qTEQ== 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=FV0JrjUUwrn1J/lFH9ZY9xyoo1mCpvdd0c+a5ZoWLOg=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=lGavmEU2++612aFR7rciEq4VrojejCDdJSl43kNxxoIb8CY59Ah9cXW7gq9Gfm3Evv C9KUJuhJYGNE0TJNRFwV3aoSgx+ZE9eePog5S9xigl3t8C908PmLQGO2J4Ay9jslvkY5 3FKunUiFCmxXzuFpKS9JFFI94TglsfSrYa+NHNGgp5KWBT/HfIzOGT1IQtvN+0Mh2Cn2 HqQeTpK9DE8fAlfcJdi/8Jo4F/luGwk6NXMJrJX2Iaz/JW4xLNfMQlqcwgUoaqCRDOC7 Ycex0Sh2c20FHq+/IdCfdXg15rOG9Jtw1CcR6+HyV3e0RPijZ3mGr7kss/G1FJVh7s15 sC3Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=Wvm6ZOb2; 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 p2-20020a1709061b4200b0099351214a94si2415878ejg.648.2023.07.28.00.59.41; Fri, 28 Jul 2023 01:00:05 -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=Wvm6ZOb2; 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 S233599AbjG1Ge3 (ORCPT + 99 others); Fri, 28 Jul 2023 02:34:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233618AbjG1GeX (ORCPT ); Fri, 28 Jul 2023 02:34:23 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2050.outbound.protection.outlook.com [40.107.96.50]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5D2E30ED; Thu, 27 Jul 2023 23:34:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n3fqLlEWu5R2xIZtBZeo/RRyI8VrYKuNcFtVsgqM7cvzAvbwRNYa1NpZaKCRIKHdYwFJDf5TApAFTz7rmRimkQnVnGoNM6wtGbloSsMcUJaeEg/UyHE3M4RED61WNUe87YQUuX2swwY+irusw42PmHF9bFafsOcQWEH/N50GrRVd4Cky/1d8L42jWyYZuaB0zBfYgXZ08d768isUGM+jCXq9T1fsx3iaEtIqIQccop/frW5DdLPdlt6HsGJj1dr24Ap/gxeBcsEaGhUNq5MnG2IwuthQfx8iug2Rk1fwLWvUoO8x7k03+EsIMpQjaysYIDdTFjLtlCR/ew3x6FisZQ== 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=FV0JrjUUwrn1J/lFH9ZY9xyoo1mCpvdd0c+a5ZoWLOg=; b=F7FKJD3Wk8TC1cmrawKNQ5SFeUSqsJIVm1/IgXuX9Iia2drmDLzK6QsamhB+cZwTRiAbQ984TjO9rvzYupGAbfcNhJHiZTk2ul4ZpPMHgDja+p0nJM9daW+PnS6/MQ4QIh0CXHDhoM43HxclNvBPSBN9h2ARk5N58DS0Hmshacjs6Loo/O+Ky5wxEKSYPCz4so2dAe4flBwpgRMKeii0eCbTl7cxCDMxOM0c/3O3UP3ueJPeGTAV6F6MQZDo7GqcA30VIg7S8AMJ14dratir58Z5EQyCugq9Vy9pVWjbrC1FD3ZPZG7Qgxo32cXVt4sxfTx1cz7D3mri9SflsQqAOw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=FV0JrjUUwrn1J/lFH9ZY9xyoo1mCpvdd0c+a5ZoWLOg=; b=Wvm6ZOb2IncEik44+URc6bfTb8EgJ9VM6WCJAW0AIryZ5qJi9nfijBFkfq1s2pUmN05pleHKJGYMKir244AJsReMVikwzh0+LyDGsqtts+iBAWbLakM3MvcT7WuSKmcy5m+c424KVpfPsyc5+QDa4enphcIxoT/aj9/PbWQT7ibCkSfzBfuV2/7B/6oIKqw9H4XJcEsuRDQ5FfZ8OK1ir4oUJXHzM4di/GpFazxW4A0/JcrEzfP8DaCLKAH+vOvgFKr0WLagcsM/qmc4QyjejqzaUYn9vxuc8R5IXJZB0HwAe/oJe1IppYbH4w5PGeKzP/vn8ixQhwmmex/YmA4CXg== Received: from DS7PR05CA0061.namprd05.prod.outlook.com (2603:10b6:8:57::15) by CH0PR12MB5076.namprd12.prod.outlook.com (2603:10b6:610:e3::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Fri, 28 Jul 2023 06:34:08 +0000 Received: from DM6NAM11FT005.eop-nam11.prod.protection.outlook.com (2603:10b6:8:57:cafe::64) by DS7PR05CA0061.outlook.office365.com (2603:10b6:8:57::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6652.5 via Frontend Transport; Fri, 28 Jul 2023 06:34:08 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DM6NAM11FT005.mail.protection.outlook.com (10.13.172.238) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.35 via Frontend Transport; Fri, 28 Jul 2023 06:34:07 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 27 Jul 2023 23:33:54 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail205.nvidia.com (10.129.68.10) 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 23:33:54 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 23:33:53 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v11 2/7] iommufd: Allow passing in iopt_access_list_id to iopt_remove_access() Date: Thu, 27 Jul 2023 23:33:24 -0700 Message-ID: <7bb939b9e0102da0c099572bb3de78ab7622221e.1690523699.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: DM6NAM11FT005:EE_|CH0PR12MB5076:EE_ X-MS-Office365-Filtering-Correlation-Id: 52379ce6-1a3c-46e6-b8bb-08db8f34a569 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sDpQ7LsrPz6f+AaeDmpIrC2oSJkaH6k3T1zqGHObOeERlVgj9L2R9MN4eB7L6Uu6mPY2zJ0wTy/v1IhjK05oxIMHyj0UCE3jGVHmqAictfujlyO6HcMcvwmNPHqWnnpCQE17kHNj0QGor28dr6TDohXFLFi7FiaFFtnt2duTSqHRXf3cAuAZ+q6C4t4/H7uMkkmrLKiD1iohMWttJYH2Wlx9apK6KXpc2LAoU88x6uwYKIaqVuHmSAoQqsv+2NvI0anSbMJZxhTtj4DHAMhoCwHVANIxZ5DYHuCx3Nl+jg26cKvpNe05CeB+Skp64IM+1Ygd2g9PGM0EjgyM4pXMOwfJaokIomUFF16a63AAmXtgSYABFLmKkrbMdD497TrVCGhNye3/z0HbU0FwqWs5zgelG+4m8q8AZBBAHbpwty2U163lj/Int3sS+/Etpx6gByUJPu9Fgf+VJKEoOUhz8fXtQMYKqkJdrmoAOdSSi2TLLP25p3MpLo7nrX6aInSZ4e5mJFn1HwUyOVgGZj/ZkMpMjzUdPVpLr8eTipPRod8Bg2UIufcTh/XRTQFNGl2AQ1GtgaBPXcXc85+t7+5CA8pvHjY0nzXOq4KCWxIil9K2ZT6+5nHYCjPA00cHCoLCBt59Mr3xI5KgFn8pUR0b46rSCn3CrBL23Ahm5DPp6Kg5ygpJW1waD72Pwt0CCz2Nv7OUrVFkYIjIE+H2AykjHFWcXiHe/D52J/QYeEXRnXvpceFSpOAKWqWY+jTARqOm X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(396003)(376002)(346002)(136003)(82310400008)(451199021)(36840700001)(40470700004)(46966006)(5660300002)(7636003)(336012)(41300700001)(26005)(36860700001)(86362001)(478600001)(36756003)(4326008)(8676002)(8936002)(70206006)(70586007)(82740400003)(316002)(186003)(40460700003)(2906002)(110136005)(47076005)(2616005)(356005)(83380400001)(54906003)(7416002)(7696005)(426003)(40480700001)(6666004);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2023 06:34:07.7735 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 52379ce6-1a3c-46e6-b8bb-08db8f34a569 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT005.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5076 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,URIBL_BLOCKED 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: 1772650449393033889 X-GMAIL-MSGID: 1772650449393033889 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. Suggested-by: Jason Gunthorpe Reviewed-by: Jason Gunthorpe Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen --- 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 Fri Jul 28 06:33:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 127384 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp261058vqg; Fri, 28 Jul 2023 00:38:24 -0700 (PDT) X-Google-Smtp-Source: APBJJlF5+U1t51lLZFUzdEYnz+MT9qooIsvbTPhPA1qvPAQfRE8EtjFHFZkz0+fjgrOZujnT2/nx X-Received: by 2002:a05:6a20:1010:b0:138:64c4:34b2 with SMTP id gs16-20020a056a20101000b0013864c434b2mr732127pzc.41.1690529903622; Fri, 28 Jul 2023 00:38:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690529903; cv=pass; d=google.com; s=arc-20160816; b=g2ycyuX4/Kz2EHTJFcqPBKki1TnyJLVJEUU9RI60xNgaYvmxgXTJZZRaj4gGoe4TsR iQrSaupeLdjtBMrWlAK4hY/W2cHnMrBwb58eUwrojJ/Shas0fdgwYLqi1/YUv/Pw3WiW ziF360QwGuPVEBomW5xPuoTXdFddw/VpbnUZRXDDLfbzGysrIWInAO53D5f6cCLaAyoD FMbdrzeedQyG2hRgREuBKP0ifCBZL9vafOzeVmYvg3jTN33izNZmfYTjl62aB2jJ05JR pPkbf70Bm5sE2YldvGCK77yPWZpFr4jthOD+cwneFDE1niThyDTwnVrkQmJscMsucxo0 Mcew== 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=xNd52/j9hQXL34viq7rGBWbItOz239GEoAU2sGAsvwo=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=dcmwQw0UBOe1HsfEU8yGGJ9793N3KiZDsQQm5Gv2u6ei6f7bLdvDwOPRwkHGJcHof/ zx3UAJDPhk9mOEvOX8sgEyvkzlHghR91PNLR9bjM7VrdW8YmRZsPreBJIEu5y8o1P+a6 GEgE1fwqWSGEr8sgDXxWyUFPkMOlSm2w/5Dyopu92OC46c4seKfl2Y/soBxqHzBIoadB 49ieeiTwpkMA+9cmDMlUUjMivYXA6AnYqfhUpwp25r5zY5jS7sLdNAypb/xPHvW8H/lL cJnuxWpH8UYw5i7iCrTo2bwdLUooH/DEFtG6PpO4J06Csy7gafeRNPt8zicc9ajxd+Mh EVwQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=A2fiTvIR; 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 f10-20020a056a0022ca00b00655f316b6b6si2793218pfj.175.2023.07.28.00.38.08; Fri, 28 Jul 2023 00:38:23 -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=A2fiTvIR; 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 S233615AbjG1GeP (ORCPT + 99 others); Fri, 28 Jul 2023 02:34:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233593AbjG1GeL (ORCPT ); Fri, 28 Jul 2023 02:34:11 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2057.outbound.protection.outlook.com [40.107.94.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 507F130E2; Thu, 27 Jul 2023 23:34:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nuxtOZWTzAfWm/TxTJNi41fid7yVKYC0jQcPzgeDIji/NG5V+L/AIlxg+DAX0QQc97OLXV6egwaBP2UJZS7LeUiH/eLfdr9jDOmH8taaTalIL3tIWEbIX/uglx8atYY9i5eSHq9Te3lRAMXTfbpmweyzVCQurc6RLYGAC6ftWaoHW4G/w+rIspno/H5Le6t4lyoJCCggGqDxobvYH3F9PoJGMIlBG6GaasH3E0UocLvuMLL8DYJD4t0Fp1PAcYySWz2U6GSnEoeMX48USAwnMBulfAIVOInt1uyKDinuJ2XFjN/PYLjBzNPzMbSK1PE5RzbmwcE0FMD69YiIfWVRPw== 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=xNd52/j9hQXL34viq7rGBWbItOz239GEoAU2sGAsvwo=; b=h1D9AdFmPpXLbfGkPE5vuGXxftJXhhjhof2GM6oCmkonw6C5Aj8dQp6WsIloLO6+WIKzEb+8JpL6fqnZG1Q1cmuXfn6lPnsjwb4Zia+Wc+oYavfAMgxdTljMV+uZfWMA2RcPmU6T/FeVz/bVvoConnLxHpy22IP2fl8hhxSOA07eR+Pbr+MiI+BjAfLy/8wmKC8ISP8dflsKYcrFYfgj7VDkTlyRW7PtpNLHi7hdlYVtIz5QXjjH1fRTxb4mdKeiY80a0MtNiLnvj7LnyzXUiTZqsvD1BGFOcQzBNTckFWZiCnASXbLdM+kPNFFPhGEJmE/tpiw6UuHqyGYKfz17ag== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xNd52/j9hQXL34viq7rGBWbItOz239GEoAU2sGAsvwo=; b=A2fiTvIRv1QgJDVlNzNBvuj49123sHgDOveKaNJmTvmXcY81tbHw7DrRO7XdnFNkNYsYqTTzoETmUiWTXfUphos8yFK9DJ7a+lA5Rq4MlxGVSvRbD+KrOP9uoJuISzEa/3fgf9h3e21VbnuzXw8eq4eZnRteOcZT297+uMlbU9ucehDEjXoqPlvHKsoJx//glY2ONPTaVLyaQWQSnsIQNLXhCkoy8A5dtvMEL8fLe0H8ouZWDhcBubU0JZg5IZst9UpGEIMapfH2xXz1SnFSnMZVIxi7AXms3W4rfn1vpjyDQ3DxnEAWkuHEtqRWt6Sx8TGR64m4zq/bEh1PDKJJOg== Received: from MW4PR03CA0121.namprd03.prod.outlook.com (2603:10b6:303:8c::6) by IA1PR12MB6553.namprd12.prod.outlook.com (2603:10b6:208:3a3::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Fri, 28 Jul 2023 06:34:07 +0000 Received: from CO1NAM11FT003.eop-nam11.prod.protection.outlook.com (2603:10b6:303:8c:cafe::a5) by MW4PR03CA0121.outlook.office365.com (2603:10b6:303:8c::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:07 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT003.mail.protection.outlook.com (10.13.175.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:07 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 27 Jul 2023 23:33:55 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail202.nvidia.com (10.129.68.7) 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 23:33:55 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 23:33:54 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v11 3/7] iommufd: Add iommufd_access_change_ioas(_id) helpers Date: Thu, 27 Jul 2023 23:33:25 -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: CO1NAM11FT003:EE_|IA1PR12MB6553:EE_ X-MS-Office365-Filtering-Correlation-Id: 6fcca52a-0a14-483d-c1d4-08db8f34a4ff X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nhdwBcGtaNhwAaXCLnPBJU2/N5nrOxmbP870BMr6R3DeWrQKmORyTwGIEpfAlJAc1F8CaNn5nam/Y7UajDWalnBcO0bVkfX1w83QoBqBRx2BznsE86t+9ef0rLTmKVM1dTjV+3uli4a4RwUXl+GEztdl1vNFS5PJpguzFlL2VZ+yUkMrsFc24JbEBFjMqAco8QxLz+llBkigEWZ3pSbl4/oJ4h7RMfAvjVL1353KOv9TtMbt/vtDNvo2Wgi2jsbqPEfS5tUZjPCc46B1POXsWGPFUahUnIbKooT153Cj/Nk7WSuquE9+h+nnkhIG5Ac6JHDSIXHsUEdadgAcJ/OPA3Bsaa0nO0O/XILittyaFTd+ogCtF5dj9vewZn3G6vi9tDsSWyKvWXS0XDzzUu2DaIjSZB7m+uMRlWcMpJufBdmH/4XVAqmeF/iSbpAtiO610EBlho63oxvnvP5aR9pLfiovSYtqXSD12y317PRDlrOxJQWLGmyjRd73XC0uzI9ZgxLTxYXtemd62ovoBtk2RTvzwdToOraolVuhY6BFi2yh+ABJqWfCiZ/70SGs4jMs42oCalrteSdCN6O+KsOriqUJmR2I70apLYc89NtWxjdR5fYPwFlq6mTamFUrmPGpjNEoxgDGXRw2y0xToOYP/PTT5uHCyF5au+JGAZy8RP0OMNdjKbuxaIjTm1RpX5pstkxlcxV+6skMnWxRzW0PMjwxVfF+bcE/9bNgfxgtbjVvlOFbWiN3akbvubmqpaCc X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(396003)(39860400002)(136003)(376002)(346002)(82310400008)(451199021)(40470700004)(36840700001)(46966006)(2906002)(316002)(8936002)(8676002)(7416002)(5660300002)(86362001)(41300700001)(40460700003)(36756003)(40480700001)(26005)(82740400003)(478600001)(356005)(6666004)(7696005)(47076005)(83380400001)(36860700001)(336012)(426003)(186003)(2616005)(4326008)(70586007)(70206006)(110136005)(54906003)(7636003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2023 06:34:07.1415 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6fcca52a-0a14-483d-c1d4-08db8f34a4ff X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT003.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6553 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,URIBL_BLOCKED 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: 1772649083961202910 X-GMAIL-MSGID: 1772649083961202910 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() and its wrapper iommufd_access_change_ioas_id(). They can simply take care of existing iommufd_access_attach() and iommufd_access_detach(), with a less risk of race condition. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen --- drivers/iommu/iommufd/device.c | 109 +++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 38 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 7a3e8660b902..e5c408415e95 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -684,6 +684,70 @@ void iommufd_device_detach(struct iommufd_device *idev) } EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD); +/* + * On success, it will refcount_inc() at a valid new_ioas and refcount_dec() at + * a valid cur_ioas (access->ioas). A caller passing in a valid new_ioas should + * call iommufd_put_object() if it does an iommufd_get_object() for a new_ioas. + */ +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 (cur_ioas == new_ioas) + 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) { + access->ioas = cur_ioas; + return rc; + } + refcount_inc(&new_ioas->obj.users); + } + + 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; +} + +static int iommufd_access_change_ioas_id(struct iommufd_access *access, u32 id) +{ + struct iommufd_ioas *ioas = iommufd_get_ioas(access->ictx, id); + int rc; + + if (IS_ERR(ioas)) + return PTR_ERR(ioas); + rc = iommufd_access_change_ioas(access, ioas); + iommufd_put_object(&ioas->obj); + return rc; +} + void iommufd_access_destroy_object(struct iommufd_object *obj) { struct iommufd_access *access = @@ -761,60 +825,29 @@ EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, IOMMUFD); void iommufd_access_detach(struct iommufd_access *access) { - struct iommufd_ioas *cur_ioas = access->ioas; - 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; + WARN_ON(iommufd_access_change_ioas(access, NULL)); 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; + 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_id(access, ioas_id); mutex_unlock(&access->ioas_lock); - return 0; + return rc; } EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD); From patchwork Fri Jul 28 06:33:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 127439 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp300235vqg; Fri, 28 Jul 2023 02:12:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlHuC5z2xg92Q9svspCE5/+F8/LbcXrrVfVVDpeiJf0BnxFb6n27d+sUFG2dzMmyrJbZ5cgX X-Received: by 2002:a17:907:784d:b0:99b:50ea:2f96 with SMTP id lb13-20020a170907784d00b0099b50ea2f96mr1939702ejc.12.1690535538930; Fri, 28 Jul 2023 02:12:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690535538; cv=pass; d=google.com; s=arc-20160816; b=JVkCHlvjdAu1aUzPCXz8fI+yvt7rIxkjAHZgqG8O0iPowhqKGLKyjEPR0UggkMCQWQ LREo7OmLE7GPFEZHIzkEo6iFI/27K56xwWO6QYyDKN+4rqFKJYNXFtaw4TVdQqDbOipz ykQywy/JfkZYwgOQ8cGxTF2zLkD5kSgjn8Zf548rGS4URdhK7Mu5UGxZwlsOr35AaZya HIc6RCBzIXK7jOG/1YW3cV42l2XlR6HyWkT0IbQIvfjGpfz3pfi+tsUzwgV03yc2XR8K DimQJbZs5zex8tnqBwht58qFEsLmZjDBg/k8W6SH2BgVm55u3xI3fR+/k/ludZAYCPdu bXFg== 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=TvoJA5BftVG5tmbJHmelzB7Akvp8C9pV19pS3cZgeLA=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=AlbmHgZZHr22JgIrECmgNP8JCy4ngV5v4gp6tU1o/6BosEzJ0LboO8ZMMwNMMw7aVQ FRNrxnhgbnkfxAab8PKH5mZ/4s34CEvcuHqDYKufPtxauDjq6sGfAp4f7wWSuFF+n21/ DaKtaZX9iQiWBJ/MDdXcUFnwyyJofVwL0BEDUmxTd6Y0/QvnPY82MBaWN1gX2tG1wlgQ /FW7ANwDDNNPjkOsel4KETEybfrqn3bLZlf+N5IlEp0mgeCXC5sdM58EjwdTu8fe/A5Y 9x3HiFgD+I78sPBzQhBDlTraCAwOisLPOGF5iQMOGOa1HzS74GRPQZWGreuKNf7vIomb dB3w== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=qR0XXjSW; 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 sb2-20020a170906edc200b0099bbc75eb36si2469198ejb.47.2023.07.28.02.11.55; Fri, 28 Jul 2023 02:12:18 -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=qR0XXjSW; 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 S233183AbjG1Gek (ORCPT + 99 others); Fri, 28 Jul 2023 02:34:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233621AbjG1GeY (ORCPT ); Fri, 28 Jul 2023 02:34:24 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2046.outbound.protection.outlook.com [40.107.220.46]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BBE430FB; Thu, 27 Jul 2023 23:34:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QBAo1E1zGow/0k1L1BzMTWBrkMzyEQt2NqFbVdy6oiARJeBBonTTJWtUo087fOV5ESC5G5Iq3gNImwEHwK7zO2SRa1RfbInzi89VmuDwrZeBfgalZT8J4MCFIA7o9JPbCshky37xworrcq/IKF5lLkxRV5KhKoWMhy2eoQSabnqcHyB5Rx2FRpUjOiN3qgYlCPURxCz+fVLPr3bl9d8U+wC/9rExBuTUxQwVeUUdg2UAAK6HQL0maGphdyZQeZEyXuO9xA0PXtHxtumpNNcuBiyaPUE08UAoEwRL1Skzg1lBE5ONQWPup0U2KN/W+z0lmzqxsaX8uiyy49q4QDke/g== 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=TvoJA5BftVG5tmbJHmelzB7Akvp8C9pV19pS3cZgeLA=; b=MfU/ynteayz5zP/4Mm9WG+2r7CIq5G9jQUu1CFoBon++3QMPycCDSyocQaEHGyHH09BVfWT1SGB5ovGh4tPBUECyUMIj3hhZEsOuaeHK2cHb+Vnb3cmb9J1ujsqggBzEMvwU4f2x3YW7zuLoOHAFRYNRNTlUU8rIUs/vFpal6wO0pK7/xBb+R6Hf6ZJnMMSAHz7YKKtAzFJWK9K9eEOHYDOBYasyUufcdysMrJZ9rE9ff0VAUxFic9WZ9m1f9E7SmdekMesj3hYBGETtLGeDB4Envjdy+Fl0JTMk26ZFvcEuTCj5KogUfzk9L1c4/ZQqKqf9BhrbSti9O8x9zlOR1w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TvoJA5BftVG5tmbJHmelzB7Akvp8C9pV19pS3cZgeLA=; b=qR0XXjSWra9oVMNFYHw4vXW604GqBZAgORGco8CLtNbAwoiV4McDUKJttAg53UQ4M0c9+cvhcCcApHIVbED3sE2uMM/7aqM1Ojt0FhJudSN7yS5YvYkEf8Q4KWZVRXwO50wTZytNI+R8MANf2fuI1T7RTPpAWWiV19+8/KSAulClGBrBZxPowm7r/UgqNvL1J3D6wlp/ksCH3n/xhDLya3P1Av72tYc6pme8WoAgcfz1vb2kliJQ74ylqWjP/E36HMdq5vyYJ240FC2o6cLPVkbAZlvKerEBmNIFH8QuUPMY9Jc3Kz6jcSIT+WgfOJk81gD72/saDYJbgWAY2tYKKg== Received: from DM6PR04CA0016.namprd04.prod.outlook.com (2603:10b6:5:334::21) by MW3PR12MB4393.namprd12.prod.outlook.com (2603:10b6:303:2c::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Fri, 28 Jul 2023 06:34:11 +0000 Received: from DM6NAM11FT092.eop-nam11.prod.protection.outlook.com (2603:10b6:5:334:cafe::25) by DM6PR04CA0016.outlook.office365.com (2603:10b6:5:334::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:11 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DM6NAM11FT092.mail.protection.outlook.com (10.13.173.44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.34 via Frontend Transport; Fri, 28 Jul 2023 06:34:10 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 27 Jul 2023 23:33:56 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail204.nvidia.com (10.129.68.6) 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 23:33:56 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 23:33:55 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v11 4/7] iommufd: Use iommufd_access_change_ioas in iommufd_access_destroy_object Date: Thu, 27 Jul 2023 23:33:26 -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: DM6NAM11FT092:EE_|MW3PR12MB4393:EE_ X-MS-Office365-Filtering-Correlation-Id: 67891272-2bab-4a1e-ccc2-08db8f34a724 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: r+q3C1oWbcyY8SyIk0ZnJFVq63qdrWJkpwEA3pjGbjXQLTqQanpPgvzkguepRME/1+S+JJNmP+Z5jyTZYG1m0SpqJjJd3DWNzqfIJQyMKCMyux3s+TSqn4tE3Yz+J91yA1/KLQ5BzrdV2HZTXvDg8Cizmm/9qXj1dOa//vLJTg6/OP/eafYswkHPuXrndi17rXdoYpJnK+ft1yLg+oEma2CS76BDQQw847ZMJGaN2Oo0OKriy7k3JMHmQ2EWj+E755gGKi5ZKml4Qgf5VUdWrukHtLIq0flVwtMwdOrWbS7In+Q04KLeQqGe++mbues/VqmwQWNJjsfH/fF6fOfM/5nwSjDabFzrg+gITuBZu3xYORgmiO9GU1HyUkFFkoGEmEelMiBwb+rwZroasNkRZQfdawW/4IpJSdvmL/rwQGtpw8uqZt8Co9YBJpuTVNuQ5/C/5F2ztjVxyJ8y7V3wOgyYffov6xs4KXPsPhOKFUdgpKEb2p4ntTDoNDZf6+ITwwBxlkGpXQ+zD5i93wjuVw7TX5VrM3PwrrT1BpbpN+XD3HPE8O/FPCJenXiS7Q7xLPrjESKp87HR4g/e+179mLuFHl7J4iFMzLPHWZOMWF+vYOV4XGN0UK6hgBiv0S0Ps8wI0R2V1VaUJW/t4XuNAS1Tn/VAej1I3aEw2JF9xnwuokZVhw6NI1FnImQExeui/OsJ9mTUWRT9/IqLxPSq8D7ZN/5eXp0w7ZmJrIdGX28XlXxBAAqcFD1IjzW5xfKW X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(376002)(396003)(136003)(346002)(451199021)(82310400008)(46966006)(36840700001)(40470700004)(40460700003)(6666004)(7696005)(36860700001)(336012)(83380400001)(186003)(426003)(2616005)(36756003)(356005)(7636003)(86362001)(82740400003)(40480700001)(26005)(47076005)(4326008)(70586007)(70206006)(2906002)(316002)(5660300002)(7416002)(41300700001)(110136005)(8676002)(8936002)(478600001)(54906003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2023 06:34:10.6772 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 67891272-2bab-4a1e-ccc2-08db8f34a724 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT092.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4393 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,URIBL_BLOCKED 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: 1772654993092767745 X-GMAIL-MSGID: 1772654993092767745 Update the unprotect routine in iommufd_access_destroy_object() to calling the new iommufd_access_change_ioas() helper. This will reduce some risk of race condition with another concurrent iommufd_access_replace/detach call. Note that the behavior of this function call is changed: a WARN_ON will be triggered by a -EBUSY return code if there is another ongoing detach. Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index e5c408415e95..c0b9cd97ec58 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -753,12 +753,10 @@ 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); } From patchwork Fri Jul 28 06:33:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 127435 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp298791vqg; Fri, 28 Jul 2023 02:09:04 -0700 (PDT) X-Google-Smtp-Source: APBJJlHqLMPfxRBmBBes5RrIe/8GNVPFnDk42shW106bwG7ntWyFeUQSddjIF4R5vTHfC0hw1Xty X-Received: by 2002:a17:906:9756:b0:98d:ebb7:a8b0 with SMTP id o22-20020a170906975600b0098debb7a8b0mr5833147ejy.14.1690535344141; Fri, 28 Jul 2023 02:09:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690535344; cv=pass; d=google.com; s=arc-20160816; b=F/TnOK6Sq7nfKrHiR1lzBYaOgAhLvekl6ZO2LlLYDrtVRxZmK32LyyYl7pJXroPZBD kUN6SnVKZZ6JC0bgo2OYVJzzxszxK7GmfTgKGIy4/G5sjUkO34UvgDyYYugj5jHBIhp1 j+qcowosMnc8kA6ntc+z5QekTSBa128nJMd8Huc4yn+9aNqKhoePNgCQnsFUp46/oj7+ 3vekwcHk1ipAPFzGxESYBj2zlJZHmiufI9c30T1pOq0IBUXQtU2HIxxp05u5uq9elk8h 22c5SDLQIb/gFGt8ZeHDv0qXJcDA8bjEzsznjX4PREREF9xehbqRr+ygg47Zo+rCto1j x6nw== 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=ed2umQe9J9l+UY7+AMoVlnRwH+qHkld/m2lP+FIIOtk=; fh=qC2Tv3KKwVGixY6WZei9zODHRrUJaQPqzd5TxSwHSnU=; b=fQCF9IQ/Bh9TcCSs5IIWYoX1aPb9pmIQ8NyyMAc/jzk/4kiWR9olOvAZIwqRODsM2J rbf8rfbS7nkZ4BRsdpb/Uimqho/A4AVBDkUJThmtxaZNWKGgEZ4xLQWu/XmjhAUFKLAj L2uEGK9tnAKLtYj6o+R2UjlJoLaNDNoSIC0SDRBEG0WPQkAncbdPmV+lTTncmhyMMGOM 7uG5EMOx7cnNzfawjF4xQFDRyHX+YquhZy4aQJQkQYH00+2d0VmfVRfybKR49WM6yWct LbGY7WAoebbRWWkYUxU13F583pxJtyOolX9V6R3kGQeX0s2hasZ/Ca3wGKQ7A6Q0XNdZ x4hw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=XwPh524J; 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 y25-20020a170906915900b0099bc9dfa042si2240578ejw.470.2023.07.28.02.08.40; Fri, 28 Jul 2023 02:09:04 -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=XwPh524J; 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 S233651AbjG1Ge0 (ORCPT + 99 others); Fri, 28 Jul 2023 02:34:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233602AbjG1GeN (ORCPT ); Fri, 28 Jul 2023 02:34:13 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2088.outbound.protection.outlook.com [40.107.244.88]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15E9C30E2; Thu, 27 Jul 2023 23:34:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RFl7sQSNIqEez84Ur7oXUGCSEa8hS2hV4vBxHXXZxdCWDp2TQ5wVYpvTGCOHBsgnpTQkxSoeHopdw/iNbEMh5kCisIeQVuXVEKvW5NyR5Xr9yDeK2uvYvUfY2Og7p5IsM+YGnBTikyyEyn3y3Fm9ILCZDfl5PvXeYIl2QsfclcVXRfOQBErId/1103VALlUu26nJ/NjdYGjihBGMfQgMTr+Z6SNTwrBO2YeFTjFBv8MNN716TylMzTaX2t6XhvW1sKfnRigJKy4QXQ2jbaCNC0pumok4QiZFfRKQWOOiT9nhl2BeBbOYvmQyKC1skh1vmjZQrATSQ6acLs+6yNXHQw== 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=ed2umQe9J9l+UY7+AMoVlnRwH+qHkld/m2lP+FIIOtk=; b=VrBS3FAJW7nroufH62j5DuqktmgHshAa31+TxkFXbLJNIOH/iIZ9ToYDPA8UAVh9YR4ZqSc/RGknFbotwijVq1PoGLLyp7axiejokHZ3N5fczv55grluhZKOVyKWkkva9+yjuy8te1bnUW164PibqbQcH/aw/9V4AsFNVi7Xsac8z9ZXlWHBlGrSLm4AhHui1DdHPEykqHNTHt7Gov4nZTVnFpG+qX3cZXil3Pm3FmaTVJTTuQTXRq1hJReYm3P9OjN1JpCHob0oN0+lrK4CfkeTDbJMMeWtlIcdoQPkcp4Rak1WOtY3y0SnOSDcLXJym+UqcLUU8vxXCiH6/DchDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ed2umQe9J9l+UY7+AMoVlnRwH+qHkld/m2lP+FIIOtk=; b=XwPh524J+i11IxG9Ia0vYiRuUvT5R9paWlanl6wh8EaTB7kd4Cs9A+9t8pHLa5ElFbZIqcmijH+3IjFSTL6SQW6l7ePXLUghu6onL4UFmp32scpW4sw4oKjoSDCJdh18vXOmVFWCfxeBToVya+bYoiWLRL9qjxVrKMb8yEXCvQFpa0uD9jtc2jb6HKdV4dGPdooMMP97T26zSzxKIfn4V4f6bR7UObVKSRluNJEkk78M5IRIqmShBLqbutzBWegoQ/lAtqculd60UulwjTF+qDKD/c2d8Hjju8B26OWL61Ir4REhc+dmkr7ibksXtC5BLmYpcyTjPQ20CGHb/VU39Q== Received: from SJ0PR03CA0048.namprd03.prod.outlook.com (2603:10b6:a03:33e::23) by BL1PR12MB5363.namprd12.prod.outlook.com (2603:10b6:208:317::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Fri, 28 Jul 2023 06:34:09 +0000 Received: from CO1NAM11FT036.eop-nam11.prod.protection.outlook.com (2603:10b6:a03:33e:cafe::8) by SJ0PR03CA0048.outlook.office365.com (2603:10b6:a03:33e::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; Fri, 28 Jul 2023 06:34:09 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT036.mail.protection.outlook.com (10.13.174.124) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:09 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 27 Jul 2023 23:33:57 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail205.nvidia.com (10.129.68.10) 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 23:33:57 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 23:33:56 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v11 5/7] iommufd: Add iommufd_access_replace() API Date: Thu, 27 Jul 2023 23:33:27 -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: CO1NAM11FT036:EE_|BL1PR12MB5363:EE_ X-MS-Office365-Filtering-Correlation-Id: 3db99757-2772-4a8d-a82e-08db8f34a619 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: l7tErble0CzgxcC3GxovEe3tcnkOChx3q1QO9f30OD+umrSgGEIIlV23AE77/35v0vki25aZnwlm2+yzAw96Dt96T3Kqn2PpGWxER9kwufqIoTFRsnhKrfQu/YQ1vFkHXBk4j0S3aIVZpeR0xxDeU/MOanB9Nb/SkS/gAK8MI7hhHnWo5CKVKoLqmlALiVthBlAzqCQEvzlFbccZpyjmzbDwrihEQyjvgDF3ONXuSItb/ORUx+0BXJDv0tjsb+FypdDE9UKiPD/NtNEMJujhB2Z7FRXzdW5BfMCXJytoTPjcxfapEjRTT2Xbu2CD85RCGRCRUObqVVaTlFIsnHi75zuKK1KSt0WyLIrn5AQ0oDw9i1NzSWY/9OU6TcNTcaDCDwySl0X3ZuKVJc+qkLNgP1qCqtQLDlyLOo354dZurL6VsqJ+XPwy7YJBXiW90Amw1ZF+m2vioQSjUWdeoMufwM7aieQujl75Wi0HQt7PwrGOrTe5fFQltPNMBW8vBC6gDjbgaAMLzbwNrJR7FGPGneW9gxhYsgnsJBI1JgRTwD8BZaaxcvI9gkdT/AvH736aXCr4pLo1tqm0rrXNkRzebpEC0bM1Jntmc7gGgnC7gszwHp1pQP6FLWujEQkZ/qD76zA4HaSrdNLuWgi+9ficQyaKzxuKVV4exFjGgk++s5sbSBTm/vrJOHOkfjHn/1xl6mqtF0bSttzrjjHZf/UaMUDRkQn52rKOjzGbg2ceq0Mbg5xReqCG8k7TvAImNZZw X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(136003)(39860400002)(376002)(346002)(396003)(82310400008)(451199021)(36840700001)(46966006)(40470700004)(86362001)(316002)(41300700001)(2906002)(7416002)(40460700003)(5660300002)(8676002)(8936002)(36756003)(40480700001)(478600001)(26005)(356005)(6666004)(82740400003)(7696005)(83380400001)(47076005)(36860700001)(186003)(336012)(2616005)(426003)(4326008)(70206006)(70586007)(110136005)(7636003)(54906003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2023 06:34:09.0046 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3db99757-2772-4a8d-a82e-08db8f34a619 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT036.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5363 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,URIBL_BLOCKED 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: 1772654788533826112 X-GMAIL-MSGID: 1772654788533826112 Taking advantage of the new iommufd_access_change_ioas_id helper, add an iommufd_access_replace() API for VFIO emulated pathway to use. Suggested-by: Jason Gunthorpe Reviewed-by: Kevin Tian Signed-off-by: Nicolin Chen Reviewed-by: Jason Gunthorpe --- drivers/iommu/iommufd/device.c | 15 +++++++++++++++ include/linux/iommufd.h | 1 + 2 files changed, 16 insertions(+) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index c0b9cd97ec58..4c37eeea2bcd 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -849,6 +849,21 @@ 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) +{ + int rc; + + mutex_lock(&access->ioas_lock); + if (!access->ioas) { + mutex_unlock(&access->ioas_lock); + return -ENOENT; + } + rc = iommufd_access_change_ioas_id(access, 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 Fri Jul 28 06:33:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 127374 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp258856vqg; Fri, 28 Jul 2023 00:32:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlE/ooVDYwh4/p7Lg1KyXidUsNrZ2Q86iFmD1cggF4cQsYs8UHZcFIKJNiF+NFJ9oMWPINjE X-Received: by 2002:a05:6808:2a6b:b0:3a6:fa95:e024 with SMTP id fu11-20020a0568082a6b00b003a6fa95e024mr675215oib.55.1690529579041; Fri, 28 Jul 2023 00:32:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690529579; cv=pass; d=google.com; s=arc-20160816; b=SSCOWbn0R0O1UmU9kVmpsytnx1+u/0fwhmG7GOKDkk+zTb6Ib5xCfciw0+hQo26vQV msmxSabfi4ramudhadf4nR2XhAKTjtSgZXJ66rFoFMWrM0beSkUXfJXdJPpMop+p38cu wNxR4jgVmNnzzYAk2rijoxsADw/X4yzBf4U70xT2KBk2dFaYA9YjI0Yx++NpjH53Wgxb ythzqyRiT+KgwkO48XzSoAuSKnRywIxL9qAdpNs90jE/H5EILAVcGrudequXJuPE5dVI 2kWdiWn3vwXcvp5FrkoNxQjbRpDtAg4IBHFjs1MxE/xGL6V/wdQoW1o+KowPZ+XUiNHL xWqA== 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=kS3m2x40d93pOEqU3Mt3bcb6K0yhmQWyuokE+jMEQzqiXjqrsHRUhPHv2sCuIyYDtR yrhXVOk7E9IhvFttNoRReG5m47MvocluhcdWzKFLSRUrypixZv33h8lCJkYwGuIj1UqF kBlfbMXsxjpjdyMKXZcUstVt7HcJN7eLfGUc7NzI2uAZWUdvxIdnOfC4Jy51fbugbb8d pMcHzgSPV8DHF0ZAv6rNJ3uhXCE8RTMe4B5UyGKLiQ1l/ORmq3KiRgR0X44yJk4Y5YK2 vh0YIY16nMHW5kVCj+7pL4Z67L/60XacYeXKEZNkLr94fx1ugasu7Cs8qNjSN03jAjyQ FLIw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=pOEYukyA; 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 w4-20020a656944000000b00534780446d9si2552153pgq.640.2023.07.28.00.32.45; Fri, 28 Jul 2023 00:32:58 -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=pOEYukyA; 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 S233696AbjG1Gec (ORCPT + 99 others); Fri, 28 Jul 2023 02:34:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233605AbjG1GeY (ORCPT ); Fri, 28 Jul 2023 02:34:24 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2060.outbound.protection.outlook.com [40.107.223.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D6A03A89; Thu, 27 Jul 2023 23:34:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lUE4Zoo20dzakAlaqTb/VYeUWDV3FpSN6WLvz44BBA1UqmCFITo6HFLt4ppLKI5geoQs5R6ltwRDLl/6HoM5el3LjZjdO5clIm2cZQM4GhbuERNftV3HECf1jaDQ9+X5tbvvsRkqhW+VohlJ0uKkEUIXAqrObFx4y6RJhyZIXWCfqX7uGLqAi0GfsP/mUXyISBswwud9BS0MPQcQUd8Bc6znKI5ImQXGfeO+glPa53vRz/Eryv7DpnuA1IptgLNIj5r/C1J7acawrQK/1FYiTUspUOPpYT5rYcb9gRauYO8XhzmHxX/5oNXG0jFDEaY9yUQ1uft9m7cF6nD2YyDchA== 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=fzLy7UV/a9cQbBWZrzkL9Z1U5LIsCZJ0rPqP4MC+N6J90POmkOIH/f2MVmI0z8tWfTLXavda9ZQeifzG1JehjvWuq1+DDepx1rLE3clPoi6KXytzGYBZlH0m1+1YLzPJfXvL69vtTglNAQxY4WM/hW+pVO1I1mYCOC+oKVOMJZM39HrefBtS698vwhboAY82X0D8Ig4dK71wjva8OeGQrsHNRym9G7rw4rnudjWGchQ6P0dMiyKrNIlPETm02pfw3cNeuCbj3ANZpaVSymOnkgy2a7WBlV25xO3LslB85v7lE74EB0v+x0BGfHlSnXvhU8BG7NfO1XwjBL8wrzHvKA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=iirMKJG1l4d932PYYOqkXDT5WydmhkNOrAq8wo1jKiw=; b=pOEYukyASmPNx929aZl3DCKHo8NEMpkWJxfRsdvE2VgfooPe+4A7U55i27xbMAdiSUfR2d3l4wc2KiVGAf+fAKZ1cBLr/J5pAsE4i23rven83lQxUhP0K0wp5do1zRfZWY8zDYLI4BjdNq7sPXnXFbtyn7MQvaisf1dpQcY++B5JfmaHQyYTstvFJL8ZL6Pqu6aFzDZhOIaY9JA7yGfIpb1aTC60+Yxe1zsTVbNh9+K9AoFmW6yFpE6RI9Xoy+aNkKbuQ9Tte673ed3Rxy/aqZyNTERK/Wd1X17xJCfPV8bZIZD0X6f2qEG+JPx0gVAao5vu9MAriLozh7pY8hqHww== Received: from DM6PR04CA0013.namprd04.prod.outlook.com (2603:10b6:5:334::18) by DS7PR12MB5719.namprd12.prod.outlook.com (2603:10b6:8:72::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Fri, 28 Jul 2023 06:34:12 +0000 Received: from DM6NAM11FT092.eop-nam11.prod.protection.outlook.com (2603:10b6:5:334:cafe::c5) by DM6PR04CA0013.outlook.office365.com (2603:10b6:5:334::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DM6NAM11FT092.mail.protection.outlook.com (10.13.173.44) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.34 via Frontend Transport; Fri, 28 Jul 2023 06:34:12 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 27 Jul 2023 23:33:58 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail202.nvidia.com (10.129.68.7) 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 23:33:58 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 23:33:57 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v11 6/7] iommufd/selftest: Add IOMMU_TEST_OP_ACCESS_REPLACE_IOAS coverage Date: Thu, 27 Jul 2023 23:33:28 -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: DM6NAM11FT092:EE_|DS7PR12MB5719:EE_ X-MS-Office365-Filtering-Correlation-Id: 1430facb-d187-4dc8-83f9-08db8f34a802 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZxvV3kXV1gSjPO69cT9Mhi3EVSJqdCAmpyfq+kIKlSDLAlmwZbsQL+5TKIYp3wFbRCy58FRH9ICsWm99ZZgYzglnji+VMBMGweNRP4YLz/mvPtpaLsU3C8T9Uu/zxA25jEO2WkkgCWGYXF+WBrUNSbZ+LhFVCQ58RceggXP41LRwRZT2tlWfdRiV141DOqKX66BRP+rGJGiXnasBriPNOutASpO5QwTwrzfXQh6Cpd92i5fZHmRlpDoO1ha6N+eJW3cHjbYyp6TAf3FWCN9T29uM9qDZ4M+jdrms4tPBhPHGgFYi8nzB8BqxP8ye8M9wlEK/tVRUPspvidBMP4t9bcZuOMl1NSVpSi2PBfrLwBOBv6qUZe4aFV+MchW0/yA5s+gVfo3NEfNrQszSTmgTAmkjrP37snyMoxq7QxZh47QB8gaS1UNDEzf8zvz7kX1EX3bJuAbRlXOlDhap0KTEg4xMba7bTlbon3d14HTRCmHNkLNyeCyb4h+ljqmCqQFw+ZnlpQwU/zLtrZcE2R/cpVOXhG8b7Upn/eTkN/oL4zF7RWUOSl+5OyMXYODhKogODGRtgrbKFRCIQeQN6vIBrOcPuG4oCBBajLTrXT5ObaBnwjd8xu7SY+GQLZEN0NFNNm5vO7+KMPZbzR86VriW4rCM3lZai3keGLDU3xMoR/p4CxchKH+udvsKBqf+WkVX0a+Q+W2WEv6OqAkqkm2lnAlMXiTPciKL/5BHxhgDDvA= X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(136003)(346002)(396003)(376002)(39860400002)(451199021)(82310400008)(40470700004)(46966006)(36840700001)(54906003)(6666004)(110136005)(7696005)(478600001)(186003)(2616005)(47076005)(336012)(426003)(70206006)(26005)(82740400003)(2906002)(70586007)(316002)(41300700001)(5660300002)(356005)(8676002)(4326008)(7416002)(8936002)(7636003)(40460700003)(86362001)(36756003)(40480700001)(83380400001)(36860700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2023 06:34:12.1459 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1430facb-d187-4dc8-83f9-08db8f34a802 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT092.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5719 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,URIBL_BLOCKED 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: 1772648743358353980 X-GMAIL-MSGID: 1772648743358353980 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 Fri Jul 28 06:33:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolin Chen X-Patchwork-Id: 127356 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp248225vqg; Fri, 28 Jul 2023 00:06:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlGrg7fVMKwmbYfSoAa7cdT2MawrNtok3LX+sLjRVA789QpjQ4Li5XkyZVKiNwaG2F/74EeZ X-Received: by 2002:a17:906:7697:b0:993:f744:d235 with SMTP id o23-20020a170906769700b00993f744d235mr1342864ejm.6.1690527965065; Fri, 28 Jul 2023 00:06:05 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1690527965; cv=pass; d=google.com; s=arc-20160816; b=mpPy5V0Hr7bozrRcCArF7WrzYNGtqKjkgCSWiJzaFRyPQo1aZ6ZMKn+Cwb6CK1+RSP 0d23LlXeeaRmexWGRQv3vf2o4IUvdf8L1PbmsZPFyXC40yMiI9dK8lppQJexII5NLwT3 y3fAudxmdwpbbQTSkSV7PT8a55j9qmz/EP7pCkq6K68us7ie6K77rYjaCR09khDons1C 30h6Rtyz7WJJrJO51impS4xYYjBX40/meb3IcN2OYvKuaK4hnsrRwddsQ8/XHpt1vVAr p9fsFcW8Is3uRIsP3ddz3QRPupbCPVrWxQKK1MuAZKChCjlz0vXUSvI3g4o0rNxSyI3w Ho3Q== 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=JpzGThtja9oyEKVQgmeNNs6gaJoi8sqS1bpQV0YOHe9HeddThdO0oQ9Bxw+KbThSRC BNkHF+Akp2xPdcX3CfkO4jES9X8v/DkquG3TcLXOm3FB0MRDUu+pDbMDst47lUnTv/QR 0tt2JcWmN9jt9WhUSrE07UglnoQad3e48LNNCk8y2BGwGMs0g4ZmFLgvbhLirCSgHknp /IGTqaXJMOuyQmHdxTRn0M1mWDI6tms9FFVE+e8FNVRfdwAoM4MeLvN8MuJl6Ide9l7L Pp4D4VeaUR6zYSeFyj4ZwOMgyI8n169bXbtlgcsk9PhzOEg5mBHv3C9MTXHoVGMfI0xo GHBg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=fScOYJE2; 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 s23-20020a1709066c9700b00997ba1c6f3dsi2266824ejr.403.2023.07.28.00.05.36; Fri, 28 Jul 2023 00:06:05 -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=fScOYJE2; 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 S233708AbjG1Geh (ORCPT + 99 others); Fri, 28 Jul 2023 02:34:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233628AbjG1GeY (ORCPT ); Fri, 28 Jul 2023 02:34:24 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2064.outbound.protection.outlook.com [40.107.223.64]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BC3C3A8D; Thu, 27 Jul 2023 23:34:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c3cAObRWh1RnZXAvC/gm2sAS27K3aBvCJ/x5y+dQRWN76ApAV1c52tmEfHoTEsOlotNjAIKRgFioaKq7kfJO1MAT7yMnKj+SjcP2GL573Zv/QucV4j2O9iA1qGApE2IPOVU5ovmbLHYYXJNUwIQgUUAtuX4qQJyMl8SYGkLKPHLjG9iIYGiCoZcoTTeYEF7Y4JOJtcwtF47c95lB9sda/ZGDhPGfdg6WPAr9Mdj2GdsRuEC8bf8VuaPbKj/1RsB7HOmVhZkWP2kgI5w3M8uP4RGDp7RiLDSALdA2noyZxTWJm5UuXEeNegVD8LnXX8Os8zzJ5+FOyh7wQ+uTqLCP/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=R6aD3dt1PckeOKkxIclyv3iBH0qTiumx46+55bGQRk0=; b=CXxC1JRIui7/bG2Ns+Cb6AicbuaTcRsxYWra/FMwM9uOiIZeZJZ+LSVG2Jh35Q5yQ53fiHUJF2SS0vhJYTLAi3KOa4F2Ym7vGhtVz2gFcE18eqqon56hGAT8S6i+Fgm6k/VXK8wJVGyZNdC+MydQjqvMBmltJBuWUN/tynJbuOb3O4a6Tnxn6FtT8jWooIQ37kpD/vgxugtDqXZ8ZFIBHPs9crJHaj9UEFa7OZkpooAR4bhqeUzKzRj5D+csrX35TLSUOAIrW/qO5gcdbU25AKu91Nhr0ZTn7iKA4GA0fDdyf/kSmmE88awCU+nkoWnR6SBhBQR7uWP+7GZ1ByQjLg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=R6aD3dt1PckeOKkxIclyv3iBH0qTiumx46+55bGQRk0=; b=fScOYJE2FWBrdJsBgHOVVVivhdNtig65ZQocOEw8g8puww771/C3XxvStGdcerGeIcuMwt/TK5bzz8ITafHMv1qyjQgS+FQGu2qbvotw24MtH/7q5l4jRCCEvWyzMHATbL5ZfXdovXZdq3UKsUybONHmwk3+54BhBdSNJKpdZA9zDvx88VDpn5Lxf+FnpDT8AsQYbrq4TmIUbBTJKuzOrmv7x8PsqNb+Q3MNYGlKMscxZ3l0288sID4PEEDKek4bphGlqLaWJjjnOOsS2QlDBzx4loFnGmC3olOPXqTQzGnY70XWRpvrzhO5AEe60P9ujTPCMNYPKXti2YFRnyOzbQ== Received: from MW4PR04CA0221.namprd04.prod.outlook.com (2603:10b6:303:87::16) by IA0PR12MB8930.namprd12.prod.outlook.com (2603:10b6:208:481::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29; Fri, 28 Jul 2023 06:34:12 +0000 Received: from CO1NAM11FT078.eop-nam11.prod.protection.outlook.com (2603:10b6:303:87:cafe::d3) by MW4PR04CA0221.outlook.office365.com (2603:10b6:303:87::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by CO1NAM11FT078.mail.protection.outlook.com (10.13.175.177) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.29 via Frontend Transport; Fri, 28 Jul 2023 06:34:12 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.5; Thu, 27 Jul 2023 23:33:59 -0700 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail205.nvidia.com (10.129.68.10) 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 23:33:59 -0700 Received: from Asurada-Nvidia.nvidia.com (10.127.8.13) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.37 via Frontend Transport; Thu, 27 Jul 2023 23:33:58 -0700 From: Nicolin Chen To: , CC: , , , , , , , , , , , Subject: [PATCH v11 7/7] vfio: Support IO page table replacement Date: Thu, 27 Jul 2023 23:33:29 -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: CO1NAM11FT078:EE_|IA0PR12MB8930:EE_ X-MS-Office365-Filtering-Correlation-Id: a6217f11-12c6-4798-db19-08db8f34a7f3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a4BgrT5wr++NsHUh0BHbBzEFOdXqTkHCDUu1x5kd1db7d/jDjwJbPHV2HxpWLZ9tb0jZ7ZI6teJ2kw74amK45LHEaNufL09iNFsZZNPPoEjEiY3Km2QQ72QEc9NwwSj2BJxDCr7ARIBgDUAePcdhwF97sP3DSm/U5bf1+s1ZGUU/WJDPTRonlN/wvvfPQfiLWehw+Js0RjBmXUwA8CXodJieF6f08X0lyNkvkmlFYSAnoz1gl2XvUo9DkqlCawi10Kht7tYs0MNxtR2giFeaItaoVrHKlQ38l2zDEIdAwfExhfhjJ8Gj6xpBwC/ZLhcSTWUwOrls8mGoWDbcZx95UltnI6bef8SMS41eTKpzlB8AbLHnDPUEmnJAn8Nuo3dMDUlmyhI1WoEBkxHh7AzcygXLSifmSoKVF4/YyAJRsXG//AniszAvweT6TZExH3UByw0596A8Hai5PPGKdv5wFkY5RptI0/uTquGZt5m1iz+YRsVxfPOD/VRPJb8tUmFAMTnhxeDAg7uq69tc3k3dJ3TWnxOj3nNmdjaDUd/0KE5Bd8dm/Gn+UHRB5c33+hylbB0v4ZddpMZRUQdc169OzQvjTy6TMwwYe5/r4xa5sPY/VHH9wMAFyaFmFSXLnZtq0BU/wN+/cq1axkP2A9MsbrG78meJK58+u9SLSd98wwUdonFgJKb+ag/6cVuG9Phw4HMTtFyx0wR0Li0r7gcneGq37DMkSzTgrnSS0kIXEWKuIxdgfJE1cud+S5KYkal7 X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230028)(4636009)(376002)(346002)(39860400002)(136003)(396003)(451199021)(82310400008)(46966006)(36840700001)(40470700004)(83380400001)(47076005)(40460700003)(36756003)(426003)(2906002)(36860700001)(86362001)(2616005)(40480700001)(356005)(7636003)(82740400003)(41300700001)(316002)(4326008)(26005)(8676002)(8936002)(54906003)(110136005)(7696005)(478600001)(70206006)(70586007)(6666004)(7416002)(336012)(186003)(5660300002);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2023 06:34:12.0967 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a6217f11-12c6-4798-db19-08db8f34a7f3 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: CO1NAM11FT078.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8930 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,URIBL_BLOCKED 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: 1772647051321250995 X-GMAIL-MSGID: 1772647051321250995 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 {