fs/proc: Show STACKLEAK metrics in the /proc file system
authorAlexander Popov <alex.popov@linux.com>
Thu, 16 Aug 2018 22:17:01 +0000 (01:17 +0300)
committerKees Cook <keescook@chromium.org>
Tue, 4 Sep 2018 17:35:48 +0000 (10:35 -0700)
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>
fs/proc/base.c
include/linux/sched.h
include/linux/stackleak.h
kernel/stackleak.c
scripts/gcc-plugins/Kconfig

index ccf86f16d9f0190c18e7f4345b7ca0960709eb05..2a238d68610ef486301c0b9ca0795106f39e2a98 100644 (file)
@@ -2891,6 +2891,21 @@ static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
 }
 #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
  */
@@ -2992,6 +3007,9 @@ static const struct pid_entry tgid_base_stuff[] = {
 #ifdef CONFIG_LIVEPATCH
        ONE("patch_state",  S_IRUSR, proc_pid_patch_state),
 #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)
index c1a23acd24e727254f317c4628d2270d0e4e8c78..ae9d10e14b82a2a35c978dce7aeed315d3b3804e 100644 (file)
@@ -1194,6 +1194,7 @@ struct task_struct {
 
 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK
        unsigned long                   lowest_stack;
+       unsigned long                   prev_lowest_stack;
 #endif
 
        /*
index 628c2b947b8928ed7dc366ab3cef45f04dc5148f..b911b973d328b91072db69f311f601ad7cc6af0e 100644 (file)
@@ -18,6 +18,9 @@
 static inline void stackleak_task_init(struct task_struct *t)
 {
        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 */
 static inline void stackleak_task_init(struct task_struct *t) { }
index 628485db37ba7c60a595600c290189d989c3acb9..f66239572c898638dc990c3c4b82e4c42bcaa8d1 100644 (file)
@@ -41,6 +41,10 @@ asmlinkage void stackleak_erase(void)
        if (kstack_ptr == boundary)
                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
         * 'kstack_ptr' and move up till the new 'boundary'. We assume that
index c65fdd823591bbeb95be6f6dc2a8777fbbad7ce9..b0a015ef52685e627c81e8868c95586e68888018 100644 (file)
@@ -170,4 +170,16 @@ config STACKLEAK_TRACK_MIN_SIZE
          a stack frame size greater than or equal to this parameter.
          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