public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Stefani Seibold <stefani@seibold.net>
To: linux-kernel <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Americo Wang <xiyou.wangcong@gmail.com>,
	Thomas Gleixner <tglx@linutronix.de>, Ingo Molnar <mingo@elte.hu>,
	Andi Kleen <andi@firstfloor.org>
Subject: [PATCH] RFC x86_64 more accurate KSTK_ESP implementation
Date: Thu, 05 Nov 2009 09:19:49 +0100	[thread overview]
Message-ID: <1257409189.26874.18.camel@wall-e> (raw)
In-Reply-To: <1257239184.4889.15.camel@wall-e>

Hi,

this is a RFC for a more accurate KSTK_ESP implementation for the x86_64
architecture.

Because the usersp will be only updated by a context switch this value
is most of the time outdated. This patch update the per CPU variable
old_rsp in the device and timer interrupt too.  

In my opinion this can be save done if the current stack pointer is
outside the kernel stack of the current task and the instruction pointer
is not inside the kernel.

The old_rsp value will be stored in usersp in case of a context switch.

The KSTK_ESP will get the value from old_rsp in case the task is the
current task, otherwise it will read usersp.

I know about the performance coast, so this is why i ask for comments.

Stefani

Signed-off-by: Stefani Seibold <stefani@seibold.net>

 include/asm/processor.h |    4 +++-
 kernel/apic/apic.c      |    3 +++
 kernel/irq_64.c         |    1 +
 kernel/process_64.c     |   20 ++++++++++++++++++++
 4 files changed, 27 insertions(+), 1 deletion(-)

--- linux-2.6.32-rc5.old/arch/x86/include/asm/processor.h	2009-10-16 02:41:50.000000000 +0200
+++ linux-2.6.32-rc5.new/arch/x86/include/asm/processor.h	2009-11-05 08:28:23.765300812 +0100
@@ -1000,7 +1000,7 @@
 #define thread_saved_pc(t)	(*(unsigned long *)((t)->thread.sp - 8))
 
 #define task_pt_regs(tsk)	((struct pt_regs *)(tsk)->thread.sp0 - 1)
-#define KSTK_ESP(tsk)		-1 /* sorry. doesn't work for syscall. */
+extern unsigned long KSTK_ESP(struct task_struct *task);
 #endif /* CONFIG_X86_64 */
 
 extern void start_thread(struct pt_regs *regs, unsigned long new_ip,
@@ -1052,4 +1052,6 @@
 	return ratio;
 }
 
+extern void update_usersp(struct pt_regs *regs);
+
 #endif /* _ASM_X86_PROCESSOR_H */
--- linux-2.6.32-rc5.old/arch/x86/kernel/process_64.c	2009-10-16 02:41:50.000000000 +0200
+++ linux-2.6.32-rc5.new/arch/x86/kernel/process_64.c	2009-11-05 08:52:39.965227285 +0100
@@ -664,3 +664,23 @@
 	return do_arch_prctl(current, code, addr);
 }
 
+void update_usersp(struct pt_regs *regs)
+{
+	unsigned long stk = (unsigned long)task_stack_page(current);
+	unsigned long stkp = (regs)->sp; 
+
+	if (((stkp < stk) || (stkp >= stk + THREAD_SIZE))
+	    && regs->ip < PAGE_OFFSET)
+		percpu_write(old_rsp, stkp);
+}
+
+unsigned long KSTK_ESP(struct task_struct *task)
+{
+	if (test_tsk_thread_flag(task, TIF_IA32)) 
+		return task_pt_regs(task)->sp;
+
+	if (task != current)
+		return task->thread.usersp;
+
+	return percpu_read(old_rsp);
+}
--- linux-2.6.32-rc5.old/arch/x86/kernel/irq_64.c	2009-10-16 02:41:50.000000000 +0200
+++ linux-2.6.32-rc5.new/arch/x86/kernel/irq_64.c	2009-11-04 22:29:55.762951577 +0100
@@ -53,6 +53,7 @@
 	struct irq_desc *desc;
 
 	stack_overflow_check(regs);
+	update_usersp(regs);
 
 	desc = irq_to_desc(irq);
 	if (unlikely(!desc))
--- linux-2.6.32-rc5.old/arch/x86/kernel/apic/apic.c	2009-10-16 02:41:50.000000000 +0200
+++ linux-2.6.32-rc5.new/arch/x86/kernel/apic/apic.c	2009-11-04 23:12:32.805086991 +0100
@@ -831,6 +831,9 @@
 {
 	struct pt_regs *old_regs = set_irq_regs(regs);
 
+#ifndef CONFIG_X86_32
+	update_usersp(regs);
+#endif
 	/*
 	 * NOTE! We'd better ACK the irq immediately,
 	 * because timer handling can be slow.



  parent reply	other threads:[~2009-11-05  8:20 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-03  7:31 [PATCH] update fix X86_64 procfs provide stack information for threads Stefani Seibold
2009-11-03  8:28 ` Ingo Molnar
2009-11-03  9:06   ` Stefani Seibold
2009-11-03 18:16     ` Ingo Molnar
2009-11-05  8:19     ` Stefani Seibold [this message]
2009-11-05 11:08       ` [PATCH] RFC x86_64 more accurate KSTK_ESP implementation Andi Kleen
2009-11-05 12:11         ` Stefani Seibold
2009-11-08 11:35       ` Ingo Molnar
2009-11-08 12:51         ` Stefani Seibold
2009-11-08 12:55           ` Ingo Molnar
2009-11-08 14:00             ` Stefani Seibold
2009-11-08 16:34               ` H. Peter Anvin
2009-11-08 19:37         ` Andi Kleen
2009-11-05 13:02     ` [PATCH] fix /proc/<pid>/stat stack pointer for kernel threads Stefani Seibold
2009-11-13  8:01     ` [tip:x86/urgent] fs: " Stefani Seibold
2009-11-04 11:17 ` [PATCH] update fix X86_64 procfs provide stack information for threads Andi Kleen
2009-11-04 11:50   ` Stefani Seibold
2009-11-04 12:00     ` Andi Kleen
2009-11-04 12:22       ` Stefani Seibold
2009-11-04 15:42       ` Stefani Seibold
2009-11-04 22:21       ` Stefani Seibold

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=1257409189.26874.18.camel@wall-e \
    --to=stefani@seibold.net \
    --cc=akpm@linux-foundation.org \
    --cc=andi@firstfloor.org \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=tglx@linutronix.de \
    --cc=xiyou.wangcong@gmail.com \
    /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