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;