Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp1742656pxf; Fri, 12 Mar 2021 18:52:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJyhM1RmgVt03QsIedmXOK6jqXoV/RAhsmfc7bzX8UU95zzkERidTB60SsuJorD5z8ssSwgB X-Received: by 2002:a17:906:8408:: with SMTP id n8mr11469648ejx.152.1615603925557; Fri, 12 Mar 2021 18:52:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615603925; cv=none; d=google.com; s=arc-20160816; b=m9VZwJS+oTrBnPRiWGlnQYD/BnwUMe+MaRVnMxzxKKvFHLMoZCnrDHYxF8h+xs3ZwI UPf1hY4QqWmqVcPV6GhpoOj8aEKV0/dnQ1cPB9tFJPOUxnWTa12vGQVRnKUDBGnme+8J HKpQEDPvI0KF058ial7N6SGEmtN8bNzo3E5hZhYp3F13h7t+3YeB9K52TJEA/S7iilt6 KSBl0DIIij14YjMJV6z2qjmCUXrkdxeOex5GkTZBMJBilJrYu5Vg15yH4d/OGXQv75lV W3X91qhBlQEgxF6ue9V1gCLjep8J0PIef4vjKUE+tf35shIAj0jQWsI4/NbhnA1Zonlh vQpQ== 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=rhyPMcnHQIQmhjUZbuPpathGLF1C0v/EujW0DiDZzMY=; b=nV5u4qX+c4DO7qMKBYDbbkuDg6fydK652ZGg0HfNJGBED4AChxBjNnrUgt9uJ1SPfU v8j3pDdKzsjkCC4vQbm47WcaLUD86BJUBMhuR27DENq/afx9s6jKnWfyNHNViC4ALG/x JcM2zDmzIBnLjX4/xhomzFmm1ThzpAK+RIfgQzMGUZxbWHAz5nteI0sRITM3h0/GNIpT pUTaLrKtvhOxiurxYECNKYJt9a4esBBMhpbTVSuaRyUcJoN+cPai6hHFBi1ON99+UdVD 7ZXUEhBMiD6S/DQjcVW1oerFsx7rXI5M8Waspm4y23FMmDKym6h7AbSO0fovXQeGOeqS 1M4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=l6j6Yc4R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pj10si5389127ejb.348.2021.03.12.18.51.43; Fri, 12 Mar 2021 18:52:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=l6j6Yc4R; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.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: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233089AbhCMCup (ORCPT + 99 others); Fri, 12 Mar 2021 21:50:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232880AbhCMCua (ORCPT ); Fri, 12 Mar 2021 21:50:30 -0500 Received: from mail-yb1-xb2f.google.com (mail-yb1-xb2f.google.com [IPv6:2607:f8b0:4864:20::b2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB9DCC061574; Fri, 12 Mar 2021 18:50:28 -0800 (PST) Received: by mail-yb1-xb2f.google.com with SMTP id m9so27409938ybk.8; Fri, 12 Mar 2021 18:50:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=rhyPMcnHQIQmhjUZbuPpathGLF1C0v/EujW0DiDZzMY=; b=l6j6Yc4RE6ZuIFQpPUxzSj/6wAmMPZhY9i1m2KkEN3J14pduSSylfBRZ5hNb7sH2Q9 BpRn2KU8KnN+IX/nPS6sJbrRLmn6lgCDVlAZO+TxsYqKnV+jZx4JPSjRuVng8WF4SAAq 2tXsGLkPdQ3/76rWCRIQEaC5V9xz905IOdEv7keShMivQ4Hv8hypfqUl7zGqf0F1rIwC Kr4mUpwdJL1zwZ2AI2RzpYleCDmpwXzZtmryy7BOJfmPQ/9hHzbt4EHyY//uBy4dZcxd bli6h6POmsjX9tE4G5blDxTKxWsh2X39J3WCGIfJGmx3bOX9jcI/+SGhfNev9h4Xuj0s JKJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=rhyPMcnHQIQmhjUZbuPpathGLF1C0v/EujW0DiDZzMY=; b=boESUJYaT1vJ9Cp1Kewpt8sifyGIlJEx+hmXQcMFsoNpRTRzZc2C7jNkuOlyjv5xay 9ghi2AedaTv3fI/1AXI8X2IZNfEbYnvE9qP+vfRL30EJ8dib2uLZcchlYLQpbsEoI8E9 fhIrZUFR55ffc95UnIF3MGGXFOI6kgvc+REFU10CYLD3gE8BtiHv2xsoJarHvLW8AIOA 7f+HfZmQJj1iTrCx1FcbxZh8Xx88wa+6hoIUo73998C7tE0IiCs/yNaV5OiEa3Ky2JUm m66SS/3m4wUnC9Xa+y89SU7wGzrHKzi0rgqwbTeXJhkJfkB5F2JuLfudBU6r60fLsUog gUcg== X-Gm-Message-State: AOAM5304epAOGM0OShB55i6v9q+6EH1HAhlFm+MUWNp83hU8LpCHUY94 EIZMXS9rJALF0gZg3ZJnf9I/UvYH8gbRMG9EnGxFm6tCV8g= X-Received: by 2002:a25:d94:: with SMTP id 142mr21883722ybn.230.1615603827962; Fri, 12 Mar 2021 18:50:27 -0800 (PST) MIME-Version: 1.0 References: <20210312214316.132993-1-sultan@kerneltoast.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 12 Mar 2021 18:50:17 -0800 Message-ID: Subject: Re: [PATCH] libbpf: Use the correct fd when attaching to perf events To: Sultan Alsawaf Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Networking , bpf , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 12, 2021 at 6:43 PM Sultan Alsawaf wrote: > > On Fri, Mar 12, 2021 at 06:33:01PM -0800, Andrii Nakryiko wrote: > > On Fri, Mar 12, 2021 at 6:22 PM Sultan Alsawaf wrote: > > > > > > On Fri, Mar 12, 2021 at 05:31:14PM -0800, Andrii Nakryiko wrote: > > > > On Fri, Mar 12, 2021 at 1:43 PM Sultan Alsawaf wrote: > > > > > > > > > > From: Sultan Alsawaf > > > > > > > > > > We should be using the program fd here, not the perf event fd. > > > > > > > > Why? Can you elaborate on what issue you ran into with the current code? > > > > > > bpf_link__pin() would fail with -EINVAL when using tracepoints, kprobes, or > > > uprobes. The failure would happen inside the kernel, in bpf_link_get_from_fd() > > > right here: > > > if (f.file->f_op != &bpf_link_fops) { > > > fdput(f); > > > return ERR_PTR(-EINVAL); > > > } > > > > kprobe/tracepoint/perf_event attachments behave like bpf_link (so > > libbpf uses user-space high-level bpf_link APIs for it), but they are > > not bpf_link-based in the kernel. So bpf_link__pin() won't work for > > such types of programs until we actually have bpf_link-backed > > attachment support in the kernel itself. I never got to implementing > > this because we already had auto-detachment properties from perf_event > > FD itself. But it would be nice to have that done as a real bpf_link > > in the kernel (with all the observability, program update, > > force-detach support). > > > > Looking for volunteers to make this happen ;) > > > > > > > > > > Since bpf wasn't looking for the perf event fd, I swapped it for the program fd > > > and bpf_link__pin() worked. > > > > But you were pinning the BPF program, not a BPF link. Which is not > > what should have happen. > > This is the code in question: > link = bpf_program__attach(prog); > // make sure `link` is valid, blah blah... > bpf_link__pin(link, some_path); > > Are you saying that this usage is incorrect? Right, for kprobe/tracepoint/perf_event attachments it's not supported. cgroup, xdp, raw_tracepoint and fentry/fexit/fmod_ret/freplace (and a few more) attachments are bpf_links in the kernel, so it works for them. > > Sultan