This is a note to let you know that I've just added the patch titled bpf: fix a memory leak in the LRU and LRU_PERCPU hash maps to the 6.1-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: bpf-fix-a-memory-leak-in-the-lru-and-lru_percpu-hash-maps.patch and it can be found in the queue-6.1 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From b34ffb0c6d23583830f9327864b9c1f486003305 Mon Sep 17 00:00:00 2001 From: Anton Protopopov <aspsk@xxxxxxxxxxxxx> Date: Mon, 22 May 2023 15:45:58 +0000 Subject: bpf: fix a memory leak in the LRU and LRU_PERCPU hash maps From: Anton Protopopov <aspsk@xxxxxxxxxxxxx> commit b34ffb0c6d23583830f9327864b9c1f486003305 upstream. The LRU and LRU_PERCPU maps allocate a new element on update before locking the target hash table bucket. Right after that the maps try to lock the bucket. If this fails, then maps return -EBUSY to the caller without releasing the allocated element. This makes the element untracked: it doesn't belong to either of free lists, and it doesn't belong to the hash table, so can't be re-used; this eventually leads to the permanent -ENOMEM on LRU map updates, which is unexpected. Fix this by returning the element to the local free list if bucket locking fails. Fixes: 20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked") Signed-off-by: Anton Protopopov <aspsk@xxxxxxxxxxxxx> Link: https://lore.kernel.org/r/20230522154558.2166815-1-aspsk@xxxxxxxxxxxxx Signed-off-by: Martin KaFai Lau <martin.lau@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- kernel/bpf/hashtab.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1203,7 +1203,7 @@ static int htab_lru_map_update_elem(stru ret = htab_lock_bucket(htab, b, hash, &flags); if (ret) - return ret; + goto err_lock_bucket; l_old = lookup_elem_raw(head, hash, key, key_size); @@ -1224,6 +1224,7 @@ static int htab_lru_map_update_elem(stru err: htab_unlock_bucket(htab, b, hash, flags); +err_lock_bucket: if (ret) htab_lru_push_free(htab, l_new); else if (l_old) @@ -1326,7 +1327,7 @@ static int __htab_lru_percpu_map_update_ ret = htab_lock_bucket(htab, b, hash, &flags); if (ret) - return ret; + goto err_lock_bucket; l_old = lookup_elem_raw(head, hash, key, key_size); @@ -1349,6 +1350,7 @@ static int __htab_lru_percpu_map_update_ ret = 0; err: htab_unlock_bucket(htab, b, hash, flags); +err_lock_bucket: if (l_new) bpf_lru_push_free(&htab->lru, &l_new->lru_node); return ret; Patches currently in stable-queue which might be from aspsk@xxxxxxxxxxxxx are queue-6.1/bpf-fix-a-memory-leak-in-the-lru-and-lru_percpu-hash-maps.patch