Skip to contentSkip to conversation

memregion: Add arch_flush_memregion() interface

PATCH Superseded Davidlohr Bueso 5 participants 20 messages Aug 29, 2022 – Sep 13, 2022

Subsystems (5)
x86 ACPI Library code x86 MM +1 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memregion: Add arch_flush_memregion() interface

With CXL security features, global CPU cache flushing nvdimm requirements are no longer specific to that subsystem, even beyond the scope of security_ops. CXL will need such semantics for features not necessarily limited to persistent memory.

The functionality this is enabling is to be able to instantaneously secure erase potentially terabytes of memory at once and the kernel needs to be sure that none of the data from before the secure is still present in the cache. It is also used when unlocking a memory device where speculative reads and firmware accesses could have cached poison from before the device was unlocked.

This capability is typically only used once per-boot (for unlock), or once per bare metal provisioning event (secure erase), like when handing off the system to another tenant or decommissioning a device.

Users must first call arch_has_flush_memregion() to know whether this functionality is available on the architecture. Only enable it on x86-64 via the wbinvd() hammer.

---

Changes from v2 (https://lore.kernel.org/all/[email protected]/):
- Redid to use memregion based interfaces + VMM check on x86 (Dan)
- Restricted the flushing to x86-64.

Note: Since we still are dealing with a physical "range" at this level, added the spa range for nfit even though this is unused.

 arch/x86/Kconfig             |  1 +
 arch/x86/mm/pat/set_memory.c | 14 +++++++++++
 drivers/acpi/nfit/intel.c    | 45 ++++++++++++++++++------------------
 include/linux/memregion.h    | 25 ++++++++++++++++++++
 lib/Kconfig                  |  3 +++
 5 files changed, 65 insertions(+), 23 deletions(-)
arch/x86/Kconfig +1 -0
index f9920f1341c8..594e6b6a4925 100644
@@ -81,6 +81,7 @@ config X86
 	select ARCH_HAS_KCOV			if X86_64
 	select ARCH_HAS_MEM_ENCRYPT
 	select ARCH_HAS_MEMBARRIER_SYNC_CORE
+	select ARCH_HAS_MEMREGION_INVALIDATE    if X86_64
 	select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
 	select ARCH_HAS_PMEM_API		if X86_64
 	select ARCH_HAS_PTE_DEVMAP		if X86_64
arch/x86/mm/pat/set_memory.c +14 -0
index 1abd5438f126..18463cb704fb 100644
@@ -330,6 +330,20 @@ void arch_invalidate_pmem(void *addr, size_t size)
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
 #endif
 
+#ifdef CONFIG_ARCH_HAS_MEMREGION_INVALIDATE
+bool arch_has_flush_memregion(void)
+{
+	return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR);
+}
+EXPORT_SYMBOL(arch_has_flush_memregion);
+
+void arch_flush_memregion(phys_addr_t phys, resource_size_t size)
+{
+	wbinvd_on_all_cpus();
+}
+EXPORT_SYMBOL(arch_flush_memregion);
+#endif
+
 static void __cpa_flush_all(void *arg)
 {
 	unsigned long cache = (unsigned long)arg;
drivers/acpi/nfit/intel.c +22 -23

129 lines not on this page · raw message

include/linux/memregion.h +25 -0
index c04c4fd2e209..c35201c0696f 100644
@@ -20,4 +20,29 @@ static inline void memregion_free(int id)
 {
 }
 #endif
+
+/*
+ * Device memory technologies like NVDIMM and CXL have events like
+ * secure erase and dynamic region provision that can invalidate an
+ * entire physical memory address range at once. Limit that
+ * functionality to architectures that have an efficient way to
+ * writeback and invalidate potentially terabytes of memory at once.
+ *
+ * To ensure this, users must first call arch_has_flush_memregion()
+ * before anything, to verify the operation is feasible.
+ */
+#ifdef CONFIG_ARCH_HAS_MEMREGION_INVALIDATE
+void arch_flush_memregion(phys_addr_t phys, resource_size_t size);
+bool arch_has_flush_memregion(void);
+#else
+static inline bool arch_has_flush_memregion(void)
+{
+       return false;
+}
+static inline void arch_flush_memregion(phys_addr_t phys, resource_size_t size)
+{
+	WARN_ON_ONCE("cache invalidation required");
+}
+#endif
+
 #endif /* _MEMREGION_H_ */
lib/Kconfig +3 -0
index dc1ab2ed1dc6..8319e7731e7b 100644
@@ -662,6 +662,9 @@ config ARCH_HAS_PMEM_API
 config MEMREGION
 	bool
 
+config ARCH_HAS_MEMREGION_INVALIDATE
+       bool
+
 config ARCH_HAS_MEMREMAP_COMPAT_ALIGN
 	bool
 
Signature · 1 line
2.37.2
Reviewed-by on v1 1/1
#1 lore raw

[ add Christoph ]

Quoted 21 lines · Davidlohr Bueso

With CXL security features, global CPU cache flushing nvdimm requirements are no longer specific to that subsystem, even beyond the scope of security_ops. CXL will need such semantics for features not necessarily limited to persistent memory.

The functionality this is enabling is to be able to instantaneously secure erase potentially terabytes of memory at once and the kernel needs to be sure that none of the data from before the secure is still present in the cache. It is also used when unlocking a memory device where speculative reads and firmware accesses could have cached poison from before the device was unlocked.

This capability is typically only used once per-boot (for unlock), or once per bare metal provisioning event (secure erase), like when handing off the system to another tenant or decommissioning a device.

Users must first call arch_has_flush_memregion() to know whether this functionality is available on the architecture. Only enable it on x86-64 via the wbinvd() hammer.

Quoted 8 lines

---

Changes from v2 (https://lore.kernel.org/all/[email protected]/):
- Redid to use memregion based interfaces + VMM check on x86 (Dan)
- Restricted the flushing to x86-64.

Note: Since we still are dealing with a physical "range" at this level, added the spa range for nfit even though this is unused.

Looks reasonable to me.

#3 lore raw
Quoted 2 lines · Davidlohr Bueso

Not sure the proper way to route this (akpm?). But unless any remaining objections, could this be picked up?

My plan was, barring objections, to take it through the CXL tree with its first user, the CXL security commands.

maintainer
#4 lore raw
In reply to Davidlohr Bueso

arch/x86/mm/pat/set_memory.c void arch_invalidate_pmem(void *addr, size_t size) · line 336

Quoted 12 lines · Davidlohr Bueso
arch/x86/mm/pat/set_memory.c +4 -0
index 1abd5438f126..18463cb704fb 100644
@@ -330,6 +330,20 @@ void arch_invalidate_pmem(void *addr, size_t size)
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
 #endif
 
+#ifdef CONFIG_ARCH_HAS_MEMREGION_INVALIDATE
+bool arch_has_flush_memregion(void)
+{
+	return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR);

This looks really weird. Why does this need to care about HV at all?

Does that nfit stuff even run in guests?

Quoted 1 line
arch/x86/mm/pat/set_memory.c +1 -0
+EXPORT_SYMBOL(arch_has_flush_memregion);

...

Quoted 1 line

+EXPORT_SYMBOL(arch_flush_memregion);

Why aren't those exports _GPL?

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#5 lore raw
Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Davidlohr Bueso
arch/x86/mm/pat/set_memory.c +0 -0
index 1abd5438f126..18463cb704fb 100644
@@ -330,6 +330,20 @@ void arch_invalidate_pmem(void *addr, size_t size)
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
 #endif
 
Quoted 6 lines

arch/x86/mm/pat/set_memory.c void arch_invalidate_pmem(void *addr, size_t size) · line 336

Quoted 4 lines
arch/x86/mm/pat/set_memory.c +4 -0
+#ifdef CONFIG_ARCH_HAS_MEMREGION_INVALIDATE
+bool arch_has_flush_memregion(void)
+{
+	return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR);

This looks really weird. Why does this need to care about HV at all?

So the context here is:

e2efb6359e62 ("ACPICA: Avoid cache flush inside virtual machines")

Quoted 2 lines

Does that nfit stuff even run in guests?

No, nor does cxl. This was mostly in general a precautionary check such that the api is unavailable in VMs.

Quoted 8 lines
Quoted 2 lines
arch/x86/mm/pat/set_memory.c +1 -0
+EXPORT_SYMBOL(arch_has_flush_memregion);

...

Quoted 1 line

+EXPORT_SYMBOL(arch_flush_memregion);

Why aren't those exports _GPL?

Fine by me.

Thanks,
Davidlohr

#6 lore raw
Quoted 20 lines · Davidlohr Bueso
Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Davidlohr Bueso
arch/x86/mm/pat/set_memory.c +0 -0
index 1abd5438f126..18463cb704fb 100644
@@ -330,6 +330,20 @@ void arch_invalidate_pmem(void *addr, size_t size)
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
 #endif
 
Quoted 6 lines

arch/x86/mm/pat/set_memory.c void arch_invalidate_pmem(void *addr, size_t size) · line 336

Quoted 4 lines
arch/x86/mm/pat/set_memory.c +4 -0
+#ifdef CONFIG_ARCH_HAS_MEMREGION_INVALIDATE
+bool arch_has_flush_memregion(void)
+{
+	return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR);

This looks really weird. Why does this need to care about HV at all?

So the context here is:

Quoted 7 lines

e2efb6359e62 ("ACPICA: Avoid cache flush inside virtual machines")

Quoted 2 lines

Does that nfit stuff even run in guests?

No, nor does cxl. This was mostly in general a precautionary check such that the api is unavailable in VMs.

To be clear nfit stuff and CXL does run in guests, but they do not support secure-erase in a guest.

However, the QEMU CXL enabling is building the ability to do *guest physical* address space management, but in that case the driver can be paravirtualized to realize that it is not managing host-physical address space and does not need to flush caches. That will need some indicator to differentiate virtual CXL memory expanders from assigned devices. Is there such a thing as a PCIe-virtio extended capability to differentiate physical vs emulated devices?

#7 lore raw
Quoted 24 lines · Dan Williams
Quoted 23 lines · Davidlohr Bueso
Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Davidlohr Bueso
arch/x86/mm/pat/set_memory.c +0 -0
index 1abd5438f126..18463cb704fb 100644
@@ -330,6 +330,20 @@ void arch_invalidate_pmem(void *addr, size_t size)
 EXPORT_SYMBOL_GPL(arch_invalidate_pmem);
 #endif
 
Quoted 6 lines

arch/x86/mm/pat/set_memory.c void arch_invalidate_pmem(void *addr, size_t size) · line 336

Quoted 4 lines
arch/x86/mm/pat/set_memory.c +4 -0
+#ifdef CONFIG_ARCH_HAS_MEMREGION_INVALIDATE
+bool arch_has_flush_memregion(void)
+{
+	return !cpu_feature_enabled(X86_FEATURE_HYPERVISOR);

This looks really weird. Why does this need to care about HV at all?

So the context here is:

e2efb6359e62 ("ACPICA: Avoid cache flush inside virtual machines")

Quoted 1 line
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Does that nfit stuff even run in guests?

No, nor does cxl. This was mostly in general a precautionary check such that the api is unavailable in VMs.

To be clear nfit stuff and CXL does run in guests, but they do not support secure-erase in a guest.

Yes, I meant the feats this api enables.

Quoted 7 lines

However, the QEMU CXL enabling is building the ability to do *guest physical* address space management, but in that case the driver can be paravirtualized to realize that it is not managing host-physical address space and does not need to flush caches. That will need some indicator to differentiate virtual CXL memory expanders from assigned devices. Is there such a thing as a PCIe-virtio extended capability to differentiate physical vs emulated devices?

In any case such check would be specific to each user (cxl in this case), and outside the scope of _this_ particular api. Here we just really want to avoid the broken TDX guest bits.

Thanks,
Davidlohr

maintainer
#8 lore raw
In reply to Dan Williams
Quoted 8 lines · Dan Williams

To be clear nfit stuff and CXL does run in guests, but they do not support secure-erase in a guest.

However, the QEMU CXL enabling is building the ability to do *guest physical* address space management, but in that case the driver can be paravirtualized to realize that it is not managing host-physical address space and does not need to flush caches. That will need some indicator to differentiate virtual CXL memory expanders from assigned devices.

Sounds to me like that check should be improved later to ask whether the kernel is managing host-physical address space, maybe arch_flush_memregion() should check whether the address it is supposed to flush is host-physical and exit early if not...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#9 lore raw
Quoted 10 lines · Borislav Petkov
Quoted 8 lines · Dan Williams

To be clear nfit stuff and CXL does run in guests, but they do not support secure-erase in a guest.

However, the QEMU CXL enabling is building the ability to do *guest physical* address space management, but in that case the driver can be paravirtualized to realize that it is not managing host-physical address space and does not need to flush caches. That will need some indicator to differentiate virtual CXL memory expanders from assigned devices.

Quoted 4 lines

Sounds to me like that check should be improved later to ask whether the kernel is managing host-physical address space, maybe arch_flush_memregion() should check whether the address it is supposed to flush is host-physical and exit early if not...

Even though I raised the possibility of guest passthrough of a CXL memory expander, I do not think it could work in practice without it being a gigantic security nightmare. So it is probably safe to just do the hypervisor check and assume that there's no such thing as guest management of host physical address space.

#10 lore raw
Quoted 16 lines · On Wed, 7 Sep 2022 23:53:31 -0700 Dan Williams <[email protected]> wrote:
Quoted 10 lines · Borislav Petkov
Quoted 8 lines · Dan Williams

To be clear nfit stuff and CXL does run in guests, but they do not support secure-erase in a guest.

However, the QEMU CXL enabling is building the ability to do *guest physical* address space management, but in that case the driver can be paravirtualized to realize that it is not managing host-physical address space and does not need to flush caches. That will need some indicator to differentiate virtual CXL memory expanders from assigned devices.

Quoted 4 lines

Sounds to me like that check should be improved later to ask whether the kernel is managing host-physical address space, maybe arch_flush_memregion() should check whether the address it is supposed to flush is host-physical and exit early if not...

Quoted 5 lines

Even though I raised the possibility of guest passthrough of a CXL memory expander, I do not think it could work in practice without it being a gigantic security nightmare. So it is probably safe to just do the hypervisor check and assume that there's no such thing as guest management of host physical address space.

Agreed. Other than occasional convenience of doing driver development in a VM (they reboot quickly ;) can't see why a product system would need to pass a CXL type 3 device through and as you say security would be 'interesting' if it were done. Might be GPU usecases down the line but I'm doubtful on that as well.

Jonathan

maintainer
#11 lore raw
In reply to Davidlohr Bueso
Quoted 3 lines · Davidlohr Bueso

So the context here is:

e2efb6359e62 ("ACPICA: Avoid cache flush inside virtual machines")

Please add this to your commit message so that at least we know how this HV dependency came about.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
maintainer
#12 lore raw
In reply to Davidlohr Bueso

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

#13 lore raw
Quoted 8 lines · Andrew Morton

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

That's a good point, "flush" has been cargo-culted along in Linux's cache management APIs to mean write-back-and-invalidate. In this case I think this API is purely about invalidate. It just so happens that x86 has not historically had a global invalidate instruction readily available which leads to the overuse of wbinvd.

It would be nice to make clear that this API is purely about invalidating any data cached for a physical address impacted by address space management event (secure erase / new region provision). Write-back is an unnecessary side-effect.

So how about:

s/arch_flush_memregion/cpu_cache_invalidate_memregion/?

#14 lore raw
Quoted 16 lines · On Wed, 7 Sep 2022 18:07:31 -0700 Dan Williams <[email protected]> wrote:
Quoted 8 lines · Andrew Morton

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

That's a good point, "flush" has been cargo-culted along in Linux's cache management APIs to mean write-back-and-invalidate. In this case I think this API is purely about invalidate. It just so happens that x86 has not historically had a global invalidate instruction readily available which leads to the overuse of wbinvd.

Quoted 8 lines

It would be nice to make clear that this API is purely about invalidating any data cached for a physical address impacted by address space management event (secure erase / new region provision). Write-back is an unnecessary side-effect.

So how about:

s/arch_flush_memregion/cpu_cache_invalidate_memregion/?

Want to indicate it 'might' write back perhaps?
So could be invalidate or clean and invalidate (using arm ARM terms just to add to the confusion ;)

Feels like there will be potential race conditions where that matters as we might force stale data to be written back.

Perhaps a comment is enough for that. Anyone have the "famous last words" feeling?

Jonathan

#15 lore raw
Quoted 28 lines · Jonathan Cameron
Quoted 16 lines · On Wed, 7 Sep 2022 18:07:31 -0700 Dan Williams <[email protected]> wrote:
Quoted 8 lines · Andrew Morton

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

That's a good point, "flush" has been cargo-culted along in Linux's cache management APIs to mean write-back-and-invalidate. In this case I think this API is purely about invalidate. It just so happens that x86 has not historically had a global invalidate instruction readily available which leads to the overuse of wbinvd.

Quoted 8 lines

It would be nice to make clear that this API is purely about invalidating any data cached for a physical address impacted by address space management event (secure erase / new region provision). Write-back is an unnecessary side-effect.

So how about:

s/arch_flush_memregion/cpu_cache_invalidate_memregion/?

Quoted 8 lines

Want to indicate it 'might' write back perhaps?
So could be invalidate or clean and invalidate (using arm ARM terms just to add to the confusion ;)

Feels like there will be potential race conditions where that matters as we might force stale data to be written back.

Perhaps a comment is enough for that. Anyone have the "famous last words" feeling?

Is "invalidate" not clear that write-back is optional? Maybe not.

Also, I realized that we tried to include the address range to allow for the possibility of flushing by virtual address range, but that overcomplicates the use. I.e. if someone issue secure erase and the region association is not established does that mean that mean that the cache invalidation is not needed? It could be the case that someone disables a device, does the secure erase, and then reattaches to the same region. The cache invalidation is needed, but at the time of the secure erase the HPA was unknown.

All this to say that I feel the bikeshedding will need to continue until morale improves.

I notice that the DMA API uses 'sync' to indicate, "make this memory consistent/coherent for the CPU or the device", so how about an API like

memregion_sync_for_cpu(int res_desc)

...where the @res_desc would be IORES_DESC_CXL for all CXL and IORES_DESC_PERSISTENT_MEMORY for the current nvdimm use case.

maintainer
#16 lore raw
Quoted 33 lines · On Thu, 8 Sep 2022 15:51:50 -0700 Dan Williams <[email protected]> wrote:
Quoted 32 lines · Jonathan Cameron
Quoted 16 lines · On Wed, 7 Sep 2022 18:07:31 -0700 Dan Williams <[email protected]> wrote:
Quoted 8 lines · Andrew Morton

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

That's a good point, "flush" has been cargo-culted along in Linux's cache management APIs to mean write-back-and-invalidate. In this case I think this API is purely about invalidate. It just so happens that x86 has not historically had a global invalidate instruction readily available which leads to the overuse of wbinvd.

Quoted 8 lines

It would be nice to make clear that this API is purely about invalidating any data cached for a physical address impacted by address space management event (secure erase / new region provision). Write-back is an unnecessary side-effect.

So how about:

s/arch_flush_memregion/cpu_cache_invalidate_memregion/?

Want to indicate it 'might' write back perhaps?
So could be invalidate or clean and invalidate (using arm ARM terms just to add to the confusion ;)

Quoted 6 lines
Quoted 4 lines

Feels like there will be potential race conditions where that matters as we might force stale data to be written back.

Perhaps a comment is enough for that. Anyone have the "famous last words" feeling?

Is "invalidate" not clear that write-back is optional? Maybe not.

Yes, I'd say that "invalidate" means "dirty stuff may of may not have been written back". Ditto for invalidate_inode_pages2().

Quoted 12 lines

Also, I realized that we tried to include the address range to allow for the possibility of flushing by virtual address range, but that overcomplicates the use. I.e. if someone issue secure erase and the region association is not established does that mean that mean that the cache invalidation is not needed? It could be the case that someone disables a device, does the secure erase, and then reattaches to the same region. The cache invalidation is needed, but at the time of the secure erase the HPA was unknown.

All this to say that I feel the bikeshedding will need to continue until morale improves.

Quoted 7 lines

I notice that the DMA API uses 'sync' to indicate, "make this memory consistent/coherent for the CPU or the device", so how about an API like

memregion_sync_for_cpu(int res_desc)

...where the @res_desc would be IORES_DESC_CXL for all CXL and IORES_DESC_PERSISTENT_MEMORY for the current nvdimm use case.

"sync" is another of my pet peeves ;) In filesystem land, at least. Does it mean "start writeback and return" or does it mean "start writeback, wait for its completion then return".

#17 lore raw
Quoted 60 lines · Andrew Morton
Quoted 33 lines · On Thu, 8 Sep 2022 15:51:50 -0700 Dan Williams <[email protected]> wrote:
Quoted 32 lines · Jonathan Cameron
Quoted 16 lines · On Wed, 7 Sep 2022 18:07:31 -0700 Dan Williams <[email protected]> wrote:
Quoted 8 lines · Andrew Morton

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

That's a good point, "flush" has been cargo-culted along in Linux's cache management APIs to mean write-back-and-invalidate. In this case I think this API is purely about invalidate. It just so happens that x86 has not historically had a global invalidate instruction readily available which leads to the overuse of wbinvd.

Quoted 8 lines

It would be nice to make clear that this API is purely about invalidating any data cached for a physical address impacted by address space management event (secure erase / new region provision). Write-back is an unnecessary side-effect.

So how about:

s/arch_flush_memregion/cpu_cache_invalidate_memregion/?

Want to indicate it 'might' write back perhaps?
So could be invalidate or clean and invalidate (using arm ARM terms just to add to the confusion ;)

Quoted 6 lines
Quoted 4 lines

Feels like there will be potential race conditions where that matters as we might force stale data to be written back.

Perhaps a comment is enough for that. Anyone have the "famous last words" feeling?

Is "invalidate" not clear that write-back is optional? Maybe not.

Yes, I'd say that "invalidate" means "dirty stuff may of may not have been written back". Ditto for invalidate_inode_pages2().

Quoted 15 lines

Also, I realized that we tried to include the address range to allow for the possibility of flushing by virtual address range, but that overcomplicates the use. I.e. if someone issue secure erase and the region association is not established does that mean that mean that the cache invalidation is not needed? It could be the case that someone disables a device, does the secure erase, and then reattaches to the same region. The cache invalidation is needed, but at the time of the secure erase the HPA was unknown.

All this to say that I feel the bikeshedding will need to continue until morale improves.

I notice that the DMA API uses 'sync' to indicate, "make this memory consistent/coherent for the CPU or the device", so how about an API like

Quoted 8 lines
Quoted 4 lines

memregion_sync_for_cpu(int res_desc)

...where the @res_desc would be IORES_DESC_CXL for all CXL and IORES_DESC_PERSISTENT_MEMORY for the current nvdimm use case.

"sync" is another of my pet peeves ;) In filesystem land, at least. Does it mean "start writeback and return" or does it mean "start writeback, wait for its completion then return".

Ok, no "sync" :).

/**
* cpu_cache_invalidate_memregion - drop any CPU cached data for
* memregions described by @res_des
* @res_desc: one of the IORES_DESC_* types
*
* Perform cache maintenance after a memory event / operation that
* changes the contents of physical memory in a cache-incoherent manner.
* For example, memory-device secure erase, or provisioning new CXL
* regions. This routine may or may not write back any dirty contents
* while performing the invalidation.
*
* Returns 0 on success or negative error code on a failure to perform
* the cache maintenance.
*/
int cpu_cache_invalidate_memregion(int res_desc)

??

#18 lore raw
Quoted 72 lines · On Thu, 8 Sep 2022 16:22:26 -0700 Dan Williams <[email protected]> wrote:
Quoted 60 lines · Andrew Morton
Quoted 33 lines · On Thu, 8 Sep 2022 15:51:50 -0700 Dan Williams <[email protected]> wrote:
Quoted 32 lines · Jonathan Cameron
Quoted 16 lines · On Wed, 7 Sep 2022 18:07:31 -0700 Dan Williams <[email protected]> wrote:
Quoted 8 lines · Andrew Morton

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

That's a good point, "flush" has been cargo-culted along in Linux's cache management APIs to mean write-back-and-invalidate. In this case I think this API is purely about invalidate. It just so happens that x86 has not historically had a global invalidate instruction readily available which leads to the overuse of wbinvd.

Quoted 8 lines

It would be nice to make clear that this API is purely about invalidating any data cached for a physical address impacted by address space management event (secure erase / new region provision). Write-back is an unnecessary side-effect.

So how about:

s/arch_flush_memregion/cpu_cache_invalidate_memregion/?

Want to indicate it 'might' write back perhaps?
So could be invalidate or clean and invalidate (using arm ARM terms just to add to the confusion ;)

Quoted 6 lines
Quoted 4 lines

Feels like there will be potential race conditions where that matters as we might force stale data to be written back.

Perhaps a comment is enough for that. Anyone have the "famous last words" feeling?

Is "invalidate" not clear that write-back is optional? Maybe not.

Yes, I'd say that "invalidate" means "dirty stuff may of may not have been written back". Ditto for invalidate_inode_pages2().

Quoted 15 lines

Also, I realized that we tried to include the address range to allow for the possibility of flushing by virtual address range, but that overcomplicates the use. I.e. if someone issue secure erase and the region association is not established does that mean that mean that the cache invalidation is not needed? It could be the case that someone disables a device, does the secure erase, and then reattaches to the same region. The cache invalidation is needed, but at the time of the secure erase the HPA was unknown.

All this to say that I feel the bikeshedding will need to continue until morale improves.

I notice that the DMA API uses 'sync' to indicate, "make this memory consistent/coherent for the CPU or the device", so how about an API like

Quoted 8 lines
Quoted 4 lines

memregion_sync_for_cpu(int res_desc)

...where the @res_desc would be IORES_DESC_CXL for all CXL and IORES_DESC_PERSISTENT_MEMORY for the current nvdimm use case.

"sync" is another of my pet peeves ;) In filesystem land, at least. Does it mean "start writeback and return" or does it mean "start writeback, wait for its completion then return".

Ok, no "sync" :).

Quoted 15 lines

/**
* cpu_cache_invalidate_memregion - drop any CPU cached data for
* memregions described by @res_des
* @res_desc: one of the IORES_DESC_* types
*
* Perform cache maintenance after a memory event / operation that
* changes the contents of physical memory in a cache-incoherent manner.
* For example, memory-device secure erase, or provisioning new CXL
* regions. This routine may or may not write back any dirty contents
* while performing the invalidation.
*
* Returns 0 on success or negative error code on a failure to perform
* the cache maintenance.
*/
int cpu_cache_invalidate_memregion(int res_desc)

lgtm

Quoted 2 lines

??

#19 lore raw
Quoted 91 lines · Jonathan Cameron
Quoted 72 lines · On Thu, 8 Sep 2022 16:22:26 -0700 Dan Williams <[email protected]> wrote:
Quoted 60 lines · Andrew Morton
Quoted 33 lines · On Thu, 8 Sep 2022 15:51:50 -0700 Dan Williams <[email protected]> wrote:
Quoted 32 lines · Jonathan Cameron
Quoted 16 lines · On Wed, 7 Sep 2022 18:07:31 -0700 Dan Williams <[email protected]> wrote:
Quoted 8 lines · Andrew Morton

I really dislike the term "flush". Sometimes it means writeback, sometimes it means invalidate. Perhaps at other times it means both.

Can we please be very clear in comments and changelogs about exactly
what this "flush" does. With bonus points for being more specific in the
function naming?

That's a good point, "flush" has been cargo-culted along in Linux's cache management APIs to mean write-back-and-invalidate. In this case I think this API is purely about invalidate. It just so happens that x86 has not historically had a global invalidate instruction readily available which leads to the overuse of wbinvd.

Quoted 8 lines

It would be nice to make clear that this API is purely about invalidating any data cached for a physical address impacted by address space management event (secure erase / new region provision). Write-back is an unnecessary side-effect.

So how about:

s/arch_flush_memregion/cpu_cache_invalidate_memregion/?

Want to indicate it 'might' write back perhaps?
So could be invalidate or clean and invalidate (using arm ARM terms just to add to the confusion ;)

Quoted 6 lines
Quoted 4 lines

Feels like there will be potential race conditions where that matters as we might force stale data to be written back.

Perhaps a comment is enough for that. Anyone have the "famous last words" feeling?

Is "invalidate" not clear that write-back is optional? Maybe not.

Yes, I'd say that "invalidate" means "dirty stuff may of may not have been written back". Ditto for invalidate_inode_pages2().

Quoted 15 lines

Also, I realized that we tried to include the address range to allow for the possibility of flushing by virtual address range, but that overcomplicates the use. I.e. if someone issue secure erase and the region association is not established does that mean that mean that the cache invalidation is not needed? It could be the case that someone disables a device, does the secure erase, and then reattaches to the same region. The cache invalidation is needed, but at the time of the secure erase the HPA was unknown.

All this to say that I feel the bikeshedding will need to continue until morale improves.

I notice that the DMA API uses 'sync' to indicate, "make this memory consistent/coherent for the CPU or the device", so how about an API like

Quoted 8 lines
Quoted 4 lines

memregion_sync_for_cpu(int res_desc)

...where the @res_desc would be IORES_DESC_CXL for all CXL and IORES_DESC_PERSISTENT_MEMORY for the current nvdimm use case.

"sync" is another of my pet peeves ;) In filesystem land, at least. Does it mean "start writeback and return" or does it mean "start writeback, wait for its completion then return".

Ok, no "sync" :).

Quoted 15 lines

/**
* cpu_cache_invalidate_memregion - drop any CPU cached data for
* memregions described by @res_des
* @res_desc: one of the IORES_DESC_* types
*
* Perform cache maintenance after a memory event / operation that
* changes the contents of physical memory in a cache-incoherent manner.
* For example, memory-device secure erase, or provisioning new CXL
* regions. This routine may or may not write back any dirty contents
* while performing the invalidation.
*
* Returns 0 on success or negative error code on a failure to perform
* the cache maintenance.
*/
int cpu_cache_invalidate_memregion(int res_desc)

Quoted 1 line

lgtm

Likewise, and I don't see anyone else objecting so I'll go ahead and send a new iteration.

Thanks,
Davidlohr