Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1927434lqa; Tue, 30 Apr 2024 03:39:54 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVoddun7V2JvGNIbxC9HG2BxySdokez954fDHrYLd8Ia9NjfE9UjeB2RxHRZvDq+kFe3bw0gwPBJsUTstDv2io+vO0sM/O2kfkxwqcrYQ== X-Google-Smtp-Source: AGHT+IG7ZLNb9GFZKfVmr1GGoDd+d5XtlhX2yqbB2hU4L5Qjl2w33Bqw14im5DX8AHq4WlueneKS X-Received: by 2002:a17:902:bb8b:b0:1ea:f9af:ee99 with SMTP id m11-20020a170902bb8b00b001eaf9afee99mr12808598pls.25.1714473594225; Tue, 30 Apr 2024 03:39:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714473594; cv=pass; d=google.com; s=arc-20160816; b=JCr+n5UN/W5Y90bENvL4lpCpe2LHPmoONNpsQ0ILxxCttlhE+FWbhgVURqyHxRL7As whRbBHhuO/ZoAViYaQ+MrGI3KYvFYznuBusHEnDZ/d4qet2GEuczF+BFNKofYkIyDSEd 8hTh0tXrKdWNeGt6HNN2QeHj2vuDvUQPaWFMSb3SUNyAoQPsoCC5IKa8Q/7SrsEV0781 LToMYDCt2CB96QrQatexTG7OZFBx+HgG9m+vS9HldWEAOT0RVaf/FBUb11K+tM3aTD8c zBiyG33qDlvdLsY+71Fz12cs1Xv8ypzjlcLFfemWvZMlHeIs5v8yl3o6lmvFFm8GLfvy v2LA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=+/RJlvSjzbYYRYm1t5LmDf2DIJY0P2eZttypJWf2xxw=; fh=yrMtFGj89yemYuvai7L4i1gGU56yqkZdjF0dciwlIt0=; b=xYdvKDJ///117jMwVXL70rZQzJqSaBmNMpjqQxGBdc5ckcIKLcv4xyFpU3DbFfm4iH 70sg6ccHq78PsIwpVD34A7QLUehva92hai61bpf6c4Prsfja62oUCVQ2PzFo1j010F3+ KMdVV9JFgmLIRwa9RDAroWmGrvK+TWBg9FcspsixubfN4iEzLuHYsvN17lj/eZ1TGTcA 3xfOhzSCV88/IlI9eWZl8VptH7d/fvjUDBHYhy7Er/DUyJdnN5It4KjQRDpoIk6iJptq 2zwI/OpJ73iN+sqx0Q772Jx7gAmRejLodsRN8agw+Hv1mQ+C3lDvgtDFp9/RO7yH+xdm WEgw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GrKPeI6b; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163778-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163778-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id y9-20020a17090264c900b001ec3f2e9728si1016861pli.57.2024.04.30.03.39.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 03:39:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163778-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GrKPeI6b; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163778-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163778-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 97DCE28504C for ; Tue, 30 Apr 2024 10:39:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E531F12C473; Tue, 30 Apr 2024 10:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GrKPeI6b" 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 052712CA7; Tue, 30 Apr 2024 10:39: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=1714473585; cv=none; b=E8P95tnYEa0awntrGD0Q+hmLqs0N3GKKPHsCogqpnYFPsgpWRFdIq4Zvld5G+7loa/2XKlN1Nsm/oIqW+QEFEyg5TBwXMdHo8S2XuqD/sRcJOZ6Pa00i9hra/nszpDxZbBySg0ul7fypYC6ZeNpt/QuORZMwZMeTif2KHAJRuT0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714473585; c=relaxed/simple; bh=sNg/sHNAD7BL/VjF8rOvfs48XQbDjX30Eb+2bNQnFuw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rflsVxuJ8TG2W7RYwGJMkTzP3BYd38P+c1B433DwidX+CRY7QitiMUK2MATcw7QIOkMpvZOVanwPz3pV63NPEOEGvHYlOCNFJeN/rOjwBpPnDVUqzJwWRWm8gxKkIEpcrOYLqW0ccj1TE0xA6vdRS3eHJuQLRCNcbroZCj5+Ut8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GrKPeI6b; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2F9DDC2BBFC; Tue, 30 Apr 2024 10:39:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714473584; bh=sNg/sHNAD7BL/VjF8rOvfs48XQbDjX30Eb+2bNQnFuw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=GrKPeI6b3To+3rhSxGeKzj5cslKwbXB0GZbb9ugcLQLe3E3mosEC6t9NDEiAqbdc/ qJJP/e/4uPB5wISoMmlHKkG3G49/6a9XdBnh748aUmcJQEIu+PWA/OshtVowjWhTXg k10mzOxYUngyTpkCMBqpvdzIGEaXx7N5GMOpPyyvOdCGrIIv4yWQBKmFAYh/T9n+oJ iYHLE2S51PtXBsUgJGzXp0od6uR9u+C/5j+QE1al+zDlLdA5gA0pR9lKIDlaPWxbkf bsQTavu+HyeRMqIhBIuXCMSnNTUB6tcqHaEJ06j5tmVrVwEcO6KNhMJ+eeOymprUNl BImRJowlPYbLQ== Date: Tue, 30 Apr 2024 12:39:38 +0200 From: Benjamin Tissoires 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 Subject: Re: [PATCH v2 2/3] bpf: do not walk twice the hash map on free Message-ID: References: <20240430-bpf-next-v2-0-140aa50f0f19@kernel.org> <20240430-bpf-next-v2-2-140aa50f0f19@kernel.org> 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=us-ascii Content-Disposition: inline In-Reply-To: <20240430-bpf-next-v2-2-140aa50f0f19@kernel.org> On Apr 30 2024, Benjamin Tissoires wrote: > 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 Sorry, this else above is wrong, it should be a check on BPF_WORKQUEUE instead. v3 is n its way (with the proper bpf-next suffix this time). Cheers, Benjamin > + 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 >