Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1058396pxm; Wed, 23 Feb 2022 17:03:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJznw5ScjUel6Bu6QZi7w93CoVGZfbOqLEJuLcjO4ALaZYzI7of4ZiLjGKue/2ggTv/s6A/k X-Received: by 2002:a63:ce51:0:b0:362:c4fd:273b with SMTP id r17-20020a63ce51000000b00362c4fd273bmr358376pgi.540.1645664611871; Wed, 23 Feb 2022 17:03:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645664611; cv=none; d=google.com; s=arc-20160816; b=vIvu3z7k1+ZETuuLw96aAIlwpkJQt6TGI1q9cfsJnKBNuubNcCcAMN7rYRjgxau6+b IQYql9iiXK/v9M8TObcufHY3fMYkU4GIVtVMfuWpCKKbl3agl9B8lIeReuRAfmp2XhbJ A1TRWy2FO6KSI/JIZlJ6e8MFkvbZ20/J75zZDBA5Y5MRt/ST6rwn/dd4hOTmOreQt1KG P4wSA2qETboBMZSGkqWw5qPaFNz31kEXXhj+9T86j1mAc6sjzH/2Ww+zMp7LVeBULysj lo+/JT+I1cyMe4ytMwT7quIX1g368ArhmAQGMd0lvkqSqVEVVYsrkpF/dPc7UJiR2S+1 tU8g== 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=ol9UlquthQunriKvzPUzEkP+k0xGTuihXIeBY0Lwb0c=; b=Ks+d57eW0+yXZCL3z7/87Xz3dFcQda9qQIpQE3zYEGht3DAQRZGsbHkKoKJcOj5XbI V9ARF5pkYOTxfSHhKWOkKr5uVa7c4nP+VvWC9udYaut7WlBpz1sx06RyKm66Xtn+avky RI6jbkQnI/Wo00xAOTkeI3XNw51I23c0oZHYESPr7f/JcdaCGl1lxh+/Fq/xMjtjFoSO HE44erZzJQxptcFRVVHU0KJN3y4bK3dLUCa4wF1lc1RcSTZgWUOJstK3REZrj3ZjHXlw iJtzUDfpxLzhfO3V90FWXi827BKWR2+AiRuksk/8xiUi4dZ2mlabWdmKNA9Yh1NVnD8N t9TQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ZbbAlKNf; 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 t14si1144927plg.243.2022.02.23.17.03.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Feb 2022 17:03:31 -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=ZbbAlKNf; 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id C7831101F1A; Wed, 23 Feb 2022 16:53:49 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245143AbiBXALe (ORCPT + 99 others); Wed, 23 Feb 2022 19:11:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245164AbiBXALd (ORCPT ); Wed, 23 Feb 2022 19:11:33 -0500 Received: from mail-il1-x12b.google.com (mail-il1-x12b.google.com [IPv6:2607:f8b0:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92A665F4D1; Wed, 23 Feb 2022 16:11:04 -0800 (PST) Received: by mail-il1-x12b.google.com with SMTP id z7so483470ilb.6; Wed, 23 Feb 2022 16:11:04 -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=ol9UlquthQunriKvzPUzEkP+k0xGTuihXIeBY0Lwb0c=; b=ZbbAlKNfVyVK58hbGxvH4SKkthcj2BPZwJvOA0LlQUi0+WzYsSxLEDu5B/A/dcNS4b xfUuEBa7qZqIVmsKP7dxJhkWMYk8R1GQOvs3wmapoqk0I8QvCb4eqhb6VVr3hTH1fJJe iHsxWSXRANyXCY7RtGeDJSf7BVnqVNgiSWP32gT4ARErzOEkMJefNeLTftXD4jOKTTPB tcaCrMVOD6dDQnzhDrQUX4tuzrrFLLa/T9I9Uj5MPBfrDVujD+NTV2BAtwDzRmFmPqpw vzAlh6BdyY3KZ4bEGGqTFiR2anVtVExgDtiAHkKtW9YDGcvhL7LzhrA/xArjMCRaeWfX p3HA== 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=ol9UlquthQunriKvzPUzEkP+k0xGTuihXIeBY0Lwb0c=; b=yHBM1TJelV1PXDR6Kri3byrSzrxqvqOO7SospMleL+f6P+OUnCyawJYokC8Fbf7xhW XtvPWCjwETsZd/CiHswgAEksmNsB3dP9kPiVxj0YO8Igd5DuBXD4BnBYGUQqAPSsvzvo VkDEuLX516vyL9ZwgoPk2VBXMyJP2CvkXW23S9ueMNWsPIQBiORTngKwax7DasMIkzM9 ZVdYVuwAvJOJc1zDpzNu9fiXmUB/nsZlszARRFT6HGhzP+/Kk/wIGLTzlZKSiTz4+qVN bjNt8Pn0KRVvE0tl3dDjceqVcDsYW8E7WBH7VuttpzgWxbwJrAMbAAqrAb8JPttHV0tH 6l8Q== X-Gm-Message-State: AOAM530vvz+NVpYRNf4gUARaU/c0kd/PLUYh7/xCtQaoKQyCzZTgJ/RK jsssbdOnOS1Si7LEqbcY0uMLCDYADZQKL4jaPOg= X-Received: by 2002:a92:c148:0:b0:2c2:615a:49e9 with SMTP id b8-20020a92c148000000b002c2615a49e9mr113780ilh.98.1645661463988; Wed, 23 Feb 2022 16:11:03 -0800 (PST) MIME-Version: 1.0 References: <20220224000531.1265030-1-haoluo@google.com> In-Reply-To: <20220224000531.1265030-1-haoluo@google.com> From: Andrii Nakryiko Date: Wed, 23 Feb 2022 16:10:52 -0800 Message-ID: Subject: Re: [PATCH bpf-next v2] bpf: Cache the last valid build_id. To: Hao Luo Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Namhyung Kim , Blake Jones , bpf , open list , Greg Thelen 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 Wed, Feb 23, 2022 at 4:05 PM Hao Luo wrote: > > For binaries that are statically linked, consecutive stack frames are > likely to be in the same VMA and therefore have the same build id. > As an optimization for this case, we can cache the previous frame's > VMA, if the new frame has the same VMA as the previous one, reuse the > previous one's build id. We are holding the MM locks as reader across > the entire loop, so we don't need to worry about VMA going away. > > Tested through "stacktrace_build_id" and "stacktrace_build_id_nmi" in > test_progs. > > Suggested-by: Greg Thelen > Signed-off-by: Hao Luo > --- LGTM. Can you share performance numbers before and after? Acked-by: Andrii Nakryiko > kernel/bpf/stackmap.c | 12 +++++++++++- > 1 file changed, 11 insertions(+), 1 deletion(-) > > diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c > index 22c8ae94e4c1..38bdfcd06f55 100644 > --- a/kernel/bpf/stackmap.c > +++ b/kernel/bpf/stackmap.c > @@ -132,7 +132,8 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, > int i; > struct mmap_unlock_irq_work *work = NULL; > bool irq_work_busy = bpf_mmap_unlock_get_irq_work(&work); > - struct vm_area_struct *vma; > + struct vm_area_struct *vma, *prev_vma = NULL; > + const char *prev_build_id; > > /* If the irq_work is in use, fall back to report ips. Same > * fallback is used for kernel stack (!user) on a stackmap with > @@ -150,6 +151,12 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, > } > > for (i = 0; i < trace_nr; i++) { > + if (range_in_vma(prev_vma, ips[i], ips[i])) { > + vma = prev_vma; > + memcpy(id_offs[i].build_id, prev_build_id, > + BUILD_ID_SIZE_MAX); > + goto build_id_valid; > + } > vma = find_vma(current->mm, ips[i]); > if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) { > /* per entry fall back to ips */ > @@ -158,9 +165,12 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, > memset(id_offs[i].build_id, 0, BUILD_ID_SIZE_MAX); > continue; > } > +build_id_valid: > id_offs[i].offset = (vma->vm_pgoff << PAGE_SHIFT) + ips[i] > - vma->vm_start; > id_offs[i].status = BPF_STACK_BUILD_ID_VALID; > + prev_vma = vma; > + prev_build_id = id_offs[i].build_id; > } > bpf_mmap_unlock_mm(work, current->mm); > } > -- > 2.35.1.473.g83b2b277ed-goog >