Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp336047lqz; Fri, 29 Mar 2024 21:15:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWKmznbQKbq86mztUtbEZHSqxlZL2K4loRQQm/qcJg/mAh4XhPyfdornNhG/eFzQ6uIVbZ7QG+iR8bZJTuSILqNXnRP0yrc9UIMSQl7wA== X-Google-Smtp-Source: AGHT+IEZK9j9JFq2Fa+dfgRn8E6F509sd6l0F3oEPgChSfEKTIHmIkcIyVKGARWGCb4ymu3U/5E0 X-Received: by 2002:a17:902:e543:b0:1e0:e8b7:1fa2 with SMTP id n3-20020a170902e54300b001e0e8b71fa2mr5476539plf.21.1711772156744; Fri, 29 Mar 2024 21:15:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711772156; cv=pass; d=google.com; s=arc-20160816; b=sbQlOm9Q+K9ET6SHSrqJgI12IYOEWSfoxSHiIfFAQi6eDjdq4bB0+iFpI/67MBSk6/ L0/XV8pWJmPxoTvHFbskpoIr9MoxzEb54SUNUwJ0dQflanON0k+IZaQ1g2dBDSh/ux8c wrg4UKtsvNeqSuX8avXc3iMsxHgp8I/GeNh0ubLSJHVgIAeortmuhxurwbzm0U5oJv5B xK5a0g9RinOzAy8swKj1ea7FcHwvrByqrhgXuVM+2tRWKZjOrGeVk05l11CPf7HbLI4/ aCHeIjsizh6M4XfPl9z1ZS1vVsUQAODHXTiyeurOXFhPIc6szYQnhL5ezsIYcfyJNGSX rpVg== 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=ofE4DiMnvHuiRZX1XlrIQJQuThx3U4684j2Vrwr4bOc=; fh=EvBSs1lFQ/DaROohYlR08UhT8WHiVKkgnRfqEzd8tFA=; b=d1BVWjNvk1E8+ribH+ncBHWiHUjm85bzsP8E13+HrRPvdOf1+rXIvVMFXKFc1YNkKe VGV/3yBGd/zHX8zLF9soPbUbEywz2xnfM4IcRZJvUMfoy+anH1LTkdgwZGxmEmjkjDRB 3S36nl9kuWJJMSGKsBo5hth2WlsObhDhqdS19UakiWBV3kqHb8r7WyU8r1L9mEYAm/RZ lCXmQCge3d+tocbNGD02iIWt9o6lz3VHzRNLa88ckktVjABmcln+m9HijfNWBJmbKm9g F9l32Bz7v+bQJcCApMv21gGTbGtK3kgjOiddeB2ABHWxVz5wu11zM+cFdw3IUwK9Vue1 H3Sg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=lotuHB3Y; 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-125597-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125597-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id c11-20020a170902b68b00b001e0892bb8d0si4582844pls.222.2024.03.29.21.15.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 21:15:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-125597-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=lotuHB3Y; 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-125597-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125597-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 0FAB9B22590 for ; Sat, 30 Mar 2024 04:15:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0C7CCB66B; Sat, 30 Mar 2024 04:15:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="lotuHB3Y" Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 09F2279E0 for ; Sat, 30 Mar 2024 04:15:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711772137; cv=none; b=ciOXiTqtFkQRaE/29gM6pMcwlirTT4NJF7hqPW8S4aIHGnx1rWkpKPauI8+d6GK5ioDxPpdnEES9HSGIFbFD1TJ7ZslsK5k7ScE6z8p2WI4v3b/Z5nHz+g7RI4w94+Rtfsz6SFjRZM3sjMOlEIsC/rW755xP+tqmI945UbX3S0c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711772137; c=relaxed/simple; bh=qgobxYsoW4+2TT3KspAAgbFZjvWwlDQJ1EahoxZ6SUc=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=ekMHrMAR2x/oVMtGV/zTMtk+3rkihZNIOhVGKhsgpBc0CYCZ47tVMlG98ByD9aXVMQt2MNsAZlb/Dxk0Q4+cj5fY/XqdMt6vKp6/8U325i9V1aOTEgRbjUFyLYP5QaxLdy+pk/lpNM1ls7/3nwpGP0D4XeAE6xZLuo4pCcoo8H0= 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=lotuHB3Y; arc=none smtp.client-ip=209.85.210.182 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-pf1-f182.google.com with SMTP id d2e1a72fcca58-6e6b729669bso2153346b3a.3 for ; Fri, 29 Mar 2024 21:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711772130; x=1712376930; 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=ofE4DiMnvHuiRZX1XlrIQJQuThx3U4684j2Vrwr4bOc=; b=lotuHB3YqxI5vNiMNQShMPcZpMVUJ465SPLPfA2ZffKWYhtiIpcCAVyZSTq4l0PlVm cPPfJ5K6RIbzdDn0T2br/TIueszyxszgWUTJ380vTjs5mQUui38KoyOVDnG1aNFg3ojX qT7UlrcsXggaNQXe9AuPEuT0Sb7F0MLi7Nw6l+TH1U0d7yiQ+7KnPDVXHegP8jKCK6Em Uow1+d0qBdtY/BbdJh5NtxxGHAY5F7ea4QmX/AcK49wR4lbjbtMz9p1a1106CIP7G/p2 F5jxnpW89ievSd3eMy5KK8/Cy/eyEh2UtRV9jEB1VoadAT58X+0FxQfYCzwBApOxWs6q CZyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711772130; x=1712376930; 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=ofE4DiMnvHuiRZX1XlrIQJQuThx3U4684j2Vrwr4bOc=; b=A/fbPEeU6JGKCOsgFyhGJttWX+wrLs+o5magw1LqBZlTiRHYZfyDHYL8R5BPPlIL1d WQcS/TWStTnwpc5p/trzlBbHU9YVoUBvQQB3+7xszjm1m0fSwtsdM+zBkFzM1ptkv9G6 aGonNkdmWKznhUoQ2xAdBwV9/ZG1W/aLcsSLw6bErfZLlwPFMSEiWVSRD/pHSUP+DycW lRVgotuJ18IIZ5DE7BnY+A5zUWgC7PBvDFpHX2R12jRo0RCmI0jYKrTNo1EUkQge1/0I Y4F+dSUDXEWB9/mXv53CoCAOSC7hp5+ow77PeyM1OQ4cXmIxQR3lK8/1AJzaRNeBLklq dCMQ== X-Forwarded-Encrypted: i=1; AJvYcCUTmJfYFIVkqXNGPQlK/ZgitbSGDHJpZF0eT7DXFDvBWZrDSp5CD8QuEoDKVuYvYYcBnMpLEA2uDX3KMyHzkVeVKW1Qs26HLheNgs+u X-Gm-Message-State: AOJu0YxwKogNii/RWZXdhhGEiextn8WfxzyTjTSNx7z9PQJKO+N5KyGd xwIwi2WQFKtXWL6gVuG0u+WRDs261vSRUOKgRWgGgmPESWOIkD+Zw3FegoGEVnoFceROmffeebB L2YCN7bSlWJYu+3/TDyUHDVWA9Bla4JiLuQMoqA== X-Received: by 2002:a05:6a20:17a2:b0:1a3:67fd:46b1 with SMTP id bl34-20020a056a2017a200b001a367fd46b1mr3653617pzb.36.1711772130610; Fri, 29 Mar 2024 21:15:30 -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 12:16:52 +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: Andrii Nakryiko Cc: Steven Rostedt , Alexei Starovoitov , Jiri Olsa , Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Eddy Z , 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 Sat, Mar 30, 2024 at 7:28=E2=80=AFAM Andrii Nakryiko 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. > > > > I thought I'll just ask instead of digging through code, sorry for > being lazy :) Is there any way to pass pt_regs/ftrace_regs captured > before function execution to a return probe (fexit/kretprobe)? I.e., > how hard is it to pass input function arguments to a kretprobe? That's > the biggest advantage of fexit over kretprobe, and if we can make > these original pt_regs/ftrace_regs available to kretprobe, then > multi-kretprobe will effectively be this multi-fexit. Yes, we can use multi-kretprobe instead of multi-fexit if we can obtain the function args in kretprobe. I think it's hard. The reason that we can obtain the function args is that we have a trampoline, and it call the origin function for FEXIT. If we do the same for multi-kretprobe, we need to modify ftrace_regs_caller to: ftrace_regs_caller | __ftrace_ops_list_func | call all multi-kprobe callbacks | call orgin | call all multi-kretprobe callbacks | call bpf trampoline(for TRACING) However, this logic conflicts with bpf trampoline, as it can also call the origin function. What's more, the FENTRY should be called before the "call origin" above. I'm sure if I understand correctly, as I have not figured out how multi-kretprobe works in fprobe. Thanks! Menglong Dong > > > -- Steve