Skip to content

Commit 8af8112

Browse files
ameryhungKernel Patches Daemon
authored andcommitted
bpf: Save memory alloction info in bpf_local_storage
Save the memory allocation method used for bpf_local_storage in the struct explicitly so that we don't need to go through the hassle to find out the info. When a later patch replaces BPF memory allocator with kmalloc_noloc(), bpf_local_storage_free() will no longer need smap->storage_ma to return the memory and completely remove the dependency on smap in bpf_local_storage_free(). Signed-off-by: Amery Hung <[email protected]>
1 parent b8f0a2f commit 8af8112

File tree

2 files changed

+9
-44
lines changed

2 files changed

+9
-44
lines changed

include/linux/bpf_local_storage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ struct bpf_local_storage {
9797
*/
9898
struct rcu_head rcu;
9999
raw_spinlock_t lock; /* Protect adding/removing from the "list" */
100+
bool bpf_ma;
100101
};
101102

102103
/* U16_MAX is much more than enough for sk local storage

kernel/bpf/bpf_local_storage.c

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ static void __bpf_local_storage_free(struct bpf_local_storage *local_storage,
157157

158158
static void bpf_local_storage_free(struct bpf_local_storage *local_storage,
159159
struct bpf_local_storage_map *smap,
160-
bool bpf_ma, bool reuse_now)
160+
bool reuse_now)
161161
{
162162
if (!local_storage)
163163
return;
164164

165-
if (!bpf_ma) {
165+
if (!local_storage->bpf_ma) {
166166
__bpf_local_storage_free(local_storage, reuse_now);
167167
return;
168168
}
@@ -336,47 +336,12 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor
336336
return free_local_storage;
337337
}
338338

339-
static bool check_storage_bpf_ma(struct bpf_local_storage *local_storage,
340-
struct bpf_local_storage_map *storage_smap,
341-
struct bpf_local_storage_elem *selem)
342-
{
343-
344-
struct bpf_local_storage_map *selem_smap;
345-
346-
/* local_storage->smap may be NULL. If it is, get the bpf_ma
347-
* from any selem in the local_storage->list. The bpf_ma of all
348-
* local_storage and selem should have the same value
349-
* for the same map type.
350-
*
351-
* If the local_storage->list is already empty, the caller will not
352-
* care about the bpf_ma value also because the caller is not
353-
* responsible to free the local_storage.
354-
*/
355-
356-
if (storage_smap)
357-
return storage_smap->bpf_ma;
358-
359-
if (!selem) {
360-
struct hlist_node *n;
361-
362-
n = rcu_dereference_check(hlist_first_rcu(&local_storage->list),
363-
bpf_rcu_lock_held());
364-
if (!n)
365-
return false;
366-
367-
selem = hlist_entry(n, struct bpf_local_storage_elem, snode);
368-
}
369-
selem_smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held());
370-
371-
return selem_smap->bpf_ma;
372-
}
373-
374339
static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
375340
bool reuse_now)
376341
{
377342
struct bpf_local_storage_map *storage_smap;
378343
struct bpf_local_storage *local_storage;
379-
bool bpf_ma, free_local_storage = false;
344+
bool free_local_storage = false;
380345
HLIST_HEAD(selem_free_list);
381346
unsigned long flags;
382347

@@ -388,7 +353,6 @@ static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
388353
bpf_rcu_lock_held());
389354
storage_smap = rcu_dereference_check(local_storage->smap,
390355
bpf_rcu_lock_held());
391-
bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, selem);
392356

393357
raw_spin_lock_irqsave(&local_storage->lock, flags);
394358
if (likely(selem_linked_to_storage(selem)))
@@ -399,7 +363,7 @@ static void bpf_selem_unlink_storage(struct bpf_local_storage_elem *selem,
399363
bpf_selem_free_list(&selem_free_list, reuse_now);
400364

401365
if (free_local_storage)
402-
bpf_local_storage_free(local_storage, storage_smap, bpf_ma, reuse_now);
366+
bpf_local_storage_free(local_storage, storage_smap, reuse_now);
403367
}
404368

405369
void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
@@ -506,6 +470,7 @@ int bpf_local_storage_alloc(void *owner,
506470
INIT_HLIST_HEAD(&storage->list);
507471
raw_spin_lock_init(&storage->lock);
508472
storage->owner = owner;
473+
storage->bpf_ma = smap->bpf_ma;
509474

510475
bpf_selem_link_storage_nolock(storage, first_selem);
511476
bpf_selem_link_map(smap, first_selem);
@@ -542,7 +507,7 @@ int bpf_local_storage_alloc(void *owner,
542507
return 0;
543508

544509
uncharge:
545-
bpf_local_storage_free(storage, smap, smap->bpf_ma, true);
510+
bpf_local_storage_free(storage, smap, true);
546511
mem_uncharge(smap, owner, sizeof(*storage));
547512
return err;
548513
}
@@ -731,13 +696,12 @@ void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
731696
{
732697
struct bpf_local_storage_map *storage_smap;
733698
struct bpf_local_storage_elem *selem;
734-
bool bpf_ma, free_storage = false;
699+
bool free_storage = false;
735700
HLIST_HEAD(free_selem_list);
736701
struct hlist_node *n;
737702
unsigned long flags;
738703

739704
storage_smap = rcu_dereference_check(local_storage->smap, bpf_rcu_lock_held());
740-
bpf_ma = check_storage_bpf_ma(local_storage, storage_smap, NULL);
741705

742706
/* Neither the bpf_prog nor the bpf_map's syscall
743707
* could be modifying the local_storage->list now.
@@ -768,7 +732,7 @@ void bpf_local_storage_destroy(struct bpf_local_storage *local_storage)
768732
bpf_selem_free_list(&free_selem_list, true);
769733

770734
if (free_storage)
771-
bpf_local_storage_free(local_storage, storage_smap, bpf_ma, true);
735+
bpf_local_storage_free(local_storage, storage_smap, true);
772736
}
773737

774738
u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map)

0 commit comments

Comments
 (0)