From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.236 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793693937; b=dITUyVgPEaH1w6yCT1HysZb054f8N081dGnRLQhQkHbcCS7GCa/XnY0rcshcLyUTsxXW iwVl0eHXx2g9yjppdxneAa2mAR0YMt+bypMYV5P/xs/Sr9U4U8jzY8mVRaKn0bNheZqlq dwVderHAJUcaC+bf/BhZauCU0x0+1LxWhlIgKUnSjFtkSD/S+jx8Cyz/W2PT6LSU0JP+o XWDwBKwfcA6rbfdBfXJZgQK3S+Oi3r6viW9/s0iCGTZSh4l9A27Y5ORosjiFZqh4+4K1i urxcgbkJpWeLujgAhwicC91YCmdHCTthkdwlEZPJWG7p6XtbWqTw8nBVX0eubBclfEA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793693937; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=q9qNvJLwCf7x1Jx3yj2W8Xx3AQDRF1mMWWisAr7uXyI=; b=UqvlzXOVYUWkuFnkEVABRxxwNOHkU53P0PufgItDmX/gogivAhkyl4QbZtSLim2Bq0ns UiLHe7WwKTVaRhNVaAGxqwuTYxpl+dsd6pxLeGMjOXiRgcM3qK5zXmQLJNBr/qvgnljg4 36T5t9D+OBqTPiReIfhA+Twmul2R32m8yAFBN/T48hJWqDER0vveG+Rg8rERN4IuzQ2mf SVPdkkbfTCn9M6KaPaYBrsPmLVaC5GtdyC5kb2j6QViWW70UOmQ8nn8oRJdzDk3dFjVJr 03EJ28CP4V1VW2zHb7SZjFnwA40Xz2m+HIJ2HOQ8zlRHkXdHXllg6zb90HlBF8e/zxg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=UzxEGwEX; arc=none smtp.remote-ip=91.218.175.236 Received: from mta0.migadu.com (out-236.mta0.migadu.com [91.218.175.236]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 014DF606A324 for ; Tue, 3 Nov 2026 08:18:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 014DF606A324 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=UzxEGwEX X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=8fFpOJ/BSEAomJCGeqs5M8Cl6EgDVyeQJlq5nmvCZ3I=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793693934; v=1; x=1794298734; b=UzxEGwEXoIKDUSf9lQ/R9fC8OypuUw5rx0KAZjmgkJO2LfE9KgvGVJzSEFFcwWHoXfDSIrzn GfqxI1FFUtHaWX6rGiWwEbTGxisj2k7DvAh6viVd+EWfpxSq/+luHPzCYx8rmWr17IVYcrGhPkU pM2/bjAQPC+EWnsZ955p4l4U= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 129aeac7966a988c; Tue, 03 Nov 2026 08:18:53 +0000 X-Mizu-Trace-ID: 129aeac7966a988c X-Migadu-Flow: FLOW_OUT From: Jiayuan Chen To: bpf@vger.kernel.org Cc: Jiayuan Chen , =?UTF-8?q?=E6=B4=9B?= , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , Alan Maguire , linux-kernel@vger.kernel.org Subject: [PATCH bpf-next v2] libbpf: Fix stale raw data after btf__relocate() Date: Tue, 3 Nov 2026 16:18:35 +0800 Message-ID: <20261103081836.381430-1-jiayuan.chen@linux.dev> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit btf_relocate() rewrites type ids in place but doesn't drop the cached raw_data, so btf__raw_data() after btf__relocate() returns the old bytes with pre-relocation ids. Only modifiable BTF keeps raw_data as a cache. For read-only BTF, e.g. module BTF relocated in btf_parse_elf(), types_data points into raw_data and is already rewritten in place, so it must not be freed. Make btf_invalidate_raw_data() tell the two apart and call it from btf__relocate(). Also recompute named_start_id since start_id changes after relocation, like the kernel does. Fixes: 19e00c897d50 ("libbpf: Split BTF relocation") Reported-by: 洛 Closes: https://lore.kernel.org/bpf/455f7257-25b9-4070-8313-42af8005b948@linux.dev/T/#t Signed-off-by: Jiayuan Chen --- v2: - Free raw_data only for modifiable BTF and call btf_invalidate_raw_data() from btf__relocate() instead of btf_ensure_modifiable(), per Alexei - Recompute named_start_id after relocation - Drop the selftest since it is contrived, as Alexei pointed out - Target bpf-next v1: https://lore.kernel.org/bpf/20261101155615.324501-1-jiayuan.chen@linux.dev/ --- tools/lib/bpf/btf.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index d2ff6f5d6d5b..d67f78d76750 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -2033,7 +2033,8 @@ struct btf *btf__load_from_kernel_by_id(__u32 id) static void btf_invalidate_raw_data(struct btf *btf) { - if (btf->raw_data) + /* types_data and strs_data of read-only BTF point into raw_data */ + if (btf->raw_data && btf_is_modifiable(btf)) btf_free_raw_data(btf); if (btf->raw_data_swapped) { free(btf->raw_data_swapped); @@ -2099,11 +2100,11 @@ static int btf_ensure_modifiable(struct btf *btf) if (!btf->base_btf && btf->hdr.str_len == 1) btf->strs_deduped = true; + btf->modifiable = true; + /* invalidate raw_data representation */ btf_invalidate_raw_data(btf); - btf->modifiable = true; - return 0; err_out: @@ -6642,8 +6643,12 @@ int btf__relocate(struct btf *btf, const struct btf *base_btf) { int err = btf_relocate(btf, base_btf, NULL, NULL); - if (!err) + /* types are rewritten in place, some of them even on error */ + btf_invalidate_raw_data(btf); + if (!err) { btf->owns_base = false; + btf_check_sorted(btf); + } return libbpf_err(err); } -- 2.43.0