From patchwork Tue Apr 30 09:36:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13648625 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 9D79C28DDF; Tue, 30 Apr 2024 09:36:25 +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=1714469785; cv=none; b=pG+VEanqL3FNPefOr+BD/kOWjLLE+Fi7RtWmjW9D+VvaReWkfHUhq4YHYlHCe8y8XDXWNkxSA+dj4leb/ONvbsrvINe7qRFV0Aenwi46oTOnAxRoWvMdsJXKdeET5pXFZF+P9ydSY8ngBIVwdmiwclt9PWsXheNqdTz6RYO6cTE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714469785; c=relaxed/simple; bh=O9q/3BVp7ykhUDY0RgKC9pwWwJupmmRs4n4Uim9KjQo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=b7H9W/utSHU0e4YX2CF2JFgmz50gn/DMca0M+UXVIVLpsuv8ebsb0lVmn9rOomXKaDErGNTCtTnNN7WYxSI2PS6pndDXhWqi206MI6gCwx4+s8998sjqn6PPKySZCI2ykDvwmisk/7quQdGt6mTowW8t4wode1f89okJQDHffa8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GRIdCHTK; 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="GRIdCHTK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27BA5C4AF4D; Tue, 30 Apr 2024 09:36:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714469785; bh=O9q/3BVp7ykhUDY0RgKC9pwWwJupmmRs4n4Uim9KjQo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GRIdCHTKlzYxlFIDQZxwLusiDbaaSNK+vyOYCFIS+Eeuwtkc25B5EhSFk7B6hm0AW Bn5v0vU5AsaO/VGeI6QQ8agnAOVJTHihuOnlWVAXnqXKX1pZ7JUWUFmd3jaJu3s61J WFprkS/cLzBy0TjxYJjquM/cO6Jn8njMH0dxF2O6VsqzAjrabmM8o8/0gGbUEXqjPi 7aTXAi0xiIpmURazQOz0DiN7AFp5edaHC6wpTRqx1Bp81bIvozJjpKh3XXk/VQfuDc 8FoyKVb5mNg5nOA4FSaloJe92drJHJ92jjNzZP15GfWsed+PLVFpgLOYtHja2a/rqx iZvaUjo9XT7pQ== From: Benjamin Tissoires Date: Tue, 30 Apr 2024 11:36:10 +0200 Subject: [PATCH v2 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: <20240430-bpf-next-v2-1-140aa50f0f19@kernel.org> References: <20240430-bpf-next-v2-0-140aa50f0f19@kernel.org> In-Reply-To: <20240430-bpf-next-v2-0-140aa50f0f19@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=1714469778; l=1659; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=O9q/3BVp7ykhUDY0RgKC9pwWwJupmmRs4n4Uim9KjQo=; b=0RyqTthwuyYXXJCv8I7WMlwiJL8U8W7Ho72KCoMyZwbQmNDJeUVyrN160lRYUE53SKRsmLoQX TWvL8F4mT2lBbE2+hmP8fKXA1iaDX0Ap0FI62ZgR0QPYq11FKrMzilo 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 --- no changes in v2 --- 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 Tue Apr 30 09:36:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 13648626 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 B4D2613D262; Tue, 30 Apr 2024 09:36:29 +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=1714469789; cv=none; b=rZDB1Vm7bxPZUpbh6E5T1pME3yiAXutR098KxBBAmqchzKf5PjcYw6t4c4INLvIZtGxPt38UGlOYTNlUFgV46/Huldob3FbulYQKecWmQAO24bi2ERrSlBv37iVXAv/ZbbLTQ7ymeq5QCBYUIJ9gljwU6nN+8dY+ra0UUiN7GDk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714469789; c=relaxed/simple; bh=mCuLxTaxKyTT1kzYlei9LV+3e2KuRqWDO+D7edZIeC0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=coIEVKk5maXzo27qNBwXHC8YrvU0CGWYYOpaw6zNfT29HsUoF+z/VUnketQ5KfRXcjgaSjMbQalH7a7x9oM19+qvi4R0DuXnsbs53xPMGtJ8+v4M+OvEdQ3xXBAzto1uAWv1Q2uaGEzL17BISDrf4YlRpz3kefMvljtgEXRnlKU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kFoabQAu; 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="kFoabQAu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7118C4AF14; Tue, 30 Apr 2024 09:36:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714469789; bh=mCuLxTaxKyTT1kzYlei9LV+3e2KuRqWDO+D7edZIeC0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kFoabQAuidhgovowuzf7LI+qbTdQ9BjBbAqUG1yis+ukojsV+egPiZyAUQloPbirW dedXaDgz9amT+Nj1aoH3ptZ0jMxDeRFvaTJqPTzae/qGSElevoShA70qZ3reGMVFeq SI3fW4BsfMfq3tA2ZPBXmuowJWSZWl4vdxOyA9dr4VgTplbACY8NjqK6VI3Ga/4/jC VnLlU0q7Xmmjo8sIrUl2tZn2U1I4r72rShiRjKP+YyQixxhbff+SaQL9SN5kejXYWM FvQVXWH01y2np1hsloFj3hfE4tUN4cjZB6oFw7ERROmLD1/koJU881brZHWn23zApN WmfLs8wC3ZFhw== From: Benjamin Tissoires Date: Tue, 30 Apr 2024 11:36:11 +0200 Subject: [PATCH v2 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: <20240430-bpf-next-v2-2-140aa50f0f19@kernel.org> References: <20240430-bpf-next-v2-0-140aa50f0f19@kernel.org> In-Reply-To: <20240430-bpf-next-v2-0-140aa50f0f19@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=1714469778; l=3927; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=mCuLxTaxKyTT1kzYlei9LV+3e2KuRqWDO+D7edZIeC0=; b=wm6bD7epmm+7HTNl9qu5AyuDg//HjDeOrs9ejYRHe57Z85wdJUHowA/HoykRqsx4iI1dZMtJs fN5UYhFyz5JCJHX5dl4z5IEeUbBOcrt+SDbQprnNbc26Ot/+GP6rteI 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 --- changes in v2: - fix wq being not freed (and static call not used) --- kernel/bpf/hashtab.c | 49 +++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/kernel/bpf/hashtab.c b/kernel/bpf/hashtab.c index 0179183c543a..5eefadfc8ea9 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -221,32 +221,11 @@ static bool htab_has_extra_elems(struct bpf_htab *htab) return !htab_is_percpu(htab) && !htab_is_lru(htab); } -static void htab_free_prealloced_timers(struct bpf_htab *htab) +static void htab_free_prealloced_timers_and_wq(struct bpf_htab *htab) { u32 num_entries = htab->map.max_entries; int i; - if (!btf_record_has_field(htab->map.record, BPF_TIMER)) - return; - if (htab_has_extra_elems(htab)) - num_entries += num_possible_cpus(); - - for (i = 0; i < num_entries; i++) { - struct htab_elem *elem; - - elem = get_htab_elem(htab, i); - bpf_obj_free_timer(htab->map.record, elem->key + round_up(htab->map.key_size, 8)); - cond_resched(); - } -} - -static void htab_free_prealloced_wq(struct bpf_htab *htab) -{ - u32 num_entries = htab->map.max_entries; - int i; - - if (!btf_record_has_field(htab->map.record, BPF_WORKQUEUE)) - return; if (htab_has_extra_elems(htab)) num_entries += num_possible_cpus(); @@ -254,8 +233,12 @@ static void htab_free_prealloced_wq(struct bpf_htab *htab) struct htab_elem *elem; elem = get_htab_elem(htab, i); - bpf_obj_free_workqueue(htab->map.record, - elem->key + round_up(htab->map.key_size, 8)); + if (btf_record_has_field(htab->map.record, BPF_TIMER)) + bpf_obj_free_timer(htab->map.record, + elem->key + round_up(htab->map.key_size, 8)); + else + bpf_obj_free_workqueue(htab->map.record, + elem->key + round_up(htab->map.key_size, 8)); cond_resched(); } } @@ -1515,7 +1498,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_and_wq(struct bpf_htab *htab) { int i; @@ -1527,10 +1510,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,17 +1527,11 @@ 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 (!htab_is_prealloc(htab)) - htab_free_malloced_timers_or_wq(htab, true); - else - htab_free_prealloced_timers(htab); - } - if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE)) { + if (btf_record_has_field(htab->map.record, BPF_TIMER | BPF_WORKQUEUE)) { if (!htab_is_prealloc(htab)) - htab_free_malloced_timers_or_wq(htab, false); + htab_free_malloced_timers_and_wq(htab); else - htab_free_prealloced_wq(htab); + htab_free_prealloced_timers_and_wq(htab); } } From patchwork Tue Apr 30 09:36: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: 13648627 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 3029E1A0B06; Tue, 30 Apr 2024 09:36: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=1714469793; cv=none; b=rs2VOdlKmIw6IHpJXrBwqYvg7/EXlNy57y4AyITq4R3iOYFBkgbk/1xlQZ7e8G7S5bk0SJxftnPcHINMlrW6DMT0U+056EVXAv9hlxIde/wlvrv5LxeMt/EK2vHZ3L650gf/r7/gwMkwCfoOXtGU5IYEKwwg5iusAWAWesDKHkQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714469793; c=relaxed/simple; bh=bhzZtbW8vADrbyYDpEyoD631jxsaMcAgBlvgv/dK/J4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RQDKS4v0R+QsqZ74QxV00VFNDdhdrPrnx/ZOcMl5D6g7QrLT/0kJlmiFqiD4iYJOTAmPslnaw+X6m0lNQBBItHhEZF6nuBcAPx9ppICXyy/7sPHWxozJTGEyFcoQJdu8BwQ/fsKevsrAIJ6FYyWcVZbGo/vm2Jea039cdYEt0DI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KUv5qPYQ; 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="KUv5qPYQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B165EC4AF19; Tue, 30 Apr 2024 09:36:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714469793; bh=bhzZtbW8vADrbyYDpEyoD631jxsaMcAgBlvgv/dK/J4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KUv5qPYQE65E6OtMiIdMBYO6aBSQUK+DK2gfLswwPV68AlPrJUwVEZRA6+9jw7IDW Y6n0al0Jt38Lu6ww3ijeOEAVwTYPfW1Foawdl4fbt4p6LlyOd1m9dI4VDgU0akIrhp f6pXJGsqBjG/8Wrq5PxbypdLpt6FZHvbTGofuJNo3ryZjgTEYwl+68Wk2UjrDAMHbN zhvIO92nOcOJj79myHtAbvVOXQUZKXGB5fmsfUP0FBezfJsUNBNj8EHTzNH3+8r2yQ 306vqNOIqB5rpoZPLmB53SLSO1njcXdlwwMPMHUxLnsJbRV5HC2yr5f//OrvGCP3/A HeFuIgcD3cBeA== From: Benjamin Tissoires Date: Tue, 30 Apr 2024 11:36:12 +0200 Subject: [PATCH v2 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: <20240430-bpf-next-v2-3-140aa50f0f19@kernel.org> References: <20240430-bpf-next-v2-0-140aa50f0f19@kernel.org> In-Reply-To: <20240430-bpf-next-v2-0-140aa50f0f19@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=1714469778; l=720; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=bhzZtbW8vADrbyYDpEyoD631jxsaMcAgBlvgv/dK/J4=; b=YmoD9kZN4gNX/W8LY6WLJWW3Y5qI5CxWasAbaHC4nBctmuFGROznShnN1hKzrua6UdQI4w0fd UdrcC2YzSwhAiWKYSvSKsDodXjLbiBwYwiyL2dQIh5ocnIyvurk4FZz 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 --- no changes in v2 --- 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); }