Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1309C05027 for ; Sat, 11 Feb 2023 00:45:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229676AbjBKApA (ORCPT ); Fri, 10 Feb 2023 19:45:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229479AbjBKAot (ORCPT ); Fri, 10 Feb 2023 19:44:49 -0500 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2058.outbound.protection.outlook.com [40.107.243.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CC3B6FE8D; Fri, 10 Feb 2023 16:44:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=n565GDYPxpdaWYV+l+pXvsGFQfxQ4BTIawPTIz7mlM9KekKJJgy2Go/6Bx5GKdSAgCjej8wIapVtDvLr3MHzS66+wl17EbkoctHu4Gw/wdW68LynNazxjSAWEWRuh7+MBYpkKu6fOuT0iU8goSdlUpWS0L67wCsPAVSwSyPwmcJpGqky4Tt1a5a1HfnZEV69mWFmvC88bEH/rsHIU7iLJkImRGUFpLVphyGUGow0Sb+V5ZpX6aEe8EHs2z6LyFhQsWsN8A/pjXaV4HfYpqSZu0as1ovf55moM9LqhJaG/EdmbPHkZkT3/XJyi6R8tY9Yt078ul7X0A5lXza3H82GLg== 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=jzyYI4SMIIRMKBVigltxXqbFrfGTHbn2xI+ZTH73ChU=; b=VjX1dgC1oTyx1gIpr0sVti+KHzFhBuFC7dEVE78wWFCR7mWc4mUXknH2//QN4IRtuSW6WWBwH38kM5xFUT+6YoBcSQBkvQUCQSUOKzY2ITZ4C2JeoCY+m7wV8tDfDUcUbQ1ECJXqJFD9HEW3HQXrnEOyrctM4HKV5FD3b2s39L/NaXZH/YioG7m3A3KSXF9P1hEYaYLJSEVv2t+97ta/fPOGFhj1Wvx63pu5Ng6McJhck1fUqfIMZQs9gauRYwFNbp+0hETpyazKa2if6k2UgAShE10L8EPitMpPO6oc76MwWjxK5c9ml8tqI2kcSByQVXlAApx6IXAkjFd9/0GgeA== 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=jzyYI4SMIIRMKBVigltxXqbFrfGTHbn2xI+ZTH73ChU=; b=lGkUmkGgWt80x0fKTa4CgJEZ/Z+XVZsZ3GiACK6ehXnzHfGNbx8axl4zVFvwRi5Y7ANx7jXkfR0LmnufJNXAdEGd6CnUTShsrd7sPdePbs+jDDWvFV52QRTZ68kLuhNzphMjmXlZHYLl+5UqKk1/K/W+Mv+VvYVHteOPq1Ga+AYRLv83Amug8A3FVCfPW0ICmx7R9YwstriKUlEkpJi4lvOjKnIbMPYQ/GL0sk462h6t4zbbGH2hVHzQnoV1EPuUyFU5RQDOd+l7F9a25oCa0gJX8SGx0U2siGGzbqew7p5GoSTnORsp7+bm5ZRMP7VGPeF4uzZXMi2PVaN5YanwZA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by PH7PR12MB8594.namprd12.prod.outlook.com (2603:10b6:510:1b3::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6086.19; Sat, 11 Feb 2023 00:44:41 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::3cb3:2fce:5c8f:82ee%4]) with mapi id 15.20.6086.019; Sat, 11 Feb 2023 00:44:41 +0000 Date: Fri, 10 Feb 2023 20:44:40 -0400 From: Jason Gunthorpe To: Alex Williamson Cc: Nicolin Chen , kevin.tian@intel.com, joro@8bytes.org, will@kernel.org, robin.murphy@arm.com, shuah@kernel.org, yi.l.liu@intel.com, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, baolu.lu@linux.intel.com, "Raj, Ashok" Subject: Re: [PATCH v2 02/10] iommu: Introduce a new iommu_group_replace_domain() API Message-ID: References: <20230210165110.4e89ce55.alex.williamson@redhat.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230210165110.4e89ce55.alex.williamson@redhat.com> X-ClientProxiedBy: BL1PR13CA0209.namprd13.prod.outlook.com (2603:10b6:208:2be::34) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB8594:EE_ X-MS-Office365-Filtering-Correlation-Id: 03a7c498-612b-4360-886c-08db0bc92916 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: O6azTgrSvBTARk5lVH/3GFVnZeCr29kIXX2O6MBFCDv2vyRAJXc53y8JoLg1XNgtdBLS+0roJrg6JAMZpq4Y6LZQoKfdjwq10h5TCMrhxz7KTIO2qu6Ul+l1FqT0Fl1Qy0oRd5XV3OlxR31Z5EMJ9WpVDVPbszqtAxVogqjd9KdDceTVcVJkplQjYlKRs0YeNRGumTdGjLEZRDSJ9581cJBtimjsrRrMQXnB2nS/ogZBnVQonX8JedrOlORVwwcJ8YoP5NdczV+yCg3S+j5sNIxykYTF6QnsLTTY1nTX6psz5dU69WK1nytO6ETjnFHDT2OOA1G+QDZWi+gSth46n2aQrwhdIh7DX/zYfKkHpy54Uxt8IOhAm+O2TQRh7X6dbcxklvjYuvf07+KDJguIclpx+XxQ6EB+QrAuvzBIyE0CUFrkw943K6nSRFQ3+1eiKb51abXW4Qk+Dg5CoCiF48Iz2/pldgtvJGb4BXwE8mJ5nC3l21HslbMGYm58AIUYIdHBBNPmLUMN1xuKrKv4MGdhYB6RtqFsxW4yGacjvpsftI275X3GS63tCtjUitjuKim20KAkhkNi70SHlMfDtMXIwTQGTx6i5Pc8sitlVsoGPtDt/pQillrEZZBDH92XWvje7phzlw2LoLfn6++KHA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(366004)(396003)(136003)(376002)(39860400002)(346002)(451199018)(86362001)(5660300002)(7416002)(41300700001)(8936002)(4326008)(66946007)(66476007)(8676002)(6916009)(66556008)(2906002)(83380400001)(38100700002)(36756003)(2616005)(26005)(6512007)(6506007)(316002)(186003)(478600001)(6486002)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?drZ8aNO3mFW7znSRkjLQrHGI0eKPXMaGy/6Zm5KjOxmbiH1Hz4tllynDlaMX?= =?us-ascii?Q?QzZJ8Qb/N72pB8mPXkcoIOJCdVl5zQMrKeq5P0rosoCHMl5dSnjLW2urD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2nNE/weCJDLP/k3JLyn9Z0QhAFqamx6vZ8rqdcl?= =?us-ascii?Q?spBt3DkVRSX1yWv6vHuOsDP34uENuGoxLCveziMOJqrto8EZoMSgwgBsVqCw?= =?us-ascii?Q?5o2vx5yriiucuMr5ea0SVWuutEn5sITI+JnzTKaFhUIzUxXnoTXcoBF3ZbAZ?= =?us-ascii?Q?ikD/NKaACSyYVWRntg+nbUIwFG/cNTIDU310LlVivYBrFHAJe5dr8RzNtrpO?= =?us-ascii?Q?UV90Jbc4UbSSi4VbTlawysB4UUZwfu2axaJjsGJNQZcF7u4OUWNQdpbMr9N9?= =?us-ascii?Q?Uh3aGX9siTAfDIANEfeD0i7lU991d7np+9+9lu2nZ3kFsSOv5EO95EbDVPeX?= =?us-ascii?Q?Ai0t5t83439EEeGmIWMeSxSoYYQ2V/41WnGQu8nc2s/+8EPvR8QVcfgIjW9P?= =?us-ascii?Q?qBRHVNY8PiI+nTbbXQX7EiTtsiWeTGaOgfq+Nxn0ugtf7ZoRPvMxxCXKVc8Y?= =?us-ascii?Q?1wWpGtDOiUOTVZ4wG2C4v0LjCUSW9UBFgwpTY5KgfRcWUsStrDIjcGIpiWW7?= =?us-ascii?Q?pC7+cwHSd/KYwKkYOuMbwCIshAVNwyuGA5xlX3wR0QI7IRenwobJzA1SurlC?= =?us-ascii?Q?itsTpRcZRjCc0toWUDxvonnMzHzEEcMTvc/FC96Z?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 03a7c498-612b-4360-886c-08db0bc92916 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Feb 2023 00:44:41.0450 (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: GRxw1X9VpO1SbKV1gAG7XALT9EMEk0dzKlP9mnVhETTo3ozmrx5qWGIed9sgbPQ4 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB8594 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 10, 2023 at 04:51:10PM -0700, Alex Williamson wrote: > On Tue, 7 Feb 2023 13:17:54 -0800 > Nicolin Chen wrote: > > > qemu has a need to replace the translations associated with a domain > > when the guest does large-scale operations like switching between an > > IDENTITY domain and, say, dma-iommu.c. > > > > Currently, it does this by replacing all the mappings in a single > > domain, but this is very inefficient and means that domains have to be > > per-device rather than per-translation. > > > > Provide a high-level API to allow replacements of one domain with > > another. This is similar to a detach/attach cycle except it doesn't > > force the group to go to the blocking domain in-between. > > > > By removing this forced blocking domain the iommu driver has the > > opportunity to implement an atomic replacement of the domains to the > > greatest extent its hardware allows. > > > > It could be possible to adderss this by simply removing the protection > > from the iommu_attach_group(), but it is not so clear if that is safe > > for the few users. Thus, add a new API to serve this new purpose. > > > > Atomic replacement allows the qemu emulation of the viommu to be more > > complete, as real hardware has this ability. > > I was under the impression that we could not atomically switch a > device's domain relative to in-flight DMA. Certainly all the HW can be proper atomic but not necessarily easily - the usual issue is a SW complication to manage the software controlled cache tags in a way that doesn't corrupt the cache. This is because the cache tag and the io page table top are in different 64 bit words so atomic writes don't cover both, and thus the IOMMU HW could tear the two stores and mismatch the cache tag to the table top. This would corrupt the cache. The easiest way to avoid this is for SW to use the same DID for the new and old tables. This is possible if this translation entry is the only user of the DID. A more complex way would use a temporary DID that can be safely corrupted. But realistically I'd expect VT-d drivers to simply make the PASID invalid for the duration of the update. However something like AMD has a single cache tag for every entry in the PASID table so you could do an atomic replace trivially. Just update the PASID and invalidate the caches. ARM has a flexible PASID table and atomic replace would be part of resizing it. eg you can atomically update the top of the PASID table with a single 64 bit store. So replace lets the drivers implement those special behaviors if it makes sense for them. > Or maybe atomic is the wrong word here since we expect no in-flight DMA > during the sort of mode transitions referred to here, and we're really > just trying to convey that we can do this via a single operation with > reduced latency? Thanks, atomic means DMA will either translate with the old domain or the new domain but never a blocking domain. Keep in mind that with nesting "domain" can mean a full PASID table in guest memory. I should reiterate that replace is not an API that is required to be atomic. Jason