forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-module.c
201 lines (168 loc) · 4.22 KB
/
core-module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022-2023 Colin Ian King.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#include "core-module.h"
#if defined(HAVE_LIBKMOD_H) && \
defined(HAVE_LIB_KMOD) && \
defined(__linux__)
#include <libkmod.h>
/*
* stress_module_load()
* load a linux kernel module
*/
int stress_module_load(
const char *name, /* name of stressor */
const char *alias, /* name of module */
const char *options, /* module options */
bool *already_loaded) /* set to true if already loaded */
{
int ret;
unsigned int flags = 0;
struct kmod_ctx *ctx;
struct kmod_list *l, *list = NULL;
void (*show_func)(struct kmod_module *m, bool install,
const char *options) = NULL;
*already_loaded = false;
ctx = kmod_new(NULL, NULL);
if (!ctx)
return -1;
ret = kmod_module_new_from_lookup(ctx, alias, &list);
if (!list || ret < 0) {
kmod_unref(ctx);
return -1;
}
kmod_list_foreach(l, list) {
struct kmod_module *mod = kmod_module_get_module(l);
if (!mod)
continue;
ret = kmod_module_probe_insert_module(mod, flags, options,
NULL, NULL, show_func);
if (ret < 0) {
if (errno == -EEXIST) {
*already_loaded = true;
kmod_unref(ctx);
return 0;
}
pr_inf("%s: failed to load module %s, errno=%d (%s)\n",
name, alias,
errno, strerror(errno));
ret = -1;
break;
}
kmod_module_unref(mod);
}
kmod_module_unref_list(list);
kmod_unref(ctx);
return ret;
}
/*
* stress_module_unload_mod_and_deps()
* recursively unload module and dependencies
*/
static int stress_module_unload_mod_and_deps(struct kmod_module *mod)
{
int ret;
struct kmod_list *deps, *l;
ret = kmod_module_remove_module(mod, 0);
deps = kmod_module_get_dependencies(mod);
if (!deps)
return ret;
kmod_list_foreach(l, deps) {
struct kmod_module *dep_mod = kmod_module_get_module(l);
if (kmod_module_get_refcnt(dep_mod) == 0)
stress_module_unload_mod_and_deps(dep_mod);
kmod_module_unref_list(deps);
}
return ret;
}
/*
* stress_module_unload()
* unload a linux kernel module
*/
int stress_module_unload(
const char *name, /* name of stressor */
const char *alias, /* name of module */
const bool already_loaded) /* don't unload if true */
{
int ret;
struct kmod_list *l, *list = NULL;
struct kmod_ctx *ctx;
if (already_loaded)
return 0;
ctx = kmod_new(NULL, NULL);
if (!ctx)
return -1;
ret = kmod_module_new_from_lookup(ctx, alias, &list);
if (ret < 0) {
kmod_unref(ctx);
return -1;
}
if (!list) {
pr_dbg("%s: module %s not found\n",
name, name);
kmod_unref(ctx);
return -1;
}
kmod_list_foreach(l, list) {
struct kmod_module *mod = kmod_module_get_module(l);
const char *module_name = kmod_module_get_name(mod);
if (!mod)
continue;
ret = kmod_module_get_initstate(mod);
if (ret < 0)
continue;
if (ret == KMOD_MODULE_BUILTIN)
continue;
ret = kmod_module_get_refcnt(mod);
if (ret > 0) {
pr_dbg("%s: cannot unload %s, it is in use\n",
name, module_name);
}
VOID_RET(int, stress_module_unload_mod_and_deps(mod));
}
kmod_module_unref_list(list);
kmod_unref(ctx);
return 0;
}
#else
int stress_module_load(
const char *name,
const char *alias,
const char *options,
bool *already_loaded)
{
(void)alias;
(void)options;
(void)already_loaded;
pr_dbg("%s: module loading not supported\n", name);
return -1;
}
int stress_module_unload(
const char *name,
const char *alias,
const bool already_loaded)
{
(void)alias;
(void)already_loaded;
pr_dbg("%s: module unloading not supported\n", name);
return -1;
}
UNEXPECTED
#endif