From patchwork Wed Jul 10 14:15:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11038639 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4605214DB for ; Wed, 10 Jul 2019 14:16:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 37676289CC for ; Wed, 10 Jul 2019 14:16:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A924289B9; Wed, 10 Jul 2019 14:16:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87838287D3 for ; Wed, 10 Jul 2019 14:16:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727764AbfGJOQC (ORCPT ); Wed, 10 Jul 2019 10:16:02 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:47067 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727325AbfGJOQB (ORCPT ); Wed, 10 Jul 2019 10:16:01 -0400 Received: by mail-pg1-f194.google.com with SMTP id i8so1314460pgm.13 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=pWWFcWDn4k/4+FLG/itXgZSd20HQRxcv0EKXzOSh3eRvRGrMg1x0IZkYkoq+6kO7Cg Vz/DxmCJamkgnyrsECtGTr33fS7ppT1Z9+GUd5weyB6/cpgNDgFGPKwLtpFsMQqtwdft S0/tYiHvpvPgZcVSeE0yVlunUmaR9UzVp0INWvtKloXWc97dtmpPIShZ/KABnwcjzv8f VaoNsPosKgO7CUlAwPF6yFsR7R/FyNHsHPjpZkIax9uG9HGeneoRkHInUxqZ6h6a2i1K bgJ1UgkvFVxicgHZkTGMkx0gg9O4QLqEOfK5iLJL1lJmDLz6jhrT+9NKsKWoDCuzpsT7 RrTw== X-Gm-Message-State: APjAAAXRxBMRc5NXW1sNsWZoUPIS1TgcoOcDP1m6EHVgrdA90+s7XgvU cMRn5qR+sfNOEF1cDBDbirzSRg== X-Google-Smtp-Source: APXvYqyVj2COh2XkJL7w391puzrxf+GO5LeBkj/huVRuoJfj6uXECXP7PK41v0m8skcXqm2b1c+vuw== 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 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 From patchwork Wed Jul 10 14:15:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11038637 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6D4E414F6 for ; Wed, 10 Jul 2019 14:16:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5D4D328774 for ; Wed, 10 Jul 2019 14:16:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 50B76287DC; Wed, 10 Jul 2019 14:16:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9AB0D2897A for ; Wed, 10 Jul 2019 14:16:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727899AbfGJOQI (ORCPT ); Wed, 10 Jul 2019 10:16:08 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:36343 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727840AbfGJOQF (ORCPT ); Wed, 10 Jul 2019 10:16:05 -0400 Received: by mail-pg1-f196.google.com with SMTP id l21so1339810pgm.3 for ; Wed, 10 Jul 2019 07:16:05 -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=AmgXE8R5A1l8PFdEQCWW828K08NV6IQfuZCkIZHdV04=; b=yNmqZE9y/HCyaXknSDANClLo8m1KbCwJuyzu12fI2stRfDmpaP04RVzd7Iz+0WF0Xi r7069QIm19EswUkCqRmJuwKjqrNnb6OnfgKq5Sm5wqUkzCV3GXW4sA1HEmA9tVgnUITC FaT6jVRZdgMDq6D42Mq1aLAJYZLDgqcNsBy+E= 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=AmgXE8R5A1l8PFdEQCWW828K08NV6IQfuZCkIZHdV04=; b=nDfWQUsvICVCgX6Hl0D7ABnb98B6m9s5tagThKdkdvR2VKo2B7xzw2afKFrqB5jKTJ H6GXPqCyLFytjWQu57KiSyqa7wuFKs+cznB/c8BxIzmP5sTa8n4CB5hAhFp2Qz7MMK/G bxB5w6DaPceT3lsBJOOPorUCk4NcMdrXhneRBrsCK+zTgR0RRQDHdCC7jdpROHpuZGpC CTKSkUivdpScJasxN7C2xlrqi0o/andKFCM7eM0Z1zhtLnFtGE5F2msdS3v83R2X3Iuv Xo1Qi4ArDGgmQV1XEW0+3SPgYHe38f9nV6fWoxtfZ4MqV6B/lqNJARmyJTBwpDfUVgsd c58A== X-Gm-Message-State: APjAAAUSzJsX8s7emBOslICQUE2RJpWii7YAg0kmk8085FZpNj5Y0yQZ ILEGxRrUlvvRZPOKwK/qLbsNjg== X-Google-Smtp-Source: APXvYqzPoycqb2n3e29kQYZVL3KTErAQGB2rsrgdO0LYMSG9rIGtHDYdJHz7JfaDzIgu3l0p7Zh0zA== X-Received: by 2002:a65:404d:: with SMTP id h13mr36664076pgp.71.1562768164742; Wed, 10 Jul 2019 07:16:04 -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.16.00 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 07:16:04 -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 2/4] trace/bpf: Add support for attach/detach of ftrace events to BPF Date: Wed, 10 Jul 2019 10:15:46 -0400 Message-Id: <20190710141548.132193-3-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 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add a new bpf file to each trace event. The following commands can be written into it: attach: Attaches BPF prog fd to tracepoint detach: Detaches BPF prog fd to tracepoint Reading the bpf file will show all the attached programs to the tracepoint. Signed-off-by: Joel Fernandes (Google) --- include/linux/bpf_trace.h | 6 ++ include/linux/trace_events.h | 1 + kernel/trace/bpf_trace.c | 169 +++++++++++++++++++++++++++++++++++ kernel/trace/trace.h | 1 + kernel/trace/trace_events.c | 9 +- 5 files changed, 184 insertions(+), 2 deletions(-) diff --git a/include/linux/bpf_trace.h b/include/linux/bpf_trace.h index 4a593827fd87..1fe73501809c 100644 --- a/include/linux/bpf_trace.h +++ b/include/linux/bpf_trace.h @@ -9,6 +9,12 @@ struct bpf_raw_tracepoint { struct bpf_raw_event_map *btp; struct bpf_prog *prog; + /* + * Multiple programs can be attached to a tracepoint, + * All of these are linked to each other and can be reached + * from the event's bpf_attach file in tracefs. + */ + struct list_head event_attached; }; struct bpf_raw_tracepoint *bpf_raw_tracepoint_open(char *tp_name, int prog_fd); diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h index 8a62731673f7..525f2ac44aa3 100644 --- a/include/linux/trace_events.h +++ b/include/linux/trace_events.h @@ -371,6 +371,7 @@ struct trace_event_file { struct trace_array *tr; struct trace_subsystem_dir *system; struct list_head triggers; + struct list_head bpf_attached; /* * 32 bit flags: diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index c4b543bc617f..28621ad88c12 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1469,3 +1469,172 @@ struct bpf_raw_tracepoint *bpf_raw_tracepoint_open(char *tp_name, int prog_fd) bpf_put_raw_tracepoint(btp); return ERR_PTR(err); } + +enum event_bpf_cmd { BPF_ATTACH, BPF_DETACH }; +#define BPF_CMD_BUF_LEN 32 + +static ssize_t +event_bpf_attach_write(struct file *filp, const char __user *ubuf, + size_t cnt, loff_t *ppos) +{ + int err, prog_fd, cmd_num, len; + struct trace_event_call *call; + struct trace_event_file *file; + struct bpf_raw_tracepoint *raw_tp, *next; + char buf[BPF_CMD_BUF_LEN], *end, *tok; + enum event_bpf_cmd cmd; + struct bpf_prog *prog; + bool prog_put = true; + + len = min((int)cnt, BPF_CMD_BUF_LEN - 1); + + err = copy_from_user(buf, ubuf, len); + if (err) + return err; + buf[len] = 0; + + /* Parse 2 arguments of format: : */ + end = &buf[0]; + cmd_num = 1; + while (cmd_num < 3) { + tok = strsep(&end, ":"); + if (!tok) + return -EINVAL; + + switch (cmd_num) { + case 1: + if (!strncmp(tok, "attach", 6)) + cmd = BPF_ATTACH; + else if (!strncmp(tok, "detach", 6)) + cmd = BPF_DETACH; + else + return -EINVAL; + break; + case 2: + err = kstrtoint(tok, 10, &prog_fd); + if (err) + return err; + break; + } + cmd_num++; + } + if (cmd_num != 3) + return -EINVAL; + + file = event_file_data(filp); + /* Command is to attach fd to tracepoint */ + if (cmd == BPF_ATTACH) { + mutex_lock(&event_mutex); + call = file->event_call; + + raw_tp = bpf_raw_tracepoint_open((char *)call->tp->name, + prog_fd); + if (IS_ERR(raw_tp)) { + mutex_unlock(&event_mutex); + return PTR_ERR(raw_tp); + } + + list_add(&raw_tp->event_attached, &file->bpf_attached); + mutex_unlock(&event_mutex); + *ppos += cnt; + return cnt; + } + + /* Command is to detach fd from tracepoint */ + prog = bpf_prog_get(prog_fd); + if (IS_ERR(prog)) + return PTR_ERR(prog); + + mutex_lock(&event_mutex); + list_for_each_entry_safe(raw_tp, next, &file->bpf_attached, + event_attached) { + if (raw_tp->prog == prog) { + list_del(&raw_tp->event_attached); + bpf_raw_tracepoint_close(raw_tp); + prog_put = false; + break; + } + } + mutex_unlock(&event_mutex); + + if (prog_put) + bpf_prog_put(prog); + *ppos += cnt; + return cnt; +} + +static void *event_bpf_attach_next(struct seq_file *m, void *t, loff_t *pos) +{ + struct trace_event_file *file = event_file_data(m->private); + + return seq_list_next(t, &file->bpf_attached, pos); +} + +static void *event_bpf_attach_start(struct seq_file *m, loff_t *pos) +{ + struct trace_event_file *event_file; + + /* ->stop() is called even if ->start() fails */ + mutex_lock(&event_mutex); + event_file = event_file_data(m->private); + if (unlikely(!event_file)) + return ERR_PTR(-ENODEV); + + if (list_empty(&event_file->bpf_attached)) + return NULL; + + return seq_list_start(&event_file->bpf_attached, *pos); +} + +static void event_bpf_attach_stop(struct seq_file *m, void *t) +{ + mutex_unlock(&event_mutex); +} + +static int event_bpf_attach_show(struct seq_file *m, void *v) +{ + struct bpf_raw_tracepoint *raw_tp; + + raw_tp = list_entry(v, struct bpf_raw_tracepoint, event_attached); + seq_printf(m, "prog id: %u\n", raw_tp->prog->aux->id); + return 0; +} + +static const struct seq_operations event_bpf_attach_seq_ops = { + .start = event_bpf_attach_start, + .next = event_bpf_attach_next, + .stop = event_bpf_attach_stop, + .show = event_bpf_attach_show, +}; + +static int event_bpf_attach_open(struct inode *inode, struct file *file) +{ + int ret = 0; + + mutex_lock(&event_mutex); + + if (unlikely(!event_file_data(file))) { + mutex_unlock(&event_mutex); + return -ENODEV; + } + + if (file->f_mode & FMODE_READ) { + ret = seq_open(file, &event_bpf_attach_seq_ops); + if (!ret) { + struct seq_file *m = file->private_data; + + m->private = file; + } + } + + mutex_unlock(&event_mutex); + + return ret; +} + +const struct file_operations event_bpf_attach_fops = { + .open = event_bpf_attach_open, + .read = seq_read, + .write = event_bpf_attach_write, + .llseek = default_llseek, +}; diff --git a/kernel/trace/trace.h b/kernel/trace/trace.h index 005f08629b8b..e33828d24eb2 100644 --- a/kernel/trace/trace.h +++ b/kernel/trace/trace.h @@ -1582,6 +1582,7 @@ extern struct list_head ftrace_events; extern const struct file_operations event_trigger_fops; extern const struct file_operations event_hist_fops; +extern const struct file_operations event_bpf_attach_fops; #ifdef CONFIG_HIST_TRIGGERS extern int register_trigger_hist_cmd(void); diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index 67851fb66b6b..79420d5efaef 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -2018,8 +2018,10 @@ 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_BPF_EVENTS + trace_create_file("bpf", 0644, file->dir, file, + &event_bpf_attach_fops); +#endif } #ifdef CONFIG_HIST_TRIGGERS @@ -2267,6 +2269,9 @@ trace_create_new_event(struct trace_event_call *call, atomic_set(&file->sm_ref, 0); atomic_set(&file->tm_ref, 0); INIT_LIST_HEAD(&file->triggers); +#ifdef CONFIG_BPF_EVENTS + INIT_LIST_HEAD(&file->bpf_attached); +#endif list_add(&file->list, &tr->events); return file; From patchwork Wed Jul 10 14:15:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11038633 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E5EA814DB for ; Wed, 10 Jul 2019 14:16:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D75BC28761 for ; Wed, 10 Jul 2019 14:16:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9FAD2883A; Wed, 10 Jul 2019 14:16:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4320428774 for ; Wed, 10 Jul 2019 14:16:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727925AbfGJOQL (ORCPT ); Wed, 10 Jul 2019 10:16:11 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:40070 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727913AbfGJOQJ (ORCPT ); Wed, 10 Jul 2019 10:16:09 -0400 Received: by mail-pf1-f195.google.com with SMTP id p184so1174751pfp.7 for ; Wed, 10 Jul 2019 07:16:08 -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=qaZWTPLSZP/SuE1pr+x4mGiPN4X4stJUVD9vrV0URR8=; b=GwgIjykLh5eqei88bOMpZHwO9qvLmQGem/1teyQxrRIg3YrPgEf4AC8HboS8y7PhCl sy7rYfBqLYAeSY65gUJlTPXjcgqU4IhpXMXWiHRkx7xCL6pL55RQehoZUGCSFc9yfrSt fHOMiuneC4CLBcIHhOEHHaXtAta0bdXQkhlFM= 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=qaZWTPLSZP/SuE1pr+x4mGiPN4X4stJUVD9vrV0URR8=; b=LhvIOffaYoj8g8hGKKrmjqZKQ/rcoWfOcP9FHkNnhLE+w2u4Vkicy+pSvrWQng6ULX c1rpzc722T/5HZof1g/yDupkgojYnNyWCciQZorUebSToQPxcrgBI5A/W4F2QNUFrEnb FzO6RG+SxVGm3yHBS/ptiTVPAQ3c5JrH3qmiQFBlU4fiIUYyfd6RCZEkxlDVyHerDpFD 5OEsDUchc8DI6QEuWHlCzGnhrgwxf8eF4JuCO7hyE+6svA8f1KN2yzYXE5X2dmAIPMW4 ZrEZdCwEM4ZFZgGBTiCD/fbj2kIeET+O20OblrW7DdFamBkf9/ZRBl844D9hoouEgkxV W61Q== X-Gm-Message-State: APjAAAUAYO1aeGXtRf/Uf0RXAMCQfCtUm5vYGlYg0OByUW4Q7wISRssv T/WNPa1fItvsSw1kLIPjbdg= X-Google-Smtp-Source: APXvYqzQgccO1TM8KZRW5WDsyupbUc+d49Jul12Ceh/xECkPSx2c0EUIk9dUXzEvGjVaaXChliSSdA== X-Received: by 2002:a17:90a:bb94:: with SMTP id v20mr7412797pjr.88.1562768168516; Wed, 10 Jul 2019 07:16:08 -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.16.04 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 07:16:07 -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 3/4] lib/bpf: Add support for ftrace event attach and detach Date: Wed, 10 Jul 2019 10:15:47 -0400 Message-Id: <20190710141548.132193-4-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 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add the needed library support in this commit. Signed-off-by: Joel Fernandes (Google) --- tools/lib/bpf/bpf.c | 53 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/bpf.h | 4 +++ tools/lib/bpf/libbpf.map | 2 ++ 3 files changed, 59 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index c4a48086dc9a..28c5a7d00d14 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -24,6 +24,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -57,6 +60,8 @@ #define min(x, y) ((x) < (y) ? (x) : (y)) #endif +#define TRACEFS "/sys/kernel/debug/tracing" + static inline __u64 ptr_to_u64(const void *ptr) { return (__u64) (unsigned long) ptr; @@ -658,6 +663,54 @@ int bpf_raw_tracepoint_open(const char *name, int prog_fd) return sys_bpf(BPF_RAW_TRACEPOINT_OPEN, &attr, sizeof(attr)); } +int bpf_raw_tracepoint_ftrace_attach(const char *subsys, const char *name, + int prog_fd) +{ + char buf[256]; + int len, ret, tfd; + + sprintf(buf, "%s/events/%s/%s/bpf", TRACEFS, subsys, name); + tfd = open(buf, O_WRONLY); + if (tfd < 0) + return tfd; + + sprintf(buf, "attach:%d", prog_fd); + len = strlen(buf); + ret = write(tfd, buf, len); + + if (ret < 0) + goto err; + if (ret != len) + ret = -1; +err: + close(tfd); + return ret; +} + +int bpf_raw_tracepoint_ftrace_detach(const char *subsys, const char *name, + int prog_fd) +{ + char buf[256]; + int len, ret, tfd; + + sprintf(buf, "%s/events/%s/%s/bpf", TRACEFS, subsys, name); + tfd = open(buf, O_WRONLY); + if (tfd < 0) + return tfd; + + sprintf(buf, "detach:%d", prog_fd); + len = strlen(buf); + ret = write(tfd, buf, len); + + if (ret < 0) + goto err; + if (ret != len) + ret = -1; +err: + close(tfd); + return ret; +} + int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log) { diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 9593fec75652..5b9c44658037 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -163,6 +163,10 @@ LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type, __u32 query_flags, __u32 *attach_flags, __u32 *prog_ids, __u32 *prog_cnt); LIBBPF_API int bpf_raw_tracepoint_open(const char *name, int prog_fd); +LIBBPF_API int bpf_raw_tracepoint_ftrace_attach(const char *subsys, + const char *name, int prog_fd); +LIBBPF_API int bpf_raw_tracepoint_ftrace_detach(const char *subsys, + const char *name, int prog_fd); LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, __u32 log_buf_size, bool do_log); LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 673001787cba..fca377b688c2 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -163,4 +163,6 @@ LIBBPF_0.0.3 { bpf_map__is_internal; bpf_map_freeze; btf__finalize_data; + bpf_raw_tracepoint_ftrace_attach; + bpf_raw_tracepoint_ftrace_detach; } LIBBPF_0.0.2; From patchwork Wed Jul 10 14:15:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Fernandes X-Patchwork-Id: 11038643 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6C55913BD for ; Wed, 10 Jul 2019 14:16:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5ED1D2883A for ; Wed, 10 Jul 2019 14:16:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 52BE0289CF; Wed, 10 Jul 2019 14:16:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AED47289B3 for ; Wed, 10 Jul 2019 14:16:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727938AbfGJOQT (ORCPT ); Wed, 10 Jul 2019 10:16:19 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:32853 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727932AbfGJOQN (ORCPT ); Wed, 10 Jul 2019 10:16:13 -0400 Received: by mail-pf1-f196.google.com with SMTP id g2so1189844pfq.0 for ; Wed, 10 Jul 2019 07:16:12 -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=Yg+Fia+4TffgvjfVtZaMrwTvAUof2HACdCWfE9Adz90=; b=puPKxWZQb+LZdgernOXSGHoCjjFfNqhgNCQgR3EAJVuYHTxPi0MWhE1gMoTZzoxDtL JPgeBqpXoNCnj35nK/tB1FCJMvJg2YoHOxNlJ0Vchnf+Zhz2VlASaorgKKmUkgTvSIJf 8FW/bsoH46dLIF8XeGGCXT64TEZRMHpQZe0+E= 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=Yg+Fia+4TffgvjfVtZaMrwTvAUof2HACdCWfE9Adz90=; b=Rc7Gdng1AtZHZQWn91n1t+zKzWRgVZce0OjfOLPDqeBPNPfALQTvmHbZtjjE3di8RI yPSil7du6hW8FS7L45FalBMyNrPdjBc35xRk/hJF4mmy8NIkJ+yYNLQoVjiuiaqhcWin oO8jRj9pPEhyNwnIR9wnSiY1GMeC4kur/28AaT4qUcGPmq3LxPA+UKoabx13xEHMIR3+ xo7bcLB2gQvVb08bI9wyDyk/8P+Uj4nSrs401kdVLIQN495gSEzi/ypAAwhwPzso9rk3 FjLdewhHHd941UgDLFW0YwOf3E/avC51AmAefGrjHHh5AxBZq+1QggAWolkYFR8litu2 uSHQ== X-Gm-Message-State: APjAAAUJUQ+uftoCJhrs3TvQbXOwwAC/HW9So2QM5L49T0XRsVsaDnfL /5IWs+SYLQAWSvyotfVVSdU= X-Google-Smtp-Source: APXvYqxireE36SRG7vQaha91GKI62TdGTjOZ+qzVeiQZzoJfKbV6cQyUm55T8TcIjbBj3K5Tt9zuKQ== X-Received: by 2002:a17:90a:290b:: with SMTP id g11mr7342173pjd.122.1562768172230; Wed, 10 Jul 2019 07:16:12 -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.16.08 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 07:16:11 -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 4/4] selftests/bpf: Add test for ftrace-based BPF attach/detach Date: Wed, 10 Jul 2019 10:15:48 -0400 Message-Id: <20190710141548.132193-5-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 Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Here we add support for testing the attach and detach of a BPF program to a tracepoint through tracefs. Signed-off-by: Joel Fernandes (Google) --- .../raw_tp_writable_test_ftrace_run.c | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c diff --git a/tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c b/tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c new file mode 100644 index 000000000000..7b42e3a69b71 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/raw_tp_writable_test_ftrace_run.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +void test_raw_tp_writable_test_ftrace_run(void) +{ + __u32 duration = 0; + char error[4096]; + int ret; + + const struct bpf_insn trace_program[] = { + BPF_LDX_MEM(BPF_DW, BPF_REG_6, BPF_REG_1, 0), + BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_6, 0), + BPF_MOV64_IMM(BPF_REG_0, 42), + BPF_STX_MEM(BPF_W, BPF_REG_6, BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + + struct bpf_load_program_attr load_attr = { + .prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE, + .license = "GPL v2", + .insns = trace_program, + .insns_cnt = sizeof(trace_program) / sizeof(struct bpf_insn), + .log_level = 2, + }; + + int bpf_fd = bpf_load_program_xattr(&load_attr, error, sizeof(error)); + + if (CHECK(bpf_fd < 0, "bpf_raw_tracepoint_writable loaded", + "failed: %d errno %d\n", bpf_fd, errno)) + return; + + const struct bpf_insn skb_program[] = { + BPF_MOV64_IMM(BPF_REG_0, 0), + BPF_EXIT_INSN(), + }; + + struct bpf_load_program_attr skb_load_attr = { + .prog_type = BPF_PROG_TYPE_SOCKET_FILTER, + .license = "GPL v2", + .insns = skb_program, + .insns_cnt = sizeof(skb_program) / sizeof(struct bpf_insn), + }; + + int filter_fd = + bpf_load_program_xattr(&skb_load_attr, error, sizeof(error)); + if (CHECK(filter_fd < 0, "test_program_loaded", "failed: %d errno %d\n", + filter_fd, errno)) + goto out_bpffd; + + ret = bpf_raw_tracepoint_ftrace_attach("bpf_test_run", + "bpf_test_finish", + bpf_fd); + if (CHECK(ret < 0, "bpf_raw_tracepoint_ftrace_attach", + "failed: %d errno %d\n", ret, errno)) + goto out_filterfd; + + char test_skb[128] = { + 0, + }; + + __u32 prog_ret; + int err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0, + 0, &prog_ret, 0); + CHECK(err != 42, "test_run", + "tracepoint did not modify return value\n"); + CHECK(prog_ret != 0, "test_run_ret", + "socket_filter did not return 0\n"); + + ret = bpf_raw_tracepoint_ftrace_detach("bpf_test_run", + "bpf_test_finish", + bpf_fd); + if (CHECK(ret < 0, "bpf_raw_tracepoint_ftrace_detach", + "failed: %d errno %d\n", ret, errno)) + goto out_filterfd; + + err = bpf_prog_test_run(filter_fd, 1, test_skb, sizeof(test_skb), 0, 0, + &prog_ret, 0); + CHECK(err != 0, "test_run_notrace", + "test_run failed with %d errno %d\n", err, errno); + CHECK(prog_ret != 0, "test_run_ret_notrace", + "socket_filter did not return 0\n"); + +out_filterfd: + close(filter_fd); +out_bpffd: + close(bpf_fd); +}