Skip to contentSkip to conversation

x86/ioremap: Use is_ioremap_addr() in iounmap()

PATCH Superseded Max Ramanouski 4 participants 9 messages Aug 12, 2024 – Aug 15, 2024

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/ioremap: Use is_ioremap_addr() in iounmap()

On systems that use HMM (most notably amdgpu driver) high_memory can jump over VMALLOC_START due to pages at the end of physical space being added with add_pages(), while gap for new pages left by KASLR is as small as 10TB. This results in early exit from iounmap() leading to leaking, and additional problems with rebinding devices to vfio_pci from other drivers with error of conflicting memtypes, as memtypes aren't freed in iounmap().

Replace comparison against high_memory with is_ioremap_addr() to fix the issue and make x86 iounmap() implementation more similar to generic one, it also uses is_ioremap_addr() to validate pointer.

Fixes 41e94a851304 ("add devm_memremap_pages")
---
 arch/x86/mm/ioremap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/ioremap.c +2 -1
index aa7d27932..464344da4 100644
@@ -11,6 +11,7 @@
 #include <linux/init.h>
 #include <linux/io.h>
 #include <linux/ioport.h>
+#include <linux/ioremap.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/mmiotrace.h>
@@ -457,7 +458,7 @@ void iounmap(volatile void __iomem *addr)
 {
 	struct vm_struct *p, *o;
 
-	if ((void __force *)addr <= high_memory)
+	if (!is_ioremap_addr(addr))
 		return;
 
 	/*
Signature · 1 line
2.45.2
#1 lore raw
Quoted 8 lines · Max Ramanouski

On systems that use HMM (most notably amdgpu driver) high_memory can jump over VMALLOC_START due to pages at the end of physical space being added with add_pages(), while gap for new pages left by KASLR is as small as 10TB. This results in early exit from iounmap() leading to leaking, and additional problems with rebinding devices to vfio_pci from other drivers with error of conflicting memtypes, as memtypes aren't freed in iounmap().

Quoted 5 lines

Replace comparison against high_memory with is_ioremap_addr() to fix the issue and make x86 iounmap() implementation more similar to generic one, it also uses is_ioremap_addr() to validate pointer.

Fixes 41e94a851304 ("add devm_memremap_pages")

This fixes absolutely nothing as we discussed already. The underlying problem is that high_memory can spill over into the VMALLOC area.

Seriously?

Thanks,

tglx

#3 lore raw
Quoted 3 lines · Christoph Hellwig

Modulo the fixes discussion (and any commit log adjustments related to that), is_ioremap_addr is the right interface to check for an ioremap address. So for the actual code change:

I'm not opposed to use is_ioremap_addr() as it restricts the check to the actual ioremp region.

That said, I'm wondering why iounmap() silently bails out when invoked with an address which is outside of the ioremap region. I'd say, any invocation with an address outside of it, is broken, but I might be missing something as always.

Thanks,

tglx

#4 lore raw
Quoted 5 lines · Thomas Gleixner
Quoted 3 lines · Christoph Hellwig

Modulo the fixes discussion (and any commit log adjustments related to that), is_ioremap_addr is the right interface to check for an ioremap address. So for the actual code change:

Quoted 7 lines

I'm not opposed to use is_ioremap_addr() as it restricts the check to the actual ioremp region.

That said, I'm wondering why iounmap() silently bails out when invoked with an address which is outside of the ioremap region. I'd say, any invocation with an address outside of it, is broken, but I might be missing something as always.

I would tend to agree and had the same thought when we found this. At least some kind of message (WARN_ON, WARN_ON_ONCE, printk, etc) would have made the issue we were debugging much more obvious. FWIW I have tested running with a WARN_ON() there and it never fired except in the bug scenario.

- Alistair

Quoted 3 lines

Thanks,

tglx

#5 lore raw
Quoted 5 lines · Alistair Popple

I would tend to agree and had the same thought when we found this. At least some kind of message (WARN_ON, WARN_ON_ONCE, printk, etc) would have made the issue we were debugging much more obvious. FWIW I have tested running with a WARN_ON() there and it never fired except in the bug scenario.

Various architectures had either an early ioremap variant that got silently ignored here, or magic carveout that don't get remapped at all. None of this should currently apply to x86, though.

#6 lore raw
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Alistair Popple

I would tend to agree and had the same thought when we found this. At least some kind of message (WARN_ON, WARN_ON_ONCE, printk, etc) would have made the issue we were debugging much more obvious. FWIW I have tested running with a WARN_ON() there and it never fired except in the bug scenario.

Various architectures had either an early ioremap variant that got silently ignored here, or magic carveout that don't get remapped at all. None of this should currently apply to x86, though.

So I'm inclined to have:

       if (WARN_ON_ONCE(is_ioremap_addr(addr)))
       		return;

in the x86 variant then.

Thanks,

tglx

#7 lore raw
Quoted 10 lines · Thomas Gleixner
Quoted 3 lines

Various architectures had either an early ioremap variant that got silently ignored here, or magic carveout that don't get remapped at all. None of this should currently apply to x86, though.

So I'm inclined to have:

       if (WARN_ON_ONCE(is_ioremap_addr(addr)))
       		return;

in the x86 variant then.

And we should do the same for the generic code eventually after accounting for all exceptions. Those should these days mostly be handled before hand and have explicit address ranges as well, but it'll need some time to figure all that out.

#8 lore raw
In reply to Thomas Gleixner
Quoted 12 lines · Thomas Gleixner
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Alistair Popple

I would tend to agree and had the same thought when we found this. At least some kind of message (WARN_ON, WARN_ON_ONCE, printk, etc) would have made the issue we were debugging much more obvious. FWIW I have tested running with a WARN_ON() there and it never fired except in the bug scenario.

Various architectures had either an early ioremap variant that got silently ignored here, or magic carveout that don't get remapped at all. None of this should currently apply to x86, though.

Quoted 6 lines

So I'm inclined to have:

       if (WARN_ON_ONCE(is_ioremap_addr(addr)))
       		return;

in the x86 variant then.

Max, care to provide that with a reasonable change log?

Thanks,

tglx