Repository navigation
devmapper: prevent libdevmapper from deleting /dev/mapper/docker-xxx symbolic links in RemoveDeviceDeferred - #24740
Conversation
|
Thanks @jizhilong for fixing this issue. LGTM |
There was a problem hiding this comment.
small nit /dev/maper should be /dev/mapper. Otherwise LGTM.
|
@shishir-a412ed typo fixed and pr branch rebased. |
|
@shishir-a412ed @rhvgoyal |
There was a problem hiding this comment.
semaphore leak observed in my development environment, I think it is caused by not calling 'UdevWait'.So, plz DONT merge this PR for now!
|
ping @rhvgoyal PR is updated, ptal |
|
ping @rhvgoyal |
|
LGTM @rhvgoyal can you check the latest change one last time and i'll merge if it looks good to you. |
|
@jizhilong disabling library fallback seems odd. I think library does some actions if it detects that udev has not done its job. So if udev is working properly and symlink is deleted when device is gone, library wil not try to cleanup that symlink at all. Can you please dive deeper into this and provide a good explanation of what is happening. |
|
@rhvgoyal you are 'partially' right, but libdevmapper will not always check udev is working properly, it depends on whether a cookie is set in the dm_task and here is the code snippets libdevmapper used to determine whether it should check udev's work and whether it should rely on udev to complete the node operations. /*
* Are we expecting a udev operation to occur that we need to check for?
*/
check_udev = dmt->cookie_set &&
!(dmt->event_nr >> DM_UDEV_FLAGS_SHIFT &
DM_UDEV_DISABLE_DM_RULES_FLAG);
rely_on_udev = dmt->cookie_set ? (dmt->event_nr >> DM_UDEV_FLAGS_SHIFT &
DM_UDEV_DISABLE_LIBRARY_FALLBACK) : 0;so there are three key flags influencing the following code path,
ioctl_with_uevent = dmt->type == DM_DEVICE_RESUME ||
dmt->type == DM_DEVICE_REMOVE ||
dmt->type == DM_DEVICE_RENAME;
if (ioctl_with_uevent && dm_cookie_supported()) {
/*
* Always mark events coming from libdevmapper as
* "primary sourced". This is needed to distinguish
* any spurious events so we can act appropriately.
* This needs to be applied even when udev_sync is
* not used because udev flags could be used alone.
*/
dmi->event_nr |= DM_UDEV_PRIMARY_SOURCE_FLAG <<
DM_UDEV_FLAGS_SHIFT;
/*
* Prevent udev vs. libdevmapper race when processing nodes
* and symlinks. This can happen when the udev rules are
* installed and udev synchronisation code is enabled in
* libdevmapper but the software using libdevmapper does not
* make use of it (by not calling dm_task_set_cookie before).
* We need to instruct the udev rules not to be applied at
* all in this situation so we can gracefully fallback to
* libdevmapper's node and symlink creation code.
*/
if (!dmt->cookie_set && dm_udev_get_sync_support()) {
log_debug_activation("Cookie value is not set while trying to call %s "
"ioctl. Please, consider using libdevmapper's udev "
"synchronisation interface or disable it explicitly "
"by calling dm_udev_set_sync_support(0).",
dmt->type == DM_DEVICE_RESUME ? "DM_DEVICE_RESUME" :
dmt->type == DM_DEVICE_REMOVE ? "DM_DEVICE_REMOVE" :
"DM_DEVICE_RENAME");
log_debug_activation("Switching off device-mapper and all subsystem related "
"udev rules. Falling back to libdevmapper node creation.");
/*
* Disable general dm and subsystem rules but keep
* dm disk rules if not flagged out explicitly before.
* We need /dev/disk content for the software that expects it.
*/
dmi->event_nr |= (DM_UDEV_DISABLE_DM_RULES_FLAG |
DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG) <<
DM_UDEV_FLAGS_SHIFT;
}
}
for a conclution, the value of
if case DM_DEVICE_REMOVE:
/* FIXME Kernel needs to fill in dmi->name */
if (dev_name && !rely_on_udev)
rm_dev_node(dev_name, check_udev, rely_on_udev);
break;so, if we don't set a cookie or don't explictly set with the reproducint script I provided in #24671, this issue can be reproduced, and a I do think this should a issue of libdevmapper, it should check the status of the dm node, if it is in a state of deferred remove, the symbolic link deletion should also be delayed. But since libdevmapper impacts a lot of applications and we can bypass it without much code change, it is better to do the fix in docker for now. BTW, this issue will rarely happen if there are no other process having races with docker daemon on devices under /dev/mapper(we saw it because we'are starting and stopping containers at quite a high rate) |
There was a problem hiding this comment.
If you wait for cookie to complete, will we not wait till udev actually as removed the device and symlink? If yes, then deferred removal does not mean anything. If no, then it bags the question, what's the point of waiting on cookie. just to clean it up?
There was a problem hiding this comment.
libdevmapper use system-v semaphore for synchronization with udev, and the cookie(which is a random string generated when dm_task_set_cookie get called) is used as the semaphore's id.Before libdevmapper do the ioctl syscall, it first create a semaphore with the cookie as id, and increment it by one, after the syscall, it checks ioctl's return value to see whether a DM_UEVENT_GENERATED_FLAG, if not set, libdevmapper knows there is no need to wait for udev completing its job, so it will decrement the semaphore immediately, if set, libdevmapper knows there is a udev event generated by kernel, and udev have some work to do for that event.When udev catches that event, it (I guess) will increment the semaphore first and do its jobs against udev rules, and finally decrement the semaphore.What libdevmapper actually do in dm_udev_wait is undo its semaphore increment first, and repeatedly wait for the semaphore's value decrements to zero.
For our case, if the target device is not busy, the kernel will remove it immediately, which acts the same as a normal remove operation, so call UdevWait totally make sense. When the device is busy, kernel will mark the device as deferred remove, and no udev event generated, the semaphore's value will be decremented right after the ioctl syscall, and what UdevWait do later is merely nothing but cleaning up the semaphore.
|
cc @gibarian |
|
cc @prajnoha |
|
@jizhilong Ok, your reasoning sounds good given current code. I talked to libdm developer @prajnoha and he said that he will add some code so that device node symlink re-appears when deferred device removal is cancelled. But as it will take some time for fix to be available and show up in various distros, in the mean time it makes sense to keep your patches. Can you please merge all 3 patches in to one. Provide a very good changelong explaining libdm behavior and what are we doing in short term. I want to make sure important points are captured so that we are not confused later. And some of those points should be captured in code so that a developer later can read it and understand this special case. |
…veDeviceDeferred if there is no cookie set in dm task, or flag DM_UDEV_DISABLE_LIBRARY_FALLBACK is cleared for a DM_DEV_REMOVE task, libdevmapper will fallback to clean up the symlink under /dev/mapper by itself, no matter the device removal is executed immediately or deferred by the kernel.In some cases, the removal is deferred by the kernel, while the symlink is deleted directly by libdevmapper, when docker tries to activate the device again, the deferred removal will be canceld, but the symlink will not show up again, so docker's attempt to mount the device by the symlink will fail, and it will eventually leads to a `docker start/diff` error. Fixes #24671 Signed-off-by: Ji.Zhilong <[email protected]>
|
@rhvgoyal commits squashed/rebased, and I also updated the |
|
@jizhilong thanks for the quick update |
|
LGTM |
|
@crosbymichael Can this fix be backported to 1.12? |
|
@rhvgoyal if we do another patch release we could backport it. We already released 1.12.1 and i'm not sure if we should consider this a priority 0 where we need to do a 1.12.2 release. What do you think? |
|
@crosbymichael I agree that this can be included if you decide to 1.12.2. Given deferred removal is not enabled by default and its a race condition, I would not consider this patch alone to be reason for 1.12.2 |
|
@rhvgoyal ok, thanks. If we end up doing another patch release we can backport this change. |
…veDeviceDeferred if there is no cookie set in dm task, or flag DM_UDEV_DISABLE_LIBRARY_FALLBACK is cleared for a DM_DEV_REMOVE task, libdevmapper will fallback to clean up the symlink under /dev/mapper by itself, no matter the device removal is executed immediately or deferred by the kernel.In some cases, the removal is deferred by the kernel, while the symlink is deleted directly by libdevmapper, when docker tries to activate the device again, the deferred removal will be canceld, but the symlink will not show up again, so docker's attempt to mount the device by the symlink will fail, and it will eventually leads to a `docker start/diff` error. Fixes moby#24671 cherry-picked from docker upstream: moby#24740 Signed-off-by: Ji.Zhilong <[email protected]> Signed-off-by: Deng Guangxing <[email protected]>
- What I did
Fixes #24671 by preventing libdevmapper from deleting
/dev/mapper/docker-xxxxxlinks whendm.use_deferred_removeis enabled.In the original implementation, libdevmapper will delete the symbolic link even if the device removal is deferred by the kernel, which will leave the device being exist but the symbolic link being lost, and docker will fail to activate and mount the device again.
By preventing libdevmapper from doing this, and leave all symlink handling to udev,
/dev/mapper/docker-xxxlink's lifecycle will always be synced with the corresponding/dev/dm-xdevice.- How I did it
I did it by set a dm task cookie and a
DmUdevDisableLibraryFallbacktask flag, liddevmapper will check these two variables to see if it need to delete the symlink by itself(ref).- How to verify it
please refer to the way to produce section in #24671
- Description for the changelog
devicemapper: Fix the issue that device symlinks may get lost when
dm.use_deferred_removalis enabled. #24740