Skip to contentSkip to conversation

um: Enable preemption in UML

PATCH In review [email protected] 3 participants 10 messages Apr 3, 2024 – Jul 1, 2024

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH v9 1/1 um: Enable preemption in UML

From: Anton Ivanov <[email protected]>

1. Preemption requires saving/restoring FPU state. This patch adds support for it using GCC intrinsics as well as appropriate storage space in the thread structure. We reuse the space which is already allocated for the userspace threads in the thread_info structure.

2. irq critical sections need preempt_disable()/preempt_enable().

3. TLB critical sections need preempt_disable()/preempt_enable().

4. UML TLB flush is also invoked during a fork. This happens with interrupts and preempt disabled which disagrees with the standard mm locking via rwsem. The mm lock for this code path had to be replaced with an rcu.

---
 arch/um/Kconfig                   |  2 +-
 arch/um/include/asm/fpu/api.h     |  9 ++--
 arch/um/include/asm/thread_info.h |  4 +-
 arch/um/kernel/Makefile           |  4 ++
 arch/um/kernel/fpu.c              | 71 +++++++++++++++++++++++++++++++
 arch/um/kernel/irq.c              |  2 +
 arch/um/kernel/tlb.c              | 13 ++++++
 7 files changed, 99 insertions(+), 6 deletions(-)
 create mode 100644 arch/um/kernel/fpu.c
arch/um/Kconfig +1 -1
index 93a5a8999b07..5b2a8991f6c8 100644
@@ -11,7 +11,7 @@ config UML
 	select ARCH_HAS_KCOV
 	select ARCH_HAS_STRNCPY_FROM_USER
 	select ARCH_HAS_STRNLEN_USER
-	select ARCH_NO_PREEMPT
+	select ARCH_NO_PREEMPT_DYNAMIC
 	select HAVE_ARCH_AUDITSYSCALL
 	select HAVE_ARCH_KASAN if X86_64
 	select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN
arch/um/include/asm/fpu/api.h +6 -3
index 71bfd9ef3938..9e7680bf48f0 100644
@@ -4,12 +4,15 @@
 
 /* Copyright (c) 2020 Cambridge Greys Ltd
  * Copyright (c) 2020 Red Hat Inc.
- * A set of "dummy" defines to allow the direct inclusion
- * of x86 optimized copy, xor, etc routines into the
- * UML code tree. */
+ */
 
+#if defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
+extern void kernel_fpu_begin(void);
+extern void kernel_fpu_end(void);
+#else
 #define kernel_fpu_begin() (void)0
 #define kernel_fpu_end() (void)0
+#endif
 
 static inline bool irq_fpu_usable(void)
 {
arch/um/include/asm/thread_info.h +2 -2
index c7b4b49826a2..8f5f9b51eb75 100644
@@ -23,8 +23,8 @@ struct thread_info {
 	int			preempt_count;  /* 0 => preemptable,
 						   <0 => BUG */
 	struct thread_info	*real_thread;    /* Points to non-IRQ stack */
-	unsigned long aux_fp_regs[FP_SIZE];	/* auxiliary fp_regs to save/restore
-						   them out-of-band */
+	/* auxiliary fp_regs to save/restore them out-of-band */
+	unsigned long aux_fp_regs[FP_SIZE] __aligned(64);
 };
 
 #define INIT_THREAD_INFO(tsk)			\
arch/um/kernel/Makefile +4 -0
index 811188be954c..c616e884a488 100644
@@ -26,9 +26,13 @@ obj-$(CONFIG_OF) += dtb.o
 obj-$(CONFIG_EARLY_PRINTK) += early_printk.o
 obj-$(CONFIG_STACKTRACE) += stacktrace.o
 obj-$(CONFIG_GENERIC_PCI_IOMAP) += ioport.o
+obj-$(CONFIG_PREEMPT) += fpu.o
+obj-$(CONFIG_PREEMPT_VOLUNTARY) += fpu.o
 
 USER_OBJS := config.o
 
+CFLAGS_fpu.o += -mxsave -mxsaveopt
+
 include $(srctree)/arch/um/scripts/Makefile.rules
 
 targets := config.c config.tmp capflags.c
arch/um/kernel/fpu.c new file +71 -0

71 lines not on this page · raw message

arch/um/kernel/irq.c +2 -0
index 635d44606bfe..c02525da45df 100644
@@ -195,7 +195,9 @@ static void _sigio_handler(struct uml_pt_regs *regs,
 
 void sigio_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
 {
+	preempt_disable();
 	_sigio_handler(regs, irqs_suspended);
+	preempt_enable();
 }
 
 static struct irq_entry *get_irq_entry_by_fd(int fd)
arch/um/kernel/tlb.c +13 -0
index 247396b732e5..61c56917361e 100644
@@ -322,6 +322,8 @@ static void fix_range_common(struct mm_struct *mm, unsigned long start_addr,
 	unsigned long addr = start_addr, next;
 	int ret = 0, userspace = 1;
 
+	preempt_disable();
+
 	hvc = INIT_HVC(mm, force, userspace);
 	pgd = pgd_offset(mm, addr);
 	do {
@@ -346,6 +348,7 @@ static void fix_range_common(struct mm_struct *mm, unsigned long start_addr,
 		       "process: %d\n", task_tgid_vnr(current));
 		mm_idp->kill = 1;
 	}
+	preempt_enable();
 }
 
 static int flush_tlb_kernel_range_common(unsigned long start, unsigned long end)
@@ -362,6 +365,9 @@ static int flush_tlb_kernel_range_common(unsigned long start, unsigned long end)
 
 	mm = &init_mm;
 	hvc = INIT_HVC(mm, force, userspace);
+
+	preempt_disable();
+
 	for (addr = start; addr < end;) {
 		pgd = pgd_offset(mm, addr);
 		if (!pgd_present(*pgd)) {
@@ -449,6 +455,9 @@ static int flush_tlb_kernel_range_common(unsigned long start, unsigned long end)
 
 	if (err < 0)
 		panic("flush_tlb_kernel failed, errno = %d\n", err);
+
+	preempt_enable();
+
 	return updated;
 }
 
@@ -466,6 +475,8 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long address)
 
 	address &= PAGE_MASK;
 
+	preempt_disable();
+
 	pgd = pgd_offset(mm, address);
 	if (!pgd_present(*pgd))
 		goto kill;
@@ -520,9 +531,11 @@ void flush_tlb_page(struct vm_area_struct *vma, unsigned long address)
 
 	*pte = pte_mkuptodate(*pte);
 
+	preempt_enable();
 	return;
 
 kill:
+	preempt_enable();
 	printk(KERN_ERR "Failed to flush page for address 0x%lx\n", address);
 	force_sig(SIGKILL);
 }
Signature · 1 line
2.39.2
#1 lore raw

Hi,

Quoted 7 lines · On Wed, 2024-04-03 at 07:27 +0100, [email protected] wrote:

From: Anton Ivanov <[email protected]>

1. Preemption requires saving/restoring FPU state. This patch adds support for it using GCC intrinsics as well as appropriate storage space in the thread structure. We reuse the space which is already allocated for the userspace threads in the thread_info structure.

Do we need to store the FPU state even? After all, we cannot simply overwrite userspace FPU registers in UML.

Seriously wondering about that. It seems to me it would be sufficient to ensure that only one kernel task is in a kern_fpu_begin()/kern_fpu_end() section at any point in time.

Now, I don't know how preempt_disable()/preempt_enable() behaves exactly. But I would assume it makes such a guarantee and then we can simply map kern_fpu_begin to preempt_disable and kern_fpu_end to preempt_enable.

Quoted 8 lines

2. irq critical sections need preempt_disable()/preempt_enable().

3. TLB critical sections need preempt_disable()/preempt_enable().

4. UML TLB flush is also invoked during a fork. This happens with interrupts and preempt disabled which disagrees with the standard mm locking via rwsem. The mm lock for this code path had to be replaced with an rcu.

Hopefully that flush during fork will be gone soon :-)

Benjamin

Quoted 1 line

[...]

author maintainer
#2 lore raw
Quoted 6 lines · Benjamin Berg

Hi,

Quoted 2 lines · On Wed, 2024-04-03 at 07:27 +0100, [email protected] wrote:

From: Anton Ivanov <[email protected]>

Quoted 7 lines
Quoted 5 lines

1. Preemption requires saving/restoring FPU state. This patch adds support for it using GCC intrinsics as well as appropriate storage space in the thread structure. We reuse the space which is already allocated for the userspace threads in the thread_info structure.

Do we need to store the FPU state even?

Everybody else does :)

Quoted 2 lines

After all, we cannot simply
overwrite userspace FPU registers in UML.

Correct, but you can have kernel threads as well.

Quoted 4 lines

Seriously wondering about that. It seems to me it would be sufficient to ensure that only one kernel task is in a kern_fpu_begin()/kern_fpu_end() section at any point in time.

So what happens if you have a task which wants fpu and cannot get it at this point?

Quoted 7 lines

Now, I don't know how preempt_disable()/preempt_enable() behaves exactly. But I would assume it makes such a guarantee and then we can simply map kern_fpu_begin to preempt_disable and kern_fpu_end to preempt_enable.

Quoted 1 line

2. irq critical sections need preempt_disable()/preempt_enable().

Yes. Otherwise RAID, crypto, ipsec can mess up things.

Quoted 2 lines
Quoted 2 lines

3. TLB critical sections need preempt_disable()/preempt_enable().

I think I added those.

Quoted 7 lines
Quoted 5 lines

4. UML TLB flush is also invoked during a fork. This happens with interrupts and preempt disabled which disagrees with the standard mm locking via rwsem. The mm lock for this code path had to be replaced with an rcu.

Hopefully that flush during fork will be gone soon :-)

Hurrah!!!

Quoted 6 lines

Benjamin

Quoted 1 line

[...]

Signature · 3 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/
#3 lore raw

Hi,

Quoted 13 lines · Anton Ivanov
Quoted 12 lines · Benjamin Berg

Hi,

Quoted 7 lines · On Wed, 2024-04-03 at 07:27 +0100, [email protected] wrote:

From: Anton Ivanov <[email protected]>

1. Preemption requires saving/restoring FPU state. This patch adds support for it using GCC intrinsics as well as appropriate storage space in the thread structure. We reuse the space which is already allocated for the userspace threads in the thread_info structure.

Quoted 3 lines
Quoted 1 line

Do we need to store the FPU state even?

Everybody else does :)

As far as I can tell, not really.

The way I understand the x86 codeis that it tries to leave the FPU untouched in the hope that it will return to the same userspace task. However, if the kernel starts using the FPU, then it saves the FPU registers into the current task struct and sets a flag that the FPU state needs to be restored when returning to userspace. See TIF_NEED_FPU_LOAD, switch_fpu_prepare and kernel_fpu_begin_mask.

We have multiple things confirming this for me:
* storing only happens if "current" is not a KTHREAD or USER_WORKER (i.e. proper userspace, we e.g. in a syscall, not a kernel thread)
* we set TIF_NEED_FPU_LOAD, which is documented with "load FPU on return to userspace"
* the in_kernel_fpu per-CPU boolean is set and must be unset later
* __schedule must only be called with preemption disabled
* switch_fpu_prepare does not save FPU registers for KTHREAD or USER_WORKER either

So, for me, all the effort is purely an optimization for the common case that a userspace process is switching into kernel space for a syscall (or pagefault), and we are coming back to the same process afterwards without scheduling.
In that case, we can just leave the entire FPU registers untouched and everything is great.

But, that case is irrelevant for us, because we always store the FPU registers of userspace processes anyway (equivalent to just setting the TIF_NEED_FPU_LOAD flag). And, for kernel tasks we never do that, but neither does x86.

So, yeah, the more I look at it, the more certain I am that we should basically just do:

#define kernel_fpu_begin preempt_disable
#define kernel_fpu_end preempt_enable

And, of course, add the preempt_* in various locations as you already have done.

Benjamin

Quoted 48 lines
Quoted 3 lines

After all, we cannot simply
overwrite userspace FPU registers in UML.

Correct, but you can have kernel threads as well.

Quoted 5 lines

Seriously wondering about that. It seems to me it would be sufficient
to ensure that only one kernel task is in a kern_fpu_begin()/kern_fpu_end() section at any point in time.

So what happens if you have a task which wants fpu and cannot get it at
this point?

Quoted 8 lines

Now, I don't know how preempt_disable()/preempt_enable() behaves exactly. But I would assume it makes such a guarantee and then we can
simply map kern_fpu_begin to preempt_disable and kern_fpu_end to preempt_enable.

Quoted 1 line

2. irq critical sections need preempt_disable()/preempt_enable().

Yes. Otherwise RAID, crypto, ipsec can mess up things.

Quoted 2 lines
Quoted 2 lines

3. TLB critical sections need preempt_disable()/preempt_enable().

I think I added those.

Quoted 7 lines
Quoted 5 lines

4. UML TLB flush is also invoked during a fork. This happens with interrupts and preempt disabled which disagrees with the standard mm locking via rwsem. The mm lock for this code path had to be replaced with an rcu.

Hopefully that flush during fork will be gone soon :-)

Hurrah!!!

Quoted 6 lines

Benjamin

Quoted 1 line

[...]

maintainer
#4 lore raw
In reply to [email protected]
Quoted 8 lines
 arch/um/Kconfig                   |  2 +-
 arch/um/include/asm/fpu/api.h     |  9 ++--
 arch/um/include/asm/thread_info.h |  4 +-
 arch/um/kernel/Makefile           |  4 ++
 arch/um/kernel/fpu.c              | 71 +++++++++++++++++++++++++++++++
 arch/um/kernel/irq.c              |  2 +
 arch/um/kernel/tlb.c              | 13 ++++++

In addition to what Benjamin pointed out (FPU state is in the MM process, so we probably don't care about save/restore), this also doesn't build any more, at least it needs:

arch/x86/um/Makefile +0 -1
@@ -31,7 +31,6 @@ obj-y += syscalls_64.o vdso/
 
 subarch-y = ../lib/csum-partial_64.o ../lib/memcpy_64.o \
        ../lib/memmove_64.o ../lib/memset_64.o
-subarch-$(CONFIG_PREEMPTION) += ../entry/thunk_64.o
 
 endif
 

git am also complained about the extra blank line at EOF here:

Review

Quoted 2 lines
(quoted diff) +2 -0
+EXPORT_SYMBOL_GPL(kernel_fpu_end);
+

johannes

maintainer
#5 lore raw

Hmm. I also see a ton of this:

BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:1525

in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 538, name: chan-switch.sh
preempt_count: 2, expected: 0
RCU nest depth: 0, expected: 0
no locks held by chan-switch.sh/538.

irq event stamp: 0
hardirqs last  enabled at (0): [<0000000000000000>] 0x0
hardirqs last disabled at (0): [<000000006004b961>] copy_process+0xbb7/0x2790
softirqs last  enabled at (0): [<000000006004b961>] copy_process+0xbb7/0x2790
softirqs last disabled at (0): [<0000000000000000>] 0x0
Preemption disabled at:
[<0000000060084b04>] preempt_count_add+0x11f/0x126
CPU: 0 PID: 538 Comm: chan-switch.sh Tainted: G        W  O       6.10.0-rc2 #11
Stack:
 714c7e40 605f2a9d 00000000 00000000
 ffffff00 607d2edc 00000000 60db7a48
 714c7e70 60620823 0000022c 606161d1
Call Trace:
 [<606161d1>] ? _printk+0x0/0x94
 [<6002fa2f>] show_stack+0xfe/0x159
 [<605f2a9d>] ? dump_stack_print_info+0xe1/0xf0
 [<60620823>] dump_stack_lvl+0x77/0xbe
 [<606161d1>] ? _printk+0x0/0x94
 [<60084b04>] ? preempt_count_add+0x11f/0x126
 [<60620884>] dump_stack+0x1a/0x1c
 [<60085b82>] __might_resched+0x3b0/0x3d2
 [<60085ca0>] __might_sleep+0xfc/0x107
 [<60627212>] down_read+0x34/0x224
 [<60032256>] force_flush_all+0x71/0x10d
 [<6002e95d>] fork_handler+0x14/0x94

johannes

maintainer
#6 lore raw
Quoted 23 lines · Johannes Berg

Hmm. I also see a ton of this:

BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:1525

in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 538, name: chan-switch.sh
preempt_count: 2, expected: 0
RCU nest depth: 0, expected: 0
no locks held by chan-switch.sh/538.

irq event stamp: 0
hardirqs last  enabled at (0): [<0000000000000000>] 0x0
hardirqs last disabled at (0): [<000000006004b961>] copy_process+0xbb7/0x2790
softirqs last  enabled at (0): [<000000006004b961>] copy_process+0xbb7/0x2790
softirqs last disabled at (0): [<0000000000000000>] 0x0
Preemption disabled at:
[<0000000060084b04>] preempt_count_add+0x11f/0x126
CPU: 0 PID: 538 Comm: chan-switch.sh Tainted: G        W  O       6.10.0-rc2 #11
Stack:
 714c7e40 605f2a9d 00000000 00000000
 ffffff00 607d2edc 00000000 60db7a48
 714c7e70 60620823 0000022c 606161d1
Call Trace:
 [<606161d1>] ? _printk+0x0/0x94
 [<6002fa2f>] show_stack+0xfe/0x159
 [<605f2a9d>] ? dump_stack_print_info+0xe1/0xf0
Quoted 8 lines
 [<60620823>] dump_stack_lvl+0x77/0xbe
 [<606161d1>] ? _printk+0x0/0x94
 [<60084b04>] ? preempt_count_add+0x11f/0x126
 [<60620884>] dump_stack+0x1a/0x1c
 [<60085b82>] __might_resched+0x3b0/0x3d2
 [<60085ca0>] __might_sleep+0xfc/0x107
 [<60627212>] down_read+0x34/0x224
 [<60032256>] force_flush_all+0x71/0x10d

Which is, however, addressed by my own patchset

https://patchwork.ozlabs.org/project/linux-um/list/?series=374492

though it doesn't apply completely cleanly any more.

johannes

author maintainer
#7 lore raw
Quoted 33 lines · Johannes Berg
Quoted 23 lines · Johannes Berg

Hmm. I also see a ton of this:

BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:1525

in_atomic(): 1, irqs_disabled(): 1, non_block: 0, pid: 538, name: chan-switch.sh
preempt_count: 2, expected: 0
RCU nest depth: 0, expected: 0
no locks held by chan-switch.sh/538.

irq event stamp: 0
hardirqs last  enabled at (0): [<0000000000000000>] 0x0
hardirqs last disabled at (0): [<000000006004b961>] copy_process+0xbb7/0x2790
softirqs last  enabled at (0): [<000000006004b961>] copy_process+0xbb7/0x2790
softirqs last disabled at (0): [<0000000000000000>] 0x0
Preemption disabled at:
[<0000000060084b04>] preempt_count_add+0x11f/0x126
CPU: 0 PID: 538 Comm: chan-switch.sh Tainted: G        W  O       6.10.0-rc2 #11
Stack:
  714c7e40 605f2a9d 00000000 00000000
  ffffff00 607d2edc 00000000 60db7a48
  714c7e70 60620823 0000022c 606161d1
Call Trace:
  [<606161d1>] ? _printk+0x0/0x94
  [<6002fa2f>] show_stack+0xfe/0x159
  [<605f2a9d>] ? dump_stack_print_info+0xe1/0xf0
Quoted 8 lines
  [<60620823>] dump_stack_lvl+0x77/0xbe
  [<606161d1>] ? _printk+0x0/0x94
  [<60084b04>] ? preempt_count_add+0x11f/0x126
  [<60620884>] dump_stack+0x1a/0x1c
  [<60085b82>] __might_resched+0x3b0/0x3d2
  [<60085ca0>] __might_sleep+0xfc/0x107
  [<60627212>] down_read+0x34/0x224
  [<60032256>] force_flush_all+0x71/0x10d
Quoted 8 lines

Which is, however, addressed by my own patchset

https://patchwork.ozlabs.org/project/linux-um/list/?series=374492

though it doesn't apply completely cleanly any more.

johannes

As Benjamin pointed out we no longer need this, so we can junk this patch at this point. If there will be any need to save/restore FPU in kernel context, I will revisit it.

Signature · 3 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/
maintainer
#8 lore raw
Quoted 4 lines · Anton Ivanov
Quoted 1 line

As Benjamin pointed out we no longer need this, so we can junk this patch at this point. If there will be any need to save/restore FPU in kernel context, I will revisit it.

I think we still want/need the parts with the preempt_disable/enable, and Kconfig though?

johannes

author maintainer
#9 lore raw
Quoted 6 lines · Johannes Berg
Quoted 3 lines · Anton Ivanov

As Benjamin pointed out we no longer need this, so we can junk this patch at this point. If there will be any need to save/restore FPU in kernel context, I will revisit it.

I think we still want/need the parts with the preempt_disable/enable, and Kconfig though?

Good point. I can do a cut down version.

Quoted 4 lines

johannes

Signature · 3 lines
Anton R. Ivanov
Cambridgegreys Limited. Registered in England. Company Number 10273661
https://www.cambridgegreys.com/