Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.175 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796414685; b=dvoArZaXxGrBDFwAa07M2xE0R2x6zDMBdwbpmUPldkl44hDbUNsKI2JSV7BiFCijBlAV 9715ztjPan027SY09x8jy6rAZWyE9jSn+YAJGYu+5n9EgNTGBmgRtaJtm3UyWa8C5GM/d xHFHoK8/SCjYHrhdG7j91S6KkM5g/lOF1/j41GoNdAzE420ytb5Abtcpv62LaeMP2UIwb 5rvSV3ymN4fKll/4L9oNosuYcUd7K1Zg86ouHJMCuDTG3/XECPsRaqfiyhAAqAFiWD2VN 4F4jSzQDjbwAPYY086rQN9pUot/VjgMTliRbcP2Q02FnT+YE2Z8XXAT68L3Q9Phr5kA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796414685; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:Date:Subject: MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id: References:In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature: X-Developer-Key; bh=Rup5W2qBXzy1+ziS41La0uje1Hhf/xm7UziGUo1/Jho=; b=enUsNtfogYrobamNRfQ4EEL3KtM3xHxuXQUy36A9KtJ6q0zZnQICxxQN3PblAE9m+Aly YZXWJVdXel2GtitF5OB+LjY20B//7aniQdpvVYUAHez+lb1RvtkCNhigyqyz40M8Ptpgb 1NvJ5lo+Im81KENuRvIZiXM07tJ/9pSxXDon8oHB5xTczVnj7vltZiu4YOINgwOYmmNkY VXpy2KcM4ruE9pKmkB2pTPW8hZ5dNQQQ5QVSvhwezgJ59PfDd76gt2bJY+YdVMuhZ8QHt TBYEJ8vTxhszsxMZEKCvlX6Ap4CDo6YALHeTMI54fGwp6Koxa89iusHcT/uG+AvfpVA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=yFVksI9B; arc=none smtp.remote-ip=209.85.128.175 Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF0FB606A322 for ; Fri, 4 Dec 2026 20:04:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CF0FB606A322 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.a=rsa-sha256 header.s=20260707 header.b=yFVksI9B Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-8b5c0d8a835so15863867b3.2 for ; Fri, 04 Dec 2026 12:04:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796414684; x=1797019484; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Rup5W2qBXzy1+ziS41La0uje1Hhf/xm7UziGUo1/Jho=; b=yFVksI9BPNpFS1M1cUEwy9OoVPVz+ex/OGEX0lN/5o9g0v3vlBpKI2Cx7SKMVKZ1IM R4FDc7xwTpbWvsvg26moj5hAo5KsP4TtumKS7oCiIl07UvU6wtLmq3e+yPhRlSX/rrdx OcLTZyl0AB9bV3E0uaWW4rxDiqA9OBLUsZfNaWA7dzfV4YwkbZua1TEY30C+diqy4ywN 0HlJdNNkBN/MWeG0PLjf5vdqytj9iWKWz+k+kNj0iRqecaxV+xZ0IFJlsOmDAXbwJKiM UvJB1ch7WvZh8tK5IQXbWNngRwaXoDToNzg3ff/V6J+PFulIjG7YpJznfvcaPui7yC0t w2XA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796414684; x=1797019484; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Rup5W2qBXzy1+ziS41La0uje1Hhf/xm7UziGUo1/Jho=; b=JiBzn/67HA+BuHyfhuKKm6g00NE50UsltLeZy0Qs86VIHSkwNNuYg55zjSkUnkQ0+d gvo8kuHI/KeRG+F2aA/wWZNACAJBWbjqfg7H6aHxtZel3EN/MoIGLtZJRBD5YUlSkalF B+lZ1H/lX0FvdWgJB4LDKxv2B1lMTkU89mc/8ckkBkjDkGRcaSOfg/DWPF1Y3JiA5RNY Qc6XBmX+tWWQNtMGKApTnhj2joHWdiyk4vKBAGUG9zhYGLvJL07Fu2z4dMsy9ZMDmWxr 8TIiJjKcFpiyWmgn7dTjsxQcBnyykAb/ISmTet16aRzFqoSLVTKMDJzqw/PHyslxY9MR B2rg== X-Gm-Message-State: AB4q+uHOa581ydtRvxMdBPEK2hPnbwrrjSY6HVzTMYmR8LTCS3JBYNZx TVJkGOnf1RQvAXRuPpUb4AEFQj4lZMkfQ7y3c86yWCMX6dNLrzBYtXkA X-Gm-Gg: AZFdTZcFe03IPONP43hYfxjo7oBY+PEPMsKlKtUKVMa9DzTbTTKfOTpLbmxFKe8gN/B 6+vJZSP+w4+aC6JzRSc/SS2AbNGAlaaPOpVNwp/aLZy7rPTMKyFVZYwLTx3rOpR+55JbmAYpNGW P6BLG5byrfgbKOQGF+3hjWJROWJaelsk9ZdKs2p260Ng0m7ENBlIafEN/aYyUsxYA7z+ES7mFeP sKPr9y7vctMSqvvBcfi52q+RuBpBPlZuZXMwX4eD2zmmhEIfmsVCtZZPqTbZbz18aEn/vj/WG/6 6lkzZRYGKlaYPOVa1ICearAoyZsGAwPFYgfOyZgbX0VfhPAOEqpUBIqOXW3pS/f6DIFP7l61nm1 GzTxAmjvmUL3UOcNdwpG9GkFgkfGXly/jrhZB0kp9z7weUF/0PgOTunrLDDSHhK2f6smWQJNd9H o0zMNRjoU316bL4BWclZPw7Kun3xwDf7N5Afo1wK1OMcpOEzZXL2vTjzgC4DsNQQ/WFlJ4xiEUF WEd9X52M70N0eOHHHPBEba5hCmObIWfvyov6u0JfPPCGTOkwPeTMe/tO7mjOg+2/MPPY9gHm6Bu 8eraxABDwOcL4U59d/Y= X-Received: by 2002:a53:a9c3:0:b0:67d:a69f:b32f with SMTP id 956f58d0204a3-67f7247460dmr1255669d50.96.1796414683882; Fri, 04 Dec 2026 12:04:43 -0800 (PST) Received: from localhost ([2a03:2880:30ff:7::]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67f0be017fdsm1495842d50.3.2026.12.04.12.04.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 12:04:43 -0800 (PST) From: Mykyta Yatsenko Date: Fri, 04 Dec 2026 12:03:52 -0800 Subject: [PATCH bpf-next 1/5] bpf: Make a NULL cache slot mean that local storage has no value X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-sk_storage_prefetch-v1-1-ea52fb48b52b@meta.com> References: <20261204-sk_storage_prefetch-v1-0-ea52fb48b52b@meta.com> In-Reply-To: <20261204-sk_storage_prefetch-v1-0-ea52fb48b52b@meta.com> To: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, kernel-team@meta.com, eddyz87@gmail.com, memxor@gmail.com Cc: Mykyta Yatsenko X-Mailer: b4 0.16-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1796414679; l=5609; i=yatsenko@meta.com; s=20260324; h=from:subject:message-id; bh=5s5t553WSY/vt8g6qs0IzcTaOnvM36x+LdncDOggDsk=; b=D/rvE+Z+TAwScJDn/fk+Td7m+3UFDGFHf2bQBnNf2V3Lvzk2n5XQyc5Y0rIjvibUSzRhfCX0I MV5/OizaIAmD7uxj5amuyOw+OVFW8XgSUl2d2zdhGFFbdW1mr7EDo2a X-Developer-Key: i=yatsenko@meta.com; a=ed25519; pk=1zCUBXUa66KmzfjNsG8YNlMj2ckPdqBPvFq2ww3/YaA= From: Mykyta Yatsenko A lookup of a value that the owner does not have walks the whole list of its storage, with one cache miss for each element when the owner is cold. A prefetch cannot hide these misses, because each address comes from the element before it. Fill the cache[] slot when an element is linked, and refill it from the list when the element in the slot is unlinked. Then a NULL slot means that no element has this slot, and the lookup stops at the first load. This applies to all local storage types. With more than 16 maps, maps share slots, and a lookup can still walk the list. A delete and map_free() now walk the list once under the lock, to refill the slot. An update could already walk the whole list under this lock. A create of an absent value no longer walks the list. destroy() does not refill the slots, because the owner goes away. On 64-bit, the copy of cache_idx goes into a hole of the element. On a 32-bit CPU with 32-byte cache lines, the element grows by 32 bytes. The local storage benchmarks in selftests (1 to 1000 task storage maps, and creates) show no change larger than the noise between runs. Suggested-by: Alexei Starovoitov Link: https://lore.kernel.org/bpf/DN92BUAQMX7H.3NKLFY0FRORT7@gmail.com/ Signed-off-by: Mykyta Yatsenko --- include/linux/bpf_local_storage.h | 14 +++++++++++--- kernel/bpf/bpf_local_storage.c | 23 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index 6c643ac510799..83d849595805f 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -85,7 +85,8 @@ struct bpf_local_storage_elem { */ }; atomic_t state; - /* 4 bytes hole */ + u16 cache_idx; /* Copy of smap->cache_idx. smap can be NULL */ + /* 2 bytes hole */ /* The data is stored in another cacheline to minimize * the number of cachelines access during a cache hit. */ @@ -93,6 +94,10 @@ struct bpf_local_storage_elem { }; struct bpf_local_storage { + /* + * A NULL slot means that no selem on the list has this cache_idx. Link + * fills the slot, and unlink refills it from the list, under the lock. + */ struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE]; struct hlist_head list; /* List of bpf_local_storage_elem */ void *owner; /* The object that owns the above "list" of @@ -164,10 +169,13 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage, /* 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) + /* The slot is NULL only if no selem on the list has this cache_idx */ + if (!sdata) + return NULL; + if (rcu_access_pointer(sdata->smap) == smap) return sdata; - /* Slow path (cache miss) */ + /* Slow path (another map has the slot) */ hlist_for_each_entry_rcu(selem, &local_storage->list, snode, rcu_read_lock_trace_held()) if (rcu_access_pointer(SDATA(selem)->smap) == smap) diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index ed849546afd1f..b61183c0c17b4 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -172,6 +172,20 @@ static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now) bpf_selem_free(selem, reuse_now); } +/* Another sdata for the cache[] slot of @selem. local_storage->lock is held. */ +static struct bpf_local_storage_data * +selem_cache_sibling(struct bpf_local_storage *local_storage, + struct bpf_local_storage_elem *selem) +{ + struct bpf_local_storage_elem *pos; + + hlist_for_each_entry(pos, &local_storage->list, snode) + if (pos != selem && pos->cache_idx == selem->cache_idx) + return SDATA(pos); + + return NULL; +} + static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem *selem, struct bpf_local_storage_map *smap, struct bpf_local_storage *local_storage, @@ -182,7 +196,8 @@ static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem * if (rcu_access_pointer(local_storage->cache[smap->cache_idx]) == SDATA(selem)) - RCU_INIT_POINTER(local_storage->cache[smap->cache_idx], NULL); + rcu_assign_pointer(local_storage->cache[smap->cache_idx], + selem_cache_sibling(local_storage, selem)); if (pin_owner && !refcount_inc_not_zero(&local_storage->owner_refcnt)) return; @@ -228,6 +243,10 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor return free_local_storage; } +/* + * The caller holds local_storage->lock, or no other CPU can unlink from the + * storage yet: a new storage before it is published, or the storage of a clone. + */ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage, struct bpf_local_storage_elem *selem) { @@ -236,8 +255,10 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage, smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); local_storage->mem_charge += smap->elem_size; + selem->cache_idx = smap->cache_idx; RCU_INIT_POINTER(selem->local_storage, local_storage); hlist_add_head_rcu(&selem->snode, &local_storage->list); + rcu_assign_pointer(local_storage->cache[smap->cache_idx], SDATA(selem)); } static void bpf_selem_unlink_map_nolock(struct bpf_local_storage_map *smap, -- 2.53.1-Meta