Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp1770237pxp; Mon, 7 Mar 2022 01:54:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxiwzoI5RBPGhJgiBJaLIis7vw8DztDVepprmNgva4EKD9k/EzCi5ZtDHZ6U05aocTe4vIV X-Received: by 2002:a17:906:a398:b0:6ce:71b:deff with SMTP id k24-20020a170906a39800b006ce071bdeffmr8214360ejz.204.1646646881316; Mon, 07 Mar 2022 01:54:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646646881; cv=none; d=google.com; s=arc-20160816; b=IoFTPtC+bEAVoDXV++vJRmVHmqrBe5+gE9eQtyvy5C+POryGkB9i/R6fGJgCnATT4F 4hCbOdAC7Qsw8xIoHG4qYcDA2Rh+3iTrX+HN3xsQIc0CLe5P5zkyeDLnEgcrrtZV/HdV NTWdQmLocmiKvj2SkRB8nxT9Q5wdMaoG8xVbQWTYDVsj4SSWrfj9YioIboDGP91w+oJb 2nTWIL+lT1GtFuQDDIlZ8oCeubD4AEfj4zwuhpG9/RcuOaPVQjWCx/fvoq0dK/k8z4nJ TC+gO5DQHpHIKKPEow+1zs4OsfGCCq2jOWwF/KMhryEOc44X7MOHMc3RL5NefxLyAAeK 0h/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=tRKtixCq2vkVN92yBMRPljmnJXwe1i5rOdDIlOZzuDk=; b=WkBxbbUwkoBdZQUp7ZZWUwF+EKeqD+CfIqaN2fojWFvN4zAPZRzck48Ebtmsgk3NJ9 pmGsIbUYPut/RKwVZFC7Omv9r/6b1y9FRXvCcPM4v70cnukyiUJXPaHdSGvdNAavJ83N y6eFvd++Vwpqke/sccNThrr6fCsXGVODbrf3yDMbDV0TPIMHUNj1Zl3sKor03nWHv4hW kIzjLt/u/C6xU3i21W+jn7WEfQYr9VSc5Q3lns8Ak3n7ghNTLavmaW8Du0DP57jOh/+5 DmtOoXH72MJ5FJo4KmTbJLwJ7rteKeL2BmoXWyLHJQy69B2aXnaq9eV7C1s5+laEaWOg mM+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=J080D3g4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f23-20020a05640214d700b0041649487fe3si1359460edx.558.2022.03.07.01.54.18; Mon, 07 Mar 2022 01:54:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=J080D3g4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233901AbiCFRaF (ORCPT + 99 others); Sun, 6 Mar 2022 12:30:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbiCFRaD (ORCPT ); Sun, 6 Mar 2022 12:30:03 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0535B275E7; Sun, 6 Mar 2022 09:29:11 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id p9so19915192wra.12; Sun, 06 Mar 2022 09:29:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=tRKtixCq2vkVN92yBMRPljmnJXwe1i5rOdDIlOZzuDk=; b=J080D3g44Cklc4QlyLmGYLzaaUx4BtacJ+HReuhiJK9yjmk5dnBeVT7y+SdAWHR/QD Uc4JTiEaj32A7R8Hb5eGCKoc/n2rnjYeMFL0Py5bdvgGYeMc90YI4w11obnh1yMWkJ83 h0nUDlP4i1F64eEIMxyMfmiZHalYqTzZOfzALs7lsIYH+LB4edjw5ClYj5Jyu6rV9H2F ricyAp4cbPFYkwFl0FWAM4PxrPQZkkaZluZAJybGRaDNcdlMAdEFsrNOsYQtHIsgtjnx PqofavJic5dFMgRe57wPfJ1ZaX5JFzBmxSm2AbALpP93zwr12soNELu1zMcQ8gim77Sn KEPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=tRKtixCq2vkVN92yBMRPljmnJXwe1i5rOdDIlOZzuDk=; b=7J7oBRqLHAH7UOlviraUTY9glShyjZ8emRAHFqwROtxdz1tOLgrHjRTI2FcgmUv48p Jg+RdNpCzrvr5wDLbBaqihG1rWjWvACGm9Id2oYGgxHgKCboO2YdMOWRkdzMSIiavkUb gf7mDE0apcDjIrj4NcsXbjl1dZ1lOqRmmwj2lvSDJWjk9rb0l2GM+YCF3XojtiM428ef 1CD5g3PppNsgnap7C/kqej9V4VbQvCUcKTOnveL5p+cg1ZbF2V7u1cSVgw66zKMzO88W AdrzS2r2Ud3baypEFhAkY86FDAp0oWFBPKGW+oo6OFh06vIysE2sK3YmIFp2Nw+08sYA IAHg== X-Gm-Message-State: AOAM5304QAbQJkFJ2cq+/aJEqLH/PlR0xY3lUXvqkz92pofuVDhdCLA0 sH79FvECj3pphuwUjf7Fdc/CD4PdLfDlFg== X-Received: by 2002:adf:f28a:0:b0:1f0:246e:11f8 with SMTP id k10-20020adff28a000000b001f0246e11f8mr5716448wro.481.1646587749465; Sun, 06 Mar 2022 09:29:09 -0800 (PST) Received: from krava ([193.85.244.190]) by smtp.gmail.com with ESMTPSA id r13-20020a5d498d000000b001f0587248c4sm8933998wrq.3.2022.03.06.09.29.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Mar 2022 09:29:09 -0800 (PST) Date: Sun, 6 Mar 2022 18:29:07 +0100 From: Jiri Olsa To: Andrii Nakryiko Cc: Jiri Olsa , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu , Networking , bpf , lkml , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Steven Rostedt Subject: Re: [PATCH 05/10] bpf: Add cookie support to programs attached with kprobe multi link Message-ID: References: <20220222170600.611515-1-jolsa@kernel.org> <20220222170600.611515-6-jolsa@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 04, 2022 at 03:11:08PM -0800, Andrii Nakryiko wrote: > On Tue, Feb 22, 2022 at 9:07 AM Jiri Olsa wrote: > > > > Adding support to call bpf_get_attach_cookie helper from > > kprobe programs attached with kprobe multi link. > > > > The cookie is provided by array of u64 values, where each > > value is paired with provided function address or symbol > > with the same array index. > > > > Suggested-by: Andrii Nakryiko > > Signed-off-by: Jiri Olsa > > --- > > include/linux/sort.h | 2 + > > include/uapi/linux/bpf.h | 1 + > > kernel/trace/bpf_trace.c | 103 ++++++++++++++++++++++++++++++++- > > lib/sort.c | 2 +- > > tools/include/uapi/linux/bpf.h | 1 + > > 5 files changed, 107 insertions(+), 2 deletions(-) > > > > [...] > > > BPF_CALL_1(bpf_get_attach_cookie_trace, void *, ctx) > > { > > struct bpf_trace_run_ctx *run_ctx; > > @@ -1297,7 +1312,9 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) > > &bpf_get_func_ip_proto_kprobe_multi : > > &bpf_get_func_ip_proto_kprobe; > > case BPF_FUNC_get_attach_cookie: > > - return &bpf_get_attach_cookie_proto_trace; > > + return prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI ? > > + &bpf_get_attach_cookie_proto_kmulti : > > + &bpf_get_attach_cookie_proto_trace; > > default: > > return bpf_tracing_func_proto(func_id, prog); > > } > > @@ -2203,6 +2220,9 @@ struct bpf_kprobe_multi_link { > > struct bpf_link link; > > struct fprobe fp; > > unsigned long *addrs; > > + struct bpf_run_ctx run_ctx; > > clever, I like it! Keep in mind, though, that this trick can only be > used here because this run_ctx is read-only (I'd leave the comment > here about this, I didn't realize immediately that this approach can't > be used for run_ctx that needs to be modified). hum, I don't see it at the moment.. I'll check on that and add the comment or come up with more questions ;-) > > > + u64 *cookies; > > + u32 cnt; > > }; > > > > static void bpf_kprobe_multi_link_release(struct bpf_link *link) > > @@ -2219,6 +2239,7 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) > > > > kmulti_link = container_of(link, struct bpf_kprobe_multi_link, link); > > kvfree(kmulti_link->addrs); > > + kvfree(kmulti_link->cookies); > > kfree(kmulti_link); > > } > > > > @@ -2227,10 +2248,57 @@ static const struct bpf_link_ops bpf_kprobe_multi_link_lops = { > > .dealloc = bpf_kprobe_multi_link_dealloc, > > }; > > > > +static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void *priv) > > +{ > > + const struct bpf_kprobe_multi_link *link = priv; > > + unsigned long *addr_a = a, *addr_b = b; > > + u64 *cookie_a, *cookie_b; > > + > > + cookie_a = link->cookies + (addr_a - link->addrs); > > + cookie_b = link->cookies + (addr_b - link->addrs); > > + > > + swap_words_64(addr_a, addr_b, size); > > + swap_words_64(cookie_a, cookie_b, size); > > is it smart to call (now) non-inlined function just to swap two longs > and u64s?.. > > unsigned long tmp1; > u64 tmp2; > > tmp1 = *addr_a; *addr_a = addr_b; *addr_b = tmp1; > tmp2 = *cookie_a; *cookie_a = cookie_b; *cookie_b = tmp2; the swap_words_64 has CONFIG_64BIT ifdef with some tweaks for 32bit, so I wanted to use that.. but I agree with your other comment below wrt performace, so will change > > ? > > > +} > > + > > +static int __bpf_kprobe_multi_cookie_cmp(const void *a, const void *b) > > +{ > > + const unsigned long *addr_a = a, *addr_b = b; > > + > > + if (*addr_a == *addr_b) > > + return 0; > > + return *addr_a < *addr_b ? -1 : 1; > > +} > > + > > [...] > > > @@ -2238,12 +2306,16 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, > > goto out; > > } > > > > + old_run_ctx = bpf_set_run_ctx(&link->run_ctx); > > + > > rcu_read_lock(); > > so looking at other code, I see that we first migrate_disable() and > then rcu_read_lock(), so let's swap? We also normally set/reset > run_ctx inside migrate+rcu_lock region. I'm not sure that's necessary, > but also shouldn't hurt to stay consistent. ok, will change > > > migrate_disable(); > > err = bpf_prog_run(link->link.prog, regs); > > migrate_enable(); > > rcu_read_unlock(); > > > > + bpf_reset_run_ctx(old_run_ctx); > > + > > out: > > __this_cpu_dec(bpf_prog_active); > > return err; > > [...] > > > diff --git a/lib/sort.c b/lib/sort.c > > index b399bf10d675..91f7ce701cf4 100644 > > --- a/lib/sort.c > > +++ b/lib/sort.c > > @@ -80,7 +80,7 @@ static void swap_words_32(void *a, void *b, size_t n) > > * but it's possible to have 64-bit loads without 64-bit pointers (e.g. > > * x32 ABI). Are there any cases the kernel needs to worry about? > > */ > > -static void swap_words_64(void *a, void *b, size_t n) > > +void swap_words_64(void *a, void *b, size_t n) > > I'm worried that this might change performance unintentionally in > other places (making the function global might pessimize inlining, I > think). So let's not do that, just do a straightforward swap in cookie > support code? right, I did not realize this.. I'll add to cookie code directly > > > { > > do { > > #ifdef CONFIG_64BIT > > diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h > > index 6c66138c1b9b..d18996502aac 100644 > > --- a/tools/include/uapi/linux/bpf.h > > +++ b/tools/include/uapi/linux/bpf.h > > @@ -1482,6 +1482,7 @@ union bpf_attr { > > struct { > > __aligned_u64 syms; > > __aligned_u64 addrs; > > + __aligned_u64 cookies; > > looks a bit weird to change layout of UAPI. That's not really a > problem, because both patches will land at the same time. But if you > move flags and cnt to the front of the struct it would a bit better. I was following your previous comment: https://lore.kernel.org/bpf/CAEf4BzbPeQbURZOD93TgPudOk3JD4odsZ9uwriNkrphes9V4dg@mail.gmail.com/ I like the idea that syms/addrs/cookies stay together, because they are all related to cnt.. but yes, it's 'breaking' KABI in between these patches jirka > > > > __u32 cnt; > > __u32 flags; > > } kprobe_multi; > > -- > > 2.35.1 > >