Ignore image resize events after disconnect in vitastor-nbd and vitastor-ublk #156
Reference in New Issue
Block a user
No description provided.
Delete Branch "koolkhel/vitastor:nbd-resize-after-disc"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
vitastor-nbdkeeps its image watch callback after the NBD device is disconnected: the main loop ends, butcli->flush()still runs the event loop. If the image is resized in that time,on_change_watch()starts the resize thread and it callsNBD_SET_SIZEon/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
4ee30fa7withtests/run_3osds.sh, defaultimmediate_commit=none:pg_size2,pg_minsize2; pool 2 withpg_minsize1. Image A is 100M in pool 1, image B is 50M in pool 2./dev/nbd0), write 4k to it without a sync.vitastor-nbd unmap /dev/nbd0. The device is disconnected, the process stays incli->flush()./dev/nbd0,/sys/block/nbd0/sizeis 102400.vitastor-cli modify A --resize 200M.On master
/sys/block/nbd0/sizebecomes 409600, although the device serves the 50M image B (2 runs of 2 on 4ee30fa7; 2 of 2 onde3644bcin a VM with Ubuntu 24.04, kernel 6.8.0-142). With this PR it stays 102400 (2 runs of 2 onde3644bcin that VM).The fix closes the watch with
close_watch()when the main loop ends with the device disconnected (nbd_fd < 0: afterNBD_CMD_DISCor 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-ublkcan't hit a device of someone else this way. The ublk device number is freed only when the last reference to/dev/ublkcNgoes (ublk_cdev_rel()), and the server keeps that file open until it exits. Its online resize had two other bugs, though:try_resize()setsize_changedto true instead of false. After the first resize every ring loop iteration sent anotherUBLK_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 noUPDATE_SIZE.unmaphad stopped the device.UPDATE_SIZEon a stopped device dereferences a NULLub_diskin 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 oncestopis 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 duringmapsentUPDATE_SIZEbefore the device had a disk: the same NULLub_diskon unpatched 6.16-6.19, and on patched kernels the command is refused and the device keeps the size given inSET_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:
vitastor-ublk, then step 6)b411d71e)Contributor Name
Yury Luneff (MIND Software LLC, https://mindsw.io)
CLA
Ну watch->callback = NULL это наглость какая-то, нельзя так, закрыть watch нормально тогда надо
5a5ebf8dc5to8ac1eea221try_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.Ignore image resize events after NBD disconnect in vitastor-nbdto Ignore image resize events after disconnect in vitastor-nbd and vitastor-ublk