1DELETE_MODULE(2) Linux Programmer's Manual DELETE_MODULE(2)
2
3
4
6 delete_module - unload a kernel module
7
9 #include <fcntl.h> /* Definition of O_* constants */
10 #include <sys/syscall.h> /* Definition of SYS_* constants */
11 #include <unistd.h>
12
13 int syscall(SYS_delete_module, const char *name, unsigned int flags);
14
15 Note: glibc provides no wrapper for delete_module(), necessitating the
16 use of syscall(2).
17
19 The delete_module() system call attempts to remove the unused loadable
20 module entry identified by name. If the module has an exit function,
21 then that function is executed before unloading the module. The flags
22 argument is used to modify the behavior of the system call, as de‐
23 scribed below. This system call requires privilege.
24
25 Module removal is attempted according to the following rules:
26
27 1. If there are other loaded modules that depend on (i.e., refer to
28 symbols defined in) this module, then the call fails.
29
30 2. Otherwise, if the reference count for the module (i.e., the number
31 of processes currently using the module) is zero, then the module
32 is immediately unloaded.
33
34 3. If a module has a nonzero reference count, then the behavior de‐
35 pends on the bits set in flags. In normal usage (see NOTES), the
36 O_NONBLOCK flag is always specified, and the O_TRUNC flag may addi‐
37 tionally be specified.
38
39 The various combinations for flags have the following effect:
40
41 flags == O_NONBLOCK
42 The call returns immediately, with an error.
43
44 flags == (O_NONBLOCK | O_TRUNC)
45 The module is unloaded immediately, regardless of whether it
46 has a nonzero reference count.
47
48 (flags & O_NONBLOCK) == 0
49 If flags does not specify O_NONBLOCK, the following steps
50 occur:
51
52 * The module is marked so that no new references are per‐
53 mitted.
54
55 * If the module's reference count is nonzero, the caller is
56 placed in an uninterruptible sleep state (TASK_UNINTER‐
57 RUPTIBLE) until the reference count is zero, at which
58 point the call unblocks.
59
60 * The module is unloaded in the usual way.
61
62 The O_TRUNC flag has one further effect on the rules described above.
63 By default, if a module has an init function but no exit function, then
64 an attempt to remove the module fails. However, if O_TRUNC was speci‐
65 fied, this requirement is bypassed.
66
67 Using the O_TRUNC flag is dangerous! If the kernel was not built with
68 CONFIG_MODULE_FORCE_UNLOAD, this flag is silently ignored. (Normally,
69 CONFIG_MODULE_FORCE_UNLOAD is enabled.) Using this flag taints the
70 kernel (TAINT_FORCED_RMMOD).
71
73 On success, zero is returned. On error, -1 is returned and errno is
74 set to indicate the error.
75
77 EBUSY The module is not "live" (i.e., it is still being initialized or
78 is already marked for removal); or, the module has an init func‐
79 tion but has no exit function, and O_TRUNC was not specified in
80 flags.
81
82 EFAULT name refers to a location outside the process's accessible ad‐
83 dress space.
84
85 ENOENT No module by that name exists.
86
87 EPERM The caller was not privileged (did not have the CAP_SYS_MODULE
88 capability), or module unloading is disabled (see /proc/sys/ker‐
89 nel/modules_disabled in proc(5)).
90
91 EWOULDBLOCK
92 Other modules depend on this module; or, O_NONBLOCK was speci‐
93 fied in flags, but the reference count of this module is nonzero
94 and O_TRUNC was not specified in flags.
95
97 delete_module() is Linux-specific.
98
100 The delete_module() system call is not supported by glibc. No declara‐
101 tion is provided in glibc headers, but, through a quirk of history,
102 glibc versions before 2.23 did export an ABI for this system call.
103 Therefore, in order to employ this system call, it is (before glibc
104 2.23) sufficient to manually declare the interface in your code; alter‐
105 natively, you can invoke the system call using syscall(2).
106
107 The uninterruptible sleep that may occur if O_NONBLOCK is omitted from
108 flags is considered undesirable, because the sleeping process is left
109 in an unkillable state. As at Linux 3.7, specifying O_NONBLOCK is op‐
110 tional, but in future kernels it is likely to become mandatory.
111
112 Linux 2.4 and earlier
113 In Linux 2.4 and earlier, the system call took only one argument:
114
115 int delete_module(const char *name);
116
117 If name is NULL, all unused modules marked auto-clean are removed.
118
119 Some further details of differences in the behavior of delete_module()
120 in Linux 2.4 and earlier are not currently explained in this manual
121 page.
122
124 create_module(2), init_module(2), query_module(2), lsmod(8), mod‐
125 probe(8), rmmod(8)
126
128 This page is part of release 5.13 of the Linux man-pages project. A
129 description of the project, information about reporting bugs, and the
130 latest version of this page, can be found at
131 https://www.kernel.org/doc/man-pages/.
132
133
134
135Linux 2021-03-22 DELETE_MODULE(2)