Skip to content

Commit 628d06a

Browse files
samitolvanenwilldeacon
authored andcommitted
scs: Add page accounting for shadow call stack allocations
This change adds accounting for the memory allocated for shadow stacks. Signed-off-by: Sami Tolvanen <[email protected]> Reviewed-by: Kees Cook <[email protected]> Acked-by: Will Deacon <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent d08b9f0 commit 628d06a

File tree

6 files changed

+37
-0
lines changed

6 files changed

+37
-0
lines changed

drivers/base/node.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ static ssize_t node_read_meminfo(struct device *dev,
415415
"Node %d AnonPages: %8lu kB\n"
416416
"Node %d Shmem: %8lu kB\n"
417417
"Node %d KernelStack: %8lu kB\n"
418+
#ifdef CONFIG_SHADOW_CALL_STACK
419+
"Node %d ShadowCallStack:%8lu kB\n"
420+
#endif
418421
"Node %d PageTables: %8lu kB\n"
419422
"Node %d NFS_Unstable: %8lu kB\n"
420423
"Node %d Bounce: %8lu kB\n"
@@ -438,6 +441,9 @@ static ssize_t node_read_meminfo(struct device *dev,
438441
nid, K(node_page_state(pgdat, NR_ANON_MAPPED)),
439442
nid, K(i.sharedram),
440443
nid, sum_zone_node_page_state(nid, NR_KERNEL_STACK_KB),
444+
#ifdef CONFIG_SHADOW_CALL_STACK
445+
nid, sum_zone_node_page_state(nid, NR_KERNEL_SCS_KB),
446+
#endif
441447
nid, K(sum_zone_node_page_state(nid, NR_PAGETABLE)),
442448
nid, K(node_page_state(pgdat, NR_UNSTABLE_NFS)),
443449
nid, K(sum_zone_node_page_state(nid, NR_BOUNCE)),

fs/proc/meminfo.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ static int meminfo_proc_show(struct seq_file *m, void *v)
103103
show_val_kb(m, "SUnreclaim: ", sunreclaim);
104104
seq_printf(m, "KernelStack: %8lu kB\n",
105105
global_zone_page_state(NR_KERNEL_STACK_KB));
106+
#ifdef CONFIG_SHADOW_CALL_STACK
107+
seq_printf(m, "ShadowCallStack:%8lu kB\n",
108+
global_zone_page_state(NR_KERNEL_SCS_KB));
109+
#endif
106110
show_val_kb(m, "PageTables: ",
107111
global_zone_page_state(NR_PAGETABLE));
108112

include/linux/mmzone.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ enum zone_stat_item {
156156
NR_MLOCK, /* mlock()ed pages found and moved off LRU */
157157
NR_PAGETABLE, /* used for pagetables */
158158
NR_KERNEL_STACK_KB, /* measured in KiB */
159+
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
160+
NR_KERNEL_SCS_KB, /* measured in KiB */
161+
#endif
159162
/* Second 128 byte cacheline */
160163
NR_BOUNCE,
161164
#if IS_ENABLED(CONFIG_ZSMALLOC)

kernel/scs.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
*/
77

88
#include <linux/kasan.h>
9+
#include <linux/mm.h>
910
#include <linux/scs.h>
1011
#include <linux/slab.h>
12+
#include <linux/vmstat.h>
1113
#include <asm/scs.h>
1214

1315
static struct kmem_cache *scs_cache;
@@ -40,6 +42,17 @@ void __init scs_init(void)
4042
scs_cache = kmem_cache_create("scs_cache", SCS_SIZE, 0, 0, NULL);
4143
}
4244

45+
static struct page *__scs_page(struct task_struct *tsk)
46+
{
47+
return virt_to_page(task_scs(tsk));
48+
}
49+
50+
static void scs_account(struct task_struct *tsk, int account)
51+
{
52+
mod_zone_page_state(page_zone(__scs_page(tsk)), NR_KERNEL_SCS_KB,
53+
account * (SCS_SIZE / 1024));
54+
}
55+
4356
int scs_prepare(struct task_struct *tsk, int node)
4457
{
4558
void *s = scs_alloc(node);
@@ -49,6 +62,7 @@ int scs_prepare(struct task_struct *tsk, int node)
4962

5063
task_scs(tsk) = s;
5164
task_scs_offset(tsk) = 0;
65+
scs_account(tsk, 1);
5266

5367
return 0;
5468
}
@@ -61,5 +75,6 @@ void scs_release(struct task_struct *tsk)
6175
return;
6276

6377
WARN(scs_corrupted(tsk), "corrupted shadow stack detected when freeing task\n");
78+
scs_account(tsk, -1);
6479
scs_free(s);
6580
}

mm/page_alloc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5411,6 +5411,9 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
54115411
" managed:%lukB"
54125412
" mlocked:%lukB"
54135413
" kernel_stack:%lukB"
5414+
#ifdef CONFIG_SHADOW_CALL_STACK
5415+
" shadow_call_stack:%lukB"
5416+
#endif
54145417
" pagetables:%lukB"
54155418
" bounce:%lukB"
54165419
" free_pcp:%lukB"
@@ -5433,6 +5436,9 @@ void show_free_areas(unsigned int filter, nodemask_t *nodemask)
54335436
K(zone_managed_pages(zone)),
54345437
K(zone_page_state(zone, NR_MLOCK)),
54355438
zone_page_state(zone, NR_KERNEL_STACK_KB),
5439+
#ifdef CONFIG_SHADOW_CALL_STACK
5440+
zone_page_state(zone, NR_KERNEL_SCS_KB),
5441+
#endif
54365442
K(zone_page_state(zone, NR_PAGETABLE)),
54375443
K(zone_page_state(zone, NR_BOUNCE)),
54385444
K(free_pcp),

mm/vmstat.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,9 @@ const char * const vmstat_text[] = {
11191119
"nr_mlock",
11201120
"nr_page_table_pages",
11211121
"nr_kernel_stack",
1122+
#if IS_ENABLED(CONFIG_SHADOW_CALL_STACK)
1123+
"nr_shadow_call_stack",
1124+
#endif
11221125
"nr_bounce",
11231126
#if IS_ENABLED(CONFIG_ZSMALLOC)
11241127
"nr_zspages",

0 commit comments

Comments
 (0)