From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796000230; b=xOMTx8wlXHxMwWwWaqicqJHWRLHrrjWFJ0EJzXbkcEQUg8vjFJcU4Tp3RyONYBC0krqq eku/jSOTYcYzMuHs6Qh3rWRZfFdzZJfTW9JQY/iAnmMJ/SdG75VMQ1NDc4BYX3nhiCTWg kCOXR1HZmGmY0mWF4Td6OQZ6LAj0H5DgxGLsrthfqyE8W72XfYO/Ip7RKFzwIfWHo5RDq FiqeECFoNUiAx+ERY+Fuoa2j/i9IVINra3EYrj8t3QbJ70cKaCfOJBZpMXpn5pXlxCIZh LDrKCn3oN63iq9+guiLGCjpFOTrJTfFKAAlTPIX9exZvpQfLoyDAxqpoJW6vEUBK/Mw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796000230; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Content-Type:Date:Message-Id:Subject:From:To:Cc:In-Reply-To: References:Content-Transfer-Encoding:MIME-Version; bh=MTPydZWL8pu/jH0fIorM1e0OpJLDq3zwuijFE3IrGVY=; b=zhKyrinSO33Kv9u+dKgaSt4sDrWzNS2hHv8mtJQj3vLCMnfjGYVbDVWxCJeWPyG1IkOX nrdFznaL5IPAiBD+EoSxvL29QUhi8223uW/LFxdqBFEWFaQ/d1gahJ1VWfbKcBJ/6ISIJ 3OZ0cm3PrihNUUo91AGXF+KQ2i5Jv/MkSHPK5u9li/9kryc2EH8frrRmutfYCNzSjV9kq CWTl9LOV+MEDQnP6W+8y8lbPu6IXq3u/I/oySDrMZ7lgERFEJLE3SxBhfEA3ZKdFCgxIt DaebDwGtmcMVJVEpE/QnzQNZqrggh77YjvJnUurAvUKYk5xpvVYLWiIi/HaFq8dv2cw== 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=o2xhGNvO; arc=none smtp.remote-ip=209.85.215.172 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) (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 7028E606A31C for ; Mon, 30 Nov 2026 00:57:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7028E606A31C 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=o2xhGNvO Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-cdb2dfcca94so4076116a12.1 for ; Sun, 29 Nov 2026 16:57:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796000229; x=1796605029; darn=vger.kernel.org; h=mime-version:content-transfer-encoding:references:in-reply-to:cc:to :from:subject:message-id:date:content-type:from:to:cc:subject:date :message-id:reply-to:content-type; bh=MTPydZWL8pu/jH0fIorM1e0OpJLDq3zwuijFE3IrGVY=; b=o2xhGNvOnKrBqnOjhr9/v9cv4c9AcoVg2flcYwyp9DkmA63ZDMaKFDEbnzAkZB5maq bwcPGbTjy++VTZnGht8BqjUgKf4m6Zy2gOD3v/Ad9JbN/UHFlNEYC0HStONe0PwAdIT4 EuMZUgeeOiriQGm9VnkwrXZY49urqbk30nfJr7jwgN9KG89LCjPWqFddthHNobPl1yaX IAZuZCLk3dQZBQEJMuhymvPbATraVMdiK1LA7ljabODgXiHTCmHik3OKyU849NdI5+5d QFPpOoOWjviw/j0sn9970pJ01EmrXCGCXPiInWuUYHXxTy7Osu8xul6a1I2AlmqwwbFv bbLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796000229; x=1796605029; h=mime-version:content-transfer-encoding:references:in-reply-to:cc:to :from:subject:message-id:date:content-type:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=MTPydZWL8pu/jH0fIorM1e0OpJLDq3zwuijFE3IrGVY=; b=WLQSeCsSR5qkx9otUdcF9hYJ+JE6RKEgfUSuR3YY+qfnub3lwE5S8EaReANbgi0YFN ovX8TZqQZHhtQ/0cEx+hdKrNOiFStob/AaPVrxKtPPEouxjAOLHNbt5XDf8sRAv3t53w iEcX7VNkbOZF0+WvCo+uj5Jg6Y1KuRNhh1+kdCg6nqP2foNh+dmOTE07jJgo3WSaj+Op 4ohCCeESXx1Pn7kxI8t8+Xtz80oZ3mlp1zU31vxoH1vW05oZOqvyGpHvNHyO9d+L+4+8 0Zzag+i/7lYaq1u5b1nrk5eR5aviC+qMid4sjWTG/D5oOlZplpcfvHoLPR1wTrDAPlUG +m/Q== X-Forwarded-Encrypted: i=1; AL+hErGz4Z0lgc5K+bnG+y2RbDPU++SjsIjzZZdXVy7BU/yerOnBOjpyQD8zd46rR5NSF9n9/II=@vger.kernel.org X-Gm-Message-State: APuU4Z843ztgpupZlONP9FmMnYZzHJ2XOJoVaGvLeCjOTM3h379xENJW hg3mnzYdGJvTt1QoBT+Tqfm30RXH0CXFLiw3JRYuKNzQqOgeE1k+RW3F X-Gm-Gg: AZFdTZct3ENLCW/NIizYycjyoYaIiEvx0pvUn6O1xezRU0rHL+hj5ejz4QTzviLGr3p AOM1EGIHADqyPWriLDQJPWvOCciXur8obJSjRZSonoZ0E72VwvejIsXT//LZCznW5t11nd9cvmn aXiaWt66PehV+IcrJQy42kJRMMIYtvlgQm/I1jTu43TllOkPqzQUnTwpxfsCSP9Ei/MU/7xVYsG 5oYmg0Qxq0w6eFajXVkUT68VFjmoFoLfDgAdc1uYNPcrJIwtzXbPQ3IXxoZOQH6LiJ8Wln0dBSg GbU+T2tQYQjTUazVnZV9WnUF/a7BYbKNDqh0sF1PRQiisK2bWaQ90S21LH0BQ7fv6VSUZV27ezF YQ4FZTWcw55Es4V+O6GCkR5JpSbq/80PlJPcr4Pr/MYRAD2mlgNRfiRNCt+wTPmbv1+sE//pGc6 aDhc7S+GbYv7pSaeWFj+w2RBbu9hrWpU19RRe8I9710aac0obAkyjJAGh0RjFBSczYWuvSa8ZUz kAsgjO16zRIq8HyTzKXCA8QZlC45PrG13bHLzDee04+pPwIHp+yLbQ2XHm8PIigvRVis2iN9abm coSjvTrztyfVmhkBNs4f1nqbxVqkSMLxxlf8Rd6l16l04MkKlCHQ1wUY7tGChqwuXi4RnbmAXxo ek3XcQNA5QKQ1wQtL+77nlJaNzWfsacB9FQ== X-Received: by 2002:a17:90b:5287:b0:3b4:97f:8fc7 with SMTP id 98e67ed59e1d1-3b41c8fe219mr29162294a91.12.1796000228491; Sun, 29 Nov 2026 16:57:08 -0800 (PST) Received: from localhost ([153.61.198.246]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b40578d771sm51334888a91.1.2026.11.29.16.57.07 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Nov 2026 16:57:07 -0800 (PST) Content-Type: text/plain; charset=UTF-8 Date: Mon, 30 Nov 2026 00:57:07 +0000 Message-Id: Subject: Re: [PATCH bpf-next v2 00/13] bpf: Embed local storage values in the storage object From: "Alexei Starovoitov" To: "Mykyta Yatsenko" , , , , , Cc: "Mykyta Yatsenko" In-Reply-To: <20261123-sls_f1-v2-0-76474de9b052@meta.com> References: <20261123-sls_f1-v2-0-76474de9b052@meta.com> Content-Transfer-Encoding: 8bit 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 On Mon, Nov 23, 2026 at 07:15 AM Mykyta Yatsenko wrote: > This series adds the map flag BPF_F_INLINE_VALUE for sk, task, inode > and cgroup storage. The value of such a map is in the storage object, > at a fixed offset, so a lookup has one dependent load less. Sorry for the delay. I guess the whole thing is sort of an alternative to increasing 'struct sock'? But it's not equivalent in terms of reducing cache misses... There are 3 misses: sk->sk_bpf_storage -> storage->cache[idx] -> sdata (smap + value) This series remove the last one... just like a simpler diff below.. There are other low hanging fruits that I'm prototyping that should help more than removal of the last miss: 1. Move sk_bpf_storage to a tx-hot line. It's a one-line change that may remove the first miss. 2. Prefetch early. Under a static key, prefetch sk->sk_bpf_storage in the TCP transmit path, well before tc egress runs. That hides the second miss. 3. Prefetch the sibling. Once cache[] is loaded, the other map's pointer is already there, so the first lookup can do prefetch(cache[B]). > - The tag is predictable, and claim numbers wrap after 2^32 claims. A > user that can create inline maps can make another map see bytes that > it did not write, in objects made before that map. > - The area and the 16-map limit are shared by the whole host, also > with BPF token users. tbh not excited about new uapi and new limit just to remove one miss. I think it's better to keep the selem and change only where its memory comes from. The storage gets a tail. The selem of an inline map takes its memory from the tail, at the same offset in every storage. With the two maps of your example workload of 2 maps: +----------------------------------+ <- sk->sk_bpf_storage 0 | struct bpf_local_storage, 184 b | | cache[16], list, lock, ... | +----------------------------------+ <- tail, units of 64 b 192 | selem of map A: header | 256 | sdata: smap, value of 124 b | <- A->sdata_off +----------------------------------+ 448 | selem of map B: header | 512 | sdata: smap, value of 2 b | <- B->sdata_off +----------------------------------+ 576 It is a normal selem: in storage->list, in the bucket of the map and in cache[]. The lookup stays as it is. The key idea is to add one prefetch of storage + smap->sdata_off in front. Your lookup reads obj_size and the tag. These two loads don't depend on each other. With the prefetch the loads of cache[] and sdata don't depend on each other either. A wrong guess costs nothing. No tag, no claim number, no obj_size, no dying, no new branch, and a weaker uapi addition. this series selem in the tail -------------------------- -------------------- ----------------------- syscall update in place, torn reads new selem, like today BPF_F_LOCK, BPF_F_CLONE no yes kptr, uptr in the value no yes pointer kept after delete sees the next value sees the old value values of a freed map stay behind a tag map_free() unlinks them no room in the area -ENOSPC, -E2BIG selem from kmalloc storage is freed at owner death with its last selem sk iter, sk diag refused, new code no change lookup of a normal map 1 more branch 1 prefetch memory of a value 8 b + value 72 b + value The uapi flag is necessary, but as a hint. It picks the maps that get room in the tail. The whole idea depends on the prefetch in bpf_local_storage_lookup(). Could you pls benchmark it? diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index 6c643ac..c58305a 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -51,9 +52,16 @@ struct bpf_local_storage_map { * multiple buckets to improve contention. */ struct bpf_local_storage_map_bucket *buckets; + struct bpf_local_storage_cache *cache; u32 bucket_log; u16 elem_size; u16 cache_idx; + u16 inline_mask; /* Units of the tail of a storage for the selem + * of this map. 0: none. + */ + u16 sdata_off; /* Offset of its sdata in the storage then. + * Else of cache[cache_idx]. + */ }; struct bpf_local_storage_data { @@ -85,7 +93,8 @@ struct bpf_local_storage_elem { */ }; atomic_t state; - /* 4 bytes hole */ + u16 inline_mask; /* Not 0: the memory is these units of the tail */ + u16 inline_off; /* of the storage that starts inline_off before */ /* The data is stored in another cacheline to minimize * the number of cachelines access during a cache hit. */ @@ -100,10 +109,24 @@ struct bpf_local_storage { */ struct rcu_head rcu; rqspinlock_t lock; /* Protect adding/removing from the "list" */ + atomic_t inline_busy; /* Units of the tail that are in use or not there */ u64 mem_charge; /* Copy of mem charged to owner. Protected by "lock" */ refcount_t owner_refcnt;/* Used to pin owner when map_free is uncharging */ + refcount_t refcnt; /* "list" and each inline selem hold the memory */ }; +/* + * A storage can have a tail with room for the selems of BPF_F_INLINE_VALUE + * maps. All storages of an owner type have the same layout, so that a lookup + * knows where the selem is before it has read cache[]. + */ +#define BPF_LOCAL_STORAGE_INLINE_UNIT SMP_CACHE_BYTES +#define BPF_LOCAL_STORAGE_INLINE_START \ + ALIGN(sizeof(struct bpf_local_storage), BPF_LOCAL_STORAGE_INLINE_UNIT) +#define BPF_LOCAL_STORAGE_INLINE_UNITS \ + MIN(16, (1024 - BPF_LOCAL_STORAGE_INLINE_START) / \ + BPF_LOCAL_STORAGE_INLINE_UNIT) + /* * Drop a reference got by refcount_inc_not_zero(&local_storage->owner_refcnt). * bpf_local_storage_destroy() may have dropped the owner's reference meanwhile @@ -135,6 +158,7 @@ bpf_local_storage_unpin_owner(struct bpf_local_storage *local_storage) struct bpf_local_storage_cache { spinlock_t idx_lock; u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE]; + unsigned long inline_units; /* Units of the tail that maps have */ }; #define DEFINE_BPF_STORAGE_CACHE(name) \ @@ -161,6 +185,9 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage, struct bpf_local_storage_data *sdata; struct bpf_local_storage_elem *selem; + /* Load an inline sdata together with cache[], and not after it */ + prefetch((void *)local_storage + smap->sdata_off); + /* Fast path (cache hit) */ sdata = rcu_dereference_check(local_storage->cache[smap->cache_idx], bpf_rcu_lock_held()); @@ -200,7 +227,8 @@ int bpf_selem_link_map(struct bpf_local_storage_map *smap, struct bpf_local_storage_elem *selem); struct bpf_local_storage_elem * -bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value, +bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, + struct bpf_local_storage *local_storage, void *value, bool swap_uptrs); void bpf_selem_free(struct bpf_local_storage_elem *selem, diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 62de091..377cdc4 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1513,6 +1513,11 @@ enum { /* Enable BPF ringbuf overwrite mode */ BPF_F_RB_OVERWRITE = (1U << 19), + +/* Local storage: most owners have a value of this map. Keep it next to the + * other values of the owner, if there is room. A hint: nothing else changes. + */ + BPF_F_INLINE_VALUE = (1U << 20), }; /* Flags for BPF_PROG_QUERY. */ diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index b12fcec..77c6ce7 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -15,7 +15,11 @@ #include #include -#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE) +#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK \ + (BPF_F_NO_PREALLOC | BPF_F_CLONE | BPF_F_INLINE_VALUE) + +static void bpf_local_storage_free(struct bpf_local_storage *local_storage, + bool reuse_now); static struct bpf_local_storage_map_bucket * select_bucket(struct bpf_local_storage_map *smap, @@ -66,17 +70,84 @@ static bool selem_linked_to_map(const struct bpf_local_storage_elem *selem) return !hlist_unhashed(&selem->map_node); } +/* The storage that has the memory of an inline selem */ +static struct bpf_local_storage * +selem_inline_storage(struct bpf_local_storage_elem *selem) +{ + return selem->inline_mask ? (void *)selem - selem->inline_off : NULL; +} + +/* With room for the selems of all inline maps that the owner type has now */ +static struct bpf_local_storage * +bpf_local_storage_kmalloc(struct bpf_local_storage_map *smap) +{ + u32 units = fls_long(READ_ONCE(smap->cache->inline_units)); + struct bpf_local_storage *storage; + u32 size = sizeof(*storage); + + if (units) + size = BPF_LOCAL_STORAGE_INLINE_START + + units * BPF_LOCAL_STORAGE_INLINE_UNIT; + + storage = bpf_map_kmalloc_nolock(&smap->map, size, __GFP_ZERO, + NUMA_NO_NODE); + if (!storage) + return NULL; + + atomic_set(&storage->inline_busy, ~0U << units); + refcount_set(&storage->refcnt, 1); + return storage; +} + +/* + * Take the memory of the selem from the tail of the storage. Without a storage + * make one: bpf_local_storage_alloc() finds it through the selem. + */ +static struct bpf_local_storage_elem * +bpf_selem_alloc_inline(struct bpf_local_storage_map *smap, + struct bpf_local_storage *storage) +{ + struct bpf_local_storage_elem *selem; + int busy; + + if (!smap->inline_mask) + return NULL; + + if (!storage) + storage = bpf_local_storage_kmalloc(smap); + if (!storage || !refcount_inc_not_zero(&storage->refcnt)) + return NULL; + + busy = atomic_read(&storage->inline_busy); + do { + if (busy & smap->inline_mask) { + bpf_local_storage_free(storage, false); + return NULL; + } + } while (!atomic_try_cmpxchg(&storage->inline_busy, &busy, + busy | smap->inline_mask)); + + selem = SELEM((void *)storage + smap->sdata_off); + memset(selem, 0, smap->elem_size); + selem->inline_mask = smap->inline_mask; + selem->inline_off = (void *)selem - (void *)storage; + return selem; +} + struct bpf_local_storage_elem * bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, - void *value, bool swap_uptrs) + struct bpf_local_storage *local_storage, void *value, + bool swap_uptrs) { struct bpf_local_storage_elem *selem; if (mem_charge(smap, owner, smap->elem_size)) return NULL; - selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, - __GFP_ZERO, NUMA_NO_NODE); + selem = bpf_selem_alloc_inline(smap, local_storage); + if (!selem) + selem = bpf_map_kmalloc_nolock(&smap->map, smap->elem_size, + __GFP_ZERO, NUMA_NO_NODE); if (selem) { RCU_INIT_POINTER(SDATA(selem)->smap, smap); @@ -111,7 +182,7 @@ static void bpf_local_storage_free_trace_rcu(struct rcu_head *rcu) static void bpf_local_storage_free(struct bpf_local_storage *local_storage, bool reuse_now) { - if (!local_storage) + if (!local_storage || !refcount_dec_and_test(&local_storage->refcnt)) return; if (reuse_now) { @@ -123,6 +194,20 @@ static void bpf_local_storage_free(struct bpf_local_storage *local_storage, bpf_local_storage_free_trace_rcu); } +/* Give the memory of an inline selem back to its storage */ +static bool bpf_selem_free_inline(struct bpf_local_storage_elem *selem) +{ + struct bpf_local_storage *local_storage = selem_inline_storage(selem); + + if (!local_storage) + return false; + + atomic_fetch_andnot_release(selem->inline_mask, + &local_storage->inline_busy); + bpf_local_storage_free(local_storage, false); + return true; +} + static void bpf_selem_free_trace_rcu(struct rcu_head *rcu) { struct bpf_local_storage_elem *selem; @@ -138,7 +223,8 @@ static void bpf_selem_free_trace_rcu(struct rcu_head *rcu) * RCU Tasks Trace grace period implies RCU grace period, do * kfree() directly. */ - kfree(selem); + if (!bpf_selem_free_inline(selem)) + kfree(selem); } void bpf_selem_free(struct bpf_local_storage_elem *selem, @@ -151,7 +237,12 @@ void bpf_selem_free(struct bpf_local_storage_elem *selem, if (reuse_now) { if (smap) bpf_obj_free_fields(smap->map.record, SDATA(selem)->data); - kfree_rcu(selem, rcu); + /* + * Nobody has seen the selem, or its owner is dead and gets no + * new selem. The storage waits for RCU readers. + */ + if (!bpf_selem_free_inline(selem)) + kfree_rcu(selem, rcu); return; } @@ -508,12 +599,14 @@ int bpf_local_storage_alloc(void *owner, unsigned long flags; int err; + storage = selem_inline_storage(first_selem); + err = mem_charge(smap, owner, sizeof(*storage)); if (err) - return err; + goto free; - storage = bpf_map_kmalloc_nolock(&smap->map, sizeof(*storage), - __GFP_ZERO, NUMA_NO_NODE); + if (!storage) + storage = bpf_local_storage_kmalloc(smap); if (!storage) { err = -ENOMEM; goto uncharge; @@ -562,8 +655,9 @@ int bpf_local_storage_alloc(void *owner, return 0; uncharge: - bpf_local_storage_free(storage, true); mem_uncharge(smap, owner, sizeof(*storage)); +free: + bpf_local_storage_free(storage, true); return err; } @@ -599,7 +693,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, if (err) return ERR_PTR(err); - selem = bpf_selem_alloc(smap, owner, value, swap_uptrs); + selem = bpf_selem_alloc(smap, owner, NULL, value, swap_uptrs); if (!selem) return ERR_PTR(-ENOMEM); @@ -633,7 +727,8 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, /* A lookup has just been done before and concluded a new selem is * needed. The chance of an unnecessary alloc is unlikely. */ - alloc_selem = selem = bpf_selem_alloc(smap, owner, value, swap_uptrs); + alloc_selem = selem = bpf_selem_alloc(smap, owner, local_storage, value, + swap_uptrs); if (!alloc_selem) return ERR_PTR(-ENOMEM); @@ -729,6 +824,38 @@ static void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cac spin_unlock(&cache->idx_lock); } +/* No room is not an error: all selems of the map come from kmalloc then */ +static void bpf_local_storage_inline_get(struct bpf_local_storage_map *smap) +{ + u32 nr = DIV_ROUND_UP(smap->elem_size, BPF_LOCAL_STORAGE_INLINE_UNIT); + struct bpf_local_storage_cache *cache = smap->cache; + unsigned long start; + + spin_lock(&cache->idx_lock); + start = bitmap_find_next_zero_area(&cache->inline_units, + BPF_LOCAL_STORAGE_INLINE_UNITS, + 0, nr, 0); + if (start < BPF_LOCAL_STORAGE_INLINE_UNITS) { + smap->inline_mask = GENMASK(start + nr - 1, start); + smap->sdata_off = BPF_LOCAL_STORAGE_INLINE_START + + start * BPF_LOCAL_STORAGE_INLINE_UNIT + + offsetof(struct bpf_local_storage_elem, sdata); + WRITE_ONCE(cache->inline_units, + cache->inline_units | smap->inline_mask); + } + spin_unlock(&cache->idx_lock); +} + +static void bpf_local_storage_inline_put(struct bpf_local_storage_map *smap) +{ + struct bpf_local_storage_cache *cache = smap->cache; + + spin_lock(&cache->idx_lock); + WRITE_ONCE(cache->inline_units, + cache->inline_units & ~smap->inline_mask); + spin_unlock(&cache->idx_lock); +} + int bpf_local_storage_map_alloc_check(union bpf_attr *attr) { if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK || @@ -833,7 +960,12 @@ bpf_local_storage_map_alloc(union bpf_attr *attr, smap->elem_size = offsetof(struct bpf_local_storage_elem, sdata.data[attr->value_size]); + smap->cache = cache; smap->cache_idx = bpf_local_storage_cache_idx_get(cache); + smap->sdata_off = offsetof(struct bpf_local_storage, + cache[smap->cache_idx]); + if (attr->map_flags & BPF_F_INLINE_VALUE) + bpf_local_storage_inline_get(smap); return &smap->map; free_smap: @@ -902,6 +1034,8 @@ restart: /* smap remains in use regardless of kmalloc_nolock, so wait unconditionally. */ rcu_barrier_tasks_trace(); rcu_barrier(); + /* The callbacks gave back the memory of the inline selems */ + bpf_local_storage_inline_put(smap); kvfree(smap->buckets); bpf_map_area_free(smap); } diff --git a/net/core/bpf_sk_storage.c b/net/core/bpf_sk_storage.c index 1c59300..41e93ba 100644 --- a/net/core/bpf_sk_storage.c +++ b/net/core/bpf_sk_storage.c @@ -132,12 +132,13 @@ static long bpf_fd_sk_storage_delete_elem(struct bpf_map *map, void *key) static struct bpf_local_storage_elem * bpf_sk_storage_clone_elem(struct sock *newsk, + struct bpf_local_storage *new_sk_storage, struct bpf_local_storage_map *smap, struct bpf_local_storage_elem *selem) { struct bpf_local_storage_elem *copy_selem; - copy_selem = bpf_selem_alloc(smap, newsk, NULL, false); + copy_selem = bpf_selem_alloc(smap, newsk, new_sk_storage, NULL, false); if (!copy_selem) return NULL; @@ -189,7 +190,8 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk) if (IS_ERR(map)) continue; - copy_selem = bpf_sk_storage_clone_elem(newsk, smap, selem); + copy_selem = bpf_sk_storage_clone_elem(newsk, new_sk_storage, + smap, selem); if (!copy_selem) { ret = -ENOMEM; bpf_map_put(map); pw-bot: cr