• Masami Hiramatsu's avatar
    kprobes: Fix optimize_kprobe()/unoptimize_kprobe() cancellation logic · e4add247
    Masami Hiramatsu authored
    optimize_kprobe() and unoptimize_kprobe() cancels if a given kprobe
    is on the optimizing_list or unoptimizing_list already. However, since
    the following commit:
    
      f66c0447 ("kprobes: Set unoptimized flag after unoptimizing code")
    
    modified the update timing of the KPROBE_FLAG_OPTIMIZED, it doesn't
    work as expected anymore.
    
    The optimized_kprobe could be in the following states:
    
    - [optimizing]: Before inserting jump instruction
      op.kp->flags has KPROBE_FLAG_OPTIMIZED and
      op->list is not empty.
    
    - [optimized]: jump inserted
      op.kp->flags has KPROBE_FLAG_OPTIMIZED and
      op->list is empty.
    
    - [unoptimizing]: Before removing jump instruction (including unused
      optprobe)
      op.kp->flags has KPROBE_FLAG_OPTIMIZED and
      op->list is not empty.
    
    - [unoptimized]: jump removed
      op.kp->flags doesn't have KPROBE_FLAG_OPTIMIZED and
      op->list is empty.
    
    Current code mis-expects [unoptimizing] state doesn't have
    KPROBE_FLAG_OPTIMIZED, and that can cause incorrect results.
    
    To fix this, introduce optprobe_queued_unopt() to distinguish [optimizing]
    and [unoptimizing] states and fixes the logic in optimize_kprobe() and
    unoptimize_kprobe().
    
    [ mingo: Cleaned up the changelog and the code a bit. ]
    Signed-off-by: default avatarMasami Hiramatsu <mhiramat@kernel.org>
    Reviewed-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
    Cc: Alexei Starovoitov <ast@kernel.org>
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Cc: bristot@redhat.com
    Fixes: f66c0447 ("kprobes: Set unoptimized flag after unoptimizing code")
    Link: https://lkml.kernel.org/r/157840814418.7181.13478003006386303481.stgit@devnote2Signed-off-by: default avatarIngo Molnar <mingo@kernel.org>
    e4add247
kprobes.c 63.8 KB