From patchwork Thu Apr 25 13:59:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13643395 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AABD6149E1A; Thu, 25 Apr 2024 13:59:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714053567; cv=none; b=ZNusgrClbFZFP2/TEsY20+y0DLzyiCBVjBam142hGF84gAtnsHLQfY4MxzU4M+Wxg4m33nW8z2pX+gajy5luPZvGSU/yGZQvj2GGGDORWLkQbl9PEUlEl6bEJ5jJ4/E9Y7jSTGWsgpdXh+4hjjdoAYEsp1blZ1KpHuBjpQjtFqk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714053567; c=relaxed/simple; bh=0UjWtmDw4NdWdiV9RiGFH2oVaGYWk2wAWC/NqY/by9Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ei1rgL8me9c+C3ekFzDbLUIqIl/c3XVvSfjsz09DCHQdaTFBKwRaKuKjasJkJa5i045TkwKmPdgSvis6VUI2b551X6vekRP1y/j7PvFSsu0qBjeVENzYjavJxElcCF3RzZUVDqsCC+LbvPjD4KGJ30zzpaDNsMLr88UrqCvW/vs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gjQcqWbv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="gjQcqWbv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB899C32783; Thu, 25 Apr 2024 13:59:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714053566; bh=0UjWtmDw4NdWdiV9RiGFH2oVaGYWk2wAWC/NqY/by9Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gjQcqWbveWT4Z3rfSXEl8UidlmtW2NjT9zckTQTkeo5WUxvUK14hRzMfntdGZJZw6 3chcFyQXhhSkQ0KP6pnigxqxM2jVH9GVgkc3vg/u4ynDVh56TkgMBDUXh/0KIzzOgY VvfVH+sAHiReO19p+7Qkx84eOekFWVB7KDYQrnDllSVI6ip/W1aIIw80KyQmQrtxZp oBCFGFBxqP6pcZL21v3fbVXLW5eC8LMjlWA79+d9xwQ8aTVmzPgEMnJbloE+uH+CI3 YrWX6iCoBW+CG25wfuqjgs36c8VihCyav2vn9ZjolqoEdKFgdIV3oSZ/7WKopJVrTP oP/xkv85jvq7w== From: Benjamin Tissoires Date: Thu, 25 Apr 2024 15:59:12 +0200 Subject: [PATCH 1/3] bpf: do not walk twice the map on free Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240425-bpf-next-v1-1-1d8330e6c643@kernel.org> References: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> In-Reply-To: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1714053558; l=1632; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=0UjWtmDw4NdWdiV9RiGFH2oVaGYWk2wAWC/NqY/by9Y=; b=RKtnAsLvD2iqjvzpo5k2I1Q4VyNf7Rj1fx5HEQ/MKI+iKzaLwwaZr/XnRQswXFymVJeBFXfYW gqBO+/jQGrtA7khINJp9kK4fFri609NjGRLuTokCwimqhrKmAoER7J8 X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= If someone stores both a timer and a workqueue in a map, on free we would walk it twice. Add a check in array_map_free_timers_wq and free the timers and workqueues if they are present. Fixes: 246331e3f1ea ("bpf: allow struct bpf_wq to be embedded in arraymaps and hashmaps") Signed-off-by: Benjamin Tissoires --- kernel/bpf/arraymap.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 580d07b15471..feabc0193852 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -436,13 +436,14 @@ static void array_map_free_timers_wq(struct bpf_map *map) /* We don't reset or free fields other than timer and workqueue * on uref dropping to zero. */ - if (btf_record_has_field(map->record, BPF_TIMER)) - for (i = 0; i < array->map.max_entries; i++) - bpf_obj_free_timer(map->record, array_map_elem_ptr(array, i)); - - if (btf_record_has_field(map->record, BPF_WORKQUEUE)) - for (i = 0; i < array->map.max_entries; i++) - bpf_obj_free_workqueue(map->record, array_map_elem_ptr(array, i)); + if (btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE)) { + for (i = 0; i < array->map.max_entries; i++) { + if (btf_record_has_field(map->record, BPF_TIMER)) + bpf_obj_free_timer(map->record, array_map_elem_ptr(array, i)); + if (btf_record_has_field(map->record, BPF_WORKQUEUE)) + bpf_obj_free_workqueue(map->record, array_map_elem_ptr(array, i)); + } + } } /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ From patchwork Thu Apr 25 13:59:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13643396 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6646C14A0AD; Thu, 25 Apr 2024 13:59:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714053570; cv=none; b=UHWUskXIeeeT/s/e2WMUotcYORq+qKq9WNlhCPEuUn1HLKRIJyc3OmRcsAS5ktpQL9I/KLCT7gnYDxIg8MFggtH1if1bfnFgzVCbx8gMerDWxwU6BUCcVPJ/yia8illxVvIEpk7PCpIRtksudADbDh+1ehOKUIF/EFcqKizfk2c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714053570; c=relaxed/simple; bh=/jfDurkkyTX1CjxTQWI9/U9pueeszadMVJLu6GiEwj0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CQBYUo6bJbXE6eQuMEX2jmxrofo7u9ogMPgii1CReUGbo6xVdA5o4Puh1sVehMqOmmeXImimULRehvh6sMbhMHlJJN0ao5ixs6A9bLywAAhcATTp47bAVSNYHfOuXFUOBVN2c9peBTHbUIBsGBqeBWQeCwEG0Lml7e4TGQHRU5w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HNE5RsAg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="HNE5RsAg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA319C3277B; Thu, 25 Apr 2024 13:59:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714053570; bh=/jfDurkkyTX1CjxTQWI9/U9pueeszadMVJLu6GiEwj0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=HNE5RsAgTaZoxrIvlG1EZ6FNVeBiolN9FzELHstT8/YZC48DrHK8K1+Z2hi5wlfjB IM8VM/RB4xTlPAfw0BetGUQ3zZewCaIPD2SZcNtgBC2fCnHIlc7GOxaiK7oua15rJb vp2k7f4kWXcVWMLkVNIFnc6ilydDxJxicpUbd/9nyL/mu9TIDV6MB0u/BeKB6jR3Jz FxO6qS2LBhun3RpAuDHASQfDrEbEendMRGjHw6kHuruOeMZ46rlfp3IxtXKnVXP/CJ JW/9GtEK/3SylZATP/21S1nSKenKfpo+1F8HvzCMxnl67Nwu213DntyRBu7WbReUpM mgGaNCNQRF0Ww== From: Benjamin Tissoires Date: Thu, 25 Apr 2024 15:59:13 +0200 Subject: [PATCH 2/3] bpf: do not walk twice the hash map on free Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240425-bpf-next-v1-2-1d8330e6c643@kernel.org> References: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> In-Reply-To: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1714053558; l=2311; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=/jfDurkkyTX1CjxTQWI9/U9pueeszadMVJLu6GiEwj0=; b=Mz0bcCjh5PHLLrFEt0aDcjPvdnjN285NxBvdb63Pfw6n7lah+bC7DmxGNCp7JMh4WAhpXyb1x UrYXaq5DWWZB4TGvBQtqBxpb+smpQMVI3NtB3RmAWaay6gpNWQu/nmE X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= If someone stores both a timer and a workqueue in a hash map, on free, we would walk it twice. Add a check in htab_free_malloced_timers_or_wq and free the timers and workqueues if they are present. Fixes: 246331e3f1ea ("bpf: allow struct bpf_wq to be embedded in arraymaps and hashmaps") Signed-off-by: Benjamin Tissoires --- kernel/bpf/hashtab.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 0179183c543a..20162ae741e9 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -1515,7 +1515,7 @@ static void delete_all_elements(struct bpf_htab *htab) migrate_enable(); } -static void htab_free_malloced_timers_or_wq(struct bpf_htab *htab, bool is_timer) +static void htab_free_malloced_timers_or_wq(struct bpf_htab *htab) { int i; @@ -1527,10 +1527,10 @@ static void htab_free_malloced_timers_or_wq(struct bpf_htab *htab, bool is_timer hlist_nulls_for_each_entry(l, n, head, hash_node) { /* We only free timer on uref dropping to zero */ - if (is_timer) + if (btf_record_has_field(htab->map.record, BPF_TIMER)) bpf_obj_free_timer(htab->map.record, l->key + round_up(htab->map.key_size, 8)); - else + if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE)) bpf_obj_free_workqueue(htab->map.record, l->key + round_up(htab->map.key_size, 8)); } @@ -1544,18 +1544,12 @@ static void htab_map_free_timers_and_wq(struct bpf_map *map) struct bpf_htab *htab = container_of(map, struct bpf_htab, map); /* We only free timer and workqueue on uref dropping to zero */ - if (btf_record_has_field(htab->map.record, BPF_TIMER)) { + if (btf_record_has_field(htab->map.record, BPF_TIMER | BPF_WORKQUEUE)) { if (!htab_is_prealloc(htab)) - htab_free_malloced_timers_or_wq(htab, true); + htab_free_malloced_timers_or_wq(htab); else htab_free_prealloced_timers(htab); } - if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE)) { - if (!htab_is_prealloc(htab)) - htab_free_malloced_timers_or_wq(htab, false); - else - htab_free_prealloced_wq(htab); - } } /* Called when map->refcnt goes to zero, either from workqueue or from syscall */ From patchwork Thu Apr 25 13:59:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13643397 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 3FD1B14A604; Thu, 25 Apr 2024 13:59:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714053574; cv=none; b=SAn26BdrTDPIj0tDPBDNouKmRdwVkTE97HsGVWBFr5iEogoh7J144MTHen0uArwOrB4NnWSPTuhCNMEo2xU+r2pI1EwcvqAXunoPRfle9oQUWTfRNt4XC9gBb96yEU7m/04kBvOt/2VmJVq+KLvdyN3aUi9vf4t1Y/IVvo2vz2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714053574; c=relaxed/simple; bh=mvQkfgZuYmY613uCDbNKQpDIm4TePVAqpmobfYU+0So=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oZ2Kv9yvgenoeDm3U9Rx27q7ypn6AHuCx3Jo0Nv7QuzwfOCJKWK3Hp4vNeYvcXjq9gM04U8Qpo7MJ945GrHU9GBSLDtCA2fc4NSMVhdb2QmQzwaTG2aoTgbqAjgWgabxexdfGBVmVpR7fgiIm06OXEkyyFMYOBUVdOgGGEmqPpQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RpIzGoKf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RpIzGoKf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6714AC2BD10; Thu, 25 Apr 2024 13:59:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714053573; bh=mvQkfgZuYmY613uCDbNKQpDIm4TePVAqpmobfYU+0So=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=RpIzGoKfMwG9Sy2keGCgECx5MrqT4aUQq3SLROhwFRMMU1zg9No7S3PBUQvGHdQRA IxMWAJ5nq16lXH9J5gcTDpu694NH+a5fblNnshkkiFxGjge+FDwdvPpZIewvYfmSJD gPVBpxNIraFliMqaqtEO/TPma90jQsFmdHw6Z2xwfeffTFW6FFpGlrMMOtdkZYfFZZ 9a+B6VuSA2DRpg/KyacJF0BHBm0bWhsTi+E5T1gTTauBlqAoh+gwwc33Nb6x9lsrfR QdF2P2ajcsrJZEEw+8mCJY4yRu/uXGdrSCWwHW/CSK84lrPkYwWtyADXPuhvtjq4Jk 3REvTRA8PSAgw== From: Benjamin Tissoires Date: Thu, 25 Apr 2024 15:59:14 +0200 Subject: [PATCH 3/3] selftests/bpf: drop an unused local variable Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20240425-bpf-next-v1-3-1d8330e6c643@kernel.org> References: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> In-Reply-To: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Mykola Lysenko , Shuah Khan Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Benjamin Tissoires X-Mailer: b4 0.12.4 X-Developer-Signature: v=1; a=ed25519-sha256; t=1714053558; l=693; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=mvQkfgZuYmY613uCDbNKQpDIm4TePVAqpmobfYU+0So=; b=ANTc6jeClzDBFOPpM80AN3a7UfMY3nh1T6nvJC96+/nDJNEFJLTR8q8OMZORC7QiGHNIOe/jA EvcXmz5JKKqCyA6GVB45OLYFlnnZ5pO8cgPAoDysHyH10c1lsM8O5kC X-Developer-Key: i=bentiss@kernel.org; a=ed25519; pk=7D1DyAVh6ajCkuUTudt/chMuXWIJHlv2qCsRkIizvFw= Some copy/paste leftover, this is never used Fixes: e3d9eac99afd ("selftests/bpf: wq: add bpf_wq_init() checks") Signed-off-by: Benjamin Tissoires --- tools/testing/selftests/bpf/prog_tests/wq.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/wq.c b/tools/testing/selftests/bpf/prog_tests/wq.c index c4bacd3160e1..99e438fe12ac 100644 --- a/tools/testing/selftests/bpf/prog_tests/wq.c +++ b/tools/testing/selftests/bpf/prog_tests/wq.c @@ -36,7 +36,5 @@ void serial_test_wq(void) void serial_test_failures_wq(void) { - LIBBPF_OPTS(bpf_test_run_opts, topts); - RUN_TESTS(wq_failures); }