summaryrefslogtreecommitdiffstats
path: root/kernel/scs.c
diff options
context:
space:
mode:
authorSami Tolvanen <samitolvanen@google.com>2020-04-27 09:00:09 -0700
committerWill Deacon <will@kernel.org>2020-05-15 16:35:49 +0100
commit5bbaf9d1fcb9be696ee9a61636ab6803556c70f2 (patch)
tree6654b089420824522d0f304575400dceffc70707 /kernel/scs.c
parent628d06a48f57c36abdc2a024930212e654a501b7 (diff)
downloadlinux-5bbaf9d1fcb9be696ee9a61636ab6803556c70f2.tar.bz2
scs: Add support for stack usage debugging
Implements CONFIG_DEBUG_STACK_USAGE for shadow stacks. When enabled, also prints out the highest shadow stack usage per process. Signed-off-by: Sami Tolvanen <samitolvanen@google.com> Reviewed-by: Kees Cook <keescook@chromium.org> Acked-by: Will Deacon <will@kernel.org> [will: rewrote most of scs_check_usage()] Signed-off-by: Will Deacon <will@kernel.org>
Diffstat (limited to 'kernel/scs.c')
-rw-r--r--kernel/scs.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/kernel/scs.c b/kernel/scs.c
index 6d2f983ac54e..9389c28f0853 100644
--- a/kernel/scs.c
+++ b/kernel/scs.c
@@ -63,10 +63,37 @@ int scs_prepare(struct task_struct *tsk, int node)
task_scs(tsk) = s;
task_scs_offset(tsk) = 0;
scs_account(tsk, 1);
-
return 0;
}
+static void scs_check_usage(struct task_struct *tsk)
+{
+ static unsigned long highest;
+
+ unsigned long *p, prev, curr = highest, used = 0;
+
+ if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
+ return;
+
+ for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
+ if (!READ_ONCE_NOCHECK(*p))
+ break;
+ used++;
+ }
+
+ while (used > curr) {
+ prev = cmpxchg_relaxed(&highest, curr, used);
+
+ if (prev == curr) {
+ pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
+ tsk->comm, task_pid_nr(tsk), used);
+ break;
+ }
+
+ curr = prev;
+ }
+}
+
void scs_release(struct task_struct *tsk)
{
void *s = task_scs(tsk);
@@ -75,6 +102,7 @@ void scs_release(struct task_struct *tsk)
return;
WARN(scs_corrupted(tsk), "corrupted shadow stack detected when freeing task\n");
+ scs_check_usage(tsk);
scs_account(tsk, -1);
scs_free(s);
}