Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp661472pxb; Fri, 3 Sep 2021 10:23:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwk1luLN8Ef9RHWt22tVBGhxJeFrAc1qsE/6crA8lFod4vrfDoj6FSSyDBgIe4WXdrtefJo X-Received: by 2002:aa7:c251:: with SMTP id y17mr54419edo.400.1630689828247; Fri, 03 Sep 2021 10:23:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630689828; cv=none; d=google.com; s=arc-20160816; b=U22Yh0kvZbnMAuaFMdLn+PXW4Hxp1S4mHZlf5kebhXgWE658AjKNBDxy8r+ukh05PU z9M4z/najfcxuz9J06+ByrmuezbUellfX3hvZDaC0YDY3lvUZZymLZRqkGIqhZ/c6+gP M4gQEHUZueqkIcdT8bC+7hKGl6/w/6BkSfg1vuNPw70NofHw1qndi7y2rjKoRrDYE+Ro VNTtRTRFPdFAsjPZIAydPikdpMTQo4nGiOhCHVPKOI83rqMAwc050xpQqMxo6/NisKuO jF+XBRsk53kw21jf5SVMjappYPxkFtiLMxmM8DgUjcUs5AANc3Qo8Lx2XS8nEkJ8zTb+ mHwQ== 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=5MjMHuC+roNm7Spa73SgumLohK2Wfsd4VJdqj/W1924=; b=PotcfCBn9MNdOZnuF9F99a82WjzNWdMYy+/7EJcx3g08+X6OSvPDNrpVzyHQmdQysx eceV+VqZakM9MsnCld8Te5ev28jeAzSh9e1MRMpuQGg1kuqpbiRlcsrBYHTCAGp7agc9 PIUhpFHfmmC9GVihTz+hF59/FBzBL0AD7S6EdiQul1MX+g1xdyCrJ4dnLz3Wo3rgkEWy JmGB385ZQv6+t+ccBhxdqCEWw68UCPCvQ3r7sulFysAmEMn5IU7p3wssLbYDoG7haHHe 19a00F4Fac8IoLOL8Pyf3yFI3O/BMHzTtvn1fx2eVmhIgJQ1aRKGyWqzFWOPfrOXGhUn kEyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="VIZ3CL/+"; 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 k14si82251eds.379.2021.09.03.10.23.17; Fri, 03 Sep 2021 10:23:48 -0700 (PDT) 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=20210112 header.b="VIZ3CL/+"; 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 S1350232AbhICRL3 (ORCPT + 99 others); Fri, 3 Sep 2021 13:11:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235425AbhICRL2 (ORCPT ); Fri, 3 Sep 2021 13:11:28 -0400 Received: from mail-yb1-xb33.google.com (mail-yb1-xb33.google.com [IPv6:2607:f8b0:4864:20::b33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FB06C061575; Fri, 3 Sep 2021 10:10:28 -0700 (PDT) Received: by mail-yb1-xb33.google.com with SMTP id j195so3298241ybg.6; Fri, 03 Sep 2021 10:10:28 -0700 (PDT) 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=5MjMHuC+roNm7Spa73SgumLohK2Wfsd4VJdqj/W1924=; b=VIZ3CL/+O3UuYMs8eVjmKMQ1O4PiKyZlLSGb+YS/eZDENI7Vh6DXz6ha6ZNMlari+x y/ze7Ji0aApiBcNbJY+FOvDQFRAPGpEA63Q/F73ioEgJHanNl7ZZkUifaQ0x51wGSKNa xR61VTmokLrpYELWjMX9M9rt+AJe7e7r2j9QWsk8YF/vHzb/nCQykFTLkMXoJ5yXvfLj Db6eJKEVUPSVDKyxeiP+78gdEYSl9sVN15q4dyXA6gqwr//3poaJ7rjqYtgMs8f1ZkzV kc8rAD8yj2AM1+lByqHnZQSYMsihCmdBaDIBOo/6V+30udlCZwibcUOj3rQov6DwD8Cx fXxg== 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=5MjMHuC+roNm7Spa73SgumLohK2Wfsd4VJdqj/W1924=; b=oQbDipayy25PijDEONkDHx0tS2wMTgKDUZtzzNFKR3zroh5Apn18tADIYIC1aSAS/l WfgkhFTtSCSxxh6XQKty9+YpN97aO2umIZK49lGxffS6qJYudcll5oLBl9pcRgjmQwVT Jl+FK95m/WFyjy/RZdnn7RG365DvkFa2Y0VO4ReZbESPf9GMCNRWTP6W+r+jz4LELRGl DO00rZDqvaeD85oRoACMdEC4pc62ja0gui7wKXodRXZFSrVHnVAVflLMueHPN11ekM7S XDZF0l0+1/px/Cdw8rtbydjfkFD535blRbhbt8fKlR2+kiFCUfwCD0cvqaVD0gVCrO2D FjRQ== X-Gm-Message-State: AOAM531n+HlZxMmrvUqiWpfHYSh+e+9u28smzbVYMifxkwg0rld9p1OO Lpb0hlFdc/Di2h90xV55lZsG5Vz11p8RwNW75Cs= X-Received: by 2002:a25:8c4:: with SMTP id 187mr67041ybi.369.1630689027611; Fri, 03 Sep 2021 10:10:27 -0700 (PDT) MIME-Version: 1.0 References: <20210902165706.2812867-1-songliubraving@fb.com> <20210902165706.2812867-3-songliubraving@fb.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 3 Sep 2021 10:10:16 -0700 Message-ID: Subject: Re: [PATCH v5 bpf-next 2/3] bpf: introduce helper bpf_get_branch_snapshot To: Peter Zijlstra Cc: Song Liu , bpf , open list , Arnaldo Carvalho de Melo , Ingo Molnar , Kajol Jain , Kernel Team Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 3, 2021 at 1:49 AM Peter Zijlstra wrote: > > On Thu, Sep 02, 2021 at 09:57:05AM -0700, Song Liu wrote: > > +BPF_CALL_3(bpf_get_branch_snapshot, void *, buf, u32, size, u64, flags) > > +{ > > + static const u32 br_entry_size = sizeof(struct perf_branch_entry); > > + u32 entry_cnt = size / br_entry_size; > > + > > + if (unlikely(flags)) > > + return -EINVAL; > > + > > + if (!buf || (size % br_entry_size != 0)) I think buf can't be NULL, this should be enforced already by verifier due to ARG_PTR_TO_UNINIT_MEM, so we can drop that. > > + return -EINVAL; > > + > > + entry_cnt = static_call(perf_snapshot_branch_stack)(buf, entry_cnt); > > That's at least 2, possibly 3 branches just from the sanity checks, plus > at least one from starting the BPF prog and one from calling this > function, gets you at ~5 branch entries gone before you even do the > snapshot thing. > > Less if you're in branch-stack mode. > > Can't the validator help with getting rid of the some of that? > Good points. I think we can drop !buf check completely. The flags and size checks can be moved after perf_snapshot_branch_stack call. In common cases those checks should always pass, but even if they don't we'll just capture the LBR anyways, but will return an error later, which seems totally acceptable. As Alexei mentioned, there is a whole non-inlined migrate_disable() call before this, which we should inline as well. It's good for performance, not just LBR. > I suppose you have to have this helper function because the JIT cannot > emit static_call()... although in this case one could cheat and simply > emit a call to static_call_query() and not bother with dynamic updates > (because there aren't any). If that's safe, let's do it.