From: Yafang Shao <laoar.shao@gmail.com>
To: jpoimboe@kernel.org, jikos@kernel.org, mbenes@suse.cz,
pmladek@suse.com, joe.lawrence@redhat.com, mcgrof@kernel.org
Cc: live-patching@vger.kernel.org, linux-modules@vger.kernel.org,
Yafang Shao <laoar.shao@gmail.com>
Subject: [PATCH v2 1/2] module: Add a new helper delete_module()
Date: Sun, 7 Apr 2024 11:57:29 +0800 [thread overview]
Message-ID: <20240407035730.20282-2-laoar.shao@gmail.com> (raw)
In-Reply-To: <20240407035730.20282-1-laoar.shao@gmail.com>
Introduce a new helper function, delete_module(), designed to delete kernel
modules from locations outside of the `kernel/module` directory.
No functional change.
Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
include/linux/module.h | 1 +
kernel/module/main.c | 82 ++++++++++++++++++++++++++++++++----------
2 files changed, 65 insertions(+), 18 deletions(-)
diff --git a/include/linux/module.h b/include/linux/module.h
index 1153b0d99a80..c24557f1b795 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -75,6 +75,7 @@ extern struct module_attribute module_uevent;
/* These are either module local, or the kernel's dummy ones. */
extern int init_module(void);
extern void cleanup_module(void);
+extern int delete_module(struct module *mod);
#ifndef MODULE
/**
diff --git a/kernel/module/main.c b/kernel/module/main.c
index e1e8a7a9d6c1..3b48ee66db41 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -695,12 +695,74 @@ EXPORT_SYMBOL(module_refcount);
/* This exists whether we can unload or not */
static void free_module(struct module *mod);
+static void __delete_module(struct module *mod)
+{
+ char buf[MODULE_FLAGS_BUF_SIZE];
+
+ WARN_ON_ONCE(mod->state != MODULE_STATE_GOING);
+
+ /* Final destruction now no one is using it. */
+ if (mod->exit != NULL)
+ mod->exit();
+ blocking_notifier_call_chain(&module_notify_list,
+ MODULE_STATE_GOING, mod);
+ klp_module_going(mod);
+ ftrace_release_mod(mod);
+
+ async_synchronize_full();
+
+ /* Store the name and taints of the last unloaded module for diagnostic purposes */
+ strscpy(last_unloaded_module.name, mod->name, sizeof(last_unloaded_module.name));
+ strscpy(last_unloaded_module.taints, module_flags(mod, buf, false),
+ sizeof(last_unloaded_module.taints));
+
+ free_module(mod);
+ /* someone could wait for the module in add_unformed_module() */
+ wake_up_all(&module_wq);
+}
+
+int delete_module(struct module *mod)
+{
+ int ret;
+
+ mutex_lock(&module_mutex);
+ if (!list_empty(&mod->source_list)) {
+ /* Other modules depend on us: get rid of them first. */
+ ret = -EWOULDBLOCK;
+ goto out;
+ }
+
+ /* Doing init or already dying? */
+ if (mod->state != MODULE_STATE_LIVE) {
+ ret = -EBUSY;
+ goto out;
+ }
+
+ /* If it has an init func, it must have an exit func to unload */
+ if (mod->init && !mod->exit) {
+ ret = -EBUSY;
+ goto out;
+ }
+
+ if (try_release_module_ref(mod) != 0) {
+ ret = -EWOULDBLOCK;
+ goto out;
+ }
+ mod->state = MODULE_STATE_GOING;
+ mutex_unlock(&module_mutex);
+ __delete_module(mod);
+ return 0;
+
+out:
+ mutex_unlock(&module_mutex);
+ return ret;
+}
+
SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
unsigned int, flags)
{
struct module *mod;
char name[MODULE_NAME_LEN];
- char buf[MODULE_FLAGS_BUF_SIZE];
int ret, forced = 0;
if (!capable(CAP_SYS_MODULE) || modules_disabled)
@@ -750,23 +812,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user,
goto out;
mutex_unlock(&module_mutex);
- /* Final destruction now no one is using it. */
- if (mod->exit != NULL)
- mod->exit();
- blocking_notifier_call_chain(&module_notify_list,
- MODULE_STATE_GOING, mod);
- klp_module_going(mod);
- ftrace_release_mod(mod);
-
- async_synchronize_full();
-
- /* Store the name and taints of the last unloaded module for diagnostic purposes */
- strscpy(last_unloaded_module.name, mod->name, sizeof(last_unloaded_module.name));
- strscpy(last_unloaded_module.taints, module_flags(mod, buf, false), sizeof(last_unloaded_module.taints));
-
- free_module(mod);
- /* someone could wait for the module in add_unformed_module() */
- wake_up_all(&module_wq);
+ __delete_module(mod);
return 0;
out:
mutex_unlock(&module_mutex);
--
2.39.1
next prev parent reply other threads:[~2024-04-07 3:58 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-07 3:57 [PATCH v2 0/2] livepatch, module: Delete the associated module of disabled livepatch Yafang Shao
2024-04-07 3:57 ` Yafang Shao [this message]
2024-04-24 12:09 ` [PATCH v2 1/2] module: Add a new helper delete_module() Yafang Shao
2024-05-04 16:53 ` Greg KH
2024-05-04 22:26 ` Josh Poimboeuf
2024-05-04 21:36 ` Luis Chamberlain
2024-05-06 11:58 ` Petr Mladek
2024-04-07 3:57 ` [PATCH v2 2/2] livepatch: Delete the associated module of disabled livepatch Yafang Shao
2024-05-03 21:14 ` Josh Poimboeuf
2024-05-06 11:32 ` Petr Mladek
2024-05-07 2:35 ` Josh Poimboeuf
2024-05-07 14:03 ` Yafang Shao
2024-05-08 5:16 ` Josh Poimboeuf
2024-05-08 6:01 ` Yafang Shao
2024-05-08 7:03 ` Josh Poimboeuf
2024-05-09 2:17 ` Yafang Shao
2024-05-09 5:20 ` Josh Poimboeuf
2024-05-09 5:53 ` Yafang Shao
2024-05-09 10:16 ` Petr Mladek
2024-05-09 11:57 ` Yafang Shao
2024-05-09 10:51 ` Petr Mladek
2024-05-06 12:20 ` Petr Mladek
2024-05-02 13:30 ` [PATCH v2 0/2] livepatch, module: " Joe Lawrence
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240407035730.20282-2-laoar.shao@gmail.com \
--to=laoar.shao@gmail.com \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=linux-modules@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=mcgrof@kernel.org \
--cc=pmladek@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).