x86/ioremap: Use is_ioremap_addr() in iounmap()
Subsystems (3)
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
@@ -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
Quoted 6 lines · Max Ramanouski
Restrict
iounmap()to memory allocated in ioremap region, by usingis_ioremap_addr(). Similarly to the genericiounmap()implementation.Additionally, add warning in case there is an attempt to
iounmap()invalid memory, instead of silently exiting, thus helping to avoid incorrect usage ofiounmap().
Looks good, thanks. Feel free to add:
Quoted 27 lines
Reviewed-by Christoph Hellwig <[email protected]>Signed-off-by Max Ramanouski <[email protected]>--- arch/x86/mm/ioremap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)arch/x86/mm/ioremap.c +2
-1@@ -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 ;/*
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
@@ -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 ;/*
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:00x86/ioremap: Use
is_ioremap_addr()iniounmap()
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
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:00x86/ioremap: Use
is_ioremap_addr()iniounmap()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 | #defineIOREMAP_STARTVMALLOC_STARTCan you please have a look?
Thanks,
tglx
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_STARTinasm/pgtable.h, and after a quick glance it seems to be the only arch to do so... Probably the correct solution is to add includeasm/vmalloc.htoinclude/linux/ioremap.h, considering it usesVMALLOC_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
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 inasm/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