blob: 37d0d3645fa6cccde515b1ad7bf5db05466d6cfd [file] [log] [blame]
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001/*
2 * core.c - Kernel Live Patching Core
3 *
4 * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
5 * Copyright (C) 2014 SUSE
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/mutex.h>
26#include <linux/slab.h>
Seth Jenningsb700e7f2014-12-16 11:58:19 -060027#include <linux/list.h>
28#include <linux/kallsyms.h>
29#include <linux/livepatch.h>
Jessica Yu425595a2016-03-22 20:03:18 -040030#include <linux/elf.h>
31#include <linux/moduleloader.h>
Josh Poimboeuf3ec24772017-03-06 11:20:29 -060032#include <linux/completion.h>
Josh Poimboeufb56b36e2015-12-03 16:33:26 -060033#include <asm/cacheflush.h>
Jiri Kosina10517422017-03-08 14:27:05 +010034#include "core.h"
Josh Poimboeufc349cdc2017-02-13 19:42:37 -060035#include "patch.h"
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060036#include "transition.h"
Seth Jenningsb700e7f2014-12-16 11:58:19 -060037
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060038/*
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060039 * klp_mutex is a coarse lock which serializes access to klp data. All
40 * accesses to klp-related variables and structures must have mutex protection,
41 * except within the following functions which carefully avoid the need for it:
42 *
43 * - klp_ftrace_handler()
44 * - klp_update_patch_state()
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060045 */
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -060046DEFINE_MUTEX(klp_mutex);
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -060047
Petr Mladek958ef1e2019-01-09 13:43:23 +010048/*
49 * Actively used patches: enabled or in transition. Note that replaced
50 * or disabled patches are not listed even though the related kernel
51 * module still can be loaded.
52 */
Petr Mladek68007282019-01-09 13:43:22 +010053LIST_HEAD(klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -060054
55static struct kobject *klp_root_kobj;
56
57static bool klp_is_module(struct klp_object *obj)
58{
59 return obj->name;
60}
61
Seth Jenningsb700e7f2014-12-16 11:58:19 -060062/* sets obj->mod if object is not vmlinux and module is found */
63static void klp_find_object_module(struct klp_object *obj)
64{
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010065 struct module *mod;
66
Seth Jenningsb700e7f2014-12-16 11:58:19 -060067 if (!klp_is_module(obj))
68 return;
69
70 mutex_lock(&module_mutex);
71 /*
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010072 * We do not want to block removal of patched modules and therefore
73 * we do not take a reference here. The patches are removed by
Jessica Yu7e545d62016-03-16 20:55:39 -040074 * klp_module_going() instead.
Seth Jenningsb700e7f2014-12-16 11:58:19 -060075 */
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010076 mod = find_module(obj->name);
77 /*
Jessica Yu7e545d62016-03-16 20:55:39 -040078 * Do not mess work of klp_module_coming() and klp_module_going().
79 * Note that the patch might still be needed before klp_module_going()
Petr Mladek8cb2c2d2015-03-12 12:55:13 +010080 * is called. Module functions can be called even in the GOING state
81 * until mod->exit() finishes. This is especially important for
82 * patches that modify semantic of the functions.
83 */
84 if (mod && mod->klp_alive)
85 obj->mod = mod;
86
Seth Jenningsb700e7f2014-12-16 11:58:19 -060087 mutex_unlock(&module_mutex);
88}
89
Seth Jenningsb700e7f2014-12-16 11:58:19 -060090static bool klp_initialized(void)
91{
Nicholas Mc Guiree76ff062015-05-11 07:52:29 +020092 return !!klp_root_kobj;
Seth Jenningsb700e7f2014-12-16 11:58:19 -060093}
94
95struct klp_find_arg {
96 const char *objname;
97 const char *name;
98 unsigned long addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -060099 unsigned long count;
Chris J Argesb2b018e2015-12-01 20:40:54 -0600100 unsigned long pos;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600101};
102
103static int klp_find_callback(void *data, const char *name,
104 struct module *mod, unsigned long addr)
105{
106 struct klp_find_arg *args = data;
107
108 if ((mod && !args->objname) || (!mod && args->objname))
109 return 0;
110
111 if (strcmp(args->name, name))
112 return 0;
113
114 if (args->objname && strcmp(args->objname, mod->name))
115 return 0;
116
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600117 args->addr = addr;
118 args->count++;
119
Chris J Argesb2b018e2015-12-01 20:40:54 -0600120 /*
121 * Finish the search when the symbol is found for the desired position
122 * or the position is not defined for a non-unique symbol.
123 */
124 if ((args->pos && (args->count == args->pos)) ||
125 (!args->pos && (args->count > 1)))
126 return 1;
127
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600128 return 0;
129}
130
131static int klp_find_object_symbol(const char *objname, const char *name,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600132 unsigned long sympos, unsigned long *addr)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600133{
134 struct klp_find_arg args = {
135 .objname = objname,
136 .name = name,
137 .addr = 0,
Chris J Argesb2b018e2015-12-01 20:40:54 -0600138 .count = 0,
139 .pos = sympos,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600140 };
141
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200142 mutex_lock(&module_mutex);
Zhou Chengming72f04b52017-03-28 21:10:35 +0800143 if (objname)
144 module_kallsyms_on_each_symbol(klp_find_callback, &args);
145 else
146 kallsyms_on_each_symbol(klp_find_callback, &args);
Miroslav Benes9a1bd632015-06-01 17:48:37 +0200147 mutex_unlock(&module_mutex);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600148
Chris J Argesb2b018e2015-12-01 20:40:54 -0600149 /*
150 * Ensure an address was found. If sympos is 0, ensure symbol is unique;
151 * otherwise ensure the symbol position count matches sympos.
152 */
153 if (args.addr == 0)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600154 pr_err("symbol '%s' not found in symbol table\n", name);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600155 else if (args.count > 1 && sympos == 0) {
Petr Mladekf995b5f2016-03-09 15:20:59 +0100156 pr_err("unresolvable ambiguity for symbol '%s' in object '%s'\n",
157 name, objname);
Chris J Argesb2b018e2015-12-01 20:40:54 -0600158 } else if (sympos != args.count && sympos > 0) {
159 pr_err("symbol position %lu for symbol '%s' in object '%s' not found\n",
160 sympos, name, objname ? objname : "vmlinux");
161 } else {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600162 *addr = args.addr;
163 return 0;
164 }
165
166 *addr = 0;
167 return -EINVAL;
168}
169
Jessica Yu425595a2016-03-22 20:03:18 -0400170static int klp_resolve_symbols(Elf_Shdr *relasec, struct module *pmod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600171{
Jessica Yu425595a2016-03-22 20:03:18 -0400172 int i, cnt, vmlinux, ret;
173 char objname[MODULE_NAME_LEN];
174 char symname[KSYM_NAME_LEN];
175 char *strtab = pmod->core_kallsyms.strtab;
176 Elf_Rela *relas;
177 Elf_Sym *sym;
178 unsigned long sympos, addr;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600179
Chris J Argesb2b018e2015-12-01 20:40:54 -0600180 /*
Jessica Yu425595a2016-03-22 20:03:18 -0400181 * Since the field widths for objname and symname in the sscanf()
182 * call are hard-coded and correspond to MODULE_NAME_LEN and
183 * KSYM_NAME_LEN respectively, we must make sure that MODULE_NAME_LEN
184 * and KSYM_NAME_LEN have the values we expect them to have.
185 *
186 * Because the value of MODULE_NAME_LEN can differ among architectures,
187 * we use the smallest/strictest upper bound possible (56, based on
188 * the current definition of MODULE_NAME_LEN) to prevent overflows.
Chris J Argesb2b018e2015-12-01 20:40:54 -0600189 */
Jessica Yu425595a2016-03-22 20:03:18 -0400190 BUILD_BUG_ON(MODULE_NAME_LEN < 56 || KSYM_NAME_LEN != 128);
191
192 relas = (Elf_Rela *) relasec->sh_addr;
193 /* For each rela in this klp relocation section */
194 for (i = 0; i < relasec->sh_size / sizeof(Elf_Rela); i++) {
195 sym = pmod->core_kallsyms.symtab + ELF_R_SYM(relas[i].r_info);
196 if (sym->st_shndx != SHN_LIVEPATCH) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500197 pr_err("symbol %s is not marked as a livepatch symbol\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400198 strtab + sym->st_name);
199 return -EINVAL;
200 }
201
202 /* Format: .klp.sym.objname.symname,sympos */
203 cnt = sscanf(strtab + sym->st_name,
204 ".klp.sym.%55[^.].%127[^,],%lu",
205 objname, symname, &sympos);
206 if (cnt != 3) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500207 pr_err("symbol %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400208 strtab + sym->st_name);
209 return -EINVAL;
210 }
211
212 /* klp_find_object_symbol() treats a NULL objname as vmlinux */
213 vmlinux = !strcmp(objname, "vmlinux");
214 ret = klp_find_object_symbol(vmlinux ? NULL : objname,
215 symname, sympos, &addr);
216 if (ret)
217 return ret;
218
219 sym->st_value = addr;
220 }
221
222 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600223}
224
225static int klp_write_object_relocations(struct module *pmod,
226 struct klp_object *obj)
227{
Jessica Yu425595a2016-03-22 20:03:18 -0400228 int i, cnt, ret = 0;
229 const char *objname, *secname;
230 char sec_objname[MODULE_NAME_LEN];
231 Elf_Shdr *sec;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600232
233 if (WARN_ON(!klp_is_object_loaded(obj)))
234 return -EINVAL;
235
Jessica Yu425595a2016-03-22 20:03:18 -0400236 objname = klp_is_module(obj) ? obj->name : "vmlinux";
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600237
Jessica Yu425595a2016-03-22 20:03:18 -0400238 /* For each klp relocation section */
239 for (i = 1; i < pmod->klp_info->hdr.e_shnum; i++) {
240 sec = pmod->klp_info->sechdrs + i;
241 secname = pmod->klp_info->secstrings + sec->sh_name;
242 if (!(sec->sh_flags & SHF_RELA_LIVEPATCH))
243 continue;
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600244
Jessica Yu425595a2016-03-22 20:03:18 -0400245 /*
246 * Format: .klp.rela.sec_objname.section_name
247 * See comment in klp_resolve_symbols() for an explanation
248 * of the selected field width value.
249 */
250 cnt = sscanf(secname, ".klp.rela.%55[^.]", sec_objname);
251 if (cnt != 1) {
Josh Poimboeuf77f8f392017-04-13 17:59:15 -0500252 pr_err("section %s has an incorrectly formatted name\n",
Jessica Yu425595a2016-03-22 20:03:18 -0400253 secname);
254 ret = -EINVAL;
255 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600256 }
Jessica Yu425595a2016-03-22 20:03:18 -0400257
258 if (strcmp(objname, sec_objname))
259 continue;
260
261 ret = klp_resolve_symbols(sec, pmod);
262 if (ret)
263 break;
264
265 ret = apply_relocate_add(pmod->klp_info->sechdrs,
266 pmod->core_kallsyms.strtab,
267 pmod->klp_info->symndx, i, pmod);
268 if (ret)
269 break;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600270 }
271
Josh Poimboeufb56b36e2015-12-03 16:33:26 -0600272 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600273}
274
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600275/*
276 * Sysfs Interface
277 *
278 * /sys/kernel/livepatch
279 * /sys/kernel/livepatch/<patch>
280 * /sys/kernel/livepatch/<patch>/enabled
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600281 * /sys/kernel/livepatch/<patch>/transition
Miroslav Benes43347d52017-11-15 14:50:13 +0100282 * /sys/kernel/livepatch/<patch>/signal
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100283 * /sys/kernel/livepatch/<patch>/force
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600284 * /sys/kernel/livepatch/<patch>/<object>
Chris J Arges444f9e92015-12-01 20:40:56 -0600285 * /sys/kernel/livepatch/<patch>/<object>/<function,sympos>
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600286 */
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100287static int __klp_disable_patch(struct klp_patch *patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600288
289static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr,
290 const char *buf, size_t count)
291{
292 struct klp_patch *patch;
293 int ret;
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600294 bool enabled;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600295
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600296 ret = kstrtobool(buf, &enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600297 if (ret)
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600298 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600299
300 patch = container_of(kobj, struct klp_patch, kobj);
301
302 mutex_lock(&klp_mutex);
303
Josh Poimboeuf68ae4b22017-02-13 19:42:38 -0600304 if (patch->enabled == enabled) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600305 /* already in requested state */
306 ret = -EINVAL;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100307 goto out;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600308 }
309
Petr Mladek958ef1e2019-01-09 13:43:23 +0100310 /*
311 * Allow to reverse a pending transition in both ways. It might be
312 * necessary to complete the transition without forcing and breaking
313 * the system integrity.
314 *
315 * Do not allow to re-enable a disabled patch.
316 */
317 if (patch == klp_transition_patch)
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600318 klp_reverse_transition();
Petr Mladek958ef1e2019-01-09 13:43:23 +0100319 else if (!enabled)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600320 ret = __klp_disable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100321 else
322 ret = -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600323
Petr Mladek958ef1e2019-01-09 13:43:23 +0100324out:
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600325 mutex_unlock(&klp_mutex);
326
Petr Mladek958ef1e2019-01-09 13:43:23 +0100327 if (ret)
328 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600329 return count;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600330}
331
332static ssize_t enabled_show(struct kobject *kobj,
333 struct kobj_attribute *attr, char *buf)
334{
335 struct klp_patch *patch;
336
337 patch = container_of(kobj, struct klp_patch, kobj);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600338 return snprintf(buf, PAGE_SIZE-1, "%d\n", patch->enabled);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600339}
340
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600341static ssize_t transition_show(struct kobject *kobj,
342 struct kobj_attribute *attr, char *buf)
343{
344 struct klp_patch *patch;
345
346 patch = container_of(kobj, struct klp_patch, kobj);
347 return snprintf(buf, PAGE_SIZE-1, "%d\n",
348 patch == klp_transition_patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600349}
350
Miroslav Benes43347d52017-11-15 14:50:13 +0100351static ssize_t signal_store(struct kobject *kobj, struct kobj_attribute *attr,
352 const char *buf, size_t count)
353{
354 struct klp_patch *patch;
355 int ret;
356 bool val;
357
Miroslav Benes43347d52017-11-15 14:50:13 +0100358 ret = kstrtobool(buf, &val);
359 if (ret)
360 return ret;
361
Miroslav Benes88690162017-12-21 14:40:43 +0100362 if (!val)
363 return count;
364
365 mutex_lock(&klp_mutex);
366
367 patch = container_of(kobj, struct klp_patch, kobj);
368 if (patch != klp_transition_patch) {
369 mutex_unlock(&klp_mutex);
370 return -EINVAL;
371 }
372
373 klp_send_signals();
374
375 mutex_unlock(&klp_mutex);
Miroslav Benes43347d52017-11-15 14:50:13 +0100376
377 return count;
378}
379
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100380static ssize_t force_store(struct kobject *kobj, struct kobj_attribute *attr,
381 const char *buf, size_t count)
382{
383 struct klp_patch *patch;
384 int ret;
385 bool val;
386
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100387 ret = kstrtobool(buf, &val);
388 if (ret)
389 return ret;
390
Miroslav Benes88690162017-12-21 14:40:43 +0100391 if (!val)
392 return count;
393
394 mutex_lock(&klp_mutex);
395
396 patch = container_of(kobj, struct klp_patch, kobj);
397 if (patch != klp_transition_patch) {
398 mutex_unlock(&klp_mutex);
399 return -EINVAL;
400 }
401
402 klp_force_transition();
403
404 mutex_unlock(&klp_mutex);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100405
406 return count;
407}
408
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600409static struct kobj_attribute enabled_kobj_attr = __ATTR_RW(enabled);
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600410static struct kobj_attribute transition_kobj_attr = __ATTR_RO(transition);
Miroslav Benes43347d52017-11-15 14:50:13 +0100411static struct kobj_attribute signal_kobj_attr = __ATTR_WO(signal);
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100412static struct kobj_attribute force_kobj_attr = __ATTR_WO(force);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600413static struct attribute *klp_patch_attrs[] = {
414 &enabled_kobj_attr.attr,
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600415 &transition_kobj_attr.attr,
Miroslav Benes43347d52017-11-15 14:50:13 +0100416 &signal_kobj_attr.attr,
Miroslav Benesc99a2be2017-11-22 11:29:21 +0100417 &force_kobj_attr.attr,
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600418 NULL
419};
420
421static void klp_kobj_release_patch(struct kobject *kobj)
422{
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600423 struct klp_patch *patch;
424
425 patch = container_of(kobj, struct klp_patch, kobj);
426 complete(&patch->finish);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600427}
428
429static struct kobj_type klp_ktype_patch = {
430 .release = klp_kobj_release_patch,
431 .sysfs_ops = &kobj_sysfs_ops,
432 .default_attrs = klp_patch_attrs,
433};
434
Miroslav Benescad706d2015-05-19 12:01:18 +0200435static void klp_kobj_release_object(struct kobject *kobj)
436{
437}
438
439static struct kobj_type klp_ktype_object = {
440 .release = klp_kobj_release_object,
441 .sysfs_ops = &kobj_sysfs_ops,
442};
443
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600444static void klp_kobj_release_func(struct kobject *kobj)
445{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600446}
447
448static struct kobj_type klp_ktype_func = {
449 .release = klp_kobj_release_func,
450 .sysfs_ops = &kobj_sysfs_ops,
451};
452
Petr Mladek0430f782019-01-09 13:43:21 +0100453static void klp_free_funcs(struct klp_object *obj)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600454{
455 struct klp_func *func;
456
Petr Mladek0430f782019-01-09 13:43:21 +0100457 klp_for_each_func(obj, func) {
458 /* Might be called from klp_init_patch() error path. */
459 if (func->kobj_added)
460 kobject_put(&func->kobj);
461 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600462}
463
464/* Clean up when a patched object is unloaded */
465static void klp_free_object_loaded(struct klp_object *obj)
466{
467 struct klp_func *func;
468
469 obj->mod = NULL;
470
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200471 klp_for_each_func(obj, func)
Petr Mladek19514912019-01-09 13:43:19 +0100472 func->old_func = NULL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600473}
474
Petr Mladek0430f782019-01-09 13:43:21 +0100475static void klp_free_objects(struct klp_patch *patch)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600476{
477 struct klp_object *obj;
478
Petr Mladek0430f782019-01-09 13:43:21 +0100479 klp_for_each_object(patch, obj) {
480 klp_free_funcs(obj);
481
482 /* Might be called from klp_init_patch() error path. */
483 if (obj->kobj_added)
484 kobject_put(&obj->kobj);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600485 }
486}
487
Petr Mladek0430f782019-01-09 13:43:21 +0100488/*
489 * This function implements the free operations that can be called safely
490 * under klp_mutex.
491 *
492 * The operation must be completed by calling klp_free_patch_finish()
493 * outside klp_mutex.
494 */
Petr Mladek958ef1e2019-01-09 13:43:23 +0100495void klp_free_patch_start(struct klp_patch *patch)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600496{
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600497 if (!list_empty(&patch->list))
498 list_del(&patch->list);
Petr Mladek0430f782019-01-09 13:43:21 +0100499
500 klp_free_objects(patch);
501}
502
503/*
504 * This function implements the free part that must be called outside
505 * klp_mutex.
506 *
507 * It must be called after klp_free_patch_start(). And it has to be
508 * the last function accessing the livepatch structures when the patch
509 * gets disabled.
510 */
511static void klp_free_patch_finish(struct klp_patch *patch)
512{
513 /*
514 * Avoid deadlock with enabled_store() sysfs callback by
515 * calling this outside klp_mutex. It is safe because
516 * this is called when the patch gets disabled and it
517 * cannot get enabled again.
518 */
519 if (patch->kobj_added) {
520 kobject_put(&patch->kobj);
521 wait_for_completion(&patch->finish);
522 }
Petr Mladek958ef1e2019-01-09 13:43:23 +0100523
524 /* Put the module after the last access to struct klp_patch. */
525 if (!patch->forced)
526 module_put(patch->mod);
527}
528
529/*
530 * The livepatch might be freed from sysfs interface created by the patch.
531 * This work allows to wait until the interface is destroyed in a separate
532 * context.
533 */
534static void klp_free_patch_work_fn(struct work_struct *work)
535{
536 struct klp_patch *patch =
537 container_of(work, struct klp_patch, free_work);
538
539 klp_free_patch_finish(patch);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600540}
541
542static int klp_init_func(struct klp_object *obj, struct klp_func *func)
543{
Petr Mladek0430f782019-01-09 13:43:21 +0100544 int ret;
545
Miroslav Benesf09d9082016-04-28 16:34:08 +0200546 if (!func->old_name || !func->new_func)
547 return -EINVAL;
548
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530549 if (strlen(func->old_name) >= KSYM_NAME_LEN)
550 return -EINVAL;
551
Josh Poimboeuf3c33f5b2015-01-20 09:26:19 -0600552 INIT_LIST_HEAD(&func->stack_node);
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600553 func->patched = false;
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600554 func->transition = false;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600555
Chris J Arges444f9e92015-12-01 20:40:56 -0600556 /* The format for the sysfs directory is <function,sympos> where sympos
557 * is the nth occurrence of this symbol in kallsyms for the patched
558 * object. If the user selects 0 for old_sympos, then 1 will be used
559 * since a unique symbol will be the first occurrence.
560 */
Petr Mladek0430f782019-01-09 13:43:21 +0100561 ret = kobject_init_and_add(&func->kobj, &klp_ktype_func,
562 &obj->kobj, "%s,%lu", func->old_name,
563 func->old_sympos ? func->old_sympos : 1);
564 if (!ret)
565 func->kobj_added = true;
566
567 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600568}
569
Jessica Yu255e7322016-08-17 20:58:28 -0400570/* Arches may override this to finish any remaining arch-specific tasks */
571void __weak arch_klp_init_object_loaded(struct klp_patch *patch,
572 struct klp_object *obj)
573{
574}
575
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600576/* parts of the initialization that is done only when the object is loaded */
577static int klp_init_object_loaded(struct klp_patch *patch,
578 struct klp_object *obj)
579{
580 struct klp_func *func;
581 int ret;
582
Jessica Yu255e7322016-08-17 20:58:28 -0400583 module_disable_ro(patch->mod);
Jessica Yu425595a2016-03-22 20:03:18 -0400584 ret = klp_write_object_relocations(patch->mod, obj);
Jessica Yu255e7322016-08-17 20:58:28 -0400585 if (ret) {
586 module_enable_ro(patch->mod, true);
Jessica Yu425595a2016-03-22 20:03:18 -0400587 return ret;
Jessica Yu255e7322016-08-17 20:58:28 -0400588 }
589
590 arch_klp_init_object_loaded(patch, obj);
591 module_enable_ro(patch->mod, true);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600592
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200593 klp_for_each_func(obj, func) {
Chris J Argesb2b018e2015-12-01 20:40:54 -0600594 ret = klp_find_object_symbol(obj->name, func->old_name,
595 func->old_sympos,
Petr Mladek19514912019-01-09 13:43:19 +0100596 (unsigned long *)&func->old_func);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600597 if (ret)
598 return ret;
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600599
Petr Mladek19514912019-01-09 13:43:19 +0100600 ret = kallsyms_lookup_size_offset((unsigned long)func->old_func,
Josh Poimboeuff5e547f2017-02-13 19:42:39 -0600601 &func->old_size, NULL);
602 if (!ret) {
603 pr_err("kallsyms size lookup failed for '%s'\n",
604 func->old_name);
605 return -ENOENT;
606 }
607
608 ret = kallsyms_lookup_size_offset((unsigned long)func->new_func,
609 &func->new_size, NULL);
610 if (!ret) {
611 pr_err("kallsyms size lookup failed for '%s' replacement\n",
612 func->old_name);
613 return -ENOENT;
614 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600615 }
616
617 return 0;
618}
619
620static int klp_init_object(struct klp_patch *patch, struct klp_object *obj)
621{
622 struct klp_func *func;
623 int ret;
624 const char *name;
625
Kamalesh Babulal6e9df952018-07-20 15:16:42 +0530626 if (klp_is_module(obj) && strlen(obj->name) >= MODULE_NAME_LEN)
627 return -EINVAL;
628
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600629 obj->patched = false;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100630 obj->mod = NULL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600631
632 klp_find_object_module(obj);
633
634 name = klp_is_module(obj) ? obj->name : "vmlinux";
Miroslav Benescad706d2015-05-19 12:01:18 +0200635 ret = kobject_init_and_add(&obj->kobj, &klp_ktype_object,
636 &patch->kobj, "%s", name);
637 if (ret)
638 return ret;
Petr Mladek0430f782019-01-09 13:43:21 +0100639 obj->kobj_added = true;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600640
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200641 klp_for_each_func(obj, func) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600642 ret = klp_init_func(obj, func);
643 if (ret)
Petr Mladek0430f782019-01-09 13:43:21 +0100644 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600645 }
646
Petr Mladek0430f782019-01-09 13:43:21 +0100647 if (klp_is_object_loaded(obj))
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600648 ret = klp_init_object_loaded(patch, obj);
Petr Mladek0430f782019-01-09 13:43:21 +0100649
650 return ret;
651}
652
653static int klp_init_patch_early(struct klp_patch *patch)
654{
655 struct klp_object *obj;
656 struct klp_func *func;
657
658 if (!patch->objs)
659 return -EINVAL;
660
661 INIT_LIST_HEAD(&patch->list);
Jason Baron20e55022019-01-09 13:43:24 +0100662 INIT_LIST_HEAD(&patch->obj_list);
Petr Mladek0430f782019-01-09 13:43:21 +0100663 patch->kobj_added = false;
664 patch->enabled = false;
Petr Mladek68007282019-01-09 13:43:22 +0100665 patch->forced = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100666 INIT_WORK(&patch->free_work, klp_free_patch_work_fn);
Petr Mladek0430f782019-01-09 13:43:21 +0100667 init_completion(&patch->finish);
668
Jason Baron20e55022019-01-09 13:43:24 +0100669 klp_for_each_object_static(patch, obj) {
Petr Mladek0430f782019-01-09 13:43:21 +0100670 if (!obj->funcs)
671 return -EINVAL;
672
Jason Baron20e55022019-01-09 13:43:24 +0100673 INIT_LIST_HEAD(&obj->func_list);
Petr Mladek0430f782019-01-09 13:43:21 +0100674 obj->kobj_added = false;
Jason Baron20e55022019-01-09 13:43:24 +0100675 list_add_tail(&obj->node, &patch->obj_list);
Petr Mladek0430f782019-01-09 13:43:21 +0100676
Jason Baron20e55022019-01-09 13:43:24 +0100677 klp_for_each_func_static(obj, func) {
Petr Mladek0430f782019-01-09 13:43:21 +0100678 func->kobj_added = false;
Jason Baron20e55022019-01-09 13:43:24 +0100679 list_add_tail(&func->node, &obj->func_list);
680 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600681 }
682
Petr Mladek958ef1e2019-01-09 13:43:23 +0100683 if (!try_module_get(patch->mod))
684 return -ENODEV;
685
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600686 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600687}
688
689static int klp_init_patch(struct klp_patch *patch)
690{
691 struct klp_object *obj;
692 int ret;
693
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600694 ret = kobject_init_and_add(&patch->kobj, &klp_ktype_patch,
Jiri Kosinae0b561e2015-02-15 10:03:20 +0100695 klp_root_kobj, "%s", patch->mod->name);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100696 if (ret)
Josh Poimboeuf3ec24772017-03-06 11:20:29 -0600697 return ret;
Petr Mladek0430f782019-01-09 13:43:21 +0100698 patch->kobj_added = true;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600699
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200700 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600701 ret = klp_init_object(patch, obj);
702 if (ret)
Petr Mladek958ef1e2019-01-09 13:43:23 +0100703 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600704 }
705
Josh Poimboeuf99590ba2015-01-09 14:03:04 -0600706 list_add_tail(&patch->list, &klp_patches);
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600707
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600708 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600709}
710
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100711static int __klp_disable_patch(struct klp_patch *patch)
712{
713 struct klp_object *obj;
714
715 if (WARN_ON(!patch->enabled))
716 return -EINVAL;
717
718 if (klp_transition_patch)
719 return -EBUSY;
720
721 /* enforce stacking: only the last enabled patch can be disabled */
Petr Mladek958ef1e2019-01-09 13:43:23 +0100722 if (!list_is_last(&patch->list, &klp_patches))
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100723 return -EBUSY;
724
725 klp_init_transition(patch, KLP_UNPATCHED);
726
727 klp_for_each_object(patch, obj)
728 if (obj->patched)
729 klp_pre_unpatch_callback(obj);
730
731 /*
732 * Enforce the order of the func->transition writes in
733 * klp_init_transition() and the TIF_PATCH_PENDING writes in
734 * klp_start_transition(). In the rare case where klp_ftrace_handler()
735 * is called shortly after klp_update_patch_state() switches the task,
736 * this ensures the handler sees that func->transition is set.
737 */
738 smp_wmb();
739
740 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100741 patch->enabled = false;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100742 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100743
744 return 0;
745}
746
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100747static int __klp_enable_patch(struct klp_patch *patch)
748{
749 struct klp_object *obj;
750 int ret;
751
752 if (klp_transition_patch)
753 return -EBUSY;
754
755 if (WARN_ON(patch->enabled))
756 return -EINVAL;
757
Petr Mladek958ef1e2019-01-09 13:43:23 +0100758 if (!patch->kobj_added)
759 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100760
761 pr_notice("enabling patch '%s'\n", patch->mod->name);
762
763 klp_init_transition(patch, KLP_PATCHED);
764
765 /*
766 * Enforce the order of the func->transition writes in
767 * klp_init_transition() and the ops->func_stack writes in
768 * klp_patch_object(), so that klp_ftrace_handler() will see the
769 * func->transition updates before the handler is registered and the
770 * new funcs become visible to the handler.
771 */
772 smp_wmb();
773
774 klp_for_each_object(patch, obj) {
775 if (!klp_is_object_loaded(obj))
776 continue;
777
778 ret = klp_pre_patch_callback(obj);
779 if (ret) {
780 pr_warn("pre-patch callback failed for object '%s'\n",
781 klp_is_module(obj) ? obj->name : "vmlinux");
782 goto err;
783 }
784
785 ret = klp_patch_object(obj);
786 if (ret) {
787 pr_warn("failed to patch object '%s'\n",
788 klp_is_module(obj) ? obj->name : "vmlinux");
789 goto err;
790 }
791 }
792
793 klp_start_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100794 patch->enabled = true;
Petr Mladek958ef1e2019-01-09 13:43:23 +0100795 klp_try_complete_transition();
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100796
797 return 0;
798err:
799 pr_warn("failed to enable patch '%s'\n", patch->mod->name);
800
801 klp_cancel_transition();
802 return ret;
803}
804
805/**
Petr Mladek958ef1e2019-01-09 13:43:23 +0100806 * klp_enable_patch() - enable the livepatch
807 * @patch: patch to be enabled
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100808 *
Petr Mladek958ef1e2019-01-09 13:43:23 +0100809 * Initializes the data structure associated with the patch, creates the sysfs
810 * interface, performs the needed symbol lookups and code relocations,
811 * registers the patched functions with ftrace.
812 *
813 * This function is supposed to be called from the livepatch module_init()
814 * callback.
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100815 *
816 * Return: 0 on success, otherwise error
817 */
818int klp_enable_patch(struct klp_patch *patch)
819{
820 int ret;
821
Petr Mladek958ef1e2019-01-09 13:43:23 +0100822 if (!patch || !patch->mod)
823 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100824
Petr Mladek958ef1e2019-01-09 13:43:23 +0100825 if (!is_livepatch_module(patch->mod)) {
826 pr_err("module %s is not marked as a livepatch module\n",
827 patch->mod->name);
828 return -EINVAL;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100829 }
830
Petr Mladek958ef1e2019-01-09 13:43:23 +0100831 if (!klp_initialized())
832 return -ENODEV;
833
834 if (!klp_have_reliable_stack()) {
835 pr_err("This architecture doesn't have support for the livepatch consistency model.\n");
836 return -ENOSYS;
837 }
838
839
840 mutex_lock(&klp_mutex);
841
842 ret = klp_init_patch_early(patch);
843 if (ret) {
844 mutex_unlock(&klp_mutex);
845 return ret;
846 }
847
848 ret = klp_init_patch(patch);
849 if (ret)
850 goto err;
851
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100852 ret = __klp_enable_patch(patch);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100853 if (ret)
854 goto err;
855
856 mutex_unlock(&klp_mutex);
857
858 return 0;
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100859
860err:
Petr Mladek958ef1e2019-01-09 13:43:23 +0100861 klp_free_patch_start(patch);
862
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100863 mutex_unlock(&klp_mutex);
Petr Mladek958ef1e2019-01-09 13:43:23 +0100864
865 klp_free_patch_finish(patch);
866
Petr Mladek26c3e98e2019-01-09 13:43:20 +0100867 return ret;
868}
869EXPORT_SYMBOL_GPL(klp_enable_patch);
870
Joe Lawrenceef8daf82017-10-02 11:56:48 -0400871/*
872 * Remove parts of patches that touch a given kernel module. The list of
873 * patches processed might be limited. When limit is NULL, all patches
874 * will be handled.
875 */
876static void klp_cleanup_module_patches_limited(struct module *mod,
877 struct klp_patch *limit)
878{
879 struct klp_patch *patch;
880 struct klp_object *obj;
881
882 list_for_each_entry(patch, &klp_patches, list) {
883 if (patch == limit)
884 break;
885
886 klp_for_each_object(patch, obj) {
887 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
888 continue;
889
890 /*
891 * Only unpatch the module if the patch is enabled or
892 * is in transition.
893 */
894 if (patch->enabled || patch == klp_transition_patch) {
Jiri Kosinafc41efc182017-11-15 10:53:24 +0100895
896 if (patch != klp_transition_patch)
897 klp_pre_unpatch_callback(obj);
898
Joe Lawrenceef8daf82017-10-02 11:56:48 -0400899 pr_notice("reverting patch '%s' on unloading module '%s'\n",
900 patch->mod->name, obj->mod->name);
901 klp_unpatch_object(obj);
Jiri Kosinafc41efc182017-11-15 10:53:24 +0100902
903 klp_post_unpatch_callback(obj);
Joe Lawrenceef8daf82017-10-02 11:56:48 -0400904 }
905
906 klp_free_object_loaded(obj);
907 break;
908 }
909 }
910}
911
Jessica Yu7e545d62016-03-16 20:55:39 -0400912int klp_module_coming(struct module *mod)
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600913{
Minfei Huang36e505c2015-05-15 10:22:48 +0800914 int ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600915 struct klp_patch *patch;
916 struct klp_object *obj;
917
Jessica Yu7e545d62016-03-16 20:55:39 -0400918 if (WARN_ON(mod->state != MODULE_STATE_COMING))
919 return -EINVAL;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600920
921 mutex_lock(&klp_mutex);
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100922 /*
Jessica Yu7e545d62016-03-16 20:55:39 -0400923 * Each module has to know that klp_module_coming()
924 * has been called. We never know what module will
925 * get patched by a new patch.
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100926 */
Jessica Yu7e545d62016-03-16 20:55:39 -0400927 mod->klp_alive = true;
Petr Mladek8cb2c2d2015-03-12 12:55:13 +0100928
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600929 list_for_each_entry(patch, &klp_patches, list) {
Jiri Slaby8cdd0432015-05-19 12:01:19 +0200930 klp_for_each_object(patch, obj) {
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600931 if (!klp_is_module(obj) || strcmp(obj->name, mod->name))
932 continue;
933
Jessica Yu7e545d62016-03-16 20:55:39 -0400934 obj->mod = mod;
935
936 ret = klp_init_object_loaded(patch, obj);
937 if (ret) {
938 pr_warn("failed to initialize patch '%s' for module '%s' (%d)\n",
939 patch->mod->name, obj->mod->name, ret);
940 goto err;
941 }
942
Josh Poimboeufd83a7cb2017-02-13 19:42:40 -0600943 /*
944 * Only patch the module if the patch is enabled or is
945 * in transition.
946 */
947 if (!patch->enabled && patch != klp_transition_patch)
Jessica Yu7e545d62016-03-16 20:55:39 -0400948 break;
949
950 pr_notice("applying patch '%s' to loading module '%s'\n",
951 patch->mod->name, obj->mod->name);
952
Joe Lawrence93862e32017-10-13 15:08:41 -0400953 ret = klp_pre_patch_callback(obj);
954 if (ret) {
955 pr_warn("pre-patch callback failed for object '%s'\n",
956 obj->name);
957 goto err;
958 }
959
Josh Poimboeuf0dade9f2017-02-13 19:42:35 -0600960 ret = klp_patch_object(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -0400961 if (ret) {
962 pr_warn("failed to apply patch '%s' to module '%s' (%d)\n",
963 patch->mod->name, obj->mod->name, ret);
Joe Lawrence93862e32017-10-13 15:08:41 -0400964
Petr Mladek5aaf1ab2017-10-20 16:56:50 +0200965 klp_post_unpatch_callback(obj);
Jessica Yu7e545d62016-03-16 20:55:39 -0400966 goto err;
967 }
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600968
Joe Lawrence93862e32017-10-13 15:08:41 -0400969 if (patch != klp_transition_patch)
970 klp_post_patch_callback(obj);
971
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600972 break;
973 }
974 }
975
976 mutex_unlock(&klp_mutex);
977
978 return 0;
Jessica Yu7e545d62016-03-16 20:55:39 -0400979
980err:
981 /*
982 * If a patch is unsuccessfully applied, return
983 * error to the module loader.
984 */
985 pr_warn("patch '%s' failed for module '%s', refusing to load module '%s'\n",
986 patch->mod->name, obj->mod->name, obj->mod->name);
987 mod->klp_alive = false;
Joe Lawrenceef8daf82017-10-02 11:56:48 -0400988 klp_cleanup_module_patches_limited(mod, patch);
Jessica Yu7e545d62016-03-16 20:55:39 -0400989 mutex_unlock(&klp_mutex);
990
991 return ret;
Seth Jenningsb700e7f2014-12-16 11:58:19 -0600992}
993
Jessica Yu7e545d62016-03-16 20:55:39 -0400994void klp_module_going(struct module *mod)
995{
Jessica Yu7e545d62016-03-16 20:55:39 -0400996 if (WARN_ON(mod->state != MODULE_STATE_GOING &&
997 mod->state != MODULE_STATE_COMING))
998 return;
999
1000 mutex_lock(&klp_mutex);
1001 /*
1002 * Each module has to know that klp_module_going()
1003 * has been called. We never know what module will
1004 * get patched by a new patch.
1005 */
1006 mod->klp_alive = false;
1007
Joe Lawrenceef8daf82017-10-02 11:56:48 -04001008 klp_cleanup_module_patches_limited(mod, NULL);
Jessica Yu7e545d62016-03-16 20:55:39 -04001009
1010 mutex_unlock(&klp_mutex);
1011}
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001012
Minfei Huang26029d82015-05-22 22:26:29 +08001013static int __init klp_init(void)
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001014{
1015 int ret;
1016
Jiri Kosinab9dfe0b2015-01-09 10:53:21 +01001017 ret = klp_check_compiler_support();
1018 if (ret) {
1019 pr_info("Your compiler is too old; turning off.\n");
1020 return -EINVAL;
1021 }
1022
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001023 klp_root_kobj = kobject_create_and_add("livepatch", kernel_kobj);
Jessica Yu7e545d62016-03-16 20:55:39 -04001024 if (!klp_root_kobj)
1025 return -ENOMEM;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001026
1027 return 0;
Seth Jenningsb700e7f2014-12-16 11:58:19 -06001028}
1029
1030module_init(klp_init);