Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp327589lqz; Fri, 29 Mar 2024 20:35:32 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCViu6hyYyFr9jfDidaGzmChBmZEjW3P6uTqAQgvlBAG65IHyolnU2Plejad1LRpiVVAq5uuKn2ivBlhDU2+sjEEfF+p2y+r+LOMUiozSQ== X-Google-Smtp-Source: AGHT+IERdeGNB9PvR/LOUhyJYl/jb7/3q4b88tlLxRtkRcCIeJK77N2pvVkyZWLqwZ4uVM9eJbDj X-Received: by 2002:a05:6a21:3384:b0:1a3:c407:39d8 with SMTP id yy4-20020a056a21338400b001a3c40739d8mr4499061pzb.45.1711769732295; Fri, 29 Mar 2024 20:35:32 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711769732; cv=pass; d=google.com; s=arc-20160816; b=UB12ZVd4ngGQmjamvQvp/JKXoH0lzR8zSPfgAo1k7s5QMTnOpuhQOpF81JxPjiRx/q 5TY6pcAPYvHTzPMqdIt5OcQb+HsyKAOOmxjAAb7lA8EYblw6vpg+FhlA9D16BA81ds6S e/DZWWzlrZdHUuu4IhYS/Mr0bl1I2/ibEy3DcRwpfVRySaPC3cbVjTW164Fs4Bbxocm3 1PRuu+ktl4o2/I0KxBKOvZ4rk48hIClXuMzL8HxaDTV3fAjuLOc+SVpOOzMWBeEGohZI U2LsjVtOz5OYaQ91Vh+kA9aNJyynPvMjGcdxcBxJespnAMud/V7wdhL7ZF8Oq7nOq8eL OQsQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=nQY9GynfzjdgI1A44B4TVgDxB7Pe9LKkrSIu40Cboqk=; fh=w/2wnV1B49PwhZ15qI9KJWTeAZnq87gyMq3nDlQWbXU=; b=pM6qbbiUERySYAsXSParISIh8V6VGtW1fQ4K5HtxBw0bcsXRF5DHndkH/V0dys2gJn VDRCiZ3gGoOt5N3oPd8vicL6qMZ8Z38pfEroeiTh3MKHy5C3rRk6zc1FMa41J7SazvR2 8zr4L3vYYdX0w4LSLc57KFZBSvzX7NGbFyPk27QOvK2BFHE6a/wyGWxA9/Q+K4zXqQp2 8++5iMki3BId8ZhDyhez5BOU7XsEtNtFLNYQr7W1aKGNwkjNRUEWjG7wBmsrh5AyL9nz EynzDFCqjyxj9Ovl/9h1Vy2NG2Ddyz3ma+tvp71r52+xwLwYUzrXBPIWOk4S5Pm8gEDy CVkA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b="KuELMr/f"; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-125584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125584-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id c19-20020a17090a8d1300b002a1f7facf5esi4951980pjo.187.2024.03.29.20.35.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 20:35:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-125584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b="KuELMr/f"; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-125584-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125584-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id EA15C28311C for ; Sat, 30 Mar 2024 03:35:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DF4F58827; Sat, 30 Mar 2024 03:35:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="KuELMr/f" Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C7D56FB9 for ; Sat, 30 Mar 2024 03:35:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711769718; cv=none; b=ayPxamHwD1+HsdeLToejREDfykph13Fno+Mma1mPxqPLp6kLUUiO01E+H4W7pbIJBWMEaCrZA7mzlILBE+7PE5yu/lUXTzQvAxDZux0W94lPm19hoV0o/TxCrzYyIsCmJSsltmOSyAV0K2kLXyoPz9O2Rtg3B5DZFpYKGNsH38s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711769718; c=relaxed/simple; bh=VpC0J23Jn0yxmT2wv6lawJLz4zGJD1QUQuuuKn67A+c=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=oUD2cIvvZEt+1lu8OPyf8CsUNYkh5PAq8FoSMCuT1Y9WJO7xPi2adsi4JMABeTX4dlLeWoHR/PbjxoZim3eKMRwtMDPmMnj8+LN5bmslFn4miQa4f0eGz7o6UoVJd3b76qQIDU36ePYO7dymIiJ0iK3BqTPkkfJM4uVjX2dTU4o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=KuELMr/f; arc=none smtp.client-ip=209.85.216.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-29c7512e3b8so1998479a91.1 for ; Fri, 29 Mar 2024 20:35:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711769716; x=1712374516; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nQY9GynfzjdgI1A44B4TVgDxB7Pe9LKkrSIu40Cboqk=; b=KuELMr/fHUb3LuvaL1wmjrIgOwqE1VjwZmGWVIb/R4EP3rfDlpG9rREXcuMK40a7Zc AGRk8zjmzqoSMSqGWWwRERmKZgpxbGVcxU8j5fZZH3SPYt58Dlz5MdUCRSLk2q8IR+sa WzNm6wsAEdtmK3ukV19cG5+97BWl74qB/bct5O60IeFY9Re7csPxBasiMGBmPwLa06U8 TyuafFyvk0kZfhscsfXB0Kqortrcof2oQnv3Z01KhxyCLqfUnRVIJeB5VrfEA2a4l/Bh 0tUmbaUO4ddBxnh1VQQhO+7SPXee7HiSfR/s+/iFY79l1dBk3xGeFPzWxPkgl/b1fgjg dgdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711769716; x=1712374516; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nQY9GynfzjdgI1A44B4TVgDxB7Pe9LKkrSIu40Cboqk=; b=MvK3UHtceunTM7J13OsvHr0Y7nra21oyrhoCc7U/RGm+vtErd83KkUtYmwpQIFLKLl Z6x71mYZEvASAik9BXKfm2NQR7HSA48KXC1wUbFr0XPkxWhtxmse3+nQ/abPK/qvihLd 3+mL9JgCAEUZfnMw/FWKCt90LEOzySwhtWYXM1ZFon5NZkegpXOU2euwAa3tzUi+BZir Yu23FCui7XrfSnCQzsF59biQZbGlTLpHIbti9KP52zfxizBO6MTBscLreNGX6qUT5TFV toOxaiiuwoH5dubS1lNKo7TvA216d0F5fF0r4KWhcq/uFa9ekl4G5+sBg6aohV64XBzU AnQg== X-Forwarded-Encrypted: i=1; AJvYcCUMsCByLLoVHEJ3Q1izpmr8qkX6dvKEn0odCqyDpSWbNHzCt1QHqqEPZMyeT0PNd8VLkVgHxLsmJBA+0l+4XboaCB9vc2VCsXA6A9Ee X-Gm-Message-State: AOJu0YxDA9iRDp2+Qf+VeWO8zZ4xFsLYrBuMyPxFlLFNmZXjXp7b4x5C UztQ1Ov5CIyJi3xgMNXVKlU03jTe9Y7HqRarEB9/pqcd/heXPETOfa3LILXybbKoFtUd68u4TEw ibqlJPhv24u+GsLCrpUOTnf2F7GJlWDBXqWQ3gg== X-Received: by 2002:a17:90a:474f:b0:29d:fe1c:79e7 with SMTP id y15-20020a17090a474f00b0029dfe1c79e7mr3306403pjg.45.1711769716617; Fri, 29 Mar 2024 20:35:16 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240311093526.1010158-1-dongmenglong.8@bytedance.com> <20240311093526.1010158-2-dongmenglong.8@bytedance.com> <20240328111330.194dcbe5@gandalf.local.home> In-Reply-To: From: =?UTF-8?B?5qKm6b6Z6JGj?= Date: Sat, 30 Mar 2024 11:36:38 +0800 Message-ID: Subject: Re: [External] Re: [PATCH bpf-next v2 1/9] bpf: tracing: add support to record and check the accessed args To: Alexei Starovoitov Cc: Steven Rostedt , Jiri Olsa , Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "David S. Miller" , David Ahern , Dave Hansen , X86 ML , Mathieu Desnoyers , Quentin Monnet , bpf , linux-arm-kernel , LKML , linux-riscv , linux-s390 , Network Development , linux-trace-kernel@vger.kernel.org, "open list:KERNEL SELFTEST FRAMEWORK" , linux-stm32@st-md-mailman.stormreply.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, Mar 29, 2024 at 7:17=E2=80=AFAM Alexei Starovoitov wrote: > > On Thu, Mar 28, 2024 at 8:10=E2=80=AFAM Steven Rostedt wrote: > > > > On Thu, 28 Mar 2024 22:43:46 +0800 > > =E6=A2=A6=E9=BE=99=E8=91=A3 wrote: > > > > > I have done a simple benchmark on creating 1000 > > > trampolines. It is slow, quite slow, which consume up to > > > 60s. We can't do it this way. > > > > > > Now, I have a bad idea. How about we introduce > > > a "dynamic trampoline"? The basic logic of it can be: > > > > > > """ > > > save regs > > > bpfs =3D trampoline_lookup_ip(ip) > > > fentry =3D bpfs->fentries > > > while fentry: > > > fentry(ctx) > > > fentry =3D fentry->next > > > > > > call origin > > > save return value > > > > > > fexit =3D bpfs->fexits > > > while fexit: > > > fexit(ctx) > > > fexit =3D fexit->next > > > > > > xxxxxx > > > """ > > > > > > And we lookup the "bpfs" by the function ip in a hash map > > > in trampoline_lookup_ip. The type of "bpfs" is: > > > > > > struct bpf_array { > > > struct bpf_prog *fentries; > > > struct bpf_prog *fexits; > > > struct bpf_prog *modify_returns; > > > } > > > > > > When we need to attach the bpf progA to function A/B/C, > > > we only need to create the bpf_arrayA, bpf_arrayB, bpf_arrayC > > > and add the progA to them, and insert them to the hash map > > > "direct_call_bpfs", and attach the "dynamic trampoline" to > > > A/B/C. If bpf_arrayA exist, just add progA to the tail of > > > bpf_arrayA->fentries. When we need to attach progB to > > > B/C, just add progB to bpf_arrayB->fentries and > > > bpf_arrayB->fentries. > > > > > > Compared to the trampoline, extra overhead is introduced > > > by the hash lookuping. > > > > > > I have not begun to code yet, and I am not sure the overhead is > > > acceptable. Considering that we also need to do hash lookup > > > by the function in kprobe_multi, maybe the overhead is > > > acceptable? > > > > Sounds like you are just recreating the function management that ftrace > > has. It also can add thousands of trampolines very quickly, because it = does > > it in batches. It takes special synchronization steps to attach to fent= ry. > > ftrace (and I believe multi-kprobes) updates all the attachments for ea= ch > > step, so the synchronization needed is only done once. > > > > If you really want to have thousands of functions, why not just registe= r it > > with ftrace itself. It will give you the arguments via the ftrace_regs > > structure. Can't you just register a program as the callback? > > > > It will probably make your accounting much easier, and just let ftrace > > handle the fentry logic. That's what it was made to do. > > Absolutely agree. > There is no point re-inventing this logic. > > Menlong, > before you hook up into ftrace check whether > it's going to be any different from kprobe-multi, > since it's the same ftrace underneath. > I suspect it will look exactly the same. Yeah, I dig it a little. I think it is different. For multi-kprobe, it registers a ftrace_ops to ftrace_ops_list for every bpf program. This means that we can register 2 or more multi-kprobe in the same function. The bpf is called in the following step: ftrace_regs_caller | __ftrace_ops_list_func -> fprobe_handler -> kprobe_multi_link_handler -> ru= n BPF And for trampoline, it needs to be called directly, so it can't be registered as a callback to ftrace_ops_list. It need to be called in the following step: ftrace_regs_caller | __ftrace_ops_list_func -> call_direct_funcs -> save trampoline to pt_regs->origin_ax | call pt_regs->origin_ax if not NULL > So it sounds like multi-fentry idea will be shelved once again. Enn...this is the best solution that I can think of. If it doesn't work, I suspect it will be shelved again. Thanks! Menglong Dong