Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1901016lqa; Tue, 30 Apr 2024 02:39:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXXqFQkjG5AHflI1JcTwFpPvO3UxkIXBxBtuhrHP1a1zjJbfctB6y5i9N41h9dLx4Gx959ViimEVVcLi1br4GiqL4Flw7vvCS3dMHsiNg== X-Google-Smtp-Source: AGHT+IErQEhQJ45EfSMHEdhDN0beIt3CmfDQp+TYeA6H5RUzMyZtsqPeifSUBReWijxb7XrUDfze X-Received: by 2002:a50:9e06:0:b0:56d:faa2:7aca with SMTP id z6-20020a509e06000000b0056dfaa27acamr9771685ede.17.1714469988954; Tue, 30 Apr 2024 02:39:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714469988; cv=pass; d=google.com; s=arc-20160816; b=0e8nnf4mPlsnePm7t7tB54Jz7xVozaXInAIw7tZX0HyS74Fe1L03UPigEYLn0fPzOy Sn1pNt7h4KGR8sbVRV+zNyBYCN65Ypg4/kENsQp5bB6z9rsi34CPJE13v5njmr57ZzTz AyZwWGbX51A24pJ3BVFP6FnsNH4CbPxnc5nRluJVzAYsw0R9nIsPEaQZs5NsX6lLkzju WrNAZdckbVYQFlaASjKHeNPnODgCPW8uWcEWZEwab9XBoc2LXBEzKltz7vxvZHu+ZMoz LWGDvoAgILtbjvt0pcXPTJxKBU5S9JT66Yz01Zpf5Twv2Jq9XHCGVGtGnoriTcX5mJeX QwXQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=9jS7AZM+glSFboH+GwtOXBqWRFg3tXnwfACC88R/YcU=; fh=Cxwo/A+C89tjOzVq8R/o/DH+l+P6Lpt8VwbqwppZI0Y=; b=uY1k8wL3ZJJrtvPl7veBhVGI9YPghq9Moa3q5soUgdCjGZEkmXdJGe+TPwNaijczEG PvjNpTQbpsbXgTU/YNpU0SnhDR+wl8G7lgifibTBvdfw+IaKYNkJe9IbtczmldemBeG7 MKD2ak5jNL4EvTIMgKsSmgw9j1rr6kXDbVHBnSXniGGFWAR8Ej80SZONrLISlRd6lbj0 H5I5M5DzRnrb8QCe09jrr3HMTnGY1D/R+qnL+yl+NM5sXox+Pc0ykWYbqR5YzgTKw+PV U9+v00NbzD+jVHB9pU57WfN60LpCE4+sk02us8yFQL8e/vPHP6JrD1Ji/eTdUT/bGnZr dzIQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kFoabQAu; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163710-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163710-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id cb25-20020a0564020b7900b005727c0643desi2978671edb.476.2024.04.30.02.39.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 02:39:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163710-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kFoabQAu; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163710-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163710-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E86291F250DA for ; Tue, 30 Apr 2024 09:38:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9FC0E1A0AF2; Tue, 30 Apr 2024 09:36:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kFoabQAu" 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 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-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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); } } -- 2.44.0