Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9280183ybi; Wed, 10 Jul 2019 07:39:32 -0700 (PDT) X-Google-Smtp-Source: APXvYqySUkI1VMiLaCT5y004bGEN7X+sVFzdg+GQrg3cklb3RJ7MmV108ggNSQ4BJdEFWeGaFE8B X-Received: by 2002:a17:90a:374a:: with SMTP id u68mr7440711pjb.4.1562769572749; Wed, 10 Jul 2019 07:39:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562769572; cv=none; d=google.com; s=arc-20160816; b=YuQFCbeeA99mdQF4aiMvWofIUJqu01hEwNDhyTrxgcFXXY3fktERfpbVbsxWixcS69 zs2duYjs3FtTPQviiH5SXbkkyR2pUnRavFBj+p+CimpTg7CyICXSL6A78mh3fjvVwmJY 66WvffO+AJFaH+kadNvXZvOJ5xsSIrrxQnGnexQ4Oq6mN97kaRZTPYn3RSUzVk+T78Ih cjF+S8Svc1kb1nB07DH6ykoRJKkp+qlMQN0Bdic1y0hcNHEJ5qNKXEYZXu68Vj6/aK7C y8NhJ2ebc5ThB3nPKEdtAr1Y5haM2zNBKCLYTMq0HGdrEFklGIYHUv7D7Rsl6dNaC86V 1omw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=98vQqac6N+4BDAWZ6eZd2FQ5tBz4a8s/ZIoQ2kqYqQU=; b=Dq/VZ2wz0JN+Gqihav+QDxXKwf+s1hSaL6R5PtXmrdWrIxbeOV9YBcqxMREI3LhxQs tykRKcG4wGh+lgzfWoptjW1SARBZaYMCaCvgL3ri3RYkPuABT22j/K6O4hh+P95ABPC9 XX7F2IUgpcMmq1u6FPJh0aQxJ9QVrUOQ0D+iPLRH7TkYKrYIhWayzgpLcl5rj8C4nCzT K5j7GwnH3L/Hvrz8gZFqe8Q1PI+ymhYf/nia3xnz/TeQZAL7Pxu3KCHkPHn42rGSlhOQ vtvXQoV+BKgD/BOChCwy+7RrDchBO6hrK9DniTs4kB8D+7ZH1+lWdLL+Nnl/7PDWVkkD FuNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=jT4rr1dR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v15si2353613pfm.238.2019.07.10.07.39.16; Wed, 10 Jul 2019 07:39:32 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=jT4rr1dR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727421AbfGJOQD (ORCPT + 99 others); Wed, 10 Jul 2019 10:16:03 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:34637 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727678AbfGJOQC (ORCPT ); Wed, 10 Jul 2019 10:16:02 -0400 Received: by mail-pf1-f193.google.com with SMTP id b13so1184759pfo.1 for ; Wed, 10 Jul 2019 07:16:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=98vQqac6N+4BDAWZ6eZd2FQ5tBz4a8s/ZIoQ2kqYqQU=; b=jT4rr1dRIIdkKbYCMVTHZ2jKpCD13OQ/g/idVRGQ/vM8Hhd00Xsma2pTkwoIGRALKP x68FvAFXmQBZ6S2Qi2SPDHxjnppLoAFpyb3FTLxg/USKswIOdGBQigG+QfVdUa434Ja6 GC2ykyqsyvETZsJEuquXZfNrr/rdVrerBxtaw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=98vQqac6N+4BDAWZ6eZd2FQ5tBz4a8s/ZIoQ2kqYqQU=; b=UHwxmRmT+I8mvah5OEnP8shzWKHRu0DMVFOZ9+9tIoo+qxQxNXcnEGsTQunaMDiEb4 35lzjCBU8UfSQEYWy8GQaknqhkiqlBiWPWmlEhvYNovV4rAkQ79XohmZf/ZD4/HcOWLP HbXQmhq7pbctis37ysdhvuL1w3ubmpdE6LBdeRaVkA7x0uuHvWSHgieFYpUlOqBSrIsX lGDwWW4JON+4DlhTxEGyS3j3rkOvJFF3tEypVcuEC4W/WNHMQns1OLeciGi38CYp95e0 VZ6B4viY4prZauydMSdEYRhqgK65xxGjvhHH9Bf/4VVENssusruB7Z9GlpmQP2Ybe3Qd fR4A== X-Gm-Message-State: APjAAAVvaUTZNBYjmi+g7FfxvMQ3jl0c1YDR3KAZTc4u87t+HqZ5A9N7 LJdzUsko+IHrOEumuNJg+9BGj4doJQc= X-Received: by 2002:a63:b904:: with SMTP id z4mr1266112pge.388.1562768160724; Wed, 10 Jul 2019 07:16:00 -0700 (PDT) Received: from joelaf.cam.corp.google.com ([2620:15c:6:12:9c46:e0da:efbf:69cc]) by smtp.gmail.com with ESMTPSA id l124sm2589249pgl.54.2019.07.10.07.15.57 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 07:16:00 -0700 (PDT) From: "Joel Fernandes (Google)" To: linux-kernel@vger.kernel.org Cc: "Joel Fernandes (Google)" , Adrian Ratiu , Alexei Starovoitov , bpf@vger.kernel.org, Brendan Gregg , connoro@google.com, Daniel Borkmann , duyuchao , Ingo Molnar , jeffv@google.com, Karim Yaghmour , kernel-team@android.com, linux-kselftest@vger.kernel.org, Manali Shukla , Manjo Raja Rao , Martin KaFai Lau , Masami Hiramatsu , Matt Mullins , Michal Gregorczyk , Michal Gregorczyk , Mohammad Husain , namhyung@google.com, namhyung@kernel.org, netdev@vger.kernel.org, paul.chaignon@gmail.com, primiano@google.com, Qais Yousef , Shuah Khan , Song Liu , Srinivas Ramana , Steven Rostedt , Tamir Carmeli , Yonghong Song Subject: [PATCH RFC 1/4] Move bpf_raw_tracepoint functionality into bpf_trace.c Date: Wed, 10 Jul 2019 10:15:45 -0400 Message-Id: <20190710141548.132193-2-joel@joelfernandes.org> X-Mailer: git-send-email 2.22.0.410.gd8fdbe21b5-goog In-Reply-To: <20190710141548.132193-1-joel@joelfernandes.org> References: <20190710141548.132193-1-joel@joelfernandes.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In preparation to use raw tracepoints for BPF directly from ftrace, move the bpf_raw_tracepoint functionality into bpf_trace.c Signed-off-by: Joel Fernandes (Google) --- include/linux/bpf_trace.h | 10 ++++++ kernel/bpf/syscall.c | 69 ++++++------------------------------- kernel/trace/bpf_trace.c | 56 ++++++++++++++++++++++++++++++ kernel/trace/trace_events.c | 3 ++ 4 files changed, 80 insertions(+), 58 deletions(-) diff --git a/include/linux/bpf_trace.h b/include/linux/bpf_trace.h index ddf896abcfb6..4a593827fd87 100644 --- a/include/linux/bpf_trace.h +++ b/include/linux/bpf_trace.h @@ -4,4 +4,14 @@ #include +#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd + +struct bpf_raw_tracepoint { + struct bpf_raw_event_map *btp; + struct bpf_prog *prog; +}; + +struct bpf_raw_tracepoint *bpf_raw_tracepoint_open(char *tp_name, int prog_fd); +void bpf_raw_tracepoint_close(struct bpf_raw_tracepoint *tp); + #endif /* __LINUX_BPF_TRACE_H__ */ diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 42d17f730780..2001949b33f1 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -1737,21 +1737,11 @@ static int bpf_obj_get(const union bpf_attr *attr) attr->file_flags); } -struct bpf_raw_tracepoint { - struct bpf_raw_event_map *btp; - struct bpf_prog *prog; -}; - static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp) { struct bpf_raw_tracepoint *raw_tp = filp->private_data; - if (raw_tp->prog) { - bpf_probe_unregister(raw_tp->btp, raw_tp->prog); - bpf_prog_put(raw_tp->prog); - } - bpf_put_raw_tracepoint(raw_tp->btp); - kfree(raw_tp); + bpf_raw_tracepoint_close(raw_tp); return 0; } @@ -1761,64 +1751,27 @@ static const struct file_operations bpf_raw_tp_fops = { .write = bpf_dummy_write, }; -#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd - -static int bpf_raw_tracepoint_open(const union bpf_attr *attr) +static int bpf_raw_tracepoint_open_syscall(const union bpf_attr *attr) { - struct bpf_raw_tracepoint *raw_tp; - struct bpf_raw_event_map *btp; - struct bpf_prog *prog; + int tp_fd; char tp_name[128]; - int tp_fd, err; + struct bpf_raw_tracepoint *raw_tp; if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name), sizeof(tp_name) - 1) < 0) return -EFAULT; tp_name[sizeof(tp_name) - 1] = 0; - btp = bpf_get_raw_tracepoint(tp_name); - if (!btp) - return -ENOENT; - - raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); - if (!raw_tp) { - err = -ENOMEM; - goto out_put_btp; - } - raw_tp->btp = btp; - - prog = bpf_prog_get(attr->raw_tracepoint.prog_fd); - if (IS_ERR(prog)) { - err = PTR_ERR(prog); - goto out_free_tp; - } - if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT && - prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) { - err = -EINVAL; - goto out_put_prog; - } - - err = bpf_probe_register(raw_tp->btp, prog); - if (err) - goto out_put_prog; + raw_tp = bpf_raw_tracepoint_open(tp_name, attr->raw_tracepoint.prog_fd); + if (IS_ERR(raw_tp)) + return PTR_ERR(raw_tp); - raw_tp->prog = prog; tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp, O_CLOEXEC); - if (tp_fd < 0) { - bpf_probe_unregister(raw_tp->btp, prog); - err = tp_fd; - goto out_put_prog; - } - return tp_fd; + if (tp_fd < 0) + bpf_probe_unregister(raw_tp->btp, raw_tp->prog); -out_put_prog: - bpf_prog_put(prog); -out_free_tp: - kfree(raw_tp); -out_put_btp: - bpf_put_raw_tracepoint(btp); - return err; + return tp_fd; } static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog, @@ -2848,7 +2801,7 @@ SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, siz err = bpf_obj_get_info_by_fd(&attr, uattr); break; case BPF_RAW_TRACEPOINT_OPEN: - err = bpf_raw_tracepoint_open(&attr); + err = bpf_raw_tracepoint_open_syscall(&attr); break; case BPF_BTF_LOAD: err = bpf_btf_load(&attr); diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 1c9a4745e596..c4b543bc617f 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -1413,3 +1414,58 @@ static int __init bpf_event_init(void) fs_initcall(bpf_event_init); #endif /* CONFIG_MODULES */ + +void bpf_raw_tracepoint_close(struct bpf_raw_tracepoint *raw_tp) +{ + if (raw_tp->prog) { + bpf_probe_unregister(raw_tp->btp, raw_tp->prog); + bpf_prog_put(raw_tp->prog); + } + bpf_put_raw_tracepoint(raw_tp->btp); + kfree(raw_tp); +} + +struct bpf_raw_tracepoint *bpf_raw_tracepoint_open(char *tp_name, int prog_fd) +{ + struct bpf_raw_tracepoint *raw_tp; + struct bpf_raw_event_map *btp; + struct bpf_prog *prog; + int err; + + btp = bpf_get_raw_tracepoint(tp_name); + if (!btp) + return ERR_PTR(-ENOENT); + + raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER); + if (!raw_tp) { + err = -ENOMEM; + goto out_put_btp; + } + raw_tp->btp = btp; + + prog = bpf_prog_get(prog_fd); + if (IS_ERR(prog)) { + err = PTR_ERR(prog); + goto out_free_tp; + } + if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT && + prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) { + err = -EINVAL; + goto out_put_prog; + } + + err = bpf_probe_register(raw_tp->btp, prog); + if (err) + goto out_put_prog; + + raw_tp->prog = prog; + return raw_tp; + +out_put_prog: + bpf_prog_put(prog); +out_free_tp: + kfree(raw_tp); +out_put_btp: + bpf_put_raw_tracepoint(btp); + return ERR_PTR(err); +} diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index 0ce3db67f556..67851fb66b6b 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -2017,6 +2017,9 @@ event_create_dir(struct dentry *parent, struct trace_event_file *file) trace_create_file("trigger", 0644, file->dir, file, &event_trigger_fops); + + trace_create_file("bpf_attach", 0644, file->dir, file, + &bpf_attach_trigger_fops); } #ifdef CONFIG_HIST_TRIGGERS -- 2.22.0.410.gd8fdbe21b5-goog