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
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
author
#126 lore raw
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.

author
#134 lore raw
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
author
#135 lore raw
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
#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
#138 lore raw
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
#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
#142 lore raw
In reply to Dave Hansen

On Tue, Apr 26, 2022 at 1:10 PM Dave Hansen <[email protected]> wrote:
[..]

Quoted 8 lines
Quoted 8 lines

3. Memory hotplug

The first generation of TDX architecturally doesn't support memory hotplug. And the first generation of TDX-capable platforms don't support physical memory hotplug. Since it physically cannot happen, this series doesn't add any check in ACPI memory hotplug code path to disable it.

A special case of memory hotplug is adding NVDIMM as system RAM using

Saw "NVDIMM" mentioned while browsing this, so stopped to make a comment...

Quoted 6 lines
Quoted 3 lines

kmem driver. However the first generation of TDX-capable platforms cannot enable TDX and NVDIMM simultaneously, so in practice this cannot happen either.

What prevents this code from today's code being run on tomorrow's platforms and breaking these assumptions?

The assumption is already broken today with NVDIMM-N. The lack of DDR-T support on TDX enabled platforms has zero effect on DDR-based persistent memory solutions. In other words, please describe the actual software and hardware conflicts at play here, and do not make the mistake of assuming that "no DDR-T support on TDX platforms" == "no NVDIMM support".

Quoted 5 lines
Quoted 5 lines

Another case is admin can use 'memmap' kernel command line to create legacy PMEMs and use them as TD guest memory, or theoretically, can use kmem driver to add them as system RAM. To avoid having to change memory hotplug code to prevent this from happening, this series always include legacy PMEMs when constructing TDMRs so they are also TDX memory.

I am not sure what you are trying to say here?

Quoted 7 lines
Quoted 7 lines

4. CPU hotplug

The first generation of TDX architecturally doesn't support ACPI CPU hotplug. All logical cpus are enabled by BIOS in MADT table. Also, the first generation of TDX-capable platforms don't support ACPI CPU hotplug either. Since this physically cannot happen, this series doesn't add any check in ACPI CPU hotplug code path to disable it.

What are the actual challenges posed to TDX with respect to CPU hotplug?

Quoted 1 line
Quoted 1 line

Also, only TDX module initialization requires all BIOS-enabled cpus are

Please define "BIOS-enabled" cpus. There is no "BIOS-enabled" line in /proc/cpuinfo for example.

author
#143 lore raw
Quoted 20 lines · Dan Williams

On Tue, Apr 26, 2022 at 1:10 PM Dave Hansen <[email protected]> wrote:
[..]

Quoted 8 lines
Quoted 8 lines

3. Memory hotplug

The first generation of TDX architecturally doesn't support memory hotplug. And the first generation of TDX-capable platforms don't support physical memory hotplug. Since it physically cannot happen, this series doesn't add any check in ACPI memory hotplug code path to disable it.

A special case of memory hotplug is adding NVDIMM as system RAM using

Saw "NVDIMM" mentioned while browsing this, so stopped to make a comment...

Quoted 6 lines
Quoted 3 lines

kmem driver. However the first generation of TDX-capable platforms cannot enable TDX and NVDIMM simultaneously, so in practice this cannot happen either.

What prevents this code from today's code being run on tomorrow's platforms and breaking these assumptions?

Quoted 6 lines

The assumption is already broken today with NVDIMM-N. The lack of DDR-T support on TDX enabled platforms has zero effect on DDR-based persistent memory solutions. In other words, please describe the actual software and hardware conflicts at play here, and do not make the mistake of assuming that "no DDR-T support on TDX platforms" == "no NVDIMM support".

Sorry I got this information from planning team or execution team I guess. I was told NVDIMM and TDX cannot "co-exist" on the first generation of TDX capable machine. "co-exist" means they cannot be turned on simultaneously on the same platform. I am also not aware NVDIMM-N, nor the difference between DDR based and DDR-T based persistent memory. Could you give some more background here so I can take a look?

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

Another case is admin can use 'memmap' kernel command line to create legacy PMEMs and use them as TD guest memory, or theoretically, can use kmem driver to add them as system RAM. To avoid having to change memory hotplug code to prevent this from happening, this series always include legacy PMEMs when constructing TDMRs so they are also TDX memory.

I am not sure what you are trying to say here?

We want to always make sure the memory managed by page allocator is TDX memory. So if the legacy PMEMs are unconditionally configured as TDX memory, then we don't need to prevent them from being added as system memory via kmem driver.

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

4. CPU hotplug

The first generation of TDX architecturally doesn't support ACPI CPU hotplug. All logical cpus are enabled by BIOS in MADT table. Also, the first generation of TDX-capable platforms don't support ACPI CPU hotplug either. Since this physically cannot happen, this series doesn't add any check in ACPI CPU hotplug code path to disable it.

What are the actual challenges posed to TDX with respect to CPU hotplug?

During the TDX module initialization, there is a step to call SEAMCALL on all logical cpus to initialize per-cpu TDX staff. TDX doesn't support initializing the new hot-added CPUs after the initialization. There are MCHECK/BIOS changes to enforce this check too I guess but I don't know details about this.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Also, only TDX module initialization requires all BIOS-enabled cpus are

Please define "BIOS-enabled" cpus. There is no "BIOS-enabled" line in /proc/cpuinfo for example.

It means the CPUs with "enable" bit set in the MADT table.

Signature · 2 lines
Thanks,
-Kai
#144 lore raw
Quoted 29 lines · Kai Huang
Quoted 20 lines · Dan Williams

On Tue, Apr 26, 2022 at 1:10 PM Dave Hansen <[email protected]> wrote:
[..]

Quoted 8 lines
Quoted 8 lines

3. Memory hotplug

The first generation of TDX architecturally doesn't support memory hotplug. And the first generation of TDX-capable platforms don't support physical memory hotplug. Since it physically cannot happen, this series doesn't add any check in ACPI memory hotplug code path to disable it.

A special case of memory hotplug is adding NVDIMM as system RAM using

Saw "NVDIMM" mentioned while browsing this, so stopped to make a comment...

Quoted 6 lines
Quoted 3 lines

kmem driver. However the first generation of TDX-capable platforms cannot enable TDX and NVDIMM simultaneously, so in practice this cannot happen either.

What prevents this code from today's code being run on tomorrow's platforms and breaking these assumptions?

Quoted 6 lines

The assumption is already broken today with NVDIMM-N. The lack of DDR-T support on TDX enabled platforms has zero effect on DDR-based persistent memory solutions. In other words, please describe the actual software and hardware conflicts at play here, and do not make the mistake of assuming that "no DDR-T support on TDX platforms" == "no NVDIMM support".

Quoted 6 lines

Sorry I got this information from planning team or execution team I guess. I was told NVDIMM and TDX cannot "co-exist" on the first generation of TDX capable machine. "co-exist" means they cannot be turned on simultaneously on the same platform. I am also not aware NVDIMM-N, nor the difference between DDR based and DDR-T based persistent memory. Could you give some more background here so I can take a look?

My rough understanding is that TDX makes use of metadata communicated "on the wire" for DDR, but that infrastructure is not there for DDR-T. However, there are plenty of DDR based NVDIMMs that use super-caps / batteries and flash to save contents. I believe the concern for TDX is that the kernel needs to know not use TDX accepted PMEM as PMEM because the contents saved by the DIMM's onboard energy source are unreadable outside of a TD.

Here is one of the links that comes up in a search for NVDIMM-N.

https://www.snia.org/educational-library/what-you-can-do-nvdimm-n-and-nvdimm-p-2019

Quoted 11 lines
Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

Another case is admin can use 'memmap' kernel command line to create legacy PMEMs and use them as TD guest memory, or theoretically, can use kmem driver to add them as system RAM. To avoid having to change memory hotplug code to prevent this from happening, this series always include legacy PMEMs when constructing TDMRs so they are also TDX memory.

I am not sure what you are trying to say here?

We want to always make sure the memory managed by page allocator is TDX memory.

That only seems possible if the kernel is given a TDX capable physical address map at the beginning of time.

Quoted 2 lines

So if the legacy PMEMs are unconditionally configured as TDX memory, then we don't need to prevent them from being added as system memory via kmem driver.

I think that is too narrow of a focus.

Does a memory map exist for the physical address ranges that are TDX capable? Please don't say EFI_MEMORY_CPU_CRYPTO, as that single bit is ambiguous beyond the point of utility across the industry's entire range of confidential computing memory capabilities.

One strawman would be an ACPI table with contents like:

struct acpi_protected_memory {
struct range range;
uuid_t platform_mem_crypto_capability;
};

With some way to map those uuids to a set of platform vendor specific constraints and specifications. Some would be shared across confidential computing vendors, some might be unique. Otherwise, I do not see how you enforce the expectation of "all memory in the page allocator is TDX capable". The other alternative is that *none* of the memory in the page allocator is TDX capable and a special memory allocation device is used to map memory for TDs. In either case a map of all possible TDX memory is needed and the discussion above seems like an incomplete / "hopeful" proposal about the memory dax_kmem, or other sources, might online. See the CXL CEDT CFWMS (CXL Fixed Memory Window Structure) as an example of an ACPI table that sets the kernel's expectations about how a physical address range might be used.

https://www.computeexpresslink.org/spec-landing

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

4. CPU hotplug

The first generation of TDX architecturally doesn't support ACPI CPU hotplug. All logical cpus are enabled by BIOS in MADT table. Also, the first generation of TDX-capable platforms don't support ACPI CPU hotplug either. Since this physically cannot happen, this series doesn't add any check in ACPI CPU hotplug code path to disable it.

Quoted 6 lines
Quoted 1 line

What are the actual challenges posed to TDX with respect to CPU hotplug?

During the TDX module initialization, there is a step to call SEAMCALL on all logical cpus to initialize per-cpu TDX staff. TDX doesn't support initializing the new hot-added CPUs after the initialization. There are MCHECK/BIOS changes to enforce this check too I guess but I don't know details about this.

Is there an ACPI table that indicates CPU-x passed the check? Or since the BIOS is invoked in the CPU-online path, is it trusted to suppress those events for CPUs outside of the mcheck domain?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line

Also, only TDX module initialization requires all BIOS-enabled cpus are

Please define "BIOS-enabled" cpus. There is no "BIOS-enabled" line in /proc/cpuinfo for example.

It means the CPUs with "enable" bit set in the MADT table.

That just indicates to the present CPUs and then a hot add event changes the state of now present CPUs to enabled. Per above is the BIOS responsible for rejecting those new CPUs, or is the kernel?

author
#145 lore raw
Quoted 45 lines · Dan Williams
Quoted 29 lines · Kai Huang
Quoted 20 lines · Dan Williams

On Tue, Apr 26, 2022 at 1:10 PM Dave Hansen <[email protected]> wrote:
[..]

Quoted 8 lines
Quoted 8 lines

3. Memory hotplug

The first generation of TDX architecturally doesn't support memory hotplug. And the first generation of TDX-capable platforms don't support physical memory hotplug. Since it physically cannot happen, this series doesn't add any check in ACPI memory hotplug code path to disable it.

A special case of memory hotplug is adding NVDIMM as system RAM using

Saw "NVDIMM" mentioned while browsing this, so stopped to make a comment...

Quoted 6 lines
Quoted 3 lines

kmem driver. However the first generation of TDX-capable platforms cannot enable TDX and NVDIMM simultaneously, so in practice this cannot happen either.

What prevents this code from today's code being run on tomorrow's platforms and breaking these assumptions?

Quoted 6 lines

The assumption is already broken today with NVDIMM-N. The lack of DDR-T support on TDX enabled platforms has zero effect on DDR-based persistent memory solutions. In other words, please describe the actual software and hardware conflicts at play here, and do not make the mistake of assuming that "no DDR-T support on TDX platforms" == "no NVDIMM support".

Quoted 6 lines

Sorry I got this information from planning team or execution team I guess. I was told NVDIMM and TDX cannot "co-exist" on the first generation of TDX capable machine. "co-exist" means they cannot be turned on simultaneously on the same platform. I am also not aware NVDIMM-N, nor the difference between DDR based and DDR-T based persistent memory. Could you give some more background here so I can take a look?

My rough understanding is that TDX makes use of metadata communicated "on the wire" for DDR, but that infrastructure is not there for DDR-T. However, there are plenty of DDR based NVDIMMs that use super-caps / batteries and flash to save contents. I believe the concern for TDX is that the kernel needs to know not use TDX accepted PMEM as PMEM because the contents saved by the DIMM's onboard energy source are unreadable outside of a TD.

Quoted 3 lines

Here is one of the links that comes up in a search for NVDIMM-N.

https://www.snia.org/educational-library/what-you-can-do-nvdimm-n-and-nvdimm-p-2019

Thanks for the info. I need some more time to digest those different types of DDRs and NVDIMMs. However I guess they are not quite relevant since TDX has a concept of "Convertible Memory Region". Please see below.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

Another case is admin can use 'memmap' kernel command line to create legacy PMEMs and use them as TD guest memory, or theoretically, can use kmem driver to add them as system RAM. To avoid having to change memory hotplug code to prevent this from happening, this series always include legacy PMEMs when constructing TDMRs so they are also TDX memory.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

I am not sure what you are trying to say here?

We want to always make sure the memory managed by page allocator is TDX memory.

That only seems possible if the kernel is given a TDX capable physical address map at the beginning of time.

Yes TDX architecture has a concept "Convertible Memory Region" (CMR). The memory used by TDX must be convertible memory. BIOS generates an array of CMR entries during boot and they are verified by MCHECK. CMRs are static during machine's runtime.

Quoted 18 lines
Quoted 3 lines

So if the legacy PMEMs are unconditionally configured as TDX memory, then we don't need to prevent them from being added as system memory via kmem driver.

I think that is too narrow of a focus.

Does a memory map exist for the physical address ranges that are TDX capable? Please don't say EFI_MEMORY_CPU_CRYPTO, as that single bit is ambiguous beyond the point of utility across the industry's entire range of confidential computing memory capabilities.

One strawman would be an ACPI table with contents like:

struct acpi_protected_memory {
   struct range range;
   uuid_t platform_mem_crypto_capability;
};
Quoted 6 lines

With some way to map those uuids to a set of platform vendor specific constraints and specifications. Some would be shared across confidential computing vendors, some might be unique. Otherwise, I do not see how you enforce the expectation of "all memory in the page allocator is TDX capable".

Please see above. TDX has CMR.

Quoted 6 lines

The other alternative is that *none* of the
memory in the page allocator is TDX capable and a special memory allocation device is used to map memory for TDs. In either case a map of all possible TDX memory is needed and the discussion above seems like an incomplete / "hopeful" proposal about the memory dax_kmem, or other sources, might online.

Yes we are also developing a new memfd based approach to support TD guest memory. Please see my another reply to you.

Quoted 6 lines

See the CXL CEDT CFWMS (CXL Fixed Memory
Window Structure) as an example of an ACPI table that sets the kernel's expectations about how a physical address range might be used.

https://www.computeexpresslink.org/spec-landing

Thanks for the info. I'll take a look to get some background.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 10 lines
Quoted 10 lines

4. CPU hotplug

The first generation of TDX architecturally doesn't support ACPI CPU hotplug. All logical cpus are enabled by BIOS in MADT table. Also, the first generation of TDX-capable platforms don't support ACPI CPU hotplug either. Since this physically cannot happen, this series doesn't add any check in ACPI CPU hotplug code path to disable it.

What are the actual challenges posed to TDX with respect to CPU hotplug?

Quoted 8 lines
Quoted 4 lines

During the TDX module initialization, there is a step to call SEAMCALL on all logical cpus to initialize per-cpu TDX staff. TDX doesn't support initializing the new hot-added CPUs after the initialization. There are MCHECK/BIOS changes to enforce this check too I guess but I don't know details about this.

Is there an ACPI table that indicates CPU-x passed the check? Or since the BIOS is invoked in the CPU-online path, is it trusted to suppress those events for CPUs outside of the mcheck domain?

No the TDX module (and the P-SEAMLDR) internally maintains some data to record the total number of LPs and packages, and which logical cpu has been initialized, etc.

I asked Intel guys whether BIOS would suppress an ACPI CPU hotplug event but I never got a concrete answer. I'll try again.

Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Also, only TDX module initialization requires all BIOS-enabled cpus are

Please define "BIOS-enabled" cpus. There is no "BIOS-enabled" line in /proc/cpuinfo for example.

It means the CPUs with "enable" bit set in the MADT table.

That just indicates to the present CPUs and then a hot add event changes the state of now present CPUs to enabled. Per above is the BIOS responsible for rejecting those new CPUs, or is the kernel?

I'll ask BIOS guys again to see whether BIOS will suppress ACPI CPU hotplug event. But I think we can have a simple patch to reject ACPI CPU hotplug if platform is TDX-capable?

Or do you think we don't need to explicitly reject ACPI CPU hotplug if we can confirm with BIOS guys that it will suppress on TDX capable machine?

Signature · 2 lines
Thanks,
-Kai
#146 lore raw
In reply to Dan Williams
Quoted 2 lines · Dan Williams

That only seems possible if the kernel is given a TDX capable physical address map at the beginning of time.

TDX actually brings along its own memory map. The "EAS"[1]. has a lot of info on it, if you know where to find it. Here's the relevant chunk:

CMR - Convertible Memory Range -
A range of physical memory configured by BIOS and verified by MCHECK. MCHECK verificatio n is intended to help ensure that a CMR may be used to hold TDX memory pages encrypted with a private HKID.

So, the BIOS has the platform knowledge to enumerate this range. It stashes the information off somewhere that the TDX module can find it. Then, during OS boot, the OS makes a SEAMCALL (TDH.SYS.CONFIG) to the TDX module and gets the list of CMRs.

The OS then has to reconcile this CMR "memory map" against the regular old BIOS-provided memory map, tossing out any memory regions which are RAM, but not covered by a CMR, or disabling TDX entirely.

Fun, eh?

I'm still grappling with how this series handles the policy of what memory to throw away when.

1.
https://www.intel.com/content/dam/develop/external/us/en/documents/intel-tdx-module-1eas.pdf

#147 lore raw
Quoted 19 lines · Dave Hansen
Quoted 2 lines · Dan Williams

That only seems possible if the kernel is given a TDX capable physical address map at the beginning of time.

TDX actually brings along its own memory map. The "EAS"[1]. has a lot of info on it, if you know where to find it. Here's the relevant chunk:

CMR - Convertible Memory Range -
A range of physical memory configured by BIOS and verified by MCHECK. MCHECK verificatio n is intended to help ensure that a CMR may be used to hold TDX memory pages encrypted with a private HKID.

So, the BIOS has the platform knowledge to enumerate this range. It stashes the information off somewhere that the TDX module can find it. Then, during OS boot, the OS makes a SEAMCALL (TDH.SYS.CONFIG) to the TDX module and gets the list of CMRs.

Quoted 5 lines

The OS then has to reconcile this CMR "memory map" against the regular old BIOS-provided memory map, tossing out any memory regions which are RAM, but not covered by a CMR, or disabling TDX entirely.

Fun, eh?

Yes, I want to challenge the idea that all core-mm memory must be TDX capable. Instead, this feels more like something that wants a hugetlbfs / dax-device like capability to ask the kernel to gather / set-aside the enumerated TDX memory out of all the general purpose memory it knows about and then VMs use that ABI to get access to convertible memory. Trying to ensure that all page allocator memory is TDX capable feels too restrictive with all the different ways pfns can get into the allocator.

#148 lore raw
Quoted 8 lines · Dan Williams

Yes, I want to challenge the idea that all core-mm memory must be TDX capable. Instead, this feels more like something that wants a hugetlbfs / dax-device like capability to ask the kernel to gather / set-aside the enumerated TDX memory out of all the general purpose memory it knows about and then VMs use that ABI to get access to convertible memory. Trying to ensure that all page allocator memory is TDX capable feels too restrictive with all the different ways pfns can get into the allocator.

The KVM users are the problem here. They use a variety of ABIs to get memory and then hand it to KVM. KVM basically just consumes the physical addresses from the page tables.

Also, there's no _practical_ problem here today. I can't actually think of a case where any memory that ends up in the allocator on today's TDX systems is not TDX capable.

Tomorrow's systems are going to be the problem. They'll (presumably) have a mix of CXL devices that will have varying capabilities. Some will surely lack the metadata storage for checksums and TD-owner bits. TDX use will be *safe* on those systems: if you take this code and run it on one tomorrow's systems, it will notice the TDX-incompatible memory and will disable TDX.

The only way around this that I can see is to introduce ABI today that anticipates the needs of the future systems. We could require that all the KVM memory be "validated" before handing it to TDX. Maybe a new syscall that says: "make sure this mapping works for TDX". It could be new sysfs ABI which specifies which NUMA nodes contain TDX-capable memory.

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.

I'm open to ideas here. If there's a viable ABI we can introduce to train TDX users today that will work tomorrow too, I'm all for it.

#149 lore raw
Quoted 26 lines · Dave Hansen
Quoted 8 lines · Dan Williams

Yes, I want to challenge the idea that all core-mm memory must be TDX capable. Instead, this feels more like something that wants a hugetlbfs / dax-device like capability to ask the kernel to gather / set-aside the enumerated TDX memory out of all the general purpose memory it knows about and then VMs use that ABI to get access to convertible memory. Trying to ensure that all page allocator memory is TDX capable feels too restrictive with all the different ways pfns can get into the allocator.

The KVM users are the problem here. They use a variety of ABIs to get memory and then hand it to KVM. KVM basically just consumes the physical addresses from the page tables.

Also, there's no _practical_ problem here today. I can't actually think of a case where any memory that ends up in the allocator on today's TDX systems is not TDX capable.

Tomorrow's systems are going to be the problem. They'll (presumably) have a mix of CXL devices that will have varying capabilities. Some will surely lack the metadata storage for checksums and TD-owner bits. TDX use will be *safe* on those systems: if you take this code and run it on one tomorrow's systems, it will notice the TDX-incompatible memory and will disable TDX.

Quoted 5 lines

The only way around this that I can see is to introduce ABI today that anticipates the needs of the future systems. We could require that all the KVM memory be "validated" before handing it to TDX. Maybe a new syscall that says: "make sure this mapping works for TDX". It could be new sysfs ABI which specifies which NUMA nodes contain TDX-capable memory.

Yes, node-id seems the only reasonable handle that can be used, and it does not seem too onerous for a KVM user to have to set a node policy preferring all the TDX / confidential-computing capable nodes.

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.

Quoted 2 lines

I'm open to ideas here. If there's a viable ABI we can introduce to train TDX users today that will work tomorrow too, I'm all for it.

In general, expressing NUMA node perf and node capabilities is something Linux needs to get better at. HMAT data for example still exists as sideband information ignored by numactl, but it feels inevitable that perf and capability details become more of a first class citizen for applications that have these mem-allocation-policy constraints in the presence of disparate memory types.

#150 lore raw
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.

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.

#151 lore raw
Quoted 13 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.

Quoted 6 lines

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 };

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

Quoted 14 lines

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.

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".

#152 lore raw
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).

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?

#153 lore raw
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
#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