Linux virtualization list
 help / color / mirror / Atom feed
From: Sourav Panda <souravpanda@google.com>
To: djbw@kernel.org, vishal.l.verma@intel.com, dave.jiang@intel.com,
	 alison.schofield@intel.com, pankaj.gupta.linux@gmail.com,
	mst@redhat.com,  jasowangio@gmail.com, eperezma@redhat.com,
	viro@zeniv.linux.org.uk,  brauner@kernel.org
Cc: iweiny@kernel.org, jack@suse.cz, xuanzhuo@linux.alibaba.com,
	 shuah@kernel.org, david@kernel.org, souravpanda@google.com,
	 nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org,
	 virtualization@lists.linux.dev, linux-fsdevel@vger.kernel.org,
	 linux-kselftest@vger.kernel.org, linux-mm@kvack.org,
	 linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/5] dax/device: validate mmap offset alignment against dev_dax->align
Date: Mon, 30 Nov 2026 01:23:50 +0000	[thread overview]
Message-ID: <20261130012354.1894611-2-souravpanda@google.com> (raw)
In-Reply-To: <20261130012354.1894611-1-souravpanda@google.com>

__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@google.com>
---
 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


  reply	other threads:[~2026-11-30  1:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  1:23 [RFC PATCH 0/5] virtio-pmem & device-dax: host-backed range discard via fallocate() / MADV_REMOVE Sourav Panda
2026-11-30  1:23 ` Sourav Panda [this message]
2026-11-30  1:23 ` [RFC PATCH 2/5] virtio_pmem: add VIRTIO_PMEM_F_DISCARD and region discard hook Sourav Panda
2026-11-30  1:23 ` [RFC PATCH 3/5] vfs: allow fallocate() on character devices implementing ->fallocate() Sourav Panda
2026-11-30 13:55   ` Jan Kara
2026-11-30 18:08     ` Sourav Panda
2026-11-30  1:23 ` [RFC PATCH 4/5] dax/device: add fallocate() discard support Sourav Panda
2026-11-30  1:23 ` [RFC PATCH 5/5] selftests/dax: add devdax_discard tests Sourav Panda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130012354.1894611-2-souravpanda@google.com \
    --to=souravpanda@google.com \
    --cc=alison.schofield@intel.com \
    --cc=brauner@kernel.org \
    --cc=dave.jiang@intel.com \
    --cc=david@kernel.org \
    --cc=djbw@kernel.org \
    --cc=eperezma@redhat.com \
    --cc=iweiny@kernel.org \
    --cc=jack@suse.cz \
    --cc=jasowangio@gmail.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mst@redhat.com \
    --cc=nvdimm@lists.linux.dev \
    --cc=pankaj.gupta.linux@gmail.com \
    --cc=shuah@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=virtualization@lists.linux.dev \
    --cc=vishal.l.verma@intel.com \
    --cc=xuanzhuo@linux.alibaba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox