From patchwork Wed Jun 29 15:18:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuang Wang X-Patchwork-Id: 12900292 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 168EEC433EF for ; Wed, 29 Jun 2022 15:19:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233738AbiF2PT0 (ORCPT ); Wed, 29 Jun 2022 11:19:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233482AbiF2PT0 (ORCPT ); Wed, 29 Jun 2022 11:19:26 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5529022BC1; Wed, 29 Jun 2022 08:19:25 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id c4so14413381plc.8; Wed, 29 Jun 2022 08:19:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=G6qukGK7dTCAThbsePIio/EYDyzyRZYWAFR5AC42Gik=; b=SBEdYXGyAFISzPwcwvlY8UCwWwi3HskIcjS8brtdanZwQV9Vj1WF6/hfUnuW1u9Y+n jjVQOkic7GgZipHvOtonXeuTgKwHyxgqAi5ccrJXPB7z8OhFcM0KmV7atyT8wJcpkwHz 3loukM2tEMSlJAsQOHLhISTQR4hc8+iB9I4MWZwD9B5pbtx3Y3DnqYqgAipvR7gpMN7p YHpT9Tb6YiB1fszVDNx/PxPLcD/NtCIRVtJ32vLGofjiKyQw9GmCsGKoRbBTy19WQopw pyXI9p2oGP5+sFo3dy1/LZGKPMEiIgmdyzA7U2i4ZvrmoZNn0382NMhD2PmnrsSyRFfG E/CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=G6qukGK7dTCAThbsePIio/EYDyzyRZYWAFR5AC42Gik=; b=RmNswNkBv6PGz4P0pBGkOPINhU/URiVMbA7h0VafBuDykIb0bsr2zgRerCXg+IsJWl UQNoc28ODHyfdSAbdN9p4n8JXzg0SeXDzzHFY8/iQfaLaxFB96al02M8E4y8wbn5WCAa dGi9gzHsP1yofwGd6NhdPlz02F4TBCdvSpTehf8XC29eHcxu9QlCm+QFGyAf+hhNnATR xjPoRnEgZ7gLzS7v2pANe3CZ1QwKlnpmiHdjGOBerDToSR65LYp5l5S0kdhew+61qh1u URYHvwZQDdFhbqx6CgVgzMXwu2C0YHECsUP8cF/qCcx3Th5oHVrRTTmSqdKqUvCOtd5g +yNA== X-Gm-Message-State: AJIora/SljJnxDJDD67MJAO8hAQTjpNVvuW1Foq1KT+/paX2uWy8zn+D lt0+nAa3sfRB/hqggnDEWtU= X-Google-Smtp-Source: AGRyM1sCZM5mr7Mv2EmyKBOBA3zMLJDnAcyd7TStwlSNHDb8XVX5quu7OboHpWSZ8D1oOKFUyusErA== X-Received: by 2002:a17:90a:f314:b0:1ec:91a9:3256 with SMTP id ca20-20020a17090af31400b001ec91a93256mr4493905pjb.155.1656515964888; Wed, 29 Jun 2022 08:19:24 -0700 (PDT) Received: from localhost.localdomain ([47.242.114.172]) by smtp.gmail.com with ESMTPSA id x20-20020a170902b41400b001676dac529asm11522657plr.146.2022.06.29.08.19.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jun 2022 08:19:24 -0700 (PDT) From: Chuang Wang Cc: Chuang Wang , Jingren Zhou , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/3] libbpf: cleanup the legacy kprobe_event on failed add/attach_event() Date: Wed, 29 Jun 2022 23:18:45 +0800 Message-Id: <20220629151848.65587-2-nashuiliang@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220629151848.65587-1-nashuiliang@gmail.com> References: <20220629151848.65587-1-nashuiliang@gmail.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Before the 0bc11ed5ab60 commit ("kprobes: Allow kprobes coexist with livepatch"), in a scenario where livepatch and kprobe coexist on the same function entry, the creation of kprobe_event using add_kprobe_event_legacy() will be successful, at the same time as a trace event (e.g. /debugfs/tracing/events/kprobe/XXX) will exist, but perf_event_open() will return an error because both livepatch and kprobe use FTRACE_OPS_FL_IPMODIFY. As follows: 1) add a livepatch $ insmod livepatch-XXX.ko 2) add a kprobe using tracefs API (i.e. add_kprobe_event_legacy) $ echo 'p:mykprobe XXX' > /sys/kernel/debug/tracing/kprobe_events 3) enable this kprobe (i.e. sys_perf_event_open) This will return an error, -EBUSY. On Andrii Nakryiko's comment, few error paths in bpf_program__attach_kprobe_opts() that should need to call remove_kprobe_event_legacy(). With this patch, whenever an error is returned after add_kprobe_event_legacy() or bpf_program__attach_perf_event_opts(), this ensures that the created kprobe_event is cleaned. Signed-off-by: Chuang Wang Signed-off-by: Jingren Zhou --- tools/lib/bpf/libbpf.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e994797bcd48..8a33a52e01a5 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -9868,10 +9868,11 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, } type = determine_kprobe_perf_type_legacy(probe_name, retprobe); if (type < 0) { + err = type; pr_warn("failed to determine legacy kprobe event id for '%s+0x%zx': %s\n", kfunc_name, offset, - libbpf_strerror_r(type, errmsg, sizeof(errmsg))); - return type; + libbpf_strerror_r(err, errmsg, sizeof(errmsg))); + goto err_clean_legacy; } attr.size = sizeof(attr); attr.config = type; @@ -9885,9 +9886,14 @@ static int perf_event_kprobe_open_legacy(const char *probe_name, bool retprobe, err = -errno; pr_warn("legacy kprobe perf_event_open() failed: %s\n", libbpf_strerror_r(err, errmsg, sizeof(errmsg))); - return err; + goto err_clean_legacy; } return pfd; + +err_clean_legacy: + /* Clear the newly added legacy kprobe_event */ + remove_kprobe_event_legacy(probe_name, retprobe); + return err; } struct bpf_link * @@ -9944,7 +9950,7 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog, prog->name, retprobe ? "kretprobe" : "kprobe", func_name, offset, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); - goto err_out; + goto err_clean_legacy; } if (legacy) { struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); @@ -9955,6 +9961,11 @@ bpf_program__attach_kprobe_opts(const struct bpf_program *prog, } return link; + +err_clean_legacy: + if (legacy) + remove_kprobe_event_legacy(legacy_probe, retprobe); + err_out: free(legacy_probe); return libbpf_err_ptr(err); From patchwork Wed Jun 29 15:18:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuang Wang X-Patchwork-Id: 12900293 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 957B7C433EF for ; Wed, 29 Jun 2022 15:19:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233482AbiF2PTg (ORCPT ); Wed, 29 Jun 2022 11:19:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233836AbiF2PTf (ORCPT ); Wed, 29 Jun 2022 11:19:35 -0400 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 232A422BC1; Wed, 29 Jun 2022 08:19:35 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id l2so14822498pjf.1; Wed, 29 Jun 2022 08:19:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=peFkrkN4nYi58ApVeiNDHkxUCVe7+HUeNvG3DELjISc=; b=faFqr/nK9MhSs9cJs5VoyyLWaNbsLPSUyAXk3uEpY2wXlOyMsFslKYKblJHGoGstF+ RmzqUzZ9d3AUz1LYKhi3wVDcZBgvar8rDqiniC1922vtSNpSq2/P+JoptANFmu6KplmU VrXUIVEqEOlEbISN/IJGak6mCNhLdKjpISj4GvANmKgbgC5a/NcVDGhnHMdAEUEuD2F/ yF2ubdiDAyHk4vkiEXyw7v8Rmx8HSKJVyArh4VLeJqQNgvcqQR6UU2SzCMV3WFOoZaOR w9bfxFPVFTpYgGhatF8bf5oT75fA8DFsp9DoQPf9F6k3JTSlIXMuNFnNO9aYlPcL/7BB THYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=peFkrkN4nYi58ApVeiNDHkxUCVe7+HUeNvG3DELjISc=; b=253nvIh0rY+b03wTeOuQzpjwvPJ6LOOxe9VnaEp+/j/kHy+CD59IgUysAtRtLUB+wi tP/6FnkSF+E5NivG8aG2DKIOUA8DZbNGCz7pMXZ2bpF1WWNKndcY58qLytQxjm65sLNf hjm8vydirO8VTrVhmRF+becKjaWeP1XIwsjfKA4BWqoXHpHHMhjvXuT2nZFOQZa9uao8 tZGcbOnkHdwD8mC3Wv9epVhpfLdoJbr87K+CSrCrdtABfOKkqSjqPHQthHExsvhi9xAl YkIWASMLITW9zxklZobF5AmVEdJudhPuyCRFKVwDR0Gb3/3FYukX5WjaITonEixZ7M+w +a9Q== X-Gm-Message-State: AJIora+FJWcBJx2wOoukKhKQ5YLJ4S/0kMVNLK6Wct0017q19FW9s8FD u6Hn3JvQY8ANRjkmKO5bKfk= X-Google-Smtp-Source: AGRyM1t9fvqN2nHCJpv2fXpxhBT3sO/vlOHImlVGBVMsons+ozc8ZyN2Li70319/JH4PqE9bO/jNGA== X-Received: by 2002:a17:90a:b88c:b0:1ec:9449:219a with SMTP id o12-20020a17090ab88c00b001ec9449219amr6259772pjr.243.1656515974635; Wed, 29 Jun 2022 08:19:34 -0700 (PDT) Received: from localhost.localdomain ([47.242.114.172]) by smtp.gmail.com with ESMTPSA id x20-20020a170902b41400b001676dac529asm11522657plr.146.2022.06.29.08.19.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jun 2022 08:19:34 -0700 (PDT) From: Chuang Wang Cc: Chuang Wang , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/3] libbpf: fix wrong variable used in perf_event_uprobe_open_legacy() Date: Wed, 29 Jun 2022 23:18:46 +0800 Message-Id: <20220629151848.65587-3-nashuiliang@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220629151848.65587-1-nashuiliang@gmail.com> References: <20220629151848.65587-1-nashuiliang@gmail.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Use "type" as opposed to "err" in pr_warn() after determine_uprobe_perf_type_legacy() returns an error. Signed-off-by: Chuang Wang --- tools/lib/bpf/libbpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8a33a52e01a5..159b69d8b941 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10241,7 +10241,7 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, type = determine_uprobe_perf_type_legacy(probe_name, retprobe); if (type < 0) { pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n", - binary_path, offset, err); + binary_path, offset, type); return type; } From patchwork Wed Jun 29 15:18:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuang Wang X-Patchwork-Id: 12900294 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 64BFACCA47C for ; Wed, 29 Jun 2022 15:19:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233926AbiF2PTo (ORCPT ); Wed, 29 Jun 2022 11:19:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233884AbiF2PTl (ORCPT ); Wed, 29 Jun 2022 11:19:41 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3DFA252BE; Wed, 29 Jun 2022 08:19:40 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id b12-20020a17090a6acc00b001ec2b181c98so19768510pjm.4; Wed, 29 Jun 2022 08:19:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NCR65AgEN7e8ge6gFaeYgLnI/aL4mIVTifuImi+nxuU=; b=qMfCbq33d4ryv+VK2N01vtFa72jjYz0nYz2EAE3ETlJJxWeYplC5Yv/k7ZMwfNXdam 6O0SP3ZF5j0U6qzqCsqJjnFpJTbUUYn7IYHZlIi5A00e4GfeCdCLXdeU8kGpF5dTMpWX 2D3POY0dLQ5PrdNZqt369mxc+U/VV4j0MIKyvs2LV1OFFRsQ2tKILcfrGtMIzkahkHu0 4+laIdJd2IfgCgVA7a/9kmpu48Ixmz3hRhPZP2W6kyr0EDR+3Nrviv/q/TNTpR5hT2ca bulkofz0F49kSujm4DeWEWVYMWKfdpGr3KWANwT2cGprEMBh4y8yLQYbs5DEHyOY/U+4 eCJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NCR65AgEN7e8ge6gFaeYgLnI/aL4mIVTifuImi+nxuU=; b=7SAAIjVJ+RT0dC6x8Z3BUuXIEgzn/OZ/GhlO2evEsbSCAW0BZ+18VqksElQ44Rx582 A/GHySEK+NjOeGF2YWHFSUWnGkMvw/JrpqaXGpJgtXFWlo5Na375ibeGcEmEMVDVDRGH 0oBjASTFCuct+hDEKb/VueiXh82OCyJfMML1TBLC8kp24+WW6zehgjwj+KkKTaqAKd2J ssWLYxIFcyTywQsy9Amt6esboDW0lFLByE44bEigbyx8PRLxAphY8fEgBt6MDAMC5mIN eLSHtupfDzp9RCy3UBV/J+HQLdTDUpHhfwiUE5a0q//Ljf63tPONg6AVfUXD/32Fs+Nw 6Rng== X-Gm-Message-State: AJIora8iNkbJxGPs0vpInq0tel7co7e4JJ1HLc2M6uDWsCFudF+quM/z T94Rmu/hgznPIl3eABfAM8Xk5/cytOPx7A== X-Google-Smtp-Source: AGRyM1ua3RPK72bxy2NoNyrj6KWzRCCDDeOXPITO7g4kSTv2G1YxGM4YumrKM4dtu5lXu2gNN/Dd1w== X-Received: by 2002:a17:90a:31c1:b0:1ec:729f:36b7 with SMTP id j1-20020a17090a31c100b001ec729f36b7mr6179995pjf.123.1656515980491; Wed, 29 Jun 2022 08:19:40 -0700 (PDT) Received: from localhost.localdomain ([47.242.114.172]) by smtp.gmail.com with ESMTPSA id x20-20020a170902b41400b001676dac529asm11522657plr.146.2022.06.29.08.19.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Jun 2022 08:19:39 -0700 (PDT) From: Chuang Wang Cc: Chuang Wang , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/3] libbpf: cleanup the legacy uprobe_event on failed add/attach_event() Date: Wed, 29 Jun 2022 23:18:47 +0800 Message-Id: <20220629151848.65587-4-nashuiliang@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220629151848.65587-1-nashuiliang@gmail.com> References: <20220629151848.65587-1-nashuiliang@gmail.com> MIME-Version: 1.0 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net A potential scenario, when an error is returned after add_uprobe_event_legacy() in perf_event_uprobe_open_legacy(), or bpf_program__attach_perf_event_opts() in bpf_program__attach_uprobe_opts() returns an error, the uprobe_event that was previously created is not cleaned. So, with this patch, when an error is returned, fix this by adding remove_uprobe_event_legacy() Signed-off-by: Chuang Wang --- tools/lib/bpf/libbpf.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 159b69d8b941..5aa6033a0284 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10240,9 +10240,10 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, } type = determine_uprobe_perf_type_legacy(probe_name, retprobe); if (type < 0) { + err = type; pr_warn("failed to determine legacy uprobe event id for %s:0x%zx: %d\n", - binary_path, offset, type); - return type; + binary_path, offset, err); + goto err_clean_legacy; } memset(&attr, 0, sizeof(attr)); @@ -10257,9 +10258,14 @@ static int perf_event_uprobe_open_legacy(const char *probe_name, bool retprobe, if (pfd < 0) { err = -errno; pr_warn("legacy uprobe perf_event_open() failed: %d\n", err); - return err; + goto err_clean_legacy; } return pfd; + +err_clean_legacy: + /* Clear the newly added legacy uprobe_event */ + remove_uprobe_event_legacy(probe_name, retprobe); + return err; } /* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */ @@ -10593,7 +10599,7 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, prog->name, retprobe ? "uretprobe" : "uprobe", binary_path, func_offset, libbpf_strerror_r(err, errmsg, sizeof(errmsg))); - goto err_out; + goto err_clean_legacy; } if (legacy) { struct bpf_link_perf *perf_link = container_of(link, struct bpf_link_perf, link); @@ -10603,10 +10609,14 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid, perf_link->legacy_is_retprobe = retprobe; } return link; + +err_clean_legacy: + if (legacy) + remove_uprobe_event_legacy(legacy_probe, retprobe); + err_out: free(legacy_probe); return libbpf_err_ptr(err); - } /* Format of u[ret]probe section definition supporting auto-attach: