Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1895925lqa; Tue, 30 Apr 2024 02:26:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWX9YN3VWKxtI71StGuUyc7tHRL3+n82UMpqbY9z8hCAUKdIX9tciwjvpfRwI41tOmJPtPes/NW7vFCQwmjvvMWALWL3GKbQVKaAG7Vww== X-Google-Smtp-Source: AGHT+IGrh+BqNGkYjHpo4At4t8t8+bD4mDY9mnik8GVePl4UIZOdqsCFpjp1p0SM00M/0dLKnEWZ X-Received: by 2002:a05:6214:2509:b0:6a0:dc65:1709 with SMTP id gf9-20020a056214250900b006a0dc651709mr3700804qvb.13.1714469200688; Tue, 30 Apr 2024 02:26:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714469200; cv=pass; d=google.com; s=arc-20160816; b=STGe2XxxKq0ZODvrW2boQX5X+OMG3uE5AFicDCm3XYhx8qLLbzrz0gBBvTGqGVbOSJ 20beTLf6UFsPx8xE3BCgPotu5Y7te8jrAmQ65hJWxRR6MvkT3PGI67/ua1Nk/EB/yHrj 10bgwGMdeYiVL3xOJoVtuWgEInTAluLytBQv38GR+AEBxTabWOR4OHS7XgZxy7qHAyTr lPE+1qMX32yLTXkdUrlMm2Yt1AOkw8QHXzynr7PuXMC+0j+/Q0JPvgTO2vQu1H2RJv0W 2GK4e4vw+o7oHpPIm5ypNCtPDyZ4YgSPgatnBqRIGIcC6jPf4RJIK2rtfbf5hdpH4tt6 Kbxw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=n1w2D2tU6d+Pr+SBO+lvsFSmV4fP2W1oDN8l5+4ppLc=; fh=q4UXWtcyd1MFDR47RrPgEwGOSJCQezDRAV0OMmsdobo=; b=Ahwp8DurM7Ibb5AojvMKN+OV3uxvNAsDo85bNOgVw7OcX/9mThKFA0Ci0lWead/SFo mdJ3gYmAsMH/efTKBZlOfNNZO8kTKnM0F8azgbxNw1/rBL6IWBZohlgjf1dF4XCcgMgE 0SoEsOBjWDrc3kO+eNBXIybJEjNnNSVTxFs7WThbAuALTZTapRc5gJ5OxUMKA835+4y/ i/ztoX6U9Q38EA1NIowzv4kAwTGFNfHu/Eg/kBomShxPN2hxL88C3NHCmJK2PD9Jr3us dvn7+6io+absIhl7ZHwUJkywJqq0qZ+3JWzftzReF2UgvqM6qJ0XlN2PApDBQD0arPvI WxHQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GA2HWGh+; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163675-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163675-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 1-20020a05621420e100b0069b4efe07e2si28191621qvk.406.2024.04.30.02.26.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 02:26:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163675-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GA2HWGh+; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-163675-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163675-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 6AEE31C229B9 for ; Tue, 30 Apr 2024 09:26:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 59B18194C8A; Tue, 30 Apr 2024 09:20:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GA2HWGh+" 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 754AC12839B; Tue, 30 Apr 2024 09:20: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=1714468826; cv=none; b=oO9pi3PDBn7Dnir+/0mlZg0XQerbetXbW8yyr+5v6jjkItNETRGbCSnDqYpPg/4Vy9wd4XbcGqzujyr9H3zVHwBwUTTpJbnnMHQASgyE2wEeqza0OysHfS2e1QR91JILv0MhkbWul4k0nNEOlSaRG0AVRBfNN28DqFY9A9Y45Q8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714468826; c=relaxed/simple; bh=Xbu2y6CEnMuOqaTTlEJrCaL9AxiPE5mbU/3qPzomh4w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=PJtr8JTyFx5MxuRfIXzISBJjPfxjwXuWhyL9feEl5aqq7W8HQLmzapyk2eSVUDtTZvLHkAHASvqrNR0EP4OoXmdXbVuxsUxO8qqfkH7L9m6HbBQodDNnTqZGWdeFnEyo3UXSm4J2XyE70NM5MmsKvGEQbryf4gIHK9nBogvDrVg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GA2HWGh+; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5C2EBC2BBFC; Tue, 30 Apr 2024 09:20:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714468825; bh=Xbu2y6CEnMuOqaTTlEJrCaL9AxiPE5mbU/3qPzomh4w=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=GA2HWGh+kXj9f0fz7r0sqxRweuRry/BPPh9hJXNUcZ7XWT4svfHvN0eTdsDl/6xce nBhUMHvPX2geNdXPclSt5Eicmy7MO2LNua3z+yPv+K9udAIli7CQK3ECWy4jZHYuLl p2AlHBDf/QGAn1JYh/yrK/q1YUXecScaX563AB64zeiW5nWj14KUwLWTY2w63KssbZ Sef5paV+hFTLQAUV/QrYUtei6/WqPO/0VhaX/vU3AdJ8xkEVUejFFqbe0QpLeT8197 KWCTaRVVkVp69jgEz4088N9i+2PZSU+2l8FpRO1cAQgOM2DEW2oUePaiAa8D1PWQ8j dTG2ZVWtShOQQ== Date: Tue, 30 Apr 2024 11:20:19 +0200 From: Benjamin Tissoires To: Alexei Starovoitov Cc: 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 , bpf , LKML , "open list:KERNEL SELFTEST FRAMEWORK" Subject: Re: [PATCH 2/3] bpf: do not walk twice the hash map on free Message-ID: References: <20240425-bpf-next-v1-0-1d8330e6c643@kernel.org> <20240425-bpf-next-v1-2-1d8330e6c643@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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Apr 25 2024, Alexei Starovoitov wrote: > On Thu, Apr 25, 2024 at 6:59 AM 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 > > --- > > 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); > > This looks wrong. > htab_free_prealloced_wq() is now unused as compiler says: > ../kernel/bpf/hashtab.c:243:13: warning: ‘htab_free_prealloced_wq’ > defined but not used [-Wunused-function] > 243 | static void htab_free_prealloced_wq(struct bpf_htab *htab) > | ^~~~~~~~~~~~~~~~~~~~~~~ > > and prealloced maps with wq leak wq-s. oops, you are right. Sending a v2 right away (sorry for the delay). Cheers, Benjamin