Ignore image resize events after disconnect in vitastor-nbd and vitastor-ublk #156

Merged
vitalif merged 3 commits from koolkhel/vitastor:nbd-resize-after-disc into master 2026-11-25 14:42:35 +03:00
Contributor

Description

vitastor-nbd keeps its image watch callback after the NBD device is disconnected: the main loop ends, but cli->flush() still runs the event loop. If the image is resized in that time, on_change_watch() starts the resize thread and it calls NBD_SET_SIZE on /dev/nbdN. The device is not ours any more, and it may already be mapped to another image, which then gets the wrong size.

The final flush is usually short, but it waits when the PG is not active, so the old process can stay for a long time after unmap.

Reproduced on master 4ee30fa7 with tests/run_3osds.sh, default immediate_commit=none:

  1. Pool 1 with 1 PG, pg_size 2, pg_minsize 2; pool 2 with pg_minsize 1. Image A is 100M in pool 1, image B is 50M in pool 2.
  2. Map A (/dev/nbd0), write 4k to it without a sync.
  3. Kill the primary OSD of the PG of pool 1. The PG is not active any more.
  4. vitastor-nbd unmap /dev/nbd0. The device is disconnected, the process stays in cli->flush().
  5. Map B. It gets /dev/nbd0, /sys/block/nbd0/size is 102400.
  6. vitastor-cli modify A --resize 200M.

On master /sys/block/nbd0/size becomes 409600, although the device serves the 50M image B (2 runs of 2 on 4ee30fa7; 2 of 2 on de3644bc in a VM with Ubuntu 24.04, kernel 6.8.0-142). With this PR it stays 102400 (2 runs of 2 on de3644bc in that VM).

The fix closes the watch with close_watch() when the main loop ends with the device disconnected (nbd_fd < 0: after NBD_CMD_DISC or a hang-up), before the flush. After SIGINT or SIGTERM the device is still connected and the flush still reads requests, which use the watch, so it stays open there.

Tested by hand with a script that does the steps above. I did not add it to tests/: it needs root, two pools and a free /dev/nbd0. I can add it if you want it there.

vitastor-ublk (second and third commits)

Checked on request: vitastor-ublk can't hit a device of someone else this way. The ublk device number is freed only when the last reference to /dev/ublkcN goes (ublk_cdev_rel()), and the server keeps that file open until it exits. Its online resize had two other bugs, though:

  • try_resize() set size_changed to true instead of false. After the first resize every ring loop iteration sent another UBLK_U_CMD_UPDATE_SIZE: 260k-1.1M commands in 10 s at 80-95% CPU, with a log line for each on 6.8, which has no UPDATE_SIZE.
  • The commands went on in the final flush after unmap had stopped the device. UPDATE_SIZE on a stopped device dereferences a NULL ub_disk in 6.16-6.19 kernels without 25966fc09769 ("ublk: fix NULL pointer dereference in ublk_ctrl_set_size()").

The fix resets the flag, submits the command at once (wait() does not submit) and sends no resize once stop is set.

The third commit closes the same window at the start. The ring consumer is registered before START_DEV, and the synchronous control commands run the ring loop, so a resize event that arrived during map sent UPDATE_SIZE before the device had a disk: the same NULL ub_disk on unpatched 6.16-6.19, and on patched kernels the command is refused and the device keeps the size given in SET_PARAMS. The resize now waits until the device is started. That window is a few milliseconds and was not reproduced; it follows from the code.

Tested in the same VM (kernel 6.8, so a sent command shows as "Operation not supported" in the log), 2 runs of 2 per binary:

Binary Commands after one resize Commands to the stopped device when the image is resized during the final flush (steps 1-4 with vitastor-ublk, then step 6)
master 260k-1.1M in 10 s, 80-95% CPU 180k-580k
only the flag reset 1, no CPU 1
this PR (also with the third commit, b411d71e) 1, no CPU 0

Contributor Name

Yury Luneff (MIND Software LLC, https://mindsw.io)

CLA

### Description `vitastor-nbd` keeps its image watch callback after the NBD device is disconnected: the main loop ends, but `cli->flush()` still runs the event loop. If the image is resized in that time, `on_change_watch()` starts the resize thread and it calls `NBD_SET_SIZE` on `/dev/nbdN`. The device is not ours any more, and it may already be mapped to another image, which then gets the wrong size. The final flush is usually short, but it waits when the PG is not active, so the old process can stay for a long time after `unmap`. Reproduced on master 4ee30fa7 with `tests/run_3osds.sh`, default `immediate_commit=none`: 1. Pool 1 with 1 PG, `pg_size` 2, `pg_minsize` 2; pool 2 with `pg_minsize` 1. Image A is 100M in pool 1, image B is 50M in pool 2. 2. Map A (`/dev/nbd0`), write 4k to it without a sync. 3. Kill the primary OSD of the PG of pool 1. The PG is not active any more. 4. `vitastor-nbd unmap /dev/nbd0`. The device is disconnected, the process stays in `cli->flush()`. 5. Map B. It gets `/dev/nbd0`, `/sys/block/nbd0/size` is 102400. 6. `vitastor-cli modify A --resize 200M`. On master `/sys/block/nbd0/size` becomes 409600, although the device serves the 50M image B (2 runs of 2 on 4ee30fa7; 2 of 2 on de3644bc in a VM with Ubuntu 24.04, kernel 6.8.0-142). With this PR it stays 102400 (2 runs of 2 on de3644bc in that VM). The fix closes the watch with `close_watch()` when the main loop ends with the device disconnected (`nbd_fd < 0`: after `NBD_CMD_DISC` or a hang-up), before the flush. After SIGINT or SIGTERM the device is still connected and the flush still reads requests, which use the watch, so it stays open there. Tested by hand with a script that does the steps above. I did not add it to `tests/`: it needs root, two pools and a free `/dev/nbd0`. I can add it if you want it there. ### vitastor-ublk (second and third commits) Checked on request: `vitastor-ublk` can't hit a device of someone else this way. The ublk device number is freed only when the last reference to `/dev/ublkcN` goes (`ublk_cdev_rel()`), and the server keeps that file open until it exits. Its online resize had two other bugs, though: - `try_resize()` set `size_changed` to true instead of false. After the first resize every ring loop iteration sent another `UBLK_U_CMD_UPDATE_SIZE`: 260k-1.1M commands in 10 s at 80-95% CPU, with a log line for each on 6.8, which has no `UPDATE_SIZE`. - The commands went on in the final flush after `unmap` had stopped the device. `UPDATE_SIZE` on a stopped device dereferences a NULL `ub_disk` in 6.16-6.19 kernels without 25966fc09769 ("ublk: fix NULL pointer dereference in ublk_ctrl_set_size()"). The fix resets the flag, submits the command at once (`wait()` does not submit) and sends no resize once `stop` is set. The third commit closes the same window at the start. The ring consumer is registered before `START_DEV`, and the synchronous control commands run the ring loop, so a resize event that arrived during `map` sent `UPDATE_SIZE` before the device had a disk: the same NULL `ub_disk` on unpatched 6.16-6.19, and on patched kernels the command is refused and the device keeps the size given in `SET_PARAMS`. The resize now waits until the device is started. That window is a few milliseconds and was not reproduced; it follows from the code. Tested in the same VM (kernel 6.8, so a sent command shows as "Operation not supported" in the log), 2 runs of 2 per binary: | Binary | Commands after one resize | Commands to the stopped device when the image is resized during the final flush (steps 1-4 with `vitastor-ublk`, then step 6) | |---|---|---| | master | 260k-1.1M in 10 s, 80-95% CPU | 180k-580k | | only the flag reset | 1, no CPU | 1 | | this PR (also with the third commit, b411d71e) | 1, no CPU | 0 | ### Contributor Name Yury Luneff (MIND Software LLC, https://mindsw.io) ### CLA - [x] I accept Vitastor CLA agreement: https://git.yourcmc.ru/vitalif/vitastor/src/branch/master/CLA-en.md
koolkhel added 1 commit 2026-11-23 04:42:23 +03:00
vitastor-nbd kept its image watch callback during the final flush,
after the NBD device was already disconnected. A resize of the image
in that time made it call NBD_SET_SIZE on the device, which by then
could be mapped again to another image and so got the wrong size.
vitalif requested changes 2026-11-24 12:38:31 +03:00
vitalif left a comment
Owner

Ну watch->callback = NULL это наглость какая-то, нельзя так, закрыть watch нормально тогда надо

Ну watch->callback = NULL это наглость какая-то, нельзя так, закрыть watch нормально тогда надо
koolkhel force-pushed nbd-resize-after-disc from 5a5ebf8dc5 to 8ac1eea221 2026-11-24 13:06:03 +03:00 Compare
koolkhel added 1 commit 2026-11-24 22:59:27 +03:00
try_resize() set size_changed to true instead of false, so after the
first online resize every ring loop iteration sent another
UBLK_U_CMD_UPDATE_SIZE: about 100k commands per second at full CPU, and
a log line for each on kernels without UPDATE_SIZE.

The commands also went on in the final flush after unmap had stopped
the device. UPDATE_SIZE on a stopped device crashes 6.16-6.19 kernels
without 25966fc09769 ("ublk: fix NULL pointer dereference in
ublk_ctrl_set_size()"). Don't resize once the server is stopping, and
submit the command at once rather than with some later submit.
koolkhel changed title from Ignore image resize events after NBD disconnect in vitastor-nbd to Ignore image resize events after disconnect in vitastor-nbd and vitastor-ublk 2026-11-24 22:59:48 +03:00
koolkhel added 1 commit 2026-11-25 11:12:28 +03:00
The ring consumer is registered before START_DEV, so that the server
answers the I/O the kernel sends while the disk is added, and the
synchronous control commands run the ring loop. A resize event that
arrived before the device was started therefore sent UPDATE_SIZE to a
device without a disk: kernels 6.16-6.19 without 25966fc09769
dereference NULL there, and patched kernels refuse it, so the device
kept the size given in SET_PARAMS. Keep the resize pending until the
device is started.
vitalif merged commit 5133d5caba into master 2026-11-25 14:42:35 +03:00
Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/vitastor#156
No description provided.