From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91FB2C4332F for ; Fri, 25 Nov 2022 09:28:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0670E6B0071; Fri, 25 Nov 2022 04:28:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 016BF6B0072; Fri, 25 Nov 2022 04:28:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E21D86B0073; Fri, 25 Nov 2022 04:28:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D3C476B0071 for ; Fri, 25 Nov 2022 04:28:23 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8E40640C84 for ; Fri, 25 Nov 2022 09:28:23 +0000 (UTC) X-FDA: 80171438886.15.238D558 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf28.hostedemail.com (Postfix) with ESMTP id DD4E6C0009 for ; Fri, 25 Nov 2022 09:28:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669368502; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=onEr/5J0flqsYvcgarvEHk+r7IyB64r8DXyLrUW/Ar0=; b=DlcBqd3gsbPs2+KEHcc37t2ezfp7xaTMpyPUsHeHAtBBPFuFveyoF86NmKSm7P8g29KQmN l2eSsFA8K61Z9waZzk6/hH/jfyIFtSLyX8NYMhdH/x3DRbe6hPEPGY3E3uux1VNTC0Samr 60rtOpBTAqKXsuEOHSNbBJdQL9QzZq0= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-74-JLH5naWnPimlbZvkHVLMew-1; Fri, 25 Nov 2022 04:28:18 -0500 X-MC-Unique: JLH5naWnPimlbZvkHVLMew-1 Received: by mail-wm1-f72.google.com with SMTP id bg25-20020a05600c3c9900b003cf3ed7e27bso2163504wmb.4 for ; Fri, 25 Nov 2022 01:28:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=onEr/5J0flqsYvcgarvEHk+r7IyB64r8DXyLrUW/Ar0=; b=LhWPggD14DB8SCOMr6jH8jMV2ux9Shfo9P1+lPv7lSrHbhu7zI7er2LbI4Gi6qt0mb RycrAkDZ8pmhfM6Vc6fT86aqfzYr8iBeCGN5bDfqQJjnzieb5NvjoCJ6meeEklvwLJZo f7/i0D5fPIq3pvvPvRcM4tjELNu9BwaqKuDptTkYO3wlqcB22NS86hW/1529+wTxK+dK sdxBOBr1siQolr1NPQWGW6vy78bDDI41l1kOlCYICuFhSaVKnZPUh+DROSrO0z5wEoeV UiZP3x7f15d51YhXtHQc12pf643g5yapwXe1oW4p5Mnqp9YPTc2i2L/aN7a9HIlgLKkH nl6A== X-Gm-Message-State: ANoB5pnQAXmDiQ/py2X4mj2p77LGrwUf+9aqpBxu3DN2/YUG4x3qfxYr cMQ2M/vjt/Bc3B/S+bFbJXXXJkUhYtVnpvrcVSQknAUsv7NqcZuoTp1TuYC+taHkAZ2WLgEsrW/ QQ+znphCe/2c= X-Received: by 2002:adf:f3d0:0:b0:241:f721:3ba2 with SMTP id g16-20020adff3d0000000b00241f7213ba2mr5788514wrp.681.1669368497459; Fri, 25 Nov 2022 01:28:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf6iXy13FMYkhy+fD3Q7XTLxdC2exbVu/sGMn5iymyk0EKRg1862HBW99ZvN/TTdGcZxZaAxKw== X-Received: by 2002:adf:f3d0:0:b0:241:f721:3ba2 with SMTP id g16-20020adff3d0000000b00241f7213ba2mr5788489wrp.681.1669368497034; Fri, 25 Nov 2022 01:28:17 -0800 (PST) Received: from ?IPV6:2003:cb:c704:d800:887:cbec:f31f:a08? (p200300cbc704d8000887cbecf31f0a08.dip0.t-ipconnect.de. [2003:cb:c704:d800:887:cbec:f31f:a08]) by smtp.gmail.com with ESMTPSA id f13-20020a05600c4e8d00b003c6c182bef9sm11363749wmq.36.2022.11.25.01.28.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 25 Nov 2022 01:28:16 -0800 (PST) Message-ID: <361875cb-e4b3-a46f-b275-6d87a98ce966@redhat.com> Date: Fri, 25 Nov 2022 10:28:15 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 Subject: Re: [PATCH v7 10/20] x86/virt/tdx: Use all system memory when initializing TDX module as TDX memory To: "Huang, Kai" , "Williams, Dan J" , "kvm@vger.kernel.org" , "linux-kernel@vger.kernel.org" Cc: "Hansen, Dave" , "Luck, Tony" , "bagasdotme@gmail.com" , "ak@linux.intel.com" , "Wysocki, Rafael J" , "kirill.shutemov@linux.intel.com" , "Christopherson,, Sean" , "Chatre, Reinette" , "pbonzini@redhat.com" , "linux-mm@kvack.org" , "Yamahata, Isaku" , "peterz@infradead.org" , "Shahar, Sagi" , "imammedo@redhat.com" , "Gao, Chao" , "Brown, Len" , "sathyanarayanan.kuppuswamy@linux.intel.com" , "Huang, Ying" References: <9b545148275b14a8c7edef1157f8ec44dc8116ee.1668988357.git.kai.huang@intel.com> <637ecded7b0f9_160eb329418@dwillia2-xfh.jf.intel.com.notmuch> <8e8f72ad5d7a3d09be32bee54e4ebb9c280610a2.camel@intel.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <8e8f72ad5d7a3d09be32bee54e4ebb9c280610a2.camel@intel.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669368503; a=rsa-sha256; cv=none; b=SeokZjNMd6har7j+bRJVTX4xYyW8sk/6K8HOys5ICX+tYgjgE5qgbM32UkC/NeogrQUmPu ldSlj0n6U8bzY4QBBeBeyPgtZIDXuyb/CcEtFAT43jTTzALM9K4yLGm8dDhjj9PuAiaiGc 8D4gwumVVYVJrq2IXiJxmgOvd3RM4Xk= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DlcBqd3g; spf=pass (imf28.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669368503; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=onEr/5J0flqsYvcgarvEHk+r7IyB64r8DXyLrUW/Ar0=; b=escLIM5H603Mla49H9QyT7r2Q+mzuD904fGM/tHslS/jebvvKEkJfC/8Cfp+TmEBaFTNHq 8NFEZ3ByODBqGnutHCDV0rrRhGGQM+z9va+ojinFicu2ruypO26lWZ7KXrJPC42mdF4Kzk 4zzQUn230+MUFjCdXeGh7+pYhsw4xTU= X-Rspam-User: Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=DlcBqd3g; spf=pass (imf28.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: gew3mu1bs8pbfmrcnaffrzjmzhw66dcs X-Rspamd-Queue-Id: DD4E6C0009 X-Rspamd-Server: rspam12 X-HE-Tag: 1669368502-69265 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 24.11.22 10:06, Huang, Kai wrote: > On Wed, 2022-11-23 at 17:50 -0800, Dan Williams wrote: >>> >>> @@ -968,6 +969,15 @@ int arch_add_memory(int nid, u64 start, u64 size, >>>    unsigned long start_pfn = start >> PAGE_SHIFT; >>>    unsigned long nr_pages = size >> PAGE_SHIFT; >>> >>> + /* >>> + * For now if TDX is enabled, all pages in the page allocator >>> + * must be TDX memory, which is a fixed set of memory regions >>> + * that are passed to the TDX module.  Reject the new region >>> + * if it is not TDX memory to guarantee above is true. >>> + */ >>> + if (!tdx_cc_memory_compatible(start_pfn, start_pfn + nr_pages)) >>> + return -EINVAL; >> >> arch_add_memory() does not add memory to the page allocator.  For >> example, memremap_pages() uses arch_add_memory() and explicitly does not >> release the memory to the page allocator. > > Indeed. Sorry I missed this. > >> This check belongs in >> add_memory_resource() to prevent new memory that violates TDX from being >> onlined. > > This would require adding another 'arch_cc_memory_compatible()' to the common > add_memory_resource() (I actually long time ago had such patch to work with the > memremap_pages() you mentioned above). > > How about adding a memory_notifier to the TDX code, and reject online of TDX > incompatible memory (something like below)? The benefit is this is TDX code > self contained and won't pollute the common mm code: > > +static int tdx_memory_notifier(struct notifier_block *nb, > + unsigned long action, void *v) > +{ > + struct memory_notify *mn = v; > + > + if (action != MEM_GOING_ONLINE) > + return NOTIFY_OK; > + > + /* > + * Not all memory is compatible with TDX. Reject > + * online of any incompatible memory. > + */ > + return tdx_cc_memory_compatible(mn->start_pfn, > + mn->start_pfn + mn->nr_pages) ? NOTIFY_OK : NOTIFY_BAD; > +} > + > +static struct notifier_block tdx_memory_nb = { > + .notifier_call = tdx_memory_notifier, > +}; With mhp_memmap_on_memory() some memory might already be touched during add_memory() (because part of the hotplug memory is used for holding the memmap), not when actually onlining memory. So in that case, this would be too late. add_memory_resource() sounds better, even though I disgust such TDX special handling in common code. -- Thanks, David / dhildenb