[v2,1/3] entry: move exit to usermode functions to header file
Commit Message
To allow inlining, move exit_to_user_mode() to
entry-common.h.
Signed-off-by: Sven Schnelle <svens@linux.ibm.com>
---
include/linux/entry-common.h | 52 +++++++++++++++++++++++++++++++++++-
kernel/entry/common.c | 50 +++++-----------------------------
2 files changed, 58 insertions(+), 44 deletions(-)
Comments
Hi Sven,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/core/entry]
[also build test WARNING on linus/master v6.7-rc6 next-20231218]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Sven-Schnelle/entry-move-exit-to-usermode-functions-to-header-file/20231218-154733
base: tip/core/entry
patch link: https://lore.kernel.org/r/20231218074520.1998026-2-svens%40linux.ibm.com
patch subject: [PATCH v2 1/3] entry: move exit to usermode functions to header file
config: x86_64-randconfig-161-20231218 (https://download.01.org/0day-ci/archive/20231219/202312190205.LbtmWWZN-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231219/202312190205.LbtmWWZN-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312190205.LbtmWWZN-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> kernel/entry/common.c:134: warning: Function parameter or member 'regs' not described in 'exit_to_user_mode_loop'
>> kernel/entry/common.c:134: warning: Function parameter or member 'ti_work' not described in 'exit_to_user_mode_loop'
vim +134 kernel/entry/common.c
a9f3a74a29af09 Thomas Gleixner 2020-07-22 128
af18f155cb4bda Sven Schnelle 2023-12-18 129 /**
af18f155cb4bda Sven Schnelle 2023-12-18 130 * exit_to_user_mode_loop - do any pending work before leaving to user space
af18f155cb4bda Sven Schnelle 2023-12-18 131 */
af18f155cb4bda Sven Schnelle 2023-12-18 132 __always_inline unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
a9f3a74a29af09 Thomas Gleixner 2020-07-22 133 unsigned long ti_work)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 @134 {
a9f3a74a29af09 Thomas Gleixner 2020-07-22 135 /*
a9f3a74a29af09 Thomas Gleixner 2020-07-22 136 * Before returning to user space ensure that all pending work
a9f3a74a29af09 Thomas Gleixner 2020-07-22 137 * items have been completed.
a9f3a74a29af09 Thomas Gleixner 2020-07-22 138 */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 139 while (ti_work & EXIT_TO_USER_MODE_WORK) {
a9f3a74a29af09 Thomas Gleixner 2020-07-22 140
a9f3a74a29af09 Thomas Gleixner 2020-07-22 141 local_irq_enable_exit_to_user(ti_work);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 142
a9f3a74a29af09 Thomas Gleixner 2020-07-22 143 if (ti_work & _TIF_NEED_RESCHED)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 144 schedule();
a9f3a74a29af09 Thomas Gleixner 2020-07-22 145
a9f3a74a29af09 Thomas Gleixner 2020-07-22 146 if (ti_work & _TIF_UPROBE)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 147 uprobe_notify_resume(regs);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 148
a9f3a74a29af09 Thomas Gleixner 2020-07-22 149 if (ti_work & _TIF_PATCH_PENDING)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 150 klp_update_patch_state(current);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 151
12db8b690010cc Jens Axboe 2020-10-26 152 if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
8ba62d37949e24 Eric W. Biederman 2022-02-09 153 arch_do_signal_or_restart(regs);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 154
a68de80f61f6af Sean Christopherson 2021-09-01 155 if (ti_work & _TIF_NOTIFY_RESUME)
03248addadf1a5 Eric W. Biederman 2022-02-09 156 resume_user_mode_work(regs);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 157
a9f3a74a29af09 Thomas Gleixner 2020-07-22 158 /* Architecture specific TIF work */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 159 arch_exit_to_user_mode_work(regs, ti_work);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 160
a9f3a74a29af09 Thomas Gleixner 2020-07-22 161 /*
a9f3a74a29af09 Thomas Gleixner 2020-07-22 162 * Disable interrupts and reevaluate the work flags as they
a9f3a74a29af09 Thomas Gleixner 2020-07-22 163 * might have changed while interrupts and preemption was
a9f3a74a29af09 Thomas Gleixner 2020-07-22 164 * enabled above.
a9f3a74a29af09 Thomas Gleixner 2020-07-22 165 */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 166 local_irq_disable_exit_to_user();
47b8ff194c1fd7 Frederic Weisbecker 2021-02-01 167
47b8ff194c1fd7 Frederic Weisbecker 2021-02-01 168 /* Check if any of the above work has queued a deferred wakeup */
f268c3737ecaef Frederic Weisbecker 2021-05-27 169 tick_nohz_user_enter_prepare();
47b8ff194c1fd7 Frederic Weisbecker 2021-02-01 170
6ce895128b3bff Mark Rutland 2021-11-29 171 ti_work = read_thread_flags();
a9f3a74a29af09 Thomas Gleixner 2020-07-22 172 }
a9f3a74a29af09 Thomas Gleixner 2020-07-22 173
a9f3a74a29af09 Thomas Gleixner 2020-07-22 174 /* Return the latest work state for arch_exit_to_user_mode() */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 175 return ti_work;
a9f3a74a29af09 Thomas Gleixner 2020-07-22 176 }
a9f3a74a29af09 Thomas Gleixner 2020-07-22 177
Hi Sven,
kernel test robot noticed the following build warnings:
[auto build test WARNING on tip/core/entry]
[also build test WARNING on linus/master v6.7-rc6 next-20231220]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Sven-Schnelle/entry-move-exit-to-usermode-functions-to-header-file/20231218-154733
base: tip/core/entry
patch link: https://lore.kernel.org/r/20231218074520.1998026-2-svens%40linux.ibm.com
patch subject: [PATCH v2 1/3] entry: move exit to usermode functions to header file
config: x86_64-rhel-8.3-kunit (https://download.01.org/0day-ci/archive/20231221/202312210535.lQDyVDDW-lkp@intel.com/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231221/202312210535.lQDyVDDW-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202312210535.lQDyVDDW-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> kernel/entry/common.c:134: warning: Function parameter or struct member 'regs' not described in 'exit_to_user_mode_loop'
>> kernel/entry/common.c:134: warning: Function parameter or struct member 'ti_work' not described in 'exit_to_user_mode_loop'
vim +134 kernel/entry/common.c
a9f3a74a29af09 Thomas Gleixner 2020-07-22 128
af18f155cb4bda Sven Schnelle 2023-12-18 129 /**
af18f155cb4bda Sven Schnelle 2023-12-18 130 * exit_to_user_mode_loop - do any pending work before leaving to user space
af18f155cb4bda Sven Schnelle 2023-12-18 131 */
af18f155cb4bda Sven Schnelle 2023-12-18 132 __always_inline unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
a9f3a74a29af09 Thomas Gleixner 2020-07-22 133 unsigned long ti_work)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 @134 {
a9f3a74a29af09 Thomas Gleixner 2020-07-22 135 /*
a9f3a74a29af09 Thomas Gleixner 2020-07-22 136 * Before returning to user space ensure that all pending work
a9f3a74a29af09 Thomas Gleixner 2020-07-22 137 * items have been completed.
a9f3a74a29af09 Thomas Gleixner 2020-07-22 138 */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 139 while (ti_work & EXIT_TO_USER_MODE_WORK) {
a9f3a74a29af09 Thomas Gleixner 2020-07-22 140
a9f3a74a29af09 Thomas Gleixner 2020-07-22 141 local_irq_enable_exit_to_user(ti_work);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 142
a9f3a74a29af09 Thomas Gleixner 2020-07-22 143 if (ti_work & _TIF_NEED_RESCHED)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 144 schedule();
a9f3a74a29af09 Thomas Gleixner 2020-07-22 145
a9f3a74a29af09 Thomas Gleixner 2020-07-22 146 if (ti_work & _TIF_UPROBE)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 147 uprobe_notify_resume(regs);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 148
a9f3a74a29af09 Thomas Gleixner 2020-07-22 149 if (ti_work & _TIF_PATCH_PENDING)
a9f3a74a29af09 Thomas Gleixner 2020-07-22 150 klp_update_patch_state(current);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 151
12db8b690010cc Jens Axboe 2020-10-26 152 if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
8ba62d37949e24 Eric W. Biederman 2022-02-09 153 arch_do_signal_or_restart(regs);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 154
a68de80f61f6af Sean Christopherson 2021-09-01 155 if (ti_work & _TIF_NOTIFY_RESUME)
03248addadf1a5 Eric W. Biederman 2022-02-09 156 resume_user_mode_work(regs);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 157
a9f3a74a29af09 Thomas Gleixner 2020-07-22 158 /* Architecture specific TIF work */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 159 arch_exit_to_user_mode_work(regs, ti_work);
a9f3a74a29af09 Thomas Gleixner 2020-07-22 160
a9f3a74a29af09 Thomas Gleixner 2020-07-22 161 /*
a9f3a74a29af09 Thomas Gleixner 2020-07-22 162 * Disable interrupts and reevaluate the work flags as they
a9f3a74a29af09 Thomas Gleixner 2020-07-22 163 * might have changed while interrupts and preemption was
a9f3a74a29af09 Thomas Gleixner 2020-07-22 164 * enabled above.
a9f3a74a29af09 Thomas Gleixner 2020-07-22 165 */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 166 local_irq_disable_exit_to_user();
47b8ff194c1fd7 Frederic Weisbecker 2021-02-01 167
47b8ff194c1fd7 Frederic Weisbecker 2021-02-01 168 /* Check if any of the above work has queued a deferred wakeup */
f268c3737ecaef Frederic Weisbecker 2021-05-27 169 tick_nohz_user_enter_prepare();
47b8ff194c1fd7 Frederic Weisbecker 2021-02-01 170
6ce895128b3bff Mark Rutland 2021-11-29 171 ti_work = read_thread_flags();
a9f3a74a29af09 Thomas Gleixner 2020-07-22 172 }
a9f3a74a29af09 Thomas Gleixner 2020-07-22 173
a9f3a74a29af09 Thomas Gleixner 2020-07-22 174 /* Return the latest work state for arch_exit_to_user_mode() */
a9f3a74a29af09 Thomas Gleixner 2020-07-22 175 return ti_work;
a9f3a74a29af09 Thomas Gleixner 2020-07-22 176 }
a9f3a74a29af09 Thomas Gleixner 2020-07-22 177
@@ -7,6 +7,10 @@
#include <linux/syscalls.h>
#include <linux/seccomp.h>
#include <linux/sched.h>
+#include <linux/context_tracking.h>
+#include <linux/livepatch.h>
+#include <linux/resume_user_mode.h>
+#include <linux/tick.h>
#include <asm/entry-common.h>
@@ -258,6 +262,42 @@ static __always_inline void arch_exit_to_user_mode(void) { }
*/
void arch_do_signal_or_restart(struct pt_regs *regs);
+/**
+ * exit_to_user_mode_loop - do any pending work before leaving to user space
+ */
+unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
+ unsigned long ti_work);
+
+/**
+ * exit_to_user_mode_prepare - call exit_to_user_mode_loop() if required
+ *
+ * 1) check that interrupts are disabled
+ * 2) call tick_nohz_user_enter_prepare()
+ * 3) call exit_to_user_mode_loop() if any flags from
+ * EXIT_TO_USER_MODE_WORK are set
+ * 4) check that interrupts are still disabled
+ */
+static __always_inline void exit_to_user_mode_prepare(struct pt_regs *regs)
+{
+ unsigned long ti_work;
+
+ lockdep_assert_irqs_disabled();
+
+ /* Flush pending rcuog wakeup before the last need_resched() check */
+ tick_nohz_user_enter_prepare();
+
+ ti_work = read_thread_flags();
+ if (unlikely(ti_work & EXIT_TO_USER_MODE_WORK))
+ ti_work = exit_to_user_mode_loop(regs, ti_work);
+
+ arch_exit_to_user_mode_prepare(regs, ti_work);
+
+ /* Ensure that kernel state is sane for a return to userspace */
+ kmap_assert_nomap();
+ lockdep_assert_irqs_disabled();
+ lockdep_sys_exit();
+}
+
/**
* exit_to_user_mode - Fixup state when exiting to user mode
*
@@ -276,7 +316,17 @@ void arch_do_signal_or_restart(struct pt_regs *regs);
* non-instrumentable.
* The caller has to invoke syscall_exit_to_user_mode_work() before this.
*/
-void exit_to_user_mode(void);
+static __always_inline void exit_to_user_mode(void)
+{
+ instrumentation_begin();
+ trace_hardirqs_on_prepare();
+ lockdep_hardirqs_on_prepare();
+ instrumentation_end();
+
+ user_enter_irqoff();
+ arch_exit_to_user_mode();
+ lockdep_hardirqs_on(CALLER_ADDR0);
+}
/**
* syscall_exit_to_user_mode_work - Handle work before returning to user mode
@@ -123,29 +123,14 @@ noinstr void syscall_enter_from_user_mode_prepare(struct pt_regs *regs)
instrumentation_end();
}
-/* See comment for exit_to_user_mode() in entry-common.h */
-static __always_inline void __exit_to_user_mode(void)
-{
- instrumentation_begin();
- trace_hardirqs_on_prepare();
- lockdep_hardirqs_on_prepare();
- instrumentation_end();
-
- user_enter_irqoff();
- arch_exit_to_user_mode();
- lockdep_hardirqs_on(CALLER_ADDR0);
-}
-
-void noinstr exit_to_user_mode(void)
-{
- __exit_to_user_mode();
-}
-
/* Workaround to allow gradual conversion of architecture code */
void __weak arch_do_signal_or_restart(struct pt_regs *regs) { }
-static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
- unsigned long ti_work)
+/**
+ * exit_to_user_mode_loop - do any pending work before leaving to user space
+ */
+__always_inline unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
+ unsigned long ti_work)
{
/*
* Before returning to user space ensure that all pending work
@@ -190,27 +175,6 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
return ti_work;
}
-static void exit_to_user_mode_prepare(struct pt_regs *regs)
-{
- unsigned long ti_work;
-
- lockdep_assert_irqs_disabled();
-
- /* Flush pending rcuog wakeup before the last need_resched() check */
- tick_nohz_user_enter_prepare();
-
- ti_work = read_thread_flags();
- if (unlikely(ti_work & EXIT_TO_USER_MODE_WORK))
- ti_work = exit_to_user_mode_loop(regs, ti_work);
-
- arch_exit_to_user_mode_prepare(regs, ti_work);
-
- /* Ensure that kernel state is sane for a return to userspace */
- kmap_assert_nomap();
- lockdep_assert_irqs_disabled();
- lockdep_sys_exit();
-}
-
/*
* If SYSCALL_EMU is set, then the only reason to report is when
* SINGLESTEP is set (i.e. PTRACE_SYSEMU_SINGLESTEP). This syscall
@@ -295,7 +259,7 @@ __visible noinstr void syscall_exit_to_user_mode(struct pt_regs *regs)
instrumentation_begin();
__syscall_exit_to_user_mode_work(regs);
instrumentation_end();
- __exit_to_user_mode();
+ exit_to_user_mode();
}
noinstr void irqentry_enter_from_user_mode(struct pt_regs *regs)
@@ -308,7 +272,7 @@ noinstr void irqentry_exit_to_user_mode(struct pt_regs *regs)
instrumentation_begin();
exit_to_user_mode_prepare(regs);
instrumentation_end();
- __exit_to_user_mode();
+ exit_to_user_mode();
}
noinstr irqentry_state_t irqentry_enter(struct pt_regs *regs)