xref: /openbmc/linux/kernel/scs.c (revision a2abe7cbd8fe2db5ff386c968e2273d9dc6c468d)
1d08b9f0cSSami Tolvanen // SPDX-License-Identifier: GPL-2.0
2d08b9f0cSSami Tolvanen /*
3d08b9f0cSSami Tolvanen  * Shadow Call Stack support.
4d08b9f0cSSami Tolvanen  *
5d08b9f0cSSami Tolvanen  * Copyright (C) 2019 Google LLC
6d08b9f0cSSami Tolvanen  */
7d08b9f0cSSami Tolvanen 
8*a2abe7cbSSami Tolvanen #include <linux/cpuhotplug.h>
9d08b9f0cSSami Tolvanen #include <linux/kasan.h>
10628d06a4SSami Tolvanen #include <linux/mm.h>
11d08b9f0cSSami Tolvanen #include <linux/scs.h>
12*a2abe7cbSSami Tolvanen #include <linux/vmalloc.h>
13628d06a4SSami Tolvanen #include <linux/vmstat.h>
14d08b9f0cSSami Tolvanen 
15bee348faSWill Deacon static void __scs_account(void *s, int account)
16bee348faSWill Deacon {
17*a2abe7cbSSami Tolvanen 	struct page *scs_page = vmalloc_to_page(s);
18bee348faSWill Deacon 
19991e7673SShakeel Butt 	mod_node_page_state(page_pgdat(scs_page), NR_KERNEL_SCS_KB,
20bee348faSWill Deacon 			    account * (SCS_SIZE / SZ_1K));
21bee348faSWill Deacon }
22bee348faSWill Deacon 
23*a2abe7cbSSami Tolvanen /* Matches NR_CACHED_STACKS for VMAP_STACK */
24*a2abe7cbSSami Tolvanen #define NR_CACHED_SCS 2
25*a2abe7cbSSami Tolvanen static DEFINE_PER_CPU(void *, scs_cache[NR_CACHED_SCS]);
26d08b9f0cSSami Tolvanen 
27*a2abe7cbSSami Tolvanen static void *__scs_alloc(int node)
28*a2abe7cbSSami Tolvanen {
29*a2abe7cbSSami Tolvanen 	int i;
30*a2abe7cbSSami Tolvanen 	void *s;
31*a2abe7cbSSami Tolvanen 
32*a2abe7cbSSami Tolvanen 	for (i = 0; i < NR_CACHED_SCS; i++) {
33*a2abe7cbSSami Tolvanen 		s = this_cpu_xchg(scs_cache[i], NULL);
34*a2abe7cbSSami Tolvanen 		if (s) {
35*a2abe7cbSSami Tolvanen 			kasan_unpoison_vmalloc(s, SCS_SIZE);
36*a2abe7cbSSami Tolvanen 			memset(s, 0, SCS_SIZE);
37*a2abe7cbSSami Tolvanen 			return s;
38*a2abe7cbSSami Tolvanen 		}
39*a2abe7cbSSami Tolvanen 	}
40*a2abe7cbSSami Tolvanen 
41*a2abe7cbSSami Tolvanen 	return __vmalloc_node_range(SCS_SIZE, 1, VMALLOC_START, VMALLOC_END,
42*a2abe7cbSSami Tolvanen 				    GFP_SCS, PAGE_KERNEL, 0, node,
43*a2abe7cbSSami Tolvanen 				    __builtin_return_address(0));
44*a2abe7cbSSami Tolvanen }
45*a2abe7cbSSami Tolvanen 
46*a2abe7cbSSami Tolvanen void *scs_alloc(int node)
47*a2abe7cbSSami Tolvanen {
48*a2abe7cbSSami Tolvanen 	void *s;
49*a2abe7cbSSami Tolvanen 
50*a2abe7cbSSami Tolvanen 	s = __scs_alloc(node);
51bee348faSWill Deacon 	if (!s)
52bee348faSWill Deacon 		return NULL;
53bee348faSWill Deacon 
54d08b9f0cSSami Tolvanen 	*__scs_magic(s) = SCS_END_MAGIC;
55bee348faSWill Deacon 
56d08b9f0cSSami Tolvanen 	/*
57d08b9f0cSSami Tolvanen 	 * Poison the allocation to catch unintentional accesses to
58d08b9f0cSSami Tolvanen 	 * the shadow stack when KASAN is enabled.
59d08b9f0cSSami Tolvanen 	 */
60*a2abe7cbSSami Tolvanen 	kasan_poison_vmalloc(s, SCS_SIZE);
61bee348faSWill Deacon 	__scs_account(s, 1);
62d08b9f0cSSami Tolvanen 	return s;
63d08b9f0cSSami Tolvanen }
64d08b9f0cSSami Tolvanen 
65*a2abe7cbSSami Tolvanen void scs_free(void *s)
66d08b9f0cSSami Tolvanen {
67*a2abe7cbSSami Tolvanen 	int i;
68*a2abe7cbSSami Tolvanen 
69bee348faSWill Deacon 	__scs_account(s, -1);
70*a2abe7cbSSami Tolvanen 
71*a2abe7cbSSami Tolvanen 	/*
72*a2abe7cbSSami Tolvanen 	 * We cannot sleep as this can be called in interrupt context,
73*a2abe7cbSSami Tolvanen 	 * so use this_cpu_cmpxchg to update the cache, and vfree_atomic
74*a2abe7cbSSami Tolvanen 	 * to free the stack.
75*a2abe7cbSSami Tolvanen 	 */
76*a2abe7cbSSami Tolvanen 
77*a2abe7cbSSami Tolvanen 	for (i = 0; i < NR_CACHED_SCS; i++)
78*a2abe7cbSSami Tolvanen 		if (this_cpu_cmpxchg(scs_cache[i], 0, s) == NULL)
79*a2abe7cbSSami Tolvanen 			return;
80*a2abe7cbSSami Tolvanen 
81*a2abe7cbSSami Tolvanen 	vfree_atomic(s);
82*a2abe7cbSSami Tolvanen }
83*a2abe7cbSSami Tolvanen 
84*a2abe7cbSSami Tolvanen static int scs_cleanup(unsigned int cpu)
85*a2abe7cbSSami Tolvanen {
86*a2abe7cbSSami Tolvanen 	int i;
87*a2abe7cbSSami Tolvanen 	void **cache = per_cpu_ptr(scs_cache, cpu);
88*a2abe7cbSSami Tolvanen 
89*a2abe7cbSSami Tolvanen 	for (i = 0; i < NR_CACHED_SCS; i++) {
90*a2abe7cbSSami Tolvanen 		vfree(cache[i]);
91*a2abe7cbSSami Tolvanen 		cache[i] = NULL;
92*a2abe7cbSSami Tolvanen 	}
93*a2abe7cbSSami Tolvanen 
94*a2abe7cbSSami Tolvanen 	return 0;
95d08b9f0cSSami Tolvanen }
96d08b9f0cSSami Tolvanen 
97d08b9f0cSSami Tolvanen void __init scs_init(void)
98d08b9f0cSSami Tolvanen {
99*a2abe7cbSSami Tolvanen 	cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "scs:scs_cache", NULL,
100*a2abe7cbSSami Tolvanen 			  scs_cleanup);
101d08b9f0cSSami Tolvanen }
102d08b9f0cSSami Tolvanen 
103d08b9f0cSSami Tolvanen int scs_prepare(struct task_struct *tsk, int node)
104d08b9f0cSSami Tolvanen {
105d08b9f0cSSami Tolvanen 	void *s = scs_alloc(node);
106d08b9f0cSSami Tolvanen 
107d08b9f0cSSami Tolvanen 	if (!s)
108d08b9f0cSSami Tolvanen 		return -ENOMEM;
109d08b9f0cSSami Tolvanen 
11051189c7aSWill Deacon 	task_scs(tsk) = task_scs_sp(tsk) = s;
111d08b9f0cSSami Tolvanen 	return 0;
112d08b9f0cSSami Tolvanen }
113d08b9f0cSSami Tolvanen 
1145bbaf9d1SSami Tolvanen static void scs_check_usage(struct task_struct *tsk)
1155bbaf9d1SSami Tolvanen {
1165bbaf9d1SSami Tolvanen 	static unsigned long highest;
1175bbaf9d1SSami Tolvanen 
1185bbaf9d1SSami Tolvanen 	unsigned long *p, prev, curr = highest, used = 0;
1195bbaf9d1SSami Tolvanen 
1205bbaf9d1SSami Tolvanen 	if (!IS_ENABLED(CONFIG_DEBUG_STACK_USAGE))
1215bbaf9d1SSami Tolvanen 		return;
1225bbaf9d1SSami Tolvanen 
1235bbaf9d1SSami Tolvanen 	for (p = task_scs(tsk); p < __scs_magic(tsk); ++p) {
1245bbaf9d1SSami Tolvanen 		if (!READ_ONCE_NOCHECK(*p))
1255bbaf9d1SSami Tolvanen 			break;
126333ed746SWill Deacon 		used += sizeof(*p);
1275bbaf9d1SSami Tolvanen 	}
1285bbaf9d1SSami Tolvanen 
1295bbaf9d1SSami Tolvanen 	while (used > curr) {
1305bbaf9d1SSami Tolvanen 		prev = cmpxchg_relaxed(&highest, curr, used);
1315bbaf9d1SSami Tolvanen 
1325bbaf9d1SSami Tolvanen 		if (prev == curr) {
1335bbaf9d1SSami Tolvanen 			pr_info("%s (%d): highest shadow stack usage: %lu bytes\n",
1345bbaf9d1SSami Tolvanen 				tsk->comm, task_pid_nr(tsk), used);
1355bbaf9d1SSami Tolvanen 			break;
1365bbaf9d1SSami Tolvanen 		}
1375bbaf9d1SSami Tolvanen 
1385bbaf9d1SSami Tolvanen 		curr = prev;
1395bbaf9d1SSami Tolvanen 	}
1405bbaf9d1SSami Tolvanen }
1415bbaf9d1SSami Tolvanen 
142d08b9f0cSSami Tolvanen void scs_release(struct task_struct *tsk)
143d08b9f0cSSami Tolvanen {
144d08b9f0cSSami Tolvanen 	void *s = task_scs(tsk);
145d08b9f0cSSami Tolvanen 
146d08b9f0cSSami Tolvanen 	if (!s)
147d08b9f0cSSami Tolvanen 		return;
148d08b9f0cSSami Tolvanen 
14988485be5SWill Deacon 	WARN(task_scs_end_corrupted(tsk),
15088485be5SWill Deacon 	     "corrupted shadow stack detected when freeing task\n");
1515bbaf9d1SSami Tolvanen 	scs_check_usage(tsk);
152d08b9f0cSSami Tolvanen 	scs_free(s);
153d08b9f0cSSami Tolvanen }
154