Skip to contentSkip to conversation

mm: use PFN_PHYS() in phys_mem_access_prot()

PATCH Applied Ivan Ezequiel Rodriguez 5 participants 5 messages Nov 13 – Nov 23

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: use PFN_PHYS() in phys_mem_access_prot()

From: Iván Ezequiel Rodriguez <[email protected]>

phys_mem_access_prot() stores "pfn << PAGE_SHIFT" in a phys_addr_t. The shift is evaluated as unsigned long, so on a 32-bit kernel with CONFIG_PHYS_ADDR_T_64BIT an address above 4 GiB is truncated before uncached_access() compares it with __pa(high_memory).

mmap_mem_prepare() already shifts in phys_addr_t, so the mapping is accepted and then left cacheable. PFN_PHYS() casts before the shift.

On a MIPS32 XPA Malta guest, mmap of /dev/mem at 0xf00000000 without O_DSYNC used the boot CCA. The same mapping with O_DSYNC was uncached. With PFN_PHYS() both are uncached.

Fixes 7e6735c3578e ("/dev/mem: use phys_addr_t for physical addresses")
---
 mm/char-mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/char-mem.c +1 -1
index c5761ddedbd7..53ce0034a4c1 100644
@@ -272,7 +272,7 @@ static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 				     unsigned long size, pgprot_t vma_prot)
 {
 #ifdef pgprot_noncached
-	phys_addr_t offset = pfn << PAGE_SHIFT;
+	phys_addr_t offset = PFN_PHYS(pfn);
 
 	if (uncached_access(file, offset))
 		return pgprot_noncached(vma_prot);
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · Ivan Ezequiel Rodriguez

From: Iván Ezequiel Rodriguez <[email protected]>

phys_mem_access_prot() stores "pfn << PAGE_SHIFT" in a phys_addr_t. The shift is evaluated as unsigned long, so on a 32-bit kernel with CONFIG_PHYS_ADDR_T_64BIT an address above 4 GiB is truncated before uncached_access() compares it with __pa(high_memory).

mmap_mem_prepare() already shifts in phys_addr_t, so the mapping is accepted and then left cacheable. PFN_PHYS() casts before the shift.

On a MIPS32 XPA Malta guest, mmap of /dev/mem at 0xf00000000 without O_DSYNC used the boot CCA. The same mapping with O_DSYNC was uncached. With PFN_PHYS() both are uncached.

Fixes 7e6735c3578e ("/dev/mem: use phys_addr_t for physical addresses")
---
 mm/char-mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/char-mem.c +0 -0
index c5761ddedbd7..53ce0034a4c1 100644

mm/char-mem.c static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, · line 278

Quoted 8 lines
mm/char-mem.c +1 -1
@@ -272,7 +272,7 @@ static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 				     unsigned long size, pgprot_t vma_prot)
 {
 #ifdef pgprot_noncached
-	phys_addr_t offset = pfn << PAGE_SHIFT;
+	phys_addr_t offset = PFN_PHYS(pfn);
 
 	if (uncached_access(file, offset))
 		return pgprot_noncached(vma_prot);

I'm relying on Andrew to pick this up.

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Ivan Ezequiel Rodriguez
Quoted 10 lines · Ivan Ezequiel Rodriguez

From: Iván Ezequiel Rodriguez <[email protected]>

phys_mem_access_prot() stores "pfn << PAGE_SHIFT" in a phys_addr_t. The shift is evaluated as unsigned long, so on a 32-bit kernel with CONFIG_PHYS_ADDR_T_64BIT an address above 4 GiB is truncated before uncached_access() compares it with __pa(high_memory).

mmap_mem_prepare() already shifts in phys_addr_t, so the mapping is accepted and then left cacheable. PFN_PHYS() casts before the shift.

Quoted 7 lines

On a MIPS32 XPA Malta guest, mmap of /dev/mem at 0xf00000000 without O_DSYNC used the boot CCA. The same mapping with O_DSYNC was uncached. With PFN_PHYS() both are uncached.

Fixes 7e6735c3578e ("/dev/mem: use phys_addr_t for physical addresses")
Quoted 20 lines
---
 mm/char-mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/char-mem.c +1 -1
index c5761ddedbd7..53ce0034a4c1 100644
@@ -272,7 +272,7 @@ static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 				     unsigned long size, pgprot_t vma_prot)
 {
 #ifdef pgprot_noncached
-	phys_addr_t offset = pfn << PAGE_SHIFT;
+	phys_addr_t offset = PFN_PHYS(pfn);
 
 	if (uncached_access(file, offset))
 		return pgprot_noncached(vma_prot);
Signature · 1 line
2.43.0
Signature · 2 lines
Sincerely yours,
Mike.
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Ivan Ezequiel Rodriguez
Quoted 10 lines · Ivan Ezequiel Rodriguez

From: Iván Ezequiel Rodriguez <[email protected]>

phys_mem_access_prot() stores "pfn << PAGE_SHIFT" in a phys_addr_t. The shift is evaluated as unsigned long, so on a 32-bit kernel with CONFIG_PHYS_ADDR_T_64BIT an address above 4 GiB is truncated before uncached_access() compares it with __pa(high_memory).

mmap_mem_prepare() already shifts in phys_addr_t, so the mapping is accepted and then left cacheable. PFN_PHYS() casts before the shift.

Quoted 7 lines

On a MIPS32 XPA Malta guest, mmap of /dev/mem at 0xf00000000 without O_DSYNC used the boot CCA. The same mapping with O_DSYNC was uncached. With PFN_PHYS() both are uncached.

Fixes 7e6735c3578e ("/dev/mem: use phys_addr_t for physical addresses")

LGTM so:

Quoted 18 lines
---
 mm/char-mem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/char-mem.c +1 -2
index c5761ddedbd7..53ce0034a4c1 100644
@@ -272,7 +272,7 @@ static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
 				     unsigned long size, pgprot_t vma_prot)
 {
 #ifdef pgprot_noncached
-	phys_addr_t offset = pfn << PAGE_SHIFT;
+	phys_addr_t offset = PFN_PHYS(pfn);
 
 	if (uncached_access(file, offset))
 		return pgprot_noncached(vma_prot);
--
Quoted 2 lines

2.43.0

--
Cheers, Lorenzo

maintainer tree notice Applied
#4 lore raw

[merged mm-hotfixes-stable] mm-use-pfn_phys-in-phys_mem_access_prot.patch removed from -mm tree

In reply to Ivan Ezequiel Rodriguez
The quilt patch titled Subject: mm: use PFN_PHYS() in phys_mem_access_prot() has been removed from the -mm tree. Its filename was mm-use-pfn_phys-in-phys_mem_access_prot.patch

The quilt patch titled
Subject: mm: use PFN_PHYS() in phys_mem_access_prot()
has been removed from the -mm tree. Its filename was
mm-use-pfn_phys-in-phys_mem_access_prot.patch

This patch was dropped because it was merged into the mm-hotfixes-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Iván Ezequiel Rodriguez <[email protected]>
Subject: mm: use PFN_PHYS() in phys_mem_access_prot()
Date: Fri, 13 Nov 2026 16:38:12 -0300

phys_mem_access_prot() stores "pfn << PAGE_SHIFT" in a phys_addr_t. The shift is evaluated as unsigned long, so on a 32-bit kernel with CONFIG_PHYS_ADDR_T_64BIT an address above 4 GiB is truncated before uncached_access() compares it with __pa(high_memory).

mmap_mem_prepare() already shifts in phys_addr_t, so the mapping is accepted and then left cacheable. PFN_PHYS() casts before the shift.

On a MIPS32 XPA Malta guest, mmap of /dev/mem at 0xf00000000 without O_DSYNC used the boot CCA. The same mapping with O_DSYNC was uncached. With PFN_PHYS() both are uncached.

Fixes 7e6735c3578e ("/dev/mem: use phys_addr_t for physical addresses")
Cc Liam R. Howlett <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Jann Horn <[email protected]>
Cc Pedro Falcato <[email protected]>
 mm/char-mem.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/char-mem.c renamed from mm/char-mem.c~mm-use-pfn_phys-in-phys_mem_access_prot +1 -1
@@ -272,7 +272,7 @@ static pgprot_t phys_mem_access_prot(str
 				     unsigned long size, pgprot_t vma_prot)
 {
 #ifdef pgprot_noncached
-	phys_addr_t offset = pfn << PAGE_SHIFT;
+	phys_addr_t offset = PFN_PHYS(pfn);
 
 	if (uncached_access(file, offset))
 		return pgprot_noncached(vma_prot);

_

Patches currently in -mm which might be from [email protected] are