From: John Groves <[email protected]>

Use __va(phys) instead of virt_addr + linear_offset for the kaddr
return in __fsdev_dax_direct_access(). The previous code added a
device-linear byte offset to virt_addr (which is __va of ranges[0]),
but for multi-range devices with physical gaps between ranges, this
linear arithmetic crosses the gap and produces a wrong kernel virtual
address. Using __va(phys) where phys comes from dax_pgoff_to_phys()
is correct for any range layout because the direct map translates
each physical address independently.

This leaves dev_dax->virt_addr write-only, so remove the field
(suggested by Dave Jiang).

Fixes: 759455848df0b ("dax: Save the kva from memremap")

Reviewed-by: Dave Jiang <[email protected]>
Reviewed-by: Alison Schofield <[email protected]>
Signed-off-by: John Groves <[email protected]>
---
 drivers/dax/dax-private.h | 2 --
 drivers/dax/fsdev.c       | 8 ++------
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/dax/dax-private.h b/drivers/dax/dax-private.h
index 81e4af49e39c1..607a53a91f58b 100644
--- a/drivers/dax/dax-private.h
+++ b/drivers/dax/dax-private.h
@@ -69,7 +69,6 @@ struct dev_dax_range {
  * data while the device is activated in the driver.
  * @region: parent region
  * @dax_dev: core dax functionality
- * @virt_addr: kva from memremap; used by fsdev_dax
  * @cached_size: size of daxdev cached by fsdev_dax
  * @align: alignment of this instance
  * @target_node: effective numa node if dev_dax memory range is onlined
@@ -85,7 +84,6 @@ struct dev_dax_range {
 struct dev_dax {
        struct dax_region *region;
        struct dax_device *dax_dev;
-       void *virt_addr;
        u64 cached_size;
        unsigned int align;
        int target_node;
diff --git a/drivers/dax/fsdev.c b/drivers/dax/fsdev.c
index 0fd5e1293d725..af9ef80c05c6d 100644
--- a/drivers/dax/fsdev.c
+++ b/drivers/dax/fsdev.c
@@ -51,9 +51,7 @@ static long __fsdev_dax_direct_access(struct dax_device 
*dax_dev, pgoff_t pgoff,
        struct dev_dax *dev_dax = dax_get_private(dax_dev);
        size_t size = nr_pages << PAGE_SHIFT;
        size_t offset = pgoff << PAGE_SHIFT;
-       void *virt_addr = dev_dax->virt_addr + offset;
        phys_addr_t phys;
-       unsigned long local_pfn;
 
        phys = dax_pgoff_to_phys(dev_dax, pgoff, size);
        if (phys == -1) {
@@ -63,11 +61,10 @@ static long __fsdev_dax_direct_access(struct dax_device 
*dax_dev, pgoff_t pgoff,
        }
 
        if (kaddr)
-               *kaddr = virt_addr;
+               *kaddr = __va(phys);
 
-       local_pfn = PHYS_PFN(phys);
        if (pfn)
-               *pfn = local_pfn;
+               *pfn = PHYS_PFN(phys);
 
        /*
         * Use cached_size which was computed at probe time. The size cannot
@@ -329,7 +326,6 @@ static int fsdev_dax_probe(struct dev_dax *dev_dax)
                pr_debug("%s: offset detected phys=%llx pgmap_phys=%llx 
offset=%llx\n",
                       __func__, phys, pgmap_phys, data_offset);
        }
-       dev_dax->virt_addr = addr + data_offset;
 
        inode = dax_inode(dax_dev);
        cdev = inode->i_cdev;
-- 
2.53.0



Reply via email to