From patchwork Sun Nov 12 23:40:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 837303 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=fb.com header.i=@fb.com header.b="eXsKx5rr"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3yZqyb3v7mz9t20 for ; Mon, 13 Nov 2017 10:40:39 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751359AbdKLXkf (ORCPT ); Sun, 12 Nov 2017 18:40:35 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:57816 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751052AbdKLXkT (ORCPT ); Sun, 12 Nov 2017 18:40:19 -0500 Received: from pps.filterd (m0001255.ppops.net [127.0.0.1]) by mx0b-00082601.pphosted.com (8.16.0.21/8.16.0.21) with SMTP id vACNadU7018555 for ; Sun, 12 Nov 2017 15:40:18 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=Wi+yTGm36T/W+2X8LXSQ+11kXQoJ/I8WhAQZB1McUas=; b=eXsKx5rrDss5P991AB5kPYo+VFGQQWpfMJcTvmrKnL/7hifMR8M82AL2cZCinP7im7se ttbZMPEm8Pv63JvAcx9Pi89uT/jScjfHMb+lPj3SGzPb0oic/RTZ8jcS0raIO/v9aJaZ L48+i3jvY1pxOgsdT02qM7A4Q/OM1tjslaQ= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 2e6xwmr3mg-1 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sun, 12 Nov 2017 15:40:18 -0800 Received: from mx-out.facebook.com (192.168.52.123) by PRN-CHUB05.TheFacebook.com (192.168.16.15) with Microsoft SMTP Server id 14.3.361.1; Sun, 12 Nov 2017 15:40:17 -0800 Received: by devbig102.frc2.facebook.com (Postfix, from userid 4523) id C51F9428249D; Sun, 12 Nov 2017 15:40:16 -0800 (PST) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig102.frc2.facebook.com To: , , , , , , CC: , Song Liu Smtp-Origin-Cluster: frc2c02 Subject: [RFC v2 5/6] bpf: add option for bpf_load.c to use PERF_TYPE_PROBE Date: Sun, 12 Nov 2017 15:40:13 -0800 Message-ID: <20171112234014.2983360-8-songliubraving@fb.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20171112234014.2983360-1-songliubraving@fb.com> References: <20171112234014.2983360-1-songliubraving@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-11-12_10:, , signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Function load_and_attach() is updated to be able to create kprobes with either old text based API, or the new PERF_TYPE_PROBE API. A global flag use_perf_type_probe is added to select between the two APIs. Signed-off-by: Song Liu Reviewed-by: Josef Bacik --- samples/bpf/bpf_load.c | 56 ++++++++++++++++++++++++++++++++------------------ samples/bpf/bpf_load.h | 8 ++++++++ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/samples/bpf/bpf_load.c b/samples/bpf/bpf_load.c index 522ca92..a47cb1c 100644 --- a/samples/bpf/bpf_load.c +++ b/samples/bpf/bpf_load.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -42,6 +41,7 @@ int prog_array_fd = -1; struct bpf_map_data map_data[MAX_MAPS]; int map_data_count = 0; +bool use_perf_type_probe = true; static int populate_prog_array(const char *event, int prog_fd) { @@ -70,8 +70,9 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) size_t insns_cnt = size / sizeof(struct bpf_insn); enum bpf_prog_type prog_type; char buf[256]; - int fd, efd, err, id; + int fd, efd, err, id = -1; struct perf_event_attr attr = {}; + struct probe_desc pd; attr.type = PERF_TYPE_TRACEPOINT; attr.sample_type = PERF_SAMPLE_RAW; @@ -128,7 +129,7 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) return populate_prog_array(event, fd); } - if (is_kprobe || is_kretprobe) { + if (!use_perf_type_probe && (is_kprobe || is_kretprobe)) { if (is_kprobe) event += 7; else @@ -169,27 +170,42 @@ static int load_and_attach(const char *event, struct bpf_insn *prog, int size) strcat(buf, "/id"); } - efd = open(buf, O_RDONLY, 0); - if (efd < 0) { - printf("failed to open event %s\n", event); - return -1; - } - - err = read(efd, buf, sizeof(buf)); - if (err < 0 || err >= sizeof(buf)) { - printf("read from '%s' failed '%s'\n", event, strerror(errno)); - return -1; + if (use_perf_type_probe && (is_kprobe || is_kretprobe)) { + attr.type = PERF_TYPE_PROBE; + pd.func = ptr_to_u64(event + strlen(is_kprobe ? "kprobe/" + : "kretprobe/")); + pd.offset = 0; + attr.is_return = !!is_kretprobe; + attr.probe_desc = ptr_to_u64(&pd); + } else { + efd = open(buf, O_RDONLY, 0); + if (efd < 0) { + printf("failed to open event %s\n", event); + return -1; + } + err = read(efd, buf, sizeof(buf)); + if (err < 0 || err >= sizeof(buf)) { + printf("read from '%s' failed '%s'\n", event, + strerror(errno)); + return -1; + } + close(efd); + buf[err] = 0; + id = atoi(buf); + attr.config = id; } - close(efd); - - buf[err] = 0; - id = atoi(buf); - attr.config = id; - efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0); if (efd < 0) { - printf("event %d fd %d err %s\n", id, efd, strerror(errno)); + if (use_perf_type_probe && (is_kprobe || is_kretprobe)) + printf("k%sprobe %s fd %d err %s\n", + is_kprobe ? "" : "ret", + event + strlen(is_kprobe ? "kprobe/" + : "kretprobe/"), + efd, strerror(errno)); + else + printf("event %d fd %d err %s\n", id, efd, + strerror(errno)); return -1; } event_fd[prog_cnt - 1] = efd; diff --git a/samples/bpf/bpf_load.h b/samples/bpf/bpf_load.h index 7d57a42..e7a8a21 100644 --- a/samples/bpf/bpf_load.h +++ b/samples/bpf/bpf_load.h @@ -2,6 +2,7 @@ #ifndef __BPF_LOAD_H #define __BPF_LOAD_H +#include #include "libbpf.h" #define MAX_MAPS 32 @@ -38,6 +39,8 @@ extern int map_fd[MAX_MAPS]; extern struct bpf_map_data map_data[MAX_MAPS]; extern int map_data_count; +extern bool use_perf_type_probe; + /* parses elf file compiled by llvm .c->.o * . parses 'maps' section and creates maps via BPF syscall * . parses 'license' section and passes it to syscall @@ -59,6 +62,11 @@ struct ksym { char *name; }; +static inline __u64 ptr_to_u64(const void *ptr) +{ + return (__u64) (unsigned long) ptr; +} + int load_kallsyms(void); struct ksym *ksym_search(long key); int set_link_xdp_fd(int ifindex, int fd, __u32 flags);