From patchwork Wed Jul 21 21:58:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12392345 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7FA0C6377B for ; Wed, 21 Jul 2021 21:58:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 950DA6124B for ; Wed, 21 Jul 2021 21:58:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230046AbhGUVRr (ORCPT ); Wed, 21 Jul 2021 17:17:47 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:60638 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbhGUVRo (ORCPT ); Wed, 21 Jul 2021 17:17:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626904700; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Rr/KKkd5zXYat1qyJl/djPxgaYzdXYoveXMN2G13uMA=; b=T+wtGSo4cSpqsFjgxFT66GUquWTNKU1n2atxKUEtoPD8subBUtFblsXPx6kXk9h0Aag7Lm hZ195qMMSQ0+Fh4IhCprzvd5BRbeAFxnyyWhrI4Lkx+vDC82CkaZQl4/OpTKoCjVnX7bzf c0m6dK+b5EYs7/zIeJTidTjvvn7O0V0= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-494-NQvnimxMNeOb_-OB_A1EcQ-1; Wed, 21 Jul 2021 17:58:18 -0400 X-MC-Unique: NQvnimxMNeOb_-OB_A1EcQ-1 Received: by mail-ed1-f69.google.com with SMTP id i19-20020a05640200d3b02903948b71f25cso1718846edu.4 for ; Wed, 21 Jul 2021 14:58:18 -0700 (PDT) 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=Rr/KKkd5zXYat1qyJl/djPxgaYzdXYoveXMN2G13uMA=; b=ozWuAl/Nr5ZJo2D2t7k5IbFimxoHx35eMZMgn79c449uloTDM+WfjRNq+GoqgLRR96 I26F1pkn+w054RHIgipf9Y6likoAcbtHA+4fo5zxZoOWBDmQAD62CK5TjaySc/2u1AC9 JTOHqAFIWi6BVbKu9kIEd/Z22zeKaM8pK58cops/C1OrR1VpHT6eo+JAfMUsS2kQlDEd E31bnLHnZQIbKfB5PuQQ8Hor9oKqi0whB/uNTeUm13B9y1w3W+PKhlV9qdhmaZPYyY9k U34rcEBh57O3A9f5olyW+zZK2DNs+NPrKhVbg44QJ9dW9rn1KRrToNRmrOXuaUVnda/v mvwA== X-Gm-Message-State: AOAM530Ak9ObTSKidXYlXhWMSSHeG1kWA4BH5gqM1ZdPMXq/gbO0MnVL UyDk76lWm2yDetPkR1w/bR86noPJKvoD+/IrMA2sAwqe4S5ekBAJVyaKrqLHlga1amZezKjMhEq YWYYIYOblIWih X-Received: by 2002:a17:906:34ca:: with SMTP id h10mr41229334ejb.41.1626904697780; Wed, 21 Jul 2021 14:58:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxFjsfbkXohPNEK4fDO1ZFmawWIRbsFGTHBmygJ88qfl8uG2fiB8lCyN3uW/+G9mioren58hg== X-Received: by 2002:a17:906:34ca:: with SMTP id h10mr41229328ejb.41.1626904697653; Wed, 21 Jul 2021 14:58:17 -0700 (PDT) Received: from krava.cust.in.nbox.cz ([83.240.60.59]) by smtp.gmail.com with ESMTPSA id t5sm5545969eji.113.2021.07.21.14.58.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jul 2021 14:58:17 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Andrii Nakryiko , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Masami Hiramatsu , Alan Maguire Subject: [PATCH bpf-next 1/3] libbpf: Fix func leak in attach_kprobe Date: Wed, 21 Jul 2021 23:58:08 +0200 Message-Id: <20210721215810.889975-2-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210721215810.889975-1-jolsa@kernel.org> References: <20210721215810.889975-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Adding missing free for func pointer in attach_kprobe function. Reported-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 4c153c379989..d46c2dd37be2 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10431,6 +10431,7 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, return libbpf_err_ptr(err); } if (opts.retprobe && offset != 0) { + free(func); err = -EINVAL; pr_warn("kretprobes do not support offset specification\n"); return libbpf_err_ptr(err); From patchwork Wed Jul 21 21:58:09 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12392347 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1E57C6377A for ; Wed, 21 Jul 2021 21:58:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 893CE6124B for ; Wed, 21 Jul 2021 21:58:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230047AbhGUVRw (ORCPT ); Wed, 21 Jul 2021 17:17:52 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:59303 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbhGUVRv (ORCPT ); Wed, 21 Jul 2021 17:17:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626904707; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cnthrEhqSK4f18XnoUkLoCiCM8/m0dpLypLCFrHKZCs=; b=G58V/M1tnZHaLHPY6bL9/0JyhxDdgkzdfX6mgrd6QZKIoviloX38IgJ4FrY7b3/JrpmcYA 9Mw7nWi7PJorRgEl9wp6DL5xbcg+6AH31uAMcxPpc5vdSfcVGdG9tEg04KsYZhzbz+Ws4v BKfxV4Wng3/4DEyfGLodbz8jK/3m2FI= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-141-emfIIkGjNUGPE7fmOR8cwQ-1; Wed, 21 Jul 2021 17:58:25 -0400 X-MC-Unique: emfIIkGjNUGPE7fmOR8cwQ-1 Received: by mail-ej1-f70.google.com with SMTP id jr6-20020a170906a986b029051b3550f911so1292680ejb.18 for ; Wed, 21 Jul 2021 14:58:25 -0700 (PDT) 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=cnthrEhqSK4f18XnoUkLoCiCM8/m0dpLypLCFrHKZCs=; b=ijpnEFmFxiV9LHa6/WcY8fgP8DDDfTe3KywIsNmecwec+W/Y3xS4BhMgCBp3CV7Mrw 0X4pPdUoMZ29IjxV7rxSd1mghYfnpQJbFwgqyTIs/v9suzRDzG3OKhBGcFI5oG0bFYf/ dDofiREatSPTL72Hw5+Hb08ICEM5HVh043Vj3PWuGE9KAM507Bqswg6lX4ZOgfPmSADz 2NR7pXfIeSX9RQDllFQXJHowy6Jc6GLD/Gcd1swky3OtWXrNc1/zK7hLINQXcfCVEk45 yK5ggZ565CAn/4g6I11FhZ9Ho/bBT2alOEHtzw7DG1GRD9h1CT8smdnDVqCxlsuJyvfR Ch5A== X-Gm-Message-State: AOAM530wynxCINSL8RY3dARA9sDX/6QruSNIvEeDFEj6ivAi4HHDpKm+ FU17GHkYDLDZvahJCG1gkGIOLgDrI2IGqjk6e66eWbdaT8fJZ8AnGu4hUoEhiLmlLWCX9kEX+5u 38cGLDyzQOnc2 X-Received: by 2002:a17:906:6811:: with SMTP id k17mr40440356ejr.280.1626904704173; Wed, 21 Jul 2021 14:58:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJ4t0XIQQe8fG93ueNgrMdaYO+hzaOLTt1gHNTjIsb+whmwX14SfN65P5+wkYLrPXQbydCmw== X-Received: by 2002:a17:906:6811:: with SMTP id k17mr40440345ejr.280.1626904703985; Wed, 21 Jul 2021 14:58:23 -0700 (PDT) Received: from krava.cust.in.nbox.cz ([83.240.60.59]) by smtp.gmail.com with ESMTPSA id q24sm11283244edc.82.2021.07.21.14.58.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jul 2021 14:58:23 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Andrii Nakryiko , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Masami Hiramatsu , Alan Maguire Subject: [PATCH bpf-next 2/3] libbpf: Allow decimal offset for kprobes Date: Wed, 21 Jul 2021 23:58:09 +0200 Message-Id: <20210721215810.889975-3-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210721215810.889975-1-jolsa@kernel.org> References: <20210721215810.889975-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Allow to specify decimal offset in SEC macro, like: SEC("kprobe/bpf_fentry_test7+5") Adding selftest for that. Suggested-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 2 +- .../selftests/bpf/prog_tests/get_func_ip_test.c | 2 ++ tools/testing/selftests/bpf/progs/get_func_ip_test.c | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index d46c2dd37be2..52f4f1d4f495 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10424,7 +10424,7 @@ static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, func_name = prog->sec_name + sec->len; opts.retprobe = strcmp(sec->sec, "kretprobe/") == 0; - n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%lx", &func, &offset); + n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset); if (n < 1) { err = -EINVAL; pr_warn("kprobe name is invalid: %s\n", func_name); diff --git a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c index 088b3653610d..02a465f36d59 100644 --- a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c +++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c @@ -17,6 +17,7 @@ void test_get_func_ip_test(void) */ #ifndef __x86_64__ bpf_program__set_autoload(skel->progs.test6, false); + bpf_program__set_autoload(skel->progs.test7, false); #endif err = get_func_ip_test__load(skel); @@ -46,6 +47,7 @@ void test_get_func_ip_test(void) ASSERT_EQ(skel->bss->test5_result, 1, "test5_result"); #ifdef __x86_64__ ASSERT_EQ(skel->bss->test6_result, 1, "test6_result"); + ASSERT_EQ(skel->bss->test7_result, 1, "test7_result"); #endif cleanup: diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c index acd587b6e859..a587aeca5ae0 100644 --- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c @@ -11,6 +11,7 @@ extern const void bpf_fentry_test3 __ksym; extern const void bpf_fentry_test4 __ksym; extern const void bpf_modify_return_test __ksym; extern const void bpf_fentry_test6 __ksym; +extern const void bpf_fentry_test7 __ksym; __u64 test1_result = 0; SEC("fentry/bpf_fentry_test1") @@ -71,3 +72,13 @@ int test6(struct pt_regs *ctx) test6_result = (const void *) addr == &bpf_fentry_test6 + 5; return 0; } + +__u64 test7_result = 0; +SEC("kprobe/bpf_fentry_test7+5") +int test7(struct pt_regs *ctx) +{ + __u64 addr = bpf_get_func_ip(ctx); + + test7_result = (const void *) addr == &bpf_fentry_test7 + 5; + return 0; +} From patchwork Wed Jul 21 21:58:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Olsa X-Patchwork-Id: 12392349 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 X-Spam-Level: X-Spam-Status: No, score=-20.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A776C6377B for ; Wed, 21 Jul 2021 21:58:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 10B2061246 for ; Wed, 21 Jul 2021 21:58:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230008AbhGUVR5 (ORCPT ); Wed, 21 Jul 2021 17:17:57 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:45712 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbhGUVR5 (ORCPT ); Wed, 21 Jul 2021 17:17:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1626904713; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ccMewzkPCGXNEiOr3BDbhXGwV/AjKpWpsb/2bGggAwo=; b=TbewAVkCJOhLDv3oMxJ700RVgkIyK7xaRfoOkWuRjU5LuXRIl8in6BAPolWvHzfhrs0dA3 qzpUqXCjRjmTWdb3/0xs0Yfw8zmgPa3M6jSmAIyj7uksCWeznoc/G3rwR6kKc0HmpRXdPD 9202i+BQzOmXOb80Zl6OdKEEMG+XVB4= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-60-sMwAKJ5pMYCHaPRvZUEo1A-1; Wed, 21 Jul 2021 17:58:31 -0400 X-MC-Unique: sMwAKJ5pMYCHaPRvZUEo1A-1 Received: by mail-ej1-f72.google.com with SMTP id e23-20020a1709062497b0290504bafdd58dso1300547ejb.4 for ; Wed, 21 Jul 2021 14:58:31 -0700 (PDT) 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=ccMewzkPCGXNEiOr3BDbhXGwV/AjKpWpsb/2bGggAwo=; b=fI/Vdb2SBLNHgSprkQASe/FsGNbAQ6bVAIP9my/PGLeUeCLblyJ3RQYrSNGc6pyRLl uuipZ5Rdn+pC0aN9/tRXecqR24rwjtQZixGxkxg+YOMoCx5Ur2o7eAvUj3hAei5yYJhc vZfWirS5uzXn0t6gfyKQtg5VBXr0gd1qw75fyFismNdhKMTGsQYPc23myd9Fks+Zj1iL oF2GB9oRJxjB65Ov7RamJjccpKJmWz2OEpQKW8fQkHMmPOJtx5HuWA8cqRLctIVw9W6g eAoGZtJWZyxtGYmrHiJ8WsBwJpPKfKxsCAcVTkcphExFlMAIRdIPtxP5RFWUKYueEQty jm6A== X-Gm-Message-State: AOAM530D5l9LWIdW5Udi58cI43zfXf9cZX/hkgxGGTqOTeNl0Mm5IFgm UtObIDvDSWDPpYIk97DeWta3PNnOpXmJkKvmWfwuNT0dOcsN5KzcxbrjiozzNS9fWa7hihF6CL7 OKXopoHKiWNUY X-Received: by 2002:a17:906:7c6:: with SMTP id m6mr24060881ejc.456.1626904710336; Wed, 21 Jul 2021 14:58:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/d/fmsrF6DFPHn3DuoAiXFEmIbfGuX7ba1DUS37m6SD8VFG7/J9X1w43TXoUs7i7D24VnZQ== X-Received: by 2002:a17:906:7c6:: with SMTP id m6mr24060870ejc.456.1626904710189; Wed, 21 Jul 2021 14:58:30 -0700 (PDT) Received: from krava.cust.in.nbox.cz ([83.240.60.59]) by smtp.gmail.com with ESMTPSA id v16sm10741216edc.52.2021.07.21.14.58.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jul 2021 14:58:29 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko Cc: Andrii Nakryiko , netdev@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Masami Hiramatsu , Alan Maguire Subject: [PATCH bpf-next 3/3] libbpf: Export bpf_program__attach_kprobe_opts function Date: Wed, 21 Jul 2021 23:58:10 +0200 Message-Id: <20210721215810.889975-4-jolsa@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210721215810.889975-1-jolsa@kernel.org> References: <20210721215810.889975-1-jolsa@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Exporting bpf_program__attach_kprobe_opts function. Renaming bpf_program_attach_kprobe_opts to bpf_kprobe_opts and adding 'sz' field for forward/backward compatiblity. Suggested-by: Andrii Nakryiko Signed-off-by: Jiri Olsa --- tools/lib/bpf/libbpf.c | 31 +++++++++++++++++-------------- tools/lib/bpf/libbpf.h | 14 ++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 52f4f1d4f495..63a6239d8569 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10366,25 +10366,28 @@ static int perf_event_open_probe(bool uprobe, bool retprobe, const char *name, return pfd; } -struct bpf_program_attach_kprobe_opts { - bool retprobe; - unsigned long offset; -}; - -static struct bpf_link* +struct bpf_link* bpf_program__attach_kprobe_opts(struct bpf_program *prog, const char *func_name, - struct bpf_program_attach_kprobe_opts *opts) + struct bpf_kprobe_opts *opts) { char errmsg[STRERR_BUFSIZE]; struct bpf_link *link; + unsigned long offset; + bool retprobe; int pfd, err; - pfd = perf_event_open_probe(false /* uprobe */, opts->retprobe, func_name, - opts->offset, -1 /* pid */); + if (!OPTS_VALID(opts, bpf_kprobe_opts)) + return libbpf_err_ptr(-EINVAL); + + retprobe = OPTS_GET(opts, retprobe, false); + offset = OPTS_GET(opts, offset, 0); + + pfd = perf_event_open_probe(false /* uprobe */, retprobe, func_name, + offset, -1 /* pid */); if (pfd < 0) { pr_warn("prog '%s': failed to create %s '%s' perf event: %s\n", - prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name, + prog->name, retprobe ? "kretprobe" : "kprobe", func_name, libbpf_strerror_r(pfd, errmsg, sizeof(errmsg))); return libbpf_err_ptr(pfd); } @@ -10393,7 +10396,7 @@ bpf_program__attach_kprobe_opts(struct bpf_program *prog, if (err) { close(pfd); pr_warn("prog '%s': failed to attach to %s '%s': %s\n", - prog->name, opts->retprobe ? "kretprobe" : "kprobe", func_name, + prog->name, retprobe ? "kretprobe" : "kprobe", func_name, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); return libbpf_err_ptr(err); } @@ -10404,9 +10407,9 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe, const char *func_name) { - struct bpf_program_attach_kprobe_opts opts = { + DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts, .retprobe = retprobe, - }; + ); return bpf_program__attach_kprobe_opts(prog, func_name, &opts); } @@ -10414,7 +10417,7 @@ struct bpf_link *bpf_program__attach_kprobe(struct bpf_program *prog, static struct bpf_link *attach_kprobe(const struct bpf_sec_def *sec, struct bpf_program *prog) { - struct bpf_program_attach_kprobe_opts opts; + DECLARE_LIBBPF_OPTS(bpf_kprobe_opts, opts); unsigned long offset = 0; struct bpf_link *link; const char *func_name; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 6b08c1023609..da4a63a1265d 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -104,6 +104,16 @@ struct bpf_object_open_opts { }; #define bpf_object_open_opts__last_field btf_custom_path +struct bpf_kprobe_opts { + /* size of this struct, for forward/backward compatiblity */ + size_t sz; + /* kprobe is return probe */ + bool retprobe; + /* function's offset to install kprobe to */ + unsigned long offset; +}; +#define bpf_kprobe_opts__last_field offset + LIBBPF_API struct bpf_object *bpf_object__open(const char *path); LIBBPF_API struct bpf_object * bpf_object__open_file(const char *path, const struct bpf_object_open_opts *opts); @@ -249,6 +259,10 @@ bpf_program__attach_perf_event(struct bpf_program *prog, int pfd); LIBBPF_API struct bpf_link * bpf_program__attach_kprobe(struct bpf_program *prog, bool retprobe, const char *func_name); +LIBBPF_API struct bpf_link* +bpf_program__attach_kprobe_opts(struct bpf_program *prog, + const char *func_name, + struct bpf_kprobe_opts *opts); LIBBPF_API struct bpf_link * bpf_program__attach_uprobe(struct bpf_program *prog, bool retprobe, pid_t pid, const char *binary_path, diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 5bfc10722647..887d372a3f27 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -372,6 +372,7 @@ LIBBPF_0.5.0 { global: bpf_map__initial_value; bpf_map_lookup_and_delete_elem_flags; + bpf_program__attach_kprobe_opts; bpf_object__gen_loader; btf_dump__dump_type_data; libbpf_set_strict_mode;