Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp835647pxb; Thu, 17 Feb 2022 16:09:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJww/QvjSe9WyxSRlDb0gUdxF5rpl1sdsEU2qYL8zw2ajC3N8TEydkZdgsedblHYZxv4sDVF X-Received: by 2002:a17:90a:f486:b0:1b8:e091:160f with SMTP id bx6-20020a17090af48600b001b8e091160fmr5513983pjb.219.1645142986476; Thu, 17 Feb 2022 16:09:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645142986; cv=none; d=google.com; s=arc-20160816; b=rBF7wf9fkNECgA1Afo7+NxVT8BeRwL+5WIunoRrTmexFaEaAGkp1iVDQuzL+gVRTQJ AMY/dIBmu+2bnZtJc2XZueoUZGf8HU3i4yBr8t2prUyGZ/C5q3KRuGXQGUNgv/TztIBX GwCwSez+7TsqJSBetf/g53LZ2aDIGF1n+rBRgO5AfLcnsG/k1b6kAJs4fMkNFZTbm/G2 1fGBFzi4vFsfFp4RWO3k+XfFi2XYHz52jG4/qvxPQuP0rW31WE0AnkmnCi+7axY2v7kp WrpfMHgUkABztJ446ie9oWJB9n+Zirm9vwdesvywJ9Yov2ZNk+8tohtkb/bGiLVX0CoA BwGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=tr4a8HS2Qs7b0BilGyhe9qjBcn8N3AAsPrFbgBEmCOM=; b=YjNIwwH8pJ83Wct/aNewuHse6UfmjqnBpCrlEy3nqbXw708vk/jZeGCPNts3Yp94sc 7Lxdmu2YC2cHjWb5pWDZmwXG7yivquxM3pTJZM3X1oxD0I7uY1KIM/hbit2aOSsko8i1 aeKgU6bhhAhD2O5t/YocOWbqtA6TL2IusSwJyQEaY5SHBS1fAE4pEfvOWpJ7OZXMIi5m +y36MGtw/bx0AH1aYUz/XowNwIqZr2kKArPfd5X+60w4/GUyTp6ZEvqLQXXC1gJCBvAH VkZoi4m0eWdWAd3Y0gvjpTRakZwZM0aPo3DRo+A6oPFuocH8CFX9eF6clMyZEtvXZwOc J6CA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=D9muwQWB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id o7si940639pfk.362.2022.02.17.16.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Feb 2022 16:09:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=D9muwQWB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 741F433C490; Thu, 17 Feb 2022 15:35:45 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343713AbiBQVxp (ORCPT + 99 others); Thu, 17 Feb 2022 16:53:45 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:52840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231612AbiBQVxn (ORCPT ); Thu, 17 Feb 2022 16:53:43 -0500 Received: from mail-io1-xd29.google.com (mail-io1-xd29.google.com [IPv6:2607:f8b0:4864:20::d29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B43515C67C; Thu, 17 Feb 2022 13:53:28 -0800 (PST) Received: by mail-io1-xd29.google.com with SMTP id y20so5400661iod.1; Thu, 17 Feb 2022 13:53:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=tr4a8HS2Qs7b0BilGyhe9qjBcn8N3AAsPrFbgBEmCOM=; b=D9muwQWBxXbzOQ6POT++4UgqXD1nMiK0RaG1CryCZRk0bXQLHXam8P8wIy68rKs98Z C9py630g2WjB+tZk+DqhYA9M45GR5+4BnuE9rPbl0KFbpd3OjredOh3KLtaaIsbEmk9P lt6QwmlZ95w6N6zlYPter1QxwhiMyEWRKsOMaZKONUTCj+pJ7YSZHv5kxvtbIAT/zYpz DoQeSNXZzGsuZ5YlKPlkqGTcnQ6ERXsyK/wRwI3xcTP3y/0qnQq0Qo/haDRDTchMgdqz 4CvaD+SHlckL+x0Y5arCCooMwfJelNot/2T4UMwq+kyTVKJm9wqMamL5q0XOyQg2rdJ4 p8nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=tr4a8HS2Qs7b0BilGyhe9qjBcn8N3AAsPrFbgBEmCOM=; b=BEvaaycswj3bExfErJphdspgMqECB7nnTFTjy8kztlFVrGyaUD7vQX2gaN5FSzsg71 nXTxS/rLUscZfYeSpTa+jkSnmVBji7smGdhxYv+vtjXHf24vj2Nk841o6fNQV7Ox5drl 0yKkqWSs64VKefEJ9swji6L2XpgHbUCotiFxFhkFZeENO9xO0oqlSIepB06nh1Yphujs QB7OS1JyUW1mQxFuk3veCK/rRbpGMylyf05yjAEIHHnKQowa6ofvHoXg+EWGa9ME7rTx SGvLoO64W7aWhQXBHyETkjGtKlmOwvzH3bV/aeI4qWssBKjhS2h02Z1vqKwkDp8piO76 hSJQ== X-Gm-Message-State: AOAM533TjsonYK7Ht0nzpsnYBpV7EM/5zCUtdY6E27s2qPMDBaPBqNny 2IoPAkhIjED+CEXYcXUwfKKMDaDN4OGanBfXsTA= X-Received: by 2002:a5d:859a:0:b0:632:7412:eb49 with SMTP id f26-20020a5d859a000000b006327412eb49mr3302966ioj.63.1645134807784; Thu, 17 Feb 2022 13:53:27 -0800 (PST) MIME-Version: 1.0 References: <20220217131916.50615-1-jolsa@kernel.org> <20220217131916.50615-4-jolsa@kernel.org> In-Reply-To: <20220217131916.50615-4-jolsa@kernel.org> From: Andrii Nakryiko Date: Thu, 17 Feb 2022 13:53:16 -0800 Message-ID: Subject: Re: [PATCH 3/3] perf tools: Rework prologue generation code To: Jiri Olsa Cc: Arnaldo Carvalho de Melo , Andrii Nakryiko , lkml , Peter Zijlstra , Ingo Molnar , Mark Rutland , Namhyung Kim , Alexander Shishkin , Ian Rogers , "linux-perf-use." , bpf Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 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? > + > 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) > + bpf_program__name(prog), "GPL", would it make sense to give each clone a distinct name? > + res.new_insn_ptr, > + res.new_insn_cnt, NULL); > + if (fd < 0) { > + char bf[128]; > + [...]