KVM: arm64: Make unwind()/on_accessible_stack() per-unwinder functions
authorMarc Zyngier <maz@kernel.org>
Wed, 27 Jul 2022 14:29:03 +0000 (15:29 +0100)
committerMarc Zyngier <maz@kernel.org>
Wed, 27 Jul 2022 17:18:03 +0000 (18:18 +0100)
Having multiple versions of on_accessible_stack() (one per unwinder)
makes it very hard to reason about what is used where due to the
complexity of the various includes, the forward declarations, and
the reliance on everything being 'inline'.

Instead, move the code back where it should be. Each unwinder
implements:

- on_accessible_stack() as well as the helpers it depends on,

- unwind()/unwind_next(), as they pass on_accessible_stack as
  a parameter to unwind_next_common() (which is the only common
  code here)

This hardly results in any duplication, and makes it much
easier to reason about the code.

Signed-off-by: Marc Zyngier <maz@kernel.org>
Reviewed-by: Kalesh Singh <kaleshsingh@google.com>
Tested-by: Kalesh Singh <kaleshsingh@google.com>
Reviewed-by: Oliver Upton <oliver.upton@linux.dev>
Link: https://lore.kernel.org/r/20220727142906.1856759-4-maz@kernel.org
arch/arm64/include/asm/stacktrace.h
arch/arm64/include/asm/stacktrace/common.h
arch/arm64/include/asm/stacktrace/nvhe.h
arch/arm64/kernel/stacktrace.c
arch/arm64/kvm/hyp/nvhe/stacktrace.c
arch/arm64/kvm/stacktrace.c

index ea828579a98b699ac1f4d85fd78db9ee13103591..6ebdcdff77f56c5e8502e45cb04daa2856954b7f 100644 (file)
@@ -57,78 +57,4 @@ static inline bool on_overflow_stack(unsigned long sp, unsigned long size,
                        struct stack_info *info) { return false; }
 #endif
 
-
-/*
- * We can only safely access per-cpu stacks from current in a non-preemptible
- * context.
- */
-static inline bool on_accessible_stack(const struct task_struct *tsk,
-                                      unsigned long sp, unsigned long size,
-                                      struct stack_info *info)
-{
-       if (on_accessible_stack_common(tsk, sp, size, info))
-               return true;
-
-       if (on_task_stack(tsk, sp, size, info))
-               return true;
-       if (tsk != current || preemptible())
-               return false;
-       if (on_irq_stack(sp, size, info))
-               return true;
-       if (on_sdei_stack(sp, size, info))
-               return true;
-
-       return false;
-}
-
-/*
- * Unwind from one frame record (A) to the next frame record (B).
- *
- * We terminate early if the location of B indicates a malformed chain of frame
- * records (e.g. a cycle), determined based on the location and fp value of A
- * and the location (but not the fp value) of B.
- */
-static inline int notrace unwind_next(struct unwind_state *state)
-{
-       struct task_struct *tsk = state->task;
-       unsigned long fp = state->fp;
-       struct stack_info info;
-       int err;
-
-       /* Final frame; nothing to unwind */
-       if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
-               return -ENOENT;
-
-       err = unwind_next_common(state, &info, NULL);
-       if (err)
-               return err;
-
-       state->pc = ptrauth_strip_insn_pac(state->pc);
-
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
-       if (tsk->ret_stack &&
-               (state->pc == (unsigned long)return_to_handler)) {
-               unsigned long orig_pc;
-               /*
-                * This is a case where function graph tracer has
-                * modified a return address (LR) in a stack frame
-                * to hook a function return.
-                * So replace it to an original value.
-                */
-               orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc,
-                                               (void *)state->fp);
-               if (WARN_ON_ONCE(state->pc == orig_pc))
-                       return -EINVAL;
-               state->pc = orig_pc;
-       }
-#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
-#ifdef CONFIG_KRETPROBES
-       if (is_kretprobe_trampoline(state->pc))
-               state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur);
-#endif
-
-       return 0;
-}
-NOKPROBE_SYMBOL(unwind_next);
-
 #endif /* __ASM_STACKTRACE_H */
index 3ebb69ea374a3d263fe29537c1bd7efd78b79b17..18046a7248a2fef9184e73a73d276ce55716f672 100644 (file)
@@ -79,15 +79,6 @@ struct unwind_state {
        struct task_struct *task;
 };
 
-static inline bool on_overflow_stack(unsigned long sp, unsigned long size,
-                                    struct stack_info *info);
-
-static inline bool on_accessible_stack(const struct task_struct *tsk,
-                                      unsigned long sp, unsigned long size,
-                                      struct stack_info *info);
-
-static inline int unwind_next(struct unwind_state *state);
-
 static inline bool on_stack(unsigned long sp, unsigned long size,
                            unsigned long low, unsigned long high,
                            enum stack_type type, struct stack_info *info)
@@ -106,21 +97,6 @@ static inline bool on_stack(unsigned long sp, unsigned long size,
        return true;
 }
 
-static inline bool on_accessible_stack_common(const struct task_struct *tsk,
-                                             unsigned long sp,
-                                             unsigned long size,
-                                             struct stack_info *info)
-{
-       if (info)
-               info->type = STACK_TYPE_UNKNOWN;
-
-       /*
-        * Both the kernel and nvhe hypervisor make use of
-        * an overflow_stack
-        */
-       return on_overflow_stack(sp, size, info);
-}
-
 static inline void unwind_init_common(struct unwind_state *state,
                                      struct task_struct *task)
 {
@@ -156,8 +132,22 @@ static inline void unwind_init_common(struct unwind_state *state,
 typedef bool (*stack_trace_translate_fp_fn)(unsigned long *fp,
                                            enum stack_type type);
 
+/*
+ * on_accessible_stack_fn() - Check whether a stack range is on any
+ * of the possible stacks.
+ *
+ * @tsk:  task whose stack is being unwound
+ * @sp:   stack address being checked
+ * @size: size of the stack range being checked
+ * @info: stack unwinding context
+ */
+typedef bool (*on_accessible_stack_fn)(const struct task_struct *tsk,
+                                      unsigned long sp, unsigned long size,
+                                      struct stack_info *info);
+
 static inline int unwind_next_common(struct unwind_state *state,
                                     struct stack_info *info,
+                                    on_accessible_stack_fn accessible,
                                     stack_trace_translate_fp_fn translate_fp)
 {
        unsigned long fp = state->fp, kern_fp = fp;
@@ -166,7 +156,7 @@ static inline int unwind_next_common(struct unwind_state *state,
        if (fp & 0x7)
                return -EINVAL;
 
-       if (!on_accessible_stack(tsk, fp, 16, info))
+       if (!accessible(tsk, fp, 16, info))
                return -EINVAL;
 
        if (test_bit(info->type, state->stacks_done))
@@ -212,19 +202,4 @@ static inline int unwind_next_common(struct unwind_state *state,
        return 0;
 }
 
-static inline void notrace unwind(struct unwind_state *state,
-                                 stack_trace_consume_fn consume_entry,
-                                 void *cookie)
-{
-       while (1) {
-               int ret;
-
-               if (!consume_entry(cookie, state->pc))
-                       break;
-               ret = unwind_next(state);
-               if (ret < 0)
-                       break;
-       }
-}
-NOKPROBE_SYMBOL(unwind);
 #endif /* __ASM_STACKTRACE_COMMON_H */
index 8a5cb96d714375f0ab46fcd38003a3030a1bb119..a096216d8970995d7836088a0d2cd2ad2e6a68d6 100644 (file)
@@ -37,59 +37,7 @@ static inline void kvm_nvhe_unwind_init(struct unwind_state *state,
        state->pc = pc;
 }
 
-static inline bool on_hyp_stack(unsigned long sp, unsigned long size,
-                               struct stack_info *info);
-
-static inline bool on_accessible_stack(const struct task_struct *tsk,
-                                      unsigned long sp, unsigned long size,
-                                      struct stack_info *info)
-{
-       if (on_accessible_stack_common(tsk, sp, size, info))
-               return true;
-
-       if (on_hyp_stack(sp, size, info))
-               return true;
-
-       return false;
-}
-
-#ifdef __KVM_NVHE_HYPERVISOR__
-/*
- * Protected nVHE HYP stack unwinder
- *
- * In protected mode, the unwinding is done by the hypervisor in EL2.
- */
-
-#ifdef CONFIG_PROTECTED_NVHE_STACKTRACE
-static inline bool on_overflow_stack(unsigned long sp, unsigned long size,
-                                    struct stack_info *info)
-{
-       unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack);
-       unsigned long high = low + OVERFLOW_STACK_SIZE;
-
-       return on_stack(sp, size, low, high, STACK_TYPE_OVERFLOW, info);
-}
-
-static inline bool on_hyp_stack(unsigned long sp, unsigned long size,
-                               struct stack_info *info)
-{
-       struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params);
-       unsigned long high = params->stack_hyp_va;
-       unsigned long low = high - PAGE_SIZE;
-
-       return on_stack(sp, size, low, high, STACK_TYPE_HYP, info);
-}
-
-static inline int notrace unwind_next(struct unwind_state *state)
-{
-       struct stack_info info;
-
-       return unwind_next_common(state, &info, NULL);
-}
-NOKPROBE_SYMBOL(unwind_next);
-#endif /* CONFIG_PROTECTED_NVHE_STACKTRACE */
-
-#else  /* !__KVM_NVHE_HYPERVISOR__ */
+#ifndef __KVM_NVHE_HYPERVISOR__
 /*
  * Conventional (non-protected) nVHE HYP stack unwinder
  *
@@ -142,36 +90,6 @@ static inline bool kvm_nvhe_stack_kern_va(unsigned long *addr,
        return true;
 }
 
-static inline bool on_overflow_stack(unsigned long sp, unsigned long size,
-                                    struct stack_info *info)
-{
-       struct kvm_nvhe_stacktrace_info *stacktrace_info
-                               = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info);
-       unsigned long low = (unsigned long)stacktrace_info->overflow_stack_base;
-       unsigned long high = low + OVERFLOW_STACK_SIZE;
-
-       return on_stack(sp, size, low, high, STACK_TYPE_OVERFLOW, info);
-}
-
-static inline bool on_hyp_stack(unsigned long sp, unsigned long size,
-                               struct stack_info *info)
-{
-       struct kvm_nvhe_stacktrace_info *stacktrace_info
-                               = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info);
-       unsigned long low = (unsigned long)stacktrace_info->stack_base;
-       unsigned long high = low + PAGE_SIZE;
-
-       return on_stack(sp, size, low, high, STACK_TYPE_HYP, info);
-}
-
-static inline int notrace unwind_next(struct unwind_state *state)
-{
-       struct stack_info info;
-
-       return unwind_next_common(state, &info, kvm_nvhe_stack_kern_va);
-}
-NOKPROBE_SYMBOL(unwind_next);
-
 void kvm_nvhe_dump_backtrace(unsigned long hyp_offset);
 
 #endif /* __KVM_NVHE_HYPERVISOR__ */
index 9fa60ee48499f126e4fa658402d787d348eeac8b..ce190ee18a20121893470ab005ff0bccb6aa5f14 100644 (file)
@@ -67,6 +67,96 @@ static inline void unwind_init_from_task(struct unwind_state *state,
        state->pc = thread_saved_pc(task);
 }
 
+/*
+ * We can only safely access per-cpu stacks from current in a non-preemptible
+ * context.
+ */
+static bool on_accessible_stack(const struct task_struct *tsk,
+                               unsigned long sp, unsigned long size,
+                               struct stack_info *info)
+{
+       if (info)
+               info->type = STACK_TYPE_UNKNOWN;
+
+       if (on_task_stack(tsk, sp, size, info))
+               return true;
+       if (tsk != current || preemptible())
+               return false;
+       if (on_irq_stack(sp, size, info))
+               return true;
+       if (on_overflow_stack(sp, size, info))
+               return true;
+       if (on_sdei_stack(sp, size, info))
+               return true;
+
+       return false;
+}
+
+/*
+ * Unwind from one frame record (A) to the next frame record (B).
+ *
+ * We terminate early if the location of B indicates a malformed chain of frame
+ * records (e.g. a cycle), determined based on the location and fp value of A
+ * and the location (but not the fp value) of B.
+ */
+static int notrace unwind_next(struct unwind_state *state)
+{
+       struct task_struct *tsk = state->task;
+       unsigned long fp = state->fp;
+       struct stack_info info;
+       int err;
+
+       /* Final frame; nothing to unwind */
+       if (fp == (unsigned long)task_pt_regs(tsk)->stackframe)
+               return -ENOENT;
+
+       err = unwind_next_common(state, &info, on_accessible_stack, NULL);
+       if (err)
+               return err;
+
+       state->pc = ptrauth_strip_insn_pac(state->pc);
+
+#ifdef CONFIG_FUNCTION_GRAPH_TRACER
+       if (tsk->ret_stack &&
+               (state->pc == (unsigned long)return_to_handler)) {
+               unsigned long orig_pc;
+               /*
+                * This is a case where function graph tracer has
+                * modified a return address (LR) in a stack frame
+                * to hook a function return.
+                * So replace it to an original value.
+                */
+               orig_pc = ftrace_graph_ret_addr(tsk, NULL, state->pc,
+                                               (void *)state->fp);
+               if (WARN_ON_ONCE(state->pc == orig_pc))
+                       return -EINVAL;
+               state->pc = orig_pc;
+       }
+#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
+#ifdef CONFIG_KRETPROBES
+       if (is_kretprobe_trampoline(state->pc))
+               state->pc = kretprobe_find_ret_addr(tsk, (void *)state->fp, &state->kr_cur);
+#endif
+
+       return 0;
+}
+NOKPROBE_SYMBOL(unwind_next);
+
+static void notrace unwind(struct unwind_state *state,
+                          stack_trace_consume_fn consume_entry, void *cookie)
+{
+       while (1) {
+               int ret;
+
+               if (!consume_entry(cookie, state->pc))
+                       break;
+               ret = unwind_next(state);
+               if (ret < 0)
+                       break;
+       }
+}
+NOKPROBE_SYMBOL(unwind);
+
 static bool dump_backtrace_entry(void *arg, unsigned long where)
 {
        char *loglvl = arg;
index 900324b7a08f1d8764e374bd88cdce332c9e0799..acbe272ecb32044b55d0bcf1b4f2ea013a919ac1 100644 (file)
@@ -39,6 +39,58 @@ static void hyp_prepare_backtrace(unsigned long fp, unsigned long pc)
 
 DEFINE_PER_CPU(unsigned long [NVHE_STACKTRACE_SIZE/sizeof(long)], pkvm_stacktrace);
 
+static bool on_overflow_stack(unsigned long sp, unsigned long size,
+                             struct stack_info *info)
+{
+       unsigned long low = (unsigned long)this_cpu_ptr(overflow_stack);
+       unsigned long high = low + OVERFLOW_STACK_SIZE;
+
+       return on_stack(sp, size, low, high, STACK_TYPE_OVERFLOW, info);
+}
+
+static bool on_hyp_stack(unsigned long sp, unsigned long size,
+                             struct stack_info *info)
+{
+       struct kvm_nvhe_init_params *params = this_cpu_ptr(&kvm_init_params);
+       unsigned long high = params->stack_hyp_va;
+       unsigned long low = high - PAGE_SIZE;
+
+       return on_stack(sp, size, low, high, STACK_TYPE_HYP, info);
+}
+
+static bool on_accessible_stack(const struct task_struct *tsk,
+                               unsigned long sp, unsigned long size,
+                               struct stack_info *info)
+{
+       if (info)
+               info->type = STACK_TYPE_UNKNOWN;
+
+       return (on_overflow_stack(sp, size, info) ||
+               on_hyp_stack(sp, size, info));
+}
+
+static int unwind_next(struct unwind_state *state)
+{
+       struct stack_info info;
+
+       return unwind_next_common(state, &info, on_accessible_stack, NULL);
+}
+
+static void notrace unwind(struct unwind_state *state,
+                          stack_trace_consume_fn consume_entry,
+                          void *cookie)
+{
+       while (1) {
+               int ret;
+
+               if (!consume_entry(cookie, state->pc))
+                       break;
+               ret = unwind_next(state);
+               if (ret < 0)
+                       break;
+       }
+}
+
 /*
  * pkvm_save_backtrace_entry - Saves a protected nVHE HYP stacktrace entry
  *
index 9812aefdcfb49416142e1d03b3e9e12befe38cb3..4d5fec3175ff129f7fa79a285dd57b6d395b58d9 100644 (file)
 
 #include <asm/stacktrace/nvhe.h>
 
+static bool on_overflow_stack(unsigned long sp, unsigned long size,
+                             struct stack_info *info)
+{
+       struct kvm_nvhe_stacktrace_info *stacktrace_info
+                               = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info);
+       unsigned long low = (unsigned long)stacktrace_info->overflow_stack_base;
+       unsigned long high = low + OVERFLOW_STACK_SIZE;
+
+       return on_stack(sp, size, low, high, STACK_TYPE_OVERFLOW, info);
+}
+
+static bool on_hyp_stack(unsigned long sp, unsigned long size,
+                        struct stack_info *info)
+{
+       struct kvm_nvhe_stacktrace_info *stacktrace_info
+                               = this_cpu_ptr_nvhe_sym(kvm_stacktrace_info);
+       unsigned long low = (unsigned long)stacktrace_info->stack_base;
+       unsigned long high = low + PAGE_SIZE;
+
+       return on_stack(sp, size, low, high, STACK_TYPE_HYP, info);
+}
+
+static bool on_accessible_stack(const struct task_struct *tsk,
+                               unsigned long sp, unsigned long size,
+                               struct stack_info *info)
+{
+       if (info)
+               info->type = STACK_TYPE_UNKNOWN;
+
+       return (on_overflow_stack(sp, size, info) ||
+               on_hyp_stack(sp, size, info));
+}
+
+static int unwind_next(struct unwind_state *state)
+{
+       struct stack_info info;
+
+       return unwind_next_common(state, &info, on_accessible_stack,
+                                 kvm_nvhe_stack_kern_va);
+}
+
+static void unwind(struct unwind_state *state,
+                  stack_trace_consume_fn consume_entry, void *cookie)
+{
+       while (1) {
+               int ret;
+
+               if (!consume_entry(cookie, state->pc))
+                       break;
+               ret = unwind_next(state);
+               if (ret < 0)
+                       break;
+       }
+}
+
 /*
  * kvm_nvhe_dump_backtrace_entry - Symbolize and print an nVHE backtrace entry
  *