Received: by 2002:a05:7412:da14:b0:e2:908c:2ebd with SMTP id fe20csp15752rdb; Thu, 5 Oct 2023 15:01:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDmAyMHbMZVEYQtaSDsg6Wr+xK9ae943ymaqQfNvQ1e/icaXVfHZmBSogCKCN0rvHfGmf8 X-Received: by 2002:a05:6a00:198f:b0:693:43b5:aaf3 with SMTP id d15-20020a056a00198f00b0069343b5aaf3mr5738639pfl.13.1696543310124; Thu, 05 Oct 2023 15:01:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1696543310; cv=pass; d=google.com; s=arc-20160816; b=gyd2gmhShcghQLaEUqNNqgH+TTkEZyhhgmI4esrAIq+th3P6O13H7Y30vOzmudCUvv ZYhQJzhLI1QqDxZwxypPSNL/tmXhyYj0LA8t5FYwFoV198pzEFk6elxDcFsW9ny7pRji pNLWWrHoO70Tk8wJo8tAQDx5Lq/np1Z5o/tWOwPPRq1VYexI9EXMgwI2qwtmHpiuRkFh W0PYpmLR3eFuGynYMVNsnavJ/nFjwsv9sl1CEZ7jmVpbPturreyrWavqDu7O+Zc+0OLS IvxSFs97cm17sXnliu1EJTLxFQQTxzTFRALIQ62A9lNZlRUhsrbtH8ts118FJxIKeuKO uWRw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :in-reply-to:from:references:cc:to:content-language:subject :user-agent:date:message-id:dkim-signature; bh=+YOUjXjhgnt3AKsdzIZrNF42fieVxjJsDSM68u1QEbU=; fh=5/y0JURB5VBvvbIutAzaL0W6RJQnajjS/E3ui5V8jig=; b=A7ozRRhFLvUWqsK0ZOGKJiXPfvyhW5CHUBONIRtYKAi3O57jf9J+Xi7wGtaNvXLgGy TtnWZecUJG01KTn3SnTWDUpVQz6lg4TL9f15Q5wjQvpsC78Gzaai9NgQxdwhttmNSIm5 r5ekjrB8yCJKkZQktnm+WCM/yBlaZI/1MDCfr9vnzjHuQd8jNOw7zuIifMCmm1gRi8Nu KtECspK0XYMd8PjC3GkbBEwf/rGHmVZoSXZpJjr8wUVMQbYhs8kjfVULLi6okoEeTWiG idqs0K/ptEHAePmOtOW0ovZiJgznlFSvH7WE2FQcDMyMUmEsSuAvggI3rkyBcQI3xJ2A sxyA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=vs1ueKbH; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id dc4-20020a056a0035c400b0068a6f6d9f85si122959pfb.140.2023.10.05.15.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 15:01:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=vs1ueKbH; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id C1D6F801B8A2; Thu, 5 Oct 2023 15:01:44 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230283AbjJEWBc (ORCPT + 99 others); Thu, 5 Oct 2023 18:01:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229537AbjJEWB3 (ORCPT ); Thu, 5 Oct 2023 18:01:29 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2050.outbound.protection.outlook.com [40.107.243.50]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4951F95 for ; Thu, 5 Oct 2023 15:01:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ILlZvg/Ez4MH9Q3v5dbh80HANBMBgNiyhieUzFU48eaoCyQYYmX9q0BFkILUXkKfGk2FhegW8UMDaaKuGlmfltlOVeeuGKPl4yg68aFjgC7XsycZW+kDmwPhULpFgmtXqYs7KEEFOJ7hE5ZfV8wcEgoVH79Z7vtEnTtxrfLAjG/wbuxaJCAoON33eGCRUsM+S7/oR7LauCUjQnnTFStJPOS6NV60OnuyoCtBJJvP0Y6wF78t/vMYTHys3qcEhG0cVc0fpR700PmtyUnjyCJfxZ8nVnlNH5AuOAPE1FjEKS2EMWMpqA1tmD5s7cboU0VYENSNML9wv9z46or4ZpzZfQ== 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=+YOUjXjhgnt3AKsdzIZrNF42fieVxjJsDSM68u1QEbU=; b=XDVOhcUnpEc7EaA30TJg1m0/Wij7La4EscgODkV/lp3GSU7FS8NpKzIyFu9tuDGhkWglJXkYuJHE4cBxljllW1bWqDDnnzVe0UP1EWi9nnSN9+wAavwZ1sIr7uQSHxoyqUk1fX3jmsa4jTmzCzzvLqHVytZHWHuBi4kL8cNYhD+83z2UBl193G/pn4ryIxgmxkfkijZ84aCZHiHq5e5YR5N04pygueL4l1QvKhL0O8z8Y2GIinGGOJO17kcNbAlmUj/p0zJskNxoRmcod+W7G+XONKshHix8dlGiP1mqhA8Rxw9/oIATbdVe1dQMlJm8kHE+j/9Kqipn0B6Dz44YTA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=+YOUjXjhgnt3AKsdzIZrNF42fieVxjJsDSM68u1QEbU=; b=vs1ueKbHnZ56I46+WefqLm5AbZDIKB+TFkqWSTu2qASewlaECi/aglNXfE/XSSYNafK32mHaGb1uRtIVrh8SrFONXn3bQz9SOS0Lz6y3wWpxiTzl4bp13DEfO4lq46EYRwc6Q6LBGaASNa0GvpRkaDTM2eEemQG3gmc8Kzyy7V0= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) by SJ2PR12MB8829.namprd12.prod.outlook.com (2603:10b6:a03:4d0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.35; Thu, 5 Oct 2023 22:01:25 +0000 Received: from SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::6ac8:eb30:6faf:32b2]) by SN6PR12MB2767.namprd12.prod.outlook.com ([fe80::6ac8:eb30:6faf:32b2%5]) with mapi id 15.20.6838.033; Thu, 5 Oct 2023 22:01:25 +0000 Message-ID: Date: Thu, 5 Oct 2023 17:01:23 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.2.1 Subject: Re: [PATCH 10/13] x86/tdx: Convert shared memory back to private on kexec Content-Language: en-US To: "Kirill A. Shutemov" Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "Rafael J. Wysocki" , Peter Zijlstra , Adrian Hunter , Kuppuswamy Sathyanarayanan , Elena Reshetova , Jun Nakajima , Rick Edgecombe , Tom Lendacky , kexec@lists.infradead.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org References: <20231005131402.14611-1-kirill.shutemov@linux.intel.com> <20231005131402.14611-11-kirill.shutemov@linux.intel.com> <8d0e4e71-0614-618a-0f84-55eeb6d27a6d@amd.com> <20231005212828.veeekxqc7rwvrbig@box> From: "Kalra, Ashish" In-Reply-To: <20231005212828.veeekxqc7rwvrbig@box> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-ClientProxiedBy: SA9PR13CA0013.namprd13.prod.outlook.com (2603:10b6:806:21::18) To SN6PR12MB2767.namprd12.prod.outlook.com (2603:10b6:805:75::23) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN6PR12MB2767:EE_|SJ2PR12MB8829:EE_ X-MS-Office365-Filtering-Correlation-Id: 92821f89-4c19-40ce-bb58-08dbc5ee9e80 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: +A2y9vwl9TJ4qegqaWrbd+TzFAvigoh5M7Y8coL2tKBlXM0FVBnbSMxMHrolnjyVGpAkWOcObDP3FWCDbe0wvPFQLHAPXJ8QsnQCKUJfZYq9xYwBHLd0dOpqQZlHdZYUt/yERBAHb05mXx3BMQMkeVdINog0NSp2IDMOm2WxUYJVU2AXY+sVO2f0K5ID7qbAoZzc65vJ9058ITP/ZrUNcIuHigrf/isAQ7HfctHKUFXhQPUR56Qc/ab6BN2aNl66Bl9shJ9PFi5DcHvPgyagDc/1WAWAaF3AvHE6i2An3AsSHoFrMgnDIXMchDFthnCaN9cHYQsAeiSZvItbzZNwK6O/KX4l56RtaqrljYCpk4JtStXAVB+cAzX1poowN5x+DYNNNTMv9v91h5Fh39o/ALZmbxfIqySO0lCO7Y8KMQntAi4hSKo4NaV2auQMZzKpkjSC/CrkLSfuq3ugkQhpmiOhh9jYJ+gnUPWgdPH06GtW6sbZ+PGW+Ji5rKrjW2VIMhS1Toh2mc+ShJcuZ+IqrXiIBRvoAqfbR4ANw1wxqQ0h1LEScssBo8D7Qs8P2B3IWqFXg+GEOFevMkTl76DgdaPqnvidoWar6HOrxVhvGmbNxvWfov/LYQpF3ZACGHnDPJiwr/Ubu4u58fqo9J7IAA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2767.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(396003)(136003)(346002)(366004)(39860400002)(376002)(230922051799003)(64100799003)(451199024)(1800799009)(186009)(31686004)(6506007)(6486002)(53546011)(478600001)(2616005)(86362001)(38100700002)(36756003)(31696002)(7416002)(41300700001)(2906002)(83380400001)(6512007)(26005)(54906003)(8936002)(66556008)(66476007)(316002)(66946007)(6916009)(5660300002)(4326008)(8676002)(43740500002)(45980500001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?VlFNRGNjUHlxM1NNT1Q5bTcrQkR6a0FrczZxTm81d1NUUXhTV1lSSE55a3NP?= =?utf-8?B?bWNsSHF3bjdZSUJzU3JRR0RkcWt4VUJPVVEzejJLSGZFREw1c1hzZXJ1OGRO?= =?utf-8?B?NWpXZmhyVGlmdWw1Q2d5T3NHd1dBMkQ4aVBHVEpUNDZzd0J6UlNhSld2Nnkw?= =?utf-8?B?T2Yr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WRUNkVwWHdn?= =?utf-8?B?cnpuRVM1TWlHa3RRUm0rMUZ0OFptdVFwTDJMeVI0SnE5Q1o2RG9CQUNvalJv?= =?utf-8?B?OXV5QXRkaUdBZ2lyZFZVV0ZVQW5lRVBqQUhVYjhYZ0RPQkJVdjhCQnhEWU5O?= =?utf-8?Q?H9fw1HoyJskideTrgWHMDCCqj?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 92821f89-4c19-40ce-bb58-08dbc5ee9e80 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2767.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Oct 2023 22:01:25.6740 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Ckdn0OHKCqi/zj9bMypgw751F0jmNae2I5KsYqgclC0oxL0k3YdWyq/Uv+AgUky+YfZWzjBZhzz/CXd0/Pdnew== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8829 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Thu, 05 Oct 2023 15:01:44 -0700 (PDT) On 10/5/2023 4:28 PM, Kirill A. Shutemov wrote: > On Thu, Oct 05, 2023 at 01:41:38PM -0500, Kalra, Ashish wrote: >>> +static void unshare_all_memory(bool unmap) >>> +{ >>> + unsigned long addr, end; >>> + long found = 0, shared; >>> + >>> + /* >>> + * Walk direct mapping and convert all shared memory back to private, >>> + */ >>> + >>> + addr = PAGE_OFFSET; >>> + end = PAGE_OFFSET + get_max_mapped(); >>> + >>> + while (addr < end) { >>> + unsigned long size; >>> + unsigned int level; >>> + pte_t *pte; >>> + >>> + pte = lookup_address(addr, &level); >> >> IIRC, you were earlier walking the direct mapping using >> walk_page_range_novma(), any particular reason to use lookup_address() >> instead ? > > walk_page_range_novma() wants mmap lock to be taken, but it is tricky as > we run here from atomic context in case of crash. > > I considered using trylock to bypass the limitation, but it is a hack. > >> >>> + size = page_level_size(level); >>> + >>> + if (pte && pte_decrypted(*pte)) { >> >> Additionally need to add check for pte_none() here to handle physical memory >> holes in direct mapping. > > lookup_address() returns NULL for none entries. > Looking at lookup_address_in_pgd(), at pte level it is simply returning pte_offset_kernel() and there does not seem to be a check for returning NULL if pte_none() ? >>> + int pages = size / PAGE_SIZE; >>> + >>> + /* >>> + * Touching memory with shared bit set triggers implicit >>> + * conversion to shared. >>> + * >>> + * Make sure nobody touches the shared range from >>> + * now on. >>> + * >>> + * Bypass unmapping for crash scenario. Unmapping >>> + * requires sleepable context, but in crash case kernel >>> + * hits the code path with interrupts disabled. >> >> In case of SNP we will need to temporarily enable interrupts during this >> unsharing as we invoke set_memory_encrypted() which then hits a BUG_ON() in >> cpa_flush() if interrupts are disabled. > > Do you really need full set_memory_encrypted()? Can't you do something > ligher? > We need to modify the PTE for setting c-bit to 1 so that will require cpa_flush(), though probably can add something lighter to do clflush_cache_range() directly ? Thanks, Ashish