linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eugene Shatokhin <eugene.shatokhin@rosalab.ru>
To: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>,
	Ingo Molnar <mingo@kernel.org>
Cc: Andy Lutomirski <luto@amacapital.net>,
	Ingo Molnar <mingo@redhat.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Eugene Shatokhin <eugene.shatokhin@rosalab.ru>
Subject: [PATCH v2 2/2] kprobes/x86: Use 16 bytes for each instruction slot again
Date: Wed,  3 Jun 2015 10:54:11 +0300	[thread overview]
Message-ID: <1433318051-12509-1-git-send-email-eugene.shatokhin@rosalab.ru> (raw)
In-Reply-To: <1433176331-479-1-git-send-email-eugene.shatokhin@rosalab.ru>

Commit 91e5ed49fca0 ("x86/asm/decoder: Fix and enforce max instruction
size in the insn decoder") has changed MAX_INSN_SIZE from 16 to 15 bytes
on x86.

As a side effect, the slots Kprobes use to store the instructions became
1 byte shorter. This is unfortunate because, for example, the Kprobes'
"boost" feature can not be used now for the instructions of length 11,
like a quite common kind of MOV:
* movq $0xffffffffffffffff,-0x3fe8(%rax) (48 c7 80 18 c0 ff ff ff ff ff ff)
* movq $0x0,0x88(%rdi)                   (48 c7 87 88 00 00 00 00 00 00 00)
and so on.

This patch makes the insn slots 16 bytes long, like they were before
while keeping MAX_INSN_SIZE intact.

Other tools may benefit from this change as well.

Changes in v2:
* Explained in the comments what KPROBE_INSN_SLOT_SIZE is and why it may
  be larger than MAX_INSN_SIZE.
* Added a compile-time check that KPROBE_INSN_SLOT_SIZE is not less than
  MAX_INSN_SIZE.

Signed-off-by: Eugene Shatokhin <eugene.shatokhin@rosalab.ru>
---
 arch/x86/include/asm/kprobes.h | 15 +++++++++++++++
 arch/x86/kernel/kprobes/core.c |  2 +-
 kernel/kprobes.c               | 20 ++++++++++++++++++--
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/arch/x86/include/asm/kprobes.h b/arch/x86/include/asm/kprobes.h
index 4421b5d..ab6e6a0 100644
--- a/arch/x86/include/asm/kprobes.h
+++ b/arch/x86/include/asm/kprobes.h
@@ -28,6 +28,21 @@
 
 #define  __ARCH_WANT_KPROBES_INSN_SLOT
 
+/*
+ * The size of the instruction slot is greater than the maximum length of
+ * an instruction (15 bytes) for Kprobes to be able to use "boost" for
+ * longer instructions.
+ *
+ * "Boost" allows to avoid single-stepping over an instruction if the Kprobe
+ * has no post handler. A jump to the next instruction is placed after the
+ * copied instruction in the slot for that to work.
+ *
+ * The length of the relative jump instruction is 5 bytes. With the slot
+ * size of 16, "boost" can be used for the instructions up to 11 bytes long,
+ * including rather common kinds of "MOV r/m64, imm32" (opcode 0xc7).
+ */
+#define KPROBE_INSN_SLOT_SIZE 16
+
 struct pt_regs;
 struct kprobe;
 
diff --git a/arch/x86/kernel/kprobes/core.c b/arch/x86/kernel/kprobes/core.c
index 0a42b76..1067f90 100644
--- a/arch/x86/kernel/kprobes/core.c
+++ b/arch/x86/kernel/kprobes/core.c
@@ -881,7 +881,7 @@ static void resume_execution(struct kprobe *p, struct pt_regs *regs,
 
 	if (p->ainsn.boostable == 0) {
 		if ((regs->ip > copy_ip) &&
-		    (regs->ip - copy_ip) + 5 <= MAX_INSN_SIZE) {
+		    (regs->ip - copy_ip) + 5 <= KPROBE_INSN_SLOT_SIZE) {
 			/*
 			 * These instructions can be executed directly if it
 			 * jumps back to correct address.
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index c90e417..92788a4 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -57,7 +57,6 @@
 #define KPROBE_HASH_BITS 6
 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
 
-
 /*
  * Some oddball architectures like 64bit powerpc have function descriptors
  * so this must be overridable.
@@ -90,6 +89,23 @@ static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
 static LIST_HEAD(kprobe_blacklist);
 
 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
+
+/*
+ * An instruction slot contains a copy of the probed instruction, relocated
+ * if needed. In some cases, it may be necessary to place additional
+ * instructions into that slot, so, the size of the slot may be larger than
+ * the maximum length of an instruction.
+ * Currently, the slots larger than MAX_INSN_SIZE may only be needed on x86
+ * to implement Kprobe "boost". Other architectures do not need to define
+ * KPROBE_INSN_SLOT_SIZE explicitly.
+ */
+#ifndef KPROBE_INSN_SLOT_SIZE
+#define KPROBE_INSN_SLOT_SIZE MAX_INSN_SIZE
+#endif
+#if (KPROBE_INSN_SLOT_SIZE < MAX_INSN_SIZE)
+#error "Size of an instruction slot must not be less than MAX_INSN_SIZE."
+#endif
+
 /*
  * kprobe->ainsn.insn points to the copy of the instruction to be
  * single-stepped. x86_64, POWER4 and above have no-exec support and
@@ -135,7 +151,7 @@ struct kprobe_insn_cache kprobe_insn_slots = {
 	.alloc = alloc_insn_page,
 	.free = free_insn_page,
 	.pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
-	.insn_size = MAX_INSN_SIZE,
+	.insn_size = KPROBE_INSN_SLOT_SIZE,
 	.nr_garbage = 0,
 };
 static int collect_garbage_slots(struct kprobe_insn_cache *c);
-- 
2.3.2


  parent reply	other threads:[~2015-06-03  7:54 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-01 16:32 [PATCH 0/2] kprobes/x86: Allow "boost" for 10- and 11-byte instructions Eugene Shatokhin
2015-06-01 16:32 ` [PATCH 1/2] kprobes/x86: boost: Fix checking if there is enough room for a jump Eugene Shatokhin
2015-06-01 21:51   ` Masami Hiramatsu
2015-06-01 16:32 ` [PATCH 2/2] kprobes/x86: Use 16 bytes for each instruction slot again Eugene Shatokhin
2015-06-01 17:04   ` Andy Lutomirski
2015-06-01 21:49     ` Masami Hiramatsu
2015-06-01 21:57       ` Andy Lutomirski
2015-06-02 21:42         ` Masami Hiramatsu
2015-06-02  5:44       ` Ingo Molnar
2015-06-02 21:46         ` Masami Hiramatsu
2015-06-02 21:55           ` Andy Lutomirski
2015-06-04 21:59             ` Masami Hiramatsu
2015-06-03  7:28           ` Ingo Molnar
2015-06-01 21:58   ` Masami Hiramatsu
2015-06-02  5:47   ` Ingo Molnar
2015-06-01 21:44 ` [PATCH 0/2] kprobes/x86: Allow "boost" for 10- and 11-byte instructions Masami Hiramatsu
2015-06-03  7:54 ` Eugene Shatokhin [this message]
2015-06-04 14:42   ` [PATCH v2 2/2] kprobes/x86: Use 16 bytes for each instruction slot again Jeff Epler

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=1433318051-12509-1-git-send-email-eugene.shatokhin@rosalab.ru \
    --to=eugene.shatokhin@rosalab.ru \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=masami.hiramatsu.pt@hitachi.com \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.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;
as well as URLs for NNTP newsgroup(s).