Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3171029rdb; Tue, 6 Feb 2024 09:04:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IElJmwCqfdjDf1qT2OM5L9T70L4GeylboiZL3d0Ct+a1nFKFttdEz2ysfIA2OoLswGXmpQG X-Received: by 2002:a05:620a:4714:b0:785:8da7:5e6f with SMTP id bs20-20020a05620a471400b007858da75e6fmr4692679qkb.0.1707239095488; Tue, 06 Feb 2024 09:04:55 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707239095; cv=pass; d=google.com; s=arc-20160816; b=olDy6mndrN74acNezIE8EmKFQ03PEW/34J4sptu6WBtjH6YEuSY2aRBy+4PkTWXPgd NUWmEaRTq4bIw3bJPE2BcG4nTMSQdM12rlWDekCfSlczWChqRVlvQRKWWpQXbxd5Q93Y PPE4rLE6XSGsj47ahr0XzfgP5jzuFObPJAncPH22WW2AtMDJWZkuMaIDYR2BVGevaefU DP+Cgb9iGJ9avo+uUzz/kbhFmGnH3U9VRvQa4hto+ob+Upq/gF2prPMXS7y9FV1uOXjY 2wOOleBHxZgSirEWMT4h1ZGBH+Ei0tZ/xb3N91OggW1l1WqjhsPeez9TNIORzQcwnJEW lH9w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=user-agent:in-reply-to:content-disposition:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:references :message-id:subject:cc:to:from:date:dkim-signature; bh=xKs9EpdikTQGsxNZdIEgN0Ut0xn9gtysp/K6k0s0DIo=; fh=yCccQPfGrwG8fyNIYISb32VQikUVqXuEPA7nl/yUKwM=; b=mNoqJ8T5aZ+rxv04u/zMiCu/Bzuq+OpwSdACKV9R4+5x3+h9eigVrDWriDD0NwS72J nSxIrh9EXmxCenEyEEGmkCkZEmRJAm/RcAaNXrM6oPE7+d2Q5hx9/3EOTD1OcA3fBngX E8AQJ1LdrmBLexyiQF+ogLK4+UhrOSvbRmw2Th88PXddn9BE17Gj2MWe0k8AEPHk+I8r Wiu6jiqoGvmDX0uYPf/hxAEc/8eIk7DU6LdA9BUtxQUY4QkP1Fx2qUmX/FrxDA41vmhI sOBOG8SXmjqjyFmNvdqONCyFdZm95+sv+YlFVT2Yu+0+RL8rClcSw61g4REebyFXgFDi jA2g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OPAJX2D0; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-55349-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55349-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=1; AJvYcCVeNDax0yaSsU2JtUQkRoIge/nLN3ODNlU6S82pBW92iVVnKcmVPECTHTJW8hxG+S8Fami/sj4Gk7quqMP2MSFWEHiI3yKxdlYmuO/XJA== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id pe47-20020a05620a852f00b007854f08e693si2569267qkn.118.2024.02.06.09.04.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 09:04:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55349-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OPAJX2D0; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-55349-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55349-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 324881C23C33 for ; Tue, 6 Feb 2024 17:04:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 533B77484; Tue, 6 Feb 2024 17:04:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="OPAJX2D0" Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (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 7A96CBA3F for ; Tue, 6 Feb 2024 17:04:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.53 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707239086; cv=none; b=O4kqaRGzXykEobVQ6GZkR0P2SmUJTnt12/jiRM3S58IEISeID9YKyzO0xjsWHxOc7da49IQLD6Cc0CfOy6DdYBHvei1pTsxmU0L4syXmbAsBzKDDICEb4Gd1oS81U1GUiyYj3moQNlghFc4FOsOy5VcwaOy8FdItzzbHNGnPShk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707239086; c=relaxed/simple; bh=SZD7IZqPuDwDaWWDIxgSBYIX4CpDHSpLDXNC/Wl4BYI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=EReqrUanJz3FK9vaz4ZaMd85NlkAtQZ3ZFXcQnzzR5ixvK2W1iBb153fQFErMTPdoIuy92GOXwqRXvdFfvtH8Jul5yxJGU5YCkQi28QXZxWePk/WAU19+vtqhkfsX6d4ul99aObhJYd997bwL7GKKIxkEgFyInlhFyiXzdT/i4M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=OPAJX2D0; arc=none smtp.client-ip=209.85.128.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-40ffce2ceffso961195e9.2 for ; Tue, 06 Feb 2024 09:04:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707239082; x=1707843882; darn=vger.kernel.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=xKs9EpdikTQGsxNZdIEgN0Ut0xn9gtysp/K6k0s0DIo=; b=OPAJX2D0g5hUwH/A8SCzHEiVx9qY/DEKb5s8D9JxBZa7Y4dk1UFRhOu6FY6le1KUI+ GSWroBMdDcloQhJgTPmyn82TfidPyCKFbjIh6P3EnrTuoV1l7B9SXKR6ElidBAKaOYPA RRDUA8fsrr70jy1ipSYZjo9hgR7Vv3fORFRFO0v8c12NfGNdUpmWbU5Nd0fm2/SZA/om 3q31EOABe9KNY++I6dFcXE9T+XJxK/3z2eVTY7C+N0UFnS60dGQUapEpQvNa0Hd+KA/3 /KCKoCMuBEF2Nz1A4dQH0UD4ujFiLEObV5CPm3syON58pIDDYJ9YmvVuleKfDyOB4qZF u+cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707239082; x=1707843882; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xKs9EpdikTQGsxNZdIEgN0Ut0xn9gtysp/K6k0s0DIo=; b=YWBLKrnTSsJnzjByjq7Hl5WD+MAFv3ewndeEXoko8mEOUDO4xTqO++NosK0EtSEEB7 CjDDKwgJBAabrvWzMRUg/e7U2ZAeLmLV5ICM+kGNEcS4ScYezRNZw8m5TD4YOUY7NjX9 a6KFulvrCCDgcLVDTWuiXvFwQySQgdB5w+R8DYib3iGHDN8Fqw2zQw/V0+0k/FuZUve+ iDOTxUaPB1YOY7zCQh6vOSL4ZiSzvJvANjhpDFEpljUoV5W6ytSayoDh0Zf9fPgjWopm E5pGkE9u5z4Cgh5fE/oArnhtstcwWLoEM4/4pCrXq+uV5hl9woMlF09LXEs8Yasg7B5E s8tA== X-Gm-Message-State: AOJu0YwNKto4Q4BReFu29GmaZche155p5Q2A9Oh8sw4zRRXzvaXjppAT mNx9vT7z3JGRUde7ddeynsrf9McjTxknyEcuOOkpzQSwxDumyM9wkhq/RSw6JQ== X-Received: by 2002:a05:6000:d85:b0:33b:1131:ebfd with SMTP id dv5-20020a0560000d8500b0033b1131ebfdmr2307228wrb.49.1707239081517; Tue, 06 Feb 2024 09:04:41 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCWpanTQMpcfoN1ely2FCOQ0KoEBp9VDxRNtVMuuGW0clWY9RXWXLvvDdMuDOlBQW0S1ESYwCymin5LQTKUfvAc4uMNXIV0PzeaFp98TQ8nIs5HEJE9LGvp9GfuNRrE0JCDTJ2gTv0tvoFgpRLRF+3q5RalV/cX6SnhrFV3E0LQilMzjlhaSGPxA1pmwsOUGhhgxWLvnS7Xt84Twes71chnHDvTXtKQNPDEejGuviP1Ju3HNQCGzAWpHapfNSl00Rot2Pf22BS6ldf4D69KcxxAz+jt/D6ydSUJ4CiU8xIEsofCtaMinW8PqvW+uAsFVwfTqaQkeY05Sc/Hfi8Zr7BmWKQ8VuvEMUccnhkywvyFzOJdHij3o8JvV6zJ0MzkDtrXJ05F1lbhCqTMUn83P+ktyyA71LMaI3TDiW4W9nFdHz/Pi+ExNwND/4y2o4ABDjImGYFXru0iRGim6 Received: from elver.google.com ([2a00:79e0:9c:201:68a9:a8bc:3a17:b501]) by smtp.gmail.com with ESMTPSA id n8-20020a5d4c48000000b0033afe816977sm2509332wrt.66.2024.02.06.09.04.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 09:04:40 -0800 (PST) Date: Tue, 6 Feb 2024 18:04:34 +0100 From: Marco Elver To: Martin KaFai Lau Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH] bpf: Separate bpf_local_storage_lookup() fast and slow paths Message-ID: References: <20240131141858.1149719-1-elver@google.com> <5a08032b-ed4d-4429-b0a9-2736689d8c33@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5a08032b-ed4d-4429-b0a9-2736689d8c33@linux.dev> User-Agent: Mutt/2.2.12 (2023-09-09) On Mon, Feb 05, 2024 at 03:24PM -0800, Martin KaFai Lau wrote: [...] > > Or can you suggest different functions to hook to for the recursion test? > > I don't prefer to add another tracepoint for the selftest. Ok - I also checked, even though it should be a no-op, it wasn't (compiler generated worse code). > The test in "SEC("fentry/bpf_local_storage_lookup")" is testing that the > initial bpf_local_storage_lookup() should work and the immediate recurred > bpf_task_storage_delete() will fail. > > Depends on how the new slow path function will look like in v2. The test can > probably be made to go through the slow path, e.g. by creating a lot of task > storage maps before triggering the lookup. Below is tentative v2, but I'm struggling with fixing up the test. In particular, bpf_task_storage_delete() now only calls out to migrate_disable/enable() and bpf_selem_unlink(), because the compiler just ends up inlining everything it can: : endbr64 nopl 0x0(%rax,%rax,1) push %r14 push %rbx test %rsi,%rsi je ffffffff81280015 mov %rsi,%rbx mov %rdi,%r14 call ffffffff810f2e40 incl %gs:0x7eda9ba5(%rip) # 29b68 mov 0xb38(%rbx),%rax mov $0xfffffffffffffffe,%rbx test %rax,%rax je ffffffff8128002f movzwl 0x10e(%r14),%ecx mov (%rax,%rcx,8),%rdi test %rdi,%rdi je ffffffff8127ffef mov (%rdi),%rcx cmp %r14,%rcx je ffffffff81280022 mov 0x88(%rax),%rdi test %rdi,%rdi je ffffffff8128002f add $0xfffffffffffffff0,%rdi je ffffffff8128002f mov 0x40(%rdi),%rax cmp %r14,%rax je ffffffff8128001e mov 0x10(%rdi),%rdi test %rdi,%rdi jne ffffffff8127fffb jmp ffffffff8128002f mov $0xffffffffffffffea,%rbx jmp ffffffff8128003b add $0x40,%rdi add $0xffffffffffffffc0,%rdi xor %ebx,%ebx xor %esi,%esi call ffffffff8127e820 decl %gs:0x7eda9b32(%rip) # 29b68 call ffffffff810f2ed0 mov %rbx,%rax pop %rbx pop %r14 cs jmp ffffffff82324ea0 <__x86_return_thunk> Could you suggest how we can fix up the tests? I'm a little stuck because there's not much we can hook to left. Thanks, -- Marco ------ >8 ------ From: Marco Elver Date: Tue, 30 Jan 2024 17:57:45 +0100 Subject: [PATCH v2] bpf: Allow compiler to inline most of bpf_local_storage_lookup() In various performance profiles of kernels with BPF programs attached, bpf_local_storage_lookup() appears as a significant portion of CPU cycles spent. To enable the compiler generate more optimal code, turn bpf_local_storage_lookup() into a static inline function, where only the cache insertion code path is outlined (call instruction can be elided entirely if cacheit_lockit is a constant expression). Based on results from './benchs/run_bench_local_storage.sh' (21 trials; reboot between each trial) this produces improvements in throughput and latency in the majority of cases, with an average (geomean) improvement of 8%: +---- Hashmap Control -------------------- | | + num keys: 10 | : | | +-+ hashmap (control) sequential get +----------------------+---------------------- | +- hits throughput | 14.789 M ops/s | 14.745 M ops/s ( ~ ) | +- hits latency | 67.679 ns/op | 67.879 ns/op ( ~ ) | +- important_hits throughput | 14.789 M ops/s | 14.745 M ops/s ( ~ ) | | + num keys: 1000 | : | | +-+ hashmap (control) sequential get +----------------------+---------------------- | +- hits throughput | 12.233 M ops/s | 12.170 M ops/s ( ~ ) | +- hits latency | 81.754 ns/op | 82.185 ns/op ( ~ ) | +- important_hits throughput | 12.233 M ops/s | 12.170 M ops/s ( ~ ) | | + num keys: 10000 | : | | +-+ hashmap (control) sequential get +----------------------+---------------------- | +- hits throughput | 7.220 M ops/s | 7.204 M ops/s ( ~ ) | +- hits latency | 138.522 ns/op | 138.842 ns/op ( ~ ) | +- important_hits throughput | 7.220 M ops/s | 7.204 M ops/s ( ~ ) | | + num keys: 100000 | : | | +-+ hashmap (control) sequential get +----------------------+---------------------- | +- hits throughput | 5.061 M ops/s | 5.165 M ops/s (+2.1%) | +- hits latency | 198.483 ns/op | 194.270 ns/op (-2.1%) | +- important_hits throughput | 5.061 M ops/s | 5.165 M ops/s (+2.1%) | | + num keys: 4194304 | : | | +-+ hashmap (control) sequential get +----------------------+---------------------- | +- hits throughput | 2.864 M ops/s | 2.882 M ops/s ( ~ ) | +- hits latency | 365.220 ns/op | 361.418 ns/op (-1.0%) | +- important_hits throughput | 2.864 M ops/s | 2.882 M ops/s ( ~ ) | +---- Local Storage ---------------------- | | + num_maps: 1 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 33.005 M ops/s | 39.068 M ops/s (+18.4%) | +- hits latency | 30.300 ns/op | 25.598 ns/op (-15.5%) | +- important_hits throughput | 33.005 M ops/s | 39.068 M ops/s (+18.4%) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 37.151 M ops/s | 44.926 M ops/s (+20.9%) | +- hits latency | 26.919 ns/op | 22.259 ns/op (-17.3%) | +- important_hits throughput | 37.151 M ops/s | 44.926 M ops/s (+20.9%) | | + num_maps: 10 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 32.288 M ops/s | 38.099 M ops/s (+18.0%) | +- hits latency | 30.972 ns/op | 26.248 ns/op (-15.3%) | +- important_hits throughput | 3.229 M ops/s | 3.810 M ops/s (+18.0%) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 34.473 M ops/s | 41.145 M ops/s (+19.4%) | +- hits latency | 29.010 ns/op | 24.307 ns/op (-16.2%) | +- important_hits throughput | 12.312 M ops/s | 14.695 M ops/s (+19.4%) | | + num_maps: 16 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 32.524 M ops/s | 38.341 M ops/s (+17.9%) | +- hits latency | 30.748 ns/op | 26.083 ns/op (-15.2%) | +- important_hits throughput | 2.033 M ops/s | 2.396 M ops/s (+17.9%) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 34.575 M ops/s | 41.338 M ops/s (+19.6%) | +- hits latency | 28.925 ns/op | 24.193 ns/op (-16.4%) | +- important_hits throughput | 11.001 M ops/s | 13.153 M ops/s (+19.6%) | | + num_maps: 17 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 28.861 M ops/s | 32.756 M ops/s (+13.5%) | +- hits latency | 34.649 ns/op | 30.530 ns/op (-11.9%) | +- important_hits throughput | 1.700 M ops/s | 1.929 M ops/s (+13.5%) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 31.529 M ops/s | 36.110 M ops/s (+14.5%) | +- hits latency | 31.719 ns/op | 27.697 ns/op (-12.7%) | +- important_hits throughput | 9.598 M ops/s | 10.993 M ops/s (+14.5%) | | + num_maps: 24 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 18.602 M ops/s | 19.937 M ops/s (+7.2%) | +- hits latency | 53.767 ns/op | 50.166 ns/op (-6.7%) | +- important_hits throughput | 0.776 M ops/s | 0.831 M ops/s (+7.2%) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 21.718 M ops/s | 23.332 M ops/s (+7.4%) | +- hits latency | 46.047 ns/op | 42.865 ns/op (-6.9%) | +- important_hits throughput | 6.110 M ops/s | 6.564 M ops/s (+7.4%) | | + num_maps: 32 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 14.118 M ops/s | 14.626 M ops/s (+3.6%) | +- hits latency | 70.856 ns/op | 68.381 ns/op (-3.5%) | +- important_hits throughput | 0.442 M ops/s | 0.458 M ops/s (+3.6%) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 17.111 M ops/s | 17.906 M ops/s (+4.6%) | +- hits latency | 58.451 ns/op | 55.865 ns/op (-4.4%) | +- important_hits throughput | 4.776 M ops/s | 4.998 M ops/s (+4.6%) | | + num_maps: 100 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 5.281 M ops/s | 5.528 M ops/s (+4.7%) | +- hits latency | 192.398 ns/op | 183.059 ns/op (-4.9%) | +- important_hits throughput | 0.053 M ops/s | 0.055 M ops/s (+4.9%) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 6.265 M ops/s | 6.498 M ops/s (+3.7%) | +- hits latency | 161.436 ns/op | 152.877 ns/op (-5.3%) | +- important_hits throughput | 1.636 M ops/s | 1.697 M ops/s (+3.7%) | | + num_maps: 1000 | : | | +-+ local_storage cache sequential get +----------------------+---------------------- | +- hits throughput | 0.355 M ops/s | 0.354 M ops/s ( ~ ) | +- hits latency | 2826.538 ns/op | 2827.139 ns/op ( ~ ) | +- important_hits throughput | 0.000 M ops/s | 0.000 M ops/s ( ~ ) | : | : | | +-+ local_storage cache interleaved get +----------------------+---------------------- | +- hits throughput | 0.404 M ops/s | 0.403 M ops/s ( ~ ) | +- hits latency | 2481.190 ns/op | 2487.555 ns/op ( ~ ) | +- important_hits throughput | 0.102 M ops/s | 0.101 M ops/s ( ~ ) Signed-off-by: Marco Elver --- v2: * Inline most of bpf_local_storage_lookup(), which produces greater speedup and avoids regressing the cases with large map arrays. * Drop "unlikely()" hint, it didn't produce much benefit. * Re-run benchmark and collect 21 trials of results. --- include/linux/bpf_local_storage.h | 30 ++++++++++- kernel/bpf/bpf_local_storage.c | 52 +++++-------------- .../selftests/bpf/progs/cgrp_ls_recursion.c | 2 +- .../selftests/bpf/progs/task_ls_recursion.c | 2 +- 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index 173ec7f43ed1..dcddb0aef7d8 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -129,10 +129,36 @@ bpf_local_storage_map_alloc(union bpf_attr *attr, struct bpf_local_storage_cache *cache, bool bpf_ma); -struct bpf_local_storage_data * +void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage, + struct bpf_local_storage_map *smap, + struct bpf_local_storage_elem *selem); +/* If cacheit_lockit is false, this lookup function is lockless */ +static inline struct bpf_local_storage_data * bpf_local_storage_lookup(struct bpf_local_storage *local_storage, struct bpf_local_storage_map *smap, - bool cacheit_lockit); + bool cacheit_lockit) +{ + struct bpf_local_storage_data *sdata; + struct bpf_local_storage_elem *selem; + + /* Fast path (cache hit) */ + sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx], + bpf_rcu_lock_held()); + if (sdata && rcu_access_pointer(sdata->smap) == smap) + return sdata; + + /* Slow path (cache miss) */ + hlist_for_each_entry_rcu(selem, &local_storage->list, snode, + rcu_read_lock_trace_held()) + if (rcu_access_pointer(SDATA(selem)->smap) == smap) + break; + + if (!selem) + return NULL; + if (cacheit_lockit) + __bpf_local_storage_insert_cache(local_storage, smap, selem); + return SDATA(selem); +} void bpf_local_storage_destroy(struct bpf_local_storage *local_storage); diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 146824cc9689..bdea1a459153 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -414,47 +414,21 @@ void bpf_selem_unlink(struct bpf_local_storage_elem *selem, bool reuse_now) bpf_selem_unlink_storage(selem, reuse_now); } -/* If cacheit_lockit is false, this lookup function is lockless */ -struct bpf_local_storage_data * -bpf_local_storage_lookup(struct bpf_local_storage *local_storage, - struct bpf_local_storage_map *smap, - bool cacheit_lockit) +void __bpf_local_storage_insert_cache(struct bpf_local_storage *local_storage, + struct bpf_local_storage_map *smap, + struct bpf_local_storage_elem *selem) { - struct bpf_local_storage_data *sdata; - struct bpf_local_storage_elem *selem; - - /* Fast path (cache hit) */ - sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx], - bpf_rcu_lock_held()); - if (sdata && rcu_access_pointer(sdata->smap) == smap) - return sdata; - - /* Slow path (cache miss) */ - hlist_for_each_entry_rcu(selem, &local_storage->list, snode, - rcu_read_lock_trace_held()) - if (rcu_access_pointer(SDATA(selem)->smap) == smap) - break; - - if (!selem) - return NULL; - - sdata = SDATA(selem); - if (cacheit_lockit) { - unsigned long flags; - - /* spinlock is needed to avoid racing with the - * parallel delete. Otherwise, publishing an already - * deleted sdata to the cache will become a use-after-free - * problem in the next bpf_local_storage_lookup(). - */ - raw_spin_lock_irqsave(&local_storage->lock, flags); - if (selem_linked_to_storage(selem)) - rcu_assign_pointer(local_storage->cache[smap->cache_idx], - sdata); - raw_spin_unlock_irqrestore(&local_storage->lock, flags); - } + unsigned long flags; - return sdata; + /* spinlock is needed to avoid racing with the + * parallel delete. Otherwise, publishing an already + * deleted sdata to the cache will become a use-after-free + * problem in the next bpf_local_storage_lookup(). + */ + raw_spin_lock_irqsave(&local_storage->lock, flags); + if (selem_linked_to_storage(selem)) + rcu_assign_pointer(local_storage->cache[smap->cache_idx], SDATA(selem)); + raw_spin_unlock_irqrestore(&local_storage->lock, flags); } static int check_flags(const struct bpf_local_storage_data *old_sdata, diff --git a/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c b/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c index 610c2427fd93..6e93f3c8b318 100644 --- a/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c +++ b/tools/testing/selftests/bpf/progs/cgrp_ls_recursion.c @@ -33,7 +33,7 @@ static void __on_lookup(struct cgroup *cgrp) bpf_cgrp_storage_delete(&map_b, cgrp); } -SEC("fentry/bpf_local_storage_lookup") +SEC("fentry/??????????????????????????") int BPF_PROG(on_lookup) { struct task_struct *task = bpf_get_current_task_btf(); diff --git a/tools/testing/selftests/bpf/progs/task_ls_recursion.c b/tools/testing/selftests/bpf/progs/task_ls_recursion.c index 4542dc683b44..d73b33a4c153 100644 --- a/tools/testing/selftests/bpf/progs/task_ls_recursion.c +++ b/tools/testing/selftests/bpf/progs/task_ls_recursion.c @@ -27,7 +27,7 @@ struct { __type(value, long); } map_b SEC(".maps"); -SEC("fentry/bpf_local_storage_lookup") +SEC("fentry/??????????????????????????") int BPF_PROG(on_lookup) { struct task_struct *task = bpf_get_current_task_btf();