1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun #ifndef _LINUX_KCSAN_H
4*4882a593Smuzhiyun #define _LINUX_KCSAN_H
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/kcsan-checks.h>
7*4882a593Smuzhiyun #include <linux/types.h>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #ifdef CONFIG_KCSAN
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * Context for each thread of execution: for tasks, this is stored in
13*4882a593Smuzhiyun * task_struct, and interrupts access internal per-CPU storage.
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun struct kcsan_ctx {
16*4882a593Smuzhiyun int disable_count; /* disable counter */
17*4882a593Smuzhiyun int atomic_next; /* number of following atomic ops */
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun /*
20*4882a593Smuzhiyun * We distinguish between: (a) nestable atomic regions that may contain
21*4882a593Smuzhiyun * other nestable regions; and (b) flat atomic regions that do not keep
22*4882a593Smuzhiyun * track of nesting. Both (a) and (b) are entirely independent of each
23*4882a593Smuzhiyun * other, and a flat region may be started in a nestable region or
24*4882a593Smuzhiyun * vice-versa.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * This is required because, for example, in the annotations for
27*4882a593Smuzhiyun * seqlocks, we declare seqlock writer critical sections as (a) nestable
28*4882a593Smuzhiyun * atomic regions, but reader critical sections as (b) flat atomic
29*4882a593Smuzhiyun * regions, but have encountered cases where seqlock reader critical
30*4882a593Smuzhiyun * sections are contained within writer critical sections (the opposite
31*4882a593Smuzhiyun * may be possible, too).
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * To support these cases, we independently track the depth of nesting
34*4882a593Smuzhiyun * for (a), and whether the leaf level is flat for (b).
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun int atomic_nest_count;
37*4882a593Smuzhiyun bool in_flat_atomic;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun * Access mask for all accesses if non-zero.
41*4882a593Smuzhiyun */
42*4882a593Smuzhiyun unsigned long access_mask;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* List of scoped accesses. */
45*4882a593Smuzhiyun struct list_head scoped_accesses;
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /**
49*4882a593Smuzhiyun * kcsan_init - initialize KCSAN runtime
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun void kcsan_init(void);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #else /* CONFIG_KCSAN */
54*4882a593Smuzhiyun
kcsan_init(void)55*4882a593Smuzhiyun static inline void kcsan_init(void) { }
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #endif /* CONFIG_KCSAN */
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #endif /* _LINUX_KCSAN_H */
60