From nobody Sun Feb 8 11:26:39 2026 Received: from out-179.mta0.migadu.com (out-179.mta0.migadu.com [91.218.175.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4BEAC37C117 for ; Mon, 19 Jan 2026 14:23:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832637; cv=none; b=ghwF7MvyutdTvZhdwbgNUF7alblkJfISOdMUUVJ57kP6OpcJOaQRfWBcXguYeycvrfo7keH0W96CC9pJX6Y5x1AwCzdXAymEYIwAJ43Ap4yLam0XX2QQsowjyzz78+DQ/ysh/dbuCRBTD1SOZqVGbYyb7pJzOg9ALHpS4kDK0uE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832637; c=relaxed/simple; bh=l+kwMUsQimoxGi8L7uWCc3QAdNgoR9d+OzSkHX2cqgs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WILwuZn+I8j2wGDZu0e2JyhuD+tXz7gxiLUIcO1tgRuOUMQxz4c96afXjYRQXmp78GhknOiPZCsh8UMoMmdyfVzdP116Mz8geBbq1HI23sUbAPqlsjN1naLCvxzYJi4wAyPUWocV/7x/p5hoIwrsrxqA891vMhGz9pF1kfFle2c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=oJJEhNwI; arc=none smtp.client-ip=91.218.175.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="oJJEhNwI" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1768832632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ny2Dbz5+1e/+8ujacpK/pSGHIZdtzJne97Rc5EYzZe4=; b=oJJEhNwI3HdqtrY/iRjl8AyZVFW2Zqc6yAgTfNdIJ0mbOfw6SnIpV43lT1vt9U3t3IhW9q AedoTn5RF84LH81JkpDZauxFxDpoMFjjtp/eVB48BQ+URCLP/L+bWSJfp9+I24wTMn4cuV ziUTKGl19jXu/luD8sCrwzn0IhCAAEA= From: Leon Hwang To: bpf@vger.kernel.org Cc: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next 1/3] bpf: Factor out bpf_lru_node_set_hash() helper Date: Mon, 19 Jan 2026 22:21:18 +0800 Message-ID: <20260119142120.28170-2-leon.hwang@linux.dev> In-Reply-To: <20260119142120.28170-1-leon.hwang@linux.dev> References: <20260119142120.28170-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" The hash field is not used directly by the LRU list itself; it is consumed by the 'del_from_htab' callback when removing entries from the hash map. The hash initialization must be performed under the LRU lock to avoid a race where a popped LRU node is evicted and deleted from the hash map with an uninitialized hash value, if defer the hash setting to hashtab.c::prealloc_lru_pop(). Factor out a dedicated bpf_lru_node_set_hash() helper and document this requirement to make the ordering and locking constraints explicit. Signed-off-by: Leon Hwang --- kernel/bpf/bpf_lru_list.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/kernel/bpf/bpf_lru_list.c b/kernel/bpf/bpf_lru_list.c index e7a2fc60523f..c091f3232cc5 100644 --- a/kernel/bpf/bpf_lru_list.c +++ b/kernel/bpf/bpf_lru_list.c @@ -341,13 +341,27 @@ static void bpf_lru_list_pop_free_to_local(struct bpf= _lru *lru, raw_spin_unlock(&l->lock); } =20 +/* + * The hash field is consumed by the 'del_from_htab' callback rather than + * the LRU list itself. Initialize it while holding the LRU lock to avoid + * a race where a popped LRU node is evicted and removed from the hash map + * with an uninitialized hash value, if defer the hash setting to + * hashtab.c::prealloc_lru_pop(). + */ +static void bpf_lru_node_set_hash(struct bpf_lru *lru, + struct bpf_lru_node *node, + u32 hash) +{ + *(u32 *)((void *)node + lru->hash_offset) =3D hash; +} + static void __local_list_add_pending(struct bpf_lru *lru, struct bpf_lru_locallist *loc_l, int cpu, struct bpf_lru_node *node, u32 hash) { - *(u32 *)((void *)node + lru->hash_offset) =3D hash; + bpf_lru_node_set_hash(lru, node, hash); node->cpu =3D cpu; node->type =3D BPF_LRU_LOCAL_LIST_T_PENDING; bpf_lru_node_clear_ref(node); @@ -415,7 +429,7 @@ static struct bpf_lru_node *bpf_percpu_lru_pop_free(str= uct bpf_lru *lru, =20 if (!list_empty(free_list)) { node =3D list_first_entry(free_list, struct bpf_lru_node, list); - *(u32 *)((void *)node + lru->hash_offset) =3D hash; + bpf_lru_node_set_hash(lru, node, hash); bpf_lru_node_clear_ref(node); __bpf_lru_node_move(l, node, BPF_LRU_LIST_T_INACTIVE); } --=20 2.52.0 From nobody Sun Feb 8 11:26:39 2026 Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2FEEB35CB6B for ; Mon, 19 Jan 2026 14:24:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832648; cv=none; b=fMjbFnMEv7P7gWcI+w1vV0o7ZraPHLQsmym7rrUyByjQX/wD4Uu8mDBkm/chu30LXIwYc2kAOa6CZfnlep6PfXIGr7vJLAg9VFGDUlMyZOXtndrysum6XmILtu3Xd83zxC8Hr7qjl/34fuI8hMCVkGIuKAPEunCkmajKlLHjVNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832648; c=relaxed/simple; bh=oayvu13mdV7aSgVp1rBKytLvqsfxdo+vvFuy6pb+7uY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=a88JdSb7HV1M6I3RS40pQhavj7NzsT5Z5mGkmt82gOp2EwyBBC8zIG2PT1J4I2wr1GR1WFjQfQxJOfPPjCZ1WvbTrKQWl4P7H55NvHeU4kiNmEuBdZU5lXG89LG3XqnJ7W638jQcIBQ8heZbQ2T3qNC1cSQhiZSSYGWFF59Q+Bs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=uHxDawZl; arc=none smtp.client-ip=91.218.175.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="uHxDawZl" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1768832644; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4Eq1NLSrw3mzgr+D9bwwnsXLMtRZs1wD5aWZcftrom8=; b=uHxDawZl5wKLRvTYnFa2iHf4BpkQMRh6QHtbyUksBI6UNsHnxGh16L+tTDM5PWyTbdTJU4 xIJN9QYJIiaOCBbwJUG/wsaBnovufi5hb+3BSg65PcAjv14yjxGHglWJljUJmEikLtpJFD /N6NnMMvvHbKoVfzY8sK0YqtWlvphng= From: Leon Hwang To: bpf@vger.kernel.org Cc: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next 2/3] bpf: Avoid deadlock using trylock when popping LRU free nodes Date: Mon, 19 Jan 2026 22:21:19 +0800 Message-ID: <20260119142120.28170-3-leon.hwang@linux.dev> In-Reply-To: <20260119142120.28170-1-leon.hwang@linux.dev> References: <20260119142120.28170-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Switch the free-node pop paths to raw_spin_trylock*() to avoid blocking on contended LRU locks. If the global or per-CPU LRU lock is unavailable, refuse to refill the local free list and return NULL instead. This allows callers to back off safely rather than blocking or re-entering the same lock context. This change avoids lockdep warnings and potential deadlocks caused by re-entrant LRU lock acquisition from NMI context, as shown below: [ 418.260323] bpf_testmod: oh no, recursing into test_1, recursion_misses 1 [ 424.982207] =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ 424.982216] WARNING: inconsistent lock state [ 424.982223] inconsistent {INITIAL USE} -> {IN-NMI} usage. [ 424.982314] *** DEADLOCK *** [...] Signed-off-by: Leon Hwang --- kernel/bpf/bpf_lru_list.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/bpf_lru_list.c b/kernel/bpf/bpf_lru_list.c index c091f3232cc5..03d37f72731a 100644 --- a/kernel/bpf/bpf_lru_list.c +++ b/kernel/bpf/bpf_lru_list.c @@ -312,14 +312,15 @@ static void bpf_lru_list_push_free(struct bpf_lru_lis= t *l, raw_spin_unlock_irqrestore(&l->lock, flags); } =20 -static void bpf_lru_list_pop_free_to_local(struct bpf_lru *lru, +static bool bpf_lru_list_pop_free_to_local(struct bpf_lru *lru, struct bpf_lru_locallist *loc_l) { struct bpf_lru_list *l =3D &lru->common_lru.lru_list; struct bpf_lru_node *node, *tmp_node; unsigned int nfree =3D 0; =20 - raw_spin_lock(&l->lock); + if (!raw_spin_trylock(&l->lock)) + return false; =20 __local_list_flush(l, loc_l); =20 @@ -339,6 +340,8 @@ static void bpf_lru_list_pop_free_to_local(struct bpf_l= ru *lru, BPF_LRU_LOCAL_LIST_T_FREE); =20 raw_spin_unlock(&l->lock); + + return true; } =20 /* @@ -418,7 +421,8 @@ static struct bpf_lru_node *bpf_percpu_lru_pop_free(str= uct bpf_lru *lru, =20 l =3D per_cpu_ptr(lru->percpu_lru, cpu); =20 - raw_spin_lock_irqsave(&l->lock, flags); + if (!raw_spin_trylock_irqsave(&l->lock, flags)) + return NULL; =20 __bpf_lru_list_rotate(lru, l); =20 @@ -451,13 +455,12 @@ static struct bpf_lru_node *bpf_common_lru_pop_free(s= truct bpf_lru *lru, =20 loc_l =3D per_cpu_ptr(clru->local_list, cpu); =20 - raw_spin_lock_irqsave(&loc_l->lock, flags); + if (!raw_spin_trylock_irqsave(&loc_l->lock, flags)) + return NULL; =20 node =3D __local_list_pop_free(loc_l); - if (!node) { - bpf_lru_list_pop_free_to_local(lru, loc_l); + if (!node && bpf_lru_list_pop_free_to_local(lru, loc_l)) node =3D __local_list_pop_free(loc_l); - } =20 if (node) __local_list_add_pending(lru, loc_l, cpu, node, hash); --=20 2.52.0 From nobody Sun Feb 8 11:26:39 2026 Received: from out-178.mta0.migadu.com (out-178.mta0.migadu.com [91.218.175.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C71D537F0FE for ; Mon, 19 Jan 2026 14:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832657; cv=none; b=qbo6PoUOLn4n4P3oqGFHwGnqKndfJn0Y7A9j3xOX721kasPe2doyrAsU/ka8W82Zg1QLGkfWFoUESHNEcWxZrmITw68n6BHcJ5ZM5meRUcqgWzKTxbNduQXhN7Sqstgqq0EWOXKDdf+ks71zx+PK8/ljqXuiPGJqQ5+375RLdzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768832657; c=relaxed/simple; bh=n9sZEwW8aRs9KO1WTVOCaIs4K7fiu1d+TWNIVzf1MeI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=liNz1sFqywSvkdVrtnibcW+x8e1jpK2Pc+tLGDb9T8bJF1Puf5ecE2a+gXTWYm7q4KNtBY3+EO2MOu2nG3aPnvnuzhiHDijL9bCxxupGQbirl7h24reGpyIZz6MOVWJQ1MI8S2EmROsjtQ7QTgPlXfXHNble03eOb+USH0mOzLc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=vLTnVnw4; arc=none smtp.client-ip=91.218.175.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="vLTnVnw4" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1768832653; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GtM8pK4JNsrvpwbWfCZ7E1yYuWQlGOAHpNcvj0+XSbY=; b=vLTnVnw4hZZv3DMC0cGRCLjxlbxdMh+uIR7+Yqw+yHfZH2+kt6u5tJvWCkObYu2hysHOAZ lIITiOxtYG6WyWqLNZ1J8+01O8kMGXTwj1UISpyZy4F0Ng0Ru9yh7hPZ4M6MtC9m0JiEUq t7qabj0fl50qqiyco7KvEyhfMDbKDes= From: Leon Hwang To: bpf@vger.kernel.org Cc: Martin KaFai Lau , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Leon Hwang , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-patches-bot@fb.com Subject: [PATCH bpf-next 3/3] selftests/bpf: Allow -ENOMEM on LRU map updates Date: Mon, 19 Jan 2026 22:21:20 +0800 Message-ID: <20260119142120.28170-4-leon.hwang@linux.dev> In-Reply-To: <20260119142120.28170-1-leon.hwang@linux.dev> References: <20260119142120.28170-1-leon.hwang@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" LRU hash map updates may legitimately return -ENOMEM. Relax the percpu stats selftest to accept that error for LRU map types so it matches the map's expected behavior. Signed-off-by: Leon Hwang --- tools/testing/selftests/bpf/map_tests/map_percpu_stats.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c b/too= ls/testing/selftests/bpf/map_tests/map_percpu_stats.c index 1c7c04288eff..d510a9c54978 100644 --- a/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c +++ b/tools/testing/selftests/bpf/map_tests/map_percpu_stats.c @@ -188,7 +188,8 @@ static void *patch_map_thread(void *arg) 40, retry_for_nomem_fn); else ret =3D bpf_map_update_elem(opts->map_fd, &i, val_ptr, 0); - CHECK(ret < 0, "bpf_map_update_elem", "key=3D%d error: %s\n", i, strerro= r(errno)); + CHECK(ret < 0 && (!is_lru(opts->map_type) || ret !=3D -ENOMEM), + "bpf_map_update_elem", "key=3D%d error: %s\n", i, strerror(errno)); =20 if (opts->map_type =3D=3D BPF_MAP_TYPE_HASH_OF_MAPS) close(val); --=20 2.52.0