public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Vineet Gupta <vgupta@kernel.org>
To: linux-snps-arc@lists.infradead.org
Cc: linux-kernel@vger.kernel.org,
	Shahab Vahedi <Shahab.Vahedi@synopsys.com>,
	Alexey Brodkin <abrodkin@synopsys.com>,
	Vineet Gupta <vgupta@kernel.org>
Subject: [PATCH 12/20] ARC: __switch_to: move ksp to thread_info from thread_struct
Date: Mon, 14 Aug 2023 17:48:05 -0700	[thread overview]
Message-ID: <20230815004813.555115-13-vgupta@kernel.org> (raw)
In-Reply-To: <20230815004813.555115-1-vgupta@kernel.org>

task's arch specific bits are carried in 2 places
 - embedded thread_struct in task_struct
 - associated thread_info (hoisted in task's stack page) and
   syntactically: (thread_info *)(task_struct->stack)

ksp (dynamic kernel stack top) currently lives in thread_struct but
given its deep location in task struct likely to cache miss when
accessed from  __switch_to(). Moving it to thread_info would be more
efficient given proximity to frequently accessed items such as
preempt_count thus very likely to be in cache, specially in schedular
code.

Note however that currently tsk.thread.ksp takes 1 memory access (off
of tsk pointer) while new code tsk->stack.ksp would take 2, but likely
to be in cache. Moreover if task is current the 2nd reference can be
elided and instead derived from SP as (SP & ~(THREAD_SIZE - 1))

All of this also makes __switch_to() code simpler and we can see the 2
ways of retirving ksp (descrobed above) in new code.

Signed-off-by: Vineet Gupta <vgupta@kernel.org>
---
 arch/arc/include/asm/processor.h   |  3 +--
 arch/arc/include/asm/thread_info.h | 10 +++++-----
 arch/arc/kernel/asm-offsets.c      |  2 +-
 arch/arc/kernel/ctx_sw_asm.S       | 22 ++++++++++------------
 arch/arc/kernel/process.c          |  6 +++---
 5 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/arch/arc/include/asm/processor.h b/arch/arc/include/asm/processor.h
index 82ddc929d6e7..d606658e2fe7 100644
--- a/arch/arc/include/asm/processor.h
+++ b/arch/arc/include/asm/processor.h
@@ -22,7 +22,6 @@
  * struct thread_info
  */
 struct thread_struct {
-	unsigned long ksp;	/* kernel mode stack pointer */
 	unsigned long callee_reg;	/* pointer to callee regs */
 	unsigned long fault_address;	/* dbls as brkpt holder as well */
 #ifdef CONFIG_ARC_DSP_SAVE_RESTORE_REGS
@@ -54,7 +53,7 @@ struct task_struct;
  * Where about of Task's sp, fp, blink when it was last seen in kernel mode.
  * Look in process.c for details of kernel stack layout
  */
-#define TSK_K_ESP(tsk)		(tsk->thread.ksp)
+#define TSK_K_ESP(tsk)		(task_thread_info(tsk)->ksp)
 
 #define TSK_K_REG(tsk, off)	(*((unsigned long *)(TSK_K_ESP(tsk) + \
 					sizeof(struct callee_regs) + off)))
diff --git a/arch/arc/include/asm/thread_info.h b/arch/arc/include/asm/thread_info.h
index 6ba7fe417095..4c530cf131f3 100644
--- a/arch/arc/include/asm/thread_info.h
+++ b/arch/arc/include/asm/thread_info.h
@@ -37,16 +37,16 @@
  */
 struct thread_info {
 	unsigned long flags;		/* low level flags */
+	unsigned long ksp;		/* kernel mode stack top in __switch_to */
 	int preempt_count;		/* 0 => preemptable, <0 => BUG */
-	struct task_struct *task;	/* main task structure */
-	__u32 cpu;			/* current CPU */
+	int cpu;			/* current CPU */
 	unsigned long thr_ptr;		/* TLS ptr */
+	struct task_struct *task;	/* main task structure */
 };
 
 /*
- * macros/functions for gaining access to the thread information structure
- *
- * preempt_count needs to be 1 initially, until the scheduler is functional.
+ * initilaize thread_info for any @tsk
+ *  - this is not related to init_task per se
  */
 #define INIT_THREAD_INFO(tsk)			\
 {						\
diff --git a/arch/arc/kernel/asm-offsets.c b/arch/arc/kernel/asm-offsets.c
index 37324fd9a72f..5b47d09b925e 100644
--- a/arch/arc/kernel/asm-offsets.c
+++ b/arch/arc/kernel/asm-offsets.c
@@ -20,13 +20,13 @@ int main(void)
 
 	BLANK();
 
-	DEFINE(THREAD_KSP, offsetof(struct thread_struct, ksp));
 	DEFINE(THREAD_CALLEE_REG, offsetof(struct thread_struct, callee_reg));
 	DEFINE(THREAD_FAULT_ADDR,
 	       offsetof(struct thread_struct, fault_address));
 
 	BLANK();
 
+	DEFINE(THREAD_INFO_KSP, offsetof(struct thread_info, ksp));
 	DEFINE(THREAD_INFO_FLAGS, offsetof(struct thread_info, flags));
 	DEFINE(THREAD_INFO_PREEMPT_COUNT,
 	       offsetof(struct thread_info, preempt_count));
diff --git a/arch/arc/kernel/ctx_sw_asm.S b/arch/arc/kernel/ctx_sw_asm.S
index 59d779004e64..48e1f21976ed 100644
--- a/arch/arc/kernel/ctx_sw_asm.S
+++ b/arch/arc/kernel/ctx_sw_asm.S
@@ -11,8 +11,6 @@
 #include <asm/entry.h>       /* For the SAVE_* macros */
 #include <asm/asm-offsets.h>
 
-#define KSP_WORD_OFF 	((TASK_THREAD + THREAD_KSP) / 4)
-
 ; IN
 ;  - r0: prev task (also current)
 ;  - r1: next task
@@ -37,19 +35,19 @@ ENTRY_CFI(__switch_to)
 	/* kernel mode callee regs of @prev */
 	SAVE_CALLEE_SAVED_KERNEL
 
-	/* save final SP to @prev->thread.ksp */
-#if KSP_WORD_OFF  <= 255
-	st.as  sp, [r0, KSP_WORD_OFF]
-#else
-	/* Workaround for NR_CPUS=4k as ST.as can only take s9 offset */
-	add2	r10, r0, KSP_WORD_OFF
-	st	sp, [r10]
-#endif
+	/*
+	 * save final SP to @prev->thread_info.ksp
+	 * @prev is "current" so thread_info derived from SP
+	 */
+	GET_CURR_THR_INFO_FROM_SP  r10
+	st	sp,  [r10, THREAD_INFO_KSP]
+
 	/* update @next in _current_task[] and GP register caching it */
 	SET_CURR_TASK_ON_CPU  r1, r10
 
-	/* load SP from @next->thread.ksp */
-	ld.as	sp, [r1, KSP_WORD_OFF]
+	/* load SP from @next->thread_info.ksp */
+	ld	r10, [r1, TASK_THREAD_INFO]
+	ld	sp,  [r10, THREAD_INFO_KSP]
 
 	/* restore callee regs, stack frame regs of @next */
 	RESTORE_CALLEE_SAVED_KERNEL
diff --git a/arch/arc/kernel/process.c b/arch/arc/kernel/process.c
index 96f591508142..186ceab661eb 100644
--- a/arch/arc/kernel/process.c
+++ b/arch/arc/kernel/process.c
@@ -141,7 +141,7 @@ asmlinkage void ret_from_fork(void);
  * |    unused      |
  * |                |
  * ------------------
- * |     r25        |   <==== top of Stack (thread.ksp)
+ * |     r25        |   <==== top of Stack (thread_info.ksp)
  * ~                ~
  * |    --to--      |   (CALLEE Regs of kernel mode)
  * |     r13        |
@@ -181,14 +181,14 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
 	c_callee = ((struct callee_regs *)childksp) - 1;
 
 	/*
-	 * __switch_to() uses thread.ksp to start unwinding stack
+	 * __switch_to() uses thread_info.ksp to start unwinding stack
 	 * For kernel threads we don't need to create callee regs, the
 	 * stack layout nevertheless needs to remain the same.
 	 * Also, since __switch_to anyways unwinds callee regs, we use
 	 * this to populate kernel thread entry-pt/args into callee regs,
 	 * so that ret_from_kernel_thread() becomes simpler.
 	 */
-	p->thread.ksp = (unsigned long)c_callee;	/* THREAD_KSP */
+	task_thread_info(p)->ksp = (unsigned long)c_callee;	/* THREAD_INFO_KSP */
 
 	/* __switch_to expects FP(0), BLINK(return addr) at top */
 	childksp[0] = 0;			/* fp */
-- 
2.34.1


  parent reply	other threads:[~2023-08-15  0:49 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-15  0:47 [PATCH 00/20] ARC updates Vineet Gupta
2023-08-15  0:47 ` [PATCH 01/20] ARC: uaccess: remove arc specific out-of-line handles for -Os Vineet Gupta
2023-08-15  0:47 ` [PATCH 02/20] ARC: uaccess: use optimized generic __strnlen_user/__strncpy_from_user Vineet Gupta
2023-08-15  0:47 ` [PATCH 03/20] ARC: uaccess: elide unaliged handling if hardware supports Vineet Gupta
2023-08-15  0:47 ` [PATCH 04/20] ARCv2: memset: don't prefetch for len == 0 which happens a alot Vineet Gupta
2023-08-15  0:47 ` [PATCH 05/20] ARC: boot log: eliminate struct cpuinfo_arc #1: mm Vineet Gupta
2023-08-15  0:47 ` [PATCH 06/20] ARC: boot log: eliminate struct cpuinfo_arc #2: cache Vineet Gupta
2023-08-15  0:48 ` [PATCH 07/20] ARC: boot log: eliminate struct cpuinfo_arc #3: don't export Vineet Gupta
2023-08-15  0:48 ` [PATCH 08/20] ARC: boot log: eliminate struct cpuinfo_arc #4: boot log per ISA Vineet Gupta
2023-08-15  0:48 ` [PATCH 09/20] ARC: entry: use gp to cache task pointer (vs. r25) Vineet Gupta
2023-08-15  0:48 ` [PATCH 10/20] ARC: kernel stack: INIT_THREAD need not setup @init_stack in @ksp Vineet Gupta
2023-08-15  0:48 ` [PATCH 11/20] ARC: __switch_to: asm with dwarf ops (vs. inline asm) Vineet Gupta
2023-08-15  0:48 ` Vineet Gupta [this message]
2023-08-15  0:48 ` [PATCH 13/20] ARC: entry: rework (non-functional) Vineet Gupta
2023-08-15  0:48 ` [PATCH 14/20] ARC: entry: ARcompact EV_ProtV to use r10 directly Vineet Gupta
2023-08-15  0:48 ` [PATCH 15/20] ARC: entry: EV_MachineCheck dont re-read ECR Vineet Gupta
2023-08-15  0:48 ` [PATCH 16/20] ARC: entry: Add more common chores to EXCEPTION_PROLOGUE Vineet Gupta
2023-08-18 12:56   ` Pavel.Kozlov
2023-08-19 23:13     ` Vineet Gupta
2023-08-19 23:14     ` [PATCH v2 " Vineet Gupta
2023-08-15  0:48 ` [PATCH 17/20] ARC: entry: replace 8 byte OR with 4 byte BSET Vineet Gupta
2023-08-15  0:48 ` [PATCH 18/20] ARC: entry: replace 8 byte ADD.ne with 4 byte ADD2.ne Vineet Gupta
2023-08-15  0:48 ` [PATCH 19/20] ARCv2: entry: rearrange pt_regs slightly Vineet Gupta
2023-08-15  0:48 ` [PATCH 20/20] ARC: pt_regs: create seperate type for ecr Vineet Gupta
2023-08-15  6:03   ` kernel test robot
2023-08-18  3:35     ` [PATCH v2 " Vineet Gupta
2023-08-17 12:09   ` [PATCH " Pavel.Kozlov
2023-08-18  3:37     ` Vineet Gupta
2023-08-22 14:07 ` [PATCH 00/20] ARC updates Pavel Kozlov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230815004813.555115-13-vgupta@kernel.org \
    --to=vgupta@kernel.org \
    --cc=Shahab.Vahedi@synopsys.com \
    --cc=abrodkin@synopsys.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-snps-arc@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox