From patchwork Sat Dec 24 07:15:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 13081282 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1ECAC10F1B for ; Sat, 24 Dec 2022 07:15:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229783AbiLXHPl (ORCPT ); Sat, 24 Dec 2022 02:15:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229541AbiLXHPi (ORCPT ); Sat, 24 Dec 2022 02:15:38 -0500 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29B8310041; Fri, 23 Dec 2022 23:15:37 -0800 (PST) Received: by mail-pj1-x1034.google.com with SMTP id c8-20020a17090a4d0800b00225c3614161so3431690pjg.5; Fri, 23 Dec 2022 23:15:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0NDUOj4bd5P9okQ+fNcx9acs9ETFFICaPPmEE79XHo4=; b=UIYisnLPYnPv8I6/so7/vvbDnAz6Od9PV+aENtxh7ChmssYGzedHmL6A9QtgDdh4aV mBC2RHHjEX3Wejfpsu4C9JJ+LUcBJihYtzr/jITodzyVfaosVmvE4wgVGH+mkUEj2dED CiXOtbEBjULJuOLxVU8cSLT9h2sM8frr0qgAH0EE23E/EBuEr4e8VbAMh9fZseCKGCQm OZW4E0e8/4U+X5q9HmTXpuiy6Tpe+p6ImsyxpsKTw2Xe2bOgN9bNCGxlovOzUk1T9nBj jiD/nfYYSxHnOb6fy2R5It9RVp/jumwgSNVr5CjzT5XMRM+gFp++ZDnrxQA7MCc47/DS mDWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0NDUOj4bd5P9okQ+fNcx9acs9ETFFICaPPmEE79XHo4=; b=FkhVmnLjdu5eI7dmCAu6F1Fb6azOfbEE5ODptrziSbJe5lxauFtloG0MgxxX+2Uw/D /DKK8aZBqSAZrJ2S1ovUAuQNKZNzRJbJ7fNq49MEKQmJHKOUk6IjiPVmapvehKLCRagZ W1m5LZn2t+7Mr6nE6xtCHyq+2l/SZSsZwU9eygXyauNrhNC9eGAXc6BNKFWhZ+AJu06v uAm34KgeeFwB+o3EjOHaykjyCDq95Thl5uG3LPTzBxgpNgYuLeuipO5FzJcbQby+Ih2e iQgjR6vCorMMJ/dhezK766rCEk29kRA0C1WvoTmcpjoz4SvxP1KQqgitPVEijakkwQn/ aIxA== X-Gm-Message-State: AFqh2kqCLwrwnKw0mVHx7hM3R6bIt3vSYnQ71uTgQZdOdxro+AWvF0ii KRYKQ7DO7FympAVSHluNAg== X-Google-Smtp-Source: AMrXdXsVN2ejtTMCxmmadavpGdAkP1wPuZAXsIyH9s90AUtTrVi6ndzSTNpgxE8ZXRXr653ZEGGWlw== X-Received: by 2002:a17:902:d683:b0:189:f1de:2d4e with SMTP id v3-20020a170902d68300b00189f1de2d4emr13886939ply.45.1671866136576; Fri, 23 Dec 2022 23:15:36 -0800 (PST) Received: from WDIR.. ([182.209.58.25]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b00186b7443082sm3433222plb.195.2022.12.23.23.15.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Dec 2022 23:15:36 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Yonghong Song , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [bpf-next v3 1/6] samples/bpf: use kyscall instead of kprobe in syscall tracing program Date: Sat, 24 Dec 2022 16:15:22 +0900 Message-Id: <20221224071527.2292-2-danieltimlee@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224071527.2292-1-danieltimlee@gmail.com> References: <20221224071527.2292-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Syscall tracing using kprobe is quite unstable. Since it uses the exact name of the kernel function, the program might broke due to the rename of a function. The problem can also be caused by a changes in the arguments of the function to which the kprobe connects. In this commit, ksyscall is used instead of kprobe. By using ksyscall, libbpf will detect the appropriate kernel function name. (e.g. sys_write -> __s390_sys_write). This eliminates the need to worry about which wrapper function to attach in order to parse arguments. In addition, ksyscall provides more fine method with attaching system call, the coarse SYSCALL helper at trace_common.h can be removed. Signed-off-by: Daniel T. Lee --- samples/bpf/map_perf_test_kern.c | 17 ++++++++--------- .../bpf/test_current_task_under_cgroup_kern.c | 3 +-- samples/bpf/test_map_in_map_kern.c | 1 - samples/bpf/test_probe_write_user_kern.c | 3 +-- samples/bpf/trace_common.h | 13 ------------- samples/bpf/trace_output_kern.c | 3 +-- samples/bpf/tracex2_kern.c | 3 +-- 7 files changed, 12 insertions(+), 31 deletions(-) delete mode 100644 samples/bpf/trace_common.h diff --git a/samples/bpf/map_perf_test_kern.c b/samples/bpf/map_perf_test_kern.c index 7342c5b2f278..874e2f7e3d5d 100644 --- a/samples/bpf/map_perf_test_kern.c +++ b/samples/bpf/map_perf_test_kern.c @@ -11,7 +11,6 @@ #include #include #include -#include "trace_common.h" #define MAX_ENTRIES 1000 #define MAX_NR_CPUS 1024 @@ -102,7 +101,7 @@ struct { __uint(max_entries, MAX_ENTRIES); } lru_hash_lookup_map SEC(".maps"); -SEC("kprobe/" SYSCALL(sys_getuid)) +SEC("ksyscall/getuid") int stress_hmap(struct pt_regs *ctx) { u32 key = bpf_get_current_pid_tgid(); @@ -120,7 +119,7 @@ int stress_hmap(struct pt_regs *ctx) return 0; } -SEC("kprobe/" SYSCALL(sys_geteuid)) +SEC("ksyscall/geteuid") int stress_percpu_hmap(struct pt_regs *ctx) { u32 key = bpf_get_current_pid_tgid(); @@ -137,7 +136,7 @@ int stress_percpu_hmap(struct pt_regs *ctx) return 0; } -SEC("kprobe/" SYSCALL(sys_getgid)) +SEC("ksyscall/getgid") int stress_hmap_alloc(struct pt_regs *ctx) { u32 key = bpf_get_current_pid_tgid(); @@ -154,7 +153,7 @@ int stress_hmap_alloc(struct pt_regs *ctx) return 0; } -SEC("kprobe/" SYSCALL(sys_getegid)) +SEC("ksyscall/getegid") int stress_percpu_hmap_alloc(struct pt_regs *ctx) { u32 key = bpf_get_current_pid_tgid(); @@ -171,7 +170,7 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx) return 0; } -SEC("kprobe/" SYSCALL(sys_connect)) +SEC("ksyscall/connect") int stress_lru_hmap_alloc(struct pt_regs *ctx) { struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx); @@ -251,7 +250,7 @@ int stress_lru_hmap_alloc(struct pt_regs *ctx) return 0; } -SEC("kprobe/" SYSCALL(sys_gettid)) +SEC("ksyscall/gettid") int stress_lpm_trie_map_alloc(struct pt_regs *ctx) { union { @@ -273,7 +272,7 @@ int stress_lpm_trie_map_alloc(struct pt_regs *ctx) return 0; } -SEC("kprobe/" SYSCALL(sys_getpgid)) +SEC("ksyscall/getpgid") int stress_hash_map_lookup(struct pt_regs *ctx) { u32 key = 1, i; @@ -286,7 +285,7 @@ int stress_hash_map_lookup(struct pt_regs *ctx) return 0; } -SEC("kprobe/" SYSCALL(sys_getppid)) +SEC("ksyscall/getppid") int stress_array_map_lookup(struct pt_regs *ctx) { u32 key = 1, i; diff --git a/samples/bpf/test_current_task_under_cgroup_kern.c b/samples/bpf/test_current_task_under_cgroup_kern.c index fbd43e2bb4d3..541fc861b984 100644 --- a/samples/bpf/test_current_task_under_cgroup_kern.c +++ b/samples/bpf/test_current_task_under_cgroup_kern.c @@ -10,7 +10,6 @@ #include #include #include -#include "trace_common.h" struct { __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY); @@ -27,7 +26,7 @@ struct { } perf_map SEC(".maps"); /* Writes the last PID that called sync to a map at index 0 */ -SEC("kprobe/" SYSCALL(sys_sync)) +SEC("ksyscall/sync") int bpf_prog1(struct pt_regs *ctx) { u64 pid = bpf_get_current_pid_tgid(); diff --git a/samples/bpf/test_map_in_map_kern.c b/samples/bpf/test_map_in_map_kern.c index b0200c8eac09..0e17f9ade5c5 100644 --- a/samples/bpf/test_map_in_map_kern.c +++ b/samples/bpf/test_map_in_map_kern.c @@ -13,7 +13,6 @@ #include #include #include -#include "trace_common.h" #define MAX_NR_PORTS 65536 diff --git a/samples/bpf/test_probe_write_user_kern.c b/samples/bpf/test_probe_write_user_kern.c index 220a96438d75..d60cabaaf753 100644 --- a/samples/bpf/test_probe_write_user_kern.c +++ b/samples/bpf/test_probe_write_user_kern.c @@ -11,7 +11,6 @@ #include #include #include -#include "trace_common.h" struct { __uint(type, BPF_MAP_TYPE_HASH); @@ -28,7 +27,7 @@ struct { * This example sits on a syscall, and the syscall ABI is relatively stable * of course, across platforms, and over time, the ABI may change. */ -SEC("kprobe/" SYSCALL(sys_connect)) +SEC("ksyscall/connect") int bpf_prog1(struct pt_regs *ctx) { struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx); diff --git a/samples/bpf/trace_common.h b/samples/bpf/trace_common.h deleted file mode 100644 index 8cb5400aed1f..000000000000 --- a/samples/bpf/trace_common.h +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#ifndef __TRACE_COMMON_H -#define __TRACE_COMMON_H - -#ifdef __x86_64__ -#define SYSCALL(SYS) "__x64_" __stringify(SYS) -#elif defined(__s390x__) -#define SYSCALL(SYS) "__s390x_" __stringify(SYS) -#else -#define SYSCALL(SYS) __stringify(SYS) -#endif - -#endif diff --git a/samples/bpf/trace_output_kern.c b/samples/bpf/trace_output_kern.c index b64815af0943..a481abf8c4c5 100644 --- a/samples/bpf/trace_output_kern.c +++ b/samples/bpf/trace_output_kern.c @@ -2,7 +2,6 @@ #include #include #include -#include "trace_common.h" struct { __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); @@ -11,7 +10,7 @@ struct { __uint(max_entries, 2); } my_map SEC(".maps"); -SEC("kprobe/" SYSCALL(sys_write)) +SEC("ksyscall/write") int bpf_prog1(struct pt_regs *ctx) { struct S { diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c index 5bc696bac27d..ecdca9620ece 100644 --- a/samples/bpf/tracex2_kern.c +++ b/samples/bpf/tracex2_kern.c @@ -10,7 +10,6 @@ #include #include #include -#include "trace_common.h" struct { __uint(type, BPF_MAP_TYPE_HASH); @@ -78,7 +77,7 @@ struct { __uint(max_entries, 1024); } my_hist_map SEC(".maps"); -SEC("kprobe/" SYSCALL(sys_write)) +SEC("ksyscall/write") int bpf_prog3(struct pt_regs *ctx) { long write_size = PT_REGS_PARM3(ctx); From patchwork Sat Dec 24 07:15:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 13081283 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60298C4332F for ; Sat, 24 Dec 2022 07:15:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229826AbiLXHPn (ORCPT ); Sat, 24 Dec 2022 02:15:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229693AbiLXHPl (ORCPT ); Sat, 24 Dec 2022 02:15:41 -0500 Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9AD8B10048; Fri, 23 Dec 2022 23:15:40 -0800 (PST) Received: by mail-pj1-x1036.google.com with SMTP id u15-20020a17090a3fcf00b002191825cf02so6761420pjm.2; Fri, 23 Dec 2022 23:15:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=XwZeuqk4CHrbQ3Wjbdmra5o77wm0nPLeqHrABP97XAY=; b=bt2AQnE8vjO0hClbz4duSXyIhTempWvd5MoHivKJwx8EyE50RWpEnVFUmce4FynvyM hp5hAqNZc8zHkmFjrANfgpr/XyoK2KL7dFDLcUhUpmXfdFaAjN28zZfaBa5wdX9XwUju kMjxw7/pcpd2+aCMoV6EXq5wI/ldGhUSwdpAXxsqWivywq3DzfPKhw+BU72tNllhkCnA DQJmhdni51cYy2596S0/Sa82Ocap954XzTLywNHJlMH+oHCcbG/H747+5xRvwBGpr/2A RD8zRdJn6Gp1ZqIQ0NsKd6mwBYvAT49sqDkreB8ld5eYDL2YjJyd7qT9oEzf+216mGQv 0G/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XwZeuqk4CHrbQ3Wjbdmra5o77wm0nPLeqHrABP97XAY=; b=ytdQg4y1dndgcpwRfGgTJqhoYx2V062o1pcTNPAyEJgqXQdtzZ/pp4so5bxJZPSbmK 8H7QYcSEVTbv9dKPMUdDcHeyaChl54pSIZzs4s0LsJ3MSftKE686WSkyyDKAWpW6d9tw 5roUtrGV5VFnIFA1PFyi1+kCs0StJCgMHlt/Q6ha04m+WkT62NU4bYy1KMC0FdRUlH0c DV9+RnfU00cATZHG2O3QPZN1Pqed8+y201r8G9e399l78SxJu1BXlWW1CnaUwkwlMjUs knrDDmQyOH64gahSbgKyvIHjX6Eansqd3QFQ0EPE6JPre0bd5IOnu1eo9kxuwQ9Hll1o xcRw== X-Gm-Message-State: AFqh2kpu1bK2N78+k3WywT9B6+C/i2KE3DkMFB0nnW4nBOwOxRyIBct9 uQ3qENPaqMD6QmtLlkiSJg== X-Google-Smtp-Source: AMrXdXuxRzTJUdPrJ/5rvozuGJf6xtUKw9ciVHgqumK5TGmJhZVWml9DoZBOo8QAC9Ed6c2XjaW3iA== X-Received: by 2002:a17:903:1305:b0:189:d025:aa23 with SMTP id iy5-20020a170903130500b00189d025aa23mr12660954plb.53.1671866140064; Fri, 23 Dec 2022 23:15:40 -0800 (PST) Received: from WDIR.. ([182.209.58.25]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b00186b7443082sm3433222plb.195.2022.12.23.23.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Dec 2022 23:15:39 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Yonghong Song , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [bpf-next v3 2/6] samples/bpf: use vmlinux.h instead of implicit headers in syscall tracing program Date: Sat, 24 Dec 2022 16:15:23 +0900 Message-Id: <20221224071527.2292-3-danieltimlee@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224071527.2292-1-danieltimlee@gmail.com> References: <20221224071527.2292-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This commit applies vmlinux.h to syscall tracing program. This change allows the bpf program to refer to the internal structure as a single "vmlinux.h" instead of including each header referenced by the bpf program. Signed-off-by: Daniel T. Lee --- samples/bpf/map_perf_test_kern.c | 5 ++--- samples/bpf/test_current_task_under_cgroup_kern.c | 4 +--- samples/bpf/test_probe_write_user_kern.c | 5 ++--- samples/bpf/trace_output_kern.c | 3 +-- samples/bpf/tracex2_kern.c | 4 +--- 5 files changed, 7 insertions(+), 14 deletions(-) diff --git a/samples/bpf/map_perf_test_kern.c b/samples/bpf/map_perf_test_kern.c index 874e2f7e3d5d..0c7885057ffe 100644 --- a/samples/bpf/map_perf_test_kern.c +++ b/samples/bpf/map_perf_test_kern.c @@ -4,10 +4,9 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ -#include -#include +#include "vmlinux.h" +#include #include -#include #include #include #include diff --git a/samples/bpf/test_current_task_under_cgroup_kern.c b/samples/bpf/test_current_task_under_cgroup_kern.c index 541fc861b984..0b059cee3cba 100644 --- a/samples/bpf/test_current_task_under_cgroup_kern.c +++ b/samples/bpf/test_current_task_under_cgroup_kern.c @@ -5,11 +5,9 @@ * License as published by the Free Software Foundation. */ -#include -#include +#include "vmlinux.h" #include #include -#include struct { __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY); diff --git a/samples/bpf/test_probe_write_user_kern.c b/samples/bpf/test_probe_write_user_kern.c index d60cabaaf753..a0f10c5ca273 100644 --- a/samples/bpf/test_probe_write_user_kern.c +++ b/samples/bpf/test_probe_write_user_kern.c @@ -4,9 +4,8 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ -#include -#include -#include +#include "vmlinux.h" +#include #include #include #include diff --git a/samples/bpf/trace_output_kern.c b/samples/bpf/trace_output_kern.c index a481abf8c4c5..565a73b51b04 100644 --- a/samples/bpf/trace_output_kern.c +++ b/samples/bpf/trace_output_kern.c @@ -1,6 +1,5 @@ -#include +#include "vmlinux.h" #include -#include #include struct { diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2_kern.c index ecdca9620ece..4b9d956a3e2c 100644 --- a/samples/bpf/tracex2_kern.c +++ b/samples/bpf/tracex2_kern.c @@ -4,10 +4,8 @@ * modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ -#include -#include +#include "vmlinux.h" #include -#include #include #include From patchwork Sat Dec 24 07:15:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 13081284 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8666CC10F1B for ; Sat, 24 Dec 2022 07:15:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230166AbiLXHPx (ORCPT ); Sat, 24 Dec 2022 02:15:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229693AbiLXHPp (ORCPT ); Sat, 24 Dec 2022 02:15:45 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C7BFF32; Fri, 23 Dec 2022 23:15:44 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id p4so6741594pjk.2; Fri, 23 Dec 2022 23:15:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c4bC2r8TQM9zsvaRFp/0j9Slk8lcxESg+J4rrAXLwNg=; b=N5qzaKC63wpzqjmI3f3vSJ4+aw46UwbN1tq8MW2KvFYAgCloL1XRYZ90/QCVq6dLjv WgjzL/1U5OllJc2z20IuIPF1f6I3v1mbn34BjrgEqAap8dn0WLbZPA8o+frEYmOqR9vO 3xSewsdVwK73yyqncprVVBItlnrqUonXevPDTr0JsseP6IsGEaSNm0H44Y0VI6/YMgW5 dW9hKhzS8JsB6iB2ybXKDjG1HIudgFHrrwpeGDZ8xe681wKFHnqO5Etwh2gTMqtj1tqi VgbKRVCbvwkBPSAQBKSsItBJQQmdhs/NyrZTvgQoA4Gy9F3PJHUUirXj/eW0tG3DjmKD wSZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c4bC2r8TQM9zsvaRFp/0j9Slk8lcxESg+J4rrAXLwNg=; b=wPqsDNLXnkW2NsbJnfHmYwER5bS1yaWcuOyg6svALO+jhj6b4S/idShqAzuyB7SOpp 5SXUghuVvQkaVt8/Y/JRsbmFPPvzoezIFjnhSwxgGg04yKeuOZXsjqF14FCwT0NAT6fR pC2pmGpF2EGd7VAj57+i4YV6PwGjSEAm9ip+TM4j0B7DVPZvax2nbBmarb++3/m4OU9b 5hH1W8st80//nf9YI8LBgL1JX+txOuAyYeH+IoVDcxZLH2m2fkmbKTSoXkkeU6Cd8X34 u9oFruVc07umLRq1zRoIlpjt3YGGRBnDzoodcAL94wf0ksfmyMSrFmc1OIbcJ58SInQc kPcw== X-Gm-Message-State: AFqh2krQt5eCP0Lq6cnz1bCK+xDMYR+S170ESDLpypKpbiJ6bLgHRFG/ 96/+hF/Bdnb5IyOPD3PBuqHx+Z6URllnxbyTLQ== X-Google-Smtp-Source: AMrXdXs+Z36jKAEJzlI+v/GQ3x8DHzl+03CnUex4/g6+y3h9d5sbtVF6juABQVjOocBN3NDyn00xVQ== X-Received: by 2002:a17:902:eecc:b0:189:184d:f0ff with SMTP id h12-20020a170902eecc00b00189184df0ffmr13396912plb.11.1671866143572; Fri, 23 Dec 2022 23:15:43 -0800 (PST) Received: from WDIR.. ([182.209.58.25]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b00186b7443082sm3433222plb.195.2022.12.23.23.15.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Dec 2022 23:15:43 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Yonghong Song , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [bpf-next v3 3/6] samples/bpf: change _kern suffix to .bpf with syscall tracing program Date: Sat, 24 Dec 2022 16:15:24 +0900 Message-Id: <20221224071527.2292-4-danieltimlee@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224071527.2292-1-danieltimlee@gmail.com> References: <20221224071527.2292-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Currently old compile rule (CLANG-bpf) doesn't contains VMLINUX_H define flag which is essential for the bpf program that includes "vmlinux.h". Also old compile rule doesn't directly specify the compile target as bpf, instead it uses bunch of extra options with clang followed by long chain of commands. (e.g. clang | opt | llvm-dis | llc) In Makefile, there is already new compile rule which is more simple and neat. And it also has -D__VMLINUX_H__ option. By just changing the _kern suffix to .bpf will inherit the benefit of the new CLANG-BPF compile target. Also, this commit adds dummy gnu/stub.h to the samples/bpf directory. As commit 1c2dd16add7e ("selftests/bpf: get rid of -D__x86_64__") noted, compiling with 'clang -target bpf' will raise an error with stubs.h unless workaround (-D__x86_64) is used. This commit solves this problem by adding dummy stub.h to make /usr/include/features.h to follow the expected path as the same way selftests/bpf dealt with. Signed-off-by: Daniel T. Lee --- samples/bpf/Makefile | 10 +++++----- samples/bpf/gnu/stubs.h | 1 + .../bpf/{map_perf_test_kern.c => map_perf_test.bpf.c} | 0 samples/bpf/map_perf_test_user.c | 2 +- ...oup_kern.c => test_current_task_under_cgroup.bpf.c} | 0 samples/bpf/test_current_task_under_cgroup_user.c | 2 +- ...e_write_user_kern.c => test_probe_write_user.bpf.c} | 0 samples/bpf/test_probe_write_user_user.c | 2 +- .../bpf/{trace_output_kern.c => trace_output.bpf.c} | 0 samples/bpf/trace_output_user.c | 2 +- samples/bpf/{tracex2_kern.c => tracex2.bpf.c} | 0 samples/bpf/tracex2_user.c | 2 +- 12 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 samples/bpf/gnu/stubs.h rename samples/bpf/{map_perf_test_kern.c => map_perf_test.bpf.c} (100%) rename samples/bpf/{test_current_task_under_cgroup_kern.c => test_current_task_under_cgroup.bpf.c} (100%) rename samples/bpf/{test_probe_write_user_kern.c => test_probe_write_user.bpf.c} (100%) rename samples/bpf/{trace_output_kern.c => trace_output.bpf.c} (100%) rename samples/bpf/{tracex2_kern.c => tracex2.bpf.c} (100%) diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile index 727da3c5879b..22039a0a5b35 100644 --- a/samples/bpf/Makefile +++ b/samples/bpf/Makefile @@ -125,21 +125,21 @@ always-y += sockex1_kern.o always-y += sockex2_kern.o always-y += sockex3_kern.o always-y += tracex1_kern.o -always-y += tracex2_kern.o +always-y += tracex2.bpf.o always-y += tracex3_kern.o always-y += tracex4_kern.o always-y += tracex5_kern.o always-y += tracex6_kern.o always-y += tracex7_kern.o always-y += sock_flags_kern.o -always-y += test_probe_write_user_kern.o -always-y += trace_output_kern.o +always-y += test_probe_write_user.bpf.o +always-y += trace_output.bpf.o always-y += tcbpf1_kern.o always-y += tc_l2_redirect_kern.o always-y += lathist_kern.o always-y += offwaketime_kern.o always-y += spintest_kern.o -always-y += map_perf_test_kern.o +always-y += map_perf_test.bpf.o always-y += test_overhead_tp_kern.o always-y += test_overhead_raw_tp_kern.o always-y += test_overhead_kprobe_kern.o @@ -147,7 +147,7 @@ always-y += parse_varlen.o parse_simple.o parse_ldabs.o always-y += test_cgrp2_tc_kern.o always-y += xdp1_kern.o always-y += xdp2_kern.o -always-y += test_current_task_under_cgroup_kern.o +always-y += test_current_task_under_cgroup.bpf.o always-y += trace_event_kern.o always-y += sampleip_kern.o always-y += lwt_len_hist_kern.o diff --git a/samples/bpf/gnu/stubs.h b/samples/bpf/gnu/stubs.h new file mode 100644 index 000000000000..719225b16626 --- /dev/null +++ b/samples/bpf/gnu/stubs.h @@ -0,0 +1 @@ +/* dummy .h to trick /usr/include/features.h to work with 'clang -target bpf' */ diff --git a/samples/bpf/map_perf_test_kern.c b/samples/bpf/map_perf_test.bpf.c similarity index 100% rename from samples/bpf/map_perf_test_kern.c rename to samples/bpf/map_perf_test.bpf.c diff --git a/samples/bpf/map_perf_test_user.c b/samples/bpf/map_perf_test_user.c index 1bb53f4b29e1..d2fbcf963cdf 100644 --- a/samples/bpf/map_perf_test_user.c +++ b/samples/bpf/map_perf_test_user.c @@ -443,7 +443,7 @@ int main(int argc, char **argv) if (argc > 4) max_cnt = atoi(argv[4]); - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]); obj = bpf_object__open_file(filename, NULL); if (libbpf_get_error(obj)) { fprintf(stderr, "ERROR: opening BPF object file failed\n"); diff --git a/samples/bpf/test_current_task_under_cgroup_kern.c b/samples/bpf/test_current_task_under_cgroup.bpf.c similarity index 100% rename from samples/bpf/test_current_task_under_cgroup_kern.c rename to samples/bpf/test_current_task_under_cgroup.bpf.c diff --git a/samples/bpf/test_current_task_under_cgroup_user.c b/samples/bpf/test_current_task_under_cgroup_user.c index ac251a417f45..a1aaea59caee 100644 --- a/samples/bpf/test_current_task_under_cgroup_user.c +++ b/samples/bpf/test_current_task_under_cgroup_user.c @@ -21,7 +21,7 @@ int main(int argc, char **argv) char filename[256]; int map_fd[2]; - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]); obj = bpf_object__open_file(filename, NULL); if (libbpf_get_error(obj)) { fprintf(stderr, "ERROR: opening BPF object file failed\n"); diff --git a/samples/bpf/test_probe_write_user_kern.c b/samples/bpf/test_probe_write_user.bpf.c similarity index 100% rename from samples/bpf/test_probe_write_user_kern.c rename to samples/bpf/test_probe_write_user.bpf.c diff --git a/samples/bpf/test_probe_write_user_user.c b/samples/bpf/test_probe_write_user_user.c index 00ccfb834e45..2a539aec4116 100644 --- a/samples/bpf/test_probe_write_user_user.c +++ b/samples/bpf/test_probe_write_user_user.c @@ -24,7 +24,7 @@ int main(int ac, char **argv) mapped_addr_in = (struct sockaddr_in *)&mapped_addr; tmp_addr_in = (struct sockaddr_in *)&tmp_addr; - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]); obj = bpf_object__open_file(filename, NULL); if (libbpf_get_error(obj)) { fprintf(stderr, "ERROR: opening BPF object file failed\n"); diff --git a/samples/bpf/trace_output_kern.c b/samples/bpf/trace_output.bpf.c similarity index 100% rename from samples/bpf/trace_output_kern.c rename to samples/bpf/trace_output.bpf.c diff --git a/samples/bpf/trace_output_user.c b/samples/bpf/trace_output_user.c index 371732f9cf8e..d316fd2c8e24 100644 --- a/samples/bpf/trace_output_user.c +++ b/samples/bpf/trace_output_user.c @@ -51,7 +51,7 @@ int main(int argc, char **argv) char filename[256]; FILE *f; - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]); obj = bpf_object__open_file(filename, NULL); if (libbpf_get_error(obj)) { fprintf(stderr, "ERROR: opening BPF object file failed\n"); diff --git a/samples/bpf/tracex2_kern.c b/samples/bpf/tracex2.bpf.c similarity index 100% rename from samples/bpf/tracex2_kern.c rename to samples/bpf/tracex2.bpf.c diff --git a/samples/bpf/tracex2_user.c b/samples/bpf/tracex2_user.c index dd6205c6b6a7..2ef5047e802b 100644 --- a/samples/bpf/tracex2_user.c +++ b/samples/bpf/tracex2_user.c @@ -123,7 +123,7 @@ int main(int ac, char **argv) int i, j = 0; FILE *f; - snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); + snprintf(filename, sizeof(filename), "%s.bpf.o", argv[0]); obj = bpf_object__open_file(filename, NULL); if (libbpf_get_error(obj)) { fprintf(stderr, "ERROR: opening BPF object file failed\n"); From patchwork Sat Dec 24 07:15:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 13081285 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82EF4C4332F for ; Sat, 24 Dec 2022 07:16:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229871AbiLXHP6 (ORCPT ); Sat, 24 Dec 2022 02:15:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230147AbiLXHPx (ORCPT ); Sat, 24 Dec 2022 02:15:53 -0500 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81ED2644B; Fri, 23 Dec 2022 23:15:47 -0800 (PST) Received: by mail-pj1-x1030.google.com with SMTP id o1-20020a17090a678100b00219cf69e5f0so10584447pjj.2; Fri, 23 Dec 2022 23:15:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=etHLeTNew4fJlRfaoMniwFyAw1Vgdj/SeT5idN6XmJY=; b=czV1PcLaQPnbrXTJ8aib7fpbN+o8rkS4vP+OxTXwTOGtifOwWt3BMvgi/5LWpwS8Tl SfEhqjNp7PXj8RirMBRj9XhjafcDnXJj3DQqu0eQWTlR+YRs0wkH59R8KfNQyWOzQeHO t1q9+k4wtzNJxgF6My/4mY7hjL7zdJ7giZZd+k8N9vUiydf2hQOsBTkPZOionyNP+hIP CKgP2bdhmxvJHW+wu9j+htgXFeT8p6BWmiBHfI4v38NcCYRq29VSRKRUG0qTHqezF4nW FPvpWqmETvOmhlDsXStLQAsx0l8vNnUpl/r0lwso/cEJz0j7QlhFuKPTSTeBeJ8D7fmy wOgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=etHLeTNew4fJlRfaoMniwFyAw1Vgdj/SeT5idN6XmJY=; b=c/wO3I9W/DaPBODIFCKJVw3bLdWKCMLbRxRCStZv23dTxwloSYLNpTXrHt/PnlZrS4 Rmsm2mn0v0BGW9s0fg0HvG6Mdw7fyQvx8XgeUML3vXu3f+Ccpzca+01CY2eS9AkSDDZA U4x7Tew1RQKnweuPgce1UQeSZOgyN+rzmNRmLMJIryHwR/V+3agOTZwObh8kRpiDLiaQ datoFQ+c7zo9n2UVm4luv1c4O7d4Qd23xX4A2oYRZQd/MlAUiQqKyKhv1xb7UVeRLm+P voDOzrZtzUkdIFUtNgVnLOiAZuCfk19CpYO/qHyVx8P7+XQjYMMB8QShcWwqvCgqvr/p V6Dw== X-Gm-Message-State: AFqh2kr/UPtp9G2YNRqlDIqlpFPh8VnZ98AU+HUPGqz77eZ36by8g4hk V++24dVKI+6TUzjZ+FhZNw== X-Google-Smtp-Source: AMrXdXvifhZa2VwYa2kLpQ5xq5fwOctvLGkjgqpeQ77KnAzPBwdSDlEwIASdV6e4e3x7No+3s40t4A== X-Received: by 2002:a17:902:eb11:b0:189:f69d:d5cc with SMTP id l17-20020a170902eb1100b00189f69dd5ccmr12230275plb.58.1671866147030; Fri, 23 Dec 2022 23:15:47 -0800 (PST) Received: from WDIR.. ([182.209.58.25]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b00186b7443082sm3433222plb.195.2022.12.23.23.15.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Dec 2022 23:15:46 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Yonghong Song , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [bpf-next v3 4/6] samples/bpf: fix tracex2 by using BPF_KSYSCALL macro Date: Sat, 24 Dec 2022 16:15:25 +0900 Message-Id: <20221224071527.2292-5-danieltimlee@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224071527.2292-1-danieltimlee@gmail.com> References: <20221224071527.2292-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Currently, there is a problem with tracex2, as it doesn't print the histogram properly and the results are misleading. (all results report as 0) The problem is caused by a change in arguments of the function to which the kprobe connects. This tracex2 bpf program uses kprobe (attached to __x64_sys_write) to figure out the size of the write system call. In order to achieve this, the third argument 'count' must be intact. The following is a prototype of the sys_write variant. (checked with pfunct) ~/git/linux$ pfunct -P fs/read_write.o | grep sys_write ssize_t ksys_write(unsigned int fd, const char * buf, size_t count); long int __x64_sys_write(const struct pt_regs * regs); ... cross compile with s390x ... long int __s390_sys_write(struct pt_regs * regs); Since the nature of SYSCALL_WRAPPER function wraps the argument once, additional process of argument extraction is required to properly parse the argument. #define BPF_KSYSCALL(name, args...) ... snip ... struct pt_regs *regs = LINUX_HAS_SYSCALL_WRAPPER \ ? (struct pt_regs *)PT_REGS_PARM1(ctx) \ : ctx; \ In order to fix this problem, the BPF_SYSCALL macro has been used. This reduces the hassle of parsing arguments from pt_regs. Since the macro uses the CORE version of argument extraction, additional portability comes too. Signed-off-by: Daniel T. Lee --- samples/bpf/tracex2.bpf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/bpf/tracex2.bpf.c b/samples/bpf/tracex2.bpf.c index 4b9d956a3e2c..1e1a75850307 100644 --- a/samples/bpf/tracex2.bpf.c +++ b/samples/bpf/tracex2.bpf.c @@ -8,6 +8,7 @@ #include #include #include +#include struct { __uint(type, BPF_MAP_TYPE_HASH); @@ -76,14 +77,13 @@ struct { } my_hist_map SEC(".maps"); SEC("ksyscall/write") -int bpf_prog3(struct pt_regs *ctx) +int BPF_KSYSCALL(bpf_prog3, unsigned int fd, const char *buf, size_t count) { - long write_size = PT_REGS_PARM3(ctx); long init_val = 1; long *value; struct hist_key key; - key.index = log2l(write_size); + key.index = log2l(count); key.pid_tgid = bpf_get_current_pid_tgid(); key.uid_gid = bpf_get_current_uid_gid(); bpf_get_current_comm(&key.comm, sizeof(key.comm)); From patchwork Sat Dec 24 07:15:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 13081286 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A4A80C4167B for ; Sat, 24 Dec 2022 07:16:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230233AbiLXHQI (ORCPT ); Sat, 24 Dec 2022 02:16:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230030AbiLXHPz (ORCPT ); Sat, 24 Dec 2022 02:15:55 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 339FB101F1; Fri, 23 Dec 2022 23:15:51 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id v23so6726357pju.3; Fri, 23 Dec 2022 23:15:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YZV/Ygg5ZLK/icKlwhdiYUcrDkEBcDRF0QNzCkC6zPE=; b=acQhmTiKluTSuwLe020qKjGdnPpLInEv+Y+NnwUtPy7ahfRmb34z9Q33OR4nE1EDwz qvu4f45Tts3lgY/UrnxZr8iloVpODzv/f0DSe5hIWkuo/XFHMpzhLGkPRkYPoCCPP0bI 2aUhyiUicc95vF7ZQ2s/ZTFCn9anOXkyoPQeTdfhPj3O+5JvPxM2UO45XIGWnqhpN02y p6tm/gqI9dziJWERA3YJLXChMctAS0c1srum6G2vygw+VZQzbxcWw6ADNIUDTp9qjJeb cMBv9fJjDAuHJnxclmnZZ72C9yryrrQwPKMnvkiAJL8TgNxtFRG1ZukcM8ECW/5Vcxdh bwow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YZV/Ygg5ZLK/icKlwhdiYUcrDkEBcDRF0QNzCkC6zPE=; b=WB/ob3g6C/VCxGzhPFHvMh3T8AyRtQXWBjVd6t6QTXF2/w7KOOEeWKGw8ofJfesbct MSjJC8VZxW8gXp8l6WgjGbasPV2n7AAOTUN5/V0lQl0MtVgKsRbFtOqyKkmsNzLAB3Wd MYvQ+ueLnl0KM7BmhdHoz5Ywq1z9Npt5JZNRz08BcJBJ68O/stjRzvgZ9iTKRNlrW5ye z9iQR9ecAP3VujXPx6nrjHTJKCXtYje5CzzYzDLRHI5gqZs2Td1M15vKXq/WbUvG1eYG RNjAPuYJwatSBHxMKnP6160LnAXgg5GYysqbxkQmt5ioVFmL1KQpHeUzGnQ44DqXXhU3 DF3Q== X-Gm-Message-State: AFqh2krHfsRC6dCKHBfrja+v9vfxSzdnpKyWS4UkSnuoLMsFZLps8VXh QpKAK3wtRhc9TgYyQr4KQw== X-Google-Smtp-Source: AMrXdXtiJeGUuWOg1ZKA6oWVklXZECdTzdwD4CAIfymlccyTGQEQmvZcq4dgdzIm8aI1iGJFz1+22Q== X-Received: by 2002:a17:902:a584:b0:18f:ac9f:29f6 with SMTP id az4-20020a170902a58400b0018fac9f29f6mr12393916plb.50.1671866150549; Fri, 23 Dec 2022 23:15:50 -0800 (PST) Received: from WDIR.. ([182.209.58.25]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b00186b7443082sm3433222plb.195.2022.12.23.23.15.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Dec 2022 23:15:50 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Yonghong Song , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [bpf-next v3 5/6] samples/bpf: use BPF_KSYSCALL macro in syscall tracing programs Date: Sat, 24 Dec 2022 16:15:26 +0900 Message-Id: <20221224071527.2292-6-danieltimlee@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224071527.2292-1-danieltimlee@gmail.com> References: <20221224071527.2292-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This commit enhances the syscall tracing programs by using the BPF_SYSCALL macro to reduce the inconvenience of parsing arguments from pt_regs. By simplifying argument extraction, bpf program will become clear to understand. Signed-off-by: Daniel T. Lee --- samples/bpf/map_perf_test.bpf.c | 26 ++++++++----------- .../bpf/test_current_task_under_cgroup.bpf.c | 4 ++- samples/bpf/test_probe_write_user.bpf.c | 12 ++++----- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/samples/bpf/map_perf_test.bpf.c b/samples/bpf/map_perf_test.bpf.c index 0c7885057ffe..3cdeba2afe12 100644 --- a/samples/bpf/map_perf_test.bpf.c +++ b/samples/bpf/map_perf_test.bpf.c @@ -101,7 +101,7 @@ struct { } lru_hash_lookup_map SEC(".maps"); SEC("ksyscall/getuid") -int stress_hmap(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_hmap) { u32 key = bpf_get_current_pid_tgid(); long init_val = 1; @@ -119,7 +119,7 @@ int stress_hmap(struct pt_regs *ctx) } SEC("ksyscall/geteuid") -int stress_percpu_hmap(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_percpu_hmap) { u32 key = bpf_get_current_pid_tgid(); long init_val = 1; @@ -136,7 +136,7 @@ int stress_percpu_hmap(struct pt_regs *ctx) } SEC("ksyscall/getgid") -int stress_hmap_alloc(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_hmap_alloc) { u32 key = bpf_get_current_pid_tgid(); long init_val = 1; @@ -153,7 +153,7 @@ int stress_hmap_alloc(struct pt_regs *ctx) } SEC("ksyscall/getegid") -int stress_percpu_hmap_alloc(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_percpu_hmap_alloc) { u32 key = bpf_get_current_pid_tgid(); long init_val = 1; @@ -168,11 +168,10 @@ int stress_percpu_hmap_alloc(struct pt_regs *ctx) } return 0; } - SEC("ksyscall/connect") -int stress_lru_hmap_alloc(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_lru_hmap_alloc, int fd, struct sockaddr_in *uservaddr, + int addrlen) { - struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx); char fmt[] = "Failed at stress_lru_hmap_alloc. ret:%dn"; union { u16 dst6[8]; @@ -185,14 +184,11 @@ int stress_lru_hmap_alloc(struct pt_regs *ctx) u32 key; }; } test_params; - struct sockaddr_in6 *in6; + struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)uservaddr; u16 test_case; - int addrlen, ret; long val = 1; u32 key = 0; - - in6 = (struct sockaddr_in6 *)PT_REGS_PARM2_CORE(real_regs); - addrlen = (int)PT_REGS_PARM3_CORE(real_regs); + int ret; if (addrlen != sizeof(*in6)) return 0; @@ -250,7 +246,7 @@ int stress_lru_hmap_alloc(struct pt_regs *ctx) } SEC("ksyscall/gettid") -int stress_lpm_trie_map_alloc(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_lpm_trie_map_alloc) { union { u32 b32[2]; @@ -272,7 +268,7 @@ int stress_lpm_trie_map_alloc(struct pt_regs *ctx) } SEC("ksyscall/getpgid") -int stress_hash_map_lookup(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_hash_map_lookup) { u32 key = 1, i; long *value; @@ -285,7 +281,7 @@ int stress_hash_map_lookup(struct pt_regs *ctx) } SEC("ksyscall/getppid") -int stress_array_map_lookup(struct pt_regs *ctx) +int BPF_KSYSCALL(stress_array_map_lookup) { u32 key = 1, i; long *value; diff --git a/samples/bpf/test_current_task_under_cgroup.bpf.c b/samples/bpf/test_current_task_under_cgroup.bpf.c index 0b059cee3cba..58b9cf7ed659 100644 --- a/samples/bpf/test_current_task_under_cgroup.bpf.c +++ b/samples/bpf/test_current_task_under_cgroup.bpf.c @@ -8,6 +8,8 @@ #include "vmlinux.h" #include #include +#include +#include struct { __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY); @@ -25,7 +27,7 @@ struct { /* Writes the last PID that called sync to a map at index 0 */ SEC("ksyscall/sync") -int bpf_prog1(struct pt_regs *ctx) +int BPF_KSYSCALL(bpf_prog1) { u64 pid = bpf_get_current_pid_tgid(); int idx = 0; diff --git a/samples/bpf/test_probe_write_user.bpf.c b/samples/bpf/test_probe_write_user.bpf.c index a0f10c5ca273..a4f3798b7fb0 100644 --- a/samples/bpf/test_probe_write_user.bpf.c +++ b/samples/bpf/test_probe_write_user.bpf.c @@ -27,24 +27,22 @@ struct { * of course, across platforms, and over time, the ABI may change. */ SEC("ksyscall/connect") -int bpf_prog1(struct pt_regs *ctx) +int BPF_KSYSCALL(bpf_prog1, int fd, struct sockaddr_in *uservaddr, + int addrlen) { - struct pt_regs *real_regs = (struct pt_regs *)PT_REGS_PARM1_CORE(ctx); - void *sockaddr_arg = (void *)PT_REGS_PARM2_CORE(real_regs); - int sockaddr_len = (int)PT_REGS_PARM3_CORE(real_regs); struct sockaddr_in new_addr, orig_addr = {}; struct sockaddr_in *mapped_addr; - if (sockaddr_len > sizeof(orig_addr)) + if (addrlen > sizeof(orig_addr)) return 0; - if (bpf_probe_read_user(&orig_addr, sizeof(orig_addr), sockaddr_arg) != 0) + if (bpf_probe_read_user(&orig_addr, sizeof(orig_addr), uservaddr) != 0) return 0; mapped_addr = bpf_map_lookup_elem(&dnat_map, &orig_addr); if (mapped_addr != NULL) { memcpy(&new_addr, mapped_addr, sizeof(new_addr)); - bpf_probe_write_user(sockaddr_arg, &new_addr, + bpf_probe_write_user(uservaddr, &new_addr, sizeof(new_addr)); } return 0; From patchwork Sat Dec 24 07:15:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Daniel T. Lee" X-Patchwork-Id: 13081287 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBD96C10F1B for ; Sat, 24 Dec 2022 07:16:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229693AbiLXHQO (ORCPT ); Sat, 24 Dec 2022 02:16:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230147AbiLXHQB (ORCPT ); Sat, 24 Dec 2022 02:16:01 -0500 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C6CF10B79; Fri, 23 Dec 2022 23:15:54 -0800 (PST) Received: by mail-pl1-x62f.google.com with SMTP id jn22so6702105plb.13; Fri, 23 Dec 2022 23:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=c6zLmWbFfcHm01hPAqRxC8QUpd9w9h8sf5XqbJ9uy68=; b=cVHpdTWAZNWgkc+4HJiZYB3du+jVU4uQf9PuYC5huIzuuvFKcIjvPhi8pOv6M6Y+QT +PzibG7KCj9xwaS6kIsqizNWUSAvX2wfv0xogQ5OigNAuklydZFwtOgiw+IUzQwVFd6Q RlJrfkiSOnxKkup+FE3BeTdIShXxy9nQTl1dOMmeeW3vayzaRvvIPxuhWOwll3WbQ22/ dP4rW07Cs8yT5Z4NZiu8qNWbu36kohey4qcDejWzYKmwhKfnRGW6YjKakzHOsdOSk40B FE/X5Ah8UajP0Sj66lyFR6Ov5sLnt1lh4SyyjYdX6QlGp0sa+KBu3lwW6uh2s/JuTK3e N2nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=c6zLmWbFfcHm01hPAqRxC8QUpd9w9h8sf5XqbJ9uy68=; b=n/AXhNndUwj9170RsR5pfqfgtT1XfaqCC/bzBMhLhx3h6vY5yjZzx4pyaQbXLTuMjm O3P23TnzgvYYAF1TaK2pKppfYMnsa+o12MjXudA+KhyxmY1V7mtuE4UQ0chmIthRvr1n RD5ldfxmrG9XlJCqGWWyWgrkQAy/nEhgMSwvPXJ4G+Ydhzcvxn4c8sRWeHS14TwXWp73 4tzFg2dNaJejGcBZofqQNcg2+OptkCT9xJbYeArp5yFIl2A/oGDImqVLM50lIkctuzIX fzShUls8lKpKRZlOWFAZmiqbxxuK8ypZVyvj/Hl8+hAv2RSp0Qzcaq0Asp2IphU6JqtR l97w== X-Gm-Message-State: AFqh2kohpHWFy0GLx7Jmh4dxkHmntU35y4pdopxKP7GT2RmUudZ14rJo NCMYE2+Gxjd0QXIrjtqD8g== X-Google-Smtp-Source: AMrXdXvYJf+z/GCGsV83lx/DXHjvlzPzNIkqiGIuUFvGxOWsVkR8TZE/uSew4HVg6edu2fGLetoXfg== X-Received: by 2002:a17:902:930b:b0:185:441e:2240 with SMTP id bc11-20020a170902930b00b00185441e2240mr12320112plb.59.1671866154104; Fri, 23 Dec 2022 23:15:54 -0800 (PST) Received: from WDIR.. ([182.209.58.25]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b00186b7443082sm3433222plb.195.2022.12.23.23.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Dec 2022 23:15:53 -0800 (PST) From: "Daniel T. Lee" To: Daniel Borkmann , Alexei Starovoitov , Andrii Nakryiko , Yonghong Song , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, netdev@vger.kernel.org Subject: [bpf-next v3 6/6] libbpf: fix invalid return address register in s390 Date: Sat, 24 Dec 2022 16:15:27 +0900 Message-Id: <20221224071527.2292-7-danieltimlee@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224071527.2292-1-danieltimlee@gmail.com> References: <20221224071527.2292-1-danieltimlee@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net There is currently an invalid register mapping in the s390 return address register. As the manual[1] states, the return address can be found at r14. In bpf_tracing.h, the s390 registers were named gprs(general purpose registers). This commit fixes the problem by correcting the mistyped mapping. [1]: https://uclibc.org/docs/psABI-s390x.pdf#page=14 Fixes: 3cc31d794097 ("libbpf: Normalize PT_REGS_xxx() macro definitions") Signed-off-by: Daniel T. Lee --- tools/lib/bpf/bpf_tracing.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/lib/bpf/bpf_tracing.h b/tools/lib/bpf/bpf_tracing.h index 2972dc25ff72..9c1b1689068d 100644 --- a/tools/lib/bpf/bpf_tracing.h +++ b/tools/lib/bpf/bpf_tracing.h @@ -137,7 +137,7 @@ struct pt_regs___s390 { #define __PT_PARM3_REG gprs[4] #define __PT_PARM4_REG gprs[5] #define __PT_PARM5_REG gprs[6] -#define __PT_RET_REG grps[14] +#define __PT_RET_REG gprs[14] #define __PT_FP_REG gprs[11] /* Works only with CONFIG_FRAME_POINTER */ #define __PT_RC_REG gprs[2] #define __PT_SP_REG gprs[15]