OSDN Git Service

Merge tag 'trace-v5.2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux...
[uclinux-h8/linux.git] / arch / x86 / kernel / ftrace.c
index bd553b3..0927bb1 100644 (file)
@@ -697,12 +697,8 @@ static inline void *alloc_tramp(unsigned long size)
 {
        return module_alloc(size);
 }
-static inline void tramp_free(void *tramp, int size)
+static inline void tramp_free(void *tramp)
 {
-       int npages = PAGE_ALIGN(size) >> PAGE_SHIFT;
-
-       set_memory_nx((unsigned long)tramp, npages);
-       set_memory_rw((unsigned long)tramp, npages);
        module_memfree(tramp);
 }
 #else
@@ -711,7 +707,7 @@ static inline void *alloc_tramp(unsigned long size)
 {
        return NULL;
 }
-static inline void tramp_free(void *tramp, int size) { }
+static inline void tramp_free(void *tramp) { }
 #endif
 
 /* Defined as markers to the end of the ftrace default trampolines */
@@ -749,6 +745,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
        unsigned long end_offset;
        unsigned long op_offset;
        unsigned long offset;
+       unsigned long npages;
        unsigned long size;
        unsigned long retq;
        unsigned long *ptr;
@@ -781,6 +778,7 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
                return 0;
 
        *tramp_size = size + RET_SIZE + sizeof(void *);
+       npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
 
        /* Copy ftrace_caller onto the trampoline memory */
        ret = probe_kernel_read(trampoline, (void *)start_offset, size);
@@ -825,9 +823,17 @@ create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
        /* ALLOC_TRAMP flags lets us know we created it */
        ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
 
+       set_vm_flush_reset_perms(trampoline);
+
+       /*
+        * Module allocation needs to be completed by making the page
+        * executable. The page is still writable, which is a security hazard,
+        * but anyhow ftrace breaks W^X completely.
+        */
+       set_memory_x((unsigned long)trampoline, npages);
        return (unsigned long)trampoline;
 fail:
-       tramp_free(trampoline, *tramp_size);
+       tramp_free(trampoline);
        return 0;
 }
 
@@ -960,7 +966,7 @@ void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
        if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
                return;
 
-       tramp_free((void *)ops->trampoline, ops->trampoline_size);
+       tramp_free((void *)ops->trampoline);
        ops->trampoline = 0;
 }