Skip to contentSkip to conversation

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

PATCH Applied Max Ramanouski 4 participants 7 messages Aug 15, 2024 – Aug 21, 2024

Subsystems (3)

v3 of 4 Applied Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

Restrict iounmap() to memory allocated in ioremap region, by using is_ioremap_addr(). Similarly to the generic iounmap() implementation.

Additionally, add warning in case there is an attempt to iounmap() invalid memory, instead of silently exiting, thus helping to avoid incorrect usage of iounmap().

---
 arch/x86/mm/ioremap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/ioremap.c +2 -1
index aa7d279321ea..70b02fc61d93 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 (WARN_ON_ONCE(!is_ioremap_addr((void __force *)addr)))
 		return;
 
 	/*
Signature · 1 line
2.45.2
Reviewed-by on v3 1/1
#1 lore raw
Quoted 6 lines · Max Ramanouski

Restrict iounmap() to memory allocated in ioremap region, by using is_ioremap_addr(). Similarly to the generic iounmap() implementation.

Additionally, add warning in case there is an attempt to iounmap() invalid memory, instead of silently exiting, thus helping to avoid incorrect usage of iounmap().

Looks good, thanks. Feel free to add:

Quoted 27 lines
---
 arch/x86/mm/ioremap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/ioremap.c +2 -1
index aa7d279321ea..70b02fc61d93 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 (WARN_ON_ONCE(!is_ioremap_addr((void __force *)addr)))
 		return;
 
 	/*
merge bot Applied
#2 lore raw
In reply to Max Ramanouski
The following commit has been merged into the x86/urgent branch of tip:

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     7b02ad32d83c16abd4961d79f3154b734d1d5d9c
Gitweb:        https://git.kernel.org/tip/7b02ad32d83c16abd4961d79f3154b734d1d5d9c
Author:        Max Ramanouski <[email protected]>
AuthorDate:    Thu, 15 Aug 2024 23:56:07 +03:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 16 Aug 2024 11:33:33 +02:00

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

Restrict iounmap() to memory allocated in the ioremap region, by using is_ioremap_addr(). Similarly to the generic iounmap() implementation.

Additionally, add a warning in case there is an attempt to iounmap() invalid memory, instead of silently exiting, thus helping to avoid incorrect usage of iounmap().

---
 arch/x86/mm/ioremap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/mm/ioremap.c +2 -1
index aa7d279..70b02fc 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 (WARN_ON_ONCE(!is_ioremap_addr((void __force *)addr)))
 		return;
 
 	/*
#3 lore raw
Quoted 10 lines · tip-bot

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     7b02ad32d83c16abd4961d79f3154b734d1d5d9c
Gitweb:        https://git.kernel.org/tip/7b02ad32d83c16abd4961d79f3154b734d1d5d9c
Author:        Max Ramanouski <[email protected]>
AuthorDate:    Thu, 15 Aug 2024 23:56:07 +03:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 16 Aug 2024 11:33:33 +02:00

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

This has been removed as it fails on a 32-bit defconfig:

include/linux/ioremap.h: In function ‘is_ioremap_addr’:
include/linux/ioremap.h:14:25: error: ‘VMALLOC_START’ undeclared (first use in this function); did you mean ‘KMALLOC_DMA’?
14 | #define IOREMAP_START VMALLOC_START

Can you please have a look?

Thanks,

tglx

author
#4 lore raw

Apparently, x86-32 doesn't provide VMALLOC_START in asm/pgtable.h, and after a quick glance it seems to be the only arch to do so... Probably the correct solution is to add include asm/vmalloc.h to include/linux/ioremap.h, considering it uses VMALLOC_START. Will resubmit the v4 version of patch later today.

Best regards,
Max

Quoted 24 lines · Thomas Gleixner
Quoted 10 lines · tip-bot

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     7b02ad32d83c16abd4961d79f3154b734d1d5d9c
Gitweb:        https://git.kernel.org/tip/7b02ad32d83c16abd4961d79f3154b734d1d5d9c
Author:        Max Ramanouski <[email protected]>
AuthorDate:    Thu, 15 Aug 2024 23:56:07 +03:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Fri, 16 Aug 2024 11:33:33 +02:00

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

This has been removed as it fails on a 32-bit defconfig:

include/linux/ioremap.h: In function ‘is_ioremap_addr’:
include/linux/ioremap.h:14:25: error: ‘VMALLOC_START’ undeclared (first use in this function); did you mean ‘KMALLOC_DMA’?
14 | #define IOREMAP_START VMALLOC_START

Can you please have a look?

Thanks,

tglx

#5 lore raw

On Wed, Aug 21 2024 at 11:35, Max R. wrote:

Please do not top-post and trim your replies.

Quoted 5 lines

Apparently, x86-32 doesn't provide VMALLOC_START in asm/pgtable.h, and after a quick glance it seems to be the only arch to do so... Probably the correct solution is to add include asm/vmalloc.h to include/linux/ioremap.h, considering it uses VMALLOC_START. Will resubmit the v4 version of patch later today.

Careful, this might end up with other issues vs. the kernel header inclusion hell.

If i386 is the only one which does not have VMALLOC start in asm/pgtable.h, then curing this might be the easier fix, no?

Thanks,

tglx

#6 lore raw
Quoted 1 line

Please do not top-post and trim your replies.

Sorry, got a bit distracted and forgot to remove the default gmail reply text. Will try to be more careful in future with better email client.

Quoted 4 lines

Careful, this might end up with other issues vs. the kernel header inclusion hell.
If i386 is the only one which does not have VMALLOC start in asm/pgtable.h, then curing this might be the easier fix, no?

I thought about that initially, but then found out that apparently it is like that for a reason. In commit:

186525bd6b8 ("mm, x86/mm: Untangle address space layout definitions from basic pgtable type definitions")

VMALLOC_START definition was specifically moved out of pgtable related headers. But for some reason only for 32 bit arch. Plus I think asm/vmalloc.h is more semantically correct to get VMALLOC_START. Although on most arches (everywhere except x86, arm64, powerpc and riscv) asm/vmalloc.h is an empty header. And on riscv it doesn't actually provide VMALLOC_START... Probably that is worth a separate fix later.

Best regards,
Max