Skip to contentSkip to conversation

um: move thread info into task

PATCH In review Benjamin Berg 3 participants 4 messages Nov 3, 2024 – Nov 5, 2024 User-mode Linux (UML)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 um: move thread info into task

From: Benjamin Berg <[email protected]>

This selects the THREAD_INFO_IN_TASK option for UM and changes the way that the current task is discovered. This is trivial though, as UML already tracks the current task in cpu_tasks[] and this can be used to retrieve it.

Also remove the signal handler code that copies the thread information into the IRQ stack. It is obsolete now, which also means that the mentioned race condition cannot happen anymore.

---
 arch/um/Kconfig                   |   1 +
 arch/um/include/asm/Kbuild        |   1 -
 arch/um/include/asm/current.h     |  24 +++++++
 arch/um/include/asm/thread_info.h |  16 -----
 arch/um/kernel/dyn.lds.S          |   2 -
 arch/um/kernel/irq.c              | 112 ------------------------------
 arch/um/kernel/process.c          |   1 +
 arch/um/kernel/skas/process.c     |   4 +-
 arch/um/kernel/um_arch.c          |   7 +-
 arch/um/kernel/uml.lds.S          |   2 -
 arch/um/os-Linux/signal.c         |  39 +----------
 11 files changed, 33 insertions(+), 176 deletions(-)
 create mode 100644 arch/um/include/asm/current.h
arch/um/Kconfig +1 -0
index a9876bdb5bf9..18051b1cfce0 100644
@@ -34,6 +34,7 @@ config UML
 	select HAVE_RUST
 	select ARCH_HAS_UBSAN
 	select HAVE_ARCH_TRACEHOOK
+	select THREAD_INFO_IN_TASK
 
 config MMU
 	bool
arch/um/include/asm/Kbuild +0 -1
index 18f902da8e99..428f2c5158c2 100644
@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 generic-y += bug.h
 generic-y += compat.h
-generic-y += current.h
 generic-y += device.h
 generic-y += dma-mapping.h
 generic-y += emergency-restart.h
arch/um/include/asm/current.h new file +24 -0
new file mode 100644
index 000000000000..93ee89e56372
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_CURRENT_H
+#define __ASM_CURRENT_H
+
+#include <linux/compiler.h>
+
+#ifndef __ASSEMBLY__
+
+#include <as-layout.h>
+
+struct task_struct;
+
+static __always_inline struct task_struct *get_current(void)
+{
+	return (struct task_struct *)cpu_tasks[0].task;
+}
+
+
+#define current get_current()
+
+#endif /* __ASSEMBLY__ */
+
+#endif /* __ASM_CURRENT_H */
+
arch/um/include/asm/thread_info.h +0 -16
index 4d2a768246bc..f9ad06fcc991 100644
@@ -17,33 +17,17 @@
 #include <sysdep/ptrace_user.h>
 
 struct thread_info {
-	struct task_struct	*task;		/* main task structure */
 	unsigned long		flags;		/* low level flags */
 	__u32			cpu;		/* current CPU */
 	int			preempt_count;  /* 0 => preemptable,
 						   <0 => BUG */
-	struct thread_info	*real_thread;    /* Points to non-IRQ stack */
 };
 
 #define INIT_THREAD_INFO(tsk)			\
 {						\
-	.task =		&tsk,			\
 	.flags =		0,		\
 	.cpu =		0,			\
 	.preempt_count = INIT_PREEMPT_COUNT,	\
-	.real_thread = NULL,			\
-}
-
-/* how to get the thread information struct from C */
-static inline struct thread_info *current_thread_info(void)
-{
-	struct thread_info *ti;
-	unsigned long mask = THREAD_SIZE - 1;
-	void *p;
-
-	asm volatile ("" : "=r" (p) : "0" (&ti));
-	ti = (struct thread_info *) (((unsigned long)p) & ~mask);
-	return ti;
 }
 
 #endif
arch/um/kernel/dyn.lds.S +0 -2
index dc9d9a68af55..a36b7918a011 100644
@@ -116,8 +116,6 @@ SECTIONS
   .fini_array     : { *(.fini_array) }
   .data           : {
     INIT_TASK_DATA(KERNEL_STACK_SIZE)
-    . = ALIGN(KERNEL_STACK_SIZE);
-    *(.data..init_irqstack)
     DATA_DATA
     *(.data.* .gnu.linkonce.d.*)
     SORT(CONSTRUCTORS)
arch/um/kernel/irq.c +0 -112

115 lines not on this page · raw message

arch/um/kernel/process.c +1 -0
index 56e7e525fc91..cd973ffe5e68 100644
@@ -44,6 +44,7 @@
  * entry.
  */
 struct cpu_task cpu_tasks[NR_CPUS] = { [0 ... NR_CPUS - 1] = { NULL } };
+EXPORT_SYMBOL(cpu_tasks);
 
 void free_stack(unsigned long stack, int order)
 {
arch/um/kernel/skas/process.c +1 -3
index 68657988c8d1..3f379351634c 100644
@@ -22,15 +22,13 @@ static int __init start_kernel_proc(void *unused)
 {
 	block_signals_trace();
 
-	cpu_tasks[0].task = current;
-
 	start_kernel();
 	return 0;
 }
 
 extern int userspace_pid[];
 
-extern char cpu0_irqstack[];
+char cpu0_irqstack[THREAD_SIZE] __aligned(THREAD_SIZE);
 
 int __init start_uml(void)
 {
arch/um/kernel/um_arch.c +3 -4
index ec17576ce9fc..4c230ab9892f 100644
@@ -65,9 +65,6 @@ struct cpuinfo_um boot_cpu_data = {
 
 EXPORT_SYMBOL(boot_cpu_data);
 
-union thread_union cpu0_irqstack
-	__section(".data..init_irqstack") =
-		{ .thread_info = INIT_THREAD_INFO(init_task) };
 
 /* Changed in setup_arch, which is called in early boot */
 static char host_info[(__NEW_UTS_LEN + 1) * 5];
@@ -244,6 +241,8 @@ static struct notifier_block panic_exit_notifier = {
 
 void uml_finishsetup(void)
 {
+	cpu_tasks[0].task = &init_task;
+
 	atomic_notifier_chain_register(&panic_notifier_list,
 				       &panic_exit_notifier);
 
@@ -418,7 +417,7 @@ void __init setup_arch(char **cmdline_p)
 {
 	u8 rng_seed[32];
 
-	stack_protections((unsigned long) &init_thread_info);
+	stack_protections((unsigned long) init_task.stack);
 	setup_physmem(uml_physmem, uml_reserved, physmem_size);
 	mem_total_pages(physmem_size, iomem_size);
 	uml_dtb_init();
arch/um/kernel/uml.lds.S +0 -2
index 5c92d58a78e8..a409d4b66114 100644
@@ -77,8 +77,6 @@ SECTIONS
   .data    :
   {
     INIT_TASK_DATA(KERNEL_STACK_SIZE)
-    . = ALIGN(KERNEL_STACK_SIZE);
-    *(.data..init_irqstack)
     DATA_DATA
     *(.gnu.linkonce.d*)
     CONSTRUCTORS
arch/um/os-Linux/signal.c +3 -36
index 1978eaa557e9..87d31a0672c0 100644
@@ -186,47 +186,14 @@ static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
 	[SIGUSR1] = sigusr1_handler,
 };
 
+extern char cpu0_irqstack[];
+
 static void hard_handler(int sig, siginfo_t *si, void *p)
 {
 	ucontext_t *uc = p;
 	mcontext_t *mc = &uc->uc_mcontext;
-	unsigned long pending = 1UL << sig;
-
-	do {
-		int nested, bail;
-
-		/*
-		 * pending comes back with one bit set for each
-		 * interrupt that arrived while setting up the stack,
-		 * plus a bit for this interrupt, plus the zero bit is
-		 * set if this is a nested interrupt.
-		 * If bail is true, then we interrupted another
-		 * handler setting up the stack.  In this case, we
-		 * have to return, and the upper handler will deal
-		 * with this interrupt.
-		 */
-		bail = to_irq_stack(&pending);
-		if (bail)
-			return;
 
-		nested = pending & 1;
-		pending &= ~1;
-
-		while ((sig = ffs(pending)) != 0){
-			sig--;
-			pending &= ~(1 << sig);
-			(*handlers[sig])(sig, (struct siginfo *)si, mc);
-		}
-
-		/*
-		 * Again, pending comes back with a mask of signals
-		 * that arrived while tearing down the stack.  If this
-		 * is non-zero, we just go back, set up the stack
-		 * again, and handle the new interrupts.
-		 */
-		if (!nested)
-			pending = from_irq_stack(nested);
-	} while (pending);
+	(*handlers[sig])(sig, (struct siginfo *)si, mc);
 }
 
 void set_handler(int sig)
Signature · 1 line
2.47.0
#1 lore raw

Hello,

this is a great clean up I think.
I've also applied my tentative nommu patch and works fine with minor modifications.

Quoted 6 lines · Benjamin Berg
arch/um/include/asm/current.h new file +0 -0
new file mode 100644
index 000000000000..93ee89e56372

arch/um/include/asm/current.h line 9

Quoted 9 lines
arch/um/include/asm/current.h new file +9 -0
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_CURRENT_H
+#define __ASM_CURRENT_H
+
+#include <linux/compiler.h>
+
+#ifndef __ASSEMBLY__
+
+#include <as-layout.h>

with this include, I guess <generated/asm-offsets.h> is now in a circular dependency.

after only applying this patch onto uml/next, and make mrproper ARCH=um, I cannot build UML kernel with defconfig.

arch/um/os-Linux/signal.c static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = { · line 190

Quoted 10 lines
arch/um/os-Linux/signal.c +2 -0
index 1978eaa557e9..87d31a0672c0 100644
@@ -186,47 +186,14 @@ static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = {
 	[SIGUSR1] = sigusr1_handler,
 };
 
+extern char cpu0_irqstack[];
+

maybe this line isn't needed ?

-- Hajime

#2 lore raw

Hi,

Quoted 27 lines · Hajime Tazaki

this is a great clean up I think.
I've also applied my tentative nommu patch and works fine with minor modifications.

Quoted 7 lines · Benjamin Berg
arch/um/include/asm/current.h new file +0 -0
new file mode 100644
index 000000000000..93ee89e56372

arch/um/include/asm/current.h line 9

Quoted 9 lines
arch/um/include/asm/current.h new file +9 -0
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_CURRENT_H
+#define __ASM_CURRENT_H
+
+#include <linux/compiler.h>
+
+#ifndef __ASSEMBLY__
+
+#include <as-layout.h>

with this include, I guess <generated/asm-offsets.h> is now in a circular dependency.

Quoted 2 lines

after only applying this patch onto uml/next, and make mrproper ARCH=um, I cannot build UML kernel with defconfig.

Oh. Probably the easiest is to just get rid of "struct cpu_task" and then copy the definition of cpu_tasks over. i.e.

  extern void * cpu_tasks[];

at that point.

Quoted 5 lines
Quoted 5 lines
arch/um/os-Linux/signal.c +0 -0
index 1978eaa557e9..87d31a0672c0 100644
@@ -186,47 +186,14 @@ static void (*handlers[_NSIG])(int sig,
Quoted 8 lines

arch/um/os-Linux/signal.c static void (*handlers[_NSIG])(int sig, struct siginfo *si, mcontext_t *mc) = { · line 190

Quoted 6 lines
arch/um/os-Linux/signal.c +0 -0
 struct siginfo *si, mcontext_t *mc) = {
  	[SIGUSR1] = sigusr1_handler,
  };

arch/um/os-Linux/signal.c +2 -0
+extern char cpu0_irqstack[];
+

maybe this line isn't needed ?

Oops, yes. Initially I thought I still needed to detect nested signals. That is a leftover of doing that by inspecting the stack pointer in the mcontext register set.

Benjamin
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

#3 lore raw
Quoted 6 lines · Benjamin
Quoted 5 lines

with this include, I guess <generated/asm-offsets.h> is now in a circular dependency.

after only applying this patch onto uml/next, and make mrproper ARCH=um, I cannot build UML kernel with defconfig.

Quoted 6 lines

Oh. Probably the easiest is to just get rid of "struct cpu_task" and then copy the definition of cpu_tasks over. i.e.

  extern void * cpu_tasks[];

at that point.

thanks, this works for me.

-- Hajime