Skip to content

Commit cf99aba

Browse files
aarcangeLinus Torvalds
authored andcommitted
make seccomp zerocost in schedule
This follows a suggestion from Chuck Ebbert on how to make seccomp absolutely zerocost in schedule too. The only remaining footprint of seccomp is in terms of the bzImage size that becomes a few bytes (perhaps even a few kbytes) larger, measure it if you care in the embedded. Signed-off-by: Andrea Arcangeli <andrea@cpushare.com> Cc: Andi Kleen <ak@suse.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1d9d02f commit cf99aba

5 files changed

Lines changed: 50 additions & 45 deletions

File tree

‎arch/i386/kernel/process.c‎

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,31 @@ int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
538538
return 1;
539539
}
540540

541-
static noinline void __switch_to_xtra(struct task_struct *next_p,
542-
struct tss_struct *tss)
541+
#ifdef CONFIG_SECCOMP
542+
void hard_disable_TSC(void)
543+
{
544+
write_cr4(read_cr4() | X86_CR4_TSD);
545+
}
546+
void disable_TSC(void)
547+
{
548+
preempt_disable();
549+
if (!test_and_set_thread_flag(TIF_NOTSC))
550+
/*
551+
* Must flip the CPU state synchronously with
552+
* TIF_NOTSC in the current running context.
553+
*/
554+
hard_disable_TSC();
555+
preempt_enable();
556+
}
557+
void hard_enable_TSC(void)
558+
{
559+
write_cr4(read_cr4() & ~X86_CR4_TSD);
560+
}
561+
#endif /* CONFIG_SECCOMP */
562+
563+
static noinline void
564+
__switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
565+
struct tss_struct *tss)
543566
{
544567
struct thread_struct *next;
545568

@@ -555,6 +578,17 @@ static noinline void __switch_to_xtra(struct task_struct *next_p,
555578
set_debugreg(next->debugreg[7], 7);
556579
}
557580

581+
#ifdef CONFIG_SECCOMP
582+
if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
583+
test_tsk_thread_flag(next_p, TIF_NOTSC)) {
584+
/* prev and next are different */
585+
if (test_tsk_thread_flag(next_p, TIF_NOTSC))
586+
hard_disable_TSC();
587+
else
588+
hard_enable_TSC();
589+
}
590+
#endif
591+
558592
if (!test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
559593
/*
560594
* Disable the bitmap via an invalid offset. We still cache
@@ -585,33 +619,6 @@ static noinline void __switch_to_xtra(struct task_struct *next_p,
585619
tss->x86_tss.io_bitmap_base = INVALID_IO_BITMAP_OFFSET_LAZY;
586620
}
587621

588-
/*
589-
* This function selects if the context switch from prev to next
590-
* has to tweak the TSC disable bit in the cr4.
591-
*/
592-
static inline void disable_tsc(struct task_struct *prev_p,
593-
struct task_struct *next_p)
594-
{
595-
struct thread_info *prev, *next;
596-
597-
/*
598-
* gcc should eliminate the ->thread_info dereference if
599-
* has_secure_computing returns 0 at compile time (SECCOMP=n).
600-
*/
601-
prev = task_thread_info(prev_p);
602-
next = task_thread_info(next_p);
603-
604-
if (has_secure_computing(prev) || has_secure_computing(next)) {
605-
/* slow path here */
606-
if (has_secure_computing(prev) &&
607-
!has_secure_computing(next)) {
608-
write_cr4(read_cr4() & ~X86_CR4_TSD);
609-
} else if (!has_secure_computing(prev) &&
610-
has_secure_computing(next))
611-
write_cr4(read_cr4() | X86_CR4_TSD);
612-
}
613-
}
614-
615622
/*
616623
* switch_to(x,yn) should switch tasks from x to y.
617624
*
@@ -689,11 +696,9 @@ struct task_struct fastcall * __switch_to(struct task_struct *prev_p, struct tas
689696
/*
690697
* Now maybe handle debug registers and/or IO bitmaps
691698
*/
692-
if (unlikely((task_thread_info(next_p)->flags & _TIF_WORK_CTXSW)
693-
|| test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)))
694-
__switch_to_xtra(next_p, tss);
695-
696-
disable_tsc(prev_p, next_p);
699+
if (unlikely(task_thread_info(prev_p)->flags & _TIF_WORK_CTXSW_PREV ||
700+
task_thread_info(next_p)->flags & _TIF_WORK_CTXSW_NEXT))
701+
__switch_to_xtra(prev_p, next_p, tss);
697702

698703
/*
699704
* Leave lazy mode, flushing any hypercalls made here.

‎include/asm-i386/processor.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ extern int bootloader_type;
228228

229229
#define HAVE_ARCH_PICK_MMAP_LAYOUT
230230

231+
extern void hard_disable_TSC(void);
232+
extern void disable_TSC(void);
233+
extern void hard_enable_TSC(void);
234+
231235
/*
232236
* Size of io_bitmap.
233237
*/

‎include/asm-i386/thread_info.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static inline struct thread_info *current_thread_info(void)
137137
#define TIF_DEBUG 17 /* uses debug registers */
138138
#define TIF_IO_BITMAP 18 /* uses I/O bitmap */
139139
#define TIF_FREEZE 19 /* is freezing for suspend */
140+
#define TIF_NOTSC 20 /* TSC is not accessible in userland */
140141

141142
#define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
142143
#define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
@@ -151,6 +152,7 @@ static inline struct thread_info *current_thread_info(void)
151152
#define _TIF_DEBUG (1<<TIF_DEBUG)
152153
#define _TIF_IO_BITMAP (1<<TIF_IO_BITMAP)
153154
#define _TIF_FREEZE (1<<TIF_FREEZE)
155+
#define _TIF_NOTSC (1<<TIF_NOTSC)
154156

155157
/* work to do on interrupt/exception return */
156158
#define _TIF_WORK_MASK \
@@ -160,7 +162,8 @@ static inline struct thread_info *current_thread_info(void)
160162
#define _TIF_ALLWORK_MASK (0x0000FFFF & ~_TIF_SECCOMP)
161163

162164
/* flags to check in __switch_to() */
163-
#define _TIF_WORK_CTXSW (_TIF_DEBUG|_TIF_IO_BITMAP)
165+
#define _TIF_WORK_CTXSW_NEXT (_TIF_IO_BITMAP | _TIF_NOTSC | _TIF_DEBUG)
166+
#define _TIF_WORK_CTXSW_PREV (_TIF_IO_BITMAP | _TIF_NOTSC)
164167

165168
/*
166169
* Thread-synchronous status.

‎include/linux/seccomp.h‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ static inline void secure_computing(int this_syscall)
1616
__secure_computing(this_syscall);
1717
}
1818

19-
static inline int has_secure_computing(struct thread_info *ti)
20-
{
21-
return unlikely(test_ti_thread_flag(ti, TIF_SECCOMP));
22-
}
23-
2419
extern long prctl_get_seccomp(void);
2520
extern long prctl_set_seccomp(unsigned long);
2621

@@ -29,11 +24,6 @@ extern long prctl_set_seccomp(unsigned long);
2924
typedef struct { } seccomp_t;
3025

3126
#define secure_computing(x) do { } while (0)
32-
/* static inline to preserve typechecking */
33-
static inline int has_secure_computing(struct thread_info *ti)
34-
{
35-
return 0;
36-
}
3727

3828
static inline long prctl_get_seccomp(void)
3929
{

‎kernel/seccomp.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ long prctl_set_seccomp(unsigned long seccomp_mode)
7474
if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
7575
current->seccomp.mode = seccomp_mode;
7676
set_thread_flag(TIF_SECCOMP);
77+
#ifdef TIF_NOTSC
78+
disable_TSC();
79+
#endif
7780
ret = 0;
7881
}
7982

0 commit comments

Comments
 (0)