Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1055881pxm; Wed, 23 Feb 2022 17:00:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJzYEnpNLrKxESRygSQdw3saaL+oYIa48XQ/Wcbs/qpN+LYoPw4bkQemwRRAKpey1rTELFes X-Received: by 2002:a05:6a00:148b:b0:4e0:1001:1063 with SMTP id v11-20020a056a00148b00b004e010011063mr434314pfu.15.1645664446448; Wed, 23 Feb 2022 17:00:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645664446; cv=none; d=google.com; s=arc-20160816; b=OicB+DtvpDBsVOjSS/NrY9HPev4IliR7OvxoiFh6Z+8skCOV5izl//sbB7UnE2wSUN k1GMCHWdbEUnanot6rJFh5SdPhpl538dM9NZu73vMi/qZDqZT9HCy/yfBy9ub/n66shA f0ue/VPJKXqnGVVwjwh3zkyyZStVfsb6TZXnCfdokTPOO7bDE7q5fmCu3TFlougGQj/s yg4qCaZ9BNjjIUao6MDGnjxHzxj84b/qHMrVnupHFciunpQMWE15vmysACt5nTNA3jZf 8PF2v8iMDPJNhh8gCwNZaqaMN6Z+REpMLy2Vn9PN1wmhLO3JsxSdCnND8E8Ppt+exgXr dobQ== 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=2o+ZPlf2KURamdvg+hitpuro1zwcVnovw9N7uB+kYcU=; b=eAbMF6wlwud2c2zWRyRMrphlc4CfWtfRrpi8TJ4AdX9FgtENKhQ3gRZw2RsOYL8+Tr CRuwXyyAKoiQRO5q1SGF8RWanaDxtwtJ4Tz86fn6iSZnC3ncRVZXhZRscB3zIMWmsHK4 xJxXXHHOvooz4RD9eDTLRsyp3VmeYUsiliOeo3ocbcVTzPgNFSwpnSCryQoxb8bnopIO QbAmrWXL9CmTbl4Ys8h2qAPumqBrQSuogBBra4ImiXKRRlGbTiKdRyxCRJgz6oMolFM7 k3YzUT3dL9Ddw3Co7c1GnE7cPPG7BWPBwQmUGQled3NACw5kixeS0k1ob756iyWZF+pO +CRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GNwrG9LN; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id o41-20020a17090a0a2c00b001bc33dbe60bsi3719219pjo.81.2022.02.23.17.00.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Feb 2022 17:00:46 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=GNwrG9LN; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 163A0C9932; Wed, 23 Feb 2022 16:51:51 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244623AbiBWWxC (ORCPT + 99 others); Wed, 23 Feb 2022 17:53:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244696AbiBWWwz (ORCPT ); Wed, 23 Feb 2022 17:52:55 -0500 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1FED554BD; Wed, 23 Feb 2022 14:52:26 -0800 (PST) Received: by mail-io1-xd2c.google.com with SMTP id m185so633294iof.10; Wed, 23 Feb 2022 14:52:26 -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=2o+ZPlf2KURamdvg+hitpuro1zwcVnovw9N7uB+kYcU=; b=GNwrG9LNEfQa403fZ4H3zN/Oe8rCNfslVnKm+cOCLCMtObn+ci8qq8RbbAvRoYaAvZ EIWFI6XhbGaAxxnEru5zLypjGGTUbbm03h1RRVoAo6GvUE/TvbuENVVzQKh93ra/ofHI 5sgOu1ynKF+rcdCX5tDxm/+D+fpERl7OJ1bYhVwH0LbTfCZZ5jbx1yY0AUtwkLHmwZre Wp7bhC3ZiI4z6KKWpcVU/qalR9M49nlwn0HNh96NQZ2fGtRVwbLGkUzZ+dS23iUWzPdv U6EOtI+MI0Qy3OeA+mx2nHMdC4ARdT08YDMwyjXfi12Q+9iixQgSzYzGukUcwA06urhi nUjw== 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=2o+ZPlf2KURamdvg+hitpuro1zwcVnovw9N7uB+kYcU=; b=gO9x1BEZvCbI8if3yyfWS3+FFqe77LkZqnIVg1HX2izTE19+rK826gG2MH8UmOi+zr aZMMou7tWzV+OohyDDh7L8OwTCCCmNRktkAKv9P5RTv7SU1wj3Q/DIZOe+5DoUhPCwT2 fgHnQFStJIubbV/0AqIgoBLNN1U+XbrU5S3NVLOsoY6ZAX0NZTlM9KipKbUpvzY6Z7vr 4pCkjvIorZ2a5zE25sY00cu/xZJwCah5tfpuq+iJ99G77TAIepU+r/pzGoDI+DPeoivv o5tqAa+GEiF3sonJ68rzs1tp4lFOeiY6ErJFFYEuCMx8L3DdmS95lJwsUDTT4ifd4Kp1 TrTw== X-Gm-Message-State: AOAM53262qZXyPIhcxBMs6j+5s2dPcVSexJto8diFDk4SyaZxHRfApSC t+VtkLCJ8kLDyQX2wqsLO9c+cWSbvPmkaSizSw4= X-Received: by 2002:a6b:e901:0:b0:640:7bf8:f61d with SMTP id u1-20020a6be901000000b006407bf8f61dmr1144042iof.112.1645656746186; Wed, 23 Feb 2022 14:52:26 -0800 (PST) MIME-Version: 1.0 References: <20220223222002.1085114-1-haoluo@google.com> In-Reply-To: <20220223222002.1085114-1-haoluo@google.com> From: Andrii Nakryiko Date: Wed, 23 Feb 2022 14:52:15 -0800 Message-ID: Subject: Re: [PATCH bpf-next] 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 2:20 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 > --- > kernel/bpf/stackmap.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c > index 22c8ae94e4c1..280b9198af27 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 > @@ -151,6 +152,11 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, > > for (i = 0; i < trace_nr; i++) { > vma = find_vma(current->mm, ips[i]); as a further optimization, shouldn't we first check if ips[i] is within prev_vma and avoid rbtree walk altogether? Would this work: if (prev_vma && range_in_vma(prev_vma, ips[i])) { /* reuse build_id */ } vma = find_vma(current->mm, ips[i]); ? > + if (vma && vma == prev_vma) { > + memcpy(id_offs[i].build_id, prev_build_id, > + BUILD_ID_SIZE_MAX); > + goto build_id_valid; > + } > if (!vma || build_id_parse(vma, id_offs[i].build_id, NULL)) { > /* per entry fall back to ips */ > id_offs[i].status = BPF_STACK_BUILD_ID_IP; > @@ -158,9 +164,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 >