undo: clean up if only new_code loaded, too
[ksplice.git] / kmodsrc / ksplice.c
blob52d60a058ec7e2482c85b9a6ff0930508f337373
1 /* Copyright (C) 2007-2009 Ksplice, Inc.
2 * Authors: Jeff Arnold, Anders Kaseorg, Tim Abbott
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
15 * 02110-1301, USA.
18 #include <linux/module.h>
19 #include <linux/version.h>
20 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
21 #include <linux/bug.h>
22 #else /* LINUX_VERSION_CODE */
23 /* 7664c5a1da4711bb6383117f51b94c8dc8f3f1cd was after 2.6.19 */
24 #endif /* LINUX_VERSION_CODE */
25 #include <linux/ctype.h>
26 #if defined CONFIG_DEBUG_FS || LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
27 #include <linux/debugfs.h>
28 #else /* CONFIG_DEBUG_FS */
29 /* a7a76cefc4b12bb6508afa4c77f11c2752cc365d was after 2.6.11 */
30 #endif /* CONFIG_DEBUG_FS */
31 #include <linux/errno.h>
32 #include <linux/kallsyms.h>
33 #include <linux/kobject.h>
34 #include <linux/kthread.h>
35 #include <linux/pagemap.h>
36 #include <linux/sched.h>
37 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12)
38 #include <linux/sort.h>
39 #else /* LINUX_VERSION_CODE < */
40 /* 8c63b6d337534a6b5fb111dc27d0850f535118c0 was after 2.6.11 */
41 #endif /* LINUX_VERSION_CODE */
42 #include <linux/stop_machine.h>
43 #include <linux/sysfs.h>
44 #include <linux/time.h>
45 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18)
46 #include <linux/uaccess.h>
47 #else /* LINUX_VERSION_CODE < */
48 /* linux/uaccess.h doesn't exist in kernels before 2.6.18 */
49 #include <asm/uaccess.h>
50 #endif /* LINUX_VERSION_CODE */
51 #include <linux/vmalloc.h>
52 #ifdef KSPLICE_STANDALONE
53 #include "ksplice.h"
54 #else /* !KSPLICE_STANDALONE */
55 #include <linux/ksplice.h>
56 #endif /* KSPLICE_STANDALONE */
57 #ifdef KSPLICE_NEED_PARAINSTRUCTIONS
58 #include <asm/alternative.h>
59 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
61 #ifdef KSPLICE_STANDALONE
62 #if !defined(CONFIG_KSPLICE) && !defined(CONFIG_KSPLICE_MODULE)
63 #define KSPLICE_NO_KERNEL_SUPPORT 1
64 #endif /* !CONFIG_KSPLICE && !CONFIG_KSPLICE_MODULE */
66 #ifndef __used
67 #define __used __attribute_used__
68 #endif
70 #define EXTRACT_SYMBOL(sym) \
71 static const typeof(&sym) PASTE(__ksplice_extract_, __LINE__) \
72 __used __attribute__((section(".ksplice_extract"))) = &sym
73 #endif /* KSPLICE_STANDALONE */
75 enum stage {
76 STAGE_PREPARING, /* the update is not yet applied */
77 STAGE_APPLIED, /* the update is applied */
78 STAGE_REVERSED, /* the update has been applied and reversed */
81 /* parameter to modify run-pre matching */
82 enum run_pre_mode {
83 RUN_PRE_INITIAL, /* dry run (only change temp_labelvals) */
84 RUN_PRE_DEBUG, /* dry run with byte-by-byte debugging */
85 RUN_PRE_FINAL, /* finalizes the matching */
86 #ifndef CONFIG_FUNCTION_DATA_SECTIONS
87 RUN_PRE_SILENT,
88 #endif /* !CONFIG_FUNCTION_DATA_SECTIONS */
91 enum { NOVAL, TEMP, VAL };
93 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9)
94 /* 5d7b32de9935c65ca8285ac6ec2382afdbb5d479 was after 2.6.8 */
95 #define __bitwise__
96 #elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
97 /* af4ca457eaf2d6682059c18463eb106e2ce58198 was after 2.6.14 */
98 #define __bitwise__ __bitwise
99 #endif
101 typedef int __bitwise__ abort_t;
103 #define OK ((__force abort_t) 0)
104 #define NO_MATCH ((__force abort_t) 1)
105 #define CODE_BUSY ((__force abort_t) 2)
106 #define MODULE_BUSY ((__force abort_t) 3)
107 #define OUT_OF_MEMORY ((__force abort_t) 4)
108 #define FAILED_TO_FIND ((__force abort_t) 5)
109 #define ALREADY_REVERSED ((__force abort_t) 6)
110 #define MISSING_EXPORT ((__force abort_t) 7)
111 #define UNEXPECTED_RUNNING_TASK ((__force abort_t) 8)
112 #define UNEXPECTED ((__force abort_t) 9)
113 #define TARGET_NOT_LOADED ((__force abort_t) 10)
114 #define CALL_FAILED ((__force abort_t) 11)
115 #define COLD_UPDATE_LOADED ((__force abort_t) 12)
116 #ifdef KSPLICE_STANDALONE
117 #define BAD_SYSTEM_MAP ((__force abort_t) 13)
118 #endif /* KSPLICE_STANDALONE */
120 struct update {
121 const char *kid;
122 const char *name;
123 struct kobject kobj;
124 enum stage stage;
125 abort_t abort_cause;
126 int debug;
127 #ifdef CONFIG_DEBUG_FS
128 struct debugfs_blob_wrapper debug_blob;
129 struct dentry *debugfs_dentry;
130 #else /* !CONFIG_DEBUG_FS */
131 bool debug_continue_line;
132 #endif /* CONFIG_DEBUG_FS */
133 bool partial; /* is it OK if some target mods aren't loaded */
134 struct list_head changes, /* changes for loaded target mods */
135 unused_changes; /* changes for non-loaded target mods */
136 struct list_head conflicts;
137 struct list_head list;
138 struct list_head ksplice_module_list;
141 /* a process conflicting with an update */
142 struct conflict {
143 const char *process_name;
144 pid_t pid;
145 struct list_head stack;
146 struct list_head list;
149 /* an address on the stack of a conflict */
150 struct conflict_addr {
151 unsigned long addr; /* the address on the stack */
152 bool has_conflict; /* does this address in particular conflict? */
153 const char *label; /* the label of the conflicting safety_record */
154 struct list_head list;
157 #if defined(CONFIG_DEBUG_FS) && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
158 /* Old kernels don't have debugfs_create_blob */
159 struct debugfs_blob_wrapper {
160 void *data;
161 unsigned long size;
163 #endif /* CONFIG_DEBUG_FS && LINUX_VERSION_CODE */
165 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
166 /* 930631edd4b1fe2781d9fe90edbe35d89dfc94cc was after 2.6.18 */
167 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
168 #endif
170 struct labelval {
171 struct list_head list;
172 struct ksplice_symbol *symbol;
173 struct list_head *saved_vals;
176 /* region to be checked for conflicts in the stack check */
177 struct safety_record {
178 struct list_head list;
179 const char *label;
180 unsigned long addr; /* the address to be checked for conflicts
181 * (e.g. an obsolete function's starting addr)
183 unsigned long size; /* the size of the region to be checked */
186 /* possible value for a symbol */
187 struct candidate_val {
188 struct list_head list;
189 unsigned long val;
192 /* private struct used by init_symbol_array */
193 struct ksplice_lookup {
194 /* input */
195 struct ksplice_mod_change *change;
196 struct ksplice_symbol **arr;
197 size_t size;
198 /* output */
199 abort_t ret;
202 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
203 /* c6b37801911d7f4663c99cad8aa230bc934cea82 was after 2.6.29 */
204 struct symsearch {
205 const struct kernel_symbol *start, *stop;
206 const unsigned long *crcs;
207 enum {
208 NOT_GPL_ONLY,
209 GPL_ONLY,
210 WILL_BE_GPL_ONLY,
211 } licence;
212 bool unused;
214 #endif /* LINUX_VERSION_CODE */
216 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
217 /* c33fa9f5609e918824446ef9a75319d4a802f1f4 was after 2.6.25 */
219 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
220 /* 2fff0a48416af891dce38fd425246e337831e0bb was after 2.6.19 */
221 static bool virtual_address_mapped(unsigned long addr)
223 char retval;
224 return probe_kernel_address(addr, retval) != -EFAULT;
226 #else /* LINUX_VERSION_CODE < */
227 static bool virtual_address_mapped(unsigned long addr);
228 #endif /* LINUX_VERSION_CODE */
230 static long probe_kernel_read(void *dst, void *src, size_t size)
232 if (size == 0)
233 return 0;
234 if (!virtual_address_mapped((unsigned long)src) ||
235 !virtual_address_mapped((unsigned long)src + size - 1))
236 return -EFAULT;
238 memcpy(dst, src, size);
239 return 0;
241 #endif /* LINUX_VERSION_CODE */
243 static LIST_HEAD(updates);
244 #ifdef KSPLICE_STANDALONE
245 #if defined(CONFIG_KSPLICE) || defined(CONFIG_KSPLICE_MODULE)
246 extern struct list_head ksplice_modules;
247 #else /* !CONFIG_KSPLICE */
248 LIST_HEAD(ksplice_modules);
249 #endif /* CONFIG_KSPLICE */
250 #else /* !KSPLICE_STANDALONE */
251 LIST_HEAD(ksplice_modules);
252 EXPORT_SYMBOL_GPL(ksplice_modules);
253 static struct kobject *ksplice_kobj;
254 #endif /* KSPLICE_STANDALONE */
256 static struct kobj_type update_ktype;
258 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9)
259 /* Old kernels do not have kcalloc
260 * e629946abd0bb8266e9c3d0fd1bff2ef8dec5443 was after 2.6.8
262 static void *kcalloc(size_t n, size_t size, typeof(GFP_KERNEL) flags)
264 char *mem;
265 if (n != 0 && size > ULONG_MAX / n)
266 return NULL;
267 mem = kmalloc(n * size, flags);
268 if (mem)
269 memset(mem, 0, n * size);
270 return mem;
272 #endif /* LINUX_VERSION_CODE */
274 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
275 /* 8c63b6d337534a6b5fb111dc27d0850f535118c0 was after 2.6.11 */
276 static void u32_swap(void *a, void *b, int size)
278 u32 t = *(u32 *)a;
279 *(u32 *)a = *(u32 *)b;
280 *(u32 *)b = t;
283 static void generic_swap(void *a, void *b, int size)
285 char t;
287 do {
288 t = *(char *)a;
289 *(char *)a++ = *(char *)b;
290 *(char *)b++ = t;
291 } while (--size > 0);
295 * sort - sort an array of elements
296 * @base: pointer to data to sort
297 * @num: number of elements
298 * @size: size of each element
299 * @cmp: pointer to comparison function
300 * @swap: pointer to swap function or NULL
302 * This function does a heapsort on the given array. You may provide a
303 * swap function optimized to your element type.
305 * Sorting time is O(n log n) both on average and worst-case. While
306 * qsort is about 20% faster on average, it suffers from exploitable
307 * O(n*n) worst-case behavior and extra memory requirements that make
308 * it less suitable for kernel use.
311 void sort(void *base, size_t num, size_t size,
312 int (*cmp)(const void *, const void *),
313 void (*swap)(void *, void *, int size))
315 /* pre-scale counters for performance */
316 int i = (num / 2 - 1) * size, n = num * size, c, r;
318 if (!swap)
319 swap = (size == 4 ? u32_swap : generic_swap);
321 /* heapify */
322 for (; i >= 0; i -= size) {
323 for (r = i; r * 2 + size < n; r = c) {
324 c = r * 2 + size;
325 if (c < n - size && cmp(base + c, base + c + size) < 0)
326 c += size;
327 if (cmp(base + r, base + c) >= 0)
328 break;
329 swap(base + r, base + c, size);
333 /* sort */
334 for (i = n - size; i > 0; i -= size) {
335 swap(base, base + i, size);
336 for (r = 0; r * 2 + size < i; r = c) {
337 c = r * 2 + size;
338 if (c < i - size && cmp(base + c, base + c + size) < 0)
339 c += size;
340 if (cmp(base + r, base + c) >= 0)
341 break;
342 swap(base + r, base + c, size);
346 #endif /* LINUX_VERSION_CODE < */
348 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
349 /* Old kernels do not have kstrdup
350 * 543537bd922692bc978e2e356fcd8bfc9c2ee7d5 was after 2.6.12
352 #define kstrdup ksplice_kstrdup
353 static char *kstrdup(const char *s, typeof(GFP_KERNEL) gfp)
355 size_t len;
356 char *buf;
358 if (!s)
359 return NULL;
361 len = strlen(s) + 1;
362 buf = kmalloc(len, gfp);
363 if (buf)
364 memcpy(buf, s, len);
365 return buf;
367 #endif /* LINUX_VERSION_CODE */
369 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
370 /* Old kernels use semaphore instead of mutex
371 * 97d1f15b7ef52c1e9c28dc48b454024bb53a5fd2 was after 2.6.16
373 #define mutex semaphore
374 #define mutex_lock down
375 #define mutex_unlock up
376 #endif /* LINUX_VERSION_CODE */
378 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
379 /* 11443ec7d9286dd25663516436a14edfb5f43857 was after 2.6.21 */
380 static char * __attribute_used__
381 kvasprintf(typeof(GFP_KERNEL) gfp, const char *fmt, va_list ap)
383 unsigned int len;
384 char *p, dummy[1];
385 va_list aq;
387 va_copy(aq, ap);
388 len = vsnprintf(dummy, 0, fmt, aq);
389 va_end(aq);
391 p = kmalloc(len + 1, gfp);
392 if (!p)
393 return NULL;
395 vsnprintf(p, len + 1, fmt, ap);
397 return p;
399 #endif
401 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
402 /* e905914f96e11862b130dd229f73045dad9a34e8 was after 2.6.17 */
403 static char * __attribute__((format (printf, 2, 3)))
404 kasprintf(typeof(GFP_KERNEL) gfp, const char *fmt, ...)
406 va_list ap;
407 char *p;
409 va_start(ap, fmt);
410 p = kvasprintf(gfp, fmt, ap);
411 va_end(ap);
413 return p;
415 #endif
417 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
418 /* 06b2a76d25d3cfbd14680021c1d356c91be6904e was after 2.6.24 */
419 static int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
421 char *tail;
422 unsigned long val;
423 size_t len;
425 *res = 0;
426 len = strlen(cp);
427 if (len == 0)
428 return -EINVAL;
430 val = simple_strtoul(cp, &tail, base);
431 if ((*tail == '\0') ||
432 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
433 *res = val;
434 return 0;
437 return -EINVAL;
439 #endif
441 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
442 /* 9b1a4d38373a5581a4e01032a3ccdd94cd93477b was after 2.6.26 */
443 /* Assume cpus == NULL. */
444 #define stop_machine(fn, data, cpus) stop_machine_run(fn, data, NR_CPUS);
445 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
446 /* ee527cd3a20c2aeaac17d939e5d011f7a76d69f5 was after 2.6.21 */
447 EXTRACT_SYMBOL(stop_machine_run);
448 #endif /* LINUX_VERSION_CODE */
449 #endif /* LINUX_VERSION_CODE */
451 #ifndef task_thread_info
452 #define task_thread_info(task) (task)->thread_info
453 #endif /* !task_thread_info */
455 #ifdef KSPLICE_STANDALONE
457 #ifdef do_each_thread_ve /* OpenVZ kernels define this */
458 #define do_each_thread do_each_thread_all
459 #define while_each_thread while_each_thread_all
460 #endif
462 static bool bootstrapped = false;
464 /* defined by ksplice-create */
465 extern const struct ksplice_reloc ksplice_init_relocs[],
466 ksplice_init_relocs_end[];
468 #endif /* KSPLICE_STANDALONE */
470 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
471 /* c6b37801911d7f4663c99cad8aa230bc934cea82 was after 2.6.29 */
472 extern struct list_head modules;
473 EXTRACT_SYMBOL(modules);
474 extern struct mutex module_mutex;
475 EXTRACT_SYMBOL(module_mutex);
476 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,18) && defined(CONFIG_UNUSED_SYMBOLS)
477 /* f71d20e961474dde77e6558396efb93d6ac80a4b was after 2.6.17 */
478 #define KSPLICE_KSYMTAB_UNUSED_SUPPORT 1
479 #endif /* LINUX_VERSION_CODE */
480 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,17)
481 /* 9f28bb7e1d0188a993403ab39b774785892805e1 was after 2.6.16 */
482 #define KSPLICE_KSYMTAB_FUTURE_SUPPORT 1
483 #endif /* LINUX_VERSION_CODE */
484 extern const struct kernel_symbol __start___ksymtab[];
485 EXTRACT_SYMBOL(__start___ksymtab);
486 extern const struct kernel_symbol __stop___ksymtab[];
487 EXTRACT_SYMBOL(__stop___ksymtab);
488 extern const unsigned long __start___kcrctab[];
489 EXTRACT_SYMBOL(__start___kcrctab);
490 extern const struct kernel_symbol __start___ksymtab_gpl[];
491 EXTRACT_SYMBOL(__start___ksymtab_gpl);
492 extern const struct kernel_symbol __stop___ksymtab_gpl[];
493 EXTRACT_SYMBOL(__stop___ksymtab_gpl);
494 extern const unsigned long __start___kcrctab_gpl[];
495 EXTRACT_SYMBOL(__start___kcrctab_gpl);
496 #ifdef KSPLICE_KSYMTAB_UNUSED_SUPPORT
497 extern const struct kernel_symbol __start___ksymtab_unused[];
498 EXTRACT_SYMBOL(__start___ksymtab_unused);
499 extern const struct kernel_symbol __stop___ksymtab_unused[];
500 EXTRACT_SYMBOL(__stop___ksymtab_unused);
501 extern const unsigned long __start___kcrctab_unused[];
502 EXTRACT_SYMBOL(__start___kcrctab_unused);
503 extern const struct kernel_symbol __start___ksymtab_unused_gpl[];
504 EXTRACT_SYMBOL(__start___ksymtab_unused_gpl);
505 extern const struct kernel_symbol __stop___ksymtab_unused_gpl[];
506 EXTRACT_SYMBOL(__stop___ksymtab_unused_gpl);
507 extern const unsigned long __start___kcrctab_unused_gpl[];
508 EXTRACT_SYMBOL(__start___kcrctab_unused_gpl);
509 #endif /* KSPLICE_KSYMTAB_UNUSED_SUPPORT */
510 #ifdef KSPLICE_KSYMTAB_FUTURE_SUPPORT
511 extern const struct kernel_symbol __start___ksymtab_gpl_future[];
512 EXTRACT_SYMBOL(__start___ksymtab_gpl_future);
513 extern const struct kernel_symbol __stop___ksymtab_gpl_future[];
514 EXTRACT_SYMBOL(__stop___ksymtab_gpl_future);
515 extern const unsigned long __start___kcrctab_gpl_future[];
516 EXTRACT_SYMBOL(__start___kcrctab_gpl_future);
517 #endif /* KSPLICE_KSYMTAB_FUTURE_SUPPORT */
518 #endif /* LINUX_VERSION_CODE */
520 static struct update *init_ksplice_update(const char *kid);
521 static void cleanup_ksplice_update(struct update *update);
522 static void maybe_cleanup_ksplice_update(struct update *update);
523 static void add_to_update(struct ksplice_mod_change *change,
524 struct update *update);
525 static int ksplice_sysfs_init(struct update *update);
527 /* Preparing the relocations and patches for application */
528 static abort_t apply_update(struct update *update);
529 static abort_t reverse_update(struct update *update);
530 static abort_t prepare_change(struct ksplice_mod_change *change);
531 static abort_t finalize_change(struct ksplice_mod_change *change);
532 static abort_t finalize_patches(struct ksplice_mod_change *change);
533 static abort_t add_dependency_on_address(struct ksplice_mod_change *change,
534 unsigned long addr);
535 static abort_t map_trampoline_pages(struct update *update);
536 static void unmap_trampoline_pages(struct update *update);
537 static void *map_writable(void *addr, size_t len);
538 static abort_t apply_relocs(struct ksplice_mod_change *change,
539 const struct ksplice_reloc *relocs,
540 const struct ksplice_reloc *relocs_end);
541 static abort_t apply_reloc(struct ksplice_mod_change *change,
542 const struct ksplice_reloc *r);
543 static abort_t apply_howto_reloc(struct ksplice_mod_change *change,
544 const struct ksplice_reloc *r);
545 static abort_t apply_howto_date(struct ksplice_mod_change *change,
546 const struct ksplice_reloc *r);
547 static abort_t read_reloc_value(struct ksplice_mod_change *change,
548 const struct ksplice_reloc *r,
549 unsigned long addr, unsigned long *valp);
550 static abort_t write_reloc_value(struct ksplice_mod_change *change,
551 const struct ksplice_reloc *r,
552 unsigned long addr, unsigned long sym_addr);
553 static abort_t create_module_list_entry(struct ksplice_mod_change *change,
554 bool to_be_applied);
555 static void cleanup_module_list_entries(struct update *update);
556 static void __attribute__((noreturn)) ksplice_deleted(void);
558 /* run-pre matching */
559 static abort_t match_change_sections(struct ksplice_mod_change *change,
560 bool consider_data_sections);
561 static abort_t find_section(struct ksplice_mod_change *change,
562 struct ksplice_section *sect);
563 static abort_t try_addr(struct ksplice_mod_change *change,
564 struct ksplice_section *sect,
565 unsigned long run_addr,
566 struct list_head *safety_records,
567 enum run_pre_mode mode);
568 static abort_t run_pre_cmp(struct ksplice_mod_change *change,
569 const struct ksplice_section *sect,
570 unsigned long run_addr,
571 struct list_head *safety_records,
572 enum run_pre_mode mode);
573 #ifndef CONFIG_FUNCTION_DATA_SECTIONS
574 /* defined in arch/ARCH/kernel/ksplice-arch.c */
575 static abort_t arch_run_pre_cmp(struct ksplice_mod_change *change,
576 struct ksplice_section *sect,
577 unsigned long run_addr,
578 struct list_head *safety_records,
579 enum run_pre_mode mode);
580 #endif /* CONFIG_FUNCTION_DATA_SECTIONS */
581 static void print_bytes(struct ksplice_mod_change *change,
582 const unsigned char *run, int runc,
583 const unsigned char *pre, int prec);
584 #if defined(KSPLICE_STANDALONE) && !defined(CONFIG_KALLSYMS)
585 static abort_t brute_search(struct ksplice_mod_change *change,
586 struct ksplice_section *sect,
587 const void *start, unsigned long len,
588 struct list_head *vals);
589 static abort_t brute_search_all(struct ksplice_mod_change *change,
590 struct ksplice_section *sect,
591 struct list_head *vals);
592 #endif /* KSPLICE_STANDALONE && !CONFIG_KALLSYMS */
593 static const struct ksplice_reloc *
594 init_reloc_search(struct ksplice_mod_change *change,
595 const struct ksplice_section *sect);
596 static const struct ksplice_reloc *find_reloc(const struct ksplice_reloc *start,
597 const struct ksplice_reloc *end,
598 unsigned long address,
599 unsigned long size);
600 static abort_t lookup_reloc(struct ksplice_mod_change *change,
601 const struct ksplice_reloc **fingerp,
602 unsigned long addr,
603 const struct ksplice_reloc **relocp);
604 static abort_t handle_reloc(struct ksplice_mod_change *change,
605 const struct ksplice_section *sect,
606 const struct ksplice_reloc *r,
607 unsigned long run_addr, enum run_pre_mode mode);
608 static abort_t handle_howto_date(struct ksplice_mod_change *change,
609 const struct ksplice_section *sect,
610 const struct ksplice_reloc *r,
611 unsigned long run_addr,
612 enum run_pre_mode mode);
613 static abort_t handle_howto_reloc(struct ksplice_mod_change *change,
614 const struct ksplice_section *sect,
615 const struct ksplice_reloc *r,
616 unsigned long run_addr,
617 enum run_pre_mode mode);
618 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
619 #ifdef CONFIG_BUG
620 static abort_t handle_bug(struct ksplice_mod_change *change,
621 const struct ksplice_reloc *r,
622 unsigned long run_addr);
623 #endif /* CONFIG_BUG */
624 #else /* LINUX_VERSION_CODE < */
625 /* 7664c5a1da4711bb6383117f51b94c8dc8f3f1cd was after 2.6.19 */
626 #endif /* LINUX_VERSION_CODE */
627 static abort_t handle_extable(struct ksplice_mod_change *change,
628 const struct ksplice_reloc *r,
629 unsigned long run_addr);
630 static struct ksplice_section *symbol_section(struct ksplice_mod_change *change,
631 const struct ksplice_symbol *sym);
632 static int compare_section_labels(const void *va, const void *vb);
633 static int symbol_section_bsearch_compare(const void *a, const void *b);
634 static const struct ksplice_reloc *
635 patch_reloc(struct ksplice_mod_change *change,
636 const struct ksplice_patch *p);
638 /* Computing possible addresses for symbols */
639 static abort_t lookup_symbol(struct ksplice_mod_change *change,
640 const struct ksplice_symbol *ksym,
641 struct list_head *vals);
642 static void cleanup_symbol_arrays(struct ksplice_mod_change *change);
643 static abort_t init_symbol_arrays(struct ksplice_mod_change *change);
644 static abort_t init_symbol_array(struct ksplice_mod_change *change,
645 struct ksplice_symbol *start,
646 struct ksplice_symbol *end);
647 static abort_t uniquify_symbols(struct ksplice_mod_change *change);
648 static abort_t add_matching_values(struct ksplice_lookup *lookup,
649 const char *sym_name, unsigned long sym_val);
650 static bool add_export_values(const struct symsearch *syms,
651 struct module *owner,
652 unsigned int symnum, void *data);
653 static int symbolp_bsearch_compare(const void *key, const void *elt);
654 static int compare_symbolp_names(const void *a, const void *b);
655 static int compare_symbolp_labels(const void *a, const void *b);
656 #ifdef CONFIG_KALLSYMS
657 static int add_kallsyms_values(void *data, const char *name,
658 struct module *owner, unsigned long val);
659 #endif /* CONFIG_KALLSYMS */
660 #ifdef KSPLICE_STANDALONE
661 static abort_t
662 add_system_map_candidates(struct ksplice_mod_change *change,
663 const struct ksplice_system_map *start,
664 const struct ksplice_system_map *end,
665 const char *label, struct list_head *vals);
666 static int compare_system_map(const void *a, const void *b);
667 static int system_map_bsearch_compare(const void *key, const void *elt);
668 #endif /* KSPLICE_STANDALONE */
669 static abort_t new_export_lookup(struct ksplice_mod_change *ichange,
670 const char *name, struct list_head *vals);
672 /* Atomic update trampoline insertion and removal */
673 static abort_t patch_action(struct update *update, enum ksplice_action action);
674 static int __apply_patches(void *update);
675 static int __reverse_patches(void *update);
676 static abort_t check_each_task(struct update *update);
677 static abort_t check_task(struct update *update,
678 const struct task_struct *t, bool rerun);
679 static abort_t check_stack(struct update *update, struct conflict *conf,
680 const struct thread_info *tinfo,
681 const unsigned long *stack);
682 static abort_t check_address(struct update *update,
683 struct conflict *conf, unsigned long addr);
684 static abort_t check_record(struct conflict_addr *ca,
685 const struct safety_record *rec,
686 unsigned long addr);
687 static bool is_stop_machine(const struct task_struct *t);
688 static void cleanup_conflicts(struct update *update);
689 static void print_conflicts(struct update *update);
690 static void insert_trampoline(struct ksplice_patch *p);
691 static abort_t verify_trampoline(struct ksplice_mod_change *change,
692 const struct ksplice_patch *p);
693 static void remove_trampoline(const struct ksplice_patch *p);
695 static abort_t create_labelval(struct ksplice_mod_change *change,
696 struct ksplice_symbol *ksym,
697 unsigned long val, int status);
698 static abort_t create_safety_record(struct ksplice_mod_change *change,
699 const struct ksplice_section *sect,
700 struct list_head *record_list,
701 unsigned long run_addr,
702 unsigned long run_size);
703 static abort_t add_candidate_val(struct ksplice_mod_change *change,
704 struct list_head *vals, unsigned long val);
705 static void release_vals(struct list_head *vals);
706 static void set_temp_labelvals(struct ksplice_mod_change *change, int status);
708 static int contains_canary(struct ksplice_mod_change *change,
709 unsigned long blank_addr,
710 const struct ksplice_reloc_howto *howto);
711 static unsigned long follow_trampolines(struct ksplice_mod_change *change,
712 unsigned long addr);
713 static bool patches_module(const struct module *a, const struct module *b);
714 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
715 /* 66f92cf9d415e96a5bdd6c64de8dd8418595d2fc was after 2.6.29 */
716 static bool strstarts(const char *str, const char *prefix);
717 #endif /* LINUX_VERSION_CODE */
718 static bool singular(struct list_head *list);
719 static void *bsearch(const void *key, const void *base, size_t n,
720 size_t size, int (*cmp)(const void *key, const void *elt));
721 static int compare_relocs(const void *a, const void *b);
722 static int reloc_bsearch_compare(const void *key, const void *elt);
724 /* Debugging */
725 static abort_t init_debug_buf(struct update *update);
726 static void clear_debug_buf(struct update *update);
727 static int __attribute__((format(printf, 2, 3)))
728 _ksdebug(struct update *update, const char *fmt, ...);
729 #define ksdebug(change, fmt, ...) \
730 _ksdebug(change->update, fmt, ## __VA_ARGS__)
732 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) && defined(CONFIG_KALLSYMS)
733 /* 75a66614db21007bcc8c37f9c5d5b922981387b9 was after 2.6.29 */
734 static int kallsyms_on_each_symbol(int (*fn)(void *, const char *,
735 struct module *, unsigned long),
736 void *data);
737 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
738 static unsigned int kallsyms_expand_symbol(unsigned int off, char *result);
739 #endif /* LINUX_VERSION_CODE */
740 static int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
741 struct module *,
742 unsigned long),
743 void *data);
744 #endif /* LINUX_VERSION_CODE && CONFIG_KALLSYMS */
746 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
747 /* c6b37801911d7f4663c99cad8aa230bc934cea82 was after 2.6.29 */
748 static struct module *find_module(const char *name);
749 static int use_module(struct module *a, struct module *b);
750 static const struct kernel_symbol *find_symbol(const char *name,
751 struct module **owner,
752 const unsigned long **crc,
753 bool gplok, bool warn);
754 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
755 struct module *owner,
756 unsigned int symnum, void *data),
757 void *data);
758 static struct module *__module_address(unsigned long addr);
759 #endif /* LINUX_VERSION_CODE */
761 /* Architecture-specific functions defined in arch/ARCH/kernel/ksplice-arch.c */
763 /* Prepare a trampoline for the given patch */
764 static abort_t prepare_trampoline(struct ksplice_mod_change *change,
765 struct ksplice_patch *p);
766 /* What address does the trampoline at addr jump to? */
767 static abort_t trampoline_target(struct ksplice_mod_change *change,
768 unsigned long addr, unsigned long *new_addr);
769 /* Hook to handle pc-relative jumps inserted by parainstructions */
770 static abort_t handle_paravirt(struct ksplice_mod_change *change,
771 unsigned long pre, unsigned long run,
772 int *matched);
773 /* Is address p on the stack of the given thread? */
774 static bool valid_stack_ptr(const struct thread_info *tinfo, const void *p);
776 #ifndef KSPLICE_STANDALONE
777 #include "ksplice-arch.c"
778 #elif defined CONFIG_X86
779 #include "x86/ksplice-arch.c"
780 #elif defined CONFIG_ARM
781 #include "arm/ksplice-arch.c"
782 #endif /* KSPLICE_STANDALONE */
784 #define clear_list(head, type, member) \
785 do { \
786 struct list_head *_pos, *_n; \
787 list_for_each_safe(_pos, _n, head) { \
788 list_del(_pos); \
789 kfree(list_entry(_pos, type, member)); \
791 } while (0)
794 * init_ksplice_mod_change() - Initializes a ksplice change
795 * @change: The change to be initialized. All of the public fields of the
796 * change and its associated data structures should be populated
797 * before this function is called. The values of the private
798 * fields will be ignored.
800 int init_ksplice_mod_change(struct ksplice_mod_change *change)
802 struct update *update;
803 struct ksplice_patch *p;
804 struct ksplice_section *s;
805 int ret = 0;
807 #ifdef KSPLICE_STANDALONE
808 if (!bootstrapped)
809 return -1;
810 #endif /* KSPLICE_STANDALONE */
812 INIT_LIST_HEAD(&change->temp_labelvals);
813 INIT_LIST_HEAD(&change->safety_records);
815 sort(change->old_code.relocs,
816 change->old_code.relocs_end - change->old_code.relocs,
817 sizeof(*change->old_code.relocs), compare_relocs, NULL);
818 sort(change->new_code.relocs,
819 change->new_code.relocs_end - change->new_code.relocs,
820 sizeof(*change->new_code.relocs), compare_relocs, NULL);
821 sort(change->old_code.sections,
822 change->old_code.sections_end - change->old_code.sections,
823 sizeof(*change->old_code.sections), compare_section_labels, NULL);
824 #ifdef KSPLICE_STANDALONE
825 sort(change->new_code.system_map,
826 change->new_code.system_map_end - change->new_code.system_map,
827 sizeof(*change->new_code.system_map), compare_system_map, NULL);
828 sort(change->old_code.system_map,
829 change->old_code.system_map_end - change->old_code.system_map,
830 sizeof(*change->old_code.system_map), compare_system_map, NULL);
831 #endif /* KSPLICE_STANDALONE */
833 for (p = change->patches; p < change->patches_end; p++)
834 p->vaddr = NULL;
835 for (s = change->old_code.sections; s < change->old_code.sections_end;
836 s++)
837 s->match_map = NULL;
838 for (p = change->patches; p < change->patches_end; p++) {
839 const struct ksplice_reloc *r = patch_reloc(change, p);
840 if (r == NULL)
841 return -ENOENT;
842 if (p->type == KSPLICE_PATCH_DATA) {
843 s = symbol_section(change, r->symbol);
844 if (s == NULL)
845 return -ENOENT;
846 /* Ksplice creates KSPLICE_PATCH_DATA patches in order
847 * to modify rodata sections that have been explicitly
848 * marked for patching using the ksplice-patch.h macro
849 * ksplice_assume_rodata. Here we modify the section
850 * flags appropriately.
852 if (s->flags & KSPLICE_SECTION_DATA)
853 s->flags = (s->flags & ~KSPLICE_SECTION_DATA) |
854 KSPLICE_SECTION_RODATA;
858 mutex_lock(&module_mutex);
859 list_for_each_entry(update, &updates, list) {
860 if (strcmp(change->kid, update->kid) == 0) {
861 if (update->stage != STAGE_PREPARING) {
862 ret = -EPERM;
863 goto out;
865 add_to_update(change, update);
866 ret = 0;
867 goto out;
870 update = init_ksplice_update(change->kid);
871 if (update == NULL) {
872 ret = -ENOMEM;
873 goto out;
875 ret = ksplice_sysfs_init(update);
876 if (ret != 0) {
877 cleanup_ksplice_update(update);
878 goto out;
880 add_to_update(change, update);
881 out:
882 mutex_unlock(&module_mutex);
883 return ret;
885 EXPORT_SYMBOL_GPL(init_ksplice_mod_change);
888 * cleanup_ksplice_mod_change() - Cleans up a change if appropriate
889 * @change: The change to be cleaned up
891 * cleanup_ksplice_mod_change is currently called twice for each
892 * Ksplice update; once when the old_code module is unloaded, and once
893 * when the new_code module is unloaded. The extra call is used to
894 * avoid leaks if you unload the old_code without applying the update.
896 void cleanup_ksplice_mod_change(struct ksplice_mod_change *change)
898 if (change->update == NULL)
899 return;
901 mutex_lock(&module_mutex);
902 if (change->update->stage == STAGE_APPLIED) {
903 /* If the change wasn't actually applied (because we
904 * only applied this update to loaded modules and this
905 * target was not loaded), then unregister the change
906 * from the list of unused changes.
908 struct ksplice_mod_change *c;
909 bool found = false;
911 list_for_each_entry(c, &change->update->unused_changes, list) {
912 if (c == change)
913 found = true;
915 if (found)
916 list_del(&change->list);
917 mutex_unlock(&module_mutex);
918 return;
920 list_del(&change->list);
921 if (change->update->stage == STAGE_PREPARING)
922 maybe_cleanup_ksplice_update(change->update);
923 change->update = NULL;
924 mutex_unlock(&module_mutex);
926 EXPORT_SYMBOL_GPL(cleanup_ksplice_mod_change);
928 static struct update *init_ksplice_update(const char *kid)
930 struct update *update;
931 update = kcalloc(1, sizeof(struct update), GFP_KERNEL);
932 if (update == NULL)
933 return NULL;
934 update->name = kasprintf(GFP_KERNEL, "ksplice_%s", kid);
935 if (update->name == NULL) {
936 kfree(update);
937 return NULL;
939 update->kid = kstrdup(kid, GFP_KERNEL);
940 if (update->kid == NULL) {
941 kfree(update->name);
942 kfree(update);
943 return NULL;
945 if (try_module_get(THIS_MODULE) != 1) {
946 kfree(update->kid);
947 kfree(update->name);
948 kfree(update);
949 return NULL;
951 INIT_LIST_HEAD(&update->changes);
952 INIT_LIST_HEAD(&update->unused_changes);
953 INIT_LIST_HEAD(&update->ksplice_module_list);
954 if (init_debug_buf(update) != OK) {
955 module_put(THIS_MODULE);
956 kfree(update->kid);
957 kfree(update->name);
958 kfree(update);
959 return NULL;
961 list_add(&update->list, &updates);
962 update->stage = STAGE_PREPARING;
963 update->abort_cause = OK;
964 update->partial = 0;
965 INIT_LIST_HEAD(&update->conflicts);
966 return update;
969 static void cleanup_ksplice_update(struct update *update)
971 list_del(&update->list);
972 cleanup_conflicts(update);
973 clear_debug_buf(update);
974 cleanup_module_list_entries(update);
975 kfree(update->kid);
976 kfree(update->name);
977 kfree(update);
978 module_put(THIS_MODULE);
981 /* Clean up the update if it no longer has any changes */
982 static void maybe_cleanup_ksplice_update(struct update *update)
984 if (list_empty(&update->changes) && list_empty(&update->unused_changes))
985 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
986 kobject_put(&update->kobj);
987 #else /* LINUX_VERSION_CODE < */
988 /* 6d06adfaf82d154023141ddc0c9de18b6a49090b was after 2.6.24 */
989 kobject_unregister(&update->kobj);
990 #endif /* LINUX_VERSION_CODE */
993 static void add_to_update(struct ksplice_mod_change *change,
994 struct update *update)
996 change->update = update;
997 list_add(&change->list, &update->unused_changes);
1000 static int ksplice_sysfs_init(struct update *update)
1002 int ret = 0;
1003 memset(&update->kobj, 0, sizeof(update->kobj));
1004 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
1005 #ifndef KSPLICE_STANDALONE
1006 ret = kobject_init_and_add(&update->kobj, &update_ktype,
1007 ksplice_kobj, "%s", update->kid);
1008 #else /* KSPLICE_STANDALONE */
1009 ret = kobject_init_and_add(&update->kobj, &update_ktype,
1010 &THIS_MODULE->mkobj.kobj, "ksplice");
1011 #endif /* KSPLICE_STANDALONE */
1012 #else /* LINUX_VERSION_CODE < */
1013 /* 6d06adfaf82d154023141ddc0c9de18b6a49090b was after 2.6.24 */
1014 ret = kobject_set_name(&update->kobj, "%s", "ksplice");
1015 if (ret != 0)
1016 return ret;
1017 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
1018 update->kobj.parent = &THIS_MODULE->mkobj.kobj;
1019 #else /* LINUX_VERSION_CODE < */
1020 /* b86ab02803095190d6b72bcc18dcf620bf378df9 was after 2.6.10 */
1021 update->kobj.parent = &THIS_MODULE->mkobj->kobj;
1022 #endif /* LINUX_VERSION_CODE */
1023 update->kobj.ktype = &update_ktype;
1024 ret = kobject_register(&update->kobj);
1025 #endif /* LINUX_VERSION_CODE */
1026 if (ret != 0)
1027 return ret;
1028 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15)
1029 kobject_uevent(&update->kobj, KOBJ_ADD);
1030 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
1031 /* 312c004d36ce6c739512bac83b452f4c20ab1f62 was after 2.6.14 */
1032 /* 12025235884570ba7f02a6f427f973ac6be7ec54 was after 2.6.9 */
1033 kobject_uevent(&update->kobj, KOBJ_ADD, NULL);
1034 #endif /* LINUX_VERSION_CODE */
1035 return 0;
1038 #ifdef KSPLICE_NEED_PARAINSTRUCTIONS
1039 EXTRACT_SYMBOL(apply_paravirt);
1040 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
1042 static abort_t apply_update(struct update *update)
1044 struct ksplice_mod_change *change, *n;
1045 abort_t ret;
1046 int retval;
1048 list_for_each_entry(change, &update->changes, list) {
1049 ret = create_module_list_entry(change, true);
1050 if (ret != OK)
1051 goto out;
1054 list_for_each_entry_safe(change, n, &update->unused_changes, list) {
1055 if (strcmp(change->target_name, "vmlinux") == 0) {
1056 change->target = NULL;
1057 } else if (change->target == NULL) {
1058 change->target = find_module(change->target_name);
1059 if (change->target == NULL ||
1060 !module_is_live(change->target)) {
1061 if (!update->partial) {
1062 ret = TARGET_NOT_LOADED;
1063 goto out;
1065 ret = create_module_list_entry(change, false);
1066 if (ret != OK)
1067 goto out;
1068 continue;
1070 retval = use_module(change->new_code_mod,
1071 change->target);
1072 if (retval != 1) {
1073 ret = UNEXPECTED;
1074 goto out;
1077 ret = create_module_list_entry(change, true);
1078 if (ret != OK)
1079 goto out;
1080 list_del(&change->list);
1081 list_add_tail(&change->list, &update->changes);
1083 #ifdef KSPLICE_NEED_PARAINSTRUCTIONS
1084 if (change->target == NULL) {
1085 apply_paravirt(change->new_code.parainstructions,
1086 change->new_code.parainstructions_end);
1087 apply_paravirt(change->old_code.parainstructions,
1088 change->old_code.parainstructions_end);
1090 #endif /* KSPLICE_NEED_PARAINSTRUCTIONS */
1093 list_for_each_entry(change, &update->changes, list) {
1094 const struct ksplice_section *sect;
1095 for (sect = change->new_code.sections;
1096 sect < change->new_code.sections_end; sect++) {
1097 struct safety_record *rec = kmalloc(sizeof(*rec),
1098 GFP_KERNEL);
1099 if (rec == NULL) {
1100 ret = OUT_OF_MEMORY;
1101 goto out;
1103 rec->addr = sect->address;
1104 rec->size = sect->size;
1105 rec->label = sect->symbol->label;
1106 list_add(&rec->list, &change->safety_records);
1110 list_for_each_entry(change, &update->changes, list) {
1111 ret = init_symbol_arrays(change);
1112 if (ret != OK) {
1113 cleanup_symbol_arrays(change);
1114 goto out;
1116 ret = prepare_change(change);
1117 cleanup_symbol_arrays(change);
1118 if (ret != OK)
1119 goto out;
1121 ret = patch_action(update, KS_APPLY);
1122 out:
1123 list_for_each_entry(change, &update->changes, list) {
1124 struct ksplice_section *s;
1125 if (update->stage == STAGE_PREPARING)
1126 clear_list(&change->safety_records,
1127 struct safety_record, list);
1128 for (s = change->old_code.sections;
1129 s < change->old_code.sections_end; s++) {
1130 if (s->match_map != NULL) {
1131 vfree(s->match_map);
1132 s->match_map = NULL;
1136 if (update->stage == STAGE_PREPARING)
1137 cleanup_module_list_entries(update);
1139 if (ret == OK)
1140 printk(KERN_INFO "ksplice: Update %s applied successfully\n",
1141 update->kid);
1142 return ret;
1145 static abort_t reverse_update(struct update *update)
1147 abort_t ret;
1148 struct ksplice_mod_change *change;
1150 clear_debug_buf(update);
1151 ret = init_debug_buf(update);
1152 if (ret != OK)
1153 return ret;
1155 _ksdebug(update, "Preparing to reverse %s\n", update->kid);
1157 ret = patch_action(update, KS_REVERSE);
1158 if (ret != OK)
1159 return ret;
1161 list_for_each_entry(change, &update->changes, list)
1162 clear_list(&change->safety_records, struct safety_record, list);
1164 printk(KERN_INFO "ksplice: Update %s reversed successfully\n",
1165 update->kid);
1166 return OK;
1169 static int compare_symbolp_names(const void *a, const void *b)
1171 const struct ksplice_symbol *const *sympa = a, *const *sympb = b;
1172 if ((*sympa)->name == NULL && (*sympb)->name == NULL)
1173 return 0;
1174 if ((*sympa)->name == NULL)
1175 return -1;
1176 if ((*sympb)->name == NULL)
1177 return 1;
1178 return strcmp((*sympa)->name, (*sympb)->name);
1181 static int compare_symbolp_labels(const void *a, const void *b)
1183 const struct ksplice_symbol *const *sympa = a, *const *sympb = b;
1184 return strcmp((*sympa)->label, (*sympb)->label);
1187 static int symbolp_bsearch_compare(const void *key, const void *elt)
1189 const char *name = key;
1190 const struct ksplice_symbol *const *symp = elt;
1191 const struct ksplice_symbol *sym = *symp;
1192 if (sym->name == NULL)
1193 return 1;
1194 return strcmp(name, sym->name);
1197 static abort_t add_matching_values(struct ksplice_lookup *lookup,
1198 const char *sym_name, unsigned long sym_val)
1200 struct ksplice_symbol **symp;
1201 abort_t ret;
1203 symp = bsearch(sym_name, lookup->arr, lookup->size,
1204 sizeof(*lookup->arr), symbolp_bsearch_compare);
1205 if (symp == NULL)
1206 return OK;
1208 while (symp > lookup->arr &&
1209 symbolp_bsearch_compare(sym_name, symp - 1) == 0)
1210 symp--;
1212 for (; symp < lookup->arr + lookup->size; symp++) {
1213 struct ksplice_symbol *sym = *symp;
1214 if (sym->name == NULL || strcmp(sym_name, sym->name) != 0)
1215 break;
1216 ret = add_candidate_val(lookup->change,
1217 sym->candidate_vals, sym_val);
1218 if (ret != OK)
1219 return ret;
1221 return OK;
1224 #ifdef CONFIG_KALLSYMS
1225 static int add_kallsyms_values(void *data, const char *name,
1226 struct module *owner, unsigned long val)
1228 struct ksplice_lookup *lookup = data;
1229 if (owner == lookup->change->new_code_mod ||
1230 !patches_module(owner, lookup->change->target))
1231 return (__force int)OK;
1232 return (__force int)add_matching_values(lookup, name, val);
1234 #endif /* CONFIG_KALLSYMS */
1236 static bool add_export_values(const struct symsearch *syms,
1237 struct module *owner,
1238 unsigned int symnum, void *data)
1240 struct ksplice_lookup *lookup = data;
1241 abort_t ret;
1243 ret = add_matching_values(lookup, syms->start[symnum].name,
1244 syms->start[symnum].value);
1245 if (ret != OK) {
1246 lookup->ret = ret;
1247 return true;
1249 return false;
1252 static void cleanup_symbol_arrays(struct ksplice_mod_change *change)
1254 struct ksplice_symbol *sym;
1255 for (sym = change->new_code.symbols; sym < change->new_code.symbols_end;
1256 sym++) {
1257 if (sym->candidate_vals != NULL) {
1258 clear_list(sym->candidate_vals, struct candidate_val,
1259 list);
1260 kfree(sym->candidate_vals);
1261 sym->candidate_vals = NULL;
1264 for (sym = change->old_code.symbols; sym < change->old_code.symbols_end;
1265 sym++) {
1266 if (sym->candidate_vals != NULL) {
1267 clear_list(sym->candidate_vals, struct candidate_val,
1268 list);
1269 kfree(sym->candidate_vals);
1270 sym->candidate_vals = NULL;
1276 * The new_code and old_code modules each have their own independent
1277 * ksplice_symbol structures. uniquify_symbols unifies these separate
1278 * pieces of kernel symbol information by replacing all references to
1279 * the old_code copy of symbols with references to the new_code copy.
1281 static abort_t uniquify_symbols(struct ksplice_mod_change *change)
1283 struct ksplice_reloc *r;
1284 struct ksplice_section *s;
1285 struct ksplice_symbol *sym, **sym_arr, **symp;
1286 size_t size = change->new_code.symbols_end - change->new_code.symbols;
1288 if (size == 0)
1289 return OK;
1291 sym_arr = vmalloc(sizeof(*sym_arr) * size);
1292 if (sym_arr == NULL)
1293 return OUT_OF_MEMORY;
1295 for (symp = sym_arr, sym = change->new_code.symbols;
1296 symp < sym_arr + size && sym < change->new_code.symbols_end;
1297 sym++, symp++)
1298 *symp = sym;
1300 sort(sym_arr, size, sizeof(*sym_arr), compare_symbolp_labels, NULL);
1302 for (r = change->old_code.relocs; r < change->old_code.relocs_end;
1303 r++) {
1304 symp = bsearch(&r->symbol, sym_arr, size, sizeof(*sym_arr),
1305 compare_symbolp_labels);
1306 if (symp != NULL) {
1307 if ((*symp)->name == NULL)
1308 (*symp)->name = r->symbol->name;
1309 r->symbol = *symp;
1313 for (s = change->old_code.sections; s < change->old_code.sections_end;
1314 s++) {
1315 symp = bsearch(&s->symbol, sym_arr, size, sizeof(*sym_arr),
1316 compare_symbolp_labels);
1317 if (symp != NULL) {
1318 if ((*symp)->name == NULL)
1319 (*symp)->name = s->symbol->name;
1320 s->symbol = *symp;
1324 vfree(sym_arr);
1325 return OK;
1329 * Initialize the ksplice_symbol structures in the given array using
1330 * the kallsyms and exported symbol tables.
1332 static abort_t init_symbol_array(struct ksplice_mod_change *change,
1333 struct ksplice_symbol *start,
1334 struct ksplice_symbol *end)
1336 struct ksplice_symbol *sym, **sym_arr, **symp;
1337 struct ksplice_lookup lookup;
1338 size_t size = end - start;
1339 abort_t ret;
1341 if (size == 0)
1342 return OK;
1344 for (sym = start; sym < end; sym++) {
1345 if (strstarts(sym->label, "__ksymtab")) {
1346 const struct kernel_symbol *ksym;
1347 const char *colon = strchr(sym->label, ':');
1348 const char *name = colon + 1;
1349 if (colon == NULL)
1350 continue;
1351 ksym = find_symbol(name, NULL, NULL, true, false);
1352 if (ksym == NULL) {
1353 ksdebug(change, "Could not find kernel_symbol "
1354 "structure for %s\n", name);
1355 continue;
1357 sym->value = (unsigned long)ksym;
1358 sym->candidate_vals = NULL;
1359 continue;
1362 sym->candidate_vals = kmalloc(sizeof(*sym->candidate_vals),
1363 GFP_KERNEL);
1364 if (sym->candidate_vals == NULL)
1365 return OUT_OF_MEMORY;
1366 INIT_LIST_HEAD(sym->candidate_vals);
1367 sym->value = 0;
1370 sym_arr = vmalloc(sizeof(*sym_arr) * size);
1371 if (sym_arr == NULL)
1372 return OUT_OF_MEMORY;
1374 for (symp = sym_arr, sym = start; symp < sym_arr + size && sym < end;
1375 sym++, symp++)
1376 *symp = sym;
1378 sort(sym_arr, size, sizeof(*sym_arr), compare_symbolp_names, NULL);
1380 lookup.change = change;
1381 lookup.arr = sym_arr;
1382 lookup.size = size;
1383 lookup.ret = OK;
1385 each_symbol(add_export_values, &lookup);
1386 ret = lookup.ret;
1387 #ifdef CONFIG_KALLSYMS
1388 if (ret == OK)
1389 ret = (__force abort_t)
1390 kallsyms_on_each_symbol(add_kallsyms_values, &lookup);
1391 #endif /* CONFIG_KALLSYMS */
1392 vfree(sym_arr);
1393 return ret;
1397 * Prepare the change's ksplice_symbol structures for run-pre matching
1399 * noinline to prevent garbage on the stack from confusing check_stack
1401 static noinline abort_t init_symbol_arrays(struct ksplice_mod_change *change)
1403 abort_t ret;
1405 ret = uniquify_symbols(change);
1406 if (ret != OK)
1407 return ret;
1409 ret = init_symbol_array(change, change->old_code.symbols,
1410 change->old_code.symbols_end);
1411 if (ret != OK)
1412 return ret;
1414 ret = init_symbol_array(change, change->new_code.symbols,
1415 change->new_code.symbols_end);
1416 if (ret != OK)
1417 return ret;
1419 return OK;
1422 /* noinline to prevent garbage on the stack from confusing check_stack */
1423 static noinline abort_t prepare_change(struct ksplice_mod_change *change)
1425 abort_t ret;
1427 ksdebug(change, "Preparing and checking %s\n", change->name);
1428 ret = match_change_sections(change, false);
1429 if (ret == NO_MATCH) {
1430 /* It is possible that by using relocations from .data sections
1431 * we can successfully run-pre match the rest of the sections.
1432 * To avoid using any symbols obtained from .data sections
1433 * (which may be unreliable) in the post code, we first prepare
1434 * the post code and then try to run-pre match the remaining
1435 * sections with the help of .data sections.
1437 ksdebug(change, "Continuing without some sections; we might "
1438 "find them later.\n");
1439 ret = finalize_change(change);
1440 if (ret != OK) {
1441 ksdebug(change, "Aborted. Unable to continue without "
1442 "the unmatched sections.\n");
1443 return ret;
1446 ksdebug(change, "run-pre: Considering .data sections to find "
1447 "the unmatched sections\n");
1448 ret = match_change_sections(change, true);
1449 if (ret != OK)
1450 return ret;
1452 ksdebug(change, "run-pre: Found all previously unmatched "
1453 "sections\n");
1454 return OK;
1455 } else if (ret != OK) {
1456 return ret;
1459 return finalize_change(change);
1463 * Finish preparing the change for insertion into the kernel.
1464 * Afterwards, the replacement code should be ready to run and the
1465 * ksplice_patches should all be ready for trampoline insertion.
1467 static abort_t finalize_change(struct ksplice_mod_change *change)
1469 abort_t ret;
1470 ret = apply_relocs(change, change->new_code.relocs,
1471 change->new_code.relocs_end);
1472 if (ret != OK)
1473 return ret;
1475 ret = finalize_patches(change);
1476 if (ret != OK)
1477 return ret;
1479 return OK;
1482 static abort_t finalize_patches(struct ksplice_mod_change *change)
1484 struct ksplice_patch *p;
1485 struct safety_record *rec;
1486 abort_t ret;
1488 for (p = change->patches; p < change->patches_end; p++) {
1489 bool found = false;
1490 list_for_each_entry(rec, &change->safety_records, list) {
1491 if (rec->addr <= p->oldaddr &&
1492 p->oldaddr < rec->addr + rec->size) {
1493 found = true;
1494 break;
1497 if (!found && p->type != KSPLICE_PATCH_EXPORT) {
1498 const struct ksplice_reloc *r = patch_reloc(change, p);
1499 if (r == NULL) {
1500 ksdebug(change, "A patch with no reloc at its "
1501 "oldaddr has no safety record\n");
1502 return NO_MATCH;
1504 ksdebug(change, "No safety record for patch with "
1505 "oldaddr %s+%lx\n", r->symbol->label,
1506 r->target_addend);
1507 return NO_MATCH;
1510 if (p->type == KSPLICE_PATCH_TEXT) {
1511 ret = prepare_trampoline(change, p);
1512 if (ret != OK)
1513 return ret;
1516 if (found && rec->addr + rec->size < p->oldaddr + p->size) {
1517 ksdebug(change, "Safety record %s is too short for "
1518 "patch\n", rec->label);
1519 return UNEXPECTED;
1522 if (p->type == KSPLICE_PATCH_TEXT) {
1523 if (p->repladdr == 0)
1524 p->repladdr = (unsigned long)ksplice_deleted;
1528 for (p = change->patches; p < change->patches_end; p++) {
1529 struct ksplice_patch *q;
1530 for (q = change->patches; q < change->patches_end; q++) {
1531 if (p != q && p->oldaddr <= q->oldaddr &&
1532 p->oldaddr + p->size > q->oldaddr) {
1533 ksdebug(change, "Overlapping oldaddrs "
1534 "for patches\n");
1535 return UNEXPECTED;
1540 return OK;
1543 /* noinline to prevent garbage on the stack from confusing check_stack */
1544 static noinline abort_t map_trampoline_pages(struct update *update)
1546 struct ksplice_mod_change *change;
1547 list_for_each_entry(change, &update->changes, list) {
1548 struct ksplice_patch *p;
1549 for (p = change->patches; p < change->patches_end; p++) {
1550 p->vaddr = map_writable((void *)p->oldaddr, p->size);
1551 if (p->vaddr == NULL) {
1552 ksdebug(change,
1553 "Unable to map oldaddr read/write\n");
1554 unmap_trampoline_pages(update);
1555 return UNEXPECTED;
1559 return OK;
1562 static void unmap_trampoline_pages(struct update *update)
1564 struct ksplice_mod_change *change;
1565 list_for_each_entry(change, &update->changes, list) {
1566 struct ksplice_patch *p;
1567 for (p = change->patches; p < change->patches_end; p++) {
1568 vunmap((void *)((unsigned long)p->vaddr & PAGE_MASK));
1569 p->vaddr = NULL;
1574 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) && defined(CONFIG_X86_64)
1575 /* e3ebadd95cb621e2c7436f3d3646447ac9d5c16d was after 2.6.21 */
1576 #define phys_base ({EXTRACT_SYMBOL(phys_base); phys_base;})
1577 #endif /* LINUX_VERSION_CODE && CONFIG_X86_64 */
1580 * map_writable creates a shadow page mapping of the range
1581 * [addr, addr + len) so that we can write to code mapped read-only.
1583 * It is similar to a generalized version of x86's text_poke. But
1584 * because one cannot use vmalloc/vfree() inside stop_machine, we use
1585 * map_writable to map the pages before stop_machine, then use the
1586 * mapping inside stop_machine, and unmap the pages afterwards.
1588 static void *map_writable(void *addr, size_t len)
1590 void *vaddr;
1591 int nr_pages = DIV_ROUND_UP(offset_in_page(addr) + len, PAGE_SIZE);
1592 struct page **pages = kmalloc(nr_pages * sizeof(*pages), GFP_KERNEL);
1593 void *page_addr = (void *)((unsigned long)addr & PAGE_MASK);
1594 int i;
1596 if (pages == NULL)
1597 return NULL;
1599 for (i = 0; i < nr_pages; i++) {
1600 if (__module_address((unsigned long)page_addr) == NULL) {
1601 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) || !defined(CONFIG_X86_64)
1602 pages[i] = virt_to_page(page_addr);
1603 #else /* LINUX_VERSION_CODE < && CONFIG_X86_64 */
1604 /* e3ebadd95cb621e2c7436f3d3646447ac9d5c16d was after 2.6.21
1605 * This works around a broken virt_to_page() from the RHEL 5 backport
1606 * of x86-64 relocatable kernel support.
1608 pages[i] =
1609 pfn_to_page(__pa_symbol(page_addr) >> PAGE_SHIFT);
1610 #endif /* LINUX_VERSION_CODE || !CONFIG_X86_64 */
1611 WARN_ON(!PageReserved(pages[i]));
1612 } else {
1613 pages[i] = vmalloc_to_page(addr);
1615 if (pages[i] == NULL) {
1616 kfree(pages);
1617 return NULL;
1619 page_addr += PAGE_SIZE;
1621 vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
1622 kfree(pages);
1623 if (vaddr == NULL)
1624 return NULL;
1625 return vaddr + offset_in_page(addr);
1628 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
1629 /* c6b37801911d7f4663c99cad8aa230bc934cea82 was after 2.6.29 */
1630 EXTRACT_SYMBOL(__module_text_address);
1631 #endif /* LINUX_VERSION_CODE */
1634 * Ksplice adds a dependency on any symbol address used to resolve
1635 * relocations in the new_code module.
1637 * Be careful to follow_trampolines so that we always depend on the
1638 * latest version of the target function, since that's the code that
1639 * will run if we call addr.
1641 static abort_t add_dependency_on_address(struct ksplice_mod_change *change,
1642 unsigned long addr)
1644 struct ksplice_mod_change *c;
1645 struct module *m =
1646 __module_text_address(follow_trampolines(change, addr));
1647 if (m == NULL)
1648 return OK;
1649 list_for_each_entry(c, &change->update->changes, list) {
1650 if (m == c->new_code_mod)
1651 return OK;
1653 if (use_module(change->new_code_mod, m) != 1)
1654 return MODULE_BUSY;
1655 return OK;
1658 static abort_t apply_relocs(struct ksplice_mod_change *change,
1659 const struct ksplice_reloc *relocs,
1660 const struct ksplice_reloc *relocs_end)
1662 const struct ksplice_reloc *r;
1663 for (r = relocs; r < relocs_end; r++) {
1664 abort_t ret = apply_reloc(change, r);
1665 if (ret != OK)
1666 return ret;
1668 return OK;
1671 static abort_t apply_reloc(struct ksplice_mod_change *change,
1672 const struct ksplice_reloc *r)
1674 switch (r->howto->type) {
1675 case KSPLICE_HOWTO_RELOC:
1676 case KSPLICE_HOWTO_RELOC_PATCH:
1677 return apply_howto_reloc(change, r);
1678 case KSPLICE_HOWTO_DATE:
1679 case KSPLICE_HOWTO_TIME:
1680 return apply_howto_date(change, r);
1681 default:
1682 ksdebug(change, "Unexpected howto type %d\n", r->howto->type);
1683 return UNEXPECTED;
1688 * Applies a relocation. Aborts if the symbol referenced in it has
1689 * not been uniquely resolved.
1691 static abort_t apply_howto_reloc(struct ksplice_mod_change *change,
1692 const struct ksplice_reloc *r)
1694 abort_t ret;
1695 int canary_ret;
1696 unsigned long sym_addr;
1697 LIST_HEAD(vals);
1699 canary_ret = contains_canary(change, r->blank_addr, r->howto);
1700 if (canary_ret < 0)
1701 return UNEXPECTED;
1702 if (canary_ret == 0) {
1703 ksdebug(change, "reloc: skipped %lx to %s+%lx (altinstr)\n",
1704 r->blank_addr, r->symbol->label, r->target_addend);
1705 return OK;
1708 #ifdef KSPLICE_STANDALONE
1709 if (!bootstrapped) {
1710 ret = add_system_map_candidates(change,
1711 change->new_code.system_map,
1712 change->new_code.system_map_end,
1713 r->symbol->label, &vals);
1714 if (ret != OK) {
1715 release_vals(&vals);
1716 return ret;
1719 #endif /* KSPLICE_STANDALONE */
1720 ret = lookup_symbol(change, r->symbol, &vals);
1721 if (ret != OK) {
1722 release_vals(&vals);
1723 return ret;
1726 * Relocations for the oldaddr fields of patches must have
1727 * been resolved via run-pre matching.
1729 if (!singular(&vals) || (r->symbol->candidate_vals != NULL &&
1730 r->howto->type == KSPLICE_HOWTO_RELOC_PATCH)) {
1731 release_vals(&vals);
1732 ksdebug(change, "Failed to find %s for reloc\n",
1733 r->symbol->label);
1734 return FAILED_TO_FIND;
1736 sym_addr = list_entry(vals.next, struct candidate_val, list)->val;
1737 release_vals(&vals);
1739 ret = write_reloc_value(change, r, r->blank_addr,
1740 r->howto->pcrel ? sym_addr - r->blank_addr :
1741 sym_addr);
1742 if (ret != OK)
1743 return ret;
1745 ksdebug(change, "reloc: %lx to %s+%lx (S=%lx ", r->blank_addr,
1746 r->symbol->label, r->target_addend, sym_addr);
1747 switch (r->howto->size) {
1748 case 1:
1749 ksdebug(change, "aft=%02x)\n", *(uint8_t *)r->blank_addr);
1750 break;
1751 case 2:
1752 ksdebug(change, "aft=%04x)\n", *(uint16_t *)r->blank_addr);
1753 break;
1754 case 4:
1755 ksdebug(change, "aft=%08x)\n", *(uint32_t *)r->blank_addr);
1756 break;
1757 #if BITS_PER_LONG >= 64
1758 case 8:
1759 ksdebug(change, "aft=%016llx)\n", *(uint64_t *)r->blank_addr);
1760 break;
1761 #endif /* BITS_PER_LONG */
1762 default:
1763 ksdebug(change, "Aborted. Invalid relocation size.\n");
1764 return UNEXPECTED;
1766 #ifdef KSPLICE_STANDALONE
1767 if (!bootstrapped)
1768 return OK;
1769 #endif /* KSPLICE_STANDALONE */
1772 * Create labelvals so that we can verify our choices in the
1773 * second round of run-pre matching that considers data sections.
1775 ret = create_labelval(change, r->symbol, sym_addr, VAL);
1776 if (ret != OK)
1777 return ret;
1779 return add_dependency_on_address(change, sym_addr);
1783 * Date relocations are created wherever __DATE__ or __TIME__ is used
1784 * in the kernel; we resolve them by simply copying in the date/time
1785 * obtained from run-pre matching the relevant compilation unit.
1787 static abort_t apply_howto_date(struct ksplice_mod_change *change,
1788 const struct ksplice_reloc *r)
1790 if (r->symbol->candidate_vals != NULL) {
1791 ksdebug(change, "Failed to find %s for date\n",
1792 r->symbol->label);
1793 return FAILED_TO_FIND;
1795 memcpy((unsigned char *)r->blank_addr,
1796 (const unsigned char *)r->symbol->value, r->howto->size);
1797 return OK;
1801 * Given a relocation and its run address, compute the address of the
1802 * symbol the relocation referenced, and store it in *valp.
1804 static abort_t read_reloc_value(struct ksplice_mod_change *change,
1805 const struct ksplice_reloc *r,
1806 unsigned long addr, unsigned long *valp)
1808 unsigned char bytes[sizeof(long)];
1809 unsigned long val;
1810 const struct ksplice_reloc_howto *howto = r->howto;
1812 if (howto->size <= 0 || howto->size > sizeof(long)) {
1813 ksdebug(change, "Aborted. Invalid relocation size.\n");
1814 return UNEXPECTED;
1817 if (probe_kernel_read(bytes, (void *)addr, howto->size) == -EFAULT)
1818 return NO_MATCH;
1820 switch (howto->size) {
1821 case 1:
1822 val = *(uint8_t *)bytes;
1823 break;
1824 case 2:
1825 val = *(uint16_t *)bytes;
1826 break;
1827 case 4:
1828 val = *(uint32_t *)bytes;
1829 break;
1830 #if BITS_PER_LONG >= 64
1831 case 8:
1832 val = *(uint64_t *)bytes;
1833 break;
1834 #endif /* BITS_PER_LONG */
1835 default:
1836 ksdebug(change, "Aborted. Invalid relocation size.\n");
1837 return UNEXPECTED;
1840 val &= howto->dst_mask;
1841 if (howto->signed_addend)
1842 val |= -(val & (howto->dst_mask & ~(howto->dst_mask >> 1)));
1843 val <<= howto->rightshift;
1844 val -= r->insn_addend + r->target_addend;
1845 *valp = val;
1846 return OK;
1850 * Given a relocation, the address of its storage unit, and the
1851 * address of the symbol the relocation references, write the
1852 * relocation's final value into the storage unit.
1854 static abort_t write_reloc_value(struct ksplice_mod_change *change,
1855 const struct ksplice_reloc *r,
1856 unsigned long addr, unsigned long sym_addr)
1858 unsigned long val = sym_addr + r->target_addend + r->insn_addend;
1859 const struct ksplice_reloc_howto *howto = r->howto;
1860 val >>= howto->rightshift;
1861 switch (howto->size) {
1862 case 1:
1863 *(uint8_t *)addr = (*(uint8_t *)addr & ~howto->dst_mask) |
1864 (val & howto->dst_mask);
1865 break;
1866 case 2:
1867 *(uint16_t *)addr = (*(uint16_t *)addr & ~howto->dst_mask) |
1868 (val & howto->dst_mask);
1869 break;
1870 case 4:
1871 *(uint32_t *)addr = (*(uint32_t *)addr & ~howto->dst_mask) |
1872 (val & howto->dst_mask);
1873 break;
1874 #if BITS_PER_LONG >= 64
1875 case 8:
1876 *(uint64_t *)addr = (*(uint64_t *)addr & ~howto->dst_mask) |
1877 (val & howto->dst_mask);
1878 break;
1879 #endif /* BITS_PER_LONG */
1880 default:
1881 ksdebug(change, "Aborted. Invalid relocation size.\n");
1882 return UNEXPECTED;
1885 if (read_reloc_value(change, r, addr, &val) != OK || val != sym_addr) {
1886 ksdebug(change, "Aborted. Relocation overflow.\n");
1887 return UNEXPECTED;
1890 return OK;
1893 static abort_t create_module_list_entry(struct ksplice_mod_change *change,
1894 bool to_be_applied)
1896 struct ksplice_module_list_entry *entry =
1897 kmalloc(sizeof(*entry), GFP_KERNEL);
1898 if (entry == NULL)
1899 return OUT_OF_MEMORY;
1900 entry->new_code_mod_name =
1901 kstrdup(change->new_code_mod->name, GFP_KERNEL);
1902 if (entry->new_code_mod_name == NULL) {
1903 kfree(entry);
1904 return OUT_OF_MEMORY;
1906 entry->target_mod_name = kstrdup(change->target_name, GFP_KERNEL);
1907 if (entry->target_mod_name == NULL) {
1908 kfree(entry->new_code_mod_name);
1909 kfree(entry);
1910 return OUT_OF_MEMORY;
1912 /* The update's kid is guaranteed to outlast the module_list_entry */
1913 entry->kid = change->update->kid;
1914 entry->applied = to_be_applied;
1915 list_add(&entry->update_list, &change->update->ksplice_module_list);
1916 return OK;
1919 static void cleanup_module_list_entries(struct update *update)
1921 struct ksplice_module_list_entry *entry;
1922 list_for_each_entry(entry, &update->ksplice_module_list, update_list) {
1923 kfree(entry->target_mod_name);
1924 kfree(entry->new_code_mod_name);
1926 clear_list(&update->ksplice_module_list,
1927 struct ksplice_module_list_entry, update_list);
1930 /* Replacement address used for functions deleted by the patch */
1931 static void __attribute__((noreturn)) ksplice_deleted(void)
1933 printk(KERN_CRIT "Called a kernel function deleted by Ksplice!\n");
1934 BUG();
1935 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
1936 /* 91768d6c2bad0d2766a166f13f2f57e197de3458 was after 2.6.19 */
1937 for (;;);
1938 #endif
1941 /* Floodfill to run-pre match the sections within a change. */
1942 static abort_t match_change_sections(struct ksplice_mod_change *change,
1943 bool consider_data_sections)
1945 struct ksplice_section *sect;
1946 abort_t ret;
1947 int remaining = 0;
1948 bool progress;
1950 for (sect = change->old_code.sections;
1951 sect < change->old_code.sections_end; sect++) {
1952 if ((sect->flags & KSPLICE_SECTION_DATA) == 0 &&
1953 (sect->flags & KSPLICE_SECTION_STRING) == 0 &&
1954 (sect->flags & KSPLICE_SECTION_MATCHED) == 0)
1955 remaining++;
1958 while (remaining > 0) {
1959 progress = false;
1960 for (sect = change->old_code.sections;
1961 sect < change->old_code.sections_end; sect++) {
1962 if ((sect->flags & KSPLICE_SECTION_MATCHED) != 0)
1963 continue;
1964 if ((!consider_data_sections &&
1965 (sect->flags & KSPLICE_SECTION_DATA) != 0) ||
1966 (sect->flags & KSPLICE_SECTION_STRING) != 0)
1967 continue;
1968 ret = find_section(change, sect);
1969 if (ret == OK) {
1970 sect->flags |= KSPLICE_SECTION_MATCHED;
1971 if ((sect->flags & KSPLICE_SECTION_DATA) == 0)
1972 remaining--;
1973 progress = true;
1974 } else if (ret != NO_MATCH) {
1975 return ret;
1979 if (progress)
1980 continue;
1982 for (sect = change->old_code.sections;
1983 sect < change->old_code.sections_end; sect++) {
1984 if ((sect->flags & KSPLICE_SECTION_MATCHED) != 0 ||
1985 (sect->flags & KSPLICE_SECTION_STRING) != 0)
1986 continue;
1987 ksdebug(change, "run-pre: could not match %s "
1988 "section %s\n",
1989 (sect->flags & KSPLICE_SECTION_DATA) != 0 ?
1990 "data" :
1991 (sect->flags & KSPLICE_SECTION_RODATA) != 0 ?
1992 "rodata" : "text", sect->symbol->label);
1994 ksdebug(change, "Aborted. run-pre: could not match some "
1995 "sections.\n");
1996 return NO_MATCH;
1998 return OK;
2002 * Search for the section in the running kernel. Returns OK if and
2003 * only if it finds precisely one address in the kernel matching the
2004 * section.
2006 static abort_t find_section(struct ksplice_mod_change *change,
2007 struct ksplice_section *sect)
2009 int i;
2010 abort_t ret;
2011 unsigned long run_addr;
2012 LIST_HEAD(vals);
2013 struct candidate_val *v, *n;
2015 #ifdef KSPLICE_STANDALONE
2016 ret = add_system_map_candidates(change, change->old_code.system_map,
2017 change->old_code.system_map_end,
2018 sect->symbol->label, &vals);
2019 if (ret != OK) {
2020 release_vals(&vals);
2021 return ret;
2023 #endif /* KSPLICE_STANDALONE */
2024 ret = lookup_symbol(change, sect->symbol, &vals);
2025 if (ret != OK) {
2026 release_vals(&vals);
2027 return ret;
2030 ksdebug(change, "run-pre: starting sect search for %s\n",
2031 sect->symbol->label);
2033 list_for_each_entry_safe(v, n, &vals, list) {
2034 run_addr = v->val;
2036 yield();
2037 ret = try_addr(change, sect, run_addr, NULL, RUN_PRE_INITIAL);
2038 if (ret == NO_MATCH) {
2039 list_del(&v->list);
2040 kfree(v);
2041 } else if (ret != OK) {
2042 release_vals(&vals);
2043 return ret;
2047 #if defined(KSPLICE_STANDALONE) && !defined(CONFIG_KALLSYMS)
2048 if (list_empty(&vals) && (sect->flags & KSPLICE_SECTION_DATA) == 0) {
2049 ret = brute_search_all(change, sect, &vals);
2050 if (ret != OK) {
2051 release_vals(&vals);
2052 return ret;
2055 * Make sure run-pre matching output is displayed if
2056 * brute_search succeeds.
2058 if (singular(&vals)) {
2059 run_addr = list_entry(vals.next, struct candidate_val,
2060 list)->val;
2061 ret = try_addr(change, sect, run_addr, NULL,
2062 RUN_PRE_INITIAL);
2063 if (ret != OK) {
2064 ksdebug(change, "run-pre: Debug run failed for "
2065 "sect %s:\n", sect->symbol->label);
2066 release_vals(&vals);
2067 return ret;
2071 #endif /* KSPLICE_STANDALONE && !CONFIG_KALLSYMS */
2073 if (singular(&vals)) {
2074 LIST_HEAD(safety_records);
2075 run_addr = list_entry(vals.next, struct candidate_val,
2076 list)->val;
2077 ret = try_addr(change, sect, run_addr, &safety_records,
2078 RUN_PRE_FINAL);
2079 release_vals(&vals);
2080 if (ret != OK) {
2081 clear_list(&safety_records, struct safety_record, list);
2082 ksdebug(change, "run-pre: Final run failed for sect "
2083 "%s:\n", sect->symbol->label);
2084 } else {
2085 list_splice(&safety_records, &change->safety_records);
2087 return ret;
2088 } else if (!list_empty(&vals)) {
2089 struct candidate_val *val;
2090 ksdebug(change, "run-pre: multiple candidates for sect %s:\n",
2091 sect->symbol->label);
2092 i = 0;
2093 list_for_each_entry(val, &vals, list) {
2094 i++;
2095 ksdebug(change, "%lx\n", val->val);
2096 if (i > 5) {
2097 ksdebug(change, "...\n");
2098 break;
2101 release_vals(&vals);
2102 return NO_MATCH;
2104 release_vals(&vals);
2105 return NO_MATCH;
2109 * try_addr is the the interface to run-pre matching. Its primary
2110 * purpose is to manage debugging information for run-pre matching;
2111 * all the hard work is in run_pre_cmp.
2113 static abort_t try_addr(struct ksplice_mod_change *change,
2114 struct ksplice_section *sect,
2115 unsigned long run_addr,
2116 struct list_head *safety_records,
2117 enum run_pre_mode mode)
2119 abort_t ret;
2120 const struct module *run_module = __module_address(run_addr);
2122 if (run_module == change->new_code_mod) {
2123 ksdebug(change, "run-pre: unexpected address %lx in new_code "
2124 "module %s for sect %s\n", run_addr, run_module->name,
2125 sect->symbol->label);
2126 return UNEXPECTED;
2128 if (!patches_module(run_module, change->target)) {
2129 ksdebug(change, "run-pre: ignoring address %lx in other module "
2130 "%s for sect %s\n", run_addr, run_module == NULL ?
2131 "vmlinux" : run_module->name, sect->symbol->label);
2132 return NO_MATCH;
2135 ret = create_labelval(change, sect->symbol, run_addr, TEMP);
2136 if (ret != OK)
2137 return ret;
2139 #ifdef CONFIG_FUNCTION_DATA_SECTIONS
2140 ret = run_pre_cmp(change, sect, run_addr, safety_records, mode);
2141 #else /* !CONFIG_FUNCTION_DATA_SECTIONS */
2142 if ((sect->flags & KSPLICE_SECTION_TEXT) != 0)
2143 ret = arch_run_pre_cmp(change, sect, run_addr, safety_records,
2144 mode);
2145 else
2146 ret = run_pre_cmp(change, sect, run_addr, safety_records, mode);
2147 #endif /* CONFIG_FUNCTION_DATA_SECTIONS */
2148 if (ret == NO_MATCH && mode != RUN_PRE_FINAL) {
2149 set_temp_labelvals(change, NOVAL);
2150 ksdebug(change, "run-pre: %s sect %s does not match (r_a=%lx "
2151 "p_a=%lx s=%lx)\n",
2152 (sect->flags & KSPLICE_SECTION_RODATA) != 0 ? "rodata" :
2153 (sect->flags & KSPLICE_SECTION_DATA) != 0 ? "data" :
2154 "text", sect->symbol->label, run_addr, sect->address,
2155 sect->size);
2156 ksdebug(change, "run-pre: ");
2157 if (change->update->debug >= 1) {
2158 #ifdef CONFIG_FUNCTION_DATA_SECTIONS
2159 ret = run_pre_cmp(change, sect, run_addr,
2160 safety_records, RUN_PRE_DEBUG);
2161 #else /* !CONFIG_FUNCTION_DATA_SECTIONS */
2162 if ((sect->flags & KSPLICE_SECTION_TEXT) != 0)
2163 ret = arch_run_pre_cmp(change, sect, run_addr,
2164 safety_records,
2165 RUN_PRE_DEBUG);
2166 else
2167 ret = run_pre_cmp(change, sect, run_addr,
2168 safety_records,
2169 RUN_PRE_DEBUG);
2170 #endif /* CONFIG_FUNCTION_DATA_SECTIONS */
2171 set_temp_labelvals(change, NOVAL);
2173 ksdebug(change, "\n");
2174 return ret;
2175 } else if (ret != OK) {
2176 set_temp_labelvals(change, NOVAL);
2177 return ret;
2180 if (mode != RUN_PRE_FINAL) {
2181 set_temp_labelvals(change, NOVAL);
2182 ksdebug(change, "run-pre: candidate for sect %s=%lx\n",
2183 sect->symbol->label, run_addr);
2184 return OK;
2187 set_temp_labelvals(change, VAL);
2188 ksdebug(change, "run-pre: found sect %s=%lx\n", sect->symbol->label,
2189 run_addr);
2190 return OK;
2194 * run_pre_cmp is the primary run-pre matching function; it determines
2195 * whether the given ksplice_section matches the code or data in the
2196 * running kernel starting at run_addr.
2198 * If run_pre_mode is RUN_PRE_FINAL, a safety record for the matched
2199 * section is created.
2201 * The run_pre_mode is also used to determine what debugging
2202 * information to display.
2204 static abort_t run_pre_cmp(struct ksplice_mod_change *change,
2205 const struct ksplice_section *sect,
2206 unsigned long run_addr,
2207 struct list_head *safety_records,
2208 enum run_pre_mode mode)
2210 int matched = 0;
2211 abort_t ret;
2212 const struct ksplice_reloc *r, *finger;
2213 const unsigned char *pre, *run, *pre_start, *run_start;
2214 unsigned char runval;
2216 pre_start = (const unsigned char *)sect->address;
2217 run_start = (const unsigned char *)run_addr;
2219 finger = init_reloc_search(change, sect);
2221 pre = pre_start;
2222 run = run_start;
2223 while (pre < pre_start + sect->size) {
2224 unsigned long offset = pre - pre_start;
2225 ret = lookup_reloc(change, &finger, (unsigned long)pre, &r);
2226 if (ret == OK) {
2227 ret = handle_reloc(change, sect, r, (unsigned long)run,
2228 mode);
2229 if (ret != OK) {
2230 if (mode == RUN_PRE_INITIAL)
2231 ksdebug(change, "reloc in sect does "
2232 "not match after %lx/%lx "
2233 "bytes\n", offset, sect->size);
2234 return ret;
2236 if (mode == RUN_PRE_DEBUG)
2237 print_bytes(change, run, r->howto->size, pre,
2238 r->howto->size);
2239 pre += r->howto->size;
2240 run += r->howto->size;
2241 finger++;
2242 continue;
2243 } else if (ret != NO_MATCH) {
2244 return ret;
2247 if ((sect->flags & KSPLICE_SECTION_TEXT) != 0) {
2248 ret = handle_paravirt(change, (unsigned long)pre,
2249 (unsigned long)run, &matched);
2250 if (ret != OK)
2251 return ret;
2252 if (matched != 0) {
2253 if (mode == RUN_PRE_DEBUG)
2254 print_bytes(change, run, matched, pre,
2255 matched);
2256 pre += matched;
2257 run += matched;
2258 continue;
2262 if (probe_kernel_read(&runval, (void *)run, 1) == -EFAULT) {
2263 if (mode == RUN_PRE_INITIAL)
2264 ksdebug(change, "sect unmapped after %lx/%lx "
2265 "bytes\n", offset, sect->size);
2266 return NO_MATCH;
2269 if (runval != *pre &&
2270 (sect->flags & KSPLICE_SECTION_DATA) == 0) {
2271 if (mode == RUN_PRE_INITIAL)
2272 ksdebug(change, "sect does not match after "
2273 "%lx/%lx bytes\n", offset, sect->size);
2274 if (mode == RUN_PRE_DEBUG) {
2275 print_bytes(change, run, 1, pre, 1);
2276 ksdebug(change, "[p_o=%lx] ! ", offset);
2277 print_bytes(change, run + 1, 2, pre + 1, 2);
2279 return NO_MATCH;
2281 if (mode == RUN_PRE_DEBUG)
2282 print_bytes(change, run, 1, pre, 1);
2283 pre++;
2284 run++;
2286 return create_safety_record(change, sect, safety_records, run_addr,
2287 run - run_start);
2290 static void print_bytes(struct ksplice_mod_change *change,
2291 const unsigned char *run, int runc,
2292 const unsigned char *pre, int prec)
2294 int o;
2295 int matched = min(runc, prec);
2296 for (o = 0; o < matched; o++) {
2297 if (run[o] == pre[o])
2298 ksdebug(change, "%02x ", run[o]);
2299 else
2300 ksdebug(change, "%02x/%02x ", run[o], pre[o]);
2302 for (o = matched; o < runc; o++)
2303 ksdebug(change, "%02x/ ", run[o]);
2304 for (o = matched; o < prec; o++)
2305 ksdebug(change, "/%02x ", pre[o]);
2308 #if defined(KSPLICE_STANDALONE) && !defined(CONFIG_KALLSYMS)
2309 static abort_t brute_search(struct ksplice_mod_change *change,
2310 struct ksplice_section *sect,
2311 const void *start, unsigned long len,
2312 struct list_head *vals)
2314 unsigned long addr;
2315 char run, pre;
2316 abort_t ret;
2318 for (addr = (unsigned long)start; addr < (unsigned long)start + len;
2319 addr++) {
2320 if (addr % 100000 == 0)
2321 yield();
2323 if (probe_kernel_read(&run, (void *)addr, 1) == -EFAULT)
2324 return OK;
2326 pre = *(const unsigned char *)(sect->address);
2328 if (run != pre)
2329 continue;
2331 ret = try_addr(change, sect, addr, NULL, RUN_PRE_INITIAL);
2332 if (ret == OK) {
2333 ret = add_candidate_val(change, vals, addr);
2334 if (ret != OK)
2335 return ret;
2336 } else if (ret != NO_MATCH) {
2337 return ret;
2341 return OK;
2344 extern struct list_head modules;
2345 EXTRACT_SYMBOL(modules);
2346 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
2347 /* 3abf024d2abb79614d8c4cb25a70d5596f77d0ad was after 2.6.24 */
2348 EXTRACT_SYMBOL(init_mm);
2349 #endif /* LINUX_VERSION_CODE */
2351 static abort_t brute_search_all(struct ksplice_mod_change *change,
2352 struct ksplice_section *sect,
2353 struct list_head *vals)
2355 struct module *m;
2356 abort_t ret = OK;
2357 int saved_debug;
2359 ksdebug(change, "brute_search: searching for %s\n",
2360 sect->symbol->label);
2361 saved_debug = change->update->debug;
2362 change->update->debug = 0;
2364 list_for_each_entry(m, &modules, list) {
2365 if (!patches_module(m, change->target) ||
2366 m == change->new_code_mod)
2367 continue;
2368 ret = brute_search(change, sect, m->module_core, m->core_size,
2369 vals);
2370 if (ret != OK)
2371 goto out;
2372 ret = brute_search(change, sect, m->module_init, m->init_size,
2373 vals);
2374 if (ret != OK)
2375 goto out;
2378 ret = brute_search(change, sect, (const void *)init_mm.start_code,
2379 init_mm.end_code - init_mm.start_code, vals);
2381 out:
2382 change->update->debug = saved_debug;
2383 return ret;
2385 #endif /* KSPLICE_STANDALONE && !CONFIG_KALLSYMS */
2387 struct range {
2388 unsigned long address;
2389 unsigned long size;
2392 static int reloc_bsearch_compare(const void *key, const void *elt)
2394 const struct range *range = key;
2395 const struct ksplice_reloc *r = elt;
2396 if (range->address + range->size <= r->blank_addr)
2397 return -1;
2398 if (range->address > r->blank_addr)
2399 return 1;
2400 return 0;
2403 static const struct ksplice_reloc *find_reloc(const struct ksplice_reloc *start,
2404 const struct ksplice_reloc *end,
2405 unsigned long address,
2406 unsigned long size)
2408 const struct ksplice_reloc *r;
2409 struct range range = { address, size };
2410 r = bsearch((void *)&range, start, end - start, sizeof(*r),
2411 reloc_bsearch_compare);
2412 if (r == NULL)
2413 return NULL;
2414 while (r > start && (r - 1)->blank_addr >= address)
2415 r--;
2416 return r;
2419 static const struct ksplice_reloc *
2420 init_reloc_search(struct ksplice_mod_change *change,
2421 const struct ksplice_section *sect)
2423 const struct ksplice_reloc *r;
2424 r = find_reloc(change->old_code.relocs, change->old_code.relocs_end,
2425 sect->address, sect->size);
2426 if (r == NULL)
2427 return change->old_code.relocs_end;
2428 return r;
2432 * lookup_reloc implements an amortized O(1) lookup for the next
2433 * old_code relocation. It must be called with a strictly increasing
2434 * sequence of addresses.
2436 * The fingerp is private data for lookup_reloc, and needs to have
2437 * been initialized as a pointer to the result of find_reloc (or
2438 * init_reloc_search).
2440 static abort_t lookup_reloc(struct ksplice_mod_change *change,
2441 const struct ksplice_reloc **fingerp,
2442 unsigned long addr,
2443 const struct ksplice_reloc **relocp)
2445 const struct ksplice_reloc *r = *fingerp;
2446 int canary_ret;
2448 while (r < change->old_code.relocs_end &&
2449 addr >= r->blank_addr + r->howto->size &&
2450 !(addr == r->blank_addr && r->howto->size == 0))
2451 r++;
2452 *fingerp = r;
2453 if (r == change->old_code.relocs_end)
2454 return NO_MATCH;
2455 if (addr < r->blank_addr)
2456 return NO_MATCH;
2457 *relocp = r;
2458 if (r->howto->type != KSPLICE_HOWTO_RELOC)
2459 return OK;
2461 canary_ret = contains_canary(change, r->blank_addr, r->howto);
2462 if (canary_ret < 0)
2463 return UNEXPECTED;
2464 if (canary_ret == 0) {
2465 ksdebug(change, "run-pre: reloc skipped at p_a=%lx to %s+%lx "
2466 "(altinstr)\n", r->blank_addr, r->symbol->label,
2467 r->target_addend);
2468 return NO_MATCH;
2470 if (addr != r->blank_addr) {
2471 ksdebug(change, "Invalid nonzero relocation offset\n");
2472 return UNEXPECTED;
2474 return OK;
2477 static abort_t handle_howto_symbol(struct ksplice_mod_change *change,
2478 const struct ksplice_reloc *r,
2479 unsigned long run_addr,
2480 enum run_pre_mode mode)
2482 if (mode == RUN_PRE_INITIAL)
2483 ksdebug(change, "run-pre: symbol %s at %lx\n", r->symbol->label,
2484 run_addr);
2485 return create_labelval(change, r->symbol, run_addr, TEMP);
2488 static abort_t handle_reloc(struct ksplice_mod_change *change,
2489 const struct ksplice_section *sect,
2490 const struct ksplice_reloc *r,
2491 unsigned long run_addr, enum run_pre_mode mode)
2493 switch (r->howto->type) {
2494 case KSPLICE_HOWTO_RELOC:
2495 return handle_howto_reloc(change, sect, r, run_addr, mode);
2496 case KSPLICE_HOWTO_DATE:
2497 case KSPLICE_HOWTO_TIME:
2498 return handle_howto_date(change, sect, r, run_addr, mode);
2499 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
2500 #ifdef CONFIG_BUG
2501 case KSPLICE_HOWTO_BUG:
2502 return handle_bug(change, r, run_addr);
2503 #endif /* CONFIG_BUG */
2504 #else /* LINUX_VERSION_CODE < */
2505 /* 7664c5a1da4711bb6383117f51b94c8dc8f3f1cd was after 2.6.19 */
2506 #endif /* LINUX_VERSION_CODE */
2507 case KSPLICE_HOWTO_EXTABLE:
2508 return handle_extable(change, r, run_addr);
2509 case KSPLICE_HOWTO_SYMBOL:
2510 return handle_howto_symbol(change, r, run_addr, mode);
2511 default:
2512 ksdebug(change, "Unexpected howto type %d\n", r->howto->type);
2513 return UNEXPECTED;
2518 * For date/time relocations, we check that the sequence of bytes
2519 * matches the format of a date or time.
2521 static abort_t handle_howto_date(struct ksplice_mod_change *change,
2522 const struct ksplice_section *sect,
2523 const struct ksplice_reloc *r,
2524 unsigned long run_addr, enum run_pre_mode mode)
2526 abort_t ret;
2527 char *buf = kmalloc(r->howto->size, GFP_KERNEL);
2529 if (buf == NULL)
2530 return OUT_OF_MEMORY;
2531 if (probe_kernel_read(buf, (void *)run_addr, r->howto->size) == -EFAULT) {
2532 ret = NO_MATCH;
2533 goto out;
2536 switch (r->howto->type) {
2537 case KSPLICE_HOWTO_TIME:
2538 if (isdigit(buf[0]) && isdigit(buf[1]) && buf[2] == ':' &&
2539 isdigit(buf[3]) && isdigit(buf[4]) && buf[5] == ':' &&
2540 isdigit(buf[6]) && isdigit(buf[7]))
2541 ret = OK;
2542 else
2543 ret = NO_MATCH;
2544 break;
2545 case KSPLICE_HOWTO_DATE:
2546 if (isalpha(buf[0]) && isalpha(buf[1]) && isalpha(buf[2]) &&
2547 buf[3] == ' ' && (buf[4] == ' ' || isdigit(buf[4])) &&
2548 isdigit(buf[5]) && buf[6] == ' ' && isdigit(buf[7]) &&
2549 isdigit(buf[8]) && isdigit(buf[9]) && isdigit(buf[10]))
2550 ret = OK;
2551 else
2552 ret = NO_MATCH;
2553 break;
2554 default:
2555 ret = UNEXPECTED;
2557 if (ret == NO_MATCH && mode == RUN_PRE_INITIAL)
2558 ksdebug(change, "%s string: \"%.*s\" does not match format\n",
2559 r->howto->type == KSPLICE_HOWTO_DATE ? "date" : "time",
2560 r->howto->size, buf);
2562 if (ret != OK)
2563 goto out;
2564 ret = create_labelval(change, r->symbol, run_addr, TEMP);
2565 out:
2566 kfree(buf);
2567 return ret;
2571 * Extract the value of a symbol used in a relocation in the pre code
2572 * during run-pre matching, giving an error if it conflicts with a
2573 * previously found value of that symbol
2575 static abort_t handle_howto_reloc(struct ksplice_mod_change *change,
2576 const struct ksplice_section *sect,
2577 const struct ksplice_reloc *r,
2578 unsigned long run_addr,
2579 enum run_pre_mode mode)
2581 struct ksplice_section *sym_sect = symbol_section(change, r->symbol);
2582 unsigned long offset = r->target_addend;
2583 unsigned long val;
2584 abort_t ret;
2586 ret = read_reloc_value(change, r, run_addr, &val);
2587 if (ret != OK)
2588 return ret;
2589 if (r->howto->pcrel)
2590 val += run_addr;
2592 #ifndef CONFIG_FUNCTION_DATA_SECTIONS
2593 if (sym_sect == NULL || sym_sect->match_map == NULL || offset == 0) {
2595 } else if (offset < 0 || offset >= sym_sect->size) {
2596 ksdebug(change, "Out of range relocation: %s+%lx -> %s+%lx",
2597 sect->symbol->label, r->blank_addr - sect->address,
2598 r->symbol->label, offset);
2599 return NO_MATCH;
2600 } else if (sect == sym_sect && sect->match_map[offset] == NULL) {
2601 sym_sect->match_map[offset] =
2602 (const unsigned char *)r->symbol->value + offset;
2603 } else if (sect == sym_sect && (unsigned long)sect->match_map[offset] ==
2604 r->symbol->value + offset) {
2606 } else if (sect == sym_sect) {
2607 ksdebug(change, "Relocations to nonmatching locations within "
2608 "section %s: %lx does not match %lx\n",
2609 sect->symbol->label, offset,
2610 (unsigned long)sect->match_map[offset] -
2611 r->symbol->value);
2612 return NO_MATCH;
2613 } else if ((sym_sect->flags & KSPLICE_SECTION_MATCHED) == 0) {
2614 if (mode == RUN_PRE_INITIAL)
2615 ksdebug(change, "Delaying matching of %s due to reloc "
2616 "from to unmatching section: %s+%lx\n",
2617 sect->symbol->label, r->symbol->label, offset);
2618 return NO_MATCH;
2619 } else if (sym_sect->match_map[offset] == NULL) {
2620 if (mode == RUN_PRE_INITIAL)
2621 ksdebug(change, "Relocation not to instruction "
2622 "boundary: %s+%lx -> %s+%lx",
2623 sect->symbol->label, r->blank_addr -
2624 sect->address, r->symbol->label, offset);
2625 return NO_MATCH;
2626 } else if ((unsigned long)sym_sect->match_map[offset] !=
2627 r->symbol->value + offset) {
2628 if (mode == RUN_PRE_INITIAL)
2629 ksdebug(change, "Match map shift %s+%lx: %lx != %lx\n",
2630 r->symbol->label, offset,
2631 r->symbol->value + offset,
2632 (unsigned long)sym_sect->match_map[offset]);
2633 val += r->symbol->value + offset -
2634 (unsigned long)sym_sect->match_map[offset];
2636 #endif /* !CONFIG_FUNCTION_DATA_SECTIONS */
2638 if (mode == RUN_PRE_INITIAL)
2639 ksdebug(change, "run-pre: reloc at r_a=%lx p_a=%lx to %s+%lx: "
2640 "found %s = %lx\n", run_addr, r->blank_addr,
2641 r->symbol->label, offset, r->symbol->label, val);
2643 if (contains_canary(change, run_addr, r->howto) != 0) {
2644 ksdebug(change, "Aborted. Unexpected canary in run code at %lx"
2645 "\n", run_addr);
2646 return UNEXPECTED;
2649 if ((sect->flags & KSPLICE_SECTION_DATA) != 0 &&
2650 sect->symbol == r->symbol)
2651 return OK;
2652 ret = create_labelval(change, r->symbol, val, TEMP);
2653 if (ret == NO_MATCH && mode == RUN_PRE_INITIAL)
2654 ksdebug(change, "run-pre: reloc at r_a=%lx p_a=%lx: labelval "
2655 "%s = %lx does not match expected %lx\n", run_addr,
2656 r->blank_addr, r->symbol->label, r->symbol->value, val);
2658 if (ret != OK)
2659 return ret;
2660 if (sym_sect != NULL && (sym_sect->flags & KSPLICE_SECTION_MATCHED) == 0
2661 && (sym_sect->flags & KSPLICE_SECTION_STRING) != 0) {
2662 if (mode == RUN_PRE_INITIAL)
2663 ksdebug(change, "Recursively comparing string section "
2664 "%s\n", sym_sect->symbol->label);
2665 else if (mode == RUN_PRE_DEBUG)
2666 ksdebug(change, "[str start] ");
2667 ret = run_pre_cmp(change, sym_sect, val, NULL, mode);
2668 if (mode == RUN_PRE_DEBUG)
2669 ksdebug(change, "[str end] ");
2670 if (ret == OK && mode == RUN_PRE_INITIAL)
2671 ksdebug(change, "Successfully matched string section %s"
2672 "\n", sym_sect->symbol->label);
2673 else if (mode == RUN_PRE_INITIAL)
2674 ksdebug(change, "Failed to match string section %s\n",
2675 sym_sect->symbol->label);
2677 return ret;
2680 #ifdef CONFIG_GENERIC_BUG
2681 #ifdef KSPLICE_NO_KERNEL_SUPPORT
2682 EXTRACT_SYMBOL(find_bug);
2683 #endif /* KSPLICE_NO_KERNEL_SUPPORT */
2684 static abort_t handle_bug(struct ksplice_mod_change *change,
2685 const struct ksplice_reloc *r, unsigned long run_addr)
2687 const struct bug_entry *run_bug = find_bug(run_addr);
2688 struct ksplice_section *bug_sect = symbol_section(change, r->symbol);
2689 if (run_bug == NULL)
2690 return NO_MATCH;
2691 if (bug_sect == NULL)
2692 return UNEXPECTED;
2693 return create_labelval(change, bug_sect->symbol, (unsigned long)run_bug,
2694 TEMP);
2696 #endif /* CONFIG_GENERIC_BUG */
2698 #ifdef KSPLICE_NO_KERNEL_SUPPORT
2699 EXTRACT_SYMBOL(search_exception_tables);
2700 #endif /* KSPLICE_NO_KERNEL_SUPPORT */
2702 static abort_t handle_extable(struct ksplice_mod_change *change,
2703 const struct ksplice_reloc *r,
2704 unsigned long run_addr)
2706 const struct exception_table_entry *run_ent =
2707 search_exception_tables(run_addr);
2708 struct ksplice_section *ex_sect = symbol_section(change, r->symbol);
2709 if (run_ent == NULL)
2710 return NO_MATCH;
2711 if (ex_sect == NULL)
2712 return UNEXPECTED;
2713 return create_labelval(change, ex_sect->symbol, (unsigned long)run_ent,
2714 TEMP);
2717 static int symbol_section_bsearch_compare(const void *a, const void *b)
2719 const struct ksplice_symbol *sym = a;
2720 const struct ksplice_section *sect = b;
2721 return strcmp(sym->label, sect->symbol->label);
2724 static int compare_section_labels(const void *va, const void *vb)
2726 const struct ksplice_section *a = va, *b = vb;
2727 return strcmp(a->symbol->label, b->symbol->label);
2730 static struct ksplice_section *symbol_section(struct ksplice_mod_change *change,
2731 const struct ksplice_symbol *sym)
2733 return bsearch(sym, change->old_code.sections,
2734 change->old_code.sections_end -
2735 change->old_code.sections,
2736 sizeof(struct ksplice_section),
2737 symbol_section_bsearch_compare);
2740 /* Find the relocation for the oldaddr of a ksplice_patch */
2741 static const struct ksplice_reloc *
2742 patch_reloc(struct ksplice_mod_change *change,
2743 const struct ksplice_patch *p)
2745 unsigned long addr = (unsigned long)&p->oldaddr;
2746 const struct ksplice_reloc *r =
2747 find_reloc(change->new_code.relocs, change->new_code.relocs_end,
2748 addr, sizeof(addr));
2749 if (r == NULL || r->blank_addr < addr ||
2750 r->blank_addr >= addr + sizeof(addr))
2751 return NULL;
2752 return r;
2756 * Populates vals with the possible values for ksym from the various
2757 * sources Ksplice uses to resolve symbols
2759 static abort_t lookup_symbol(struct ksplice_mod_change *change,
2760 const struct ksplice_symbol *ksym,
2761 struct list_head *vals)
2763 abort_t ret;
2765 #ifdef KSPLICE_STANDALONE
2766 if (!bootstrapped)
2767 return OK;
2768 #endif /* KSPLICE_STANDALONE */
2770 if (ksym->candidate_vals == NULL) {
2771 release_vals(vals);
2772 ksdebug(change, "using detected sym %s=%lx\n", ksym->label,
2773 ksym->value);
2774 return add_candidate_val(change, vals, ksym->value);
2777 #ifdef CONFIG_MODULE_UNLOAD
2778 if (strcmp(ksym->label, "cleanup_module") == 0 && change->target != NULL
2779 && change->target->exit != NULL) {
2780 ret = add_candidate_val(change, vals,
2781 (unsigned long)change->target->exit);
2782 if (ret != OK)
2783 return ret;
2785 #endif
2787 if (ksym->name != NULL) {
2788 struct candidate_val *val;
2789 list_for_each_entry(val, ksym->candidate_vals, list) {
2790 ret = add_candidate_val(change, vals, val->val);
2791 if (ret != OK)
2792 return ret;
2795 ret = new_export_lookup(change, ksym->name, vals);
2796 if (ret != OK)
2797 return ret;
2800 return OK;
2803 #ifdef KSPLICE_STANDALONE
2804 static abort_t
2805 add_system_map_candidates(struct ksplice_mod_change *change,
2806 const struct ksplice_system_map *start,
2807 const struct ksplice_system_map *end,
2808 const char *label, struct list_head *vals)
2810 abort_t ret;
2811 long off;
2812 int i;
2813 const struct ksplice_system_map *smap;
2815 /* Some Fedora kernel releases have System.map files whose symbol
2816 * addresses disagree with the running kernel by a constant address
2817 * offset because of the CONFIG_PHYSICAL_START and CONFIG_PHYSICAL_ALIGN
2818 * values used to compile these kernels. This constant address offset
2819 * is always a multiple of 0x100000.
2821 * If we observe an offset that is NOT a multiple of 0x100000, then the
2822 * user provided us with an incorrect System.map file, and we should
2823 * abort.
2824 * If we observe an offset that is a multiple of 0x100000, then we can
2825 * adjust the System.map address values accordingly and proceed.
2827 off = (unsigned long)printk - change->map_printk;
2828 if (off & 0xfffff) {
2829 ksdebug(change,
2830 "Aborted. System.map does not match kernel.\n");
2831 return BAD_SYSTEM_MAP;
2834 smap = bsearch(label, start, end - start, sizeof(*smap),
2835 system_map_bsearch_compare);
2836 if (smap == NULL)
2837 return OK;
2839 for (i = 0; i < smap->nr_candidates; i++) {
2840 ret = add_candidate_val(change, vals,
2841 smap->candidates[i] + off);
2842 if (ret != OK)
2843 return ret;
2845 return OK;
2848 static int system_map_bsearch_compare(const void *key, const void *elt)
2850 const struct ksplice_system_map *map = elt;
2851 const char *label = key;
2852 return strcmp(label, map->label);
2854 #endif /* !KSPLICE_STANDALONE */
2857 * An update could one module to export a symbol and at the same time
2858 * change another module to use that symbol. This violates the normal
2859 * situation where the changes can be handled independently.
2861 * new_export_lookup obtains symbol values from the changes to the
2862 * exported symbol table made by other changes.
2864 static abort_t new_export_lookup(struct ksplice_mod_change *ichange,
2865 const char *name, struct list_head *vals)
2867 struct ksplice_mod_change *change;
2868 struct ksplice_patch *p;
2869 list_for_each_entry(change, &ichange->update->changes, list) {
2870 for (p = change->patches; p < change->patches_end; p++) {
2871 const struct kernel_symbol *sym;
2872 const struct ksplice_reloc *r;
2873 if (p->type != KSPLICE_PATCH_EXPORT ||
2874 strcmp(name, *(const char **)p->contents) != 0)
2875 continue;
2877 /* Check that the p->oldaddr reloc has been resolved. */
2878 r = patch_reloc(change, p);
2879 if (r == NULL ||
2880 contains_canary(change, r->blank_addr,
2881 r->howto) != 0)
2882 continue;
2883 sym = (const struct kernel_symbol *)r->symbol->value;
2886 * Check that the sym->value reloc has been resolved,
2887 * if there is a Ksplice relocation there.
2889 r = find_reloc(change->new_code.relocs,
2890 change->new_code.relocs_end,
2891 (unsigned long)&sym->value,
2892 sizeof(&sym->value));
2893 if (r != NULL &&
2894 r->blank_addr == (unsigned long)&sym->value &&
2895 contains_canary(change, r->blank_addr,
2896 r->howto) != 0)
2897 continue;
2898 return add_candidate_val(ichange, vals, sym->value);
2901 return OK;
2904 #ifdef KSPLICE_STANDALONE
2905 EXTRACT_SYMBOL(bust_spinlocks);
2906 #endif /* KSPLICE_STANDALONE */
2909 * When patch_action is called, the update should be fully prepared.
2910 * patch_action will try to actually insert or remove trampolines for
2911 * the update.
2913 static abort_t patch_action(struct update *update, enum ksplice_action action)
2915 static int (*const __patch_actions[KS_ACTIONS])(void *) = {
2916 [KS_APPLY] = __apply_patches,
2917 [KS_REVERSE] = __reverse_patches,
2919 int i;
2920 abort_t ret;
2921 struct ksplice_mod_change *change;
2923 ret = map_trampoline_pages(update);
2924 if (ret != OK)
2925 return ret;
2927 list_for_each_entry(change, &update->changes, list) {
2928 const typeof(int (*)(void)) *f;
2929 for (f = change->hooks[action].pre;
2930 f < change->hooks[action].pre_end; f++) {
2931 if ((*f)() != 0) {
2932 ret = CALL_FAILED;
2933 goto out;
2938 for (i = 0; i < 5; i++) {
2939 cleanup_conflicts(update);
2940 #ifdef KSPLICE_STANDALONE
2941 bust_spinlocks(1);
2942 #endif /* KSPLICE_STANDALONE */
2943 ret = (__force abort_t)stop_machine(__patch_actions[action],
2944 update, NULL);
2945 #ifdef KSPLICE_STANDALONE
2946 bust_spinlocks(0);
2947 #endif /* KSPLICE_STANDALONE */
2948 if (ret != CODE_BUSY)
2949 break;
2950 set_current_state(TASK_INTERRUPTIBLE);
2951 schedule_timeout(msecs_to_jiffies(1000));
2953 out:
2954 unmap_trampoline_pages(update);
2956 if (ret == CODE_BUSY) {
2957 print_conflicts(update);
2958 _ksdebug(update, "Aborted %s. stack check: to-be-%s "
2959 "code is busy.\n", update->kid,
2960 action == KS_APPLY ? "replaced" : "reversed");
2961 } else if (ret == ALREADY_REVERSED) {
2962 _ksdebug(update, "Aborted %s. Ksplice update %s is already "
2963 "reversed.\n", update->kid, update->kid);
2964 } else if (ret == MODULE_BUSY) {
2965 _ksdebug(update, "Update %s is in use by another module\n",
2966 update->kid);
2969 if (ret != OK) {
2970 list_for_each_entry(change, &update->changes, list) {
2971 const typeof(void (*)(void)) *f;
2972 for (f = change->hooks[action].fail;
2973 f < change->hooks[action].fail_end; f++)
2974 (*f)();
2977 return ret;
2980 list_for_each_entry(change, &update->changes, list) {
2981 const typeof(void (*)(void)) *f;
2982 for (f = change->hooks[action].post;
2983 f < change->hooks[action].post_end; f++)
2984 (*f)();
2987 _ksdebug(update, "Atomic patch %s for %s complete\n",
2988 action == KS_APPLY ? "insertion" : "removal", update->kid);
2989 return OK;
2992 /* Atomically insert the update; run from within stop_machine */
2993 static int __apply_patches(void *updateptr)
2995 struct update *update = updateptr;
2996 struct ksplice_mod_change *change;
2997 struct ksplice_module_list_entry *entry;
2998 struct ksplice_patch *p;
2999 abort_t ret;
3001 if (update->stage == STAGE_APPLIED)
3002 return (__force int)OK;
3004 if (update->stage != STAGE_PREPARING)
3005 return (__force int)UNEXPECTED;
3007 ret = check_each_task(update);
3008 if (ret != OK)
3009 return (__force int)ret;
3011 list_for_each_entry(change, &update->changes, list) {
3012 if (try_module_get(change->new_code_mod) != 1) {
3013 struct ksplice_mod_change *change1;
3014 list_for_each_entry(change1, &update->changes, list) {
3015 if (change1 == change)
3016 break;
3017 module_put(change1->new_code_mod);
3019 module_put(THIS_MODULE);
3020 return (__force int)UNEXPECTED;
3024 list_for_each_entry(change, &update->changes, list) {
3025 const typeof(int (*)(void)) *f;
3026 for (f = change->hooks[KS_APPLY].check;
3027 f < change->hooks[KS_APPLY].check_end; f++) {
3028 if ((*f)() != 0)
3029 return (__force int)CALL_FAILED;
3033 /* Commit point: the update application will succeed. */
3035 update->stage = STAGE_APPLIED;
3036 #ifdef TAINT_KSPLICE
3037 add_taint(TAINT_KSPLICE);
3038 #endif
3040 list_for_each_entry(entry, &update->ksplice_module_list, update_list)
3041 list_add(&entry->list, &ksplice_modules);
3043 list_for_each_entry(change, &update->changes, list) {
3044 for (p = change->patches; p < change->patches_end; p++)
3045 insert_trampoline(p);
3048 list_for_each_entry(change, &update->changes, list) {
3049 const typeof(void (*)(void)) *f;
3050 for (f = change->hooks[KS_APPLY].intra;
3051 f < change->hooks[KS_APPLY].intra_end; f++)
3052 (*f)();
3055 return (__force int)OK;
3058 /* Atomically remove the update; run from within stop_machine */
3059 static int __reverse_patches(void *updateptr)
3061 struct update *update = updateptr;
3062 struct ksplice_mod_change *change;
3063 struct ksplice_module_list_entry *entry;
3064 const struct ksplice_patch *p;
3065 abort_t ret;
3067 if (update->stage != STAGE_APPLIED)
3068 return (__force int)OK;
3070 #ifdef CONFIG_MODULE_UNLOAD
3071 list_for_each_entry(change, &update->changes, list) {
3072 if (module_refcount(change->new_code_mod) != 1)
3073 return (__force int)MODULE_BUSY;
3075 #endif /* CONFIG_MODULE_UNLOAD */
3077 list_for_each_entry(entry, &update->ksplice_module_list, update_list) {
3078 if (!entry->applied &&
3079 find_module(entry->target_mod_name) != NULL)
3080 return COLD_UPDATE_LOADED;
3083 ret = check_each_task(update);
3084 if (ret != OK)
3085 return (__force int)ret;
3087 list_for_each_entry(change, &update->changes, list) {
3088 for (p = change->patches; p < change->patches_end; p++) {
3089 ret = verify_trampoline(change, p);
3090 if (ret != OK)
3091 return (__force int)ret;
3095 list_for_each_entry(change, &update->changes, list) {
3096 const typeof(int (*)(void)) *f;
3097 for (f = change->hooks[KS_REVERSE].check;
3098 f < change->hooks[KS_REVERSE].check_end; f++) {
3099 if ((*f)() != 0)
3100 return (__force int)CALL_FAILED;
3104 /* Commit point: the update reversal will succeed. */
3106 update->stage = STAGE_REVERSED;
3108 list_for_each_entry(change, &update->changes, list)
3109 module_put(change->new_code_mod);
3111 list_for_each_entry(entry, &update->ksplice_module_list, update_list)
3112 list_del(&entry->list);
3114 list_for_each_entry(change, &update->changes, list) {
3115 const typeof(void (*)(void)) *f;
3116 for (f = change->hooks[KS_REVERSE].intra;
3117 f < change->hooks[KS_REVERSE].intra_end; f++)
3118 (*f)();
3121 list_for_each_entry(change, &update->changes, list) {
3122 for (p = change->patches; p < change->patches_end; p++)
3123 remove_trampoline(p);
3126 return (__force int)OK;
3129 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
3130 /* 5d4564e68210e4b1edb3f013bc3e59982bb35737 was after 2.6.10 */
3131 EXTRACT_SYMBOL(tasklist_lock);
3132 #endif /* LINUX_VERSION_CODE */
3135 * Check whether any thread's instruction pointer or any address of
3136 * its stack is contained in one of the safety_records associated with
3137 * the update.
3139 * check_each_task must be called from inside stop_machine, because it
3140 * does not take tasklist_lock (which cannot be held by anyone else
3141 * during stop_machine).
3143 static abort_t check_each_task(struct update *update)
3145 const struct task_struct *g, *p;
3146 abort_t status = OK, ret;
3147 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
3148 /* 5d4564e68210e4b1edb3f013bc3e59982bb35737 was after 2.6.10 */
3149 read_lock(&tasklist_lock);
3150 #endif /* LINUX_VERSION_CODE */
3151 do_each_thread(g, p) {
3152 /* do_each_thread is a double loop! */
3153 ret = check_task(update, p, false);
3154 if (ret != OK) {
3155 check_task(update, p, true);
3156 status = ret;
3158 if (ret != OK && ret != CODE_BUSY)
3159 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
3160 /* 5d4564e68210e4b1edb3f013bc3e59982bb35737 was after 2.6.10 */
3161 goto out;
3162 #else /* LINUX_VERSION_CODE < */
3163 return ret;
3164 #endif /* LINUX_VERSION_CODE */
3165 } while_each_thread(g, p);
3166 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
3167 /* 5d4564e68210e4b1edb3f013bc3e59982bb35737 was after 2.6.10 */
3168 out:
3169 read_unlock(&tasklist_lock);
3170 #endif /* LINUX_VERSION_CODE */
3171 return status;
3174 #ifdef KSPLICE_NO_KERNEL_SUPPORT
3175 EXTRACT_SYMBOL(task_curr);
3176 #endif /* KSPLICE_NO_KERNEL_SUPPORT */
3178 static abort_t check_task(struct update *update,
3179 const struct task_struct *t, bool rerun)
3181 abort_t status, ret;
3182 struct conflict *conf = NULL;
3184 if (rerun) {
3185 conf = kmalloc(sizeof(*conf), GFP_ATOMIC);
3186 if (conf == NULL)
3187 return OUT_OF_MEMORY;
3188 conf->process_name = kstrdup(t->comm, GFP_ATOMIC);
3189 if (conf->process_name == NULL) {
3190 kfree(conf);
3191 return OUT_OF_MEMORY;
3193 conf->pid = t->pid;
3194 INIT_LIST_HEAD(&conf->stack);
3195 list_add(&conf->list, &update->conflicts);
3198 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)
3199 if (t->state == TASK_DEAD)
3200 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
3201 /* c394cc9fbb367f87faa2228ec2eabacd2d4701c6 was after 2.6.18 */
3202 if ((t->flags & PF_DEAD) != 0)
3203 #endif
3204 return OK;
3206 status = check_address(update, conf, KSPLICE_IP(t));
3207 if (t == current) {
3208 ret = check_stack(update, conf, task_thread_info(t),
3209 (unsigned long *)__builtin_frame_address(0));
3210 if (status == OK)
3211 status = ret;
3212 } else if (!task_curr(t)) {
3213 ret = check_stack(update, conf, task_thread_info(t),
3214 (unsigned long *)KSPLICE_SP(t));
3215 if (status == OK)
3216 status = ret;
3217 } else if (!is_stop_machine(t)) {
3218 status = UNEXPECTED_RUNNING_TASK;
3220 return status;
3223 static abort_t check_stack(struct update *update, struct conflict *conf,
3224 const struct thread_info *tinfo,
3225 const unsigned long *stack)
3227 abort_t status = OK, ret;
3228 unsigned long addr;
3230 while (valid_stack_ptr(tinfo, stack)) {
3231 addr = *stack++;
3232 ret = check_address(update, conf, addr);
3233 if (ret != OK)
3234 status = ret;
3236 return status;
3239 static abort_t check_address(struct update *update,
3240 struct conflict *conf, unsigned long addr)
3242 abort_t status = OK, ret;
3243 const struct safety_record *rec;
3244 struct ksplice_mod_change *change;
3245 struct conflict_addr *ca = NULL;
3247 if (conf != NULL) {
3248 ca = kmalloc(sizeof(*ca), GFP_ATOMIC);
3249 if (ca == NULL)
3250 return OUT_OF_MEMORY;
3251 ca->addr = addr;
3252 ca->has_conflict = false;
3253 ca->label = NULL;
3254 list_add(&ca->list, &conf->stack);
3257 list_for_each_entry(change, &update->changes, list) {
3258 unsigned long tramp_addr = follow_trampolines(change, addr);
3259 list_for_each_entry(rec, &change->safety_records, list) {
3260 ret = check_record(ca, rec, tramp_addr);
3261 if (ret != OK)
3262 status = ret;
3265 return status;
3268 static abort_t check_record(struct conflict_addr *ca,
3269 const struct safety_record *rec, unsigned long addr)
3271 if (addr >= rec->addr && addr < rec->addr + rec->size) {
3272 if (ca != NULL) {
3273 ca->label = rec->label;
3274 ca->has_conflict = true;
3276 return CODE_BUSY;
3278 return OK;
3281 /* Is the task one of the stop_machine tasks? */
3282 static bool is_stop_machine(const struct task_struct *t)
3284 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27)
3285 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
3286 const char *kstop_prefix = "kstop/";
3287 #else /* LINUX_VERSION_CODE < */
3288 /* c9583e55fa2b08a230c549bd1e3c0bde6c50d9cc was after 2.6.27 */
3289 const char *kstop_prefix = "kstop";
3290 #endif /* LINUX_VERSION_CODE */
3291 const char *num;
3292 if (!strstarts(t->comm, kstop_prefix))
3293 return false;
3294 num = t->comm + strlen(kstop_prefix);
3295 return num[strspn(num, "0123456789")] == '\0';
3296 #else /* LINUX_VERSION_CODE < */
3297 /* ffdb5976c47609c862917d4c186ecbb5706d2dda was after 2.6.26 */
3298 return strcmp(t->comm, "kstopmachine") == 0;
3299 #endif /* LINUX_VERSION_CODE */
3302 static void cleanup_conflicts(struct update *update)
3304 struct conflict *conf;
3305 list_for_each_entry(conf, &update->conflicts, list) {
3306 clear_list(&conf->stack, struct conflict_addr, list);
3307 kfree(conf->process_name);
3309 clear_list(&update->conflicts, struct conflict, list);
3312 static void print_conflicts(struct update *update)
3314 const struct conflict *conf;
3315 const struct conflict_addr *ca;
3316 list_for_each_entry(conf, &update->conflicts, list) {
3317 _ksdebug(update, "stack check: pid %d (%s):", conf->pid,
3318 conf->process_name);
3319 list_for_each_entry(ca, &conf->stack, list) {
3320 _ksdebug(update, " %lx", ca->addr);
3321 if (ca->has_conflict)
3322 _ksdebug(update, " [<-CONFLICT]");
3324 _ksdebug(update, "\n");
3328 static void insert_trampoline(struct ksplice_patch *p)
3330 mm_segment_t old_fs = get_fs();
3331 set_fs(KERNEL_DS);
3332 memcpy(p->saved, p->vaddr, p->size);
3333 memcpy(p->vaddr, p->contents, p->size);
3334 flush_icache_range(p->oldaddr, p->oldaddr + p->size);
3335 set_fs(old_fs);
3338 static abort_t verify_trampoline(struct ksplice_mod_change *change,
3339 const struct ksplice_patch *p)
3341 if (memcmp(p->vaddr, p->contents, p->size) != 0) {
3342 ksdebug(change, "Aborted. Trampoline at %lx has been "
3343 "overwritten.\n", p->oldaddr);
3344 return CODE_BUSY;
3346 return OK;
3349 static void remove_trampoline(const struct ksplice_patch *p)
3351 mm_segment_t old_fs = get_fs();
3352 set_fs(KERNEL_DS);
3353 memcpy(p->vaddr, p->saved, p->size);
3354 flush_icache_range(p->oldaddr, p->oldaddr + p->size);
3355 set_fs(old_fs);
3358 /* Returns NO_MATCH if there's already a labelval with a different value */
3359 static abort_t create_labelval(struct ksplice_mod_change *change,
3360 struct ksplice_symbol *ksym,
3361 unsigned long val, int status)
3363 val = follow_trampolines(change, val);
3364 if (ksym->candidate_vals == NULL)
3365 return ksym->value == val ? OK : NO_MATCH;
3367 ksym->value = val;
3368 if (status == TEMP) {
3369 struct labelval *lv = kmalloc(sizeof(*lv), GFP_KERNEL);
3370 if (lv == NULL)
3371 return OUT_OF_MEMORY;
3372 lv->symbol = ksym;
3373 lv->saved_vals = ksym->candidate_vals;
3374 list_add(&lv->list, &change->temp_labelvals);
3376 ksym->candidate_vals = NULL;
3377 return OK;
3381 * Creates a new safety_record for a old_code section based on its
3382 * ksplice_section and run-pre matching information.
3384 static abort_t create_safety_record(struct ksplice_mod_change *change,
3385 const struct ksplice_section *sect,
3386 struct list_head *record_list,
3387 unsigned long run_addr,
3388 unsigned long run_size)
3390 struct safety_record *rec;
3391 struct ksplice_patch *p;
3393 if (record_list == NULL)
3394 return OK;
3396 for (p = change->patches; p < change->patches_end; p++) {
3397 const struct ksplice_reloc *r = patch_reloc(change, p);
3398 if (strcmp(sect->symbol->label, r->symbol->label) == 0)
3399 break;
3401 if (p >= change->patches_end)
3402 return OK;
3404 rec = kmalloc(sizeof(*rec), GFP_KERNEL);
3405 if (rec == NULL)
3406 return OUT_OF_MEMORY;
3408 * The old_code might be unloaded when checking reversing
3409 * patches, so we need to kstrdup the label here.
3411 rec->label = kstrdup(sect->symbol->label, GFP_KERNEL);
3412 if (rec->label == NULL) {
3413 kfree(rec);
3414 return OUT_OF_MEMORY;
3416 rec->addr = run_addr;
3417 rec->size = run_size;
3419 list_add(&rec->list, record_list);
3420 return OK;
3423 static abort_t add_candidate_val(struct ksplice_mod_change *change,
3424 struct list_head *vals, unsigned long val)
3426 struct candidate_val *tmp, *new;
3429 * Careful: follow trampolines before comparing values so that we do
3430 * not mistake the obsolete function for another copy of the function.
3432 val = follow_trampolines(change, val);
3434 list_for_each_entry(tmp, vals, list) {
3435 if (tmp->val == val)
3436 return OK;
3438 new = kmalloc(sizeof(*new), GFP_KERNEL);
3439 if (new == NULL)
3440 return OUT_OF_MEMORY;
3441 new->val = val;
3442 list_add(&new->list, vals);
3443 return OK;
3446 static void release_vals(struct list_head *vals)
3448 clear_list(vals, struct candidate_val, list);
3452 * The temp_labelvals list is used to cache those temporary labelvals
3453 * that have been created to cross-check the symbol values obtained
3454 * from different relocations within a single section being matched.
3456 * If status is VAL, commit the temp_labelvals as final values.
3458 * If status is NOVAL, restore the list of possible values to the
3459 * ksplice_symbol, so that it no longer has a known value.
3461 static void set_temp_labelvals(struct ksplice_mod_change *change, int status)
3463 struct labelval *lv, *n;
3464 list_for_each_entry_safe(lv, n, &change->temp_labelvals, list) {
3465 if (status == NOVAL) {
3466 lv->symbol->candidate_vals = lv->saved_vals;
3467 } else {
3468 release_vals(lv->saved_vals);
3469 kfree(lv->saved_vals);
3471 list_del(&lv->list);
3472 kfree(lv);
3476 /* Is there a Ksplice canary with given howto at blank_addr? */
3477 static int contains_canary(struct ksplice_mod_change *change,
3478 unsigned long blank_addr,
3479 const struct ksplice_reloc_howto *howto)
3481 switch (howto->size) {
3482 case 1:
3483 return (*(uint8_t *)blank_addr & howto->dst_mask) ==
3484 (KSPLICE_CANARY & howto->dst_mask);
3485 case 2:
3486 return (*(uint16_t *)blank_addr & howto->dst_mask) ==
3487 (KSPLICE_CANARY & howto->dst_mask);
3488 case 4:
3489 return (*(uint32_t *)blank_addr & howto->dst_mask) ==
3490 (KSPLICE_CANARY & howto->dst_mask);
3491 #if BITS_PER_LONG >= 64
3492 case 8:
3493 return (*(uint64_t *)blank_addr & howto->dst_mask) ==
3494 (KSPLICE_CANARY & howto->dst_mask);
3495 #endif /* BITS_PER_LONG */
3496 default:
3497 ksdebug(change, "Aborted. Invalid relocation size.\n");
3498 return -1;
3502 #ifdef KSPLICE_NO_KERNEL_SUPPORT
3503 EXTRACT_SYMBOL(__kernel_text_address);
3504 #endif /* KSPLICE_NO_KERNEL_SUPPORT */
3507 * Compute the address of the code you would actually run if you were
3508 * to call the function at addr (i.e., follow the sequence of jumps
3509 * starting at addr)
3511 static unsigned long follow_trampolines(struct ksplice_mod_change *change,
3512 unsigned long addr)
3514 unsigned long new_addr;
3515 struct module *m;
3517 while (1) {
3518 #ifdef KSPLICE_STANDALONE
3519 if (!bootstrapped)
3520 return addr;
3521 #endif /* KSPLICE_STANDALONE */
3522 if (!__kernel_text_address(addr) ||
3523 trampoline_target(change, addr, &new_addr) != OK)
3524 return addr;
3525 m = __module_text_address(new_addr);
3526 if (m == NULL || m == change->target ||
3527 !strstarts(m->name, "ksplice"))
3528 return addr;
3529 addr = new_addr;
3533 /* Does module a patch module b? */
3534 static bool patches_module(const struct module *a, const struct module *b)
3536 #ifdef KSPLICE_NO_KERNEL_SUPPORT
3537 const char *name;
3538 const char *modname = b == NULL ? "vmlinux" : b->name;
3539 if (a == b)
3540 return true;
3541 if (a == NULL || !strstarts(a->name, "ksplice_"))
3542 return false;
3543 name = a->name + strlen("ksplice_");
3544 name += strcspn(name, "_");
3545 if (name[0] != '_')
3546 return false;
3547 name++;
3548 return strstarts(name, modname) &&
3549 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
3550 strcmp(name + strlen(modname), "_new") == 0;
3551 #else /* LINUX_VERSION_CODE < */
3552 /* 0e8a2de644a93132594f66222a9d48405674eacd was after 2.6.9 */
3553 (strcmp(name + strlen(modname), "_n") == 0
3554 || strcmp(name + strlen(modname), "_new") == 0);
3555 #endif /* LINUX_VERSION_CODE */
3556 #else /* !KSPLICE_NO_KERNEL_SUPPORT */
3557 struct ksplice_module_list_entry *entry;
3558 if (a == b)
3559 return true;
3560 list_for_each_entry(entry, &ksplice_modules, list) {
3561 if (strcmp(entry->target_mod_name, b->name) == 0 &&
3562 strcmp(entry->new_code_mod_name, a->name) == 0)
3563 return true;
3565 return false;
3566 #endif /* KSPLICE_NO_KERNEL_SUPPORT */
3569 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
3570 /* 66f92cf9d415e96a5bdd6c64de8dd8418595d2fc was after 2.6.29 */
3571 static bool strstarts(const char *str, const char *prefix)
3573 return strncmp(str, prefix, strlen(prefix)) == 0;
3575 #endif /* LINUX_VERSION_CODE */
3577 static bool singular(struct list_head *list)
3579 return !list_empty(list) && list->next->next == list;
3582 static void *bsearch(const void *key, const void *base, size_t n,
3583 size_t size, int (*cmp)(const void *key, const void *elt))
3585 int start = 0, end = n - 1, mid, result;
3586 if (n == 0)
3587 return NULL;
3588 while (start <= end) {
3589 mid = (start + end) / 2;
3590 result = cmp(key, base + mid * size);
3591 if (result < 0)
3592 end = mid - 1;
3593 else if (result > 0)
3594 start = mid + 1;
3595 else
3596 return (void *)base + mid * size;
3598 return NULL;
3601 static int compare_relocs(const void *a, const void *b)
3603 const struct ksplice_reloc *ra = a, *rb = b;
3604 if (ra->blank_addr > rb->blank_addr)
3605 return 1;
3606 else if (ra->blank_addr < rb->blank_addr)
3607 return -1;
3608 else
3609 return ra->howto->size - rb->howto->size;
3612 #ifdef KSPLICE_STANDALONE
3613 static int compare_system_map(const void *a, const void *b)
3615 const struct ksplice_system_map *sa = a, *sb = b;
3616 return strcmp(sa->label, sb->label);
3618 #endif /* KSPLICE_STANDALONE */
3620 #ifdef CONFIG_DEBUG_FS
3621 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
3622 /* Old kernels don't have debugfs_create_blob */
3623 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
3624 size_t count, loff_t *ppos)
3626 struct debugfs_blob_wrapper *blob = file->private_data;
3627 return simple_read_from_buffer(user_buf, count, ppos, blob->data,
3628 blob->size);
3631 static int blob_open(struct inode *inode, struct file *file)
3633 if (inode->i_private)
3634 file->private_data = inode->i_private;
3635 return 0;
3638 static struct file_operations fops_blob = {
3639 .read = read_file_blob,
3640 .open = blob_open,
3643 static struct dentry *debugfs_create_blob(const char *name, mode_t mode,
3644 struct dentry *parent,
3645 struct debugfs_blob_wrapper *blob)
3647 return debugfs_create_file(name, mode, parent, blob, &fops_blob);
3649 #endif /* LINUX_VERSION_CODE */
3651 static abort_t init_debug_buf(struct update *update)
3653 update->debug_blob.size = 0;
3654 update->debug_blob.data = NULL;
3655 update->debugfs_dentry =
3656 debugfs_create_blob(update->name, S_IFREG | S_IRUSR, NULL,
3657 &update->debug_blob);
3658 if (update->debugfs_dentry == NULL)
3659 return OUT_OF_MEMORY;
3660 return OK;
3663 static void clear_debug_buf(struct update *update)
3665 if (update->debugfs_dentry == NULL)
3666 return;
3667 debugfs_remove(update->debugfs_dentry);
3668 update->debugfs_dentry = NULL;
3669 update->debug_blob.size = 0;
3670 vfree(update->debug_blob.data);
3671 update->debug_blob.data = NULL;
3674 static int _ksdebug(struct update *update, const char *fmt, ...)
3676 va_list args;
3677 unsigned long size, old_size, new_size;
3679 if (update->debug == 0)
3680 return 0;
3682 /* size includes the trailing '\0' */
3683 va_start(args, fmt);
3684 size = 1 + vsnprintf(update->debug_blob.data, 0, fmt, args);
3685 va_end(args);
3686 old_size = update->debug_blob.size == 0 ? 0 :
3687 max(PAGE_SIZE, roundup_pow_of_two(update->debug_blob.size));
3688 new_size = update->debug_blob.size + size == 0 ? 0 :
3689 max(PAGE_SIZE, roundup_pow_of_two(update->debug_blob.size + size));
3690 if (new_size > old_size) {
3691 char *buf = vmalloc(new_size);
3692 if (buf == NULL)
3693 return -ENOMEM;
3694 memcpy(buf, update->debug_blob.data, update->debug_blob.size);
3695 vfree(update->debug_blob.data);
3696 update->debug_blob.data = buf;
3698 va_start(args, fmt);
3699 update->debug_blob.size += vsnprintf(update->debug_blob.data +
3700 update->debug_blob.size,
3701 size, fmt, args);
3702 va_end(args);
3703 return 0;
3705 #else /* CONFIG_DEBUG_FS */
3706 static abort_t init_debug_buf(struct update *update)
3708 return OK;
3711 static void clear_debug_buf(struct update *update)
3713 return;
3716 static int _ksdebug(struct update *update, const char *fmt, ...)
3718 va_list args;
3720 if (update->debug == 0)
3721 return 0;
3723 if (!update->debug_continue_line)
3724 printk(KERN_DEBUG "ksplice: ");
3726 va_start(args, fmt);
3727 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,9)
3728 vprintk(fmt, args);
3729 #else /* LINUX_VERSION_CODE < */
3730 /* 683b229286b429244f35726b3c18caec429233bd was after 2.6.8 */
3732 char *buf = kvasprintf(GFP_KERNEL, fmt, args);
3733 printk("%s", buf);
3734 kfree(buf);
3736 #endif /* LINUX_VERSION_CODE */
3737 va_end(args);
3739 update->debug_continue_line =
3740 fmt[0] == '\0' || fmt[strlen(fmt) - 1] != '\n';
3741 return 0;
3743 #endif /* CONFIG_DEBUG_FS */
3745 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) && defined(CONFIG_KALLSYMS)
3746 /* 75a66614db21007bcc8c37f9c5d5b922981387b9 was after 2.6.29 */
3747 extern unsigned long kallsyms_addresses[];
3748 EXTRACT_SYMBOL(kallsyms_addresses);
3749 extern unsigned long kallsyms_num_syms;
3750 EXTRACT_SYMBOL(kallsyms_num_syms);
3751 extern u8 kallsyms_names[];
3752 EXTRACT_SYMBOL(kallsyms_names);
3754 static int kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3755 struct module *, unsigned long),
3756 void *data)
3758 char namebuf[KSYM_NAME_LEN];
3759 unsigned long i;
3760 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
3761 unsigned int off;
3762 #endif /* LINUX_VERSION_CODE */
3763 int ret;
3765 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
3766 for (i = 0, off = 0; i < kallsyms_num_syms; i++) {
3767 off = kallsyms_expand_symbol(off, namebuf);
3768 ret = fn(data, namebuf, NULL, kallsyms_addresses[i]);
3769 if (ret != 0)
3770 return ret;
3772 #else /* LINUX_VERSION_CODE < */
3773 /* 5648d78927ca65e74aadc88a2b1d6431e55e78ec was after 2.6.9 */
3774 char *knames;
3776 for (i = 0, knames = kallsyms_names; i < kallsyms_num_syms; i++) {
3777 unsigned prefix = *knames++;
3779 strlcpy(namebuf + prefix, knames, KSYM_NAME_LEN - prefix);
3781 ret = fn(data, namebuf, NULL, kallsyms_addresses[i]);
3782 if (ret != OK)
3783 return ret;
3785 knames += strlen(knames) + 1;
3787 #endif /* LINUX_VERSION_CODE */
3788 return module_kallsyms_on_each_symbol(fn, data);
3791 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
3792 extern u8 kallsyms_token_table[];
3793 EXTRACT_SYMBOL(kallsyms_token_table);
3794 extern u16 kallsyms_token_index[];
3795 EXTRACT_SYMBOL(kallsyms_token_index);
3797 static unsigned int kallsyms_expand_symbol(unsigned int off, char *result)
3799 long len, skipped_first = 0;
3800 const u8 *tptr, *data;
3802 data = &kallsyms_names[off];
3803 len = *data;
3804 data++;
3806 off += len + 1;
3808 while (len) {
3809 tptr = &kallsyms_token_table[kallsyms_token_index[*data]];
3810 data++;
3811 len--;
3813 while (*tptr) {
3814 if (skipped_first) {
3815 *result = *tptr;
3816 result++;
3817 } else
3818 skipped_first = 1;
3819 tptr++;
3823 *result = '\0';
3825 return off;
3827 #else /* LINUX_VERSION_CODE < */
3828 /* 5648d78927ca65e74aadc88a2b1d6431e55e78ec was after 2.6.9 */
3829 #endif /* LINUX_VERSION_CODE */
3831 static int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
3832 struct module *,
3833 unsigned long),
3834 void *data)
3836 struct module *mod;
3837 unsigned int i;
3838 int ret;
3840 list_for_each_entry(mod, &modules, list) {
3841 for (i = 0; i < mod->num_symtab; i++) {
3842 ret = fn(data, mod->strtab + mod->symtab[i].st_name,
3843 mod, mod->symtab[i].st_value);
3844 if (ret != 0)
3845 return ret;
3848 return 0;
3850 #endif /* LINUX_VERSION_CODE && CONFIG_KALLSYMS */
3852 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
3853 /* c6b37801911d7f4663c99cad8aa230bc934cea82 was after 2.6.29 */
3854 static struct module *find_module(const char *name)
3856 struct module *mod;
3858 list_for_each_entry(mod, &modules, list) {
3859 if (strcmp(mod->name, name) == 0)
3860 return mod;
3862 return NULL;
3865 #ifdef CONFIG_MODULE_UNLOAD
3866 struct module_use {
3867 struct list_head list;
3868 struct module *module_which_uses;
3871 /* I'm not yet certain whether we need the strong form of this. */
3872 static inline int strong_try_module_get(struct module *mod)
3874 if (mod && mod->state != MODULE_STATE_LIVE)
3875 return -EBUSY;
3876 if (try_module_get(mod))
3877 return 0;
3878 return -ENOENT;
3881 /* Does a already use b? */
3882 static int already_uses(struct module *a, struct module *b)
3884 struct module_use *use;
3885 list_for_each_entry(use, &b->modules_which_use_me, list) {
3886 if (use->module_which_uses == a)
3887 return 1;
3889 return 0;
3892 /* Make it so module a uses b. Must be holding module_mutex */
3893 static int use_module(struct module *a, struct module *b)
3895 struct module_use *use;
3896 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
3897 /* 270a6c4cad809e92d7b81adde92d0b3d94eeb8ee was after 2.6.20 */
3898 int no_warn;
3899 #endif /* LINUX_VERSION_CODE */
3900 if (b == NULL || already_uses(a, b))
3901 return 1;
3903 if (strong_try_module_get(b) < 0)
3904 return 0;
3906 use = kmalloc(sizeof(*use), GFP_ATOMIC);
3907 if (!use) {
3908 module_put(b);
3909 return 0;
3911 use->module_which_uses = a;
3912 list_add(&use->list, &b->modules_which_use_me);
3913 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
3914 /* 270a6c4cad809e92d7b81adde92d0b3d94eeb8ee was after 2.6.20 */
3915 no_warn = sysfs_create_link(b->holders_dir, &a->mkobj.kobj, a->name);
3916 #endif /* LINUX_VERSION_CODE */
3917 return 1;
3919 #else /* CONFIG_MODULE_UNLOAD */
3920 static int use_module(struct module *a, struct module *b)
3922 return 1;
3924 #endif /* CONFIG_MODULE_UNLOAD */
3926 #ifndef CONFIG_MODVERSIONS
3927 #define symversion(base, idx) NULL
3928 #else
3929 #define symversion(base, idx) ((base != NULL) ? ((base) + (idx)) : NULL)
3930 #endif
3932 static bool each_symbol_in_section(const struct symsearch *arr,
3933 unsigned int arrsize,
3934 struct module *owner,
3935 bool (*fn)(const struct symsearch *syms,
3936 struct module *owner,
3937 unsigned int symnum, void *data),
3938 void *data)
3940 unsigned int i, j;
3942 for (j = 0; j < arrsize; j++) {
3943 for (i = 0; i < arr[j].stop - arr[j].start; i++)
3944 if (fn(&arr[j], owner, i, data))
3945 return true;
3948 return false;
3951 /* Returns true as soon as fn returns true, otherwise false. */
3952 static bool each_symbol(bool (*fn)(const struct symsearch *arr,
3953 struct module *owner,
3954 unsigned int symnum, void *data),
3955 void *data)
3957 struct module *mod;
3958 const struct symsearch arr[] = {
3959 { __start___ksymtab, __stop___ksymtab, __start___kcrctab,
3960 NOT_GPL_ONLY, false },
3961 { __start___ksymtab_gpl, __stop___ksymtab_gpl,
3962 __start___kcrctab_gpl,
3963 GPL_ONLY, false },
3964 #ifdef KSPLICE_KSYMTAB_FUTURE_SUPPORT
3965 { __start___ksymtab_gpl_future, __stop___ksymtab_gpl_future,
3966 __start___kcrctab_gpl_future,
3967 WILL_BE_GPL_ONLY, false },
3968 #endif /* KSPLICE_KSYMTAB_FUTURE_SUPPORT */
3969 #ifdef KSPLICE_KSYMTAB_UNUSED_SUPPORT
3970 { __start___ksymtab_unused, __stop___ksymtab_unused,
3971 __start___kcrctab_unused,
3972 NOT_GPL_ONLY, true },
3973 { __start___ksymtab_unused_gpl, __stop___ksymtab_unused_gpl,
3974 __start___kcrctab_unused_gpl,
3975 GPL_ONLY, true },
3976 #endif /* KSPLICE_KSYMTAB_UNUSED_SUPPORT */
3979 if (each_symbol_in_section(arr, ARRAY_SIZE(arr), NULL, fn, data))
3980 return 1;
3982 list_for_each_entry(mod, &modules, list) {
3983 struct symsearch module_arr[] = {
3984 { mod->syms, mod->syms + mod->num_syms, mod->crcs,
3985 NOT_GPL_ONLY, false },
3986 { mod->gpl_syms, mod->gpl_syms + mod->num_gpl_syms,
3987 mod->gpl_crcs,
3988 GPL_ONLY, false },
3989 #ifdef KSPLICE_KSYMTAB_FUTURE_SUPPORT
3990 { mod->gpl_future_syms,
3991 mod->gpl_future_syms + mod->num_gpl_future_syms,
3992 mod->gpl_future_crcs,
3993 WILL_BE_GPL_ONLY, false },
3994 #endif /* KSPLICE_KSYMTAB_FUTURE_SUPPORT */
3995 #ifdef KSPLICE_KSYMTAB_UNUSED_SUPPORT
3996 { mod->unused_syms,
3997 mod->unused_syms + mod->num_unused_syms,
3998 mod->unused_crcs,
3999 NOT_GPL_ONLY, true },
4000 { mod->unused_gpl_syms,
4001 mod->unused_gpl_syms + mod->num_unused_gpl_syms,
4002 mod->unused_gpl_crcs,
4003 GPL_ONLY, true },
4004 #endif /* KSPLICE_KSYMTAB_UNUSED_SUPPORT */
4007 if (each_symbol_in_section(module_arr, ARRAY_SIZE(module_arr),
4008 mod, fn, data))
4009 return true;
4011 return false;
4014 struct find_symbol_arg {
4015 /* Input */
4016 const char *name;
4017 bool gplok;
4018 bool warn;
4020 /* Output */
4021 struct module *owner;
4022 const unsigned long *crc;
4023 const struct kernel_symbol *sym;
4026 static bool find_symbol_in_section(const struct symsearch *syms,
4027 struct module *owner,
4028 unsigned int symnum, void *data)
4030 struct find_symbol_arg *fsa = data;
4032 if (strcmp(syms->start[symnum].name, fsa->name) != 0)
4033 return false;
4035 if (!fsa->gplok) {
4036 if (syms->licence == GPL_ONLY)
4037 return false;
4038 if (syms->licence == WILL_BE_GPL_ONLY && fsa->warn) {
4039 printk(KERN_WARNING "Symbol %s is being used "
4040 "by a non-GPL module, which will not "
4041 "be allowed in the future\n", fsa->name);
4042 printk(KERN_WARNING "Please see the file "
4043 "Documentation/feature-removal-schedule.txt "
4044 "in the kernel source tree for more details.\n");
4048 #ifdef CONFIG_UNUSED_SYMBOLS
4049 if (syms->unused && fsa->warn) {
4050 printk(KERN_WARNING "Symbol %s is marked as UNUSED, "
4051 "however this module is using it.\n", fsa->name);
4052 printk(KERN_WARNING
4053 "This symbol will go away in the future.\n");
4054 printk(KERN_WARNING
4055 "Please evalute if this is the right api to use and if "
4056 "it really is, submit a report the linux kernel "
4057 "mailinglist together with submitting your code for "
4058 "inclusion.\n");
4060 #endif
4062 fsa->owner = owner;
4063 fsa->crc = symversion(syms->crcs, symnum);
4064 fsa->sym = &syms->start[symnum];
4065 return true;
4068 /* Find a symbol and return it, along with, (optional) crc and
4069 * (optional) module which owns it */
4070 static const struct kernel_symbol *find_symbol(const char *name,
4071 struct module **owner,
4072 const unsigned long **crc,
4073 bool gplok, bool warn)
4075 struct find_symbol_arg fsa;
4077 fsa.name = name;
4078 fsa.gplok = gplok;
4079 fsa.warn = warn;
4081 if (each_symbol(find_symbol_in_section, &fsa)) {
4082 if (owner)
4083 *owner = fsa.owner;
4084 if (crc)
4085 *crc = fsa.crc;
4086 return fsa.sym;
4089 return NULL;
4092 static inline int within_module_core(unsigned long addr, struct module *mod)
4094 return (unsigned long)mod->module_core <= addr &&
4095 addr < (unsigned long)mod->module_core + mod->core_size;
4098 static inline int within_module_init(unsigned long addr, struct module *mod)
4100 return (unsigned long)mod->module_init <= addr &&
4101 addr < (unsigned long)mod->module_init + mod->init_size;
4104 static struct module *__module_address(unsigned long addr)
4106 struct module *mod;
4108 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
4109 list_for_each_entry_rcu(mod, &modules, list)
4110 #else
4111 /* d72b37513cdfbd3f53f3d485a8c403cc96d2c95f was after 2.6.27 */
4112 list_for_each_entry(mod, &modules, list)
4113 #endif
4114 if (within_module_core(addr, mod) ||
4115 within_module_init(addr, mod))
4116 return mod;
4117 return NULL;
4119 #endif /* LINUX_VERSION_CODE */
4121 struct update_attribute {
4122 struct attribute attr;
4123 ssize_t (*show)(struct update *update, char *buf);
4124 ssize_t (*store)(struct update *update, const char *buf, size_t len);
4127 static ssize_t update_attr_show(struct kobject *kobj, struct attribute *attr,
4128 char *buf)
4130 struct update_attribute *attribute =
4131 container_of(attr, struct update_attribute, attr);
4132 struct update *update = container_of(kobj, struct update, kobj);
4133 if (attribute->show == NULL)
4134 return -EIO;
4135 return attribute->show(update, buf);
4138 static ssize_t update_attr_store(struct kobject *kobj, struct attribute *attr,
4139 const char *buf, size_t len)
4141 struct update_attribute *attribute =
4142 container_of(attr, struct update_attribute, attr);
4143 struct update *update = container_of(kobj, struct update, kobj);
4144 if (attribute->store == NULL)
4145 return -EIO;
4146 return attribute->store(update, buf, len);
4149 static struct sysfs_ops update_sysfs_ops = {
4150 .show = update_attr_show,
4151 .store = update_attr_store,
4154 static void update_release(struct kobject *kobj)
4156 struct update *update;
4157 update = container_of(kobj, struct update, kobj);
4158 cleanup_ksplice_update(update);
4161 static ssize_t stage_show(struct update *update, char *buf)
4163 switch (update->stage) {
4164 case STAGE_PREPARING:
4165 return snprintf(buf, PAGE_SIZE, "preparing\n");
4166 case STAGE_APPLIED:
4167 return snprintf(buf, PAGE_SIZE, "applied\n");
4168 case STAGE_REVERSED:
4169 return snprintf(buf, PAGE_SIZE, "reversed\n");
4171 return 0;
4174 static ssize_t abort_cause_show(struct update *update, char *buf)
4176 switch (update->abort_cause) {
4177 case OK:
4178 return snprintf(buf, PAGE_SIZE, "ok\n");
4179 case NO_MATCH:
4180 return snprintf(buf, PAGE_SIZE, "no_match\n");
4181 #ifdef KSPLICE_STANDALONE
4182 case BAD_SYSTEM_MAP:
4183 return snprintf(buf, PAGE_SIZE, "bad_system_map\n");
4184 #endif /* KSPLICE_STANDALONE */
4185 case CODE_BUSY:
4186 return snprintf(buf, PAGE_SIZE, "code_busy\n");
4187 case MODULE_BUSY:
4188 return snprintf(buf, PAGE_SIZE, "module_busy\n");
4189 case OUT_OF_MEMORY:
4190 return snprintf(buf, PAGE_SIZE, "out_of_memory\n");
4191 case FAILED_TO_FIND:
4192 return snprintf(buf, PAGE_SIZE, "failed_to_find\n");
4193 case ALREADY_REVERSED:
4194 return snprintf(buf, PAGE_SIZE, "already_reversed\n");
4195 case MISSING_EXPORT:
4196 return snprintf(buf, PAGE_SIZE, "missing_export\n");
4197 case UNEXPECTED_RUNNING_TASK:
4198 return snprintf(buf, PAGE_SIZE, "unexpected_running_task\n");
4199 case TARGET_NOT_LOADED:
4200 return snprintf(buf, PAGE_SIZE, "target_not_loaded\n");
4201 case CALL_FAILED:
4202 return snprintf(buf, PAGE_SIZE, "call_failed\n");
4203 case COLD_UPDATE_LOADED:
4204 return snprintf(buf, PAGE_SIZE, "cold_update_loaded\n");
4205 case UNEXPECTED:
4206 return snprintf(buf, PAGE_SIZE, "unexpected\n");
4207 default:
4208 return snprintf(buf, PAGE_SIZE, "unknown\n");
4210 return 0;
4213 static ssize_t conflict_show(struct update *update, char *buf)
4215 const struct conflict *conf;
4216 const struct conflict_addr *ca;
4217 int used = 0;
4218 mutex_lock(&module_mutex);
4219 list_for_each_entry(conf, &update->conflicts, list) {
4220 used += snprintf(buf + used, PAGE_SIZE - used, "%s %d",
4221 conf->process_name, conf->pid);
4222 list_for_each_entry(ca, &conf->stack, list) {
4223 if (!ca->has_conflict)
4224 continue;
4225 used += snprintf(buf + used, PAGE_SIZE - used, " %s",
4226 ca->label);
4228 used += snprintf(buf + used, PAGE_SIZE - used, "\n");
4230 mutex_unlock(&module_mutex);
4231 return used;
4234 /* Used to pass maybe_cleanup_ksplice_update to kthread_run */
4235 static int maybe_cleanup_ksplice_update_wrapper(void *updateptr)
4237 struct update *update = updateptr;
4238 mutex_lock(&module_mutex);
4239 maybe_cleanup_ksplice_update(update);
4240 mutex_unlock(&module_mutex);
4241 return 0;
4244 static ssize_t stage_store(struct update *update, const char *buf, size_t len)
4246 enum stage old_stage;
4247 mutex_lock(&module_mutex);
4248 old_stage = update->stage;
4249 if ((strncmp(buf, "applied", len) == 0 ||
4250 strncmp(buf, "applied\n", len) == 0) &&
4251 update->stage == STAGE_PREPARING)
4252 update->abort_cause = apply_update(update);
4253 else if ((strncmp(buf, "reversed", len) == 0 ||
4254 strncmp(buf, "reversed\n", len) == 0) &&
4255 update->stage == STAGE_APPLIED)
4256 update->abort_cause = reverse_update(update);
4257 else if ((strncmp(buf, "cleanup", len) == 0 ||
4258 strncmp(buf, "cleanup\n", len) == 0) &&
4259 update->stage == STAGE_REVERSED)
4260 kthread_run(maybe_cleanup_ksplice_update_wrapper, update,
4261 "ksplice_cleanup_%s", update->kid);
4263 mutex_unlock(&module_mutex);
4264 return len;
4267 static ssize_t debug_show(struct update *update, char *buf)
4269 return snprintf(buf, PAGE_SIZE, "%d\n", update->debug);
4272 static ssize_t debug_store(struct update *update, const char *buf, size_t len)
4274 unsigned long l;
4275 int ret = strict_strtoul(buf, 10, &l);
4276 if (ret != 0)
4277 return ret;
4278 update->debug = l;
4279 return len;
4282 static ssize_t partial_show(struct update *update, char *buf)
4284 return snprintf(buf, PAGE_SIZE, "%d\n", update->partial);
4287 static ssize_t partial_store(struct update *update, const char *buf, size_t len)
4289 unsigned long l;
4290 int ret = strict_strtoul(buf, 10, &l);
4291 if (ret != 0)
4292 return ret;
4293 update->partial = l;
4294 return len;
4297 static struct update_attribute stage_attribute =
4298 __ATTR(stage, 0600, stage_show, stage_store);
4299 static struct update_attribute abort_cause_attribute =
4300 __ATTR(abort_cause, 0400, abort_cause_show, NULL);
4301 static struct update_attribute debug_attribute =
4302 __ATTR(debug, 0600, debug_show, debug_store);
4303 static struct update_attribute partial_attribute =
4304 __ATTR(partial, 0600, partial_show, partial_store);
4305 static struct update_attribute conflict_attribute =
4306 __ATTR(conflicts, 0400, conflict_show, NULL);
4308 static struct attribute *update_attrs[] = {
4309 &stage_attribute.attr,
4310 &abort_cause_attribute.attr,
4311 &debug_attribute.attr,
4312 &partial_attribute.attr,
4313 &conflict_attribute.attr,
4314 NULL
4317 static struct kobj_type update_ktype = {
4318 .sysfs_ops = &update_sysfs_ops,
4319 .release = update_release,
4320 .default_attrs = update_attrs,
4323 #ifdef KSPLICE_STANDALONE
4324 static int debug;
4325 module_param(debug, int, 0600);
4326 MODULE_PARM_DESC(debug, "Debug level");
4328 extern struct ksplice_system_map ksplice_system_map[], ksplice_system_map_end[];
4330 static struct ksplice_mod_change bootstrap_mod_change = {
4331 .name = "ksplice_" __stringify(KSPLICE_KID),
4332 .kid = "init_" __stringify(KSPLICE_KID),
4333 .target_name = NULL,
4334 .target = NULL,
4335 .map_printk = MAP_PRINTK,
4336 .new_code_mod = THIS_MODULE,
4337 .new_code.system_map = ksplice_system_map,
4338 .new_code.system_map_end = ksplice_system_map_end,
4340 #endif /* KSPLICE_STANDALONE */
4342 static int init_ksplice(void)
4344 #ifdef KSPLICE_STANDALONE
4345 struct ksplice_mod_change *change = &bootstrap_mod_change;
4346 change->update = init_ksplice_update(change->kid);
4347 sort(change->new_code.system_map,
4348 change->new_code.system_map_end - change->new_code.system_map,
4349 sizeof(struct ksplice_system_map), compare_system_map, NULL);
4350 if (change->update == NULL)
4351 return -ENOMEM;
4352 add_to_update(change, change->update);
4353 change->update->debug = debug;
4354 change->update->abort_cause =
4355 apply_relocs(change, ksplice_init_relocs, ksplice_init_relocs_end);
4356 if (change->update->abort_cause == OK)
4357 bootstrapped = true;
4358 cleanup_ksplice_update(bootstrap_mod_change.update);
4359 #else /* !KSPLICE_STANDALONE */
4360 ksplice_kobj = kobject_create_and_add("ksplice", kernel_kobj);
4361 if (ksplice_kobj == NULL)
4362 return -ENOMEM;
4363 #endif /* KSPLICE_STANDALONE */
4364 return 0;
4367 static void cleanup_ksplice(void)
4369 #ifndef KSPLICE_STANDALONE
4370 kobject_put(ksplice_kobj);
4371 #endif /* KSPLICE_STANDALONE */
4374 module_init(init_ksplice);
4375 module_exit(cleanup_ksplice);
4377 MODULE_AUTHOR("Ksplice, Inc.");
4378 MODULE_DESCRIPTION("Ksplice rebootless update system");
4379 #ifdef KSPLICE_VERSION
4380 MODULE_VERSION(KSPLICE_VERSION);
4381 #endif
4382 MODULE_LICENSE("GPL v2");