[PATCH] x86/devmem: Honor O_DSYNC when PAT is disabled

From: Prasanna Panchamukhi

Date: Thu Dec 03 2026 - 19:52:08 EST


Upstream commit 1b3f2bd04d90
("x86/devmem: Remove duplicate range_is_allowed() definition") introduced
a regression by adding a PAT-disabled early return to
phys_mem_access_prot_allowed(). This skips the O_DSYNC check and
cache-mode selection. O_SYNC sets the O_DSYNC flag bit, so skipping this
check also affects /dev/mem mappings opened with O_SYNC.

Userspace applications may access memory-mapped device registers by
opening /dev/mem with O_SYNC and mapping the register range with mmap().
For example, accessing device status and FIFO registers requires
uncached mappings to avoid stale reads or delayed writes.

Previously, with CONFIG_STRICT_DEVMEM=y, the local range_is_allowed()
helper returned success unconditionally because drivers/char/mem.c
already checked access permissions. With CONFIG_STRICT_DEVMEM=n, the
PAT-disabled early return was confined to that helper. In both cases,
phys_mem_access_prot_allowed() continued to cache-mode selection.

Consequently, on systems booted with nopat, these mappings can retain
write-back protection instead of the requested UC-minus protection.
Disabling PAT does not disable CPU caches, so selecting the appropriate
page-table cache attributes remains necessary.

Gate the range check on pat_enabled() instead of returning early.
This restores O_DSYNC cache-mode selection when PAT is disabled while
preserving the behavior with PAT enabled. The separate permission check
in drivers/char/mem.c remains unchanged.

Fixes: 1b3f2bd04d90 ("x86/devmem: Remove duplicate range_is_allowed() definition")
Signed-off-by: Prasanna S Panchamukhi <panchamukhi@xxxxxxxxx>
Cc: Dan Williams <dan.j.williams@xxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: Nikolay Borisov <nik.borisov@xxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Cc: Venki Pallipadi <venkatesh.pallipadi@xxxxxxxxx>
Cc: Ravikiran G Thirumalai <kiran@xxxxxxxxxxxx>
Cc: Toshi Kani <toshi.kani@xxxxxxx>
Cc: Suresh Siddha <suresh.b.siddha@xxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
---
arch/x86/mm/pat/memtype.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c
index cf94fb561310..15e4df64da71 100644
--- a/arch/x86/mm/pat/memtype.c
+++ b/arch/x86/mm/pat/memtype.c
@@ -786,10 +786,8 @@ int phys_mem_access_prot_allowed(struct file *file, unsigned long pfn,
{
enum page_cache_mode pcm = _PAGE_CACHE_MODE_WB;

- if (!pat_enabled())
- return 1;
-
- if (!range_is_allowed(pfn, size))
+ /* Honor O_DSYNC cache mode selection even when PAT is disabled. */
+ if (pat_enabled() && !range_is_allowed(pfn, size))
return 0;

if (file->f_flags & O_DSYNC)

base-commit: 007722b8211f7b34fdf21b1e6a9c32497633ea29
--
2.50.1 (Apple Git-155)