Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp1484531pxb; Fri, 18 Feb 2022 08:43:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJyjn0NAgP635cEevZNkZ3foDz6a4mnEsH92trNeBZDA1F5ePWNqZk/uhLp8IrzjNKMhrdvs X-Received: by 2002:a17:906:6b8e:b0:6b9:1f27:73e with SMTP id l14-20020a1709066b8e00b006b91f27073emr6905620ejr.361.1645202605872; Fri, 18 Feb 2022 08:43:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645202605; cv=none; d=google.com; s=arc-20160816; b=UF2I/xnL/rVfZeG/4jC5F4xlORpwPXyTrhWkdcD2llO6rxGJHTFfLzCxKfdidMl9Q3 /M5tA9/mWWCm437xrMxbzkvsLP6aCbKxOTWlN1dFKLUi5+45AulUXxtwa5t6ZpkK67zl hBoFqLKgvxD55okfHAZ5ZW1XTHMQv4ygPo7a0z24K7dhXqy8S+3CbpnW4ERVu/oxsJ8u X2jQoDc3VUePsZYjPGYCRYIlaqgaR2kDvQGHdELcF7i+sYwTXPKCQBeTEFKHBVnEAo4A FjaoDfhr2nryRKIooA/OJGF55xXHC8FObG6fmk0E/ZlLhzwGIvtUV/kDeOFsOYIP28dI bWUA== 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=VnAIJ0UvovmuuRgmc/P0eoCaMTkjs2w9LqScQtLxo3g=; b=PbiQuzVQiCi8RtdI+BXYt6L2YFKbneQx5t8AvGRmh7LgmSklfJCpGTfKn9yxGAgXQl U/n8Tow83yU7l9J15Ie1nDnw76gWIUxvbxt1AhUFdm1AiXDg4AGpHJYx2nzN0+MXv+TM uzOR5vfpDFsVwP+/vjChmtb1MCYMMQl+2kywdpz8aEMJyUUZx2jNJbjQvHTqCzAAB5oM EfZGWVf52I9dF/adN+X2fITT1evmsfCcVbeExYIwOYezpmPtth/4wQhHlnOO9WMzmIpV sqdy6a4yQLsxVYw/Vav3obfPfQXKT4wVV+YGf1mcW5q9CwoVMSx7lsR6VDn9FJIZ63To T2jQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Px6loXhQ; 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 md17si3100661ejb.986.2022.02.18.08.42.59; Fri, 18 Feb 2022 08:43:25 -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=Px6loXhQ; 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 S235999AbiBROXN (ORCPT + 99 others); Fri, 18 Feb 2022 09:23:13 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:44944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235993AbiBROXM (ORCPT ); Fri, 18 Feb 2022 09:23:12 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96D402459E; Fri, 18 Feb 2022 06:22:52 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id p9so14840995wra.12; Fri, 18 Feb 2022 06:22:52 -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=VnAIJ0UvovmuuRgmc/P0eoCaMTkjs2w9LqScQtLxo3g=; b=Px6loXhQkxStejGmXJpI+5UAeQ9EfLtKQPkqYEikfuKSw1EzIkaz0YZ1Dibes4Mpii EotzNG5rl+jqnQMR5J3kkeTAer9ndjct5npAKK1EIHVpbiaL5I9TC7/je4AWTMtdHfhd /SnwAdLdGvNUPHpmjVZCTfpmn+9lrX8FcvQHM4mGQU8io+4JLu8OK0y6kmPWbv/AWHUU clFjFg69ChGs8iyMPFAbHCD+9Hkgk9lHkbWBxWC+3dxpUtppgVAIoV7f9yH3USdw9VlQ ZKk12XpwJhjI7BRzYaP3nxatFuazHwavRq/AI78Md3qELUZhC1gI0G5yfW0WVif5KkLZ 09Tg== 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=VnAIJ0UvovmuuRgmc/P0eoCaMTkjs2w9LqScQtLxo3g=; b=n6YxPgCMigLQHXvB1+RZa3aDOb7Bdt5qJtphlM3+9qtC3fnfVzvPtXzcMUaiRCtPxy 2sUGpA4tMzyD6WhDcLi+w547KKqVVmUBlh+lqEkPQDbmUycBnA4tGtXJhXUcuNHQNf1d 64iIdzJimIHvsQC+DSq6JqZL8E9Df15MNC/Z9Gu0GWNNLk0VQqNhosPsmFPFhzx5lCb0 7HeQ1gduxX5S8LEBJKxkZec8OQxehxCl579QclvYjknICfEtmvzXkxvsBgxu/D9oC0MV HrYqvgi1UHlz8omtGtibhoxS0dKB9qcB0IDyNwczReWKjGtdXWZe5ZvrDzP0UttRvBWn Atvg== X-Gm-Message-State: AOAM530g4ylGxnINcELUdD805e1mzJRTBsy3kf5Z6uUl2ZOXbBTvTrzB YhW2iT/Y6fxW4Mtz0JZm0cw= X-Received: by 2002:adf:9106:0:b0:1e3:c02a:f4af with SMTP id j6-20020adf9106000000b001e3c02af4afmr6353398wrj.150.1645194171020; Fri, 18 Feb 2022 06:22:51 -0800 (PST) Received: from krava ([83.240.63.12]) by smtp.gmail.com with ESMTPSA id b18sm34031223wrx.92.2022.02.18.06.22.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Feb 2022 06:22:50 -0800 (PST) Date: Fri, 18 Feb 2022 15:22:48 +0100 From: Jiri Olsa To: Andrii Nakryiko Cc: Jiri Olsa , Arnaldo Carvalho de Melo , Andrii Nakryiko , lkml , Peter Zijlstra , Ingo Molnar , Mark Rutland , Namhyung Kim , Alexander Shishkin , Ian Rogers , "linux-perf-use." , bpf Subject: Re: [PATCH 3/3] perf tools: Rework prologue generation code Message-ID: References: <20220217131916.50615-1-jolsa@kernel.org> <20220217131916.50615-4-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, Feb 18, 2022 at 02:03:28PM +0100, Jiri Olsa wrote: > On Fri, Feb 18, 2022 at 10:01:45AM +0100, Jiri Olsa wrote: > > On Thu, Feb 17, 2022 at 01:53:16PM -0800, Andrii Nakryiko wrote: > > > On Thu, Feb 17, 2022 at 5:19 AM Jiri Olsa wrote: > > > > > > > > Some functions we use now for bpf prologue generation are > > > > going to be deprecated, so reworking the current code not > > > > to use them. > > > > > > > > We need to replace following functions/struct: > > > > bpf_program__set_prep > > > > bpf_program__nth_fd > > > > struct bpf_prog_prep_result > > > > > > > > Current code uses bpf_program__set_prep to hook perf callback > > > > before the program is loaded and provide new instructions with > > > > the prologue. > > > > > > > > We workaround this by using objects's 'unloaded' programs instructions > > > > for that specific program and load new ebpf programs with prologue > > > > using separate bpf_prog_load calls. > > > > > > > > We keep new ebpf program instances descriptors in bpf programs > > > > private struct. > > > > > > > > Suggested-by: Andrii Nakryiko > > > > Signed-off-by: Jiri Olsa > > > > --- > > > > tools/perf/util/bpf-loader.c | 122 +++++++++++++++++++++++++++++------ > > > > 1 file changed, 104 insertions(+), 18 deletions(-) > > > > > > > > > > [...] > > > > > > > errout: > > > > @@ -696,7 +718,7 @@ static int hook_load_preprocessor(struct bpf_program *prog) > > > > struct bpf_prog_priv *priv = program_priv(prog); > > > > struct perf_probe_event *pev; > > > > bool need_prologue = false; > > > > - int err, i; > > > > + int i; > > > > > > > > if (IS_ERR_OR_NULL(priv)) { > > > > pr_debug("Internal error when hook preprocessor\n"); > > > > @@ -727,6 +749,12 @@ static int hook_load_preprocessor(struct bpf_program *prog) > > > > return 0; > > > > } > > > > > > > > + /* > > > > + * Do not load programs that need prologue, because we need > > > > + * to add prologue first, check bpf_object__load_prologue. > > > > + */ > > > > + bpf_program__set_autoload(prog, false); > > > > > > if you set autoload to false, program instructions might be invalid in > > > the end. Libbpf doesn't apply some (all?) relocations to such > > > programs, doesn't resolve CO-RE, etc, etc. You have to let > > > "prototypal" BPF program to be loaded before you can grab final > > > instructions. It's not great, but in your case it should work, right? > > > > hum, do we care? it should all be done when the 'new' program with > > the prologue is loaded, right? > > > > I switched it off because the verifier failed to load the program > > without the prologue.. because in the originaal program there's no > > code to grab the arguments that the rest of the code depends on, > > so the verifier sees invalid access > > > > > > > > > + > > > > priv->need_prologue = true; > > > > priv->insns_buf = malloc(sizeof(struct bpf_insn) * BPF_MAXINSNS); > > > > if (!priv->insns_buf) { > > > > @@ -734,6 +762,13 @@ static int hook_load_preprocessor(struct bpf_program *prog) > > > > return -ENOMEM; > > > > } > > > > > > > > > > [...] > > > > > > > + /* > > > > + * For each program that needs prologue we do following: > > > > + * > > > > + * - take its current instructions and use them > > > > + * to generate the new code with prologue > > > > + * > > > > + * - load new instructions with bpf_prog_load > > > > + * and keep the fd in proglogue_fds > > > > + * > > > > + * - new fd will be used bpf__foreach_event > > > > + * to connect this program with perf evsel > > > > + */ > > > > + orig_insns = bpf_program__insns(prog); > > > > + orig_insns_cnt = bpf_program__insn_cnt(prog); > > > > + > > > > + pev = &priv->pev; > > > > + for (i = 0; i < pev->ntevs; i++) { > > > > + err = preproc_gen_prologue(prog, i, orig_insns, > > > > + orig_insns_cnt, &res); > > > > + if (err) > > > > + return err; > > > > + > > > > + fd = bpf_prog_load(bpf_program__get_type(prog), > > > > > > nit: bpf_program__type() is preferred (we are deprecating/discouraging > > > "get_" prefixed getters in libbpf 1.0) > > > > ok, will change > > hum, I can't see bpf_program__type.. what do I miss? nah I was on top of perf/core.. I see it now ;-) jirka