The only instances in which we customise this function are ones in which we
customise the PFN used, other than the fact that, when a custom
io_remap_pfn_range() function is provided, the prot value passed is not
filtered through pgprot_decrypted().
Use this fact to simplify the use of io_remap_pfn_range(), by abstracting
the PFN function as io_remap_pfn_range_pfn(), and simply have the
convention that, should a custom handler be specified, we do not utilise
pgprot_decrypted().
If we require in future prot customisation, we can make
io_remap_pfn_range_prot() available for override.
Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
arch/csky/include/asm/pgtable.h | 3 +--
arch/mips/alchemy/common/setup.c | 9 +++++----
arch/mips/include/asm/pgtable.h | 5 ++---
arch/sparc/include/asm/pgtable_32.h | 12 ++++--------
arch/sparc/include/asm/pgtable_64.h | 12 ++++--------
include/linux/mm.h | 30 ++++++++++++++++++++++++-----
6 files changed, 41 insertions(+), 30 deletions(-)
diff --git a/arch/csky/include/asm/pgtable.h b/arch/csky/include/asm/pgtable.h
index 5a394be09c35..967c86b38f11 100644
--- a/arch/csky/include/asm/pgtable.h
+++ b/arch/csky/include/asm/pgtable.h
@@ -263,7 +263,6 @@ void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
#define update_mmu_cache(vma, addr, ptep) \
update_mmu_cache_range(NULL, vma, addr, ptep, 1)
-#define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \
- remap_pfn_range(vma, vaddr, pfn, size, prot)
+#define io_remap_pfn_range_pfn(pfn, size) (pfn)
#endif /* __ASM_CSKY_PGTABLE_H */
diff --git a/arch/mips/alchemy/common/setup.c b/arch/mips/alchemy/common/setup.c
index a7a6d31a7a41..c35b4f809d51 100644
--- a/arch/mips/alchemy/common/setup.c
+++ b/arch/mips/alchemy/common/setup.c
@@ -94,12 +94,13 @@ phys_addr_t fixup_bigphys_addr(phys_addr_t phys_addr, phys_addr_t size)
return phys_addr;
}
-int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long vaddr,
- unsigned long pfn, unsigned long size, pgprot_t prot)
+static inline unsigned long io_remap_pfn_range_pfn(unsigned long pfn,
+ unsigned long size)
{
phys_addr_t phys_addr = fixup_bigphys_addr(pfn << PAGE_SHIFT, size);
- return remap_pfn_range(vma, vaddr, phys_addr >> PAGE_SHIFT, size, prot);
+ return phys_addr >> PAGE_SHIFT;
}
-EXPORT_SYMBOL(io_remap_pfn_range);
+EXPORT_SYMBOL(io_remap_pfn_range_pfn);
+
#endif /* CONFIG_MIPS_FIXUP_BIGPHYS_ADDR */
diff --git a/arch/mips/include/asm/pgtable.h b/arch/mips/include/asm/pgtable.h
index ae73ecf4c41a..9c06a612d33a 100644
--- a/arch/mips/include/asm/pgtable.h
+++ b/arch/mips/include/asm/pgtable.h
@@ -604,9 +604,8 @@ static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
*/
#ifdef CONFIG_MIPS_FIXUP_BIGPHYS_ADDR
phys_addr_t fixup_bigphys_addr(phys_addr_t addr, phys_addr_t size);
-int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long vaddr,
- unsigned long pfn, unsigned long size, pgprot_t prot);
-#define io_remap_pfn_range io_remap_pfn_range
+unsigned long io_remap_pfn_range_pfn(unsigned long pfn, unsigned long size);
+#define io_remap_pfn_range_pfn io_remap_pfn_range_pfn
#else
#define fixup_bigphys_addr(addr, size) (addr)
#endif /* CONFIG_MIPS_FIXUP_BIGPHYS_ADDR */
diff --git a/arch/sparc/include/asm/pgtable_32.h b/arch/sparc/include/asm/pgtable_32.h
index 7c199c003ffe..fd7be02dd46c 100644
--- a/arch/sparc/include/asm/pgtable_32.h
+++ b/arch/sparc/include/asm/pgtable_32.h
@@ -395,12 +395,8 @@ __get_iospace (unsigned long addr)
#define GET_IOSPACE(pfn) (pfn >> (BITS_PER_LONG - 4))
#define GET_PFN(pfn) (pfn & 0x0fffffffUL)
-int remap_pfn_range(struct vm_area_struct *, unsigned long, unsigned long,
- unsigned long, pgprot_t);
-
-static inline int io_remap_pfn_range(struct vm_area_struct *vma,
- unsigned long from, unsigned long pfn,
- unsigned long size, pgprot_t prot)
+static inline unsigned long io_remap_pfn_range_pfn(unsigned long pfn,
+ unsigned long size)
{
unsigned long long offset, space, phys_base;
@@ -408,9 +404,9 @@ static inline int io_remap_pfn_range(struct vm_area_struct *vma,
space = GET_IOSPACE(pfn);
phys_base = offset | (space << 32ULL);
- return remap_pfn_range(vma, from, phys_base >> PAGE_SHIFT, size, prot);
+ return phys_base >> PAGE_SHIFT;
}
-#define io_remap_pfn_range io_remap_pfn_range
+#define io_remap_pfn_range_pfn io_remap_pfn_range_pfn
#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
#define ptep_set_access_flags(__vma, __address, __ptep, __entry, __dirty) \
diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/pgtable_64.h
index 669cd02469a1..f54f385a92c6 100644
--- a/arch/sparc/include/asm/pgtable_64.h
+++ b/arch/sparc/include/asm/pgtable_64.h
@@ -1048,9 +1048,6 @@ int page_in_phys_avail(unsigned long paddr);
#define GET_IOSPACE(pfn) (pfn >> (BITS_PER_LONG - 4))
#define GET_PFN(pfn) (pfn & 0x0fffffffffffffffUL)
-int remap_pfn_range(struct vm_area_struct *, unsigned long, unsigned long,
- unsigned long, pgprot_t);
-
void adi_restore_tags(struct mm_struct *mm, struct vm_area_struct *vma,
unsigned long addr, pte_t pte);
@@ -1084,9 +1081,8 @@ static inline int arch_unmap_one(struct mm_struct *mm,
return 0;
}
-static inline int io_remap_pfn_range(struct vm_area_struct *vma,
- unsigned long from, unsigned long pfn,
- unsigned long size, pgprot_t prot)
+static inline unsigned long io_remap_pfn_range_pfn(unsigned long pfn,
+ unsigned long size)
{
unsigned long offset = GET_PFN(pfn) << PAGE_SHIFT;
int space = GET_IOSPACE(pfn);
@@ -1094,9 +1090,9 @@ static inline int io_remap_pfn_range(struct vm_area_struct *vma,
phys_base = offset | (((unsigned long) space) << 32UL);
- return remap_pfn_range(vma, from, phys_base >> PAGE_SHIFT, size, prot);
+ return phys_base >> PAGE_SHIFT;
}
-#define io_remap_pfn_range io_remap_pfn_range
+#define io_remap_pfn_range_pfn io_remap_pfn_range_pfn
static inline unsigned long __untagged_addr(unsigned long start)
{
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 8e4006eaf4dd..9b65c33bb31a 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3672,15 +3672,35 @@ static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma,
return VM_FAULT_NOPAGE;
}
-#ifndef io_remap_pfn_range
-static inline int io_remap_pfn_range(struct vm_area_struct *vma,
- unsigned long addr, unsigned long pfn,
- unsigned long size, pgprot_t prot)
+#ifdef io_remap_pfn_range_pfn
+static inline unsigned long io_remap_pfn_range_prot(pgprot_t prot)
+{
+ /* We do not decrypt if arch customises PFN. */
+ return prot;
+}
+#else
+static inline unsigned long io_remap_pfn_range_pfn(unsigned long pfn,
+ unsigned long size)
+{
+ return pfn;
+}
+
+static inline pgprot_t io_remap_pfn_range_prot(pgprot_t prot)
{
- return remap_pfn_range(vma, addr, pfn, size, pgprot_decrypted(prot));
+ return pgprot_decrypted(prot);
}
#endif
+static inline int io_remap_pfn_range(struct vm_area_struct *vma,
+ unsigned long addr, unsigned long orig_pfn,
+ unsigned long size, pgprot_t orig_prot)
+{
+ const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size);
+ const pgprot_t prot = io_remap_pfn_range_prot(orig_prot);
+
+ return remap_pfn_range(vma, addr, pfn, size, prot);
+}
+
static inline vm_fault_t vmf_error(int err)
{
if (err == -ENOMEM)
--
2.51.0
Hi Andrew, Could you apply the below fix-patch please? Jason pointed out correctly that pgprot_decrypted() is a noop for the arches in question so there's no need to do anything special with them. Cheers, Lorenzo ----8<---- From 9bd1cafa84108a06db8e2135f5e5b0d3e0bf3859 Mon Sep 17 00:00:00 2001 From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Date: Thu, 18 Sep 2025 07:41:37 +0100 Subject: [PATCH] io_remap_pfn_range_pfn fixup Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> --- arch/csky/include/asm/pgtable.h | 2 -- include/linux/mm.h | 15 ++------------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/arch/csky/include/asm/pgtable.h b/arch/csky/include/asm/pgtable.h index 967c86b38f11..d606afbabce1 100644 --- a/arch/csky/include/asm/pgtable.h +++ b/arch/csky/include/asm/pgtable.h @@ -263,6 +263,4 @@ void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma, #define update_mmu_cache(vma, addr, ptep) \ update_mmu_cache_range(NULL, vma, addr, ptep, 1) -#define io_remap_pfn_range_pfn(pfn, size) (pfn) - #endif /* __ASM_CSKY_PGTABLE_H */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 9b65c33bb31a..08261f2f6244 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3672,23 +3672,12 @@ static inline vm_fault_t vmf_insert_page(struct vm_area_struct *vma, return VM_FAULT_NOPAGE; } -#ifdef io_remap_pfn_range_pfn -static inline unsigned long io_remap_pfn_range_prot(pgprot_t prot) -{ - /* We do not decrypt if arch customises PFN. */ - return prot; -} -#else +#ifndef io_remap_pfn_range_pfn static inline unsigned long io_remap_pfn_range_pfn(unsigned long pfn, unsigned long size) { return pfn; } - -static inline pgprot_t io_remap_pfn_range_prot(pgprot_t prot) -{ - return pgprot_decrypted(prot); -} #endif static inline int io_remap_pfn_range(struct vm_area_struct *vma, @@ -3696,7 +3685,7 @@ static inline int io_remap_pfn_range(struct vm_area_struct *vma, unsigned long size, pgprot_t orig_prot) { const unsigned long pfn = io_remap_pfn_range_pfn(orig_pfn, size); - const pgprot_t prot = io_remap_pfn_range_prot(orig_prot); + const pgprot_t prot = pgprot_decrypted(orig_prot); return remap_pfn_range(vma, addr, pfn, size, prot); } -- 2.51.0
On Wed, Sep 17, 2025 at 08:11:09PM +0100, Lorenzo Stoakes wrote: > -#define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \ > - remap_pfn_range(vma, vaddr, pfn, size, prot) > +#define io_remap_pfn_range_pfn(pfn, size) (pfn) ?? Just delete it? Looks like cargo cult cruft, see below about pgprot_decrypted(). > +#ifdef io_remap_pfn_range_pfn > +static inline unsigned long io_remap_pfn_range_prot(pgprot_t prot) > +{ > + /* We do not decrypt if arch customises PFN. */ > + return prot; pgprot_decrypted() is a NOP on all the arches that use this override, please drop this. Soon future work will require something more complicated to compute if pgprot_decrypted() should be called so this unused stuff isn't going to hold up. Otherwise looks good to me Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Jason
On Wed, Sep 17, 2025 at 06:19:44PM -0300, Jason Gunthorpe wrote: > On Wed, Sep 17, 2025 at 08:11:09PM +0100, Lorenzo Stoakes wrote: > > > -#define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \ > > - remap_pfn_range(vma, vaddr, pfn, size, prot) > > +#define io_remap_pfn_range_pfn(pfn, size) (pfn) > > ?? > > Just delete it? Looks like cargo cult cruft, see below about > pgprot_decrypted(). ?? yourself! I'm not responsible for the code I touch ;) I very obviously did this to prevent pgprot_decrypted() being invoked, keeping the code idempotent to the original. I obviously didn't account for the fact it's a nop on these arches, which is your main point here. Which is a great point and really neatly cleans things up, thanks! > > > +#ifdef io_remap_pfn_range_pfn > > +static inline unsigned long io_remap_pfn_range_prot(pgprot_t prot) > > +{ > > + /* We do not decrypt if arch customises PFN. */ > > + return prot; > > pgprot_decrypted() is a NOP on all the arches that use this override, > please drop this. Yes that's a great insight that I missed, and radically simplifies this. I think my discovering that the PFN is all that varies apart from this + your pedan^W careful review has led us somewhere nice once I drop this stuff. > > Soon future work will require something more complicated to compute if > pgprot_decrypted() should be called so this unused stuff isn't going > to hold up. Right, not sure what you're getting at here, for these arches will be nop, so we're all good? > > Otherwise looks good to me > > Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Thanks! > > Jason
© 2016 - 2025 Red Hat, Inc.