daxctl_memory_online_with_zone() is meant to scan the device's blocks
after onlining them and fail with -EBUSY if any block is not in the
requested zone: an agent such as an auto-online policy or a udev rule
may have onlined some blocks first, into a zone daxctl did not ask for.
online_one_memblock() relies on that scan, it ignores a failed write
for a block that someone else onlined ("There will be a full scan for
zone correctness later").

But daxctl_memory_op() returns the number of blocks it onlined, and the
function returns early on any non-zero value, so the scan only ever
runs when no block was onlined at all.  Whenever daxctl does online
memory, a zone mismatch goes unreported.

Example: 
  memory_hotplug.online_policy=auto-movable
  reconfigure-device --no-movable -m system-ram

  => reports success with the memory in ZONE_MOVABLE.

Return early only on errors, and return the number of onlined blocks
after the scan.

Fixes: 0542201b5902 ("daxctl: detect races when onlining memory blocks")
Signed-off-by: "Gregory Price (Meta)" <[email protected]>

---
 daxctl/lib/libdaxctl.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
index 01b1915..f4b2fda 100644
--- a/daxctl/lib/libdaxctl.c
+++ b/daxctl/lib/libdaxctl.c
@@ -1691,7 +1691,7 @@ static int daxctl_memory_online_with_zone(struct 
daxctl_memory *mem,
        struct daxctl_dev *dev = daxctl_memory_get_dev(mem);
        const char *devname = daxctl_dev_get_devname(dev);
        struct daxctl_ctx *ctx = daxctl_dev_get_ctx(dev);
-       int rc;
+       int rc, count;
 
        switch (zone) {
        case MEM_ZONE_MOVABLE:
@@ -1704,8 +1704,9 @@ static int daxctl_memory_online_with_zone(struct 
daxctl_memory *mem,
                err(ctx, "%s: BUG: invalid zone for onlining\n", devname);
                rc = -EINVAL;
        }
-       if (rc)
+       if (rc < 0)
                return rc;
+       count = rc;
 
        /*
         * Detect any potential races when blocks were being brought online by
@@ -1724,7 +1725,7 @@ static int daxctl_memory_online_with_zone(struct 
daxctl_memory *mem,
                return -EBUSY;
        }
 
-       return rc;
+       return count;
 }
 
 DAXCTL_EXPORT int daxctl_memory_online(struct daxctl_memory *mem)


Reply via email to