Skip to contentSkip to conversation

Move task_struct::cpu back into thread_info

PATCH In review Ard Biesheuvel 3 participants 18 messages Sep 30, 2021

Subsystems (6)
x86 PowerPC RISC-V arm64 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover Move task_struct::cpu back into thread_info

Commit c65eacbe290b ("sched/core: Allow putting thread_info into task_struct") mentions that, along with moving thread_info into task_struct, the cpu field is moved out of the former into the latter, as at that point, it was the intention to ultimately get rid of thread_info entirely, but this never happened.

While collaborating with Keith on adding THREAD_INFO_IN_TASK support to ARM, we noticed that keeping CPU in task_struct is problematic for architectures that define raw_smp_processor_id() in terms of this field, as it requires linux/sched.h to be included, which causes a lot of pain in terms of circular dependencies (or 'header soup', as the original commit refers to it).

For examples of how existing architectures work around this, please refer to patches #6 or #7. In the former case, it uses an awful asm-offsets hack to index thread_info/current without using its type definition. The latter approach simply keeps a copy of the task_struct CPU field in thread_info, and keeps it in sync at context switch time.

We also discussed introducing yet another Kconfig symbol to indicate that the arch has THREAD_INFO_IN_TASK enabled but still prefers to keep its CPU field in thread_info, but simply keeping it in thread_info in all cases seems to be the cleanest approach here, so that is what this series implements.

Changes since v1/RFC:
- use macro for task_thread_info() to work around constness of task_cpu()'s task_struct argument
- add various acks
- drop patch #8 for ARM - it was preliminary in the RFC, and it can be taken as a fix in the ARM tree once THREAD_INFO_IN_TASK actually lands.

Code can be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/ardb/linux.git/log/?h=move-task-cpu-to-ti-v2

Cc Keith Packard <[email protected]>
Cc Russell King <[email protected]>
Cc Catalin Marinas <[email protected]>
Cc Will Deacon <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Christophe Leroy <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Heiko Carstens <[email protected]>

Ard Biesheuvel (7):
arm64: add CPU field to struct thread_info
x86: add CPU field to struct thread_info
s390: add CPU field to struct thread_info
powerpc: add CPU field to struct thread_info
sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y
powerpc: smp: remove hack to obtain offset of task_struct::cpu
riscv: rely on core code to keep thread_info::cpu updated

 arch/arm64/include/asm/thread_info.h   |  1 +
 arch/arm64/kernel/asm-offsets.c        |  2 +-
 arch/arm64/kernel/head.S               |  2 +-
 arch/powerpc/Makefile                  | 11 -----------
 arch/powerpc/include/asm/smp.h         | 17 +----------------
 arch/powerpc/include/asm/thread_info.h |  3 +++
 arch/powerpc/kernel/asm-offsets.c      |  4 +---
 arch/powerpc/kernel/smp.c              |  2 +-
 arch/riscv/kernel/asm-offsets.c        |  1 -
 arch/riscv/kernel/entry.S              |  5 -----
 arch/riscv/kernel/head.S               |  1 -
 arch/s390/include/asm/thread_info.h    |  1 +
 arch/x86/include/asm/thread_info.h     |  3 +++
 include/linux/sched.h                  | 13 +------------
 kernel/sched/sched.h                   |  4 ----
 15 files changed, 14 insertions(+), 56 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/7 arm64: add CPU field to struct thread_info

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to arm64's definition of struct thread_info.

Note that arm64 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

---
 arch/arm64/include/asm/thread_info.h | 1 +
 arch/arm64/kernel/asm-offsets.c      | 1 +
 2 files changed, 2 insertions(+)
arch/arm64/include/asm/thread_info.h +1 -0
index 6623c99f0984..c02bc8c183c3 100644
@@ -42,6 +42,7 @@ struct thread_info {
 	void			*scs_base;
 	void			*scs_sp;
 #endif
+	u32			cpu;
 };
 
 #define thread_saved_pc(tsk)	\
arch/arm64/kernel/asm-offsets.c +1 -0
index 551427ae8cc5..cee9f3e9f906 100644
@@ -29,6 +29,7 @@ int main(void)
   DEFINE(TSK_ACTIVE_MM,		offsetof(struct task_struct, active_mm));
   DEFINE(TSK_CPU,		offsetof(struct task_struct, cpu));
   BLANK();
+  DEFINE(TSK_TI_CPU,		offsetof(struct task_struct, thread_info.cpu));
   DEFINE(TSK_TI_FLAGS,		offsetof(struct task_struct, thread_info.flags));
   DEFINE(TSK_TI_PREEMPT,	offsetof(struct task_struct, thread_info.preempt_count));
 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v2 1/7] arm64: add CPU field to struct thread_info

Le 30/09/2021 à 14:58, Ard Biesheuvel a écrit :

Quoted 32 lines

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to arm64's definition of struct thread_info.

Note that arm64 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

---
  arch/arm64/include/asm/thread_info.h | 1 +
  arch/arm64/kernel/asm-offsets.c      | 1 +
  2 files changed, 2 insertions(+)
arch/arm64/include/asm/thread_info.h +1 -0
index 6623c99f0984..c02bc8c183c3 100644
@@ -42,6 +42,7 @@ struct thread_info {
  	void			*scs_base;
  	void			*scs_sp;
  #endif
+	u32			cpu;
  };
 
  #define thread_saved_pc(tsk)	\
arch/arm64/kernel/asm-offsets.c +0 -0
index 551427ae8cc5..cee9f3e9f906 100644

arch/arm64/kernel/asm-offsets.c int main(void) · line 32

Quoted 4 lines
arch/arm64/kernel/asm-offsets.c +1 -0
@@ -29,6 +29,7 @@ int main(void)
    DEFINE(TSK_ACTIVE_MM,		offsetof(struct task_struct, active_mm));
    DEFINE(TSK_CPU,		offsetof(struct task_struct, cpu));
    BLANK();
+  DEFINE(TSK_TI_CPU,		offsetof(struct task_struct, thread_info.cpu));

Why adding that now ? For powerpc you do the switch in 5.

Quoted 4 lines
arch/arm64/kernel/asm-offsets.c +0 -0
    DEFINE(TSK_TI_FLAGS,		offsetof(struct task_struct, thread_info.flags));
    DEFINE(TSK_TI_PREEMPT,	offsetof(struct task_struct, thread_info.preempt_count));
  #ifdef CONFIG_ARM64_SW_TTBR0_PAN
#3 lore raw

Re: [PATCH v2 1/7] arm64: add CPU field to struct thread_info

Quoted 30 lines · Christophe Leroy
Quoted 26 lines · Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to arm64's definition of struct thread_info.

Note that arm64 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

---
  arch/arm64/include/asm/thread_info.h | 1 +
  arch/arm64/kernel/asm-offsets.c      | 1 +
  2 files changed, 2 insertions(+)
arch/arm64/include/asm/thread_info.h +1 -0
index 6623c99f0984..c02bc8c183c3 100644
@@ -42,6 +42,7 @@ struct thread_info {
      void                    *scs_base;
      void                    *scs_sp;
  #endif
+     u32                     cpu;
  };
 
Quoted 13 lines

arch/arm64/kernel/asm-offsets.c int main(void) · line 32

Quoted 10 lines
arch/arm64/include/asm/thread_info.h +0 -0
  #define thread_saved_pc(tsk)        \
arch/arm64/kernel/asm-offsets.c +1 -0
index 551427ae8cc5..cee9f3e9f906 100644
@@ -29,6 +29,7 @@ int main(void)
    DEFINE(TSK_ACTIVE_MM,             offsetof(struct task_struct, active_mm));
    DEFINE(TSK_CPU,           offsetof(struct task_struct, cpu));
    BLANK();
+  DEFINE(TSK_TI_CPU,         offsetof(struct task_struct, thread_info.cpu));

Why adding that now ? For powerpc you do the switch in 5.

Why not?

Quoted 4 lines
Quoted 4 lines
arch/arm64/kernel/asm-offsets.c +0 -0
    DEFINE(TSK_TI_FLAGS,              offsetof(struct task_struct, thread_info.flags));
    DEFINE(TSK_TI_PREEMPT,    offsetof(struct task_struct, thread_info.preempt_count));
  #ifdef CONFIG_ARM64_SW_TTBR0_PAN
#4 lore raw

Re: [PATCH v2 1/7] arm64: add CPU field to struct thread_info

Le 30/09/2021 à 15:07, Ard Biesheuvel a écrit :

Quoted 47 lines
Quoted 30 lines · Christophe Leroy
Quoted 26 lines · Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to arm64's definition of struct thread_info.

Note that arm64 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

---
   arch/arm64/include/asm/thread_info.h | 1 +
   arch/arm64/kernel/asm-offsets.c      | 1 +
   2 files changed, 2 insertions(+)
arch/arm64/include/asm/thread_info.h +1 -0
index 6623c99f0984..c02bc8c183c3 100644
@@ -42,6 +42,7 @@ struct thread_info {
       void                    *scs_base;
       void                    *scs_sp;
   #endif
+     u32                     cpu;
   };
 
Quoted 13 lines

arch/arm64/kernel/asm-offsets.c int main(void) · line 32

Quoted 10 lines
arch/arm64/include/asm/thread_info.h +0 -0
   #define thread_saved_pc(tsk)        \
arch/arm64/kernel/asm-offsets.c +1 -0
index 551427ae8cc5..cee9f3e9f906 100644
@@ -29,6 +29,7 @@ int main(void)
     DEFINE(TSK_ACTIVE_MM,             offsetof(struct task_struct, active_mm));
     DEFINE(TSK_CPU,           offsetof(struct task_struct, cpu));
     BLANK();
+  DEFINE(TSK_TI_CPU,         offsetof(struct task_struct, thread_info.cpu));

Why adding that now ? For powerpc you do the switch in 5.

Quoted 1 line

Why not?

Maybe to remain consistent between archs ?

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/arm64/kernel/asm-offsets.c +0 -0
     DEFINE(TSK_TI_FLAGS,              offsetof(struct task_struct, thread_info.flags));
     DEFINE(TSK_TI_PREEMPT,    offsetof(struct task_struct, thread_info.preempt_count));
   #ifdef CONFIG_ARM64_SW_TTBR0_PAN
#5 lore raw

Re: [PATCH v2 1/7] arm64: add CPU field to struct thread_info

Quoted 51 lines · Christophe Leroy
Quoted 47 lines · Ard Biesheuvel
Quoted 43 lines · Christophe Leroy
Quoted 32 lines · Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to arm64's definition of struct thread_info.

Note that arm64 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

---
   arch/arm64/include/asm/thread_info.h | 1 +
   arch/arm64/kernel/asm-offsets.c      | 1 +
   2 files changed, 2 insertions(+)
arch/arm64/include/asm/thread_info.h +1 -0
index 6623c99f0984..c02bc8c183c3 100644
@@ -42,6 +42,7 @@ struct thread_info {
       void                    *scs_base;
       void                    *scs_sp;
   #endif
+     u32                     cpu;
   };
 
   #define thread_saved_pc(tsk)        \
arch/arm64/kernel/asm-offsets.c +0 -0
index 551427ae8cc5..cee9f3e9f906 100644

arch/arm64/kernel/asm-offsets.c int main(void) · line 32

Quoted 4 lines
arch/arm64/kernel/asm-offsets.c +1 -0
@@ -29,6 +29,7 @@ int main(void)
     DEFINE(TSK_ACTIVE_MM,             offsetof(struct task_struct, active_mm));
     DEFINE(TSK_CPU,           offsetof(struct task_struct, cpu));
     BLANK();
+  DEFINE(TSK_TI_CPU,         offsetof(struct task_struct, thread_info.cpu));

Why adding that now ? For powerpc you do the switch in 5.

Quoted 4 lines
Quoted 1 line

Why not?

Maybe to remain consistent between archs ?

Does it matter?

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/arm64/kernel/asm-offsets.c +0 -0
     DEFINE(TSK_TI_FLAGS,              offsetof(struct task_struct, thread_info.flags));
     DEFINE(TSK_TI_PREEMPT,    offsetof(struct task_struct, thread_info.preempt_count));
   #ifdef CONFIG_ARM64_SW_TTBR0_PAN
#6 lore raw

Re: [PATCH v2 1/7] arm64: add CPU field to struct thread_info

Le 30/09/2021 à 15:20, Ard Biesheuvel a écrit :

Quoted 53 lines
Quoted 51 lines · Christophe Leroy
Quoted 47 lines · Ard Biesheuvel
Quoted 43 lines · Christophe Leroy
Quoted 32 lines · Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to arm64's definition of struct thread_info.

Note that arm64 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

---
    arch/arm64/include/asm/thread_info.h | 1 +
    arch/arm64/kernel/asm-offsets.c      | 1 +
    2 files changed, 2 insertions(+)
arch/arm64/include/asm/thread_info.h +1 -0
index 6623c99f0984..c02bc8c183c3 100644
@@ -42,6 +42,7 @@ struct thread_info {
        void                    *scs_base;
        void                    *scs_sp;
    #endif
+     u32                     cpu;
    };
 
    #define thread_saved_pc(tsk)        \
arch/arm64/kernel/asm-offsets.c +0 -0
index 551427ae8cc5..cee9f3e9f906 100644

arch/arm64/kernel/asm-offsets.c int main(void) · line 32

Quoted 4 lines
arch/arm64/kernel/asm-offsets.c +1 -0
@@ -29,6 +29,7 @@ int main(void)
      DEFINE(TSK_ACTIVE_MM,             offsetof(struct task_struct, active_mm));
      DEFINE(TSK_CPU,           offsetof(struct task_struct, cpu));
      BLANK();
+  DEFINE(TSK_TI_CPU,         offsetof(struct task_struct, thread_info.cpu));

Why adding that now ? For powerpc you do the switch in 5.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Why not?

Maybe to remain consistent between archs ?

Does it matter?

Probably not :)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
arch/arm64/kernel/asm-offsets.c +0 -0
      DEFINE(TSK_TI_FLAGS,              offsetof(struct task_struct, thread_info.flags));
      DEFINE(TSK_TI_PREEMPT,    offsetof(struct task_struct, thread_info.preempt_count));
    #ifdef CONFIG_ARM64_SW_TTBR0_PAN
#7 lore raw

PATCH v2 2/7 x86: add CPU field to struct thread_info

In reply to Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to x86's definition of struct thread_info.

---
 arch/x86/include/asm/thread_info.h | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/include/asm/thread_info.h +3 -0
index cf132663c219..ebec69c35e95 100644
@@ -57,6 +57,9 @@ struct thread_info {
 	unsigned long		flags;		/* low level flags */
 	unsigned long		syscall_work;	/* SYSCALL_WORK_ flags */
 	u32			status;		/* thread synchronous flags */
+#ifdef CONFIG_SMP
+	u32			cpu;		/* current CPU */
+#endif
 };
 
 #define INIT_THREAD_INFO(tsk)			\
Signature · 1 line
2.30.2
#8 lore raw

PATCH v2 3/7 s390: add CPU field to struct thread_info

In reply to Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to s390's definition of struct thread_info.

Note that s390 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 arch/s390/include/asm/thread_info.h | 1 +
 1 file changed, 1 insertion(+)
arch/s390/include/asm/thread_info.h +1 -0
index e6674796aa6f..b2ffcb4fe000 100644
@@ -37,6 +37,7 @@
 struct thread_info {
 	unsigned long		flags;		/* low level flags */
 	unsigned long		syscall_work;	/* SYSCALL_WORK_ flags */
+	unsigned int		cpu;		/* current CPU */
 };
 
 /*
Signature · 1 line
2.30.2
maintainer Acked-by on v2 3/7
#9 lore raw

Re: [PATCH v2 3/7] s390: add CPU field to struct thread_info

Quoted 7 lines · Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to s390's definition of struct thread_info.

Note that s390 always has CONFIG_SMP=y so there is no point in guarding the CPU field with an #ifdef.

Quoted 8 lines
Cc Heiko Carstens <[email protected]>
Cc Vasily Gorbik <[email protected]>
Cc Christian Borntraeger <[email protected]>
---
 arch/s390/include/asm/thread_info.h | 1 +
 1 file changed, 1 insertion(+)
#10 lore raw

PATCH v2 4/7 powerpc: add CPU field to struct thread_info

In reply to Ard Biesheuvel

The CPU field will be moved back into thread_info even when THREAD_INFO_IN_TASK is enabled, so add it back to powerpc's definition of struct thread_info.

---
 arch/powerpc/include/asm/thread_info.h | 3 +++
 1 file changed, 3 insertions(+)
arch/powerpc/include/asm/thread_info.h +3 -0
index b4ec6c7dd72e..5725029aaa29 100644
@@ -47,6 +47,9 @@
 struct thread_info {
 	int		preempt_count;		/* 0 => preemptable,
 						   <0 => BUG */
+#ifdef CONFIG_SMP
+	unsigned int	cpu;
+#endif
 	unsigned long	local_flags;		/* private flags for thread */
 #ifdef CONFIG_LIVEPATCH
 	unsigned long *livepatch_sp;
Signature · 1 line
2.30.2
#11 lore raw

PATCH v2 5/7 sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y

In reply to Ard Biesheuvel

THREAD_INFO_IN_TASK moved the CPU field out of thread_info, but this causes some issues on architectures that define raw_smp_processor_id() in terms of this field, due to the fact that #include'ing linux/sched.h to get at struct task_struct is problematic in terms of circular dependencies.

Given that thread_info and task_struct are the same data structure anyway when THREAD_INFO_IN_TASK=y, let's move it back so that having access to the type definition of struct thread_info is sufficient to reference the CPU number of the current task.

Note that this requires THREAD_INFO_IN_TASK's definition of the task_thread_info() helper to be updated, as task_cpu() takes a pointer-to-const, whereas task_thread_info() (which is used to generate lvalues as well), needs a non-const pointer. So make it a macro instead.

---
 arch/arm64/kernel/asm-offsets.c   |  1 -
 arch/arm64/kernel/head.S          |  2 +-
 arch/powerpc/kernel/asm-offsets.c |  2 +-
 arch/powerpc/kernel/smp.c         |  2 +-
 include/linux/sched.h             | 13 +------------
 kernel/sched/sched.h              |  4 ----
 6 files changed, 4 insertions(+), 20 deletions(-)
arch/arm64/kernel/asm-offsets.c +0 -1
index cee9f3e9f906..0bfc048221af 100644
@@ -27,7 +27,6 @@
 int main(void)
 {
   DEFINE(TSK_ACTIVE_MM,		offsetof(struct task_struct, active_mm));
-  DEFINE(TSK_CPU,		offsetof(struct task_struct, cpu));
   BLANK();
   DEFINE(TSK_TI_CPU,		offsetof(struct task_struct, thread_info.cpu));
   DEFINE(TSK_TI_FLAGS,		offsetof(struct task_struct, thread_info.flags));
arch/arm64/kernel/head.S +1 -1
index 17962452e31d..6a98f1a38c29 100644
@@ -412,7 +412,7 @@ SYM_FUNC_END(__create_page_tables)
 	scs_load \tsk
 
 	adr_l	\tmp1, __per_cpu_offset
-	ldr	w\tmp2, [\tsk, #TSK_CPU]
+	ldr	w\tmp2, [\tsk, #TSK_TI_CPU]
 	ldr	\tmp1, [\tmp1, \tmp2, lsl #3]
 	set_this_cpu_offset \tmp1
 	.endm
arch/powerpc/kernel/asm-offsets.c +1 -1
index e563d3222d69..e37e4546034e 100644
@@ -93,7 +93,7 @@ int main(void)
 #endif /* CONFIG_PPC64 */
 	OFFSET(TASK_STACK, task_struct, stack);
 #ifdef CONFIG_SMP
-	OFFSET(TASK_CPU, task_struct, cpu);
+	OFFSET(TASK_CPU, task_struct, thread_info.cpu);
 #endif
 
 #ifdef CONFIG_LIVEPATCH
arch/powerpc/kernel/smp.c +1 -1
index 9cc7d3dbf439..512d875b45e0 100644
@@ -1223,7 +1223,7 @@ static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
 	paca_ptrs[cpu]->kstack = (unsigned long)task_stack_page(idle) +
 				 THREAD_SIZE - STACK_FRAME_OVERHEAD;
 #endif
-	idle->cpu = cpu;
+	task_thread_info(idle)->cpu = cpu;
 	secondary_current = current_set[cpu] = idle;
 }
 
include/linux/sched.h +1 -12
index 39039ce8ac4c..8699594e3f99 100644
@@ -750,10 +750,6 @@ struct task_struct {
 #ifdef CONFIG_SMP
 	int				on_cpu;
 	struct __call_single_node	wake_entry;
-#ifdef CONFIG_THREAD_INFO_IN_TASK
-	/* Current CPU: */
-	unsigned int			cpu;
-#endif
 	unsigned int			wakee_flips;
 	unsigned long			wakee_flip_decay_ts;
 	struct task_struct		*last_wakee;
@@ -1886,10 +1882,7 @@ extern struct thread_info init_thread_info;
 extern unsigned long init_stack[THREAD_SIZE / sizeof(unsigned long)];
 
 #ifdef CONFIG_THREAD_INFO_IN_TASK
-static inline struct thread_info *task_thread_info(struct task_struct *task)
-{
-	return &task->thread_info;
-}
+# define task_thread_info(task)	(&(task)->thread_info)
 #elif !defined(__HAVE_THREAD_FUNCTIONS)
 # define task_thread_info(task)	((struct thread_info *)(task)->stack)
 #endif
@@ -2114,11 +2107,7 @@ static __always_inline bool need_resched(void)
 
 static inline unsigned int task_cpu(const struct task_struct *p)
 {
-#ifdef CONFIG_THREAD_INFO_IN_TASK
-	return READ_ONCE(p->cpu);
-#else
 	return READ_ONCE(task_thread_info(p)->cpu);
-#endif
 }
 
 extern void set_task_cpu(struct task_struct *p, unsigned int cpu);
kernel/sched/sched.h +0 -4
index 3d3e5793e117..79fcbad11450 100644
@@ -1926,11 +1926,7 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
 	 * per-task data have been completed by this moment.
 	 */
 	smp_wmb();
-#ifdef CONFIG_THREAD_INFO_IN_TASK
-	WRITE_ONCE(p->cpu, cpu);
-#else
 	WRITE_ONCE(task_thread_info(p)->cpu, cpu);
-#endif
 	p->wake_cpu = cpu;
 #endif
 }
Signature · 1 line
2.30.2
#12 lore raw

Re: [PATCH v2 5/7] sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y

Le 30/09/2021 à 14:58, Ard Biesheuvel a écrit :

Quoted 46 lines

THREAD_INFO_IN_TASK moved the CPU field out of thread_info, but this causes some issues on architectures that define raw_smp_processor_id() in terms of this field, due to the fact that #include'ing linux/sched.h to get at struct task_struct is problematic in terms of circular dependencies.

Given that thread_info and task_struct are the same data structure anyway when THREAD_INFO_IN_TASK=y, let's move it back so that having access to the type definition of struct thread_info is sufficient to reference the CPU number of the current task.

Note that this requires THREAD_INFO_IN_TASK's definition of the task_thread_info() helper to be updated, as task_cpu() takes a pointer-to-const, whereas task_thread_info() (which is used to generate lvalues as well), needs a non-const pointer. So make it a macro instead.

---
  arch/arm64/kernel/asm-offsets.c   |  1 -
  arch/arm64/kernel/head.S          |  2 +-
  arch/powerpc/kernel/asm-offsets.c |  2 +-
  arch/powerpc/kernel/smp.c         |  2 +-
  include/linux/sched.h             | 13 +------------
  kernel/sched/sched.h              |  4 ----
  6 files changed, 4 insertions(+), 20 deletions(-)
arch/arm64/kernel/asm-offsets.c +0 -1
index cee9f3e9f906..0bfc048221af 100644
@@ -27,7 +27,6 @@
  int main(void)
  {
    DEFINE(TSK_ACTIVE_MM,		offsetof(struct task_struct, active_mm));
-  DEFINE(TSK_CPU,		offsetof(struct task_struct, cpu));
    BLANK();
    DEFINE(TSK_TI_CPU,		offsetof(struct task_struct, thread_info.cpu));
    DEFINE(TSK_TI_FLAGS,		offsetof(struct task_struct, thread_info.flags));
arch/arm64/kernel/head.S +0 -0
index 17962452e31d..6a98f1a38c29 100644

arch/arm64/kernel/head.S SYM_FUNC_END(__create_page_tables) · line 415

Quoted 5 lines
arch/arm64/kernel/head.S +1 -1
@@ -412,7 +412,7 @@ SYM_FUNC_END(__create_page_tables)
  	scs_load \tsk
 
  	adr_l	\tmp1, __per_cpu_offset
-	ldr	w\tmp2, [\tsk, #TSK_CPU]
+	ldr	w\tmp2, [\tsk, #TSK_TI_CPU]

Why do you need to change the name ?

For powerpc64, you leave TASK_CPU.

Quoted 16 lines
arch/arm64/kernel/head.S +0 -0
  	ldr	\tmp1, [\tmp1, \tmp2, lsl #3]
  	set_this_cpu_offset \tmp1
  	.endm
arch/powerpc/kernel/asm-offsets.c +1 -1
index e563d3222d69..e37e4546034e 100644
@@ -93,7 +93,7 @@ int main(void)
  #endif /* CONFIG_PPC64 */
  	OFFSET(TASK_STACK, task_struct, stack);
  #ifdef CONFIG_SMP
-	OFFSET(TASK_CPU, task_struct, cpu);
+	OFFSET(TASK_CPU, task_struct, thread_info.cpu);
  #endif
 
  #ifdef CONFIG_LIVEPATCH

...

#13 lore raw

Re: [PATCH v2 5/7] sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y

Quoted 52 lines · Christophe Leroy
Quoted 48 lines · Ard Biesheuvel

THREAD_INFO_IN_TASK moved the CPU field out of thread_info, but this causes some issues on architectures that define raw_smp_processor_id() in terms of this field, due to the fact that #include'ing linux/sched.h to get at struct task_struct is problematic in terms of circular dependencies.

Given that thread_info and task_struct are the same data structure anyway when THREAD_INFO_IN_TASK=y, let's move it back so that having access to the type definition of struct thread_info is sufficient to reference the CPU number of the current task.

Note that this requires THREAD_INFO_IN_TASK's definition of the task_thread_info() helper to be updated, as task_cpu() takes a pointer-to-const, whereas task_thread_info() (which is used to generate lvalues as well), needs a non-const pointer. So make it a macro instead.

---
  arch/arm64/kernel/asm-offsets.c   |  1 -
  arch/arm64/kernel/head.S          |  2 +-
  arch/powerpc/kernel/asm-offsets.c |  2 +-
  arch/powerpc/kernel/smp.c         |  2 +-
  include/linux/sched.h             | 13 +------------
  kernel/sched/sched.h              |  4 ----
  6 files changed, 4 insertions(+), 20 deletions(-)
arch/arm64/kernel/asm-offsets.c +0 -1
index cee9f3e9f906..0bfc048221af 100644
@@ -27,7 +27,6 @@
  int main(void)
  {
    DEFINE(TSK_ACTIVE_MM,             offsetof(struct task_struct, active_mm));
-  DEFINE(TSK_CPU,            offsetof(struct task_struct, cpu));
    BLANK();
    DEFINE(TSK_TI_CPU,                offsetof(struct task_struct, thread_info.cpu));
    DEFINE(TSK_TI_FLAGS,              offsetof(struct task_struct, thread_info.flags));
arch/arm64/kernel/head.S +0 -0
index 17962452e31d..6a98f1a38c29 100644
@@ -412,7 +412,7 @@ SYM_FUNC_END(__create_page_tables)
      scs_load \tsk
 
Quoted 8 lines

arch/arm64/kernel/head.S SYM_FUNC_END(__create_page_tables) · line 415

Quoted 3 lines
arch/arm64/kernel/head.S +1 -1
      adr_l   \tmp1, __per_cpu_offset
-     ldr     w\tmp2, [\tsk, #TSK_CPU]
+     ldr     w\tmp2, [\tsk, #TSK_TI_CPU]

Why do you need to change the name ?

For powerpc64, you leave TASK_CPU.

Because arm64 has a clear idiom here, where TSK_TI_ is used for thread_info fields accessed via a task_struct pointer. Also, it only occurs once in the code.

Power does not seem to have this idiom, and TASK_CPU is used in many more places, so I don't think it makes sense to change its name.

Quoted 18 lines
Quoted 16 lines
arch/arm64/kernel/head.S +0 -0
      ldr     \tmp1, [\tmp1, \tmp2, lsl #3]
      set_this_cpu_offset \tmp1
      .endm
arch/powerpc/kernel/asm-offsets.c +1 -1
index e563d3222d69..e37e4546034e 100644
@@ -93,7 +93,7 @@ int main(void)
  #endif /* CONFIG_PPC64 */
      OFFSET(TASK_STACK, task_struct, stack);
  #ifdef CONFIG_SMP
-     OFFSET(TASK_CPU, task_struct, cpu);
+     OFFSET(TASK_CPU, task_struct, thread_info.cpu);
  #endif
 
  #ifdef CONFIG_LIVEPATCH

...

#14 lore raw

Re: [PATCH v2 5/7] sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y

Le 30/09/2021 à 15:12, Ard Biesheuvel a écrit :

Quoted 63 lines
Quoted 52 lines · Christophe Leroy
Quoted 48 lines · Ard Biesheuvel

THREAD_INFO_IN_TASK moved the CPU field out of thread_info, but this causes some issues on architectures that define raw_smp_processor_id() in terms of this field, due to the fact that #include'ing linux/sched.h to get at struct task_struct is problematic in terms of circular dependencies.

Given that thread_info and task_struct are the same data structure anyway when THREAD_INFO_IN_TASK=y, let's move it back so that having access to the type definition of struct thread_info is sufficient to reference the CPU number of the current task.

Note that this requires THREAD_INFO_IN_TASK's definition of the task_thread_info() helper to be updated, as task_cpu() takes a pointer-to-const, whereas task_thread_info() (which is used to generate lvalues as well), needs a non-const pointer. So make it a macro instead.

---
   arch/arm64/kernel/asm-offsets.c   |  1 -
   arch/arm64/kernel/head.S          |  2 +-
   arch/powerpc/kernel/asm-offsets.c |  2 +-
   arch/powerpc/kernel/smp.c         |  2 +-
   include/linux/sched.h             | 13 +------------
   kernel/sched/sched.h              |  4 ----
   6 files changed, 4 insertions(+), 20 deletions(-)
arch/arm64/kernel/asm-offsets.c +0 -1
index cee9f3e9f906..0bfc048221af 100644
@@ -27,7 +27,6 @@
   int main(void)
   {
     DEFINE(TSK_ACTIVE_MM,             offsetof(struct task_struct, active_mm));
-  DEFINE(TSK_CPU,            offsetof(struct task_struct, cpu));
     BLANK();
     DEFINE(TSK_TI_CPU,                offsetof(struct task_struct, thread_info.cpu));
     DEFINE(TSK_TI_FLAGS,              offsetof(struct task_struct, thread_info.flags));
arch/arm64/kernel/head.S +0 -0
index 17962452e31d..6a98f1a38c29 100644
@@ -412,7 +412,7 @@ SYM_FUNC_END(__create_page_tables)
       scs_load \tsk
 
Quoted 8 lines

arch/arm64/kernel/head.S SYM_FUNC_END(__create_page_tables) · line 415

Quoted 3 lines
arch/arm64/kernel/head.S +1 -1
       adr_l   \tmp1, __per_cpu_offset
-     ldr     w\tmp2, [\tsk, #TSK_CPU]
+     ldr     w\tmp2, [\tsk, #TSK_TI_CPU]

Why do you need to change the name ?

For powerpc64, you leave TASK_CPU.

Quoted 6 lines

Because arm64 has a clear idiom here, where TSK_TI_ is used for thread_info fields accessed via a task_struct pointer. Also, it only occurs once in the code.

Power does not seem to have this idiom, and TASK_CPU is used in many more places, so I don't think it makes sense to change its name.

In the old days it was called TI_CPU, was changed by commit f7354ccac844 ("powerpc/32: Remove CURRENT_THREAD_INFO and rename TI_CPU") after commit ed1cd6deb013 ("powerpc: Activate CONFIG_THREAD_INFO_IN_TASK")

I don't have a strong opinion about it but we have:

$ git grep thread_info arch/powerpc/kernel/asm-offsets.c
arch/powerpc/kernel/asm-offsets.c:#include <asm/thread_info.h>
arch/powerpc/kernel/asm-offsets.c:      OFFSET(TI_livepatch_sp, thread_info, livepatch_sp);
arch/powerpc/kernel/asm-offsets.c:      OFFSET(TI_LOCAL_FLAGS, thread_info, local_flags);
arch/powerpc/kernel/asm-offsets.c: offsetof(struct task_struct, thread_info));
Quoted 20 lines
Quoted 20 lines
Quoted 18 lines
arch/arm64/kernel/head.S +0 -0
       ldr     \tmp1, [\tmp1, \tmp2, lsl #3]
       set_this_cpu_offset \tmp1
       .endm
arch/powerpc/kernel/asm-offsets.c +1 -1
index e563d3222d69..e37e4546034e 100644
@@ -93,7 +93,7 @@ int main(void)
   #endif /* CONFIG_PPC64 */
       OFFSET(TASK_STACK, task_struct, stack);
   #ifdef CONFIG_SMP
-     OFFSET(TASK_CPU, task_struct, cpu);
+     OFFSET(TASK_CPU, task_struct, thread_info.cpu);
   #endif
 
   #ifdef CONFIG_LIVEPATCH

...

#15 lore raw

Re: [PATCH v2 5/7] sched: move CPU field back into thread_info if THREAD_INFO_IN_TASK=y

Le 30/09/2021 à 15:22, Christophe Leroy a écrit :

Quoted 75 lines

Le 30/09/2021 à 15:12, Ard Biesheuvel a écrit :

Quoted 63 lines
Quoted 52 lines · Christophe Leroy
Quoted 48 lines · Ard Biesheuvel

THREAD_INFO_IN_TASK moved the CPU field out of thread_info, but this causes some issues on architectures that define raw_smp_processor_id() in terms of this field, due to the fact that #include'ing linux/sched.h to get at struct task_struct is problematic in terms of circular dependencies.

Given that thread_info and task_struct are the same data structure anyway when THREAD_INFO_IN_TASK=y, let's move it back so that having access to the type definition of struct thread_info is sufficient to reference the CPU number of the current task.

Note that this requires THREAD_INFO_IN_TASK's definition of the task_thread_info() helper to be updated, as task_cpu() takes a pointer-to-const, whereas task_thread_info() (which is used to generate lvalues as well), needs a non-const pointer. So make it a macro instead.

---
   arch/arm64/kernel/asm-offsets.c   |  1 - arch/arm64/kernel/head.S          |  2 +- arch/powerpc/kernel/asm-offsets.c |  2 +- arch/powerpc/kernel/smp.c         |  2 +- include/linux/sched.h             | 13 +------------ kernel/sched/sched.h              |  4 ----

   6 files changed, 4 insertions(+), 20 deletions(-)
arch/arm64/kernel/asm-offsets.c +0 -1
index cee9f3e9f906..0bfc048221af 100644
@@ -27,7 +27,6 @@
    int main(void)
    {
      DEFINE(TSK_ACTIVE_MM,             offsetof(struct task_struct, active_mm));
-  DEFINE(TSK_CPU,            offsetof(struct task_struct, cpu));
      BLANK();
      DEFINE(TSK_TI_CPU,                offsetof(struct task_struct, thread_info.cpu));
      DEFINE(TSK_TI_FLAGS,              offsetof(struct task_struct, thread_info.flags));
arch/arm64/kernel/head.S +0 -0
index 17962452e31d..6a98f1a38c29 100644
@@ -412,7 +412,7 @@ SYM_FUNC_END(__create_page_tables)

       scs_load \tsk

Quoted 8 lines
Quoted 3 lines

       adr_l   \tmp1, __per_cpu_offset
-     ldr     w\tmp2, [\tsk, #TSK_CPU]
+     ldr     w\tmp2, [\tsk, #TSK_TI_CPU]

Why do you need to change the name ?

For powerpc64, you leave TASK_CPU.

Quoted 6 lines

Because arm64 has a clear idiom here, where TSK_TI_ is used for thread_info fields accessed via a task_struct pointer. Also, it only occurs once in the code.

Power does not seem to have this idiom, and TASK_CPU is used in many more places, so I don't think it makes sense to change its name.

In the old days it was called TI_CPU, was changed by commit f7354ccac844 ("powerpc/32: Remove CURRENT_THREAD_INFO and rename TI_CPU") after commit ed1cd6deb013 ("powerpc: Activate CONFIG_THREAD_INFO_IN_TASK")

Quoted 7 lines

I don't have a strong opinion about it but we have:

$ git grep thread_info arch/powerpc/kernel/asm-offsets.c
arch/powerpc/kernel/asm-offsets.c:#include <asm/thread_info.h>
arch/powerpc/kernel/asm-offsets.c:      OFFSET(TI_livepatch_sp, thread_info, livepatch_sp);
arch/powerpc/kernel/asm-offsets.c:      OFFSET(TI_LOCAL_FLAGS, thread_info, local_flags);
arch/powerpc/kernel/asm-offsets.c: offsetof(struct task_struct, thread_info));

Forget that. I didn't realise that your change keeps it based on 'task_struct' so it makes sense to keep it named TASK_CPU.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 20 lines

       ldr     \tmp1, [\tmp1, \tmp2, lsl #3]
       set_this_cpu_offset \tmp1
       .endm

arch/powerpc/kernel/asm-offsets.c +1 -1
index e563d3222d69..e37e4546034e 100644
@@ -93,7 +93,7 @@ int main(void)
    #endif /* CONFIG_PPC64 */
        OFFSET(TASK_STACK, task_struct, stack);
    #ifdef CONFIG_SMP
-     OFFSET(TASK_CPU, task_struct, cpu);
+     OFFSET(TASK_CPU, task_struct, thread_info.cpu);
    #endif
 
    #ifdef CONFIG_LIVEPATCH

...

#16 lore raw

PATCH v2 6/7 powerpc: smp: remove hack to obtain offset of task_struct::cpu

In reply to Ard Biesheuvel

Instead of relying on awful hacks to obtain the offset of the cpu field in struct task_struct, move it back into struct thread_info, which does not create the same level of circular dependency hell when trying to include the header file that defines it.

---
 arch/powerpc/Makefile             | 11 -----------
 arch/powerpc/include/asm/smp.h    | 17 +----------------
 arch/powerpc/kernel/asm-offsets.c |  2 --
 3 files changed, 1 insertion(+), 29 deletions(-)
arch/powerpc/Makefile +0 -11
index aa6808e70647..54cad1faa5d0 100644
@@ -446,17 +446,6 @@ else
 endif
 endif
 
-ifdef CONFIG_SMP
-ifdef CONFIG_PPC32
-prepare: task_cpu_prepare
-
-PHONY += task_cpu_prepare
-task_cpu_prepare: prepare0
-	$(eval KBUILD_CFLAGS += -D_TASK_CPU=$(shell awk '{if ($$2 == "TASK_CPU") print $$3;}' include/generated/asm-offsets.h))
-
-endif # CONFIG_PPC32
-endif # CONFIG_SMP
-
 PHONY += checkbin
 # Check toolchain versions:
 # - gcc-4.6 is the minimum kernel-wide version so nothing required.
arch/powerpc/include/asm/smp.h +1 -16
index 7ef1cd8168a0..007332a4a732 100644
@@ -87,22 +87,7 @@ int is_cpu_dead(unsigned int cpu);
 /* 32-bit */
 extern int smp_hw_index[];
 
-/*
- * This is particularly ugly: it appears we can't actually get the definition
- * of task_struct here, but we need access to the CPU this task is running on.
- * Instead of using task_struct we're using _TASK_CPU which is extracted from
- * asm-offsets.h by kbuild to get the current processor ID.
- *
- * This also needs to be safeguarded when building asm-offsets.s because at
- * that time _TASK_CPU is not defined yet. It could have been guarded by
- * _TASK_CPU itself, but we want the build to fail if _TASK_CPU is missing
- * when building something else than asm-offsets.s
- */
-#ifdef GENERATING_ASM_OFFSETS
-#define raw_smp_processor_id()		(0)
-#else
-#define raw_smp_processor_id()		(*(unsigned int *)((void *)current + _TASK_CPU))
-#endif
+#define raw_smp_processor_id()		(current_thread_info()->cpu)
 #define hard_smp_processor_id() 	(smp_hw_index[smp_processor_id()])
 
 static inline int get_hard_smp_processor_id(int cpu)
arch/powerpc/kernel/asm-offsets.c +0 -2
index e37e4546034e..cc05522f50bf 100644
@@ -9,8 +9,6 @@
  * #defines from the assembly-language output.
  */
 
-#define GENERATING_ASM_OFFSETS	/* asm/smp.h */
-
 #include <linux/compat.h>
 #include <linux/signal.h>
 #include <linux/sched.h>
Signature · 1 line
2.30.2
#17 lore raw

PATCH v2 7/7 riscv: rely on core code to keep thread_info::cpu updated

In reply to Ard Biesheuvel

Now that the core code switched back to using thread_info::cpu to keep a task's CPU number, we no longer need to keep it in sync explicitly. So just drop the code that does this.

---
 arch/riscv/kernel/asm-offsets.c | 1 -
 arch/riscv/kernel/entry.S       | 5 -----
 arch/riscv/kernel/head.S        | 1 -
 3 files changed, 7 deletions(-)
arch/riscv/kernel/asm-offsets.c +0 -1
index 90f8ce64fa6f..478d9f02dab5 100644
@@ -33,7 +33,6 @@ void asm_offsets(void)
 	OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count);
 	OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp);
 	OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp);
-	OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu);
 
 	OFFSET(TASK_THREAD_F0,  task_struct, thread.fstate.f[0]);
 	OFFSET(TASK_THREAD_F1,  task_struct, thread.fstate.f[1]);
arch/riscv/kernel/entry.S +0 -5
index 98f502654edd..459eb1714353 100644
@@ -544,11 +544,6 @@ ENTRY(__switch_to)
 	REG_L s9,  TASK_THREAD_S9_RA(a4)
 	REG_L s10, TASK_THREAD_S10_RA(a4)
 	REG_L s11, TASK_THREAD_S11_RA(a4)
-	/* Swap the CPU entry around. */
-	lw a3, TASK_TI_CPU(a0)
-	lw a4, TASK_TI_CPU(a1)
-	sw a3, TASK_TI_CPU(a1)
-	sw a4, TASK_TI_CPU(a0)
 	/* The offset of thread_info in task_struct is zero. */
 	move tp, a1
 	ret
arch/riscv/kernel/head.S +0 -1
index fce5184b22c3..d5ec30ef6f5d 100644
@@ -317,7 +317,6 @@ clear_bss_done:
 	call setup_trap_vector
 	/* Restore C environment */
 	la tp, init_task
-	sw zero, TASK_TI_CPU(tp)
 	la sp, init_thread_union + THREAD_SIZE
 
 #ifdef CONFIG_KASAN
Signature · 1 line
2.30.2