Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753146AbbGBNvg (ORCPT ); Thu, 2 Jul 2015 09:51:36 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:29661 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753197AbbGBNvO (ORCPT ); Thu, 2 Jul 2015 09:51:14 -0400 From: He Kuang To: , , , , , , , CC: , , Subject: [RFC PATCH v2 1/4] bpf: Put perf_events check ahead of bpf prog Date: Thu, 2 Jul 2015 13:50:40 +0000 Message-ID: <1435845043-107196-2-git-send-email-hekuang@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1435845043-107196-1-git-send-email-hekuang@huawei.com> References: <559386D7.1020208@huawei.com> <1435845043-107196-1-git-send-email-hekuang@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.210] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3903 Lines: 94 When we add a kprobe point and record events by perf, the execution path of all threads on each cpu will enter this point, but perf may only record events on a particular thread or cpu at this kprobe point, a check on call->perf_events list filters out the threads which perf is not recording. Currently, bpf_prog will be entered at the beginning of kprobe_perf_func() before the above check, which makes bpf_prog be executed in every threads including determined not to be recorded threads. A simple test can demonstrate this: 'bpf_prog_on_write.o' contains a bpf prog which outputs to trace buffer when it is entered. Run a background thread 'another-dd' and 'dd' simultaneously, but only record 'dd' thread by perf. The result shows all threads trigger bpf_prog. $ another-dd if=/dev/zero of=test1 bs=4k count=1000000 $ perf record -v --event bpf_prog_on_write.o -- dd if=/dev/zero of=test2 bs=4k count=3 $ cat /sys/kernel/debug/tracing/trace another-dd-1007 [000] d... 120.225835: : generic_perform_write: tgid=1007, pid=1007 another-dd-1007 [000] d... 120.227123: : generic_perform_write: tgid=1007, pid=1007 [repeat many times...] another-dd-1007 [000] d... 120.412395: : generic_perform_write: tgid=1007, pid=1007 another-dd-1007 [000] d... 120.412524: : generic_perform_write: tgid=1007, pid=1007 dd-1009 [000] d... 120.413080: : generic_perform_write: tgid=1009, pid=1009 dd-1009 [000] d... 120.414846: : generic_perform_write: tgid=1009, pid=1009 dd-1009 [000] d... 120.415013: : generic_perform_write: tgid=1009, pid=1009 another-dd-1007 [000] d... 120.416128: : generic_perform_write: tgid=1007, pid=1007 another-dd-1007 [000] d... 120.416295: : generic_perform_write: tgid=1007, pid=1007 This patch moves the check on perf_events list ahead and skip running bpf_prog on threads perf not care. After this patch: $ another-dd if=/dev/zero of=test1 bs=4k count=1000000 $ perf record -v --event bpf_prog_on_write.o -- dd if=/dev/zero of=test2 bs=4k count=3 $ cat /sys/kernel/debug/tracing/trace dd-994 [000] d... 46.386754: : generic_perform_write: tgid=994, pid=994 dd-994 [000] d... 46.389167: : generic_perform_write: tgid=994, pid=994 dd-994 [000] d... 46.389551: : generic_perform_write: tgid=994, pid=994 Signed-off-by: He Kuang Acked-by: Masami Hiramatsu --- kernel/trace/trace_kprobe.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c index d0ce590..5600df8 100644 --- a/kernel/trace/trace_kprobe.c +++ b/kernel/trace/trace_kprobe.c @@ -1141,13 +1141,13 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) int size, __size, dsize; int rctx; - if (prog && !trace_call_bpf(prog, regs)) - return; - head = this_cpu_ptr(call->perf_events); if (hlist_empty(head)) return; + if (prog && !trace_call_bpf(prog, regs)) + return; + dsize = __get_data_size(&tk->tp, regs); __size = sizeof(*entry) + tk->tp.size + dsize; size = ALIGN(__size + sizeof(u32), sizeof(u64)); @@ -1176,13 +1176,13 @@ kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri, int size, __size, dsize; int rctx; - if (prog && !trace_call_bpf(prog, regs)) - return; - head = this_cpu_ptr(call->perf_events); if (hlist_empty(head)) return; + if (prog && !trace_call_bpf(prog, regs)) + return; + dsize = __get_data_size(&tk->tp, regs); __size = sizeof(*entry) + tk->tp.size + dsize; size = ALIGN(__size + sizeof(u32), sizeof(u64)); -- 1.8.5.2 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/