Skip to content

devmapper: prevent libdevmapper from deleting /dev/mapper/docker-xxx symbolic links in RemoveDeviceDeferred - #24740

Merged
crosbymichael merged 1 commit into
moby:masterfrom
jizhilong:devmapper-deferred-remove
Aug 23, 2016
Merged

crosbymichael merged 1 commit into
moby:masterfrom
jizhilong:devmapper-deferred-remove

Conversation

@jizhilong

@jizhilong jizhilong commented Jul 18, 2016 •

Copy link
Copy Markdown
Contributor

- What I did
Fixes #24671 by preventing libdevmapper from deleting /dev/mapper/docker-xxxxx links when dm.use_deferred_remove is 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-xxx link's lifecycle will always be synced with the corresponding /dev/dm-x device.

- How I did it
I did it by set a dm task cookie and a DmUdevDisableLibraryFallback task 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_removal is enabled. #24740

@GordonTheTurtle GordonTheTurtle added status/0-triage dco/no Automatically set by a bot when one of the commits lacks proper signature and removed dco/no Automatically set by a bot when one of the commits lacks proper signature labels Jul 18, 2016
@runcom

runcom commented Jul 18, 2016

Copy link
Copy Markdown
Member

/cc @rhvgoyal @shishir-a412ed

@rhvgoyal

Copy link
Copy Markdown
Contributor

Thanks @jizhilong for fixing this issue.

LGTM

Comment thread pkg/devicemapper/devmapper.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small nit /dev/maper should be /dev/mapper. Otherwise LGTM.

@jizhilong

Copy link
Copy Markdown
Contributor Author

@shishir-a412ed typo fixed and pr branch rebased.

@GordonTheTurtle GordonTheTurtle added dco/no Automatically set by a bot when one of the commits lacks proper signature and removed dco/no Automatically set by a bot when one of the commits lacks proper signature labels Jul 21, 2016
@jizhilong

Copy link
Copy Markdown
Contributor Author

@shishir-a412ed @rhvgoyal
I noticed only setting a task cookie is not enough to prevent libdevmapper from deleting /dev/mapper/dodkcer-xxxx symlinks by itself, we should also add a DmUdevDisableLibraryFallback flag to the dm task.
I made a new commit for DmUdevDisableLibraryFallback and pushed to the PR branch, should I also update the title and the content of the PR?

@jizhilong jizhilong changed the title devmapper: set task cookie in RemoveDeviceDeferred [WIP]devmapper: set task cookie in RemoveDeviceDeferred Jul 26, 2016
Comment thread pkg/devicemapper/devmapper.go Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

semaphore leak observed in my development environment, I think it is caused by not calling 'UdevWait'.So, plz DONT merge this PR for now!

@jizhilong jizhilong changed the title [WIP]devmapper: set task cookie in RemoveDeviceDeferred devmapper: prevent libdevmapper from deleting /dev/mapper/docker-xxx symbolic links in RemoveDeviceDeferred Jul 27, 2016
@thaJeztah

Copy link
Copy Markdown
Member

ping @rhvgoyal PR is updated, ptal

@LK4D4

LK4D4 commented Aug 5, 2016

Copy link
Copy Markdown
Contributor

ping @rhvgoyal

@crosbymichael

Copy link
Copy Markdown
Contributor

LGTM

@rhvgoyal can you check the latest change one last time and i'll merge if it looks good to you.

@rhvgoyal

Copy link
Copy Markdown
Contributor

@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.

@jizhilong

Copy link
Copy Markdown
Contributor Author

@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 DM_UDEV_DISABLE_LIBRARY_FALLBACK is turned on or not.

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,

  • dmt->cookie_set, a flag indicating a cookie is set in the dm_task structure or not, which is totally and directly determined by the library user.
  • DM_UDEV_DISABLE_DM_RULES_FLAG, indicates whether udev rules are disabled or not, which can be set explicitly set by the library user(not happen in our scenario) OR inferred by libdevmapper by checking the presence of dm cookie, without dm cookie provided, DM_UDEV_DISABLE_DM_RULES_FLAG will always be set, following is the corresponding code snippets:
    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;
        }
    }
  • DM_UDEV_DISABLE_LIBRARY_FALLBACK, which can be explicitly set by the library user.

for a conclution, the value of check_udev and rely_on_udev will be eventually determined by the value of cookie_set and DM_UDEV_DISABLE_LIBRARY_FALLBACK:

cookie_set==true cookie_set == false
DM_UDEV_DISABLE_LIBRARY_FALLBACK=true check_udev==true rely_on_udev==true check_udev==false rely_on_udev==false
DM_UDEV_DISABLE_LIBRARY_FALLBACK=false check_udev==true rely_on_udev==false check_udev==false rely_on_udev==false

if rely_on_udev is true, libdevmapper wil bypass node operations, and udev will delete the symlink when kernel execute the deferred removal eventually.If rely_on_dev is false, libdevmapper will try to delete the symlink with no regarding to it's a deferred DM_DEV_REMOVE or immediate DM_DEV_REMOVE,code:

    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 DM_UDEV_DISABLE_LIBRARY_FALLBACK, rely_on_udev will always be true, and libdevmapper will always try to delete symlinks, even if the actual node removal was deferred by the kernel.

with the reproducint script I provided in #24671, this issue can be reproduced, and a strace -e trace=unlink on the docker damon process confirmed my hypothesis that docker daemon(libdevmapper) will delete symlinks by itself.

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)

Comment thread pkg/devicemapper/devmapper.go Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rhvgoyal

Copy link
Copy Markdown
Contributor

cc @gibarian

@rhvgoyal

Copy link
Copy Markdown
Contributor

cc @prajnoha

@rhvgoyal

Copy link
Copy Markdown
Contributor

@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]>
@jizhilong

jizhilong commented Aug 23, 2016 •

Copy link
Copy Markdown
Contributor Author

@rhvgoyal commits squashed/rebased, and I also updated the ChangeLog section in the PR description.I inserted several comments trying to explain why we should set a cookie and set the DmUdevDisableLibraryFallback flag and call UdevWait(cookie), I'm not quite sure if they are clear enough or are those comments redundant since we have linked to this PR in the commit message.Anyway, please feel free to ping me for any suggestions, thanks for your reviewing.

@crosbymichael

Copy link
Copy Markdown
Contributor

@jizhilong thanks for the quick update

@crosbymichael

Copy link
Copy Markdown
Contributor

LGTM

@crosbymichael
crosbymichael merged commit 684d3f0 into moby:master Aug 23, 2016
@jizhilong
jizhilong deleted the devmapper-deferred-remove branch August 23, 2016 22:25
@rhvgoyal

Copy link
Copy Markdown
Contributor

@crosbymichael Can this fix be backported to 1.12?

@crosbymichael

Copy link
Copy Markdown
Contributor

@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?

@rhvgoyal

Copy link
Copy Markdown
Contributor

@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

@crosbymichael

Copy link
Copy Markdown
Contributor

@rhvgoyal ok, thanks. If we end up doing another patch release we can backport this change.

@crosbymichael crosbymichael added this to the 1.12.2 milestone Aug 30, 2016
@vieux vieux mentioned this pull request Sep 26, 2016
liusdu pushed a commit to liusdu/moby that referenced this pull request Oct 30, 2017
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants