Skip to content

Commit bee348f

Browse files
committed
scs: Move accounting into alloc/free functions
There's no need to perform the shadow stack page accounting independently of the lifetime of the underlying allocation, so call the accounting code from the {alloc,free}() functions and simplify the code in the process. Tested-by: Sami Tolvanen <[email protected]> Reviewed-by: Mark Rutland <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 51189c7 commit bee348f

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

kernel/scs.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,35 @@
1414

1515
static struct kmem_cache *scs_cache;
1616

17+
static void __scs_account(void *s, int account)
18+
{
19+
struct page *scs_page = virt_to_page(s);
20+
21+
mod_zone_page_state(page_zone(scs_page), NR_KERNEL_SCS_KB,
22+
account * (SCS_SIZE / SZ_1K));
23+
}
24+
1725
static void *scs_alloc(int node)
1826
{
19-
void *s;
20-
21-
s = kmem_cache_alloc_node(scs_cache, GFP_SCS, node);
22-
if (s) {
23-
*__scs_magic(s) = SCS_END_MAGIC;
24-
/*
25-
* Poison the allocation to catch unintentional accesses to
26-
* the shadow stack when KASAN is enabled.
27-
*/
28-
kasan_poison_object_data(scs_cache, s);
29-
}
27+
void *s = kmem_cache_alloc_node(scs_cache, GFP_SCS, node);
28+
29+
if (!s)
30+
return NULL;
3031

32+
*__scs_magic(s) = SCS_END_MAGIC;
33+
34+
/*
35+
* Poison the allocation to catch unintentional accesses to
36+
* the shadow stack when KASAN is enabled.
37+
*/
38+
kasan_poison_object_data(scs_cache, s);
39+
__scs_account(s, 1);
3140
return s;
3241
}
3342

3443
static void scs_free(void *s)
3544
{
45+
__scs_account(s, -1);
3646
kasan_unpoison_object_data(scs_cache, s);
3747
kmem_cache_free(scs_cache, s);
3848
}
@@ -42,17 +52,6 @@ void __init scs_init(void)
4252
scs_cache = kmem_cache_create("scs_cache", SCS_SIZE, 0, 0, NULL);
4353
}
4454

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-
5655
int scs_prepare(struct task_struct *tsk, int node)
5756
{
5857
void *s = scs_alloc(node);
@@ -61,7 +60,6 @@ int scs_prepare(struct task_struct *tsk, int node)
6160
return -ENOMEM;
6261

6362
task_scs(tsk) = task_scs_sp(tsk) = s;
64-
scs_account(tsk, 1);
6563
return 0;
6664
}
6765

@@ -102,6 +100,5 @@ void scs_release(struct task_struct *tsk)
102100

103101
WARN(scs_corrupted(tsk), "corrupted shadow stack detected when freeing task\n");
104102
scs_check_usage(tsk);
105-
scs_account(tsk, -1);
106103
scs_free(s);
107104
}

0 commit comments

Comments
 (0)