Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/entry: Add new, comprehensible entry and exit handlers written in C

The current x86 entry and exit code, written in a mixture of assembly and
C code, is incomprehensible due to being open-coded in a lot of places
without coherent documentation.

It appears to work primary by luck and duct tape: i.e. obvious runtime
failures were fixed on-demand, without re-thinking the design.

Due to those reasons our confidence level in that code is low, and it is
very difficult to incrementally improve.

Add new code written in C, in preparation for simply deleting the old
entry code.

prepare_exit_to_usermode() is a new function that will handle all
slow path exits to user mode. It is called with IRQs disabled
and it leaves us in a state in which it is safe to immediately
return to user mode. IRQs must not be re-enabled at any point
after prepare_exit_to_usermode() returns and user mode is actually
entered. (We can, of course, fail to enter user mode and treat
that failure as a fresh entry to kernel mode.)

All callers of do_notify_resume() will be migrated to call
prepare_exit_to_usermode() instead; prepare_exit_to_usermode() needs
to do everything that do_notify_resume() does today, but it also
takes care of scheduling and context tracking. Unlike
do_notify_resume(), it does not need to be called in a loop.

syscall_return_slowpath() is exactly what it sounds like: it will
be called on any syscall exit slow path. It will replace
syscall_trace_leave() and it calls prepare_exit_to_usermode() on the
way out.

Signed-off-by: Andy Lutomirski <luto@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Denys Vlasenko <dvlasenk@redhat.com>
Cc: Denys Vlasenko <vda.linux@googlemail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Rik van Riel <riel@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: paulmck@linux.vnet.ibm.com
Link: http://lkml.kernel.org/r/c57c8b87661a4152801d7d3786eac2d1a2f209dd.1435952415.git.luto@kernel.org
[ Improved the changelog a bit. ]
Signed-off-by: Ingo Molnar <mingo@kernel.org>

authored by

Andy Lutomirski and committed by
Ingo Molnar
c5c46f59 feed36cd

+111 -1
+111 -1
arch/x86/entry/common.c
··· 207 207 return syscall_trace_enter_phase2(regs, arch, phase1_result); 208 208 } 209 209 210 + /* Deprecated. */ 210 211 void syscall_trace_leave(struct pt_regs *regs) 211 212 { 212 213 bool step; ··· 238 237 user_enter(); 239 238 } 240 239 240 + static struct thread_info *pt_regs_to_thread_info(struct pt_regs *regs) 241 + { 242 + unsigned long top_of_stack = 243 + (unsigned long)(regs + 1) + TOP_OF_KERNEL_STACK_PADDING; 244 + return (struct thread_info *)(top_of_stack - THREAD_SIZE); 245 + } 246 + 247 + /* Called with IRQs disabled. */ 248 + __visible void prepare_exit_to_usermode(struct pt_regs *regs) 249 + { 250 + if (WARN_ON(!irqs_disabled())) 251 + local_irq_disable(); 252 + 253 + /* 254 + * In order to return to user mode, we need to have IRQs off with 255 + * none of _TIF_SIGPENDING, _TIF_NOTIFY_RESUME, _TIF_USER_RETURN_NOTIFY, 256 + * _TIF_UPROBE, or _TIF_NEED_RESCHED set. Several of these flags 257 + * can be set at any time on preemptable kernels if we have IRQs on, 258 + * so we need to loop. Disabling preemption wouldn't help: doing the 259 + * work to clear some of the flags can sleep. 260 + */ 261 + while (true) { 262 + u32 cached_flags = 263 + READ_ONCE(pt_regs_to_thread_info(regs)->flags); 264 + 265 + if (!(cached_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | 266 + _TIF_UPROBE | _TIF_NEED_RESCHED))) 267 + break; 268 + 269 + /* We have work to do. */ 270 + local_irq_enable(); 271 + 272 + if (cached_flags & _TIF_NEED_RESCHED) 273 + schedule(); 274 + 275 + if (cached_flags & _TIF_UPROBE) 276 + uprobe_notify_resume(regs); 277 + 278 + /* deal with pending signal delivery */ 279 + if (cached_flags & _TIF_SIGPENDING) 280 + do_signal(regs); 281 + 282 + if (cached_flags & _TIF_NOTIFY_RESUME) { 283 + clear_thread_flag(TIF_NOTIFY_RESUME); 284 + tracehook_notify_resume(regs); 285 + } 286 + 287 + if (cached_flags & _TIF_USER_RETURN_NOTIFY) 288 + fire_user_return_notifiers(); 289 + 290 + /* Disable IRQs and retry */ 291 + local_irq_disable(); 292 + } 293 + 294 + user_enter(); 295 + } 296 + 241 297 /* 242 - * notification of userspace execution resumption 298 + * Called with IRQs on and fully valid regs. Returns with IRQs off in a 299 + * state such that we can immediately switch to user mode. 300 + */ 301 + __visible void syscall_return_slowpath(struct pt_regs *regs) 302 + { 303 + struct thread_info *ti = pt_regs_to_thread_info(regs); 304 + u32 cached_flags = READ_ONCE(ti->flags); 305 + bool step; 306 + 307 + CT_WARN_ON(ct_state() != CONTEXT_KERNEL); 308 + 309 + if (WARN(irqs_disabled(), "syscall %ld left IRQs disabled", 310 + regs->orig_ax)) 311 + local_irq_enable(); 312 + 313 + /* 314 + * First do one-time work. If these work items are enabled, we 315 + * want to run them exactly once per syscall exit with IRQs on. 316 + */ 317 + if (cached_flags & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | 318 + _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT)) { 319 + audit_syscall_exit(regs); 320 + 321 + if (cached_flags & _TIF_SYSCALL_TRACEPOINT) 322 + trace_sys_exit(regs, regs->ax); 323 + 324 + /* 325 + * If TIF_SYSCALL_EMU is set, we only get here because of 326 + * TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP). 327 + * We already reported this syscall instruction in 328 + * syscall_trace_enter(). 329 + */ 330 + step = unlikely( 331 + (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU)) 332 + == _TIF_SINGLESTEP); 333 + if (step || cached_flags & _TIF_SYSCALL_TRACE) 334 + tracehook_report_syscall_exit(regs, step); 335 + } 336 + 337 + #ifdef CONFIG_COMPAT 338 + /* 339 + * Compat syscalls set TS_COMPAT. Make sure we clear it before 340 + * returning to user mode. 341 + */ 342 + ti->status &= ~TS_COMPAT; 343 + #endif 344 + 345 + local_irq_disable(); 346 + prepare_exit_to_usermode(regs); 347 + } 348 + 349 + /* 350 + * Deprecated notification of userspace execution resumption 243 351 * - triggered by the TIF_WORK_MASK flags 244 352 */ 245 353 __visible void