From nobody Sat Feb 7 15:09:58 2026 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (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 27B70363377; Thu, 30 Oct 2025 15:25:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837939; cv=none; b=QHCaVXCmyuFEdsyFGwhefXzZhXIlS7IcfZpNgSb88nQfc12T5RkcEv75gydaJod8pWFkln4bX6AcAkeePqlEd5v4f1wzw/N9ceuWagnzqo1BFBlOlq87K1DX6woSwXyALkEtHWorPQulDwyPqbbXk9p5cf6XTxe+rerjHRDYT4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837939; c=relaxed/simple; bh=Yzwzxilhtx1Dgk+h+/t4mnPaUIxRfXCRt2Cs2zg0eGA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eB4ySoVgNo5rEYUmBnR0q6wCsjyp5Jy6xE3dZmWaaGk7xvRL8eg8HMYgqTRZGXnp0uFTWcoQTGWrH1yUowRjsXsJ2j7hIYpxIi+Hz1rW6CP4IIqdfE1mj/aIbHwKOOSd2H+kM+KPuqbI39PZA2FZIv5lAselFgitR5BzVNUcEUo= 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=cDchMR8v; arc=none smtp.client-ip=95.215.58.181 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="cDchMR8v" 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=1761837936; 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=FMIKbvL/Yl5vdyppYR1GBXYBoknYHgCa29cF1TMUEDc=; b=cDchMR8vhlYumlAnZchXQlZPW+Rq/xn81iLfUNjpl8TLj4mm0Q8AdhFmNqGFdzI5Tb76Rb xhSp5Ld/iEE67NTsDpUq5XUqDtov4JPfBu4neNvV6G/khvrEyP0QL/BKg5jo3hMOnwhleh DaeSHwXqTln8/64/XCVMgDwI4MPTge0= From: Leon Hwang To: bpf@vger.kernel.org Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, ameryhung@gmail.com, linux-kernel@vger.kernel.org, kernel-patches-bot@fb.com, Leon Hwang Subject: [PATCH bpf-next v4 1/4] bpf: Free special fields when update [lru_,]percpu_hash maps Date: Thu, 30 Oct 2025 23:24:48 +0800 Message-ID: <20251030152451.62778-2-leon.hwang@linux.dev> In-Reply-To: <20251030152451.62778-1-leon.hwang@linux.dev> References: <20251030152451.62778-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" As [lru_,]percpu_hash maps support BPF_KPTR_{REF,PERCPU}, missing calls to 'bpf_obj_free_fields()' in 'pcpu_copy_value()' could cause the memory referenced by BPF_KPTR_{REF,PERCPU} fields to be held until the map gets freed. Fix this by calling 'bpf_obj_free_fields()' after 'copy_map_value[,_long]()' in 'pcpu_copy_value()'. Fixes: 65334e64a493 ("bpf: Support kptrs in percpu hashmap and percpu LRU h= ashmap") Signed-off-by: Leon Hwang --- kernel/bpf/hashtab.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index f876f09355f0d..3861f28a6be81 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -937,12 +937,14 @@ static void pcpu_copy_value(struct bpf_htab *htab, vo= id __percpu *pptr, if (!onallcpus) { /* copy true value_size bytes */ copy_map_value(&htab->map, this_cpu_ptr(pptr), value); + bpf_obj_free_fields(htab->map.record, this_cpu_ptr(pptr)); } else { u32 size =3D round_up(htab->map.value_size, 8); int off =3D 0, cpu; =20 for_each_possible_cpu(cpu) { copy_map_value_long(&htab->map, per_cpu_ptr(pptr, cpu), value + off); + bpf_obj_free_fields(htab->map.record, per_cpu_ptr(pptr, cpu)); off +=3D size; } } --=20 2.51.1 From nobody Sat Feb 7 15:09:58 2026 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.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 5F42336337A for ; Thu, 30 Oct 2025 15:25:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837947; cv=none; b=Dhv7mp7zY2oVMOFX3IBd24umH5wLbwOKFngunPe+KYInVyOTZqMRK7gh6kIrvHuOWpDMdII64Ifd9pQKDYrcChltUZL+8oYoc7JbRfH/ypBKaqjWijwi8bUWv+3vumTllr4NgtqIoLIBcSv734eltjoMvfzu0ZEC7g67CC3wEso= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837947; c=relaxed/simple; bh=C1VYz5QuVV63H7XNPupCntSTAeRkeenISLIRXxOgMNA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SxEMrko/4H1NWn4zQXZJWR6XEL6by0TnnNsJLWk5xhRCRJBsD64bE2QLDt1R7HTfUpk4+iHX2nPC366yo1XyEIjk+1EbIinQ/5rdwFNuK9s9ME8P8gA0YLK1afsP9MFDwDdwbIcZw4tvIknMjMSx0t7dHguCGrf9uN9Hd+SJQWU= 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=rkPTnvfB; arc=none smtp.client-ip=95.215.58.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="rkPTnvfB" 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=1761837943; 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=ZREJDifRPsmjHO+i1eo1SrExYnnJEjUJF7OJxIRlEuQ=; b=rkPTnvfBfMhAces2LgaFJDOLgHIQ7RUYmPAue1jMXv8YcPwDxWxUb3iQu04o4Lt385OuDr 9HaVJhWBhxXGBYrTtI9UfONq34UvhIGFK1HDHFkhGdK8zp932dkrlb0QMrd12cHqeI3vjf hWyWglnkr54TAGsAu0BXmkc67JWquLU= From: Leon Hwang To: bpf@vger.kernel.org Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, ameryhung@gmail.com, linux-kernel@vger.kernel.org, kernel-patches-bot@fb.com, Leon Hwang Subject: [PATCH bpf-next v4 2/4] bpf: Free special fields when update hash maps with BPF_F_LOCK Date: Thu, 30 Oct 2025 23:24:49 +0800 Message-ID: <20251030152451.62778-3-leon.hwang@linux.dev> In-Reply-To: <20251030152451.62778-1-leon.hwang@linux.dev> References: <20251030152451.62778-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" When updating hash maps with BPF_F_LOCK, the special fields were not freed after being replaced. This could cause memory referenced by BPF_KPTR_{REF,PERCPU} fields to be held until the map gets freed. Fix this by calling 'check_and_free_fields()' after 'copy_map_value_locked()' to properly release the old fields. Fixes: 14a324f6a67e ("bpf: Wire up freeing of referenced kptr") Signed-off-by: Leon Hwang --- kernel/bpf/hashtab.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 3861f28a6be81..fc3c7ede3cd0c 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1110,6 +1110,7 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, copy_map_value_locked(map, htab_elem_value(l_old, key_size), value, false); + check_and_free_fields(htab, l_old); return 0; } /* fall through, grab the bucket lock and lookup again. @@ -1138,6 +1139,7 @@ static long htab_map_update_elem(struct bpf_map *map,= void *key, void *value, copy_map_value_locked(map, htab_elem_value(l_old, key_size), value, false); + check_and_free_fields(htab, l_old); ret =3D 0; goto err; } --=20 2.51.1 From nobody Sat Feb 7 15:09:58 2026 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) (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 C013A2E8B94 for ; Thu, 30 Oct 2025 15:25:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837954; cv=none; b=NvVoT0PHykaAozVO/y75BVWjwBdJZyvvBQvc6+BV7NZEw/mr1IiTxcd+ZIrf2TVIhh2xXfSpYTxForAOxuksspNQGbL2yMUMNp7VJLl6D0jXvnlN0H9TL+VLsAiPlCaWC5z5H2GeXF9xmhWW0ao9bQqOeV1aTf3zIDMvHw5O5qQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837954; c=relaxed/simple; bh=H67r4w88zlU1921c5/QKXr/Bl4R9EUxsrk8OaJ2KI6Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U2IRJG80NnQBftIXS7z4rYZLpalif6ykiJYnSlJBjYli8TGpsRuk0VI5IenFnOiHTAO5ROGnXNMpFWXqyEw6c1riKFuOcCXCXp/1v01ZBBuoZP5bRIDgs0XbVh3VtavbNfXosUqCk7GC1GjTIV4s5oWxHnNE7707dcg518epv9Q= 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=JBT6OLyq; arc=none smtp.client-ip=95.215.58.173 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="JBT6OLyq" 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=1761837950; 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=U7kvFPkRp/dS2L9NbQ/G23ovFdfU8KUNzcqgDlTrqmc=; b=JBT6OLyqL/vjHQhB6jtxJDzNTE1FP1PeS5UnxvFwIl7EaUSmVKzh9h09dHQL0NCPRSEQ8+ 8lEqZu9afpy6z4KXEi6UorfSsTNWtr4rENMf0yMHu5CIecHIECXppb0xPvg3tF3GU9OJhz gTB1PyBGAgxbBKtDbz96eXkOorJRoWM= From: Leon Hwang To: bpf@vger.kernel.org Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, ameryhung@gmail.com, linux-kernel@vger.kernel.org, kernel-patches-bot@fb.com, Leon Hwang Subject: [PATCH bpf-next v4 3/4] bpf: Free special fields when update local storage maps with BPF_F_LOCK Date: Thu, 30 Oct 2025 23:24:50 +0800 Message-ID: <20251030152451.62778-4-leon.hwang@linux.dev> In-Reply-To: <20251030152451.62778-1-leon.hwang@linux.dev> References: <20251030152451.62778-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" When updating local storage maps with BPF_F_LOCK on the fast path, the special fields were not freed after being replaced. This could cause memory referenced by BPF_KPTR_{REF,PERCPU} fields to be held until the map gets freed. Similarly, on the other path, the old sdata's special fields were never freed when BPF_F_LOCK was specified, causing the same issue. Fix this by calling 'bpf_obj_free_fields()' after 'copy_map_value_locked()' to properly release the old fields. Fixes: 9db44fdd8105 ("bpf: Support kptrs in local storage maps") Signed-off-by: Leon Hwang --- kernel/bpf/bpf_local_storage.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index b931fbceb54da..9f447530f9564 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -609,6 +609,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_= storage_map *smap, if (old_sdata && selem_linked_to_storage_lockless(SELEM(old_sdata))) { copy_map_value_locked(&smap->map, old_sdata->data, value, false); + bpf_obj_free_fields(smap->map.record, old_sdata->data); return old_sdata; } } @@ -641,6 +642,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_= storage_map *smap, if (old_sdata && (map_flags & BPF_F_LOCK)) { copy_map_value_locked(&smap->map, old_sdata->data, value, false); + bpf_obj_free_fields(smap->map.record, old_sdata->data); selem =3D SELEM(old_sdata); goto unlock; } --=20 2.51.1 From nobody Sat Feb 7 15:09:58 2026 Received: from out-179.mta1.migadu.com (out-179.mta1.migadu.com [95.215.58.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 A8D55363B85 for ; Thu, 30 Oct 2025 15:26:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837963; cv=none; b=ManXB6DYo4F2R48jLMXHxxumpnE1Zard3YvWNXPYBodilq8vFnLOh3VOOsf6qKkgxEQakmvHxbUM+Wrbqq54y3VPSjaBYYXyxWQplX704A2cosx5NKWe+ihIFa0znYjxf5y+d0ksGh5ybGHE+HK7KkMIkTuyr35wpM89UuXKPE4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761837963; c=relaxed/simple; bh=jvy1Eb3PmqABBKxpsTcON/POtQ/qj+v8F/oF0lZz51A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DFV+DFay9j4IrlfrVIV4kpUMCE9FiAHJU3TS3dteCoo1ThiTRx9SbHCpYiHUZCyQdPgiEMkOwFhpTX486JThxtj6H0U3MeinVrkct3kgnklpZfRs4CgxPODzbqxO6vmPw5ttbL/abEKy8nnxtv3v3jWWHopmyfFetwiw0nIf5fY= 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=U7jloeTH; arc=none smtp.client-ip=95.215.58.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="U7jloeTH" 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=1761837958; 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=vg1HmdjWPqR7MRQoIa6eFE7P3tDnDAjv+S/N3RgV7ds=; b=U7jloeTHKUK6ahYFcfIFkfbPuoKGc5/o9uNkYz9+7ZTESO2WyMZKJ9HZIu3N7MyEptFHzw vkF4KyVFkUhMvbDUgbYqGB2ERkoDDsbuk+pUFWCEVMoPhTx9PEwcik9I5kTOPDAYTJfWKt q9fN+RyezUHx4D/d17QBPzPUFCaiEJg= From: Leon Hwang To: bpf@vger.kernel.org Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, eddyz87@gmail.com, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, memxor@gmail.com, ameryhung@gmail.com, linux-kernel@vger.kernel.org, kernel-patches-bot@fb.com, Leon Hwang Subject: [PATCH bpf-next v4 4/4] selftests/bpf: Add tests to verify freeing the special fields when update hash and local storage maps Date: Thu, 30 Oct 2025 23:24:51 +0800 Message-ID: <20251030152451.62778-5-leon.hwang@linux.dev> In-Reply-To: <20251030152451.62778-1-leon.hwang@linux.dev> References: <20251030152451.62778-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" Add tests to verify that updating hash and local storage maps decrements refcount when BPF_KPTR_REF objects are involved. The tests perform the following steps: 1. Call update_elem() to insert an initial value. 2. Use bpf_refcount_acquire() to increment the refcount. 3. Store the node pointer in the map value. 4. Add the node to a linked list. 5. Probe-read the refcount and verify it is *2*. 6. Call update_elem() again to trigger refcount decrement. 7. Probe-read the refcount and verify it is *1*. Signed-off-by: Leon Hwang --- .../bpf/prog_tests/refcounted_kptr.c | 134 +++++++++++++++++- .../selftests/bpf/progs/refcounted_kptr.c | 129 +++++++++++++++++ 2 files changed, 262 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/bpf/prog_tests/refcounted_kptr.c b/too= ls/testing/selftests/bpf/prog_tests/refcounted_kptr.c index d6bd5e16e6372..0ec91ff914af7 100644 --- a/tools/testing/selftests/bpf/prog_tests/refcounted_kptr.c +++ b/tools/testing/selftests/bpf/prog_tests/refcounted_kptr.c @@ -3,7 +3,7 @@ =20 #include #include - +#include "cgroup_helpers.h" #include "refcounted_kptr.skel.h" #include "refcounted_kptr_fail.skel.h" =20 @@ -44,3 +44,135 @@ void test_refcounted_kptr_wrong_owner(void) ASSERT_OK(opts.retval, "rbtree_wrong_owner_remove_fail_a2 retval"); refcounted_kptr__destroy(skel); } + +static void test_refcnt_leak(struct refcounted_kptr *skel, int key, void *= values, size_t values_sz, + u64 flags, struct bpf_map *map, struct bpf_program *prog_leak, + struct bpf_program *prog_check, struct bpf_test_run_opts *opts) +{ + int ret, fd; + + ret =3D bpf_map__update_elem(map, &key, sizeof(key), values, values_sz, f= lags); + if (!ASSERT_OK(ret, "bpf_map__update_elem init")) + return; + + fd =3D bpf_program__fd(prog_leak); + ret =3D bpf_prog_test_run_opts(fd, opts); + if (!ASSERT_OK(ret, "bpf_prog_test_run_opts")) + return; + if (!ASSERT_EQ(skel->bss->kptr_refcount, 2, "refcount")) + return; + + ret =3D bpf_map__update_elem(map, &key, sizeof(key), values, values_sz, f= lags); + if (!ASSERT_OK(ret, "bpf_map__update_elem dec refcount")) + return; + + fd =3D bpf_program__fd(prog_check); + ret =3D bpf_prog_test_run_opts(fd, opts); + ASSERT_OK(ret, "bpf_prog_test_run_opts"); + ASSERT_EQ(skel->bss->kptr_refcount, 1, "refcount"); +} + +static void test_percpu_hash_refcount_leak(void) +{ + struct refcounted_kptr *skel; + size_t values_sz; + u64 *values; + int cpu_nr; + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in =3D &pkt_v4, + .data_size_in =3D sizeof(pkt_v4), + .repeat =3D 1, + ); + + cpu_nr =3D libbpf_num_possible_cpus(); + if (!ASSERT_GT(cpu_nr, 0, "libbpf_num_possible_cpus")) + return; + + values =3D calloc(cpu_nr, sizeof(u64)); + if (!ASSERT_OK_PTR(values, "calloc values")) + return; + + skel =3D refcounted_kptr__open_and_load(); + if (!ASSERT_OK_PTR(skel, "refcounted_kptr__open_and_load")) { + free(values); + return; + } + + values_sz =3D cpu_nr * sizeof(u64); + memset(values, 0, values_sz); + + test_refcnt_leak(skel, 0, values, values_sz, 0, skel->maps.pcpu_hash, + skel->progs.pcpu_hash_refcount_leak, + skel->progs.check_pcpu_hash_refcount, &opts); + + refcounted_kptr__destroy(skel); + free(values); +} + +struct lock_map_value { + u64 kptr; + struct bpf_spin_lock lock; + int value; +}; + +static void test_hash_lock_refcount_leak(void) +{ + struct lock_map_value value =3D {}; + struct refcounted_kptr *skel; + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in =3D &pkt_v4, + .data_size_in =3D sizeof(pkt_v4), + .repeat =3D 1, + ); + + skel =3D refcounted_kptr__open_and_load(); + if (!ASSERT_OK_PTR(skel, "refcounted_kptr__open_and_load")) + return; + + test_refcnt_leak(skel, 0, &value, sizeof(value), BPF_F_LOCK, skel->maps.l= ock_hash, + skel->progs.hash_lock_refcount_leak, + skel->progs.check_hash_lock_refcount, &opts); + + refcounted_kptr__destroy(skel); +} + +static void test_cgroup_storage_lock_refcount_leak(void) +{ + struct lock_map_value value =3D {}; + struct refcounted_kptr *skel; + int cgroup, err; + LIBBPF_OPTS(bpf_test_run_opts, opts); + + err =3D setup_cgroup_environment(); + if (!ASSERT_OK(err, "setup_cgroup_environment")) + return; + + cgroup =3D get_root_cgroup(); + if (!ASSERT_GE(cgroup, 0, "get_root_cgroup")) { + cleanup_cgroup_environment(); + return; + } + + skel =3D refcounted_kptr__open_and_load(); + if (!ASSERT_OK_PTR(skel, "refcounted_kptr__open_and_load")) + goto out; + + test_refcnt_leak(skel, cgroup, &value, sizeof(value), BPF_F_LOCK, skel->m= aps.cgrp_strg, + skel->progs.cgroup_storage_lock_refcount_leak, + skel->progs.check_cgroup_storage_lock_refcount, &opts); + + refcounted_kptr__destroy(skel); +out: + close(cgroup); + cleanup_cgroup_environment(); +} + +void test_kptr_refcount_leak(void) +{ + if (test__start_subtest("percpu_hash_refcount_leak")) + test_percpu_hash_refcount_leak(); + if (test__start_subtest("hash_lock_refcount_leak")) + test_hash_lock_refcount_leak(); + if (test__start_subtest("cgroup_storage_lock_refcount_leak")) + test_cgroup_storage_lock_refcount_leak(); +} diff --git a/tools/testing/selftests/bpf/progs/refcounted_kptr.c b/tools/te= sting/selftests/bpf/progs/refcounted_kptr.c index 893a4fdb4b6e9..101ba630d93e8 100644 --- a/tools/testing/selftests/bpf/progs/refcounted_kptr.c +++ b/tools/testing/selftests/bpf/progs/refcounted_kptr.c @@ -568,4 +568,133 @@ int BPF_PROG(rbtree_sleepable_rcu_no_explicit_rcu_loc= k, return 0; } =20 +private(kptr_ref) u64 ref; +u32 kptr_refcount; + +static int probe_read_refcount(void) +{ + bpf_probe_read_kernel(&kptr_refcount, sizeof(kptr_refcount), (void *) ref= ); + return 0; +} + +static int __insert_in_list(struct bpf_list_head *head, struct bpf_spin_lo= ck *lock, + struct node_data __kptr **node) +{ + struct node_data *n, *m; + + n =3D bpf_obj_new(typeof(*n)); + if (!n) + return 0; + + m =3D bpf_refcount_acquire(n); + n =3D bpf_kptr_xchg(node, n); + if (n) { + bpf_obj_drop(n); + bpf_obj_drop(m); + return 0; + } + + bpf_spin_lock(lock); + bpf_list_push_front(head, &m->l); + ref =3D (u64)(void *) &m->ref; + bpf_spin_unlock(lock); + return probe_read_refcount(); +} + +static void *__lookup_map(void *map) +{ + int key =3D 0; + + return bpf_map_lookup_elem(map, &key); +} + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_HASH); + __type(key, int); + __type(value, struct map_value); + __uint(max_entries, 1); +} pcpu_hash SEC(".maps"); + +SEC("tc") +int pcpu_hash_refcount_leak(void *ctx) +{ + struct map_value *v; + + v =3D __lookup_map(&pcpu_hash); + if (!v) + return 0; + + return __insert_in_list(&head, &lock, &v->node); +} + +SEC("tc") +int check_pcpu_hash_refcount(void *ctx) +{ + return probe_read_refcount(); +} + +struct lock_map_value { + struct node_data __kptr *node; + struct bpf_spin_lock lock; + int value; +}; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, int); + __type(value, struct lock_map_value); + __uint(max_entries, 1); +} lock_hash SEC(".maps"); + +SEC("tc") +int hash_lock_refcount_leak(void *ctx) +{ + struct lock_map_value *v; + + v =3D __lookup_map(&lock_hash); + if (!v) + return 0; + + bpf_spin_lock(&v->lock); + v->value =3D 42; + bpf_spin_unlock(&v->lock); + return __insert_in_list(&head, &lock, &v->node); +} + +SEC("tc") +int check_hash_lock_refcount(void *ctx) +{ + return probe_read_refcount(); +} + +struct { + __uint(type, BPF_MAP_TYPE_CGRP_STORAGE); + __uint(map_flags, BPF_F_NO_PREALLOC); + __type(key, int); + __type(value, struct lock_map_value); +} cgrp_strg SEC(".maps"); + +SEC("syscall") +int BPF_PROG(cgroup_storage_lock_refcount_leak) +{ + struct lock_map_value *v; + struct task_struct *task; + + task =3D bpf_get_current_task_btf(); + bpf_rcu_read_lock(); + v =3D bpf_cgrp_storage_get(&cgrp_strg, task->cgroups->dfl_cgrp, 0, + BPF_LOCAL_STORAGE_GET_F_CREATE); + bpf_rcu_read_unlock(); + if (!v) + return 0; + + return __insert_in_list(&head, &lock, &v->node); +} + +SEC("syscall") +int BPF_PROG(check_cgroup_storage_lock_refcount) +{ + return probe_read_refcount(); +} + char _license[] SEC("license") =3D "GPL"; --=20 2.51.1