From patchwork Mon May 15 13:08:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13241393 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA9E7C8EC for ; Mon, 15 May 2023 13:09:23 +0000 (UTC) Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF11A2D61 for ; Mon, 15 May 2023 06:09:02 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1aaf70676b6so90655935ad.3 for ; Mon, 15 May 2023 06:09:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684156140; x=1686748140; 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=n7lJXeGdmmXQ6fZ/+VqhCkbCDOtoN1rzSWwemISQpoA=; b=UTGf3mAVK3minW+Q9Cy02OrkGezP8IBv9H2NupFnarj9t8QZYDJeYB1xeF6MprzKzm 9Bass1p4KzxWBirWxeCCpBKSVQwoUR+c1u1jEGK0yoKM26jBRA/LA3bkIS1E+kt2v2Y5 el7ZUd9uEMD+Lvj7/l8IVxHCR6/9eoW2Xl6eX4Ueq7h97i3yAJTOscxob7La2gOxjk9d 60YotAjRbdTfHS7myoodTxFcCBQLI1JkU8CqI5VKyTydKt40IArvIH5T6LJuqnswHFZw 7vZFNB7MWtCjsoljAxpjoNfZxJKv1Pf9dNdVy8w3kwaAJWImqVIfNEx9LC7x/v9+YoEF 4MNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684156140; x=1686748140; 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=n7lJXeGdmmXQ6fZ/+VqhCkbCDOtoN1rzSWwemISQpoA=; b=L2hnHycq9M+aAf6pCu70aPUzBaIMu4NY3Dp09qkuRDCBZBvRvP4U/T6AtvuMrjjz5C DIQtEc0+aQs0ZK20dOWxZZZ/dighv61ePl9XSPUSNz3bN68WucMfMaKcm2bttmyPt9hB uQRQvVX270jh+L30JadhvnAM2RJ048/41JHh+itf7iYduYHsKHEvT32/8W+CotgSkaj/ j0drcZSJxp4GctFNlNFgkCmMZhkx1pMHbM9niJf5b9PfnTRtgyvkT3gJzm5Kn6yrGBam GOIRfAKz5W16p+e/95Cyb6gHHBl46EYVh7Pofiw78MTsVxPn6Ntfm8b4iIog5MEJ4ZW6 IigQ== X-Gm-Message-State: AC+VfDxANKOfoFMh0JKVSsSdUYmqg+8Cgrch3j5suB/4Rs+bQv7I/2ET KFqZy/Y3fiYYx55+4ij0STkFE6g2ubbByfSwkHM= X-Google-Smtp-Source: ACHHUZ6EZNlE4Du5N3Y72WmKvuiRmzbXsGULqnTizIwyb/L+YgoR7ll0VG9n8yUkHJQxTrOSWtMnmw== X-Received: by 2002:a17:902:c946:b0:1a5:22a:165c with SMTP id i6-20020a170902c94600b001a5022a165cmr43716275pla.0.1684156140372; Mon, 15 May 2023 06:09:00 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:991:5400:4ff:fe70:1e06]) by smtp.gmail.com with ESMTPSA id h5-20020a170902748500b001ac2a73dbf2sm13458723pll.291.2023.05.15.06.08.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 06:08:59 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao , Song Liu , Jiri Olsa Subject: [PATCH bpf-next v2 1/3] bpf: Fix memleak due to fentry attach failure Date: Mon, 15 May 2023 13:08:47 +0000 Message-Id: <20230515130849.57502-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230515130849.57502-1-laoar.shao@gmail.com> References: <20230515130849.57502-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net If it fails to attach fentry, the allocated bpf trampoline image will be left in the system. That can be verified by checking /proc/kallsyms. This meamleak can be verified by a simple bpf program as follows, SEC("fentry/trap_init") int fentry_run() { return 0; } It will fail to attach trap_init because this function is freed after kernel init, and then we can find the trampoline image is left in the system by checking /proc/kallsyms. $ tail /proc/kallsyms ffffffffc0613000 t bpf_trampoline_6442453466_1 [bpf] ffffffffc06c3000 t bpf_trampoline_6442453466_1 [bpf] $ bpftool btf dump file /sys/kernel/btf/vmlinux | grep "FUNC 'trap_init'" [2522] FUNC 'trap_init' type_id=119 linkage=static $ echo $((6442453466 & 0x7fffffff)) 2522 Note that there are two left bpf trampoline images, that is because the libbpf will fallback to raw tracepoint if -EINVAL is returned. Fixes: e21aa341785c ("bpf: Fix fexit trampoline.") Signed-off-by: Yafang Shao Cc: Song Liu Cc: Jiri Olsa Acked-by: Song Liu --- kernel/bpf/trampoline.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index ac021bc..2a3849c 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -251,11 +251,8 @@ static int register_fentry(struct bpf_trampoline *tr, void *new_addr) return tlinks; } -static void __bpf_tramp_image_put_deferred(struct work_struct *work) +static void bpf_tramp_image_free(struct bpf_tramp_image *im) { - struct bpf_tramp_image *im; - - im = container_of(work, struct bpf_tramp_image, work); bpf_image_ksym_del(&im->ksym); bpf_jit_free_exec(im->image); bpf_jit_uncharge_modmem(PAGE_SIZE); @@ -263,6 +260,14 @@ static void __bpf_tramp_image_put_deferred(struct work_struct *work) kfree_rcu(im, rcu); } +static void __bpf_tramp_image_put_deferred(struct work_struct *work) +{ + struct bpf_tramp_image *im; + + im = container_of(work, struct bpf_tramp_image, work); + bpf_tramp_image_free(im); +} + /* callback, fexit step 3 or fentry step 2 */ static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) { @@ -438,7 +443,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut &tr->func.model, tr->flags, tlinks, tr->func.addr); if (err < 0) - goto out; + goto out_free; set_memory_rox((long)im->image, 1); @@ -468,7 +473,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut } #endif if (err) - goto out; + goto out_free; if (tr->cur_image) bpf_tramp_image_put(tr->cur_image); @@ -480,6 +485,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut tr->flags = orig_flags; kfree(tlinks); return err; + +out_free: + bpf_tramp_image_free(im); + goto out; } static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) From patchwork Mon May 15 13:08:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13241395 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1502CC8F0 for ; Mon, 15 May 2023 13:09:24 +0000 (UTC) Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FCA32706 for ; Mon, 15 May 2023 06:09:03 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-24e2b2a27ebso11843918a91.3 for ; Mon, 15 May 2023 06:09:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684156142; x=1686748142; 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=o1zxgFA/RYniDFm8rB33KhKyBx/11A9AN1LlDpM6cgs=; b=J3FvBbUK4hwiNEzIybZXWHuVGrN9lHCYMwJTiDP4YOEMOAkVkqz2wV18cy78q6byIh g1qDcji+VBztImF9CYKETOSlhLt6ejg1rgRM+mW1eS84nIFE8QgrDwKMfvb/BQ/8s00L P7JRaLqFNX4nGlXNf+uTRcS5frszTRXJz9XJOHxW/CcS5rL13pxFNa461R/IqXdFarL/ ZZq/pvM9QaeTgHZ2RMFH41JzOCcb/5KKbkpHBODeEPyfcC5EtJDWdapNHyfYas1Xo+7/ DAK2CVcABKqPA0hAs+pRusPV0odrudrHUNWjMW/5RdAJJHRuigFhJkoFwroBkP9/BcYd hWKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684156142; x=1686748142; 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=o1zxgFA/RYniDFm8rB33KhKyBx/11A9AN1LlDpM6cgs=; b=WVnpwO6bT657HA9KiTulVDTjNZauCFJpOaaxyiOb28AxV4OjkQ/VcHFkA251jQ5xUA uRY8j4AyqN/RGbNmU3O46xWHhYW9oSCpJX2S1riUoAE63GGBU+ITktnnKOvPew7rYJMl vOOkNI/W4YkhCX8Sx6Tza6ujcCLSME2c35/AJ8oiXwvbfkJdIQDQwD+zFOrKxDEPaJxi jrcJjK8N7GHMM+iNY5/uaf4MsJgqdA0Eq08ySCUBo8IF45B7V9kdyoka5Wb7sKbKg/rw JdULQleD/4g98j11A/PtODBddjVvJQkddE18jkynMPJ4wrMWs2vZ764MRSHjOTAkkQgB C9ow== X-Gm-Message-State: AC+VfDz8Fb4fsVM1sAQS5zh2Jf8HwvPZeI71PNVFpL10jfTVHoRg31rK 7Qv4ksoYm4bGfVcPA8yz3O4= X-Google-Smtp-Source: ACHHUZ6k/fPjLOGfx+8yYH/VY765dpiOneJ4n9Hp7vQA0qbT9D2ztb6+H61I6BIXsrzh2ee63QBYMw== X-Received: by 2002:a17:90b:ecd:b0:252:a7b3:c24c with SMTP id gz13-20020a17090b0ecd00b00252a7b3c24cmr12698286pjb.20.1684156142061; Mon, 15 May 2023 06:09:02 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:991:5400:4ff:fe70:1e06]) by smtp.gmail.com with ESMTPSA id h5-20020a170902748500b001ac2a73dbf2sm13458723pll.291.2023.05.15.06.09.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 06:09:01 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao , Song Liu , Jiri Olsa Subject: [PATCH bpf-next v2 2/3] bpf: Remove bpf trampoline selector Date: Mon, 15 May 2023 13:08:48 +0000 Message-Id: <20230515130849.57502-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230515130849.57502-1-laoar.shao@gmail.com> References: <20230515130849.57502-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net After commit e21aa341785c ("bpf: Fix fexit trampoline."), the selector is only used to indicate how many times the bpf trampoline image are updated and been displayed in the trampoline ksym name. After the trampoline is freed, the selector will start from 0 again. So the selector is a useless value to the user. We can remove it. If the user want to check whether the bpf trampoline image has been updated or not, the user can compare the address. Each time the trampoline image is updated, the address will change consequently. Jiri pointed out antoher issue that perf is still using the old name "bpf_trampoline_%lu", so this change can fix the issue in perf. Fixes: e21aa341785c ("bpf: Fix fexit trampoline.") Signed-off-by: Yafang Shao Cc: Song Liu Cc: Jiri Olsa Acked-by: Song Liu --- include/linux/bpf.h | 1 - kernel/bpf/trampoline.c | 11 ++++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 456f33b..36e4b2d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1125,7 +1125,6 @@ struct bpf_trampoline { int progs_cnt[BPF_TRAMP_MAX]; /* Executable image of trampoline */ struct bpf_tramp_image *cur_image; - u64 selector; struct module *mod; }; diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c index 2a3849c..78acf28 100644 --- a/kernel/bpf/trampoline.c +++ b/kernel/bpf/trampoline.c @@ -349,7 +349,7 @@ static void bpf_tramp_image_put(struct bpf_tramp_image *im) call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); } -static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx) +static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key) { struct bpf_tramp_image *im; struct bpf_ksym *ksym; @@ -376,7 +376,7 @@ static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx) ksym = &im->ksym; INIT_LIST_HEAD_RCU(&ksym->lnode); - snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu_%u", key, idx); + snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key); bpf_image_ksym_add(image, ksym); return im; @@ -406,11 +406,10 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut err = unregister_fentry(tr, tr->cur_image->image); bpf_tramp_image_put(tr->cur_image); tr->cur_image = NULL; - tr->selector = 0; goto out; } - im = bpf_tramp_image_alloc(tr->key, tr->selector); + im = bpf_tramp_image_alloc(tr->key); if (IS_ERR(im)) { err = PTR_ERR(im); goto out; @@ -447,8 +446,7 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut set_memory_rox((long)im->image, 1); - WARN_ON(tr->cur_image && tr->selector == 0); - WARN_ON(!tr->cur_image && tr->selector); + WARN_ON(tr->cur_image && total == 0); if (tr->cur_image) /* progs already running at this address */ err = modify_fentry(tr, tr->cur_image->image, im->image, lock_direct_mutex); @@ -478,7 +476,6 @@ static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mut if (tr->cur_image) bpf_tramp_image_put(tr->cur_image); tr->cur_image = im; - tr->selector++; out: /* If any error happens, restore previous flags */ if (err) From patchwork Mon May 15 13:08:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13241394 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3549C8ED for ; Mon, 15 May 2023 13:09:23 +0000 (UTC) Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7871B271B for ; Mon, 15 May 2023 06:09:05 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1aaed87d8bdso89797795ad.3 for ; Mon, 15 May 2023 06:09:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684156143; x=1686748143; 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=+ZChEe8EJBWWIaKA4QFg5WE9+TdNDvwZFYcQOzDAkC4=; b=NU1abb/kLwY/0YDRPFi7q/ZZpXl20ZcOzeliZ/vZ3QBf7hHff6J6bLRxr8SfMzoADr zyLHwmhohoLnZK7WDZmyXnv7tOJPheJnqQ9whXCnKeGi1X8Aa3Mo2RxD1DswqKzvxrE+ ncB0nfBL+/4yaZaMzZ63+waZTWOD2lOKCCkSgynYhv9bTdwVAgIYdfQ/e9a3hAk+hfu1 8Fv0E9Fo1hVuxO50u9UYXqlyHzP2DDUhk2/cDsoSfxIKgLZ9L2NVt0q1l320j6txQTfA NECndyFNM3ELWSkCheae0Fhh3zXLwddq80DpIPcTR9TtQP130KES/WUpKFyw+14OYOGE cH1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684156143; x=1686748143; 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=+ZChEe8EJBWWIaKA4QFg5WE9+TdNDvwZFYcQOzDAkC4=; b=fVpsOBsP6XH8725W5vzFL58Og08V1jaIu2tsP5IYeOQIY3ouCOYR39Btgw//0BpXgP IceaZCe5f6/ftrzpEEHVjDGg1b/Lxnn387xLZPhcmoUHrMuiUDd4JajYe87XGOMgaWkE AiB7Mdon8/66QBraZ5eRlSe00Wwv3amD6bNPE6nt2r/IK0t6/txiza/O4ZCo+1//Fytc iGxp/aomTUBEmnrbTD+KjTh/UdmB8OX5VqiZjFgbHuWMazkK48RSHTKtn0bvTmzr4E2e yWVeWIth3IvneV5j1TQ6y3UdVOK1AsjJeIVR8oDmwt3KRv6K9zgerhEN5n22YAfkU5DW NbfQ== X-Gm-Message-State: AC+VfDxjtxCkiXd2LQ2bn9hNhz+EutrXedtkDlGeFllXvWOvZEwwuoiw moMi63DBWcnz2s26BPK48xI= X-Google-Smtp-Source: ACHHUZ7FOp6Z8HKfb81wlVVLkdD6Cv0Q5fg52mj2trBbuC4r2P6Sa28VLeMih12iSx6zCakVjGNn8g== X-Received: by 2002:a17:903:11cd:b0:1ab:94:1ee4 with SMTP id q13-20020a17090311cd00b001ab00941ee4mr42096430plh.2.1684156143566; Mon, 15 May 2023 06:09:03 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:991:5400:4ff:fe70:1e06]) by smtp.gmail.com with ESMTPSA id h5-20020a170902748500b001ac2a73dbf2sm13458723pll.291.2023.05.15.06.09.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 06:09:03 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org Cc: bpf@vger.kernel.org, Yafang Shao Subject: [PATCH bpf-next v2 3/3] bpf: Show target_{obj,btf}_id in tracing link info Date: Mon, 15 May 2023 13:08:49 +0000 Message-Id: <20230515130849.57502-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230515130849.57502-1-laoar.shao@gmail.com> References: <20230515130849.57502-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net The target_btf_id can help us understand which kernel function is linked by a tracing prog. The target_btf_id and target_obj_id have already been exposed to userspace, so we just need to show them. The result as follows, tools/bpf/bpftool/bpftool link show 2: tracing prog 13 prog_type tracing attach_type trace_fentry target_obj_id 1 target_btf_id 13964 pids trace(10673) $ tools/bpf/bpftool/bpftool link show -j [{"id":2,"type":"tracing","prog_id":13,"prog_type":"tracing","attach_type":"trace_fentry","target_obj_id":1,"target_btf_id":13964,"pids":[{"pid":10673,"comm":"trace"}]}] $ cat /proc/10673/fdinfo/10 pos: 0 flags: 02000000 mnt_id: 15 ino: 2094 link_type: tracing link_id: 2 prog_tag: a04f5eef06a7f555 prog_id: 13 attach_type: 24 target_obj_id: 1 target_btf_id: 13964 $ tail /proc/kallsyms ffffffffc0400fa0 t bpf_prog_a04f5eef06a7f555_fentry_run [bpf] ffffffffc062f000 t bpf_trampoline_6442464908 [bpf] $ echo $((6442464908 & 0x7fffffff)) $((6442464908 >> 32)) 13964 1 Signed-off-by: Yafang Shao Acked-by: Song Liu --- kernel/bpf/syscall.c | 12 ++++++++++-- tools/bpf/bpftool/link.c | 4 ++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 909c112..870395a 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2968,10 +2968,18 @@ static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link, { struct bpf_tracing_link *tr_link = container_of(link, struct bpf_tracing_link, link.link); + u32 target_btf_id; + u32 target_obj_id; + bpf_trampoline_unpack_key(tr_link->trampoline->key, + &target_obj_id, &target_btf_id); seq_printf(seq, - "attach_type:\t%d\n", - tr_link->attach_type); + "attach_type:\t%d\n" + "target_obj_id:\t%u\n" + "target_btf_id:\t%u\n", + tr_link->attach_type, + target_obj_id, + target_btf_id); } static int bpf_tracing_link_fill_link_info(const struct bpf_link *link, diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c index 243b74e..cfe896f 100644 --- a/tools/bpf/bpftool/link.c +++ b/tools/bpf/bpftool/link.c @@ -195,6 +195,8 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) show_link_attach_type_json(info->tracing.attach_type, json_wtr); + jsonw_uint_field(json_wtr, "target_obj_id", info->tracing.target_obj_id); + jsonw_uint_field(json_wtr, "target_btf_id", info->tracing.target_btf_id); break; case BPF_LINK_TYPE_CGROUP: jsonw_lluint_field(json_wtr, "cgroup_id", @@ -375,6 +377,8 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info) printf("\n\tprog_type %u ", prog_info.type); show_link_attach_type_plain(info->tracing.attach_type); + printf("\n\ttarget_obj_id %u target_btf_id %u ", + info->tracing.target_obj_id, info->tracing.target_btf_id); break; case BPF_LINK_TYPE_CGROUP: printf("\n\tcgroup_id %zu ", (size_t)info->cgroup.cgroup_id);