Skip to contentSkip to conversation

ftrace, kprobes: Introduce IPMODIFY flag for ftrace_ops to detect conflicts

PATCH Superseded Masami Hiramatsu 4 participants 17 messages Jun 10, 2014 – Jun 13, 2014

Subsystems (4)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover ftrace, kprobes: Introduce IPMODIFY flag for ftrace_ops to detect conflicts

Hi,

Here is a pair of patches which introduces IPMODIFY flag for ftrace_ops to detect conflicts of ftrace users who can modify regs->ip in their handler.
Currently, only kprobes can change the regs->ip in the handler, but recently kpatch is also want to change it. Moreover, since the ftrace itself exported to modules, it might be considerable senario.

Here we talked on github.
https://github.com/dynup/kpatch/issues/47

To protect modified regs-ip from each other, this series introduces FTRACE_OPS_FL_IPMODIFY flag and ftrace now ensures the flag can be set on each function entry location. If there is someone who already reserve regs->ip on target function entry, ftrace_set_filter_ip or register_ftrace_function will return -EBUSY. Users must handle that.

At this point, all kprobes will reserve regs->ip, since jprobe requires it.

Thank you,

---

Masami Hiramatsu (2):
ftrace: Simplify ftrace_hash_disable/enable path in ftrace_hash_move ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

 Documentation/trace/ftrace.txt |    5 +
 include/linux/ftrace.h         |   10 ++-
 kernel/kprobes.c               |    2 -
 kernel/trace/ftrace.c          |  152 ++++++++++++++++++++++++++++++++++------
 4 files changed, 143 insertions(+), 26 deletions(-)

--

#1 lore raw

PATCH 1/2 ftrace: Simplify ftrace_hash_disable/enable path in ftrace_hash_move

Simplify ftrace_hash_disable/enable path in ftrace_hash_move for hardening the process if the memory allocation failed.

Cc Steven Rostedt <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 kernel/trace/ftrace.c |   33 +++++++++++----------------------
 1 file changed, 11 insertions(+), 22 deletions(-)
kernel/trace/ftrace.c +11 -22
index cc07b7f..e355b60 100644
@@ -1304,25 +1304,15 @@ ftrace_hash_move(struct ftrace_ops *ops, int enable,
 	struct ftrace_hash *new_hash;
 	int size = src->count;
 	int bits = 0;
-	int ret;
 	int i;
 
 	/*
-	 * Remove the current set, update the hash and add
-	 * them back.
-	 */
-	ftrace_hash_rec_disable(ops, enable);
-
-	/*
 	 * If the new source is empty, just free dst and assign it
 	 * the empty_hash.
 	 */
 	if (!src->count) {
-		free_ftrace_hash_rcu(*dst);
-		rcu_assign_pointer(*dst, EMPTY_HASH);
-		/* still need to update the function records */
-		ret = 0;
-		goto out;
+		new_hash = EMPTY_HASH;
+		goto update;
 	}
 
 	/*
@@ -1335,10 +1325,9 @@ ftrace_hash_move(struct ftrace_ops *ops, int enable,
 	if (bits > FTRACE_HASH_MAX_BITS)
 		bits = FTRACE_HASH_MAX_BITS;
 
-	ret = -ENOMEM;
 	new_hash = alloc_ftrace_hash(bits);
 	if (!new_hash)
-		goto out;
+		return -ENOMEM;
 
 	size = 1 << src->size_bits;
 	for (i = 0; i < size; i++) {
@@ -1349,20 +1338,20 @@ ftrace_hash_move(struct ftrace_ops *ops, int enable,
 		}
 	}
 
+update:
+	/*
+	 * Remove the current set, update the hash and add
+	 * them back.
+	 */
+	ftrace_hash_rec_disable(ops, enable);
+
 	old_hash = *dst;
 	rcu_assign_pointer(*dst, new_hash);
 	free_ftrace_hash_rcu(old_hash);
 
-	ret = 0;
- out:
-	/*
-	 * Enable regardless of ret:
-	 *  On success, we enable the new hash.
-	 *  On failure, we re-enable the original hash.
-	 */
 	ftrace_hash_rec_enable(ops, enable);
 
-	return ret;
+	return 0;
 }
 
 /*
#2 lore raw

PATCH 2/2 ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

In reply to Masami Hiramatsu

Introduce FTRACE_OPS_FL_IPMODIFY to avoid conflict among ftrace users who may modify regs->ip to change the execution path. This also adds the flag to kprobe_ftrace_ops, since ftrace-based kprobes already modifies regs->ip. Thus, if another user modifies the regs->ip on the same function entry, one of them will be broken. So both should add IPMODIFY flag and make sure that ftrace_set_filter_ip() succeeds.

Note that currently conflicts of IPMODIFY are detected on the filter hash. It does NOT care about the notrace hash. This means that if you set filter hash all functions and notrace(mask) some of them, the IPMODIFY flag will be applied to all functions.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 Documentation/trace/ftrace.txt |    5 ++
 include/linux/ftrace.h         |   10 +++
 kernel/kprobes.c               |    2 -
 kernel/trace/ftrace.c          |  121 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 133 insertions(+), 5 deletions(-)
Documentation/trace/ftrace.txt +5 -0
index 2479b2a..0fcad7d 100644
@@ -234,6 +234,11 @@ of ftrace. Here is a list of some of the key files:
 	will be displayed on the same line as the function that
 	is returning registers.
 
+	If the callback registered to be traced by a function with
+	the "ip modify" attribute (thus the regs->ip can be changed),
+	a 'I' will be displayed on the same line as the function that
+	can be overridden.
+
   function_profile_enabled:
 
 	When set it will enable all functions with either the function
include/linux/ftrace.h +8 -2
index 3e6dfb3..517112a 100644
@@ -90,6 +90,9 @@ typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
  * INITIALIZED - The ftrace_ops has already been initialized (first use time
  *            register_ftrace_function() is called, it will initialized the ops)
  * DELETED - The ops are being deleted, do not let them be registered again.
+ * IPMODIFY - The ops can modify IP register. This must be set with SAVE_REGS
+ *            and if the other ops has been set this on same function, filter
+ *            update must be failed.
  */
 enum {
 	FTRACE_OPS_FL_ENABLED			= 1 << 0,
@@ -101,6 +104,7 @@ enum {
 	FTRACE_OPS_FL_STUB			= 1 << 6,
 	FTRACE_OPS_FL_INITIALIZED		= 1 << 7,
 	FTRACE_OPS_FL_DELETED			= 1 << 8,
+	FTRACE_OPS_FL_IPMODIFY			= 1 << 9,
 };
 
 /*
@@ -306,6 +310,7 @@ extern int ftrace_nr_registered_ops(void);
  * the dyn_ftrace descriptor represents.
  *
  * The second part is a mask:
+ *  IPMODIFY - the record wants to change IP address.
  *  ENABLED - the function is being traced
  *  REGS    - the record wants the function to save regs
  *  REGS_EN - the function is set up to save regs.
@@ -317,13 +322,14 @@ extern int ftrace_nr_registered_ops(void);
  * from tracing that function.
  */
 enum {
+	FTRACE_FL_IPMODIFY	= (1UL << 28),
 	FTRACE_FL_ENABLED	= (1UL << 29),
 	FTRACE_FL_REGS		= (1UL << 30),
 	FTRACE_FL_REGS_EN	= (1UL << 31)
 };
 
-#define FTRACE_FL_MASK		(0x7UL << 29)
-#define FTRACE_REF_MAX		((1UL << 29) - 1)
+#define FTRACE_FL_MASK		(0xfUL << 28)
+#define FTRACE_REF_MAX		((1UL << 28) - 1)
 
 struct dyn_ftrace {
 	unsigned long		ip; /* address of mcount call-site */
kernel/kprobes.c +1 -1
index ceeadfc..3ff7dbd 100644
@@ -926,7 +926,7 @@ static __kprobes struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
 #ifdef CONFIG_KPROBES_ON_FTRACE
 static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
 	.func = kprobe_ftrace_handler,
-	.flags = FTRACE_OPS_FL_SAVE_REGS,
+	.flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
 };
 static int kprobe_ftrace_enabled;
 
kernel/trace/ftrace.c +119 -2

164 lines not on this page · raw message

#3 lore raw

Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

Hi Masami,

2014-06-10 (화), 10:50 +0000, Masami Hiramatsu:

Quoted 14 lines

Introduce FTRACE_OPS_FL_IPMODIFY to avoid conflict among ftrace users who may modify regs->ip to change the execution path. This also adds the flag to kprobe_ftrace_ops, since ftrace-based kprobes already modifies regs->ip. Thus, if another user modifies the regs->ip on the same function entry, one of them will be broken. So both should add IPMODIFY flag and make sure that ftrace_set_filter_ip() succeeds.

Note that currently conflicts of IPMODIFY are detected on the filter hash. It does NOT care about the notrace hash. This means that if you set filter hash all functions and notrace(mask) some of them, the IPMODIFY flag will be applied to all functions.

[SNIP]

Quoted 11 lines
kernel/trace/ftrace.c +11 -0
+static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
+					 struct ftrace_hash *old_hash,
+					 struct ftrace_hash *new_hash)
+{
+	struct ftrace_page *pg;
+	struct dyn_ftrace *rec, *end = NULL;
+	int in_old, in_new;
+
+	/* Only update if the ops has been registered */
+	if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
+		return 0;

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1627

Quoted 10 lines
kernel/trace/ftrace.c +10 -0
+
+	if (!(ops->flags & FTRACE_OPS_FL_SAVE_REGS) ||
+	    !(ops->flags & FTRACE_OPS_FL_IPMODIFY))
+		return 0;
+
+	/* Update rec->flags */
+	do_for_each_ftrace_rec(pg, rec) {
+		/* We need to update only differences of filter_hash */
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);

Why not use ftrace_hash_empty() here instead of checking NULL? Also return value of ftrace_lookup_ip is not boolean.. maybe you need to add !! or convert type of the in_{old,new} to bool.

Quoted 36 lines
kernel/trace/ftrace.c +36 -0
+		if (in_old == in_new)
+			continue;
+
+		if (in_new) {
+			/* New entries must ensure no others are using it */
+			if (rec->flags & FTRACE_FL_IPMODIFY)
+				goto rollback;
+			rec->flags |= FTRACE_FL_IPMODIFY;
+		} else /* Removed entry */
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+	return 0;
+
+rollback:
+	end = rec;
+
+	/* Roll back what we did above */
+	do_for_each_ftrace_rec(pg, rec) {
+		if (rec == end)
+			goto err_out;
+
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);
+		if (in_old == in_new)
+			continue;
+
+		if (in_new)
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+		else
+			rec->flags |= FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+err_out:
+	return -EBUSY;
+}

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1673

Quoted 10 lines
kernel/trace/ftrace.c +10 -0
+
+static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
+}

Please see above comment. You can pass an empty hash as is, or pass NULL as second arg. The same goes to below...

Thanks,
Namhyung

Quoted 26 lines
kernel/trace/ftrace.c +26 -0
+
+/* Disabling always succeeds */
+static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	__ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
+}
+
+static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
+				       struct ftrace_hash *new_hash)
+{
+	struct ftrace_hash *old_hash = ops->filter_hash;
+
+	if (ftrace_hash_empty(old_hash))
+		old_hash = NULL;
+
+	if (ftrace_hash_empty(new_hash))
+		new_hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
+}
+
#4 lore raw

Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

Quoted 34 lines · Namhyung Kim

Hi Masami,

2014-06-10 (화), 10:50 +0000, Masami Hiramatsu:

Quoted 14 lines

Introduce FTRACE_OPS_FL_IPMODIFY to avoid conflict among ftrace users who may modify regs->ip to change the execution path. This also adds the flag to kprobe_ftrace_ops, since ftrace-based kprobes already modifies regs->ip. Thus, if another user modifies the regs->ip on the same function entry, one of them will be broken. So both should add IPMODIFY flag and make sure that ftrace_set_filter_ip() succeeds.

Note that currently conflicts of IPMODIFY are detected on the filter hash. It does NOT care about the notrace hash. This means that if you set filter hash all functions and notrace(mask) some of them, the IPMODIFY flag will be applied to all functions.

[SNIP]

Quoted 15 lines
kernel/trace/ftrace.c +15 -0
+static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
+					 struct ftrace_hash *old_hash,
+					 struct ftrace_hash *new_hash)
+{
+	struct ftrace_page *pg;
+	struct dyn_ftrace *rec, *end = NULL;
+	int in_old, in_new;
+
+	/* Only update if the ops has been registered */
+	if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
+		return 0;
+
+	if (!(ops->flags & FTRACE_OPS_FL_SAVE_REGS) ||
+	    !(ops->flags & FTRACE_OPS_FL_IPMODIFY))
+		return 0;
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1627

Quoted 6 lines
kernel/trace/ftrace.c +6 -0
+
+	/* Update rec->flags */
+	do_for_each_ftrace_rec(pg, rec) {
+		/* We need to update only differences of filter_hash */
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);

Why not use ftrace_hash_empty() here instead of checking NULL?

Ah, a trick is here. Since an empty filter_hash must hit all, we can not enable/disable filter_hash if we use ftrace_hash_empty() here.

To enabling the new_hash, old_hash must be EMPTY_HASH which means in_old always be false. To disabling, new_hash is EMPTY_HASH too. Please see ftrace_hash_ipmodify_enable/disable/update().

Quoted 3 lines

Also
return value of ftrace_lookup_ip is not boolean.. maybe you need to add !! or convert type of the in_{old,new} to bool.

Yeah, I see. And there is '||' (logical OR) which evaluates the result as boolean. :)

Quoted 43 lines
Quoted 43 lines
kernel/trace/ftrace.c +41 -0
+		if (in_old == in_new)
+			continue;
+
+		if (in_new) {
+			/* New entries must ensure no others are using it */
+			if (rec->flags & FTRACE_FL_IPMODIFY)
+				goto rollback;
+			rec->flags |= FTRACE_FL_IPMODIFY;
+		} else /* Removed entry */
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+	return 0;
+
+rollback:
+	end = rec;
+
+	/* Roll back what we did above */
+	do_for_each_ftrace_rec(pg, rec) {
+		if (rec == end)
+			goto err_out;
+
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);
+		if (in_old == in_new)
+			continue;
+
+		if (in_new)
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+		else
+			rec->flags |= FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+err_out:
+	return -EBUSY;
+}
+
+static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1673

Quoted 5 lines
kernel/trace/ftrace.c +5 -0
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
+}

Please see above comment. You can pass an empty hash as is, or pass NULL as second arg. The same goes to below...

As I said above, that is by design :). EMPTY_HASH means it hits nothing, NULL means it hits all.

Quoted 34 lines

Thanks,
Namhyung

Quoted 26 lines
kernel/trace/ftrace.c +26 -0
+
+/* Disabling always succeeds */
+static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	__ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
+}
+
+static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
+				       struct ftrace_hash *new_hash)
+{
+	struct ftrace_hash *old_hash = ops->filter_hash;
+
+	if (ftrace_hash_empty(old_hash))
+		old_hash = NULL;
+
+	if (ftrace_hash_empty(new_hash))
+		new_hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
+}
+
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#5 lore raw

Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

Hi Masami,

Quoted 44 lines · Masami Hiramatsu
Quoted 34 lines · Namhyung Kim

Hi Masami,

2014-06-10 (화), 10:50 +0000, Masami Hiramatsu:

Quoted 14 lines

Introduce FTRACE_OPS_FL_IPMODIFY to avoid conflict among ftrace users who may modify regs->ip to change the execution path. This also adds the flag to kprobe_ftrace_ops, since ftrace-based kprobes already modifies regs->ip. Thus, if another user modifies the regs->ip on the same function entry, one of them will be broken. So both should add IPMODIFY flag and make sure that ftrace_set_filter_ip() succeeds.

Note that currently conflicts of IPMODIFY are detected on the filter hash. It does NOT care about the notrace hash. This means that if you set filter hash all functions and notrace(mask) some of them, the IPMODIFY flag will be applied to all functions.

[SNIP]

Quoted 15 lines
kernel/trace/ftrace.c +15 -0
+static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
+					 struct ftrace_hash *old_hash,
+					 struct ftrace_hash *new_hash)
+{
+	struct ftrace_page *pg;
+	struct dyn_ftrace *rec, *end = NULL;
+	int in_old, in_new;
+
+	/* Only update if the ops has been registered */
+	if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
+		return 0;
+
+	if (!(ops->flags & FTRACE_OPS_FL_SAVE_REGS) ||
+	    !(ops->flags & FTRACE_OPS_FL_IPMODIFY))
+		return 0;
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1627

Quoted 6 lines
kernel/trace/ftrace.c +6 -0
+
+	/* Update rec->flags */
+	do_for_each_ftrace_rec(pg, rec) {
+		/* We need to update only differences of filter_hash */
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);

Why not use ftrace_hash_empty() here instead of checking NULL?

Quoted 6 lines

Ah, a trick is here. Since an empty filter_hash must hit all, we can not enable/disable filter_hash if we use ftrace_hash_empty() here.

To enabling the new_hash, old_hash must be EMPTY_HASH which means in_old always be false. To disabling, new_hash is EMPTY_HASH too. Please see ftrace_hash_ipmodify_enable/disable/update().

I'm confused. 8-p I guess what you want to do is checking records in either of the filter_hash, right? If so, what about this?

	in_old = !ftrace_hash_empty(old_hash) && ftrace_lookup_ip(old_hash, rec->ip);
	in_new = !ftrace_hash_empty(new_hash) && ftrace_lookup_ip(new_hash, rec->ip);
Quoted 7 lines
Quoted 4 lines

Also
return value of ftrace_lookup_ip is not boolean.. maybe you need to add !! or convert type of the in_{old,new} to bool.

Yeah, I see. And there is '||' (logical OR) which evaluates the result as boolean. :)

Argh... you're right! :)

Quoted 53 lines
Quoted 44 lines
Quoted 44 lines
kernel/trace/ftrace.c +41 -0
+		if (in_old == in_new)
+			continue;
+
+		if (in_new) {
+			/* New entries must ensure no others are using it */
+			if (rec->flags & FTRACE_FL_IPMODIFY)
+				goto rollback;
+			rec->flags |= FTRACE_FL_IPMODIFY;
+		} else /* Removed entry */
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+	return 0;
+
+rollback:
+	end = rec;
+
+	/* Roll back what we did above */
+	do_for_each_ftrace_rec(pg, rec) {
+		if (rec == end)
+			goto err_out;
+
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);
+		if (in_old == in_new)
+			continue;
+
+		if (in_new)
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+		else
+			rec->flags |= FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+err_out:
+	return -EBUSY;
+}
+
+static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1673

Quoted 5 lines
kernel/trace/ftrace.c +5 -0
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
+}

Please see above comment. You can pass an empty hash as is, or pass NULL as second arg. The same goes to below...

Quoted 2 lines

As I said above, that is by design :). EMPTY_HASH means it hits nothing, NULL means it hits all.

But doesn't it make unrelated records also get the flag updated? I'm curious when new_hash can be empty on _enable() case..

Thanks,
Namhyung

#6 lore raw

Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

Quoted 54 lines · Namhyung Kim

Hi Masami,

Quoted 44 lines · Masami Hiramatsu
Quoted 34 lines · Namhyung Kim

Hi Masami,

2014-06-10 (화), 10:50 +0000, Masami Hiramatsu:

Quoted 14 lines

Introduce FTRACE_OPS_FL_IPMODIFY to avoid conflict among ftrace users who may modify regs->ip to change the execution path. This also adds the flag to kprobe_ftrace_ops, since ftrace-based kprobes already modifies regs->ip. Thus, if another user modifies the regs->ip on the same function entry, one of them will be broken. So both should add IPMODIFY flag and make sure that ftrace_set_filter_ip() succeeds.

Note that currently conflicts of IPMODIFY are detected on the filter hash. It does NOT care about the notrace hash. This means that if you set filter hash all functions and notrace(mask) some of them, the IPMODIFY flag will be applied to all functions.

[SNIP]

Quoted 15 lines
kernel/trace/ftrace.c +15 -0
+static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
+					 struct ftrace_hash *old_hash,
+					 struct ftrace_hash *new_hash)
+{
+	struct ftrace_page *pg;
+	struct dyn_ftrace *rec, *end = NULL;
+	int in_old, in_new;
+
+	/* Only update if the ops has been registered */
+	if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
+		return 0;
+
+	if (!(ops->flags & FTRACE_OPS_FL_SAVE_REGS) ||
+	    !(ops->flags & FTRACE_OPS_FL_IPMODIFY))
+		return 0;
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1627

Quoted 6 lines
kernel/trace/ftrace.c +6 -0
+
+	/* Update rec->flags */
+	do_for_each_ftrace_rec(pg, rec) {
+		/* We need to update only differences of filter_hash */
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);

Why not use ftrace_hash_empty() here instead of checking NULL?

Quoted 6 lines

Ah, a trick is here. Since an empty filter_hash must hit all, we can not enable/disable filter_hash if we use ftrace_hash_empty() here.

To enabling the new_hash, old_hash must be EMPTY_HASH which means in_old always be false. To disabling, new_hash is EMPTY_HASH too. Please see ftrace_hash_ipmodify_enable/disable/update().

Quoted 5 lines

I'm confused. 8-p I guess what you want to do is checking records in either of the filter_hash, right? If so, what about this?

	in_old = !ftrace_hash_empty(old_hash) && ftrace_lookup_ip(old_hash, rec->ip);
	in_new = !ftrace_hash_empty(new_hash) && ftrace_lookup_ip(new_hash, rec->ip);

NO, ftrace_lookup_ip() returns NULL if the hash is empty, so adding !ftrace_hash_empty() is meaningless :)

Actually, here I intended to have 3 meanings for the new/old_hash arguments,
- If it is NULL, it hits all
- If it is EMPTY_HASH, it hits nothing
- If it has some entries, it hits those entries.

And in ftrace.c(__ftrace_hash_rec_update), AFAICS, ops->filter_hash has only 2 meanings,
- If it is EMPTY_HASH or NULL, it hits all
- If it has some entries, it hits those entries.

So I had to do above change...

Quoted 62 lines
Quoted 6 lines
Quoted 3 lines

Also
return value of ftrace_lookup_ip is not boolean.. maybe you need to add !! or convert type of the in_{old,new} to bool.

Yeah, I see. And there is '||' (logical OR) which evaluates the result as boolean. :)

Argh... you're right! :)

Quoted 53 lines
Quoted 52 lines
Quoted 39 lines
kernel/trace/ftrace.c +36 -0
+		if (in_old == in_new)
+			continue;
+
+		if (in_new) {
+			/* New entries must ensure no others are using it */
+			if (rec->flags & FTRACE_FL_IPMODIFY)
+				goto rollback;
+			rec->flags |= FTRACE_FL_IPMODIFY;
+		} else /* Removed entry */
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+	return 0;
+
+rollback:
+	end = rec;
+
+	/* Roll back what we did above */
+	do_for_each_ftrace_rec(pg, rec) {
+		if (rec == end)
+			goto err_out;
+
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);
+		if (in_old == in_new)
+			continue;
+
+		if (in_new)
+			rec->flags &= ~FTRACE_FL_IPMODIFY;
+		else
+			rec->flags |= FTRACE_FL_IPMODIFY;
+	} while_for_each_ftrace_rec();
+
+err_out:
+	return -EBUSY;
+}

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1673

Quoted 10 lines
kernel/trace/ftrace.c +10 -0
+
+static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
+}

Please see above comment. You can pass an empty hash as is, or pass NULL as second arg. The same goes to below...

Quoted 5 lines
Quoted 2 lines

As I said above, that is by design :). EMPTY_HASH means it hits nothing, NULL means it hits all.

But doesn't it make unrelated records also get the flag updated? I'm curious when new_hash can be empty on _enable() case..

NO, _enable() is called right before ftrace_hash_rec_enable(ops,1) which always enables filter_hash (since the 2nd arg is 1). If the filter_hash is empty, ftrace_hash_rec_enable() enables ftrace_ops on all ftrace_recs.

Ah, but I found I made a redundant mistake (different one) in ftrace_hash_move(), ftrace_hash_ipmodify_update() should be done only if "enable" is set (that means ftrace_hash_move() updates filter_hash, not notrace_hash). I'll update this patch.

Thank you, :)

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#7 lore raw

Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

Hi Masami,

Quoted 24 lines · Masami Hiramatsu
Quoted 23 lines · Namhyung Kim

Hi Masami,

Quoted 19 lines · Masami Hiramatsu
Quoted 11 lines · Namhyung Kim

Hi Masami,

2014-06-10 (화), 10:50 +0000, Masami Hiramatsu:

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1627

Quoted 6 lines

Introduce FTRACE_OPS_FL_IPMODIFY to avoid conflict among

kernel/trace/ftrace.c +5 -0
+	/* Update rec->flags */
+	do_for_each_ftrace_rec(pg, rec) {
+		/* We need to update only differences of filter_hash */
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);

Why not use ftrace_hash_empty() here instead of checking NULL?

Ah, a trick is here. Since an empty filter_hash must hit all, we can not enable/disable filter_hash if we use ftrace_hash_empty() here.

To enabling the new_hash, old_hash must be EMPTY_HASH which means in_old always be false. To disabling, new_hash is EMPTY_HASH too. Please see ftrace_hash_ipmodify_enable/disable/update().

Quoted 8 lines
Quoted 5 lines

I'm confused. 8-p I guess what you want to do is checking records in either of the filter_hash, right? If so, what about this?

	in_old = !ftrace_hash_empty(old_hash) && ftrace_lookup_ip(old_hash, rec->ip);
	in_new = !ftrace_hash_empty(new_hash) && ftrace_lookup_ip(new_hash, rec->ip);

NO, ftrace_lookup_ip() returns NULL if the hash is empty, so adding !ftrace_hash_empty() is meaningless :)

Ah, you're right!

Quoted 6 lines

Actually, here I intended to have 3 meanings for the new/old_hash arguments,
- If it is NULL, it hits all
- If it is EMPTY_HASH, it hits nothing
- If it has some entries, it hits those entries.

Quoted 6 lines

And in ftrace.c(__ftrace_hash_rec_update), AFAICS, ops->filter_hash has only 2 meanings,
- If it is EMPTY_HASH or NULL, it hits all
- If it has some entries, it hits those entries.

So I had to do above change...

Then I propose to use a different value/symbol instead of EMPTY_HASH in order to prevent future confusion and add some comments there.

[SNIP]

Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
kernel/trace/ftrace.c +4 -0
+static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1673

Quoted 5 lines
kernel/trace/ftrace.c +5 -0
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
+}

Please see above comment. You can pass an empty hash as is, or pass NULL as second arg. The same goes to below...

As I said above, that is by design :). EMPTY_HASH means it hits nothing, NULL means it hits all.

Quoted 6 lines
Quoted 2 lines

But doesn't it make unrelated records also get the flag updated? I'm curious when new_hash can be empty on _enable() case..

NO, _enable() is called right before ftrace_hash_rec_enable(ops,1) which always enables filter_hash (since the 2nd arg is 1). If the filter_hash is empty, ftrace_hash_rec_enable() enables ftrace_ops on all ftrace_recs.

But AFAICS both of kprobes and kpatch call ftrace_set_filter_ip() before calling register_ftrace_function(). That means there's no case when ops->filter_hash can be empty, right?

Quoted 5 lines

Ah, but I found I made a redundant mistake (different one) in ftrace_hash_move(), ftrace_hash_ipmodify_update() should be done only if "enable" is set (that means ftrace_hash_move() updates filter_hash, not notrace_hash). I'll update this patch.

Right.

Thanks,
Namhyung

#8 lore raw

Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

Quoted 49 lines · Namhyung Kim

Hi Masami,

Quoted 24 lines · Masami Hiramatsu
Quoted 23 lines · Namhyung Kim

Hi Masami,

Quoted 19 lines · Masami Hiramatsu
Quoted 11 lines · Namhyung Kim

Hi Masami,

2014-06-10 (화), 10:50 +0000, Masami Hiramatsu:

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1627

Quoted 6 lines

Introduce FTRACE_OPS_FL_IPMODIFY to avoid conflict among

kernel/trace/ftrace.c +5 -0
+	/* Update rec->flags */
+	do_for_each_ftrace_rec(pg, rec) {
+		/* We need to update only differences of filter_hash */
+		in_old = !old_hash || ftrace_lookup_ip(old_hash, rec->ip);
+		in_new = !new_hash || ftrace_lookup_ip(new_hash, rec->ip);

Why not use ftrace_hash_empty() here instead of checking NULL?

Ah, a trick is here. Since an empty filter_hash must hit all, we can not enable/disable filter_hash if we use ftrace_hash_empty() here.

To enabling the new_hash, old_hash must be EMPTY_HASH which means in_old always be false. To disabling, new_hash is EMPTY_HASH too. Please see ftrace_hash_ipmodify_enable/disable/update().

Quoted 8 lines
Quoted 5 lines

I'm confused. 8-p I guess what you want to do is checking records in either of the filter_hash, right? If so, what about this?

	in_old = !ftrace_hash_empty(old_hash) && ftrace_lookup_ip(old_hash, rec->ip);
	in_new = !ftrace_hash_empty(new_hash) && ftrace_lookup_ip(new_hash, rec->ip);

NO, ftrace_lookup_ip() returns NULL if the hash is empty, so adding !ftrace_hash_empty() is meaningless :)

Ah, you're right!

Quoted 11 lines

Actually, here I intended to have 3 meanings for the new/old_hash arguments,
- If it is NULL, it hits all
- If it is EMPTY_HASH, it hits nothing
- If it has some entries, it hits those entries.

And in ftrace.c(__ftrace_hash_rec_update), AFAICS, ops->filter_hash has only 2 meanings,
- If it is EMPTY_HASH or NULL, it hits all
- If it has some entries, it hits those entries.

Quoted 4 lines
Quoted 1 line

So I had to do above change...

Then I propose to use a different value/symbol instead of EMPTY_HASH in order to prevent future confusion and add some comments there.

I doubt I need another symbol since the EMPTY_HASH means normally empty and no hit(filter_hash case is a special one). I'd like to add a comment on it.

Quoted 20 lines

[SNIP]

Quoted 19 lines
Quoted 18 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
kernel/trace/ftrace.c +4 -0
+static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
+{
+	struct ftrace_hash *hash = ops->filter_hash;
+
Quoted 8 lines

kernel/trace/ftrace.c static void ftrace_hash_rec_enable(struct ftrace_ops *ops, · line 1673

Quoted 5 lines
kernel/trace/ftrace.c +5 -0
+	if (ftrace_hash_empty(hash))
+		hash = NULL;
+
+	return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
+}

Please see above comment. You can pass an empty hash as is, or pass NULL as second arg. The same goes to below...

Quoted 2 lines

As I said above, that is by design :). EMPTY_HASH means it hits nothing, NULL means it hits all.

But doesn't it make unrelated records also get the flag updated? I'm curious when new_hash can be empty on _enable() case..

Quoted 7 lines
Quoted 3 lines

NO, _enable() is called right before ftrace_hash_rec_enable(ops,1) which always enables filter_hash (since the 2nd arg is 1). If the filter_hash is empty, ftrace_hash_rec_enable() enables ftrace_ops on all ftrace_recs.

But AFAICS both of kprobes and kpatch call ftrace_set_filter_ip() before calling register_ftrace_function(). That means there's no case when ops->filter_hash can be empty, right?

No, unless it is registered, FTRACE_OPS_FL_ENABLED flag is not set on the ftrace_ops. In that case, recs are not updated. :)

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#9 lore raw

Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

In reply to Masami Hiramatsu
Quoted 8 lines · Masami Hiramatsu

NO, ftrace_lookup_ip() returns NULL if the hash is empty, so adding !ftrace_hash_empty() is meaningless :)

Actually, here I intended to have 3 meanings for the new/old_hash arguments,
- If it is NULL, it hits all
- If it is EMPTY_HASH, it hits nothing
- If it has some entries, it hits those entries.

Quoted 4 lines

And in ftrace.c(__ftrace_hash_rec_update), AFAICS, ops->filter_hash has only 2 meanings,
- If it is EMPTY_HASH or NULL, it hits all
- If it has some entries, it hits those entries.

Then I found an unrelated issue during review.

It seems that checking NULL other_hash for the 'all' case in __ftrace_hash_rec_update() is not sufficient. It should check EMPTY_HASH case too, but then it ends up removing the check at all since it can be covered in ftrace_lookup_ip().

Thanks,
Namhyung

kernel/trace/ftrace.c +1 -1
index 13885590a184..8bd7aa69a479 100644
@@ -1545,7 +1545,7 @@ static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
 			 * Only the filter_hash affects all records.
 			 * Update if the record is not in the notrace hash.
 			 */
-			if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
+			if (!ftrace_lookup_ip(other_hash, rec->ip))
 				match = 1;
 		} else {
 			in_hash = !!ftrace_lookup_ip(hash, rec->ip);
author Reviewed-by
#10 lore raw

Re: Re: [PATCH ftrace/core 2/2] ftrace, kprobes: Support IPMODIFY flag to find IP modify conflict

Quoted 14 lines · Namhyung Kim
Quoted 8 lines · Masami Hiramatsu

NO, ftrace_lookup_ip() returns NULL if the hash is empty, so adding !ftrace_hash_empty() is meaningless :)

Actually, here I intended to have 3 meanings for the new/old_hash arguments,
- If it is NULL, it hits all
- If it is EMPTY_HASH, it hits nothing
- If it has some entries, it hits those entries.

Quoted 4 lines

And in ftrace.c(__ftrace_hash_rec_update), AFAICS, ops->filter_hash has only 2 meanings,
- If it is EMPTY_HASH or NULL, it hits all
- If it has some entries, it hits those entries.

Quoted 6 lines

Then I found an unrelated issue during review.

It seems that checking NULL other_hash for the 'all' case in __ftrace_hash_rec_update() is not sufficient. It should check EMPTY_HASH case too, but then it ends up removing the check at all since it can be covered in ftrace_lookup_ip().

I guessed that was just for speeding up by skipping ftrace_lookup_ip(). (but I'm not sure how much it improves...)

However, I'd like to ask Steven his Ack.

Thank you,

Quoted 24 lines

Thanks,
Namhyung

kernel/trace/ftrace.c +1 -2
index 13885590a184..8bd7aa69a479 100644
@@ -1545,7 +1545,7 @@ static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
 			 * Only the filter_hash affects all records.
 			 * Update if the record is not in the notrace hash.
 			 */
-			if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
+			if (!ftrace_lookup_ip(other_hash, rec->ip))
 				match = 1;
 		} else {
 			in_hash = !!ftrace_lookup_ip(hash, rec->ip);
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#11 lore raw
In reply to Masami Hiramatsu
Quoted 20 lines · Masami Hiramatsu

Hi,

Here is a pair of patches which introduces IPMODIFY flag for ftrace_ops to detect conflicts of ftrace users who can modify regs->ip in their handler.
Currently, only kprobes can change the regs->ip in the handler, but recently kpatch is also want to change it. Moreover, since the ftrace itself exported to modules, it might be considerable senario.

Here we talked on github.
https://github.com/dynup/kpatch/issues/47

To protect modified regs-ip from each other, this series introduces FTRACE_OPS_FL_IPMODIFY flag and ftrace now ensures the flag can be set on each function entry location. If there is someone who already reserve regs->ip on target function entry, ftrace_set_filter_ip or register_ftrace_function will return -EBUSY. Users must handle that.

Quoted 2 lines

At this point, all kprobes will reserve regs->ip, since jprobe requires it.

Masami, thanks very much for this!

One issue with this approach is that it _always_ makes kprobes and kpatch incompatible when probing/patching the same function, even when kprobes doesn't need to touch regs->ip.

Is it possible to add a kprobes flag (KPROBE_FLAG_IPMODIFY), which is only set by those kprobes users (just jprobes?) which need to modify IP? Then kprobes could only set the corresponding ftrace flag when it's really needed. And I think kprobes could even enforce the fact that !KPROBE_FLAG_IPMODIFY users don't change regs->ip.

BTW, I've done some testing with this patch set by patching/probing the same function with FTRACE_OPS_FL_IPMODIFY, and got some warnings. I saw the following warning when attempting to kpatch a kprobed function:

  WARNING: CPU: 2 PID: 18351 at kernel/trace/ftrace.c:419 __unregister_ftrace_function+0x1be/0x1d0()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) stap_8d70d6e041605bd1e144cba4801652_14636(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic x86_pkg_temp_thermal coretemp kvm_intel snd_hda_intel iTCO_wdt iTCO_vendor_support snd_hda_controller kvm snd_hda_codec iwlwifi snd_hwdep uvcvideo snd_seq videobuf2_vmalloc videobuf2_memops snd_seq_device
   videobuf2_core btusb v4l2_common snd_pcm videodev nfsd cfg80211 microcode e1000e bluetooth media thinkpad_acpi joydev sdhci_pci sdhci pcspkr serio_raw snd_timer i2c_i801 snd mmc_core auth_rpcgss mei_me mei lpc_ich mfd_core shpchp ptp pps_core wmi tpm_tis soundcore tpm rfkill nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video
  CPU: 2 PID: 18351 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 00000000b39bd289 ffff8803b78d7bc0 ffffffff816f31ed
   0000000000000000 ffff8803b78d7bf8 ffffffff8108914d ffffffffa07f9040
   00000000fffffff0 0000000000000000 0000000000000001 ffff8803e7ac4200
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff81135bae>] __unregister_ftrace_function+0x1be/0x1d0
   [<ffffffff81137294>] ftrace_startup+0x1e4/0x220
   [<ffffffff81137313>] register_ftrace_function+0x43/0x60
   [<ffffffffa07f6c84>] kpatch_register+0x664/0x830 [kpatch]
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0046194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0046000>] ? 0xffffffffa0045fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffff81059d43>] ? set_memory_nx+0x43/0x50
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b

That warning happened because __unregister_ftrace_function() doesn't expect FTRACE_OPS_FL_ENABLED to be cleared in the ftrace_startup error path. I tried removing the FTRACE_OPS_FL_ENABLED clearing line in ftrace_startup, but I saw more warnings. This one happened when attempting to kprobe a kpatched function:

  WARNING: CPU: 3 PID: 4444 at kernel/kprobes.c:953 arm_kprobe+0xa7/0xe0()
  Failed to init kprobe-ftrace (-16)
  Modules linked in: stap_b2ea0de23f179d8ded86fcc19fcc533_4444(OE) kpatch_meminfo_string(OE) kpatch(OE) rfcomm fuse ccm ipt_MASQUERADE xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 iTCO_wdt iTCO_vendor_support x86_pkg_temp_thermal coretemp kvm_intel kvm snd_hda_codec_hdmi iwlwifi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq uvcvideo btusb cfg80211 bluetooth videobuf2_vmalloc videobuf2_memops
   videobuf2_core v4l2_common videodev snd_seq_device snd_pcm sdhci_pci media sdhci joydev nfsd i2c_i801 serio_raw pcspkr mmc_core microcode snd_timer e1000e lpc_ich thinkpad_acpi mfd_core shpchp snd wmi tpm_tis soundcore tpm ptp rfkill mei_me auth_rpcgss mei pps_core nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: kpatch_meminfo_string]
  CPU: 3 PID: 4444 Comm: stapio Tainted: G     U  W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 000000009cd22363 ffff880427bdfd80 ffffffff816f31ed
   ffff880427bdfdc8 ffff880427bdfdb8 ffffffff8108914d ffffffffa08258e0
   ffffffffa08258f0 0000000000000000 0000000000000000 0000000000000000
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff810891cc>] warn_slowpath_fmt+0x5c/0x80
   [<ffffffff816ff9d7>] arm_kprobe+0xa7/0xe0
   [<ffffffff817007f7>] register_kprobe+0x557/0x5d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffffa0820c95>] _stp_ctl_write_cmd+0x8d5/0x930 [stap_b2ea0de23f179d8ded86fcc19fcc533_4444]
   [<ffffffff811e5daa>] vfs_write+0xba/0x1e0
   [<ffffffff811e6965>] SyS_write+0x55/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b

And this one happened after unregistering a probe and then attempting to register kpatch:

  WARNING: CPU: 1 PID: 18041 at kernel/trace/ftrace.c:1584 __ftrace_hash_rec_update.part.35+0x20a/0x240()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge bnep stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm iTCO_wdt iTCO_vendor_support mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller x86_pkg_temp_thermal snd_hda_codec coretemp iwlwifi kvm_intel kvm snd_hwdep snd_seq cfg80211 snd_seq_device uvcvideo btusb videobuf2_vmalloc snd_pcm videobuf2_memops videobuf2_core v4l2_common
   videodev microcode bluetooth media e1000e pcspkr snd_timer sdhci_pci sdhci thinkpad_acpi i2c_i801 joydev snd serio_raw mmc_core lpc_ich ptp mei_me mfd_core pps_core mei shpchp soundcore rfkill tpm_tis wmi tpm nfsd auth_rpcgss nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: stap_bc293456f85bd78c424ce27a4ab459_18005]
  CPU: 1 PID: 18041 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   ffff8803b4717ad8 00000000af73dd79 ffff8803b4717ae8 ffffffff816f31ed
   0000000000000000 ffff8803b4717b20 ffffffff8108914d 0000000000000000
   0000000000000001 ffff8804280a0ea0 00000000000020ea ffff88042dc04aa0
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff811359ba>] __ftrace_hash_rec_update.part.35+0x20a/0x240
   [<ffffffff81135e25>] ftrace_hash_move+0x1d5/0x200
   [<ffffffff81137e16>] ftrace_set_hash+0x126/0x1d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81137f20>] ftrace_set_filter_ip+0x60/0x70
   [<ffffffffa07efa50>] kpatch_register+0x430/0x830 [kpatch]
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0047194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Signature · 1 line
Josh
#12 lore raw

Hi Josh,

Quoted 72 lines · Josh Poimboeuf
Quoted 20 lines · Masami Hiramatsu

Hi,

Here is a pair of patches which introduces IPMODIFY flag for ftrace_ops to detect conflicts of ftrace users who can modify regs->ip in their handler.
Currently, only kprobes can change the regs->ip in the handler, but recently kpatch is also want to change it. Moreover, since the ftrace itself exported to modules, it might be considerable senario.

Here we talked on github.
https://github.com/dynup/kpatch/issues/47

To protect modified regs-ip from each other, this series introduces FTRACE_OPS_FL_IPMODIFY flag and ftrace now ensures the flag can be set on each function entry location. If there is someone who already reserve regs->ip on target function entry, ftrace_set_filter_ip or register_ftrace_function will return -EBUSY. Users must handle that.

Quoted 2 lines

At this point, all kprobes will reserve regs->ip, since jprobe requires it.

Masami, thanks very much for this!

One issue with this approach is that it _always_ makes kprobes and kpatch incompatible when probing/patching the same function, even when kprobes doesn't need to touch regs->ip.

Is it possible to add a kprobes flag (KPROBE_FLAG_IPMODIFY), which is only set by those kprobes users (just jprobes?) which need to modify IP? Then kprobes could only set the corresponding ftrace flag when it's really needed. And I think kprobes could even enforce the fact that !KPROBE_FLAG_IPMODIFY users don't change regs->ip.

BTW, I've done some testing with this patch set by patching/probing the same function with FTRACE_OPS_FL_IPMODIFY, and got some warnings. I saw the following warning when attempting to kpatch a kprobed function:

  WARNING: CPU: 2 PID: 18351 at kernel/trace/ftrace.c:419 __unregister_ftrace_function+0x1be/0x1d0()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) stap_8d70d6e041605bd1e144cba4801652_14636(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic x86_pkg_temp_thermal coretemp kvm_intel snd_hda_intel iTCO_wdt iTCO_vendor_support snd_hda_controller kvm snd_hda_codec iwlwifi snd_hwdep uvcvideo snd_seq videobuf2_vmalloc videobuf2_memops snd_seq_dev
 ice
   videobuf2_core btusb v4l2_common snd_pcm videodev nfsd cfg80211 microcode e1000e bluetooth media thinkpad_acpi joydev sdhci_pci sdhci pcspkr serio_raw snd_timer i2c_i801 snd mmc_core auth_rpcgss mei_me mei lpc_ich mfd_core shpchp ptp pps_core wmi tpm_tis soundcore tpm rfkill nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video
  CPU: 2 PID: 18351 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 00000000b39bd289 ffff8803b78d7bc0 ffffffff816f31ed
   0000000000000000 ffff8803b78d7bf8 ffffffff8108914d ffffffffa07f9040
   00000000fffffff0 0000000000000000 0000000000000001 ffff8803e7ac4200
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff81135bae>] __unregister_ftrace_function+0x1be/0x1d0
   [<ffffffff81137294>] ftrace_startup+0x1e4/0x220
   [<ffffffff81137313>] register_ftrace_function+0x43/0x60
   [<ffffffffa07f6c84>] kpatch_register+0x664/0x830 [kpatch]
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0046194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0046000>] ? 0xffffffffa0045fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffff81059d43>] ? set_memory_nx+0x43/0x50
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 4 lines

That warning happened because __unregister_ftrace_function() doesn't expect FTRACE_OPS_FL_ENABLED to be cleared in the ftrace_startup error path. I tried removing the FTRACE_OPS_FL_ENABLED clearing line in ftrace_startup, but I saw more warnings.

Did you just remove the clearing line or actually clear the flag after __unregister_ftrace_function() was called?

Quoted 16 lines

This one happened when attempting to kprobe a kpatched function:

  WARNING: CPU: 3 PID: 4444 at kernel/kprobes.c:953 arm_kprobe+0xa7/0xe0()
  Failed to init kprobe-ftrace (-16)
  Modules linked in: stap_b2ea0de23f179d8ded86fcc19fcc533_4444(OE) kpatch_meminfo_string(OE) kpatch(OE) rfcomm fuse ccm ipt_MASQUERADE xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 iTCO_wdt iTCO_vendor_support x86_pkg_temp_thermal coretemp kvm_intel kvm snd_hda_codec_hdmi iwlwifi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq uvcvideo btusb cfg80211 bluetooth videobuf2_vmalloc vide
 obuf2_memops
   videobuf2_core v4l2_common videodev snd_seq_device snd_pcm sdhci_pci media sdhci joydev nfsd i2c_i801 serio_raw pcspkr mmc_core microcode snd_timer e1000e lpc_ich thinkpad_acpi mfd_core shpchp snd wmi tpm_tis soundcore tpm ptp rfkill mei_me auth_rpcgss mei pps_core nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: kpatch_meminfo_string]
  CPU: 3 PID: 4444 Comm: stapio Tainted: G     U  W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 000000009cd22363 ffff880427bdfd80 ffffffff816f31ed
   ffff880427bdfdc8 ffff880427bdfdb8 ffffffff8108914d ffffffffa08258e0
   ffffffffa08258f0 0000000000000000 0000000000000000 0000000000000000
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
Quoted 8 lines
   [<ffffffff810891cc>] warn_slowpath_fmt+0x5c/0x80
   [<ffffffff816ff9d7>] arm_kprobe+0xa7/0xe0
   [<ffffffff817007f7>] register_kprobe+0x557/0x5d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffffa0820c95>] _stp_ctl_write_cmd+0x8d5/0x930 [stap_b2ea0de23f179d8ded86fcc19fcc533_4444]
   [<ffffffff811e5daa>] vfs_write+0xba/0x1e0
   [<ffffffff811e6965>] SyS_write+0x55/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b

This one looks fairly normal as kpatch set FTRACE_FL_IPMODIFY and kprobe see it again..

Quoted 29 lines

And this one happened after unregistering a probe and then attempting to register kpatch:

  WARNING: CPU: 1 PID: 18041 at kernel/trace/ftrace.c:1584 __ftrace_hash_rec_update.part.35+0x20a/0x240()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge bnep stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm iTCO_wdt iTCO_vendor_support mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller x86_pkg_temp_thermal snd_hda_codec coretemp iwlwifi kvm_intel kvm snd_hwdep snd_seq cfg80211 snd_seq_device uvcvideo btusb videobuf2_vmalloc snd_pcm videobuf2_memops videobuf2_core v4l2
 _common
   videodev microcode bluetooth media e1000e pcspkr snd_timer sdhci_pci sdhci thinkpad_acpi i2c_i801 joydev snd serio_raw mmc_core lpc_ich ptp mei_me mfd_core pps_core mei shpchp soundcore rfkill tpm_tis wmi tpm nfsd auth_rpcgss nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: stap_bc293456f85bd78c424ce27a4ab459_18005]
  CPU: 1 PID: 18041 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   ffff8803b4717ad8 00000000af73dd79 ffff8803b4717ae8 ffffffff816f31ed
   0000000000000000 ffff8803b4717b20 ffffffff8108914d 0000000000000000
   0000000000000001 ffff8804280a0ea0 00000000000020ea ffff88042dc04aa0
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff811359ba>] __ftrace_hash_rec_update.part.35+0x20a/0x240
   [<ffffffff81135e25>] ftrace_hash_move+0x1d5/0x200
   [<ffffffff81137e16>] ftrace_set_hash+0x126/0x1d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81137f20>] ftrace_set_filter_ip+0x60/0x70
   [<ffffffffa07efa50>] kpatch_register+0x430/0x830 [kpatch]
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0047194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
Quoted 8 lines
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b

But this is weird since kpatch_register() calls ftrace_set_filter_ip() before registering kpatch_ftrace_ops. So the ops->flags should not have FTRACE_OPS_FL_ENABLED and it makes __ftrace_hash_rec_update() return immediately.

So I guessed a following scenario:

register kprobe (success)
register kpatch (-EBUSY, but not reset _FL_ENABLED)
unregister kprobe (success)
register kpatch (boom...)

Thanks,
Namhyung

#13 lore raw
Quoted 75 lines · Namhyung Kim

Hi Josh,

Quoted 72 lines · Josh Poimboeuf
Quoted 20 lines · Masami Hiramatsu

Hi,

Here is a pair of patches which introduces IPMODIFY flag for ftrace_ops to detect conflicts of ftrace users who can modify regs->ip in their handler.
Currently, only kprobes can change the regs->ip in the handler, but recently kpatch is also want to change it. Moreover, since the ftrace itself exported to modules, it might be considerable senario.

Here we talked on github.
https://github.com/dynup/kpatch/issues/47

To protect modified regs-ip from each other, this series introduces FTRACE_OPS_FL_IPMODIFY flag and ftrace now ensures the flag can be set on each function entry location. If there is someone who already reserve regs->ip on target function entry, ftrace_set_filter_ip or register_ftrace_function will return -EBUSY. Users must handle that.

Quoted 2 lines

At this point, all kprobes will reserve regs->ip, since jprobe requires it.

Masami, thanks very much for this!

One issue with this approach is that it _always_ makes kprobes and kpatch incompatible when probing/patching the same function, even when kprobes doesn't need to touch regs->ip.

Is it possible to add a kprobes flag (KPROBE_FLAG_IPMODIFY), which is only set by those kprobes users (just jprobes?) which need to modify IP? Then kprobes could only set the corresponding ftrace flag when it's really needed. And I think kprobes could even enforce the fact that !KPROBE_FLAG_IPMODIFY users don't change regs->ip.

BTW, I've done some testing with this patch set by patching/probing the same function with FTRACE_OPS_FL_IPMODIFY, and got some warnings. I saw the following warning when attempting to kpatch a kprobed function:

  WARNING: CPU: 2 PID: 18351 at kernel/trace/ftrace.c:419 __unregister_ftrace_function+0x1be/0x1d0()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) stap_8d70d6e041605bd1e144cba4801652_14636(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic x86_pkg_temp_thermal coretemp kvm_intel snd_hda_intel iTCO_wdt iTCO_vendor_support snd_hda_controller kvm snd_hda_codec iwlwifi snd_hwdep uvcvideo snd_seq videobuf2_vmalloc videobuf2_memops snd_seq_dev
 ice
   videobuf2_core btusb v4l2_common snd_pcm videodev nfsd cfg80211 microcode e1000e bluetooth media thinkpad_acpi joydev sdhci_pci sdhci pcspkr serio_raw snd_timer i2c_i801 snd mmc_core auth_rpcgss mei_me mei lpc_ich mfd_core shpchp ptp pps_core wmi tpm_tis soundcore tpm rfkill nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video
  CPU: 2 PID: 18351 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 00000000b39bd289 ffff8803b78d7bc0 ffffffff816f31ed
   0000000000000000 ffff8803b78d7bf8 ffffffff8108914d ffffffffa07f9040
   00000000fffffff0 0000000000000000 0000000000000001 ffff8803e7ac4200
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff81135bae>] __unregister_ftrace_function+0x1be/0x1d0
   [<ffffffff81137294>] ftrace_startup+0x1e4/0x220
   [<ffffffff81137313>] register_ftrace_function+0x43/0x60
   [<ffffffffa07f6c84>] kpatch_register+0x664/0x830 [kpatch]
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0046194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0046000>] ? 0xffffffffa0045fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffff81059d43>] ? set_memory_nx+0x43/0x50
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 7 lines
Quoted 4 lines

That warning happened because __unregister_ftrace_function() doesn't expect FTRACE_OPS_FL_ENABLED to be cleared in the ftrace_startup error path. I tried removing the FTRACE_OPS_FL_ENABLED clearing line in ftrace_startup, but I saw more warnings.

Did you just remove the clearing line or actually clear the flag after __unregister_ftrace_function() was called?

Ah, I just removed the clearing line. Looks like I should have also cleared it after calling __unregister_ftrace_function().

Quoted 27 lines
Quoted 18 lines

This one happened when attempting to kprobe a kpatched function:

  WARNING: CPU: 3 PID: 4444 at kernel/kprobes.c:953 arm_kprobe+0xa7/0xe0()
  Failed to init kprobe-ftrace (-16)
  Modules linked in: stap_b2ea0de23f179d8ded86fcc19fcc533_4444(OE) kpatch_meminfo_string(OE) kpatch(OE) rfcomm fuse ccm ipt_MASQUERADE xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 iTCO_wdt iTCO_vendor_support x86_pkg_temp_thermal coretemp kvm_intel kvm snd_hda_codec_hdmi iwlwifi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq uvcvideo btusb cfg80211 bluetooth videobuf2_vmalloc vide
 obuf2_memops
   videobuf2_core v4l2_common videodev snd_seq_device snd_pcm sdhci_pci media sdhci joydev nfsd i2c_i801 serio_raw pcspkr mmc_core microcode snd_timer e1000e lpc_ich thinkpad_acpi mfd_core shpchp snd wmi tpm_tis soundcore tpm ptp rfkill mei_me auth_rpcgss mei pps_core nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: kpatch_meminfo_string]
  CPU: 3 PID: 4444 Comm: stapio Tainted: G     U  W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 000000009cd22363 ffff880427bdfd80 ffffffff816f31ed
   ffff880427bdfdc8 ffff880427bdfdb8 ffffffff8108914d ffffffffa08258e0
   ffffffffa08258f0 0000000000000000 0000000000000000 0000000000000000
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
Quoted 8 lines
   [<ffffffff810891cc>] warn_slowpath_fmt+0x5c/0x80
   [<ffffffff816ff9d7>] arm_kprobe+0xa7/0xe0
   [<ffffffff817007f7>] register_kprobe+0x557/0x5d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffffa0820c95>] _stp_ctl_write_cmd+0x8d5/0x930 [stap_b2ea0de23f179d8ded86fcc19fcc533_4444]
   [<ffffffff811e5daa>] vfs_write+0xba/0x1e0
   [<ffffffff811e6965>] SyS_write+0x55/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 2 lines

This one looks fairly normal as kpatch set FTRACE_FL_IPMODIFY and kprobe see it again..

Instead of a warning I'd expect to see register_kprobe return an error back to stap.

Quoted 45 lines
Quoted 31 lines

And this one happened after unregistering a probe and then attempting to register kpatch:

  WARNING: CPU: 1 PID: 18041 at kernel/trace/ftrace.c:1584 __ftrace_hash_rec_update.part.35+0x20a/0x240()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge bnep stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm iTCO_wdt iTCO_vendor_support mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller x86_pkg_temp_thermal snd_hda_codec coretemp iwlwifi kvm_intel kvm snd_hwdep snd_seq cfg80211 snd_seq_device uvcvideo btusb videobuf2_vmalloc snd_pcm videobuf2_memops videobuf2_core v4l2
 _common
   videodev microcode bluetooth media e1000e pcspkr snd_timer sdhci_pci sdhci thinkpad_acpi i2c_i801 joydev snd serio_raw mmc_core lpc_ich ptp mei_me mfd_core pps_core mei shpchp soundcore rfkill tpm_tis wmi tpm nfsd auth_rpcgss nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: stap_bc293456f85bd78c424ce27a4ab459_18005]
  CPU: 1 PID: 18041 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   ffff8803b4717ad8 00000000af73dd79 ffff8803b4717ae8 ffffffff816f31ed
   0000000000000000 ffff8803b4717b20 ffffffff8108914d 0000000000000000
   0000000000000001 ffff8804280a0ea0 00000000000020ea ffff88042dc04aa0
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff811359ba>] __ftrace_hash_rec_update.part.35+0x20a/0x240
   [<ffffffff81135e25>] ftrace_hash_move+0x1d5/0x200
   [<ffffffff81137e16>] ftrace_set_hash+0x126/0x1d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81137f20>] ftrace_set_filter_ip+0x60/0x70
   [<ffffffffa07efa50>] kpatch_register+0x430/0x830 [kpatch]
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0047194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
Quoted 8 lines
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b

But this is weird since kpatch_register() calls ftrace_set_filter_ip() before registering kpatch_ftrace_ops. So the ops->flags should not have FTRACE_OPS_FL_ENABLED and it makes __ftrace_hash_rec_update() return immediately.

Quoted 6 lines

So I guessed a following scenario:

register kprobe (success)
register kpatch (-EBUSY, but not reset _FL_ENABLED)
unregister kprobe (success)
register kpatch (boom...)

That's entirely possible :-)

Signature · 1 line
Josh
#14 lore raw
In reply to Josh Poimboeuf
Quoted 21 lines · Josh Poimboeuf
Quoted 20 lines · Masami Hiramatsu

Hi,

Here is a pair of patches which introduces IPMODIFY flag for ftrace_ops to detect conflicts of ftrace users who can modify regs->ip in their handler.
Currently, only kprobes can change the regs->ip in the handler, but recently kpatch is also want to change it. Moreover, since the ftrace itself exported to modules, it might be considerable senario.

Here we talked on github.
https://github.com/dynup/kpatch/issues/47

To protect modified regs-ip from each other, this series introduces FTRACE_OPS_FL_IPMODIFY flag and ftrace now ensures the flag can be set on each function entry location. If there is someone who already reserve regs->ip on target function entry, ftrace_set_filter_ip or register_ftrace_function will return -EBUSY. Users must handle that.

Quoted 8 lines
Quoted 2 lines

At this point, all kprobes will reserve regs->ip, since jprobe requires it.

Masami, thanks very much for this!

One issue with this approach is that it _always_ makes kprobes and kpatch incompatible when probing/patching the same function, even when kprobes doesn't need to touch regs->ip.

Right.

Quoted 5 lines

Is it possible to add a kprobes flag (KPROBE_FLAG_IPMODIFY), which is only set by those kprobes users (just jprobes?) which need to modify IP? Then kprobes could only set the corresponding ftrace flag when it's really needed. And I think kprobes could even enforce the fact that !KPROBE_FLAG_IPMODIFY users don't change regs->ip.

No, actually we don't need that additional flag, we can slightly change the kprobes behavior(spec) that requires setting kprobe->break_handler a function if it modifies regs->ip. (this doesn't break jprobe) The problem is that we need a separate ftrace_ops for jprobe and other probes which can change the regs->ip. But current kprobes don't expected that such case...

Quoted 3 lines

BTW, I've done some testing with this patch set by patching/probing the same function with FTRACE_OPS_FL_IPMODIFY, and got some warnings. I saw the following warning when attempting to kpatch a kprobed function:

Ah, thanks for testing! I think it needs more work on failure path.

Quoted 30 lines
  WARNING: CPU: 2 PID: 18351 at kernel/trace/ftrace.c:419 __unregister_ftrace_function+0x1be/0x1d0()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) stap_8d70d6e041605bd1e144cba4801652_14636(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic x86_pkg_temp_thermal coretemp kvm_intel snd_hda_intel iTCO_wdt iTCO_vendor_support snd_hda_controller kvm snd_hda_codec iwlwifi snd_hwdep uvcvideo snd_seq videobuf2_vmalloc videobuf2_memops snd_seq_device
   videobuf2_core btusb v4l2_common snd_pcm videodev nfsd cfg80211 microcode e1000e bluetooth media thinkpad_acpi joydev sdhci_pci sdhci pcspkr serio_raw snd_timer i2c_i801 snd mmc_core auth_rpcgss mei_me mei lpc_ich mfd_core shpchp ptp pps_core wmi tpm_tis soundcore tpm rfkill nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video
  CPU: 2 PID: 18351 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 00000000b39bd289 ffff8803b78d7bc0 ffffffff816f31ed
   0000000000000000 ffff8803b78d7bf8 ffffffff8108914d ffffffffa07f9040
   00000000fffffff0 0000000000000000 0000000000000001 ffff8803e7ac4200
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff81135bae>] __unregister_ftrace_function+0x1be/0x1d0
   [<ffffffff81137294>] ftrace_startup+0x1e4/0x220
   [<ffffffff81137313>] register_ftrace_function+0x43/0x60
   [<ffffffffa07f6c84>] kpatch_register+0x664/0x830 [kpatch]
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0046194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0046000>] ? 0xffffffffa0045fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffff81059d43>] ? set_memory_nx+0x43/0x50
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 3 lines

That warning happened because __unregister_ftrace_function() doesn't expect FTRACE_OPS_FL_ENABLED to be cleared in the ftrace_startup error path.

Ah, right! I'll fix that.

Quoted 3 lines

I tried removing the FTRACE_OPS_FL_ENABLED clearing line in
ftrace_startup, but I saw more warnings. This one happened when attempting to kprobe a kpatched function:

Oops! yes, this should happen...

Quoted 24 lines

  WARNING: CPU: 3 PID: 4444 at kernel/kprobes.c:953 arm_kprobe+0xa7/0xe0()
  Failed to init kprobe-ftrace (-16)
  Modules linked in: stap_b2ea0de23f179d8ded86fcc19fcc533_4444(OE) kpatch_meminfo_string(OE) kpatch(OE) rfcomm fuse ccm ipt_MASQUERADE xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 iTCO_wdt iTCO_vendor_support x86_pkg_temp_thermal coretemp kvm_intel kvm snd_hda_codec_hdmi iwlwifi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq uvcvideo btusb cfg80211 bluetooth videobuf2_vmalloc videobuf2_memops
   videobuf2_core v4l2_common videodev snd_seq_device snd_pcm sdhci_pci media sdhci joydev nfsd i2c_i801 serio_raw pcspkr mmc_core microcode snd_timer e1000e lpc_ich thinkpad_acpi mfd_core shpchp snd wmi tpm_tis soundcore tpm ptp rfkill mei_me auth_rpcgss mei pps_core nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: kpatch_meminfo_string]
  CPU: 3 PID: 4444 Comm: stapio Tainted: G     U  W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 000000009cd22363 ffff880427bdfd80 ffffffff816f31ed
   ffff880427bdfdc8 ffff880427bdfdb8 ffffffff8108914d ffffffffa08258e0
   ffffffffa08258f0 0000000000000000 0000000000000000 0000000000000000
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff810891cc>] warn_slowpath_fmt+0x5c/0x80
   [<ffffffff816ff9d7>] arm_kprobe+0xa7/0xe0
   [<ffffffff817007f7>] register_kprobe+0x557/0x5d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffffa0820c95>] _stp_ctl_write_cmd+0x8d5/0x930 [stap_b2ea0de23f179d8ded86fcc19fcc533_4444]
   [<ffffffff811e5daa>] vfs_write+0xba/0x1e0
   [<ffffffff811e6965>] SyS_write+0x55/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 2 lines

And this one happened after unregistering a probe and then attempting to register kpatch:

Did you see this on unpatching? it seems to happen on disabling a hash...

Quoted 34 lines

  WARNING: CPU: 1 PID: 18041 at kernel/trace/ftrace.c:1584 __ftrace_hash_rec_update.part.35+0x20a/0x240()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge bnep stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm iTCO_wdt iTCO_vendor_support mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller x86_pkg_temp_thermal snd_hda_codec coretemp iwlwifi kvm_intel kvm snd_hwdep snd_seq cfg80211 snd_seq_device uvcvideo btusb videobuf2_vmalloc snd_pcm videobuf2_memops videobuf2_core v4l2_common
   videodev microcode bluetooth media e1000e pcspkr snd_timer sdhci_pci sdhci thinkpad_acpi i2c_i801 joydev snd serio_raw mmc_core lpc_ich ptp mei_me mfd_core pps_core mei shpchp soundcore rfkill tpm_tis wmi tpm nfsd auth_rpcgss nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: stap_bc293456f85bd78c424ce27a4ab459_18005]
  CPU: 1 PID: 18041 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   ffff8803b4717ad8 00000000af73dd79 ffff8803b4717ae8 ffffffff816f31ed
   0000000000000000 ffff8803b4717b20 ffffffff8108914d 0000000000000000
   0000000000000001 ffff8804280a0ea0 00000000000020ea ffff88042dc04aa0
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff811359ba>] __ftrace_hash_rec_update.part.35+0x20a/0x240
   [<ffffffff81135e25>] ftrace_hash_move+0x1d5/0x200
   [<ffffffff81137e16>] ftrace_set_hash+0x126/0x1d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81137f20>] ftrace_set_filter_ip+0x60/0x70
   [<ffffffffa07efa50>] kpatch_register+0x430/0x830 [kpatch]
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0047194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#15 lore raw
Quoted 39 lines · Masami Hiramatsu
Quoted 21 lines · Josh Poimboeuf
Quoted 20 lines · Masami Hiramatsu

Hi,

Here is a pair of patches which introduces IPMODIFY flag for ftrace_ops to detect conflicts of ftrace users who can modify regs->ip in their handler.
Currently, only kprobes can change the regs->ip in the handler, but recently kpatch is also want to change it. Moreover, since the ftrace itself exported to modules, it might be considerable senario.

Here we talked on github.
https://github.com/dynup/kpatch/issues/47

To protect modified regs-ip from each other, this series introduces FTRACE_OPS_FL_IPMODIFY flag and ftrace now ensures the flag can be set on each function entry location. If there is someone who already reserve regs->ip on target function entry, ftrace_set_filter_ip or register_ftrace_function will return -EBUSY. Users must handle that.

Quoted 8 lines
Quoted 2 lines

At this point, all kprobes will reserve regs->ip, since jprobe requires it.

Masami, thanks very much for this!

One issue with this approach is that it _always_ makes kprobes and kpatch incompatible when probing/patching the same function, even when kprobes doesn't need to touch regs->ip.

Right.

Quoted 5 lines

Is it possible to add a kprobes flag (KPROBE_FLAG_IPMODIFY), which is only set by those kprobes users (just jprobes?) which need to modify IP? Then kprobes could only set the corresponding ftrace flag when it's really needed. And I think kprobes could even enforce the fact that !KPROBE_FLAG_IPMODIFY users don't change regs->ip.

Quoted 6 lines

No, actually we don't need that additional flag, we can slightly change the kprobes behavior(spec) that requires setting kprobe->break_handler a function if it modifies regs->ip. (this doesn't break jprobe) The problem is that we need a separate ftrace_ops for jprobe and other probes which can change the regs->ip. But current kprobes don't expected that such case...

Sounds good to me.

Quoted 42 lines
Quoted 4 lines

BTW, I've done some testing with this patch set by patching/probing the same function with FTRACE_OPS_FL_IPMODIFY, and got some warnings. I saw the following warning when attempting to kpatch a kprobed function:

Ah, thanks for testing! I think it needs more work on failure path.

Quoted 30 lines
  WARNING: CPU: 2 PID: 18351 at kernel/trace/ftrace.c:419 __unregister_ftrace_function+0x1be/0x1d0()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) stap_8d70d6e041605bd1e144cba4801652_14636(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic x86_pkg_temp_thermal coretemp kvm_intel snd_hda_intel iTCO_wdt iTCO_vendor_support snd_hda_controller kvm snd_hda_codec iwlwifi snd_hwdep uvcvideo snd_seq videobuf2_vmalloc videobuf2_memops snd_seq_device
   videobuf2_core btusb v4l2_common snd_pcm videodev nfsd cfg80211 microcode e1000e bluetooth media thinkpad_acpi joydev sdhci_pci sdhci pcspkr serio_raw snd_timer i2c_i801 snd mmc_core auth_rpcgss mei_me mei lpc_ich mfd_core shpchp ptp pps_core wmi tpm_tis soundcore tpm rfkill nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video
  CPU: 2 PID: 18351 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 00000000b39bd289 ffff8803b78d7bc0 ffffffff816f31ed
   0000000000000000 ffff8803b78d7bf8 ffffffff8108914d ffffffffa07f9040
   00000000fffffff0 0000000000000000 0000000000000001 ffff8803e7ac4200
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff81135bae>] __unregister_ftrace_function+0x1be/0x1d0
   [<ffffffff81137294>] ftrace_startup+0x1e4/0x220
   [<ffffffff81137313>] register_ftrace_function+0x43/0x60
   [<ffffffffa07f6c84>] kpatch_register+0x664/0x830 [kpatch]
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0810000>] ? 0xffffffffa080ffff
   [<ffffffffa0046194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0046000>] ? 0xffffffffa0045fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffff81059d43>] ? set_memory_nx+0x43/0x50
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 3 lines

That warning happened because __unregister_ftrace_function() doesn't expect FTRACE_OPS_FL_ENABLED to be cleared in the ftrace_startup error path.

Quoted 7 lines

Ah, right! I'll fix that.

Quoted 3 lines

I tried removing the FTRACE_OPS_FL_ENABLED clearing line in
ftrace_startup, but I saw more warnings. This one happened when attempting to kprobe a kpatched function:

Oops! yes, this should happen...

Instead of a warning here I'd expect to see register_kprobe return an error and a staprun failure.

Quoted 25 lines
Quoted 25 lines


  WARNING: CPU: 3 PID: 4444 at kernel/kprobes.c:953 arm_kprobe+0xa7/0xe0()
  Failed to init kprobe-ftrace (-16)
  Modules linked in: stap_b2ea0de23f179d8ded86fcc19fcc533_4444(OE) kpatch_meminfo_string(OE) kpatch(OE) rfcomm fuse ccm ipt_MASQUERADE xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 iTCO_wdt iTCO_vendor_support x86_pkg_temp_thermal coretemp kvm_intel kvm snd_hda_codec_hdmi iwlwifi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq uvcvideo btusb cfg80211 bluetooth videobuf2_vmalloc videobuf2_memops
   videobuf2_core v4l2_common videodev snd_seq_device snd_pcm sdhci_pci media sdhci joydev nfsd i2c_i801 serio_raw pcspkr mmc_core microcode snd_timer e1000e lpc_ich thinkpad_acpi mfd_core shpchp snd wmi tpm_tis soundcore tpm ptp rfkill mei_me auth_rpcgss mei pps_core nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: kpatch_meminfo_string]
  CPU: 3 PID: 4444 Comm: stapio Tainted: G     U  W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 000000009cd22363 ffff880427bdfd80 ffffffff816f31ed
   ffff880427bdfdc8 ffff880427bdfdb8 ffffffff8108914d ffffffffa08258e0
   ffffffffa08258f0 0000000000000000 0000000000000000 0000000000000000
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff810891cc>] warn_slowpath_fmt+0x5c/0x80
   [<ffffffff816ff9d7>] arm_kprobe+0xa7/0xe0
   [<ffffffff817007f7>] register_kprobe+0x557/0x5d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffffa0820c95>] _stp_ctl_write_cmd+0x8d5/0x930 [stap_b2ea0de23f179d8ded86fcc19fcc533_4444]
   [<ffffffff811e5daa>] vfs_write+0xba/0x1e0
   [<ffffffff811e6965>] SyS_write+0x55/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 4 lines
Quoted 2 lines

And this one happened after unregistering a probe and then attempting to register kpatch:

Did you see this on unpatching? it seems to happen on disabling a hash...

No, I think it was in the patching path, after removing the kprobe. kpatch was calling ftrace_set_filter_ip(), and had not called register_ftrace_function() yet.

Quoted 44 lines
Quoted 35 lines


  WARNING: CPU: 1 PID: 18041 at kernel/trace/ftrace.c:1584 __ftrace_hash_rec_update.part.35+0x20a/0x240()
  Modules linked in: kpatch_meminfo_string(OE+) kpatch(OE) rfcomm fuse ipt_MASQUERADE ccm xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack ebtable_nat ebtable_broute bridge bnep stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm iTCO_wdt iTCO_vendor_support mac80211 snd_hda_codec_hdmi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller x86_pkg_temp_thermal snd_hda_codec coretemp iwlwifi kvm_intel kvm snd_hwdep snd_seq cfg80211 snd_seq_device uvcvideo btusb videobuf2_vmalloc snd_pcm videobuf2_memops videobuf2_core v4l2_common
   videodev microcode bluetooth media e1000e pcspkr snd_timer sdhci_pci sdhci thinkpad_acpi i2c_i801 joydev snd serio_raw mmc_core lpc_ich ptp mei_me mfd_core pps_core mei shpchp soundcore rfkill tpm_tis wmi tpm nfsd auth_rpcgss nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: stap_bc293456f85bd78c424ce27a4ab459_18005]
  CPU: 1 PID: 18041 Comm: insmod Tainted: G        W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   ffff8803b4717ad8 00000000af73dd79 ffff8803b4717ae8 ffffffff816f31ed
   0000000000000000 ffff8803b4717b20 ffffffff8108914d 0000000000000000
   0000000000000001 ffff8804280a0ea0 00000000000020ea ffff88042dc04aa0
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff8108927a>] warn_slowpath_null+0x1a/0x20
   [<ffffffff811359ba>] __ftrace_hash_rec_update.part.35+0x20a/0x240
   [<ffffffff81135e25>] ftrace_hash_move+0x1d5/0x200
   [<ffffffff81137e16>] ftrace_set_hash+0x126/0x1d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffff81137f20>] ftrace_set_filter_ip+0x60/0x70
   [<ffffffffa07efa50>] kpatch_register+0x430/0x830 [kpatch]
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0809000>] ? 0xffffffffa0808fff
   [<ffffffffa0047194>] patch_init+0x194/0x1000 [kpatch_meminfo_string]
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81002144>] do_one_initcall+0xd4/0x210
   [<ffffffffa0047000>] ? 0xffffffffa0046fff
   [<ffffffff81102e42>] load_module+0x1d92/0x25e0
   [<ffffffff810feb60>] ? store_uevent+0x70/0x70
   [<ffffffff811eba30>] ? kernel_read+0x50/0x80
   [<ffffffff81103846>] SyS_finit_module+0xa6/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
Signature · 1 line
Josh
#16 lore raw
Quoted 8 lines · Josh Poimboeuf
Quoted 5 lines
Quoted 3 lines

I tried removing the FTRACE_OPS_FL_ENABLED clearing line in
ftrace_startup, but I saw more warnings. This one happened when attempting to kprobe a kpatched function:

Oops! yes, this should happen...

Instead of a warning here I'd expect to see register_kprobe return an error and a staprun failure.

Actually, since kprobes has "disabled" state, not only register_kprobe but also enable_kprobe also can be failed. :)

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
  WARNING: CPU: 3 PID: 4444 at kernel/kprobes.c:953 arm_kprobe+0xa7/0xe0()
  Failed to init kprobe-ftrace (-16)
  Modules linked in: stap_b2ea0de23f179d8ded86fcc19fcc533_4444(OE) kpatch_meminfo_string(OE) kpatch(OE) rfcomm fuse ccm ipt_MASQUERADE xt_CHECKSUM tun ip6t_rpfilter ip6t_REJECT xt_conntrack bnep ebtable_nat ebtable_broute bridge stp llc ebtable_filter ebtables ip6table_nat nf_conntrack_ipv6 nf_defrag_ipv6 nf_nat_ipv6 ip6table_mangle ip6table_security ip6table_raw ip6table_filter ip6_tables iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack iptable_mangle iptable_security iptable_raw arc4 iwldvm mac80211 iTCO_wdt iTCO_vendor_support x86_pkg_temp_thermal coretemp kvm_intel kvm snd_hda_codec_hdmi iwlwifi snd_hda_codec_realtek snd_hda_codec_generic snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq uvcvideo btusb cfg80211 bluetooth videobuf2_vmalloc videobuf2_memops
   videobuf2_core v4l2_common videodev snd_seq_device snd_pcm sdhci_pci media sdhci joydev nfsd i2c_i801 serio_raw pcspkr mmc_core microcode snd_timer e1000e lpc_ich thinkpad_acpi mfd_core shpchp snd wmi tpm_tis soundcore tpm ptp rfkill mei_me auth_rpcgss mei pps_core nfs_acl lockd sunrpc dm_crypt i915 i2c_algo_bit drm_kms_helper drm crct10dif_pclmul crc32_pclmul crc32c_intel ghash_clmulni_intel i2c_core video [last unloaded: kpatch_meminfo_string]
  CPU: 3 PID: 4444 Comm: stapio Tainted: G     U  W  OE 3.15.0-IPMODIFY+ #1
  Hardware name: LENOVO 2356BH8/2356BH8, BIOS G7ET63WW (2.05 ) 11/12/2012
   0000000000000000 000000009cd22363 ffff880427bdfd80 ffffffff816f31ed
   ffff880427bdfdc8 ffff880427bdfdb8 ffffffff8108914d ffffffffa08258e0
   ffffffffa08258f0 0000000000000000 0000000000000000 0000000000000000
  Call Trace:
   [<ffffffff816f31ed>] dump_stack+0x45/0x56
   [<ffffffff8108914d>] warn_slowpath_common+0x7d/0xa0
   [<ffffffff810891cc>] warn_slowpath_fmt+0x5c/0x80
   [<ffffffff816ff9d7>] arm_kprobe+0xa7/0xe0
   [<ffffffff817007f7>] register_kprobe+0x557/0x5d0
   [<ffffffff81254da0>] ? meminfo_proc_open+0x30/0x30
   [<ffffffffa0820c95>] _stp_ctl_write_cmd+0x8d5/0x930 [stap_b2ea0de23f179d8ded86fcc19fcc533_4444]
   [<ffffffff811e5daa>] vfs_write+0xba/0x1e0
   [<ffffffff811e6965>] SyS_write+0x55/0xd0
   [<ffffffff81703179>] system_call_fastpath+0x16/0x1b
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

And this one happened after unregistering a probe and then attempting to register kpatch:

Did you see this on unpatching? it seems to happen on disabling a hash...

No, I think it was in the patching path, after removing the kprobe. kpatch was calling ftrace_set_filter_ip(), and had not called register_ftrace_function() yet.

OK, anyway, I'll make a test case for checking it. Thanks!

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]