[RFC PATCH 1/5] dax/device: validate mmap offset alignment against dev_dax->align

From: Sourav Panda

Date: Sun Nov 29 2026 - 20:24:09 EST


__check_vma() requires the start and end of a device-dax mapping to be
aligned to dev_dax->align, but it never looks at the file offset. A
caller that supplies an aligned address, either as a hint that happens
to be honoured or with MAP_FIXED, together with an offset that is only
page aligned gets a mapping. Its PMD and PUD faults then resolve to a
physical address that is not aligned to the fault size.

Reject offsets that are not a multiple of dev_dax->align at mmap() time.
Also make the PMD and PUD fault handlers refuse to install an entry for
an unaligned physical address, as a backstop in case an aligned offset
still translates to one.

Fixes: dee410792419 ("/dev/dax, core: file operations and dax-mmap")
Signed-off-by: Sourav Panda <souravpanda@xxxxxxxxxx>
---
drivers/dax/device.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/drivers/dax/device.c b/drivers/dax/device.c
index e52f4886fbaa4..9c956085d9122 100644
--- a/drivers/dax/device.c
+++ b/drivers/dax/device.c
@@ -14,8 +14,8 @@
#include "bus.h"

static int __check_vma(struct dev_dax *dev_dax, vma_flags_t flags,
- unsigned long start, unsigned long end, struct file *file,
- const char *func)
+ unsigned long start, unsigned long end, pgoff_t pgoff,
+ struct file *file, const char *func)
{
struct device *dev = &dev_dax->dev;
unsigned long mask;
@@ -39,6 +39,12 @@ static int __check_vma(struct dev_dax *dev_dax, vma_flags_t flags,
mask);
return -EINVAL;
}
+ if (pgoff & (mask >> PAGE_SHIFT)) {
+ dev_info_ratelimited(dev,
+ "%s: %s: fail, unaligned offset (%#lx, %#lx)\n",
+ current->comm, func, pgoff, mask >> PAGE_SHIFT);
+ return -EINVAL;
+ }

if (!file_is_dax(file)) {
dev_info_ratelimited(dev,
@@ -54,7 +60,7 @@ static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma,
const char *func)
{
return __check_vma(dev_dax, vma->flags, vma->vm_start, vma->vm_end,
- vma->vm_file, func);
+ vma->vm_pgoff, vma->vm_file, func);
}

static void dax_set_mapping(struct vm_fault *vmf, unsigned long pfn,
@@ -148,7 +154,7 @@ static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax,

pgoff = linear_page_index(vmf->vma, pmd_addr);
phys = dax_pgoff_to_phys(dev_dax, pgoff, PMD_SIZE);
- if (phys == -1) {
+ if (phys == -1 || !IS_ALIGNED(phys, fault_size)) {
dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
return VM_FAULT_SIGBUS;
}
@@ -194,7 +200,7 @@ static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,

pgoff = linear_page_index(vmf->vma, pud_addr);
phys = dax_pgoff_to_phys(dev_dax, pgoff, PUD_SIZE);
- if (phys == -1) {
+ if (phys == -1 || !IS_ALIGNED(phys, fault_size)) {
dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
return VM_FAULT_SIGBUS;
}
@@ -283,8 +289,8 @@ static int dax_mmap_prepare(struct vm_area_desc *desc)
* fault time.
*/
id = dax_read_lock();
- rc = __check_vma(dev_dax, desc->vma_flags, desc->start, desc->end, filp,
- __func__);
+ rc = __check_vma(dev_dax, desc->vma_flags, desc->start, desc->end,
+ desc->pgoff, filp, __func__);
dax_read_unlock(id);
if (rc)
return rc;
--
2.56.1.839.g22a8290db9-goog