bpf: allow networking programs to use bpf_trace_printk() for debugging
authorAlexei Starovoitov <ast@plumgrid.com>
Sat, 13 Jun 2015 02:39:13 +0000 (19:39 -0700)
committerDavid S. Miller <davem@davemloft.net>
Mon, 15 Jun 2015 22:53:50 +0000 (15:53 -0700)
bpf_trace_printk() is a helper function used to debug eBPF programs.
Let socket and TC programs use it as well.
Note, it's DEBUG ONLY helper. If it's used in the program,
the kernel will print warning banner to make sure users don't use
it in production.

Signed-off-by: Alexei Starovoitov <ast@plumgrid.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/bpf.h
kernel/bpf/core.c
kernel/trace/bpf_trace.c
net/core/filter.c

index 1b9a3f5b27f6cdfccc5e7e6f85fcb1e7f9dc9a37..4383476a0d4814beb6c03d32feb52e8ce94a0014 100644 (file)
@@ -150,6 +150,7 @@ struct bpf_array {
 u64 bpf_tail_call(u64 ctx, u64 r2, u64 index, u64 r4, u64 r5);
 void bpf_prog_array_map_clear(struct bpf_map *map);
 bool bpf_prog_array_compatible(struct bpf_array *array, const struct bpf_prog *fp);
+const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
 
 #ifdef CONFIG_BPF_SYSCALL
 void bpf_register_prog_type(struct bpf_prog_type_list *tl);
index 1fc45cc830766219bef6a7d7e267a4cd63a436a2..c5bedc82bc1c540bf466c4d2e64f8663a974536d 100644 (file)
@@ -733,6 +733,10 @@ const struct bpf_func_proto bpf_ktime_get_ns_proto __weak;
 const struct bpf_func_proto bpf_get_current_pid_tgid_proto __weak;
 const struct bpf_func_proto bpf_get_current_uid_gid_proto __weak;
 const struct bpf_func_proto bpf_get_current_comm_proto __weak;
+const struct bpf_func_proto * __weak bpf_get_trace_printk_proto(void)
+{
+       return NULL;
+}
 
 /* Always built-in helper functions. */
 const struct bpf_func_proto bpf_tail_call_proto = {
index 3a17638cdf460c9a499357ae3c08e89f3ae6aa0b..4f9b5d41869b320266b8902bd84270d34709df60 100644 (file)
@@ -147,6 +147,17 @@ static const struct bpf_func_proto bpf_trace_printk_proto = {
        .arg2_type      = ARG_CONST_STACK_SIZE,
 };
 
+const struct bpf_func_proto *bpf_get_trace_printk_proto(void)
+{
+       /*
+        * this program might be calling bpf_trace_printk,
+        * so allocate per-cpu printk buffers
+        */
+       trace_printk_init_buffers();
+
+       return &bpf_trace_printk_proto;
+}
+
 static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func_id)
 {
        switch (func_id) {
@@ -168,15 +179,8 @@ static const struct bpf_func_proto *kprobe_prog_func_proto(enum bpf_func_id func
                return &bpf_get_current_uid_gid_proto;
        case BPF_FUNC_get_current_comm:
                return &bpf_get_current_comm_proto;
-
        case BPF_FUNC_trace_printk:
-               /*
-                * this program might be calling bpf_trace_printk,
-                * so allocate per-cpu printk buffers
-                */
-               trace_printk_init_buffers();
-
-               return &bpf_trace_printk_proto;
+               return bpf_get_trace_printk_proto();
        default:
                return NULL;
        }
index 20aa51ccbf9dfd21a0fb2269ebcebf5bdd15368d..65ff107d3d29599ecf728c3c1b1492d4d2cc076f 100644 (file)
@@ -1442,6 +1442,8 @@ sk_filter_func_proto(enum bpf_func_id func_id)
                return &bpf_tail_call_proto;
        case BPF_FUNC_ktime_get_ns:
                return &bpf_ktime_get_ns_proto;
+       case BPF_FUNC_trace_printk:
+               return bpf_get_trace_printk_proto();
        default:
                return NULL;
        }