Skip to contentSkip to conversation

TDX host kernel support

PATCH Superseded Kai Huang 8 participants 156 messages Apr 6, 2022 – May 25, 2022 Page 2 of 2

Subsystems (4)

v3 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#153 lore raw
In reply to Dave Hansen
Quoted 53 lines · Dave Hansen
Quoted 17 lines · Dan Williams
Quoted 16 lines · Dave Hansen
Quoted 10 lines · Dan Williams
Quoted 5 lines

But, neither of those really help with, say, a device-DAX mapping of TDX-*IN*capable memory handed to KVM. The "new syscall" would just throw up its hands and leave users with the same result: TDX can't be used. The new sysfs ABI for NUMA nodes wouldn't clearly apply to device-DAX because they don't respect the NUMA policy ABI.

They do have "target_node" attributes to associate node specific metadata, and could certainly express target_node capabilities in its own ABI. Then it's just a matter of making pfn_to_nid() do the right thing so KVM kernel side can validate the capabilities of all inbound pfns.

Let's walk through how this would work with today's kernel on tomorrow's hardware, without KVM validating PFNs:

Quoted 6 lines
Quoted 3 lines

1. daxaddr mmap("/dev/dax1234")
2. kvmfd = open("/dev/kvm")
3. ioctl(KVM_SET_USER_MEMORY_REGION, { daxaddr };

At least for a file backed mapping the capability lookup could be done here, no need to wait for the fault.

For DAX mappings, sure. But, anything that's backed by page cache, you can't know until the RAM is allocated.

...

Quoted 9 lines
Quoted 4 lines

Those pledges are hard for anonymous memory though. To fulfill the pledge, we not only have to validate that the NUMA policy is compatible at KVM_SET_USER_MEMORY_REGION, we also need to decline changes to the policy that might undermine the pledge.

I think it's less that the kernel needs to enforce a pledge and more that an interface is needed to communicate the guest death reason. I.e. "here is the impossible thing you asked for, next time set this policy to avoid this problem".

IF this code is booted on a system where non-TDX-capable memory is discovered, do we:
1. Disable TDX, printk() some nasty message, then boot as normal or,
2a. Boot normally with TDX enabled
2b. Add enhanced error messages in case of TDH.MEM.PAGE.AUG/ADD failure
(the "SEAMCALLs" which are the last line of defense and will reject
the request to add non-TDX-capable memory to a guest). Or maybe
an even earlier message.

For #1, if TDX is on, we are quite sure it will work. But, it will probably throw up its hands on tomorrow's hardware. (This patch set).

Quoted 6 lines

For #2, TDX might break (guests get killed) at runtime on tomorrow's hardware, but it also might be just fine. Users might be able to work around things by, for instance, figuring out a NUMA policy which excludes TDX-incapable memory. (I think what Dan is looking for)

Is that a fair summary?

Yes, just the option for TDX and non-TDX to live alongside each other... although in the past I have argued to do option-1 and enforce it at the lowest level [1]. Like platform BIOS is responsible to disable CXL if CXL support for a given CPU security feature is missing. However, I think end users will want to have their confidential computing and capacity too. As long as that is not precluded to be added after the fact, option-1 can be a way forward until a concrete user for mixed mode shows up.

Is there something already like this today for people that, for example, attempt to use PCI BAR mappings as memory? Or does KVM simply allow for garbage-in garbage-out?

In the end the patches shouldn't talk about whether or not PMEM is supported on a platform or not, that's irrelevant. What matters is that misconfigurations can happen, should be rare to non-existent on current platforms, and if it becomes a problem the kernel can grow ABI to let userspace enumerate the conflicts.

[1]: https://lore.kernel.org/linux-cxl/CAPcyv4jMQbHYQssaDDDQFEbOR1v14VUnejcSwOP9VGUnZSsCKw@mail.gmail.com/

#154 lore raw
Quoted 3 lines · Dan Williams

Is there something already like this today for people that, for example, attempt to use PCI BAR mappings as memory? Or does KVM simply allow for garbage-in garbage-out?

I'm just guessing, but I _assume_ those garbage PCI BAR mappings are how KVM does device passthrough.

I know that some KVM users even use mem= to chop down the kernel-owned 'struct page'-backed memory, then have a kind of /dev/mem driver to let the memory get mapped back into userspace. KVM is happy to pass through those mappings.

author
#69 lore raw

Re: [PATCH v3 09/21] x86/virt/tdx: Get information about TDX module and convertible memory

In reply to Dave Hansen
Quoted 20 lines · Dave Hansen
Quoted 19 lines · Kai Huang
Quoted 17 lines · Dave Hansen
Quoted 9 lines · Kai Huang
Quoted 3 lines

Couldn't we get rid of that comment if you did something like:

	ret = tdx_get_sysinfo(&tdx_cmr_array, &tdx_sysinfo);

Yes will do.

Quoted 1 line

and preferably make the variables function-local.

'tdx_sysinfo' will be used by KVM too.

In other words, it's not a part of this series so I can't review whether this statement is correct or whether there's a better way to hand this information over to KVM.

This (minor) nugget influencing the design also isn't even commented or addressed in the changelog.

Quoted 8 lines
Quoted 4 lines

TDSYSINFO_STRUCT is 1024B and CMR array is 512B, so I don't think it should be in the stack. I can change to use dynamic allocation at the beginning and free it at the end of the function. KVM support patches can change it to static variable in the file.

2k of stack is big, but it isn't a deal breaker for something that's not nested anywhere and that's only called once in a pretty controlled setting and not in interrupt context. I wouldn't cry about it.

OK. I'll change to use function local variables for both of them.

Signature · 2 lines
Thanks,
-Kai
author
#101 lore raw

Re: [PATCH v3 13/21] x86/virt/tdx: Allocate and set up PAMTs for TDMRs

In reply to Dave Hansen
Quoted 14 lines · Dave Hansen
Quoted 7 lines · Kai Huang
Quoted 2 lines · Dave Hansen

This is also a good place to note the downsides of using alloc_contig_pages().

For instance:

The allocation may fail when memory usage is under pressure.

It's not really memory pressure, though. The larger the allocation, the more likely it is to fail. The more likely it is that the kernel can't free the memory or that if you need 1GB of contiguous memory that 999.996MB gets freed, but there is one stubborn page left.

Quoted 8 lines

alloc_contig_pages() can and will fail. The only mitigation which is guaranteed to avoid this is doing the allocation at boot. But, you're not doing that to avoid wasting memory on every TDX system that doesn't use TDX.

A *good* way (although not foolproof) is to launch a TDX VM early in boot before memory gets fragmented or consumed. You might even want to recommend this in the documentation.

"launch a TDX VM early in boot" I suppose you mean having some boot-time service which launches a TDX VM before we get the login interface. I'll put this in the documentation.

How about adding below in the changelog:

"
However using alloc_contig_pages() to allocate large physically contiguous memory at runtime may fail. The larger the allocation, the more likely it is to fail. Due to the fragmentation, the kernel may need to move pages out of the to-be-allocated contiguous memory range but it may fail to move even the last stubborn page. A good way (although not foolproof) is to launch a TD VM early in boot to get PAMTs allocated before memory gets fragmented or consumed. "

Quoted 19 lines
Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

+/*
+ * Locate the NUMA node containing the start of the given TDMR's first
+ * RAM entry. The given TDMR may also cover memory in other NUMA nodes.
+ */

Please add a sentence or two on the implications here of what this means when it happens. Also, the joining of e820 regions seems like it might span NUMA nodes. What prevents that code from just creating one large e820 area that leads to one large TDMR and horrible NUMA affinity for these structures?

Quoted 3 lines

How about adding:

When TDMR is created, it stops spanning at NUAM boundary.

I actually don't know what that means at all. I was thinking of something like this.

Quoted 8 lines

/*
* Pick a NUMA node on which to allocate this TDMR's metadata. *
* This is imprecise since TDMRs are 1GB aligned and NUMA nodes might
* not be. If the TDMR covers more than one node, just use the _first_
* one. This can lead to small areas of off-node metadata for some
* memory.
*/

Thanks.

Quoted 12 lines
Quoted 7 lines
Quoted 7 lines

arch/x86/virt/vmx/tdx/tdx.c static int create_tdmrs(struct tdmr_info **tdmr_array, int *tdmr_num) · line 1009

Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+static int tdmr_get_nid(struct tdmr_info *tdmr)
+{
+	u64 start, end;
+	int i;
+
+	/* Find the first RAM entry covered by the TDMR */

There's something else missing in here. Why not just do:

	return phys_to_target_node(TDMR_START(tdmr));
Quoted 7 lines

This would explain it:

/*
* The beginning of the TDMR might not point to RAM.
* Find its first RAM address which which its node can
* be found.
*/

Will use this. Thanks.

Quoted 10 lines
Quoted 10 lines
Quoted 6 lines

arch/x86/virt/vmx/tdx/tdx.c static int create_tdmrs(struct tdmr_info **tdmr_array, int *tdmr_num) · line 1012

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +3 -0
+	e820_for_each_mem(i, start, end)
+		if (end > TDMR_START(tdmr))
+			break;

Brackets around the big loop, please.

OK.

Quoted 1 line
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/virt/vmx/tdx/tdx.c static int create_tdmrs(struct tdmr_info **tdmr_array, int *tdmr_num) · line 1019

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+	/*
+	 * One TDMR must cover at least one (or partial) RAM entry,
+	 * otherwise it is kernel bug.  WARN_ON() in this case.
+	 */
+	if (WARN_ON_ONCE((start >= end) || start >= TDMR_END(tdmr)))
+		return 0;

This really means "no RAM found for this TDMR", right? Can we say that, please.

OK will add it. How about:

	/*
	 * No RAM found for this TDMR.  WARN() in this case, as it
	 * cannot happen otherwise it is a kernel bug.
	 */
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

arch/x86/virt/vmx/tdx/tdx.c static int create_tdmrs(struct tdmr_info **tdmr_array, int *tdmr_num) · line 1044

Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +5 -0
+	/*
+	 * Allocate one chunk of physically contiguous memory for all
+	 * PAMTs.  This helps minimize the PAMT's use of reserved areas
+	 * in overlapped TDMRs.
+	 */

Ahh, this explains it. Considering that tdmr_get_pamt_sz() is really just two lines of code, I'd probably just the helper and open-code it here. Then you only have one place to comment on it.

Quoted 8 lines
Quoted 4 lines

It has a loop and internally calls __tdmr_get_pamt_sz(). It looks doesn't fit if we open-code it here.

How about move this comment to tdmr_get_pamt_sz()?

I thought about that. But tdmr_get_pamt_sz() isn't itself doing any allocation so it doesn't make a whole lot of logical sense. This is a place where a helper _can_ be removed. Remove it, please.

OK. Will remove the helper. Thanks.

Signature · 2 lines
Thanks,
-Kai
author
#155 lore raw
In reply to Dave Hansen
Quoted 32 lines · Dave Hansen
Quoted 10 lines · Dan Williams
Quoted 5 lines

But, neither of those really help with, say, a device-DAX mapping of TDX-*IN*capable memory handed to KVM. The "new syscall" would just throw up its hands and leave users with the same result: TDX can't be used. The new sysfs ABI for NUMA nodes wouldn't clearly apply to device-DAX because they don't respect the NUMA policy ABI.

They do have "target_node" attributes to associate node specific metadata, and could certainly express target_node capabilities in its own ABI. Then it's just a matter of making pfn_to_nid() do the right thing so KVM kernel side can validate the capabilities of all inbound pfns.

Let's walk through how this would work with today's kernel on tomorrow's hardware, without KVM validating PFNs:

1. daxaddr mmap("/dev/dax1234")
2. kvmfd = open("/dev/kvm")
3. ioctl(KVM_SET_USER_MEMORY_REGION, { daxaddr };
4. guest starts running
5. guest touches 'daxaddr'
6. Page fault handler maps 'daxaddr'
7. KVM finds new 'daxaddr' PTE
8. TDX code tries to add physical address to Secure-EPT
9. TDX "SEAMCALL" fails because page is not convertible
10. Guest dies

All we can do to improve on that is call something that pledges to only map convertible memory at 'daxaddr'. We can't *actually* validate the physical addresses at mmap() time or even KVM_SET_USER_MEMORY_REGION-time because the memory might not have been allocated.

Quoted 4 lines

Those pledges are hard for anonymous memory though. To fulfill the pledge, we not only have to validate that the NUMA policy is compatible at KVM_SET_USER_MEMORY_REGION, we also need to decline changes to the policy that might undermine the pledge.

Hi Dave,

There's another series done by Chao "KVM: mm: fd-based approach for supporting KVM guest private memory" which essentially allows KVM to ask guest memory backend to allocate page w/o having to mmap() to userspace. Please see my reply below:

https://lore.kernel.org/lkml/[email protected]/T/#mf9bf10a63eaaf0968c46ab33bdaf06bd2cfdd948

My understanding is for TDX guest KVM will be enforced to use the new mechanism. So when TDX supports NVDIMM in the future, dax can be extended to support the new mechanism to support using it as TD guest backend.

Sean, Paolo, Isaku, Chao,

Please correct me if I am wrong?

Signature · 2 lines
Thanks,
-Kai
#102 lore raw

Re: [PATCH v3 13/21] x86/virt/tdx: Allocate and set up PAMTs for TDMRs

In reply to Kai Huang
Quoted 11 lines · Kai Huang

On Fri, 2022-04-29 at 07:20 -0700, Dave Hansen wrote:
How about adding below in the changelog:

"
However using alloc_contig_pages() to allocate large physically contiguous memory at runtime may fail. The larger the allocation, the more likely it is to fail. Due to the fragmentation, the kernel may need to move pages out of the to-be-allocated contiguous memory range but it may fail to move even the last stubborn page. A good way (although not foolproof) is to launch a TD VM early in boot to get PAMTs allocated before memory gets fragmented or consumed. "

Better, although it's getting a bit off topic for this changelog.

Just be short and sweet:

1. the allocation can fail
2. Launch a VM early to (badly) mitigate this
3. the only way to fix it is to add a boot option

Quoted 10 lines
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

arch/x86/virt/vmx/tdx/tdx.c static int create_tdmrs(struct tdmr_info **tdmr_array, int *tdmr_num) · line 1019

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+	/*
+	 * One TDMR must cover at least one (or partial) RAM entry,
+	 * otherwise it is kernel bug.  WARN_ON() in this case.
+	 */
+	if (WARN_ON_ONCE((start >= end) || start >= TDMR_END(tdmr)))
+		return 0;

This really means "no RAM found for this TDMR", right? Can we say that, please.

Quoted 6 lines

OK will add it. How about:

	/*
	 * No RAM found for this TDMR.  WARN() in this case, as it
	 * cannot happen otherwise it is a kernel bug.
	 */

The only useful information in that comment is the first sentence. The jibberish about WARN() is patently obvious from the next two lines of code.

*WHY* can't this happen? How might it have actually happened?

author
#103 lore raw

Re: [PATCH v3 13/21] x86/virt/tdx: Allocate and set up PAMTs for TDMRs

Quoted 13 lines · Dave Hansen
Quoted 11 lines · Kai Huang

On Fri, 2022-04-29 at 07:20 -0700, Dave Hansen wrote:
How about adding below in the changelog:

"
However using alloc_contig_pages() to allocate large physically contiguous memory at runtime may fail. The larger the allocation, the more likely it is to fail. Due to the fragmentation, the kernel may need to move pages out of the to-be-allocated contiguous memory range but it may fail to move even the last stubborn page. A good way (although not foolproof) is to launch a TD VM early in boot to get PAMTs allocated before memory gets fragmented or consumed. "

Quoted 8 lines

Better, although it's getting a bit off topic for this changelog.

Just be short and sweet:

1. the allocation can fail
2. Launch a VM early to (badly) mitigate this
3. the only way to fix it is to add a boot option

OK. Thanks.

Quoted 18 lines
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines

arch/x86/virt/vmx/tdx/tdx.c static int create_tdmrs(struct tdmr_info **tdmr_array, int *tdmr_num) · line 1019

Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+	/*
+	 * One TDMR must cover at least one (or partial) RAM entry,
+	 * otherwise it is kernel bug.  WARN_ON() in this case.
+	 */
+	if (WARN_ON_ONCE((start >= end) || start >= TDMR_END(tdmr)))
+		return 0;

This really means "no RAM found for this TDMR", right? Can we say that, please.

Quoted 6 lines

OK will add it. How about:

	/*
	 * No RAM found for this TDMR.  WARN() in this case, as it
	 * cannot happen otherwise it is a kernel bug.
	 */
Quoted 4 lines

The only useful information in that comment is the first sentence. The jibberish about WARN() is patently obvious from the next two lines of code.

*WHY* can't this happen? How might it have actually happened?

When TDMRs are created, we already have made sure one TDMR must cover at least one or partial RAM entry.

Signature · 2 lines
Thanks,
-Kai
author
#123 lore raw
In reply to Kai Huang
Quoted 44 lines · Kai Huang
Quoted 30 lines · Kai Huang
Quoted 29 lines · Dave Hansen
Quoted 9 lines · Kai Huang
Quoted 2 lines · Dave Hansen

In 5 years, if someone takes this code and runs it on Intel hardware with memory hotplug, CPU hotplug, NVDIMMs *AND* TDX support, what happens?

I thought we could document this in the documentation saying that this code can only work on TDX machines that don't have above capabilities (SPR for now). We can change the code and the documentation when we add the support of those features in the future, and update the documentation.

Quoted 5 lines

If 5 years later someone takes this code, he/she should take a look at the documentation and figure out that he/she should choose a newer kernel if the machine support those features.

I'll think about design solutions if above doesn't look good for you.

No, it doesn't look good to me.

You can't just say:

	/*
	 * This code will eat puppies if used on systems with hotplug.
	 */

and merrily await the puppy bloodbath.

If it's not compatible, then you have to *MAKE* it not compatible in a safe, controlled way.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

You can't just ignore the problems because they're not present on one version of the hardware.

Please, please read this again ^^

OK. I'll think about solutions and come back later.

Quoted 1 line

Hi Dave,

I think we have two approaches to handle memory hotplug interaction with the TDX module initialization.

Quoted 6 lines

The first approach is simple. We just block memory from being added as system RAM managed by page allocator when the platform supports TDX [1]. It seems we can add some arch-specific-check to __add_memory_resource() and reject the new memory resource if platform supports TDX. __add_memory_resource() is called by both __add_memory() and add_memory_driver_managed() so it prevents from adding NVDIMM as system RAM and normal ACPI memory hotplug [2].

Hi Dave,

Try to close how to handle memory hotplug. Any comments to below?

For the first approach, I forgot to think about memory hot-remove case. If we just reject adding new memory resource when TDX is capable on the platform, then if the memory is hot-removed, we won't be able to add it back. My thinking is we still want to support memory online/offline because it is purely in software but has nothing to do with TDX. But if one memory resource can be put to offline, it seems we don't have any way to prevent it from being removed.

So if we do above, on the future platforms when memory hotplug can co-exist with TDX, ACPI hot-add and kmem-hot-add memory will be prevented. However if some memory is hot-removed, it won't be able to be added back (even it is included in CMR, or TDMRs after TDX module is initialized).

Is this behavior acceptable? Or perhaps I have misunderstanding?

The second approach will behave more nicely, but I don't know whether it is worth to do it now.

Btw, below logic when adding a new memory resource has a minor problem, please see below...

Quoted 8 lines

The second approach is relatively more complicated. Instead of directly rejecting the new memory resource in __add_memory_resource(), we check whether the memory resource can be added based on CMR and the TDX module initialization
status. This is feasible as with the latest public P-SEAMLDR spec, we can get
CMR from P-SEAMLDR SEAMCALL[3]. So we can detect P-SEAMLDR and get CMR info during kernel boots. And in __add_memory_resource() we do below check:

Quoted 8 lines
	tdx_init_disable();	/*similar to cpu_hotplug_disable() */
	if (tdx_module_initialized())
		// reject memory hotplug
	else if (new_memory_resource NOT in CMRs)
		// reject memory hotplug
	else
		allow memory hotplug
	tdx_init_enable();	/*similar to cpu_hotplug_enable() */

...

Should be:

	// prevent racing with TDX module initialization */
	tdx_init_disable();

	if (tdx_module_initialized()) {
		if (new_memory_resource in TDMRs)
			// allow memory hot-add
		else
			// reject memory hot-add
	} else if (new_memory_resource in CMR) {
		// add new memory to TDX memory so it can be
		// included into TDMRs

		// allow memory hot-add
	}
	else
		// reject memory hot-add
	
	tdx_module_enable();

And when platform doesn't TDX, always allow memory hot-add.

Quoted 29 lines

tdx_init_disable() temporarily disables TDX module initialization by trying to grab the mutex. If the TDX module initialization is already on going, then it waits until it completes.

This should work better for future platforms, but would requires non-trivial more code as we need to add VMXON/VMXOFF support to the core-kernel to detect CMR using SEAMCALL. A side advantage is with VMXON in core-kernel we can shutdown the TDX module in kexec().

But for this series I think the second approach is overkill and we can choose to use the first simple approach?

Any suggestions?

[1] Platform supports TDX means SEAMRR is enabled, and there are at least 2 TDX keyIDs. Or we can just check SEAMRR is enabled, as in practice a SEAMRR is enabled means the machine is TDX-capable, and for now a TDX-capable machine doesn't support ACPI memory hotplug.

[2] It prevents adding legacy PMEM as system RAM too but I think it's fine. If user wants legacy PMEM then it is unlikely user will add it back and use as system RAM. User is unlikely to use legacy PMEM as TD guest memory directly as TD guests is likely to use a new memfd backend which allows private page not accessible from usrspace, so in this way we can exclude legacy PMEM from TDMRs.

[3] Please refer to SEAMLDR.SEAMINFO SEAMCALL in latest P-SEAMLDR spec:
https://www.intel.com/content/dam/develop/external/us/en/documents-tps/intel-tdx-seamldr-interface-specification.pdf

Quoted 1 line
Quoted 1 line
Quoted 1 line
#124 lore raw
Quoted 21 lines · Kai Huang

Should be:

	// prevent racing with TDX module initialization */
	tdx_init_disable();

	if (tdx_module_initialized()) {
		if (new_memory_resource in TDMRs)
			// allow memory hot-add
		else
			// reject memory hot-add
	} else if (new_memory_resource in CMR) {
		// add new memory to TDX memory so it can be
		// included into TDMRs

		// allow memory hot-add
	}
	else
		// reject memory hot-add
	
	tdx_module_enable();
Quoted 1 line

And when platform doesn't TDX, always allow memory hot-add.

I don't think it even needs to be *that* complicated.

It could just be winner take all: if TDX is initialized first, don't allow memory hotplug. If memory hotplug happens first, don't allow TDX to be initialized.

That's fine at least for a minimal patch set.

What you have up above is probably where you want to go eventually, but it means doing things like augmenting the e820 since it's the single source of truth for creating the TMDRs right now.

author
#125 lore raw
Quoted 24 lines · Dave Hansen
Quoted 21 lines · Kai Huang

Should be:

	// prevent racing with TDX module initialization */
	tdx_init_disable();

	if (tdx_module_initialized()) {
		if (new_memory_resource in TDMRs)
			// allow memory hot-add
		else
			// reject memory hot-add
	} else if (new_memory_resource in CMR) {
		// add new memory to TDX memory so it can be
		// included into TDMRs

		// allow memory hot-add
	}
	else
		// reject memory hot-add
	
	tdx_module_enable();
Quoted 1 line

And when platform doesn't TDX, always allow memory hot-add.

Quoted 7 lines

I don't think it even needs to be *that* complicated.

It could just be winner take all: if TDX is initialized first, don't allow memory hotplug. If memory hotplug happens first, don't allow TDX to be initialized.

That's fine at least for a minimal patch set.

OK. This should also work.

We will need tdx_init_disable() which grabs the mutex to prevent TDX module initialization from running concurrently, and to disable TDX module initialization once for all.

Quoted 5 lines

What you have up above is probably where you want to go eventually, but it means doing things like augmenting the e820 since it's the single source of truth for creating the TMDRs right now.

Yes. But in this case, I am thinking about probably we should switch from consulting e820 to consulting memblock. The advantage of using e820 is it's easy to include legacy PMEM as TDX memory, but the disadvantage is (as you can see in e820_for_each_mem() loop) I am actually merging contiguous different types of RAM entries in order to be consistent with the behavior of e820_memblock_setup(). This is not nice.

If memory hot-add and TDX can only be one winner, legacy PMEM actually won't be used as TDX memory anyway now. The reason is TDX guest will very likely needing to use the new fd-based backend (see my reply in other emails), but not just some random backend. To me it's totally fine to not support using legacy PMEM directly as TD guest backend (and if we create a TD with real NVDIMM as backend using dax, the TD cannot be created anyway). Given we cannot kmem-hot-add legacy PMEM back as system RAM, to me it's pointless to include legacy PMEM into TDMRs.

In this case, we can just create TDMRs based on memblock directly. One problem is memblock will be gone after kernel boots, but this can be solved either by keeping the memblock, or build the TDX memory early when memblock is still alive.

Btw, eventually, as it's likely we need to support different source of TDX memory (CLX memory, etc), I think eventually we will need some data structures to represent TDX memory block and APIs to add those blocks to the whole TDX memory so those TDX memory ranges from different source can be added before initializing the TDX module.

	struct tdx_memblock {
		struct list_head list;
		phys_addr_t start;
		phys_addr_t end;
		int nid;
		...
	};

	struct tdx_memory {
		struct list_head tmb_list;
		...
	};

	int tdx_memory_add_memblock(start, end, nid, ...);

And the TDMRs can be created based on 'struct tdx_memory'.

For now, we only need to add memblock to TDX memory.

Any comments?

Signature · 2 lines
Thanks,
-Kai
#140 lore raw
In reply to Kai Huang
Quoted 64 lines · Kai Huang
Quoted 44 lines · Kai Huang
Quoted 30 lines · Kai Huang
Quoted 29 lines · Dave Hansen
Quoted 9 lines · Kai Huang
Quoted 2 lines · Dave Hansen

In 5 years, if someone takes this code and runs it on Intel hardware with memory hotplug, CPU hotplug, NVDIMMs *AND* TDX support, what happens?

I thought we could document this in the documentation saying that this code can only work on TDX machines that don't have above capabilities (SPR for now). We can change the code and the documentation when we add the support of those features in the future, and update the documentation.

Quoted 5 lines

If 5 years later someone takes this code, he/she should take a look at the documentation and figure out that he/she should choose a newer kernel if the machine support those features.

I'll think about design solutions if above doesn't look good for you.

No, it doesn't look good to me.

You can't just say:

/*
* This code will eat puppies if used on systems with hotplug. */

and merrily await the puppy bloodbath.

If it's not compatible, then you have to *MAKE* it not compatible in a safe, controlled way.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

You can't just ignore the problems because they're not present on one version of the hardware.

Please, please read this again ^^

OK. I'll think about solutions and come back later.

Quoted 1 line

Hi Dave,

I think we have two approaches to handle memory hotplug interaction with the TDX module initialization.

Quoted 6 lines

The first approach is simple. We just block memory from being added as system RAM managed by page allocator when the platform supports TDX [1]. It seems we can add some arch-specific-check to __add_memory_resource() and reject the new memory resource if platform supports TDX. __add_memory_resource() is called by both __add_memory() and add_memory_driver_managed() so it prevents from adding NVDIMM as system RAM and normal ACPI memory hotplug [2].

Hi Dave,

Try to close how to handle memory hotplug. Any comments to below?

For the first approach, I forgot to think about memory hot-remove case. If we just reject adding new memory resource when TDX is capable on the platform, then if the memory is hot-removed, we won't be able to add it back. My thinking is we still want to support memory online/offline because it is purely in software but has nothing to do with TDX. But if one memory resource can be put to offline, it seems we don't have any way to prevent it from being removed.

Quoted 6 lines

So if we do above, on the future platforms when memory hotplug can co-exist with TDX, ACPI hot-add and kmem-hot-add memory will be prevented. However if some memory is hot-removed, it won't be able to be added back (even it is included in CMR, or TDMRs after TDX module is initialized).

Is this behavior acceptable? Or perhaps I have misunderstanding?

Memory online at boot uses similar kernel paths as memory-online at run time, so it sounds like your question is confusing physical vs logical remove. Consider the case of logical offline then re-online where the proposed TDX sanity check blocks the memory online, but then a new kernel is kexec'd and that kernel again trusts the memory as TD convertible again just because it onlines the memory in the boot path. For physical memory remove it seems up to the platform to block that if it conflicts with TDX, not for the kernel to add extra assumptions that logical offline / online is incompatible with TDX.

author
#141 lore raw
Quoted 72 lines · Dan Williams
Quoted 64 lines · Kai Huang
Quoted 44 lines · Kai Huang
Quoted 30 lines · Kai Huang
Quoted 29 lines · Dave Hansen
Quoted 9 lines · Kai Huang
Quoted 2 lines · Dave Hansen

In 5 years, if someone takes this code and runs it on Intel hardware with memory hotplug, CPU hotplug, NVDIMMs *AND* TDX support, what happens?

I thought we could document this in the documentation saying that this code can only work on TDX machines that don't have above capabilities (SPR for now). We can change the code and the documentation when we add the support of those features in the future, and update the documentation.

Quoted 5 lines

If 5 years later someone takes this code, he/she should take a look at the documentation and figure out that he/she should choose a newer kernel if the machine support those features.

I'll think about design solutions if above doesn't look good for you.

No, it doesn't look good to me.

You can't just say:

/*
* This code will eat puppies if used on systems with hotplug. */

and merrily await the puppy bloodbath.

If it's not compatible, then you have to *MAKE* it not compatible in a safe, controlled way.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

You can't just ignore the problems because they're not present on one version of the hardware.

Please, please read this again ^^

OK. I'll think about solutions and come back later.

Quoted 1 line

Hi Dave,

I think we have two approaches to handle memory hotplug interaction with the TDX module initialization.

Quoted 6 lines

The first approach is simple. We just block memory from being added as system RAM managed by page allocator when the platform supports TDX [1]. It seems we can add some arch-specific-check to __add_memory_resource() and reject the new memory resource if platform supports TDX. __add_memory_resource() is called by both __add_memory() and add_memory_driver_managed() so it prevents from adding NVDIMM as system RAM and normal ACPI memory hotplug [2].

Hi Dave,

Try to close how to handle memory hotplug. Any comments to below?

For the first approach, I forgot to think about memory hot-remove case. If we just reject adding new memory resource when TDX is capable on the platform, then if the memory is hot-removed, we won't be able to add it back. My thinking is we still want to support memory online/offline because it is purely in software but has nothing to do with TDX. But if one memory resource can be put to offline, it seems we don't have any way to prevent it from being removed.

Quoted 6 lines

So if we do above, on the future platforms when memory hotplug can co-exist with TDX, ACPI hot-add and kmem-hot-add memory will be prevented. However if some memory is hot-removed, it won't be able to be added back (even it is included in CMR, or TDMRs after TDX module is initialized).

Is this behavior acceptable? Or perhaps I have misunderstanding?

Quoted 9 lines

Memory online at boot uses similar kernel paths as memory-online at run time, so it sounds like your question is confusing physical vs logical remove. Consider the case of logical offline then re-online where the proposed TDX sanity check blocks the memory online, but then a new kernel is kexec'd and that kernel again trusts the memory as TD convertible again just because it onlines the memory in the boot path. For physical memory remove it seems up to the platform to block that if it conflicts with TDX, not for the kernel to add extra assumptions that logical offline / online is incompatible with TDX.

Hi Dan,

No we don't block memory online, but we block memory add. The code I mentioned is add_memory_resource(), while memory online code path is memory_block_online(). Or am I wrong?

Signature · 2 lines
Thanks,
-Kai
author
#126 lore raw
In reply to Kai Huang
Quoted 88 lines · Kai Huang
Quoted 24 lines · Dave Hansen
Quoted 21 lines · Kai Huang

Should be:

	// prevent racing with TDX module initialization */
	tdx_init_disable();

	if (tdx_module_initialized()) {
		if (new_memory_resource in TDMRs)
			// allow memory hot-add
		else
			// reject memory hot-add
	} else if (new_memory_resource in CMR) {
		// add new memory to TDX memory so it can be
		// included into TDMRs

		// allow memory hot-add
	}
	else
		// reject memory hot-add
	
	tdx_module_enable();
Quoted 1 line

And when platform doesn't TDX, always allow memory hot-add.

Quoted 7 lines

I don't think it even needs to be *that* complicated.

It could just be winner take all: if TDX is initialized first, don't allow memory hotplug. If memory hotplug happens first, don't allow TDX to be initialized.

That's fine at least for a minimal patch set.

OK. This should also work.

We will need tdx_init_disable() which grabs the mutex to prevent TDX module initialization from running concurrently, and to disable TDX module initialization once for all.

Quoted 5 lines

What you have up above is probably where you want to go eventually, but it means doing things like augmenting the e820 since it's the single source of truth for creating the TMDRs right now.

Yes. But in this case, I am thinking about probably we should switch from consulting e820 to consulting memblock. The advantage of using e820 is it's easy to include legacy PMEM as TDX memory, but the disadvantage is (as you can see in e820_for_each_mem() loop) I am actually merging contiguous different types of RAM entries in order to be consistent with the behavior of e820_memblock_setup(). This is not nice.

If memory hot-add and TDX can only be one winner, legacy PMEM actually won't be used as TDX memory anyway now. The reason is TDX guest will very likely needing to use the new fd-based backend (see my reply in other emails), but not just some random backend. To me it's totally fine to not support using legacy PMEM directly as TD guest backend (and if we create a TD with real NVDIMM as backend using dax, the TD cannot be created anyway). Given we cannot kmem-hot-add legacy PMEM back as system RAM, to me it's pointless to include legacy PMEM into TDMRs.

In this case, we can just create TDMRs based on memblock directly. One problem is memblock will be gone after kernel boots, but this can be solved either by keeping the memblock, or build the TDX memory early when memblock is still alive.

Btw, eventually, as it's likely we need to support different source of TDX memory (CLX memory, etc), I think eventually we will need some data structures to represent TDX memory block and APIs to add those blocks to the whole TDX memory so those TDX memory ranges from different source can be added before initializing the TDX module.

	struct tdx_memblock {
		struct list_head list;
		phys_addr_t start;
		phys_addr_t end;
		int nid;
		...
	};

	struct tdx_memory {
		struct list_head tmb_list;
		...
	};

	int tdx_memory_add_memblock(start, end, nid, ...);
Quoted 6 lines

And the TDMRs can be created based on 'struct tdx_memory'.

For now, we only need to add memblock to TDX memory.

Any comments?

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Please let me know if you have any objection? Thanks!

Signature · 2 lines
Thanks,
-Kai
#127 lore raw

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

The platform will already do this, right? I don't understand why this is trying to take proactive action versus documenting the error conditions and steps someone needs to take to avoid unconvertible memory. There is already the CONFIG_HMEM_REPORTING that describes relative performance properties between initiators and targets, it seems fitting to also add security properties between initiators and targets so someone can enumerate the numa-mempolicy that avoids unconvertible memory.

No, special casing in hotplug code paths needed.

Quoted 5 lines

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

Quoted 8 lines

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Please let me know if you have any objection? Thanks!

It's already the case that x86 maintains sideband structures to preserve memory after exiting the early memblock code. Mike, correct me if I am wrong, but adding more is less desirable than just keeping the memblock around?

author
#128 lore raw

Thanks for feedback!

Quoted 10 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 5 lines

Hi Dave,

Sorry to ping (trying to close this).

Quoted 8 lines
Quoted 5 lines

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

The platform will already do this, right?

In the current v3 implementation, we don't have any code to handle memory hotplug, therefore nothing prevents people from adding legacy PMEMs as system RAM using kmem driver. In order to guarantee all pages managed by page allocator are all TDX memory, the v3 implementation needs to always include legacy PMEMs as TDX memory so that even people truly add legacy PMEMs as system RAM, we can still guarantee all pages in page allocator are TDX memory.

Of course, a side benefit of always including legacy PMEMs is people theoretically can use them directly as TD guest backend, but this is just a bonus but not something that we need to guarantee.

Quoted 8 lines

I don't understand why this
is trying to take proactive action versus documenting the error conditions and steps someone needs to take to avoid unconvertible memory. There is already the CONFIG_HMEM_REPORTING that describes relative performance properties between initiators and targets, it seems fitting to also add security properties between initiators and targets so someone can enumerate the numa-mempolicy that avoids unconvertible memory.

I don't think there's anything related to performance properties here. The only goal here is to make sure all pages in page allocator are TDX memory pages.

Quoted 15 lines

No, special casing in hotplug code paths needed.

Quoted 12 lines

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Quoted 5 lines
Quoted 1 line

Please let me know if you have any objection? Thanks!

It's already the case that x86 maintains sideband structures to preserve memory after exiting the early memblock code.

May I ask what data structures are you referring to?

Btw, the purpose of 'tdx_memblock' and 'tdx_memory' is not only just to preserve memblock info during boot. It is also used to provide a common data structure that the "constructing TDMRs" code can work on. If you look at patch 11-14, the logic (create TDMRs, allocate PAMTs, sets up reserved areas) around how to construct TDMRs doesn't have hard dependency on e820. If we construct TDMRs based on a common 'tdx_memory' like below:

	int construct_tdmrs(struct tdx_memory *tmem, ...);

It would be much easier to support other TDX memory resources in the future.

The thing I am not sure is Dave wants to keep the code minimal (as this series is already very big in terms of LoC) to make TDX running, and for now in practice there's only system RAM during boot is TDX capable, so I am not sure we should introduce those structures now.

Quoted 3 lines

Mike, correct
me if I am wrong, but adding more is less desirable than just keeping the memblock around?

#129 lore raw
Quoted 20 lines · Kai Huang

Thanks for feedback!

Quoted 16 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

Quoted 6 lines
Quoted 2 lines

The platform will already do this, right?

In the current v3 implementation, we don't have any code to handle memory hotplug, therefore nothing prevents people from adding legacy PMEMs as system RAM using kmem driver. In order to guarantee all pages managed by page

That's the fundamental question I am asking why is "guarantee all pages managed by page allocator are TDX memory". That seems overkill compared to indicating the incompatibility after the fact.

Quoted 3 lines

allocator are all TDX memory, the v3 implementation needs to always include legacy PMEMs as TDX memory so that even people truly add legacy PMEMs as system RAM, we can still guarantee all pages in page allocator are TDX memory.

Why?

Quoted 14 lines

Of course, a side benefit of always including legacy PMEMs is people theoretically can use them directly as TD guest backend, but this is just a bonus but not something that we need to guarantee.

Quoted 8 lines

I don't understand why this
is trying to take proactive action versus documenting the error conditions and steps someone needs to take to avoid unconvertible memory. There is already the CONFIG_HMEM_REPORTING that describes relative performance properties between initiators and targets, it seems fitting to also add security properties between initiators and targets so someone can enumerate the numa-mempolicy that avoids unconvertible memory.

Quoted 2 lines

I don't think there's anything related to performance properties here. The only goal here is to make sure all pages in page allocator are TDX memory pages.

Please reconsider or re-clarify that goal.

Quoted 16 lines
Quoted 16 lines

No, special casing in hotplug code paths needed.

Quoted 12 lines

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Please let me know if you have any objection? Thanks!

It's already the case that x86 maintains sideband structures to preserve memory after exiting the early memblock code.

May I ask what data structures are you referring to?

struct numa_meminfo.

Quoted 10 lines

Btw, the purpose of 'tdx_memblock' and 'tdx_memory' is not only just to preserve memblock info during boot. It is also used to provide a common data structure that the "constructing TDMRs" code can work on. If you look at patch 11-14, the logic (create TDMRs, allocate PAMTs, sets up reserved areas) around how to construct TDMRs doesn't have hard dependency on e820. If we construct TDMRs based on a common 'tdx_memory' like below:

        int construct_tdmrs(struct tdx_memory *tmem, ...);

It would be much easier to support other TDX memory resources in the future.

"in the future" is a prompt to ask "Why not wait until that future / need arrives before adding new infrastructure?"

Quoted 8 lines

The thing I am not sure is Dave wants to keep the code minimal (as this series is already very big in terms of LoC) to make TDX running, and for now in practice there's only system RAM during boot is TDX capable, so I am not sure we should introduce those structures now.

Quoted 3 lines

Mike, correct
me if I am wrong, but adding more is less desirable than just keeping the memblock around?

author
#130 lore raw
Quoted 24 lines · Dan Williams
Quoted 23 lines · Kai Huang

Thanks for feedback!

Quoted 18 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

The platform will already do this, right?

Quoted 7 lines
Quoted 3 lines

In the current v3 implementation, we don't have any code to handle memory hotplug, therefore nothing prevents people from adding legacy PMEMs as system RAM using kmem driver. In order to guarantee all pages managed by page

That's the fundamental question I am asking why is "guarantee all pages managed by page allocator are TDX memory". That seems overkill compared to indicating the incompatibility after the fact.

As I explained, the reason is I don't want to modify page allocator to distinguish TDX and non-TDX allocation, for instance, having to have a ZONE_TDX and GFP_TDX.

KVM depends on host's page fault handler to allocate the page. In fact KVM only consumes PFN from host's page tables. For now only RAM is TDX memory. By guaranteeing all pages in page allocator is TDX memory, we can easily use anonymous pages as TD guest memory. This also allows us to easily extend the shmem to support a new fd-based backend which doesn't require having to mmap() TD guest memory to host userspace:

https://lore.kernel.org/kvm/[email protected]/

Also, besides TD guest memory, there are some per-TD control data structures (which must be TDX memory too) need to be allocated for each TD. Normal memory allocation APIs can be used for such allocation if we guarantee all pages in page allocator is TDX memory.

Quoted 6 lines
Quoted 4 lines

allocator are all TDX memory, the v3 implementation needs to always include legacy PMEMs as TDX memory so that even people truly add legacy PMEMs as system RAM, we can still guarantee all pages in page allocator are TDX memory.

Why?

If we don't include legacy PMEMs as TDX memory, then after they are hot-added as system RAM using kmem driver, the assumption of "all pages in page allocator are TDX memory" is broken. A TD can be killed during runtime.

Quoted 53 lines
Quoted 15 lines

Of course, a side benefit of always including legacy PMEMs is people theoretically can use them directly as TD guest backend, but this is just a bonus but not something that we need to guarantee.

Quoted 8 lines

I don't understand why this
is trying to take proactive action versus documenting the error conditions and steps someone needs to take to avoid unconvertible memory. There is already the CONFIG_HMEM_REPORTING that describes relative performance properties between initiators and targets, it seems fitting to also add security properties between initiators and targets so someone can enumerate the numa-mempolicy that avoids unconvertible memory.

Quoted 2 lines

I don't think there's anything related to performance properties here. The only goal here is to make sure all pages in page allocator are TDX memory pages.

Please reconsider or re-clarify that goal.

Quoted 16 lines
Quoted 16 lines

No, special casing in hotplug code paths needed.

Quoted 12 lines

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Please let me know if you have any objection? Thanks!

It's already the case that x86 maintains sideband structures to preserve memory after exiting the early memblock code.

May I ask what data structures are you referring to?

struct numa_meminfo.

Quoted 7 lines

Btw, the purpose of 'tdx_memblock' and 'tdx_memory' is not only just to preserve memblock info during boot. It is also used to provide a common data structure that the "constructing TDMRs" code can work on. If you look at patch 11-14, the logic (create TDMRs, allocate PAMTs, sets up reserved areas) around how to construct TDMRs doesn't have hard dependency on e820. If we construct TDMRs based on a common 'tdx_memory' like below:

Quoted 6 lines
Quoted 3 lines
        int construct_tdmrs(struct tdx_memory *tmem, ...);

It would be much easier to support other TDX memory resources in the future.

"in the future" is a prompt to ask "Why not wait until that future / need arrives before adding new infrastructure?"

Fine to me.

Signature · 2 lines
Thanks,
-Kai
#131 lore raw
Quoted 30 lines · Kai Huang
Quoted 28 lines · Dan Williams
Quoted 26 lines · Kai Huang

Thanks for feedback!

Quoted 10 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 5 lines

Hi Dave,

Sorry to ping (trying to close this).

Quoted 8 lines
Quoted 5 lines

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

The platform will already do this, right?

In the current v3 implementation, we don't have any code to handle memory hotplug, therefore nothing prevents people from adding legacy PMEMs as system RAM using kmem driver. In order to guarantee all pages managed by page

Quoted 7 lines
Quoted 3 lines

That's the fundamental question I am asking why is "guarantee all pages managed by page allocator are TDX memory". That seems overkill compared to indicating the incompatibility after the fact.

As I explained, the reason is I don't want to modify page allocator to distinguish TDX and non-TDX allocation, for instance, having to have a ZONE_TDX and GFP_TDX.

Right, TDX details do not belong at that level, but it will work almost all the time if you do nothing to "guarantee" all TDX capable pages all the time.

Quoted 4 lines

KVM depends on host's page fault handler to allocate the page. In fact KVM only consumes PFN from host's page tables. For now only RAM is TDX memory. By guaranteeing all pages in page allocator is TDX memory, we can easily use anonymous pages as TD guest memory.

Again, TDX capable pages will be the overwhelming default, why are you worried about cluttering the memory hotplug path for nice corner cases.

Consider the fact that end users can break the kernel by specifying invalid memmap= command line options. The memory hotplug code does not take any steps to add safety in those cases because there are already too many ways it can go wrong. TDX is just one more corner case where the memmap= user needs to be careful. Otherwise, it is up to the platform firmware to make sure everything in the base memory map is TDX capable, and then all you need is documentation about the failure mode when extending "System RAM" beyond that baseline.

Quoted 9 lines

shmem to support a new fd-based backend which doesn't require having to mmap() TD guest memory to host userspace:

https://lore.kernel.org/kvm/[email protected]/

Also, besides TD guest memory, there are some per-TD control data structures (which must be TDX memory too) need to be allocated for each TD. Normal memory allocation APIs can be used for such allocation if we guarantee all pages in page allocator is TDX memory.

You don't need that guarantee, just check it after the fact and fail if that assertion fails. It should almost always be the case that it succeeds and if it doesn't then something special is happening with that system and the end user has effectively opt-ed out of TDX operation.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

allocator are all TDX memory, the v3 implementation needs to always include legacy PMEMs as TDX memory so that even people truly add legacy PMEMs as system RAM, we can still guarantee all pages in page allocator are TDX memory.

Why?

If we don't include legacy PMEMs as TDX memory, then after they are hot-added as system RAM using kmem driver, the assumption of "all pages in page allocator are TDX memory" is broken. A TD can be killed during runtime.

Yes, that is what the end user asked for. If they don't want that to happen then the policy decision about using kmem needs to be updated in userspace, not hard code that policy decision towards TDX inside the kernel.

author
#132 lore raw
Quoted 35 lines · Dan Williams
Quoted 34 lines · Kai Huang
Quoted 31 lines · Dan Williams
Quoted 20 lines · Kai Huang

Thanks for feedback!

Quoted 16 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

Quoted 6 lines
Quoted 2 lines

The platform will already do this, right?

In the current v3 implementation, we don't have any code to handle memory hotplug, therefore nothing prevents people from adding legacy PMEMs as system RAM using kmem driver. In order to guarantee all pages managed by page

That's the fundamental question I am asking why is "guarantee all pages managed by page allocator are TDX memory". That seems overkill compared to indicating the incompatibility after the fact.

Quoted 7 lines
Quoted 3 lines

As I explained, the reason is I don't want to modify page allocator to distinguish TDX and non-TDX allocation, for instance, having to have a ZONE_TDX and GFP_TDX.

Right, TDX details do not belong at that level, but it will work almost all the time if you do nothing to "guarantee" all TDX capable pages all the time.

"almost all the time" do you mean?

Quoted 9 lines
Quoted 5 lines

KVM depends on host's page fault handler to allocate the page. In fact KVM only consumes PFN from host's page tables. For now only RAM is TDX memory. By guaranteeing all pages in page allocator is TDX memory, we can easily use anonymous pages as TD guest memory.

Again, TDX capable pages will be the overwhelming default, why are you worried about cluttering the memory hotplug path for nice corner cases.

Firstly perhaps I forgot to mention there are two concepts about TDX memory, so let me clarify first:

1) Convertible Memory Regions (CMRs). This is reported by BIOS (thus static) to indicate which memory regions *can* be used as TDX memory. This basically means all RAM during boot for now.

2) TD Memory Regions (TDMRs). Memory pages in CMRs are not automatically TDX usable memory. The TDX module needs to be configured which (convertible) memory regions can be used as TDX memory. Kernel is responsible for choosing the ranges, and configure to the TDX module. If a convertible memory page is not included into TDMRs, the TDX module will report error when it is assigned to a TD.

Quoted 9 lines

Consider the fact that end users can break the kernel by specifying invalid memmap= command line options. The memory hotplug code does not take any steps to add safety in those cases because there are already too many ways it can go wrong. TDX is just one more corner case where the memmap= user needs to be careful. Otherwise, it is up to the platform firmware to make sure everything in the base memory map is TDX capable, and then all you need is documentation about the failure mode when extending "System RAM" beyond that baseline.

So the fact is, if we don't include legacy PMEMs into TDMRs, and don't do anything in memory hotplug, then if user does kmem-hot-add legacy PMEMs as system RAM, a live TD may eventually be killed.

If such case is a corner case that we don't need to guarantee, then even better. And we have an additional reason that those legacy PMEMs don't need to be in TDMRs. As you suggested, we can add some documentation to point out.

But the point we want to do some code check and prevent memory hotplug is, as Dave said, we want this piece of code to work on *ANY* TDX capable machines, including future machines which may, i.e. supports NVDIMM/CLX memory as TDX memory. If we don't do any code check in memory hotplug in this series, then when this code runs in future platforms, user can plug NVDIMM or CLX memory as system RAM thus break the assumption "all pages in page allocator are TDX memory", which eventually leads to live TDs being killed potentially.

Dave said we need to guarantee this code can work on *ANY* TDX machines. Some documentation saying it only works one some platforms and you shouldn't do things on other platforms are not good enough:

https://lore.kernel.org/lkml/[email protected]/T/#m6df45b6e1702bb03dcb027044a0dabf30a86e471

Quoted 12 lines
Quoted 11 lines

shmem to support a new fd-based backend which doesn't require having to mmap() TD guest memory to host userspace:

https://lore.kernel.org/kvm/[email protected]/

Also, besides TD guest memory, there are some per-TD control data structures (which must be TDX memory too) need to be allocated for each TD. Normal memory allocation APIs can be used for such allocation if we guarantee all pages in page allocator is TDX memory.

Quoted 5 lines

You don't need that guarantee, just check it after the fact and fail if that assertion fails. It should almost always be the case that it succeeds and if it doesn't then something special is happening with that system and the end user has effectively opt-ed out of TDX operation.

This doesn't guarantee consistent behaviour. For instance, for one TD it can be created, while the second may fail. We should provide a consistent service.

The thing is anyway we need to configure some memory regions to the TDX module. To me there's no reason we don't want to guarantee all pages in page allocator are TDX memory.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

allocator are all TDX memory, the v3 implementation needs to always include legacy PMEMs as TDX memory so that even people truly add legacy PMEMs as system RAM, we can still guarantee all pages in page allocator are TDX memory.

Why?

Quoted 8 lines
Quoted 3 lines

If we don't include legacy PMEMs as TDX memory, then after they are hot-added as system RAM using kmem driver, the assumption of "all pages in page allocator are TDX memory" is broken. A TD can be killed during runtime.

Yes, that is what the end user asked for. If they don't want that to happen then the policy decision about using kmem needs to be updated in userspace, not hard code that policy decision towards TDX inside the kernel.

This is also fine to me. But please also see above Dave's comment.

Thanks for those valuable feedback!

Signature · 2 lines
Thanks,
-Kai
#133 lore raw
Quoted 97 lines · Kai Huang
Quoted 35 lines · Dan Williams
Quoted 34 lines · Kai Huang
Quoted 31 lines · Dan Williams
Quoted 20 lines · Kai Huang

Thanks for feedback!

Quoted 16 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

Quoted 6 lines
Quoted 2 lines

The platform will already do this, right?

In the current v3 implementation, we don't have any code to handle memory hotplug, therefore nothing prevents people from adding legacy PMEMs as system RAM using kmem driver. In order to guarantee all pages managed by page

That's the fundamental question I am asking why is "guarantee all pages managed by page allocator are TDX memory". That seems overkill compared to indicating the incompatibility after the fact.

Quoted 7 lines
Quoted 3 lines

As I explained, the reason is I don't want to modify page allocator to distinguish TDX and non-TDX allocation, for instance, having to have a ZONE_TDX and GFP_TDX.

Right, TDX details do not belong at that level, but it will work almost all the time if you do nothing to "guarantee" all TDX capable pages all the time.

"almost all the time" do you mean?

Quoted 9 lines
Quoted 5 lines

KVM depends on host's page fault handler to allocate the page. In fact KVM only consumes PFN from host's page tables. For now only RAM is TDX memory. By guaranteeing all pages in page allocator is TDX memory, we can easily use anonymous pages as TD guest memory.

Again, TDX capable pages will be the overwhelming default, why are you worried about cluttering the memory hotplug path for nice corner cases.

Firstly perhaps I forgot to mention there are two concepts about TDX memory, so let me clarify first:

1) Convertible Memory Regions (CMRs). This is reported by BIOS (thus static) to indicate which memory regions *can* be used as TDX memory. This basically means all RAM during boot for now.

2) TD Memory Regions (TDMRs). Memory pages in CMRs are not automatically TDX usable memory. The TDX module needs to be configured which (convertible) memory regions can be used as TDX memory. Kernel is responsible for choosing the ranges, and configure to the TDX module. If a convertible memory page is not included into TDMRs, the TDX module will report error when it is assigned to a TD.

Quoted 9 lines

Consider the fact that end users can break the kernel by specifying invalid memmap= command line options. The memory hotplug code does not take any steps to add safety in those cases because there are already too many ways it can go wrong. TDX is just one more corner case where the memmap= user needs to be careful. Otherwise, it is up to the platform firmware to make sure everything in the base memory map is TDX capable, and then all you need is documentation about the failure mode when extending "System RAM" beyond that baseline.

So the fact is, if we don't include legacy PMEMs into TDMRs, and don't do anything in memory hotplug, then if user does kmem-hot-add legacy PMEMs as system RAM, a live TD may eventually be killed.

If such case is a corner case that we don't need to guarantee, then even better. And we have an additional reason that those legacy PMEMs don't need to be in TDMRs. As you suggested, we can add some documentation to point out.

But the point we want to do some code check and prevent memory hotplug is, as Dave said, we want this piece of code to work on *ANY* TDX capable machines, including future machines which may, i.e. supports NVDIMM/CLX memory as TDX memory. If we don't do any code check in memory hotplug in this series, then when this code runs in future platforms, user can plug NVDIMM or CLX memory as system RAM thus break the assumption "all pages in page allocator are TDX memory", which eventually leads to live TDs being killed potentially.

Quoted 5 lines

Dave said we need to guarantee this code can work on *ANY* TDX machines. Some documentation saying it only works one some platforms and you shouldn't do things on other platforms are not good enough:

https://lore.kernel.org/lkml/[email protected]/T/#m6df45b6e1702bb03dcb027044a0dabf30a86e471

Yes, the incompatible cases cannot be ignored, but I disagree that they actively need to be prevented. One way to achieve that is to explicitly enumerate TDX capable memory and document how mempolicy can be used to avoid killing TDs.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

shmem to support a new fd-based backend which doesn't require having to mmap() TD guest memory to host userspace:

https://lore.kernel.org/kvm/[email protected]/

Also, besides TD guest memory, there are some per-TD control data structures (which must be TDX memory too) need to be allocated for each TD. Normal memory allocation APIs can be used for such allocation if we guarantee all pages in page allocator is TDX memory.

Quoted 8 lines
Quoted 5 lines

You don't need that guarantee, just check it after the fact and fail if that assertion fails. It should almost always be the case that it succeeds and if it doesn't then something special is happening with that system and the end user has effectively opt-ed out of TDX operation.

This doesn't guarantee consistent behaviour. For instance, for one TD it can be created, while the second may fail. We should provide a consistent service.

Yes, there needs to be enumeration and policy knobs to avoid failures, hard coded "no memory hotplug" hacks do not seem the right enumeration and policy knobs to me.

Quoted 15 lines

The thing is anyway we need to configure some memory regions to the TDX module. To me there's no reason we don't want to guarantee all pages in page allocator are TDX memory.

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

allocator are all TDX memory, the v3 implementation needs to always include legacy PMEMs as TDX memory so that even people truly add legacy PMEMs as system RAM, we can still guarantee all pages in page allocator are TDX memory.

Why?

If we don't include legacy PMEMs as TDX memory, then after they are hot-added as system RAM using kmem driver, the assumption of "all pages in page allocator are TDX memory" is broken. A TD can be killed during runtime.

Quoted 6 lines
Quoted 4 lines

Yes, that is what the end user asked for. If they don't want that to happen then the policy decision about using kmem needs to be updated in userspace, not hard code that policy decision towards TDX inside the kernel.

This is also fine to me. But please also see above Dave's comment.

Dave is right, the implementation can not just ignore the conflict. To me, enumeration plus error reporting allows for flexibility without hard coding policy in the kernel.

#136 lore raw
In reply to Dan Williams
Quoted 38 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

The platform will already do this, right? I don't understand why this is trying to take proactive action versus documenting the error conditions and steps someone needs to take to avoid unconvertible memory. There is already the CONFIG_HMEM_REPORTING that describes relative performance properties between initiators and targets, it seems fitting to also add security properties between initiators and targets so someone can enumerate the numa-mempolicy that avoids unconvertible memory.

No, special casing in hotplug code paths needed.

Quoted 12 lines

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Quoted 6 lines
Quoted 1 line

Please let me know if you have any objection? Thanks!

It's already the case that x86 maintains sideband structures to preserve memory after exiting the early memblock code. Mike, correct me if I am wrong, but adding more is less desirable than just keeping the memblock around?

TBH, I didn't read the entire thread yet, but at the first glance, keeping memblock around is much more preferable that adding yet another { .start, .end, .flags } data structure. To keep memblock after boot all is needed is something like

select ARCH_KEEP_MEMBLOCK if INTEL_TDX_HOST

I'll take a closer look next week on the entire series, maybe I'm missing some details.

Signature · 2 lines
Sincerely yours,
Mike.
author
#137 lore raw
Quoted 51 lines · Mike Rapoport
Quoted 38 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

The platform will already do this, right? I don't understand why this is trying to take proactive action versus documenting the error conditions and steps someone needs to take to avoid unconvertible memory. There is already the CONFIG_HMEM_REPORTING that describes relative performance properties between initiators and targets, it seems fitting to also add security properties between initiators and targets so someone can enumerate the numa-mempolicy that avoids unconvertible memory.

No, special casing in hotplug code paths needed.

Quoted 12 lines

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Quoted 6 lines
Quoted 1 line

Please let me know if you have any objection? Thanks!

It's already the case that x86 maintains sideband structures to preserve memory after exiting the early memblock code. Mike, correct me if I am wrong, but adding more is less desirable than just keeping the memblock around?

TBH, I didn't read the entire thread yet, but at the first glance, keeping memblock around is much more preferable that adding yet another { .start, .end, .flags } data structure. To keep memblock after boot all is needed is something like

Quoted 5 lines

select ARCH_KEEP_MEMBLOCK if INTEL_TDX_HOST

I'll take a closer look next week on the entire series, maybe I'm missing some details.

Hi Mike,

Thanks for feedback.

Perhaps I haven't put a lot details of the new TDX data structures, so let me point out that the new two data structures 'struct tdx_memblock' and 'struct tdx_memory' that I am proposing are mostly supposed to be used by TDX code only, which is pretty standalone. They are not supposed to be some basic infrastructure that can be widely used by other random kernel components.

In fact, currently the only operation we need is to allow memblock to register all memory regions as TDX memory blocks when the memblock is still alive. Therefore, in fact, the new data structures can even be completely invisible to other kernel components. For instance, TDX code can provide below API w/o exposing any data structures to other kernel components:

int tdx_add_memory_block(phys_addr_t start, phys_addr_t end, int nid);

And we call above API for each memory region in memblock when it is alive.

TDX code internally manages those memory regions via the new data structures that I mentioned above, so we don't need to keep memblock after boot. The advantage of this approach is it is more flexible to support other potential TDX memory resources (such as CLX memory) in the future.

Otherwise, we can do as you suggested to select ARCH_KEEP_MEMBLOCK when INTEL_TDX_HOST is on and TDX code internally uses memblock API directly.

Signature · 2 lines
Thanks,
-Kai
author
#134 lore raw
In reply to Dan Williams
Quoted 102 lines · Dan Williams
Quoted 101 lines · Kai Huang
Quoted 35 lines · Dan Williams
Quoted 34 lines · Kai Huang
Quoted 31 lines · Dan Williams
Quoted 20 lines · Kai Huang

Thanks for feedback!

Quoted 16 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

Quoted 6 lines
Quoted 2 lines

The platform will already do this, right?

In the current v3 implementation, we don't have any code to handle memory hotplug, therefore nothing prevents people from adding legacy PMEMs as system RAM using kmem driver. In order to guarantee all pages managed by page

That's the fundamental question I am asking why is "guarantee all pages managed by page allocator are TDX memory". That seems overkill compared to indicating the incompatibility after the fact.

Quoted 7 lines
Quoted 3 lines

As I explained, the reason is I don't want to modify page allocator to distinguish TDX and non-TDX allocation, for instance, having to have a ZONE_TDX and GFP_TDX.

Right, TDX details do not belong at that level, but it will work almost all the time if you do nothing to "guarantee" all TDX capable pages all the time.

"almost all the time" do you mean?

Quoted 9 lines
Quoted 5 lines

KVM depends on host's page fault handler to allocate the page. In fact KVM only consumes PFN from host's page tables. For now only RAM is TDX memory. By guaranteeing all pages in page allocator is TDX memory, we can easily use anonymous pages as TD guest memory.

Again, TDX capable pages will be the overwhelming default, why are you worried about cluttering the memory hotplug path for nice corner cases.

Firstly perhaps I forgot to mention there are two concepts about TDX memory, so let me clarify first:

1) Convertible Memory Regions (CMRs). This is reported by BIOS (thus static) to indicate which memory regions *can* be used as TDX memory. This basically means all RAM during boot for now.

2) TD Memory Regions (TDMRs). Memory pages in CMRs are not automatically TDX usable memory. The TDX module needs to be configured which (convertible) memory regions can be used as TDX memory. Kernel is responsible for choosing the ranges, and configure to the TDX module. If a convertible memory page is not included into TDMRs, the TDX module will report error when it is assigned to a TD.

Quoted 9 lines

Consider the fact that end users can break the kernel by specifying invalid memmap= command line options. The memory hotplug code does not take any steps to add safety in those cases because there are already too many ways it can go wrong. TDX is just one more corner case where the memmap= user needs to be careful. Otherwise, it is up to the platform firmware to make sure everything in the base memory map is TDX capable, and then all you need is documentation about the failure mode when extending "System RAM" beyond that baseline.

So the fact is, if we don't include legacy PMEMs into TDMRs, and don't do anything in memory hotplug, then if user does kmem-hot-add legacy PMEMs as system RAM, a live TD may eventually be killed.

If such case is a corner case that we don't need to guarantee, then even better. And we have an additional reason that those legacy PMEMs don't need to be in TDMRs. As you suggested, we can add some documentation to point out.

But the point we want to do some code check and prevent memory hotplug is, as Dave said, we want this piece of code to work on *ANY* TDX capable machines, including future machines which may, i.e. supports NVDIMM/CLX memory as TDX memory. If we don't do any code check in memory hotplug in this series, then when this code runs in future platforms, user can plug NVDIMM or CLX memory as system RAM thus break the assumption "all pages in page allocator are TDX memory", which eventually leads to live TDs being killed potentially.

Dave said we need to guarantee this code can work on *ANY* TDX machines. Some documentation saying it only works one some platforms and you shouldn't do things on other platforms are not good enough:

Quoted 6 lines
Quoted 1 line

https://lore.kernel.org/lkml/[email protected]/T/#m6df45b6e1702bb03dcb027044a0dabf30a86e471

Yes, the incompatible cases cannot be ignored, but I disagree that they actively need to be prevented. One way to achieve that is to explicitly enumerate TDX capable memory and document how mempolicy can be used to avoid killing TDs.

Hi Dan,

Thanks for feedback.

Could you elaborate what does "explicitly enumerate TDX capable memory" mean? How to enumerate exactly?

And for "document how mempolicy can be used to avoid killing TDs", what mempolicy (and error reporting you mentioned below) are you referring to?

I skipped to reply your below your two replies as I think they are referring to the same "enumerate" and "mempolicy" that I am asking above.

Quoted 49 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

shmem to support a new fd-based backend which doesn't require having to mmap() TD guest memory to host userspace:

https://lore.kernel.org/kvm/[email protected]/

Also, besides TD guest memory, there are some per-TD control data structures (which must be TDX memory too) need to be allocated for each TD. Normal memory allocation APIs can be used for such allocation if we guarantee all pages in page allocator is TDX memory.

Quoted 8 lines
Quoted 5 lines

You don't need that guarantee, just check it after the fact and fail if that assertion fails. It should almost always be the case that it succeeds and if it doesn't then something special is happening with that system and the end user has effectively opt-ed out of TDX operation.

This doesn't guarantee consistent behaviour. For instance, for one TD it can be created, while the second may fail. We should provide a consistent service.

Yes, there needs to be enumeration and policy knobs to avoid failures, hard coded "no memory hotplug" hacks do not seem the right enumeration and policy knobs to me.

Quoted 15 lines

The thing is anyway we need to configure some memory regions to the TDX module. To me there's no reason we don't want to guarantee all pages in page allocator are TDX memory.

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines

allocator are all TDX memory, the v3 implementation needs to always include legacy PMEMs as TDX memory so that even people truly add legacy PMEMs as system RAM, we can still guarantee all pages in page allocator are TDX memory.

Why?

If we don't include legacy PMEMs as TDX memory, then after they are hot-added as system RAM using kmem driver, the assumption of "all pages in page allocator are TDX memory" is broken. A TD can be killed during runtime.

Quoted 6 lines
Quoted 4 lines

Yes, that is what the end user asked for. If they don't want that to happen then the policy decision about using kmem needs to be updated in userspace, not hard code that policy decision towards TDX inside the kernel.

This is also fine to me. But please also see above Dave's comment.

Dave is right, the implementation can not just ignore the conflict. To me, enumeration plus error reporting allows for flexibility without hard coding policy in the kernel.

Signature · 2 lines
Thanks,
-Kai
#138 lore raw
In reply to Kai Huang
Quoted 60 lines · Kai Huang
Quoted 51 lines · Mike Rapoport
Quoted 38 lines · Dan Williams

[ add Mike ]

On Thu, May 5, 2022 at 2:54 AM Kai Huang <[email protected]> wrote:
[..]

Quoted 10 lines

Hi Dave,

Sorry to ping (trying to close this).

Given we don't need to consider kmem-hot-add legacy PMEM after TDX module initialization, I think for now it's totally fine to exclude legacy PMEMs from TDMRs. The worst case is when someone tries to use them as TD guest backend directly, the TD will fail to create. IMO it's acceptable, as it is supposedly that no one should just use some random backend to run TD.

The platform will already do this, right? I don't understand why this is trying to take proactive action versus documenting the error conditions and steps someone needs to take to avoid unconvertible memory. There is already the CONFIG_HMEM_REPORTING that describes relative performance properties between initiators and targets, it seems fitting to also add security properties between initiators and targets so someone can enumerate the numa-mempolicy that avoids unconvertible memory.

No, special casing in hotplug code paths needed.

Quoted 12 lines

I think w/o needing to include legacy PMEM, it's better to get all TDX memory blocks based on memblock, but not e820. The pages managed by page allocator are from memblock anyway (w/o those from memory hotplug).

And I also think it makes more sense to introduce 'tdx_memblock' and 'tdx_memory' data structures to gather all TDX memory blocks during boot when memblock is still alive. When TDX module is initialized during runtime, TDMRs can be created based on the 'struct tdx_memory' which contains all TDX memory blocks we gathered based on memblock during boot. This is also more flexible to support other TDX memory from other sources such as CLX memory in the future.

Quoted 6 lines
Quoted 1 line

Please let me know if you have any objection? Thanks!

It's already the case that x86 maintains sideband structures to preserve memory after exiting the early memblock code. Mike, correct me if I am wrong, but adding more is less desirable than just keeping the memblock around?

TBH, I didn't read the entire thread yet, but at the first glance, keeping memblock around is much more preferable that adding yet another { .start, .end, .flags } data structure. To keep memblock after boot all is needed is something like

Quoted 5 lines

select ARCH_KEEP_MEMBLOCK if INTEL_TDX_HOST

I'll take a closer look next week on the entire series, maybe I'm missing some details.

Hi Mike,

Quoted 7 lines

Thanks for feedback.

Perhaps I haven't put a lot details of the new TDX data structures, so let me point out that the new two data structures 'struct tdx_memblock' and 'struct tdx_memory' that I am proposing are mostly supposed to be used by TDX code only, which is pretty standalone. They are not supposed to be some basic infrastructure that can be widely used by other random kernel components.

We already have "pretty standalone" numa_meminfo that originally was used to setup NUMA memory topology, but now it's used by other code as well. And e820 tables also contain similar data and they are supposedly should be used only at boot time, but in reality there are too much callbacks into e820 way after the system is booted.

So any additional memory representation will only add to the overall complexity and well have even more "eventually consistent" collections of { .start, .end, .flags } structures.

Quoted 6 lines

In fact, currently the only operation we need is to allow memblock to register all memory regions as TDX memory blocks when the memblock is still alive. Therefore, in fact, the new data structures can even be completely invisible to other kernel components. For instance, TDX code can provide below API w/o exposing any data structures to other kernel components:

Quoted 8 lines

int tdx_add_memory_block(phys_addr_t start, phys_addr_t end, int nid);

And we call above API for each memory region in memblock when it is alive.

TDX code internally manages those memory regions via the new data structures that I mentioned above, so we don't need to keep memblock after boot. The advantage of this approach is it is more flexible to support other potential TDX memory resources (such as CLX memory) in the future.

Please let keep things simple. If other TDX memory resources will need different handling it can be implemented then. For now, just enable ARCH_KEEP_MEMBLOCK and use memblock to track TDX memory.

Quoted 8 lines

Otherwise, we can do as you suggested to select ARCH_KEEP_MEMBLOCK when INTEL_TDX_HOST is on and TDX code internally uses memblock API directly.

Signature · 2 lines
Thanks,
-Kai
Signature · 2 lines
Sincerely yours,
Mike.
author
#139 lore raw
Quoted 31 lines
Quoted 10 lines

Hi Mike,

Thanks for feedback.

Perhaps I haven't put a lot details of the new TDX data structures, so let me point out that the new two data structures 'struct tdx_memblock' and 'struct tdx_memory' that I am proposing are mostly supposed to be used by TDX code only, which is pretty standalone. They are not supposed to be some basic infrastructure that can be widely used by other random kernel components.

We already have "pretty standalone" numa_meminfo that originally was used to setup NUMA memory topology, but now it's used by other code as well. And e820 tables also contain similar data and they are supposedly should be used only at boot time, but in reality there are too much callbacks into e820 way after the system is booted.

So any additional memory representation will only add to the overall complexity and well have even more "eventually consistent" collections of { .start, .end, .flags } structures.

Quoted 10 lines

In fact, currently the only operation we need is to allow memblock to register all memory regions as TDX memory blocks when the memblock is still alive. Therefore, in fact, the new data structures can even be completely invisible to other kernel components. For instance, TDX code can provide below API w/o exposing any data structures to other kernel components:

int tdx_add_memory_block(phys_addr_t start, phys_addr_t end, int nid);

And we call above API for each memory region in memblock when it is alive.

Quoted 9 lines
Quoted 4 lines

TDX code internally manages those memory regions via the new data structures that I mentioned above, so we don't need to keep memblock after boot. The advantage of this approach is it is more flexible to support other potential TDX memory resources (such as CLX memory) in the future.

Please let keep things simple. If other TDX memory resources will need different handling it can be implemented then. For now, just enable ARCH_KEEP_MEMBLOCK and use memblock to track TDX memory.

Looks good to me. Thanks for the feedback.

Signature · 2 lines
Thanks,
-Kai
author
#135 lore raw
In reply to Kai Huang
Quoted 42 lines
Quoted 31 lines
Quoted 31 lines
Quoted 10 lines

Consider the fact that end users can break the kernel by specifying invalid memmap= command line options. The memory hotplug code does not take any steps to add safety in those cases because there are already too many ways it can go wrong. TDX is just one more corner case where the memmap= user needs to be careful. Otherwise, it is up to the platform firmware to make sure everything in the base memory map is TDX capable, and then all you need is documentation about the failure mode when extending "System RAM" beyond that baseline.

So the fact is, if we don't include legacy PMEMs into TDMRs, and don't do anything in memory hotplug, then if user does kmem-hot-add legacy PMEMs as system RAM, a live TD may eventually be killed.

If such case is a corner case that we don't need to guarantee, then even better. And we have an additional reason that those legacy PMEMs don't need to be in TDMRs. As you suggested, we can add some documentation to point out.

But the point we want to do some code check and prevent memory hotplug is, as Dave said, we want this piece of code to work on *ANY* TDX capable machines, including future machines which may, i.e. supports NVDIMM/CLX memory as TDX memory. If we don't do any code check in memory hotplug in this series, then when this code runs in future platforms, user can plug NVDIMM or CLX memory as system RAM thus break the assumption "all pages in page allocator are TDX memory", which eventually leads to live TDs being killed potentially.

Dave said we need to guarantee this code can work on *ANY* TDX machines. Some documentation saying it only works one some platforms and you shouldn't do things on other platforms are not good enough:

Quoted 6 lines
Quoted 1 line

https://lore.kernel.org/lkml/[email protected]/T/#m6df45b6e1702bb03dcb027044a0dabf30a86e471

Yes, the incompatible cases cannot be ignored, but I disagree that they actively need to be prevented. One way to achieve that is to explicitly enumerate TDX capable memory and document how mempolicy can be used to avoid killing TDs.

Hi Dan,

Thanks for feedback.

Quoted 10 lines

Could you elaborate what does "explicitly enumerate TDX capable memory" mean? How to enumerate exactly?

And for "document how mempolicy can be used to avoid killing TDs", what mempolicy (and error reporting you mentioned below) are you referring to?

I skipped to reply your below your two replies as I think they are referring to the same "enumerate" and "mempolicy" that I am asking above.

Hi Dan,

I guess "explicitly enumerate TDX capable memory" means getting the Convertible Memory Regions (CMR). And "document how mempolicy can be used to avoid killing TDs" means we say something like below in the documentation?

Any non TDX capable memory hot-add will result in non TDX capable pages being potentially allocated to a TD, in which case a TD may fail to be created or a live TD may be killed at runtime.

And "error reporting" do you mean in memory hot-add code path, we check whether the new memory resource is TDX capable, if not we print some error similar to above message in documentation, but still allow the memory hot-add to happen?

Something like below in add_memory_resource()?

	if (platform_has_tdx() && new memory resource NOT in CMRs)
		pr_err("Hot-add non-TDX memory on TDX capable system. TD may
			fail to be created, or a live TD may be killed during
			runtime.\n");

	// allow memory hot-add anyway

I have below concerns of this approach:

1) I think we should provide a consistent service to user, that is, we either to guarantee that TD won't be failed to be created randomly and a running TD won't be killed during runtime, or we don't provide any TDX functionality at all. So I am not sure only "document how mempolicy can be use to avoid killing TDs" is good enough.

2) Above code to check whether a new memory resource is in CMRs or not requires the kernel to get CMRs during kernel boot. However getting CMRs requires calling SEAMCALL which requires kernel to support VMXON/VMXOFF. VMXON/VMXOFF is currently only handled by KVM. We'd like to avoid adding VMXON/VMXOFF to core- kernel now if not mandatory, as eventually we will very likely need to have a reference-based approach to call VMXON/VMXOFF. This part is explained in the cover letter in this series.

Dave suggested for now to keep things simple, we can use "winner take all"
approach: If TDX is initialized first, don't allow memory hotplug. If memory hotplug happens first, don't allow TDX to be initialized.

https://lore.kernel.org/lkml/[email protected]/T/#mfa6b5dcc536d8a7b78522f46ccd1230f84d52ae0

I think this is perhaps more reasonable as we are at least providing some consistent service to user. And in this approach we don't need to handle VMXON/VMXOFF in core-kernel.

Comments?

Signature · 2 lines
Thanks,
-Kai
#53 lore raw

Re: [PATCH v3 06/21] x86/virt/tdx: Shut down TDX module in case of error

In reply to Kai Huang
Quoted 27 lines · Kai Huang
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Kai Huang

TDX supports shutting down the TDX module at any time during its lifetime. After TDX module is shut down, no further SEAMCALL can be made on any logical cpu.

Is this strictly true?

I thought SEAMCALLs were used for the P-SEAMLDR too.

Sorry will change to no TDX module SEAMCALL can be made on any logical cpu.

[...]

Quoted 12 lines
Quoted 12 lines
arch/x86/virt/vmx/tdx/tdx.c +11 -0
+/* Data structure to make SEAMCALL on multiple CPUs concurrently */
+struct seamcall_ctx {
+   u64 fn;
+   u64 rcx;
+   u64 rdx;
+   u64 r8;
+   u64 r9;
+   atomic_t err;
+   u64 seamcall_ret;
+   struct tdx_module_output out;
+};
Quoted 8 lines
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static int seamcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, · line 351

Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c +8 -0
+
+static void seamcall_smp_call_function(void *data)
+{
+   struct seamcall_ctx *sc = data;
+   int ret;
+
+   ret = seamcall(sc->fn, sc->rcx, sc->rdx, sc->r8, sc->r9,
+                   &sc->seamcall_ret, &sc->out);

Are the seamcall_ret and out fields in seamcall_ctx going to be used? Right now it looks like no one is going to read them.
If they are going to be used then this is going to cause a race since the different CPUs are going to write concurrently to the same address inside seamcall().
We should either use local memory and write using atomic_set like the case for the err field or hard code NULL at the call site if they are not going to be used.

Quoted 18 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/virt/vmx/tdx/tdx.c +7 -0
+   if (ret)
+           atomic_set(&sc->err, ret);
+}
+
+/*
+ * Call the SEAMCALL on all online cpus concurrently.
+ * Return error if SEAMCALL fails on any cpu.
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static int seamcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, · line 364

Quoted 6 lines

+ */

arch/x86/virt/vmx/tdx/tdx.c +5 -0
+static int seamcall_on_each_cpu(struct seamcall_ctx *sc)
+{
+   on_each_cpu(seamcall_smp_call_function, sc, true);
+   return atomic_read(&sc->err);
+}

Why bother returning something that's not read?

It's not needed. I'll make it void.

Quoted 10 lines

Caller can check seamcall_ctx::err directly if they want to know whether any error happened.

--
Thanks,
-Kai

Sagi

#73 lore raw

Re: [PATCH v3 09/21] x86/virt/tdx: Get information about TDX module and convertible memory

In reply to Kai Huang
Quoted 215 lines · Kai Huang
Quoted 34 lines · Dave Hansen
Quoted 33 lines · Kai Huang

TDX provides increased levels of memory confidentiality and integrity. This requires special hardware support for features like memory encryption and storage of memory integrity checksums. Not all memory satisfies these requirements.

As a result, TDX introduced the concept of a "Convertible Memory Region" (CMR). During boot, the firmware builds a list of all of the memory ranges which can provide the TDX security guarantees. The list of these ranges, along with TDX module information, is available to the kernel by querying the TDX module via TDH.SYS.INFO SEAMCALL.

Host kernel can choose whether or not to use all convertible memory regions as TDX memory. Before TDX module is ready to create any TD guests, all TDX memory regions that host kernel intends to use must be configured to the TDX module, using specific data structures defined by TDX architecture. Constructing those structures requires information of both TDX module and the Convertible Memory Regions. Call TDH.SYS.INFO to get this information as preparation to construct those structures.

---
 arch/x86/virt/vmx/tdx/tdx.c | 131 ++++++++++++++++++++++++++++++++++++
 arch/x86/virt/vmx/tdx/tdx.h |  61 +++++++++++++++++
 2 files changed, 192 insertions(+)
arch/x86/virt/vmx/tdx/tdx.c +0 -0
index ef2718423f0f..482e6d858181 100644
@@ -80,6 +80,11 @@ static DEFINE_MUTEX(tdx_module_lock);
 
 static struct p_seamldr_info p_seamldr_info;
 
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static DEFINE_MUTEX(tdx_module_lock); · line 86

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4 -0
+/* Base address of CMR array needs to be 512 bytes aligned. */
+static struct cmr_info tdx_cmr_array[MAX_CMRS] __aligned(CMR_INFO_ARRAY_ALIGNMENT);
+static int tdx_cmr_num;
+static struct tdsysinfo_struct tdx_sysinfo;

I really dislike mixing hardware and software structures. Please make it clear which of these are fully software-defined and which are part of the hardware ABI.

Both 'struct tdsysinfo_struct' and 'struct cmr_info' are hardware structures. They are defined in tdx.h which has a comment saying the data structures below this comment is hardware structures:

+/*
+ * TDX architectural data structures
+ */

It is introduced in the P-SEAMLDR patch.

Should I explicitly add comments around the variables saying they are used by hardware, something like:

/*
* Data structures used by TDH.SYS.INFO SEAMCALL to return CMRs and
* TDX module system information.
*/

?

Quoted 54 lines
Quoted 54 lines
arch/x86/virt/vmx/tdx/tdx.c +46 -0
 static bool __seamrr_enabled(void)
 {
    return (seamrr_mask & SEAMRR_ENABLED_BITS) == SEAMRR_ENABLED_BITS;
@@ -468,6 +473,127 @@ static int tdx_module_init_cpus(void)
    return seamcall_on_each_cpu(&sc);
 }
 
+static inline bool cmr_valid(struct cmr_info *cmr)
+{
+   return !!cmr->size;
+}
+
+static void print_cmrs(struct cmr_info *cmr_array, int cmr_num,
+                  const char *name)
+{
+   int i;
+
+   for (i = 0; i < cmr_num; i++) {
+           struct cmr_info *cmr = &cmr_array[i];
+
+           pr_info("%s : [0x%llx, 0x%llx)\n", name,
+                           cmr->base, cmr->base + cmr->size);
+   }
+}
+
+static int sanitize_cmrs(struct cmr_info *cmr_array, int cmr_num)
+{
+   int i, j;
+
+   /*
+    * Intel TDX module spec, 20.7.3 CMR_INFO:
+    *
+    *   TDH.SYS.INFO leaf function returns a MAX_CMRS (32) entry
+    *   array of CMR_INFO entries. The CMRs are sorted from the
+    *   lowest base address to the highest base address, and they
+    *   are non-overlapping.
+    *
+    * This implies that BIOS may generate invalid empty entries
+    * if total CMRs are less than 32.  Skip them manually.
+    */
+   for (i = 0; i < cmr_num; i++) {
+           struct cmr_info *cmr = &cmr_array[i];
+           struct cmr_info *prev_cmr = NULL;
+
+           /* Skip further invalid CMRs */
+           if (!cmr_valid(cmr))
+                   break;
+
+           if (i > 0)
+                   prev_cmr = &cmr_array[i - 1];
+
+           /*
+            * It is a TDX firmware bug if CMRs are not
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static int tdx_module_init_cpus(void) · line 527

Quoted 6 lines
arch/x86/virt/vmx/tdx/tdx.c +6 -0
+            * in address ascending order.
+            */
+           if (prev_cmr && ((prev_cmr->base + prev_cmr->size) >
+                                   cmr->base)) {
+                   pr_err("Firmware bug: CMRs not in address ascending order.\n");
+                   return -EFAULT;

-EFAULT is a really weird return code to use for this. I'd use -EINVAL.

OK thanks.

Quoted 19 lines
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c +7 -0
+           }
+   }
+
+   /*
+    * Also a sane BIOS should never generate invalid CMR(s) between
+    * two valid CMRs.  Sanity check this and simply return error in
+    * this case.

arch/x86/virt/vmx/tdx/tdx.c static int tdx_module_init_cpus(void) · line 544

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+    *
+    * By reaching here @i is the index of the first invalid CMR (or
+    * cmr_num).  Starting with next entry of @i since it has already
+    * been checked.
+    */
+   for (j = i + 1; j < cmr_num; j++)
+           if (cmr_valid(&cmr_array[j])) {
+                   pr_err("Firmware bug: invalid CMR(s) among valid CMRs.\n");
+                   return -EFAULT;
+           }
Quoted 1 line

Please add brackets for the for().

OK.

Quoted 40 lines
Quoted 27 lines
arch/x86/virt/vmx/tdx/tdx.c +26 -0
+   /*
+    * Trim all tail invalid empty CMRs.  BIOS should generate at
+    * least one valid CMR, otherwise it's a TDX firmware bug.
+    */
+   tdx_cmr_num = i;
+   if (!tdx_cmr_num) {
+           pr_err("Firmware bug: No valid CMR.\n");
+           return -EFAULT;
+   }
+
+   /* Print kernel sanitized CMRs */
+   print_cmrs(tdx_cmr_array, tdx_cmr_num, "Kernel-sanitized-CMR");
+
+   return 0;
+}
+
+static int tdx_get_sysinfo(void)
+{
+   struct tdx_module_output out;
+   u64 tdsysinfo_sz, cmr_num;
+   int ret;
+
+   BUILD_BUG_ON(sizeof(struct tdsysinfo_struct) != TDSYSINFO_STRUCT_SIZE);
+
+   ret = seamcall(TDH_SYS_INFO, __pa(&tdx_sysinfo), TDSYSINFO_STRUCT_SIZE,
+                   __pa(tdx_cmr_array), MAX_CMRS, NULL, &out);

arch/x86/virt/vmx/tdx/tdx.c static int tdx_module_init_cpus(void) · line 581

Quoted 10 lines
arch/x86/virt/vmx/tdx/tdx.c +10 -0
+   if (ret)
+           return ret;
+
+   /*
+    * If TDH.SYS.CONFIG succeeds, RDX contains the actual bytes
+    * written to @tdx_sysinfo and R9 contains the actual entries
+    * written to @tdx_cmr_array.  Sanity check them.
+    */
+   tdsysinfo_sz = out.rdx;
+   cmr_num = out.r9;

Please vertically align things like this:

Quoted 2 lines
      tdsysinfo_sz = out.rdx;
      cmr_num      = out.r9;

OK.

Quoted 5 lines

arch/x86/virt/vmx/tdx/tdx.c static int tdx_module_init_cpus(void) · line 584

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +3 -0
+   if (WARN_ON_ONCE((tdsysinfo_sz > sizeof(tdx_sysinfo)) || !tdsysinfo_sz ||
+                           (cmr_num > MAX_CMRS) || !cmr_num))
+           return -EFAULT;
Quoted 8 lines

Sanity checking is good, but this makes me wonder how much is too much.
I don't see a lot of code for instance checking if sys_write() writes
more than how much it was supposed to.

Why are these sanity checks necessary here? Is the TDX module expected to be *THAT* buggy? The thing that's providing, oh, basically all of the security guarantees of this architecture. It's overflowing the buffers you hand it?

I think this check can be removed. Will remove.

Quoted 1 line
Quoted 8 lines
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static int tdx_module_init_cpus(void) · line 593

Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c +8 -0
+   pr_info("TDX module: vendor_id 0x%x, major_version %u, minor_version %u, build_date %u, build_num %u",
+           tdx_sysinfo.vendor_id, tdx_sysinfo.major_version,
+           tdx_sysinfo.minor_version, tdx_sysinfo.build_date,
+           tdx_sysinfo.build_num);
+
+   /* Print BIOS provided CMRs */
+   print_cmrs(tdx_cmr_array, cmr_num, "BIOS-CMR");
+

sanitize_cmrs already prints the cmrs in case of success. So for valid cmrs we are going to print them twice.
Would it be better to only print cmrs here in case sanitize_cmrs fails?

Quoted 32 lines
Quoted 7 lines

arch/x86/virt/vmx/tdx/tdx.c static int tdx_module_init_cpus(void) · line 595

Quoted 2 lines
arch/x86/virt/vmx/tdx/tdx.c +2 -0
+   return sanitize_cmrs(tdx_cmr_array, cmr_num);
+}

Does sanitize_cmrs() sanitize anything? It looks to me like it *checks* the CMRs. But, sanitizing is an active operation that writes to the data being sanitized. This looks read-only to me. check_cmrs() would be a better name for a passive check.

Sure will change to check_cmrs().

Quoted 8 lines
Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c +0 -0
 static int init_tdx_module(void)
 {
    int ret;
@@ -482,6 +608,11 @@ static int init_tdx_module(void)
    if (ret)
            goto out;
 
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static int init_tdx_module(void) · line 614

Quoted 4 lines
arch/x86/virt/vmx/tdx/tdx.c +4 -0
+   /* Get TDX module information and CMRs */
+   ret = tdx_get_sysinfo();
+   if (ret)
+           goto out;

Couldn't we get rid of that comment if you did something like:

      ret = tdx_get_sysinfo(&tdx_cmr_array, &tdx_sysinfo);

Yes will do.

Quoted 2 lines

and preferably make the variables function-local.

Quoted 9 lines

'tdx_sysinfo' will be used by KVM too.

--
Thanks,
-Kai

Sagi

author
#54 lore raw

Re: [PATCH v3 06/21] x86/virt/tdx: Shut down TDX module in case of error

In reply to Sagi Shahar
Quoted 37 lines · Sagi Shahar
Quoted 27 lines · Kai Huang
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Kai Huang

TDX supports shutting down the TDX module at any time during its lifetime. After TDX module is shut down, no further SEAMCALL can be made on any logical cpu.

Is this strictly true?

I thought SEAMCALLs were used for the P-SEAMLDR too.

Sorry will change to no TDX module SEAMCALL can be made on any logical cpu.

[...]

Quoted 12 lines
Quoted 12 lines
arch/x86/virt/vmx/tdx/tdx.c +11 -0
+/* Data structure to make SEAMCALL on multiple CPUs concurrently */
+struct seamcall_ctx {
+   u64 fn;
+   u64 rcx;
+   u64 rdx;
+   u64 r8;
+   u64 r9;
+   atomic_t err;
+   u64 seamcall_ret;
+   struct tdx_module_output out;
+};
Quoted 8 lines
Quoted 8 lines

arch/x86/virt/vmx/tdx/tdx.c static int seamcall(u64 fn, u64 rcx, u64 rdx, u64 r8, u64 r9, · line 351

Quoted 8 lines
arch/x86/virt/vmx/tdx/tdx.c +8 -0
+
+static void seamcall_smp_call_function(void *data)
+{
+   struct seamcall_ctx *sc = data;
+   int ret;
+
+   ret = seamcall(sc->fn, sc->rcx, sc->rdx, sc->r8, sc->r9,
+                   &sc->seamcall_ret, &sc->out);
Quoted 9 lines

Are the seamcall_ret and out fields in seamcall_ctx going to be used? Right now it looks like no one is going to read them.
If they are going to be used then this is going to cause a race since the different CPUs are going to write concurrently to the same address inside seamcall().
We should either use local memory and write using atomic_set like the case for the err field or hard code NULL at the call site if they are not going to be used.

Quoted 1 line
Quoted 1 line
Quoted 1 line

Thanks for catching this. Both 'seamcall_ret' and 'out' are actually not used in this series, but this needs to be improved for sure.

I think I can just remove them from the 'seamcall_ctx' for now, since they are not used at all.

Signature · 2 lines
Thanks,
-Kai
author
#74 lore raw

Re: [PATCH v3 09/21] x86/virt/tdx: Get information about TDX module and convertible memory

In reply to Sagi Shahar
Quoted 14 lines · Sagi Shahar
Quoted 10 lines
Quoted 10 lines

arch/x86/virt/vmx/tdx/tdx.c static int tdx_module_init_cpus(void) · line 593

Quoted 10 lines

+   pr_info("TDX module: vendor_id 0x%x, major_version %u, minor_version %u, build_date %u, build_num %u",
+           tdx_sysinfo.vendor_id, tdx_sysinfo.major_version, +           tdx_sysinfo.minor_version, tdx_sysinfo.build_date, +           tdx_sysinfo.build_num);

arch/x86/virt/vmx/tdx/tdx.c +4 -0
+
+   /* Print BIOS provided CMRs */
+   print_cmrs(tdx_cmr_array, cmr_num, "BIOS-CMR");
+

sanitize_cmrs already prints the cmrs in case of success. So for valid cmrs we are going to print them twice.
Would it be better to only print cmrs here in case sanitize_cmrs fails?

The "BIOS-CMR" will always have 32 entries. It includes all the *empty* CMRs after the valid CMRs, so the two are different. But yes it seems there's no need to always print "BIOS-CMR".

Signature · 2 lines
Thanks,
-Kai
author
#70 lore raw

Re: [PATCH v3 09/21] x86/virt/tdx: Get information about TDX module and convertible memory

In reply to Dave Hansen
Quoted 20 lines · Dave Hansen
Quoted 19 lines · Kai Huang
Quoted 17 lines · Dave Hansen
Quoted 9 lines · Kai Huang
Quoted 3 lines

Couldn't we get rid of that comment if you did something like:

	ret = tdx_get_sysinfo(&tdx_cmr_array, &tdx_sysinfo);

Yes will do.

Quoted 1 line

and preferably make the variables function-local.

'tdx_sysinfo' will be used by KVM too.

In other words, it's not a part of this series so I can't review whether this statement is correct or whether there's a better way to hand this information over to KVM.

This (minor) nugget influencing the design also isn't even commented or addressed in the changelog.

Quoted 8 lines
Quoted 4 lines

TDSYSINFO_STRUCT is 1024B and CMR array is 512B, so I don't think it should be in the stack. I can change to use dynamic allocation at the beginning and free it at the end of the function. KVM support patches can change it to static variable in the file.

2k of stack is big, but it isn't a deal breaker for something that's not nested anywhere and that's only called once in a pretty controlled setting and not in interrupt context. I wouldn't cry about it.

Hi Dave,

I got below complaining when I use local variable for TDSYSINFO_STRUCT and CMR array:

arch/x86/virt/vmx/tdx/tdx.c:383:1: warning: the frame size of 3072 bytes is larger than 1024 bytes [-Wframe-larger-than=]
383 | }

So I don't think we can use local variable for them. I'll still use static variables to avoid dynamic allocation. In the commit message, I'll explain they are too big to put into the stack, and KVM will need to use TDSYSINFO_STRUCT reported by TDX module anyway.

Let me know if you don't agree?

author
#71 lore raw

Re: [PATCH v3 09/21] x86/virt/tdx: Get information about TDX module and convertible memory

Quoted 39 lines · Kai Huang
Quoted 20 lines · Dave Hansen
Quoted 19 lines · Kai Huang
Quoted 17 lines · Dave Hansen
Quoted 9 lines · Kai Huang
Quoted 3 lines

Couldn't we get rid of that comment if you did something like:

	ret = tdx_get_sysinfo(&tdx_cmr_array, &tdx_sysinfo);

Yes will do.

Quoted 1 line

and preferably make the variables function-local.

'tdx_sysinfo' will be used by KVM too.

In other words, it's not a part of this series so I can't review whether this statement is correct or whether there's a better way to hand this information over to KVM.

This (minor) nugget influencing the design also isn't even commented or addressed in the changelog.

Quoted 8 lines
Quoted 4 lines

TDSYSINFO_STRUCT is 1024B and CMR array is 512B, so I don't think it should be in the stack. I can change to use dynamic allocation at the beginning and free it at the end of the function. KVM support patches can change it to static variable in the file.

2k of stack is big, but it isn't a deal breaker for something that's not nested anywhere and that's only called once in a pretty controlled setting and not in interrupt context. I wouldn't cry about it.

Hi Dave,

I got below complaining when I use local variable for TDSYSINFO_STRUCT and CMR array:

arch/x86/virt/vmx/tdx/tdx.c:383:1: warning: the frame size of 3072 bytes is larger than 1024 bytes [-Wframe-larger-than=]
383 | }

Quoted 6 lines

So I don't think we can use local variable for them. I'll still use static variables to avoid dynamic allocation. In the commit message, I'll explain they are too big to put into the stack, and KVM will need to use TDSYSINFO_STRUCT reported by TDX module anyway.

Let me know if you don't agree?

Btw, CMR array alone can be put into the stack. It will never be used by KVM, so I'll put CMR array as local variable, but keep tdx_sysinfo as static variable.

Signature · 2 lines
Thanks,
-Kai
author
#72 lore raw

Re: [PATCH v3 09/21] x86/virt/tdx: Get information about TDX module and convertible memory

Quoted 45 lines · Kai Huang
Quoted 44 lines · Kai Huang
Quoted 20 lines · Dave Hansen
Quoted 19 lines · Kai Huang
Quoted 17 lines · Dave Hansen
Quoted 9 lines · Kai Huang
Quoted 3 lines

Couldn't we get rid of that comment if you did something like:

	ret = tdx_get_sysinfo(&tdx_cmr_array, &tdx_sysinfo);

Yes will do.

Quoted 1 line

and preferably make the variables function-local.

'tdx_sysinfo' will be used by KVM too.

In other words, it's not a part of this series so I can't review whether this statement is correct or whether there's a better way to hand this information over to KVM.

This (minor) nugget influencing the design also isn't even commented or addressed in the changelog.

Quoted 8 lines
Quoted 4 lines

TDSYSINFO_STRUCT is 1024B and CMR array is 512B, so I don't think it should be in the stack. I can change to use dynamic allocation at the beginning and free it at the end of the function. KVM support patches can change it to static variable in the file.

2k of stack is big, but it isn't a deal breaker for something that's not nested anywhere and that's only called once in a pretty controlled setting and not in interrupt context. I wouldn't cry about it.

Hi Dave,

I got below complaining when I use local variable for TDSYSINFO_STRUCT and CMR array:

arch/x86/virt/vmx/tdx/tdx.c:383:1: warning: the frame size of 3072 bytes is larger than 1024 bytes [-Wframe-larger-than=]
383 | }

So I don't think we can use local variable for them. I'll still use static variables to avoid dynamic allocation. In the commit message, I'll explain they are too big to put into the stack, and KVM will need to use TDSYSINFO_STRUCT reported by TDX module anyway.

Quoted 6 lines
Quoted 1 line

Let me know if you don't agree?

Btw, CMR array alone can be put into the stack. It will never be used by KVM, so I'll put CMR array as local variable, but keep tdx_sysinfo as static variable.

Sorry for multiple emails about this. If I put CMR array to the stack, I still sometimes get the build warning. So will use static variables.

Also, constructing TDMRs internally needs to use tdx_sysinfo (max_tdmrs, pamt_entry_size, max_rsvd_per_tdmr), so with static variable they don't need to be passed around as function arguments.

Signature · 2 lines
Thanks,
-Kai