1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include "dm-core.h"
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun /*
5*4882a593Smuzhiyun * The kobject release method must not be placed in the module itself,
6*4882a593Smuzhiyun * otherwise we are subject to module unload races.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The release method is called when the last reference to the kobject is
9*4882a593Smuzhiyun * dropped. It may be called by any other kernel code that drops the last
10*4882a593Smuzhiyun * reference.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * The release method suffers from module unload race. We may prevent the
13*4882a593Smuzhiyun * module from being unloaded at the start of the release method (using
14*4882a593Smuzhiyun * increased module reference count or synchronizing against the release
15*4882a593Smuzhiyun * method), however there is no way to prevent the module from being
16*4882a593Smuzhiyun * unloaded at the end of the release method.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * If this code were placed in the dm module, the following race may
19*4882a593Smuzhiyun * happen:
20*4882a593Smuzhiyun * 1. Some other process takes a reference to dm kobject
21*4882a593Smuzhiyun * 2. The user issues ioctl function to unload the dm device
22*4882a593Smuzhiyun * 3. dm_sysfs_exit calls kobject_put, however the object is not released
23*4882a593Smuzhiyun * because of the other reference taken at step 1
24*4882a593Smuzhiyun * 4. dm_sysfs_exit waits on the completion
25*4882a593Smuzhiyun * 5. The other process that took the reference in step 1 drops it,
26*4882a593Smuzhiyun * dm_kobject_release is called from this process
27*4882a593Smuzhiyun * 6. dm_kobject_release calls complete()
28*4882a593Smuzhiyun * 7. a reschedule happens before dm_kobject_release returns
29*4882a593Smuzhiyun * 8. dm_sysfs_exit continues, the dm device is unloaded, module reference
30*4882a593Smuzhiyun * count is decremented
31*4882a593Smuzhiyun * 9. The user unloads the dm module
32*4882a593Smuzhiyun * 10. The other process that was rescheduled in step 7 continues to run,
33*4882a593Smuzhiyun * it is now executing code in unloaded module, so it crashes
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * Note that if the process that takes the foreign reference to dm kobject
36*4882a593Smuzhiyun * has a low priority and the system is sufficiently loaded with
37*4882a593Smuzhiyun * higher-priority processes that prevent the low-priority process from
38*4882a593Smuzhiyun * being scheduled long enough, this bug may really happen.
39*4882a593Smuzhiyun *
40*4882a593Smuzhiyun * In order to fix this module unload race, we place the release method
41*4882a593Smuzhiyun * into a helper code that is compiled directly into the kernel.
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun
dm_kobject_release(struct kobject * kobj)44*4882a593Smuzhiyun void dm_kobject_release(struct kobject *kobj)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun complete(dm_get_completion_from_kobject(kobj));
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun EXPORT_SYMBOL(dm_kobject_release);
50