From patchwork Tue Apr 30 10:43:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 793903 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 3DA5412CD88; Tue, 30 Apr 2024 10:43:37 +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=1714473817; cv=none; b=iW1hVUETOju28OBy+19P8RTpnqzj89DH9oM5+p1mS8bvOphdp0zi4gfELOL/O8rEz47aNDuspZKTDaBQn2Ba6ErqZmU/3iIBxSCgsw556Goh0QG8kWaziEMrOjJvIFN9dL3IYfbAQVG2xmIJiMoJ0vuK7X2mQBh2YsYkB5QAo74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714473817; c=relaxed/simple; bh=2DbLgfRPOuQjtWgbOdxT93yoQ/Xxj7Etz/Li4OEnMe4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Pg8M96a9FwcRd1QZ+jQfLyuxBPVfRnRRrtt7YwP7OoTxJrZJnoAylFEt8ryK+n3SMDHpRYpyKoiT5WugNBzyZY820naNy4GmrztrGmh9IeWw3IRuHCHKLIAWOlFd8WQTIbH+LtEPe0GhL7ID55iA3rFE2PjHi+53RAJB/NLkdT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CwemDBum; 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="CwemDBum" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7EA2C2BBFC; Tue, 30 Apr 2024 10:43:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714473817; bh=2DbLgfRPOuQjtWgbOdxT93yoQ/Xxj7Etz/Li4OEnMe4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CwemDBumFWOv3RQoJS6ME8j80MgE/M38tYI5FqsAhMypHiM24SHGc4DG/J2qb1/BR LiiV/dTeClQeZuI+eG1KKrSgQO2yq+Pe7OxFvnZtBHxijTCorqFTmkKQ22/4ZPsG8f +3pZlKn3PEq24wMp57rV6E7Iaeq82xm6KseJIihChz5Y6h8mWQ+eAw4v9p5314pDaE Pm77Js3/u3Halh4f3OLYkw0LGHENEswuAANzQo1sd/cV/Y5OKHYFxZuPr8cA8FToY4 SA26efYxmNZvTEF0eYiL/BTqMvsiG9vvuYIe8h/N+YAJjgD7bPoq5WUNPAeTgen6nC 0EmwNpehbPvKQ== From: Benjamin Tissoires Date: Tue, 30 Apr 2024 12:43:24 +0200 Subject: [PATCH bpf-next v3 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-v3-1-27afe7f3b17c@kernel.org> References: <20240430-bpf-next-v3-0-27afe7f3b17c@kernel.org> In-Reply-To: <20240430-bpf-next-v3-0-27afe7f3b17c@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=1714473809; l=1679; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=2DbLgfRPOuQjtWgbOdxT93yoQ/Xxj7Etz/Li4OEnMe4=; b=bLOyRikr37Rv6TtAU8CNK9bpzfelszIO81xHpwQbLkZkchDjeXzOcB+veuA9vAGW059bXuqOL wbBE8+z3610B3GRskiP899gGK2d5KhojbMCwAdX7gpT17EbB8eTddDP 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 v3 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 10:43:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 793528 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 08ED712C55D; Tue, 30 Apr 2024 10:43:40 +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=1714473821; cv=none; b=GJ8bqmknY/UJ4l3q1D14EE71VveiXuWKX39XnZ8HLsZvKjPhC/ufJUMZ7XPlltzsA1dazA6aXDbjuzWikBt1vFZ1Fwcdk9TZURoeAgRQXhX4C7SQNgt3A8w605Lt+TLP/IRBkVfArz0OjKLRVS1Sa84mwYwbnOy+tMCS+yUd3BM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714473821; c=relaxed/simple; bh=scdYD6Pl04lfvb+n/4ZxgZp2ul4X56LAd6/2RPvnbKw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=k7T/9za3pjnJKbvJ9MJ4xukgiTkhfR+KEZ5PxT4TWOuVH8TzCGfqt3aZusAOu624+amu/UICvXW+B0HldUWEyes9RTMxo0OXfsVOoUcIhwZDIR/2ho8Nfy9TsVn6wGcJavFzWqD9j0HgduIOd1hxKZOE9vpAQvQXuQShyIdbGiY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Azz8RW30; 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="Azz8RW30" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63B11C4AF4D; Tue, 30 Apr 2024 10:43:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714473820; bh=scdYD6Pl04lfvb+n/4ZxgZp2ul4X56LAd6/2RPvnbKw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Azz8RW30b5RWa3U2ynB4523004XmMEyDSEzgd+zgLwJr2HbmgqfSwQUwBPg1XdL0o 6A8JtOO1jbz0AP5pXB38y63bX7Lakr1X/yj3k6KD5uFBy+rbcGfAtBNh+5DvliZ3Xi BMLtPjmj1Sqg+Xt5VXDy/jUMzQXjHRUYSKLf8ommbs+fnMGUfR08D3io1RqfCNsrSv cxrhj0T8C7XKlLPLKR2p41RcZEE32d1ot/eSdy0EOYsMwmHM662fp6TZ/vG81sGSbG sEXZ+YjnjXsMgyRppkGhJq797bOrJPYBhMFDwVzPUlV7Zo9XGO+iA5Fg9TKDtXaQ53 7Feph65qAWl4g== From: Benjamin Tissoires Date: Tue, 30 Apr 2024 12:43:25 +0200 Subject: [PATCH bpf-next v3 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-v3-2-27afe7f3b17c@kernel.org> References: <20240430-bpf-next-v3-0-27afe7f3b17c@kernel.org> In-Reply-To: <20240430-bpf-next-v3-0-27afe7f3b17c@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=1714473809; l=4038; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=scdYD6Pl04lfvb+n/4ZxgZp2ul4X56LAd6/2RPvnbKw=; b=EZ24aspZONQX1VcEEk8ik4tH9E5dhc0Fa79o6LjWzDOVfZiVlcvCNZb9tfgjqeJf3I2/HOyWD y9vk62TupKiBFG72Hnhwt6C74ro4kR3Reo2PKBvtCg9IDvZ3RKAivbU 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 Acked-by: Kumar Kartikeya Dwivedi --- changes in v3: - fix unloading of bpf_wq, again 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..06115f8728e8 100644 --- a/kernel/bpf/hashtab.c +++ b/kernel/bpf/hashtab.c @@ -221,13 +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(); @@ -235,27 +233,12 @@ static void htab_free_prealloced_timers(struct bpf_htab *htab) 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(); - - for (i = 0; i < num_entries; i++) { - 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)); + if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE)) + 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 10:43:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 793902 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 74A0012D74D; Tue, 30 Apr 2024 10:43:44 +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=1714473824; cv=none; b=RwqURF5cor/wp9I0yByxy4Jj9vTsmF7iROKWihe5e5HwwjYqY8sy/adaskCoYlNG6tvTk4b7cZ+cVqI88DBevz5YcNtl27mPSUOr5/XlLMxP9RMGBUX6opfO90rYj2gW7+MZciSIdEHLn5AybwkbCV8TeOlwPwUVweTu+NloKOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714473824; c=relaxed/simple; bh=4M62uKbJjJpbp48Z33RrSvgDZmn03G5iR1xlxAVnPMY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Ni6HkmncsxAbBDBiQA0d2leUGKJlJg1Epn9UYoENPAkC5Skb1bgzIfbWaNblPmgcKKxr1STZipg7ULdShkixqB8SFrkEdJxsDMp7YqYi+P/R01F9L4lYstFr5mBtiiFNJqL64VfNBjDot8hXxVa+PnHeNssr3oJu0Sk3VhL5b38= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ifDCvrpN; 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="ifDCvrpN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0CF36C4AF1C; Tue, 30 Apr 2024 10:43:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714473824; bh=4M62uKbJjJpbp48Z33RrSvgDZmn03G5iR1xlxAVnPMY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ifDCvrpNhZFSHfxWm/v7XMNPAeGlZ9T0gqfQOn4+TYYFlhmFbBFiEzbQy6nZvO5fe Plamzt3vHEMOsEa0r2VNpNntbo2UD9c4+hGXQr6XTGYIVQO9NGNvCE9qdYCTC9/zUA /Mo/YhQ9YW1ITXJO/28abiE67diVegaHqwMQ6/pxU5RKz4uzuT5ywwASvLKmOIc6bK PRFhPzROKzvUFT5YzpP9PPBAjKCcIN29mQXuFmrmRDPIXlDeVFTFydbPvHWgtOqEe5 DmXlbRZZ7nX0kJrerKXcLhzeH71CGR30Hq48cUxWuITTtDQEaQsNAbapaRv0mMuzMC 30NCnSuycGLAw== From: Benjamin Tissoires Date: Tue, 30 Apr 2024 12:43:26 +0200 Subject: [PATCH bpf-next v3 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-v3-3-27afe7f3b17c@kernel.org> References: <20240430-bpf-next-v3-0-27afe7f3b17c@kernel.org> In-Reply-To: <20240430-bpf-next-v3-0-27afe7f3b17c@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=1714473809; l=740; i=bentiss@kernel.org; s=20230215; h=from:subject:message-id; bh=4M62uKbJjJpbp48Z33RrSvgDZmn03G5iR1xlxAVnPMY=; b=qxKGCgVmXoRpWZXc6rzS3G33goRV8NDVHOEjRqixiJUHAaLMZAkWomh6jDSmRaC8/wUWFHkxc ZldfzVkRELfCHB+OnO71VChr0jYnPLxYMKJc1SP+H0U63KkhD03azMq 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 v3 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); }