From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from www62.your-server.de (www62.your-server.de [213.133.104.62]) (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 0A53F1FFC48; Mon, 7 Jul 2025 13:09:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.133.104.62 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751893757; cv=none; b=afFiJnJ37nRn5sr3WEMBGeV64fRVt5WT0rrf/mRGXJtPko1BMmbsE4zerG8ujgUef9Zw7GbuFmzOFYktKQ9ILC99/a620R2xJ5WUjuRk/dRkUy/D9clpyio6LJ02f6diXw2SCKQTg44qw7p4hgyMlKNpzUwIS0/TvPiSJ2YxLeI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751893757; c=relaxed/simple; bh=YeMOVCglBU8p164rswsGlI71AdJFCu0J+zIZHooKAUo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=STEP0OQFbAVxyF0gvIBWluyz/TWgP3B3zU0PlqdMXic16XChoNhFBTAoSEToR1uNu2Kr0lsNvOtJ0EVEopx31TG76hNMtCJ9k1AjSW5+epLqcmDJtNeiZeC9r7VaUc0meR2MWWgJy0LPjkQyc5A78lccat66vdYz+aRLd2Lx6JM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=iogearbox.net; spf=pass smtp.mailfrom=iogearbox.net; dkim=pass (2048-bit key) header.d=iogearbox.net header.i=@iogearbox.net header.b=leCL277o; arc=none smtp.client-ip=213.133.104.62 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=iogearbox.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iogearbox.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=iogearbox.net header.i=@iogearbox.net header.b="leCL277o" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=iogearbox.net; s=default2302; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=aYWQg/UvnmCmXtVe8KM5L6hltd/YwWA4VEQIY1VMyHI=; b=leCL277oy7Tss29U5TzSSnKppN nMVoBAqlEy2uBPr4koxnqDWHqANIaLmxz4hfr45k6RCxRsuZCrGh8b8vynOyJhIQaFHUoLvu3CIao fElvV8XElTMiLSze01B/QuWxEVb6d655KEh2soKuT/FAm8jHXTQPb4QPs1gPhze6TAIE2oylfaVmb 40meVabdff3yP1t48ObR0RJ22XQwj4PhB6toW42jvJRxKQXn8I9UedrxT8UowYitPdAg98ImL3j6t Pp7iEZ4CWdCxE3jR57ddF7hM6Zmq7sI9R1g5vagShwUch2MpCvvONfbitv5sZJP/qImqqhLsGF+5H nJxd1W8A==; Received: from sslproxy07.your-server.de ([78.47.199.104]) by www62.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1uYlai-000Iwg-0k; Mon, 07 Jul 2025 15:09:04 +0200 Received: from localhost ([127.0.0.1]) by sslproxy07.your-server.de with esmtpsa (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uYlah-0008Pz-0R; Mon, 07 Jul 2025 15:09:03 +0200 Message-ID: Date: Mon, 7 Jul 2025 15:09:02 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf-next] bpf: make the attach target more accurate To: Menglong Dong , ast@kernel.org Cc: john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Menglong Dong , alan.maguire@oracle.com References: <20250707113528.378303-1-dongml2@chinatelecom.cn> Content-Language: en-US From: Daniel Borkmann Autocrypt: addr=daniel@iogearbox.net; keydata= xsFNBGNAkI0BEADiPFmKwpD3+vG5nsOznvJgrxUPJhFE46hARXWYbCxLxpbf2nehmtgnYpAN 2HY+OJmdspBntWzGX8lnXF6eFUYLOoQpugoJHbehn9c0Dcictj8tc28MGMzxh4aK02H99KA8 VaRBIDhmR7NJxLWAg9PgneTFzl2lRnycv8vSzj35L+W6XT7wDKoV4KtMr3Szu3g68OBbp1TV HbJH8qe2rl2QKOkysTFRXgpu/haWGs1BPpzKH/ua59+lVQt3ZupePpmzBEkevJK3iwR95TYF 06Ltpw9ArW/g3KF0kFUQkGXYXe/icyzHrH1Yxqar/hsJhYImqoGRSKs1VLA5WkRI6KebfpJ+ RK7Jxrt02AxZkivjAdIifFvarPPu0ydxxDAmgCq5mYJ5I/+BY0DdCAaZezKQvKw+RUEvXmbL 94IfAwTFA1RAAuZw3Rz5SNVz7p4FzD54G4pWr3mUv7l6dV7W5DnnuohG1x6qCp+/3O619R26 1a7Zh2HlrcNZfUmUUcpaRPP7sPkBBLhJfqjUzc2oHRNpK/1mQ/+mD9CjVFNz9OAGD0xFzNUo yOFu/N8EQfYD9lwntxM0dl+QPjYsH81H6zw6ofq+jVKcEMI/JAgFMU0EnxrtQKH7WXxhO4hx 3DFM7Ui90hbExlFrXELyl/ahlll8gfrXY2cevtQsoJDvQLbv7QARAQABzSZEYW5pZWwgQm9y a21hbm4gPGRhbmllbEBpb2dlYXJib3gubmV0PsLBkQQTAQoAOxYhBCrUdtCTcZyapV2h+93z cY/jfzlXBQJjQJCNAhsDBQkHhM4ACAsJCAcNDAsKBRU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GPb3jSLzucMAFXgoI8rVqoq6si2sxr2l0VsNH5o3NgoAgJNIg= In-Reply-To: <20250707113528.378303-1-dongml2@chinatelecom.cn> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Clear (ClamAV 1.0.7/27692/Mon Jul 7 10:35:53 2025) On 7/7/25 1:35 PM, Menglong Dong wrote: > For now, we lookup the address of the attach target in > bpf_check_attach_target() with find_kallsyms_symbol_value or > kallsyms_lookup_name, which is not accurate in some cases. > > For example, we want to attach to the target "t_next", but there are > multiple symbols with the name "t_next" exist in the kallsyms. The one > that kallsyms_lookup_name() returned may have no ftrace record, which > makes the attach target not available. So we want the one that has ftrace > record to be returned. > > Meanwhile, there may be multiple symbols with the name "t_next" in ftrace > record. In this case, the attach target is ambiguous, so the attach should > fail. > > Introduce the function bpf_lookup_attach_addr() to do the address lookup, > which is able to solve this problem. > > Signed-off-by: Menglong Dong Breaks CI, see also: First test_progs failure (test_progs-aarch64-gcc-14): #467/1 tracing_failure/bpf_spin_lock test_bpf_spin_lock:PASS:tracing_failure__open 0 nsec libbpf: prog 'test_spin_lock': BPF program load failed: -ENOENT libbpf: prog 'test_spin_lock': -- BEGIN PROG LOAD LOG -- The address of function bpf_spin_lock cannot be found processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 -- END PROG LOAD LOG -- libbpf: prog 'test_spin_lock': failed to load: -ENOENT libbpf: failed to load object 'tracing_failure' libbpf: failed to load BPF skeleton 'tracing_failure': -ENOENT test_bpf_spin_lock:FAIL:tracing_failure__load unexpected error: -2 (errno 2) #467/2 tracing_failure/bpf_spin_unlock test_bpf_spin_lock:PASS:tracing_failure__open 0 nsec libbpf: prog 'test_spin_unlock': BPF program load failed: -ENOENT libbpf: prog 'test_spin_unlock': -- BEGIN PROG LOAD LOG -- The address of function bpf_spin_unlock cannot be found processed 0 insns (limit 1000000) max_states_per_insn 0 total_states 0 peak_states 0 mark_read 0 -- END PROG LOAD LOG -- libbpf: prog 'test_spin_unlock': failed to load: -ENOENT libbpf: failed to load object 'tracing_failure' libbpf: failed to load BPF skeleton 'tracing_failure': -ENOENT test_bpf_spin_lock:FAIL:tracing_failure__load unexpected error: -2 (errno 2) > kernel/bpf/verifier.c | 76 ++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 71 insertions(+), 5 deletions(-) > > diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c > index 0f6cc2275695..9a7128da6d13 100644 > --- a/kernel/bpf/verifier.c > +++ b/kernel/bpf/verifier.c > @@ -23436,6 +23436,72 @@ static int check_non_sleepable_error_inject(u32 btf_id) > return btf_id_set_contains(&btf_non_sleepable_error_inject, btf_id); > } > > +struct symbol_lookup_ctx { > + const char *name; > + unsigned long addr; > +}; > + > +static int symbol_callback(void *data, unsigned long addr) > +{ > + struct symbol_lookup_ctx *ctx = data; > + > + if (!ftrace_location(addr)) > + return 0; > + > + if (ctx->addr) > + return -EADDRNOTAVAIL; > + > + ctx->addr = addr; > + > + return 0; > +} > + > +static int symbol_mod_callback(void *data, const char *name, unsigned long addr) > +{ > + if (strcmp(((struct symbol_lookup_ctx *)data)->name, name) != 0) > + return 0; > + > + return symbol_callback(data, addr); > +} > + > +/** > + * bpf_lookup_attach_addr: Lookup address for a symbol > + * > + * @mod: kernel module to lookup the symbol, NULL means to lookup the kernel > + * symbols > + * @sym: the symbol to resolve > + * @addr: pointer to store the result > + * > + * Lookup the address of the symbol @sym, and the address should has > + * corresponding ftrace location. If multiple symbols with the name @sym > + * exist, the one that has ftrace location will be returned. If more than > + * 1 has ftrace location, -EADDRNOTAVAIL will be returned. > + * > + * Returns: 0 on success, -errno otherwise. > + */ > +static int bpf_lookup_attach_addr(const struct module *mod, const char *sym, > + unsigned long *addr) > +{ > + struct symbol_lookup_ctx ctx = { .addr = 0, .name = sym }; > + int err; > + > + if (!mod) > + err = kallsyms_on_each_match_symbol(symbol_callback, sym, &ctx); This is also not really equivalent to kallsyms_lookup_name(). kallsyms_on_each_match_symbol() only iterates over all symbols in vmlinux whereas kallsyms_lookup_name() looks up both vmlinux and modules. > + else > + err = module_kallsyms_on_each_symbol(mod->name, symbol_mod_callback, > + &ctx); > + > + if (!ctx.addr) > + return -ENOENT; > + > + if (err) > + return err; > + > + *addr = ctx.addr; > + > + return 0; > +} > + > int bpf_check_attach_target(struct bpf_verifier_log *log, > const struct bpf_prog *prog, > const struct bpf_prog *tgt_prog, > @@ -23689,18 +23755,18 @@ int bpf_check_attach_target(struct bpf_verifier_log *log, > if (btf_is_module(btf)) { > mod = btf_try_get_module(btf); > if (mod) > - addr = find_kallsyms_symbol_value(mod, tname); > + ret = bpf_lookup_attach_addr(mod, tname, &addr); > else > - addr = 0; > + ret = -ENOENT; > } else { > - addr = kallsyms_lookup_name(tname); > + ret = bpf_lookup_attach_addr(NULL, tname, &addr); > } > - if (!addr) { > + if (ret) { > module_put(mod); > bpf_log(log, > "The address of function %s cannot be found\n", > tname); > - return -ENOENT; > + return ret; > } > } >