common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/context_tracking.h>
  3. #include <linux/entry-common.h>
  4. #include <linux/resume_user_mode.h>
  5. #include <linux/highmem.h>
  6. #include <linux/jump_label.h>
  7. #include <linux/kmsan.h>
  8. #include <linux/livepatch.h>
  9. #include <linux/audit.h>
  10. #include <linux/tick.h>
  11. #include "common.h"
  12. #define CREATE_TRACE_POINTS
  13. #include <trace/events/syscalls.h>
  14. static inline void syscall_enter_audit(struct pt_regs *regs, long syscall)
  15. {
  16. if (unlikely(audit_context())) {
  17. unsigned long args[6];
  18. syscall_get_arguments(current, regs, args);
  19. audit_syscall_entry(syscall, args[0], args[1], args[2], args[3]);
  20. }
  21. }
  22. long syscall_trace_enter(struct pt_regs *regs, long syscall,
  23. unsigned long work)
  24. {
  25. long ret = 0;
  26. /*
  27. * Handle Syscall User Dispatch. This must comes first, since
  28. * the ABI here can be something that doesn't make sense for
  29. * other syscall_work features.
  30. */
  31. if (work & SYSCALL_WORK_SYSCALL_USER_DISPATCH) {
  32. if (syscall_user_dispatch(regs))
  33. return -1L;
  34. }
  35. /* Handle ptrace */
  36. if (work & (SYSCALL_WORK_SYSCALL_TRACE | SYSCALL_WORK_SYSCALL_EMU)) {
  37. ret = ptrace_report_syscall_entry(regs);
  38. if (ret || (work & SYSCALL_WORK_SYSCALL_EMU))
  39. return -1L;
  40. }
  41. /* Do seccomp after ptrace, to catch any tracer changes. */
  42. if (work & SYSCALL_WORK_SECCOMP) {
  43. ret = __secure_computing(NULL);
  44. if (ret == -1L)
  45. return ret;
  46. }
  47. /* Either of the above might have changed the syscall number */
  48. syscall = syscall_get_nr(current, regs);
  49. if (unlikely(work & SYSCALL_WORK_SYSCALL_TRACEPOINT)) {
  50. trace_sys_enter(regs, syscall);
  51. /*
  52. * Probes or BPF hooks in the tracepoint may have changed the
  53. * system call number as well.
  54. */
  55. syscall = syscall_get_nr(current, regs);
  56. }
  57. syscall_enter_audit(regs, syscall);
  58. return ret ? : syscall;
  59. }
  60. noinstr void syscall_enter_from_user_mode_prepare(struct pt_regs *regs)
  61. {
  62. enter_from_user_mode(regs);
  63. instrumentation_begin();
  64. local_irq_enable();
  65. instrumentation_end();
  66. }
  67. /* Workaround to allow gradual conversion of architecture code */
  68. void __weak arch_do_signal_or_restart(struct pt_regs *regs) { }
  69. /**
  70. * exit_to_user_mode_loop - do any pending work before leaving to user space
  71. * @regs: Pointer to pt_regs on entry stack
  72. * @ti_work: TIF work flags as read by the caller
  73. */
  74. __always_inline unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
  75. unsigned long ti_work)
  76. {
  77. /*
  78. * Before returning to user space ensure that all pending work
  79. * items have been completed.
  80. */
  81. while (ti_work & EXIT_TO_USER_MODE_WORK) {
  82. local_irq_enable_exit_to_user(ti_work);
  83. if (ti_work & _TIF_NEED_RESCHED)
  84. schedule();
  85. if (ti_work & _TIF_UPROBE)
  86. uprobe_notify_resume(regs);
  87. if (ti_work & _TIF_PATCH_PENDING)
  88. klp_update_patch_state(current);
  89. if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
  90. arch_do_signal_or_restart(regs);
  91. if (ti_work & _TIF_NOTIFY_RESUME)
  92. resume_user_mode_work(regs);
  93. /* Architecture specific TIF work */
  94. arch_exit_to_user_mode_work(regs, ti_work);
  95. /*
  96. * Disable interrupts and reevaluate the work flags as they
  97. * might have changed while interrupts and preemption was
  98. * enabled above.
  99. */
  100. local_irq_disable_exit_to_user();
  101. /* Check if any of the above work has queued a deferred wakeup */
  102. tick_nohz_user_enter_prepare();
  103. ti_work = read_thread_flags();
  104. }
  105. /* Return the latest work state for arch_exit_to_user_mode() */
  106. return ti_work;
  107. }
  108. /*
  109. * If SYSCALL_EMU is set, then the only reason to report is when
  110. * SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
  111. * instruction has been already reported in syscall_enter_from_user_mode().
  112. */
  113. static inline bool report_single_step(unsigned long work)
  114. {
  115. if (work & SYSCALL_WORK_SYSCALL_EMU)
  116. return false;
  117. return work & SYSCALL_WORK_SYSCALL_EXIT_TRAP;
  118. }
  119. static void syscall_exit_work(struct pt_regs *regs, unsigned long work)
  120. {
  121. bool step;
  122. /*
  123. * If the syscall was rolled back due to syscall user dispatching,
  124. * then the tracers below are not invoked for the same reason as
  125. * the entry side was not invoked in syscall_trace_enter(): The ABI
  126. * of these syscalls is unknown.
  127. */
  128. if (work & SYSCALL_WORK_SYSCALL_USER_DISPATCH) {
  129. if (unlikely(current->syscall_dispatch.on_dispatch)) {
  130. current->syscall_dispatch.on_dispatch = false;
  131. return;
  132. }
  133. }
  134. audit_syscall_exit(regs);
  135. if (work & SYSCALL_WORK_SYSCALL_TRACEPOINT)
  136. trace_sys_exit(regs, syscall_get_return_value(current, regs));
  137. step = report_single_step(work);
  138. if (step || work & SYSCALL_WORK_SYSCALL_TRACE)
  139. ptrace_report_syscall_exit(regs, step);
  140. }
  141. /*
  142. * Syscall specific exit to user mode preparation. Runs with interrupts
  143. * enabled.
  144. */
  145. static void syscall_exit_to_user_mode_prepare(struct pt_regs *regs)
  146. {
  147. unsigned long work = READ_ONCE(current_thread_info()->syscall_work);
  148. unsigned long nr = syscall_get_nr(current, regs);
  149. CT_WARN_ON(ct_state() != CT_STATE_KERNEL);
  150. if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
  151. if (WARN(irqs_disabled(), "syscall %lu left IRQs disabled", nr))
  152. local_irq_enable();
  153. }
  154. rseq_syscall(regs);
  155. /*
  156. * Do one-time syscall specific work. If these work items are
  157. * enabled, we want to run them exactly once per syscall exit with
  158. * interrupts enabled.
  159. */
  160. if (unlikely(work & SYSCALL_WORK_EXIT))
  161. syscall_exit_work(regs, work);
  162. }
  163. static __always_inline void __syscall_exit_to_user_mode_work(struct pt_regs *regs)
  164. {
  165. syscall_exit_to_user_mode_prepare(regs);
  166. local_irq_disable_exit_to_user();
  167. exit_to_user_mode_prepare(regs);
  168. }
  169. void syscall_exit_to_user_mode_work(struct pt_regs *regs)
  170. {
  171. __syscall_exit_to_user_mode_work(regs);
  172. }
  173. __visible noinstr void syscall_exit_to_user_mode(struct pt_regs *regs)
  174. {
  175. instrumentation_begin();
  176. __syscall_exit_to_user_mode_work(regs);
  177. instrumentation_end();
  178. exit_to_user_mode();
  179. }
  180. noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs)
  181. {
  182. enter_from_user_mode(regs);
  183. }
  184. noinstr void irqentry_exit_to_user_mode(struct pt_regs *regs)
  185. {
  186. instrumentation_begin();
  187. exit_to_user_mode_prepare(regs);
  188. instrumentation_end();
  189. exit_to_user_mode();
  190. }
  191. noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)
  192. {
  193. irqentry_state_t ret = {
  194. .exit_rcu = false,
  195. };
  196. if (user_mode(regs)) {
  197. irqentry_enter_from_user_mode(regs);
  198. return ret;
  199. }
  200. /*
  201. * If this entry hit the idle task invoke ct_irq_enter() whether
  202. * RCU is watching or not.
  203. *
  204. * Interrupts can nest when the first interrupt invokes softirq
  205. * processing on return which enables interrupts.
  206. *
  207. * Scheduler ticks in the idle task can mark quiescent state and
  208. * terminate a grace period, if and only if the timer interrupt is
  209. * not nested into another interrupt.
  210. *
  211. * Checking for rcu_is_watching() here would prevent the nesting
  212. * interrupt to invoke ct_irq_enter(). If that nested interrupt is
  213. * the tick then rcu_flavor_sched_clock_irq() would wrongfully
  214. * assume that it is the first interrupt and eventually claim
  215. * quiescent state and end grace periods prematurely.
  216. *
  217. * Unconditionally invoke ct_irq_enter() so RCU state stays
  218. * consistent.
  219. *
  220. * TINY_RCU does not support EQS, so let the compiler eliminate
  221. * this part when enabled.
  222. */
  223. if (!IS_ENABLED(CONFIG_TINY_RCU) && is_idle_task(current)) {
  224. /*
  225. * If RCU is not watching then the same careful
  226. * sequence vs. lockdep and tracing is required
  227. * as in irqentry_enter_from_user_mode().
  228. */
  229. lockdep_hardirqs_off(CALLER_ADDR0);
  230. ct_irq_enter();
  231. instrumentation_begin();
  232. kmsan_unpoison_entry_regs(regs);
  233. trace_hardirqs_off_finish();
  234. instrumentation_end();
  235. ret.exit_rcu = true;
  236. return ret;
  237. }
  238. /*
  239. * If RCU is watching then RCU only wants to check whether it needs
  240. * to restart the tick in NOHZ mode. rcu_irq_enter_check_tick()
  241. * already contains a warning when RCU is not watching, so no point
  242. * in having another one here.
  243. */
  244. lockdep_hardirqs_off(CALLER_ADDR0);
  245. instrumentation_begin();
  246. kmsan_unpoison_entry_regs(regs);
  247. rcu_irq_enter_check_tick();
  248. trace_hardirqs_off_finish();
  249. instrumentation_end();
  250. return ret;
  251. }
  252. void raw_irqentry_exit_cond_resched(void)
  253. {
  254. if (!preempt_count()) {
  255. /* Sanity check RCU and thread stack */
  256. rcu_irq_exit_check_preempt();
  257. if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
  258. WARN_ON_ONCE(!on_thread_stack());
  259. if (need_resched())
  260. preempt_schedule_irq();
  261. }
  262. }
  263. #ifdef CONFIG_PREEMPT_DYNAMIC
  264. #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
  265. DEFINE_STATIC_CALL(irqentry_exit_cond_resched, raw_irqentry_exit_cond_resched);
  266. #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
  267. DEFINE_STATIC_KEY_TRUE(sk_dynamic_irqentry_exit_cond_resched);
  268. void dynamic_irqentry_exit_cond_resched(void)
  269. {
  270. if (!static_branch_unlikely(&sk_dynamic_irqentry_exit_cond_resched))
  271. return;
  272. raw_irqentry_exit_cond_resched();
  273. }
  274. #endif
  275. #endif
  276. noinstr void irqentry_exit(struct pt_regs *regs, irqentry_state_t state)
  277. {
  278. lockdep_assert_irqs_disabled();
  279. /* Check whether this returns to user mode */
  280. if (user_mode(regs)) {
  281. irqentry_exit_to_user_mode(regs);
  282. } else if (!regs_irqs_disabled(regs)) {
  283. /*
  284. * If RCU was not watching on entry this needs to be done
  285. * carefully and needs the same ordering of lockdep/tracing
  286. * and RCU as the return to user mode path.
  287. */
  288. if (state.exit_rcu) {
  289. instrumentation_begin();
  290. /* Tell the tracer that IRET will enable interrupts */
  291. trace_hardirqs_on_prepare();
  292. lockdep_hardirqs_on_prepare();
  293. instrumentation_end();
  294. ct_irq_exit();
  295. lockdep_hardirqs_on(CALLER_ADDR0);
  296. return;
  297. }
  298. instrumentation_begin();
  299. if (IS_ENABLED(CONFIG_PREEMPTION))
  300. irqentry_exit_cond_resched();
  301. /* Covers both tracing and lockdep */
  302. trace_hardirqs_on();
  303. instrumentation_end();
  304. } else {
  305. /*
  306. * IRQ flags state is correct already. Just tell RCU if it
  307. * was not watching on entry.
  308. */
  309. if (state.exit_rcu)
  310. ct_irq_exit();
  311. }
  312. }
  313. irqentry_state_t noinstr irqentry_nmi_enter(struct pt_regs *regs)
  314. {
  315. irqentry_state_t irq_state;
  316. irq_state.lockdep = lockdep_hardirqs_enabled();
  317. __nmi_enter();
  318. lockdep_hardirqs_off(CALLER_ADDR0);
  319. lockdep_hardirq_enter();
  320. ct_nmi_enter();
  321. instrumentation_begin();
  322. kmsan_unpoison_entry_regs(regs);
  323. trace_hardirqs_off_finish();
  324. ftrace_nmi_enter();
  325. instrumentation_end();
  326. return irq_state;
  327. }
  328. void noinstr irqentry_nmi_exit(struct pt_regs *regs, irqentry_state_t irq_state)
  329. {
  330. instrumentation_begin();
  331. ftrace_nmi_exit();
  332. if (irq_state.lockdep) {
  333. trace_hardirqs_on_prepare();
  334. lockdep_hardirqs_on_prepare();
  335. }
  336. instrumentation_end();
  337. ct_nmi_exit();
  338. lockdep_hardirq_exit();
  339. if (irq_state.lockdep)
  340. lockdep_hardirqs_on(CALLER_ADDR0);
  341. __nmi_exit();
  342. }