From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=192.198.163.12 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796408525; b=5FCbdzlYDHsyv08XV4k7SZVYVqyDxTtZPQBpotfGZo5MHKmiRSVNeAsVArmb7vGObPAz 1oFMnM7FblT+poyUCLLqrDRNFyANS+s7LxgYbN247nb+4OUk6nPdx7Gonoqz4XG9Uk5bT Wc4k0jCVrHECDvc1IT71Z5gTLMlH5jRGYU3c3gG/217OYm3ux16tG3ARH0FNny8dOZYLY /7BaGiSbQ9kouWV/gZSxbuknQTt0zqGCtR8JbYRKqObFI4vRrffWR5j1HNWSiRGtCbesZ OVueVjR9nwlIkciPYvERQZ/ERTqgjX9gHjb6S5e+i5Z6aAJLXWUZMOnizNnu/T6kqjA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796408525; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Received:Received: Received:Received:Received:From:To:CC:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:Content-Transfer-Encoding: Content-Type:X-ClientProxiedBy:MIME-Version:X-MS-PublicTrafficType: X-MS-TrafficTypeDiagnostic:X-MS-Office365-Filtering-Correlation-Id: X-MS-Exchange-SenderADCheck:X-MS-Exchange-AntiSpam-Relay: X-Microsoft-Antispam:X-Microsoft-Antispam-Message-Info: X-Forefront-Antispam-Report: X-MS-Exchange-AntiSpam-MessageData-ChunkCount: X-MS-Exchange-AntiSpam-MessageData-0:X-Exchange-RoutingPolicyChecked: X-MS-Exchange-CrossTenant-Network-Message-Id: X-MS-Exchange-CrossTenant-AuthSource:X-MS-Exchange-CrossTenant-AuthAs: X-MS-Exchange-CrossTenant-OriginalArrivalTime: X-MS-Exchange-CrossTenant-FromEntityHeader: X-MS-Exchange-CrossTenant-Id:X-MS-Exchange-CrossTenant-MailboxType: X-MS-Exchange-CrossTenant-UserPrincipalName: X-MS-Exchange-Transport-CrossTenantHeadersStamped:X-OriginatorOrg; bh=EyG4MoaiWCnln+LdKRAb+Ck2MTEV0MkhyEigw2XOtEg=; b=sAW6jfPxmZaX0aEI7x18FWJdqlfvSMtGuCdwB7UyTR/YEMhXrnAEx7079BIDun9jCBue pUIcADfS81qwOPQ+p1hVfWXcGe/k9KCa4QBmidPev354H0UHbf/O4WWRJ8aZOuXee0GYc wJaVMVUnBAe4M/PKcfDcnek/z9IyrXFdGFpqmjGugMnoBWU+VGUiL5nN2FuZU3B48IFQP CIUT/MIoteMBuRq3OOX222XjFjfjwoefxit38uCOr88s1oPPSXFDqiQnQKGvxy7QMyfvO feZY1MKpyeFwii0ZsP3Nq9af8xsCejIJwBs1v9pJh2kJMgwcE78kBkWXxhjb6rAmU/A== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=BO3xr3Yq; arc=pass header.oldest-pass=0 smtp.remote-ip=192.198.163.12 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3ECBD606A33A; Fri, 4 Dec 2026 18:22:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3ECBD606A33A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=BO3xr3Yq DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796408526; x=1827944526; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=EyG4MoaiWCnln+LdKRAb+Ck2MTEV0MkhyEigw2XOtEg=; b=BO3xr3Yq91FNSTaLDSLsTNeR1N/USgegjQppwWk6fXDnTOTj+qWhPgcc BpTRKDmWeG4BOQnI4jZ/Ma4ZPG8owZE43b5PH0ILLvrzf925cYcglnv4K Wtp+VxBtZ9CQlHDYN4BKojsnqZ+hRJBOS2yqZ7axc1taGVbOfT/dpz5T5 llnW36P4SnHsrSR4Nu1gxnHHj4TyQDGHUsvI5kFZxcZBbxnmbHGgMGUx9 z3QOdracrc7Xb+bFCPh5Df4/SyieiYl/3D8jXsONqUezDg0pjT7vrGAD3 2+C9q2ckP/E6MuDyxCn2dshN5tkj0qreWwIzVslaITQJwDYkKmgKkg+CW Q==; X-CSE-ConnectionGUID: uQUEBfRRRvWlg+KlPtd2/A== X-CSE-MsgGUID: b/9dIPZhRR6Pi29fBXQQAA== X-IronPort-AV: E=McAfee;i="6800,10657,11986"; a="5300573" X-IronPort-AV: E=Sophos;i="6.29,78,1794902400"; d="scan'208";a="5300573" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 10:21:53 -0800 X-CSE-ConnectionGUID: w73+ME2xRQC5gue7npxovQ== X-CSE-MsgGUID: QSXZHXtRR+ap0XICqdKgLQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,78,1794902400"; d="scan'208";a="14300028" Received: from fmsmsx902.amr.corp.intel.com ([10.18.126.91]) by fmviesa009.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2026 10:21:53 -0800 Received: from FMSMSX902.amr.corp.intel.com (10.18.126.91) by fmsmsx902.amr.corp.intel.com (10.18.126.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 10:21:52 -0800 Received: from fmsedg903.ED.cps.intel.com (10.1.192.145) by FMSMSX902.amr.corp.intel.com (10.18.126.91) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57 via Frontend Transport; Fri, 4 Dec 2026 10:21:52 -0800 Received: from MW6PR02CU001.outbound.protection.outlook.com (52.101.48.57) by edgegateway.intel.com (192.55.55.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.57; Fri, 4 Dec 2026 10:21:52 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=pI05mUTlYZt10baXEb83SQ3GUAGKtJOU3EP8wuhz1RvkxmDW94rXaYnHCRcfS5OUGru5varaxj5pAPK0o5Y3fG6cEJSu9l5mti0UYfunIK6vdxnRnA8adMWY2QEbLxp4xZink4Mdou0EhGy5GQONq5Aunx7stWzbYQ45qpsni8X3aeJfncVqjE8C7n+272GmyFJ6cS+1S3IDVLTE7pndx7xoc81A32i5x8nqDThQmvR9RzcoBaNT0y2HGgM/E9H8XiQRrnFnhHhuH16m9ix/eHkKM6PLAkTasgiJ0fQO9FaoiNXJV30sMENvdbaxc8JspLnlfOiqOmFUeNjxhv7K+A== 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=EyG4MoaiWCnln+LdKRAb+Ck2MTEV0MkhyEigw2XOtEg=; b=fSNe/hb86/JpIbWBQLx9SNtMq9gFUJgig9Rc0iDRDHzw2ivRxLPZlEBma5RwbhqHqZgzkU5kyMef7J3T4Eu9Ex2Z9luSOjMBH26Cee5KoL3td64AjiaGqLW9CHgXpBTdWzX4BerJV5HIsMu0auqlHopAjIubEb14k+ZiqWJeIc8wCqz5n+xrRsJXavgtEzrU8i1v4zpw9WRfvM286skelRZfDSBok/3/iahtqoKg1qns51ipsc+012VF+sANZl+lrzVoS7gdd4yeUSONwFa0ZNinufBsntTn0s4+oUKV4gl+wMkH44bGV1xj9L1YHeV+vDTqfkwx0R0Rqc/Nm6sxng== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from CH3PR11MB435924.namprd11.prod.outlook.com (2603:10b6:610:34a::8) by SJ2PR11MB8345.namprd11.prod.outlook.com (2603:10b6:a03:53c::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.688.10; Fri, 4 Dec 2026 18:21:49 +0000 Received: from CH3PR11MB435924.namprd11.prod.outlook.com ([fe80::1696:c90e:60af:a0cd]) by CH3PR11MB435924.namprd11.prod.outlook.com ([fe80::1696:c90e:60af:a0cd%3]) with mapi id 15.21.0688.011; Fri, 4 Dec 2026 18:21:49 +0000 From: Peter Fang To: Dave Hansen , Kiryl Shutsemau , Rick Edgecombe , "Sean Christopherson" , Paolo Bonzini CC: Thomas Gleixner , Ingo Molnar , Borislav Petkov , , "H. Peter Anvin" , , , , Xu Yilun , Jason Gunthorpe , Tony Lindgren , Chao Gao , Xiaoyao Li , Binbin Wu , Sohil Mehta , Adrian Hunter , "Kishen Maloor" , Nikolay Borisov , Elena Reshetova , Peter Fang Subject: [PATCH v3 4/8] KVM: TDX: Add KVM_TDX_GET_QUOTE sub-ioctl for quoting Date: Fri, 4 Dec 2026 10:18:25 -0800 Message-ID: <20261204182022.324230-5-peter.fang@intel.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204182022.324230-1-peter.fang@intel.com> References: <20261204182022.324230-1-peter.fang@intel.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SJ0PR13CA0236.namprd13.prod.outlook.com (2603:10b6:a03:2c1::31) To CH3PR11MB435924.namprd11.prod.outlook.com (2603:10b6:610:34a::8) X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH3PR11MB435924:EE_|SJ2PR11MB8345:EE_ X-MS-Office365-Filtering-Correlation-Id: ef50f96b-61c6-4221-eb70-08df5233751b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014|23010399003|18002099003|22082099003|10067099003|56012099006|261028144727099003|261026140227099003|261102123227099003|261021121427099003|261022105027099003|261022075727099003|261026121527099003|261009223027099003|11063799006|6133799003; X-Microsoft-Antispam-Message-Info: o2Giy88VzmlSHswlsy6ORXnLe1iFcKoPJ9KKHqYe3arJx77JJMPKoUxmCwLrWIx6M0GheFQGUyVJ+xQc4mw4G2t74guGekKliWj+K7be/o+WyILTTCXQxvkDfcve0Jpjilz8Qcu9ZFwXhknFoP329o3VLqcD5Nust0oUDGB+RM0suMeSiiyOn+egJ5pPexZIF19gZTxR0z85eiD1E6d26f1K3L6wnkTiJNe4+bZJ7sKlsOsAPrlkxAwdhp17atWFgUWLsjL9i6ZDeMjCr6oiQtrLG8k+2OWY4Q1UMrne0vrgGq190nvP2MUQUKIX+pXjxtIdQEvSriPoe1mOPGW2/OSBPA/DpIEV1PgR8pK6840zBOvYpRDrq8/uHcRjW+YMThRBakjc/Q6pbKkB9SPdrGi9d/2kks+AAQ9vGKQ3mSlSXs/KV5EDftPfsIU9Xs43UGzU9+wKAaGSnSn9T2E6wc4uiREeB4sf4aeKkmjHI5xVNiiR9aDL01YZS4JQJaLvx81AA++3VcdjU8adl0VB7BP2ZP6pWp44D2HP98f4vdki51B4AhLMzQJnSjyEcVXL4IiudQQI05p4zC54+5haJBwogYGGh7lFE25aYRzq3CYS2rWBETm7E3YBdlpsrJp2 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH3PR11MB435924.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014)(23010399003)(18002099003)(22082099003)(10067099003)(56012099006)(261028144727099003)(261026140227099003)(261102123227099003)(261021121427099003)(261022105027099003)(261022075727099003)(261026121527099003)(261009223027099003)(11063799006)(6133799003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ulEdRf000nbsafEx5GqhAY4uSV6GX1f3N3/UXlUOclyJfvQ69pLiNGXWd7p+?= =?us-ascii?Q?BpmLdqhwVRzdSdmE3SwRKoNHk+d0DmK8CMOExgkrlwiOFly398dzZUWNNI3Z?= =?us-ascii?Q?o9dATPE49WK3JvddHzwXDyuvrCpV/nSkvTSe/RGuBNnh7NczQ3vTmiAlTFPF?= =?us-ascii?Q?H97CmqPa42yMwyQxk2JBmYn2Dg/x+0WfuAhI4TRcjOO3OSsf+A8spXmFrvTM?= =?us-ascii?Q?XJ+t4yean9Oeuan2DIQM9IntVJ09kHfYCPfFqaRofzPrKxI+hQp789UlpnVn?= =?us-ascii?Q?aa0vfo2KBQCOK7Vb/xSJocmqAPo6cEBUkecUthOoq1hhZ9fBSUYy71yGaYC7?= =?us-ascii?Q?CDYDM+XJMP0gj4eEDTYU37WpYgvZdCuyJuqcVxCHTGO6APMidL3S8rjJnIIc?= =?us-ascii?Q?MAD3tbnQscvK8048bGkLJGctYyCkD6QXaKiaw/mtH+TeqnVMqWsaJDYTpuHS?= =?us-ascii?Q?TutXBgEhe7ZCeJHtl2KV2k/GjaRBm25+J/IeNZh9NWDahJwU17i1D850G8ZM?= =?us-ascii?Q?ZhFtITzJFi5b5CeSC0miUxwz2xBdEBxNLWC81JqUnB/npQge70lJVrUOfLh1?= =?us-ascii?Q?VwPspmXTVKWEDSPAgMaC8wB4g5jXGRtlZh8DKvDxdJrei/qWWn4R2RN2lZiH?= =?us-ascii?Q?OJ9CetCIVPB26D7gXeBnKQ0lGEzeA/rhZD6jfWha97SMDKjDTik62LwTFw2f?= =?us-ascii?Q?s7ZC0IQCgN2ec84dArE14dsn0d7Bu5zCEQ0YcasaaFkXsfon8it6TNUpJqX+?= =?us-ascii?Q?b46Wq2Zvh2MXEQjgPhIkcKZpWXSXrW8RrZ++IigA/o1/BCZUTDSbINQukNtO?= =?us-ascii?Q?w1PdLPX8h+q8AQXqKeUeigsgJ5OO9L9XFXkoK0WLpRHkzxHxGCSUeZK+krJu?= =?us-ascii?Q?xBGoXiEjzcHymbE1JVSpqMZQs+LrKEuCX0v3IQWdfFAaNS6gapGhXiNIFwS3?= =?us-ascii?Q?P3QOVs/Jtjjx54vC8YVPFu2hr8fN8jEVmmgHtpVaNZi8lXEUzV2lrK6CYu3V?= =?us-ascii?Q?qjkXKN3EqvjSwwDKyY7dohPRGQyPIOY0pUG6tP+u5EoKrpi8Y3VL7y2M8vgW?= =?us-ascii?Q?2n+yMrnuPUtDyDnhUn7eUkRRE+wPdo8MN3gQyrNpVNFEUmg73qBKNSElkDEr?= =?us-ascii?Q?7pu3HJ1sa3u37h0DMGIg3sWWOZf2kL9L3+C+M/9d6/xJIO5FBILx2APbjV9G?= =?us-ascii?Q?cODVlxrEvveg26fwU5JR8Iv1F0sWzIuq7zPKUKBqRs71akCckpXD0C2JhMwD?= =?us-ascii?Q?2L4ppcAjpI4sa9XT7CJJmvLrW7D1p50zCgM4FsI1ZecNGdafdhR5Cfgvhccs?= =?us-ascii?Q?Hg76vcaEopKa34KNcbFmg51P9lPG6uyOgGirp+UrT6KzKcV5Q7C9xPpydOut?= =?us-ascii?Q?lAIIulZ7KkGv4wny4lJkCfNAwes8izfRbJ+9HmfxEPemOVK/aJc84Z6n6bd7?= =?us-ascii?Q?cHR4wowcVoEsG0/8xtqLfZ0p4FXmKVUPRpePzMjJ6h8Fmdlai23jBNya5WXv?= =?us-ascii?Q?F8V/q8/2c8saY1xZfQuEtBHEQFP0ClcULTzyGpRl/dtz1JMtBurVxNaWcoqc?= =?us-ascii?Q?hoJznvFr0DyAFFJ9Szh8G7c3K/szELxLMg5+LYbrIfXzHBN9ByWh8cNGGImh?= =?us-ascii?Q?SAVQ0sL+7VG68TlXFelxN31x9rNaaPk0nk4Rm4IRvvhnuVojzU4uqKl1AFp0?= =?us-ascii?Q?Oa9kAEGavvduhvut3D1fDkXJLCK5q0XOWq1BcAoHk9XmJV503VKpy87E0PfS?= =?us-ascii?Q?+Yd/qYM25w=3D=3D?= X-Exchange-RoutingPolicyChecked: gzrZjDr3zjdhEwRgGzlsnfP7+42FfXxhuKlRBMAwqkBNniIA34RoCo1cuvYfVXtSNfwTDoIdF2OM2borWlloPElw5q5X1WjDWB5tEwXrJiW7vhXhG4UmkD8yhMgSCS5v/kUt/RGgmgS2+38ftxXpVFhaNS4Oq6knsl5umfvLCk0yNY+2JYeCb3x5fMbutBKglxMy3HX7KlLHUbstNHlEf1kBIgMBR8G7xhFGesvY60q1+QtJWK3MuNOp57Zw/yuEFSVgAwWE6Wj+FvPlLTnkkjQWX/uoonEg1BIkW3Gup7W0WQ7gD270LI2qdYcKt87rpv4SKHQFQrkCsUIdfxA9LQ== X-MS-Exchange-CrossTenant-Network-Message-Id: ef50f96b-61c6-4221-eb70-08df5233751b X-MS-Exchange-CrossTenant-AuthSource: CH3PR11MB435924.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Dec 2026 18:21:47.2530 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pdkZHLHmn8lt7DJTGDgosNnAnbmzE/msY1jKAiNLEx2vgfDSIgNg0l2JV5Wy0HoUKxRsF8ggCM3TpBB2kPRfhg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR11MB8345 X-OriginatorOrg: intel.com Add a new VM sub-ioctl, KVM_TDX_GET_QUOTE, so userspace can generate a DICE-based Quote through the TDX-Module if it supports quoting. To convert the guest's local report into a remotely verifiable Quote, the host currently forwards the guest's GetQuote request to an SGX Quoting Enclave in userspace. Now the host can also request a DICE-based Quote from the TDX-Module. These Quotes leverage Intel's security hardware, such as the S3M, so they work without SGX. The GetQuote flow remains unchanged, so existing TDX setups can continue to work. The TDX-Module needs its internal TD states to generate a Quote, so the quoting SEAMCALL (TDH.QUOTE.GET) takes the TD's TDR page as input. Make KVM_TDX_GET_QUOTE a TDX VM sub-ioctl accordingly. Only allow quoting after the TD is finalized, so it doesn't bump into the TD build ioctls. These ioctls use SEAMCALLs that take an exclusive lock on the TD, so the quoting SEAMCALL can't take a shared lock at the same time. Doing so could fail a build-time SEAMCALL and bug the VM. Use a plain read for the TD state without tdx_vm_state_guard. The read could race with the following: - KVM_TDX_INIT_VM: sets the state to initialized, so a racy read would never observe a finalized state. Updating the state only changes its lowest byte, so this is true even with a torn access. - KVM_TDX_FINALIZE_VM: the only place that sets the state to finalized, which is the stable state of a running TD. Being a VM ioctl like KVM_TDX_FINALIZE_VM, KVM_TDX_GET_QUOTE's observation of the TD state is governed by the VFS layer. Dropping and grabbing a reference to the VM file have release and acquire semantics. This guarantees that if userspace calls KVM_TDX_GET_QUOTE after KVM_TDX_FINALIZE_VM returns, it must observe the state change fully. A racy userspace implementation would at worst fail with -EINVAL, which just means "not ready yet". Add a mutex to allow one Quote request per TD at a time. This lets the checks at TDX init guarantee that quoting sessions never run out when there is no oversubscription of TDs. Each request has a unique "session ID", so create a session ID allocator to support concurrent requests. To keep the uABI predictable, always use the number of logical CPUs as the session limit, since userspace has no way of knowing how many sessions the TDX-Module actually supports. If userspace decides to oversubscribe TDs, it must be prepared to handle -EBUSY, as KVM could run out of quoting sessions in this case. The TDX-Module takes quoting data blobs in pages. Rather than having to support unaligned data in the uABI in perpetuity, require both the report and the Quote blobs to be page-aligned from day one. This gives KVM the freedom to change its implementation underneath later (e.g. pin the report with GUP) without breaking userspace. Pin the Quote buffer with GUP. DICE-based Quotes can reach several megabytes in size [1], so using bounce buffers could at worst mean gigabytes of kernel memory to shuffle data around on a large system. Signed-off-by: Peter Fang Link: https://lore.kernel.org/all/20260211001712.1531955-1-sathyanarayanan.kuppuswamy@linux.intel.com/ # [1] --- v3: - Redesign the Quote generation path. Keep the GetQuote flow unchanged, and let userspace request a Quote with the new KVM_TDX_GET_QUOTE sub-ioctl. [Sean, Rick] - Allow one Quote request per TD at a time, and support concurrent requests across TDs. [Sean] --- arch/x86/include/uapi/asm/kvm.h | 12 +++ arch/x86/kvm/vmx/tdx.c | 152 ++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.h | 6 ++ 3 files changed, 170 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 00ce265391e11..9326f48c0d377 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -980,6 +980,7 @@ enum kvm_tdx_cmd_id { KVM_TDX_INIT_MEM_REGION, KVM_TDX_FINALIZE_VM, KVM_TDX_GET_CPUID, + KVM_TDX_GET_QUOTE, KVM_TDX_CMD_NR_MAX, }; @@ -1017,6 +1018,17 @@ struct kvm_tdx_capabilities { struct kvm_cpuid2 cpuid; }; +struct kvm_tdx_get_quote { + __u64 report_uaddr; + __u64 report_size; + + __u64 quote_buf_uaddr; + __u64 quote_buf_size; + __u64 quote_size; + + __u64 reserved[27]; +}; + struct kvm_tdx_init_vm { __u64 attributes; __u64 xfam; diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 4ccb41f002dff..704a267e9f8be 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -58,6 +58,8 @@ module_param_named(tdx, enable_tdx, bool, 0444); static const struct tdx_sys_info *tdx_sysinfo; +static DEFINE_IDA(tdx_quoting_ida); + void tdh_vp_rd_failed(struct vcpu_tdx *tdx, char *uclass, u32 field, u64 err) { KVM_BUG_ON(1, tdx->vcpu.kvm); @@ -658,6 +660,8 @@ int tdx_vm_init(struct kvm *kvm) */ kvm->max_vcpus = min_t(int, kvm->max_vcpus, num_present_cpus()); + mutex_init(&kvm_tdx->quote_lock); + kvm_tdx->state = TD_STATE_UNINITIALIZED; return 0; @@ -2363,6 +2367,146 @@ static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) return ret; } +/* + * No need to take tdx_vm_state_guard here. Quoting takes TD locks as shared, so + * it doesn't run up against any other runtime SEAMCALLs. + */ +static int tdx_td_get_quote(struct kvm *kvm, struct kvm_tdx_cmd *cmd) +{ + struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); + struct kvm_tdx_get_quote __user *ugq; + int num_quote_pages, ret, session_id; + struct kvm_tdx_get_quote gq; + struct page **quote_pages; + u16 sessions, threads; + u32 max_quote_size; + u64 quote_size; + void *report; + + if (cmd->flags) + return -EINVAL; + + /* + * TDH.QUOTE.GET needs the TD locks, so avoid colliding with + * build-time SEAMCALLs that lock the TD exclusively. + * + * The VFS syscall path guarantees that any VM ioctl issued after + * KVM_TDX_FINALIZE_VM succeeds observes the state change fully. It also + * ensures that tdx_vm_destroy() can't run and reset the state while + * this call is in progress. + */ + if (kvm_tdx->state != TD_STATE_RUNNABLE) + return -EINVAL; + + /* Grab the lock early to throttle the TD before pinning memory */ + if (mutex_lock_killable(&kvm_tdx->quote_lock)) + return -EINTR; + + if (!tdx_quoting_enabled(&max_quote_size, &sessions, &threads)) { + ret = -EINVAL; + goto out_unlock; + } + + ugq = u64_to_user_ptr(cmd->data); + + if (copy_from_user(&gq, ugq, sizeof(gq))) { + ret = -EFAULT; + goto out_unlock; + } + + if (memchr_inv(gq.reserved, 0, sizeof(gq.reserved))) { + ret = -EINVAL; + goto out_unlock; + } + + /* The TDX-Module takes the report in a single 4K page */ + if (!PAGE_ALIGNED(gq.report_uaddr) || gq.report_size > PAGE_SIZE) { + ret = -EINVAL; + goto out_unlock; + } + + report = kmalloc(PAGE_SIZE, GFP_KERNEL); + if (!report) { + ret = -ENOMEM; + goto out_unlock; + } + + if (copy_from_user(report, u64_to_user_ptr(gq.report_uaddr), + gq.report_size)) { + ret = -EFAULT; + goto out_free_report; + } + + /* Avoid pinning an unreasonable number of pages */ + gq.quote_buf_size = umin(gq.quote_buf_size, max_quote_size); + + if (!PAGE_ALIGNED(gq.quote_buf_uaddr) || !gq.quote_buf_size) { + ret = -EINVAL; + goto out_free_report; + } + + num_quote_pages = PFN_UP(gq.quote_buf_size); + + quote_pages = kmalloc_objs(*quote_pages, num_quote_pages); + if (!quote_pages) { + ret = -ENOMEM; + goto out_free_report; + } + + ret = pin_user_pages_fast(gq.quote_buf_uaddr, num_quote_pages, + FOLL_WRITE, quote_pages); + if (ret != num_quote_pages) { + if (ret > 0) + unpin_user_pages(quote_pages, ret); + ret = -ENOMEM; + goto out_free_quote; + } + + /* + * There is a 1:1 mapping between a quoting session in use and a + * quoting thread in the TDX-Module. Since KVM guarantees there are at + * least as many threads as logical CPUs, limit the number of sessions + * to the same number to ensure there is always a free thread. Using + * the module's own limit would make the ABI unpredictable. + * + * In a typical setup where TDs aren't oversubscribed, there is always a + * free session since each TD runs at most one session at a time. + * Otherwise, let userspace handle -EBUSY when sessions run out. + */ + session_id = ida_alloc_range(&tdx_quoting_ida, 0, + num_present_cpus() - 1, GFP_KERNEL); + if (session_id == -ENOSPC) + session_id = -EBUSY; + if (session_id < 0) { + ret = session_id; + goto out_unpin_quote; + } + + ret = tdx_quote_generate(&kvm_tdx->td, session_id, virt_to_page(report), + gq.report_size, quote_pages, gq.quote_buf_size, + "e_size, &cmd->hw_error); + + ida_free(&tdx_quoting_ida, session_id); + +out_unpin_quote: + /* + * The TDX-Module bypasses the kernel's page tables when writing the + * quote. Mark the pages dirty so reclaim doesn't drop them silently. + */ + unpin_user_pages_dirty_lock(quote_pages, num_quote_pages, true); +out_free_quote: + kfree(quote_pages); +out_free_report: + kfree(report); +out_unlock: + mutex_unlock(&kvm_tdx->quote_lock); + + if (ret) + return ret; + + return put_user(quote_size, &ugq->quote_size); +} + /* * KVM reports guest physical address in CPUID.0x800000008.EAX[23:16], which is * similar to TDX's GPAW. Use this field as the interface for userspace to @@ -2982,6 +3126,14 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) if (tdx_cmd.id == KVM_TDX_CAPABILITIES) return tdx_get_capabilities(&tdx_cmd); + if (tdx_cmd.id == KVM_TDX_GET_QUOTE) { + r = tdx_td_get_quote(kvm, &tdx_cmd); + if (copy_to_user(argp, &tdx_cmd, sizeof(struct kvm_tdx_cmd))) + return -EFAULT; + + return r; + } + CLASS(tdx_vm_state_guard, guard)(kvm); if (IS_ERR(guard)) return PTR_ERR(guard); diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index fd368e3ee060b..f67d81f1aab73 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -47,6 +47,12 @@ struct kvm_tdx { * Set/unset is protected with kvm->mmu_lock. */ bool wait_for_sept_zap; + + /* + * Allow one quoting session per TD. This prevents a TD from hogging + * the quoting resources. + */ + struct mutex quote_lock; }; /* TDX module vCPU states */ -- 2.53.0