From patchwork Mon Nov 21 19:51:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dexuan Cui X-Patchwork-Id: 24004 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1798647wrr; Mon, 21 Nov 2022 11:54:28 -0800 (PST) X-Google-Smtp-Source: AA0mqf5NI7CQRRgwgst5HzzlN96wLGHxcBG7AbKDIRZMfKxxfe7e4pdDTZ6wF1+7/XHxoM6nf/Gt X-Received: by 2002:a63:ce58:0:b0:473:e2bb:7fc0 with SMTP id r24-20020a63ce58000000b00473e2bb7fc0mr648114pgi.604.1669060468226; Mon, 21 Nov 2022 11:54:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1669060468; cv=pass; d=google.com; s=arc-20160816; b=bk0S4zqizQ7LNdah6QokLRCD7DgNS60rw4YW/ikqZMY0+c6FnmMM/tGTcjKGniPo4v 5mvVWL90Fr8dP8fUCzp7Ce7ZNIJS0W6EL4GLqsPbB6XY6JlC7cb3QunRJx3nBuOcO+cw 5vte7yt/hHWb6XHpx9f59WNCVwbaPMg7U/QNxLYboUG0hIyEYL6wlATbJSMo0I7E8D/A /3DcKWsdzGJW7ou0enIOuwiWOlSCMYMCN8G65EYqSKY5UzvKQ7/Zdl5HVYPrGPjqE1Ag ZJZ/bnsa3txyVIg2GXLpRbYAsBAzsyCB1STaCpCjCvRh5aXTrxEpFPoPDGglTIVTzCmr OEsg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=pla7uQG4lVqvFfu1Gw/DyERK93jZZt64p0einsyczpQ=; b=pATm7lhQNNUYVeXH6MwvRDoL9M2t2Fqh2jiuzGXQm7wFOtH01zrDi8mvY7Mie4c7f3 IptJPULBL47xHSw/Uvd8Ym3hpJcu9ir6DDM04iKxklfw9349Id9GFHarg3ZpI8NTv7j1 siyBgHIFVnZvvG4Z+HGTr6gcUvzWyM0o4UCjHMnTg2KIA4dZ9TJqtTNWd2q0xQbXRjvg hrnoNd+XhUnoPszgNR0cO2qiAZ5+h5taTsBk515OdnEZp9LyjtOx/tM4USx+8P4QQMDL OK4GQGGXFlhdwf7d2k4EmgtPO/67ElpJAF874/mgZtlgt1LO7IBcn5UlTY9fuONs9NRr Ldxw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@microsoft.com header.s=selector2 header.b=cZM3eZpG; arc=pass (i=1 spf=pass spfdomain=microsoft.com dkim=pass dkdomain=microsoft.com dmarc=pass fromdomain=microsoft.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=microsoft.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w20-20020a63fb54000000b00461ffae7b37si12151436pgj.0.2022.11.21.11.54.15; Mon, 21 Nov 2022 11:54:28 -0800 (PST) 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=@microsoft.com header.s=selector2 header.b=cZM3eZpG; arc=pass (i=1 spf=pass spfdomain=microsoft.com dkim=pass dkdomain=microsoft.com dmarc=pass fromdomain=microsoft.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=microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231739AbiKUTxe (ORCPT + 99 others); Mon, 21 Nov 2022 14:53:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231748AbiKUTxE (ORCPT ); Mon, 21 Nov 2022 14:53:04 -0500 Received: from na01-obe.outbound.protection.outlook.com (mail-eastusazon11022019.outbound.protection.outlook.com [52.101.53.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40E1115A34; Mon, 21 Nov 2022 11:53:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XIs7HH7tVo4Wh8N/s2NcGz4RAwvP9T0wOp7mDvUkFa9ZJ/wpN2tMAIKgWR52repWq/SMBQM4W3ZL4ALzFIG3xe+CpyPPUYEEdf7LHqzFi2xw4JxLCIOOjr1XnVBYqdhwkIgbRkbq7EfYChT/Dlmjbr3KQ7hU13PPL6fr7MW8V67yRV1zofkWRGNJI6daWhZQVFEmPyfL+47hQWUfKk0ybBgNSaX4IOhIoMq5mqgnM+gaAiaUHdmWUJxzDoQPS4sTnsMXV1RZKggtV7G/dgsCQ8xWTdfwbXkDX7cZ++WtuULJAwqj/B8BJ1eJm7ZkuK08r4uwmxrz5/GRURTjduDWKw== 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=pla7uQG4lVqvFfu1Gw/DyERK93jZZt64p0einsyczpQ=; b=W/VNT3U0l4iwHDNyuxQtNeC5AI9EpVYxmi10XwXcolo6uZptuefeXS77VLrtaHh1mmqI8qeqzJ7cyCVYeAw2pj7oFT28o+rYdCjOCIouAGvPoPHfO9yeMdJH66cykcFrMeYcnv1Gee3PXJdJlzGwEJFuhha3k0l1HYLbkeRc2WcJRWesXifMPp1JPOv+U/r6gUy2esc52RYssyhfgQz5jlzsjJgXbwJThw1jfareih3K84A2XkeSA07vW7BCA+1AXfnaHHd2MG60935FrqF9Xa4V7UkvA/XzHqNswv9nJRQWUxW3k1LKCDtys49CJS/4xalQmoNsWicIOsNg2DbbSA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=microsoft.com; dmarc=pass action=none header.from=microsoft.com; dkim=pass header.d=microsoft.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pla7uQG4lVqvFfu1Gw/DyERK93jZZt64p0einsyczpQ=; b=cZM3eZpGOzTe2kRKWPGph5qWoHX14lnSYW1I9i2RAHwYMTe8YMuZHFrvlttYNefDNfDBYH5h7oGMmMTK2LCBTlcdbpkWYTOsI2WGVArKzj6Y839UxJ2iId+XiLubt+fK05iPHzlETDSHpI/rVss89yU0Xdp1A0eodTSdBUSqsOM= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=microsoft.com; Received: from BL0PR2101MB1092.namprd21.prod.outlook.com (2603:10b6:207:30::23) by BL1PR21MB3280.namprd21.prod.outlook.com (2603:10b6:208:398::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.2; Mon, 21 Nov 2022 19:52:55 +0000 Received: from BL0PR2101MB1092.namprd21.prod.outlook.com ([fe80::ae03:5b1e:755c:a020]) by BL0PR2101MB1092.namprd21.prod.outlook.com ([fe80::ae03:5b1e:755c:a020%7]) with mapi id 15.20.5880.001; Mon, 21 Nov 2022 19:52:55 +0000 From: Dexuan Cui To: ak@linux.intel.com, arnd@arndb.de, bp@alien8.de, brijesh.singh@amd.com, dan.j.williams@intel.com, dave.hansen@linux.intel.com, haiyangz@microsoft.com, hpa@zytor.com, jane.chu@oracle.com, kirill.shutemov@linux.intel.com, kys@microsoft.com, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, rostedt@goodmis.org, sathyanarayanan.kuppuswamy@linux.intel.com, seanjc@google.com, tglx@linutronix.de, tony.luck@intel.com, wei.liu@kernel.org, x86@kernel.org Cc: linux-kernel@vger.kernel.org, Dexuan Cui Subject: [PATCH 3/6] x86/tdx: Support vmalloc() for tdx_enc_status_changed() Date: Mon, 21 Nov 2022 11:51:48 -0800 Message-Id: <20221121195151.21812-4-decui@microsoft.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20221121195151.21812-1-decui@microsoft.com> References: <20221121195151.21812-1-decui@microsoft.com> X-ClientProxiedBy: BY3PR03CA0027.namprd03.prod.outlook.com (2603:10b6:a03:39a::32) To BL0PR2101MB1092.namprd21.prod.outlook.com (2603:10b6:207:30::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL0PR2101MB1092:EE_|BL1PR21MB3280:EE_ X-MS-Office365-Filtering-Correlation-Id: 1fb044d9-54f8-482c-945b-08dacbf9fb9e X-LD-Processed: 72f988bf-86f1-41af-91ab-2d7cd011db47,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: h1pet3+1/gFMuw/zXD93ksb5HxBwditLhOB2Q+IH9hHacNUWuMslAwfNupInf4jz2gIbUKZZotAC+YyX+zB3b9XkV3y0Nv2jfFaV0yTgeCK1OTry45KK1Z1vUIjZLHcdzistDmn8in3RupdyTw58zSV3ec+A5TYpatJCk0n3ALYMPjsANb92NCE6Vdq59aHrMhF9GaN8SQVDe0m3pimmFNCrMxWpUbBO1akcos4aKlXRYh5nQwoe+ofb8W5izawz8kIjZmf8hiITwGSTMzv61mnDTlxaly1ZmOQADXBf/XsgieOQ8yFk1Bm+FI85t9qG5aNAX0jGrLZlKuTJJoCIjdac+7L0QzWTqBziXLndmW0qTq54P3niwtw0ffiA/9Xs1aoeKAi2XQ/NNaAsYg1DxpDpIL8xNm1UFcTt75YjikQ7RcvByPNMbmw6EBoE0JquLkJUHuCZi2LLTFiO+p9T616LNjkMJ4kCPaUChjzHrVpjiOdCFjlaLeFyYU8agpgw91/P12Yjk2N2KWqwEl+vCGxFs7HoXcm+e4pd+kdeYkNwARf97q7pieFnhK5C9UThKfLNRD2PjDoa+/wjsKMi0gyAxaBf21RZgKM3+eBs/Wi0UPGDZj2uQWDe6WPr6UPBSm3XyzBi9JI5iHkOH9AixECreffj+FPhxG/w+jzP4tx2wRQheKx8S3ubge8yrxRmINxRPvHrG2sXvXkbEPRlihXI4Er+V57MM3F3kQf6dJU= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR2101MB1092.namprd21.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(396003)(136003)(376002)(39860400002)(451199015)(83380400001)(2906002)(7416002)(5660300002)(8936002)(2616005)(186003)(1076003)(36756003)(86362001)(41300700001)(82950400001)(38100700002)(921005)(82960400001)(6666004)(10290500003)(107886003)(6506007)(52116002)(6512007)(316002)(478600001)(66556008)(8676002)(66946007)(66476007)(6486002)(4326008);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: FAcyz1CYOlAs13EhkkK5N7mwIwerzs0HvcOaKv+gJlQrkIuL2c7IjwBEjPuKsTfBVVm+K8fxh/L109DkEOnBp6a+A88HtK9G4QffeoeGDz1lr6rBR8pbhPF3H9k+krtK5vRPIdAIaODVjKTWglOzibo9UybVDXgePcE7ZYGzCB/eymWiMFQTL6NkLw42jDTX8eGVBmMQTKKw5FI9CQu5f2gPYK3zeYjWokdC7BjgI3eUBRhLOz+pQjgu0Dw1+woIdc33dbvIh8Gxici0QW59GpuY/hy5tn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Qqz40FMR+430Z0ozaCL6jN/uvK0yx2c6ZcCNjVptQY01XVdcREcnV5HONnJ/QR0/+iZi05ANa8jawV+nCi6pofUC6GEt5I9G72bUcnvze4Vih9xVXlXb8XUGFPxrsP4BGaZCjGrR9sqoev6kQQd4h7nQm3xPHFum9mDIn6nm9pLayIEZRGLVzmsqlnLTZSJHqlozOTPVSnYxi4M0NokD76Q9nsfCF1YPxkxZ2rVG1EJGpO/EyfL6Bj9rEg8Tm8nORj9oC2HHx97+rUJdAHlhM4QO5k3xsZeU770OY/CS3irp4/OGNhdNB X-OriginatorOrg: microsoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1fb044d9-54f8-482c-945b-08dacbf9fb9e X-MS-Exchange-CrossTenant-AuthSource: BL0PR2101MB1092.namprd21.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Nov 2022 19:52:55.6834 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 72f988bf-86f1-41af-91ab-2d7cd011db47 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: ApMGIJ3c8vbrVpXTxtGY1kvBBw1XNSthBTR6PLAy4Ln1SpjYJEnv5EVz+9kvqGA1Cr9ZN8H1pCNyMWR9MXYWtw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR21MB3280 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 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750136749486411387?= X-GMAIL-MSGID: =?utf-8?q?1750136749486411387?= When a TDX guest runs on Hyper-V, the hv_netvsc driver's netvsc_init_buf() allocates buffers using vzalloc(), and needs to share the buffers with the host OS by calling set_memory_decrypted(), which is not working for vmalloc() yet. Add the support by handling the pages one by one. Signed-off-by: Dexuan Cui --- arch/x86/coco/tdx/tdx.c | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/arch/x86/coco/tdx/tdx.c b/arch/x86/coco/tdx/tdx.c index 46971cc7d006..8bccae962b6d 100644 --- a/arch/x86/coco/tdx/tdx.c +++ b/arch/x86/coco/tdx/tdx.c @@ -5,6 +5,7 @@ #define pr_fmt(fmt) "tdx: " fmt #include +#include #include #include #include @@ -754,7 +755,8 @@ static bool tdx_map_gpa(phys_addr_t start, phys_addr_t end, bool enc) * the VMM or private to the guest. The VMM is expected to change its mapping * of the page in response. */ -static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) +static bool tdx_enc_status_changed_for_contiguous_pages(unsigned long vaddr, + int numpages, bool enc) { phys_addr_t start = __pa(vaddr); phys_addr_t end = __pa(vaddr + numpages * PAGE_SIZE); @@ -798,6 +800,47 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) return true; } +static bool tdx_enc_status_changed_for_vmalloc(unsigned long vaddr, + int numpages, bool enc) +{ + void *start_va = (void *)vaddr; + void *end_va = start_va + numpages * PAGE_SIZE; + phys_addr_t pa; + + if (offset_in_page(vaddr) != 0) + return false; + + while (start_va < end_va) { + pa = slow_virt_to_phys(start_va); + if (!enc) + pa |= cc_mkdec(0); + + if (!tdx_map_gpa(pa, pa + PAGE_SIZE, enc)) + return false; + + /* + * private->shared conversion requires only MapGPA call. + * + * For shared->private conversion, accept the page using + * TDX_ACCEPT_PAGE TDX module call. + */ + if (enc && !try_accept_one(&pa, PAGE_SIZE, PG_LEVEL_4K)) + return false; + + start_va += PAGE_SIZE; + } + + return true; +} + +static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc) +{ + if (is_vmalloc_addr((void *)vaddr)) + return tdx_enc_status_changed_for_vmalloc(vaddr, numpages, enc); + + return tdx_enc_status_changed_for_contiguous_pages(vaddr, numpages, enc); +} + void __init tdx_early_init(void) { u64 cc_mask;