Skip to contentSkip to conversation

kprobes: Remove BUG_ON from kprobes

PATCH New Masami Hiramatsu 2 participants 11 messages Sep 11, 2018 – Sep 12, 2018 kprobes

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v2 cover kprobes: Remove BUG_ON from kprobes

Hi,

Here are v2 patches to remove BUG_ON from kprobes.

Since BUG_ON() kills the machine, it must not be used for debugging or assertion. These patches remove meaningless BUG_ON or replace it with return errors.

In this version, I updated 5/5 to use WARN_ON_ONCE() for unusual case, according to Ingo's comment.

Thank you,

---

Masami Hiramatsu (5):
kprobes: Remove meaningless BUG_ON from disarming process
kprobes: Remove meaningless BUG_ON from add_new_kprobe
kprobes: Remove meaningless BUG_ON from reuse_unused_kprobe
kprobes: Return error if fail to reuse kprobe instead of BUG_ON
kprobes: Don't call BUG_ON if there is a kprobe in use on free list

 kernel/kprobes.c |   39 +++++++++++++++++++++++++++------------
 1 file changed, 27 insertions(+), 12 deletions(-)

--
Masami Hiramatsu (Linaro) <[email protected]>

author maintainer
#1 lore raw

PATCH v2 1/5 kprobes: Remove meaningless BUG_ON from disarming process

All aggr_probe at this line are already disarmed by disable_kprobe() or checked by kprobe_disarmed(). So this BUG_ON() does meaningless check. Remove it.

---
 kernel/kprobes.c |    1 -
 1 file changed, 1 deletion(-)
kernel/kprobes.c +0 -1
index ab257be4d924..d1edd8d5641e 100644
@@ -1704,7 +1704,6 @@ static int __unregister_kprobe_top(struct kprobe *p)
 	return 0;
 
 disarmed:
-	BUG_ON(!kprobe_disarmed(ap));
 	hlist_del_rcu(&ap->hlist);
 	return 0;
 }
#2 lore raw

[tip:perf/core] kprobes: Remove pointless BUG_ON() from disarming process

kprobes: Remove pointless BUG_ON() from disarming process
Commit-ID:  d0555fc78fdba5646a460e83bd2d8249c539bb89
Gitweb:     https://git.kernel.org/tip/d0555fc78fdba5646a460e83bd2d8249c539bb89
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Tue, 11 Sep 2018 19:19:14 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 12 Sep 2018 08:01:15 +0200

kprobes: Remove pointless BUG_ON() from disarming process

All aggr_probes at this line are already disarmed by disable_kprobe() or checked by kprobe_disarmed().

So this BUG_ON() is pointless, remove it.

---
 kernel/kprobes.c | 1 -
 1 file changed, 1 deletion(-)
kernel/kprobes.c +0 -1
index ab257be4d924..d1edd8d5641e 100644
@@ -1704,7 +1704,6 @@ noclean:
 	return 0;
 
 disarmed:
-	BUG_ON(!kprobe_disarmed(ap));
 	hlist_del_rcu(&ap->hlist);
 	return 0;
 }
author maintainer
#3 lore raw

PATCH v2 2/5 kprobes: Remove meaningless BUG_ON from add_new_kprobe

In reply to Masami Hiramatsu

Before calling add_new_kprobe(), aggr_probe's GONE flag and kprobe GONE flag are cleared. We don't need to worry about that flag at this point.

---
 kernel/kprobes.c |    2 --
 1 file changed, 2 deletions(-)
kernel/kprobes.c +0 -2
index d1edd8d5641e..231569e1e2c8 100644
@@ -1259,8 +1259,6 @@ NOKPROBE_SYMBOL(cleanup_rp_inst);
 /* Add the new probe to ap->list */
 static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
 {
-	BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
-
 	if (p->post_handler)
 		unoptimize_kprobe(ap, true);	/* Fall back to normal kprobe */
 
#4 lore raw

[tip:perf/core] kprobes: Remove pointless BUG_ON() from add_new_kprobe()

kprobes: Remove pointless BUG_ON() from add_new_kprobe()
Commit-ID:  c72e6742f62d7bb82a77a41ca53940cb8f73e60f
Gitweb:     https://git.kernel.org/tip/c72e6742f62d7bb82a77a41ca53940cb8f73e60f
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Tue, 11 Sep 2018 19:19:43 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 12 Sep 2018 08:01:15 +0200

kprobes: Remove pointless BUG_ON() from add_new_kprobe()

Before calling add_new_kprobe(), aggr_probe's GONE flag and kprobe GONE flag are cleared. We don't need to worry about that flag at this point.

---
 kernel/kprobes.c | 2 --
 1 file changed, 2 deletions(-)
kernel/kprobes.c +0 -2
index d1edd8d5641e..231569e1e2c8 100644
@@ -1259,8 +1259,6 @@ NOKPROBE_SYMBOL(cleanup_rp_inst);
 /* Add the new probe to ap->list */
 static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
 {
-	BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
-
 	if (p->post_handler)
 		unoptimize_kprobe(ap, true);	/* Fall back to normal kprobe */
 
author maintainer
#5 lore raw

PATCH v2 3/5 kprobes: Remove meaningless BUG_ON from reuse_unused_kprobe

In reply to Masami Hiramatsu

Since reuse_unused_kprobe is called when the given kprobe is unused, checking it inside again with BUG_ON is meaningless. Remove it.

---
 kernel/kprobes.c |    1 -
 1 file changed, 1 deletion(-)
kernel/kprobes.c +0 -1
index 231569e1e2c8..277a6cbe83db 100644
@@ -704,7 +704,6 @@ static void reuse_unused_kprobe(struct kprobe *ap)
 {
 	struct optimized_kprobe *op;
 
-	BUG_ON(!kprobe_unused(ap));
 	/*
 	 * Unused kprobe MUST be on the way of delayed unoptimizing (means
 	 * there is still a relative jump) and disabled.
#6 lore raw

[tip:perf/core] kprobes: Remove pointless BUG_ON() from reuse_unused_kprobe()

kprobes: Remove pointless BUG_ON() from reuse_unused_kprobe()
Commit-ID:  a6d18e65dff2b73ceeb187c598b48898e36ad7b1
Gitweb:     https://git.kernel.org/tip/a6d18e65dff2b73ceeb187c598b48898e36ad7b1
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Tue, 11 Sep 2018 19:20:11 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 12 Sep 2018 08:01:16 +0200

kprobes: Remove pointless BUG_ON() from reuse_unused_kprobe()

Since reuse_unused_kprobe() is called when the given kprobe is unused, checking it inside again with BUG_ON() is pointless. Remove it.

---
 kernel/kprobes.c | 1 -
 1 file changed, 1 deletion(-)
kernel/kprobes.c +0 -1
index 231569e1e2c8..277a6cbe83db 100644
@@ -704,7 +704,6 @@ static void reuse_unused_kprobe(struct kprobe *ap)
 {
 	struct optimized_kprobe *op;
 
-	BUG_ON(!kprobe_unused(ap));
 	/*
 	 * Unused kprobe MUST be on the way of delayed unoptimizing (means
 	 * there is still a relative jump) and disabled.
author maintainer
#7 lore raw

PATCH v2 4/5 kprobes: Return error if fail to reuse kprobe instead of BUG_ON

In reply to Masami Hiramatsu

Make reuse_unused_kprobe() to return error code if it fails to reuse unused kprobe for optprobe instead of calling BUG_ON().

---
 kernel/kprobes.c |   27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
kernel/kprobes.c +20 -7
index 277a6cbe83db..63c342e5e6c3 100644
@@ -700,9 +700,10 @@ static void unoptimize_kprobe(struct kprobe *p, bool force)
 }
 
 /* Cancel unoptimizing for reusing */
-static void reuse_unused_kprobe(struct kprobe *ap)
+static int reuse_unused_kprobe(struct kprobe *ap)
 {
 	struct optimized_kprobe *op;
+	int ret;
 
 	/*
 	 * Unused kprobe MUST be on the way of delayed unoptimizing (means
@@ -713,8 +714,12 @@ static void reuse_unused_kprobe(struct kprobe *ap)
 	/* Enable the probe again */
 	ap->flags &= ~KPROBE_FLAG_DISABLED;
 	/* Optimize it again (remove from op->list) */
-	BUG_ON(!kprobe_optready(ap));
+	ret = kprobe_optready(ap);
+	if (ret)
+		return ret;
+
 	optimize_kprobe(ap);
+	return 0;
 }
 
 /* Remove optimized instructions */
@@ -939,11 +944,16 @@ static void __disarm_kprobe(struct kprobe *p, bool reopt)
 #define kprobe_disarmed(p)			kprobe_disabled(p)
 #define wait_for_kprobe_optimizer()		do {} while (0)
 
-/* There should be no unused kprobes can be reused without optimization */
-static void reuse_unused_kprobe(struct kprobe *ap)
+static int reuse_unused_kprobe(struct kprobe *ap)
 {
+	/*
+	 * If the optimized kprobe is NOT supported, the aggr kprobe is
+	 * released at the same time that the last aggregated kprobe is
+	 * unregistered.
+	 * Thus there should be no chance to reuse unused kprobe.
+	 */
 	printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
-	BUG_ON(kprobe_unused(ap));
+	return -EINVAL;
 }
 
 static void free_aggr_kprobe(struct kprobe *p)
@@ -1315,9 +1325,12 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
 			goto out;
 		}
 		init_aggr_kprobe(ap, orig_p);
-	} else if (kprobe_unused(ap))
+	} else if (kprobe_unused(ap)) {
 		/* This probe is going to die. Rescue it */
-		reuse_unused_kprobe(ap);
+		ret = reuse_unused_kprobe(ap);
+		if (ret)
+			goto out;
+	}
 
 	if (kprobe_gone(ap)) {
 		/*
#8 lore raw

[tip:perf/core] kprobes: Return error if we fail to reuse kprobe instead of BUG_ON()

kprobes: Return error if we fail to reuse kprobe instead of BUG_ON()
Commit-ID:  819319fc93461c07b9cdb3064f154bd8cfd48172
Gitweb:     https://git.kernel.org/tip/819319fc93461c07b9cdb3064f154bd8cfd48172
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Tue, 11 Sep 2018 19:20:40 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 12 Sep 2018 08:01:16 +0200

kprobes: Return error if we fail to reuse kprobe instead of BUG_ON()

Make reuse_unused_kprobe() to return error code if it fails to reuse unused kprobe for optprobe instead of calling BUG_ON().

---
 kernel/kprobes.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
kernel/kprobes.c +20 -7
index 277a6cbe83db..63c342e5e6c3 100644
@@ -700,9 +700,10 @@ static void unoptimize_kprobe(struct kprobe *p, bool force)
 }
 
 /* Cancel unoptimizing for reusing */
-static void reuse_unused_kprobe(struct kprobe *ap)
+static int reuse_unused_kprobe(struct kprobe *ap)
 {
 	struct optimized_kprobe *op;
+	int ret;
 
 	/*
 	 * Unused kprobe MUST be on the way of delayed unoptimizing (means
@@ -713,8 +714,12 @@ static void reuse_unused_kprobe(struct kprobe *ap)
 	/* Enable the probe again */
 	ap->flags &= ~KPROBE_FLAG_DISABLED;
 	/* Optimize it again (remove from op->list) */
-	BUG_ON(!kprobe_optready(ap));
+	ret = kprobe_optready(ap);
+	if (ret)
+		return ret;
+
 	optimize_kprobe(ap);
+	return 0;
 }
 
 /* Remove optimized instructions */
@@ -939,11 +944,16 @@ static void __disarm_kprobe(struct kprobe *p, bool reopt)
 #define kprobe_disarmed(p)			kprobe_disabled(p)
 #define wait_for_kprobe_optimizer()		do {} while (0)
 
-/* There should be no unused kprobes can be reused without optimization */
-static void reuse_unused_kprobe(struct kprobe *ap)
+static int reuse_unused_kprobe(struct kprobe *ap)
 {
+	/*
+	 * If the optimized kprobe is NOT supported, the aggr kprobe is
+	 * released at the same time that the last aggregated kprobe is
+	 * unregistered.
+	 * Thus there should be no chance to reuse unused kprobe.
+	 */
 	printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
-	BUG_ON(kprobe_unused(ap));
+	return -EINVAL;
 }
 
 static void free_aggr_kprobe(struct kprobe *p)
@@ -1315,9 +1325,12 @@ static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
 			goto out;
 		}
 		init_aggr_kprobe(ap, orig_p);
-	} else if (kprobe_unused(ap))
+	} else if (kprobe_unused(ap)) {
 		/* This probe is going to die. Rescue it */
-		reuse_unused_kprobe(ap);
+		ret = reuse_unused_kprobe(ap);
+		if (ret)
+			goto out;
+	}
 
 	if (kprobe_gone(ap)) {
 		/*
author maintainer
#9 lore raw

PATCH v2 5/5 kprobes: Don't call BUG_ON if there is a kprobe in use on free list

In reply to Masami Hiramatsu

Instead of calling BUG_ON, if we find a kprobe in use on free kprobe list, just remove it from the list and keep it on kprobe hash list as same as other in-use kprobes.

---
Changes in v2:
- Use WARN_ON_ONCE() for unusual case.

---
 kernel/kprobes.c |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/kprobes.c +7 -1
index 63c342e5e6c3..90e98e233647 100644
@@ -546,8 +546,14 @@ static void do_free_cleaned_kprobes(void)
 	struct optimized_kprobe *op, *tmp;
 
 	list_for_each_entry_safe(op, tmp, &freeing_list, list) {
-		BUG_ON(!kprobe_unused(&op->kp));
 		list_del_init(&op->list);
+		if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
+			/*
+			 * This must not happen, but if there is a kprobe
+			 * still in use, keep it on kprobes hash list.
+			 */
+			continue;
+		}
 		free_aggr_kprobe(&op->kp);
 	}
 }
#10 lore raw

[tip:perf/core] kprobes: Don't call BUG_ON() if there is a kprobe in use on free list

kprobes: Don't call BUG_ON() if there is a kprobe in use on free list
Commit-ID:  cbdd96f5586151e48317d90a403941ec23f12660
Gitweb:     https://git.kernel.org/tip/cbdd96f5586151e48317d90a403941ec23f12660
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Tue, 11 Sep 2018 19:21:09 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 12 Sep 2018 08:01:16 +0200

kprobes: Don't call BUG_ON() if there is a kprobe in use on free list

Instead of calling BUG_ON(), if we find a kprobe in use on free kprobe list, just remove it from the list and keep it on kprobe hash list as same as other in-use kprobes.

---
 kernel/kprobes.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/kprobes.c +7 -1
index 63c342e5e6c3..90e98e233647 100644
@@ -546,8 +546,14 @@ static void do_free_cleaned_kprobes(void)
 	struct optimized_kprobe *op, *tmp;
 
 	list_for_each_entry_safe(op, tmp, &freeing_list, list) {
-		BUG_ON(!kprobe_unused(&op->kp));
 		list_del_init(&op->list);
+		if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
+			/*
+			 * This must not happen, but if there is a kprobe
+			 * still in use, keep it on kprobes hash list.
+			 */
+			continue;
+		}
 		free_aggr_kprobe(&op->kp);
 	}
 }