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