From: Mykyta Yatsenko A later patch lets programs get pointers to values in the inline area. Such a value has no selem, so an empty list does not mean that nobody uses the object. Free an object with an inline area only after destroy(), and always after an RCU tasks trace grace period, so that sleepable programs cannot see freed memory. Updaters can no longer use an empty list to see that the object goes away, so destroy() marks it as dying under the storage lock. If it cannot take that lock, it tries again after a grace period, so the mark adds no leak. A failed unlink of an selem leaks as before. While an inline map exists, new objects of its owner type are bigger and live until owner death, also when the owner uses only normal maps. Signed-off-by: Mykyta Yatsenko --- include/linux/bpf_local_storage.h | 11 +++++ kernel/bpf/bpf_local_storage.c | 96 +++++++++++++++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index f8e4e47b47c6..7a5f024622e6 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -111,8 +111,19 @@ struct bpf_local_storage { u32 obj_size; /* Size of this object. It does not change. */ 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 */ + bool dying; /* destroy() has run. Only with an inline area */ }; +/* + * A program can hold a pointer into the inline area without an selem. So an + * object with an inline area lives until its owner goes away. + */ +static inline bool +bpf_local_storage_has_inline(const struct bpf_local_storage *local_storage) +{ + return local_storage->obj_size > sizeof(*local_storage); +} + /* * 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 diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 072807a1245f..b737b2e67c8a 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -114,7 +114,12 @@ static void bpf_local_storage_free(struct bpf_local_storage *local_storage, if (!local_storage) return; - if (reuse_now) { + /* + * A sleepable program can still use an inline value: wait for it. A + * tasks trace grace period also covers plain RCU readers: RCU Tasks + * Trace is SRCU-fast, and its grace period calls synchronize_rcu(). + */ + if (reuse_now && !bpf_local_storage_has_inline(local_storage)) { kfree_rcu(local_storage, rcu); return; } @@ -184,6 +189,31 @@ static void bpf_selem_free_list(struct hlist_head *list, bool reuse_now) bpf_selem_free(selem, reuse_now); } +/* + * Free local_storage when selem, its last selem, goes away. An object with an + * inline area can hold values with no selem, so free it only after destroy() + * marked it. + */ +static bool bpf_local_storage_free_last(struct bpf_local_storage *local_storage, + struct bpf_local_storage_elem *selem) +{ + if (!hlist_is_singular_node(&selem->snode, &local_storage->list)) + return false; + return !bpf_local_storage_has_inline(local_storage) || + READ_ONCE(local_storage->dying); +} + +/* + * local_storage goes away: the owner needs a new one for a new value. An + * object with an inline area can be empty, so destroy() marks it instead. + */ +static bool bpf_local_storage_gone(const struct bpf_local_storage *local_storage) +{ + if (bpf_local_storage_has_inline(local_storage)) + return READ_ONCE(local_storage->dying); + return hlist_empty(&local_storage->list); +} + 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, @@ -227,8 +257,7 @@ static bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_stor smap = rcu_dereference_check(SDATA(selem)->smap, bpf_rcu_lock_held()); - free_local_storage = hlist_is_singular_node(&selem->snode, - &local_storage->list); + free_local_storage = bpf_local_storage_free_last(local_storage, selem); bpf_selem_unlink_storage_nolock_misc(selem, smap, local_storage, free_local_storage, false); @@ -440,8 +469,8 @@ static void bpf_selem_unlink_nofail(struct bpf_local_storage_elem *selem, err = raw_res_spin_lock_irqsave(&local_storage->lock, flags); if (!err) { if (likely(selem_linked_to_storage(selem))) { - free_storage = hlist_is_singular_node(&selem->snode, - &local_storage->list); + free_storage = bpf_local_storage_free_last(local_storage, + selem); /* * Okay to skip clearing owner_storage and storage->owner in * destroy() since the owner is going away. No user or bpf @@ -634,7 +663,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, local_storage = rcu_dereference_check(*owner_storage(smap, owner), bpf_rcu_lock_held()); - if (!local_storage || hlist_empty(&local_storage->list)) { + if (!local_storage || bpf_local_storage_gone(local_storage)) { /* Very first elem for the owner */ err = check_flags(NULL, map_flags); if (err) @@ -683,7 +712,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, goto free_selem; /* Recheck local_storage->list under local_storage->lock */ - if (unlikely(hlist_empty(&local_storage->list))) { + if (unlikely(bpf_local_storage_gone(local_storage))) { /* A parallel del is happening and local_storage is going * away. It has just been checked before, so very * unlikely. Return instead of retry to keep things @@ -901,6 +930,48 @@ int bpf_local_storage_map_check_btf(struct bpf_map *map, return 0; } +/* + * A storage is freed when its last selem goes away. An object with an inline + * area can hold values with an empty list, so mark it as dying under the + * lock: from now on updaters return -EAGAIN, and the last selem frees the + * object. Free an empty object now. + * + * If destroy() could not take the lock to unlink an selem, the selem stays + * linked. A racing map free that read its storage pointer before destroy() + * cleared it unlinks it and frees the object. Else the object leaks, as the + * selem does: map free skips the storage, and bpf_selem_unlink_nofail() + * warns. + * + * Only a timeout can fail the lock here: destroy() holds no other lock. + * Then try again after a grace period. Nothing frees the object while dying + * is false, so its rcu head is free. destroy() never runs in NMI. + */ +static void bpf_local_storage_mark_dying_rcu(struct rcu_head *rcu); + +static void bpf_local_storage_mark_dying(struct bpf_local_storage *local_storage) +{ + unsigned long flags; + bool empty; + + if (raw_res_spin_lock_irqsave(&local_storage->lock, flags)) { + call_rcu_tasks_trace(&local_storage->rcu, + bpf_local_storage_mark_dying_rcu); + return; + } + WRITE_ONCE(local_storage->dying, true); + empty = hlist_empty(&local_storage->list); + raw_res_spin_unlock_irqrestore(&local_storage->lock, flags); + + if (empty) + bpf_local_storage_free(local_storage, false); +} + +static void bpf_local_storage_mark_dying_rcu(struct rcu_head *rcu) +{ + bpf_local_storage_mark_dying(container_of(rcu, struct bpf_local_storage, + rcu)); +} + /* * Destroy local storage when the owner is going away. Caller must uncharge memory * if memory charging is used. @@ -908,6 +979,7 @@ int bpf_local_storage_map_check_btf(struct bpf_map *map, u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage) { struct bpf_local_storage_elem *selem; + u32 mem; /* Neither the bpf_prog nor the bpf_map's syscall * could be modifying the local_storage->list now. @@ -931,7 +1003,15 @@ u32 bpf_local_storage_destroy(struct bpf_local_storage *local_storage) smp_mb(); } - return local_storage->mem_charge; + /* + * The loop must not free an object with an inline area: only the mark + * lets the last selem free it, so mark it after the loop. The mark can + * free the object, so read mem_charge first. + */ + mem = local_storage->mem_charge; + if (bpf_local_storage_has_inline(local_storage)) + bpf_local_storage_mark_dying(local_storage); + return mem; } u64 bpf_local_storage_map_mem_usage(const struct bpf_map *map) -- 2.53.1-Meta