memblock_in_dev() treats a DAX device as one contiguous physical address
range, even when the device has multiple ranges separated by gaps. This
misses blocks in later ranges and may incorrectly include blocks belonging
to another device.
Memory block counts and per-block online/offline operations use this
lookup, so daxctl may operate on memory outside the requested device.
Match blocks against each individual mapping, retaining the single-range
fallback for older kernels. Reset errno before each readdir() so a stale
ENOENT from the mapping lookup is not reported as a directory read error.
Fixes: e8bf803e359b ("libdaxctl: add a 'daxctl_memory' object for memory based
operations")
Signed-off-by: Alison Schofield <[email protected]>
---
daxctl/lib/libdaxctl.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
index f7cbf3fa12e7..1327a2d0afe2 100644
--- a/daxctl/lib/libdaxctl.c
+++ b/daxctl/lib/libdaxctl.c
@@ -1615,6 +1615,7 @@ static int memblock_in_dev(struct daxctl_memory *mem,
const char *memblock)
const char *devname = daxctl_dev_get_devname(dev);
struct daxctl_ctx *ctx = daxctl_dev_get_ctx(dev);
int rc, path_len = mem->buf_len;
+ struct daxctl_mapping *mapping;
unsigned long memblock_size;
char buf[SYSFS_ATTR_SIZE];
unsigned long phys_index;
@@ -1654,8 +1655,18 @@ static int memblock_in_dev(struct daxctl_memory *mem,
const char *memblock)
}
memblock_res = phys_index * memblock_size;
- if (memblock_res >= dev_start && memblock_res <= dev_end)
- return 1;
+ rc = mappings_init(dev);
+ if (rc < 0)
+ return rc;
+
+ /* kernels without mappingN report a single range */
+ if (list_empty(&dev->mappings))
+ return memblock_res >= dev_start && memblock_res <= dev_end;
+
+ daxctl_mapping_foreach(dev, mapping)
+ if (memblock_res >= mapping->start &&
+ memblock_res <= mapping->end)
+ return 1;
return 0;
}
@@ -1716,8 +1727,8 @@ static int daxctl_memory_op(struct daxctl_memory *mem,
enum memory_op op)
if (!node_dir)
return -errno;
- errno = 0;
- while ((de = readdir(node_dir)) != NULL) {
+ /* Helpers may leave errno set even when a block is skipped. */
+ for (errno = 0; (de = readdir(node_dir)) != NULL; errno = 0) {
if (strncmp(de->d_name, "memory", 6) == 0) {
if (strncmp(de->d_name, "memory_", 7) == 0)
continue;
@@ -1734,7 +1745,6 @@ static int daxctl_memory_op(struct daxctl_memory *mem,
enum memory_op op)
if (rc == 0)
count++;
}
- errno = 0;
}
if (status_flags & MEM_ST_ZONE_INCONSISTENT)
--
2.37.3