#define pr_fmt(fmt) "module: " fmt
#include <linux/bug.h>
#include <linux/cleanup.h>
#include <linux/completion.h>
#include <linux/container_of.h>
#include <linux/list.h>
#include <linux/lockdep.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/param.h>
#include <linux/printk.h>
#include <linux/refcount.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/workqueue.h>
#include "internal.h"
#undef MODULE_PARAM_PREFIX
#define MODULE_PARAM_PREFIX "module."
static bool enable_dups_trace = IS_ENABLED(CONFIG_MODULE_DEBUG_AUTOLOAD_DUPS_TRACE);
module_param(enable_dups_trace, bool_enable_only, 0644);
static DEFINE_MUTEX(kmod_dup_mutex);
static LIST_HEAD(dup_kmod_reqs);
struct kmod_dup_req {
refcount_t refcount;
struct list_head list;
char name[MODULE_NAME_LEN];
struct completion first_req_done;
struct delayed_work delete_work;
int dup_ret;
};
static void get_kmod_req(struct kmod_dup_req *kmod_req)
{
refcount_inc(&kmod_req->refcount);
}
static void put_kmod_req(struct kmod_dup_req *kmod_req)
{
if (refcount_dec_and_test(&kmod_req->refcount))
kfree(kmod_req);
}
DEFINE_FREE(put_kmod_req, struct kmod_dup_req *, if (_T) put_kmod_req(_T))
static struct kmod_dup_req *kmod_dup_request_lookup(char *module_name)
{
struct kmod_dup_req *kmod_req;
lockdep_assert_held(&kmod_dup_mutex);
list_for_each_entry(kmod_req, &dup_kmod_reqs, list) {
if (!strcmp(kmod_req->name, module_name))
return kmod_req;
}
return NULL;
}
static void kmod_dup_request_delete(struct work_struct *work)
{
struct kmod_dup_req *kmod_req;
kmod_req = container_of(to_delayed_work(work), struct kmod_dup_req, delete_work);
scoped_guard(mutex, &kmod_dup_mutex)
list_del(&kmod_req->list);
put_kmod_req(kmod_req);
}
static struct kmod_dup_req *alloc_kmod_req(const char *module_name)
{
struct kmod_dup_req *kmod_req = kzalloc_obj(*kmod_req);
if (!kmod_req)
return NULL;
refcount_set(&kmod_req->refcount, 1);
strscpy(kmod_req->name, module_name);
INIT_DELAYED_WORK(&kmod_req->delete_work, kmod_dup_request_delete);
init_completion(&kmod_req->first_req_done);
return kmod_req;
}
bool kmod_dup_request_exists_wait(char *module_name, bool wait, int *dup_ret)
{
struct kmod_dup_req *kmod_req __free(put_kmod_req) = NULL;
int ret;
scoped_guard(mutex, &kmod_dup_mutex) {
struct kmod_dup_req *new_kmod_req;
kmod_req = kmod_dup_request_lookup(module_name);
if (kmod_req) {
get_kmod_req(kmod_req);
break;
}
if (!wait) {
pr_debug("New request_module_nowait() for %s -- cannot track duplicates for this request\n", module_name);
return false;
}
pr_debug("New request_module() for %s\n", module_name);
new_kmod_req = alloc_kmod_req(module_name);
if (!new_kmod_req)
return false;
list_add(&new_kmod_req->list, &dup_kmod_reqs);
return false;
}
if (enable_dups_trace)
WARN(1, "module-autoload: duplicate request for module %s\n", module_name);
else
pr_warn("module-autoload: duplicate request for module %s\n", module_name);
if (!wait) {
*dup_ret = 0;
return true;
}
ret = wait_for_completion_state(&kmod_req->first_req_done,
TASK_KILLABLE);
if (ret) {
*dup_ret = ret;
return true;
}
*dup_ret = kmod_req->dup_ret;
return true;
}
void kmod_dup_request_announce(char *module_name, int ret)
{
struct kmod_dup_req *kmod_req;
scoped_guard(mutex, &kmod_dup_mutex) {
kmod_req = kmod_dup_request_lookup(module_name);
if (!kmod_req || completion_done(&kmod_req->first_req_done))
return;
kmod_req->dup_ret = ret;
complete_all(&kmod_req->first_req_done);
}
queue_delayed_work(system_dfl_wq, &kmod_req->delete_work, 60 * HZ);
}