瀏覽代碼

fs/proc: Show STACKLEAK metrics in the /proc file system

Introduce CONFIG_STACKLEAK_METRICS providing STACKLEAK information about
tasks via the /proc file system. In particular, /proc/<pid>/stack_depth
shows the maximum kernel stack consumption for the current and previous
syscalls. Although this information is not precise, it can be useful for
estimating the STACKLEAK performance impact for your workloads.

Suggested-by: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Alexander Popov <alex.popov@linux.com>
Tested-by: Laura Abbott <labbott@redhat.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Alexander Popov 7 年之前
父節點
當前提交
c8d126275a
共有 5 個文件被更改,包括 38 次插入0 次删除
  1. 18 0
      fs/proc/base.c
  2. 1 0
      include/linux/sched.h
  3. 3 0
      include/linux/stackleak.h
  4. 4 0
      kernel/stackleak.c
  5. 12 0
      scripts/gcc-plugins/Kconfig

+ 18 - 0
fs/proc/base.c

@@ -2891,6 +2891,21 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
 }
 }
 #endif /* CONFIG_LIVEPATCH */
 #endif /* CONFIG_LIVEPATCH */
 
 
+#ifdef CONFIG_STACKLEAK_METRICS
+static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
+				struct pid *pid, struct task_struct *task)
+{
+	unsigned long prev_depth = THREAD_SIZE -
+				(task->prev_lowest_stack & (THREAD_SIZE - 1));
+	unsigned long depth = THREAD_SIZE -
+				(task->lowest_stack & (THREAD_SIZE - 1));
+
+	seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
+							prev_depth, depth);
+	return 0;
+}
+#endif /* CONFIG_STACKLEAK_METRICS */
+
 /*
 /*
  * Thread groups
  * Thread groups
  */
  */
@@ -2992,6 +3007,9 @@ static const struct pid_entry tgid_base_stuff[] = {
 #ifdef CONFIG_LIVEPATCH
 #ifdef CONFIG_LIVEPATCH
 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
 	ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
 #endif
 #endif
+#ifdef CONFIG_STACKLEAK_METRICS
+	ONE("stack_depth", S_IRUGO, proc_stack_depth),
+#endif
 };
 };
 
 
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)

+ 1 - 0
include/linux/sched.h

@@ -1194,6 +1194,7 @@ struct task_struct {
 
 
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
 	unsigned long			lowest_stack;
 	unsigned long			lowest_stack;
+	unsigned long			prev_lowest_stack;
 #endif
 #endif
 
 
 	/*
 	/*

+ 3 - 0
include/linux/stackleak.h

@@ -18,6 +18,9 @@
 static inline void stackleak_task_init(struct task_struct *t)
 static inline void stackleak_task_init(struct task_struct *t)
 {
 {
 	t->lowest_stack = (unsigned long)end_of_stack(t) + sizeof(unsigned long);
 	t->lowest_stack = (unsigned long)end_of_stack(t) + sizeof(unsigned long);
+# ifdef CONFIG_STACKLEAK_METRICS
+	t->prev_lowest_stack = t->lowest_stack;
+# endif
 }
 }
 #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
 #else /* !CONFIG_GCC_PLUGIN_STACKLEAK */
 static inline void stackleak_task_init(struct task_struct *t) { }
 static inline void stackleak_task_init(struct task_struct *t) { }

+ 4 - 0
kernel/stackleak.c

@@ -41,6 +41,10 @@ asmlinkage void stackleak_erase(void)
 	if (kstack_ptr == boundary)
 	if (kstack_ptr == boundary)
 		kstack_ptr += sizeof(unsigned long);
 		kstack_ptr += sizeof(unsigned long);
 
 
+#ifdef CONFIG_STACKLEAK_METRICS
+	current->prev_lowest_stack = kstack_ptr;
+#endif
+
 	/*
 	/*
 	 * Now write the poison value to the kernel stack. Start from
 	 * Now write the poison value to the kernel stack. Start from
 	 * 'kstack_ptr' and move up till the new 'boundary'. We assume that
 	 * 'kstack_ptr' and move up till the new 'boundary'. We assume that

+ 12 - 0
scripts/gcc-plugins/Kconfig

@@ -170,4 +170,16 @@ config STACKLEAK_TRACK_MIN_SIZE
 	  a stack frame size greater than or equal to this parameter.
 	  a stack frame size greater than or equal to this parameter.
 	  If unsure, leave the default value 100.
 	  If unsure, leave the default value 100.
 
 
+config STACKLEAK_METRICS
+	bool "Show STACKLEAK metrics in the /proc file system"
+	depends on GCC_PLUGIN_STACKLEAK
+	depends on PROC_FS
+	help
+	  If this is set, STACKLEAK metrics for every task are available in
+	  the /proc file system. In particular, /proc/<pid>/stack_depth
+	  shows the maximum kernel stack consumption for the current and
+	  previous syscalls. Although this information is not precise, it
+	  can be useful for estimating the STACKLEAK performance impact for
+	  your workloads.
+
 endif
 endif