delete_module(2)

๐Ÿ’– delete_module(2) โ€” the "okay bestie let's unload a kernel module" page ๐Ÿ’–

System Calls Manual โœจ literally the manual for system calls, and this one is delete_module โ€” she unloads kernel modules. Yes, really.


๐ŸŒธ NAME ๐ŸŒธ

delete_module โ€” unload a kernel module. That's the whole gig. Bye bye module ๐Ÿ’…


๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C library (libc, -lc). bestie pls note this does NOT mean you get a cute little wrapper function, keep reading ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <fcntl.h>            /* Definition of O_* constants */
#include <sys/syscall.h>      /* Definition of SYS_* constants */
#include <unistd.h>

int syscall(SYS_delete_module, const char *name, unsigned int flags);
Note: glibc provides no wrapper for delete_module(), which means you have to go through syscall(2). There is no shortcut. There is no cute little delete_module(name, flags) call waiting for you. It's giving "you must do the ritual yourself" ๐Ÿ˜ญ

๐Ÿ’… DESCRIPTION ๐Ÿ’…

Okay so here's the tea. The delete_module() system call attempts to remove the unused loadable module entry identified by name. If the module has an exit function, then that function is executed before unloading the module. The flags argument is used to modify the behavior of the system call, as described below. This system call requires privilege โ€” so no, you can't just do this from your little unprivileged user shell, bestie ๐Ÿ‘‘

Module removal is attempted according to the following rules:

  1. If there are other loaded modules that depend on (i.e., refer to symbols defined in) this module, then the call fails. ๐Ÿ’€
  2. Otherwise, if the reference count for the module (i.e., the number of processes currently using the module) is zero, then the module is immediately unloaded. slay โœจ
  3. If a module has a nonzero reference count, then the behavior depends on the bits set in flags. In normal usage (see NOTES), the O_NONBLOCK flag is always specified, and the O_TRUNC flag may additionally be specified.

The various combinations for flags have the following effect:

  • flags == O_NONBLOCK โ€” The call returns immediately, with an error. not even gonna wait for you, she's out
  • flags == (O_NONBLOCK | O_TRUNC) โ€” The module is unloaded immediately, regardless of whether it has a nonzero reference count. chaotic and powerful ๐Ÿ”ฅ
  • (flags & O_NONBLOCK) == 0 โ€” If flags does not specify O_NONBLOCK, the following steps occur:
    • The module is marked so that no new references are permitted.
    • If the module's reference count is nonzero, the caller is placed in an uninterruptible sleep state (TASK_UNINTERRUPTIBLE) until the reference count is zero, at which point the call unblocks. ๐Ÿ˜ด๐Ÿ”’
    • The module is unloaded in the usual way.

The O_TRUNC flag has one further effect on the rules described above. By default, if a module has an init function but no exit function, then an attempt to remove the module fails. However, if O_TRUNC was specified, this requirement is bypassed. the loophole of it all

โš ๏ธ Using the O_TRUNC flag is dangerous! If the kernel was not built with CONFIG_MODULE_FORCE_UNLOAD, this flag is silently ignored. (Normally, CONFIG_MODULE_FORCE_UNLOAD is enabled.) Using this flag taints the kernel (TAINT_FORCED_RMMOD). ๐Ÿšจ girl no. be careful.

๐ŸŒˆ RETURN VALUE ๐ŸŒˆ

On success, zero is returned. On error, -1 is returned and errno is set to indicate the error. Standard behavior, iconic behavior, no notes ๐Ÿ’…


๐Ÿฆ‹ ERRORS ๐Ÿฆ‹

  • EBUSY โ€” The module is not "live" (i.e., it is still being initialized or is already marked for removal); or, the module has an init function but has no exit function, and O_TRUNC was not specified in flags.
  • EFAULT โ€” name refers to a location outside the process's accessible address space.
  • ENOENT โ€” No module by that name exists. she's not here bestie, wrong name
  • EPERM โ€” The caller was not privileged (did not have the CAP_SYS_MODULE capability), or module unloading is disabled (see /proc/sys/kernel/modules_disabled in proc(5)).
  • EWOULDBLOCK โ€” Other modules depend on this module; or, O_NONBLOCK was specified in flags, but the reference count of this module is nonzero and O_TRUNC was not specified in flags. caught in 4k blocking

โญ STANDARDS โญ

Linux. Just Linux. That's it. ๐Ÿง


๐Ÿ’œ HISTORY ๐Ÿ’œ

The delete_module() system call is not supported by glibc. No declaration is provided in glibc headers, but, through a quirk of history, glibc versions before glibc 2.23 did export an ABI for this system call. Therefore, in order to employ this system call, it is (before glibc 2.23) sufficient to manually declare the interface in your code; alternatively, you can invoke the system call using syscall(2). the lore is real, iykyk ๐Ÿ“œ

๐Ÿ•ฐ๏ธ Linux 2.4 and earlier ๐Ÿ•ฐ๏ธ

In Linux 2.4 and earlier, the system call took only one argument:

int delete_module(const char *name);

If name is NULL, all unused modules marked auto-clean are removed. ๐ŸŒช๏ธ

Some further details of differences in the behavior of delete_module() in Linux 2.4 and earlier are not currently explained in this manual page. (yes, really, that's what it says โ€” the mystery stays a mystery ๐Ÿ™ˆ)


๐Ÿงš NOTES ๐Ÿงš

The uninterruptible sleep that may occur if O_NONBLOCK is omitted from flags is considered undesirable, because the sleeping process is left in an unkillable state. As at Linux 3.7, specifying O_NONBLOCK is optional, but in future kernels it is likely to become mandatory. so like, just pass it, bestie. future-proof your vibes โœจ


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘


and that's the whole page, you ate it, go unload something responsibly (or don't, it needs privilege anyway ๐Ÿ’…) ๐ŸŽ€

this lil masterpiece is from Linux man-pages 6.20, dated 2026-02-08, and yes it's literally called delete_module(2) โ€” she contains multitudes, mwah ๐Ÿซถ๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms