From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD2B13C3795 for ; Thu, 14 May 2026 14:58:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778770735; cv=none; b=XnIH62AopistPOomR68cKH2MRnCNf+zL1h8RVD968U+OMBTf5UI3gVf3jTa52HUvB78xsZlgFSMtYCXHdHCzY3Z1BuxUOmyl8GrWiMwWBCdKSN9NGcTPCYjqsBtCGqmJvB7u4dK9aeJHNBThp7aIizeHja+faZcsZTrY3RXxOV0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778770735; c=relaxed/simple; bh=8ZC3+79Yxue6fFpKj0KpJNbK0AxVS1cBMVtnilmCAYc=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=R9FSgqvqfWCrfNdycwg2iaedPovxUUxHWXrikJ5d0vt5jbYTX8yPOmeKtnIljs6WCB1QW8w8knDLt2b40zbbPolFCvdQsz/Ef0ilaDXvdFlf9UtAwSijOcRiZANaAdzCaH8EQFRRv0/XVaeRExjbubVssEj6F1QGyeF9Yj6i+Z4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=L/teZnwy; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="L/teZnwy" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-488b150559bso63493605e9.1 for ; Thu, 14 May 2026 07:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778770732; x=1779375532; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=Ezlw2RI0fogBg6DmCkNoJAM/12GP1CD5Qxxk2BsDjnY=; b=L/teZnwy2+AcYmpCKRa2WC4/Ebt0jcM0myKjQ8Pko/Y8BCpx+jglvcfdnRYeZEhHde 1qd/kA0Elc8E05xGxGFPMKrhC4kFSlNh2i+kwjnHNF5Wf19aIH1UImOvwAwF+3sbsD2J 2DD3QE0GG0O5bU4hAsSTzVPOVJ+h3Eszkgt5r2sbafHw7POceGdKbb6yiVg13BAjqwHB f5D62Iahl0x4umnVivBHPWUZHbWhKr7o9uTWA8Yw84N/OuFbe3kB1p2Y4p0S6+JjdqkO qeZq20P6DWpL+Csi/WZpfZC5mVj/ot00IZaHUvsy60bJVOuwz2GDEHQ8P4ujs2ByUFlD sETA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778770732; x=1779375532; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Ezlw2RI0fogBg6DmCkNoJAM/12GP1CD5Qxxk2BsDjnY=; b=bTthyFnVVl+Kkm6VuhBP/YPqsCPRFU3Oari8w6Eu2r91GtIlvuyqMUopXUQ8QshJvu nALjCOtHZPyCFic9zx3iH12ZfFhwCfGYACafT8ksUVWUq50n4D0LAgh28sx4PJvjZaa1 cldq8Io8MY+dxa0h7ztRhBB8mU++kSC39IWpwvCi6WhIUR2MrMXl7Uab7bSAE/LNkVGb 8yp1a6vEWyEWQmLTtw7PAnh67/ETTOMDViduAr/gcAXOw+MiiJK9I8uoZuFzSUGjrdJ6 vD/YpJ5iXXswJ5bVk2FV0KhwWglnlJZALMnAy2aKLxB9e9snBlj5jSu5iY5fcmqUPobU iNxg== X-Forwarded-Encrypted: i=1; AFNElJ+UF5F2t+c2QHaljhbpj+SLrhKBpoZyEcKaR2EC3VI+EhhVABwpUnmFkLbpwrjXKeVnvKsTqQjxBBemKnI=@vger.kernel.org X-Gm-Message-State: AOJu0YwhL3LfEviu5ee+N4Bp0cc/SpO4xGE0VamA0kAa5S1kq5ZnaHsq L7ucYRTOQDX4FENWcO/o8KArKQvR6Fo9p5y2WR3RIZ5HDkInN2hdo59z X-Gm-Gg: Acq92OFrpodw7X+dkdu96C66NDuAFMyttNp4QEmqEtDb7SAiKXhFNtFFDJ7+ta0crFA nyBgYWXkfrjP6ntkVEhYNPlMeuMMtsGONXqjSbY4umC3H33RxAjklzH7zMUo8JfzK6wr2t7TZ8v oOSzCaQUO/o3HmQiM7gKsgUrp2jAiG2ZZ1dY7ibMY4AK79l4wSnPsO47rpQJYMzPiFt5VY1LNhc T4aQNE05vEJP9DYcyV6QbK3s50iAKK4LoPvy0n73xwOLjLRuqqxL3cB5J0pdOBl1t7NPmjdXlZt uG8N19KXfCWqgrQZGRZ3Pg7mw3homvHl+atBqj0qo2SkBNxl12cwQ9onJRtxvxxDH13tQ2nq7Q1 wwiukQ9dSmoSmDCluz2tPCyQ9ubG/McxIeeokotb6wKrRrYzGz8F33JtYHcWwWFF7v5fr1MUevO U4Q9ypexubzjNmGEUilw16l+T8IHSoMvSH+HYc+0SjBFQTGfqxAl6i7tV7MtgbJUbPgPRIJIOOw UftUguIcQ== X-Received: by 2002:a05:600c:3b20:b0:489:5022:39a4 with SMTP id 5b1f17b1804b1-48fc9a0cb7cmr121507175e9.9.1778770732031; Thu, 14 May 2026 07:58:52 -0700 (PDT) Received: from ?IPV6:2a03:83e0:1126:4:9cef:93b1:6db4:786b? ([2620:10d:c092:500::4:5c83]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45d9ec3b18fsm6906090f8f.11.2026.05.14.07.58.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 May 2026 07:58:51 -0700 (PDT) Message-ID: <6b4e0918-79a5-49ae-a172-c7d6df558dec@gmail.com> Date: Thu, 14 May 2026 15:58:49 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH bpf v3 3/3] bpf: Cache build IDs in sleepable stackmap path To: Ihor Solodrai , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Eduard Zingerman , Kumar Kartikeya Dwivedi Cc: Puranjay Mohan , Shakeel Butt , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com References: <20260512032906.2670326-1-ihor.solodrai@linux.dev> <20260512032906.2670326-4-ihor.solodrai@linux.dev> Content-Language: en-US From: Mykyta Yatsenko In-Reply-To: <20260512032906.2670326-4-ihor.solodrai@linux.dev> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 5/12/26 4:29 AM, Ihor Solodrai wrote: > Stack traces often contain adjacent IPs from the same VMA or from > different VMAs backed by the same ELF file. Cache the last successfully > parsed build ID together with the resolved VMA range and backing file > so the sleepable build-ID path can avoid repeated VMA locking and file > parsing in common cases. > > Suggested-by: Mykyta Yatsenko > Signed-off-by: Ihor Solodrai > --- > kernel/bpf/stackmap.c | 51 ++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 48 insertions(+), 3 deletions(-) > > diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c > index c1e96df360c3..318ce9ed0dd5 100644 > --- a/kernel/bpf/stackmap.c > +++ b/kernel/bpf/stackmap.c > @@ -226,13 +226,34 @@ static void stack_map_get_build_id_offset_sleepable(struct bpf_stack_build_id *i > .vma = NULL, > .mm = mm, > }; > - unsigned long vm_pgoff, vm_start; > + struct { > + struct file *file; > + const char *build_id; > + unsigned long vm_start; > + unsigned long vm_end; > + unsigned long vm_pgoff; > + } cache = {}; > + unsigned long vm_pgoff, vm_start, vm_end; > struct vm_area_struct *vma; > struct file *file; > u64 ip; > > for (u32 i = 0; i < trace_nr; i++) { > ip = READ_ONCE(id_offs[i].ip); > + > + /* > + * Range cache fast path: if ip falls within the previously > + * resolved VMA range, reuse the cache build_id without > + * re-acquiring the VMA lock. > + */ > + if (cache.build_id && ip >= cache.vm_start && ip < cache.vm_end) { > + memcpy(id_offs[i].build_id, cache.build_id, BUILD_ID_SIZE_MAX); > + vm_start = cache.vm_start; > + vm_end = cache.vm_end; > + vm_pgoff = cache.vm_pgoff; > + goto build_id_valid; > + } > + > vma = stack_map_lock_vma(&lock, ip); > if (!vma || !vma->vm_file) { > stack_map_build_id_set_ip(&id_offs[i]); > @@ -240,9 +261,22 @@ static void stack_map_get_build_id_offset_sleepable(struct bpf_stack_build_id *i > continue; > } > > - file = get_file(vma->vm_file); > + file = vma->vm_file; > vm_pgoff = vma->vm_pgoff; > vm_start = vma->vm_start; > + vm_end = vma->vm_end; > + > + if (file == cache.file) { > + /* > + * Same backing file as previous (e.g. different VMAs > + * of the same ELF binary). Reuse the cache build_id. > + */ > + memcpy(id_offs[i].build_id, cache.build_id, BUILD_ID_SIZE_MAX); > + stack_map_unlock_vma(&lock); > + goto build_id_valid; > + } > + > + file = get_file(file); > stack_map_unlock_vma(&lock); > > /* build_id_parse_file() may block on filesystem reads */ > @@ -251,11 +285,22 @@ static void stack_map_get_build_id_offset_sleepable(struct bpf_stack_build_id *i > fput(file); > continue; > } > - fput(file); > > + if (cache.file) > + fput(cache.file); > + cache.file = file; > + cache.build_id = id_offs[i].build_id; > + > +build_id_valid: > + cache.vm_start = vm_start; > + cache.vm_end = vm_end; > + cache.vm_pgoff = vm_pgoff; nit: Maybe we can also put if (cache.build_id != id_offs[i]) memcpy(id_offs[i].build_id, cache.build_id, BUILD_ID_SIZE_MAX); here? So all writes to the id_offs[i] are at the same place for clearer structure? Overall it looks correct. Did you try to run it to see if we hit this cache at all? Acked-by: Mykyta Yatsenko > id_offs[i].offset = (vm_pgoff << PAGE_SHIFT) + ip - vm_start; > id_offs[i].status = BPF_STACK_BUILD_ID_VALID; > } > + > + if (cache.file) > + fput(cache.file); > } > > /*