1 // SPDX-License-Identifier: GPL-2.0
3 * Shadow Call Stack support.
5 * Copyright (C) 2019 Google LLC
8 #include <linux/kasan.h>
10 #include <linux/scs.h>
11 #include <linux/slab.h>
12 #include <linux/vmstat.h>
15 static struct kmem_cache *scs_cache;
17 static void *scs_alloc(int node)
21 s = kmem_cache_alloc_node(scs_cache, GFP_SCS, node);
23 *__scs_magic(s) = SCS_END_MAGIC;
25 * Poison the allocation to catch unintentional accesses to
26 * the shadow stack when KASAN is enabled.
28 kasan_poison_object_data(scs_cache, s);
34 static void scs_free(void *s)
36 kasan_unpoison_object_data(scs_cache, s);
37 kmem_cache_free(scs_cache, s);
40 void __init scs_init(void)
42 scs_cache = kmem_cache_create("scs_cache", SCS_SIZE, 0, 0, NULL);
45 static struct page *__scs_page(struct task_struct *tsk)
47 return virt_to_page(task_scs(tsk));
50 static void scs_account(struct task_struct *tsk, int account)
52 mod_zone_page_state(page_zone(__scs_page(tsk)), NR_KERNEL_SCS_KB,
53 account * (SCS_SIZE / 1024));
56 int scs_prepare(struct task_struct *tsk, int node)
58 void *s = scs_alloc(node);
64 task_scs_offset(tsk) = 0;
70 void scs_release(struct task_struct *tsk)
72 void *s = task_scs(tsk);
77 WARN(scs_corrupted(tsk), "corrupted shadow stack detected when freeing task\n");