From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azolkn19010000.outbound.protection.outlook.com [52.103.10.0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 949542AEED for ; Fri, 28 Mar 2025 16:31:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.10.0 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743179463; cv=fail; b=eDpOCV2bENNt8S1DpHKYhUQ5RpuVp/5fg1J6dk6pid0tTH4B6ysk9KAObJEv0x06t28LFpKKWma8Q/uAnmlQwwOlLKBZ6RXxfRNEkEexhkgccXf4ueUz0Ctm+0eLq9LKa6uR6prvh/2GBY20/0UFZvRZmQ3cwQfbtG+EK3i4/Vk= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743179463; c=relaxed/simple; bh=oyGA/Rdp3o29wdjh8f066pvb2joifA+MjP2/feNbVHM=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=NkbjWCbtgXzxEsO+8f9iNeBsypsnDBG/vdp8TfpEh2cNkja+IafC5gmht/P33FUn8d8kjv276sxg4DcmZddHOYbDoWjD2R7h11fkuaSE0utMmMjimKquSmEIyjSt3WQDD9pqi0rBRYTegv/jyaiwrZ9gFuBnz9xHsRbiMmEDJ9Y= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=qwMZkS4u; arc=fail smtp.client-ip=52.103.10.0 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="qwMZkS4u" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=U6JmUu/Hqs+frGhi/fJ3DguzQY3OpEsdgo/1IlK+Q3XsoaspjPWB6FD2ETktWRSQ/sU0nRJNoevv1Z5YVhBxV+9ToDDHyZ1ht2siY7LKS0Hyv5CFX9u+E3AYXfHEmTeKJZYBS1x4lrM0mlUvZeodrEUqL+f89xbjYntqrX68lk8YnXz+kgaT/tmejJRuDqPTOsFRIhTKS/lpAp7t5N4ltPifgMHEtDUoEy9I5Fwm+BSvJGUot7P0Zmq0ZWT59OTncK2p9bfvM2pOjZr49nY9yejIL7X7i8PenEAqgXwDIimxbOjRo/R2awsrIxEZ1hhDTcy3CsAxRHY+Zeq9Yfhxlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=eKfPkJWqqtna9YgBxWWb70kH76Gv5EB8a2uCVkpzmGM=; b=xEpPeBj3yVGPrS4HjlK9JbVoQGFxsWTJeMJL/u0YKoQDpAzaixddVJz80UI5h1FMVDl2BXsdelHBhuQl+4YeSqTqL5RQ2A+e4OtTM2bQ1hx4zr9WjSc9CxZB09+KuifXVkqnjz0DVdqzW6GoKXe8fK1uDCU20lieJazFRI/CzTSsBmP7FzioirSB2dacELAhOsYM0cx0gU8pMcQcBycrhSBm9sd/b9CNnPkB2bWAe6TI6nyCN8w981gv8KZav7HpF8DsiPTJumJ7WkH9V78fNJu8DRLMsWIZdb0vxJ7YeP+q0IFaQK77SJHoU8YfyZkv/adUthZrf1vZx+0LXYixlQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=eKfPkJWqqtna9YgBxWWb70kH76Gv5EB8a2uCVkpzmGM=; b=qwMZkS4uT4BA7NOVwrkrlP62A0H8/nWVVUQgjsnwMjZNJKsiQh5kVdfHfQYBZRF0iaGDmxKBWBY2z9DXi79581b3dm5zUE3520UIpeXlisGYJmWSqFmX5JvVQs/QeTN/KD/oo+/Dsr8LFko57VO+fig6goB/sg4xzAhM9emNY10HuKgNIJN+Pt54mt+S9FCxepaTGoo6QzujVD3alf72VjcZkYGzlQwuu8a1VI8KP5dFR0A7zM721oDrefzRwHkkXWAxxc9RpKWtai6pTjOSn2jISz2cGjp+EfErgCBsYTEh1NDtI3nSA3qL3vZ9Be7mcxFJ8sHKx+XIu6th/XMXXg== Received: from SN6PR02MB4157.namprd02.prod.outlook.com (2603:10b6:805:33::23) by LV3PR02MB10209.namprd02.prod.outlook.com (2603:10b6:408:21e::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8534.44; Fri, 28 Mar 2025 16:30:57 +0000 Received: from SN6PR02MB4157.namprd02.prod.outlook.com ([fe80::cedd:1e64:8f61:b9df]) by SN6PR02MB4157.namprd02.prod.outlook.com ([fe80::cedd:1e64:8f61:b9df%4]) with mapi id 15.20.8534.043; Fri, 28 Mar 2025 16:30:57 +0000 From: Michael Kelley To: Teddy Astie , "linux-kernel@vger.kernel.org" , "linux-mm@vger.kernel.org" CC: Xen-devel Subject: RE: Allocating SEV C-bit-cleared pages (without relying on swiotlb) Thread-Topic: Allocating SEV C-bit-cleared pages (without relying on swiotlb) Thread-Index: AQHbnyKPeCLT8FgtS0qlE8WmAsket7OIu/hA Date: Fri, 28 Mar 2025 16:30:57 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: SN6PR02MB4157:EE_|LV3PR02MB10209:EE_ x-ms-office365-filtering-correlation-id: 25cbc120-57c0-4dd9-a8c8-08dd6e15eb5b x-microsoft-antispam: BCL:0;ARA:14566002|19110799003|8062599003|8060799006|15080799006|461199028|440099028|3412199025|12091999003|102099032; x-microsoft-antispam-message-info: =?us-ascii?Q?O1DDhA1dGv5CV/809zFqC40jwS6IHbERt5RRaO/HQl6tEjZuCoZd/Bp7+tI7?= =?us-ascii?Q?E04lscUt7jIszTZKwlZyY+VZewNRaoTTeHiLcX3NFV/TV8zl0f4A0mvRR/wm?= =?us-ascii?Q?6sa4/tqsHYh4SvMqufU1UzUK30Jp1AgiadL/UpHQpmxytWDPM2cwM9MXaHXg?= =?us-ascii?Q?lNIj739AMiDE6szoVNUwDK+t1i9G8rqMj8+LokKlmjT4VrLmY77/m69/pAu8?= =?us-ascii?Q?6+SVli7HxE2EY1yfncf7yl9f2D3MNTPq1Z7fC4fl3xjYklaGvs8xDZ0nkdTl?= =?us-ascii?Q?O+OmAsuHcewFXfJ7auWol5sTnlSJPIZlU1FTtQRnQih02YyO8FU5N61QHrkV?= =?us-ascii?Q?duGmhx7sY3dsJPwrR5/YnT8fmbZuJ/3dYld+YjQPMsvgrHGXcMBfy/+MvG1w?= =?us-ascii?Q?TfGL5AruIhyRYuLKvsn9oXS4jVH10W6dPstxOFyvrbau6kMWwiHB4WS9zw43?= =?us-ascii?Q?Tv4FhV6RtTGlLjGY66nd/MhGjBfQHZwPhQ8fi2JlX2Zf9H1PTiR2/tRNFFnw?= =?us-ascii?Q?TM7zOwrDdslfES2sDjlD8FZ/ttcgCJ+7iuUsyQQNBD+LiHEPZPIoobewmOVF?= =?us-ascii?Q?j3bhbC1hAPCB+D6JNksf2fN9DymsNJPvxC5sVvv0BqyU+5ZYNAkY+MdqFh0u?= =?us-ascii?Q?92mU6heOjXTgn0bWR0NDxaL6/FR8GF3ZRZojIreY3EDq3nTKgxtGR6paDwAs?= =?us-ascii?Q?0lZ/paWIyTTQHXqVM+LNdPHe3h4IMZ7GUt/T1ph15X7c5Blab+MnYjWtS0Cw?= =?us-ascii?Q?krDMuup6SKa45VCQ5L0tWvkJCkJHW+paWqDjGJ+BsIty3C+Jw62Dtrf3pwdI?= =?us-ascii?Q?UeEBlKj8HqbRyZC1cmt6GloA9QBt8U2V+Ehufrok1zAhEGjQurk4+w4aq552?= =?us-ascii?Q?wgBrEH4bzdr60F2LJI3B8Y4pgMBmskRkVZxdHSngWpSHeudyQpwgHWdtMz3L?= =?us-ascii?Q?O03uQZgJOmfXP/SzQ6iLJ5/7uklXblfydmVZEmlwn+PkK2T0U4k+mHg7DYh7?= =?us-ascii?Q?VKAN1IzoXFFSVG7sclXhY9rMFzCZX8n2qQYyjgdPdfGtBx6uqnDq3ShbVFsp?= =?us-ascii?Q?F2XCL6trQBgU4xCV15iYIB9yeBzBkkK8LbcEdLh+87+vwZwa2j34EZWP1ZFU?= =?us-ascii?Q?UVtSMumlJ9yo?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?QauGjtUi7yRbM5De0W5mbr7xev2SJvEcOhT63ic8DU+EH487u1Gv+vD5pw0Y?= =?us-ascii?Q?NE94xoxux554nOg/p4+MPS2QCvrl8H4YimSTqF7sI6r9mulxIRJCRtmg2haE?= =?us-ascii?Q?SXEhRcKSf+DIalnwJ+sudX0kZ5XYBtJtyBWH9cLGJL2cIG/yFdxRKNgDdxd2?= =?us-ascii?Q?7jXnRKPUOmKejQNLcDgu/N+w7d4vxBDHwWbRzVX8nc6VJGk5vmIyjT9aahwE?= =?us-ascii?Q?JJQUQ7uxeCV8dtnyj122riqGbbZtSccm1dgSXnnO9s4rNOXIoPUl5SimpgdV?= =?us-ascii?Q?vD5mEK9hMI3sPbXuaqurm9PfqO7wpBZyJVT6gz5etO5TdiscqYMtHt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23pxFkM6DAN73/iP3/kVewPFtHnhyrFprFQ?= =?us-ascii?Q?=3D?= Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SN6PR02MB4157.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 25cbc120-57c0-4dd9-a8c8-08dd6e15eb5b X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Mar 2025 16:30:57.7903 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR02MB10209 From: Teddy Astie Sent: Thursday, March 27, 2025 7= :12 AM > To: linux-kernel@vger.kernel.org; linux-mm@vger.kernel.org > Cc: Xen-devel > Subject: Allocating SEV C-bit-cleared pages (without relying on swiotlb) >=20 > Hello Linux mailing list ! >=20 > For porting Linux code to make it work on Xen with AMD-SEV, I need to > change the allocation of some pages to use "shared pages" (C-bit > cleared) instead of private pages (C-bit set, which is the default kind) > as these pages needs to be shared with the hypervisor/Dom0. >=20 > Is there a facility to allocate pages with C-bit cleared (and if not > running under SEV, just allocate a plain page) ? Current Linux code for > SEV seems to only rely on swiotlb as access to shared page is mostly > made through DMA-kind devices (e.g virtio or emulated device), but I > don't think it is the best approach. >=20 For allocating memory that can be shared with the hypervisor, allocate memory as usual (with alloc_pages(), for example), then call set_memory_decrypted() on that memory. This approach works in general for Confidential Computing (CoCo) VMs, regardless of whether the underlying hardware is AMD SEV-SNP, Intel TDX, or ARM64 CCA. If you are running in a non-CoCo VM, set_memory_decrypted() is a no-op, so you can call it without having to check whether you are in a CoCo VM. When freeing the memory, do the reverse. Call set_memory_encrypted() first, then free the memory as usual. Note that if set_memory_encrypted() fails for any reason, just leak the memory instead of freeing it because the encrypted state is unknown after such a failure. If you search for set_memory_decrypted() in kernel code, you'll find several examples. See drivers/hv/hv_connection.c as one place where code for running on Hyper-V follows this paradigm. There are several other examples as well. Michael