From patchwork Mon Apr 29 22:45:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 13647847 X-Patchwork-Delegate: bpf@iogearbox.net Received: from wfout2-smtp.messagingengine.com (wfout2-smtp.messagingengine.com [64.147.123.145]) (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 BFE5818028 for ; Mon, 29 Apr 2024 22:46:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.145 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714430784; cv=none; b=qBMy+v7PzTq3zkf7NyC9tJjP9mn7ApP6zhkMKbUi03SlAToe/+hA7e1RY7SBFua5NKwd/OlG8Coqp+Iw96d+0FSPaMGZt/c/GHAyUTN5vF/5OQiwHv9UGD8uTW2ywk1EAH9ryqXqH5CIHzVOr7l6qm4ae4nK4HUUJEssvmFphIE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714430784; c=relaxed/simple; bh=YfE1nuWixn7IuvdoEPPm5kDU5Wjy2Y6UUXifL6WaGXA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=phnTsBVjxpDrkfHJ4+PeVkCra+d9v7ZNGsIit5y6oyR9egINUH3Q1ut9x02mUsfSR7W9WPsGnnrlSHVUpxU0OiTUEpG6ETlteVFYgxLOkX/DoS1Bew3lz5cQe6civPxTjoucc2R+aiPU3u0+JCqXPt42BOkyH6qQ+OeU7QXBKaM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz; spf=pass smtp.mailfrom=dxuuu.xyz; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b=H9DbyykL; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=iyHCJ8XP; arc=none smtp.client-ip=64.147.123.145 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="H9DbyykL"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="iyHCJ8XP" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfout.west.internal (Postfix) with ESMTP id 8908D1C0016B; Mon, 29 Apr 2024 18:46:21 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Mon, 29 Apr 2024 18:46:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1714430781; x= 1714517181; bh=euHHNw1HiVnE36g36ggJPa3VXrD//XqZAWsQ6yPcvuU=; b=H 9DbyykL+EQlH5240EhksIfA4GHdRujM0wTTfg8q8DrDwd86IYu6wYSvrctGXGU6y 0FR0P2VZUx6NC9FTlHAW5VA7E8IwfZ0/s8VmG/ibPKSO5CidO3nuczirxDQBBq/l /lIAm8S8GFMUHNFFvla5ujn3DLIIgS92jhhZ0/rNnsZNIiI3DFYxOGtkHeNnM0oe sYRCvVZhfoerPmP4gAqyFJmL2bT75ZReuHbF8L8iU1VL84MrSPjtZPASYATBsEAs MHAk+Kt2prybksIFaGB0pMh8wUg/p9h8z3BGsiQdcH1GnAN4ABhiwtZ3ZHa1V59z ILa/og/WFK8qQLrqK7s3A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1714430781; x= 1714517181; bh=euHHNw1HiVnE36g36ggJPa3VXrD//XqZAWsQ6yPcvuU=; b=i yHCJ8XPsKm9uU6KJM6/Jcu6wfKvPpOBYSk89CCfo4kGLlKo28f9ugpMTvNGLYq4e iRBCzJy2DffAFFo5VDAHDJdJvOm2xKI++VHTn26PgmsUyPfFeHgG2FyoloSw6+Dm GgZ4PHG3DFsXLpSZL+riVRG9J7Nf+Pw3G/5RwAWqZOkE5LN8b/Zia8c+vqhafsU6 zyqh0f5GR/eHmaVj+k9CxKZ8Sh7YU0r+30qVMQnQ20TH+ZDaRWUl8b+hdGoVuhfW eyTWxYlUfTDWF7eaYnXBsnBeXaQNjxCG+aM3h1PxEbhPx+HaOKvFPg8Amc/s3F4U uQCu6ATPvlxp/YebJ83fQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdduvddgudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 29 Apr 2024 18:46:20 -0400 (EDT) From: Daniel Xu To: acme@kernel.org, jolsa@kernel.org, quentin@isovalent.com, alan.maguire@oracle.com, eddyz87@gmail.com Cc: andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net, bpf@vger.kernel.org Subject: [PATCH dwarves v9 1/3] pahole: Save input filename separate from output Date: Mon, 29 Apr 2024 16:45:58 -0600 Message-ID: <1728b8d941d2658b310457b6c59d97f102aaf66d.1714430735.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 During detached BTF encoding, the input file is not necessarily the same as the output file. So save them separately. This matters when we need to look at the input file again, such as for kfunc tagging. Signed-off-by: Daniel Xu --- btf_encoder.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/btf_encoder.c b/btf_encoder.c index 19e9d90..5ffaf5d 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -64,6 +64,7 @@ struct btf_encoder { struct btf *btf; struct cu *cu; struct gobuffer percpu_secinfo; + const char *source_filename; const char *filename; struct elf_symtab *symtab; uint32_t type_id_off; @@ -1648,6 +1649,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam if (encoder) { encoder->raw_output = detached_filename != NULL; + encoder->source_filename = strdup(cu->filename); encoder->filename = strdup(encoder->raw_output ? detached_filename : cu->filename); if (encoder->filename == NULL) goto out_delete; @@ -1730,6 +1732,7 @@ void btf_encoder__delete(struct btf_encoder *encoder) btf_encoders__delete(encoder); __gobuffer__delete(&encoder->percpu_secinfo); zfree(&encoder->filename); + zfree(&encoder->source_filename); btf__free(encoder->btf); encoder->btf = NULL; elf_symtab__delete(encoder->symtab); From patchwork Mon Apr 29 22:45:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 13647848 X-Patchwork-Delegate: bpf@iogearbox.net Received: from wfout2-smtp.messagingengine.com (wfout2-smtp.messagingengine.com [64.147.123.145]) (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 E54A9335A5 for ; Mon, 29 Apr 2024 22:46:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.145 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714430786; cv=none; b=p7f9tbIKgnYxac56k6x8C1C8zQ6u2e96BCBVw3Vvfxx1+J+HwLfHQzaCpJS9pkLzpU3xSQwxF6AuVdpZwXXRxqj3qtabzOmim7MMtqjTxOg5vSryGNB9j83VUH9ff6wvWMzhkoL+C6KMIDEBACJ9Kb8GEcIk2e7epO+JALSQTrc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714430786; c=relaxed/simple; bh=KZb3ylmHkZRjfNhPtnDMwCRusC/Necha6l3BTr6fzAo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q9F4lEZCIXA4BX5PlEjGn+nO2k0YXDOtC2FOSqAUDs1SLznZ2qzGHBcRq2Z0G5j5r+f3WmIblQHGYa25XwiurCsoMWD7uTWcPEdHnYOatQ5aFKWe9kyZPemWEpuZLpn8HRFiyOb3fVIfbCZpJMov1cD0WYXycdEsnyK8YENGn/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz; spf=pass smtp.mailfrom=dxuuu.xyz; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b=1qylGMZN; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=AsnX6Z9X; arc=none smtp.client-ip=64.147.123.145 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="1qylGMZN"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="AsnX6Z9X" Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.west.internal (Postfix) with ESMTP id D523E1C0017A; Mon, 29 Apr 2024 18:46:23 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 29 Apr 2024 18:46:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1714430783; x= 1714517183; bh=vt11BUjZiHiL6ZazwmCpcxk1cFrD+6KlVBb6TnSg2uY=; b=1 qylGMZNP0QRJ3QRgquCsJLiFMw+6cpX0lQaSwoDInUwM0buJTfgqJGm5rvKpax5u 5nMPODUIGmTOSCaZ9NE7/3AznUUu7e17/Nqlv8O5l/cWSPGrmBAPJOwAk7eRPVrR kpc5JEiaVT26qLtybLA8zwiFIP+ym+bfhzFCCdoRNCzqwuP1toz+lZNrBjhf0FGD 7OGDrW4NSuHU5VuhooqTBEGIJC0AxU5vytFXMOpgwwZm69bDb1pp5E64ppGveIAP agGBu+Amd2TPvB4nwk2Z26XUTrycZhOAVTJGAZbMSWk/MYh4WVwA76M0uw+k6coc 4Cs1E+3otQ6rgwShSa1Dg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1714430783; x= 1714517183; bh=vt11BUjZiHiL6ZazwmCpcxk1cFrD+6KlVBb6TnSg2uY=; b=A snX6Z9Xe1/uHV1fQrUjm3s8J24wsmt+VO8pm7YflNBHSnys6dE/br1Skp8MpizwY hEdIZsEK4eUEUyBJtVEYth3xiO0uKj4FEH6tbINXnpM4YtoI/ClkLtOP+zwhRzHQ Wq0dFP62t9UMRL4jewOmBwqGXIV+56NcJ43/Xx6e11QqIQl3mJB9cK/YZBRWN5x0 w3zPdYI23dZqBa4Hg0w/f00NlHcokP8gXPi/wEEk607uhH6ig14DitTx87vjaF5E tTgSPXH1SbRi2gY0ZAEn+nPxkJbzAwvDwPPOC/gKf8NTmBi/6+65530YAYP+ZdOT rQ9yD9dt6Xj2WlEyUCSug== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdduvddgudegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdlfeehmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 29 Apr 2024 18:46:22 -0400 (EDT) From: Daniel Xu To: acme@kernel.org, jolsa@kernel.org, quentin@isovalent.com, alan.maguire@oracle.com, eddyz87@gmail.com Cc: andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net, bpf@vger.kernel.org Subject: [PATCH dwarves v9 2/3] pahole: Add --btf_feature=decl_tag_kfuncs feature Date: Mon, 29 Apr 2024 16:45:59 -0600 Message-ID: <6d69d6dce917475ffe9c1bd7bc53358904f60915.1714430735.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a feature flag to guard tagging of kfuncs. The next commit will implement the actual tagging. Reviewed-by: Alan Maguire Signed-off-by: Daniel Xu --- btf_encoder.c | 2 ++ dwarves.h | 1 + man-pages/pahole.1 | 1 + pahole.c | 1 + 4 files changed, 5 insertions(+) diff --git a/btf_encoder.c b/btf_encoder.c index 5ffaf5d..f0ef20a 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -76,6 +76,7 @@ struct btf_encoder { verbose, force, gen_floats, + tag_kfuncs, is_rel; uint32_t array_index_id; struct { @@ -1661,6 +1662,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam encoder->force = conf_load->btf_encode_force; encoder->gen_floats = conf_load->btf_gen_floats; encoder->skip_encoding_vars = conf_load->skip_encoding_btf_vars; + encoder->tag_kfuncs = conf_load->btf_decl_tag_kfuncs; encoder->verbose = verbose; encoder->has_index_type = false; encoder->need_index_type = false; diff --git a/dwarves.h b/dwarves.h index dd35a4e..7d566b6 100644 --- a/dwarves.h +++ b/dwarves.h @@ -94,6 +94,7 @@ struct conf_load { bool btf_gen_floats; bool btf_encode_force; bool reproducible_build; + bool btf_decl_tag_kfuncs; uint8_t hashtable_bits; uint8_t max_hashtable_bits; uint16_t kabi_prefix_len; diff --git a/man-pages/pahole.1 b/man-pages/pahole.1 index e3c58e0..4769b58 100644 --- a/man-pages/pahole.1 +++ b/man-pages/pahole.1 @@ -308,6 +308,7 @@ Encode BTF using the specified feature list, or specify 'default' for all standa in some CUs and not others, or when the same function name has inconsistent BTF descriptions in different CUs. + decl_tag_kfuncs Inject a BTF_KIND_DECL_TAG for each discovered kfunc. .fi Supported non-standard features (not enabled for 'default') diff --git a/pahole.c b/pahole.c index 750b847..954498d 100644 --- a/pahole.c +++ b/pahole.c @@ -1289,6 +1289,7 @@ struct btf_feature { BTF_DEFAULT_FEATURE(enum64, skip_encoding_btf_enum64, true), BTF_DEFAULT_FEATURE(optimized_func, btf_gen_optimized, false), BTF_DEFAULT_FEATURE(consistent_func, skip_encoding_btf_inconsistent_proto, false), + BTF_DEFAULT_FEATURE(decl_tag_kfuncs, btf_decl_tag_kfuncs, false), BTF_NON_DEFAULT_FEATURE(reproducible_build, reproducible_build, false), }; From patchwork Mon Apr 29 22:46:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 13647849 X-Patchwork-Delegate: bpf@iogearbox.net Received: from wfhigh4-smtp.messagingengine.com (wfhigh4-smtp.messagingengine.com [64.147.123.155]) (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 8A4C218C1F for ; Mon, 29 Apr 2024 22:46:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.155 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714430789; cv=none; b=ps67/DJ0a8zSFOwNoV1l+p0fY5QctlX0sW2cKXQvp8IN3tcSXGJw8hvB4agPDaaCRzYAf4HfuU3pBOIXHmg+kL2wcntYqZGH+o+vLYFhBP/KDA6tBlprdes+hrcayj+jIxj2lZJHIU24xVzVaQGFnjFwety+i2Ii3dXxxjRgrTE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714430789; c=relaxed/simple; bh=VA1CmiFy4gKsCoOZM/fBiBUGSj7d/GaT8Wcfc8c98pw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qx85+v0EhBoA25YKIrkn5ly5p/tEeTDcfK64kz37jigEmElEbU3QzyCXl9Gq5QokfDPNDN7CNoQCuFhim2lwBDIZ+bKcrqb0EnGOXPGpT7hHkUFDsneAkjasQz4fpA6dTwWV52hP2ybdrpLWIcrj+JD1KNrr/LybbCYrODg5euo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz; spf=pass smtp.mailfrom=dxuuu.xyz; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b=JM1hvzsK; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=G1xl6sko; arc=none smtp.client-ip=64.147.123.155 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="JM1hvzsK"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="G1xl6sko" Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailfhigh.west.internal (Postfix) with ESMTP id 1ACD1180018D; Mon, 29 Apr 2024 18:46:26 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Mon, 29 Apr 2024 18:46:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1714430785; x= 1714517185; bh=iUxJ1AXeKAumBghb7XYqyLbgXhR3FVd2SdPo64+eEG0=; b=J M1hvzsKP/aqKGnMFWyKjC4hgBiM9AmPi1WcIXa1oumI+ZaNtuMPSvu2TcusmtGrN LIVpeBxcN1qXwbFPBWN3UHlRKtWBJwmHhoIXF3/2hKbC2A41VATGXW9x32wtsL9g 1rvmBqD1X4S2cMrsThvW3QibS3pNNBtoZdXkB+lxFkba1op0h6q4EI8l7vhipETD 1D3wZJ2efMMwFrnbPCHaNVmA9FDOCbzIO3X1X6nS0PV8IO8g+ya1xNqFSd+XBNeL 9XzHIX8KVOAZgfsuVxqkK9Og8ZNfSWMdMorPeVcgfo4bpz6m2HsdH0FYdtkYzXTQ sZKYbbS7YBKFpvRasTxmA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1714430785; x= 1714517185; bh=iUxJ1AXeKAumBghb7XYqyLbgXhR3FVd2SdPo64+eEG0=; b=G 1xl6skoxL5g6QjiZw2Lg1mUHzy4r6Qmz7Eypib8PQl6OS1K+l9v1haSSXU0PZvWK 8uvZ6tI6hsHUJS3S9HZYgxbwqMCMxPQmx3QsULvsbo7I3LTekZcPhmAN2LRVmPeu u75BJL5f/UGQ4FUDyVfrtZgGzQH/XE6ExAagL4Zf1z5AL6LMlKy/kFBZk3Zq/3qv r/oiAhgwWjbzSQ67/kubwgSCaqDkFjlOWmMb05x3YKCSSFT2OTzkDnG4HVUMGQCD /W5DzsYIoiZkqmVeZpUGmlP9QObMqd3AL/a5CwP11pftW2nkNL6SqpmJltIVqhPB egQhGbNNgwUz1qP7TeK0Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdduvddgudefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdlvdefmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 29 Apr 2024 18:46:24 -0400 (EDT) From: Daniel Xu To: acme@kernel.org, jolsa@kernel.org, quentin@isovalent.com, alan.maguire@oracle.com, eddyz87@gmail.com Cc: andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net, bpf@vger.kernel.org Subject: [PATCH dwarves v9 3/3] pahole: Inject kfunc decl tags into BTF Date: Mon, 29 Apr 2024 16:46:00 -0600 Message-ID: <26ec519a00aa47f25bc6b4c7e4e15e5191ba4d45.1714430735.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This commit teaches pahole to parse symbols in .BTF_ids section in vmlinux and discover exported kfuncs. Pahole then takes the list of kfuncs and injects a BTF_KIND_DECL_TAG for each kfunc. Example of encoding: $ bpftool btf dump file .tmp_vmlinux.btf | rg "DECL_TAG 'bpf_kfunc'" | wc -l 121 $ bpftool btf dump file .tmp_vmlinux.btf | rg 56337 [56337] FUNC 'bpf_ct_change_timeout' type_id=56336 linkage=static [127861] DECL_TAG 'bpf_kfunc' type_id=56337 component_idx=-1 This enables downstream users and tools to dynamically discover which kfuncs are available on a system by parsing vmlinux or module BTF, both available in /sys/kernel/btf. This feature is enabled with --btf_features=decl_tag,decl_tag_kfuncs. Acked-by: Jiri Olsa Tested-by: Jiri Olsa Reviewed-by: Alan Maguire Tested-by: Alan Maguire Signed-off-by: Daniel Xu Acked-by: Daniel Xu --- btf_encoder.c | 372 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 372 insertions(+) diff --git a/btf_encoder.c b/btf_encoder.c index f0ef20a..6cb0c8f 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -34,6 +34,21 @@ #include #define BTF_ENCODER_MAX_PROTO 512 +#define BTF_IDS_SECTION ".BTF_ids" +#define BTF_ID_FUNC_PFX "__BTF_ID__func__" +#define BTF_ID_SET8_PFX "__BTF_ID__set8__" +#define BTF_SET8_KFUNCS (1 << 0) +#define BTF_KFUNC_TYPE_TAG "bpf_kfunc" + +/* Adapted from include/linux/btf_ids.h */ +struct btf_id_set8 { + uint32_t cnt; + uint32_t flags; + struct { + uint32_t id; + uint32_t flags; + } pairs[]; +}; /* state used to do later encoding of saved functions */ struct btf_encoder_state { @@ -76,6 +91,7 @@ struct btf_encoder { verbose, force, gen_floats, + skip_encoding_decl_tag, tag_kfuncs, is_rel; uint32_t array_index_id; @@ -95,6 +111,17 @@ struct btf_encoder { } functions; }; +struct btf_func { + const char *name; + int type_id; +}; + +/* Half open interval representing range of addresses containing kfuncs */ +struct btf_kfunc_set_range { + uint64_t start; + uint64_t end; +}; + static LIST_HEAD(encoders); static pthread_mutex_t encoders__lock = PTHREAD_MUTEX_INITIALIZER; @@ -1364,8 +1391,343 @@ out: return err; } +/* Returns if `sym` points to a kfunc set */ +static int is_sym_kfunc_set(GElf_Sym *sym, const char *name, Elf_Data *idlist, size_t idlist_addr) +{ + void *ptr = idlist->d_buf; + struct btf_id_set8 *set; + int off; + + /* kfuncs are only found in BTF_SET8's */ + if (!strstarts(name, BTF_ID_SET8_PFX)) + return false; + + off = sym->st_value - idlist_addr; + if (off >= idlist->d_size) { + fprintf(stderr, "%s: symbol '%s' out of bounds\n", __func__, name); + return false; + } + + /* Check the set8 flags to see if it was marked as kfunc */ + set = ptr + off; + return set->flags & BTF_SET8_KFUNCS; +} + +/* + * Parse BTF_ID symbol and return the func name. + * + * Returns: + * Caller-owned string containing func name if successful. + * NULL if !func or on error. + */ +static char *get_func_name(const char *sym) +{ + char *func, *end; + + /* Example input: __BTF_ID__func__vfs_close__1 + * + * The goal is to strip the prefix and suffix such that we only + * return vfs_close. + */ + + if (!strstarts(sym, BTF_ID_FUNC_PFX)) + return NULL; + + /* Strip prefix and handle malformed input such as __BTF_ID__func___ */ + func = strdup(sym + sizeof(BTF_ID_FUNC_PFX) - 1); + if (!strstr(func, "__")) { + free(func); + return NULL; + } + + /* Strip suffix */ + end = strrchr(func, '_'); + if (!end || *(end - 1) != '_') { + free(func); + return NULL; + } + *(end - 1) = '\0'; + + return func; +} + +static int btf_func_cmp(const void *_a, const void *_b) +{ + const struct btf_func *a = _a; + const struct btf_func *b = _b; + + return strcmp(a->name, b->name); +} + +/* + * Collects all functions described in BTF. + * Returns non-zero on error. + */ +static int btf_encoder__collect_btf_funcs(struct btf_encoder *encoder, struct gobuffer *funcs) +{ + struct btf *btf = encoder->btf; + int nr_types, type_id; + int err = -1; + + /* First collect all the func entries into an array */ + nr_types = btf__type_cnt(btf); + for (type_id = 1; type_id < nr_types; type_id++) { + const struct btf_type *type; + struct btf_func func = {}; + const char *name; + + type = btf__type_by_id(btf, type_id); + if (!type) { + fprintf(stderr, "%s: malformed BTF, can't resolve type for ID %d\n", + __func__, type_id); + err = -EINVAL; + goto out; + } + + if (!btf_is_func(type)) + continue; + + name = btf__name_by_offset(btf, type->name_off); + if (!name) { + fprintf(stderr, "%s: malformed BTF, can't resolve name for ID %d\n", + __func__, type_id); + err = -EINVAL; + goto out; + } + + func.name = name; + func.type_id = type_id; + err = gobuffer__add(funcs, &func, sizeof(func)); + if (err < 0) + goto out; + } + + /* Now that we've collected funcs, sort them by name */ + gobuffer__sort(funcs, sizeof(struct btf_func), btf_func_cmp); + + err = 0; +out: + return err; +} + +static int btf_encoder__tag_kfunc(struct btf_encoder *encoder, struct gobuffer *funcs, const char *kfunc) +{ + struct btf_func key = { .name = kfunc }; + struct btf *btf = encoder->btf; + struct btf_func *target; + const void *base; + unsigned int cnt; + int err = -1; + + base = gobuffer__entries(funcs); + cnt = gobuffer__nr_entries(funcs); + target = bsearch(&key, base, cnt, sizeof(key), btf_func_cmp); + if (!target) { + fprintf(stderr, "%s: failed to find kfunc '%s' in BTF\n", __func__, kfunc); + goto out; + } + + /* Note we are unconditionally adding the btf_decl_tag even + * though vmlinux may already contain btf_decl_tags for kfuncs. + * We are ok to do this b/c we will later btf__dedup() to remove + * any duplicates. + */ + err = btf__add_decl_tag(btf, BTF_KFUNC_TYPE_TAG, target->type_id, -1); + if (err < 0) { + fprintf(stderr, "%s: failed to insert kfunc decl tag for '%s': %d\n", + __func__, kfunc, err); + goto out; + } + + err = 0; +out: + return err; +} + +static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) +{ + const char *filename = encoder->source_filename; + struct gobuffer btf_kfunc_ranges = {}; + struct gobuffer btf_funcs = {}; + Elf_Data *symbols = NULL; + Elf_Data *idlist = NULL; + Elf_Scn *symscn = NULL; + int symbols_shndx = -1; + size_t idlist_addr = 0; + int fd = -1, err = -1; + int idlist_shndx = -1; + size_t strtabidx = 0; + Elf_Scn *scn = NULL; + Elf *elf = NULL; + GElf_Shdr shdr; + size_t strndx; + char *secname; + int nr_syms; + int i = 0; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "Cannot open %s\n", filename); + goto out; + } + + if (elf_version(EV_CURRENT) == EV_NONE) { + elf_error("Cannot set libelf version"); + goto out; + } + + elf = elf_begin(fd, ELF_C_READ, NULL); + if (elf == NULL) { + elf_error("Cannot update ELF file"); + goto out; + } + + /* Locate symbol table and .BTF_ids sections */ + if (elf_getshdrstrndx(elf, &strndx) < 0) + goto out; + + while ((scn = elf_nextscn(elf, scn)) != NULL) { + Elf_Data *data; + + i++; + if (!gelf_getshdr(scn, &shdr)) { + elf_error("Failed to get ELF section(%d) hdr", i); + goto out; + } + + secname = elf_strptr(elf, strndx, shdr.sh_name); + if (!secname) { + elf_error("Failed to get ELF section(%d) hdr name", i); + goto out; + } + + data = elf_getdata(scn, 0); + if (!data) { + elf_error("Failed to get ELF section(%d) data", i); + goto out; + } + + if (shdr.sh_type == SHT_SYMTAB) { + symbols_shndx = i; + symscn = scn; + symbols = data; + strtabidx = shdr.sh_link; + } else if (!strcmp(secname, BTF_IDS_SECTION)) { + idlist_shndx = i; + idlist_addr = shdr.sh_addr; + idlist = data; + } + } + + /* Cannot resolve symbol or .BTF_ids sections. Nothing to do. */ + if (symbols_shndx == -1 || idlist_shndx == -1) { + err = 0; + goto out; + } + + if (!gelf_getshdr(symscn, &shdr)) { + elf_error("Failed to get ELF symbol table header"); + goto out; + } + nr_syms = shdr.sh_size / shdr.sh_entsize; + + err = btf_encoder__collect_btf_funcs(encoder, &btf_funcs); + if (err) { + fprintf(stderr, "%s: failed to collect BTF funcs\n", __func__); + goto out; + } + + /* First collect all kfunc set ranges. + * + * Note we choose not to sort these ranges and accept a linear + * search when doing lookups. Reasoning is that the number of + * sets is ~O(100) and not worth the additional code to optimize. + */ + for (i = 0; i < nr_syms; i++) { + struct btf_kfunc_set_range range = {}; + const char *name; + GElf_Sym sym; + + if (!gelf_getsym(symbols, i, &sym)) { + elf_error("Failed to get ELF symbol(%d)", i); + goto out; + } + + if (sym.st_shndx != idlist_shndx) + continue; + + name = elf_strptr(elf, strtabidx, sym.st_name); + if (!is_sym_kfunc_set(&sym, name, idlist, idlist_addr)) + continue; + + range.start = sym.st_value; + range.end = sym.st_value + sym.st_size; + gobuffer__add(&btf_kfunc_ranges, &range, sizeof(range)); + } + + /* Now inject BTF with kfunc decl tag for detected kfuncs */ + for (i = 0; i < nr_syms; i++) { + const struct btf_kfunc_set_range *ranges; + unsigned int ranges_cnt; + char *func, *name; + GElf_Sym sym; + bool found; + int err; + int j; + + if (!gelf_getsym(symbols, i, &sym)) { + elf_error("Failed to get ELF symbol(%d)", i); + goto out; + } + + if (sym.st_shndx != idlist_shndx) + continue; + + name = elf_strptr(elf, strtabidx, sym.st_name); + func = get_func_name(name); + if (!func) + continue; + + /* Check if function belongs to a kfunc set */ + ranges = gobuffer__entries(&btf_kfunc_ranges); + ranges_cnt = gobuffer__nr_entries(&btf_kfunc_ranges); + found = false; + for (j = 0; j < ranges_cnt; j++) { + size_t addr = sym.st_value; + + if (ranges[j].start <= addr && addr < ranges[j].end) { + found = true; + break; + } + } + if (!found) { + free(func); + continue; + } + + err = btf_encoder__tag_kfunc(encoder, &btf_funcs, func); + if (err) { + fprintf(stderr, "%s: failed to tag kfunc '%s'\n", __func__, func); + free(func); + goto out; + } + free(func); + } + + err = 0; +out: + __gobuffer__delete(&btf_funcs); + __gobuffer__delete(&btf_kfunc_ranges); + if (elf) + elf_end(elf); + if (fd != -1) + close(fd); + return err; +} + int btf_encoder__encode(struct btf_encoder *encoder) { + bool should_tag_kfuncs; int err; /* for single-threaded case, saved funcs are added here */ @@ -1378,6 +1740,15 @@ int btf_encoder__encode(struct btf_encoder *encoder) if (btf__type_cnt(encoder->btf) == 1) return 0; + /* Note vmlinux may already contain btf_decl_tag's for kfuncs. So + * take care to call this before btf_dedup(). + */ + should_tag_kfuncs = encoder->tag_kfuncs && !encoder->skip_encoding_decl_tag; + if (should_tag_kfuncs && btf_encoder__tag_kfuncs(encoder)) { + fprintf(stderr, "%s: failed to tag kfuncs!\n", __func__); + return -1; + } + if (btf__dedup(encoder->btf, NULL)) { fprintf(stderr, "%s: btf__dedup failed!\n", __func__); return -1; @@ -1662,6 +2033,7 @@ struct btf_encoder *btf_encoder__new(struct cu *cu, const char *detached_filenam encoder->force = conf_load->btf_encode_force; encoder->gen_floats = conf_load->btf_gen_floats; encoder->skip_encoding_vars = conf_load->skip_encoding_btf_vars; + encoder->skip_encoding_decl_tag = conf_load->skip_encoding_btf_decl_tag; encoder->tag_kfuncs = conf_load->btf_decl_tag_kfuncs; encoder->verbose = verbose; encoder->has_index_type = false;