Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6261379iob; Tue, 10 May 2022 14:15:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywYuBlt1Ulkz19HTxNgL9GHBq6f75zzTlx20THQvIxDShYoo/O6IIjofOCP8R7gv2cX05R X-Received: by 2002:a17:906:1b1b:b0:6f3:9044:5fb4 with SMTP id o27-20020a1709061b1b00b006f390445fb4mr21321601ejg.763.1652217339613; Tue, 10 May 2022 14:15:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1652217339; cv=pass; d=google.com; s=arc-20160816; b=EeF3uGWh067vh91T4U4DKQisjei6P5AQd4FH4bvSlK8xzi9PUTHe5ZILpjYHlTRQ5U UoQ80YNVpHTmhlOr2bR2pcA70ucet+RRFfy66SA1mCRlT/t3pyAvI7RXv+T59nrntbw+ vG/9OfRAEsEqPRcGAWMbFwwsH6aLCXAR9EBIPjmlJr0nOqQLxcpWoJXXI6Ra/0N9kLMB XvzsGf8BHyFGBmfrteKfIAqgzaZ5kq38WHwSgFnAixx0kp9wf5kWmyzObb/4vIqCGkBP D7rN6J+//29Os3pHHT91yFG076aeyC/CLx/G0YSwTcaQiik5SwzkFWGo0DS6YwNtNkfX MlZQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:in-reply-to:content-disposition :references:message-id:subject:cc:to:from:date:dkim-signature; bh=x3qxxIbtinC5jkEoro+y7CsWf73geLNZE15fSbU/ee8=; b=BQtS+iA4lMdP+PwZ0ZG7ZOu1pQjtXWxqtl0eBvEv9RHYeY+Q7uOb3zpwvn5zCVl+Op CzR+CoyxWMQzu6eznl2hFe80ZvK0QA2Px7L3OO4fDM+z+4UBUMsvngOPPbY1iN5mGWkQ 5kNuIOAbtYQR1l5i6lkdVkUWTJU5BnkLuyfMpiiVmHUkb3VQ0DA5wUjcjd2b1lqEmjTt PKTLBQM2C3r0zJ0sxTF9pYzSPo9wEcRLTS9IPdOhzY0NFAhtLWwjXAXeQ4DNvqRF5Inz pLfwAnapmRJ+4D4Otmw8X8l/so6imz1zR3RZ66UXTYSFvS5LdUnRmGsqsFHKJGj+bEMt jl4A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=SzY0Wxer; arc=pass (i=1 spf=pass spfdomain=nvidia.com dkim=pass dkdomain=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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id eg20-20020a056402289400b00425d7acafeesi270438edb.374.2022.05.10.14.15.16; Tue, 10 May 2022 14:15:39 -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=SzY0Wxer; arc=pass (i=1 spf=pass spfdomain=nvidia.com dkim=pass dkdomain=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 S1345797AbiEJPcK (ORCPT + 99 others); Tue, 10 May 2022 11:32:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345930AbiEJPbO (ORCPT ); Tue, 10 May 2022 11:31:14 -0400 Received: from NAM04-MW2-obe.outbound.protection.outlook.com (mail-mw2nam08on2071.outbound.protection.outlook.com [40.107.101.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06E6512A8A for ; Tue, 10 May 2022 08:23:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JcDfWsw3sPhLz1sXsaPbkXZeS0K8rxSJpsgziQspvevlekw11lUS8DBtvdqMhTPGVcSg3Q9LtyJ19vGUV05nD/R/wGW5UJ5jNRyK3qS021EiwE2cXYXBwJ/YMT+uI5Dpt+rctltk+425fGj9oXHpIQJNrpcIo6FFrBddF6mQ83mQRZy5JY0Nquk7Zs6QFoWpah1DCzFv1EHg42FfXmgtmalDQ+DJ7FxhSYQjVH8+DrDpK2dsXq1LVfofdXiPy4eZerL+AMsUD3N4d0OVJmUYQESncaoRBrB4m308x/L91TPstg93oGwLiqCSdUnfZdaYA1aFndTMBtiXNr2VMbfD9A== 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=x3qxxIbtinC5jkEoro+y7CsWf73geLNZE15fSbU/ee8=; b=EBKJtbEoWJ6KrKpcOrqV0MhuiLCGPutSk0qiT+ZaKn9PTbtV0v8S8r10rivrzWtmiNPF9l8b/vHlDReQ56MfZ7icJAuek92SrhUglEMBMJpbyTW4vPCfAzn8+d2XgFDoEVNKpogCMhZVFPhHFiKFKmg7UgHC6c6XT2EhHqrNSWi831vvmMSGE/yeeSuqqZh9NoZeG/G4aoxbFC8EOycCmHugSn6WJkA1mgfpi9VPt0prJYIXPHkWi4DgYE0SE99v4YBSx2k6KSbgVh5bIKqDtUzLNxtzVPfdg0xvadE+HTMwJ1aAFYTFA8ohi96DD1VvNgLPA3YuQMmo7yf+mhknDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=x3qxxIbtinC5jkEoro+y7CsWf73geLNZE15fSbU/ee8=; b=SzY0Wxer5Yw7Wj0ITp7faq7wIuXSXaTvUo9dc2HmY65102Kznnq2PbEyMygk0ULoTm1jKQg0HX+b5y087NbQ56uRIZ/9yRkSB/zZXPKcq2BRVQ6JnHZyLdxhjwQesFcIIU22lNYqoTMDUFszyy+itX2tDgkDOnHD5UIceOe+tUlY4VwGVyv1Q7epUNZ3uWd2XFuJBYy1xZ+8MMOEbpoNX0eOeDDC+ZedFlS3jt4lU2mi6mWK2NdLcoRBePdkoMQzZ2aimyG8zPh7oIAYx7PnkZaL4Hw2IdYFdei9xhC73At/xWAini+LPgmRYEH+s5ekPCe1ofKoSG4iGuVstITKWA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) by SA0PR12MB4510.namprd12.prod.outlook.com (2603:10b6:806:94::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.20; Tue, 10 May 2022 15:23:31 +0000 Received: from MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::ec2d:9167:1b47:2db2]) by MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::ec2d:9167:1b47:2db2%5]) with mapi id 15.20.5227.023; Tue, 10 May 2022 15:23:31 +0000 Date: Tue, 10 May 2022 12:23:30 -0300 From: Jason Gunthorpe To: Lu Baolu Cc: Joerg Roedel , Christoph Hellwig , Kevin Tian , Ashok Raj , Will Deacon , Robin Murphy , Jean-Philippe Brucker , Dave Jiang , Vinod Koul , Eric Auger , Liu Yi L , Jacob jun Pan , iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v6 08/12] iommu/sva: Use attach/detach_pasid_dev in SVA interfaces Message-ID: <20220510152330.GG49344@nvidia.com> References: <20220510061738.2761430-1-baolu.lu@linux.intel.com> <20220510061738.2761430-9-baolu.lu@linux.intel.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220510061738.2761430-9-baolu.lu@linux.intel.com> X-ClientProxiedBy: MN2PR20CA0030.namprd20.prod.outlook.com (2603:10b6:208:e8::43) To MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d21d48eb-8269-4bb9-328d-08da32990a5d X-MS-TrafficTypeDiagnostic: SA0PR12MB4510:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yKFdoNKvmzmmGQuR4nRkIuUQ1xSpjQKPornHDXOQioVKRZ924bEXwQ/1qxpV3TdmviZoRHgUy3qz4SAl5WP8QqNps0dROr229MnAepo/yTzavNMmaijQ0W9WAf1bSFDUb6VTlRIvDc7yMlFdQl3qLbhCzG4m0kgHMrCV7gIRPUUSGwM+oxIYRYo7rFmYWyixtFz2naWIGm3B0e+cap3w8mnxNRLYnHYurjp6AunAWSLfi63DvowVCvpWjT8Ovuu/KAg90hZzg0n1JY2giwUvzO/0FXwrBli6dUoXdprP5DvZNfacg+BgvVu2uPPuES/7F1juT0NFEfeHKZlw6balTK4UGl/5WZAVfOqmlj3QIVBCD8dXIMNFApKasEzthtP4z6rAX4t7POARPYN7Ec5iC9o3d4bvzdFableV+9Gc47KnZnN/WB87r/nA9ucgiVhX5YhcBemrlCNEYRm3nkmWuN4TefEAjFCCEUlriYGqds2HxtD6Jf9q477h0Lm5r8y/xU2gtvFBJULL/pNL/16bI2FkDub+9HQulQApvw9RiCVtW/kk96T965j6xuI4DebgSMNXDiV5RbTW5sE6v//vE82rW2qJ30LHB8xitGuHukYFQZbKn69pSrr44a8J/VgXLySRM5drha6w0ty3e0nvqA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MN2PR12MB4192.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(38100700002)(508600001)(6486002)(8936002)(316002)(54906003)(66556008)(6916009)(86362001)(66946007)(8676002)(4326008)(66476007)(6512007)(7416002)(83380400001)(26005)(6506007)(186003)(2616005)(1076003)(2906002)(33656002)(36756003)(5660300002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?9hPr+psUlkUgXgMNl6vvbxl9+dC92QB+XfR3E/B0gGW/ddIIW7nAfgcVF+kn?= =?us-ascii?Q?XoEZrkS89x4goNFRkbFmDA6V0Qrg/FE4qdrn9lkGTrce1vBcnFNUJkO7V5cO?= =?us-ascii?Q?+OVnKqwViwCtdotitG9PPT+u9A7igKri64tQniStUeuVg+phdFKP9tol22MO?= =?us-ascii?Q?1Zx0ZmaO2GnaE+Bp948hzgseJv+X9JDV3SLPJ61ctBNzxKTS2oycNdnrlG78?= =?us-ascii?Q?jSAADRRj2j3OniCRC1j80R0d4ofbYni/jNvgFW5EvcRl3p9rKcumsPJ3iL1K?= =?us-ascii?Q?kLO8Losm8Y9lzyBjx4cXJUVQ1UfZ3mJF0q1py9YiUC0tpfA0F7l5euaefZSB?= =?us-ascii?Q?N5S2SpEEkxQ50iHkMrvjzE3x9UldsThCJvPeyZS5VKGnSauSCjPTM9YeHh1z?= =?us-ascii?Q?PJ2vgappWkgXNmon8u2UweBIklOyafzZziOwZsN7XQcxHKkMng/Rss0bl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1R3TrizoCMVif+4hczrxlON7o11uw6H89jXV?= =?us-ascii?Q?VoZa1KSoyDY1ffaM3+aPS/CRsmA6lR4ZFKdlTr2/xe8gOZXPvawNhotcADkm?= =?us-ascii?Q?evUiGeW6znhirh9yJ10ml5HhWedZ/YA29J3FvbzonRz15pgiTZmb4QrShHAY?= =?us-ascii?Q?L43ew/zfwtILemfLI2tVUwUTWhz/QOJwljatY60isjk85VC6bGON2lyog+nZ?= =?us-ascii?Q?tbWFPhISZNhTecJZGzsqgEVrLKqGUSATdpKg9gKV1Z6rjiTRMy3OdCuHQqR5?= =?us-ascii?Q?zErVbQwa1krVolJWNzrl2mWc15HrLCrStapttzFrI91073KyC825qBak4UcX?= =?us-ascii?Q?omf8kL1uOAcW8zAHAHPVYDJb0gLQxnGPLIoYL0kkfQYSuQ6f+jrmb37BKcMP?= =?us-ascii?Q?Xyna2sZYXw=3D=3D?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d21d48eb-8269-4bb9-328d-08da32990a5d X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB4192.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 May 2022 15:23:31.3017 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 8MsOCAssO5PH2fj2sl5glfreOBl9QF/xGVSBLqqWKhyAeTF6i2bmUOLLZ4Mqb2Yj X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4510 X-Spam-Status: No, score=-1.7 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 On Tue, May 10, 2022 at 02:17:34PM +0800, Lu Baolu wrote: > +/** > + * iommu_sva_bind_device() - Bind a process address space to a device > + * @dev: the device > + * @mm: the mm to bind, caller must hold a reference to mm_users > + * @drvdata: opaque data pointer to pass to bind callback > + * > + * Create a bond between device and address space, allowing the device to access > + * the mm using the returned PASID. If a bond already exists between @device and > + * @mm, it is returned and an additional reference is taken. Caller must call > + * iommu_sva_unbind_device() to release each reference. > + * > + * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to > + * initialize the required SVA features. > + * > + * On error, returns an ERR_PTR value. > + */ > +struct iommu_sva * > +iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata) > +{ > + int ret = -EINVAL; > + struct iommu_sva *handle; > + struct iommu_domain *domain; > + > + /* > + * TODO: Remove the drvdata parameter after kernel PASID support is > + * enabled for the idxd driver. > + */ > + if (drvdata) > + return ERR_PTR(-EOPNOTSUPP); Why is this being left behind? Clean up the callers too please. > + /* Allocate mm->pasid if necessary. */ > + ret = iommu_sva_alloc_pasid(mm, 1, (1U << dev->iommu->pasid_bits) - 1); > + if (ret) > + return ERR_PTR(ret); > + > + mutex_lock(&iommu_sva_lock); > + /* Search for an existing bond. */ > + handle = xa_load(&dev->iommu->sva_bonds, mm->pasid); > + if (handle) { > + refcount_inc(&handle->users); > + goto out_success; > + } How can there be an existing bond? dev->iommu is per-device The device_group_immutable_singleton() insists on a single device group Basically 'sva_bonds' is the same thing as the group->pasid_array. Assuming we leave room for multi-device groups this logic should just be group = iommu_group_get(dev); if (!group) return -ENODEV; mutex_lock(&group->mutex); domain = xa_load(&group->pasid_array, mm->pasid); if (!domain || domain->type != IOMMU_DOMAIN_SVA || domain->mm != mm) domain = iommu_sva_alloc_domain(dev, mm); ? And stick the refcount in the sva_domain Also, given the current arrangement it might make sense to have a struct iommu_domain_sva given that no driver is wrappering this in something else. Jason